Shown below are typically the values for each button (depending upon how controller reading is performed, they may be backwards if reading is done in reverse):
Code: Select all
RIGHT_BUTTON = $01
LEFT_BUTTON = $02
DOWN_BUTTON = $04
UP_BUTTON = $08
START_BUTTON = $10
SELECT_BUTTON = $20
B_BUTTON = $40
A_BUTTON = $80
Code: Select all
%1101_0010 = $d2
|| | |
|| | +-------- LEFT_BUTTON = $02
|| +------------ START_BUTTON = $10
|+-------------- B_BUTTON = $40
+--------------- A_BUTTON = $80
For the previous example, here's what happens when that value is AND'd with the value for RIGHT_BUTTON, which is not currently being pressed:
Code: Select all
%1101_0010 = $d2
AND
%0000_0001 = $01 (RIGHT_BUTTON)
----------
%0000_0000
Code: Select all
%1101_0010 = $d2
AND
%0100_0000 = $40 (B_BUTTON)
----------
%0100_0000
Whether the result is zero or non-zero is useful, because then we can use BEQ or BNE to jump to another piece of code.