summaryrefslogtreecommitdiff
path: root/z180/msgbuf-a.180
blob: 3575569de9a764be14a985905d74bcd19ccea86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
	page	255
	.z80

	public	mrx.fifo,mtx.fifo

	public	msginit,msgi.st,msg.in,msgo.st
	public	msg.sm,msg.sout

	extrn	bufinit,hwl2phy
	extrn	fifolst

	include config.inc
	if CPU_Z180
	include	z180reg.inc
	endif

;--------------------------------------------------------------

	dseg

	mkbuf	mtx.fifo_id, mtx.fifo, mtx.fifo_len
	mkbuf	mrx.fifo_id, mrx.fifo, mrx.fifo_len

;--------------------------------------------------------------

	cseg

;
; Init buffer
;

msginit:
	ld	a,(043h)
;TODO: value should be 0
	ld	ix,mtx.fifo
	call	bufinit
	push	ix
	pop	hl
	call	hwl2phy
	ld	(040h),hl
	ld	(040h+2),a
	ld	a,0ffh
	ld	(043h),a
	out	(AVRINT5),a
wait:
	ld	a,(043h)
	or	a
	jr	nz,wait

	ld	ix,mrx.fifo
	jp	bufinit

;--------------------------------------------------------------

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

;--------------------------------------------------------------

    if 0

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

    endif

;--------------------------------------------------------------

   if 0

msg.out:
	push	ix
	ld	ix,mtx.fifo		;

buf.put:
	push	bc
	ld	c,(ix+o.in_idx)		;
	ld	b,0
	push	ix			;14
	add	ix,bc			;10
	ld	(ix+0),a		;15
	pop	ix			;12 / 51
	ld	b,a			; 4
	ld	a,c			;
	inc	a			;
	and	(ix+o.mask)		;
bp.wait:
	cp	(ix+o.out_idx)		;
	jr	z,bp.wait		;
	ld	(ix+o.in_idx),a		;

	ld	a,b
	out	(AVRINT5),a
	pop	bc
	pop	ix
	ret

   endif

;----------------------------------------------------------------------
;
; Put char in message buffer:
;     ix: buffer to put into
;     c:  char

buf.put:
	push	ix			;15
	push	bc			;11
	ld	a,(ix+o.in_idx)		;19
	ld	c,a			;4
	ld	b,0			;7
	add	ix,bc			;11
	pop	bc			;10
	ld	(ix),c			;7
	pop	ix			;14

	inc	a			;4
	and	(ix+o.mask)		;19	=121
bufp.wait:
	cp	(ix+o.out_idx)		;19
	jr	z,bufp.wait		;12/7
	ld	(ix+o.in_idx),a		;19

	out	(AVRINT5),a		;11
	ld	a,c			;4
	ret				;10	=191


;--------------------------------------------------------------

    if 0
    	; Works only, if buffer size < (128 - 3)
    	; 	--> mask must be 03fh or less

msg.out:
	push	ix
	ld	ix,mtx.fifo		;

buf.put:
	push	bc
   	ld	b,a			; 4
	ld	a,(ix+o.in_idx)		;14
	ld	($ + 3 + 2),a		;15
	ld	(ix+0),b		;15
	inc	a			;
	and	(ix+o.mask)		;
bp.wait:
	cp	(ix+o.out_idx)		;
	jr	z,bp.wait		;
	ld	(ix+o.in_idx),a		;

	ld	a,b
	out	(AVRINT5),a
	pop	bc
	pop	ix
	ret

    endif

;--------------------------------------------------------------
;
; (hl): data

msg.sout:
	push	ix			;14
	ld	ix,mtx.fifo		;12
	push	bc			;11
	push	de			;11
	ld	c,(hl)			; 6
	ld	b,0			; 6
	inc	hl			; 7
ms.ol:					;	\
	ld	a,low mtx.fifo		; 6
	add	a,(ix+o.in_idx)		;14
	ld	e,a			; 4
	ld	a,high mtx.fifo		; 6
	adc	a,b			; 4
	ld	d,a			; 4

	ld	a,(ix+o.in_idx)		;14
	inc	a			; 4
	and	(ix+o.mask)		;14
ms.wait:
	cp	(ix+o.out_idx)		;14
	jr	z,ms.wait		; 6/8

	ldi				;12
	ld	(ix+o.in_idx),a		;15
	jp	pe,ms.ol		; 6/9    -- 126

	out	(AVRINT5),a		;10
	pop	de			; 9
	pop	bc			; 9
	pop	ix			;12
	ret				; 9

;--------------------------------------------------------------
;
; (hl): data

    if 0

msg.sout:
	push	ix			;14
	ld	ix,mtx.fifo		;12
	push	bc			;11
	push	de			;11
	ld	b,(hl)			; 6
	inc	hl			; 4
	ex	de,hl			; 3
ms.ol:					;	\
	push	ix			;14
	pop	hl			; 9
	ld	c,(ix+o.in_idx)		;14
	ld	a,c			; 4
	add	l			; 4
	ld	l,a			; 4
	jr	nc,ms.on		; 6/8
	inc	h			; 4
ms.on:
	ld	a,c			; 4
	inc	a			; 4
	and	(ix+o.mask)		;14
ms.wait:
	cp	(ix+o.out_idx)		;14
	jr	z,ms.wait		; 6/8
	ld	c,a			; 4
	ld	a,(de)			; 6
	inc	de			; 4
	ld	(hl),a			; 7
	ld	(ix+o.in_idx),c		;15
	djnz	ms.ol			; 7/9	-- 130

	out	(AVRINT5),a		;10
	ex	de,hl			; 3
	pop	de			; 9
	pop	bc			; 9
	pop	ix			;12
	ret				; 9

    endif

;--------------------------------------------------------------

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:

;----------------------------------------------------------------------

;----------------------------------------------------------------------
; Send message MEMORY
;
;     hl: pointer to message (netto)
;     b: msg length

msg.sm:
	push	ix
	ld	ix,mtx.fifo
	ld	c,0AEh
	call	buf.put
	ld	c,b
	call	buf.put
msm_l:
	ld	c,(hl)
	inc	hl
	call	buf.put
	djnz	msm_l
	pop	ix
	ret


	end