# Handoff: build `gsu-python`, the SNES Super FX core > **Sources (wave 2, 2026-11-19).** byuu's model is cited to **ares** (ISC, > `ares/component/processor/gsu` and `ares/sfc/coprocessor/superfx` at `9408cb4`; notice > in [NOTICE](../NOTICE)), its readable form; bsnes is a closed box and is not cited. > MAME is cited from its BSD-3 files (`bus/snes/sfx.cpp`, `sfx_dasm.cpp`) and from runs > (OBSERVED, MAME 0.285: traces, krom's rows, the rung-4 replays); its GSU file > `cpu/superfx/superfx.cpp` is GPL and a closed box. A **†** marks a statement about > Mesen2, snes9x, the sd2snes GSU or MAME's `superfx.cpp` that was **read from source under > the old rule (before 2026-11-19)**: those are closed boxes from now on, and such a > statement is never the basis of a default. Results of running Mesen2 or MAME are > closed-box observations and carry no mark. > **Built 2026-11-07** (rungs 0-4): results in [claims.md](claims.md) and > [validation.md](validation.md#results-2026-11-07-the-build-session). The > public API is the contract below as written; `run` returns the clocks used and > the board keeps the overshoot. ## Context you are inheriting The family's shape is **chips are cores, boards compose cores**; CPUs always get their own core (Aubrey's chip-extraction rule), and a coprocessor that runs its own program is a CPU. This core's first user is `snes-python` (`../snes-python`), milestone 7 ("Super FX, Cx4, Super Game Boy, MSU-1. GSU and Cx4 as their own cores (each its own Phase 0) ... **Acceptance**: krom's 31 `GSUTest` PASS and 27 GSU demos vs Mesen2"). A board that finds a corner of this core raises it **on the core** (an issue or PR here; re-certification is fine), never as a workaround in the board. Models for the code and the tooling: - `upd7725-python` (`../upd7725-python`): the previous SNES coprocessor core, built from the same Phase 0 shape: a frozen `Model`, contested behaviours as class attributes, the host port as methods the board calls after catching the chip up, `harness.py`, a MAME `replay.py`, the tooling. - `spc700-python`: the readability contract (one instruction family per module, the hardware reason as a comment on the line that encodes it), the tooling bar (`state`, `disasm`, `debug`, `trace`, `console`, `python -m`). - snes-python's `necdsp.py` (a timed cartridge with an exact clock fraction and `advance`) and `sa1.py` (a second processor sharing ROM and RAM with the S-CPU, run in `advance` until it has caught up): the board side of this core will be a blend of the two. This chip is harder than the uPD7725: **variable instruction time** (fetch source, buffers, multiplier, plot, bus waits), **a pipeline with delay slots and prefixes**, **two shared memories with arbitration**, a cache and two pixel caches. The value semantics are well covered (BK2, fullsnes, krom's 1,226 rows, two emulator lineages agreeing); **the time is covered by BK2 alone** and the emulators disagree with it. Get values right first (rungs 0-2, 4), then time (rung 3), then the board. The user's rules that bind this work: - **Oracle tiers**: hardware-captured > hardware-designed self-test > manufacturer > program > research > emulator (validation.md, which adds the test-author tier for krom's rows); a low tier detects, never judges; state the tier of every claim. Authority for conflicts: console > fullsnes/anomie > two emulators agreeing > MAME. **No hardware capture of this chip exists.** - **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. A pure-Python GSU at 10.7-21.4 MHz will run far below real time; the board is allowed to. - **Modern over compat**: dataclasses, callables, no shims; no external users. - **BYOR**: no ROM, trace, screenshot or program image is committed or fetched; game dumps and krom's ROMs are read in place. - **No dangerous-looking shell one-liners** (`rm -rf`, `sed -i`, `ln -sfn`) in scripts or docs; anything that reads `reference/` or a downloaded file runs with `python -I`; **keep the machine light** (`nice -n 19`, one heavy process at a time, short MAME windows); **local git only** until Aubrey says otherwise; keep CI minimal (PyPy only, main + PRs) if a GitHub repo exists. - **Closed box only** (rule of 2026-11-19, replacing "read and cite, never copy"): bsnes, Mesen2, MAME's `superfx.cpp`, snes9x and the sd2snes GSU may be built and run as detectors, never opened. byuu's model is read and cited in its ISC form, **ares**, and MAME through its BSD-3 files (`bus/snes/sfx.cpp`, `sfx_dasm.cpp`), with their notices (NOTICE). This brief was written under the old rule; its emulator citations are history (docs/provenance.md). ### Read, in this order, before writing code 1. `README.md`. 2. `docs/start-here.md`: registers, the S-CPU interface, the opcode map and prefixes, the pipeline, memory, buffers, cache, plot, multiply, STOP/IRQ, arbitration, reset, the boundary. 3. `docs/timing.md`: the clocks, BK2's cycle tables and its six worked examples, the buffers, multiply, plot and bus waits, what the games set. 4. `docs/undocumented-behavior.md`: U1-U32, C1-C17. 5. `docs/validation.md`: every oracle with tier; what does not exist; rungs. 6. `docs/mame-oracle.md`: the trace recipe, its traps, the replay design. 7. BK2 Section 2 (`reference/docs/book2.pdf`, image-only; read the pages, not only the OCR): chapters 4-8 and the chapter 9 entries you implement. Then `python -I scripts/fetch_references.py` (BK2 and its OCR; ares, MAME's BSD-3 files and fullsnes are satisfied in place from a sibling snes-python; the closed boxes are not fetched) and `python -I scripts/survey_games.py`. ## Decided by the user - **The Super FX is its own core, `gsu-python`** (snes-python handoff-brief, decision 3: "Super FX goes to `gsu-python` and Cx4 to `hg51b-python`"). - **ROMs are BYOR** (snes-python decision 5): the GSU's program is the cartridge's own ROM, read in place. ## The core boundary | Part | Where it should live | Why | | --- | --- | --- | | R0-R15, SFR, PBR, ROMBR, RAMBR, CBR, SCBR, SCMR, COLR, POR, BRAMR, VCR, CFGR, CLSR; Sreg/Dreg, prefixes, the pipeline byte | **core** | the chip | | Decode and all 98 instructions; ALU, shifts, multiplier | **core** | the chip | | The ROM buffer, the RAM write buffer, the RAM address register, their waits | **core** | inside the chip (BK2 2-2-2 "ROM buffering system") | | The 512-byte cache, its flags and its S-CPU window `$3100-$32FF` | **core** | the chip's RAM | | The plot unit, both pixel caches, RPIX | **core** | the chip | | The GSU's own memory map (ROM `00-5F`, RAM `70-71`, the `0000-7FFF` mirror) | **core** | the chip's address pins (BK2 3.2, fullsnes "at GSU side") | | The S-CPU's register interface (`$3000-$34FF` semantics: latch, GO, IRQ clear, access while running) | **core** | the chip's pins: the S-CPU talks to the GSU, not to RAM, there | | RON/RAN arbitration: the GSU's WAIT, the S-CPU's fixed vectors while GO = 1 and RON = 1, RAM unavailable while RAN = 1 | **core** decides, **board** applies | the GSU sits between the SNES bus and the memories (BK2 2-1-4 figure 2-1-2) | | STOP's IRQ flag and the /IRQ output level | **core** | the chip's pin | | Time in input clocks, CLSR's divide-by-two, MS0 | **core** | inside the chip | | The input clock source (master clock for MC1, 21.4/21.44 MHz crystals) and its fraction of the master clock; catching the GSU up before every S-CPU access | board | the oscillator is on the cartridge (timing.md) | | Where ROM, RAM and the register file appear in the S-CPU's map, per chip and PCB; mirrors; open bus | board | cartridge decode (fullsnes "Memory Map" per chip, U22) | | RAM size, battery save, the ROM file | board | the PCB and the user's files | | ORing /IRQ into the S-CPU's IRQ | board | snes-python `Cartridge.irq` | ## The bus contract snes-python needs What the board's `SuperFXCartridge` (its milestone 7) does with this core, in the connector protocol of snes-python `docs/cartridge.md` (`timed = True`, `advance(master_clocks)` before every cartridge access and at each line's end, `irq`, `battery`): 1. **Clock.** `advance(master_clocks)` converts master clocks to the GSU's **input clocks** with an exact fraction and runs the core that far, like `necdsp.py`: MC1 = 1/1 (it is the master clock); GSU-1 = 4708/4725 (21.4 MHz); GSU-2 = 23584/23625 (21.44 MHz); PAL MC1 boards follow the PAL master clock. The core runs whole instructions and may overshoot by the rest of one; it keeps that debt (as `sa1.py` does) and spends idle time in one step when GO = 0. CLSR's 10.7/21.4 MHz is the core's business: at CLSR = 0 every GSU cycle is two input clocks. 2. **S-CPU accesses to the register file**: banks `00-3F`/`80-BF`, `$3000-$34FF` (fullsnes; BK2 maps `$3000-$32FF`; MC1's window is `$3000-$347F`): `gsu.read_io(address)` / `gsu.write_io(address, value)` **after** the `advance` that brought the GSU to the access's master time. `read_io` returns `None` where the chip does not drive the bus (unmapped holes, and, by C7, registers other than SFR/VCR while GO = 1): the board answers open bus. 3. **S-CPU accesses to ROM** (`00-3F/80-BF:8000-FFFF`, `40-5F/C0-DF`; MC1 `00-1F/80-9F` only): `gsu.cpu_rom_byte(address)` returns the fixed vector byte while GO = 1 and RON = 1 (U16), else `None` and the board reads the ROM. 4. **S-CPU accesses to Game Pak RAM** (`70-71/F0-F1`, and `00-3F/80-BF:6000-7FFF` for its first 8 KiB; MC1 `60-7D/E0-FF`): allowed while `gsu.cpu_owns_ram` is true (GO = 0 or RAN = 0); otherwise reads are open bus and writes dropped (U16; decision 6). 5. **The memories are shared buffers**: the board hands the core the ROM as `bytes` and the RAM as the same `bytearray` it maps for the S-CPU (and saves as the battery file), so there is no copy and no bus callback in the GSU's hot path (decision 3). The core never resizes them; RAM size and mask come from their lengths. 6. **IRQ**: `cartridge.irq = gsu.irq` after every `advance` and every `read_io` (a read of `$3031` releases it). 7. **Reset**: the console's reset calls `gsu.reset()` (GO = 0, buses to the S-CPU; BK2 1.5.1); power-on constructs the core (U26). 8. **S-CPU wait states**: the GSU boards are SlowROM; the board's 6/8/12 table applies unchanged (no `access_cycles` override). ## The contract (required shape; names are decision 2) ```python from gsu_python import GSU, MC1, GSU1, GSU2 gsu = GSU(rom, ram, model=GSU2) # rom: bytes (the cartridge image), ram: bytearray gsu.reset() # BK2 1.5.1: idle, buses to the S-CPU; power-on values U26 gsu.run(clocks) -> int # spend this many input clocks; whole instructions; # returns the clocks used (>= clocks while running) gsu.read_io(address) -> int | None # S-CPU read of $3000-$34FF (bank already decoded) gsu.write_io(address, value) # S-CPU write; $301F starts, SFR GO=0 stops gsu.cpu_rom_byte(address) -> int | None # the fixed vector byte while GO and RON gsu.cpu_owns_ram -> bool # False while GO and RAN gsu.irq -> bool # the /IRQ output (SFR.IRQ and not masked) gsu.running -> bool # SFR.GO gsu.capture_state() / gsu.restore_state(state) ``` - The board calls `run` before every S-CPU access (through `advance`), so an access lands between two GSU instructions, as in every emulator. **Inside** an instruction the core keeps its own clock exact (fetches, buffers, waits, multiply, plot), so the time it reports is BK2's (rung 3). - A WAIT on RON/RAN (BK2 5.3) consumes the clocks given and makes no progress until the S-CPU's SCMR write grants the bus again; the core must not spin per clock in Python: it returns the remaining clocks as spent at once. - Contested behaviours are class attributes or `Model` fields (decision 5) so a replay can flip one without editing code. Plain integers and lists in the hot path, no generators, no per-instruction allocation, so a `gsu-rust` and a `-fast` fork can transcribe it. ## Proposed layout (the build session may refine) ``` src/gsu_python/ __init__.py GSU, the models, version model.py frozen Model: name, vcr, ms0_implemented, rom/ram limits, register mirrors core.py registers, reset, run loop, pipeline (fetch from cache/ROM/RAM), host interface _decode.py the 256 x ALT x B table -> handler and form name (U3, U4) _alu.py ADD..CMP, AND..XOR, shifts, byte ops, MERGE, DIV2 (each flag rule commented) _multiply.py MULT/UMULT/FMULT/LMULT, BK2 8.2.1's partial products, R4 rules (C9), MS0 time _memory.py GSU-side map, ROM buffer, RAM write buffer, RAM address register (U7, U17, U29) _cache.py CBR, flags, fill order (C10), S-CPU window (U14, U15) _plot.py PLOT, RPIX, COLOR/GETC/CMODE, both pixel caches, address formulas (U20, C5) _jump.py branches, JMP/LJMP, LOOP, LINK, STOP (C1, C3) timing.py every cycle constant in one place, each with its BK2 page disasm.py BK2 syntax, prefixes folded state.py, debug.py, trace.py, console.py, harness.py, replay.py, __main__.py, py.typed tests/ test_decode.py, test_disasm_vs_mame.py (rung 0) test_alu.py, test_multiply.py, test_memory.py, test_cache.py, test_plot.py, test_jump.py, test_pipeline.py, test_host.py (rung 1) test_krom.py (rung 2), test_timing_bk2.py (rung 3), test_mame_replay.py (rung 4, slow) ``` ## Your task: the rung ladder Build to the contract and pass the rungs of [validation.md](validation.md) "Rung plan", in order: 0. **Decode.** The opcode table for all 256 opcodes x 4 ALT states x B, from BK2 chapter 9 and fullsnes, with C2 and C3 as fullsnes and U4's fallback; `disasm.py` against MAME's `dasm` (`mame_gsu_trace.py --dasm`) of every PC in the Star Fox window, folding MAME's prefix lines. 1. **Directed tests**, one file per family, each citing BK2's page or a U/C entry: every instruction's result and flags; prefixes across branches (BK2 examples 7-10); the pipeline (examples 1-4, 12-14 as programs); R15 after STOP in all four cases of BK2's table (C1); the ROM buffer across ROMB (U7); odd RAM words (U17); SBK's address (U29); the cache window and flags (U14, U15); PLOT in 4 heights x 3 depths with transparency, dither, high nibble, freeze (U20, C5), RPIX after partial rows (U21); MULT/FMULT/LMULT including Dreg = R4 (C9); the S-CPU interface (U8-U11, C6, C7) and the fixed vectors (U16); RON/RAN WAIT. 2. **krom's 31 `GSUTest` in `harness.py`**: all 1,226 rows, values read from the ROMs and their sources in snes-python's checkout (BYOR; tests skip when absent). MAME and Mesen2 pass every row; so must the core. 3. **BK2's timing vectors**: the six chapter-7 examples in the cache at CLSR = 1 (14, 10, 17, 14, 19, 16 cycles) and the "Cache RAM execution time" column of chapter 9 for every instruction, the ROM/RAM columns at CLSR = 0, the multiply extras (C14). Where BK2 gives a range ("1-48"), pin both ends. Record every place where the core's model is a choice BK2 does not make (C10, C12, C15) in claims.md. 4. **MAME replay (values)** (`replay.py`, mame-oracle.md "The replay"): Star Fox frames 600-602 first (225,133 instructions, 70 instruction forms), then Stunt Race FX, Vortex, Doom, Yoshi's Island (RAM and cache code; extend `trace_coverage.py` to decode RAM-resident code from the replayed RAM), Winter Gold. Classify every divergence by C/U entry. 5. **(Follow-up) Mesen2 replay** per instruction through its Lua GSU trace, values and Mesen2's cycle counts (a timing detector with a known model). 6. **Through snes-python milestone 7**: krom's 31 `GSUTest` PASS on the board; krom's 27 demos vs Mesen2; games vs Mesen2 (Star Fox, Stunt Race FX, Yoshi's Island, Doom, Winter Gold's race start). ### The minimal harness (`harness.py`) The core over the ROM bytes and a RAM `bytearray`, plus a scripted S-CPU: writes registers and the cache window through `write_io`, starts at R15, runs until GO = 0 or a clock budget, reads results through `read_io`; it replays krom's per-row protocol (rung 2) and a MAME `gsu.log.gz` (rung 4: apply W lines in log order, check R lines). No S-CPU core, no cartridge decode, no clock ratio. ### Tooling at the family bar `state.py` (`CPUState`: r0-r15, sfr with named flags, pbr, rombr, rambr, cbr, scbr, scmr, colr, por, cfgr, clsr, sreg, dreg, pipeline, the buffers and pixel caches), `disasm.py` (BK2 syntax: `ADC R3`, `ADD #3`, `MOVE R4,R5`, `LMS R1,(24h)`, `IWT R15,#A103h`; prefixes folded into the instruction they modify, a lone prefix printed as itself), `debug.py` (`DebugSession`: PC breakpoints, RAM and register watchpoints, S-CPU access records, PLOT records), `trace.py` (JSONL, one record per instruction with the registers and the cycles it took, one per S-CPU access), `console.py`, `py.typed`, and `python -m gsu_python ROM [--model gsu2] [--start BANK:ADDR] [--clsr 1] [--max-clocks N] [--trace out.jsonl] [--krom]` running the harness and printing the registers at STOP. ## Decided by the user, 2026-11-07: every recommendation below taken Aubrey took all twelve as recommended ("Take all, build it"). Going public still waits for Aubrey's word, as for the other cores. The items keep their reasoning for the build session: 1. **One core for all five chips.** Recommend **yes**: one `GSU` class and a frozen `Model` per chip (`MC1`, `GSU1`, `GSU1A`, `GSU2`, `GSU2_SP1`: VCR, whether MS0 exists, ROM/RAM limits, the register mirrors), as every emulator does with one implementation. No source documents an instruction-set difference between them. 2. **Names.** Repository `gsu-python` (decided). Recommend import package **`gsu_python`**, class **`GSU`**, models **`MC1`, `GSU1`, `GSU1A`, `GSU2`, `GSU2_SP1`**. Alternative: `SuperFX` (ares, MAME), which names the product rather than the chip the boards print. 3. **Memory access.** Recommend the core **holds the ROM `bytes` and the RAM `bytearray` the board shares**, and decodes the GSU's own map itself (it is the chip's: BK2 3.2). Alternative: read/write callbacks into the board, which cost a call per opcode byte in a pure-Python hot path and move the chip's map out of the chip. 4. **Unit of time.** Recommend the core counts **input clocks** (the 21.4 MHz unit every emulator uses; CLSR = 0 doubles every cost inside the core) and the board owns the input-clock-per-master-clock fraction (MC1 1/1, GSU-1 4708/4725, GSU-2 23584/23625: C8 decided for the crystals). Alternative: GSU cycles, which pushes CLSR into the board. 5. **The contested defaults, as a group** (each a class attribute so a replay can flip it): C1 **R15 = STOP + 2** (emulators, fullsnes' table; BK2's + 1 as the switch); C6 **the write latch** (fullsnes, Mesen2†); C7 **only SFR, SCMR and VCR while GO = 1**, other reads `None` (open bus), other writes ignored; C9 **FMULT leaves R4 unchanged** (BK2, fullsnes); C11 **SFR.IRQ only when unmasked** (four lineages); C12 **5-cycle ROM/RAM opcode fetch at 21.4 MHz**; C13 **BK2's RAM model** (word reads charged, word-wide write buffer); C14 **BK2's multiply time**; C15 **overlapped pixel write-back**; C10 **BK2's incremental cache fill**; C16 **POR kept at STOP** (its switch was removed on 2026-11-19: provenance.md). Recommend accepting all of them. 6. **S-CPU RAM writes while the GSU owns RAM** (U16): recommend **dropped** (MAME, Mesen2†; the GSU drives the RAM's address bus then), ares' "performed" as a switch. And **S-CPU reads** then: open bus (the board's `mdr`). 7. **Per-chip quirks.** Recommend: **VCR** 1 on `MC1`, 4 on `GSU2`/`GSU2_SP1`, and 4 (flagged Undecidable, U23) on `GSU1`/`GSU1A`; **MS0 ignored on MC1** (fullsnes U25: "always using slow multiply mode"); **no MC1 STOP hang** (U24: not characterised well enough to model; Star Fox avoids it by construction). 8. **Register mirrors** (U12): recommend fullsnes' per-VCR mirror tables in the `Model` (research tier; the emulators have none), so an S-CPU access to `$3040`, `$3300-$34FF` behaves per chip; no shipped game is known to use a mirror, so this is low-stakes either way. 9. **A hardware route** for C1, C11, U9, U23 and the timing BK2 does not fix: it needs a Super FX board with a rewritable ROM (an EPROM-modified Star Fox / Stunt Race FX / Yoshi's Island cartridge, or a reproduction board on a donor GSU-2) on Aubrey's Super Famicom; an FXPak Pro is an FPGA model, not the chip. Recommend **not now**: build on BK2 and the detectors, and revisit if rung 3 and the games at rung 6 leave a timing dispute that shows on screen. 10. **Local vs GitHub.** Recommend local until rung 2 passes, then **public** like the other cores (history audited first). 11. **Rung 5** (Mesen2 per-instruction replay): recommend a follow-up session; rungs 0-4 cover values with two lineages and time with BK2. 12. **Disassembler syntax.** Recommend **BK2's mnemonics** with prefixes folded (`ADC #3`, `MOVE R4,R5`, `LMS R1,(24h)`), MAME's one-line-per-byte form only inside the rung-0 test.