Controllers

From Snesdev wiki
Jump to navigation Jump to search

SOURCE: Anomie's Register Doc

Overview

The SNES has 2 controller ports on the front of the unit, and an "expansion port" on the bottom (which AFAIK was only used by a few things released only in Japan). Little is known about the expansion port.

A number of peripherals could be plugged into the controller ports:

  • Joypads
  • The Multitap (aka MP5), into which up to 4 joypads may be plugged.
  • A Mouse, with 2 buttons.
  • The SuperScope, a bazooka-like light gun.
  • The Konami Justifiers, a normal style gun into which a second gun could be plugged.

There are probably others, these are just the ones I know anything about.

Generic

The controller ports of the SNES has 7 pins, laid out something like this:

  _________________ ____________
 |                 |            \
 | (1) (2) (3) (4) | (5) (6) (7) |
 |_________________|____________/

The pins are:

1: +5v (power)
2: Clock
3: Latch
4: Data1
5: Data2
6: IOBit
7: Ground

Latch is written trhough bit 0 of register $4016. Writing 1 to this bit results in Latch going to whatever state means 'latch' to a joypad.

Clock of Port 1 is connected to the 'read' signal of $4016, in that reading $4016 causes Clock to transition. Data1 and Data2 are then read, and Clock transitions back (at this point, the pad is expected to stick its next bits of data on Data1 and Data2). Clock of Port 2 is connected to $4017.

Data1 and Data2 are read through bits 0 and 1 (respectively) of $4016 and $4017 (for Ports 1 and 2, respectively). Thus, you must read both bits at once, you can't choose to read only Data1 and leave Data2 for later.

IOBit is connected to the I/O Port (which is accessed through registers $4201 and $4213). Port 1's IOBit is connected to bit 6 of the I/O Port, and Port 2's IOBit is connected to bit 7. Note that, since bit 7 of the I/O Port is connected to the PPU Counter Latch, anything plugged into Port 2 may latch the H and V Counters by setting IOBit to 0.

Auto Joypad Read, when enabled by bit 0 of $4200, effectively does the following (in pseudo-ASM):

  LDA $4212
  ORA #$01
  STA $4212  ; pretend it's writable
  
  LDA #$01
  STA $4016
  ; There may be a delay here
  STZ $4016
  
  LDX #$0010
  loop:
      LDA $4016
      REP #$20
      LSR
      ROL $4218
      LSR
      ROL $421C
      SEP #$20

      LDA $4017
      REP #$20
      LSR
      ROL $421A
      LSR
      ROL $421E
      SEP #$20
      
      DEX
  BNE loop
  
  LDA $4212
  AND #$7E
  STA $4212  ; pretend it's writable again

"Open Port"

If nothing is plugged into a port (or the thing plugged in doesn't connect to the pin), the SNES will read zeros from Data1 and Data2.


Joypads

The joypads return 16 bits of data out Data1, then one bits until latched again. The data is:

 byetUDLRaxlr0000

b/y/a/x/l/r are the similarly named buttons. 'e' is select. 't' is start. U/D/L/R are the pad directions. Note that the standard joypad can only return either U or D set, and either L or R set. Some games will crash or exhibit other odd behavior if both U and D and/or both L and R are set.

Data2 is not even connected, nor is IOBit.


Mouse

The mouse returns 32 bits of data out Data1, and 1 bits thereafter. The data is:

 00000000rlss0001 YyyyyyyyXxxxxxxx

l/r are the two mouse buttons. 'ss' are the "speed bits", which are incremented mod 3 if Clock cycles while Latch is active. Y/X are the direction bits (set is up/left), and yyyyyyy/xxxxxxx are the distance traveled in the appropriate direction.

Supposedly, the 'speed bits' may not match the internal speed setting when the mouse first receives power. The speed setting controls the delta curve of the mouse, with 0 giving a flat curve and 2 giving the greatest delta response.

Data2 and IOBit are presumably not connected, but this is not known for sure.


SuperScope

The SuperScope returns 8 bits of data out Data1, and 1 bits thereafter. The data is:

 fctp00on

'f' is Fire, 'c' is Cursor, 't' is Turbo, 'p' is Pause, 'o' is Offscreen, and 'n' is Noise.

The SuperScope has two modes of operation: normal mode and turbo mode. The current mode is controlled by a switch on the unit, and is indicated by the 't' bit. Note however that the 't' bit is only updated when the Fire button is pressed (i.e. the 'f' bit is set). Thus, when you turn turbo on the 't' bit remains clear until you shoot, and similarly when turbo is deactivated the bit remains set until you fire.

In either mode, the Pause bit will be set for the first strobe after the pause button is pressed, and then will be clear for subsequent strobes until the button is pressed again. However, the pause button is ignored if either cursor or fire are down(?).

In either mode, the Cursor bit will be set while the Cursor button is pressed.

In normal mode, the Fire bit operates like Pause: it is on for only one strobe. In turbo mode, it remains set as long as the button is held down.

When Fire/Cursor are set, Offscreen will be set if the gun did not latch during the previous strobe and cleared otherwise (Offscreen is not altered when Fire/Cursor are both clear).

Noise is set if there is interference in the infrared transmission from the Scope to the receiver.

If the Fire button is being held when turbo mode is activated, the gun sets the Fire bit and begins latching. If the Fire button is being held when turbo mode is deactivated, the next poll will have Fire clear but the Turbo bit will not be updated until the next fire (i.e. FcTp => turbo off => fcTp, not fctp).

The PPU latch operates as follows: When Fire or Cursor is set, IOBit is set to 0 when the gun sees the TV's electron gun, and left a 1 otherwise. Thus, if the SNES also leaves it one (bit 7 of $4201), the PPU Counters will be latched at that point. This would also imply that bit 7 of $4213 will be 0 at the moment the SuperScope sees the electron gun.

Since the gun depends on the latching behavior of IOBit, it will only function properly when plugged into Port 2. If plugged into Port 1 instead, everything will work except that there will be no way to tell where on the screen the gun is pointing.

When creating graphics for the SuperScope, note that the color red is not detected. For best results, use colors with the blue component over 75% and/or the green component over 50%.

Data2 is presumably not connected, but this is not known for sure.


Justifiers

The Justifier returns 48 bits of data out Data1. Presumably it returns one bits after (if so, it really only returns 32 bits), but this is not known. The data is:

 0000000000001110 01010101TtSsl000 1111111111111111

T/t are the trigger states for guns 1 and 2. S/s are the start button states for guns 1 and 2. 'l' indicates which gun was connected to IOBit: 1 means gun 1, 0 means gun 2. Note that 'l' toggles even when gun 2 is not connected.

IOBit is used just like for the SuperScope. However, since two guns may be plugged into one port, which gun is actually connected to IOBit changes each time Latch cycles. Also note, the Justifier does not wait for the trigger to be pulled before attempting to latch, it will latch every time it sees the electron gun. Bit 6 of $213F may be used to determine if the Justifier was pointed at the screen or not.

Data2 is presumably not connected, but this is not known for sure.


MP5 (Multitap)

The MP5 plugs into one Controller Port on the SNES (typically Port 2), and has 4 ports for controllers to be plugged into it (labeled 2 through 5). It also has an override switch which makes it pass through Pad 2 and ignore everything else.

If IOBit is 1, Clock is passed through to Pad 2 and Pad 3, Data1 is connected to Data1 on Pad 2, and Data2 is connected to Data1 on Pad 3. If IOBit is 0, Pads 4 and 5 are used instead of 2 and 3, respectively. In either case, Latch is passed through to all pads, and IOBit is presumably not passed through at all.

Note that Clock is only passed through to the pads that are actually being passed through. Thus, you can read the first two pads (or let Auto-Joypad Read do it), then toggle IOBit and read the other two pads manually. Most games requiring more than 3 players do exactly this.

Also note that there is nothing preventing the MP5 from functioning perfectly when plugged in to Port 1, except that the game must use bit 6 of $4201 instead of bit 7 to set IOBit and must use the Port 1 registers instead of the Port 2 registers. With 2 MP5 units, one could actually create an 8-player game!

When Latch is active, 1s will be read from Data2 and 0s from Data1. This is sometmies used to detect the presence of an MP5 unit. The override switch disables this behavior.

There are reports that the MP5 does not react immediately when IOBit is transitioned from 0 to 1. Thus, reading 2&3 then 4&5 will probably work better than vice versa.