// ================================
// LED Candle for MCOC115 (Moscovium series MCU)
//		(c) 2021,2023	1YEN Toru
//
//
//		2023/09/30	ver.1.00
//			led candle with hue rotation
//			corresponding to Cmod A7 FPGA board
//			corresponding to Tennessine core
//			need timled5 unit, and mulur instruction
//
// ================================
//		2021/05/01	ver.1.00
//
// ================================
def		target,"moscovium"
#def		target,"tennessine"
asm		"mcoc_irom.mem",$(target)
#asm		"mcoc_irom.dat2",bit16,list,$(target)
#asm		"mcoc_irom.mif",$(target)
incl	"mcoc115.incl"
$(target_mn)incl	"mcvm_remv_mcr_word.incl"
$(target_ts)incl	"tnsn_mcr_word.incl"
# ================================
# string macros
def		prog_name,"led_candle_ca7"
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_cma,0x2c					// comma ',' code
equ		chr_mns,0x2d					// minus '-' code
equ		max_hue2rgb,128					// count of hue2rgb_tab[]
equ		max_rand_1_f,1024				// count of rand_1_f[]
equ		msec_intvl,8*4096/max_rand_1_f	// rand_1_f update interval [ms]
# variables
equ		va,ramtop
equ		msec,va							// pwm: current time [ms]
equ		va,va+2
equ		idx_rand,va						// index of rand_1_f[] array
equ		va,va+2
equ		idx_hue,va						// index of hue2rgb_tab[] array
equ		va,va+2
equ		col_r,va						// color red for rgb-led (0~255)
equ		va,va+2
equ		col_g,va						// color green for rgb-led (0~255)
equ		va,va+2
equ		col_b,va						// color blue for rgb-led (0~255)
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
// ================================

// ================================		// 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 Tennessine core
srbk	sreg_bk_3
movfc	r0,sr
andi	r0,sreg_bk_3
cmpi	r0,sreg_bk_0
srbk	sreg_bk_0
$(target_ts)beq		target_cpu_ok
$(target_mn)bne		target_cpu_ok
// ERROR: not target CPU=$(target), halt
ldc		sp,iome_botm
ldbiu	r1,chr_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
lsrwi	r0,sreg_b_id0
andi	r0,sreg_id_2>>sreg_b_id0
beq		pcnt+2
pause

// 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]
addw	r7,r6
movtc	sp,r7

// check mulur instruction
ldbiu	r0,2
ldbiu	r1,3
mulur	r0,r1
cmpi	r0,2*3
beq		st_has_mult
// ERROR: not have valid mulur instruction
ldwi	r1,lab_str_err_no_mult
jalwr7	print
$(s)// ********************************
$(s)ldwi	r0,simfail
$(s)ldwi	r7,simfnsh
$(s)stw		[r7],r0
$(s)// ********************************
pause
st_has_mult:

// clear variables
ldwi	r7,ramtop
ldwi	r1,static_va
ldbiu	r0,0
st_clrvar:
stw		[r7],r0
addwi	r7,2
cmpw	r7,r1
blo		st_clrvar

// timled5 setting
// tledctr: RGBE=1, LED[GRB]=1
ldwi	r7,tledctl
ldbiu	r0,tled_rgbe|tled_ledg|tled_ledr|tled_ledb
stw		[r7],r0
ldw		r1,[r7]
cmpw	r0,r1
beq		st_has_timled5
// ERROR: not have timled5 unit
ldwi	r1,lab_str_err_no_tled
jalwr7	print
$(s)// ********************************
$(s)ldwi	r0,simfail
$(s)ldwi	r7,simfnsh
$(s)stw		[r7],r0
$(s)// ********************************
pause
st_has_timled5:
// tledintc: LOFE=1, LOFF clear
ldwi	r7,tledintc
ldbiu	r0,tled_lofe|tled_loff
stw		[r7],r0
$(s)// ********************************
$(s)// timled5: tledpsc=0x10
$(s)ldwi	r7,tledpsc
$(s)ldbiu	r0,0x10
$(s)stw		[r7],r0
$(s)// ********************************

// set iv, interrupt enable
ldc		iv,lab_isr_vect
seie

// 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)addwi	r6,1
$(s)stb		[r7],r0
$(s)addwi	r7,1
$(s)cmpi	r0,0
$(s)bne		sim_cmnd_loop
$(s)pushw	r6
$(s)jalwr7	sim_cmnd
$(s)// skip nil, check end mark (*)
$(s)popw	r6
$(s)sim_loop_1:
$(s)ldb		r0,[r6]
$(s)addwi	r6,1
$(s)cmpi	r0,0
$(s)beq		sim_loop_1
$(s)subwi	r6,1
$(s)cmpi	r0,0x2a						// asterisk(*)
$(s)bne		sim_cmnd_loop2
$(s)bra		sim_cmnd_end
$(s)sim_str_cmnd:
$(s)dats	"who\0"
$(s)dats	"help\0"
$(s)dats	"*"							// end mark
$(s)sim_cmnd_end:
$(s)ldbiu	r0,0
$(s)// simulation finish
$(s)cmpi	r0,0
$(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 "who" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_who
jalwr7	strcmp
cmpi	r0,0
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
cmpi	r0,0
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]
cmpi	r0,0
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

// end of command interpretation
no_data:


// ================================
// led candle
cdl_update:
// update rand_1_f
ldwi	r7,idx_rand
ldw		r0,[r7]
ldwi	r7,lab_rand_1_f
add		r7,r0
ldb		r4,[r7]							// r4=rand_1_f[idx_rand]
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r4
$(s)// ********************************

// hue to rgb conversion
ldwi	r7,idx_hue
ldw		r0,[r7]
ldwi	r7,lab_hue2rgb_tab
lslwi	r0,1
addw	r7,r0
ldw		r0,[r7]
// hue to rgb
// red
cendw	r0
mov		r1,r0
cendw	r0
lsli	r1,1
andwi	r1,0x1f<<3
ldwi	r7,col_r
stw		[r7],r1
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r1
$(s)// ********************************
// green
movw	r1,r0
lsrwi	r1,5-3
andwi	r1,0x1f<<3
ldwi	r7,col_g
stw		[r7],r1
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r1
$(s)// ********************************
// blue
mov		r1,r0
lsli	r1,3
andwi	r1,0x1f<<3
ldwi	r7,col_b
stw		[r7],r1
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r1
$(s)// ********************************

// r2=duty_r=col_r*r4/256;
// analogWrite (tled_ledr, duty_r);
ldwi	r7,col_r
ldw		r1,[r7]
mov		r2,r4
mulur	r2,r1
cendw	r2
andwi	r2,0xff
ldwi	r7,tleddtyr
stw		[r7],r2
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r2
$(s)// ********************************

// r2=duty_g=col_g*r4/256;
// analogWrite (tled_ledg, duty_g);
ldwi	r7,col_g
ldw		r1,[r7]
mov		r2,r4
mulur	r2,r1
cendw	r2
andwi	r2,0xff
ldwi	r7,tleddtyg
stw		[r7],r2
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r2
$(s)// ********************************

// r2=duty_b=col_b*r4/256;
// analogWrite (tled_ledb, duty_b);
ldwi	r7,col_b
ldw		r1,[r7]
mov		r2,r4
mulur	r2,r1
cendw	r2
andwi	r2,0xff
ldwi	r7,tleddtyb
stw		[r7],r2
$(s)// ********************************
$(s)ldwi	r7,prindec
$(s)stw		[r7],r2
$(s)// ********************************

// candle: end point
cdl_endpt:

// ================================
// end of loop
$(s)// ********************************
$(s)ldwi	r1,lab_sim_str_delim
$(s)jalwr7	print
$(s)rtnw
$(s)sim_str_delim:
$(s)dats	"--------\r\n\0"
$(s)// ********************************
bra		loop


// ================================
// ISR
// ================================
isr_vect:
$(target_mn)srbk	sreg_bk_3
$(target_ts)pushwm	r0,r1,r2,r3,r6,r7
// msec += 2
ldwi	r7,msec
ldw		r0,[r7]
addwi	r0,2
movw	r2,r0
// (msec>=msec_intvl)? msec-msec_intvl
ldwi	r1,msec_intvl
subw	r2,r1
blo		isr_next
movw	r0,r2
// idx_hue++
ldwi	r6,idx_hue
ldw		r1,[r6]
addwi	r1,1
cmpwi	r1,max_hue2rgb
blo		isr_hue_endif
ldbiu	r1,0
isr_hue_endif:
stw		[r6],r1
// idx_rand++
ldwi	r6,idx_rand
ldw		r1,[r6]
addwi	r1,1
// (idx_rand>=max_rand_1_f)? idx_rand-max_rand_1_f
ldwi	r2,max_rand_1_f
movw	r3,r1
subw	r3,r2
blo		isr_endif
movw	r1,r3
isr_endif:
stw		[r6],r1							// idx_rand=r1
isr_next:
stw		[r7],r0							// msec=r0
// flag clear
ldwi	r7,tledintc
ldw		r0,[r7]
stw		[r7],r0
// return from ISR
$(target_ts)popwm	r7,r6,r3,r2,r1,r0
rti


// ================================
// 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
addwi	r5,1
// LF code check
cmpi	r0,chr_lf
beq		gl_got
// size check
ldwi	r0,lbuf+max_lbuf-1
cmpw	r5,r0
blo		gl_loop
// got line
gl_got:
// string terminate
ldbiu	r0,0
subwi	r5,1
stb		[r5],r0
// check CR code
subwi	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]
addwi	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_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
movw	r0,r1
movw	r4,r5
cendw	r0
lsrwi	r0,4
andwi	r0,0x0f
addw	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
lslwi	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

// ================================
// string compare: r0=strcmp (r1,r2) ; disturbed r1~r4
// ================================
strcmp:
ldb		r3,[r1]
addwi	r1,1
ldb		r4,[r2]
addwi	r2,1
mov		r0,r3
sub		r0,r4
bne		sc_finish
// check nil code
cmpi	r3,0
beq		sc_finish
cmpi	r4,0
bne		strcmp
sc_finish:
rtnw

// ================================
// string n compare: r0=strncmp (r1,r2,r5) ; disturbed r1~r5
// ================================
strncmp:
ldb		r3,[r1]
addwi	r1,1
ldb		r4,[r2]
addwi	r2,1
mov		r0,r3
sub		r0,r4
bne		snc_finish
// check n character
subwi	r5,1
beq		snc_finish
// check nil code
cmpi	r3,0
beq		snc_finish
cmpi	r4,0
bne		strncmp
snc_finish:
rtnw

// ================================
// ascii to integer: r0=atoi (r1)>=0 ; disturbed r1~r3
// ================================
atoi:
ldbiu	r0,0
// r2=[r1]-'0'
ldb		r2,[r1]
subi	r2,0a0
blo		a2i_finish
cmpi	r2,10
bhs		a2i_finish
// r0=10*r0+r2
movw	r3,r0
lslwi	r3,3
addw	r0,r0
addw	r0,r3
addw	r0,r2
// end of loop
addwi	r1,1
bra		atoi+2
a2i_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+ who\r\n"
dats	"OK: help\r\n\0\0"
str_msg_ok:
dats	"OK: \0\r\n\0"
str_err_unspt:
dats	"ERR: unsupported command: \0\r\n\0"
str_err_syntx:
dats	"ERR: syntax\r\n\0"
str_err_no_mult:
dats	"ERR: : not have valid mulur instruction, halted.\r\n\0"
str_err_no_tled:
dats	"ERR: : not have timled5 unit, halted.\r\n\0"
str_cmd_who:
dats	"who\0"
str_cmd_help:
dats	"help\0"

// ================================
// hue to rgb conversion table
// ================================
hue2rgb_tab:
incl	"mcoc_candle_hue.incl"

// ================================
// rand_1_f[] data
// ================================
rand_1_f:
incl	"mcoc_candle.incl"

