Controller Interface (Sega Mega Drive)
The Mega Drive controller has two main variants, that act differently. Both have the exact same pins and work on all consoles, but games specifically drive the controller reading and can choose to read 6 button controllers in a special way if they desire.
Here is the pinout looking into the console port from the front.
The general idea of the controller interface is very simply. The game (via the Sega ASIC) controls the SELECT pin (pin 7 on the controller port).
This SELECT pin when HIGH reads a certain set of buttons, when low reads another set of buttons. For example when high pin 6 reads B, when low it reads A.
That is about as complicated as the 3 button controller is (explained in more detail below).
The 6 button controller uses a trick where it toggles the SELECT pin rapidly (under 12ms between toggles) and if on the third low pulse in under 12ms the controller sends LEFT, RIGHT, UP and DOWN all low then this tells the console/game that it is a 6 button controller.
After that low pulse the next high pulse will then expect to read the additional buttons X, Y, Z and Mode.
More detail about each specific controller type is below.
NOTE: The game is responsible for controlling the SELECT pin, so all pins including SELECT will sit high (due to pull-up resistors) until the game decides it wants to read the controller.
This means typically while the SEGA logo is displayed and the game hasn't started fully, you won't see activity on the SELECT pin. After a few seconds and the game loads fully, you start to see the pulses.
This is a good way to detect if your console is running a game if you get no video output, to diagnose if the video out could be the problem, or if the game has successfully loaded.
The SELECT button (pin 7) is an output from the console and an input to the controller, in which the console sets the pin high or low. More specifically, it is the game that directly controls if the SELECT pin is high or low, and it is up to the game to do this whenever it wants and in whatever order it wants.
Some games like Sonic for example send the SELECT pin low for a short pulse every 20ms approximately.
Whereas games like Mortal Kombat II do the opposite and keep SELECT low most of the time and pulse high briefly.
It is important to understand how the console (so the Sega ASIC) controls the pins to explain how things happen.
Every pin (including SELECT) is pulled high via a pull-up resistor. The Sega ASIC has a transistor at the gates of each pin that is either disconnected (Hi-Z) meaning it doesn't influence or control the pin at all, as if its not connected, therefore lets the pins float to 5V, or when it wants to control them sinks them to ground.
This is important in how the controller detect works.
This is the point at which the console tries to detect if a controller is connected, as well as what kind (3 button or 6 button).
The console sends the SELECT pin low after being high, and right after that checks if the RIGHT and LEFT pins get pulled low too.
This works because the controller is wired to a multiplexer chip where the LEFT and RIGHT pins should be shorted to ground when the SELECT pin is low.
If there is no controller inserted, there is nothing driving the LEFT and RIGHT pins low, so they stay high when the SELECT pin is low.
For the 6 button controller, there is a semiconductor IC with logic that detects more advanced pulses we will discuss later.
The pulses low and high when reading button presses are bet ween 4-7us in length. So a low pulse is between 4-7us, and a high pulse is.
The gap between consecutive reads (after the low pulse or 4 low pulses for 6 buttons), is around 20ms. This means games read the button inputs every 20ms on average.
For the 3 button interface, there is no microcontroller or advanced logic, simply a directly wired single 74HC157 Quad 2-input multiplexer.
The multiplexer has its SELECT pin wired to the SELECT pin of the console.
This IC allows the controller buttons all to be wired as inputs, and based on the SELECT pin being high or low, have 2 different outputs.
Hopefully the above diagram makes it pretty simple to understand. The controller simply sends the UP and DOWN signals always low if pressed, or floating high if not.
When SELECT is high, LEFT, RIGHT, B and C pull the output pins low if pressed, or high if not.
When SELECT is low, LEFT and RIGHT are grounded always, and A and START pull the output pins low if pressed, or high if not.
The 6 button pad is functionally identical to the 3 button pad but with 3 extra buttons X, Y and Z, as well as a Mode button.
Some games do not function with the 6 button controller. To fix this, Sega added a MODE button on the top right of the controller. If held while the system is booting, the controller will act like a three button pad.
NOTE: I tested this myself on Shaq Fu and the controller doesn't change to a 3 button type response, it remains as 6 button. So I presume some games perhaps do this and others don't, as it is the game that controls the SELECT pin not the controller so that behaviour is entirely up to the game to choose.
Many 6 button games make use of the MODE button in game.
The way the 6 button pad works is supported games would toggle the SELECT in rapidly 4 times low, within 12ms.
On the third toggle low, if the controller responded by sending the UP and DOWN pins low as well as LEFT and RIGHT, then the game knew it had a 6 button controller connected.
Here is Shaq Fu (a game that supports 6 button controllers) without a controller plugged in.
And here is with a 6 button controller connected but no buttons pressed.
Notice how the UP, DOWN, LEFT and RIGHT are all low on the third low pulse. This is the trigger for detecting the 6 button controller.
Here is a 3 button controller connected when using a game that supports 6 buttons. Notice how it simply replies with LEFT and RIGHT low but UP and DOWN remain high.
Therefore, the game knows that all reads are simply the 3 button controller reads.
Notice also the START button glitches when SELECT rises. Ignore this, it is just interference on the logic probes.
Here is the comparison between a 6 button and 3 button controller connected when there is a 6 button game inserted.
Here are where the buttons are read.
The 6 button controller acts like a 3 button controller for the first 2 low pulses, but on the third low (if done in under 12ms) the 6 button controllers send the UP and DOWN low as well as LEFT and RIGHT triggering the 6 button mode.
Now the next rise is when the X, Y, Z and MODE buttons can be read.
The 4th low pulse can read the START and A buttons again, and on all official controllers I've tested the remaining buttons are all 5V during this 4th pulse.
Finally, the SELECT should rise again for a 4th time to reset the controller. This means the next low pulse (usually in 20ms time) will start this 4 pulse cycle again.
During the idle SELECT pin being HIGH the controller buttons can be read (so UP, DOWN, LEFT, RIGHT, B and C).