2 ; Serial driver for the builtin 6551 ACIA of the Plus/4.
4 ; Ullrich von Bassewitz, 2003-12-13
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"
32 ; ------------------------------------------------------------------------
33 ; Header. Includes jump table
35 module_header _plus4_stdser_ser
39 .byte $73, $65, $72 ; "ser"
40 .byte SER_API_VERSION ; Serial API version number
58 ;----------------------------------------------------------------------------
62 ACIA_DATA = ACIA+0 ; Data register
63 ACIA_STATUS = ACIA+1 ; Status register
64 ACIA_CMD = ACIA+2 ; Command register
65 ACIA_CTRL = ACIA+3 ; Control register
67 ;----------------------------------------------------------------------------
73 RecvHead: .res 1 ; Head of receive buffer
74 RecvTail: .res 1 ; Tail of receive buffer
75 RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
76 SendHead: .res 1 ; Head of send buffer
77 SendTail: .res 1 ; Tail of send buffer
78 SendFreeCnt: .res 1 ; Number of bytes in send buffer
80 Stopped: .res 1 ; Flow-stopped flag
83 ; Send and receive buffers: 256 bytes each
89 ; Tables used to translate RS232 params into register values
91 BaudTable: ; bit7 = 1 means setting is invalid
92 .byte $FF ; SER_BAUD_45_5
93 .byte $01 ; SER_BAUD_50
94 .byte $02 ; SER_BAUD_75
95 .byte $03 ; SER_BAUD_110
96 .byte $04 ; SER_BAUD_134_5
97 .byte $05 ; SER_BAUD_150
98 .byte $06 ; SER_BAUD_300
99 .byte $07 ; SER_BAUD_600
100 .byte $08 ; SER_BAUD_1200
101 .byte $09 ; SER_BAUD_1800
102 .byte $0A ; SER_BAUD_2400
103 .byte $0B ; SER_BAUD_3600
104 .byte $0C ; SER_BAUD_4800
105 .byte $0D ; SER_BAUD_7200
106 .byte $0E ; SER_BAUD_9600
107 .byte $0F ; SER_BAUD_19200
108 .byte $FF ; SER_BAUD_38400
109 .byte $FF ; SER_BAUD_57600
110 .byte $FF ; SER_BAUD_115200
111 .byte $FF ; SER_BAUD_230400
114 .byte $60 ; SER_BITS_5
115 .byte $40 ; SER_BITS_6
116 .byte $20 ; SER_BITS_7
117 .byte $00 ; SER_BITS_8
120 .byte $00 ; SER_STOP_1
121 .byte $80 ; SER_STOP_2
124 .byte $00 ; SER_PAR_NONE
125 .byte $20 ; SER_PAR_ODD
126 .byte $60 ; SER_PAR_EVEN
127 .byte $A0 ; SER_PAR_MARK
128 .byte $E0 ; SER_PAR_SPACE
132 ;----------------------------------------------------------------------------
133 ; INSTALL routine. Is called after the driver is loaded into memory. If
134 ; possible, check if the hardware is present.
135 ; Must return an SER_ERR_xx code in a/x.
137 ; Since we don't have to manage the IRQ vector on the Plus/4, this is actually
140 ; UNINSTALL routine. Is called before the driver is removed from memory.
141 ; Must return an SER_ERR_xx code in a/x.
145 ; CLOSE: Close the port, disable interrupts and flush the buffer. Called
146 ; without parameters. Must return an error code in a/x.
153 ; Deactivate DTR and disable 6551 interrupts
158 ; Done, return an error code
164 ;----------------------------------------------------------------------------
165 ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1.
166 ; Must return an SER_ERR_xx code in a/x.
170 ; Check if the handshake setting is valid
172 ldy #SER_PARAMS::HANDSHAKE ; Handshake
174 cmp #SER_HS_HW ; This is all we support
189 ; Set the value for the control register, which contains stop bits, word
190 ; length and the baud rate.
192 ldy #SER_PARAMS::BAUDRATE
193 lda (ptr1),y ; Baudrate index
195 lda BaudTable,y ; Get 6551 value
196 bmi InvBaud ; Branch if rate not supported
199 ldy #SER_PARAMS::DATABITS ; Databits
206 ldy #SER_PARAMS::STOPBITS ; Stopbits
211 ora #%00010000 ; Receiver clock source = baudrate
214 ; Set the value for the command register. We remember the base value in
215 ; RtsOff, since we will have to manipulate ACIA_CMD often.
217 ldy #SER_PARAMS::PARITY ; Parity
221 ora #%00000001 ; DTR active
223 ora #%00001000 ; Enable receive interrupts
235 lda #<SER_ERR_INIT_FAILED
236 ldx #>SER_ERR_INIT_FAILED
239 ; Baud rate not available
242 lda #<SER_ERR_BAUD_UNAVAIL
243 ldx #>SER_ERR_BAUD_UNAVAIL
246 ;----------------------------------------------------------------------------
247 ; GET: Will fetch a character from the receive buffer and store it into the
248 ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is
252 GET: ldx SendFreeCnt ; Send data if necessary
258 ; Check for buffer empty
260 @L1: lda RecvFreeCnt ; (25)
263 lda #<SER_ERR_NO_DATA
264 ldx #>SER_ERR_NO_DATA
267 ; Check for flow stopped & enough free: release flow control
269 @L2: ldx Stopped ; (34)
279 ; Get byte from buffer
281 @L3: ldx RecvHead ; (41)
287 txa ; Return code = 0
290 ;----------------------------------------------------------------------------
291 ; PUT: Output character in A.
292 ; Must return an error code in a/x.
307 ; Put byte into send buffer & send
311 lda #<SER_ERR_OVERFLOW ; X is already zero
324 ;----------------------------------------------------------------------------
325 ; STATUS: Return the status in the variable pointed to by ptr1.
326 ; Must return an error code in a/x.
329 STATUS: lda ACIA_STATUS
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
343 rts ; Run into IRQ instead
345 ;----------------------------------------------------------------------------
346 ; IRQ: Called from the builtin runtime IRQ handler as a subroutine. All
347 ; registers are already save, no parameters are passed, but the carry flag
348 ; is clear on entry. The routine must return with carry set if the interrupt
349 ; was handled, otherwise with carry clear.
352 IRQ: lda ACIA_STATUS ; Check ACIA status for receive interrupt
354 beq @L9 ; Jump if no ACIA interrupt (carry still clear)
355 lda ACIA_DATA ; Get byte from ACIA
356 ldx RecvFreeCnt ; Check if we have free space left
357 beq @L1 ; Jump if no space in receive buffer
358 ldy RecvTail ; Load buffer pointer
359 sta RecvBuf,y ; Store received byte in buffer
360 inc RecvTail ; Increment buffer pointer
361 dec RecvFreeCnt ; Decrement free space counter
362 cpx #33 ; Check for buffer space low
363 bcc @L1 ; Assert flow control if buffer space low
364 rts ; Return with carry set (interrupt handled)
366 ; Assert flow control if buffer space too low
371 sec ; Interrupt handled
374 ;----------------------------------------------------------------------------
375 ; Try to send a byte. Internal routine. A = TryHard
379 sta tmp1 ; Remember tryHard flag
384 ; Check for flow stopped
389 ; Check that swiftlink is ready to send
394 bit tmp1 ;keep trying if must try hard
398 ; Send byte and try again