← Writing EN / ES

Building a Game Boy Emulator in Rust + WebAssembly


Building an emulator is one of those projects that sounds impossible until you actually do it. You’re essentially writing a software replica of physical hardware — every clock cycle, every memory read, every pixel drawn on screen. I built Gameboy4me over several months, and this is what I learned.

Why Rust + WebAssembly?

The Game Boy’s CPU (the LR35902) runs at 4.194304 MHz. That’s over 4 million operations per second that need to be emulated accurately, in a browser, without dropping frames. JavaScript alone wasn’t going to cut it.

Rust compiles to WebAssembly, which runs near-native speed in the browser. More importantly, Rust’s ownership model meant I could reason about memory with zero garbage collector pauses — important when you’re rendering at 59.73 fps.

pub struct Cpu {
    pub a: u8,
    pub b: u8,
    pub c: u8,
    pub d: u8,
    pub e: u8,
    pub h: u8,
    pub l: u8,
    pub f: Flags,
    pub sp: u16,
    pub pc: u16,
}

The hardest parts

1. Timing is everything

The Game Boy doesn’t just execute instructions sequentially — everything is tied to the master clock. The PPU (pixel processing unit), the APU (audio), the timer registers — they all tick in sync. Get this wrong and games freeze, glitch, or crash.

The key insight: every instruction has a known cycle cost. After executing each instruction, you advance all subsystems by that many cycles.

let cycles = self.execute_instruction();
self.ppu.step(cycles);
self.apu.step(cycles);
self.timer.step(cycles);

2. The PPU is a state machine

The Game Boy screen is 160×144 pixels, drawn line by line (scanline rendering). Each scanline has four modes: OAM scan, pixel transfer, horizontal blank, and vertical blank. Miss a mode transition at the wrong cycle and you’ll see graphical glitches in games that rely on raster effects.

3. The HALT bug

The original Game Boy has a documented hardware bug: if you execute HALT with interrupts disabled and there’s a pending interrupt, the next instruction executes twice. Several games depend on this behaviour. Emulating bugs is harder than emulating correct behaviour.

Game Boy Color support

GBC added a second CPU speed mode (double speed), 32KB extra RAM, an extended palette of 32,768 colours, and compatibility detection via register A at boot. Supporting both DMG and CGB in the same codebase required careful flag management throughout.

The frontend

The Rust core compiles to a .wasm binary that the Next.js frontend loads via WebAssembly.instantiate. Each frame, the WASM module writes pixel data to a shared Uint8Array and the frontend renders it to a <canvas>.

Audio was the trickiest frontend integration. The Web Audio API works with sample rates (44100 Hz), but the APU produces samples at ~4194304 Hz. You need a downsampling buffer and careful synchronisation to avoid crackling.

What I’d do differently

I’d use a cycle-accurate test ROM suite from day one. The Blargg test ROMs test every corner of the CPU. Running them early would have saved me days of debugging subtle timing issues I discovered much later.

I’d separate the core from the WASM bindings earlier. I initially mixed emulator logic with WASM-specific code. Refactoring this later to make the core testable in pure Rust was painful.

Try it

You can play any .gb or .gbc ROM directly in your browser at gameboy4me.vercel.app. No downloads, no installs — your ROMs never leave your device.

Source code is on GitHub.


References & resources

These are the sources and guides that made this project possible. If you want to build your own emulator, start here.

← All posts