(Almost) pixel-perfect arcade emulation on Raspberry Pi with AdvanceMAME

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • (Almost) pixel-perfect arcade emulation on Raspberry Pi with AdvanceMAME

      Hi, this is an (updated) translation of a tutorial that I wrote on Arcadeitalia two years ago, which has become quite popular at the time.
      Even if a little late, I'd like to share it on Arcade Projects (sorry for my bad "Engrish", any translation improvement is welcome, of course).

      A couple of years ago emulation on Raspberry Pi was (IMHO) rather inconsistent due to two technical limitations: very low computing power and absence of a good analogue video output (unless using adapters that introduced lag and invalidated the convenience of RPi as in this old but yet heroically pioneering tutorial).

      At the beginning of 2016, however, things changed: the Raspberry Pi 3 finally had enough computing power to emulate decently most of the arcade classics of the early '90s.
      As for analogue video output, there have been two significant improvements:
      1. Gert's VGA 666 board: a small hat that, through the GPIO port, provides a direct VGA output, i.e. analogue video signal in RGB and no lag.
      2. An "explosive" combo of two new firmware features: creation of custom non-standard video modes and the ability to enable and modify them on the fly.
      At that time AdvanceMAME (a port of MAME 0.106) ran adequately on RPi 3 at fixed resolution, so in november 2016 I contacted Andrea Mazzoleni (developer of the Advance project) to chat about these new features and possibilities. A pleasant correspondence started (he is Italian like me), and now AdvanceMAME offers full support to Raspberry Pi including automatic creation of 'perfect' video modes with correct size and clock.

      Neogeo MV-1FZ and AdvanceMAME on Raspberry Pi: which is which?

      Not a noisy PC with modded GPU and Windows' hourglass, just a 35 $ Raspberry Pi 3! :thumbup:

      Compact, silent, inexpensive.
      Here's how it works.

      • Raspberry Pi 2 or 3 (with keyboard, power supply and micro-SD card)
      • Gert's VGA 666 hat
      • VGA - SCART cable (USB powered)
      • CRT TV with (SCART) RGB input, preferably multistandard (PAL 50Hz and NTSC 60Hz), or Arcade monitor of course
      • PC (or Mac) with a micro-SD card reader (only for initial configuration)
      • HDMI monitor (only for initial configuration)

      Download Raspbian LITE OS here: raspberrypi.org/downloads/raspbian/.
      Unzip the archive and write (DO NOT copy) the ISO image on your micro-SD card: raspberrypi.org/documentation/…lation/installing-images/.

      Connect RPi to Internet: wired (ethernet) connection, which I recommend to beginners, does not requires any specific configuration in most cases.
      Get ready for first boot: insert the micro-SD card and connect keyboard, HDMI monitor and power supply to RPi.
      It will turn on and you'll see the boot sequence, then you will be asked to login with username (pi) and password (raspberry).

      After the first boot it is recommended to run raspi-config utility to expand the filesystem and set correct date and time. Type:
      sudo raspi-config

      First of all expand the filesystem to take advantage of all the available storage on the micro-SD card: select the EXPAND FILESYSTEM option and follow the instructions (millennials: navigate with arrows and TAB keys, SPACE to select, ENTER to confirm, ESC to cancel). I would also suggest to enable auto-login and disable wait-for-network in BOOT OPTIONS. Finally, make sure that date and time are correct in INTERNATIONALISATION OPTIONS > CHANGE TIMEZONE (this to prevent issues during the build process of AdvanceMAME)
      Exit and reboot.

      Now it is good to check if there are updates for the OS or its packages. Type:
      sudo apt-get update

      ...and then update with:
      sudo apt-get upgrade

      You will be asked to confirm installation with uppercase "Y". At the end you will be greeted again by the flashing cursor of the prompt.

      Time to build AdvanceMAME and AdvanceMENU from sources. First of all, install all the tools required to download and build latest source code on RPi, taking care to correctly type names of the various packages (git, autoconf etc.) followed by a space as separator:
      sudo apt-get install git autoconf automake libsdl2-dev libasound2-dev libfreetype6-dev zlib1g-dev libexpat1-dev libslang2-dev libncurses5-dev

      Confirm the installation with uppercase "Y".
      Proceed to download of AdvanceMAME source code from the official GitHub repository, using the git tool just installed:
      git clone https://github.com/amadvance/advancemame.git

      Source code will be downloaded into the advancemame folder of user's home, so enter this folder to continue:
      cd advancemame

      To start the build process, which will generate the optimized executables of AdvanceMAME and AdvanceMENU, two preliminary steps are needed.
      First launch the script that generates the configuration file needed to build the sources:
      sh autogen.sh

      Then configure automatically all the parameters:

      ...and finally start building:
      make -j3

      The "-j3" option will use three parallel processes, speeding up the building process (which, in any case, will take about half an hour to complete, so it's the right time for a nice cup of tea, coffee etc). At the end, if everything was successful, you'll see the prompt flashing without errors. This means that in the source code folder the binary (executable) files of AdvanceMAME and AdvanceMENU have just been created.

      Now launch the installation script which will copy all the executable files into the right folders to make everything work properly:
      sudo make install

      cd ..
      advmame robby

      If MAME starts up, congratulations: you have just built (and installed) latest version of AdvanceMAME and AdvanceMENU on your Raspberry Pi!

      MANUAL SETUP (recommended for CRT TVs)
      If you will use the Raspberry Pi on an old CRT TV via VGA 666 hat and SCART cable (RPi > VGA 666 > VGA-SCART > CRT TV), it's preferable to manually select and fine-tune the "archetypical" (basic) video modes supported by your TV: among these AdvanceMAME will then automatically pick the best one for the game ROM, varying horizontal resolution and clock in order to offer the most accurate visual experience possible. Next steps are aimed at configuring AdvanceMAME in this way, while the advv utility will be used for video modes selection and tuning.

      Enter in the hidden configuration folder of AdvanceMAME:
      cd .advance

      Open with a text editor (on Raspbian there's one named "nano") the config of AdvanceMAME:
      sudo nano advmame.rc

      Since you will use the RPi on a multistandard cathode TV (i.e. capable of supporting both PAL 50Hz and NTSC 60Hz), add the following line:
      device_video_clock 4-100 / 15-16 / 50-60

      ...then look for the line with display_adjust: this option controls how AdvanceMAME generates the video modes, modify it as follows:
      display_adjust xclock

      AdvanceMAME will adjusts the available modeline's horizontal resolution and the vertical clock.

      Now look for the line with display_magnify. This option suggests the adoption of a double or bigger resolution video mode. Since it is mainly used to enable effects like 'scalex', 'scalek', 'lq', 'hq' and 'xbr' which are beyond the purpose of this tutorial, stick it to normal size:
      display_magnify 1

      Finally look for the line with display_resize. It suggests the favorite image stretching when a video mode with the correct size isn't available, so stick it again to the original size:
      display_resize none

      Save changes ("CTRL+X" then "Y") and, once back at the prompt, turn off the RPi with

      Now it's time to connect the RPi to the CRT TV, but do another operation meanwhile. Remove the micro-SD card from RPi and insert it into the card reader of your PC: two volumes will open. Not recognizing the Linux filesystem, Windows will ask to format one partition: obviously do NOT proceed, and click CANCEL instead. On the other partition, perfectly readable in Windows (and Mac) there is the Raspberry Pi main configuration file named config.txt.
      Open it with a text editor (I would suggest Notepad++ or TextWrangler) and change it as follows:

      Source Code: config.txt

      1. device_tree=bcm2710-rpi-3-b.dtb
      2. dtparam=i2c_arm=off
      3. dtparam=i2s=off
      4. dtparam=spi=off
      5. dtparam=uart0=off
      6. dtparam=uart1=off
      7. dtoverlay=pi3-disable-bt-overlay
      8. dtoverlay=pi3-enable-wifi
      9. dtoverlay=vga666
      10. enable_dpi_lcd=1
      11. display_default_lcd=1
      12. dpi_group=2
      13. dpi_mode=87
      14. hdmi_timings=320 1 20 29 35 224 1 10 14 16 0 0 0 60 0 6400000 1
      15. scaling_kernel=8
      16. gpu_mem=128
      17. dtparam=audio=on
      18. sdtv_mode=16
      19. boot_delay=1
      20. force_turbo=1
      Display All
      Let's see briefly what these options do:
      • lines 1-6 free the GPIO pins for the VGA 666 hat
      • lines 7-9 disable Bluetooth (not mandatory), enable Wifi (not mandatory) and, most of all, enable the VGA 666
      • lines 10-11 set VGA 666 as main video output
      • lines 12-14 are very important: they set a custom video mode (mode 87) that AdvanceMAME can change on the fly. At boot a 320x224 resolution (like SEGA Genesis) is set.
      • line 15 is a little-known but fundamental option that sets a nearest-neighbour scaling of RPi framebuffer, avoiding any kind of blending or filtering
      • lines 16-17 enable audio and set the memory split
      • line 18 is a nice "extra feature" beyond this tutorial (so it's not mandatory) which enables 240p through the composite-out of RPi (very nice for console emulation!) ;)
      • lines 19-20 are not mandatory, but they disable dynamic clocking (so all frequencies and voltages stay high), and preserve the micro-SD card from corruption (if a yellow lightning strike icon appears on the screen, get a power supply with a higher or more stable amperage)
      Save changes to config.txt, eject the micro-SD card (both partitions) from PC and finally hook the RPi to the TV: place the VGA 666 hat on the GPIO port (check picture for right orientation). Connect the VGA-SCART cable as follows: VGA socket to VGA 666; SCART socket to TV, USB connector to one of the RPi ports, mini-jack to 4 pole stereo-output/composite-video port of RPi. Don't forget the keyboard, and lastly the power supply to turn it on.

      If everything is fine, the boot sequence of RPi will appear on the "AV-1" channel of your CRT TV!

      Selection of basic video modes through advv
      You are almost there! Now it's time to manually select and fine-tune the "archetypical" (basic) video modes supported by your TV: it might seem a bit inconvenient on CRT, but there are no alternatives. :thumbup:

      BTW, although I explain how to do it now, I really would suggest doing this afterwards, and precisely after loading the ROMs on the RPi: I realized indeed that it is much more convenient to open in AdvanceMAME only the games that immediately interest us, and after checking the original video mode in the Information menu to useadvv to enable the closest video mode available (AdvanceMAME will do the rest). In this way all game ROMs with similar video modes will be just ready and the dvmame.rc configuration file will only be populated by video modes actually useful, without redundant or duplicate config lines.

      So open the almighty advv utility typing:

      A long list of video modes will be presented: those in red are not supported by TV (ignore them) while that in blue should be supported, being in the range of the values set with the device_video_clock option in advmame.rc.
      So choose with the arrow keys a supported video mode, press ENTER and you'll see "the magic": advv will set that resolution (and refresh rate) on the fly, allowing a slight centering with directional arrow keys and sizing with "I" and "K" letter keys. ENTER again to save your adjustments for this video mode.
      Now back to the main list, tag the video mode just tested with SPACE BAR: it will be highlighted and added in the "basket" of valid video modes on which AdvanceMAME will dynamically offer the most accurate one for the game ROM (with appropriate automatic adjustments of horizontal resolution and clock, as previously stated in advmame.rc config file with the display_adjust xclock option).

      Pressing F2 key will save the list of all modes selected: you can repeat these steps for every basic video mode supported by your TV at the same time or do it gradually instead, depending on the game actually played and running advv each time is needed, as previously suggested (remember that games with similar video modes will all benefit from the same basic video mode, appropriately optimized by AdvanceMAME automatically).

      AUTOMATIC CREATION OF 'PERFECT' VIDEO MODES (for multisync PC monitors and Arcade monitors)
      Coming soon...

      Given that the default folder for ROMs is located in /home/pi/.advance/roms, copying them from a USB stick should be quite simple and straightforward for those who already know a minimum of Linux CLI. Anyhow, I briefly describe the procedure below.

      Get a USB flash drive formatted in FAT (FAT16 or FAT32) and on your PC copy the ROMs to a folder, i.e. "advroms".
      Now connect the USB stick to one of the RPi ports, turn it on and, if this is the first time you do this, type:
      mkdir usb

      So you have created the "usb" folder to read the contents of your USB stick.
      Now mount the device (sda1) right in this folder:
      sudo mount /dev/sda1 ./usb

      Enter the "advroms" folder and view the files it contains:
      cd ./usb/advroms

      Now copy all files to AdvanceMAME roms folder:
      cp * /home/pi/.advance/roms

      ...and that's it! (eventually cd /home/pi/ will always bring you back to home folder)

      Coming soon...

      Coming soon...

      START ADVANCEMENU AT BOOT (hiding verbose)
      Coming soon...

      ENABLE KIOSK MODE (load/save states and turn off RPi with Joystick)
      Coming soon...

      Coming soon...

      Coming soon...

      Coming soon...

      The post was edited 30 times, last by Yami ().

    • PC for MAME is the way to go. RPi can't play Killer Instinct (fact). Probably 3-5 hardware revisions away until an RPi is in the same ballpark as a PC (opininon). By then, RPI will have to compete with the MiSTer and the like in the FPGA arena. The trick is in mastering a PC's more complex setup for MAME. But once you have that down, it is fun, Windows is completely hidden, the front-end is more robust, and you can play all working games in MAME.

      I love the RPi. But I love my arcade/retro gaming more. The PC is still my choice for emulation overall, including MAME.
    • acblunden2 wrote:

      PC for MAME is the way to go. RPi can't play Killer Instinct (fact). Probably 3-5 hardware revisions away until an RPi is in the same ballpark as a PC (opininon). By then, RPI will have to compete with the MiSTer and the like in the FPGA arena. The trick is in mastering a PC's more complex setup for MAME. But once you have that down, it is fun, Windows is completely hidden, the front-end is more robust, and you can play all working games in MAME.

      I love the RPi. But I love my arcade/retro gaming more. The PC is still my choice for emulation overall, including MAME.

      It's a matter of taste, and one does not exclude the other.

      Personally I don't care of Killer Instinct, and although I have a gaming PC with, among other things, an i7 and latest version of MAME, I find it impractical to configure and connect to CRT TV. On RPi setup for CRT is fast and easy (at least for my needs), input lag is low, and the hardware is tiny, silent and with low consumption.

      Excluding the RPi only because PC does exists it means not having fully understood the meaning of the Raspberry project IMHO.
      But it really depends on what you need. RPi it's an extra option.

      The post was edited 1 time, last by Yami ().

    • Hatsune Mike wrote:

      This doesn't look pixel perfect at all, there is some pretty clear blending making a single row span across two scanlines in the pictures posted. This is probably a configuration problem, but calling it pixel perfect is really pushing it...
      That was my old 14'' Daewoo CRT (old pictures and not in full screen).
      On my 'new' 20'' Sony Trinitron really I can't spot any difference between RPi and my Neogeo CMVS (using a RGB switch selector).

      Results are better visible in this video (not mine, watch it in 1080p full screen)
    • Pixel Perfect and Raspberry Pi are two terms that should never go together.

      I speak from a high level of experience. I created many custom video timing scripts, spoke with Rpi hardware developers, spent countless hours writing code etc, pixel perfect is simply not possible. The Rpi has a fixed pixel clock at low resolutions which prohibit exact timings. You can get close, or use stretched and scaled horizontal resolutions to lessen the pixel clock limit, but you cannot achieve pixel perfect results.

      Arcade games all run at varying vertical refresh rates, they're not locked to exactly 60Hz. Aside from incorrect resolutions you will experience incorrect horizontal and vertical frequencies, resulting in out of centre images and stuttering in movement / scrolling. You can fix one but not the other, as in both problems can't be addressed simultaneously, due to working within a fixed pixel clock.

      I'm not saying it's not worth it and that good results can't be obtained, but the claim of 'pixel perfect' is an embellishment.

      The post was edited 1 time, last by Frank_fjs ().

    • I don't think it's perfect, but it is very impressive for such a small device.
      Darksoft/Apocalypse: CPS2, CPS3, F3-β, MVS, S16-β
      Marqs: OSSC v1.5/1.6 VGP, OSSC v1.6 Kaico
      invzim: Jammafier v1.6b/1.7c, Tri-Sync Helper, JVSPAC2
      XianXi: JNX Raiden, SC Taito Classic, SC Sega S16/24, Namco S11/12 KHA
      Frank_fjs: JAMMA Extender SE-HAS, Jassifier v1.6
    • I've been making my own homebrew pi to jamma boards for a little while now, with the help of quite a few others.
      I use a modified image that is tailored for pi use in a arcade cab, and runs exclusively at 15khz, with various mode timings for varying game resolutions.

      My boards have a 'service button' on them, so that by default they load directly into the last game played (just like a real PCB). To change game, I have to open the front door of the cab and press the service button to access the games list menu. This avoids time spent navigating annoying menus - I just power up, play, and switch off the cab when I'm done, just like with a real PCB. I treat it much more as a 'pcb replacement' than a multi-board.
      Emulation is near 1:1 on many games and highscores are saved. With the game PCBs I own, I've been able to side-by-side on near identical Dino King cabinets. To play stuff like Cave shooters without a 4 figure outlay is a joy, frankly.

      I'm just moving onto making boards using pi3B+ - the extra processing power should allow titles on Taito F3 for instance to play well (Elevator Action Returns was laggy at times), and stop the occasional sound issues on the Sega sprite scalers like Outrun & Powerdrift.

      Power Drift


      Attract modes (which of these is running on a pi...)
      [astrocity] Wanted: - PGM Cave conversions, Dino King / Mushiking / Love n Berry coin box, coin chute, back door. Time machine.

      The post was edited 2 times, last by Flinnster ().

    • @Frank_fjs Yes the Rpi has locked pixel clock but the good news is it's a driver problem. When you use X11 to render video you unlock the pixel clock. Ben Templeman worked with the Retroarch team to integrate modeline generation with Retroarch Linux and Windows. He told me Linux is completely unlocked with X11 but the Windows version is still locked to 60hz because he needed more documentation on the CRT Emu drivers usage to achieve what Groovymame can do with Windows limitations. He's also tried to compile an Rpi distro with X11 which works with no limitation. He tried to reach the gaming distributions devs but they find X11 integration too memory consuming and not going to the same direction they are heading with all those LCD overlays. So now he's working alone on that it's just a matter of time for him to release a distro that could support perfect modelines and guess what, he's using hdmi>vga adapter so no need for gpio with X11.
      Any Multi MVS bug ? Before asking delete BUFF FILE on the SD card and Reset NVRAM procedure : Power on without cart, Press Start multiple times until it asks to press ABC + Start, Do it, Power off > Done

      The post was edited 2 times, last by archimage ().

    • Frank_fjs wrote:

      ...but I can't use it as a dedicated gaming device as I can notice the difference between it and real hardware quite drastically. That's just me though, I'm an elitist snob. :D
      @Frank_fjs I am aware of limited pixel clock of RPi and what you write is undoubtedly true. Topic title was inherited indeed by the original tutorial, intentionally enthusiastic and hyperbolic to "wake up" the consciences of those who perhaps replaced the cabinets' arcade monitors with LCDs to more easily use Retropie (however isn't more hyperbolic than the "arcade perfect" declared by Capcom for the SF 30th Anniversary Collection, to say). I'm going to fix the new title to avoid confusion and disinformation. :thumbup:

      This afternoon, however, I made a couple of quick shots of both AdvanceMAME on RPi and my MV-1FZ.
      My Sony Trinitron's grid is undoubtedly not ideal to highlight the "drastic difference" between the two, but I'm sure that an "elitist snob" like you (I'm joking, of course!) will have no difficulty in recognizing the impostor. :P ;)






    • Ben Templeman told me Lakka would integrate his work to their distro, I guess CRT switchres is integrated now but still locked to 60hz ?
      Any Multi MVS bug ? Before asking delete BUFF FILE on the SD card and Reset NVRAM procedure : Power on without cart, Press Start multiple times until it asks to press ABC + Start, Do it, Power off > Done
    • Ok Ben is kinda alone trying to do this, the problem is most devs don't want to jump into X11 to make this available. He made this work as a proof of concept but need more optimization for public release. His setup works directly from hdmi-vga and modelines are already implemented to Retroarch. He also made Retroarch capable of displaying 240p with 31khz monitors with double frame option at 120hz refresh rate to make these monitors support 240p, with thick authentic scanlines as 15khz monitors do. Really awesome possibilities behind this hardware but lack of communication and convergence in this area so the guy is kinda tired and busy with his job/family to do this alone.
      Any Multi MVS bug ? Before asking delete BUFF FILE on the SD card and Reset NVRAM procedure : Power on without cart, Press Start multiple times until it asks to press ABC + Start, Do it, Power off > Done
    • Sounds like a perfect solution @archimage!
      Hoping he can finalize it on his own and we can enjoy it at some point, will follow this closely ;)

      But up to that point I will also try @Scandy’s guide!
      Any idea if it also works on RPi2?
    • It will work with Rpi2 as well.

      I know where @Scandy 's setup heading, our paths crossed some years ago in Recalbox forums. So it's not something new to me because I also tried a lot of solutions. ;)

      However It's always interesting to learn how to do this so I can only encourage his tutorial because it is a very good initiative.

      I guess he can also cover different ways to do it hardware wise as gert's vga 666 module is today declined in different solutions : RGB-Pi Pi2scart Rpi2Jamma Rpi2Scart Rpi2Nuc Hdmi2Vga...

      Then software wise there are more or less ready solutions : RGB-Pi Rpi2Jamma Pi2scart distros or Lakka with the CRT switchres. They use different approaches.

      IMHO in the present time there are lot of solutions already known this could be a wider tutorial rather than focusing only on advancemame and scart to achieve 240p with Rpi.

      If it's worth focusing on this particular setup, it could be interesting to know what are pros and cons compared to other setups.
      Any Multi MVS bug ? Before asking delete BUFF FILE on the SD card and Reset NVRAM procedure : Power on without cart, Press Start multiple times until it asks to press ABC + Start, Do it, Power off > Done

      The post was edited 1 time, last by archimage ().