// ================================
//	MCOC115 Combinational Test
//		trng32: Nh, random number
//		(c) 2025	1YEN Toru
// ================================
asm		"mcoc_irom.mem"
#asm		"mcoc_irom.dat2",bit16,list
incl	"mcoc115.incl"
# ================================
# string macros
def		s,"#"							// simulation="" / fpga="#"
# constants
equ		baud,9600						// baud rate
equ		uart_baud,fcpu/baud-1			// uartbaud setting
# user macros
macro	delay_us_r0
lsli	r0,2
beq		dlus_$(macro)_break
// delay loop: 6 cycle for (fcpu(=24MHz)/1_000_000/6)=4 (=lsl2)
dlus_$(macro)_loop:
subi	r0,1
brn
brn
brn
bne		dlus_$(macro)_loop
dlus_$(macro)_break:
endm
# ================================


// ================================		// general 8 bit Tennessine codes
// uart setting: RXE, TXE, baud rate
ldwi	r7,uartbaud
ldwi	r0,uart_baud
stw		[r7],r0
ldwi	r7,uartctl
ldbiu	r0,uart_rxe|uart_txe
stw		[r7],r0

// check cpu core
ldch	sr,(sreg_nh)>>8
movfc	r0,sr
ldch	sr,0x00
cendw	r0
andi	r0,(sreg_nh)>>8
bne		target_cpu_ok
// ERROR: not target CPU=Nihonium, halt
ldc		sp,iome_botm
ldbiu	r1,asc_mns
jalwr7	print_chr
$(s)// ********************************
$(s)ldwi	r0,simfail
$(s)ldwi	r7,simfnsh
$(s)stw		[r7],r0
$(s)// ********************************
pause
// ================================		// target cpu codes from here
target_cpu_ok:

// stop if cpu2 for dual core cpu edition
movfc	r0,sr
lsri	r0,sreg_b_id0
andi	r0,sreg_id_2>>sreg_b_id0
beq		pcnt+2
pause

// set sp to iomem16 unit
ldc		sp,iome_botm

// 32 bit mode
sesr	sreg_b_nh

// ready
ldwi	r1,lab_msg_rdy
jalwr7	print

// ================================
// test trng32
// ================================
// random value test
ldwi	r1,lab_msg_rand
jalwr7	print
ldwi	r4,1_000
$(s)// ********************************
$(s)ldbiu	r4,8
$(s)// ********************************
tst_loop:

// read trngrand register
ldwi	r7,trngrandh
ldl		r1,[r7]
pushl	r4
jalwr7	print_hex_l
popl	r4
ldwi	r1,lab_msg_crlf
jalwr7	print

// clear trngrand register
ldbiu	r0,0
ldwi	r7,trngrandh
stl		[r7],r0

// wait
ldbiu	r0,100
$(s)// ********************************
$(s)ldbiu	r0,0
$(s)// ********************************
delay_us_r0

// end of loop
subi	r4,1
bne		tst_loop

// ================================
// show counter
ldwi	r7,trng1cnth
ldl		r0,[r7]
pushl	r0
ldwi	r7,trng0cnth
ldl		r1,[r7]
pushl	r1
add		r1,r0
pushl	r1
// trngxcnt
ldwi	r1,lab_msg_xcnt
jalwr7	print
popl	r1
jalwr7	print_hex_l
ldwi	r1,lab_msg_crlf
jalwr7	print
// trng0cnt
ldwi	r1,lab_msg_0cnt
jalwr7	print
popl	r1
jalwr7	print_hex_l
ldwi	r1,lab_msg_crlf
jalwr7	print
// trng1cnt
ldwi	r1,lab_msg_1cnt
jalwr7	print
popl	r1
jalwr7	print_hex_l
ldwi	r1,lab_msg_crlf
jalwr7	print


// ================================
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
// ================================


// ================================
// print r1 ; disturbed r0~r2,r6,r7
// ================================
print:
ldwi	r7,uarttdat
ldwi	r6,uartctl
pr_loop:
// get 1 byte
ldb		r2,[r1]
addi	r1,1
cmpi	r2,0
beq		pr_finish
$(s)// ********************************
$(s)ldwi	r7,princhr
$(s)stw		[r7],r2
$(s)bra		pr_loop
$(s)// ********************************
// polling uartctl.TFUL=0
pr_poll:
ldw		r0,[r6]
andi	r0,uart_tful
bne		pr_poll
// send data
stw		[r7],r2
bra		pr_loop
pr_finish:
rtnw

// ================================
// print_lst r1 ; disturbed r0~r2,r6,r7
// ================================
print_lst:
jalwr7	print
ldb		r0,[r1]
cmpi	r0,0
bne		print_lst
rtnw

// ================================
// print_hexl r1 ; disturbed r0~r7
//		print r1 as 8 digits hexadecimal number
// ================================
print_hex_l:
pushl	r1
lsri	r1,16
jalwr7	print_hex
popl	r1
jalwr7	print_hex
rtnw

// ================================
// print_hex r1 ; disturbed r0~r7
//		print r1 as 4 digits hexadecimal number
// ================================
print_hex:
ldbiu	r3,4
$(s)// ********************************
$(s)ldwi	r7,princtl
$(s)ldbiu	r0,prin_nld
$(s)stw		[r7],r0
$(s)ldwi	r7,prinhex
$(s)cmpi	r3,4
$(s)beq		ph_else
$(s)cendw	r1
$(s)stb		[r7],r1
$(s)bra		ph_endif
$(s)ph_else:
$(s)stw		[r7],r1
$(s)ph_endif:
$(s)rtnw
$(s)// ********************************
ldwi	r7,uarttdat
ldwi	r6,uartctl
ldwi	r5,lab_ph_hexdig
ph_loop:
// print nibble
mov		r0,r1
mov		r4,r5
cendw	r0
lsri	r0,4
andi	r0,0x0f
add		r4,r0
ldb		r2,[r4]
// polling uartctl.TFUL=0
ph_poll:
ldw		r0,[r6]
andi	r0,uart_tful
bne		ph_poll
// send data
stw		[r7],r2
// loop
lsli	r1,4
subi	r3,1
bne		ph_loop
rtnw

ph_hexdig:
dats	"0123456789abcdef"

// ================================
// print_chr r1 ; disturbed r0,r7
// ================================
print_chr:
$(s)// ********************************
$(s)ldwi	r7,princhr
$(s)stw		[r7],r1
$(s)rtnw
$(s)// ********************************
// polling uartctl.TFUL=0
ldwi	r7,uartctl
pc_poll:
ldw		r0,[r7]
andi	r0,uart_tful
bne		pc_poll
// send data
ldwi	r7,uarttdat
stw		[r7],r1
rtnw

// ================================
// c string data
// ================================
msg_rdy:
dats	"RDY:\r\n\0"
msg_crlf:
dats	"\r\n\0"
msg_rand:
dats	"trngrand:\r\n\0"
msg_0cnt:
dats	"trng0cnt:\r\n\0"
msg_1cnt:
dats	"trng1cnt:\r\n\0"
msg_xcnt:
dats	"trng1cnt+0cnt:\r\n\0"

