; Serial I/O at 57600 bits/sec on controller port 2 ; ; Uses stack and register A only, and doesn't mind page crossing ; (uses subroutines instead of loops). ; Initialize serial and wait so PC won't receive junk for the first byte. ; Preserved: A, X, Y serial_init: pha lda #1 sta JOY1 delay_msec 10 pla rts ; Write byte A to serial ; Preserved: X, Y serial_write: clc jsr serial_write_bit ; start nop ; TODO: why this extra delay of 6? jsr serial_write_bit2 ; bit 0 jsr serial_write_bit ; bit 1 jsr serial_write_bit ; bit 2 jsr serial_write_bit ; bit 3 jsr serial_write_bit ; bit 4 jsr serial_write_bit ; bit 5 jsr serial_write_bit ; bit 6 jsr serial_write_bit ; bit 7 sec ; 2 stop bit serial_write_bit2: nop ; 2 nop ; 2 serial_write_bit: ; 6 jsr pha ; 3 rol a ; 2 and #1 ; 2 sta JOY1 ; 4 pla ; 4 ror a ; 2 nop ; 2 rts ; 6 ; Wait for and read byte from serial into A ; Preserved: X, Y serial_read: lda #1 ; 2 wait_start_bit: bit JOY2 ; 4 beq wait_start_bit ; 3 ; 4-10 latency (average 7) jsr serial_start_bit ; 38 delay jsr serial_read_bit ; bit 0 jsr serial_read_bit ; bit 1 jsr serial_read_bit ; bit 2 jsr serial_read_bit ; bit 3 jsr serial_read_bit ; bit 4 jsr serial_read_bit ; bit 5 jsr serial_read_bit ; bit 6 nop ; bit 7 serial_start_bit: nop nop serial_read_bit: ; 6 jsr pha ; 3 sec ; 2 rol a ; 2 eor JOY2 ; 4 lsr a ; 2 pla ; 4 ror a ; 2 rts ; 6