How to configure and use the Raspberry Pi GPIO pins as a joystick in MAME. This article describes how to make a MAME hand held console with the Raspberry Pi.
The video here demonstrates the project which this article describes how to build.
The article breaks the project down into several stages:
Configuring the OS
The requirements for the hardware are to have a set of switches and to provide enough for a player to control a standard MAME game. This involves the following switches:
GPIO Pin Allocation
One Player Start
Two Player Start
Circuit for each switch of a GPIO pin
The hardware is very simple, the circuit required for each switch is identical. However, because there are fourteen switches, the wiring required on tri-pad board is quite a lot. There is a 10K pull up resister on each circuit to ensure the default state of the switch when open is high. And there is a 1K resistor on each switch to restrict the current flowing from the GPIO pin when the switch is closed, in case the pin has been configured as an output.
The switches are the only components mounted on the top of the tri-pad board, so that the lid fits flush with the board. This is to keep the profile of the enclosure as low as possible. The board is cut to fit around the tall components on the Raspberry Pi board, again to keep the profile of the overall case as low as possible.
The resistors are mounted on the lower side of the tri-pad board. Though there are fourteen switches to wire up and many wires are required, the layout management below the board is still relatively easy. The headers are just the right height to mount the board on the Raspberry Pi, I have added the P6 pins for support of the opposite side of the board to the GPIO pins.
The Raspberry Pi is mounted in the lower part of the case using the mounting holes on the Rev 2 Raspberry Pi.
The tri-pad board mounts directory on the header pins which hold the board in place. The direction I have located the switches means the HDMI port is at the top. On reflection I will do this the other way around when the Model A Raspberry Pi is available, in the hope to mount a PAL LCD display and audio amp on top of the composite and audio sockets. To attempt to take advantage of the lower power of the Model A, and add a battery to make a portable gaming console.
With the lid in place the console is a very compact gaming device with lots of potential.
A full description of how to build the XMAME source code can be found in the following article. The source code has been modified to add a Raspberry Pi GPIO joystick driver, and the configuration files for the build have been modified to make the build very simple. Raspberry Pi MAME & MESS Build Article
Only one file has been added to the source code for the Raspberry Pi GPIO code, the file is: xmame-0.103-RPi-master/src/unix/joystick-drivers/joy_RPi.c
The other modifications are to add the joy_RPi.c file to the configuration files and add a type 6 joystick to the configuration.
When the Raspberry Pi is booted, there will be no keyboard or mouse connected. The device boots into this menu system which displays valid available game ROMs. The menu is controlled by the GPIO keys. I have made the source code for this menu system open source on GitHub. To retrieve the source code and build it use the following commands in a terminal session on the Raspberry Pi:
pacman -S unzip
Copy the application to the .xmame directory: cp MameVT100 ~/.xmame/
The menu application starts up very quickly in general use by separating the process for scanning valid ROMs into a manual process which should be run whenever the ROMs in the roms directory are altered. Run the scan now using the following commands:
./MameVT100 /root/.xmame/ /root/.xmame/roms/ "|correct|best available|" RESCAN
The menu system can also be controlled from a keyboard using the cursor keys, '1' to select an item and '2' to exit.
If there are no valid ROMs present, the menu will be a blank screen, pressing '2' to exit the menu system will still work in this event.
To configure the Raspberry Pi default keys, connect a USB keyboard and start a game from the menu. When the game is running, press the tab key, this will display the MAME configuration menu. Select Input (general), in this menu the default keys can be set for all games. Select User Interface. Define the Raspberry Pi keys for the user interface by selecting an item using the keyboard, press enter and then press the key on the Raspberry Pi for that item. This stage is a little confusing, as the keys are defined on the Raspberry Pi, from that point use the key on the Raspberry Pi rather than the keyboard. Set the keys for items UI Up, UI Down, UI Left, UI Right, UI Select, UI Clear. From now on the keys on the Raspberry Pi can be used to perform the remaining configuration. Press the exit key (the key which was just defined as UI Clear).
Now select Player 1 Controls. Set the keys for P1 Up, P1 Down, P1 Left, P1 Right, P1 Button 1, P1 Button 2, P1 Button 3, P1 Button 4, P1 Button 5, P1 Button 6, P1 Start, P1 Select. I set P1 Start, P1 Select to be the same as P1 Button 1. Press the exit key.
Now select Other Controls. Set the keys for 1 Player Start, 2 Player Start, Coin 1 . Press the exit key three times.
The controls should now be configured for use in MAME.
MAME "TAB" Menu
MAME "Input (general)" Menu
MAME "User Interface" Menu
MAME "Player 1 Controls" Menu
MAME "Other Controls" Menu
Configuring the OS
At this point the following files should be in the /root/.xmame/ directory:
ROM Menu application.
ROM Menu application session data, remembers last selected ROM between uses.
ROM Menu application list of all games which this version of MAME supports.
ROM Menu application list of local ROM status for each ROM present which this version of MAME supports.
Directory containing ROMs which have been uploaded to the Raspberry Pi.
Configure the system to auto start the MameVT100 menu application, on login, by editing the .bash_profile file and adding to the end of the file the following lines. The last line will automatically shutdown the Raspberry Pi when the menu is exited. After this if you need to access the Raspberry Pi again, you can press ^C with a keyboard when the menu is displayed and the command prompt will be displayed.
./MameVT100 /root/.xmame/ /root/.xmame/roms/ "|correct|best available|"
shutdown -h now
^O ENTER ^X
Configure the system to auto login at boot. This process is for Arch Linux, other Linux distributions probably require a different method to configure auto login. cp /usr/lib/systemd/system/getty\@.service \
Use nano to edit the following file: nano /etc/systemd/system/getty.target.wants/autologin\@tty1.service
Find the section [Service], and then modify the following line: ExecStart=-/sbin/agetty --noclear -a root %I 38400
Find the section [Install], and then modify the following line: Aliasfirstname.lastname@example.org
Save the file and exit: ^O ENTER ^X
Now run the following commands:
systemctl disable getty@tty1
systemctl start getty@tty1
Now reboot the system by typing reboot. When the system boots it should automatically login and start the menu application. The menu application will respond to Raspberry Pi GPIO key presses.