Didn't I hear something about how you have to read it every scanline? Or is that just when you are checking to see if there was a hit?
I'm assuming actually that's just during the reading phase that you have to check every scanline. But what I do in my game is I have three variables:
ControlPrevious
ControlCurrent
ControlTrigger
And this is how my controller code looks:
Code: Select all
lda ControlCurrent
sta ControlPrevious
ldx #1
stx $4016
dex
stx $4016
ldy #8
-
lda $4016
lsr a
rol ControlCurrent
dey
bne -
lda ControlCurrent
and ControlPrevious
eor ControlCurrent
sta ControlTrigger
For ControlCurrent and ControlPrevious, it's obvious what those hold. Current holds the current button press bits for this frame. Previous holds the previous frame's button press status. And of course, each bit in the byte holds the status for the corresponding button:
Bit 7 - A button status
Bit 6 - B button status
Bit 5 - Select button Status (or is it start?)
Bit 4 - Start button Status (or is it select? I get these two confused all the time)
Bit 3 - Up button status
Bit 2 - Down button status
Bit 1 - Left button status
Bit 0 - Right button status
However, ControlTrigger holds which buttons have been NEWLY pressed. If you press A this frame, and it hasn't been pressed last frame, it will return a 1 in bit 7. However, if you have pressed A last frame, it will not return a 1 in bit 7. I call it "ControlTrigger" because it kind of reminds me of a trigger of a pistol being pulled or something where it only has effect the instant it's pulled. I didn't know what else to call it. It's really nice to have this byte because I can do things like:
lda ControlTrigger
and #BButton
bne Shoot
.... blah blah code for not shooting
Shoot:
.... blah blah code for shooting
I would absolutely stay away from reading hardware registers in game logic code as much as possible. It is kind of a bug waiting to happen in my opinion. So I would put all the information for button presses and trigger status into bytes in RAM.