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