# Handoff: build `spc700-python`, the SNES sound CPU core ## Context you are inheriting The family's shape is **chips are cores, boards compose cores**. Each core is certified on its own against its oracles and pinned by the boards that use it. > **Wave 2 (2026-11-19).** GPL and LGPL sources (bsnes/higan, Mesen2, snes_spc in > libgme, MAME's `machine/s_smp` and `sound/s_dsp`) are **closed boxes only**: run, > never read. ares (ISC) is the citable form of byuu's model; MAME's `cpu/spc700` is > BSD-3. Where this Phase 0 brief describes what a closed box's source does, it records > a reading withdrawn in Wave 2 (docs/provenance.md). This core's user is `snes-python` (a sibling repository, being prepared in parallel, with the 65816 core `65816-python`). A board that finds a corner of this core raises it **on the core** (an issue or a PR here, re-certification is fine), never as a workaround in the board. Models for the code and the tooling: - `z80-python` (a sibling repository, v0.4.0 on PyPI): the readability contract (one instruction family per module, one mnemonic per handler docstring, the hardware reason as a comment on the line that encodes it), the tooling bar (`state.py`, `disasm.py`, `debug.py`, `trace.py`, `console.py`, `python -m`), `docs/` layout, oracle tiers. - `6502-python` (a sibling repository): **every CPU cycle is one bus callback, in chip order**, dummy reads included. The SPC700 needs the same, plus internal cycles, which the 6502 does not have. - `sm83-python` (a sibling repository): the closest sibling: a 6502/Z80 cousin with internal cycles, the same Phase 0 shape, a generated opcode table, a minimal harness for board-free test ROMs. The user's rules that bind this work: - **Oracle tiers**: hardware-captured > hardware-verified > research documentation > emulator-derived (see validation.md; there is no hardware-captured or die-traced oracle for this chip); a low-tier oracle is a detector, never a judge; state the tier of every claim. - **Python floor 3.11** (the newest PyPy supports): `requires-python = ">=3.11"`, ruff `target-version = "py311"`, CPython and PyPy both green. - **Reference cores are correct, not fast.** A `-fast` fork is later work. - **Modern over compat**: callables, dataclasses, no shims; no external users. - **No dangerous-looking shell one-liners** in scripts or docs (no `rm -rf`, `sed -i`); anything that reads `reference/` runs with `python -I`; keep CI minimal (PyPy only, main + PRs, cancel in progress) if and when a GitHub repo exists. This repository is **local git only** until the user says otherwise. ### Read, in this order, before writing code 1. `README.md`. 2. `docs/start-here.md`: registers, flags, addressing modes, the bus, control flow, no interrupts, SLEEP/STOP, reset, what the core does not own. 3. `docs/timing.md`: every group's cycles with addresses, and every opcode's kind sequence. 4. `docs/undocumented-behavior.md`: U1-U15, C1-C9, B1-B5. 5. `docs/validation.md`: every oracle with tier and pin; measured results; the rungs. 6. `docs/mame-oracle.md`. 7. 6502-python's `src/sixfiveohtwo/` (the per-cycle bus) and z80-python's `docs/start-here.md` ("The embedding contract"). Then `python scripts/fetch_references.py` (9 repositories, 6 files, ~70 MB), `python -I scripts/build_referees.py` (the three spctest programs, the IPL ROM), `python -I scripts/anomie_opcodes.py` (`tests/anomie_opcodes.json`). ## Decided by the user - **The SPC700 is its own core repository, `spc700-python`**, even though only SNES hardware uses it here: CPU cores are always their own repo, and SingleStepTests judges it without a board. - **The S-DSP lives inside `snes-python`**, not in a repository of its own. The DSP ports `$F2`/`$F3` connect to the board's in-board DSP module; the core never sees them except as ordinary bus cycles. ## Decisions already made (by this groundwork; overrule freely) - **One class, no variants.** The SNES has one S-SMP; Sony's CXP8nnnn microcontrollers (fullsnes) and the "SPC700 alpha II" with fewer opcodes are out of scope. - **Fetch-first `step()`**: a step starts with the opcode fetch and ends after the instruction's last cycle, the order SingleStepTests records (first cycle = fetch at PC), so rung 1 compares cycle for cycle. - **All 256 opcodes implemented**, SLEEP and STOP as a `stopped` state. ## Core boundary (recommendation) | Part of the S-SMP | Where it should live | Why | | --- | --- | --- | | SPC700 registers, decode, ALU, every bus cycle | **core** | the CPU | | 64 KiB RAM | **board** (`snes-python`), behind the core's callables | shared with the S-DSP (BRR samples, echo buffer); the S-DSP reads it on its own schedule | | IPL ROM at `$FFC0`-`$FFFF` and its CONTROL bit 7 overlay | board's `apu`/`smp` module | it is a read-path decision (reads from ROM, writes to RAM: B4); the core only fetches through the bus. The ROM bytes come from `reference/build/iplrom.bin`, never committed | | `$F0` TEST (wait states, timer gates, RAM write/read disable) | board's `smp` module | changes the cost of each cycle and the RAM path; needs the core's P flag at write time (U9), which it reads from `cpu.psw` | | `$F1` CONTROL (timer enables, port clears, IPL map) | board's `smp` module | | | `$F2`/`$F3` DSP address/data | board's in-board DSP module (decided above) | | | `$F4`-`$F7` CPU ports | board's `smp` module, shared with the 65816 side | they are a pair of latches between two cores | | `$F8`/`$F9` AUXIO | board's `smp` module | RAM-like on the SNES | | Timers 0-2, `$FA`-`$FF` | board's `smp` module | clocked by the same cycle stream; reads clear counters (B2) | | Cycle clock, wait states, the 24.576 MHz relation to the S-DSP | board | the core reports cycles; the board prices them | A thin `smp` module in snes-python (one class owning the I/O page, timers and IPL overlay, wrapping this core) keeps the core reusable by a standalone `.spc` harness, which needs the same module without the rest of the SNES. The harness in this repository (below) carries a minimal copy of it for the rungs. ## The bus contract (required) One callback per SPC700 cycle, in chip order, **including dummy reads and internal cycles**: ```python cpu = SPC700(read_byte, write_byte, idle) # read_byte(address) -> int one cycle that reads (operands, data, stack, # vectors, and every dummy read of PC or of a # store's destination: U1, C2) # write_byte(address, value) one cycle that writes # idle() one internal cycle: no address (C1) # Readable inside any callback: # cpu.psw (current, incl. P: U9), cpu.opcode, cpu.cycle (index of this cycle # within the instruction, 0 = fetch), cpu.stopped cycles = cpu.step() # SPC700 cycles consumed == number of callbacks made ``` - `idle` defaults to a no-op; the return value still gives correct timing. - The board advances its devices **inside** the callbacks (24 crystal clocks per cycle at TEST = `$0A`, more with wait states), never by adding `step()`'s return value on top. - The return value equals the number of callbacks. Assert it in tests. - `cpu.opcode` and `cpu.cycle` let the board apply either internal-cycle wait model (C3) without the core knowing about wait states. - A stopped CPU (after SLEEP or STOP) makes `step()` perform exactly one `idle()` and return 1 (C5); only `reset()` leaves the state. - `reset()` reads the vector at `$FFFE`/`$FFFF` through `read_byte` and sets P = B = I = 0; other registers keep settable defaults (U12). - No interrupt input (U11; see the open decisions). - Keep the contract transcribable: plain integers, no generators or coroutines in the hot path, so `spc700-rust` and `-fast` forks can copy it. ## Your task Build `src/spc700_python/` to the contract above and pass the rungs in [validation.md](validation.md) "The plan", in order. In summary: 0. **Tables.** Generate the opcode map (mnemonic, operands, bytes, cycles, flags) from `tests/anomie_opcodes.json`; hand-write the handlers by addressing-mode group as [timing.md](timing.md) lists them. Tests: cycle count == anomie for every opcode and path; per-group (kind, address) sequence on random states == timing.md; readability (one mnemonic per handler docstring). 1. **SingleStepTests spc700**, 256 files × 1,000: registers, PSW, RAM, every cycle's kind and address, data where not null (C9); `EF`/`FF` registers only. Where SST and anomie differ (C4, C6, C7) follow SST and say so in the handler's comment. 2. **gilyon spctest** in the harness: 1,368/1,368 ($558 tests: the ROM numbers them in hexadecimal; this brief first said 558). 3. **PeterLemon CPU tests** (`.spc`) in the harness: 162/162 `$0n`. 4. **MAME replay** of real code: detector. 5. **A second per-cycle emulator** diffed per cycle: ares (ISC) only, or Mesen2 strictly as a closed box (its trace logger, no hook that needs its source). 6. **Through snes-python**: the board's rungs. ### The minimal harness (`spc700_python.harness`) A host that is not a SNES, for rungs 2-4 and the CLI: 64 KiB RAM; the I/O page `$F0`-`$FF` with RAM write-through (B4); the IPL ROM overlay (from `reference/build/iplrom.bin` when present, otherwise none: the test programs do not need it to start); CONTROL; timers 0-2 at 128/16 cycles (B2, enough for PeterLemon's waits); DSP ports as a 128-byte register file with no audio; `$F4`-`$F7` driven by a small scripted S-CPU (for spctest: `$F5 = 1`, `$F6/$F7` = start test number − 1, then watch `$F4` for 1, or 2/3 with the failing test's results; for PeterLemon: collect `$F4` writes); a `.spc` loader (header "SNES-SPC700 Sound File Data v0.30", registers at $25-$2B, RAM at $100, DSP at $10100, the 64 IPL-area bytes at $101C0, as the SPC file format document gives them); a stop condition on a branch to itself. No S-DSP audio, no S-CPU: those are the board's. ### Tooling at the family bar `state.py` (`CPUState` with `a x y sp pc psw`, `stopped`, a `ya` property), `disasm.py` (sharing the generated table; anomie's syntax, `!a` for absolute, `d.b` / `m.b` bit operands; check against MAME's `spc700ds` on the same trace), `debug.py` (`DebugSession` with breakpoints and bus watchpoints that see dummy reads and internal cycles), `trace.py` (JSONL, the family schema with `kind` = read, write, internal and a `dummy` flag), `console.py`, `py.typed`, and `python -m spc700_python FILE.spc|PROGRAM.bin [--org 0x300] [--max-cycles N] [--trace out.jsonl]` running in the harness and printing the `$F4` port log. ## Decided by the user: the open decisions (2026-10-21) Aubrey took every recommendation this groundwork made: 1. **Import package `spc700_python`, class `SPC700`.** 2. **The S-SMP's I/O page, timers and IPL overlay live in a thin `smp` module inside snes-python** (the table under "Core boundary"); this repo's harness carries a minimal copy for the rungs. 3. **Internal cycles call `idle()` with no arguments**; the board reads `cpu.opcode`/`cpu.cycle` in the callback to price wait states (C3). 4. **One-byte opcodes do a real `read_byte(PC)` on their second cycle** (C2); revisit only if a hardware run of blargg's `1-test_exec_from_io` disagrees. 5. **No interrupt input** (U11). 6. **Local git until rung 2 passes, then public** like the other cores, with the history audited first. 7. **Rung 5 is a follow-up session**, not part of the first build.