The joypad (as I've now learned from disassembly) is very primitive - 6 normally-open switches, each connected between a pin on the DE-9 connector and ground. The accepted layout adapter uses the 25-pin parallel port, connecting ground to pin 18, power to pin 1 (not that the control pad uses this pin) and 7 further connections from D0 to D6 for the buttons.
Master System Control Pad and a poorly-soldered DB-25 to DE-9 adapter.
I had been assured that the data lines on parallel ports (D0..D7) were pulled up, and so the layout seemed easy enough - D0..D6 will return highs normally, and when a button is pressed it is connected to ground.
Unfortunately, for whatever reason the data lines on the parallel port on my PC are not pulled up, at least not in any way that I can find to control. However, if you set the lines to be outputs (using bit 5 of the control register), set them all high, then flip them to inputs, they'll read as highs for a while until they float (slowly) back low again. I've used this to my advantage, and so have this:
/// Flags corresponding to which buttons are pressed.[Flags]public enum Buttons { None = 0x00, Up = 0x01, Down = 0x02, Left = 0x04, Right = 0x08, Button1 = 0x10, Button2 = 0x20, All = 0x3F,} // Retrieve the status of the port.private Buttons GetRawStatus() { // Set D0..D7 as outputs. Output(this.BaseAddress + 2, 0x00); // Set them high: Output(this.BaseAddress + 0, 0xFF); // Set D0..D7 as inputs. Output(this.BaseAddress + 2, 0x20); // Retrieve, invert and mask the data lines. return (Buttons)(~(Input(this.BaseAddress + 0)) & (int)Buttons.All);}
This works very well, with one small problem: nothing is debounced, so pressing any button causes 10 or so press/release actions to be detected until the contacts settle. Therefore, the exposed method for retrieving the status is this:
/// Gets the status of the buttons from the connected SMS joypad./// The status of the buttons.public Buttons GetStatus() { if (!this.Debounced) { return GetRawStatus(); } else { Buttons Last = GetRawStatus(); Buttons Current; int MaximumIterations = 100; while (((Current = GetRawStatus()) != Last) && (MaximumIterations-- > 0)) { Last = Current; Thread.Sleep(0); } return Current; }}
For some strange reason, this doesn't quite work; after a while (or rebooting, or reading/writing the EPP registers) the port starts reading nothing but zeroes again. Running another piece of software that uses the parallel port fixes it.
One missing feature of the emulator was support for the SMS pause button. This button is attached to the Z80's non-maskable interrupt line, so pressing it results in the CPU pushing the program counter to the stack then jumping to $0066.
For most games the pause button just pauses the game, but for some others it will display a menu - such as in Psycho Fox, which lets you use the items you have collected to change animal or use a power-up.
Psycho Fox's in-game menu
One major long-standing bug in the emulator has been interrupt handling by the CPU. I think I've (finally!) got it, though it's still not entirely perfect. How I've set it up now is that a flag is set - IntPending or NmiPending, depending on whether the maskable or non-maskable interrupt pin has been modified - when the interrupt is requested, and cleared when it's been handled.
Japanese Master System BIOS
I have updated the memory emulation to better support BIOS ROMs. Initially, the "Majesco" Game Gear BIOS and some of the "Snail Maze" SMS BIOS worked (though the SMS BIOS would display "Software Error" on several games). I've tested a few of them and they seem to work pretty well.
Hang On and Safari Hunt
Whilst the Japanese BIOS has (in my opinion) the best final effect, it's the M404 prototype BIOS that has the best effect overall: