# Plan for the Specification agent How to build the board from these documents, rung by rung. It refines [handoff-brief.md](handoff-brief.md) and keeps every owner's decision recorded there (own or free BIOS, ROMs in place from `$NO_INTRO_DIR`, a catch-up scheduler, gb-apu-python pinned, private repository, PyPy floor 3.11). Read [restart.md](restart.md) first: no GPL/LGPL/MPL/unlicensed source, the archive unread, every rule tagged. ## Before rung 0 1. Pin the cores by revision in `pyproject.toml` (git URLs, never local paths): arm7tdmi-python (head at Phase 0: `51bb589`) and gb-apu-python (head: `7523b48`; gameboy-python pins `f0a7f48`, the brief only requires "pinned by revision"). Use them through their documented interfaces only. 2. `python3 scripts/fetch_references.py --verify`; `python3 scripts/build_referees.py --nba`. 3. Every new file starts with `# SPDX-License-Identifier: MIT`; every rule has its tag and the document section it comes from (this folder's IDs, e.g. "U-TIM-3, FITTED to prefetcher_*"). ## Module layout (package `gba_python`) One clock domain (the charter's era 1 shape). The channels that later tiers may thread are named now: the sound unit takes register writes and time (`APU.run`, FIFO pushes); the picture processor takes register and video-memory writes and time. Nothing pokes another device's state except through the bus or a named method. | Module | Holds | Documents | | --- | --- | --- | | `board.py` | `GBA`: builds the CPU (`ARM7TDMI(read, write, idle)`), the bus and devices; `run_frame()`, `step()`; the scheduler (below); save/load state | README.md, timing.md "The clock" | | `bus.py` | region decode, access pricing (wait states, 16-bit splits, `gba_access`), the CPU open-bus latch, the BIOS latch, the cartridge's address latch, video-memory contention hook | memory-map.md, timing.md | | `prefetch.py` | the Game Pak prefetch buffer as its own small state machine | timing.md "The Game Pak prefetch buffer", U-TIM-3 | | `io.py` | the I/O register table: address to device and readable mask | memory-map.md "I/O map", mGBA suite io-read table | | `irq.py` | IE, IF, IME, the CPU IRQ line with its delay; HALTCNT, halt and stop; POSTFLG; `04000800` | timing.md "Interrupts", "HALT and STOP" | | `dma.py` | four channels, their latches, priority, start conditions, FIFO and capture modes | timing.md "DMA", memory-map.md "DMA and the bus latch" | | `timers.py` | four timers, the shared prescaler, count-up, overflow events to `irq` and `sound` | timing.md "Timers" | | `keypad.py`, `serial.py` | KEYINPUT/KEYCNT; SIO registers and internal-clock transfers without a partner | timing.md | | `ppu/timing.py` | VCOUNT, DISPSTAT, H/V-blank and V-count events, DMA triggers, the per-line latches (affine reference points, DISPCNT) | video.md "Timing", "Forced blank, display enables, latches" | | `ppu/line.py` | rung 3: one line from the registers (BGs, OBJs, windows, effects, mosaic) | video.md | | `ppu/dots.py` | rung 4: the dot-level fetch model and video-memory contention | video.md "CPU access to video memory", U-VID-11 | | `sound.py` | the APU glue (clock /4, sequencer, register map), FIFOs A/B, the mixer, SOUNDBIAS | sound.md | | `cartridge/rom.py` | the ROM image (in place from a path or a No-Intro zip), header, GPIO window | cartridge.md | | `cartridge/backup.py` | SRAM/FRAM, Flash (IDs, commands, banks), EEPROM (bit-serial protocol) | cartridge.md | | `cartridge/rtc.py` | the S-3511 behind GPIO | cartridge.md | | `cartridge/detect.py` | save type from the ID strings, with an override | cartridge.md "Choosing the save device" | | `bios.py` | the BIOS image: `--bios` path or zip, default Cult-of-GBA's `bios.bin` | handoff-brief.md | | `__main__.py` | headless runs (frames, keys, PNG, registers), later the window | validation.md | Keep each hot class under 80 attributes (PyPy). Tests live in `tests/`; the judge scripts in `scripts/` (judge_text.py exists; add a suite runner and a hw-test reader for the board). ## The scheduler and the order of record (decide at rung 0, write it down) Catch-up (owner's decision): the board keeps one cycle counter. Every CPU callback is priced before it completes; devices that could see or change something at that time are brought up to it first (timers, the PPU's events, the APU when a sound register is touched, DMA requests). Proposed order of record for one CPU access at time t (HYPOTHESIS, to be confirmed by rung 1's tests): (1) run pending device events due before t, (2) let a requesting DMA take the bus if its start delay has passed (the CPU stalls at its next memory access but keeps running internal cycles: alyosha-tas Interactions), (3) perform the access and advance by its cost, (4) update the IRQ line. Record each choice in `docs/timing.md` with the test that fixed it. ## Rungs (each passes before the next) | Rung | Build | Judge (calibration, [validation.md](validation.md)) | Detectors today (NBA retail / mGBA retail) | | --- | --- | --- | --- | | 0 | bus without picture: memory map and mirrors, I/O table, BIOS loading and protection, open bus, IRQ controller, timers (count only), DMA (function), keypad, a minimal frame loop for `VCOUNT`/V-blank | jsmolka arm, thumb, memory, bios, nes, save (`judge_text.py`); mGBA suite memory, I/O read, DMA, BIOS math, shifter, carry, multiply long; krom BIOS (by eye, 25); ARMWrestler | jsmolka 9/9, 5/9; suite: memory 1,514 / 1,552, rest full | | 1 | cycle timing: wait states, 16-bit splits, the 128 KB rule, the ROM address latch, the prefetch buffer, DMA timing and latches, IRQ delay, HALT, timer start/stop/reload edges, serial timing | alyosha-tas Bus, DMA, Interactions, irq, prefetcher, timer, timing, psr, Serial; hw-test bus, dma, haltcnt, irq, timer; mGBA suite timing, timer count-up, timer IRQ; retAddr; Hades-Tests (run only) | see validation.md; NBA misses 8 of 18 prefetcher ROMs, all 8 LDM | | 2 | backup devices and the RTC | jsmolka save (4); Backup LIB sample; Pokémon Emerald accepting the clock | 4/4, 2/4 | | 3 | the picture a line at a time | hw-test ppu photographs (bgpd, bgx, dispcnt-latch, greenswap), destoer pictures, alyosha-tas/jsmolka ppu picture ROMs, NBA frames (detector) | NBA matches 3 photographs | | 4 | the picture dot by dot; CPU/DMA waits on video memory | alyosha-tas ppu (text), destoer lyc_midline (photo), window_midframe, hw-test status-irq-dma and ram-access-timing (by eye) | NBA 3/6 alyosha ppu, lyc_midline matches | | 5 | sound: FIFOs, mixer, SOUNDBIAS around the APU | alyosha-tas fifo_dma (9); aging cartridges' sound tests; NBA levels (detector) | 3/9 | | 6 | Nintendo's aging cartridges pass their automatic tests and keep settings in EEPROM across a power cycle | AGS v7.0-v10.0, AGB; AGBEEG | not scored here | | 7 | games and a window | tests/smoke_set.txt (39), tests/games_set.txt (57) | not scored here | At each rung: seed one-line faults in every rule the rung adds and show a judge catches each (charter); record results in `docs/status.md` and `docs/validation.md` with the commit; compare with the targets in handoff-brief.md and report a shortfall with what was found, never by fitting to a detector. ## Order inside rung 1 (the hard part) 1. Plain wait states and splits: mGBA suite timing in IWRAM/EWRAM modes, hw-test `dma/start-delay`. 2. The 128 KB rule and the ROM latch: hw-test `bus/128kb-boundary`, `dma/burst-into-tears`, alyosha-tas `DMA/DMA_ROM_Fixed`. 3. Timers' edges: hw-test `timer/*`, alyosha-tas `timer/*`, mGBA timer tables. 4. IRQ delay and HALT: hw-test `irq-delay`, `haltcnt`; alyosha-tas `irq/*`. 5. DMA against the CPU: alyosha-tas `DMA/*`, `Interactions/*`, hw-test `force-nseq-access`. 6. The prefetch buffer last, with the readme facts first and every remaining choice `FITTED`: alyosha-tas `prefetcher/*`, mGBA timing in the prefetch modes, `timing/prefetch_enable*`. ## What the agent must not do - Read any source restart.md rule 1 lists, the archive, or the cores' undocumented-behaviour, timing, validation or handoff documents. - Copy `gba_access`; patch a core (report to the owner instead). - Fix a difference from a detector without a document or a hardware test saying the board is wrong (restart.md rule 6). - Read r12 as a verdict for the library ROMs (use the screen).