/
ay-uart.asm
349 lines (314 loc) · 6.19 KB
/
ay-uart.asm
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
DISPLAY "UART BEGINS AT ", $
; Enable UART
; Cleaning all flags by reading UART regs
; Wastes AF and BC
uartBegin:
ld a, #07
ld bc, #fffd
out (c), a
ld a, #fc
ld b, #bf
out (c), a ; Enable read mode
ld a, #0e
ld bc, #fffd
out (c), a
ld a, #fb
ld b, #bf
out (c), a
ld b, #ff
ei
.flush
halt
djnz .flush
ret
; Blocking read one byte
uartReadBlocking:
call uartRead
push af : jr c, urb : pop af
jp uartReadBlocking
urb:
pop af
ret
readSilient:
call uartRead
ret c
jr readSilient
uartWriteByte:
push af
call putC
ld c, #fd ; prepare port addresses
ld d, #ff
ld e, #bf
ld b, d
ld a, #0e
out (c), a ; Select AY's PORT A
ld hl, (_baud)
ld de, #0002
or a
sbc hl, de
ex hl, de
pop af
cpl
scf
ld b, #0b ; Numbers of bits - 1 start, 8 data, 2 stop
di ; Hard timing starts
transmitBit:
push bc
push af
ld a, #fe
ld h, d
ld l, e
ld bc, #bffd
jp nc, transmitOne
; Transmit Zero:
and #f7
out (c), a
jr transmitNext
transmitOne:
or 8
out (c), a
jr transmitNext
transmitNext:
dec hl
ld a, h
or l
jr nz, transmitNext
nop
nop
nop
pop af
pop bc
or a
rra
djnz transmitBit
ei
ret
uartRead:
call _uartRead
ret nc
push af
call putC
pop af
scf
ret
_uartRead:
ld hl, _testByte
ld a, (hl)
and a
jr z, testSecond
inc hl
ld a, (hl)
scf
ret
testSecond:
ld hl, _isSecondByteAvail
ld a, (hl)
and a
jr z, startReadByte
ld (hl), 0
inc hl
ld a, (hl)
scf
ret
startReadByte:
di
xor a
exx
ld de, (_baud)
ld hl, (_baud)
srl h
rr l ; HL=_baud/2
or a
ld b, #FA ; Wait look length
exx
ld c, #fd
ld d, #ff
ld e, #bf
ld b, d
ld a, #0e
out (c), a
in a, (c)
or #f0 ; Input lines is 1
and #fb ; CTS force to 0
ld b, e ; B = #BF
out (c), a ; Make CTS high
ld h, a
waitStartBit:
ld b, d
in a, (c)
and #80
jr z, startBitFound
readTimeOut:
exx
dec b
exx
jr nz, waitStartBit
xor a
push af
jr readFinish
startBitFound:
in a, (c)
and #80
jr nz, readTimeOut
in a, (c)
and #80
jr nz, readTimeOut
;; Start bit found!
exx
ld bc, #fffd
ld a, #80
ex af, af
readTune:
add hl, de ; HL = 1.5 * _baud
nop
nop
nop
nop ; Fine tuning delay
bdDelay:
dec hl
ld a, h
or l
jr nz, bdDelay
in a, (c)
and #80
jp z, zeroReceived
; One received:
ex af, af
scf
rra
jr c, receivedByte
ex af, af
jp readTune
zeroReceived:
ex af, af
or a
RRA
jr c, receivedByte
ex af, af
jp readTune
receivedByte:
scf
push af
exx
readFinish:
ld a, h
or #04
ld b, e
out (c), a
exx
ld h, d
ld l, e
ld bc, #0007
or a
sbc hl, bc
delayForStopBit:
dec hl
ld a, h
or l
jr nz, delayForStopBit
ld bc, #fffd
add hl, de
add hl, de
add hl, de
waitStartBitSecondByte:
in a, (c)
and #80
jr z, secondStartBitFound
dec hl
ld a, h
or l
jr nz, waitStartBitSecondByte
; No second byte
pop af
ei
ret
secondStartBitFound:
in a, (c)
and #80
jr nz, waitStartBitSecondByte
ld h, d
ld l, e
ld bc, #0002
srl h
rr l
or a
sbc hl, bc
ld bc, #fffd
ld a, #80
ex af, af
secondByteTune:
nop
nop
nop
nop
add hl, de
secondDelay:
dec hl
ld a, h
or l
jr nz, secondDelay
in a, (c)
and #80
jr z, secondZeroReceived
; Second 1 received
ex af, af
scf
rra
jr c, secondByteFinished
ex af, af
jp secondByteTune
secondZeroReceived:
ex af, af
or a
rra
jr c, secondByteFinished
ex af, af
jp secondByteTune
secondByteFinished:
ld hl, _isSecondByteAvail
ld (hl), 1
inc hl
ld (hl), a
pop af
ei
ret
dataSequence:
db #f6, #fe, #f6, #fe, #fe, #f6, #f6, #f6, #f6, #fe
db #f6, #f6, #fe, #f6, #fe, #f6, #f6, #f6, #f6, #fe
db #f6, #fe, #f6, #f6, #f6, #f6, #f6, #fe, #f6, #fe
db #f6, #f6, #f6, #fe, #f6, #fe, #f6, #fe, #f6, #fe
db #f6, #fe, #fe, #f6, #fe, #f6, #fe, #f6, #f6, #fe
db #f6, #fe, #f6, #fe, #f6, #fe, #f6, #fe, #f6, #fe
db #f6, #fe, #f6, #f6, #f6, #f6, #f6, #fe, #f6, #fe
db #f6, #f6, #fe, #f6, #f6, #fe, #f6, #fe, #f6, #fe
db #f6, #f6, #f6, #fe, #f6, #fe, #f6, #fe, #f6, #fe
db #f6, #fe, #fe, #fe, #fe, #fe, #f6, #fe, #f6, #fe
db #f6, #f6, #f6, #fe, #f6, #f6, #f6, #fe, #f6, #fe
db #f6, #fe, #f6, #fe, #f6, #f6, #f6, #fe, #f6, #fe
db #f6, #f6, #fe, #fe, #f6, #f6, #f6, #fe, #f6, #fe
db #f6, #fe, #f6, #fe, #fe, #fe, #fe, #f6, #f6, #fe
db #f6, #fe, #f6, #f6, #fe, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #fe, #fe, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #f6, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #f6, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #fe, #fe, #f6, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #f6, #fe, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #fe, #fe, #f6, #fe, #f6, #f6, #fe
db #f6, #fe, #f6, #f6, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #fe, #fe, #f6, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #f6, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #f6, #f6, #fe, #fe, #f6, #fe, #f6, #f6, #fe
db #f6, #f6, #fe, #f6, #f6, #fe, #fe, #f6, #f6, #fe
db #f6, #fe, #f6, #fe, #fe, #f6, #f6, #f6, #f6, #fe
db #f6, #f6, #fe, #f6, #fe, #f6, #f6, #f6, #f6, #fe
dataSize = $ - dataSequence
; Total bytes 28
setSpeed:
ld hl, dataSequence : ld bc, #bffd
dup dataSize
outi : ld bc, #bffd : nop
edup
ret
_baud dw 11 ; 54 - 2400 --- 25 - 4800 --- 11 - 9600
_isSecondByteAvail dw #0
_testByte dw #0