# What CPS-3 needs from sh2-python `../sh2-python` is the family's SH7604: the SH-2 CPU with cache, BSC, INTC, DMAC, DIVU, FRT, WDT, SCI and power-down as modules. Rungs 0-4 pass; it stopped for review before rung 5 (peripherals against the manual and referees). It was read here, not modified. **Pin: `8755c3ae85ed9013a33f910019650475ccc69374`** (`main`, 2026-11-05, "Merge pull request #1 from alewman/sdram-bsc"). `sega32x-python` pins this core too. Package `sh2_python`, version `0.1.0.dev0`, Python >= 3.11. Its contract suits this board: `SH7604(read, write)` with `read(address, width, kind)` and `write(address, width, value, kind)` once per external bus cycle, `chip.clock` and `chip.wait` inside the callback, `chip.irl` for the external level, cache hits visible only through `chip.observe`. One fact makes CPS-3 an unusually good client: the core's only hardware-tier evidence **is a CPS-3**. Dunkley's loops were measured on this board's bus, and the core's rung 4 already rebuilds them on a "CPS3-like" harness (BCR1 `0020`, BCR2 `00E8`, WCR `AA57`). ## What the 32X did not exercise | Need | Why CPS-3 needs it | State in the core at the pin | | --- | --- | --- | | **A decrypted-opcode view** | The cartridge CPU XORs words by address; in dead-cart mode only instruction fetches are decrypted, and the mask also applies to code run from the cache data array | **Missing.** See below. This is the one blocking change. | | DMAC reads that bypass decryption | "On the actual CPS3 hardware the SH2 DMA bypasses the encryption" `[mame]` | DMA cycles already carry `kind & DMA`, so the board can tell. Enough. | | Cache as a cache | The BIOS and games run with CCR = `01`; a conflicting load costs a 36-clock line fill; purge order matters `[board]` | Built and judged at rung 4 (the eight purge-order values). | | Two-way mode and the data array as RAM | Code executes from `C0000000-C00003FF` `[mame]`; CCR bit 3 turns ways 0-1 into 2 KiB of RAM `[HM 8]` | `CCR_TW` and the data array are modelled; **executing** from the data array is untested (the 32X does not do it). | | FRT exactly | clock/8, period OCRA + 1, compare flag surviving a counter write, TEMP latch, 3 to 12 clocks per register access, priority 15 `[board]` | FRT is built; these five behaviours are rung 5 material. Dunkley's unrun "timer semantics" tests are the plan. | | INTC with IRL 12 and 10 below an on-chip level 15 | Interrupt order decides the 65-tick IRQ10 band `[board]` | Built; the one-instruction inhibit after `LDC`/`LDS` matters here (`06000DC6`). | | BSC as programmed | CS0 8-bit by mode pins, CS1/CS2 16-bit, CS3 32-bit, WCR waits, idle cycles, WAIT sampling, the write buffer | `area0_width` is a constructor argument; waits and the write buffer are built. The board adds per-device WAIT states to reach DP's table 3. | | A bus cycle that never ends | A read past work RAM hangs the board with interrupts blocked `[board]` | Not expressible: `chip.wait` is a finite count. Low priority (U29). | | WDT | The -4 board has no external watchdog chip; whether the games arm the on-chip WDT was not checked | Built per HM; unverified (U23). | | DIVU | Used by compiled game code | Built; contested rows are rung 5. | | DMAC channels | The palette, character and sprite-list "DMA" are **GLL1's, on the main board**, not the SH-2's. The on-chip DMAC is used for memory copies; MAME hooks it only to undo decryption | Auto-request burst and cycle-steal are built; DREQ is not needed (no board device requests). | | 25 MHz, one chip | The 32X runs two at 23 MHz | Nothing to change. | | IRL auto-vectors | 3rd Strike's table puts IRQ10 on vector 69, VBlank (12) on 70 and a level-14 handler on 71: auto-vectors, ICR.VECMD = 0 `[rom]` | Built (HM table 5.3); ICR resets to 0, auto-vector mode, in `intc.py`. Nothing to change | | Fetches from work RAM | No game is known to execute from work RAM (DP 8.4); MAME has no opcode view there | Nothing to change; the filter sees the address (U30) | | Speed | About 8 M instructions/s for real time ([start-here.md](start-here.md)) | **160,000 (CPython) and 252,000 (PyPy) instructions/s measured here at the pin** (2026-11-12); 30 to 60 times too slow | ## The decrypt hook (to raise on sh2-python as an issue or PR) ### What has to be decrypted, by mode From MAME's driver (`cps3_mask`, the `decrypted_opcodes` map, `sh2cache_ram_w`, the DMA kludge callback), the dead-cart report (herzmx) and JTCPS3's cache module, checked against the BIOS images here ([encryption.md](encryption.md)). `mask(a)` is the 32-bit mask of the longword at `a`. | Fetch or read of | Live key (5 games) | Dead cart / key 0 (2nd Impact) | Stock CPU (`cps3boot`) | | --- | --- | --- | --- | | BIOS `00000000-0007FFFF`, CPU data read | `raw ^ mask(a)` | `raw ^ mask(a)` with key 0 | `raw` | | BIOS, instruction fetch | the same as data | the same as data | `raw` | | SIMM 1-2 `06000000-06FFFFFF`, CPU data read | `raw ^ mask(a)` | **`raw`** | `raw` | | SIMM 1-2, instruction fetch | the same as data | **`raw ^ mask(a)`** with key 0 | `raw` | | Cache data array `C0000000-C00003FF`, instruction fetch | **`stored ^ mask(C0000000 + offset)`** | the same, key 0 | `stored` | | Cache data array, data read | `stored` | `stored` | `stored` | | Work RAM and everything else, either | plain (MAME has no opcode view of work RAM at all: unknown, U30) | | | | SH-2 DMAC read from the BIOS or SIMMs | `raw` (DMA bypasses the decryption) | `raw` | `raw` | Two rows decide the shape of the hook: 1. **Data and fetches differ at the same address** in dead-cart mode (SIMMs) and in every mode for the cache data array. The board sees one bus cycle per line fill and cannot tell whether the line will later be executed or read as data; the same line serves both. And a fetch from the data array, or a cache hit, makes no bus cycle at all. 2. The **code stored in the data array is stored encrypted for its own address**: MAME keeps `stored ^ mask(C0000000 + offset)` as the opcode view, and the BIOS, which reads its own code decrypted, boots that way in MAME. So the decryption sits between the cache (including the data array used as RAM) and the instruction decoder. The live-key rows could be done in the board's `read` callback alone (decrypt every non-DMA cycle in the two ranges; the cache then holds plaintext). The dead-cart rows and the data-array row cannot. ### The request: a fetch filter, applied after the cache, before decode At the pin, every instruction fetch goes through `SH7604._read_fetch(address) -> int` (`src/sh2_python/_core.py`): one 32-bit longword per IF, for partition 0 (cached, through `_cache_read(..., FETCH)`), partition 1 (cache-through bus read with `FETCH | THROUGH`) and partition 6 (`C0000000`, the data array, `cache.get`). `pipeline._fetch_into` and the fallback in `_opcode` both call it. The request: - **`chip.fetch_filter: Callable[[int, int], int] | None`, default `None`**, an attribute like `observe`. - Called **once per fetched longword**, inside `_read_fetch`, on the value returned by the cache, the bus or the data array, before it is kept for decode: `value = filter(address, value)`. - `address` is the **full 32-bit fetch address of the longword, partition bits included** (`06001660`, `26001660` and `C0000040` must be distinguishable: the board keys the data array by `C0000000 + offset`, and the partitions by the external address). - Applied to **instruction fetches only**: not to data reads, not to the vector-table reads made through `_read32`, not to DMAC cycles, not to `observe`, and not to `debug`/`disasm` reads unless they ask for it (a disassembler wants the decrypted view: give `disasm` an option to apply the filter). - **Zero cost when `None`**: one attribute test per IF longword, which already exists for `observe`. - Does not change timing: the filter is a value transform. Whether Capcom's part adds fetch latency is unmeasured (DP 2.4); if it does, that is a separate change. - State: the filter is the board's; save states do not include it. Alternatives considered and rejected: - **A per-address mask callback** (`fetch_mask(address) -> int`, the core XORs). Equivalent for this cipher, which depends only on the address; less general (no other transform) and saves nothing measurable. Acceptable if the core's owner prefers it. - **A separate opcode read callback on the bus** (MAME's `decrypted_opcodes` space). Wrong for a core with a cache: a line filled by a data read and then executed would never reach it, and the data array never reaches the bus. - **Decrypting in the board's `read` by looking at `kind & FETCH`.** Line fills caused by a fetch carry `FETCH | FILL`, but the line then also serves data reads, and data-array fetches and cache hits make no bus cycle. Correct only for live keys with the cache treated as plaintext. The board then does data-side decryption in its `read` callback (live key: BIOS and SIMMs, every cycle without `DMA`; dead: BIOS only) and fetch-side decryption in the filter (dead: SIMM fetches; every mode: data-array fetches; live: nothing else, since the cache already holds plaintext). JTCPS3's SH7604 model (GPL HDL, read in Phase 0, not fetched since 2026-11-24; HYPOTHESIS) does the same thing in the same place: on the cache's CPU-side output, for instruction fetches only, it XORs `mask(fetch address)` onto data-array reads (live and dead) and, in dead mode, onto cached-area fetches whose address is `06xxxxxx` (from the line or from the bus); cache-through fetches (`26xxxxxx`) are not decrypted there. MAME's opcode map covers `06xxxxxx` only. So which partitions a dead cart decrypts is a detail the two detectors leave open (U2). Whether the real chip decrypts on the bus side of the cache (so that a line filled by a data read and then executed would hold plaintext in dead mode) is U2; the filter models the decode-side placement MAME and the dead-cart modders imply, and JTCPS3's cache-module placement. ## Raise on the core, not here - The decrypt hook above. - The PyPy speed (91 instance attributes, past PyPy's 80; the core's own record shows PyPy falling from 2.4 M to about 0.1 M instructions/s when the pipeline scheduler landed; measured here at 252,000 on a short run). - Rung 5 for the FRT behaviours this board depends on. - Executing from the cache data array (a test: load a routine into way 0-1 in two-way mode, jump to `C0000000`).