Super NES Programming/Joypad Input
NOTE: This tutorial is a work-in-progress. I'm still relatively new to the SNES, so please feel free to correct any errors in this document.
To start off, we'll list the registers that we're going to use:
|Word||$4218||Joypad #1 Status|
|Word||$421a||Joypad #2 Status|
|Word||$421c||Joypad #3 Status|
|Word||$421e||Joypad #4 Status|
|Byte||$4016||Joypad #1 Old-Style Status|
|Byte||$4017||Joypad #2 Old-Style Status|
Now, let's look at them in a bit more depth:
$4200 - Counter Enable n-vh---j
This register is most likely a bit familiar to you. If you've done any prior SNES coding, you'll recognize that you've probably used this register to enable a VSync interrupt. For joypad input, we must make sure bit 0 ("j") is set. This tells the SNES that we will be polling the other joypad registers.
For reference, the n-vh---j bits which make up the rest of the byte we'll be loading in to register $4200 are as follows:
|v||5||Enables/Disables an IRQ on a vertical trigger|
|h||4||Enables/Disables an IRQ on a horizontal trigger|
|j||0||The Joypad polling bit|
$4212 - SNES Status Register vh-----j
This register is used to see if certain data is ready to be polled from the SNES. In this case, we only care about bit 0 ("j"). If we poll $4212 and bit 0 is set, then we know that the other SNES joypad registers will contain relevant data. It's usually not so bad if this step is left out, though. In the algorithm I'll be showing later, this check is skipped, but this explanation still exists for completeness.
$4218/9 through $421e/f - Joypad Status Registers hi:bystudlr lo:axlriiii
These are 16-bit registers that return the button and/or type states for each joypad connected to the SNES. $4218 and $4219 contain joypad 1's data, and joypad 2, 3, and 4 follow. The bits are laid out like this:
|b||B Button||y||Y Button|
|u||Up D-Pad Button|
|d||Down D-Pad Button|
|l||Left D-Pad Button|
|r||Right D-Pad Button|
|iiii||Controller Type ID|
Most of these are quite self-explanatory. If a corresponding button's bit is set, then that button is pressed. The only semi-confusing part of this is the mysterious "iiii" bits - these determine which kind of controller is connected to the port. Basically, if they read 0000, then it's a standard SNES controller. Otherwise, it's a custom controller type or corrupted data.
$4016 and $4017 - Joypad 1 and 2 Old-Style Status
For these, you'll notice I left out the specific bits of each register. Basically, these work just like reading states from NES. Because of this, I think it's redundant to include that information here, except that bit 0 of $4200 must be clear to do use them this way. These registers can still be useful, though. If you write 0 to $4016, then these two registers can be used to check if joypads 1 and 2 are connected. After writing the 0 (and you only need to write to $4016, not both), reading from the registers will return 0 if not connected, and something else if connected.