-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIOSerial.asm
504 lines (450 loc) · 10.9 KB
/
IOSerial.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
; ROM code
; Targets:
; TEC-1,TEC-1D,TEC-1F,Southern Cross,RC2014
; Memory Map: 2k ROM/RAM, 8K ROM/RAM, RC2014
; Serial: Bit Bang, 6850 ACIA
.if TEC_1
.if BITBANG
; bit bang baud rate constants @ 4MHz
B300: .EQU 0220H
B1200: .EQU 0080H
B2400: .EQU 003FH
B4800: .EQU 001BH
B9600: .EQU 000BH
.else ;6850
;
; 6850 ACIA registers
;----------------------
CONTROL .EQU $80 ;(write)
STATUS .EQU $80 ;(read)
TDR .EQU $81 ;(write)
RDR .EQU $81 ;(read)
;
; control register bits
;----------------------
;
;clock divisor
;
MRESET .EQU $03 ;master reset the ACIA
; DIV_0 .EQU $00 ;CLOCK/1
; DIV_16 .EQU $01 ;CLOCK/16
DIV_64 .EQU $02 ;CLOCK/64
;
; format select
;
F7E2 .EQU $00 ;7 data bits, EVEN parity, 2 stop bits (1+7+1+2= 11 bits)
F7O2 .EQU $04 ;7 data bits, ODD parity, 2 stop bits (1+7+1+2= 11 bits)
F7E1 .EQU $08 ;7 data bits, EVEN parity, 1 stop bit (1+7+1+1= 10 bits)
F7O1 .EQU $0C ;7 data bits, ODD parity, 1 stop bit (1+7+1+1= 10 bits)
F8N2 .EQU $10 ;8 data bits, NO parity, 2 stop bits (1+8+0+2= 11 bits)
F8N1 .EQU $14 ;8 data bits, NO parity, 1 stop bit (1+8+0+1= 10 bits)
F8E1 .EQU $18 ;8 data bits, EVEN parity, 1 stop bit (1+8+1+1= 11 bits)
F8O1 .EQU $1C ;8 data bits, ODD parity,1 stop bit (1+8+1+1= 11 bits)
;
; transmitter control
;
RTSLID .EQU $00 ;RTS LOW, transmit interrupt disabled
RTSLIE .EQU $20 ;RTS LOW, transmit interrupt enabled
RTSHID .EQU $40 ;RTS HIGH, transmit interrupt disabled
RTSLIDB .EQU $60 ;RTS LOW, transmit interrupt disabled and 'break' transmitted
;
; receiver interrupt
;
RIE .EQU $80 ;receiver interrupt enabled
;
; status register bits
;---------------------
RDRF .EQU 0 ;receive data register full
TDRE .EQU 1 ;transmit data register empty
DCD .EQU 2 ;data carrier detect
CTS .EQU 3 ;clear to send
FE .EQU 4 ;framing error
OVRN .EQU 5 ;overrun
PE .EQU 6 ;parity error
IRQ .EQU 7 ;interrupt request
.endif
.endif
; I/O port addresses
.if TEC_1
KEYBUF: .EQU 00H ;MM74C923N KEYBOARD ENCODER
SCAN: .EQU 01H ;DISPLAY SCAN LATCH
DISPLY: .EQU 02H ;DISPLAY LATCH
PORT3: .EQU 03H ;ST3 (8X8), STROBE (RELAY BOARD) DATLATCH (DAT BOARD)
PORT4: .EQU 04H ;ST4 (8X8), LCD 'E' (DAT BOARD)
PORT5: .EQU 05H
PORT6: .EQU 06H
PORT7: .EQU 07H ;ENABLE/DISABLE SINGLE STEPPER (IF INSTALLED)
.else ;SC
IO0: .EQU 80H ;IO PORT 0
IO1: .EQU 81H ;IO PORT 1
IO2: .EQU 82H ;IO PORT 2
IO3: .EQU 83H ;IO PORT 3
DISPLY: .EQU 84H ;DISPLAY LATCH
SCAN: .EQU 85H ;DISPLAY SCAN LATCH
KEYBUF: .EQU 86H ;KEYBOARD BUFFER
IO7: .EQU 87H ;ENABLE/DISABLE SINGLE STEPPER (IF INSTALLED)
.endif
; ASCII codes
ESC: .EQU 1BH
CR: .EQU 0DH
LF: .EQU 0AH
.ORG ROMSTART
;reset
RSTVEC:
JP RESET
rst1:
.ORG ROMSTART+$08
ld l,1
jp ISR
rst2:
.ORG ROMSTART+$10
ld l,2
jp ISR
rst3:
.ORG ROMSTART+$18
ld l,3
jp ISR
rst4:
.ORG ROMSTART+$20
ld l,4
jp ISR
rst5:
.ORG ROMSTART+$28
ld l,5
jp ISR
rst6:
.ORG ROMSTART+$30
ld l,6
jp ISR
;RST 7 Interrupt
.ORG ROMSTART+$38
.if BITBANG
ld l,7
jp ISR
.else
ret
.endif
.ORG ROMSTART+$40
;hexadecimal to 7 segment display code table
.if TEC_1
sevensegment:
.DB 0EBH,28H,0CDH,0ADH ;0,1,2,3
.DB 2EH,0A7H,0E7H,29H ;4,5,6,7
.DB 0EFH,2FH,6FH,0E6H ;8,9,A,B
.DB 0C3H,0ECH,0C7H,47H ;C,D,E,F
.else ;SC
sevensegment:
.DB 3FH,06H,5BH,4FH ;0,1,2,3
.DB 66H,6DH,7DH,07H ;4,5,6,7
.DB 7FH,6FH,77H,7CH ;8,9,A,B
.DB 39H,5EH,79H,71H ;C,D,E,F
.endif
;---------------
; BIT TIME DELAY
;---------------
;DELAY FOR ONE SERIAL BIT TIME
;ENTRY : HL = DELAY TIME
; NO REGISTERS MODIFIED
;
PWRUP:
LD hl,$2000
BITIME:
PUSH HL
PUSH DE
LD DE,0001H
BITIM1:
SBC HL,DE
JP NC,BITIM1
POP DE
POP HL
IntRet:
RET
;RST 8 Non Maskable Interrupt
.ORG ROMSTART+$66
; PUSH HL
; LD HL,(NMIVEC)
; JP (HL)
ld l,8
jp ISR
.if BITBANG
;------------------------
; SERIAL TRANSMIT ROUTINE
;------------------------
;TRANSMIT BYTE SERIALLY ON DOUT
;
; ENTRY : A = BYTE TO TRANSMIT
; EXIT : NO REGISTERS MODIFIED
;
TxChar:
TXDATA:
PUSH AF
PUSH BC
PUSH HL
LD HL,(BAUD)
LD C,A
;
; TRANSMIT START BIT
;
XOR A
OUT (SCAN),A
CALL BITIME
;
; TRANSMIT DATA
;
LD B,08H
RRC C
NXTBIT:
RRC C ;SHIFT BITS TO D6,
LD A,C ;LSB FIRST AND OUTPUT
AND 40H ;THEM FOR ONE BIT TIME.
OUT (SCAN),A
CALL BITIME
DJNZ NXTBIT
;
; SEND STOP BITS
;
LD A,40H
OUT (SCAN),A
CALL BITIME
CALL BITIME
POP HL
POP BC
POP AF
RET
;-----------------------
; SERIAL RECEIVE ROUTINE
;-----------------------
;RECEIVE SERIAL BYTE FROM DIN
;
; ENTRY : NONE
; EXIT : A= RECEIVED BYTE IF CARRY CLEAR
;
; REGISTERS MODIFIED A AND F
;
RxChar:
RXDATA:
PUSH BC
PUSH HL
;
; WAIT FOR START BIT
;
RXDAT1: IN A,(KEYBUF)
BIT 7,A
JR NZ,RXDAT1 ;NO START BIT
;
; DETECTED START BIT
;
LD HL,(BAUD)
SRL H
RR L ;DELAY FOR HALF BIT TIME
CALL BITIME
IN A,(KEYBUF)
BIT 7,A
JR NZ,RXDAT1 ;START BIT NOT VALID
;
; DETECTED VALID START BIT,READ IN DATA
;
LD B,08H
RXDAT2:
LD HL,(BAUD)
CALL BITIME ;DELAY ONE BIT TIME
IN A,(KEYBUF)
RL A
RR C ;SHIFT BIT INTO DATA REG
DJNZ RXDAT2
LD A,C
OR A ;CLEAR CARRY FLAG
POP HL
POP BC
RET
.else ;6850
;
; transmit a character in a
;--------------------------
TXDATA:
TxChar:
push bc
ld b,a ;save the character for later
TxChar1:
in a,(STATUS) ;get the ACIA status
bit 1,a
; bit TDRE,a ;is the TDRE bit high?
jr z,TxChar1 ;no, the TDR is not empty
ld a,b ;yes, get the character
out (TDR),a ;and put it in the TDR
pop bc
ret
;
; receive a character in a
;---------------------------------
RXDATA:
RxChar:
in a,(STATUS) ;get the ACIA status
bit 0,a
; bit RDRF,a ;is the RDRF bit high?
jr z,RxChar ;no, the RDR is empty
in a,(RDR) ;yes, read the received char
ret
.endif
.if LOADER
; .ORG ROMSTART + $0700
;-----------------------
; RECEIVE INTEL HEX FILE
;-----------------------
INTELH:
LD IX,BUF
;
; WAIT FOR RECORD MARK
;
INTEL1:
XOR A
LD (IX+3),A ;CLEAR CHECKSUM
CALL RXDATA ;WAIT FOR THE RECORD MARK
CP ':' ;TO BE TRANSMITTED
JR NZ,INTEL1 ;NOT RECORD MARK
;
; GET RECORD LENGTH
;
CALL GETBYT
LD (IX+0),A ;NUMBER OF DATA BYTES
;
; GET ADDRESS FIELD
;
CALL GETBYT
LD (IX+2),A ;LOAD ADDRESS HIGH BYTE
CALL GETBYT
LD (IX+1),A ;LOAD ADDRESS LOW BYTE
;
; GET RECORD TYPE
;
CALL GETBYT
JR NZ,INTEL4 ;END OF FILE RECORD
;
; READ IN THE DATA
;
LD B,(IX+0) ;NUMBER OF DATA BYTES
LD H,(IX+2) ;LOAD ADDRESS HIGH BYTE
LD L,(IX+1) ;LOAD ADDRESS LOW BYTE
INTEL2:
CALL GETBYT ;GET DATA BYTE
LD (HL),A ;STORE DATA BYTE
INC HL
DJNZ INTEL2 ;LOAD MORE BYTES
;
; GET CHECKSUM AND COMPARE
;
LD A,(IX+3) ;CONVERT CHECKSUM TO
NEG ;TWO'S COMPLEMENT
LD (IX+4),A ;SAVE COMPUTED CHECKSUM
CALL GETBYT
LD (IX+3),A ;SAVE RECORD CHECKSUM
CP (IX+4) ;COMPARE CHECKSUM
JR Z,INTEL1 ;CHECKSUM OK,NEXT RECORD
RET ;NZ=CHECKSUM ERROR
;
; END OF FILE RECORD
;
INTEL4:
LD A,(IX+3) ;CONVERT CHECKSUM TO
NEG ;TWO'S COMPLEMENT
LD (IX+4),A ;SAVE COMPUTED CHECKSUM
CALL GETBYT
LD (IX+3),A ;SAVE EOF CHECKSUM
CP (IX+4) ;COMPARE CHECKSUM
RET ;NZ=CHECKSUM ERROR
;--------------------------
; GET BYTE FROM SERIAL PORT
;--------------------------
GETBYT:
PUSH BC
CALL RXDATA
BIT 6,A
JR Z,GETBT1
ADD A,09H
GETBT1:
AND 0FH
SLA A
SLA A
SLA A
SLA A
LD C,A
;
; GET LOW NYBBLE
;
CALL RXDATA
BIT 6,A
JR Z,GETBT2
ADD A,09H
GETBT2 AND 0FH
OR C
LD B,A
ADD A,(IX+3)
LD (IX+3),A ;ADD TO CHECKSUM
LD A,B
AND A ;CLEAR CARRY
POP BC
RET
.endif
; in this example code just wait for an INTEL Hex file download
;just going to send a char to let you know I'm here
.if LOADER
Load:
ld a,'L' ; L for load
call TxChar
call INTELH
jp z,RAMSTART ;assume the downloaded code starts here
ld a,'0' ;0 is false
call TxChar
jr load ;if at first you don't succeed...
.endif
getchar:
LD HL,(GETCVEC)
JP (HL)
putchar:
PUSH HL
LD HL,(PUTCVEC)
EX (SP),HL
RET
ISR:
ld h,0
ld (vIntID),hl
call enter
.cstr "Z"
ret
RESET:
ld SP,stack
LD HL,IntRet
LD (RST08),HL
LD (RST10),HL
LD (RST18),HL
LD (RST20),HL
LD (RST28),HL
LD (RST30),HL
LD (INTVEC),HL
LD (NMIVEC),HL
LD HL,RXDATA
LD (GETCVEC),HL
LD HL,TXDATA
LD (PUTCVEC),HL
.if TEC_1
.if BITBANG = 0
ld a,MRESET
out (CONTROL),a ;reset the ACIA
.endif
.endif
call PWRUP
IM 1
EI
.if TEC_1
.if BITBANG
;inline serial initialisation
LD A,$40
LD C,SCAN
OUT (C),A
LD HL,B4800
LD (BAUD),HL
.else ;6850
ld a,RTSLID+F8N2+DIV_64
out (CONTROL),a ;initialise ACIA 8 bit word, No parity 2 stop divide by 64 for 115200 baud
.endif
.endif