]> git.sur5r.net Git - cc65/blob - libsrc/plus4/plus4-stdser.s
30569a31fdfc9a4b786c9bf8d103249573c28fca
[cc65] / libsrc / plus4 / plus4-stdser.s
1 ;
2 ; Serial driver for the builtin 6551 ACIA of the Plus/4.
3 ;
4 ; Ullrich von Bassewitz, 2003-12-13
5 ;
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.
8 ;
9 ; SwiftLink/Turbo-232 v0.90 device driver, by Craig Bruce, 14-Apr-1998.
10 ;
11 ; This software is Public Domain.  It is in Buddy assembler format.
12 ;
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.
17 ;
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.
22 ;
23
24         .include        "zeropage.inc"
25         .include        "ser-kernel.inc"
26         .include        "ser-error.inc"
27         .include        "plus4.inc"
28
29
30 ; ------------------------------------------------------------------------
31 ; Header. Includes jump table
32
33 .segment        "JUMPTABLE"
34
35 ; Driver signature
36
37         .byte   $73, $65, $72           ; "ser"
38         .byte   SER_API_VERSION         ; Serial API version number
39
40 ; Jump table.
41
42         .word   INSTALL
43         .word   UNINSTALL
44         .word   OPEN
45         .word   CLOSE
46         .word   GET
47         .word   PUT
48         .word   STATUS
49         .word   IOCTL
50         .word   IRQ
51
52 ;----------------------------------------------------------------------------
53 ; I/O definitions
54
55 ACIA            = $DE00
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
61 ;----------------------------------------------------------------------------
62 ;
63 ; Global variables
64 ;
65
66 .bss
67 RecvHead:       .res    1       ; Head of receive buffer
68 RecvTail:       .res    1       ; Tail of receive buffer
69 RecvFreeCnt:    .res    1       ; Number of bytes in receive buffer
70 SendHead:       .res    1       ; Head of send buffer
71 SendTail:       .res    1       ; Tail of send buffer
72 SendFreeCnt:    .res    1       ; Number of bytes in send buffer
73
74 Stopped:        .res    1       ; Flow-stopped flag
75 RtsOff:         .res    1       ;
76
77 ; Send and receive buffers: 256 bytes each
78 RecvBuf:        .res    256
79 SendBuf:        .res    256
80
81 .rodata
82
83 ; Tables used to translate RS232 params into register values
84
85 BaudTable:                      ; bit7 = 1 means setting is invalid
86         .byte   $FF             ; SER_BAUD_45_5
87         .byte   $01             ; SER_BAUD_50
88         .byte   $02             ; SER_BAUD_75
89         .byte   $03             ; SER_BAUD_110
90         .byte   $04             ; SER_BAUD_134_5
91         .byte   $05             ; SER_BAUD_150
92         .byte   $06             ; SER_BAUD_300
93         .byte   $07             ; SER_BAUD_600
94         .byte   $08             ; SER_BAUD_1200
95         .byte   $09             ; SER_BAUD_1800
96         .byte   $0A             ; SER_BAUD_2400
97         .byte   $0B             ; SER_BAUD_3600
98         .byte   $0C             ; SER_BAUD_4800
99         .byte   $0D             ; SER_BAUD_7200
100         .byte   $0E             ; SER_BAUD_9600
101         .byte   $0F             ; SER_BAUD_19200
102         .byte   $FF             ; SER_BAUD_38400
103         .byte   $FF             ; SER_BAUD_57600
104         .byte   $FF             ; SER_BAUD_115200
105         .byte   $FF             ; SER_BAUD_230400
106
107 BitTable:
108         .byte   $60             ; SER_BITS_5
109         .byte   $40             ; SER_BITS_6
110         .byte   $20             ; SER_BITS_7
111         .byte   $00             ; SER_BITS_8
112
113 StopTable:
114         .byte   $00             ; SER_STOP_1
115         .byte   $80             ; SER_STOP_2
116
117 ParityTable:
118         .byte   $00             ; SER_PAR_NONE
119         .byte   $20             ; SER_PAR_ODD
120         .byte   $60             ; SER_PAR_EVEN
121         .byte   $A0             ; SER_PAR_MARK
122         .byte   $E0             ; SER_PAR_SPACE
123
124 .code
125
126 ;----------------------------------------------------------------------------
127 ; INSTALL routine. Is called after the driver is loaded into memory. If
128 ; possible, check if the hardware is present.
129 ; Must return an SER_ERR_xx code in a/x.
130 ;
131 ; Since we don't have to manage the IRQ vector on the Plus/4, this is actually
132 ; the same as:
133 ;
134 ; UNINSTALL routine. Is called before the driver is removed from memory.
135 ; Must return an SER_ERR_xx code in a/x.
136 ;
137 ; and:
138 ;
139 ; CLOSE: Close the port, disable interrupts and flush the buffer. Called
140 ; without parameters. Must return an error code in a/x.
141 ;
142
143 INSTALL:
144 UNINSTALL:
145 CLOSE:
146
147 ; Deactivate DTR and disable 6551 interrupts
148
149         lda     #%00001010
150         sta     ACIA_CMD
151
152 ; Done, return an error code
153
154         lda     #<SER_ERR_OK
155         tax                     ; A is zero
156         rts
157
158 ;----------------------------------------------------------------------------
159 ; PARAMS routine. A pointer to a ser_params structure is passed in ptr1.
160 ; Must return an SER_ERR_xx code in a/x.
161
162 OPEN:
163
164 ; Check if the handshake setting is valid
165
166         ldy     #SER_PARAMS::HANDSHAKE  ; Handshake
167         lda     (ptr1),y
168         cmp     #SER_HS_HW              ; This is all we support
169         bne     InvParam
170
171 ; Initialize buffers
172
173         ldx     #0
174         stx     Stopped
175         stx     RecvHead
176         stx     RecvTail
177         stx     SendHead
178         stx     SendTail
179         dex                             ; X = 255
180         stx     RecvFreeCnt
181         stx     SendFreeCnt
182
183 ; Set the value for the control register, which contains stop bits, word
184 ; length and the baud rate.
185
186         ldy     #SER_PARAMS::BAUDRATE
187         lda     (ptr1),y                ; Baudrate index
188         tay
189         lda     BaudTable,y             ; Get 6551 value
190         bmi     InvBaud                 ; Branch if rate not supported
191         sta     tmp1
192
193         ldy     #SER_PARAMS::DATABITS   ; Databits
194         lda     (ptr1),y
195         tay
196         lda     BitTable,y
197         ora     tmp1
198         sta     tmp1
199
200         ldy     #SER_PARAMS::STOPBITS   ; Stopbits
201         lda     (ptr1),y
202         tay
203         lda     StopTable,y
204         ora     tmp1
205         ora     #%00010000              ; Receiver clock source = baudrate
206         sta     ACIA_CTRL
207
208 ; Set the value for the command register. We remember the base value in
209 ; RtsOff, since we will have to manipulate ACIA_CMD often.
210
211         ldy     #SER_PARAMS::PARITY     ; Parity
212         lda     (ptr1),y
213         tay
214         lda     ParityTable,y
215         ora     #%00000001              ; DTR active
216         sta     RtsOff
217         ora     #%00001000              ; Enable receive interrupts
218         sta     ACIA_CMD
219
220 ; Done
221
222         lda     #<SER_ERR_OK
223         tax                             ; A is zero
224         rts
225
226 ; Invalid parameter
227
228 InvParam:
229         lda     #<SER_ERR_INIT_FAILED
230         ldx     #>SER_ERR_INIT_FAILED
231         rts
232
233 ; Baud rate not available
234
235 InvBaud:
236         lda     #<SER_ERR_BAUD_UNAVAIL
237         ldx     #>SER_ERR_BAUD_UNAVAIL
238         rts
239
240 ;----------------------------------------------------------------------------
241 ; GET: Will fetch a character from the receive buffer and store it into the
242 ; variable pointer to by ptr1. If no data is available, SER_ERR_NO_DATA is
243 ; return.
244 ;
245
246 GET:    ldx     SendFreeCnt             ; Send data if necessary
247         inx                             ; X == $FF?
248         beq     @L1
249         lda     #$00
250         jsr     TryToSend
251
252 ; Check for buffer empty
253
254 @L1:    lda     RecvFreeCnt             ; (25)
255         cmp     #$ff
256         bne     @L2
257         lda     #<SER_ERR_NO_DATA
258         ldx     #>SER_ERR_NO_DATA
259         rts
260
261 ; Check for flow stopped & enough free: release flow control
262
263 @L2:    ldx     Stopped                 ; (34)
264         beq     @L3
265         cmp     #63
266         bcc     @L3
267         lda     #$00
268         sta     Stopped
269         lda     RtsOff
270         ora     #%00001000
271         sta     ACIA_CMD
272
273 ; Get byte from buffer
274
275 @L3:    ldx     RecvHead                ; (41)
276         lda     RecvBuf,x
277         inc     RecvHead
278         inc     RecvFreeCnt
279         ldx     #$00                    ; (59)
280         sta     (ptr1,x)
281         txa                             ; Return code = 0
282         rts
283
284 ;----------------------------------------------------------------------------
285 ; PUT: Output character in A.
286 ; Must return an error code in a/x.
287 ;
288
289 PUT:
290
291 ; Try to send
292
293         ldx     SendFreeCnt
294         inx                             ; X = $ff?
295         beq     @L2
296         pha
297         lda     #$00
298         jsr     TryToSend
299         pla
300
301 ; Put byte into send buffer & send
302
303 @L2:    ldx     SendFreeCnt
304         bne     @L3
305         lda     #<SER_ERR_OVERFLOW      ; X is already zero
306         rts
307
308 @L3:    ldx     SendTail
309         sta     SendBuf,x
310         inc     SendTail
311         dec     SendFreeCnt
312         lda     #$ff
313         jsr     TryToSend
314         lda     #<SER_ERR_OK
315         tax
316         rts
317
318 ;----------------------------------------------------------------------------
319 ; STATUS: Return the status in the variable pointed to by ptr1.
320 ; Must return an error code in a/x.
321 ;
322
323 STATUS: lda     ACIA_STATUS
324         ldx     #0
325         sta     (ptr1,x)
326         txa                             ; SER_ERR_OK
327         rts
328
329 ;----------------------------------------------------------------------------
330 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
331 ; specific data in ptr1, and the ioctl code in A.
332 ; Must return an error code in a/x.
333 ;
334
335 IOCTL:  lda     #<SER_ERR_INV_IOCTL     ; We don't support ioclts for now
336         ldx     #>SER_ERR_INV_IOCTL
337         rts                             ; Run into IRQ instead
338
339 ;----------------------------------------------------------------------------
340 ; IRQ: Called from the builtin runtime IRQ handler as a subroutine. All
341 ; registers are already save, no parameters are passed, but the carry flag
342 ; is clear on entry. The routine must return with carry set if the interrupt
343 ; was handled, otherwise with carry clear.
344 ;
345
346 IRQ:    lda     ACIA_STATUS     ; Check ACIA status for receive interrupt
347         and     #$08
348         beq     @L9             ; Jump if no ACIA interrupt (carry still clear)
349         lda     ACIA_DATA       ; Get byte from ACIA
350         ldx     RecvFreeCnt     ; Check if we have free space left
351         beq     @L1             ; Jump if no space in receive buffer
352         ldy     RecvTail        ; Load buffer pointer
353         sta     RecvBuf,y       ; Store received byte in buffer
354         inc     RecvTail        ; Increment buffer pointer
355         dec     RecvFreeCnt     ; Decrement free space counter
356         cpx     #33             ; Check for buffer space low
357         bcc     @L1             ; Assert flow control if buffer space low
358         rts                     ; Return with carry set (interrupt handled)
359
360 ; Assert flow control if buffer space too low
361
362 @L1:    lda     RtsOff
363         sta     ACIA_CMD
364         sta     Stopped
365         sec                     ; Interrupt handled
366 @L9:    rts
367
368 ;----------------------------------------------------------------------------
369 ; Try to send a byte. Internal routine. A = TryHard
370
371 .proc   TryToSend
372
373         sta     tmp1            ; Remember tryHard flag
374 @L0:    lda     SendFreeCnt
375         cmp     #$ff
376         beq     @L3             ; Bail out
377
378 ; Check for flow stopped
379
380 @L1:    lda     Stopped
381         bne     @L3             ; Bail out
382
383 ; Check that swiftlink is ready to send
384
385 @L2:    lda     ACIA_STATUS
386         and     #$10
387         bne     @L4
388         bit     tmp1            ;keep trying if must try hard
389         bmi     @L0
390 @L3:    rts
391
392 ; Send byte and try again
393
394 @L4:    ldx     SendHead
395         lda     SendBuf,x
396         sta     ACIA_DATA
397         inc     SendHead
398         inc     SendFreeCnt
399         jmp     @L0
400
401 .endproc
402
403