controllers

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
controllers
by on (#4602)
when should the controller status be updated? when $4016 is strobed or a specific scanline or is it some other time?

by on (#4603)
You should check to see whether a button is pressed at the start of a VBlank. When the joypad is strobed then return the bits via 4016.

example;

- Check for input (via directinput)
- start VBlank
- joypad strobe here (then begin to return bits)

Hope this helps.

by on (#4606)
Bit 0 of the value written to $4016 sets the strobe, which is connected to a shift register in both joypads. When the strobe is 1, reading from $4016 or $4017 always returns the status of the respective A button in bit 0. When the strobe is changed from 1 to 0, the current state of the buttons is latched into the shift registers of both joypads. Then, with the strobe left at 0, reading from $4016 and $4017 reads the low bit in the corresponding shift register and then clocks it right one bit.

An emulator could latch the current host keyboard/joystick state into a shift register when the strobe changed from 1 to 0. The following code demonstrates the behavior (for clarity is reads joypad #2):

Code:
lda  #1     ; strobe = 1
sta  $4016
lda  $4017  ; bit 0 has A button status at this exact moment
lda  $4017  ; bit 0 has A button status at this exact moment
            ; etc.

lda  #0     ; change strobe to 0 and latch current buttons
sta  $4016

lda  $4017  ; bit 0 has A button status when it was latched
lda  $4017  ; bit 0 has B button status when it was latched
            ; etc.

lda  #0     ; strobe already 0, thus no effect on shift register
sta  $4016

lda  $4017  ; bit 0 has does *not* have A button status