2 ; Serial driver for the builtin 6551 ACIA of the Commodore 610.
4 ; Ullrich von Bassewitz, 2003-12-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"
26 .include "ser-kernel.inc"
27 .include "ser-error.inc"
31 ; ------------------------------------------------------------------------
32 ; Header. Includes jump table
38 .byte $73, $65, $72 ; "ser"
39 .byte SER_API_VERSION ; Serial API version number
53 ;----------------------------------------------------------------------------
59 RecvHead: .res 1 ; Head of receive buffer
60 RecvTail: .res 1 ; Tail of receive buffer
61 RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
62 SendHead: .res 1 ; Head of send buffer
63 SendTail: .res 1 ; Tail of send buffer
64 SendFreeCnt: .res 1 ; Number of bytes in send buffer
66 Stopped: .res 1 ; Flow-stopped flag
69 ; Send and receive buffers: 256 bytes each
75 ; Tables used to translate RS232 params into register values
77 BaudTable: ; bit7 = 1 means setting is invalid
78 .byte $FF ; SER_BAUD_45_5
79 .byte $01 ; SER_BAUD_50
80 .byte $02 ; SER_BAUD_75
81 .byte $03 ; SER_BAUD_110
82 .byte $04 ; SER_BAUD_134_5
83 .byte $05 ; SER_BAUD_150
84 .byte $06 ; SER_BAUD_300
85 .byte $07 ; SER_BAUD_600
86 .byte $08 ; SER_BAUD_1200
87 .byte $09 ; SER_BAUD_1800
88 .byte $0A ; SER_BAUD_2400
89 .byte $0B ; SER_BAUD_3600
90 .byte $0C ; SER_BAUD_4800
91 .byte $0D ; SER_BAUD_7200
92 .byte $0E ; SER_BAUD_9600
93 .byte $0F ; SER_BAUD_19200
94 .byte $FF ; SER_BAUD_38400
95 .byte $FF ; SER_BAUD_57600
96 .byte $FF ; SER_BAUD_115200
97 .byte $FF ; SER_BAUD_230400
100 .byte $60 ; SER_BITS_5
101 .byte $40 ; SER_BITS_6
102 .byte $20 ; SER_BITS_7
103 .byte $00 ; SER_BITS_8
106 .byte $00 ; SER_STOP_1
107 .byte $80 ; SER_STOP_2
110 .byte $00 ; SER_PAR_NONE
111 .byte $20 ; SER_PAR_ODD
112 .byte $60 ; SER_PAR_EVEN
113 .byte $A0 ; SER_PAR_MARK
114 .byte $E0 ; SER_PAR_SPACE
118 ;----------------------------------------------------------------------------
119 ; INSTALL routine. Is called after the driver is loaded into memory. If
120 ; possible, check if the hardware is present.
121 ; Must return an SER_ERR_xx code in a/x.
123 ; Since we don't have to manage the IRQ vector on the Plus/4, this is actually
126 ; UNINSTALL routine. Is called before the driver is removed from memory.
127 ; Must return an SER_ERR_xx code in a/x.
133 ; Deactivate DTR and disable 6551 interrupts
138 ; Done, return an error code
144 ;----------------------------------------------------------------------------
145 ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1.
146 ; Must return an SER_ERR_xx code in a/x.
150 ; Check if the handshake setting is valid
152 ldy #SER_PARAMS::HANDSHAKE ; Handshake
154 cmp #SER_HS_HW ; This is all we support
161 ; Set the value for the control register, which contains stop bits, word
162 ; length and the baud rate.
164 ldy #SER_PARAMS::BAUDRATE
165 lda (ptr1),y ; Baudrate index
167 lda BaudTable,y ; Get 6551 value
168 bmi InvBaud ; Branch if rate not supported
171 ldy #SER_PARAMS::DATABITS ; Databits
178 ldy #SER_PARAMS::STOPBITS ; Stopbits
183 ora #%00010000 ; Receiver clock source = baudrate
187 ; Set the value for the command register. We remember the base value in
188 ; RtsOff, since we will have to manipulate ACIA_CMD often.
190 ldy #SER_PARAMS::PARITY ; Parity
194 ora #%00000001 ; DTR active
196 ora #%00001000 ; Enable receive interrupts
208 lda #<SER_ERR_INIT_FAILED
209 ldx #>SER_ERR_INIT_FAILED
212 ; Baud rate not available
215 lda #<SER_ERR_BAUD_UNAVAIL
216 ldx #>SER_ERR_BAUD_UNAVAIL
219 ;----------------------------------------------------------------------------
220 ; CLOSE: Close the port, disable interrupts and flush the buffer. Called
221 ; without parameters. Must return an error code in a/x.
226 ; Stop interrupts, drop DTR
241 ;----------------------------------------------------------------------------
242 ; GET: Will fetch a character from the receive buffer and store it into the
243 ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is
247 GET: ldx SendFreeCnt ; Send data if necessary
253 ; Check for buffer empty
258 lda #<SER_ERR_NO_DATA
259 ldx #>SER_ERR_NO_DATA
262 ; Check for flow stopped & enough free: release flow control
274 ; Get byte from buffer
282 txa ; Return code = 0
285 ;----------------------------------------------------------------------------
286 ; PUT: Output character in A.
287 ; Must return an error code in a/x.
302 ; Put byte into send buffer & send
306 lda #<SER_ERR_OVERFLOW ; X is already zero
319 ;----------------------------------------------------------------------------
320 ; STATUS: Return the status in the variable pointed to by ptr1.
321 ; Must return an error code in a/x.
335 ;----------------------------------------------------------------------------
336 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
337 ; specific data in ptr1, and the ioctl code in A.
338 ; Must return an error code in a/x.
341 IOCTL: lda #<SER_ERR_INV_IOCTL ; We don't support ioclts for now
342 ldx #>SER_ERR_INV_IOCTL
345 ;----------------------------------------------------------------------------
346 ; IRQ: Called from the builtin runtime IRQ handler as a subroutine. All
347 ; registers are already save, no parameters are passed and no return code
352 sta IndReg ; Switch to the system bank
354 lda (acia),y ; Check ACIA status for receive interrupt
356 beq @L9 ; Jump if no ACIA interrupt
358 lda (acia),y ; Get byte from ACIA
359 ldx RecvFreeCnt ; Check if we have free space left
360 beq @L1 ; Jump if no space in receive buffer
361 ldy RecvTail ; Load buffer pointer
362 sta RecvBuf,y ; Store received byte in buffer
363 inc RecvTail ; Increment buffer pointer
364 dec RecvFreeCnt ; Decrement free space counter
365 cpx #33 ; Check for buffer space low
366 bcs @L9 ; Assert flow control if buffer space low
368 ; Assert flow control if buffer space too low
375 ; Done, switch back to the execution segment
381 ;----------------------------------------------------------------------------
382 ; Try to send a byte. Internal routine. A = TryHard
386 sta tmp1 ; Remember tryHard flag
388 sta IndReg ; Switch to the system bank
393 ; Check for flow stopped
398 ; Check that swiftlink is ready to send
400 @L2: ldy #ACIA::STATUS
404 bit tmp1 ; Keep trying if must try hard
407 ; Switch back the bank and return
413 ; Send byte and try again
426 ;----------------------------------------------------------------------------
441 ;----------------------------------------------------------------------------
442 ; Write to the ACIA changing the indirect segment. Offset is in Y, value in A.