Coding for fun - the hard way. Trying to implement a Game Boyยฎ compatible machine with Verilog.
This project consists of two parts:
- An open source Game Boyยฎ compatible console Verilog RTL implementation (the VerilogBoy Core)
- An open source FPGA-based handheld game console hardware (the VerilogBoy Handheld, or vbh for short.)
Both are hosted in this repository.
The main system architecture is designed as follows (outdated):
There are three major parts needs to be implemented: the Game Boy CPU (8-bit CISC Processor called SM83, Intel 8080 like), the PPU (Pixel Processing Unit), and the sound unit (sometimes referred as a PSG, Programmable Sound Generator). Several interfacing modules are needed to support the IO capability provided by the specific hardware (FPGA development board or "VerilogBoy Handheld").
Current Pano Logic G1 is the only supported platform. ML505 and VBH support are planned.
Refactoring in progress. Current version could run several commerical games with no noticable glitch on the Pano Logic G1 device. 'Master' branch contains the previous version that runs on the ML505.
- SM83 CPU (Refactoring stage 1 done, passes all Blargg's tests)
- PPU (Pixel Processing Unit, Pixel-FIFO architecture)
- OAMDMA (Object Attribute Memory DMA)
- PSG (Programmable Sound Generator)
- Timer
- Link (Dummy link module, allows some games to run in single player mode)
Next step would be improving the accuracy.
Photo of VerilogBoy on Pano G1 running open source GameBoy game Tobu Tobu Girl:
For progress regarding different ports, view README.md under the specific target folder.
This project is not built to be entirely accurate, but built with accuracy in mind. Most of the CPU timing should follow the original DMG-CPU, and implement a rudimentary 2-stage pipelining just as the original one does.
Here are the results of several tests I have tried to run on it.
PPU implementation is ... wrong: There are two pipelines in the GameBoy PPU, one for BG and one for sprite. I only implemented one for both. There is only one fetcher, though.
Note: Tests which depends on the revision / model of GameBoy are omitted. VerilogBoy only focus on behaviors that are common among all monochrome GameBoys (GS).
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
cpu instrs | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
Notes: other tests hasn't been tried.
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
add sp e timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
call timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
call timing2 | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
call cc_timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
call cc_timing2 | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
di timing GS | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
div timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
ei sequence | ๐ | ๐ | ๐ | ๐ | โ | ๐ |
ei timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
halt ime0 ei | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
halt ime0 nointr_timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
halt ime1 timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
halt ime1 timing2 GS | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
if ie registers | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
intr timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
jp timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
jp cc timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
ld hl sp e timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
oam dma_restart | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
oam dma start | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
oam dma timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
pop timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
push timing | ๐ | โ | โ | ๐ | ๐ | ๐ |
rapid di ei | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
ret timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
ret cc timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
reti timing | ๐ | โ | ๐ | ๐ | ๐ | ๐ |
reti intr timing | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
rst timing | ๐ | โ | โ | ๐ | ๐ | ๐ |
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
daa | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
ie push | ๐ | โ | โ | โ | โ | ๐ |
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
basic | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
reg_read | ๐ | ๐ | ๐ | โ | โ | ๐ |
sources dmgABCmgbS | ๐ | ๐ | โ | โ | โ | โ |
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
boot sclk align dmgABCmgb | โ | ๐ | ๐ | โ | โ | โ |
Note: this test only seems to test the time to finish the first transfer. What about the second? (Delta time required to do a transfer and get notified by the interrupt)
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
hblank ly scx timing GS | ๐ | ๐ | โ | โ | ๐ | โ |
intr 1 2 timing GS | ๐ | ๐ | ๐ | ๐ | ๐ | โ |
intr 2 0 timing | ๐ | ๐ | โ | ๐ | ๐ | โ |
intr 2 mode0 timing | ๐ | ๐ | โ | โ | ๐ | โ |
intr 2 mode3 timing | ๐ | ๐ | โ | โ | ๐ | โ |
intr 2 oam ok timing | ๐ | ๐ | โ | โ | ๐ | โ |
intr 2 mode0 timing sprites | โ | ๐ | โ | โ | ๐ | โ |
lcdon timing dmgABCmgbS | โ | ๐ | โ | โ | โ | โ |
lcdon write timing GS | โ | ๐ | โ | โ | โ | โ |
stat irq blocking | โ | ๐ | ๐ | โ | ๐ | โ |
stat lyc onoff | โ | ๐ | โ | โ | โ | โ |
vblank stat intr GS | ๐ | ๐ | โ | ๐ | ๐ | โ |
Test | mooneye-gb | BGB | Gambatte | Higan | MESS | VerilogBoy |
---|---|---|---|---|---|---|
div write | ๐ | ๐ | โ | ๐ | ๐ | ๐ |
rapid toggle | ๐ | ๐ | โ | โ | ๐ | ๐ |
tim00 div trigger | ๐ | ๐ | ๐ | โ | ๐ | ๐ |
tim00 | ๐ | ๐ | โ | ๐ | ๐ | ๐ |
tim01 div trigger | ๐ | ๐ | โ | โ | ๐ | ๐ |
tim01 | ๐ | ๐ | ๐ | ๐ | ๐ | ๐ |
tim10 div trigger | ๐ | ๐ | โ | โ | ๐ | ๐ |
tim10 | ๐ | ๐ | โ | ๐ | ๐ | ๐ |
tim11 div trigger | ๐ | ๐ | โ | โ | ๐ | ๐ |
tim11 | ๐ | ๐ | โ | ๐ | ๐ | ๐ |
tima reload | ๐ | ๐ | โ | โ | ๐ | ๐ |
tima write reloading | ๐ | ๐ | โ | โ | ๐ | ๐ |
tma write reloading | ๐ | ๐ | โ | โ | ๐ | ๐ |
Note: things that will be removed in the near future may not shown here. Not all source files are shown here.
.
โโโ doc -- Documents
โ โโโ control_unit.ods -- CPU control unit LUT
โ โโโ cpu_internal.md -- CPU internal signal encoding
โ โโโ cpu.md -- CPU design note
โโโ hardware -- Hardware source files for VBH
โ โโโ 3d_models -- 3D Model in SolidWorks
โโโ LICENSE -- OHDL for VerilogBoy Core
โโโ Makefile -- Toplevel Makefile
โโโ README.md -- This document
โโโ roms -- Place for platform independent ROMs
โ โโโ bootrom.s -- VBC Boot ROM
โ โโโ Makefile -- Makefile for building the Boot ROM
โ โโโ tests -- Unit tests
โ โโโ compare.sh -- Script for comparing results between VBC and emulator
โ โโโ dmg_emu.exe -- Reference emulator
โ โโโ Makefile -- Makefile for running tests
โโโ rtl -- VBC RTL files
โ โโโ alu.v -- ALU
โ โโโ boy.v -- VerilogBoy portable top level file
โ โโโ brom.v -- Boot ROM
โ โโโ common.v -- Common definitions
โ โโโ control.v -- Control Unit
โ โโโ cpu.v -- CPU top level
โ โโโ dma.v -- OAM DMA
โ โโโ Makefile -- Makefile for building the VBC using Verilator
โ โโโ mbc5.v -- Cartridge paging, optional
โ โโโ ppu.v -- Pixel Processing Unit
โ โโโ regfile.v -- CPU Register file
โ โโโ singleport_ram.v -- Singleport RAM template
โ โโโ singlereg.v -- Singlebit register template
โ โโโ timer.v -- Timer
โโโ sim -- Simualtion
โ โโโ verilator -- Verilator-based simulator
โ โโโ dispsim.cpp -- LCD simulator based on SDL 2.0
โ โโโ Makefile -- Makefile for building the simulator
โ โโโ memsim.cpp -- Boot ROM or Game ROM simulator
โ โโโ mmrprobe.cpp -- Unit for dumping memory or MMR accesses
โ โโโ vb_sim.cpp -- Simulator main file
โโโ target -- Targets (ports) of VBC
โ โโโ panog1 -- Pano Logic G1 devices
โ โ โโโ fpga -- FPGA RTL
โ โ โ โโโ dualport_ram.v -- Template of dual port RAM
โ โ โ โโโ idt_clkgen.v -- Code for interfacing with clock generator
โ โ โ โโโ mig -- LPDDR memory controller
โ โ โ โโโ mig_picorv_bridge.v -- Bridge for connecting PicoRV32 and LPDDR controller
โ โ โ โโโ mig.ucf -- Timing constraint for memory controller
โ โ โ โโโ mobile_ddr.v -- Simulation model for LPDDR
โ โ โ โโโ panog1.xise -- ISE project file
โ โ โ โโโ pano_top.v -- Top level HDL
โ โ โ โโโ pano.ucf -- Main pin location constraint
โ โ โ โโโ picorv32.v -- PicoRV32 RISC-V softcore
โ โ โ โโโ picosoc_mem.v -- PicoRV32 scratchpad memory
โ โ โ โโโ simple_uart.v -- Simple UART transmitter
โ โ โ โโโ testbench.v -- Testbench with LPDDR model
โ โ โ โโโ usb_picorv_bridge.v -- Bridge for connecting PicoRV32 and USB controller
โ โ โ โโโ vga_font.v -- VGA font ROM
โ โ โ โโโ vga_mixer.v -- Mixing PicoRV32 and VerilogBoy image
โ โ โ โโโ vga_timing.v -- VGA timing generator
โ โ โโโ fw -- Firmware for RV softcore
โ โ โ โโโ bootloader -- Bootloader for loading firmware from SPI Flash
โ โ โ โ โโโ bootloader.c -- Main source code
โ โ โ โ โโโ Makefile -- Makefile for building the bootloader
โ โ โ โ โโโ sections_bl.lds -- Link script
โ โ โ โ โโโ start_bl.s -- Startup code
โ โ โ โโโ dhrystone -- Dhrystone test for RV softcore
โ โ โ โโโ firmware -- Firmware for softcore
โ โ โ โ โโโ COPYING -- GPLv2 License for firmware
โ โ โ โ โโโ firmware.c -- Main source code
โ โ โ โ โโโ isp1760.c -- ISP1760 HCD
โ โ โ โ โโโ isp_roothub.h -- RootHub emulation for HCD (optional)
โ โ โ โ โโโ Makefile -- Makefile for building the firmware
โ โ โ โ โโโ misc.c -- Delay and other common routines
โ โ โ โ โโโ sections.lds -- Link script
โ โ โ โ โโโ syscalls.c -- Syscalls for the newlib
โ โ โ โ โโโ term.c -- Simple virtual terminal
โ โ โ โ โโโ usb.c -- Host USB stack
โ โ โ โ โโโ usb_gamepad.c -- Generic USB HIB gamepad driver
โ โ โ โ โโโ usb_storage.c -- USB mass storage driver
โ โ โ โโโ README.md -- README about firmware
โ โ โโโ README.md -- README about Pano Logic G1 port
โ โโโ vbh -- VerilogBoy Handheld
โ โโโ fpga -- FPGA RTL
โ โโโ fw -- Firmware for on-board MCU
โโโ tools -- Tools for building things
โโโ bin2mif -- Convert binary blob to mif
โโโ vga_timing.xlsx -- Tool for calculating VGA timing
Tested environment: Ubuntu 18.04.2 LTS.
Dependencies: build-essential, verilator, libsdl2-dev, wine(for unit test only, for now), rgbds(not available from apt, need build manually).
On Debian/ Ubuntu, do the following:
sudo apt install build-essential verilator libsdl2-dev wine byacc flex pkg-config libpng-dev
git clone https://github.com/rednex/rgbds
cd rgbds
make
make install
At project directory:
make
Copy the bootrom.mif to where it is required. (For example, target/panog1)
At project directory:
cd roms/tests
make
./compare.sh
Please be aware currently tests are broken... (Difference in PC counting)
At project directory:
./sim/verilator/vb_sim <path_to_your_rom.gb> --mbc --nostop
Few parameters:
--nostop: Don't stop execution at halt/stop (but stop at illegal instructions)
--trace: Generate vcd waveform trace
--testmode: Disable internal Boot ROM, headless mode, limit cycles to 32K.
--noboot: Disable internal Boot ROM, code execution starts at 0x0000 rather than 0x0100.
--verbose: Enable debug output
--mbc: Enable MBC1/3/5 emulation for ROMs larger than 32KB
See README inside target directory.
VerilogBoy Handheld is an open source FPGA-based handheld game console. All design sources of the VerilogBoy Handheld are hosted in this repository.
Note: the architecture is subject to change.
- FPGA: Xilinxยฎ Spartanยฎ-6 XC6SLX16
- RAM: Using on-board PSRAM for VRAM/WRAM
- ROM: Using on-board PSRAM for game cartridge emulation
- Video output: 1.54" MIPI-DSI 320x320 IPS TFT-LCD
- Audio output: On-board I2S codec (WM8960)
Photo of the Rev. 0.2 prototype:
This project reused codes from several other projects. A great thanks to their efforts!
- https://github.com/nightslide7/Gameboy
- https://github.com/cliffordwolf/picorv32
- https://github.com/u-boot/u-boot
- https://github.com/twlostow/dsi-shield
- https://github.com/MParygin/v.vga.font8x16
These projects are used as references. Again, thanks for sharing.
These are extremely helpful resources about the Game Boyยฎ itself:
Game used for demonstration, thanks for the great game:
I'm not affiliated with Nintendo in any way. Game Boyยฎ is a registered trademark by Nintendo. Nintendoยฎ is a registered trademark. All other trademarks are property of their respective owner.
The dsicore is made by twlostow, released under LGPL 3.0.
The PicoRV32 is free and open hardware licensed under the ISC license (a license that is similar in terms to the MIT license or the 2-clause BSD license).
Some firmware code of pano-g1 target are released to public domain.
All other software codes are licensed under GNU GPL 2.0.
All other HDL codes are licensed under OHDL 1.0.
All other text documents are licensed under CC BY-SA 4.0