]> git.sur5r.net Git - cc65/blob - libsrc/atmos/ser/atmos-acia.s
c3d465d120a8823d4eb30731d28d0365c9fb2a4a
[cc65] / libsrc / atmos / ser / atmos-acia.s
1 ;
2 ; Serial driver for the Telestrat integrated serial controller and the
3 ; Atmos with a serial add-on.
4 ;
5 ; 2012-03-05, Stefan Haubenthal
6 ; 2013-07-15, Greg King
7 ;
8 ; The driver is based on the cc65 rs232 module, which in turn is based on
9 ; Craig Bruce device driver for the Switftlink/Turbo-232.
10 ;
11 ; SwiftLink/Turbo-232 v0.90 device driver, by Craig Bruce, 14-Apr-1998.
12 ;
13 ; This software is Public Domain.  It is in Buddy assembler format.
14 ;
15 ; This device driver uses the SwiftLink RS-232 Serial Cartridge, available from
16 ; Creative Micro Designs, Inc, and also supports the extensions of the Turbo232
17 ; Serial Cartridge.  Both devices are based on the 6551 ACIA chip.  It also
18 ; supports the "hacked" SwiftLink with a 1.8432 MHz crystal.
19 ;
20 ; The code assumes that the kernal + I/O are in context.  On the C128, call
21 ; it from Bank 15.  On the C64, don't flip out the Kernal unless a suitable
22 ; NMI catcher is put into the RAM under then Kernal.  For the SuperCPU, the
23 ; interrupt handling assumes that the 65816 is in 6502-emulation mode.
24 ;
25
26         .include        "zeropage.inc"
27         .include        "ser-kernel.inc"
28         .include        "ser-error.inc"
29         .include        "atmos.inc"
30
31 ; ------------------------------------------------------------------------
32 ; Header. Includes jump table
33
34         .segment        "JUMPTABLE"
35
36         ; Driver signature
37         .byte   $73, $65, $72           ; "ser"
38         .byte   SER_API_VERSION         ; Serial API version number
39
40         ; Library reference
41         .addr   $0000
42
43         ; Jump table
44         .addr   INSTALL
45         .addr   UNINSTALL
46         .addr   OPEN
47         .addr   CLOSE
48         .addr   GET
49         .addr   PUT
50         .addr   SER_STATUS
51         .addr   IOCTL
52         .addr   IRQ
53
54 ;----------------------------------------------------------------------------
55 ; Global variables
56
57         .bss
58
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
65
66 Stopped:        .res    1       ; Flow-stopped flag
67 RtsOff:         .res    1       ;
68
69 RecvBuf:        .res    256     ; Receive buffers: 256 bytes
70 SendBuf:        .res    256     ; Send buffers: 256 bytes
71
72 Index:          .res    1       ; I/O register index
73
74         .rodata
75
76         ; 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
98 BitTable:
99         .byte   $60             ; SER_BITS_5
100         .byte   $40             ; SER_BITS_6
101         .byte   $20             ; SER_BITS_7
102         .byte   $00             ; SER_BITS_8
103 StopTable:
104         .byte   $00             ; SER_STOP_1
105         .byte   $80             ; SER_STOP_2
106 ParityTable:
107         .byte   $00             ; SER_PAR_NONE
108         .byte   $20             ; SER_PAR_ODD
109         .byte   $60             ; SER_PAR_EVEN
110         .byte   $A0             ; SER_PAR_MARK
111         .byte   $E0             ; SER_PAR_SPACE
112
113         .code
114
115 ;----------------------------------------------------------------------------
116 ; INSTALL: Is called after the driver is loaded into memory. If possible,
117 ; check if the hardware is present. Must return an SER_ERR_xx code in a/x.
118 ;
119 ; Since we don't have to manage the IRQ vector on the Telestrat/Atmos, this is
120 ; actually the same as:
121 ;
122 ; UNINSTALL: Is called before the driver is removed from memory.
123 ; No return code required (the driver is removed from memory on return).
124 ;
125 ; and:
126 ;
127 ; CLOSE: Close the port and disable interrupts. Called without parameters.
128 ; Must return an SER_ERR_xx code in a/x.
129
130 INSTALL:
131 UNINSTALL:
132 CLOSE:
133         ldx     Index           ; Check for open port
134         beq     :+
135
136         ; Deactivate DTR and disable 6551 interrupts
137         lda     #%00001010
138         sta     ACIA::CMD,x
139
140         ; Done, return an error code
141 :       lda     #<SER_ERR_OK
142         tax                     ; A is zero
143         stx     Index           ; Mark port as closed
144         rts
145
146 ;----------------------------------------------------------------------------
147 ; OPEN: A pointer to a ser_params structure is passed in ptr1.
148 ; Must return an SER_ERR_xx code in a/x.
149
150 OPEN:
151         ; Check if the handshake setting is valid
152         ldy     #SER_PARAMS::HANDSHAKE  ; Handshake
153         lda     (ptr1),y
154         cmp     #SER_HS_HW              ; This is all we support
155         bne     InvParam
156
157         ; Initialize buffers
158         ldy     #$00
159         sty     Stopped
160         sty     RecvHead
161         sty     RecvTail
162         sty     SendHead
163         sty     SendTail
164         dey                             ; Y = 255
165         sty     RecvFreeCnt
166         sty     SendFreeCnt
167
168         ; Set the value for the control register, which contains stop bits,
169         ; word length and the baud rate.
170         ldy     #SER_PARAMS::BAUDRATE
171         lda     (ptr1),y                ; Baudrate index
172         tay
173         lda     BaudTable,y             ; Get 6551 value
174         bmi     InvBaud                 ; Branch if rate not supported
175         sta     tmp1
176
177         ldy     #SER_PARAMS::DATABITS   ; Databits
178         lda     (ptr1),y
179         tay
180         lda     BitTable,y
181         ora     tmp1
182         sta     tmp1
183
184         ldy     #SER_PARAMS::STOPBITS   ; Stopbits
185         lda     (ptr1),y
186         tay
187         lda     StopTable,y
188         ora     tmp1
189         ora     #%00010000              ; Receiver clock source = baudrate
190         sta     ACIA::CTRL
191
192         ; Set the value for the command register. We remember the base value
193         ; in RtsOff, since we will have to manipulate ACIA::CMD often.
194         ldy     #SER_PARAMS::PARITY     ; Parity
195         lda     (ptr1),y
196         tay
197         lda     ParityTable,y
198         ora     #%00000001              ; DTR active
199         sta     RtsOff
200         ora     #%00001000              ; Enable receive interrupts
201         sta     ACIA::CMD
202
203         ; Done
204         stx     Index                   ; Mark port as open
205         lda     #<SER_ERR_OK
206         tax                             ; A is zero
207         rts
208
209         ; Invalid parameter
210 InvParam:lda    #<SER_ERR_INIT_FAILED
211         ldx     #>SER_ERR_INIT_FAILED
212         rts
213
214         ; Baud rate not available
215 InvBaud:lda     #<SER_ERR_BAUD_UNAVAIL
216         ldx     #>SER_ERR_BAUD_UNAVAIL
217         rts
218
219 ;----------------------------------------------------------------------------
220 ; GET: Will fetch a character from the receive buffer and store it into the
221 ; variable pointed to by ptr1. If no data is available, SER_ERR_NO_DATA is
222 ; returned.
223
224 GET:
225         ldy     SendFreeCnt     ; Send data if necessary
226         iny                     ; Y == $FF?
227         beq     :+
228         lda     #$00            ; TryHard = false
229         jsr     TryToSend
230
231         ; Check for buffer empty
232 :       lda     RecvFreeCnt     ; (25)
233         cmp     #$FF
234         bne     :+
235         lda     #<SER_ERR_NO_DATA
236         ldx     #>SER_ERR_NO_DATA
237         rts
238
239         ; Check for flow stopped & enough free: release flow control
240 :       ldy     Stopped         ; (34)
241         beq     :+
242         cmp     #63
243         bcc     :+
244         lda     #$00
245         sta     Stopped
246         lda     RtsOff
247         ora     #%00001000
248         sta     ACIA::CMD
249
250         ; Get byte from buffer
251 :       ldy     RecvHead        ; (41)
252         lda     RecvBuf,y
253         inc     RecvHead
254         inc     RecvFreeCnt
255         ldx     #$00            ; (59)
256         sta     (ptr1,x)
257         txa                     ; Return code = 0
258         rts
259
260 ;----------------------------------------------------------------------------
261 ; PUT: Output character in A.
262 ; Must return an SER_ERR_xx code in a/x.
263
264 PUT:
265         ; Try to send
266         ldy     SendFreeCnt
267         iny                     ; Y = $FF?
268         beq     :+
269         pha
270         lda     #$00            ; TryHard = false
271         jsr     TryToSend
272         pla
273
274         ; Put byte into send buffer & send
275 :       ldy     SendFreeCnt
276         bne     :+
277         lda     #<SER_ERR_OVERFLOW
278         ldx     #>SER_ERR_OVERFLOW
279         rts
280
281 :       ldy     SendTail
282         sta     SendBuf,y
283         inc     SendTail
284         dec     SendFreeCnt
285         lda     #$FF            ; TryHard = true
286         jsr     TryToSend
287         lda     #<SER_ERR_OK
288         tax
289         rts
290
291 ;----------------------------------------------------------------------------
292 ; SER_STATUS: Return the status in the variable pointed to by ptr1.
293 ; Must return an SER_ERR_xx code in a/x.
294
295 SER_STATUS:
296         lda     ACIA::STATUS
297         ldx     #$00
298         sta     (ptr1,x)
299         txa                     ; SER_ERR_OK
300         rts
301
302 ;----------------------------------------------------------------------------
303 ; IOCTL: Driver defined entry point. The wrapper will pass a pointer to ioctl
304 ; specific data in ptr1, and the ioctl code in A.
305 ; Must return an SER_ERR_xx code in a/x.
306
307 IOCTL:
308         lda     #<SER_ERR_INV_IOCTL
309         ldx     #>SER_ERR_INV_IOCTL
310         rts
311
312 ;----------------------------------------------------------------------------
313 ; IRQ: Called from the builtin runtime IRQ handler as a subroutine. All
314 ; registers are already saved, no parameters are passed, but the carry flag
315 ; is clear on entry. The routine must return with carry set if the interrupt
316 ; was handled, otherwise with carry clear.
317
318 IRQ:
319         ldx     Index           ; Check for open port
320         beq     Done
321         lda     ACIA::STATUS,x  ; Check ACIA status for receive interrupt
322         and     #$08
323         beq     Done            ; Jump if no ACIA interrupt
324         lda     ACIA::DATA,x    ; Get byte from ACIA
325         ldy     RecvFreeCnt     ; Check if we have free space left
326         beq     Flow            ; Jump if no space in receive buffer
327         ldy     RecvTail        ; Load buffer pointer
328         sta     RecvBuf,y       ; Store received byte in buffer
329         inc     RecvTail        ; Increment buffer pointer
330         dec     RecvFreeCnt     ; Decrement free space counter
331         ldy     RecvFreeCnt     ; Check for buffer space low
332         cpy     #33
333         bcc     Flow            ; Assert flow control if buffer space low
334         rts                     ; Interrupt handled (carry already set)
335
336         ; Assert flow control if buffer space too low
337 Flow:   lda     RtsOff
338         sta     ACIA::CMD,x
339         sta     Stopped
340         sec                     ; Interrupt handled
341 Done:   rts
342
343 ;----------------------------------------------------------------------------
344 ; Try to send a byte. Internal routine. A = TryHard
345
346 TryToSend:
347         sta     tmp1            ; Remember tryHard flag
348 Again:  lda     SendFreeCnt
349         cmp     #$FF
350         beq     Quit            ; Bail out
351
352         ; Check for flow stopped
353         lda     Stopped
354         bne     Quit            ; Bail out
355
356         ; Check that ACIA is ready to send
357         lda     ACIA::STATUS
358         and     #$10
359         bne     Send
360         bit     tmp1            ; Keep trying if must try hard
361         bmi     Again
362 Quit:   rts
363
364         ; Send byte and try again
365 Send:   ldy     SendHead
366         lda     SendBuf,y
367         sta     ACIA::DATA
368         inc     SendHead
369         inc     SendFreeCnt
370         jmp     Again