I have some code that was mostly given to me in another post, which I learned tremendously from. However now having some problems actually putting the pieces together correctly.
If using the emulators, would the arrow keys on my PC automatically replace the arrow keys from a real nes gamepad? Should I be using a PC joystick?
I compiled with nesasm and using Nintendulator. (But also tried Jnes, Nestopia.)
I wanted to use the code to test the reading of game pad keys and assumed the emulator made use of the arrow keys on the PC.
In the code I just wanted to press the right arrow and make some noise.
I left in the subroutine lables for each of the buttons, however I just wanted to test one button (the right arrow). That's why the other key_press subroutines are empty.
Thanks,
T
If using the emulators, would the arrow keys on my PC automatically replace the arrow keys from a real nes gamepad? Should I be using a PC joystick?
I compiled with nesasm and using Nintendulator. (But also tried Jnes, Nestopia.)
I wanted to use the code to test the reading of game pad keys and assumed the emulator made use of the arrow keys on the PC.
In the code I just wanted to press the right arrow and make some noise.
I left in the subroutine lables for each of the buttons, however I just wanted to test one button (the right arrow). That's why the other key_press subroutines are empty.
Code:
;Nes Gamepad demo program
;-------------------
.inesprg 1
.ineschr 0
.bank 1 ; don't know if I need this.
.org $FFFA
.dw 0
.dw updatejoy ; should my code start here?
.dw 0
.bank 0
.org $8000
;;Equates for masking
key_a EQU %00000001 ; A button press
key_b EQU %00000010 ; B
key_select EQU %00000100 ; select
key_start EQU %00001000 ; start
key_up EQU %00010000 ; up arrow
key_down EQU %00100000 ; down
key_left EQU %01000000 ; left arrow
key_right EQU %10000000 ; right
button_state EQU $00 ; my button_state variable
updatejoy:
LDA #1 ; first, strobe the joypad
STA $4016
LDA #0
STA $4016
; now we're going to loop 8 times (once for each button), and
; rotate each button's status (in the carry flag) into our button_state variable
LDX #$08 ; set X to 8 (the number of times we want to loop)
joyloop:
LDA $4016 ; get button state
LSR A ; shift it into the C flag
ROR button_state ; rotate C flag into our button_state variable
DEX ; decrement X (our loop counter)
BNE joyloop ; jump back to our loop until X is zero
RTS
LDA button_state
AND key_right ; masking using AND. Check right arrow press
BNE right_is_pressed ; branch to subroutine
LDA button_state
AND key_left ; check left arrow press
BNE left_is_pressed
LDA button_state
AND key_down ; check down arrow press
BNE down_is_pressed
LDA button_state
AND key_up ; check up arrow press
BNE up_is_pressed
LDA button_state
AND key_start ; check start button press
BNE start_is_pressed
LDA button_state
AND key_select ; check select button
BNE select_is_pressed
LDA button_state
AND key_b ; check b button press
BNE b_is_pressed
LDA button_state
AND key_a ; check a button press
BNE a_is_pressed
right_is_pressed: ; play sound when right arrow is pressed.
lda #$FF ;
sta $4000 ;
lda #%11011011 ;
sta $4001 ;
lda #$A5
sta $4002
lda #$AB
sta $4003
lda #%00000001
sta $4015
jmp updatejoy ; is this jmp right?
left_is_pressed:
rts
down_is_pressed:
rts
up_is_pressed:
rts
start_is_pressed:
rts
select_is_pressed:
rts
b_is_pressed:
rts
a_is_pressed:
rts
;-------------------
.inesprg 1
.ineschr 0
.bank 1 ; don't know if I need this.
.org $FFFA
.dw 0
.dw updatejoy ; should my code start here?
.dw 0
.bank 0
.org $8000
;;Equates for masking
key_a EQU %00000001 ; A button press
key_b EQU %00000010 ; B
key_select EQU %00000100 ; select
key_start EQU %00001000 ; start
key_up EQU %00010000 ; up arrow
key_down EQU %00100000 ; down
key_left EQU %01000000 ; left arrow
key_right EQU %10000000 ; right
button_state EQU $00 ; my button_state variable
updatejoy:
LDA #1 ; first, strobe the joypad
STA $4016
LDA #0
STA $4016
; now we're going to loop 8 times (once for each button), and
; rotate each button's status (in the carry flag) into our button_state variable
LDX #$08 ; set X to 8 (the number of times we want to loop)
joyloop:
LDA $4016 ; get button state
LSR A ; shift it into the C flag
ROR button_state ; rotate C flag into our button_state variable
DEX ; decrement X (our loop counter)
BNE joyloop ; jump back to our loop until X is zero
RTS
LDA button_state
AND key_right ; masking using AND. Check right arrow press
BNE right_is_pressed ; branch to subroutine
LDA button_state
AND key_left ; check left arrow press
BNE left_is_pressed
LDA button_state
AND key_down ; check down arrow press
BNE down_is_pressed
LDA button_state
AND key_up ; check up arrow press
BNE up_is_pressed
LDA button_state
AND key_start ; check start button press
BNE start_is_pressed
LDA button_state
AND key_select ; check select button
BNE select_is_pressed
LDA button_state
AND key_b ; check b button press
BNE b_is_pressed
LDA button_state
AND key_a ; check a button press
BNE a_is_pressed
right_is_pressed: ; play sound when right arrow is pressed.
lda #$FF ;
sta $4000 ;
lda #%11011011 ;
sta $4001 ;
lda #$A5
sta $4002
lda #$AB
sta $4003
lda #%00000001
sta $4015
jmp updatejoy ; is this jmp right?
left_is_pressed:
rts
down_is_pressed:
rts
up_is_pressed:
rts
start_is_pressed:
rts
select_is_pressed:
rts
b_is_pressed:
rts
a_is_pressed:
rts
Thanks,
T