gamePad code Branch Not Equal... Help!

RPlude

New member
Im really struggling to wrap my head around this input check I found while I was digging around in the scripts.
its in Tile type 11 collision script and I am assuming the code only runs while you are touching that tile.

What Im trying to do is change the tile only when you push down and B. here is the code Im using to check for input and I was hoping someone could shed some light on how this works.

LDA gamepad ; Im assuming we are loading the state of the controller into the Accumulator.
AND #$00000010 ; here we Do a bitwise operation, I think it makes the value from the accumulator = #$00000010 which I think is The Binary code for the B button.
BNE changeBlock ; so why does BRANCH NOT EQUAL Branch when we press B and why not use BEQ "branch on equal" I think this line is really hurting my brain. hahaha
JMP dontchangeBlock ; and when does this line execute?

Ive watched the tutorials from Chronos on you tube and they were helpful, ive watched them 2 time and ready for a 3rd hahaha.
Thanks in advance!

~R
 

Kasumi

New member
#0 = 0 in decimal.
#$0 = 0 in hexadecimal
#%0 = 0 in binary
#$00000010 = $10 = Hexadecimal 10 = 16
#%00000010 = %10 = Binary 10 = 2
You're not doing AND with binary value 10, because you're using the syntax for hexadecimal. Hexademical 10 is a different button.

To understand bitwise operators, know 1 is true, and 0 is false. They check pairs of bits (at a time). AND returns true (1) if bit1 AND bit2 were both one. In 6502, imagine that the value in A and the given value (%00000010 in this case) get stacked on top of each other.
A: 01010010
M: 00000010
Then it checks the corresponding columns. If bit1 and bit2 were both 1 for that column, the resulting bit in the same position is 1. Otherwise, the resulting bit in the same position is 0.

So anytime you AND with only one true (1) bit, it guarantees all bits in all places except the place that's one will be zero. (Because if either bit is 0, both bits can't be 1, which makes AND false.) An AND with 1 essentially copies the other value. (1 AND 0 = 0. 1 AND 1 = 1). So by ANDing with 1 value, you get a set bit (1) in that place if the value in A had a set bit in that place, and a clear bit (0) in that place if the value in A had a clear bit in that place.

In this case, by anding a singular button's bit, you'll get a non zero value if the button was pressed, and zero value if the button was not pressed. Then you can branch, or not based on that.

If the button is pressed, the corresponding bit is set. So if you want to branch somewhere when the button is pressed, you use bne because any set bit anywhere makes the whole byte non zero.
 

RPlude

New member
Thank you Kasumi, That makes sense to me now. My artist brain has trouble with this whole asm thing but I think Im starting to get it... a little hahahaa. Thanks again for your help :)
 
Top Bottom