ldx #$09 LOOP: dex beq DONE lda $4016 and #$01 beq LOOP DONE: ... ; if x ==... 8=A 7=B 6=Se 5=St 4=U 3=D 2=L 1=R 0=Nonewould (likely be faster and) work too I guess!
LDA #$01 STA $4016 LDA #$00 STA $4016 ; tell both the controllers to latch buttons
2. Try pushing A and B buttons.
I hope this helps.
gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin
The controllers are accessed through memory port addresses $4016 and $4017. First you have to write the value $01 then the value $00 to port $4016. This tells the controllers to latch the current button positions. Then you read from $4016 for first player or $4017 for second player. The buttons are sent one at a time, in bit 0. If bit 0 is 0, the button is not pressed. If bit 0 is 1, the button is pressed.
Button status for each controller is returned in the following order: A, B, Select, Start, Up, Down, Left, Right.
LDA #$01
STA $4016
LDA #$00
STA $4016 ; tell both the controllers to latch buttons
LDA $4016 ; player 1 - A
LDA $4016 ; player 1 - B
LDA $4016 ; player 1 - Select
LDA $4016 ; player 1 - Start
LDA $4016 ; player 1 - Up
LDA $4016 ; player 1 - Down
LDA $4016 ; player 1 - Left
LDA $4016 ; player 1 - Right
Button information is only sent in bit 0, so we want to erase all the other bits. This can be done with the AND instruction. Each of the 8 bits is ANDed with the bits from another value. If the bit from both the first AND second value is 1, then the result is 1. Otherwise the result is 0.
If all this BunnyBoy wrote is undestood, try looking at the following (really bad, good-coding wise) example, hoping that you can find out what your code is missing. Think this way if this helps: when you ask about inputs, you get always _8_ answers, yes or no (1 or 0), one for each possible input (A,B,Sel,Start,U,D,L,R), which are in bit 0 of a read to $4016, forcefully in that specific order.
LDA #$01 STA $4016 LDA #$00 STA $4016 ; tell both the controllers to latch buttonsIt is ugly coding wise, but it works.
LDA $4016 ; player 1 - A, you don't care about the answer LDA $4016 ; player 1 - B, you don't care about the answer LDA $4016 ; player 1 - Select, you don't care about the answer LDA $4016 ; player 1 - Start, you don't care about the answer
LDA $4016 ; player 1 - Up, NOW you DO care about the answer AND #%00000001 ; you want to keep only bit0 BEQ SKIPUP ; Up was NOT pressed, so skip the next instructions
LDA $0200 ; sprite y position SEC ; set carry SBC #$01 ; subtract 1 STA $0200 ; sprite NEW y position JMP SKIPDOWN ; it is either Up or Down, so if Up is pressed you skip the next check
SKIPUP: LDA $4016 ; player 1 - Down, NOW you DO care about the answer AND #%00000001 ; you want to keep only bit0 BEQ SKIPDOWN ; Down was NOT pressed, so skip the next instructions LDA $0200 ; sprite y position CLC ; clear carry ADC #$01 ; add 1 STA $0200 ; sprite NEW y position
SKIPDOWN: ; ... ; the program continues here
gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin