// ================================
// OLED display kanji print program
//		(c) 2023	1YEN Toru
//
//
//		2023/11/25	ver.1.00
//
// ================================
asm		"mcoc_irom.mem"
#asm		"mcoc_irom.dat2",bit16,list
incl	"mcoc115.incl"
incl	"mcvm_lib_wire_h.incl"
# ================================
# string macros
def		prog_name,"oled_kprint"
def		prog_vers,"1.00"
def		s,"#"							// simulation="" / real fpga="#"
def		r,""							// ole: rotate="" / normal="#"
# constants
equ		max_lbuf,64						// size of lbuf
equ		baud,9_600						// baud rate
equ		uart_baud,fcpu/baud-1			// uartbaud setting
equ		stws_baud,fcpu/400_000-1		// i2c: clock speed
$(s)// ********************************
$(s)equ		stws_baud,fcpu/400000/3-1	// i2c: clock speed for sim
$(s)// ********************************
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
# oled driver ssd1306 i/o registers
equ		i2c_ssd1306,0x3c				// i2c: ssd1306 slave address
equ		ole_siz_h,128					// ole: screen size
equ		ole_siz_v,64
equ		ole_reg_ctrl_c,0x00				// ole: ctr: co=0, d/c#=0 (command)
equ		ole_reg_ctrl_d,0x40				// ole: ctr: co=0, d/c#=1 (data)
equ		ole_reg_ctrl_cc,0x80			// ole: ctr: co=1, d/c#=0 (command)
equ		ole_reg_ctrl_cd,0xc0			// ole: ctr: co=1, d/c#=1 (data)
equ		ole_reg_madr_mod,0x20			// ole: reg: memory addressing mode
equ		ole_reg_col_adr,0x21			// ole: reg: column address
equ		ole_reg_pag_adr,0x22			// ole: reg: page address
equ		ole_reg_disp_lin,0x40			// ole: reg: display start line
equ		ole_reg_cont_ctrl,0x81			// ole: reg: contrast control
equ		ole_reg_chg_pump,0x8d			// ole: reg: charge pump setting
equ		ole_reg_seg_remap,0xa0			// ole: reg: segment re-map
equ		ole_reg_ent_disp,0xa4			// ole: reg: entire display on
equ		ole_reg_nor_disp,0xa6			// ole: reg: normal display
equ		ole_reg_inv_disp,0xa7			// ole: reg: inverse display
equ		ole_reg_mux_rat,0xa8			// ole: reg: multiplex ratio
equ		ole_reg_disp_off,0xae			// ole: reg: display off
equ		ole_reg_disp_on,0xaf			// ole: reg: display on
equ		ole_reg_scan_dir,0xc0			// ole: reg: com output scan dir
equ		ole_reg_disp_ofst,0xd3			// ole: reg: display offset
equ		ole_reg_clk_div,0xd5			// ole: reg: display clock divide
equ		ole_reg_com_cnfg,0xda			// ole: reg: com pins h/w config
# variables
equ		va,ramtop
equ		prn_line,va						// ole: line number to draw string
equ		uprn_line,va
equ		va,va+2
equ		scr_ofst,va						// ole: scroll offset
equ		va,va+2
equ		static_va,va
# array variables
equ		lbuf,va							// receive data buffer
equ		va,va+max_lbuf
equ		i2c_buf,va						// i2c: buffer for i2c frame
equ		va,va+2*64
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

// stwser setting
ldwi	r0,stws_baud
ldwi	r7,stwmbaud
stw		[r7],r0
// check stwser unit
ldw		r1,[r7]
cmp		r0,r1
beq		stw_found
// ERROR: stwser unit not found
ldwi	r1,lab_str_err_nounit
jmpr7	error_halt
stw_found:

// fontjp setting
ldwi	r7,fnjpctl
ldbiu	r0,fnjp_rot_1
stw		[r7],r0
// check fontjp unit
ldw		r1,[r7]
cmp		r0,r1
beq		fjp_found
// ERROR: fontjp unit not found
ldwi	r1,lab_str_err_nounit
jmpr7	error_halt
fjp_found:

// check unisji unit
ldwi	r7,unsjctl
ldw		r0,[r7]
andi	r0,unsj_unsj
bne		unsj_found
// ERROR: unisji unit not found
ldwi	r1,lab_str_err_nounit
jmpr7	error_halt
unsj_found:

$(s)// ********************************
$(s)// setup dummy slave
$(s)ldbiu	r0,i2c_ssd1306
$(s)ldwi	r7,stwsadr
$(s)stw		[r7],r0
$(s)ldbiu	r0,0
$(s)bra		pcnt+6
$(s)// ********************************
// oled display init
jalwr7	ole_init
cmpi	r0,0
beq		ole_init_done
// ERROR: oled display could not initialize
ldwi	r1,lab_str_err_oled
jmpr7	error_halt
ole_init_done:
$(s)// ********************************
$(s)bra		sim_skip_oleprn
$(s)// ********************************
ldwi	r1,lab_str_ole_rdy
ldbiu	r2,0
jalwr7	ole_kprint
ldwi	r1,lab_str_ole_clr
ldbiu	r2,1
jalwr7	ole_kprint
ldwi	r1,lab_str_ole_clr
ldbiu	r2,2
jalwr7	ole_kprint
ldwi	r1,lab_str_ole_clr
ldbiu	r2,3
jalwr7	ole_kprint
$(s)// ********************************
$(s)sim_skip_oleprn:
$(s)// ********************************

// 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)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)addi	r6,1
$(s)cmpi	r0,0
$(s)beq		sim_loop_1
$(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	"uprn,0,\x58\xef\xbd\xb6\0"		// "Xｶ"
$(s)dats	"uprn,0,\xe3\x83\xbb\xef\xbc\x81\0"		// "・！"
$(s)dats	"uprn,0,\xef\xbc\x95\xef\xbc\xb1\0"		// "５Ｑ"
$(s)dats	"uprn,0,\xe3\x82\x80\xe3\x82\xb6\0"		// "むザ"
$(s)dats	"uprn,0,\xe4\xba\x9c\xe8\x85\x95\0"		// "亜腕"
$(s)dats	"uprn,0,\xe5\xbc\x8c\xe7\x86\x99\0"		// "弌熙"
$(s)dats	"*"							// end mark
$(s)dats	"who\0"
$(s)dats	"help\0"
$(s)dats	"*"							// end mark
$(s)dats	"prn,0,こんにちは、世界\0"
$(s)dats	"uprn,0,\xef\xbc\xbf\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 "prn," command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_prn
ldbiu	r5,4
jalwr7	strncmp
cmpi	r0,0
bne		prn_notid
// ================================
// prn,<line_0_3>,<string_sjis>
// <line_0_3>
ldwi	r1,lbuf+4
jalwr7	atoi
andi	r0,0x03
ldwi	r7,prn_line
stw		[r7],r0
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <string_sjis>
addi	r1,1
mov		r2,r1
ldwi	r1,lbuf
jalwr7	strcpy
// print
ldwi	r1,lbuf
ldwi	r7,prn_line
ldw		r2,[r7]
jalwr7	ole_kprint
// response
ldwi	r1,lab_str_msg_prn
jalwr7	print
bra		no_data
prn_notid:

// check "uprn," command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_uprn
ldbiu	r5,5
jalwr7	strncmp
cmpi	r0,0
bne		uprn_notid
// ================================
// uprn,<line_0_3>,<string_utf8>
// <line_0_3>
ldwi	r1,lbuf+5
jalwr7	atoi
andi	r0,0x03
ldwi	r7,uprn_line
stw		[r7],r0
ldb		r0,[r1]
cmpi	r0,chr_cma
bne		err_syntx
// <string_utf8>
addi	r1,1
mov		r2,r1
ldwi	r1,lbuf
jalwr7	strcpy
// utf-8 -> s-jis
ldwi	r1,lbuf
jalwr7	utf82sjis
// print
ldwi	r1,lbuf
ldwi	r7,uprn_line
ldw		r2,[r7]
jalwr7	ole_kprint
// response
ldwi	r1,lab_str_msg_uprn
jalwr7	print
bra		no_data
uprn_notid:

// check "scr," command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_scr
ldbiu	r5,4
jalwr7	strncmp
cmpi	r0,0
bne		scr_notid
// ================================
// scr,<offset_0_63>
// <offset_0_63>
ldwi	r1,lbuf+4
jalwr7	atoi
andi	r0,0x3f
ldwi	r7,scr_ofst
stw		[r7],r0
// scroll
mov		r1,r0
jalwr7	ole_set_offset
// response
ldwi	r1,lab_str_msg_scr
jalwr7	print
pushw	r1
ldwi	r1,scr_ofst
ldw		r1,[r1]
jalwr7	print_dec
popw	r1
jalwr7	print
bra		no_data
scr_notid:

// 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:


// ================================
// 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


// ================================
// ERROR: halted
error_halt:
jalwr7	print
$(s)// ********************************
$(s)ldwi	r0,simfail
$(s)ldwi	r7,simfnsh
$(s)stw		[r7],r0
$(s)// ********************************
pause
bra		pcnt-4


// ================================
// oled set offset r1 ; disturbed r0,r1,r7
// ================================
ole_set_offset:
ldwi	r7,i2c_buf
// S [adr,w]
ldwi	r0,0x4100+(i2c_ssd1306<<1)
stw		[r7],r0
addi	r7,2
// [0x00]
ldwi	r0,0x4000+ole_reg_ctrl_c
stw		[r7],r0
addi	r7,2
// [DISP_OFST]
ldwi	r0,0x4000+ole_reg_disp_ofst
stw		[r7],r0
addi	r7,2
// [r1]
ldwi	r0,0x4000
add		r0,r1
stw		[r7],r0
addi	r7,2
// P
ldbiu	r0,0
stw		[r7],r0
addi	r7,2
// call mcvm_wire
ldwi	r1,i2c_buf
$(s)// ********************************
$(s)bra		pcnt+6
$(s)// ********************************
jalwr7	mcvm_wire
rtnw


// ================================
// oled kanji print r1 at line r2 ; disturbed r0~r3,r7
// ================================
ole_kprint:
ldbiu	r3,0
pushw	r1
cmpi	r2,ole_siz_v/16
blo		pcnt+2
ldbiu	r2,ole_siz_v/16-1
// set rect
ldwi	r7,i2c_buf
// S [adr,w]
ldwi	r0,0x4100+(i2c_ssd1306<<1)
stw		[r7],r0
addi	r7,2
// [0x00]
ldwi	r0,0x4000+ole_reg_ctrl_c
stw		[r7],r0
addi	r7,2
// [COL_ADR]
ldwi	r0,0x4000+ole_reg_col_adr
stw		[r7],r0
addi	r7,2
// [8*r3]
lsli	r3,3
ldwi	r0,0x4000
add		r0,r3
stw		[r7],r0
addi	r7,2
// [127]
ldwi	r0,0x4000+ole_siz_h-1
stw		[r7],r0
addi	r7,2
// [PAG_ADR]
ldwi	r0,0x4000+ole_reg_pag_adr
stw		[r7],r0
addi	r7,2
// [2*r2]
ldwi	r0,0x4000
lsli	r2,1
add		r0,r2
stw		[r7],r0
addi	r7,2
// [2*r2+1]
addi	r0,1
stw		[r7],r0
addi	r7,2
// P
ldbiu	r0,0
stw		[r7],r0
addi	r7,2
// call mcvm_wire
ldwi	r1,i2c_buf
$(s)// ********************************
$(s)bra		pcnt+6
$(s)// ********************************
jalwr7	mcvm_wire

// print
popw	r1
ole_prn_loop:
ldb		r0,[r1]
addi	r1,1
lsli	r0,8
beq		ole_prn_break
// s-jis?
ldwi	r7,fnjpcod
stw		[r7],r0
ldwi	r7,fnjpctl
ldw		r2,[r7]
andi	r2,fnjp_sjs1
beq		pcnt+6
// yes, s-jis 2 bytes code
ldb		r2,[r1]
addi	r1,1
add		r0,r2
jalwr7	ole_kprint_chr
bra		ole_prn_loop

ole_prn_break:

ole_prn_return:
rtnw

// ================================
// oled kanji print 1 character r0 ; disturbed r0,r7
// ================================
ole_kprint_chr:
pushwm	r1,r2,r3,r4,r6
// font
ldwi	r7,fnjpcod
stw		[r7],r0
$(s)// ********************************
$(s)ldwi	r7,prinhex
$(s)stw		[r7],r0
$(s)// ********************************
// i2c frame
ldwi	r1,i2c_buf
ldwi	r0,0x4100+(i2c_ssd1306<<1)
stw		[r1],r0
addi	r1,2
ldwi	r0,0x4000+ole_reg_ctrl_d
stw		[r1],r0
addi	r1,2
// draw character
ldwi	r6,fnjpdbl
ldwi	r7,fnjpdata
ldbiu	r4,8
ole_prc_loop:
ldb		r0,[r7]
stw		[r6],r0
ldw		r0,[r6]
mov		r2,r0
andi	r0,0xff
lsri	r2,8
ldwi	r3,0x4000
add		r0,r3
add		r2,r3
stw		[r1],r0
addi	r1,2
stw		[r1],r2
addi	r1,2
addi	r7,1
// end of loop
subi	r4,1
bne		ole_prc_loop
// P
ldbiu	r0,0
stw		[r1],r0
// call mcvm_wire
ldwi	r1,i2c_buf
$(s)// ********************************
$(s)pushc	sr
$(s)ldcl	sr,sreg_bk_1
$(s)ldwi	r7,fnjpdata
$(s)ldwi	r6,princhr
$(s)ldbiu	r2,8
$(s)sim_ole_prc_loop2:
$(s)ldb		r0,[r7]
$(s)lsli	r0,16-8						// 16=Mc
$(s)ldbiu	r1,8
$(s)sim_ole_prc_loop1:
$(s)lsli	r0,1
$(s)bcc		pcnt+4
$(s)ldbiu	r3,0ax-0x20
$(s)bra		pcnt+2
$(s)ldbiu	r3,chr_spc
$(s)stw		[r6],r3
$(s)stw		[r6],r3
$(s)subi	r1,1
$(s)bne		sim_ole_prc_loop1
$(s)ldbiu	r3,chr_lf
$(s)stw		[r6],r3
$(s)addi	r7,1
$(s)subi	r2,1
$(s)bne		sim_ole_prc_loop2
$(s)popc	sr
$(s)bra		ole_prc_return
$(s)// ********************************
jalwr7	mcvm_wire

ole_prc_return:
popwm	r6,r4,r3,r2,r1
rtnw

// ================================
// oled display (ssd1306) initialize
// ================================
ole_init:
// call mcvm_wire
ldwi	r1,lab_ole_init_reg
jalwr7	mcvm_wire
rtnw

// oled display (ssd1306) initialization i2c frame
ole_init_reg:
// [adr,w] [0x00]
datw	0x4100+(i2c_ssd1306<<1)
datw	0x4000+ole_reg_ctrl_c
// set mux ratio (0xa8, 0x3f)
datw	0x4000+ole_reg_mux_rat
datw	0x4000+0x3f
// set display offset (0xd3,0x00)
datw	0x4000+ole_reg_disp_ofst
datw	0x4000+0x00
// set display start line (0x40)
datw	0x4000+ole_reg_disp_lin+0x00
// set segment re-map (0xa0 / 0x0a1)
datw	0x4000+ole_reg_seg_remap
$(r)datw	0x4000+ole_reg_seg_remap+0x01
// set com output scan direction (0xc0 / 0xc8)
datw	0x4000+ole_reg_scan_dir
$(r)datw	0x4000+ole_reg_scan_dir+0x08
// set com pins hardware configuration (0xda,0x02)
datw	0x4000+ole_reg_com_cnfg
datw	0x4000+0x02
$(r)datw	0x4000+ole_reg_com_cnfg
$(r)datw	0x4000+0x22
// set contrast control (0x81,0x7f)
datw	0x4000+ole_reg_cont_ctrl
datw	0x4000+0x7f
// disable entire display on (0xa4)
datw	0x4000+ole_reg_ent_disp
// set normal display (0xa6)
datw	0x4000+ole_reg_nor_disp
// set osc frequency (0xd5,0x80)
datw	0x4000+ole_reg_clk_div
datw	0x4000+0x80
// enable charge pump regulator (0x8d,0x14)
datw	0x4000+ole_reg_chg_pump
datw	0x4000+0x14
// display on (0xaf)
datw	0x4000+ole_reg_disp_on
// vertical addressing mode
datw	0x4000+ole_reg_madr_mod
datw	0x4000+0x01
// P
datw	0x0000


// ================================
// 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
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_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

// ================================
// print decimal r1 ; disturbed r0~r4,r6,r7
// ================================
print_dec:
#$(s)// ********************************
#$(s)ldwi	r7,prindec
#$(s)stw	[r7],r1
#$(s)rtnw
#$(s)// ********************************
cmpi	r1,0
bne		pdc_not_0
// 0
ldbiu	r1,0a0
jalwr7	print_chr
bra		pdc_fin

pdc_not_0:
// for each r0=10000, ~, 10, 1
ldwi	r6,lab_pdc_tab
ldbiu	r3,true							// omit leading zero flag
pdc_loop:
ldw		r0,[r6]
cmpi	r0,0
beq		pdc_fin
// r2=r1/r0
ldbiu	r2,0
pdc_div:
sub		r1,r0
blo		pcnt+4
addi	r2,1
bra		pdc_div

// omit leading zero
cmpi	r3,0
beq		pcnt+4
cmpi	r2,0
beq		pdc_omit
// print 1 digit
addi	r2,0a0
pushwm	r0,r1,r7
mov		r1,r2
jalwr7	print_chr
popwm	r7,r1,r0
ldbiu	r3,false
pdc_omit:
// add back r1; loop
add		r1,r0
addi	r6,2
bra		pdc_loop

pdc_fin:
rtnw

pdc_tab:
datw	10000
datw	1000
datw	100
datw	10
datw	1
datw	0

// ================================
// 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
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]
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
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
mov		r3,r0
lsli	r3,3
add		r0,r0
add		r0,r3
add		r0,r2
// loop
addi	r1,1
bra		atoi+2
a2i_finish:
rtnw

// ================================
// string copy: strcpy (r1,r2) ; disturbed r0~r2
//	return: r1 pointed to '\0' position
//			r2 pointed to '\0'+1 position
// ================================
strcpy:
ldb		r0,[r2]
addi	r2,1
stb		[r1],r0
addi	r1,1
mov		r0,r0
bne		strcpy
subi	r1,1
rtnw

// ================================
// string copy decimal r2 to r1 ; disturbed r0,r2~r4,r6,r7
//	'\0' is copied after copying decimal
//	return: r1=pointer to the last '\0'
// ================================
strcpy_dec:
mov		r2,r2
bne		cpd_not_0
// 0
ldbiu	r2,0a0
stb		[r1],r2
addi	r1,1
ldbiu	r0,0
bra		cpd_fin

cpd_not_0:
// for each r0=10000, ~, 10, 1
ldwi	r6,lab_cpd_tab
ldbiu	r3,true							// omit leading zero flag
cpd_loop:
ldw		r0,[r6]
mov		r0,r0
beq		cpd_fin
// r4=r2/r0
ldbiu	r4,0
cpd_div:
sub		r2,r0
blo		pcnt+4
addi	r4,1
bra		cpd_div

// omit leading zero
mov		r3,r3
beq		pcnt+4
mov		r4,r4
beq		cpd_omit
// copy 1 digit
addi	r4,0a0
stb		[r1],r4
addi	r1,1
ldbiu	r3,false
cpd_omit:
// add back r2; loop
add		r2,r0
addi	r6,2
bra		cpd_loop

cpd_fin:
stb		[r1],r0
rtnw

cpd_tab:
datw	10000
datw	1000
datw	100
datw	10
datw	1
datw	0

// ================================
// utf-8 -> s-jis conversion for string r1; disturbed r0,r2,r3,r7
//	4 byte code of utf-8 is not supported
//	return: r1=converted string
// ================================
utf82sjis:
pushw	r1
mov		r3,r1
u2s_loop:
// get 1 char
ldb		r0,[r1]
cmpi	r0,0
beq		u2s_break
addi	r1,1
// utf-8 to unicode
mov		r2,r0
andi	r2,0xf0
cmpi	r2,0xe0
beq		u2s_utf_3b
mov		r2,r0
andi	r2,0xe0
cmpi	r2,0xc0
beq		u2s_utf_2b
// utf-8: 1 byte code
u2s_utf_1b:
andi	r0,0x7f
bra		u2s_unicode

// utf-8: 2 byte code
u2s_utf_2b:
mov		r2,r0
andi	r2,0x1f
lsli	r2,6
ldb		r0,[r1]
addi	r1,1
andi	r0,0x3f
or		r0,r2
bra		u2s_unicode

// utf-8: 3 byte code
u2s_utf_3b:
mov		r2,r0
andi	r2,0x0f
lsli	r2,6
ldb		r0,[r1]
addi	r1,1
andi	r0,0x3f
or		r2,r0
lsli	r2,6
ldb		r0,[r1]
addi	r1,1
andi	r0,0x3f
or		r0,r2
bra		u2s_unicode

// unicode to s-jis
u2s_unicode:
$(s)// ********************************
$(s)ldwi	r7,prinhex
$(s)stw		[r7],r0
$(s)// ********************************
cmpi	r0,0x80
blo		u2s_ascii
ldwi	r7,unsjunic
stw		[r7],r0
ldwi	r7,unsjctl
u2s_poll:
ldw		r0,[r7]
andi	r0,unsj_bsy
bne		u2s_poll
ldwi	r7,unsjsjis
ldw		r0,[r7]
u2s_ascii:
$(s)// ********************************
$(s)ldwi	r7,prinhex
$(s)stw		[r7],r0
$(s)// ********************************

// store converted character
mov		r2,r0
cendw	r2
andi	r2,0xff
bne		u2s_sjis_2b
// s-jis: 1 byte code
u2s_sjis_1b:
stb		[r3],r0
bra		u2s_sjis_endif

// s-jis: 2 byte code
u2s_sjis_2b:
stb		[r3],r2
addi	r3,1
stb		[r3],r0
u2s_sjis_endif:
addi	r3,1

// end of loop
bra		u2s_loop
u2s_break:
// string terminate
ldbiu	r0,0
stb		[r3],r0

// end of subroutine
popw	r1
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+ prn,<line_0_3>,<string_sjis>\r\n"
dats	"OK+ uprn,<line_0_3>,<string_utf8>\r\n"
dats	"OK+ scr,<offset_0_63>\r\n"
dats	"OK+ who\r\n"
dats	"OK: help\r\n\0\0"
str_msg_prn:
dats	"OK: prn\r\n\0"
str_msg_uprn:
dats	"OK: uprn\r\n\0"
str_msg_scr:
dats	"OK: scr=\0\r\n\0"
str_ole_rdy:
#		 0123456789012345
dats	"Ready           \0"
str_ole_clr:
dats	"                \0"
str_err_unspt:
dats	"ERR: unsupported command: \0\r\n\0"
str_err_syntx:
dats	"ERR: syntax\r\n\0"
str_err_nounit:
dats	"ERR: not found: STWSER, FONTJP, UNISJI unit, halted.\r\n\0"
str_err_oled:
dats	"ERR: could not initialize OLED display, halted.\r\n\0"
str_cmd_prn:
dats	"prn,\0"
str_cmd_uprn:
dats	"uprn,\0"
str_cmd_scr:
dats	"scr,\0"
str_cmd_who:
dats	"who\0"
str_cmd_help:
dats	"help\0"

# ================================
incl	"mcvm_lib_wire_m.incl"

