Howto:Read Control Pads

From MegaDrive Wiki
Revision as of 10:24, 10 August 2012 by Tristanseifert (talk | contribs) (Wrote basic page explaining how to to initialise control pads.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

All Mega Drives have at least two connectors for joypads, which are the only source of input to the game. Reading the control pads is accomplished through a simple routine. One might expect to just read a register and get all button states, but due to the limited amount of IO lines, data is multiplexed. This is especially true of 6 button controllers.

To read this data, we first must initialise the control ports to configure them as inputs, save for the /TH pin. This pin controls the multiplexer in control pads. The following code can be used to set up the controllers: <source lang="asm" line="GESHI_FANCY_LINE_NUMBERS">

Routine to initialise control ports

SetUpJoypads: move #$2700, sr ; Disable all ints

move.w #$100, $A11100 ; Stop Z80

@waitZ80: btst #0, $A11101 ; Has the Z80 stopped? bne.s @waitZ80 ; If not, wait.

moveq #$40, d0 ; PD6 is an output move.b d0, $A10009 ; Configure port A move.b d0, $A1000B ; Configure port B move.b d0, $A1000D ; Configure port C move.w #0, $A11100 ; Restart the Z80

move #$2000, sr ; Re-enable ints rts </source>