# Handoff: build `cps2-python`, the Capcom CP System II board ## Context you are inheriting The family's shape is **chips are cores, boards compose cores; never copy code between repositories; a non-CPU chip gets its own repository when a second family emulator uses it**. Python is the readable reference; correct, not fast; PyPy must work (floor 3.11). This board runs the **original encrypted sets with their key files**, as the hardware does, and also the Phoenix (dead-board) sets. Phase 0 (this directory, 2026-11-11 and 2026-11-12) wrote no emulator code. Read, in order: 1. [start-here.md](start-here.md): scope, machine shape, clocks, performance. 2. [memory-map.md](memory-map.md): which board holds what, both CPU maps, interrupts, wait states. 3. [encryption.md](encryption.md): the cipher, the key file, Phoenix sets, rung 1's test vectors. 4. [video.md](video.md): what CPS-2 keeps from CPS-1, every difference, the sharing decision. 5. [qsound.md](qsound.md), [eeprom.md](eeprom.md), [inputs.md](inputs.md), [sockets.md](sockets.md). 6. [census.md](census.md), [validation.md](validation.md), [mame-oracle.md](mame-oracle.md). Then `python -I scripts/fetch_references.py --verify` (7 repositories, 1 file; about 300 MB, git-ignored) and `python -I scripts/census.py`. ## Decided - **QSound is the real DSP program `dl-1425.bin` on `dsp16-python`, low level** (Aubrey, 2026-11-11). No high-level model as a goal; an HLE could only ever be a proven-equal speed option. - From the task: package **`cps2`**; repository `cps2-python`; coordinate with `cps1-python` and `dsp16-python`, never write to them. - Family rules: BYO ROMs, keys and EEPROM images, read in place; never commit them. ## Scope The whole CPS-2 machine: B-board (68000 in DL-1525 with the opcode cipher; DL-1727 registers; DL-1827 object RAM, banking, interrupts and wait states; DL-1927/2027 graphics ROM mangling; program, graphics, Z80 and sample ROMs; EEPROM) and A-board (CPS-A-01 and CPS-B-21 tile layers, palette and timing through the shared video package; the object frame buffer and final mix; Z80 at 8 MHz; QSound through the shared device; I/O; volume). Machines are rows of data built from MAME's set list (323 sets, 40 parents). Out of scope until asked: link play between two boards (`ssf2tb` network), the CN9 key-programming path as anything but a test, the 8051 bootleg `gigaman2`. ## Proposed names and the import boundary | Piece | Where | Name | | --- | --- | --- | | Board, scheduler, machine table, memory decode, I/O, object engine and mixer, cipher, wait states | **cps2-python** | package `cps2`: `CPS2Board`, `Machine` (rows), `cipher.decrypt_word(word, word_address, key)` and `cipher.Key.from_file(bytes)`, `objects.ObjectEngine` | | 68000 | `m68000-python`, pin **`3029f56`** (HEAD; tag `v0.1.0` = `f557abf` plus two documentation commits) | `M68000CPU(..., function_codes=True, acknowledge=...)` | | Z80 | `z80-python`, pin **`65e7f08`** (`v0.4.0` + 9 commits incl. "RESET clears I and R") | as megadrive-python uses it | | DSP16A | `dsp16-python` (Phase 0 unfinished on 2026-11-12) | `dsp16` (proposed there) | | QSound glue | `qsound-python` pinning dsp16-python (open decision 3) | `qsound` | | CPS-A-01 / CPS-B-21 | `cps-video-python`, built first as `cps1/video/` in cps1-python and moved out when this board reaches M3 (open decision 1) | `cpsvideo` (`CPSA`, `CPSB`, `CPSBConfig`) | | 93C46 | open decision 4 | | Import-boundary tests from M0: `cps2` imports the cores and shared chips only through their public API; nothing in `cps2.cipher` imports the board; the object engine does not import the shared video package (it hands pixels and priorities to the mixer). ## What the board needs from its cores (checked, no change needed) - **m68000-python** already separates opcode fetches from data: with `function_codes=True` every bus call gets `fc=`; instruction words, extension words, immediates and PC-relative operands use fc 2/6 (program); the reset fetch of SSP/PC is fc 6; exception vectors are fc 5 (data). That is MAME's split exactly ([encryption.md](encryption.md)). The board returns the deciphered word for fc 2/6 inside the key's range, the ROM word otherwise. **No hook is needed.** If a later finding showed DL-1525 deciphers only instruction-stream fetches and not PC-relative data (U2), the request to the core would be: "add an access kind distinguishing instruction-stream fetches (opcode, extension, immediate) from PC-relative operand reads, both with fc 2/6" (the core's callbacks would carry it as a keyword, off by default). - `set_ipl(level)` for IPL1/IPL2 (vblank = 2, raster = 4, both = 6); `acknowledge(level)` returning `AUTOVECTOR` and **clearing both lines** (MAME `irqack_r`); `step_clocks()` and per-access timing for the board's wait states (the core models none). - z80-python: plain Z80, IM 1 interrupt held until acknowledged, reset line from `804040` bit 3. The Z80 bus request for the shared RAM (U9) is not modelled by MAME; if the board ever models it, it needs BUSRQ/BUSACK on the core: check the core's API then. ## Milestones (rungs), each with its judge and exit test | # | Rung | Judge | Exit | | --- | --- | --- | --- | | **M0** | Skeleton: machine table generated from `reference/mame-listxml/cps2.xml` as data; ROM loader (non-merged zip in place, word swap, `ROM_LOAD64_WORD`, graphics unshuffle per 2 MiB); key decode; sockets with fakes ([sockets.md](sockets.md)); import-boundary tests | MAME `-listxml` CRCs; the `cps2 decrypt` lines | all 323 sets load with every CRC matching; for `spf2t`, `spf2tu`, `vsav2` the decoded key halves and range equal MAME's logged line; the 175 live keys all give word 7 = `4000`, word 8 = `0900` | | **M1** | **The cipher** (`cps2.cipher`, pure functions, 65,536-entry subkey table) | Phoenix plaintext (tier 1 by provenance) + MAME's opcodes image (tier 5) | (a) bit-exact against MAME's `saveo` image over the whole encrypted range for `spf2tu` and `vsav2` and three more sets chosen for ranges `0x200000` and `0x800000` (MAME's extra word at `upper` excepted, U3); (b) `scripts/phoenix_pair.py`-style check: on every unpatched word of at least 10 Phoenix pairs, `cipher(E)` equals the Phoenix word wherever the Phoenix word differs from `E`; report the counts per pair | | **M2** | **CPU, bus and boot**: 68000 with fc split; the full 68000 map; work and graphics RAM; IN0-IN2; `804040`; DL-1727 REGA; object RAM banks (storage only); vblank IRQ; Z80 + shared RAM + a QSound fake (ready always set); EEPROM; **timing profile `mame`** (16 MHz x 0.7375, no wait states, no bus stealing) | MAME instruction stream (tier 5) | `spf2t` boots: 68000 and Z80 instruction streams (PC, SR, D0/D1/A0/A7) and every write to `804xxx`, `400000`, `618000` match MAME for 2 emulated seconds, `0 unexplained` | | **M3** | **Tile layers, palette, timing** through the shared CPS-A/CPS-B package; graphics ROM mapping (D8); black background (D9) | the package's own certification (tier 1 docs); MAME pictures at palette-index level | `spf2t` and `ssf2` test screens (cross-hatch, colour bars) and title screens equal MAME's at palette index for 60 frames, objects off | | **M4** | **Objects, priority, raster IRQ**: latch at vblank, list walk, blocks, flips, offsets, MAME's mask priority model and draw order ([video.md](video.md)), raster counters | MAME (tier 5); jtcps2 as the second model where they differ (C4, C5) | the smoke list ([census.md](census.md)) reaches gameplay in attract mode with pictures equal to MAME at palette index, or each difference named and traced to C4/C5/U6 | | **M5** | **QSound, low level**: the shared device on `dsp16-python`, the Z80 interface, the volume register; catch-up scheduling of the DSP whenever the Z80 reads `D007` or writes `D002` | the qsound repo's certification (sample-for-sample against MAME's LLE); the games' sound test | `spf2t`'s sound test plays: the Z80 command stream equals MAME's (same `D000-D002` writes in the same order per frame); the audio stream equals what the qsound repo's referee produces from the same command stream; **wall time per emulated second measured and recorded** | | **M6** | **Service mode, EEPROM, inputs, special controllers**: every port layout as data; Puzz Loop 2 paddles; Eco Fighters dials; extra RAM socket | the games' own test menus (tier 3) | each layout's input test shows every bit; `spf2t` settings changed in the menu survive a power cycle through the EEPROM image; the RAM/ROM check screens pass | | **M7** | **Timing profile `board`**: one wait per access in `000000-4FFFFF` and `800000-FFFFFF` (DL-1827 per jtcps2), CPS-A bus stealing per line, and the Z80 shared-RAM interaction if modelled | jtcps2's DL-1827 reading (tier 4 from tier 1); MAME's known problems as qualitative tests | Giga Wing's attract mode stays in sync with its music, its sprites are not a frame late, and Progear's slowdown is reduced relative to profile `mame`, or each is explained; profile `mame` still passes M2 unchanged | | **M8** | **Smoke list, then all 40 parents, then the 323 sets** (a full sweep only when the board might be ready) | MAME pictures and instruction streams | the smoke list in attract mode, then 40 parents to gameplay; failures kept as the next smoke list | | **M9** (optional) | Watchdog (U5), the CN9 key-load protocol as a test, link play | none above tier 3 | as specified then | A rung is closed when its exit holds and its undecidable items are recorded as "provisional, tier N" in the certification record. ## Unknowns (U) | # | Unknown | Best source / tier | Status | | --- | --- | --- | --- | | U1 | Which Phoenix sets are CPS-2 Shock hardware dumps (pre-2007) and which were made later from MAME's cipher (then not independent) | set histories; none pinned | **undecidable here**; M1's Phoenix check is tier 1 only for the former | | U2 | Whether DL-1525 deciphers PC-relative operand reads (MAME does: all program-FC reads) | none (DL-1525 unreversed) | follow MAME; provisional tier 5 | | U3 | Is the range limit inclusive (MAME deciphers the word at `upper`) | none | exclusive by the formula; a flag for MAME-lockstep | | U4 | Deciphering of fetches outside ROM for keys with range `0x800000` | none | follow the key's range everywhere (MAME: ROM only); provisional | | U5 | The watchdog: period, matching rule, effect of failure | MAME header prose | off by default (M9) | | U6 | Object engine: per-line limits, frame-buffer double buffering and latch moment, overlap rule between objects (MAME: highest entry wins, hidden pixels still mask), the unknown registers `400000/2/6`, the low byte of the object-register base (key word 7 gives bits 8-23: `$400000` live, `$FFFF00` dead; MAME's dead map puts them at `$FFFFF0`) | MAME, jtcps2 (tier 4-5) | provisional | | U7 | Raster counter semantics (C5); the "unknown behaviour of the MSB" | die doc incomplete | provisional | | U8 | Exact wait states and stolen cycles (DL-1525 internals, CPS-A requests on CPS-2) | DL-1827 sheets via jtcps2 (tier 4) | M7 | | U9 | Stall of the Z80 when the 68000 side uses the shared RAM, and its effect on CPS-A bus requests | die doc prose | not modelled until M7 | | U10 | Which of the three 93C46 positions a game uses | MAME drawings (unsourced) | one EEPROM per machine; irrelevant until sockets | | U11 | EEPROM write busy time | 93C46 data sheet (not pinned) | pin a data sheet before M6 | | U12 | Eco Fighters dial signalling | MAME reconstruction | provisional | | U13 | `804030` volume codes vs the TC9185P | MAME table | data | | U14 | QSound sample period (1,248 vs about 1,245 machine cycles) and the sample ROM's low byte (C6, C7) | dsp16 / qsound repos | theirs to settle | | U15 | Power-on contents of RAMs and registers; what reads return while DL-1727 REGA is still set | die doc (REGA mutes the bus) | zeros by default, an option for random | | U16 | On a dead board, whether code run from `FF0000-FFFFFF` is deciphered with the all-`FF` key, and what the all-`FF` key's cipher does | MAME header ("s-boxes chosen to use an empty key for the dead board") | compute it at M1 (the cipher with key `FF..FF` over `FF0000-FFFFFF`) and record | | U17 | Python speed of the whole board | estimates only ([qsound.md](qsound.md)) | measure at M2 and M5 | | U18 | Background colour outside all layers (MAME forces black, "apparently") | MAME comment | follow MAME; provisional | | U19 | The real frame rate (59.637 Hz by arithmetic; 59.6388 Hz in MAME's notes, unsourced) | none measured | arithmetic | ## Conflicts between sources (C) jtcps2's side of each row was read from its GPL HDL in Phase 0, before the 2026-11-14 rule; it is HYPOTHESIS (for review) and its source is not fetched since 2026-11-24 (Aubrey). | # | Conflict | Sides | Decision | | --- | --- | --- | --- | | C1 | Where the 68000 is | MAME's overview prose: base board; die work, MAME's own B-board drawing, Eduardo Cruz: B-board (DL-1525) | B-board (no effect on emulation) | | C2 | Where the keys are | common wording "DL-1827 holds the keys"; die: DL-1827 only passes CN9 signals, keys are in DL-1525 | DL-1525 | | C3 | CPU speed | MAME: 16 MHz scaled to 11.8 MHz ("HACK"); jtcps2 from DL-1827: 16 MHz with one wait per external access in two ranges; MAME's notes: "timing loops show 11.8 on average" | two timing profiles: `mame` for lockstep (M2), `board` as the reference default once M7 passes | | C4 | Object priority | MAME: masks over all three scroll layers from `400004`; jtcps2: object vs the topmost scroll pixel's layer nibble | MAME first (it is the lockstep detector); jtcps2 recorded; decide by games at M4 | | C5 | Raster IRQ | MAME: a line counter reaching 0, then a delay of `2 x (+0E)` pixels; jtcps2: column counter and a line counter both zero, 4 MHz count; die: "still unknown" | MAME first; provisional | | C6 | QSound sample period | MAME's HLE and header 1,248 machine cycles (24,038 Hz); jtcps2 3,746 ticks of 90 MHz (24,019 Hz); dsp16 brief: the program loops in 1,245 (24,096 Hz) by the manual and MAME's DSP core | the DSP program decides: run it (low level) | | C7 | Sample ROM low byte | MAME header "assuming byte smearing"; MAME code "bit 0-7 tied to ground" | the code (zero) until a board trace says otherwise | | C8 | MAME's range end | formula gives an exclusive end; the loop compares inclusively | exclusive; MAME-compatible flag for lockstep | | C9 | "Decrypted set" | common usage: a decrypted ROM; measured: a Phoenix image merges deciphered code, raw data and patches | documented in encryption.md; M1 uses both checks | | C10 | EEPROM bit labels | MAME's comment numbers bits within the high byte and leaves CS unlabelled; its port map puts CS at bit 14 | the port map | ## Speed (frank) The 68000 at 16 MHz (about twice the Mega Drive's), an 8 MHz Z80, and a DSP16A executing about 24 million instructions per second. megadrive-python reaches about a third of real time on PyPy; dsp16-python's own estimate for the DSP alone is 0.3-0.8 x real time on PyPy and 2-3 % on CPython. **Expect the whole CPS-2 board with low-level QSound at roughly 1/10 to 1/30 of real time on PyPy and around 1/100 on CPython** (estimates, to be measured at M2 and M5). A silent QSound fake keeps the video rungs (M2-M4) at CPU speed. The Z80 busy-waits on the DSP before each command, so the DSP must be caught up whenever the Z80 touches `D002`/`D007`; batching the DSP far ahead is not possible without changing behaviour. ## What was not verified in Phase 0 (candour) - No emulator code ran; the cipher was not reimplemented here (M1's job); the comparisons used MAME's opcodes image. - `cps2-reverse` was read as documents: its KiCad sheets were not opened, the DL-1827 sheets that jtcps2 cites were not read gate by gate, `scroll.md`, `sram.md`, `stars.md` were not read. - jtcps2's object HDL (`jtcps2_obj*.v`), `jtcps2_main.v`, `jtcps2_keyload.v` beyond its header, the FBNeo driver and `cps2-utils`' cipher were not read; FBNeo was not built. - `cps2comm.cpp` (network) and MAME's per-game DIP/debug notes were not read. - The ArcadeHacker PDF guide was read as text by the first session; its diagrams were not viewed. - Rule slip: one `sed -i` on a scratch Lua file in a git-ignored test directory (2026-11-12), since moved to `trash/2026-11-12/`; one `pkill -f` on the session's own job (2026-11-11). ## Open decisions for Aubrey (each with a recommendation) **Decided by Aubrey on 2026-11-12** (recorded after a walk through all fourteen Phase 0 briefs): - The Python package is `cps2_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: **shared CPS video, staged** (option C), as cps1-python's brief. Decision 3: **`qsound-python`** pinning dsp16-python. Decision 4: the 93Cxx EEPROM as **its own small shared core** (this brief's recommendation). - QSound runs the real DSP program on dsp16-python (low level), 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, a private GitHub remote (going public later is a separate decision). 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. 1. **How CPS-1 and CPS-2 share the CPS-A/CPS-B code** ([video.md](video.md)). Options: (A) cps2-python pins cps1-python and imports its video subpackage; (B) one repository with two machines; (C) a shared chip repository both boards pin. **Recommend C, staged, the same as cps1-python's brief (its open decision 1)**: build `cps1/video/` (`CPSA`, `CPSB`, `CPSBConfig`, palette) inside cps1-python behind an import-boundary test; when this board reaches M3, move it with its history to **`cps-video-python`** (package `cpsvideo`) and both boards pin it. Until then cps2-python does not import cps1-python. Reasons: two consumers from day one (the chip rule); CPS-2 uses only the tile/palette/timing half plus the B-21 raster counters; pinning a whole CPS-1 board would drag in YM2151, MSM6295 and Kabuki for nothing. Fallback A if Aubrey wants fewer repositories (same interface, so A to C stays mechanical); reject B. The CPS-2 object engine and mixer stay in cps2-python either way. **The two briefs are reconciled on this point**; the only open part is Aubrey's yes. 2. **Name and package**: `cps2-python`, package `cps2`, class `CPS2Board`. *Recommend* as proposed. 3. **QSound glue home**: `qsound-python` pinning `dsp16-python` (two consumers: cps1-python's CPS-1.5 Q-Sound boards and this one), as the dsp16 Phase 0 leans. *Recommend* it. 4. **93C46 home**: users are cps2-python (16-bit), cps1-python (the CPS-1.5 Q-Sound boards in 8-bit wiring, Pang! 3 in 16-bit), likely cps3 and other arcade boards later. Both briefs agree: **not inside either board**. cps1-python's brief suggests a small module inside `qsound-python`; *this brief recommends* a tiny `eeprom93c-python` (93Cxx family, both organisations), because Pang! 3 has a 93C46 and no QSound, so tying the EEPROM to the QSound repository couples two unrelated chips. Low stakes: three lines and a 128-byte image. 5. **The cipher's home**: only CPS-2 uses it. *Recommend* inside the board (`cps2.cipher`), pure functions with no board imports. 6. **Encrypted sets as primary input**: run the original ROM + key, deciphering on program-FC fetches, with the deciphered words computed lazily from a 65,536-entry subkey table and cached (deciphering 4 MiB up front costs seconds on PyPy and much longer on CPython: measure at M1). Phoenix sets run on the same path with the all-`FF` key. *Recommend* it; never ship or require a pre-decrypted image. 7. **Timing profiles**: `mame` (11.8 MHz effective, no waits) for lockstep, `board` (16 MHz + the DL-1827 wait states + bus stealing) as the reference default after M7. *Recommend* both, with the profile recorded in every trace and certification row. 8. **Watchdog**: *Recommend* off by default, an option at M9; MAME has never emulated it and no game is known to depend on its absence or presence. 9. **Dead-board map**: derive the object-register base from key word 7 (bits 8-23) rather than a second memory map, and record U6's low-byte question. *Recommend* it (one mechanism for live and dead keys). 10. **Local or public**: *Recommend* local through M4, then a private GitHub remote under the "remotes as needed" rule; public once M6 passes, with CI PyPy-only on main and PRs. 11. **Pins**: m68000-python `3029f56`, z80-python `65e7f08`; dsp16-python and the qsound and video packages pinned when they exist. *Recommend*; repin deliberately per milestone with the gate rerun. 12. **`trash/` (2.3 GB)**: holds the first session's full clone of `jwestfall69/mad` (`trash/mad-full-clone`) and a partial `cps2-reverse` clone; the pinned sparse copies replace them. *Recommend* Aubrey deletes `trash/` (agents may not).