zboy-game

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.
- A Zig compiler with an SM83 target turns
game.ziginto LLVM IR. optremoves the globals that nothing uses.llc, from llvm-z80, turns the IR into assembly for SDCC.lcc, from GBDK-2020, assembles and linksgame.gb.
The build fetches the compilers, so a ROM needs only Zig on the host.
zig buildHow 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