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 DropCnt: .res 4 ; Number of bytes lost from rx buffer full
69 Stopped: .res 1 ; Flow-stopped flag
71 Errors: .res 1 ; Number of bytes received in error, low byte
72 RecvHead: .res 1 ; Head of receive buffer
73 RecvTail: .res 1 ; Tail of receive buffer
74 RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
75 SendHead: .res 1 ; Head of send buffer
76 SendTail: .res 1 ; Tail of send buffer
77 SendFreeCnt: .res 1 ; Number of bytes free in send buffer
78 BaudCode: .res 1 ; Current baud in effect
80 ; Send and receive buffers: 256 bytes each
85 NmiContinue: .byte $4c ; JMP instruction for NMI save -- continue
86 NmiSave: .res 2 ; normal NMI handler
90 ; Tables used to translate RS232 params into register values
92 BaudTable: ; bit7 = 1 means setting is invalid
93 .byte $FF ; SER_BAUD_45_5
94 .byte $FF ; SER_BAUD_50
95 .byte $FF ; SER_BAUD_75
96 .byte $FF ; SER_BAUD_110
97 .byte $FF ; SER_BAUD_134_5
98 .byte $FF ; SER_BAUD_150
99 .byte $05 ; SER_BAUD_300
100 .byte $06 ; SER_BAUD_600
101 .byte $07 ; SER_BAUD_1200
102 .byte $FF ; SER_BAUD_1800
103 .byte $08 ; SER_BAUD_2400
104 .byte $FF ; SER_BAUD_3600
105 .byte $0A ; SER_BAUD_4800
106 .byte $FF ; SER_BAUD_7200
107 .byte $0C ; SER_BAUD_9600
108 .byte $0E ; SER_BAUD_19200
109 .byte $0F ; SER_BAUD_38400
110 .byte $FF ; SER_BAUD_57600
111 .byte $FF ; SER_BAUD_115200
112 .byte $FF ; SER_BAUD_230400
115 .byte $60 ; SER_BITS_5
116 .byte $40 ; SER_BITS_6
117 .byte $20 ; SER_BITS_7
118 .byte $00 ; SER_BITS_8
121 .byte $00 ; SER_STOP_1
122 .byte $80 ; SER_STOP_2
125 .byte $00 ; SER_PAR_NONE
126 .byte $20 ; SER_PAR_ODD
127 .byte $60 ; SER_PAR_EVEN
128 .byte $A0 ; SER_PAR_MARK
129 .byte $E0 ; SER_PAR_SPACE
131 HandshakeTable: ; bit7 = 1 means that setting is invalid
132 .byte $FF ; SER_HS_NONE
133 .byte $00 ; SER_HS_HW
134 .byte $FF ; SER_HS_SW
136 ; Delay times: 32 byte-receive times in milliseconds, or 100 max.
137 ; Formula = 320,000 / baud. Invalid values do contain $FF.
139 .byte $FF ; SER_BAUD_45_5
140 .byte $FF ; SER_BAUD_50
141 .byte $FF ; SER_BAUD_75
142 .byte $FF ; SER_BAUD_110
143 .byte $FF ; SER_BAUD_134_5
144 .byte $FF ; SER_BAUD_150
145 .byte 100 ; SER_BAUD_300
146 .byte 100 ; SER_BAUD_600
147 .byte 100 ; SER_BAUD_1200
148 .byte $FF ; SER_BAUD_1800
149 .byte 100 ; SER_BAUD_2400
150 .byte $FF ; SER_BAUD_3600
151 .byte 67 ; SER_BAUD_4800
152 .byte $FF ; SER_BAUD_7200
153 .byte 34 ; SER_BAUD_9600
154 .byte 17 ; SER_BAUD_19200
155 .byte 9 ; SER_BAUD_38400
156 .byte $FF ; SER_BAUD_57600
157 .byte $FF ; SER_BAUD_115200
158 .byte $FF ; SER_BAUD_230400
163 ;----------------------------------------------------------------------------
164 ; INSTALL routine. Is called after the driver is loaded into memory. If
165 ; possible, check if the hardware is present.
166 ; Must return an SER_ERR_xx code in a/x.
170 ; Initialize buffers & control
194 ; Set default to 2400-8N1, enable interrupts
208 ; Done, return an error code
214 ;----------------------------------------------------------------------------
215 ; UNINSTALL routine. Is called before the driver is removed from memory.
216 ; Must return an SER_ERR_xx code in a/x.
220 ; Stop interrupts, drop DTR
240 ;----------------------------------------------------------------------------
241 ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1.
242 ; Must return an SER_ERR_xx code in a/x.
245 ldy #SER_PARAMS_BAUDRATE
246 lda (ptr1),y ; Baudrate index
248 lda BaudTable,x ; Get 6551 value
249 bmi invalid ; Branch if rate not supported
284 ;----------------------------------------------------------------------------
286 ; unsigned char __fastcall__ rs232_params (unsigned char params, unsigned char parity);
287 ; /* Set the port parameters. Use a combination of the #defined values above. */
289 ; Set communication parameters.
291 ; baud rates stops word | parity
292 ; --------------------- ----- ----- | ---------
293 ; $00=50 $08=9600 $00=1 $00=8 | $00=none
294 ; $01=110 $09=19200 $80=2 $20=7 | $20=odd
295 ; $02=134.5 $0a=38400 $40=6 | $60=even
296 ; $03=300 $0b=57600 $60=5 | $A0=mark
297 ; $04=600 $0c=115200 | $E0=space
298 ; $05=1200 $0d=230400
299 ; $06=2400 $0e=future
300 ; $07=4800 $0f=future
304 and #%11100000 ; Save new parity
308 ; Check cpu speed against baud rate
323 @L2: lda #RS_ERR_BAUD_TOO_FAST
326 ; Set baud/parameters
334 lda #RS_ERR_BAUD_NOT_AVAIL
342 lda #RS_ERR_BAUD_NOT_AVAIL
379 .byte $ff,$ff,$ff,$05,$06,$07,$08,$0a,$0c,$0e,$0f,$10,$20,$30,$ff,$ff
380 ;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
381 ;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
382 ;out masks: $0F=Baud, val$FF=err
383 ; $30=t232ExtBaud: $00=none, $10=57.6, $20=115.2, $30=230.4
386 ;----------------------------------------------------------------------------
387 ; GET: Will fetch a character from the receive buffer and store it into the
388 ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is
394 ; Check for bytes to send
402 ; Check for buffer empty
404 @L1: lda RecvFreeCnt ; (25)
411 ; Check for flow stopped & enough free: release flow control
413 @L2: ldx Stopped ; (34)
423 ; Get byte from buffer
425 @L3: ldx RecvHead ; (41)
431 txa ; Return code = 0
434 ;----------------------------------------------------------------------------
435 ; PUT: Output character in A.
436 ; Must return an error code in a/x.
451 ; Put byte into send buffer & send
455 lda #<SER_ERR_OVERFLOW ; X is already zero
468 ;----------------------------------------------------------------------------
469 ; PAUSE: Assert flow control and disable interrupts.
470 ; Must return an error code in a/x.
475 ; Assert flow control
481 ; Delay for flow stop to be received
497 ;----------------------------------------------------------------------------
498 ; UNPAUSE: Re-enable interrupts and release flow control.
499 ; Must return an error code in a/x.
504 ; Re-enable rx interrupts & release flow control
512 ; Poll for stalled char & exit
519 ;----------------------------------------------------------------------------
520 ; STATUS: Return the status in the variable pointed to by ptr1.
521 ; Must return an error code in a/x.
531 jsr PollReceive ; bug-recovery hack
536 ;----------------------------------------------------------------------------
539 ; C128 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=33, ROMexit=30
540 ; C64 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=34, ROMexit=29
542 ; timing: normal=76+43+9=128 cycles, assertFlow=76+52+9=137 cycles
544 ; C128 @ 115.2k: 177 cycles avail (fast)
545 ; C64 @ 57.6k: 177 cycles avail, worstAvail=177-43? = 134
546 ; SCPU @ 230.4k: 868 cycles avail: for a joke!
551 lda ACIA_STATUS ;(4) ;status ;check for byte received
559 lda ACIA_STATUS ;(4) opt ;status ;check for receive errors
563 @L1: lda ACIA_DATA ;(4) ;data ;get byte and put into receive buffer
570 cpx #33 ;(2) ;check for buffer space low
574 ; Assert flow control
576 @L2: lda RtsOff ;(3) ;assert flow control if buffer space too low
577 sta ACIA_CMD ;(4) ;command
583 @L3: inc DropCnt+0 ; not time-critical
595 ;----------------------------------------------------------------------------
596 ; Try to send a byte. Internal routine. A = TryHard
600 sta tmp1 ; Remember tryHard flag
605 ; Check for flow stopped
610 ; Check that swiftlink is ready to send
615 bit tmp1 ;keep trying if must try hard
619 ; Send byte and try again
630 ;----------------------------------------------------------------------------
632 ; PollReceive - poll for rx char
633 ; This function is useful in odd cases where the 6551 has a character in
634 ; it but it fails to raise an NMI. It might be edge-triggering conditions?
635 ; Actually, I'm not entirely sure that this condition can still arrise, but
636 ; calling this function does no harm.
657 ;----------------------------------------------------------------------------
659 ; DelayMs : delay for given number of milliseconds
660 ; This implementation isn't very rigerous; it merely delays for the
661 ; approximate number of clock cycles for the processor speed.
663 ; repeat for number of milliseconds:
664 ; delay for 1017 clock cycles
668 .proc DelayMs ;( .A=milliseconds )
672 bne @L2 ;(3) // 1017 cycles