// ================================
// test SMBus (over I2C) protocol
//		(c) 2021	1YEN Toru
//
//
//		2021/08/28	ver.1.00
//			256 byte serial RAM emulation
//
// ================================
asm		"mcoc_irom.mif"
#asm		"mcoc_irom.mem"
#asm		"mcoc_irom.dat2",bit16,list
incl	"mcoc115.incl"
incl	"mcvm_lib_wire_h.incl"
# ================================
# string macros
def		prog_name,"test_smbus"
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		i2c_slv_adr,0x08				// i2c: slave address
equ		i2c_max_reg,256					// i2c: size of i2c_reg
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_cln,0x3a					// colon ':' code
# variables
equ		va,ramtop
equ		i2c_cmd,va
equ		va,va+2
equ		static_va,va
# array variables
equ		lbuf,va							// receive data buffer
equ		va,va+max_lbuf
equ		i2c_reg,va						// 256 byte RAM buffer
equ		va,va+i2c_max_reg
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: SRAR enable, set slave address
ldbiu	r0,stws_srae
ldwi	r7,stwsctl
stw		[r7],r0
ldbiu	r0,i2c_slv_adr
ldwi	r7,stwsadr
stw		[r7],r0

// interrupt setting
ldc		iv,lab_mcvm_wire_isr
seie

// initialize i2c_reg
ldwi	r7,i2c_reg
ldwi	r6,lab_str_message
ldwi	r1,i2c_max_reg
// copy message
iorg_loop1:
ldb		r0,[r6]
mov		r0,r0
beq		iorg_break
stb		[r7],r0
addi	r7,1
addi	r6,1
subi	r1,1
bne		iorg_loop1
// fill 0x00
iorg_break:
ldbiu	r0,0
iorg_loop2:
stb		[r7],r0
addi	r7,1
subi	r1,1
bne		iorg_loop2

// send RDY
ldwi	r1,lab_str_msg_rdy
jalwr7	print

$(s)// ********************************
$(s)// for simulation
$(s)jalwr7	cmd_dump
$(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

// check "msg" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_msg
ldbiu	r5,3
jalwr7	strncmp
mov		r0,r0
bne		msg_notid
// ================================
// msg
// set a message in ram
ldwi	r7,lab_str_message
ldwi	r6,i2c_reg
msg_loop:
ldb		r0,[r7]
mov		r0,r0
beq		msg_break
stb		[r6],r0
addi	r7,1
addi	r6,1
bra		msg_loop

msg_break:
// response
ldwi	r1,lab_str_msg_msg
jalwr7	print
bra		no_data
msg_notid:

// check "seta" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_seta
ldbiu	r5,4
jalwr7	strncmp
mov		r0,r0
beq		cmd_seta_clra
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_clra
ldbiu	r5,4
jalwr7	strncmp
mov		r0,r0
bne		clra_notid
// ================================
// seta / clra
cmd_seta_clra:
ldwi	r7,lbuf
ldb		r7,[r7]
cmpi	r7,0as
ldbiu	r0,0xff
beq		pcnt+2
ldbiu	r0,0
// set/clear ram
ldwi	r7,i2c_reg
ldwi	r1,256
seta_loop:
stb		[r7],r0
addi	r7,1
subi	r1,1
bne		seta_loop
// response
mov		r0,r0
beq		pcnt+6
ldwi	r1,lab_str_msg_seta
bra		pcnt+4
ldwi	r1,lab_str_msg_clra
jalwr7	print
bra		no_data
clra_notid:

// check "dump" command
ldwi	r1,lbuf
ldwi	r2,lab_str_cmd_dump
ldbiu	r5,4
jalwr7	strncmp
mov		r0,r0
bne		dump_notid
// ================================
// dump
cmd_dump:
ldwi	r1,lab_str_msg_dump_adr
jalwr7	print

ldwi	r3,0
dump_loop:
ldwi	r0,i2c_max_reg
cmp		r3,r0
bhs		dump_break

// print address
mov		r0,r3
andi	r0,0x0f
bne		dump_notzero
ldwi	r1,lab_str_crlf
jalwr7	print
mov		r1,r3
pushw	r3
jalwr7	print_hex
popw	r3
ldbiu	r1,chr_cln
jalwr7	print_chr
dump_notzero:

// print 8 bit data
ldbiu	r1,chr_spc
jalwr7	print_chr
ldwi	r7,i2c_reg
add		r7,r3
ldb		r1,[r7]
lsfti	r1,8
pushw	r3
ldbiu	r3,2
jalwr7	print_hex+2
popw	r3
addi	r3,1
bra		dump_loop

dump_break:
ldwi	r1,lab_str_crlf
jalwr7	print
// response
ldwi	r1,lab_str_msg_dump
jalwr7	print
$(s)// ********************************
$(s)rtnw
$(s)// ********************************
bra		no_data
dump_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
ldwi	r1,lbuf
jalwr7	print
ldwi	r1,lab_str_crlf
jalwr7	print
bra		no_data

// ERROR: syntax
err_syntx:
ldwi	r1,lab_str_err_syntx
jalwr7	print
bra		no_data

no_data:

// ================================
// end of loop
bra		loop


// ================================
// moscovium wire library
// ================================
// ================================
// start condition event
mcvm_wire_onstar:
// nothing to do
rtnw

// ================================
// receive address byte event
mcvm_wire_onadrb:
// read or write?
andi	r0,stws_adrr
beq		mwona_write

// read access
ldwi	r7,i2c_cmd
ldw		r1,[r7]
ldwi	r7,i2c_reg
add		r7,r1
ldb		r0,[r7]
// slave send
ldwi	r7,stwsdats
stw		[r7],r0
bra		mwona_return

// write access
mwona_write:
// clear command
ldwi	r0,-1
ldwi	r7,i2c_cmd
stw		[r7],r0
bra		mwona_return

mwona_return:
rtnw

// ================================
// receive data event
mcvm_wire_onrecv:
// check command byte
ldwi	r7,i2c_cmd
ldw		r1,[r7]
mov		r1,r1
bpl		mwonw_normal

// command byte
andi	r0,0xff
stw		[r7],r0
bra		mwonr_return

// SMBus byte write
mwonw_normal:
ldwi	r7,i2c_reg
add		r7,r1
stb		[r7],r0
bra		mwonr_return

mwonr_return:
rtnw

// ================================
// stop condition event
mcvm_wire_onstop:
// nothing to do
rtnw


// ================================
// 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,4
$(s)beq		pcnt+6
$(s)lsfti	r1,-8
$(s)stb		[r7],r1
$(s)bra		pcnt+2
$(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"

// ================================
// 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]
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

// ================================
// 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+ msg\r\n"
dats	"OK+ seta\r\n"
dats	"OK+ clra\r\n"
dats	"OK+ dump\r\n"
dats	"OK+ who\r\n"
dats	"OK: help\r\n\0\0"
str_msg_msg:
dats	"OK: message write to i/o register\r\n\0"
str_msg_seta:
dats	"OK: all 0xff write\r\n\0"
str_msg_clra:
dats	"OK: all 0x00 write\r\n\0"
str_msg_dump_adr:
dats	"addr: +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f\0"
str_msg_dump:
dats	"OK: dump\r\n\0"
str_err_unspt:
dats	"ERR: unsupported command: \0"
str_err_syntx:
dats	"ERR: syntax\r\n\0"
str_cmd_msg:
dats	"msg\0"
str_cmd_seta:
dats	"seta\0"
str_cmd_clra:
dats	"clra\0"
str_cmd_dump:
dats	"dump\0"
str_cmd_who:
dats	"who\0"
str_cmd_help:
dats	"help\0"
str_crlf:
dats	"\r\n\0"
str_message:
dats	"hello, raspbian\0"

# ================================
incl	"mcvm_lib_wire_s.incl"

