Log inGet started
World

Save Point

Branchmain
Branchesmain
Commit8937fc8d08

README.md

gb — Game Boy Color emulator core

A Game Boy / Game Boy Color emulator implemented in Luau. Loads actual .gb / .gbc cartridge ROMs and runs them: LR35902 CPU interpreter, CGB PPU rendered into a 160×144 RGBA framebuffer, MBC1/MBC3/MBC5 memory-bank controllers with battery RAM, timers, joypad, and a 4-voice APU.

Passes blargg's cpu_instrs (11/11) and instr_timing hardware-accuracy test ROMs. Boots and plays real CGB cartridges (validated with µCity, a CGB-only MBC5 cart).

require the module and assemble a machine from ROM bytes:

local Gb = require("gb")
local gb = Gb.new(romBytes)        -- romBytes: binary string of the cartridge
gb:runFrame()                      -- advance one video frame (~70224 dots)
gb.ppu.fb                          -- 160*144*4 RGBA byte buffer, row-major
gb:setInput(dpad, btns)            -- active-high masks (see joypad.luau)
gb:saveRam() / gb:loadRam(bytes)   -- battery-backed cart RAM
local state = gb:snapshot()        -- whole-machine save state (binary string)
gb:restore(state)                  -- into a Gb.new(sameRomBytes) machine
gb.apu:voices()                    -- live tone descriptors for an output stage
gb:serialText()                    -- captured serial output (test ROMs report here)

Submodules: bus.luau (address space + CGB banking), mbc.luau (cartridge mappers), cpu.luau + cpu_cb.luau (LR35902), timers.luau, ppu.luau (CGB scanline renderer with a decoded-tile cache), joypad.luau, apu.luau, display.luau (framebuffer → runtime texture), snapshot.luau (save states).

Save states

gb:snapshot() serializes the complete machine between frames into one compact binary string (~10–50 KB; large buffers are run-length encoded): CPU registers, IME and its EI delay, HALT / halt-bug state; WRAM (all 8 CGB banks), HRAM, VRAM (both banks), OAM, IO registers, IE/IF, VBK/SVBK and KEY1 double-speed state; mapper registers (ROM/RAM bank, mode, RAM enable, MBC3 RTC registers) and cartridge RAM; PPU mode, LY, dot counter, window line, LCDC/STAT and scroll/window registers, DMG palettes, CGB BG/OBJ palette RAM with BCPS/OCPS, HDMA source/destination/length/active, and the current framebuffer; DIV counter, TIMA/TMA/TAC and the TIMA accumulator; joypad select latch and held buttons; APU channels, frame sequencer, NR50/51 and wave RAM; frame count and serial log.

gb:restore(state) puts it back into a machine built from the same ROM; the next runFrame() is bit-identical to what the original would have produced. The state starts with GBSS, a version byte and the cartridge identity (title, header and global checksum, ROM size, CGB flag): restoring into a different cartridge or a newer format raises a clear error. Snapshot and restore each take well under a few milliseconds. Restoring marks cart RAM dirty so a host persists the battery save. /zero/source/gbtest/snapshot.luau is the determinism test (run N frames, snapshot, compare 120 continued frames).

The GameBoy component hosts a machine on an entity: it loads romPath, steps frames in play mode, blits to the gb_screen_mat screen texture, maps keyboard input, voices the APU through the engine's MIDI synth, and persists battery saves in runtime_data.world under gb_saves/<title>.

Test scripts live in /zero/source/gbtest/ — run with luau <path>; bench_cpu.luau measures interpreter throughput, blargg.luau runs the accuracy ROMs.