# Handoff: build `snes-python`, the Super Nintendo board ## Context you are inheriting The family's shape is **chips are cores, boards compose cores; never copy a chip file between repositories; extract on the second consumer**. Each core is certified on its own against its oracles and pinned by the boards that use it; each board is validated against hardware-tier test programs, with MAME and other emulators as detectors only. `famicom-python` (NES, on 6502-python), `gameboy-python` (on sm83-python), `sms-python` and `megadrive-python` are the boards to model this one on. This board embeds two CPU cores whose Phase 0 ran in parallel with this one: - **`65816-python`** (`/data/emu/65816-python`): the WDC 65C816 only. The S-CPU's extras (DMA, timer, mul/div, joypad, the access-speed decoder) are this board's. - **`spc700-python`** (`/data/emu/spc700-python`): the SPC700 only. The rest of the S-SMP die (IPL ROM overlay and the `$F0-$FF` I/O page) is this board's thin `smp` module, which wraps spc700-python's `SPC700` class (package `spc700_python`). and holds the **S-DSP as an in-board `dsp` module** (decided below), whose research is in `docs/dsp/` (start with `docs/dsp/README.md` and its own `handoff-brief.md`). Read, in order, before writing code: 1. `README.md`: scope, what is fetched. 2. `docs/start-here.md`: the machines as data, clocks, the CPU-side map and registers, DMA, the PPU's programming model, access rules and output, the APU split, the module shape. 3. `docs/timing.md`: the cycle-callback design with address-dependent cycle length, the line and dot, refresh, DMA/HDMA, NMI/IRQ, mul/div, auto-joypad, the two clock domains. 4. `docs/cartridge.md`: the connector protocol, maps, header detection, measured coverage, coprocessors in order, SGB, saves, BYOR. 5. `docs/undocumented-behavior.md`: quirks Q1-Q22, revisions, contradictions C1-C7, open items U1-U8. 6. `docs/validation.md`: every oracle with its tier and pin, what ran here (MAME, Mesen2, the two against each other). 7. `docs/mame-oracle.md`: the verified MAME 0.285 recipe and its traps. 8. `docs/dsp/` (after the fold): the S-DSP module, its interface and rungs. 9. 65816-python's and spc700-python's `docs/handoff-brief.md` ("bus contract"): the contracts you embed. Then `python scripts/fetch_references.py` (or `--verify`): 74 pinned files and 16 pinned repositories. ## Decided by the user - **Name `snes-python`** (Aubrey, 2026-10-21), not `superfamicom-python`. - **The S-SMP's I/O page and IPL overlay are a thin `smp` module here** (Aubrey, 2026-10-21): `$F0-$FF` (TEST, CONTROL, DSP address/data, ports, AUXIO, timers 0-2) and the IPL ROM overlay, wrapping spc700-python's `SPC700` class (package `spc700_python`). The core calls `idle()` with no arguments on internal cycles; the board reads `cpu.opcode`/`cpu.cycle` to price wait states. - **ARAM is a 64 KiB `bytearray` shared by the SMP and the DSP**; the DSP's power-on registers are a constructor argument (the decision said "defaulting to snes_spc 0.9.0's table"; since the restart of 2026-11-19 no table is taken from LGPL code, so the default is a documented or hardware value, marked HYPOTHESIS until a console probe reads it; docs/restart.md); the DSP-only replay host / `.spc` player lives in the board's `tests/` and `scripts/` (`python -m snes_python.dsp`) (Aubrey, 2026-10-21). - **Game `.spc` files and ROMs are never committed** (Aubrey, 2026-10-21). - **The S-DSP stays inside snes-python** (Aubrey, 2026-10-21): it is SNES-only, so there is no separate core repository; chips are broken out only when another emulator could use them. It is an in-board `dsp` package behind a pin-shaped interface with an import-boundary test, as gameboy-python keeps its APU. Its research, staged in `/data/emu/s-dsp-python`, was folded into `docs/dsp/` here on 2026-10-21. ## Decisions proposed by this Phase 0 (see "Open decisions" for the ones that need Aubrey) - **NTSC first**, reference console SNS-001 with **CPU version 2** (5A22-02), PPU1 v1, PPU2 v3; PAL, CPU version 1 and 1CHIP are rows of the machine table from day one (start-here.md "The machines"). - **Python floor 3.11** (the newest PyPy supports), CPython and PyPy; `requires-python = ">=3.11"`, ruff `target-version = "py311"`, ruff pinned. - **Reference boards are correct, not fast**: a pure-Python SNES will run far below real time (megadrive-python runs 16-21 frames/s with sound on PyPy; the SNES has more per-dot work). Speed is a later `snes-rust`/`-fast` project's job. - **The PPU is dot-accurate from the start** (no scanline renderer): HDMA and mid-line writes are what the hard tests measure. - **One master-clock timeline**; the APU is a second domain kept as an exact fraction and synchronised at every `$2140-$2143` access (timing.md). - **DMA and HDMA are the board's**, run inside the CPU's bus callback (no RDY on the core), as famicom-python runs OAM/DMC DMA. - **Cartridge maps and decoders live here**, behind the connector protocol with an import-boundary test; programmable coprocessors are cores (cartridge.md "Coprocessors"). - **BYOR**: no ROM, IPL ROM or coprocessor program is committed or fetched. ## What the board needs from 65816-python (the embedding contract) 1. **One bus callback per CPU cycle, in chip order, including internal cycles and dummy accesses**, each with the 24-bit address the CPU drives and an **access kind** (opcode fetch, operand, data read, data write, stack, vector, dummy read, dummy write, internal). The board chooses the cycle's length (6, 8, 12 master clocks) from the address and MEMSEL; the core never counts master clocks. A dummy read of `$2139`, `$213F`, `$4210`, `$4211` or `$FD-$FF`-style registers has side effects, so dummy reads must be real callbacks with their real addresses. 2. **Read and write as separate callbacks** (`read(address, kind) -> int`, `write(address, value, kind)`, `idle(address, kind)`), so the board can sample a read 4 clocks before the cycle's end and commit a write at its end (timing.md "The design"). 3. **Interrupt lines the board sets from inside a callback**: `/NMI` (edge-detected by the core), `/IRQ` (level), `/RESET`; the core samples them at a documented cycle of each instruction (65816-python's brief is authoritative for which), and `WAI` wakes on the line with its 2 IO cycles. The board needs to know the exact sampling cycle to place the H/V IRQ and NMI correctly; the core's brief must state it with its source. 4. **Halted states that pass time**: `WAI` and `STP` make one `idle` callback per `step()` and return, so the board advances the clock cycle by cycle; readable `waiting`, `stopped`. 5. **No RDY input needed**: the board runs DMA inside a callback and returns when it ends. 6. **Instances are independent** (no module globals): the **SA-1** embeds a second 65816-python with its own bus. This is the core's second consumer; it needs nothing extra beyond (1)-(4), plus the SA-1's own interrupt vectors (the SA-1 maps its vectors through registers, which is a bus matter: the board answers the vector reads). 7. **`capture_state()` / `restore_state()`, the disassembler and `DebugSession`**, as the family cores have, so `state.py` and the debugger reuse them; a JSONL trace in the core's schema. Core rungs this board depends on: milestone 1 needs the core's single-instruction rungs and its gilyon `cputest` rung passed; the board's milestone 1 lockstep against MAME is the core's on-board rung. ## What the board needs from spc700-python The contract is spc700-python's (`docs/handoff-brief.md` "The bus contract"); decided by Aubrey 2026-10-21: 1. `SPC700(read_byte, write_byte, idle)`: **one callback per SPC700 cycle, in chip order, including dummy reads**; `read_byte(address)` and `write_byte(address, value)` carry addresses (a dummy read of `$FD-$FF` clears a timer counter); **`idle()` takes no arguments** on internal cycles. 2. The board's `smp` module **reads `cpu.opcode` and `cpu.cycle`** (and `cpu.psw` for TEST writes) inside a callback to price wait states: the TEST register's internal and external wait states are the board's, the core never knows them. 3. The `smp` module **owns the address space**: the IPL ROM overlay at `$FFC0-$FFFF` (when `$F1` bit 7 says so), the I/O page `$F0-$FF` (TEST, CONTROL, DSP address/data, ports `$F4-$F7`, AUXIO `$F8/$F9`, timers 0-2 `$FA-$FF`), and every other access to the 64 KiB ARAM, a **`bytearray` shared with the `dsp` module**. 4. `SLEEP`/`STOP` as a `stopped` state whose `step()` makes one `idle()` and returns, so time passes; `capture_state()`. ## What the board needs from its `dsp` module As `docs/dsp/interface.md` says: `SDSP(aram, registers=...)` with the 64 KiB ARAM `bytearray` shared with the SMP and the 128 power-on register bytes as a constructor argument (Aubrey, 2026-10-21; the default is no longer snes_spc's table, see "Decided by the user"); clocked once per S-SMP clock; `$F2/$F3` register reads/writes from the `smp` module; stereo samples out at 32 kHz; it imports nothing from the board, and a test enforces it. The DSP-only replay host and `.spc` player live in this board's `tests/` and `scripts/`, runnable as `python -m snes_python.dsp`. ## Your task Build `src/snes_python/` (start-here.md's module shape is a suggestion) **from permitted sources only** (docs/restart.md): anomie, fullsnes, the SNESdev wiki, ares (ISC, with its notice), MAME's BSD-3 files, test ROMs, the console. bsnes, Mesen2, snes9x and snes_spc are run, never read. Requirements: - **Machines as data** with every quirk field sourced (comment naming the source and tier). - **One master-clock timeline**, advanced inside the core callbacks; refresh, DMA/HDMA, IRQ/NMI, auto-joypad, mul/div all on it. - **The cartridge connector protocol** of cartridge.md, settled in milestone 4, with the import-boundary test; header detection plus MAME's `snes.xml`. - **A test-ROM harness** reading every protocol in validation.md: gilyon's "Success"/"Failed" (read from the BG tilemap in VRAM, so milestone 1 needs no pixels); krom's PASS/FAIL tokens (same reader), krom pictures at 5 bits (tier 3); undisbeliever probes by picture against recorded console results; the `.spc`/DSP rungs of docs/dsp. - **Tooling at the family bar**: `state.py`, a debugger reusing the cores' `DebugSession` with board watchpoints (B-bus registers, DMA, VRAM/OAM/CGRAM, cartridge registers), a JSONL trace with master-clock times and H/V, `py.typed`, `python -m snes_python ROM --frames N --png out.png --wav out.wav`. ### Milestones, each with its acceptance test, in oracle-tier order 1. **The CPU on the board: bus, cycle lengths, CPU I/O, DMA, timing skeleton, LoROM/HiROM.** Address decode with 6/8/12 cycles and MEMSEL, open bus (Q1), WRAM + WMADD, mul/div with per-cycle steps (Q5, Q6), NMI/IRQ timer (Q12, Q13), GP-DMA and HDMA timing (Q7, Q8) against a PPU *skeleton* (H/V counters, short line, VBlank, latching; VRAM/OAM/CGRAM ports; no pixels), refresh (Q11), auto-joypad (Q14), plain LoROM/HiROM. The APU may be a **port stub** that answers the IPL handshake (`$AA/$BB`) for ROMs that need it. **Acceptance**: gilyon `cputest-full` and `cputest-basic` "Success" (649 tests) read from VRAM; krom's 23 `CPUTest/CPU` ROMs all PASS (tokens from VRAM); krom `BANK/*` (5) PASS; **MAME lockstep**: Super Mario World and two HiROM games, first 2,000,000 instructions, PC/A/X/Y/P/S/D/DB/E equal, master clocks equal except the named causes of mame-oracle.md "What MAME does not model" (each difference classified); instruction counts and wall time under PyPy. 2. **The PPU, dot by dot.** BG fetch schedule, modes 0-7, mode 7 math (Q18), sprites with range/time over (Q17), windows, colour math, mosaic, offset-per-tile, hi-res and pseudo-hires, interlace and overscan, access rules and mid-frame writes (Q15, Q16), the two PPU open-bus latches (Q2), INIDISP behaviour per revision (Q10). **Acceptance**: krom's 41 PPU pictures and the CPUTest pictures where **Mesen2 and bsnes agree**, run as closed boxes (two detectors agreeing; tier 3, recorded as such); undisbeliever's probes whose console result is recorded (U1 done first), as judges; the MAME/Mesen2 disagreements of validation.md each decided by a console result or left open with evidence; gradient-test's own text; 240p suite screens vs Mesen2. 3. **The APU.** `smp` module (IPL overlay, the `$F0-$FF` page) on spc700-python, the two-domain synchronisation, and the `dsp` module through its rungs (docs/dsp/handoff-brief.md). **Acceptance**: gilyon `spctest` "Success" (557); krom's 7 `CPUTest/SPC700` PASS; blargg's timer probes against his `notes.txt` table; the DSP rungs as docs/dsp specifies (their own judges: the snes_spc referee run as a closed box, .spc files); the import-boundary tests. 4. **Cartridges.** Maps (LoROM, HiROM, ExHiROM), SRAM and its mirrors, header detection + `snes.xml`, saves. **Acceptance**: a **compatibility sweep** over the licensed No-Intro set's 3,165 coprocessor-free images: each boots and runs N frames without an exception, frame hash compared with Mesen2 at fixed frames; every difference triaged (sms-python's sweep is the model). 5. **Coprocessors, first wave.** DSP-1 (and 2/3/4, ST010/011) on a new uPD7725/uPD96050 core (its own Phase 0), SA-1 on a second 65816-python, S-DD1, SPC7110 (+RTC), OBC1, S-RTC. **Acceptance**: the SPC7110 Check Program and the games; SA-1 games in the sweep; DSP-1 games' frames vs Mesen2; SnesSpeedTest SA-1 numbers vs its console values. 6. **The hardware-probe sweep.** Every undisbeliever and blargg probe, the official test cartridges (BYOR), Mega Man X2/X3's Cx4 test (after milestone 7's Cx4), the `wiki/Tricky-to-emulate games` list. **Acceptance**: every failure fixed, shown to be a revision difference, or an open item with its evidence. 7. **Super FX, Cx4, Super Game Boy, MSU-1.** GSU and Cx4 as their own cores (each its own Phase 0); SGB embeds gameboy-python (its SGB machine row) with the ICD2 here; MSU-1 from files. **Acceptance**: krom's 31 `GSUTest` PASS and 27 GSU demos vs Mesen2; Cx4 self-test PASS; Mooneye S-group tests through the SGB; krom MSU demos. 8. **Frontend and PAL.** pygame-ce window, audio, saves, controllers; the PAL machine row certified on the PAL-specific probes. ## Constraints - No ROMs, IPL ROMs, coprocessor programs, DATs, traces, screens or fetched references are committed; `.gitignore` already excludes `reference/`, `tests/test_roms/`, `tests/mame_traces/`, `*.sfc`, `*.smc`, `*.srm`, `*.sav`. - No `rm -rf`, `sed -i`, `ln -sfn` one-liners in scripts or docs; edit with the editor tool. - Run Python that reads fetched or downloaded files with `python -I`. - Commit in logical steps with the family's attribution line; local git only until Aubrey decides otherwise. - Every number in a document says where it came from and when it was measured. - The board finds a core's corner → it is raised **on the core** (issue or PR with a failing core-level test), never worked around here. ## Decided by the user: the open decisions (2026-10-21) Aubrey took every recommendation this Phase 0 made: 1. **NTSC first**; PAL is a data row. 2. **Reference revision: CPU version 2 + PPU2 v3, non-1CHIP**; version 1 and 1CHIP are fields. 3. **Coprocessor cores**: SA-1 runs on `65816-python` (its second consumer); DSP-1..4/ST010/011 go to a new `upd7725-python`, with its own Phase 0 before milestone 5. Super FX goes to `gsu-python` and Cx4 to `hg51b-python`; ST018 waits for a GBA board's ARM core. S-DD1, SPC7110, OBC1, S-RTC, MSU-1 and ICD2 are cartridge code in this board. Super Game Boy embeds `gameboy-python`. 4. **Local git until milestone 1 passes**, then a **private** GitHub repository. 5. **BYOR ROMs and IPL** read from user paths (IPL from MAME's `snes.zip`, checked by SHA-1), a skip-IPL path for tests, IPL bytes never copied from another emulator. 6. **A console is available.** Aubrey owns a **Super Famicom** (Japanese NTSC console, same timing as the NTSC SNES) and a **Krikzz Super EverDrive X5** flash cart. Every question that only a console can close (undocumented-behavior C1, U1-U5, the 18 undisbeliever probes on which MAME and Mesen2 disagree, the 65816's C3) is now a **hardware-run probe, tier 1**. Plan one probe session per milestone. The first probe reports the console's own revision: CPU version from `$4210` bits 0-3, PPU1 and PPU2 versions from `$213E`/`$213F` bits 0-3, and the board revision printed inside the shell. That fixes which revision row the console verifies. Results come back as a photo of the screen and, where the X5 writes battery RAM to its SD card, as a `.srm` file a script can diff. Check the X5's save behaviour on the first session. The X5 has no USB link, and coprocessor carts (DSP-1, Super FX, SA-1) need their real cartridges.