Monday, January 9, 2023

Building an SD Card-Sized Handheld NES Emulator with Raspberry Pi Zero W

I built a tiny NES emulator console with a 1.4-inch LCD and integrated joystick, using a board the same size as a Raspberry Pi Zero W.
Here's what it looks like:



The board shipped with no manual, and even following the manufacturer's documentation didn't get a game on screen. After considerable trial and error, here's what actually works.

The overall process:
  1. Install the game emulator OS (RetroPie / Raspbian) to an SD card
  2. Configure WiFi & SSH and connect to Raspberry Pi Zero W
  3. Install the LCD driver
  4. Enable the joystick on the LCD board
  5. Play

Parts

Amazon
Pi Zero works but having WiFi built in makes the whole process easier.
Amazon prices on this can get absurdly inflated — watch out.

Amazon
The main component for this build.
The Amazon listing says a manual is included — it isn't. The chip type is also unknown.
Going in blind is a dead end. This post is the missing documentation.

Other things needed: SD card, portable battery, USB cable — use whatever you have.

Installing RetroPie on Raspberry Pi

Download the OS Imager from the official Raspberry Pi website and install it on your PC.
The Imager is self-explanatory, so setup steps are skipped here.

The OS to install is not standard Raspbian — choose:
Emulation and game OS → RetroPie → RetroPie 4.8 (RP 1/Zero)

Enabling WiFi & SSH

After writing the OS to the SD card it auto-unmounts — eject and reinsert it to remount.
  1. The "boot" folder will be mounted. Create a file named wpa_supplicant.conf directly in that folder with the following content:
    country=JP
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    network={
            ssid="{your SSID}"
            psk="{WiFi password in plain text}"
    }
  2. Create an empty text file named "ssh" (no extension) in the "boot" folder.
    This enables SSH on first boot.

SSH into Raspberry Pi Zero W

Insert the SD card into Raspberry Pi Zero W and connect a portable battery.
The LCD will be all white at this point — that's expected, ignore it.

If WiFi is configured correctly, the Pi will appear on the network after a moment. Check with:
ping retropie.local
Once SSH starts, connect with:
ssh pi@retropie.local
Default password: raspberry

Installing the LCD Driver

Follow the Waveshare documentation:
https://www.waveshare.com/wiki/1.44inch_LCD_HAT
  1. Enable the SPI Interface in raspi-config
    sudo raspi-config
    # Select: Interfacing Options → SPI → Yes
    sudo reboot
    
  2. Install the BCM2835 library
    wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
    tar zxvf bcm2835-1.71.tar.gz
    cd bcm2835-1.71/
    sudo ./configure && sudo make && sudo make check && sudo make install
    
  3. Install wiringPi
    sudo apt-get install wiringpi
    wget https://project-downloads.drogon.net/wiringpi-latest.deb
    sudo dpkg -i wiringpi-latest.deb
    gpio -v
    # Should show version 2.5.x
    
    git clone https://github.com/WiringPi/WiringPi
    cd WiringPi
    ./build
    gpio -v
    # Should show v2.6 or v2.7
The Waveshare manual continues with Python library installation — not needed for this build.


Installing the FBCP Driver

This is a framebuffer control driver. After this step, a console finally appears on the LCD — though the text is far too small to read.
  1. Build the driver
    cd ~
    sudo apt-get install cmake -y
    sudo apt-get install p7zip-full -y
    wget https://www.waveshare.com/w/upload/f/f9/Waveshare_fbcp.7z
    7z x Waveshare_fbcp.7z -o./waveshare_fbcp
    cd waveshare_fbcp
    mkdir build
    cd build
    cmake -DSPI_BUS_CLOCK_DIVISOR=20 -DWAVESHARE_1INCH44_LCD_HAT=ON -DBACKLIGHT_CONTROL=ON -DSTATISTICS=0 ..
    make -j
    sudo ./fbcp
  2. Configure to run at startup
    sudo cp ~/waveshare_fbcp/build/fbcp /usr/local/bin/fbcp
    sudo nano /etc/rc.local
    # Add "fbcp &" before the "exit 0" line at the end
  3. Set the display resolution
    Append to /boot/config.txt. For Raspberry Pi 4, refer to the Waveshare manual for additional settings.
    hdmi_force_hotplug=1
    hdmi_cvt=300 300 60 1 0 0 0
    hdmi_group=2
    hdmi_mode=87
    display_rotate=0
  4. Reboot
    sudo reboot


Getting the RetroPie Screen to Show on the LCD

At this point the console appears on the LCD, but the RetroPie (EmulationStation) interface doesn't.
Enable Fake KMS via raspi-config → Advanced → GL Driver → Fake KMS.
If that doesn't work, Full KMS is also reportedly an option.
After rebooting, the EmulationStation startup screen should appear.

At this point the controller configuration screen will show up — and since the joystick isn't recognized yet, nothing can be done.
If you have a USB controller handy, connecting it to the Raspberry Pi Zero W's Micro USB port will let you navigate.


Enabling the Joystick

Getting EmulationStation to recognize the joystick took some effort. This page was the key reference:
"てきとうにブログ — Raspberry Pi Zero の例"

curl -O https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/retrogame.sh
sudo bash retrogame.sh
# When prompted, select option 3
After rebooting, edit /boot/retrogame.cfg:
LEFT 5 # Joypad left
RIGHT 26 # Joypad right
UP 6 # Joypad up
DOWN 19 # Joypad down
LEFTCTRL 13 # 'A' button JOY PRESS
LEFTALT 16 # 'B' button BTN3
Z 20 # 'X' button BTN2
X 21 # 'Y' button BTN1
ESC 13 16 # Exit ROM JOY + BTN3
These mappings were taken from:
MELTWATER's RASPBERRY PI HARDWARE

After rebooting, the joystick and buttons will work and the key assignment screen will appear for further customization.


Playing

Access retropie.local via file sharing and copy ROM files into the "roms" folder.
Sample ROMs are sometimes included with the RetroPie image — the easiest way to get started.
For dumping ROMs from cartridges you own, refer to other guides for that process.

The finished result:




No comments:

Post a Comment