]> cloudbase.mooo.com Git - z180-stamp-cpm3.git/blob - cbios/fifo.180
ca2924a7df3651d4617cc7006f146c5624d7573e
[z180-stamp-cpm3.git] / cbios / fifo.180
1
2 public bufinit
3 public ff_empty,ff_get,ff_full,ff_put
4 public ff_puth,ff_cnt,ff_gech
5
6 public fifolst
7
8 extrn msg.sm,hwl2phy
9
10 maclib z180reg.inc
11 maclib config.inc
12
13
14 ;--------------------------------------------------------------------
15
16 dseg
17
18 fifolst:
19 rept 4
20 dw 0
21 endm
22
23 ;--------------------------------------------------------------------
24
25 dseg
26
27 bufinit:
28 ld (ix+o.in_idx),0 ;reset pointers (empty fifo)
29 ld (ix+o.out_idx),0
30 ld a,(ix+o.id)
31 ld hl,fifolst
32 ld e,a
33 ld d,0
34 add hl,de
35 add hl,de
36 push ix
37 pop de
38 cp 4
39 jr nc,bfi_skip
40
41 ld (hl),e
42 inc hl
43 ld (hl),d
44
45 bfi_skip:
46 ex de,hl
47 call hwl2phy ;get phys. address of fifo
48 ld c,a
49 ld a,(ix+o.id) ;fifo id
50 or a ;test if fifo 0
51 ret z
52
53 cp 4
54 ret nc
55
56 ; TODO: move to better place
57
58 ld b,a
59 push bc ;c: bank-addr, b: ignored
60 push hl ;address
61 ld c,0
62 push bc ;c: function, b:subf
63 ld b,5
64 ld h,c
65 ld l,c
66 add hl,sp
67 call msg.sm
68 pop hl
69 pop hl
70 pop hl
71 ret
72
73 ;--------------------------------------------------------------
74 ; Check if characters in fifo
75 ; Fifo is empty, if output index and input index are the same
76
77 ff_empty:
78 ld a,(ix+o.in_idx) ;
79 sub (ix+o.out_idx) ;
80 ret z
81 or 0ffh
82 ret
83
84 ;--------------------------------------------------------------
85 ; get character from fifo
86 ; destroys hl,b
87 ;
88 ; return:
89 ; c,a: next character
90
91 ff_get:
92 push ix
93 pop hl
94 ld c,(ix+o.out_idx) ;
95 ld b,0
96 add hl,bc
97 ld a,c
98 bg.wait:
99 cp (ix+o.in_idx) ;
100 jr z,bg.wait
101 inc a
102 and (ix+o.mask)
103 ld c,(hl)
104 ld (ix+o.out_idx),a
105 ld a,c
106 ret
107
108 ;--------------------------------------------------------------
109 ; get character and ramaining count from fifo
110 ; halt cpu, while buffer is empty
111 ; destroys hl
112 ;
113 ; return:
114 ; c,a: next character
115 ; b: number of charachters in fifo
116
117 ff_gech:
118 push ix
119 pop hl
120 ld c,(ix+o.out_idx) ;
121 ld b,0
122 add hl,bc
123 jr $+3
124 gech.wait:
125 halt
126 ld a,(ix+o.in_idx) ;
127 sub c
128 jr z,gech.wait
129 jr nc,$+5
130 adc (ix+o.mask) ; mask+carry == buffer size
131 dec a
132 ld b,a
133 ld a,c
134 inc a
135 and (ix+o.mask)
136 ld c,(hl)
137 ld (ix+o.out_idx),a
138 ld a,c
139 ret
140
141 ;--------------------------------------------------------------
142 ; Check if room in fifo
143 ; buffer is full, if output index is one behind input index
144 ;
145 ; return
146 ; a = 0 and z if buffer full
147 ; a = ff and nz if buffer not full
148
149 ff_full:
150 ld a,(ix+o.in_idx) ;
151 inc a ;
152 and (ix+o.mask) ;
153 sub (ix+o.out_idx) ;
154 ret z
155 or 0ffh
156 ret
157
158
159 ;--------------------------------------------------------------
160 ; put character in c in buffer
161 ; destroys hl, bc
162 ; returns output char in a
163
164 ff_put:
165 push ix ;
166 pop hl ; get buffer start address
167
168 ld a,c ;
169 ld c,(ix+o.in_idx) ; add input index
170 ld b,0 ;
171 add hl,bc ;
172 ld (hl),a ; one place is allways free
173 ld b,a ;
174
175 ld a,c ; bump input index
176 inc a ;
177 and (ix+o.mask) ;
178 bp.wait: ; do
179 cp (ix+o.out_idx) ;
180 jr z,bp.wait ; while new input idx == ouput idx
181 ld (ix+o.in_idx),a ;
182 ld a,b ;
183 ret ;
184
185 ;--------------------------------------------------------------
186 ; put character in c in buffer
187 ; halt cpu, while buffer is is full
188 ; destroys hl, bc
189 ; returns output char in a
190
191 ff_puth:
192 push ix ;
193 pop hl ; get buffer start address
194
195 ld a,c ;
196 ld c,(ix+o.in_idx) ; add input index
197 ld b,0 ;
198 add hl,bc ;
199 ld (hl),a ; one place is allways free
200 ld b,a ;
201
202 ld a,c ; bump input index
203 inc a ;
204 and (ix+o.mask) ;
205 jr $+3
206 bph.wait: ; do
207 halt ; halt cpu
208 cp (ix+o.out_idx) ;
209 jr z,bph.wait ; while new input idx == ouput idx
210 ld (ix+o.in_idx),a ;
211 ld a,b ;
212 ret ;
213
214 ;--------------------------------------------------------------
215 ; Return number of characters in fifo
216 ;
217
218 ff_cnt:
219 ld a,(ix+o.in_idx) ;
220 sub (ix+o.out_idx) ;
221 ret nc
222 adc (ix+o.mask) ; mask+carry == buffer size
223 ret
224
225 ;--------------------------------------------------------------
226
227 end