Controller detection

From Nesdev wiki
Jump to: navigation, search

In some NES programs, it's worthwhile to support multiple controllers. Because different controllers use different protocols, a program supporting multiple controllers should follow a process of elimination to determine what protocol to use. This can be done after reset; real-time controller detection isn't very useful because connecting a controller often causes a voltage sag that freezes the CPU. However, this also means that the player needs to use the same controller for selecting an activity and for actually using it. And when a single cartridge contains activities designed to use different controllers, the menu has to support all of them. For example, the Action 53 menu supports a standard controller in port 1 or in port 3 of the Famicom 4-player adapter, the Super NES Mouse in port 1, and the Zapper in port 2.

The following procedure is intended to distinguish among a standard controller, a Four Score, a Super NES Mouse, an Arkanoid controller, a Zapper, and a Power Pad.

Detection

Much of the detection involves strobing the controller (write 1 then 0 to $4016) and reading nine times. Circuits that ignore the clock will return the same value nine times in a row; circuits that use the clock will return values that vary. So keep track of which bits were 0 throughout and which bits were 1 throughout. In some cases, detection will be inconclusive if the player is holding a button. Repeat the detection each frame until the player releases the button.

Four Score

A Four Score or Satellite has four controller ports and produces a 24-bit report on D0: eight buttons for player 1 or 2, eight buttons for player 3 or 4, and a signature of $10 for port 1 or $20 for port 2. The momentary movements of two mice may produce the signature, appearing as if players 3 and 4 held Right, but this is exceedingly unlikely. If player 3 is not pressing Right, and player 4 is not pressing Right, and the signature is present on both ports, the controllers in both ports are a Four Score.

Zapper

A Zapper has a photosensor (1=dark) on D3 and a trigger (1=half pulled) on D4. After a few lines into vblank, the photosensor should return dark. Strobe the controller ports and read $4017 nine times. If D3 is constant 1 and D4 is constant 0, the controller is a Zapper. A standard controller or mouse will never have D3=1, a correctly calibrated Arkanoid controller will never have D4 constant, and a Power Pad will have D3=1 starting at the ninth read. If both D3 and D4 are constant 1, the controller is either a Zapper with its trigger half pulled or a Power Pad with all buttons held down.

Arkanoid controller

An Arkanoid controller for NES has a fire button (1=pressed) on D3 and a control knob (serial) on D4. If D3 is constant 0 and D4 is not constant, the controller is an Arkanoid controller. If D3 is constant 1 and D4 is not constant, the controller could be an Arkanoid controller with the fire button held down or a Power Pad with buttons 1-2, 5-7, and 9-11 held down. Wait for the player to let go of the fire button.

Power Pad

The Power Pad has 8-bit serial on D3 and 4-bit serial on D4, in both cases followed by constant 1 values. We need not require the player to step off the Power Pad, just to not intentionally press all 12 buttons. If D3 is not constant, D4 is not constant, the bit corresponding to button 4 is 0, and the last 4 of the first 8 bits from D4 are 1, the controller is a Power Pad.

Mouse

A Super NES Mouse can be connected to port 1 or 2 of an NES or AV Famicom or to port 3 or 4 of a Famicom 4-player adapter. It returns a 32-bit report on D0 or D1 whose 13th through 16th bits are the signature 0001, and whose 11th through 12th bits change (among 00, 01, or 10) when the host changes its speed. To change the mouse's speed, read the controller once in the middle of a strobe (between writes of 1 and 0 to $4016). If D4 and D3 are constant 0, and the signature is present, and changing the mouse speed works, the controller is a mouse.

Standard controller

If D4 and D3 are constant 0, and the controller is not a mouse or Four Score, it's probably a standard controller. The hardwired controllers on a Famicom and the 7-pin ports on an NES or AV Famicom are connected to D0. A Famicom 4-player adapter adds two additional controllers on D1. In Famicom games with two or fewer players, the user expects to be able to use controllers 3 and 4 as players 1 and 2 in case the hardwired controllers are worn.

The bits after the report aren't quite predictable as they are with the other serial devices. The Super NES controller has four more buttons and four zero bits after the existing buttons. Some clone controllers return a stream of 0 after the report instead of a stream of 1.

Confirmation

Just because a controller is connected doesn't mean a player is holding it. Wait for a player to claim ownership of a controller before expecting further input from it.

Standard controller, Four Score
Wait for an A (10000000) or Start (00010000) press.
Mouse
Show a sprite for the mouse pointer and controls for "Start" and the current mouse speed. Wait for the player to move the pointer over a control and press the left mouse button. Clicking the mouse speed changes it to the next setting; clicking Start confirms the mouse.
Zapper
Once a player pulls the trigger, brighten the screen and count scanlines until the photodiode goes bright. If it doesn't, the player shot offscreen.
Arkanoid controller
Wait for the fire button to be pressed.
Power Pad
Wait for button 4 to be 0 then 1.