The zboy-game logo, a sleeping Game Boy

zboy-game builds Game Boy ROMs from Zig source. The code is on Codeberg under the MIT licence.

A ROM in Zig

A ROM is one Zig file that imports the gb library and exports main. This one prints whether the A button is held and flips a toggle on each press.

const gb = @import("gb");

export fn main() void {
    gb.puts("Input test");
    gb.zgb_gotoxy(0, 2);
    gb.puts("A held: 0");
    gb.zgb_gotoxy(0, 4);
    gb.puts("Toggle: 0");

    var input: gb.Input = .{};
    var toggled: bool = false;

    while (true) {
        gb.vsync();

        const state = input.poll();
        if (state.just_pressed.a) {
            toggled = !toggled;
        }

        gb.zgb_gotoxy(8, 2);
        gb.zgb_setchar(if (state.held.a) '1' else '0');

        gb.zgb_gotoxy(8, 4);
        gb.zgb_setchar(if (toggled) '1' else '0');
    }
}

How a ROM is built

Zig has no target for the Game Boy CPU, the Sharp SM83, so the build goes through LLVM.

  1. A Zig compiler with an SM83 target turns game.zig into LLVM IR.
  2. opt removes the globals that nothing uses.
  3. llc, from llvm-z80, turns the IR into assembly for SDCC.
  4. lcc, from GBDK-2020, assembles and links game.gb.

The build fetches the compilers, so a ROM needs only Zig on the host.

zig build

How a ROM is tested

Tests run on the host. Each test loads a built ROM into the SameBoy core, reads what the ROM writes to the serial port, and checks it with std.testing.

zig build test
Table of Contents