// ================================
// RTC test
//		(c) 2022	1YEN Toru
//
//
//		2022/10/08	ver.1.00
//
// ================================
asm		"mcoc_irom.mif"
#asm		"mcoc_irom.mem"
#asm		"mcoc_irom.dat2",bit16,list
incl	"mcoc115.incl"
# ================================
# string macros
def		prog_name,"test_rtc"
def		prog_vers,"1.00"
def		s,"#"							// simulation="" / real fpga="#"
# constants
equ		max_lbuf,64						// size of lbuf
equ		baud,9600						// baud rate
equ		uart_baud,fcpu/baud-1			// uartbaud setting
equ		chr_lf,0x0a						// lf code
equ		chr_cr,0x0d						// cr code
equ		chr_spc,0x20					// space ' ' code
equ		chr_cma,0x2c					// comma ',' code
equ		chr_mns,0x2d					// minus '-' code
# variables
equ		va,ramtop
equ		mset_yr,va						// year
equ		va,va+2
equ		mset_mn,va						// month
equ		va,va+2
equ		mset_dy,va						// day
equ		va,va+2
equ		mset_hr,va						// hour
equ		va,va+2
equ		mset_mi,va						// minute
equ		va,va+2
equ		mset_sc,va						// second
equ		va,va+2
equ		static_va,va
# array variables
equ		lbuf,va							// receive data buffer
equ		va,va+max_lbuf
equ		array_va,va
# ================================


// ================================
// setup
// ================================

// set sp to the bottom of ram area
ldwi	r7,idrgramt						// idrg: ram top addr.
ldw		r7,[r7]
ldwi	r6,idrgrams						// idrg: ram size
ldw		r6,[r6]
add		r7,r6
movtc	sp,r7

// clear variables
ldwi	r7,ramtop
ldwi	r1,static_va
ldbiu	r0,0
st_clrvar:
stw		[r7],r0
addi	r7,2
cmp		r7,r1
blo		st_clrvar

// 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

// rtc setting: internal clock source select
ldwi	r7,rtcctl
ldw		r0,[r7]
andi	r0,rtc_esel
beq		pcnt+4
ldbiu	r0,rtc_rtcw
stw		[r7],r0

// send RDY
ldwi	r1,lab_str_msg_rdy
jalwr7	print

$(s)// ********************************
$(s)// for simulation
#0$(s)// cancel time out
#0$(s)ldwi	r7,simctrl
#0$(s)ldbiu	r0,simc_noto
#0$(s)stw		[r7],r0
$(s)// command test
$(s)ldwi	r6,lab_sim_str_cmnd
$(s)sim_cmnd_loop2:
$(s)ldwi	r7,lbuf
$(s)sim_cmnd_loop:
$(s)ldb		r0,[r6]
$(s)addi	r6,1
$(s)stb		[r7],r0
$(s)addi	r7,1
$(s)mov		r0,r0
$(s)bne		sim_cmnd_loop
$(s)pushw	r6
$(s)jalwr7	sim_cmnd
$(s)// skip nil, check end mark (*)
$(s)popw	r6
$(s)ldb		r0,[r6]
$(s)addi	r6,1
$(s)cmpi	r0,0
$(s)beq		pcnt-8
$(s)subi	r6,1
$(s)cmpi	r0,0x2a						// asterisk(*)
$(s)bne		sim_cmnd_loop2
$(s)bra		sim_cmnd_end
$(s)sim_str_cmnd:
$(s)dats	"mset,2022,09,06,21,43,50\0"
$(s)dats	"show\0"
$(s)dats	"*"							// end mark
$(s)dats	"who\0"
$(s)dats	"help\0"
$(s)sim_cmnd_end:
$(s)ldbiu	r0,0
$(s)// simulation finish
$(s)mov		r0,r0
$(s)beq		pcnt+6
$(s)ldwi	r0,simfail
$(s)bra		pcnt+4
$(s)ldwi	r0,simpass
$(s)ldwi	r7,simfnsh
$(s)stw		[r7],r0
$(s)bra		pcnt-2
$(s)// ********************************


// ================================
// loop
// ================================
loop:

// ================================
// serial monitor handling
// check uartctl.RAVL
ldwi	r7,uartctl
ldw		r0,[r7]
andi	r0,uart_ravl
beq		no_data
jalwr7	get_line
$(s)// ********************************
$(s)sim_cmnd:
$(s)// ********************************

// check "mset," command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_mset
ldbiu	r5,5
jalwr7	strncmp
mov		r0,r0
bne		mset_notid
// ================================
// mset
ldwi	r1,lbuf+5
// <yyyy>
jalwr7	xtoi
ldwi	r7,mset_yr
stw		[r7],r0
// ,
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <mm>
addi	r1,1
jalwr7	xtoi
ldwi	r7,mset_mn
stw		[r7],r0
// ,
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <dd>
addi	r1,1
jalwr7	xtoi
ldwi	r7,mset_dy
stw		[r7],r0
// ,
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <HH>
addi	r1,1
jalwr7	xtoi
ldwi	r7,mset_hr
stw		[r7],r0
// ,
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <MM>
addi	r1,1
jalwr7	xtoi
ldwi	r7,mset_mi
stw		[r7],r0
// ,
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <SS>
addi	r1,1
jalwr7	xtoi
ldwi	r7,mset_sc
stw		[r7],r0
// set rtc counter
// SNAP=1
ldwi	r7,rtcctl
ldbiu	r0,rtc_snap
stw		[r7],r0
// rtcyear
ldwi	r0,mset_yr
ldw		r0,[r0]
ldwi	r7,rtcyear
stw		[r7],r0
// rtcmody
ldwi	r0,mset_mn
ldw		r0,[r0]
ldwi	r1,mset_dy
ldw		r1,[r1]
lsfti	r0,8
or		r0,r1
ldwi	r7,rtcmody
stw		[r7],r0
// rtchrmi
ldwi	r0,mset_hr
ldw		r0,[r0]
ldwi	r1,mset_mi
ldw		r1,[r1]
lsfti	r0,8
or		r0,r1
ldwi	r7,rtchrmi
stw		[r7],r0
// rtcscps
ldwi	r0,mset_sc
ldw		r0,[r0]
lsfti	r0,8
ldwi	r7,rtcscps
stw		[r7],r0
// rtcweek (dummy write)
ldbiu	r0,0
ldwi	r7,rtcweek
stw		[r7],r0
// rtcw=1, snap=1
ldwi	r7,rtcctl
ldbiu	r0,rtc_rtcw|rtc_snap
stw		[r7],r0
// polling rtcw=0
ldw		r0,[r7]
andi	r0,rtc_rtcw
bne		pcnt-6
// response
ldwi	r1,lab_str_msg_mset
jalwr7	print
bra		no_data
mset_notid:

// check "show" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_show
ldbiu	r5,4
jalwr7	strncmp
mov		r0,r0
bne		show_notid
// ================================
// show
// snap=1
ldwi	r7,rtcctl
ldbiu	r0,rtc_snap
stw		[r7],r0
// response
ldwi	r1,lab_str_msg_show
jalwr7	print
pushw	r1
// year
ldwi	r7,rtcyear
ldw		r1,[r7]
jalwr7	print_hex
popw	r1
jalwr7	print
pushw	r1
// month
ldwi	r7,rtcmody
ldw		r1,[r7]
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
pushw	r1
// day
ldwi	r7,rtcmody
ldw		r1,[r7]
lsfti	r1,8
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
pushw	r1
// hour
ldwi	r7,rtchrmi
ldw		r1,[r7]
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
pushw	r1
// minute
ldwi	r7,rtchrmi
ldw		r1,[r7]
lsfti	r1,8
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
pushw	r1
// second
ldwi	r7,rtcscps
ldw		r1,[r7]
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
pushw	r1
// prescaler
ldwi	r7,rtcscps
ldw		r1,[r7]
lsfti	r1,8
ldbiu	r3,2
jalwr7	print_hex+2
popw	r1
jalwr7	print
// snap=0
ldwi	r7,rtcctl
ldbiu	r0,0
stw		[r7],r0
bra		no_data
show_notid:

// check "who" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_who
jalwr7	strcmp
mov		r0,r0
bne		who_notid
// ================================
// who
// response
ldwi	r1,lab_str_msg_who
jalwr7	print
bra		no_data
who_notid:

// check "help" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_help
jalwr7	strcmp
mov		r0,r0
bne		help_notid
// ================================
// help
// response
ldwi	r1,lab_str_msg_help
jalwr7	print_lst
bra		no_data
help_notid:

// ================================
// ignore empty line
ldwi	r1,lbuf
ldb		r0,[r1]
mov		r0,r0
beq		no_data
// ================================
// ERROR: unsupported command
ldwi	r1,lab_str_err_unspt
jalwr7	print
pushw	r1
ldwi	r1,lbuf
jalwr7	print
popw	r1
jalwr7	print
bra		no_data

// ERROR: syntax
err_syntx:
ldwi	r1,lab_str_err_syntx
jalwr7	print
bra		no_data

no_data:
// end of command interpretation


// ================================
// end of loop
$(s)// ********************************
$(s)rtnw
$(s)// ********************************
bra		loop


// ================================
// get line from uart ; disturbed r0,r5~r7
// ================================
get_line:
ldwi	r7,uartrdat
ldwi	r6,uartctl
ldwi	r5,lbuf
gl_loop:
// polling until RAVL=1
ldw		r0,[r6]
andi	r0,uart_ravl
beq		gl_loop
// get byte
ldw		r0,[r7]
stb		[r5],r0
addi	r5,1
// LF code check
cmpi	r0,chr_lf
beq		gl_got
// size check
ldwi	r0,lbuf+max_lbuf-1
cmp		r5,r0
blo		gl_loop
// got line
gl_got:
// string terminate
ldbiu	r0,0
subi	r5,1
stb		[r5],r0
// check CR code
subi	r5,1
ldb		r0,[r5]
cmpi	r0,chr_cr
bne		gl_notcr
ldbiu	r0,0
stb		[r5],r0
gl_notcr:
rtnw

// ================================
// 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
mov		r2,r2
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]
mov		r0,r0
bne		print_lst
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,2
$(s)bne		pcnt+6
$(s)lsfti	r1,-8
$(s)stb		[r7],r1
$(s)bra		pcnt+8
$(s)cmpi	r3,3
$(s)bne		pcnt+2
$(s)lsfti	r1,-4
$(s)stw		[r7],r1
$(s)rtnw
$(s)// ********************************
ldwi	r7,uarttdat
ldwi	r6,uartctl
ldwi	r5,lab_ph_hexdig
ph_loop:
// print nibble
mov		r0,r1
mov		r4,r5
lsfti	r0,-12
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
lsfti	r1,4
subi	r3,1
bne		ph_loop
rtnw

ph_hexdig:
dats	"0123456789abcdef"

// ================================
// string compare: r0=strcmp (r1,r2) ; disturbed r1~r4
// ================================
strcmp:
ldb		r3,[r1]
addi	r1,1
ldb		r4,[r2]
addi	r2,1
mov		r0,r3
sub		r0,r4
bne		sc_finish
// check nil code
mov		r3,r3
beq		sc_finish
mov		r4,r4
bne		strcmp
sc_finish:
rtnw

// ================================
// string n compare: r0=strncmp (r1,r2,r5) ; disturbed r1~r5
// ================================
strncmp:
ldb		r3,[r1]
addi	r1,1
ldb		r4,[r2]
addi	r2,1
mov		r0,r3
sub		r0,r4
bne		snc_finish
// check n character
subi	r5,1
beq		snc_finish
// check nil code
mov		r3,r3
beq		snc_finish
mov		r4,r4
bne		strncmp
snc_finish:
rtnw

// ================================
// hexadecimal to integer: r0=xtoi (r1) ; disturbed r1,r2
// ================================
xtoi:
ldbiu	r0,0
// r2=[r1]-'0'
ldb		r2,[r1]
subi	r2,0a0
blo		x2i_finish						// [r1]<'0'
cmpi	r2,10
blo		x2i_next						// [r1]<='9'
subi	r2,(0aA-0x20)-0a0-10
blo		x2i_finish						// [r1]<'A'
cmpi	r2,16
blo		x2i_next						// [r1]<='F'
subi	r2,0aa-(0aA-0x20)
blo		x2i_finish						// [r1]<'a'
cmpi	r2,16
bhs		x2i_finish						// [r1]>'f'
x2i_next:
// r0=16*r0+r2
lsfti	r0,4
add		r0,r2
// loop
addi	r1,1
bra		xtoi+2
x2i_finish:
rtnw


// ================================
// c string data
// ================================
str_msg_who:
dats	"OK: $(prog_name) ver.$(prog_vers)\r\n\0"
str_msg_rdy:
dats	"RDY\r\n\0"
str_msg_help:
dats	"OK+ mset,<yyyy>,<mm>,<dd>,<HH>,<MM>,<SS>\r\n"
dats	"OK+ show\r\n"
dats	"OK+ who\r\n"
dats	"OK: help\r\n\0\0"
str_msg_mset:
dats	"OK: mset\r\n\0"
str_msg_show:
dats	"OK: \0/\0/\0 \0:\0:\0 .0x\0/0x100\r\n\0"
str_err_unspt:
dats	"ERR: unsupported command: \0\r\n\0"
str_err_syntx:
dats	"ERR: syntax\r\n\0"
str_cmd_mset:
dats	"mset,\0"
str_cmd_show:
dats	"show\0"
str_cmd_who:
dats	"who\0"
str_cmd_help:
dats	"help\0"

