Metadata-Version: 2.5 Name: upd7725-python Version: 0.1.0.dev0 Summary: Readable, pure-Python NEC uPD7725/uPD96050 DSP core (the SNES DSP-1..4 and ST010/ST011), instruction-exact Author: alewman License: MIT License Copyright (c) 2026 alewman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License-File: LICENSE Classifier: Development Status :: 3 - Alpha Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Software Development :: Libraries Classifier: Topic :: System :: Emulators Requires-Python: >=3.11 Provides-Extra: dev Requires-Dist: pytest>=8; extra == 'dev' Requires-Dist: ruff==0.16.8; extra == 'dev' Description-Content-Type: text/markdown # upd7725-python A readable, pure-Python **NEC uPD7725 / uPD96050** DSP core, instruction-exact: the 16-bit fixed-point signal processor inside the Super Nintendo's DSP-1, DSP-1A, DSP-1B, DSP-2, DSP-3 and DSP-4 (the uPD7725: 2K x 24-bit program ROM, 1K x 16-bit data ROM, 256 words of RAM) and Seta's ST010 and ST011 (the uPD96050: 16K program words, 2K data ROM words, 2K words of battery-backed RAM). Its first user is the board `snes-python` (milestone 5). **Status (2026-11-05): core built; rungs 0, 1, 2 and 4 of [docs/validation.md](docs/validation.md) passed** (decode against MAME for every word of the seven programs; directed tests from NEC's data sheet; the programs' own self-tests; MAME 0.285 replays of seven games, one window per program). Rungs 3 and 5 are a follow-up session. No dependencies, CPython and PyPy (Python 3.11+). Correct, not fast. Local git only. **There is no hardware-captured data, test ROM or die model for this chip**; NEC's data sheets document the uPD7725, nothing documents the uPD96050, and the judges are the data sheets, the programs' own self-test commands, and emulator-tier detectors. [docs/claims.md](docs/claims.md) says how strongly each behaviour is claimed; [docs/validation.md](docs/validation.md) ranks the oracles and records the results. ```python from upd7725_python import UPD7725, UPD96050_MODEL, load_image, model_for program, data_rom = load_image("sns_dsp1leg.zip") # PROM layout, raw big-endian, or a bsnes pair dsp = UPD7725(program, data_rom) # model=UPD96050_MODEL for the ST010/ST011 dsp.reset() # DS (25): PC, flags, SR = 0, RP = 3FFh dsp.run(1000) # one instruction = one input clock; no wait-loop skipping dsp.read_sr() # host, A0 = 1: SR bits 15-8, no side effect dsp.write_dr(0x2F) # host, A0 = 0: one byte (16-bit mode: low byte, then high) dsp.read_dr() # host, A0 = 0: one byte; DRS/RQM as the data sheet says dsp.ram # data RAM words (the ST010/ST011's battery save) state = dsp.capture_state() # registers and RAM; restore_state(state) ``` The contested behaviours (C1-C16 in [docs/undocumented-behavior.md](docs/undocumented-behavior.md)) are class attributes (`CARRY_BY_COMPARISON`, `S1_OV1_RULE`, `ALU_NOP_WHEN_MOVE_TO_ACC`, `SERIAL_BIT_REVERSE`, `RESET_SETS_RP`, `DRS_READS_ZERO_IN_8BIT_MODE`, `DPL_CLEARS_DP_BITS_10_8`) and model fields (stack depth, widths), so a comparison flips one without editing code. Tooling, at the family's bar: `CPUState`, `disasm` (NEC syntax), `DebugSession` (PC breakpoints, data RAM watchpoints, host-access records), JSONL traces, a command debugger that can also act as the host, the minimal `harness`, the MAME `replay`, and: ```sh python -m upd7725_python sns_dsp1leg.zip --command 2F/0=1 --command 1F/0=1024 python -m upd7725_python sns_dsp4leg.zip --word --command 14=1 --trace run.jsonl python -m upd7725_python sns_dsp1leg.zip --debug -c "break 4" -c continue -c "host write 0x2F" python -m upd7725_python.replay tests/mame_traces/pilotwings-1800 ``` ## Setting up ```sh python3 -m venv .venv && .venv/bin/pip install -e '.[dev]' pypy3.11 -m venv .venv-pypy && .venv-pypy/bin/pip install -e '.[dev]' .venv-pypy/bin/python -m pytest -m "not slow" # the quick loop nice -n 19 .venv-pypy/bin/python -m pytest -m slow # the MAME replays (traces needed) ``` ## Wiring it into a board (snes-python milestone 5) The core owns the chip; the board owns the clock, the address decode and the files. - **Clock**: the DSP runs from the cartridge's own oscillator, one instruction per clock (C10, fullsnes "External Oscillators"): DSP-1..4 at **7.6 MHz** = exactly **1672/4725 instructions per master clock** (NTSC 21.477 MHz); ST010 11 MHz (22 MHz crystal halved), ST011 15 MHz. Keep it as a second clock domain like the APU's and the SA-1's: accumulate master clocks, and **before every DR/SR/RAM access run the DSP up to the access's master time** (`run(target - done)`), as bsnes (`synchronizeCoprocessors`) and Mesen2 (`Run()` in `Read`/`Write`) do. The access then falls between two instructions (decision 5: no extra RQM latency in the core; a one-instruction delay is the board's option, U19/C13). MAME clocks 8, 10 and 15 MHz; use those only to compare against MAME. - **Address decode** (fullsnes "DSP Mapping"; MAME `snes.cpp` l.1218-1262, `bus/snes/upd.cpp`), A0 = 0 is DR, A0 = 1 is SR; SR writes are ignored (DS (20) "Inhibit"); everything mirrors into banks 80-FF: - DSP-1/1A/1B LoROM 1 MB (SHVC-1B0N, Pilotwings; MAME `lorom_dsp*`): banks **30-3F (fullsnes) / 20-3F (MAME)**, `$8000-$FFFF`, **A14** selects: DR `$8000-$BFFF`, SR `$C000-$FFFF`. DSP-2 (1B5B) and DSP-3 (1B3B) the same at 20-3F; DSP-4 (Top Gear 3000) at 30-3F (MAME `SNES_DSP4`). - LoROM 2 MB boards (SHVC-2B3B): banks 60-6F, `$0000-$7FFF`, A14: DR `$0000-$3FFF`, SR `$4000-$7FFF`. - DSP-1/1B HiROM (Super Mario Kart; MAME `hirom_dsp1leg`): banks 00-1F (fullsnes; MAD-2 boards 00-0F), `$6000-$7FFF`, **A12**: DR `$6000-$6FFF`, SR `$7000-$7FFF`. - ST010/ST011: DR `$60:0000`, SR `$60:0001` (A0; MAME accepts 60-67:0000-3FFF), and the **data RAM at `$68-$6F:0000-$0FFF`** (MAME mirrors to `$7FFF`) as bytes: even address = low byte of `dsp.ram[address >> 1 & 0x7FF]`, odd = high byte (U21; `Harness.read_ram_byte`/`write_ram_byte` show the split). Save that RAM as the battery file (`dsp.ram`, 2,048 words). - **The program image**: BYOR from MAME's device sets (`sns_dsp1leg.zip` ... `sns_seta11leg.zip`) or a bsnes `.program.rom` + `.data.rom` pair, read with `load_image(path[, data])`; pick `UPD96050_MODEL` for the ST01x (`model_for(program)`). The header does not say which DSP-n a cartridge needs (chipset `$03-$05` DSP, `$F6` + subtype `$01` ST01x): use the software list (MAME's slot names) or a title table; DSP-1 vs DSP-1B matters (the Pilotwings `28h` demo glitch is the DSP-1's). - **Power-on**: construct, then `reset()` (the DS requires a reset after power-on); the console's reset line resets the DSP again (`reset()` keeps RAM and every other register). ## What is fetched Nothing third-party is committed. `scripts/fetch_references.py` fills `reference/` from `scripts/pinned_files.json` (NEC's 1997 uPD77C25 data sheet, NEC's 1992 DSP data book, fullsnes; by SHA-256) and `scripts/pinned_repos.json` (bsnes, ares, MAME 0.285, Mesen2, snes9x at full commit SHAs, sparse). A pin whose local copy elsewhere on this machine is at the pinned hash or revision is satisfied in place: nothing is cloned or copied. The DSP programs are **bring your own**: MAME's device sets read in place from a MAME `ROMs (bios-devices)` directory (tests: `UPD7725_ROMS`; they skip when absent). Never commit a program image, ROM or trace. ## Scripts | Script | What | | --- | --- | | `fetch_references.py` | fetch, verify, or find in place the pinned documents and source trees | | `survey_programs.py` | the seven programs: formats, SHA-1 against MAME's, instruction mix, every contested field combination | | `mame_dsp_trace.py` | MAME 0.285's per-instruction DSP trace and the host's DR/SR/RAM accesses (`--host-only`, `--dasm`, `--press`, `--snapshots`, `--list-inputs`) | They need `git`, `curl`, `7z` and MAME 0.285 at `/usr/games/mame` (rung 0 also uses its `unidasm`). Run anything that reads `reference/` or a ROM set with `python -I`. No sudo. ## Documents [docs/README.md](docs/README.md) is the index. MIT licensed ([LICENSE](LICENSE)).