GithubHelp home page GithubHelp logo

elipsitz / gameboy-fpga Goto Github PK

View Code? Open in Web Editor NEW
26.0 1.0 0.0 1.37 MB

License: GNU General Public License v3.0

Makefile 0.41% Scala 44.41% C++ 4.14% Assembly 3.93% Tcl 25.89% Verilog 2.68% SystemVerilog 6.25% Python 8.36% Jupyter Notebook 3.92%

gameboy-fpga's Introduction

FPGA Game Boy Emulator

This repository contains a Game Boy / Game Boy Color emulator, written in Chisel, that can be run on an FPGA. Read the full writeup here.

Currently, only the Pynq-Z2 is supported, although the core of the emulator should be able to be used on any FPGA with sufficient resources.

Building

A precompiled bitstream is available under Releases.

Setup

Install Vivado

Currently this project uses Vivado 2020.2. The free edition is sufficient. Make sure to install with support for Zynq-7000 SoCs.

Install the Pynq-Z2 Vivado board files.

Additionally, you should set up a shared IP cache to speed up synthesis of Xilinx IP:

  1. Create the ~/vivado_ip_cache directory (could be located somewhere else).
  2. Create the file ~/.Xilinx/Vivado/Vivado_init.tcl
  3. In that file, add set_param project.defaultIPCacheSetting /home/eli/vivado_ip_cache/ (substituting the absolute path to the IP cache directory).

Install other tools

You'll need to install JDK 8 or later, and Scala Build Tool (sbt). Additionally, install FuseSoC.

Building the bitstream

From the top-level directory:

fusesoc --cores-root . run --build --target=pynq_z2 elipsitz:gameboy:gameboy

This will generate a .bit and .hwh file, which need to be accessible to the PS Python code.

Running

Setup

Install Pynq Linux on the Pynq-Z2, and install the pip packages listed in python/requirements.txt.

Transfer the gameboy_ps Python module (located in the python directory) to the Pynq-Z2.

Place the gameboy.bit and gameboy.hwh files from the build process in the gameboy_ps/resources directory.

HDMI and Controller

Connect an HDMI cable from the "HDMI OUT" port on the Pynq-Z2 to a monitor or TV.

Attach an Xbox-compatible controller to the USB port of the Pynq-Z2.

Running

As root, run python3 -m gameboy_ps <path to ROM directory>, passing the path to the directory containing ROM files.

The program will load the bitstream to the PL. It takes a few seconds to load all of the Pynq libraries, but the main menu should soon show up on the display.

Warning: the audio output can be quite loud. Start at the lowest setting on the monitor/TV and increase it as needed.

Building the cartridge adapter board

To play physical cartridges, you'll need to assemble the adapter board. KiCad board files can be found in the pcb/pynq_adapter_rev2 directory. The schematic is pre-populated with LCSC part numbers for easy assembly at JLCPCB.

You'll need to add a few components yourself. These are all easy to solder:

  • A 2x20 0.1mm female pin header (e.g. this one). These are used for Raspberry Pi HATs, so they're pretty easy to find.
  • A SPDT slide switch such as the EG1218, or just a 1x3 strip of 0.1mm male pin header and a jumper.
  • A GBA SP slot (such as this one). These can be found quite cheaply on eBay or Aliexpress as well. They're all pretty much the same.
  • If desired, a GBA link port (e.g. this one). These can also be found easily from other sources.

The other parts are relatively small, so I don't recommend hand soldering the board. However, if you want to assemble the whole thing yourself, in addition to assorted resistors and capacitors, you'll also need:

  • 6x 74LVC1T45DCK 1-bit level shifters.
  • 2x 74LVCH16T245DGGR 16-bit level shifters. The non 'H' version should work as well, but the 'H' (bus hold) version is preferable.

Appendix

Getting Xbox controller to work on Pynq-Z2

This needs the xpad and joydev drivers. Pynq Linux isn't built with them. If you're running Pynq Linux with kernel 5.15.19-xilinx-v2022.1, prebuilt kernel modules can be found in the extra directory in the repository. Otherwise, to build loadable kernel modules from scratch:

On the Pynq-Z2, go to /lib/modules/5.15.19-xilinx-v2022.1/build.

Download the right version of xpad.c to drivers/input/joystick/xpad.c.

And do the same with joydev.c, to drivers/input/joydev.c.

The build commands in /lib/modules need to run with sudo due to permissions issues.

Do sudo make menuconfig and set:

  • Device Drivers --> Input device support --> Joystick interface to M (for 'module')
  • Device Drivers --> Input device support --> Joysticks/gamepads --> X-box Gamepad support to M (for 'module')

Save and exit.

Then build the modules:

sudo make scripts
sudo make distclean
sudo make prepare
sudo make modules_prepare

sudo make -C . M=drivers/input/ joydev.ko
sudo make -C . M=drivers/input/joystick xpad.ko

Install the modules so that they'll be loaded automatically on boot. Copy the .ko files to /usr/lib/modules/$(uname -r)/kernel/, and append to /etc/modules:

joydev
xpad

Alternatively, use insmod <modules>.ko to load them once.

Make sure the xilinx user is in the group input: sudo usermod -a -G input xilinx (and re-login)

Install the Python library with sudo pip3 install xbox360controller

Create the device nodes if needed: https://docs.kernel.org/input/joydev/joystick.html#device-nodes

cd /dev
rm js*
mkdir input
mknod input/js0 c 13 0
mknod input/js1 c 13 1
mknod input/js2 c 13 2
mknod input/js3 c 13 3
ln -s input/js0 js0
ln -s input/js1 js1
ln -s input/js2 js2
ln -s input/js3 js3

gameboy-fpga's People

Contributors

elipsitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gameboy-fpga's Issues

CacheMetadataError when loading overlay

I have been trying to run the emulator on my PYNQ-Z2 board, but before the overlay loads, I keep encountering the same CacheMetadataError. I am using this repository for a class project, and I would really appreciate some help troubleshooting. The program's output is:

[2022-10-22 03:55:53,880][INFO] Loading Pynq libraries...
[2022-10-22 03:56:03,622][INFO] Finished loading Pynq libraries
[2022-10-22 03:56:04,176][INFO] Loading overlay...
[2022-10-22 03:56:04,190][DEBUG] Using selector: EpollSelector
Traceback (most recent call last):
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/pl_server/embedded_device.py", line 252, in get_parser
    parser = self._get_cache() 
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/pl_server/embedded_device.py", line 228, in _get_cache
    raise CacheMetadataError(f"No cached metadata present")
pynq.pl_server.embedded_device.CacheMetadataError: No cached metadata present

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/xilinx/final/gameboy_ps/__main__.py", line 17, in <module>
    system = system.System(rom_directory)
  File "/home/xilinx/final/gameboy_ps/system.py", line 15, in __init__
    self.gameboy = Gameboy()
  File "/home/xilinx/final/gameboy_ps/gameboy.py", line 72, in __init__
    self.overlay = Overlay(str(overlay_path))
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/overlay.py", line 319, in __init__
    self.device.set_bitfile_name(self.bitfile_name)
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/pl_server/device.py", line 118, in set_bitfile_name
    self.parser = self.get_bitfile_metadata(self.bitfile_name)
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/pl_server/embedded_device.py", line 690, in get_bitfile_metadata
    parser = _get_bitstream_handler(bitfile_name).get_parser(partial=partial)
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynq/pl_server/embedded_device.py", line 254, in get_parser
    parser = RuntimeMetadataParser(Metadata(input=self._filepath.with_suffix(".hwh")))
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynqmetadata/frontends/metadata.py", line 40, in Metadata
    return HwhFrontend(_hwhfile=input)
  File "<string>", line 25, in __init__
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynqmetadata/frontends/hwh_frontend.py", line 219, in __post_init__
    self.parse()
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynqmetadata/frontends/hwh_frontend.py", line 240, in parse
    self.resolve_addressing()
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynqmetadata/frontends/hwh_frontend.py", line 596, in resolve_addressing
    self._resolve_subordinate_addressing()
  File "/usr/local/share/pynq-venv/lib/python3.10/site-packages/pynqmetadata/frontends/hwh_frontend.py", line 452, in _resolve_subordinate_addressing
    core = self.blocks[i.get("INSTANCE")]
KeyError: 'M_AXI_0'

Additional info:

  • Using v3.0.1 of the PYNQ-Z2 SD card image
  • Using the precompiled bitstream
  • Tested with multiple ROM files
  • Followed set-up instructions: moved gameboy.bit and gameboy.hwh to gameboy_ps/resources; loaded xpad and joydev drivers from the repo's extra directory; installed required Python packages

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.