SPI is a rather simple bus. It only uses 4 pins, SCK, MISO, MOSI, CS. Long story short, joyport SEL and CLR can be used for SCK and MISO, any of the D0-D3 lines can be used for MOSI, and CS can be directly wired to GND. The code may look like this:
Code: Select all
.zp
spi_polarity .ds 1
spi_phase .ds 1
.code
;;
;; Function: spi_mode
;; Set SPI phase and polarity.
;;
;; Parameters:
;; A - mode
;;
spi_mode:
pha
and #$02
sta <spi_polarity
pla
and #$01
sta <spi_phase
rts
Code: Select all
;;
;; Function: spi_write
;;
;; Parameters:
;; _al - byte to send
;;
;; Return:
;; _ah - byte read
;;
spi_write:
stz <_ah
ldx #$08
@loop:
lda <_al
and #$01
sta joyport
lda <spi_phase
bne @l0
lda joyport
lsr A
rol <_ah
@l0:
lda <spi_polarity
eor #$02
sta joyport
; todo : delay
lda <spi_phase
beq @l1
lda joyport
lsr A
rol <_ah
@l1:
lda <spi_polarity
sta joyport
; todo : delay
dex
bne @loop
rts