page 255 .z80 global mrx.fifo,mtx.fifo global msginit,msgi.st,msg.in,msgo.st,msg.out global msg.sout extrn buf.init include config.inc if CPU_Z180 include z180reg.inc endif ;-------------------------------------------------------------- dseg mkbuf mrx.fifo,mrx.fifo_len mkbuf mtx.fifo,mtx.fifo_len ;-------------------------------------------------------------- cseg ; ; Init buffer ; msginit: ld ix,mrx.fifo ld a,mrx.fifo.mask call buf.init ld ix,mtx.fifo ld a,mtx.fifo.mask jp buf.init ;-------------------------------------------------------------- msgi.st: push ix ld ix,mrx.fifo ; buf.empty: ld a,(ix+o.in_idx) ; sub (ix+o.out_idx) ; pop ix ret z or 0ffh ret ;-------------------------------------------------------------- msg.in: push ix ld ix,mrx.fifo ; buf.get: ld a,(ix+o.out_idx) ; bg.wait: cp (ix+o.in_idx) ; jr z,bg.wait push hl ; push ix pop hl add a,l ld l,a jr nc,bg.nc inc h bg.nc: ld l,(hl) ld a,(ix+o.out_idx) ; inc a and (ix+o.mask) ld (ix+o.out_idx),a ld a,l pop hl pop ix ret ;-------------------------------------------------------------- msgo.st: push ix ld ix,mtx.fifo ; buf.full: ld a,(ix+o.in_idx) ; inc a and (ix+o.mask) sub (ix+o.out_idx) ; pop ix ret z or 0ffh ret ;-------------------------------------------------------------- msg.out: push ix ld ix,mtx.fifo ; buf.put: push hl ; push bc push ix pop hl ld c,(ix+o.in_idx) ; ld b,0 add hl,bc ld b,a ld a,c ; inc a and (ix+o.mask) bp.wait: cp (ix+o.out_idx) ; jr z,bp.wait ld (hl),b ld (ix+o.in_idx),a ld a,b out (AVRINT5),a pop bc pop hl pop ix ret ;-------------------------------------------------------------- ; ; (hl): data msg.sout: push ix ld ix,mtx.fifo ; push bc push de ld b,(hl) ; inc hl ex de,hl ms.ol: push ix pop hl ld c,(ix+o.in_idx) ; ld a,c add l ld l,a jr nc,ms.on inc h ms.on: ld a,c ; inc a and (ix+o.mask) ms.wait: cp (ix+o.out_idx) ; jr z,ms.wait ld c,a ld a,(de) inc de ld (hl),a ld (ix+o.in_idx),c djnz ms.ol out (AVRINT5),a ex de,hl pop de pop bc pop ix ret ;-------------------------------------------------------------- msg.co: push af push hl ld (buf_char),a ld hl,buf call msg.sout pop hl pop af ret buf: db buf_end - $ - 1 ;output string length db 0AEh ; message start token db buf_end - $ - 1 ; message length db 1 ; command db 1 ; subcommand buf_char: db 0 ; pay load buf_end: ;---------------------------------------------------------------------- end