When you read controller port $4016 or $4017, bit 0 represents whether a button is pressed on the plug-in controller (NES) or hardwired controller (Famicom), bit 1 represents whether the corresponding button is pressed on the expansion controller (Famicom), and bits 7-2 are unrelated to button presses. A 4-player game for Famicom will treat bit 0 and bit 1 separately by shifting each into carry (lsr a) and then shifting it out into that player's variable:Friendly_Noob wrote:GetInput2 is a loop checking $4016 8 times, because there are 8 buttons. Then depending on if the button is pressed or not 00000000 or 00000001 is ANDed with 00000011 ($03) so it's 00000000 or 00000001 again?
Code: Select all
lda $4016
lsr a
rol player1_buttons
lsr a
rol player2_buttons
To detect the difference between pressing a button and holding a button:Also, why should I store oldbutton?
Code: Select all
lda oldbutton ; A = buttons pressed last frame
eor #$FF ; A = buttons NOT pressed last frame
and button ; A = buttons pressed this frame but not last frame
sta newbutton
Code: Select all
JOYPAD1 = $4016
JOYPAD2 = $4017
.zeropage
buttons1: .res 1
buttons2: .res 1
.code
readjoy:
lda #$01
sta JOYPAD1
sta buttons2 ; player 2's buttons double as a ring counter
lsr a ; now A is 0
sta JOYPAD1
@loop:
lda JOYPAD1
and #$03 ; ignore bits other than controller
cmp #$01 ; Set carry if and only if nonzero
rol buttons1 ; Carry -> bit0; bit 7 -> Carry
lda JOYPAD2 ; Repeat
and #$03
cmp #$01
rol buttons2 ; Carry -> bit0; bit 7 -> Carry
bcc @loop ; Loop is done when the last bit of $01 is shifted out
rts