// ================================
//	MCOC115 Combinational Test
//		ROM: instruction ROM (48KB) test
//			need: `define MCOC_ROM_48K
//		(c) 2024	1YEN Toru
// ================================
asm		"mcoc_irom.mem"
incl	"mcoc115.incl"
def		rom_kb,"48"						// ROM size [KB]
romsiz	$(rom_kb)*1024
# ================================
# constants
equ		rom_size,$(rom_kb)*1024
# ================================


// initialize
ldc		sp,iome_botm

// read out IDREG unit
ldwi	r6,prinhex
ldwi	r7,idrgcode
rept	8
ldw		r0,[r7]
addi	r7,2
stw		[r6],r0
endr

// check I-RAM existence
ldwi	r0,0xffff
ldwi	r7,rom_size
stw		[r7],r0
ldw		r1,[r7]
cmp		r0,r1
beq		iram_endif
// INFORMATION:
ldwi	r1,lab_iram_info
jalwr7	sim_print_top
bra		iram_endif

iram_info:
dats	"INF: no I-RAM found.\n\0"
iram_endif:

// print message
ldwi	r1,lab_rom_dat1
jalwr7	sim_print_top
ldwi	r1,lab_rom_dat2
jalwr7	sim_print_top

// jump to bottom of ROM area
jmpr7	rom_bottom

// data #1 top of instruction ROM
rom_dat1:
dats	"message #1 at top of ROM\n\0"


// ================================
// print subroutine for simulation on top
sim_print_top:
ldwi	r7,princhr
sprt_loop:
ldb		r0,[r1]
addi	r1,1
cmpi	r0,0
beq		sprt_break
stb		[r7],r0
bra		sprt_loop
sprt_break:
rtnw


// ================================
// bottom of instruction ROM
org		rom_size-512
rom_bottom:

// print message
ldwi	r1,lab_rom_dat2
jalwr7	sim_print_btm
ldwi	r1,lab_rom_dat1
jalwr7	sim_print_btm

// test pass
jmpr7	tstpass


// data #2 bottom of instruction ROM
rom_dat2:
dats	"message #2 at bottom of ROM\n\0"


// ================================
// print subroutine for simulation on bottom
sim_print_btm:
ldwi	r7,princhr
ldbiu	r0,0aB-0x20
stb		[r7],r0
sprb_loop:
ldb		r0,[r1]
addi	r1,1
cmpi	r0,0
beq		sprb_break
stb		[r7],r0
bra		sprb_loop
sprb_break:
rtnw


// ================================
tstpass:
ldwi	r0,simpass						// pass: LED=blue
bra		tstfnsh
tstfail:
ldwi	r0,simfail						// fail: LED=red
tstfnsh:
ldwi	r7,simfnsh
stw		[r7],r0
bra		pcnt-2
nop
// ================================

