Howto:Read Control Pads

From MegaDrive Wiki
Revision as of 14:01, 8 January 2013 by Walker7 (talk) (Initialising Control Pads)
Jump to: navigation, search

All Mega Drives have at least two connectors for joypads, which are the only source of input to the game without special hardware on the cartridge or extension port. 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 has to be multiplexed. This is especially true of 6 button controllers, and any other peripherals that utilise the Mega Drive's controller port connectors.

Controller interfacing and the version register is handled by the I/O controller, a discrete chip labeled 315-5309 on early Mega Drives, or integrated with the Z80 control chip in later revisions, and integrated with all other system control chips in Model 2 and later Mega Drives.

Initialising Control Pads

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 same applies to six button controllers - although those require a slightly different /TH toggling sequence to read data.) The following code can be used to set up the controllers:

;-------------------------------------
; Routine to initialise control ports
;-------------------------------------
SetUpJoypads:
	move    #$2700,sr	; Disable Interrupts.
	move.w  #$100,($A11100)	; Stop the Z80.
@waitZ80:
	btst    #0,($A11101)	; Has the Z80 stopped?
	bne.s   @waitZ80	; If not, wait.
	moveq   #$40,d0		; PC6 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	; Enable Interrupts.
	rts

To begin with, the code will disable interrupts, so it can not get interrupted while the Z80 is stopped. This is a precaution so that in case of VBlank or HBlank routines also stop and afterwards resume the Z80 interrupt the code are called. This would leave the Z80 running during joypad reads. Controller ports could be read with the Z80 running, but this is not recommended practice, due to a bug in the I/O controller, which may cause the Z80's wait state timing to change from 250ns to 110ns, which is too fast for it to read data correctly. Thusly, it will misread all data.

Next, the code writes the value $40 to a register for optimisation purposes, reducing size by two bytes as well as time required to fetch the opcode. That value is then written to the control registers for ports A through C (front ports and EXT port.) The control port's bit format is as follows:

Control Port Format
128 64 32 16 8 4 2 1
INT PC6 PC5 PC4 PC3 PC2 PC1 PC0

A set bit indicates that the pin of the port is configured as an output. In the example above, we configure /TH as an output, and all other pins as an input. If INT were set and /TH was an input, it could be used to cause a level 2 interrupt on the Motorola 68000 which is used by light guns. The set up routine should be ran once, as close to the beginning of your program as possible to ensure that the pads are initialised.

Reading Data

With control pads set up, the pads themselves can finally be read. The routine below will read all three ports, and writes not only the 'raw' button state to a RAM address, but also the 'pressed' state of buttons, which will only be set for one frame.

ControlStateArea	EQU	$FFFFFF82	; Area in RAM that holds the button states.

;------------------------------------------------------------------------------------------
; Routine to read the currently pressed buttons from all three IO ports (control 1&2, EXT)
; Outputted format is in %SACBRLDU
;------------------------------------------------------------------------------------------
JoypadRead:
	move.w	#$100,($A11100)		; Stop Z80 and wait.
@waitZ80:
	btst	#0,($A11101)		; Has the Z80 stopped?
	bne.s	@waitZ80		; If not, wait.
	lea	ControlStateArea,a0	; Point to area where Joypad states are written.
	lea	$A10003,a1		; Point to first Joypad port.
	moveq	#2,d7			; Initialize Loop Counter for reading all 3 control ports.
@readJoypads:
	move.b	#0,(a1)			; Assert /TH.
	rept 4
	nop				; Wait until data is ready.
	endr
	move.b	(a1),d0			; Read back controller states. (d0.b = %00SA00DU)
	lsl.b	#2,d0			; Shift start and A into the high 2 bits. (d0.b = %SA00DU..)
	andi.b	#$C0,d0			; Get only S+A buttons. (d0.b = %SA......)
	move.b	#$40,(a1)		; De-assert /TH.
	rept 4
	nop				; Wait until data is ready.
	endr
	move.b	(a1),d1			; Read back the controller states. (d1.b = %11CBRLDU)
	andi.b	#$3F,d1			; Get only CBRLDU alone. (d1.b = %..CBRLDU)
	or.b	d1,d0			; OR together the control states. (d1.b = %SACBRLDU)	***
	not.b	d0			; Invert the bits.
	move.b	(a0),d1			; Get the current button press bits from RAM.
	eor.b	d0,d1			; OR the pressed buttons with the last frame's pressed buttons.
	move.b	d0,(a0)+		; Write the Pressed bits.
	and.b	d0,d1			; AND the bits together.
	move.b	d1,(a0)+		; Write the held bits.
	addq.l	#2,a1			; Point to next Control Port.
	dbf	d7,@readJoypads		; Loop for next Joypad until all Joypads are read.
	move.w	#$0,($A11100)		; Re-start the Z80.
	rts

The bit layout for each RAM address will be Start, A, C, B, Right, Left, Down, Up, starting from the MSB. For example, the following code can be used to check the 'raw' button state (the condition will evaluate to true every frame the button is pressed):

	btst    #7,(ControlStateArea)	; Is Start pressed?
	bne.s   @startPressed		; If so, branch.

However, if you were to want to check if start was pressed using the held button state (will only be ran the first frame the button is held down) the following code could be used:

	move.b	(ControlStateArea+1),d0	; Get button state to d0 (if we AND the address, it will get destroyed).
	andi.b	#$80,d0			; Is Start pressed?
	bne.s	@startPressed		; If so, branch.

Note the address is directly the next one over from the raw state. Each port's state is 2 bytes, so the second controller port's data would be at $FFFFFF84.