|
|
FF00 (P1)
The BITs and their meanings are shown below:
This register is used to read the state of the joypad and buttons. To do this, BIT 4 or 5 is set which tells the gameboy to scan the buttons or the joypad, and the state of the selected group is returned in BITs 3 to 0, a value of 0 when the appropriate button or direction is pressed, and a 1 when it isn't. The diagram below should help explain what's happening.
P14 P15
: :
P10------O-Right----O-A
: :
P11------O-Left-----O-B
: :
P12------O-Up-------O-Select
: :
P13------O-Down-----O-Start
: :
If we set BIT 4 to 1 in this register, the gameboy would scan down the
P14 column, checking the joypad to see if Right, Left, Up, and/or Down were
pressed. If we set BIT 5 to 1, the gameboy would scan down the P15 column,
checking the state of the buttons to see if any of them were pressed.
If any of them are, the appropriate BIT would be set to 0 accordingly (and the others set to 1). For example, if we were checking the P14 column, and if Right and Down were pressed, the BITs for P10 and P13 would be set to 0 (BITs 0 and 3 respectively) and the others set to 1. This would give us the following BYTE: 00010110Just setting the appropriate BIT (4 or 5) to tell the gameboy to read the joypad or buttons, and then to immediately read the values in BITs 0-3 wouldn't give a very good, responsive joypad routine. This is due to joypad 'bounce'. As a person presses down (or lets go of) a direction or button, the appropriate switch, being electronic, opens and closes several times. This isn't a problem with just this switch, it's a problem with any electrical switch. The contact is so small that the electricity arcs across sometimes. This only happens whilst the switch is being opened or closed, and doesn't happen when the switch is fully open or closed. To remove the effect of 'bounce' it is normal to read the state of the buttons several times, giving the switch time to open or close properly. Now that we can read the state of the buttons, we need to know how to use this information. To start off with, the fact that the buttons or directions that are actually pressed have a value of 0 rather than 1 is mildly annoying. This is simply remedied by inverting all of the BITs so that the 1's become 0's and vice versa. The command to invert (or complement) the BITs within the Accumulator is shown below: CPLFor each reading, we also only need the low NIBBLE, or the right hand four BITs. Masking out the other BITs is also easy, we just need to use the command shown below: AND $0FOf course, if we want to read both the joypad and the buttons, we can return the state of everything in a single BYTE. A normal method of doing this is to do the following:
1. Read the Directions
SWAP A5. Place the resulting byte in a temporary register like B 6. Read the Buttons 7. Complement the BITs (Invert them) 8. Mask out all but the BITs we want (AND $0F) 9. OR the BYTE with the stored BYTE This leaves us with a stored BYTE that has the Direction BITs in the high NIBBLE (left hand four BITs) and the Button BITs in the low NIBBLE (right hand four BITs). A simple check of these BITs will let us know whether a certain direction or button has been pressed. For a working code snippet that does all of this for you, go to the joypad.asm section of the source code area. |