Skip to content

Commit

Permalink
Improve performance without breaking SNES timing, complete scan now 1…
Browse files Browse the repository at this point in the history
…,739 cycles = +33 us compared to current master
  • Loading branch information
stefan-b-jakobsson committed Nov 23, 2024
1 parent 9d762dc commit 291ef21
Showing 1 changed file with 29 additions and 41 deletions.
70 changes: 29 additions & 41 deletions kernal/drivers/x16/joystick.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ mintabs_len = mintab0_len+mintab1_len

j0tmp: .res 1 ; keyboard joystick temp
joy0: .res 3 ; keyboard joystick status
joy1: .res 3 ; joystick 1 status
joy2: .res 3 ; joystick 2 status
joy3: .res 3 ; joystick 3 status
joy4: .res 3 ; joystick 4 status
joy1: .res 2 ; joystick 1 status
joy2: .res 2 ; joystick 2 status
joy3: .res 2 ; joystick 3 status
joy4: .res 2 ; joystick 4 status
joycon: .res 1 ; joystick connected flags

; Keep these allocations adjacent for code in joystick_ps2_keycodes

Expand Down Expand Up @@ -135,38 +136,12 @@ l2: ; Drive SNES clock low for approx 6 us and read data (SNES controller doesn'
joy_detect:
; Read one extra bit from SNES controller to detect if connected (0 = connected, 1 = not connected)
stz nes_data ; Drive SNES clock low for approx 6 us (SNES controller doesn't change when low)

; Set default values while waiting...
stz joy1+2 ; Set joy 1 connected as default
stz joy2+2 ; Set joy 2 connected as default
stz joy3+2 ; Set joy 3 connected as default
stz joy4+2 ; Set joy 4 connected as default

; And wait some more...
jsr wait_3us

jsr wait_6us
lda nes_data ; Read all controller bits
stx nes_data ; Drive SNES clock high

; Process while SNES clock is high (bits change)
ldy #$ff

rol ; Move bit 7 (joy 1) into C
bcc :+
sty joy1+2 ; Set joy 1 not connected

: rol ; Move bit 6 (joy 2) into C
bcc :+
sty joy2+2 ; Set joy 2 not connected

: rol ; Move bit 5 (joy 3) into C
bcc :+
sty joy3+2 ; Set joy 3 not connected

: rol ; Move bit 4 (joy 4) into C
bcc :+
sty joy4+2 ; Set joy 4 not connected

; Store joytstick connection flags
sta joycon
:
KVARS_END_TRASH_A_NZ
rts
Expand Down Expand Up @@ -204,6 +179,7 @@ wait_3us:
joystick_get:
KVARS_START_TRASH_X_NZ
tax
ldy #$ff
beq @0 ; -> joy0
dex
beq @1 ; -> joy1
Expand All @@ -215,7 +191,6 @@ joystick_get:
beq @4 ; -> joy4
lda #$ff
tax
tay
bra @5

@0:
Expand All @@ -225,25 +200,38 @@ joystick_get:
bra @5

@1:
lda joy1
lda joycon
and #bit_data1
bne :+
ldy #0
: lda joy1
ldx joy1+1
ldy joy1+2
bra @5

@2:
lda joy2
lda joycon
and #bit_data2
bne :+
ldy #0
: lda joy2
ldx joy2+1
ldy joy2+2
bra @5

@3:
lda joy3
lda joycon
and #bit_data3
bne :+
ldy #0
: lda joy3
ldx joy3+1
ldy joy3+2
bra @5

@4:
lda joy4
lda joycon
and #bit_data4
bne :+
ldy #0
: lda joy4
ldx joy4+1
ldy joy4+2

Expand Down

0 comments on commit 291ef21

Please sign in to comment.