Controllers
The console has two ports. Two pads are here, and no other device. They are in
crates/snesemu-core/src/input.rs, and their place on the bus is in bus.rs. What is here rests on fullsnes
("SNES Controllers"), the Super Famicom Development Wiki ("Registers" and "Timing") and the SNESdev Wiki
("MMIO registers") (credits).
The pad
A pad keeps its buttons as 16 bits, and gives them one at a time. A 1 means that the button is held:
| Bit | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 to 0 |
| B | Y | Select | Start | up | down | left | right | A | X | L | R | 0 |
Bit 15 comes first. The last four say which device it is, and are 0 for a pad. After 16 bits, it gives only
ones.
A front end gives run_frame the same 16 bits for each pad. The last four do not count. The app's keys and
gamepad buttons follow the places of the buttons on the console's own pad. The core does not stop left and
right, or up and down, from being held together.
There are two ways to read the pads, and both use the same 16 bits in the pad. So they can disturb each other.
Read by the program
A program writes to $4016 and reads $4016 (port 1) and $4017 (port 2):
- It writes 1 to
$4016. While bit 0 is 1, the pads take their buttons all the time, and each read gives
the first bit, B.
- It writes 0. The pads keep what was held at that moment. What is pressed later is not seen.
- It reads each port 16 times. Bit 0 has the next button each time.
- The 17th read and all later ones give 1.
| Bits 7-5 | 4-2 | 1 | 0 |
$4016 | open bus | open bus | 0 | pad 1 |
$4017 | open bus | always 1 | 0 | pad 2 |
Bit 1 is where a second device on a port would be, such as on a multitap. Nothing is there, so it is 0. These
two registers are slow: 12 clocks.
Read by the console
If bit 0 of NMITIMEN ($4200) is set, the console reads both pads in each V-blank, and the program does
not have to:
- It starts in the first line of V-blank. The first time after turning on or a reset, that is at clock
298 of this line. Each later start is a multiple of 256 clocks after the last one. It is the first of these
that is at clock 130 or later, so it is somewhere from clock 130 to 385. If NMITIMEN's bit is 0 then,
nothing is read. That time still counts as a start for the next one.
- The pads take their buttons at that moment. HVBJOY (
$4212) has bit 0 set from here on.
- It takes 4,224 clocks, which is more than three lines. So it is over in the fourth line of V-blank
(line 228).
- The results are in four registers, two for each pad:
| Address | |
$4218, $4219 | pad 1: A, X, L, R and four zeros, and then B, Y, Select, Start, up, down, left and right |
$421A, $421B | pad 2, in the same way |
$421C-$421F | the second device on each port: always 0 |
The pads have now given 16 bits. So if the program reads $4016 after that, it gets a 1 each time, until it
makes the pads take their buttons again.
For example, a game turns on NMIs and the automatic reading, and a player holds Start on pad 1, on NTSC:
- The front end calls
run_frame with Start ($1000) for pad 1. Apart from the first one, each call begins
just after a V-blank started.
- V-blank's first line has begun. The NMI's flag was set at its clock 2, and the game's NMI program
starts. In the first frame that has one, the reading starts at clock 298.
- The pads now have
$1000. If the game reads $4218 at once, it gets what was read in the frame before.
So the game waits until bit 0 of $4212 is 0, or does something else first that takes long enough,
such as a long DMA.
- 4,224 clocks after the start, at clock 4,522 of V-blank,
$4219 has $10 and $4218 has 0. This is in
line 228.
- The game reads them, and knows that Start is held. It can do what it has to in the same frame, and the
player sees it in the picture of that frame or the next, depending on the game.
If one step of the main processor is so long that it goes past the start of the reading, such as a DMA that
begins before V-blank, that reading still has the buttons of the call before.
What is a guess
- The four registers change all at once, at the end of the reading. On a console, they change while it
runs. What a program sees that reads them too early can be different.
- Turning the reading on or off while it runs does nothing.
- What a program gets when it reads
$4016 or writes to it while the console reads is not worked out. Here,
it uses the same bits, and takes some of them away.
What is not there
The multitap, the mouse, the Super Scope, the Justifier and all other devices. A light gun tells the picture
chips where the beam is through a line on port 2. That line is only there
for WRIO ($4201): when its bit 7 goes from 1 to 0, the counters are latched.
Tests for the bits are in input.rs. Those for the times of the automatic reading are in bus/tests.rs. They
have the documents' times, and not those of the code.