+ page 255\r
+ .z80\r
+ \r
+ global msg_rx_fifo,msg_tx_fifo\r
+\r
+ global msginit,msgi.st,msg.in,msgo.st,msg.out\r
+ global msg.sout,msg.co\r
+ \r
+ extrn buf.init\r
+\r
+ include config.inc\r
+ include z180reg.inc\r
+\r
+;--------------------------------------------------------------\r
+\r
+ dseg\r
+\r
+ mkbuf msg_rx_fifo,msg_rx_fifo_len\r
+ mkbuf msg_tx_fifo,msg_tx_fifo_len\r
+\r
+\r
+\r
+;--------------------------------------------------------------\r
+\r
+ cseg\r
+\r
+;\r
+; Init buffer\r
+;\r
+ \r
+msginit:\r
+ ld ix,msg_rx_fifo\r
+ ld a,msg_rx_fifo.mask\r
+ call buf.init\r
+ ld ix,msg_tx_fifo\r
+ ld a,msg_tx_fifo.mask\r
+ jp buf.init\r
+\r
+;--------------------------------------------------------------\r
+\r
+msgi.st:\r
+ push ix\r
+ ld ix,msg_rx_fifo ;\r
+\r
+buf.empty:\r
+ ld a,(ix+o.in_idx) ;\r
+ sub (ix+o.out_idx) ;\r
+ pop ix\r
+ ret z\r
+ or 0ffh\r
+ ret\r
+ \r
+;--------------------------------------------------------------\r
+\r
+msg.in:\r
+ push ix\r
+ ld ix,msg_rx_fifo ;\r
+\r
+buf.get:\r
+ ld a,(ix+o.out_idx) ;\r
+bg.wait:\r
+ cp (ix+o.in_idx) ;\r
+ jr z,bg.wait\r
+\r
+ push hl ;\r
+ push ix\r
+ pop hl\r
+ add a,l\r
+ ld l,a\r
+ jr nc,bg.nc\r
+ inc h\r
+bg.nc:\r
+ ld l,(hl)\r
+ \r
+ ld a,(ix+o.out_idx) ;\r
+ inc a\r
+ and (ix+o.mask)\r
+ ld (ix+o.out_idx),a\r
+ \r
+ ld a,l\r
+ pop hl\r
+ pop ix\r
+ ret\r
+\r
+;--------------------------------------------------------------\r
+\r
+msgo.st:\r
+ push ix\r
+ ld ix,msg_tx_fifo ;\r
+\r
+buf.full:\r
+ ld a,(ix+o.in_idx) ;\r
+ inc a\r
+ and (ix+o.mask)\r
+ sub (ix+o.out_idx) ;\r
+ pop ix\r
+ ret z\r
+ or 0ffh\r
+ ret\r
+\r
+;--------------------------------------------------------------\r
+\r
+msg.out:\r
+ push ix\r
+ ld ix,msg_tx_fifo ;\r
+\r
+buf.put:\r
+ push hl ;\r
+ push bc\r
+ push ix\r
+ pop hl\r
+ ld c,(ix+o.in_idx) ;\r
+ ld b,0\r
+ add hl,bc\r
+ ld b,a\r
+\r
+ ld a,c ;\r
+ inc a\r
+ and (ix+o.mask)\r
+bp.wait:\r
+ cp (ix+o.out_idx) ;\r
+ jr z,bp.wait\r
+ ld (hl),b\r
+ ld (ix+o.in_idx),a\r
+ \r
+ ld a,b\r
+ out0 (AVRINT5),a\r
+ pop bc\r
+ pop hl\r
+ pop ix\r
+ ret\r
+\r
+\r
+;--------------------------------------------------------------\r
+;--------------------------------------------------------------\r
+;--------------------------------------------------------------\r
+\r
+if 0\r
+msg.out:\r
+ push ix\r
+ ld ix,msg_fifo ;\r
+\r
+ push bc\r
+ ld b,a ;save char\r
+ ld a,(ix+o.in_idx) ;\r
+ inc a\r
+ and (ix+o.mask)\r
+bp.wait:\r
+ cp (ix+o.out_idx) ;\r
+ jr z,bp.wait\r
+ ld c,a\r
+ ld a,b\r
+ out (PMSG),a\r
+ ld (ix+o.in_idx),c\r
+ \r
+ pop bc\r
+ pop ix\r
+ ret\r
+endif\r
+\r
+;--------------------------------------------------------------\r
+;\r
+; (hl): data\r
+\r
+msg.sout:\r
+ push ix\r
+ ld ix,msg_tx_fifo ;\r
+\r
+ push bc\r
+ push de\r
+ ld b,(hl) ;\r
+ inc hl\r
+ ex de,hl\r
+ \r
+ms.ol:\r
+ push ix\r
+ pop hl\r
+ ld c,(ix+o.in_idx) ;\r
+ ld a,c\r
+ add l\r
+ ld l,a\r
+ jr nc,ms.on\r
+ inc h\r
+ms.on:\r
+ ld a,c ;\r
+ inc a\r
+ and (ix+o.mask)\r
+ms.wait:\r
+ cp (ix+o.out_idx) ;\r
+ jr z,ms.wait\r
+ ld c,a\r
+ ld a,(de)\r
+ inc de\r
+ ld (hl),a\r
+ ld (ix+o.in_idx),c\r
+ djnz ms.ol\r
+ out0 (AVRINT5),a\r
+ ex de,hl\r
+ pop de\r
+ pop bc\r
+ pop ix\r
+ ret\r
+\r
+;--------------------------------------------------------------\r
+\r
+if 0\r
+msg.sout:\r
+ push ix\r
+ ld ix,msg_fifo ;\r
+ push bc\r
+\r
+ ld b,(hl) ;count\r
+ inc hl\r
+obs_1:\r
+ ld a,(ix+o.out_idx) ;\r
+ sub (ix+o.in_idx) ;\r
+ dec a\r
+ and (ix+o.mask)\r
+ cp b\r
+ jr c,obs_1\r
+\r
+ ld c,(hl) ;port address\r
+ inc hl\r
+ ld a,b\r
+ otir\r
+ add (ix+o.in_idx)\r
+ and (ix+o.mask)\r
+ ld (ix+o.in_idx),a\r
+ pop bc\r
+ pop ix\r
+ ret\r
+\r
+;----------------------------------------------------------------------\r
+endif\r
+\r
+msg.co:\r
+ push af\r
+ push hl\r
+ ld (buf_char),a\r
+ ld hl,buf\r
+ call msg.sout\r
+ pop hl\r
+ pop af\r
+ ret\r
+ \r
+ \r
+buf:\r
+ db buf_end - $ - 1 ;output string length\r
+ db 081h ; message start token\r
+ db buf_end - $ - 1 ; message length\r
+ db 1 ; command\r
+ db 1 ; subcommand\r
+buf_char:\r
+ db 0 ; pay load\r
+buf_end:\r
+\r
+;----------------------------------------------------------------------\r
+\r
+ end\r
+\r