public bufinit public ff_empty,ff_get,ff_full,ff_put public ff_puth,ff_cnt,ff_gech public fifolst extrn msg.sm,hwl2phy maclib z180reg.inc maclib config.inc ;-------------------------------------------------------------------- dseg fifolst: rept 4 dw 0 endm ;-------------------------------------------------------------------- dseg bufinit: ld (ix+o.in_idx),0 ;reset pointers (empty fifo) ld (ix+o.out_idx),0 ld a,(ix+o.id) ld hl,fifolst ld e,a ld d,0 add hl,de add hl,de push ix pop de cp 4 jr nc,bfi_skip ld (hl),e inc hl ld (hl),d bfi_skip: ex de,hl call hwl2phy ;get phys. address of fifo ld c,a ld a,(ix+o.id) ;fifo id or a ;test if fifo 0 ret z cp 4 ret nc ; TODO: move to better place ld b,a push bc ;c: bank-addr, b: ignored push hl ;address ld c,0 push bc ;c: function, b:subf ld b,5 ld h,c ld l,c add hl,sp call msg.sm pop hl pop hl pop hl ret ;-------------------------------------------------------------- ; Check if characters in fifo ; Fifo is empty, if output index and input index are the same ff_empty: ld a,(ix+o.in_idx) ; sub (ix+o.out_idx) ; ret z or 0ffh ret ;-------------------------------------------------------------- ; get character from fifo ; destroys hl,b ; ; return: ; c,a: next character ff_get: push ix pop hl ld c,(ix+o.out_idx) ; ld b,0 add hl,bc ld a,c bg.wait: cp (ix+o.in_idx) ; jr z,bg.wait inc a and (ix+o.mask) ld c,(hl) ld (ix+o.out_idx),a ld a,c ret ;-------------------------------------------------------------- ; get character and ramaining count from fifo ; halt cpu, while buffer is empty ; destroys hl ; ; return: ; c,a: next character ; b: number of charachters in fifo ff_gech: push ix pop hl ld c,(ix+o.out_idx) ; ld b,0 add hl,bc gech.wait: ld a,(ix+o.in_idx) ; sub c jr nz,gech.cont halt jr gech.wait gech.cont: and (ix+o.mask) ; dec a ld b,a ld a,c inc a and (ix+o.mask) ld c,(hl) ld (ix+o.out_idx),a ld a,c ret ;-------------------------------------------------------------- ; Check if room in fifo ; buffer is full, if output index is one behind input index ; ; return ; a = 0 and z if buffer full ; a = ff and nz if buffer not full ff_full: ld a,(ix+o.in_idx) ; inc a ; and (ix+o.mask) ; sub (ix+o.out_idx) ; ret z or 0ffh ret ;-------------------------------------------------------------- ; put character in c in buffer ; destroys bc ff_put: push ix ; ex (sp),hl ; get buffer start address ld a,c ; ld c,(ix+o.in_idx) ; add input index ld b,0 ; add hl,bc ; ld (hl),a ; one place is allways free pop hl ; restore hl ld a,c ; bump input index inc a ; and (ix+o.mask) ; bp.wait: ; do cp (ix+o.out_idx) ; jr z,bp.wait ; while new input idx == ouput idx ld (ix+o.in_idx),a ; ret ; ;-------------------------------------------------------------- ; put character in c in buffer ; halt cpu, while buffer is is full ; destroys bc ff_puth: push ix ; ex (sp),hl ; get buffer start address ld a,c ; ld c,(ix+o.in_idx) ; add input index ld b,0 ; add hl,bc ; ld (hl),a ; one place is allways free pop hl ; restore hl ld a,c ; bump input index inc a ; and (ix+o.mask) ; jr $+3 bph.wait: ; do halt ; halt cpu cp (ix+o.out_idx) ; jr z,bph.wait ; while new input idx == ouput idx ld (ix+o.in_idx),a ; ret ; ;-------------------------------------------------------------- ; Return number of characters in fifo ; ff_cnt: ld a,(ix+o.in_idx) ; sub (ix+o.out_idx) ; and (ix+o.mask) ; ret ;-------------------------------------------------------------- end