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
27 .export _rs232_init, _rs232_params, _rs232_done, _rs232_get
28 .export _rs232_put, _rs232_pause, _rs232_unpause, _rs232_status
34 NmiExit = $ff33 ;exit address for nmi
40 ;----------------------------------------------------------------------------
46 DropCnt: .res 4 ; Number of bytes lost from rx buffer full
47 Initialized: .res 1 ; Flag indicating driver is initialized
48 Stopped: .res 1 ; Flow-stopped flag
50 Errors: .res 1 ; Number of bytes received in error, low byte
51 Turbo232: .res 1 ; Flag indicating turbo-232
52 HackedFlag: .res 1 ; Flag indicating hacked-crystal swiftlink
53 CpuSpeed: .res 1 ; In MHz
54 RecvHead: .res 1 ; Head of receive buffer
55 RecvTail: .res 1 ; Tail of receive buffer
56 RecvFreeCnt: .res 1 ; Number of bytes in receive buffer
57 SendHead: .res 1 ; Head of send buffer
58 SendTail: .res 1 ; Tail of send buffer
59 SendFreeCnt: .res 1 ; Number of bytes free in send buffer
60 BaudCode: .res 1 ; Current baud in effect
62 ; Send and receive buffers: 256 bytes each
67 NmiContinue: .byte $4c ; JMP instruction for NMI save -- continue
68 NmiSave: .res 2 ; normal NMI handler
70 ; Switftlink register offsets
71 RegData = 0 ; Data register
72 RegStatus = 1 ; Status register
73 RegCommand = 2 ; Command register
74 RegControl = 3 ; Control register
75 RegClock = 7 ; Turbo232 external baud-rate generator
81 ;----------------------------------------------------------------------------
83 ; unsigned char __fastcall__ rs232_init (char hacked);
84 ; /* Initialize the serial port, install the interrupt handler. The parameter
85 ; * must be true (non zero) for a hacked swiftlink and false (zero) otherwise.
90 bit Initialized ;** shut down if started
96 ;** set hacked-crystal
100 ;** check for turbo-232
110 ;** get C128 cpu speed
118 ;** check for super-cpu at 20 MHz
127 ;** initialize buffers & control
151 ;** set default to 2400-8N1, enable interrupts
159 @L5: sta ACIA+RegControl
176 ;----------------------------------------------------------------------------
178 ; unsigned char __fastcall__ rs232_params (unsigned char params, unsigned char parity);
179 ; /* Set the port parameters. Use a combination of the #defined values above. */
181 ; Set communication parameters.
183 ; baud rates stops word | parity
184 ; --------------------- ----- ----- | ---------
185 ; $00=50 $08=9600 $00=1 $00=8 | $00=none
186 ; $01=110 $09=19200 $80=2 $20=7 | $20=odd
187 ; $02=134.5 $0a=38400 $40=6 | $60=even
188 ; $03=300 $0b=57600 $60=5 | $A0=mark
189 ; $04=600 $0c=115200 | $E0=space
190 ; $05=1200 $0d=230400
191 ; $06=2400 $0e=future
192 ; $07=4800 $0f=future
198 jmp NotInitialized ; Return an error code
206 ; Check cpu speed against baud rate
220 @L2: lda #RS_ERR_BAUD_TOO_FAST
223 ; Set baud/parameters
234 lda #RS_ERR_BAUD_NOT_AVAIL
242 lda #RS_ERR_BAUD_NOT_AVAIL
279 .byte $ff,$ff,$ff,$05,$06,$07,$08,$0a,$0c,$0e,$0f,$10,$20,$30,$ff,$ff
281 .byte $01,$03,$04,$06,$07,$08,$0a,$0c,$0e,$0f,$ff,$ff,$00,$ff,$ff,$ff
282 ;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
283 ;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
284 ;out masks: $0F=Baud, val$FF=err
285 ; $30=t232ExtBaud: $00=none, $10=57.6, $20=115.2, $30=230.4
288 ;----------------------------------------------------------------------------
290 ; unsigned char __fastcall__ rs232_done (void);
291 ; /* Close the port, deinstall the interrupt hander. You MUST call this function
292 ; * before terminating the program, otherwise the machine may crash later. If
293 ; * in doubt, install an exit handler using atexit(). The function will do
294 ; * nothing, if it was already called.
300 bit Initialized ;** check initialized
303 ; Stop interrupts, drop DTR
324 ;----------------------------------------------------------------------------
326 ; unsigned char __fastcall__ rs232_get (char* B);
327 ; /* Get a character from the serial port. If no characters are available, the
328 ; * function will return RS_ERR_NO_DATA, so this is not a fatal error.
334 bpl NotInitialized ; Jump if not initialized
336 ; Check for bytes to send
339 stx ptr1+1 ; Store pointer to received char
346 ; Check for buffer empty
355 ; Check for flow stopped & enough free: release flow control
367 ; Get byte from buffer
375 txa ; Return code = 0
378 ;----------------------------------------------------------------------------
380 ; RS232 module not initialized
383 lda #<RS_ERR_NOT_INITIALIZED
384 ldx #>RS_ERR_NOT_INITIALIZED
387 ;----------------------------------------------------------------------------
389 ; unsigned char __fastcall__ rs232_put (char B);
390 ; /* Send a character via the serial port. There is a transmit buffer, but
391 ; * transmitting is not done via interrupt. The function returns
392 ; * RS_ERR_OVERFLOW if there is no space left in the transmit buffer.
398 bpl NotInitialized ; Jump if not initialized
410 ; Put byte into send buffer & send
428 ;----------------------------------------------------------------------------
430 ; unsigned char __fastcall__ rs232_pause (void);
431 ; /* Assert flow control and disable interrupts. */
436 bpl NotInitialized ; Jump if not initialized
438 ; Assert flow control
444 ; Delay for flow stop to be received
461 ; Delay times: 32 byte-receive times in milliseconds, or 100 max.
462 ; Formula = 320,000 / baud
464 .byte 100,100,100,100,100,100,100,067,034,017,009,006,003,002,001,001
465 ;in: 0 1 2 3 4 5 6 7 8 9 a b c d e f
466 ;baud50 110 134 3 6 12 24 48 96 19 38 57 115 230 exp exp
469 ;----------------------------------------------------------------------------
471 ; unsigned char __fastcall__ rs232_unpause (void);
472 ; /* Re-enable interrupts and release flow control */
477 bpl NotInitialized ; Jump if not initialized
479 ; Re-enable rx interrupts & release flow control
487 ; Poll for stalled char & exit
494 ;----------------------------------------------------------------------------
496 ; unsigned char __fastcall__ rs232_status (unsigned char* status,
497 ; unsigned char* errors);
498 ; /* Return the serial port status. */
513 @L1: lda ACIA+RegStatus
516 jsr PollReceive ; bug-recovery hack
523 ;----------------------------------------------------------------------------
526 ; C128 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=33, ROMexit=30
527 ; C64 NMI overhead=76 cycles: int=7, maxLatency=6, ROMenter=34, ROMexit=29
529 ; timing: normal=76+43+9=128 cycles, assertFlow=76+52+9=137 cycles
531 ; C128 @ 115.2k: 177 cycles avail (fast)
532 ; C64 @ 57.6k: 177 cycles avail, worstAvail=177-43? = 134
533 ; SCPU @ 230.4k: 868 cycles avail: for a joke!
535 ; Because of the C128 banking, the NMI handler must go into the non banked
536 ; memory, since the ROM NMI entry point will switch to a configuration where
537 ; only the lowest 16K of RAM are visible. We will place the NMI handler into
538 ; the LOWCODE segment and map this segment into the lower 16K in the linker
544 lda #MMU_CFG_CC65 ;(2)
546 lda ACIA+RegStatus ;(4) ;status ;check for byte received
549 lda ACIA+RegStatus ;(4) opt ;status ;check for receive errors
553 @L1: lda ACIA+RegData ;(4) ;data ;get byte and put into receive buffer
560 cpx #33 ;(2) ;check for buffer space low
564 ; Assert flow control
566 @L2: lda RtsOff ;(3) ;assert flow control if buffer space too low
567 sta ACIA+RegCommand ;(4) ;command
573 @L3: inc DropCnt+0 ;not time-critical
587 ;----------------------------------------------------------------------------
588 ; Try to send a byte. Internal routine. A = TryHard
591 sta tmp1 ; Remember tryHard flag
596 ; Check for flow stopped
601 ;** check that swiftlink is ready to send
603 @L2: lda ACIA+RegStatus
606 bit tmp1 ;keep trying if must try hard
610 ;** send byte and try again
619 ;----------------------------------------------------------------------------
621 ; PollReceive - poll for rx char
622 ; This function is useful in odd cases where the 6551 has a character in
623 ; it but it fails to raise an NMI. It might be edge-triggering conditions?
624 ; Actually, I'm not entirely sure that this condition can still arrise, but
625 ; calling this function does no harm.
643 ;----------------------------------------------------------------------------
645 ; DelayMs : delay for given number of milliseconds
646 ; This implementation isn't very rigerous; it merely delays for the
647 ; approximate number of clock cycles for the processor speed.
649 ; repeat for number of milliseconds:
650 ; repeat for number of MHz of cpu speed:
651 ; delay for 1017 clock cycles
654 DelayMs: ;( .A=milliseconds )
658 bne @L3 ;(3) // 1017 cycles