# Handoff: build `gba-python`, the Game Boy Advance board ## Context you are inheriting The family's shape is **chips are cores, boards compose cores**. This board composes `arm7tdmi-python` (`/data/emu/arm7tdmi-python`, prepared in parallel with this repository) and, from rung 5, the Game Boy sound channels extracted from `gameboy-python`. Everything else on the AGB CPU chip and the cartridge is this repository's. A corner of a core found here is raised **on the core** (an issue or PR there; re-certification is fine), never worked around in the board. Models for the code and the tooling: - `gameboy-python` (`/data/emu/gameboy-python`): the closest board: a pin-shaped APU with an import-boundary test, the cartridge package behind a connector, `testrom.py` reading result protocols, pass lists per suite, real boot ROMs read in place. - `famicom-python` and `megadrive-python`: milestone acceptance against hardware-captured suites, detectors driven by scripts, seeded-fault checks. - `z80-python`: the readability contract and the tooling bar (`state.py`, `trace.py`, `debug.py`, `console.py`, `python -m`). The user's rules that bind this work: - **Oracle tiers**: hardware-captured > hardware-corrected > emulator-derived; a low-tier oracle is a detector, never a judge; state the tier of every claim ([validation.md](validation.md)). - **Python floor 3.11** (the newest PyPy supports): `requires-python = ">=3.11"`, ruff `target-version = "py311"`, CPython and PyPy both green. - **Reference boards are correct, not fast.** Sub-real-time is expected; speed is a later `-fast` fork's job. - **Modern over compat**: callables, dataclasses, no shims. - **Never copy a chip file between repositories; extract on the second consumer.** This board is the second consumer of the Game Boy APU. - **No dangerous-looking shell one-liners** in scripts or docs (no `rm -rf`, `sed -i`); keep CI minimal (PyPy only, main + PRs, cancel in progress) if and when a GitHub repository exists. **Local git only** until rung 1 passes ("Decided by the user" 8). - The BIOS is **bring-your-own**: never fetched, never committed. ### Read, in this order, before writing code 1. `README.md`. 2. [start-here.md](start-here.md): the machine, buses and wait states, the prefetch buffer, DMA, timers, IRQ/HALT/STOP, keypad, serial, the BIOS and open bus, the PPU, the sound unit, the suggested module shape. 3. [timing.md](timing.md): callbacks to clocks, the prefetch buffer's rules, DMA, IRQ latency, timers, the PPU's line, sound clocks. 4. [undocumented-behavior.md](undocumented-behavior.md): quirks 1–22, contradictions C1–C11, open items. 5. [cartridge.md](cartridge.md): ROM, header, save chips, GPIO, the census. 6. [validation.md](validation.md): every oracle with tier and pin; the measured detector results; the rungs. 7. [mame-oracle.md](mame-oracle.md). 8. `/data/emu/arm7tdmi-python/docs/handoff-brief.md` ("The bus contract") and `/data/emu/gameboy-python/docs/handoff-brief.md`. Then `python scripts/fetch_references.py` (23 repositories and 7 files, about 410 MB) and `python scripts/build_referees.py --nba` (Lua, mGBA's core and driver, NanoBoyAdvance 1.8.3's core and driver: a few minutes, needs `cmake`, `ninja`, a C/C++ compiler, libpng, zlib). `python scripts/referee_run.py r12 --emu nba --bios ` reproduces the numbers in validation.md. ## Decisions already made (2026-10-21, when this repository was started) - **Name `gba-python`**, package `gba_python`. Reference machine: AGB-001 (GBA; the SP and the Game Boy Player share the chip). The NDS/3DS GBA modes are not references (they fail `hw-test/ppu/vram-mirror`). - **The BIOS is bring-your-own.** Never fetched or committed. The pinned free replacement is **Cult-of-GBA** (MIT, prebuilt). Judge runs need the retail BIOS (C3); the retail dump is the default (user decision 1). - **Game Boy (CGB) mode is out of scope** for the first build. It would embed `gameboy-python` with an AGB row and `sm83-python`; recorded as later work. - **The Game Boy sound channels are not copied.** They were extracted from `gameboy-python/src/gameboy_python/apu/` with their history into their own core, `gb-apu-python` (user decision 4; done 2026-11-06), which gameboy-python pins too; the GBA's differences are its AGB row (2026-11-11) and this board keeps the FIFOs, mixer and SOUNDBIAS. - **No link partner** in the first build: serial registers, internal-clock timing and IRQ only. - **The CPU is pinned by revision**, never a local path, once arm7tdmi-python passes its rung 3; develop against `pip install -e ../arm7tdmi-python`. ## What the board needs from arm7tdmi-python, and how it uses it The core's contract (its `docs/handoff-brief.md`, 2026-10-21): ```python cpu = ARM7TDMI(read, write, idle) # read(address, width, access) -> int width 8/16/32; access: SEQ=1, CODE=2, LOCK=8 # write(address, width, value, access) # idle(address) one I cycle # cpu.irq / cpu.fiq: levels the board drives; sampled at the start of each step() # cpu.cpsr, cpu.thumb, cpu.privileged readable inside callbacks cycles = cpu.step() # == callbacks made access = gba_access(access, after_idle) # the GBA's S-after-I rule, from the core ``` | Board need | How the contract meets it | | --- | --- | | Wait states per region, width and N/S | each callback carries address, width and `SEQ`; the board computes the clocks and advances every device **inside** the callback ([timing.md](timing.md)) | | S after I is N on the GBA | the core reports the TRM's S; the board passes every access through **`arm7tdmi_python.gba_access(access, after_idle)`** (arm7tdmi-python `17fd0c8`), which clears `SEQ` after an I cycle. The rule is written once, in the core, and shared with its rung-1 comparison: **never re-implement it here**. The core's decision 1, decided by the user | | Prefetch buffer | `CODE` marks opcode fetches; `idle()` marks the cycles the buffer runs ahead; fetch width says ARM or Thumb | | Open bus and the BIOS latch | the board served every `CODE` read, so it knows the CPU's two pipeline opcodes and whether execution is in the BIOS (the latest opcode fetch's address); `cpu.thumb` for the Thumb shapes | | Unaligned accesses | the core passes them unaligned and rotates what it gets; the board aligns | | DMA overlapping I cycles | the board runs DMA inside `idle()` and lets following `idle()`s cost nothing until the next `read`/`write` (NBA's `Bus::Idle`) | | SWP never split by DMA | `LOCK` | | IRQ latency | the board delays `cpu.irq` itself (3 cycles, [timing.md](timing.md)); the core has no synchronizer | | HALT/STOP | the board stops calling `step()`; the core has no halted state. HALTCNT is written by the last (write) cycle of an STRB, so halting between steps is exact | | Reset and boot | `cpu.reset()` then `step()` from `0`: the BIOS runs as code. A skip-BIOS start (user decision 2) sets registers through `cpu.state` | **No conflicts with the core's contract were found.** Two things to keep in view: (1) the core samples `irq` only at instruction boundaries, so the board's IRQ delay must be counted in cycles inside callbacks, never by `step()`'s return value; (2) for Thumb code in the BIOS or OAM at a non-word-aligned address, GBATEK's open-bus value includes `[$+6]`, which no fetch has read yet (NBA's TODO says the same); the board cannot know it without the core, and no test is known to need it, so it is an open item, not a contract change. ## Your task Build `src/gba_python/` and pass the rungs in [validation.md](validation.md) "The plan", in order: 0. **Bus without a picture** (jsmolka CPU/memory/BIOS ROMs; mGBA suite non-timing groups at NBA's retail score or better). 1. **Cycle timing of the bus** (mGBA suite timing 2,020, count-up 936, timer IRQ 90; `hw-test` timer/dma/irq/haltcnt/bus; Hades-Tests; alyosha-tas timing groups). 2. **Saves and the RTC** (jsmolka `save/*`, AGS backup, a Pokémon clock). 3. **PPU, line-exact.** 4. **PPU, dot-exact.** 5. **Sound** (after the APU extraction). 6. **AGS aging cartridge, every group.** 7. **Games and a front end.** Every rung adds its ROMs to a pass list (as gameboy-python does) and a seeded-fault check. ### Tooling at the family bar `state.py` (board + CPU state, save/load), `testrom.py` (r12, mGBA debug-port END lines and `savprintf` SRAM text, screen capture to PNG), `trace.py` (JSONL with every bus callback, its clocks, and DMA cycles), `debug.py`, `console.py`, `python -m gba_python ROM.gba [--bios PATH] [--frames N] [--png out.png]`. A lockstep script against NanoBoyAdvance (`scripts/nba_driver/` already exposes registers per frame; extend it to a per-instruction trace when a rung needs one). ## Decided by the user (2026-10-21) The user accepted every recommendation. Build to these; raise new evidence against one as an issue rather than changing it silently. 1. **Default BIOS: the retail dump, read in place** from the documented No-Intro path (`--bios`, `GBA_BIOS`, `GBA_NOINTRO_DIR`). Cult-of-GBA only when no dump is found, labelled in every result; mGBA-style HLE never. Judge results quote the retail BIOS only (C3). 2. **Skip-BIOS start** is offered for development and the CPU-only ROMs, never for judge runs of timing tests (the hand-over phase is observable, C3). 3. **Scheduler: cycle-stamped events**, each device catching up to "now" before any access to it, driven from the bus callbacks. No per-cycle ticking of every device. 4. **The extracted Game Boy APU core is `gb-apu-python`** (`gb_apu_python`), with model rows DMG, CGB and AGB, extracted from gameboy-python with its history (stash first: filter-repo resets a dirty tree) before rung 5. 5. **No PPU beyond line timing until rung 3**: every rung 0–2 judge reads registers, the debug port or SRAM, not the screen. 6. **Boot-phase-sensitive alyosha-tas tests (C4)** are listed (those whose result changes between BIOSes on NBA), marked "GBP-calibrated" in the pass list, and the board is not fitted to them. 7. **Game Boy (CGB) mode and the link cable** stay out of the first build. 8. **GitHub:** local until rung 1 passes, then a **private** repository (boards stay private), PyPy-only CI on main and PRs. 9. **The core's decision 1 (merged IS)** is answered once for both repositories: the core reports the TRM's S, and the board converts through `arm7tdmi_python.gba_access`.