Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Super NES Programming/Joypad Input

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Joypads are the SNES' means of input from the user. There can be up to four connected to the system, and there's support for special controllers, too (mouse, Super Scope, etc). 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. Joypad Registers To start off, we'll list the registers that we're going to use: SizeAddressOfficial NameDescriptionByte $4200NMITIMEN Counter EnableByte $4212HVBJOY Status RegisterWord $4218CNTRL1L & -H Joypad #1 StatusWord $421aCNTRL2L & -H Joypad #2 StatusWord $421cCNTRL3L & -H Joypad #3 StatusWord $421eCNTRL4L & -H Joypad #4 StatusByte $4016 Joypad #1 Old-Style StatusByte $4017 Joypad #2 Old-Style Status Now, let's look at them in a bit more depth: $4200 - NMITIMEN - 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: Bit namePositionDescriptionn 7 Enables/Disables NMIv 5 Enables/Disables an IRQ on a vertical triggerh 4 Enables/Disables an IRQ on a horizontal triggerj 0 The Joypad polling bit $4212 - HVBJOY - 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: Bit(s)Description$4219/b/d/f(hi)bB ButtonyY ButtonsSelect ButtontStart ButtonuUp D-Pad ButtondDown D-Pad ButtonlLeft D-Pad ButtonrRight D-Pad Button$4218/a/c/e(lo)aA ButtonxX ButtonlL ButtonrR ButtoniiiiController 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. Basic Joypad Read Algorithm When you initialize: .ENUM $80 Joy1A db ;B, Y, Select, Start, Up, Down, Left, Right Joy1B db ;A, X, L, R, iiii-ID .ENDE lda #%10000001 ; Enable NMI and Auto Joypad read sta NMITIMEN ; Interrupt Enable Flags In your main game loop (or somewhere appropriate) without checks: lda CNTRL1L ; $4218 sta Joy1A lda CNTRL1H ; $4219 sta Joy1B However, before reading from the Auto Joypad Read registers, the lowest bit of HVBJOY should be tested to be 0, as the reading process takes some time to finish.