Jump to content
  • 0

Questions on Keyboard Emulation of Joystick



The Commander X16 Programmer's Reference Guide indicates the following:


Description: The routine joystick_get retrieves all state from one of the joysticks. The number of the joystick is passed in .A (0 through 3), and the state is returned in .A, .X and .Y.

  .A, byte 0:      | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
              NES  | A | B |SEL|STA|UP |DN |LT |RT |
              SNES | B | Y |SEL|STA|UP |DN |LT |RT |

  .X, byte 1:      | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
              NES  | 0 | 0 | 0 | 0 | 0 | 0 | 0 | X |
              SNES | A | X | L | R | 1 | 1 | 1 | 1 |
  .Y, byte 2:
              $00 = joystick present
              $FF = joystick not present

If joystick 0 is not present, it will fall back to returning the state of the keyboard, if present:
Keyboard Key     NES Equivalent
Ctrl                 A
Alt                 B
Space             SELECT
Enter             START
Cursor Up         UP
Cursor Down     DOWN
Cursor Left     LEFT
Cursor Right     RIGHT


I thought the play was to support SNES controllers, not NES controllers.

Why is the NES equivalent shown instead of the SNES equivalent? This is confusing as the A button maps to a different byte and the B button maps to a different bit in the controller data returned by  the joystick_get function.

Why are there no keys assigned for the extra buttons on the SNES controller (X, Y, L, and R)?

Also it seems to me there should be keys assigned to the four diagonal directions.

What are the codes returned when the Ctrl and Alt keys are pressed which correspond to the NES A and B buttons?

Edited by CX16UserSteveC
Added mention of diagonal keys.
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

I can't answer all of this, but it would be inappropriate to assign keys on a keyboard to switches that never existed on the SNES controller.

My guess is that the emulator only maps the NES equivalent because the NES vs. SNES decision was in limbo for a long time.

The SNES controller had Up, Down, Left, and Right. Games that understood the concept of "diagonals" did so by checking for Up+Left, Up+Right, Down+Left, or Down+Right.

I don't understand your question about what codes are returned for Ctrl and Alt... if you're polling the joystick, you don't care about scancodes or the like, at all. The joystick interface doesn't get you codes. It gives you those 3 bytes, so if someone pressed Ctrl, it'll set bit 7 on byte 0. If someone presses Alt, it'll set bit 6 on byte 0. If they press both, it'll set both of those.

What this means for the SNES controller, however, is that if you press Ctrl, it'll still set bit 7 on byte 0, but this corresponds to the B button, while Alt still sets bit 6 on byte 0, which corresponds to the Y button. The reason why is because of how of the NES and SNES controllers were physically wired, and its this wiring that is what the emulator is emulating. NES and SNES controllers used a compatible wiring standard -- it's possible to plug a NES controller to a SNES, or vice versa, with a passive adapter. A NES will only read 1 byte from the SNES controller, containingg B, Y, Select, Start, Up, Down, Left, Right. But that's okay, the shift register will be re-latched and reset the bit order of buttons before the next polling cycle. A SNES will try to read two bytes from the NES controller, but there's only one shift register on a NES controller with 8 input lines, but that's okay because the NES controller will just output a fixed value on the extra bits when the SNES tries to get them from the shift register, so it'll output A, B, Select, Start, Up, Down, Left, Right, 0, 0, 0, 0, 0, 0, 0, 0.

Or if you *really* want to understand the wiring of SNES controllers and NES controllers, I recommend Retro Game Mechanics Explained's video on the subject.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

Please review our Terms of Use