# Handoff: build `dsp16-python`, the AT&T DSP16A core that runs QSound ## 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 commit by the boards that use it. This core's purpose is Capcom's QSound: the DL-1425 is a mask-programmed DSP16A, and **Aubrey has decided that QSound is emulated by running the real DL-1425 program on this core** (low-level). That program on this core is the goal of the ladder below and the boards' reference sound path. The consumers are `cps1-python` (the six CPS-1.5 QSound parents) and `cps2-python` (322 machines), both in Phase 0 in parallel (read-only for you); the 47 ZN machines have no family board yet. A board that finds a corner of this core raises it **on the core** (an issue or a PR here), never as a workaround in the board. Models for the code (read-only): - `upd7725-python` (the family repository): **the closest sibling**, a DSP core with a host port, class attributes for contested behaviours (`RESET_SETS_RP` with its C number in the docstring), `state.py`, `trace.py` (JSONL, `first_divergence`), `debug.py`, `replay.py` (MAME lockstep), `console.py`, `harness.py`, `__main__.py`. Copy its shape, never its files. - `ssp1601-python`: the other DSP (a Mega Drive cartridge's SVP), MAME's cycle counts and a lockstep replay of seven games. - `z80-python`: the readability contract (one instruction family per module, one mnemonic per handler docstring, the source and tier as a comment on the line that encodes each fact), the tooling bar, the `docs/` layout. - `huc6280-python` at `b6c5f22`: the shape of this Phase 0. The rules that bind this work: oracle tiers (a low tier detects, never judges; state the tier of every claim); **Python floor 3.11** (the newest PyPy supports), CPython and PyPy both green; reference cores are correct, not fast (a `-rust`/`-fast` fork is later work); modern forms over shims; no dangerous-looking shell one-liners; CI minimal (PyPy only, main + PRs, cancel in progress) if a GitHub repository is ever made; never copy a file between repositories; the user has no CPS hardware, so no rung needs any; run Python that reads `reference/` with `-I`. **This repository is local git only until Aubrey says otherwise.** ### Read, in this order, before writing code 1. `README.md`. 2. `docs/start-here.md`: the part, the three units, memory spaces, reset, interrupts, the cache, what the core owns. 3. `docs/instruction-set.md`: formats, field tables, statement rules, what the program uses. 4. `docs/timing.md`: cycles per class, the cache rules, the measured agreement with MAME, the sample period. 5. `docs/peripherals.md`: PIO and SIO. 6. `docs/undocumented-behavior.md`: C1-C10, U1-U21. 7. `docs/qsound.md`: what the boards need (host port, register file, sample ROM, output), the census, the speed estimate. 8. `docs/validation.md`, `docs/mame-oracle.md`. 9. `upd7725-python`'s `src/upd7725_python/cpu.py`, `trace.py`, `replay.py`. Then `python -I scripts/fetch_references.py --verify` (or without `--verify` to fetch), `python -I scripts/ocr_reference.py` (text copies of the scans), and `python -I scripts/mame_lle_trace.py` followed by `python -I scripts/cycle_audit.py tests/mame_traces/tone/dsp.trace` to see the detector run. ## Scope - **In**: the DSP16A as AT&T documents it: XAAU, YAAU, DAU, the cache, the interrupt logic, 4,096 words of internal ROM (given by the caller), 2,048 words of RAM, the external program space, the parallel unit, the serial unit (output and registers fully; input passively; multiprocessor mode raising), `icall`, reset. Directed tests for every instruction class, used by the program or not. - **Out**: QSound's glue, the sample ROM, the DAC, the volume chip, the host's latches (qsound.md; where they live is open decision 2); the DSP16 (2K ROM, 512 RAM, 9-bit YAAU) except as a possible later subclass (open decision 3); the DSP1610 family (timers, power-down, bit manipulation; none of it exists on the DSP16A); a high-level QSound (see "Speed"). ## Names (open decision 1) Distribution `dsp16-python`, import package `dsp16`, class `DSP16A`. The core knows nothing about QSound: no import of anything QSound, no QSound constant in `src/dsp16/`. ## The import boundary | Piece | Owner | Why | | --- | --- | --- | | instruction set, the three units, flags, cache, `icall`, reset | core | the CPU | | interrupt sources, `pioc` enables and status, the one-instruction enable latency, INT sampling | core | on the die, CPU behaviour | | PIO: `pdx0`/`pdx1`, PSEL, strobes, the latent read, the PIDS shadow (U7), S/C mode | core | on the die | | SIO: `sdx`, `sioc`, `srta`, `tdms`, the active clock and load generators, OBE/OSE/IBF, SADD | core | on the die; event-driven (timing.md) | | internal ROM contents | caller | the core takes 4,096 words; it never opens a ROM file | | external program space (AB/RB) | caller, through a callback | QSound's sample ROM and latch | | what PSEL/PODS/PIDS/SADD/OCK mean on a board | board / QSound device | qsound.md | ## The callback contract (proposed; the build session may refine, recording why) ```python from dsp16 import DSP16A dsp = DSP16A( rom, # Sequence[int]: up to 4,096 16-bit words (internal ROM) read_external, # (address: int) -> int: a program-space read at 0x1000-0xFFFF # (EXM low) or anywhere (EXM high); 16-bit result pio_read, # (psel: int) -> int: an active-mode PIDS strobe; the word on PDB pio_write, # (psel: int, value: int) -> None: an active-mode PODS strobe serial_word, # (word: int, bits: int, psel: int, address: int) -> None: # a word begins shifting out of DO (the OLD edge); # psel = PSEL at that edge, address = srta's transmit field *, exm=False, ) dsp.reset() # RSTB: pc = 0, re = 0, pioc = 0x0008, sioc = tdms = 0 cycles = dsp.step() # one instruction, or one interrupt entry; machine cycles used dsp.set_int(level) # the INT pin; sampled before each interruptible instruction (U11) dsp.write_pdx(value) # passive mode: a host PIDS write (sets PIDS status) dsp.read_pdx() # passive mode: a host PODS read (sets PODS status) dsp.ab # the address bus as held after the last external access (U19) dsp.psel # the PSEL pin dsp.cycles # machine cycles since reset (the serial unit's time base) ``` - **One `step()` is one instruction** (a cached pass instruction is one step; the `if CON` qualifier and its branch are one step, as MAME traces them) and returns machine cycles by the manual's rules (timing.md). The core keeps a cycle counter; the serial unit's OCK/OLD edges are computed from it, and any edge that falls inside an instruction is processed, in cycle order, at the end of that instruction with PSEL as it stood at the edge's cycle (state the sub-instruction placement chosen, as a U entry). - Callbacks are plain callables with plain integers; no generators, so a `dsp16-rust` and `-fast` forks can transcribe it. - Contested behaviours are **class attributes** named after their entry (`ALIGN_COPY_CLEARS_LOW_BITS` for C3, `ALIGN_AT_ALU_INPUT` for C9, `Y_TRANSFER_SATURATES` for U5, `MODULO_ON_PLUS_J` for U8, `RAM_ADDRESS_MASK` for U9, ...), the upd7725-python pattern. - `heads`/`tails` raise `NotImplementedError("U3")` until a source turns up; so do multiprocessor mode and reserved encodings (U13 for `do` misuse). - External fetch (executing from external memory, C7) goes through `read_external`, as the manual allows. ## The ladder One rung per commit, in order; each ends with its exit test passing on CPython and PyPy and a results section added to `docs/validation.md`. **Rung 0: manual to tables.** `scripts/manual_tables.py` (or modules under `src/dsp16/`) holds the T, F1, F2, CON, R, B, Y, Z tables and the cycle classes as data, each line citing its manual page. Exit: a test decodes all 65,536 words, every word maps to exactly one format or "reserved", and the per-class cycle table reproduces the manual's worked example (81 and 43 cycles, 4-6). **Rung 1: directed tests per instruction (tier 2 by construction).** For every T class, every F1 and F2, every CON (counter increments included, U20), every R register (read, write, sign and zero extension, write-only registers), every Y and Z mode with and without modulo (`re`/`rb`), saturation and the CLR bits, ALIGN (C3, C9), the 12-bit XAAU adder and page wrap, `pi` shadowing, the interrupt rules (non-interruptible classes, the enable latency, no nesting, `icall`, `ireturn` clearing INT status), the PIO latent read and strobe spacing, the SIO word sequence with OBE/OSE and SADD. Tests written from the manual's text, each citing its page; a small **assembler** in the package (`dsp16.asm`, the syntax of chapter 3) makes them readable. Then, as a detector: assemble jtdsp16's 34 `ver/top/tests/*.asm` and compare the final registers with their `.out` files (tier 4; differences named, not fixed by fiat). **Conditions (wave 2, 2026-11-19):** the programs are GPL-3.0 test inputs, used as closed-box data: they are fed to this repository's assembler and run **from the git-ignored `reference/`**, never committed, copied into `tests/` or quoted; their `.out` values are the HDL's own output and **never overrule the manual**. Exit: every T class and field value has at least one test; the jtdsp16 comparison is recorded. **Rung 2: the disassembler against MAME's.** `dsp16.disasm` prints the whole program 0x0000-0x0FFF; a script normalises both texts and compares with MAME's `program.dasm` (3,810 lines, `scripts/mame_lle_trace.py --dasm`). Exit: zero differences after normalisation, every normalisation rule listed (spacing, hex case, MAME's `move`/`set`/`au` prefixes). **Rung 3: the program, instruction by instruction, against MAME's LLE.** A replay harness (upd7725-python's `replay.py` is the model) runs the program from reset on the core with the host commands MAME delivered, and compares every instruction's registers with MAME's trace (the `tracelog` format in `scripts/mame_lle_trace.py`; `pc` from the disassembly line, mame-oracle.md). Commands are injected so the interrupt is taken at the same instruction as in MAME (the program takes at most one per sample, at 0x0542). Exit: the `tone` and `busy` windows (and a trace from reset, which the build session records: the 0x0288 initialisation) agree register for register and cycle for cycle; every divergence is named with its U or C entry. **Rung 4: the real program, sample for sample, against two detectors.** - *Stimulus*: recorded host writes from real games. jtdsp16's `ver/fw/xmvsf.qs` (383 KB) and `spf2t_b1.qs` (three decimal columns per line, a format to be decoded **from the data files themselves**, never by reading jtdsp16's `QSCmd` reader; QSound VGM rips through MAME are preferred), and QSound VGM rips played through `scripts/mame_lle_trace.py --vgm` (pin any VGM fetched, never commit it). Plus a synthetic stimulus per register of qsound.md's table, including each 0xE3 state (0x0288, 0x061A, 0x0039, 0x004F, 0x000C, 0x000F, 0x0000) and the ADPCM voices. - *Detector A, MAME's LLE*: its output words come straight from the trace (the accumulator before `sdx = a0` at 0x059C (left) and 0x05D7 (right), saturated as the core reads it). Exit: **every output word equal** over at least ten emulated seconds per stimulus, or each difference named with its U/C entry. - *Detector B, the HLE*: libvgm's `qsound_ctr.c` (BSD-3-Clause, the same code as MAME's `qsound_hle`) built as a small stand-alone C referee that takes the same register writes and prints raw samples (a named referee, small: allowed). Compare by sample index (its clock assumes 1,248 cycles, C1). Exit: the agreement measured and every class of difference named (expected: equal if superctr's port is exact; nobody has published that check). - Settle U17 (the quad-mode loop length) and record the measured periods. **Rung 5: a second referee (estimate only).** jtdsp16 under Verilator or Icarus, **as a closed box**: a harness written here drives its top-level ports and prints them; its HDL and scripts are not opened, including when a comparison fails (GPL-3.0; wave-2 rule of 2026-11-19). Its benches already compare the HDL against MAME traces, so it adds an HDL's reading where MAME and the manual are silent (U5, U6, U8 and the like), not independence from MAME's choices. Cost: installing a simulator (no sudo: a static OSS CAD Suite bundle, as huc6280-python's build_referees did) and a few hours; one emulated second is 60 M HDL clocks, minutes to tens of minutes. Do it only if rung 4 leaves differences that need a third reading. **Rung 6: tooling at the family bar.** `state.py` (`CPUState`: every register, RAM, cache contents and pointers, interrupt and peripheral state; save and restore), `trace.py` (JSONL, version 1, the upd7725-python schema plus `cycles`, external reads, PIO strobes and serial words; `first_divergence`), `debug.py` (breakpoints on pc, watchpoints on RAM and on external addresses, a step record with RAM accesses), `console.py` (a command debugger), `harness.py` and `python -m dsp16 PROGRAM.bin [--max-cycles N] [--trace out.jsonl] [--commands FILE]`, `disasm.py`, `asm.py`. A MAME-format text writer for the trace, so a core trace diffs directly against `dsp.trace`. ## Speed, frankly The program executes about 1,003 instructions and 1,245 machine cycles per output sample at 24,096 samples per second: **24.1 million instructions per emulated second**. A readable pure-Python interpreter in the family's style does roughly 0.4-0.8 million such instructions per second on CPython (0.02-0.03 of real time: a 3-minute tune takes one to two hours) and perhaps 8-20 million on PyPy (0.3-0.8 of real time). These are estimates from the family's other cores, not measurements; measure at rung 3 and record the figures. MAME's own interpreter ran the LLE at about 4 % of real time in the traced run (debugger on, machine loaded). For a board this means QSound dominates the frame budget: the boards are reference, sub-real-time is expected, speed is a `-rust`/`-fast` fork's job. (Footnote, by Aubrey's decision: a high-level QSound, a few hundred multiply-adds per sample, is at most a later speed option, admitted only when proven sample-equal to this low-level core over rung 4's stimuli.) ## Decided - QSound = the real DL-1425 program on this core, low-level (Aubrey, 2026-11-11, relayed). The ladder's goal and the boards' reference path. - The program image is read in place from MAME's `qsound.zip` (`dl-1425.bin`, SHA-1 `555f50fe...`); words 0x0000-0x0FFF; nothing ROM-like is committed. ## Open decisions for Aubrey **Decided by Aubrey on 2026-11-12** (recorded after a walk through all fourteen Phase 0 briefs): - The Python package is `dsp16_python` (family rule set the same day: a repository `x-python` always imports as `x_python`); read any other package name in these documents as that. - Decision 1: package **`dsp16_python`** (not `dsp16`), class `DSP16A`. Decision 2: **`qsound-python`**, a separate repository pinning this core. Decision 4: 24,096 Hz. - The low-level route (the real DL-1425 program on this core) is the reference sound path, as decided on 2026-11-11. - Referees built from die-derived material are built just before the rung that needs them, not up front. - Outside contacts proposed below are **deferred until the core or board exists**; Aubrey sends them himself, from a draft. - Remote: local through Phase 0; after the first passing rung or milestone, public like the other CPU cores, after the usual history audit. Where a recommendation below names a different point, this replaces it. - Every other recommendation below is **accepted as written** unless a line here says otherwise. Items 1-2 block the first line of code. 1. **Names.** Distribution `dsp16-python`, package `dsp16`, class `DSP16A`. The two sibling DSP cores use `_python` packages (`upd7725_python`, `ssp1601_python`); the CPU cores use bare names (`huc6280`, `w65c816`). **Recommend `dsp16`** (a CPU core, the family's CPU pattern) and the class `DSP16A` (the part QSound uses; see 3). 2. **Where the QSound device lives.** Options: (a) a `qsound` module in this repository; (b) a separate `qsound-python` chip repository that pins this core and holds the host latch, the sample-address latch with its one-read bank latency, the serial-to-DAC capture and the register-level test stimuli; (c) inside each board. The chip rule: a non-CPU chip used by two family boards gets its own repository; QSound has two now (`cps1-python`, `cps2-python`) and a third later (a ZN board). (a) breaks "the core knows nothing about QSound" and would put QSound's rung 4 inside the CPU's certification; (c) duplicates the glue in two boards. **Recommend (b), `qsound-python`**, small (the glue is a few hundred lines), its contract written from `docs/qsound.md`; this core's rung 4 harness can live there, or here as a test-only dependency on recorded traces (no QSound code in `src/`). Until it exists, the boards should not write their own glue. 3. **One class or two for DSP16 and DSP16A.** Nothing in the family uses the DSP16 (no MAME driver either). **Recommend `DSP16A` only**, with the size and width differences (ROM 2K/4K, RAM 512/2K, YAAU 9/16 bits, EXM rules) kept as constants so a `DSP16` subclass is a later small change. 4. **The sample period (C1).** By the manual's rules and MAME's own core the program's loop is 1,245 machine cycles (24,096.39 Hz), not the 1,248 (24,038.46 Hz) every HLE uses. **Recommend: the core charges the manual's cycles, the boards take the rate from the core's output, and the docs of both boards say 24,096 Hz**, with C1 cited; revisit only if a hardware capture of a CPS-2 DAC stream turns up. 5. **The serial unit's grain.** **Recommend event-driven output words with the cycle and PSEL of the OLD edge** (exact for QSound, cheap), with a per-bit pin interface only if a later board needs one. 6. **Defaults where no judge exists** (validation.md "What no judge decides"): U1, U2, U4, U5, U6, U8, U9, U10, U11, U13, U20, C3, C9 take MAME's readings, each a named class attribute flagged emulator-derived, and U3, multiprocessor mode and reserved encodings raise. **Recommend: adopt.** 7. **Assembler in the package.** No AT&T assembler survives; directed tests read far better in assembly. **Recommend a small `dsp16.asm`** (chapter 3 syntax), tested by round trip against the disassembler over all 65,536 words. 8. **Local or public.** **Recommend local until rung 3 passes, then public** (a CPU core, MIT, no third-party file committed), as the other chip cores. 9. **Rust and `-fast` forks.** Not now. Revisit with rung 3's speed figures and the CPS boards' frame budgets; QSound will dominate them.