I do something like this...
Code:
enum $000
joypad1: .db 0
joypad1_last_frame: .db 0
.ende
;A, B, Select, Start, Up, Down, Left, Right
right = $01
left = $02
down = $04
up = $08
start = $10
select = $20
b_button = $40
a_button = $80
Edit, fixed wrong button order.
(in NMI, after all PPU / Sprite / Scrolling stuff)
Code:
LDA joypad1
STA joypad1_last_frame ; save the last frame's buttons
ReadController:
LDX #$01
STX $4016
DEX
STX $4016
LDX #$08
ControllerLoop:
LDA $4016
LSR
ROL joypad1
DEX
BNE ControllerLoop
;new buttons now stored in joypad1 RAM address
(if you want to do something, anytime B is pressed, even if held)
Code:
LDA joypad1
AND #b_button ;check if b press
BEQ b_exit
JSR Whatever_B_Does
b_exit:
LDA joypad1
AND #a_button ;check if a press
BEQ a_exit
JSR Whatever_A_Does
a_exit:
;etc, on down the line
(if you want to do something, only on NEW B presses...)
Code:
LDA joypad1_last_frame
AND #b_button ;check if b still held from last frame
BNE b_exit
LDA joypad1
AND #b_button ;check if new b press
BEQ b_exit
JSR Whatever_B_Does
b_exit:
;check some other button now
the AND here does a bit mask...removing all the other buttons from the Accumulator, so that we are only concerned with just that bit. If the result is zero (because the button is not pressed) it will set the z flag, and we can BEQ / BNE away from the subroutine.
_________________
nesdoug.com -- blog/tutorial on programming for the NES