summaryrefslogtreecommitdiffstats
path: root/sw/z80/drivers/build/usart.asm
blob: fb65d62f5e1b491ed35b2b6d34269d32ca65755d (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
;--------------------------------------------------------
; File Created by SDCC : free open source ANSI-C Compiler
; Version 3.6.0 #9615 (Linux)
;--------------------------------------------------------
	.module usart
	.optsdcc -mz80
	
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
	.globl _usart_word_length
	.globl _usart_set_baudrate
	.globl _usart_set_parity
	.globl _usart_set_stop_bits
	.globl _usart_set_autoflow
	.globl _usart_init
	.globl _usart_transmit
	.globl _usart_receive
	.globl _usart_write
	.globl _usart_read
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
;--------------------------------------------------------
; ram data
;--------------------------------------------------------
	.area _DATA
;--------------------------------------------------------
; ram data
;--------------------------------------------------------
	.area _INITIALIZED
__usart:
	.ds 2
;--------------------------------------------------------
; absolute external ram data
;--------------------------------------------------------
	.area _DABS (ABS)
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
	.area _HOME
	.area _GSINIT
	.area _GSFINAL
	.area _GSINIT
;--------------------------------------------------------
; Home
;--------------------------------------------------------
	.area _HOME
	.area _HOME
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area _CODE
;usart.c:5: void usart_set_baudrate(uint16_t baudrate)
;	---------------------------------
; Function usart_set_baudrate
; ---------------------------------
_usart_set_baudrate::
	push	ix
	ld	ix,#0
	add	ix,sp
;usart.c:8: _usart->LCR.divisor_latch_access = 1;
	ld	hl,(__usart)
	ld	bc,#0x0004
	add	hl,bc
	ld	a,(hl)
	or	a,#0x80
	ld	(hl),a
;usart.c:9: _usart->buffer = 0x00FF & baudrate; // LSBs
	ld	hl,(__usart)
	ld	c,4 (ix)
	ld	(hl),c
;usart.c:10: memcpy(&_usart->IER, &(baudrate >>8), 1);
	ld	de,(__usart)
	inc	de
	ld	l,5 (ix)
	ld	h,#0x00
	ld	a, (hl)
	ld	(de), a
;usart.c:12: _usart->LCR.divisor_latch_access = 0;
	ld	hl,(__usart)
	ld	bc,#0x0004
	add	hl,bc
	ld	a,(hl)
	and	a,#0x7f
	ld	(hl),a
	pop	ix
	ret
;usart.c:15: void usart_set_parity(int mode)
;	---------------------------------
; Function usart_set_parity
; ---------------------------------
_usart_set_parity::
;usart.c:18: _usart->LCR.even_parity = 1;
	ld	hl,(__usart)
	ld	bc,#0x0004
	add	hl,bc
;usart.c:17: if (mode == USART_PARITY_EVEN) {
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	dec	a
	jr	NZ,00104$
	ld	a,1 (iy)
	or	a, a
	jr	NZ,00104$
;usart.c:18: _usart->LCR.even_parity = 1;
	ld	a,(hl)
	or	a,#0x10
	ld	(hl),a
	jr	00105$
00104$:
;usart.c:20: else if (mode == USART_PARITY_ODD) {
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	sub	a, #0x02
	jr	NZ,00105$
	ld	a,1 (iy)
	or	a, a
	jr	NZ,00105$
;usart.c:21: _usart->LCR.even_parity = 0;
	ld	a,(hl)
	and	a,#0xef
	ld	(hl),a
00105$:
;usart.c:24: _usart->LCR.parity = (mode == USART_PARITY_NONE) ? 0 : 1;
	ld	hl,(__usart)
	ld	bc,#0x0004
	add	hl,bc
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	rlca
	rlca
	rlca
	and	a,#0xf8
	and	a,#0x08
	ld	c,a
	ld	a,(hl)
	and	a,#0xf7
	or	a,c
	ld	(hl),a
	ret
;usart.c:27: void usart_set_stop_bits(int count)
;	---------------------------------
; Function usart_set_stop_bits
; ---------------------------------
_usart_set_stop_bits::
;usart.c:29: _usart->LCR.stop_bits = (count == USART_STOP_BITS_1) ? 0 : 1;
	ld	bc,(__usart)
	inc	bc
	inc	bc
	inc	bc
	inc	bc
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	sub	a, #0x0a
	jr	NZ,00103$
	ld	a,1 (iy)
	or	a, a
	jr	NZ,00103$
	ld	a,#0x01
	jr	00104$
00103$:
	xor	a,a
00104$:
	xor	a, #0x01
	add	a, a
	add	a, a
	and	a,#0x04
	ld	l,a
	ld	a,(bc)
	and	a,#0xfb
	or	a,l
	ld	(bc),a
	ret
;usart.c:32: void usart_word_length(int length)
;	---------------------------------
; Function usart_word_length
; ---------------------------------
_usart_word_length::
;usart.c:34: _usart->LCR.word_length = length;
	ld	hl,(__usart)
	ld	bc,#0x0004
	add	hl,bc
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	and	a,#0x03
	ld	c,a
	ld	a,(hl)
	and	a,#0xfc
	or	a,c
	ld	(hl),a
	ret
;usart.c:37: void usart_set_autoflow(int mode)
;	---------------------------------
; Function usart_set_autoflow
; ---------------------------------
_usart_set_autoflow::
;usart.c:39: _usart->MCR.autoflow = (mode == USART_AUTOFLOW_OFF) ? 0 : 1;
	ld	hl,(__usart)
	ld	bc,#0x000c
	add	hl,bc
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	and	a,#0x01
	ld	c,a
	ld	a,(hl)
	and	a,#0xfe
	or	a,c
	ld	(hl),a
;usart.c:40: _usart->MCR.data_terminal_ready = (mode == USART_AUTOFLOW_ALL);
	ld	hl,(__usart)
	ld	bc,#0x0005
	add	hl,bc
	ld	a,0 (iy)
	sub	a, #0x03
	jr	NZ,00103$
	ld	a,1 (iy)
	or	a, a
	jr	NZ,00103$
	ld	a,#0x01
	jr	00104$
00103$:
	xor	a,a
00104$:
	and	a,#0x01
	ld	c,a
	ld	a,(hl)
	and	a,#0xfe
	or	a,c
	ld	(hl),a
	ret
;usart.c:43: inline void usart_init(uint16_t baudrate, int parity, int stop_bits)
;	---------------------------------
; Function usart_init
; ---------------------------------
_usart_init::
;usart.c:45: usart_set_baudrate(baudrate);
	pop	bc
	pop	hl
	push	hl
	push	bc
	push	hl
	call	_usart_set_baudrate
	pop	af
;usart.c:46: usart_set_parity(parity);
	ld	hl, #4
	add	hl, sp
	ld	c, (hl)
	inc	hl
	ld	b, (hl)
	push	bc
	call	_usart_set_parity
	pop	af
;usart.c:47: usart_set_stop_bits(stop_bits);
	ld	hl, #6
	add	hl, sp
	ld	c, (hl)
	inc	hl
	ld	b, (hl)
	push	bc
	call	_usart_set_stop_bits
;usart.c:48: usart_set_autoflow(USART_AUTOFLOW_OFF);
	ld	hl, #0x0000
	ex	(sp),hl
	call	_usart_set_autoflow
	pop	af
	ret
;usart.c:51: void usart_transmit(uint8_t data)
;	---------------------------------
; Function usart_transmit
; ---------------------------------
_usart_transmit::
;usart.c:53: _usart->buffer = data;
	ld	hl,(__usart)
	ld	iy,#2
	add	iy,sp
	ld	a,0 (iy)
	ld	(hl),a
;usart.c:54: while (_usart->LSR.transmitter_holder_empty == 0); // wait
00101$:
	ld	hl,(__usart)
	ld	bc,#0x000d
	add	hl,bc
	ld	a,(hl)
	rlca
	rlca
	rlca
	jr	NC,00101$
	ret
;usart.c:57: uint8_t usart_receive()
;	---------------------------------
; Function usart_receive
; ---------------------------------
_usart_receive::
;usart.c:59: return _usart->buffer;
	ld	hl,(__usart)
	ld	l,(hl)
	ret
;usart.c:62: int usart_write(uint8_t *data, size_t size)
;	---------------------------------
; Function usart_write
; ---------------------------------
_usart_write::
	push	ix
	ld	ix,#0
	add	ix,sp
	push	af
;usart.c:64: uint8_t *dp = data;
	ld	a,4 (ix)
	ld	-2 (ix),a
	ld	a,5 (ix)
	ld	-1 (ix),a
;usart.c:66: while (size--) {
	ld	c,6 (ix)
	ld	b,7 (ix)
00104$:
	ld	e, c
	ld	d, b
	dec	bc
	ld	a,d
	or	a,e
	jr	Z,00106$
;usart.c:67: _usart->buffer = *(dp++);
	ld	de,(__usart)
	pop	hl
	push	hl
	ld	a,(hl)
	inc	-2 (ix)
	jr	NZ,00126$
	inc	-1 (ix)
00126$:
	ld	(de),a
;usart.c:68: while (_usart->LSR.transmitter_empty);
00101$:
	ld	hl,(__usart)
	ld	de,#0x000d
	add	hl,de
	ld	a,(hl)
	rlca
	rlca
	jr	NC,00104$
	jr	00101$
00106$:
;usart.c:72: return size;
	ld	l, c
	ld	h, b
	ld	sp, ix
	pop	ix
	ret
;usart.c:75: int usart_read(uint8_t *buffer, size_t count)
;	---------------------------------
; Function usart_read
; ---------------------------------
_usart_read::
	push	ix
	ld	ix,#0
	add	ix,sp
	ld	hl,#-6
	add	hl,sp
	ld	sp,hl
;usart.c:77: uint8_t *bp = buffer;
	ld	a,4 (ix)
	ld	-4 (ix),a
	ld	a,5 (ix)
	ld	-3 (ix),a
;usart.c:80: while (count--) {
	ld	c,6 (ix)
	ld	b,7 (ix)
	ld	hl,#0x0000
	ex	(sp), hl
00105$:
	ld	e, c
	ld	d, b
	dec	bc
	ld	a,d
	or	a,e
	jr	Z,00107$
;usart.c:81: *(bp++) = _usart->buffer;
	ld	hl,(__usart)
	ld	e,(hl)
	ld	l,-4 (ix)
	ld	h,-3 (ix)
	ld	(hl),e
	inc	-4 (ix)
	jr	NZ,00123$
	inc	-3 (ix)
00123$:
	ld	de,(__usart)
;usart.c:83: if (_usart->LSR.framing_error || _usart->LSR.parity_error) {
	ld	hl,#0x000d
	add	hl,de
	ld	-2 (ix),l
	ld	-1 (ix),h
	ld	l,-2 (ix)
	ld	h,-1 (ix)
	ld	a,(hl)
	rrca
	rrca
	rrca
	and	a,#0x01
	jr	NZ,00101$
	ld	l,-2 (ix)
	ld	h,-1 (ix)
	ld	a,(hl)
	rrca
	rrca
	and	a,#0x01
	jr	Z,00102$
00101$:
;usart.c:84: bp--; // delete last byte (?)
	ld	l,-4 (ix)
	ld	h,-3 (ix)
	dec	hl
	ld	-4 (ix),l
	ld	-3 (ix),h
	jr	00105$
00102$:
;usart.c:86: read_count++;
	inc	-6 (ix)
	jr	NZ,00105$
	inc	-5 (ix)
	jr	00105$
00107$:
;usart.c:90: return read_count;
	pop	hl
	push	hl
	ld	sp, ix
	pop	ix
	ret
	.area _CODE
	.area _INITIALIZER
__xinit___usart:
	.dw #0x4000
	.area _CABS (ABS)