The Lush Engine





Introduction
General Assembler
Mono Gameboy
Gameboy Colour
Reference
Introduction
Control Registers
Introduction
FF00 (P1)
FF01 (SB)
FF02 (SC)
FF04 (DIV)
FF05 (TIMA)
FF06 (TMA)
FF07 (TAC)
FF0F (IF)
FF10 (NR 10)
FF11 (NR 11)
FF12 (NR 12)
FF13 (NR 13)
FF14 (NR 14)
FF16 (NR 21)
FF17 (NR 22)
FF18 (NR 23)
FF19 (NR 24)
FF1A (NR 30)
FF1B (NR 31)
FF1C (NR 32)
FF1D (NR 33)
FF1E (NR 34)
FF20 (NR 41)
FF21 (NR 42)
FF22 (NR 43)
FF23 (NR 44)
FF24 (NR 50)
FF25 (NR 51)
FF26 (NR 52)
FF40 (LCDC)
FF41 (STAT)
FF42 (SCY)
FF43 (SCX)
FF44 (LY)
FF45 (LYC)
FF46 (DMA)
FF47 (BGP)
FF48 (OBP0)
FF49 (OBP1)
FF4A (WY)
FF4B (WX)
FF4D (KEY 1)
FF4F (VBK)
FF51 (HDMA1)
FF52 (HDMA2)
FF53 (HDMA3)
FF54 (HDMA4)
FF55 (HDMA5)
FF56 (RP)
FF68 (BCPS)
FF69 (BCPD)
FF6A (OCPS)
FF6B (OCPD)
FF70 (SVBK)
FFFF (IE)




FF00 (P1)

Register Name Shorthand Address Read/Write
Port P15-P10 P1 FF00 R/W

The BITs and their meanings are shown below:

BIT Meaning
7 Not Used
6 Not Used
5 P15 Out Port
4 P14 Out Port
3 P13 In Port
2 P12 In Port
1 P11 In Port
0 P10 In Port

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:

 
00010110
Just 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:

 CPL
For 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	$0F
Of 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
2. Complement the BITs (Invert them)
3. Mask out all but the BITs we want (AND $0F)
4. SWAP the two NIBBLEs over by using the following code:

 SWAP A
5. 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.


Buttons created using the interactive button maker at Coolarchive