Howto:Read Control Pads

From MegaDrive Wiki
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 I/O 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:

  1. ; Routine to initialise control ports
  2. SetUpJoypads:
  3. 		move	#$2700, sr								; Disable all ints
  4.  
  5. 		move.w	#$100, $A11100								; Stop Z80
  6.  
  7. @waitZ80:
  8. 		btst	#0, $A11101								; Has the Z80 stopped?
  9. 		bne.s	@waitZ80								; If not, wait.
  10.  
  11. 		moveq	#$40, d0								; PD6 is an output
  12. 		move.b	d0, $A10009								; Configure port A
  13. 		move.b	d0, $A1000B								; Configure port B
  14. 		move.b	d0, $A1000D								; Configure port C
  15.  
  16. 		move.w	#0, $A11100								; Restart the Z80
  17. 		move	#$2000, sr								; Re-enable ints
  18. 		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. When the Z80 waitstate is reduced to 110ns, is too low for the Z80 to read data correctly from the bus. 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, the TH pin is configured 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 the program as possible to ensure that the pads are initialised when they are accessed later.

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.

  1. ControlStateArea: EQU $FFFFFF82									; Area in RAM that holds the button states
  2.  
  3. ; Routine to read the currently pressed buttons from all three IO ports (control 1&2, EXT)
  4. ; Outputted format is in S ACB RL DU
  5.  
  6. JoypadRead:
  7. 		move.w	#$100, $A11100								; Stop Z80 and wait
  8.  
  9. @waitZ80:
  10. 		btst	#0, $A11101								; Has the Z80 stopped?
  11. 		bne.s	@waitZ80								; If not, wait.
  12.  
  13. 		lea	ControlStateArea, a0							; Area where joypad states are written
  14. 		lea	$A10003, a1								; First joypad port
  15.  
  16. 		moveq	#2, d7									; Read all 3 control ports
  17.  
  18. @readJoypads:
  19. 		move.b	#0, (a1)								; Assert /TH
  20. 		rept 4
  21. 		nop										; Wait until data is ready.
  22. 		endr
  23.  
  24. 		move.b	(a1), d0								; Read back controller states. (00SA00DU)
  25. 		lsl.b	#2, d0									; Shift start and A into the high 2 bits
  26. 		and.b	#$C0, d0								; Get only S+A buttons
  27.  
  28. 		move.b	#$40, (a1)								; De-assert /TH
  29. 		rept 4
  30. 		nop										; Wait until data is ready.
  31. 		endr
  32.  
  33. 		move.b	(a1), d1								; Read back the controller states. (11CBRLDU)
  34. 		and.b	#$3F, d1								; Get only CBRLDU alone
  35. 		or.b	d1, d0									; OR together the control states
  36. 		not.b	d0									; Invert the bits
  37.  
  38. 		move.b	(a0), d1								; Get the current button press bits from RAM
  39. 		eor.b	d0, d1									; OR the pressed buttons with the last frame's pressed buttons
  40.  
  41. 		move.b	d0, (a0)+								; Write the pressed bits
  42. 		and.b	d0, d1									; AND the bits together.
  43. 		move.b	d1, (a0)+								; Write the held bits
  44.  
  45. 		addq.w	#2, a1									; Use next control port
  46. 		dbf	d7, @readJoypads							; Loop until all joypads are read
  47.  
  48. 		move.w	#$0, $A11100								; Re-start the Z80
  49. 		rts

The bit layout for each RAM address is 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):

  1. 		btst	#7, ControlStateArea 							; Is start pressed?
  2. 		bne.s	@startPressed								; If so, branch.

However, if one 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:

  1. 		move.b	ControlStateArea+1, d0							; Get button state to d0 (if we AND the address directly, it will get destroyed)
  2. 		and.b	#$80, d0 								; Is start pressed?
  3. 		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.