2 ; SwiftLink/Turbo-232 v0.90 device driver, by Craig Bruce, 14-Apr-1998.
4 ; This software is Public Domain. It is in Buddy assembler format.
6 ; This device driver uses the SwiftLink RS-232 Serial Cartridge, available from
7 ; Creative Micro Designs, Inc, and also supports the extensions of the Turbo232
8 ; Serial Cartridge. Both devices are based on the 6551 ACIA chip. It also
9 ; supports the "hacked" SwiftLink with a 1.8432 MHz crystal.
11 ; The code assumes that the kernal + I/O are in context. On the C128, call
12 ; it from Bank 15. On the C64, don't flip out the Kernal unless a suitable
13 ; NMI catcher is put into the RAM under then Kernal. For the SuperCPU, the
14 ; interrupt handling assumes that the 65816 is in 6502-emulation mode.
16 ;--------------------------------------------------------------------------
18 ; Adapted for the use with the cc65 runtime library by
19 ; Ullrich von Bassewitz (uz@musoftware.de) 02-May-1999.
21 ; All external functions are C callable, the return value is an error code.
25 .importzp ptr1, ptr2, tmp1, tmp2
26 .importzp acia, RecvBuf, SendBuf
28 .import sys_bank, restore_bank
29 .export _rs232_init, _rs232_params, _rs232_done, _rs232_get
30 .export _rs232_put, _rs232_pause, _rs232_unpause, _rs232_status
36 ;----------------------------------------------------------------------------
42 DropCnt: .res 4 ; Number of bytes lost from rx buffer full
43 Initialized: .res 1 ; Flag indicating driver is initialized
44 Stopped: .res 1 ; Flow-stopped flag
46 Errors: .res 1 ; Number of bytes received in error, low byte
47 BaudCode: .res 1 ; Current baud in effect
49 ; Segment, the RS232 buffers are in
52 ; UART register offsets
53 RegData = 0 ; Data register
54 RegStatus = 1 ; Status register
55 RegCommand = 2 ; Command register
56 RegControl = 3 ; Control register
58 ; Error codes. Beware: The codes must match the codes in the C header file
59 ErrNotInitialized = $01
68 ;----------------------------------------------------------------------------
70 ; unsigned char __fastcall__ rs232_init (char hacked);
71 ; /* Initialize the serial port, install the interrupt handler. The parameter
72 ; * must be true (non zero) for a hacked swiftlink and false (zero) otherwise.
77 bit Initialized ;** shut down if started
83 ; Initialize buffers & control
96 ; Set default to 2400-8N1, enable interrupts
98 jsr sys_bank ; Switch indirect to system bank
124 ;----------------------------------------------------------------------------
126 ; unsigned char __fastcall__ rs232_params (unsigned char params, unsigned char parity);
127 ; /* Set the port parameters. Use a combination of the #defined values above. */
129 ; Set communication parameters.
131 ; baud rates stops word | parity
132 ; --------------------- ----- ----- | ---------
133 ; $00=50 $08=9600 $00=1 $00=8 | $00=none
134 ; $01=110 $09=19200 $80=2 $20=7 | $20=odd
135 ; $02=134.5 $0a=38400 $40=6 | $60=even
136 ; $03=300 $0b=57600 $60=5 | $A0=mark
137 ; $04=600 $0c=115200 | $E0=space
138 ; $05=1200 $0d=230400
139 ; $06=2400 $0e=future
140 ; $07=4800 $0f=future
144 jsr CheckInitialized ;** check initialized
154 ; Set baud/parameters
166 @L5: jsr sys_bank ; Indirect segment to system bank
188 jsr restore_bank ; Restore indirect bank
195 .byte $01,$03,$04,$06,$07,$08,$0a,$0c,$0e,$0f,$ff,$ff,$ff,$ff,$ff,$ff
196 ;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
197 ;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
198 ;out masks: $0F=Baud, val$FF=err
201 ;----------------------------------------------------------------------------
203 ; unsigned char __fastcall__ rs232_done (void);
204 ; /* Close the port, deinstall the interrupt hander. You MUST call this function
205 ; * before terminating the program, otherwise the machine may crash later. If
206 ; * in doubt, install an exit handler using atexit(). The function will do
207 ; * nothing, if it was already called.
213 bit Initialized ;** check initialized
216 ; Stop interrupts, drop DTR
223 sty IndReg ; Set indirect to system bank
226 stx IndReg ; Restore old indirect bank
235 ;----------------------------------------------------------------------------
237 ; unsigned char __fastcall__ rs232_get (char* B);
238 ; /* Get a character from the serial port. If no characters are available, the
239 ; * function will return RS_ERR_NO_DATA, so this is not a fatal error.
244 jsr CheckInitialized ; Check if initialized
248 ; Check for bytes to send
251 stx ptr1+1 ; Store pointer to received char
258 ; Check for buffer empty
267 ; Check for flow stopped & enough free: release flow control
278 ldy #$0F ; Set indirect to system bank
284 ; Get byte from buffer
287 lda #BufferSeg ; Set indirect to buffer bank
291 stx IndReg ; Restore indirect bank
296 txa ; Return code = 0
299 ;----------------------------------------------------------------------------
301 ; unsigned char __fastcall__ rs232_put (char B);
302 ; /* Send a character via the serial port. There is a transmit buffer, but
303 ; * transmitting is not done via interrupt. The function returns
304 ; * RS_ERR_OVERFLOW if there is no space left in the transmit buffer.
309 jsr CheckInitialized ; Check initialized
323 ; Put byte into send buffer & send
331 ; There is enough room (character still in A)
334 ldy #BufferSeg ; Set indirect to buffer segment
338 stx IndReg ; Restore indirect bank
347 ;----------------------------------------------------------------------------
349 ; unsigned char __fastcall__ rs232_pause (void);
350 ; /* Assert flow control and disable interrupts. */
359 ; Assert flow control
363 jsr sys_bank ; Set indirect to system bank
367 ; Delay for flow stop to be received
379 jsr restore_bank ; Restore indirect segment
386 ; Delay times: 32 byte-receive times in milliseconds, or 100 max.
387 ; Formula = 320,000 / baud
389 .byte 100,100,100,100,100,100,100,067,034,017,009,006,003,002,001,001
390 ;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
391 ;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
394 ;----------------------------------------------------------------------------
396 ; unsigned char __fastcall__ rs232_unpause (void);
397 ; /* Re-enable interrupts and release flow control */
406 ; Re-enable rx interrupts & release flow control
414 sty IndReg ; Set indirect to system bank
417 stx IndReg ; Restore indirect bank
419 ; Poll for stalled char & exit
426 ;----------------------------------------------------------------------------
428 ; unsigned char __fastcall__ rs232_status (unsigned char* status,
429 ; unsigned char* errors);
430 ; /* Return the serial port status. */
444 ldx IndReg ; Save indirect segment
446 sta IndReg ; Set system bank as indirect segment
448 lda (acia),y ; Read status register
452 jsr PollReceive ; bug-recovery hack
460 ;----------------------------------------------------------------------------
462 ; RS232 interrupt handler.
463 ; The RS232 handler will be called with the system bank as indirect bank
464 ; and all registers saved.
469 lda (acia),y ; check for byte received
471 beq @L9 ; Nothing to receive
472 lda (acia),y ; check for receive errors
477 lda (acia),y ; get byte and put into receive buffer
483 sta (RecvBuf),y ; Store received character
485 sta IndReg ; Restore indirect segment
488 cpx #33 ; check for buffer space low
491 ; Assert flow control
493 @L2: lda RtsOff ; assert flow control if buffer space too low
501 @L3: inc DropCnt+0 ;not time-critical
511 ;----------------------------------------------------------------------------
513 ; CheckInitialized - internal check if initialized
514 ; Set carry and an error code if not initialized, clear carry and do not
515 ; change any registers if initialized.
521 lda #ErrNotInitialized
529 ;----------------------------------------------------------------------------
530 ; Try to send a byte. Internal routine. A = TryHard
533 sta tmp1 ; Remember tryHard flag
534 ldx IndReg ; Save indirect segment
536 sta IndReg ; Set system segment as indirect segment
541 ; Check for flow stopped
546 ; Check that the UART is ready to send
552 bit tmp1 ; Keep trying if must try hard
554 @L3: stx IndReg ; Restore indirect segment
557 ; Send byte and try again
572 ;----------------------------------------------------------------------------
574 ; PollReceive - poll for rx char
575 ; This function is useful in odd cases where the 6551 has a character in
576 ; it but it fails to raise an NMI. It might be edge-triggering conditions?
577 ; Actually, I'm not entirely sure that this condition can still arrise, but
578 ; calling this function does no harm.
582 ldx IndReg ; Save indirect segment
584 sta IndReg ; Set system bank as indirect segment
589 lda (acia),y ; Read a second time? ###
602 @L9: stx IndReg ; Restore indirect segment
605 ;----------------------------------------------------------------------------
607 ; DelayMs : delay for given number of milliseconds
608 ; This implementation isn't very rigerous; it merely delays for the
609 ; approximate number of clock cycles for the processor speed.
611 ; repeat for number of milliseconds:
612 ; repeat for number of MHz of cpu speed:
613 ; delay for 1017 clock cycles
616 DelayMs: ;( .A=milliseconds )
620 bne @L3 ;(3) // 1017 cycles