// ================================
// MCOC115 Combinational Test
//		Synchronous Two Wire Serial unit test
//		(c) 2021	1YEN Toru
// ================================
asm		"mcoc_irom.mem"
incl	"mcoc115.incl"
# ================================
equ		stws_baud,fcpu/400000/3-1
equ		mst_cmd,0x5a
equ		mst_rcv1,0x3c
equ		mst_rcv2,0xc3
equ		slv_adr,0x60
# ================================

// ================================
// master send: S [ADR,W] [CMD] R [ADR,R] [[RCV1]] [[RCV2]] P
// ================================
// master unit settings
ldbiu	r0,stws_mtee|stws_mrae
ldwi	r7,stwmctl
stw		[r7],r0
ldbiu	r0,stws_baud
ldwi	r7,stwmbaud
stw		[r7],r0
// slave unit settings
ldbiu	r0,stws_stae|stws_srae
ldwi	r7,stwsctl
stw		[r7],r0
ldbiu	r0,slv_adr
ldwi	r7,stwsadr
stw		[r7],r0

// ================================
// master send: S [ADR,W]
ldwi	r7,stwmdats
ldwi	r0,stws_msta|(slv_adr<<1)|stws_adrw
stw		[r7],r0

// wait SRAF=1
ldwi	r0,~intc_sram
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail
// slave detect: S
ldwi	r7,stwsdatr
ldw		r0,[r7]
ldwi	r1,stws_ssta
cmp		r0,r1
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// wait MTEF=1
ldwi	r0,~intc_mtem
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtef|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail
ldbiu	r0,stws_mtef|stws_mtee|stws_mrae
stw		[r7],r0
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail
// master send: [CMD]
ldwi	r7,stwmdats
ldbiu	r0,mst_cmd
stw		[r7],r0

// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail
// slave receive: [ADR,W]
ldwi	r7,stwsdatr
ldw		r0,[r7]
ldwi	r1,stws_sadr|(slv_adr<<1)|stws_adrw
cmp		r0,r1
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// wait MTEF=1
ldwi	r0,~intc_mtem
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtef|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail
ldbiu	r0,stws_mtef|stws_mtee|stws_mrae
stw		[r7],r0
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail

// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail
// slave receive: [CMD]
ldwi	r7,stwsdatr
ldw		r0,[r7]
cmpi	r0,mst_cmd
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// ================================
// master send: R [ADR,R]
ldwi	r7,stwmdats
ldwi	r0,stws_msta|(slv_adr<<1)|stws_adrr
stw		[r7],r0
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtee|stws_mrae
bne		tstfail

// wait SRAF=1
ldwi	r0,~intc_sram
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail
// slave detect: R
ldwi	r7,stwsdatr
ldw		r0,[r7]
ldwi	r1,stws_ssta
cmp		r0,r1
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// wait SRAF=1
ldwi	r0,~intc_sram
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail

// slave receive: [ADR,R]
ldwi	r7,stwsdatr
ldw		r0,[r7]
ldwi	r1,stws_sadr|(slv_adr<<1)|stws_adrr
cmp		r0,r1
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// slave send: [[RCV1]]
ldwi	r7,stwsdats
ldbiu	r0,mst_rcv1
stw		[r7],r0

// wait MTEF=1
ldwi	r0,~intc_mtem
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtef|stws_mtee|stws_mrae
bne		tstfail
ldbiu	r0,stws_mtef|stws_mtee|stws_mrae
stw		[r7],r0
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail

// ================================
// master request: [[RCV1]]
ldbiu	r0,stws_recv
ldwi	r7,stwmreqr
stw		[r7],r0

// wait STAF=1
ldwi	r0,~intc_stam
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// slave send: [[RCV2]]
ldbiu	r0,mst_rcv2
ldwi	r7,stwsdats
stw		[r7],r0
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_stae|stws_srae
bne		tstfail

// wait MRAF=1
ldwi	r0,~intc_mram
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mraf|stws_mtee|stws_mrae
bne		tstfail

// master receive: [[RCV1]]
ldwi	r7,stwmdatr
ldw		r0,[r7]
cmpi	r0,mst_rcv1
bne		tstfail
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail

// master request: [[RCV2]](N) P
ldwi	r7,stwmreqr
ldwi	r0,stws_stpr|stws_nack|stws_recv
stw		[r7],r0

// wait STAF=1
ldwi	r0,~intc_stam
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// slave send: [[RCV3]] (over written)
ldbiu	r0,0x96
ldwi	r7,stwsdats
stw		[r7],r0

// wait MRAF=1
ldwi	r0,~intc_mram
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mraf|stws_mtee|stws_mrae
bne		tstfail

// master receive: [[RCV2]]
ldwi	r7,stwmdatr
ldw		r0,[r7]
cmpi	r0,mst_rcv2
bne		tstfail
// check flags
ldwi	r7,stwmctl
ldw		r0,[r7]
cmpi	r0,stws_mack|stws_mtaf|stws_mtee|stws_mrae
bne		tstfail

// wait MTEF=1, until stop condition issued
ldwi	r0,~intc_mtem
ldwi	r7,intmsk
stw		[r7],r0
nop
pause
// clear MTEF
ldwi	r7,stwmctl
ldw		r0,[r7]
stw		[r7],r0

// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_sraf|stws_stae|stws_srae
bne		tstfail
// slave detect: P
ldwi	r7,stwsdatr
ldw		r0,[r7]
ldwi	r1,stws_sstp
cmp		r0,r1
bne		tstfail
// check flags
ldwi	r7,stwsctl
ldw		r0,[r7]
cmpi	r0,stws_staf|stws_stae|stws_srae
bne		tstfail

// ================================
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
nop
// ================================

