# gba-python A readable, pure-Python emulator of **Nintendo's Game Boy Advance**, to be built as a board on [arm7tdmi-python](../arm7tdmi-python): the core owns the ARM7TDMI's instruction sets and its cycle-by-cycle bus; this repository will own everything around it: the memory map and its wait states, the Game Pak prefetch buffer, four DMA channels, four timers, the interrupt controller, HALT and STOP, the keypad and serial port, the BIOS's read protection and open bus, the picture processor, the sound unit, and the cartridge's save chips and clock. **Status (2026-11-12): rung 0 passes; rungs 1, 3, 4, 5 and 7 in progress.** The board runs the retail BIOS (read in place) and every CPU, memory, I/O, timer, IRQ and DMA judge of rung 0; of rung 1 it passes the mGBA suite's timing groups, all of NanoBoyAdvance's non-PPU `hw-test` but one case, all 16 of alyosha-tas's IRQ-and-DMA interaction ROMs (NBA: 7) and 17 of its 18 prefetcher ROMs (NBA: 10). It draws the picture dot-exact where a line changes while it is drawn (rung 4) and a line at a time elsewhere, with the same result: every BG mode, OBJs, windows, blending and mosaic; mid-line register writes; CPU and DMA accesses to VRAM, palette and OAM waiting for the PPU's fetches. 21 judge and game frames (games to 1,200 frames) match NanoBoyAdvance pixel for pixel, including the green swap photograph; alyosha-tas's OBJ VRAM timing tests pass (one needed a hardware correction to NanoBoyAdvance); a 40,000,000-instruction lockstep with NanoBoyAdvance agrees on two games. Nintendo's five local aging cartridges (AGS v7.0-v10.0 and the older AGB) pass their automatic tests, LCD included, keep their settings in their EEPROM across a power cycle, and pass rounds of their aging loop. ARMWrestler (79/79 rows), FuzzARM (5 × 10,000), krom's 25 BIOS-call tests (results and Timer 0 cycle counts) and Nintendo's backup library sample on SRAM, Flash and EEPROM pass too. It has sound (rung 5): the Game Boy's four channels from the `gb-apu-python` core, the two DMA sound FIFOs, the mixer and SOUNDBIAS; over eight games its level is within about 1 % of NanoBoyAdvance's and the loudness curves correlate at 0.995 or better, and all 9 of alyosha-tas's `fifo_dma` ROMs pass (mGBA and NanoBoyAdvance: 6). Of 57 more games run for a minute or more with scripted keys, 56 reach the game itself and one stops at a timing test its script cannot pass; none crashes or draws a broken picture, and 43 end on NanoBoyAdvance's exact picture (rung 7). `python -m gba_python play ROM` opens a window with sound, keys, a gamepad and saves, at about a sixth of real time on PyPy. Results, the places hardware tests overruled NanoBoyAdvance's model, and the seeded faults are in [docs/validation.md](docs/validation.md) "The board's results". ```sh python -m venv .venv && .venv/bin/pip install -e '.[dev]' # PyPy: the same with pypy3 python -m gba_python ROM.gba [--bios PATH] [--frames N] [--png out.png] [--wav out.wav] [--trace bus.jsonl] python -m gba_python ROM.gba --debug [-c COMMAND]... [--batch] # the command debugger pip install -e '.[frontend]' && python -m gba_python play ROM.zip # a window: picture, sound, keys, pad, saves python scripts/run_test_roms.py jsmolka alyosha hwtest hades mgba frames ags aging --jobs 16 python scripts/build_test_roms.py # keyirq, against scripts/libgba_shim python scripts/run_test_roms.py armwrestler fuzzarm krom retaddr keyirq backuplib python scripts/frame_diff.py --judges # pictures vs NanoBoyAdvance python scripts/sound_diff.py --games # sound vs NanoBoyAdvance python scripts/smoke.py --quick # 11 commercial ROMs, ~3 min: a regression detector python scripts/smoke.py --set tests/games_set.txt --nba # the 57-game list (long) pytest -m "not slow" # quick; -m slow: the ROM judges ``` ## The console `python -m gba_python ROM --debug` (with `--skip-bios`, `--load-state` as for a run) opens arm7tdmi-python's command debugger over the whole board (`gba_python.console`): the core's commands (registers and banks, stepping and step-over, breakpoints, bus watchpoints typed N/S/I/C, listings, memory) plus the board's, added through the core's `commands` hook: | Command | Does | | --- | --- | | `io [NAME\|ADDRESS ...]` | I/O registers by GBATEK name; write-only ones show what was written (`w`); no names: a summary with every DMA channel and timer | | `video` / `v` | frame, VCOUNT, line cycle and dot, the DISPSTAT flags, mode and layers | | `break irq\|dma\|halt\|self\|line N` | stop at an IRQ entry, after a step in which a DMA took the bus, after the instruction that halted the CPU, at a branch to itself, at the start of line N (`delete` removes them; `break ADDRESS` as before) | | `frame [N]` / `f` | run until N more frames end; breakpoints still stop it | | `png FILE`, `layer 0-3 FILE` | the last finished frame, or one background's whole plane (text map, affine map or bitmap page) as a PNG | | `state save\|load FILE` | the whole console (`gba_python.state`); breakpoints carry over a load | A step is one CPU boundary, counted in system clocks with wait states and DMA; the instruction that halts the CPU (the BIOS's write to HALTCNT) runs the HALT until the wake-up. The memory view and listings read the board through a peek with no side effects. Mario Kart: Super Circuit, abridged (registers left out): ```text gba> frame 120 stopped: frame 120 ended: 7515933 steps, 33515545 cycles gba> break halt gba> c stopped: the last step halted the CPU until an interrupt: 79 steps, 110879 cycles gba> cycles #7516011 00000344 STRB R3, [R12, #0x301] 110613 cycles -> 00000348 S read 0000034C 32-bit 0AFFFFFC code N write 04000301 8-bit 00 gba> delete halt gba> break irq gba> c stopped: IRQ entry (IE 2005 IF 0003): 1 steps, 3 cycles gba> delete irq gba> break dma gba> c stopped: a DMA transfer ran in the last step: 67 steps, 802 cycles gba> io DMA1CNT_H DMA1SAD TM0CNT_H DMA1CNT_H=B600 DMA1SAD=03005FF0w TM0CNT_H=0080 gba> video frame 120 VCOUNT 160 line cycle 812 (dot 203) cycles 33,627,246 mode 0 layers BG2 BG3 OBJ flags VBlank ``` ## Scope | Machine | Phase | | --- | --- | | AGB-001 (GBA), the reference; AGS (SP) and the Game Boy Player share the chip | 1 | | Game Boy / Game Boy Color cartridges in the GBA (SM83 mode) | later: embeds gameboy-python and sm83-python | | Link-cable partners | later | The Game Boy's four sound channels are the `gb-apu-python` core's AGB row, pinned by revision: this board was their second consumer, so they were **extracted with their history into their own core** from `gameboy-python` (user decision 4), never copied. ## What is fetched Nothing third-party is committed. `scripts/fetch_references.py` rebuilds `reference/` and `tests/test_roms/` from `scripts/pinned_files.json` (GBATEK, CowBite, the prebuilt mGBA suite, AGBEEG, the Arm GNU toolchain 14.3, Lua 5.4.7; by SHA-256) and `scripts/pinned_repos.json` (23 repositories at full commit SHAs: gbadoc, Tonc, the test suites of jsmolka, alyosha-tas, NanoBoyAdvance, mGBA, destoer and Hades, ARMWrestler, the free BIOSes, NanoBoyAdvance 1.8.3, mGBA, SkyEmu, Hades, ares, the MiSTer core, MAME's driver): ```sh python scripts/fetch_references.py # everything missing python scripts/fetch_references.py --verify # check what is present python scripts/build_referees.py --nba # mGBA and NanoBoyAdvance drivers ``` It needs `git` and `curl`; the build needs `cmake`, `ninja`, a C/C++ compiler, libpng and zlib. **The BIOS is bring-your-own**: Nintendo's is never fetched or committed; the scripts read your dump in place (`--bios`, a `.bin` or a No-Intro `.zip`). Cult-of-GBA's free BIOS (MIT) is pinned for runs without one; judge runs need the retail BIOS ([docs/start-here.md](docs/start-here.md) "The BIOS"). ## Scripts | Script | What | | --- | --- | | `scripts/fetch_references.py` | the pinned references, tools and test ROMs | | `scripts/build_referees.py` | Lua, mGBA's core + `mgba_driver`, NanoBoyAdvance 1.8.3's core + `nba_driver` | | `scripts/mgba_driver.c`, `scripts/nba_driver/` | one command line for both detectors: run N frames with held keys, print the debug-port log, registers, I/O words, save the frame as PNG | | `scripts/referee_run.py` | scores jsmolka/alyosha-tas ROMs (r12), the mGBA suite (END lines) and screenshots on either detector ([docs/validation.md](docs/validation.md)) | | `scripts/mame_run.py` | MAME 0.285's `gba` driver with your BIOS: a frame and the registers ([docs/mame-oracle.md](docs/mame-oracle.md)) | | `scripts/count_saves.py` | save-chip and RTC census over the local No-Intro set, read in place ([docs/cartridge.md](docs/cartridge.md)) | | `scripts/run_test_roms.py` | the board's judges: jsmolka, alyosha-tas, hw-test, Hades-Tests, the mGBA suite, Nintendo's aging cartridges (`ags`, and `aging`: settings across a power cycle and the aging loop), ARMWrestler, FuzzARM, krom's BIOS tests, GBA-Test-Collection's `retAddr`, mGBA's `keyirq`, Nintendo's Backup LIB sample; `--write` updates the pass lists | | `scripts/build_test_roms.py` | builds pinned test programs written for devkitARM's libgba (mGBA's `keyirq`) with the pinned Arm toolchain, against `scripts/libgba_shim/` | | `scripts/seeded_faults.py` | one fault per rule in a copy of `src/`, each run against its judge | | `scripts/smoke.py` | the smoke set (`tests/smoke_set.txt`, 39 commercial ROMs read in place): picture, state and sound digests against `tests/smoke_baseline.txt`, in parallel; `--quick`, `--record`, `--nba`, `--dump` ([docs/validation.md](docs/validation.md) "Smoke set") | | `scripts/nba_lockstep.py` | instruction-by-instruction comparison with NanoBoyAdvance (`nba_trace`): cycles, R15 and optionally R0-R14, streaming any length of run, with switches to look past the board's known differences (docs/validation.md) | ## Licence MIT (see `LICENSE`), for what is in this repository. The fetched material keeps its own terms, recorded per entry in the pin lists.