Use the SNES/NES controller in Assembly
; Code contributed by Stefany Allaire on Discord[edit | edit source]
; This is test code that goes through all the ports and mux the NES and SNES...[edit | edit source]
NES_CTRL = $D880
NES_STAT = $D880
NES_EN = $01
NES_MODE = $04
NES_TRIG = $80
NES_DONE = $40
NES_JOY0 = $D884
NES_JOY1 = $D886
NES_JOY2 = $D888
NES_JOY3 = $D88A
SNES_JOY0_LO = $D884
SNES_JOY0_HI = $D885
SNES_JOY1_LO = $D886
SNES_JOY1_HI = $D887
SNES_JOY2_LO = $D888
SNES_JOY2_HI = $D889
SNES_JOY3_LO = $D88A
SNES_JOY3_HI = $D88B
Test_4N4S:
jsr SetIOPage0
; Test NES First
; Enable and Begin a Sample
NES_Start_Again:
lda #NES_EN
sta NES_CTRL
lda NES_CTRL
ora #NES_TRIG
sta NES_CTRL
and #~NES_TRIG
sta NES_CTRL
NESWait2beDone
lda NES_STAT
and #NES_DONE
cmp #NES_DONE
bne NESWait2beDone
; When here, the Joystick Sampling is done
lda NES_JOY0
eor #$FF
ldx #$00
jsr Byte2Hex
lda NES_JOY1
eor #$FF
ldx #$03
jsr Byte2Hex
lda NES_JOY2
eor #$FF
ldx #$06
jsr Byte2Hex
lda NES_JOY3
eor #$FF
ldx #$09
jsr Byte2Hex
lda NES_JOY3
eor #$FF
and #$C0
cmp #$C0
beq NESDoneWithTest
; Let's do the SNES
lda #NES_EN | NES_MODE
sta NES_CTRL
lda NES_CTRL
ora #NES_TRIG
sta NES_CTRL
and #~NES_TRIG
sta NES_CTRL
SNESWait2beDone
lda NES_STAT
and #NES_DONE
cmp #NES_DONE
bne SNESWait2beDone
; When here, the Joystick Sampling is done
lda SNES_JOY0_HI
and #$0F
eor #$0F
ldx #$50
jsr Byte2Hex
lda SNES_JOY0_LO
eor #$FF
ldx #$52
jsr Byte2Hex
lda SNES_JOY1_HI
and #$0F
eor #$0F
ldx #$55
jsr Byte2Hex
lda SNES_JOY1_LO
eor #$FF
ldx #$57
jsr Byte2Hex
lda SNES_JOY2_HI
and #$0F
eor #$0F
ldx #$5A
jsr Byte2Hex
lda SNES_JOY2_LO
eor #$FF
ldx #$5C
jsr Byte2Hex
lda SNES_JOY3_HI
and #$0F
eor #$0F
ldx #$5F
jsr Byte2Hex
lda SNES_JOY3_LO
eor #$FF
ldx #$61
jsr Byte2Hex
jmp NES_Start_Again
NESDoneWithTest:
lda #$00
sta NES_CTRL
rts
Byte2Hex:
sta $50
stx $51
jsr SetIOPage2
; Hi Part
lda $50
and #$F0
lsr a
lsr a
lsr a
lsr a
tax
lda HEX, x ;
ldx $51
sta $c320,x
; Low Part
lda $50
and #$0F
tax
lda HEX, x
ldx $51
sta $c321,x
jsr SetIOPage0
rts