2 ; Serial driver for the C64 using a Swiftlink or Turbo-232 cartridge.
4 ; Ullrich von Bassewitz, 2003-04-18
6 ; The driver is based on the cc65 rs232 module, which in turn is based on
7 ; Craig Bruce device driver for the Switftlink/Turbo-232.
9 ; SwiftLink/Turbo-232 v0.90 device driver, by Craig Bruce, 14-Apr-1998.
11 ; This software is Public Domain. It is in Buddy assembler format.
13 ; This device driver uses the SwiftLink RS-232 Serial Cartridge, available from
14 ; Creative Micro Designs, Inc, and also supports the extensions of the Turbo232
15 ; Serial Cartridge. Both devices are based on the 6551 ACIA chip. It also
16 ; supports the "hacked" SwiftLink with a 1.8432 MHz crystal.
18 ; The code assumes that the kernal + I/O are in context. On the C128, call
19 ; it from Bank 15. On the C64, don't flip out the Kernal unless a suitable
20 ; NMI catcher is put into the RAM under then Kernal. For the SuperCPU, the
21 ; interrupt handling assumes that the 65816 is in 6502-emulation mode.
24 .include "zeropage.inc"
25 .include "ser-kernel.inc"
26 .include "ser-error.inc"
30 ; ------------------------------------------------------------------------
31 ; Header. Includes jump table
37 .byte $73, $65, $72 ; "ser"
38 .byte $00 ; Serial API version number
52 ;----------------------------------------------------------------------------
56 ACIA_DATA = ACIA+0 ; Data register
57 ACIA_STATUS = ACIA+1 ; Status register
58 ACIA_CMD = ACIA+2 ; Command register
59 ACIA_CTRL = ACIA+3 ; Control register
60 ACIA_CLOCK = ACIA+7 ; Turbo232 external baud-rate generator
62 ;----------------------------------------------------------------------------
68 Stopped: .res 1 ; Flow-stopped flag
70 RecvHead: .res 1 ; Head of receive buffer
71 RecvTail: .res 1 ; Tail of receive buffer
72 RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
73 SendHead: .res 1 ; Head of send buffer
74 SendTail: .res 1 ; Tail of send buffer
75 SendFreeCnt: .res 1 ; Number of bytes free in send buffer
76 BaudCode: .res 1 ; Current baud in effect
78 ; Send and receive buffers: 256 bytes each
83 NmiContinue: .byte $4c ; JMP instruction for NMI save -- continue
84 NmiSave: .res 2 ; normal NMI handler
88 ; Tables used to translate RS232 params into register values
90 BaudTable: ; bit7 = 1 means setting is invalid
91 .byte $FF ; SER_BAUD_45_5
92 .byte $FF ; SER_BAUD_50
93 .byte $FF ; SER_BAUD_75
94 .byte $FF ; SER_BAUD_110
95 .byte $FF ; SER_BAUD_134_5
96 .byte $02 ; SER_BAUD_150
97 .byte $05 ; SER_BAUD_300
98 .byte $06 ; SER_BAUD_600
99 .byte $07 ; SER_BAUD_1200
100 .byte $FF ; SER_BAUD_1800
101 .byte $08 ; SER_BAUD_2400
102 .byte $09 ; SER_BAUD_3600
103 .byte $0A ; SER_BAUD_4800
104 .byte $0B ; SER_BAUD_7200
105 .byte $0C ; SER_BAUD_9600
106 .byte $0E ; SER_BAUD_19200
107 .byte $0F ; SER_BAUD_38400
108 .byte $FF ; SER_BAUD_57600
109 .byte $FF ; SER_BAUD_115200
110 .byte $FF ; SER_BAUD_230400
113 .byte $60 ; SER_BITS_5
114 .byte $40 ; SER_BITS_6
115 .byte $20 ; SER_BITS_7
116 .byte $00 ; SER_BITS_8
119 .byte $00 ; SER_STOP_1
120 .byte $80 ; SER_STOP_2
123 .byte $00 ; SER_PAR_NONE
124 .byte $20 ; SER_PAR_ODD
125 .byte $60 ; SER_PAR_EVEN
126 .byte $A0 ; SER_PAR_MARK
127 .byte $E0 ; SER_PAR_SPACE
129 ; Delay times: 32 byte-receive times in milliseconds, or 100 max.
130 ; Formula = 320,000 / baud. Invalid values do contain $FF.
132 .byte $FF ; SER_BAUD_45_5
133 .byte $FF ; SER_BAUD_50
134 .byte $FF ; SER_BAUD_75
135 .byte $FF ; SER_BAUD_110
136 .byte $FF ; SER_BAUD_134_5
137 .byte $FF ; SER_BAUD_150
138 .byte 100 ; SER_BAUD_300
139 .byte 100 ; SER_BAUD_600
140 .byte 100 ; SER_BAUD_1200
141 .byte $FF ; SER_BAUD_1800
142 .byte 100 ; SER_BAUD_2400
143 .byte $FF ; SER_BAUD_3600
144 .byte 67 ; SER_BAUD_4800
145 .byte $FF ; SER_BAUD_7200
146 .byte 34 ; SER_BAUD_9600
147 .byte 17 ; SER_BAUD_19200
148 .byte 9 ; SER_BAUD_38400
149 .byte $FF ; SER_BAUD_57600
150 .byte $FF ; SER_BAUD_115200
151 .byte $FF ; SER_BAUD_230400
156 ;----------------------------------------------------------------------------
157 ; INSTALL routine. Is called after the driver is loaded into memory. If
158 ; possible, check if the hardware is present.
159 ; Must return an SER_ERR_xx code in a/x.
163 ; Deactivate DTR and disable 6551 interrupts
168 ; Initialize buffers & control
191 ; Done, return an error code
197 ;----------------------------------------------------------------------------
198 ; UNINSTALL routine. Is called before the driver is removed from memory.
199 ; Must return an SER_ERR_xx code in a/x.
203 ; Stop interrupts, drop DTR
208 ; Restore NMI vector and return OK
214 ;----------------------------------------------------------------------------
215 ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1.
216 ; Must return an SER_ERR_xx code in a/x.
220 ; First, check if the handshake setting is valid
222 ldy #SER_PARAMS_HANDSHAKE ; Handshake
224 cmp #SER_HS_HW ; This is all we support
227 ; Set the value for the control register, which contains stop bits, word
228 ; length and the baud rate.
230 ldy #SER_PARAMS_BAUDRATE
231 lda (ptr1),y ; Baudrate index
233 lda BaudTable,y ; Get 6551 value
234 bmi InvBaud ; Branch if rate not supported
236 sty BaudCode ; Remember baud rate index
238 ldy #SER_PARAMS_DATABITS ; Databits
245 ldy #SER_PARAMS_STOPBITS ; Stopbits
250 ora #%00010000 ; Receiver clock source = baudrate
253 ; Set the value for the command register. We remember the base value in
254 ; RtsOff, since we will have to manipulate ACIT_CMD often.
256 ldy #SER_PARAMS_PARITY ; Parity
260 ora #%00000001 ; DTR active
262 ora #%00010000 ; Enable receive interrupts
274 lda #<SER_ERR_INIT_FAILED
275 ldx #>SER_ERR_INIT_FAILED
278 ; Baud rate not available
281 lda #<SER_ERR_BAUD_UNAVAIL
282 ldx #>SER_ERR_BAUD_UNAVAIL
285 ;----------------------------------------------------------------------------
286 ; GET: Will fetch a character from the receive buffer and store it into the
287 ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is
293 ; Check for bytes to send
301 ; Check for buffer empty
303 @L1: lda RecvFreeCnt ; (25)
306 lda #<SER_ERR_NO_DATA
307 ldx #>SER_ERR_NO_DATA
310 ; Check for flow stopped & enough free: release flow control
312 @L2: ldx Stopped ; (34)
322 ; Get byte from buffer
324 @L3: ldx RecvHead ; (41)
330 txa ; Return code = 0
333 ;----------------------------------------------------------------------------
334 ; PUT: Output character in A.
335 ; Must return an error code in a/x.
350 ; Put byte into send buffer & send
354 lda #<SER_ERR_OVERFLOW ; X is already zero
367 ;----------------------------------------------------------------------------
368 ; PAUSE: Assert flow control and disable interrupts.
369 ; Must return an error code in a/x.
374 ; Assert flow control
380 ; Delay for flow stop to be received
389 ora #%00000010 ; Disable interrupts
396 ;----------------------------------------------------------------------------
397 ; UNPAUSE: Re-enable interrupts and release flow control.
398 ; Must return an error code in a/x.
403 ; Re-enable rx interrupts & release flow control
411 ; Poll for stalled char & exit
418 ;----------------------------------------------------------------------------
419 ; STATUS: Return the status in the variable pointed to by ptr1.
420 ; Must return an error code in a/x.
427 jsr PollReceive ; bug-recovery hack
432 ;----------------------------------------------------------------------------
433 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
434 ; specific data in ptr1, and the ioctl code in A.
435 ; Must return an error code in a/x.
439 lda #<SER_ERR_INV_IOCTL ; We don't support ioclts for now
440 ldx #>SER_ERR_INV_IOCTL
443 ;----------------------------------------------------------------------------
446 ; C128 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=33, ROMexit=30
447 ; C64 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=34, ROMexit=29
449 ; timing: normal=76+43+9=128 cycles, assertFlow=76+52+9=137 cycles
451 ; C128 @ 115.2k: 177 cycles avail (fast)
452 ; C64 @ 57.6k: 177 cycles avail, worstAvail=177-43? = 134
453 ; SCPU @ 230.4k: 868 cycles avail: for a joke!
458 lda ACIA_STATUS ;(4) ;status ;check for byte received
466 @L1: lda ACIA_DATA ;(4) data ;get byte and put into receive buffer
469 beq @L3 ;(2*) Jump if no space in receive buffer
473 cpx #33 ;(2) check for buffer space low
477 ; Assert flow control
479 @L2: lda RtsOff ;(3) assert flow control if buffer space too low
480 sta ACIA_CMD ;(4) command
482 @L3: jmp NMIEXIT ;(3)
487 ;----------------------------------------------------------------------------
488 ; Try to send a byte. Internal routine. A = TryHard
492 sta tmp1 ; Remember tryHard flag
497 ; Check for flow stopped
502 ; Check that swiftlink is ready to send
507 bit tmp1 ;keep trying if must try hard
511 ; Send byte and try again
522 ;----------------------------------------------------------------------------
524 ; PollReceive - poll for rx char
525 ; This function is useful in odd cases where the 6551 has a character in
526 ; it but it fails to raise an NMI. It might be edge-triggering conditions?
527 ; Actually, I'm not entirely sure that this condition can still arrise, but
528 ; calling this function does no harm.
549 ;----------------------------------------------------------------------------
551 ; DelayMs : delay for given number of milliseconds
552 ; This implementation isn't very rigerous; it merely delays for the
553 ; approximate number of clock cycles for the processor speed.
555 ; repeat for number of milliseconds:
556 ; delay for 1017 clock cycles
560 .proc DelayMs ;( .A=milliseconds )
564 bne @L2 ;(3) // 1017 cycles