# Handoff: build `upd7725-python`, the SNES DSP-n / ST01x core ## 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 DSP that runs its own program is a CPU. This core's first user is `snes-python` (`snes-python`), milestone 5 ("Coprocessors, first wave": DSP-1, and 2/3/4, ST010/011, "on a new uPD7725/uPD96050 core (its own Phase 0)"). 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: - `spc700-python`: the closest Phase 0 and build of a SNES-side core: 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`), a minimal harness, a MAME replay. - `huc6280-python`: the same Phase 0 shape, MAME replay of real games, a second-emulator rung run as a follow-up. - `z80-python` (v0.4.0): `docs/` layout, oracle tiers. This chip is simpler than any of those: **no memory bus, no cycles inside an instruction, no addressing modes**. One 24-bit word, one clock, a fixed sequence of parts (start-here.md "Order inside one instruction"). The work is in getting every field's semantics and the contested corners right, and in the host port. The user's rules that bind this work: - **Oracle tiers**: hardware-captured > hardware-designed self-test > manufacturer > program > research > emulator (validation.md); a low tier detects, never judges; state the tier of every claim. **No hardware capture of this chip exists**, and the uPD96050 has no NEC document at all. - **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**: dataclasses, callables, no shims; no external users. - **BYOR**: no program image, ROM or trace is committed or fetched; the images are read in place from MAME's device sets (validation.md). - **No dangerous-looking shell one-liners** in scripts or docs; anything that reads `reference/` runs with `python -I`; **local git only** until Aubrey says otherwise; keep CI minimal (PyPy only, main + PRs) if a GitHub repo ever exists. - **Closed box only** for GPL and non-commercial sources (bsnes, Mesen2, snes9x, MAME's GPL files): they may be built and run as detectors, their source is never opened (rule of 2026-11-19, docs/provenance.md). byuu's model is read and cited in its permissive forms: ares (ISC) and MAME's `upd7725.cpp` (BSD-3-Clause), notices in NOTICE. ### Read, in this order, before writing code 1. `README.md`. 2. `docs/start-here.md`: registers, flags, SR, formats, the order inside an instruction, the host port, reset, ROM images, the boundary, the contract. 3. `docs/timing.md`: the clocks, the field tables, what each program uses. 4. `docs/undocumented-behavior.md`: U1-U21, C1-C15. 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. The DS (`reference/docs/upd77c25-datasheet-U12675EJ7V0DS00.pdf`) sections 2-4: it is short (56 pages) and its tables are the rung-1 expectations. Then `python -I scripts/fetch_references.py` (the DS and fullsnes; the emulator trees are satisfied in place from snes-python), `python -I scripts/survey_programs.py` (needs MAME's device sets at the default path). ## Decided by the user - **The uPD7725 core is its own repository, `upd7725-python`**, and the DSP-1..4 and ST010/ST011 all run on it (snes-python handoff-brief, decision 3: "DSP-1..4/ST010/011 go to a new `upd7725-python`, with its own Phase 0 before milestone 5"). - **Programs are BYOR** (snes-python decision 5): MAME's `sns_dsp*`/`sns_seta*` sets read in place. ## The core boundary | Part | Where it should live | Why | | --- | --- | --- | | Registers, decode, ALU, multiplier, flags, stack, data RAM, ROM contents | **core** | the chip | | The host port: DR/SR, DRC 8/16-bit, DRS, RQM | **core** | the chip's pins: A0, /RD, /WR; their state machine is inside the die | | uPD96050 data RAM as words (`dsp.ram`) | **core** | the chip's RAM; the board splits bytes and maps `$68-$6F` | | Program/data image formats (PROM layout, raw big-endian, the little-endian `.program.rom`/`.data.rom` split) | **core**, a small `images` module (open decision 7) | the PROM layout is the chip's (DS p.27); every consumer needs it | | A0 wiring, the banks and addresses per PCB | board | cartridge decode (fullsnes "DSP Mapping") | | The DSP clock and its fraction of the master clock; catching the DSP up before each host access | board | the oscillator is on the cartridge (timing.md) | | The ST010/ST011 battery save | board | `.srm` beside the ROM, from `dsp.ram` | | Detecting which program a cartridge needs | board | header + `snes.xml` (fullsnes: the header does not say which DSP) | ## The contract (required shape; names are open decision 2) ```python from upd7725_python import UPD7725, UPD96050_MODEL, load_image program, data = load_image(path_or_bytes) # any of the three formats dsp = UPD7725(program, data, model=UPD96050_MODEL) # default model: the uPD7725 dsp.reset() # DS (25): PC, FLAGA, FLAGB, SR = 0; RP = 3FFh; nothing else dsp.step() -> int # one instruction, returns 1 (one clock) dsp.run(n) -> int # n instructions, plain loop; returns n dsp.read_sr() -> int # host, A0 = 1: SR bits 15-8; no side effect dsp.read_dr() -> int # host, A0 = 0: one byte; DRS/RQM as U7 dsp.write_dr(value) # host, A0 = 0 dsp.ram # data RAM words (list of int) dsp.capture_state() / dsp.restore_state(state) ``` - The board runs the DSP to the access's time, then calls the host method: an access always falls **between two instructions**, as in every emulator and MAME's trace. Whether the board adds the DS's one-instruction RQM latency is decision 5. - `step()` executes U1's order exactly; flags, M/N and the stack follow the C-entry defaults in decision 4, each behind a named class attribute or constructor flag where the entry says "switch", so a replay against MAME or a comparison with fullsnes' rule can select the other behaviour without editing code. - Plain integers and lists, no generators, no per-instruction allocation, so `upd7725-rust` and a `-fast` fork can transcribe it. ## Proposed layout (the build session may refine) ``` src/upd7725_python/ __init__.py UPD7725, the two models, load_image model.py the frozen Model dataclass: widths, stack depth, RAM/ROM sizes, extra BRCH cpu.py registers, reset, step/run, the host port _fields.py field extraction; the SRC and DST tables _alu.py the 16 ALU ops and both flag sets (C1, C2, C3 decided here, each commented) _op.py OP and RT _jump.py JP: the BRCH table, PC widths, HJMP/HCALL/JMPSO images.py the three program image formats (fullsnes' JRQM detection) disasm.py NEC syntax, sharing _fields state.py, debug.py, trace.py, console.py, harness.py, replay.py, __main__.py, py.typed tests/ test_fields.py, test_alu.py, test_op.py, test_jump.py, test_host.py, test_reset.py (rung 1) test_disasm_vs_mame.py (rung 0), test_selftests.py (rung 2), 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.** Field extraction and the tables (ALU, P, SRC, DST, DPL, DPHM, BRCH) checked against the DS pages; `disasm.py` checked against MAME's `dasm` of all seven programs (`scripts/mame_dsp_trace.py --dasm`; map MAME's `mov src,dst` to NEC's `MOV @dst,src`; MAME omits uPD96050 bank bits). Every program word decodes; unknown BRCH values fall through. 1. **Directed tests from the DS**, one file per family: every ALU op on both accumulators with the other carry (U14), the flag table (p.25), SGN (U4), the multiplier (U3), @KLR/@KLM (U5), DPL/DPHM wrap (U6), RPDEC wrap (U9), SR's write mask (U8), the host port in both modes (U7, U20), reset (C6), the stack depth (C5), the uPD96050 jumps (U11). Each test cites its DS page or names its U/C entry and tier. 2. **The programs' built-in self-tests** in `harness.py`: DSP-1, DSP-1B, DSP-3 `0Fh`/`1Fh`/`2Fh`; DSP-4 `13h`/`14h`; ST011 `F1h`/`F2h`. Read each command's expected result from the program and fullsnes; the data ROM transfer must return the image word for word; the version words are `0100h`, `0101h`, `0300h`, `0400h`. 3. **(Optional, follow-up) HLE command comparison**: random and recorded DSP-1 command streams against snes9x/bsnes HLE results. Needs a build of the HLE code, run as a closed box. 4. **MAME replay** (`replay.py`, mame-oracle.md "The replay"): Pilotwings frames 1,800-1,829 first (71 % of the DSP-1 program), then Super Mario Kart, SD Gundam GX, Exhaust Heat II; extend `mame_dsp_trace.py` to press buttons (a Lua `ioport` field) to reach Dungeon Master's, Top Gear 3000's and Morita Shougi's DSP work. Classify every divergence by C-entry; record the deepest stack each window reaches (C5). 5. **(Follow-up) Mesen2 replay**, per instruction, on the same host streams. 6. **Through snes-python milestone 5**: the board's acceptance (DSP-1 games' frames vs Mesen2), the Pilotwings DSP-1/DSP-1B demo difference. ### The minimal harness (`harness.py`) The core plus a scripted host: write command and parameter bytes through `write_dr` when RQM = 1, read results through `read_dr`, poll `read_sr`; the ST01x RAM as words; stop on a cycle budget or on reaching the program's idle `JRQM $`. It also replays a MAME `dsp.log.gz` (rung 4). No S-CPU, no cartridge map, no clock ratio. ### Tooling at the family bar `state.py` (`CPUState`: pc, rp, dp, sp, stack, k, l, m, n, a, b, flaga, flagb, tr, trb, sr, dr, si, so; flags as named booleans), `disasm.py` (NEC mnemonics: `OP`/`RT` lines as `ALU ACC,P` plus `MOV @DST,SRC`, `DPINC`, `M5`, `RPDEC`, `RET`; JP and LD as in the DS), `debug.py` (`DebugSession`: breakpoints on PC, watchpoints on DR/SR host accesses and on data RAM words), `trace.py` (JSONL, one record per instruction with the registers, and one per host access), `console.py`, `py.typed`, and `python -m upd7725_python IMAGE [--model upd96050] [--command HEX ...] [--max-instructions N] [--trace out.jsonl]` running the harness and printing the results the host read. ## Decided by the user, 2026-11-05: every recommendation below taken Aubrey took all eleven as recommended ("Take all, build it"). Going public after rung 2 still waits for Aubrey's word, as for the other cores. The items keep their reasoning for the build session: 1. **One core for both chips.** Recommend **yes**: one class and a frozen `Model` (PC/RP/DP widths, stack depth 4/8, RAM 256/2,048, data ROM 1,024/2,048, program 2,048/16,384, whether HJMP/HCALL/JMPSO and NA bank bits exist), as ares does with its `Revision`. The uPD96050 is a uPD77C25 "souped up" (byuu); a second class would duplicate everything for six differences. 2. **Names.** Repository `upd7725-python` (decided). Recommend import package **`upd7725_python`** (the sm83/spc700 pattern) and class **`UPD7725`** with models `UPD7725_MODEL` and `UPD96050_MODEL`. Alternative: a neutral class name such as `NecDsp`, which reads worse beside the chip names the boards use. 3. **How the host port is exposed.** Recommend **methods on the core** (`read_sr`, `read_dr`, `write_dr`, `ram`), the board catching the DSP up before each call, as ares does (`cpu.synchronize` in `NECDSP::read`). Alternative: the core owns a clock and a `sync(master_time)`; that puts the cartridge oscillator in the core, which it is not. 4. **The contested defaults, as a group** (each a class attribute so a replay can flip it): C1 **carry chain** (ares; DS meaning) rather than MAME's comparison; C2 **the emulators' S1/OV1 formula**, fullsnes' rule as the alternative; C3 **flags from the ALU, accumulator from the move** (the DSP-4 code relies on it); C4 **no bit reversal of SO/SI** (the ST011's jump tables); C5 **stack 4 / 8** (DS, fullsnes); C6 **reset per the DS** (RP = 3FFh), power-on registers zero; C9 **the DS widths**; C14/C15 **read before write**. Recommend accepting all of them. 5. **RQM latency (U19, C13).** Recommend the core applies a host access between two instructions with no extra delay (every emulator; MAME's stamps agree), and the board keeps a one-instruction delay as an option to try if a game's timing ever disagrees with the DS chart. 6. **INT input.** Recommend **none**: no SNES cartridge drives INT, no program sets EI, and "modern over compat" argues against an unused hook; add it (with C7's DS behaviour) if a second board needs it. 7. **Image loader in the core.** Recommend **yes**, `images.load_image` accepting MAME's PROM layout (10 KB / 68 KB), raw big-endian, and the `.program.rom` + `.data.rom` pair (fullsnes' "Newer Files"), by fullsnes' `JRQM` rule: the PROM layout is the chip's own (DS p.27), and both snes-python and any other consumer need it. 8. **The `JRQM $` skip.** 79 % of the DSP-1's instructions in the Pilotwings window were that one-instruction wait. Mesen2 skips it exactly (read from source under the old rule; a closed box since 2026-11-19). Recommend **not in the reference core** (speed is the `-fast` fork's job; the board is allowed to run below real time); the board may stop stepping the DSP while `pc` is a known wait loop and RQM is 1 only if Aubrey prefers that to a slower board. 9. **Disassembler syntax.** Recommend **NEC's** (the DS's mnemonics and `@DST` order) for `disasm.py`, with the MAME mapping only inside the rung-0 test. 10. **Local vs GitHub.** Recommend local until rung 2 passes, then **public** like the other cores (history audited first). 11. **Rungs 3 and 5** (HLE comparison, Mesen2 replay): recommend a follow-up session; rungs 0-2 and 4 already cover the instruction set, the programs' own checks and real game streams.