Mooneye GB

June 5, 2016 · View on GitHub

Mooneye GB is a Game Boy emulator written in Rust.

Build Status

The main goals of this project are accuracy and documentation. Some existing emulators are very accurate (Gambatte, BGB >= 1.5) but are not documented very clearly, so they are not that good references for emulator developers. I want this project to document as clearly as possible why certain behaviour is emulated in a certain way. This also means writing a lot of test ROMs to figure out corner cases and precise behaviour on real hardware.

Non-goals:

  • CGB (Game Boy Color) support. It would be nice, but I want to make the normal Game Boy support extremely robust first.
  • A good debugger. A primitive debugger exists for development purposes, and it is enough.
  • A user interface. Building native UIs with Rust is a bit painful at the moment.

Warning:

  • Project is WIP
  • Doesn't work properly without a boot ROM

Accuracy

This project already passes Blargg's cpu_instrs, instr_timing, and mem_timing-2 tests.

Things that need significant work:

  • GPU emulation accuracy
  • APU emulation in general

There's tons of documentation and tons of emulators in the internet, but in the end I only trust real hardware. I follow a fairly "scientific" process when developing emulation for a feature:

  1. Think of different ways how it might behave on real hardware
  2. Make a hypothesis based on the most probable behaviour
  3. Write a test ROM for such behaviour
  4. Run the test ROM on real hardware. If the test ROM made an invalid hypothesis, go back to 1.
  5. Replicate the behaviour in the emulator

All test ROMs are manually run with these devices:

DeviceModelMainboardCPU
Game BoyDMG-01DMG-CPU-04DMG CPU B
Game Boy PocketMGB-001MGB-ECPU-01CPU MGB  
Game Boy PocketMGB-001MGB-LCPU-01CPU MGB  
Super Game BoySNSP-027SGB-R-10SGB-CPU-01
Super Game BoySHVC-027SGB-R-10SGB-CPU-01
Super Game Boy 2SHVC-042SHVC-SGB2-01CPU SGB2
Game Boy ColorCGB-001CGB-CPU-03CPU CGB C
Game Boy ColorCGB-001CGB-CPU-04CPU CGB D
Game Boy ColorCGB-001CGB-CPU-05CPU CGB D
Game Boy AdvanceAGB-001AGB-CPU-10CPU AGB A
Game Boy Advance SPAGS-001 C/AGS-CPU-21CPU AGB B E
Game Boy Advance SPAGS-101 C/AGT-CPU-01CPU AGB B E

These devices will also be used, but results for old tests have not yet been verified:

DeviceModelMainboardCPU
Game BoyDMG-01DMG-CPU-02DMG CPU A
Game BoyDMG-01DMG-CPU-06DMG CPU B
Game Boy ColorCGB-001CGB-CPU-02CPU CGB B
Game Boy ColorCGB-001CGB-CPU-06CPU CGB E
Game Boy AdvanceAGB-001AGB-CPU-01CPU AGB

For now, the focus is on DMG/MGB/SGB/SGB2 emulation, so not all tests pass on CGB/AGB/AGS or emulators emulating those devices.

Performance

Always compile in release mode if you care about performance!

On a i7-3770K desktop machine I can usually run ROMs with 2000 - 4000% speed. Without optimizations the speed drops to 150 - 200%, which is still fine for development purposes.

Raspberry Pi with X11 desktop works but is too slow because there is no OpenGL acceleration.

The emulator is runnable on Android, but cross-compiling and packaging is a huge pain and touch controls would have to be implemented, so I'm not supporting Android at the moment.

Running the emulator

GUI

  1. cargo run --release
  2. Follow the instructions

Command-line

  1. Acquire a Game Boy bootrom, and put it to $HOME/.mooneye-gb/dmg_boot.bin
  2. cargo build --release
  3. cargo run --release -- PATH_TO_GAMEBOY_ROM

On Windows, also download an SDL2 package containing SDL2.dll, and put it to target/debug and target/release.

Game Boy keys

Game BoyKey
DpadArrow keys
AZ
BX
StartReturn
SelectBackspace

Other keys

FunctionKey
Fast forwardShift
Toggle performance overlayF2

Accuracy comparison

Versions used:

  • mooneye-gb (master)
  • BGB 1.5.2
  • Gambatte 2015-03-23 (f9fb003)
  • GiiBiiAdvance 2015-05-16 (dbf669a)
  • Higan v098 (in Game Boy mode, except for SGB/SGB2-specific test ROMs)
  • KiGB 2.05
  • MESS 0.163

Blargg's tests

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
cpu instrs:+1::+1::+1::+1::+1::x::+1:
dmg sound 2:x::+1::+1::x::x::x::x:
instr timing:+1::+1::+1::+1::+1::x::+1:
mem timing 2:+1::+1::+1::+1::+1::x::+1:
oam bug 2:x::x::x::x::x::x::x:

Mooneye GB acceptance tests

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
add sp e timing:+1::x::+1::x::+1::x::x:
boot hwio G:+1::+1::+1::x::+1::x::x:
boot regs dmg:+1::+1::+1::+1::+1::+1::+1:
call timing:+1::x::+1::x::+1::x::x:
call timing2:+1::x::+1::x::+1::x::x:
call cc_timing:+1::x::+1::x::+1::x::x:
call cc_timing2:+1::x::+1::x::+1::x::x:
di timing GS:+1::+1::+1::x::+1::+1::+1:
div timing:+1::+1::+1::+1::+1::x::+1:
ei timing:+1::+1::+1::+1::+1::+1::+1:
halt ime0 ei:+1::+1::+1::+1::+1::+1::+1:
halt ime0 nointr_timing:+1::+1::+1::+1::+1::x::x:
halt ime1 timing:+1::+1::+1::+1::+1::x::+1:
halt ime1 timing2 GS:+1::+1::+1::x::+1::x::+1:
if ie registers:+1::+1::+1::+1::+1::x::+1:
intr timing:+1::+1::+1::+1::+1::x::+1:
jp timing:+1::x::+1::x::+1::x::x:
jp cc timing:+1::x::+1::x::+1::x::x:
ld hl sp e timing:+1::x::+1::x::+1::x::x:
oam dma_restart:+1::x::+1::x::+1::x::x:
oam dma start:+1::x::+1::x::+1::x::x:
oam dma timing:+1::x::+1::x::+1::x::x:
pop timing:+1::x::+1::+1::+1::x::+1:
push timing:+1::x::x::x::+1::x::x:
rapid di ei:+1::+1::+1::+1::+1::+1::+1:
ret timing:+1::x::+1::x::+1::x::x:
ret cc timing:+1::x::+1::x::+1::x::x:
reti timing:+1::x::+1::x::+1::x::x:
reti intr timing:+1::+1::+1::+1::+1::+1::+1:
rst timing:+1::x::x::x::+1::x::x:

Bits (unusable bits in memory and registers)

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
mem oam:+1::+1::+1::+1::+1::+1::+1:
reg f:+1::+1::+1::+1::+1::+1::+1:
unused_hwio GS:+1::x::+1::x::x::x::x:

GPU

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
hblank ly scx timing GS:+1::x::x::x::x::x::x:
intr 1 2 timing GS:+1::+1::+1::x::+1::x::+1:
intr 2 0 timing:+1::+1::x::x::+1::x::x:
intr 2 mode0 timing:+1::+1::x::x::x::x::x:
intr 2 mode3 timing:+1::+1::x::x::x::x::x:
intr 2 oam ok timing:+1::+1::x::x::x::x::x:
intr 2 mode0 timing sprites:x::x::x::x::x::x::x:
stat irq blocking:x::x::+1::x::x::x::x:
vblank stat intr GS:+1::+1::x::+1::+1::x:  :x:

Timer

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
div write:x::+1::x::+1::+1::x::x:
rapid toggle:x::x::x::+1::x::x::+1:
tim00 div trigger:+1::x::+1::+1::x::x::x:
tim00:x::+1::x::+1::+1::x::x:
tim01 div trigger:x::+1::x::+1::x::x::x:
tim01 :+1::+1::+1::+1::+1::x::x:
tim10 div trigger:x: :+1::x::+1::x::x::+1:
tim10:x: :+1::x::+1::+1::x::x:
tim11 div trigger:+1::x::x::+1::x::x::x:
tim11:x: :+1::x::+1::+1::x::x:
tima reload:x::x::x::+1::x::x::x:
tima write reloading:x::x::x::+1::x::x::x:
tma write reloading:x::x::x::+1::x::x::x:

Mooneye GB emulator-only tests

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
mbc1 rom 4banks:+1::x::+1::+1::+1::x::+1:

Mooneye GB manual tests

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
sprite priority:+1::+1::+1::x::+1::x::x:

Mooneye GB misc tests

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
boot hwio C:+1::x::x::x:
boot hwio S:x: :x::+1::x::x:
boot regs A:x::x::x:
boot regs cgb:+1::x::x::+1:
boot regs mgb:+1::+1::+1:
boot regs sgb:x::+1::+1::x::+1:
boot regs sgb2:x::+1::x::x:

Bits

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
unused hwio C:x::x::x::x:

GPU

Testmooneye-gbBGBGambatteGiiBiiAdvanceHiganKiGBMESS
vblank stat intr C:x::x::x::x:

Test naming

Some tests are expected to pass only a single type of hardware:

  • dmg = Game Boy
  • mgb = Game Boy Pocket
  • sgb = Super Game Boy
  • sgb2 = Super Game Boy 2
  • cgb = Game Boy Color
  • agb = Game Boy Advance
  • ags = Game Boy Advance SP

In general, hardware can be divided on to a couple of groups based on their behaviour. Some tests are expected to pass on a single or multiple groups:

  • G = dmg+mgb
  • S = sgb+sgb2
  • C = cgb+agb+ags
  • A = agb+ags

For example, a test with GS in the name is expected to pass on dmg+mgb + sgb+sgb2.

License and copyright

Mooneye GB is licensed under GPLv3+. Copyright (C) 2014-2016 Joonas Javanainen joonas.javanainen@gmail.com