# What sh4-python can take from sh2-python [sh2-python](../../sh2-python) is the family's SH-2 (SH7604), rungs 0-4 passed, with sh2-rust and sh2-rust-fast below it. Three things were named in the brief: the instruction overlap, the generated decode table, and the handler-subset lint (sh2-python branch `handler-subset`, commit `c486758` "Name the handler subset and lint it (J2)", with `f50de25` on top; not merged into `main` at `8824075` as of 2026-11-26). The family's rule decides the form: **never copy a file between repositories; depend on the pinned sibling, or write it anew from the sources.** ## 1. The instruction overlap All 142 SH-2 forms keep their encoding on the SH-4 (isa.md). Of those, the register-to-register and plain load/store semantics are the same text in both manuals (ADD, ADDC, ADDV, SUB*, NEG*, AND/OR/XOR/NOT/TST, the shifts and rotates, DIV0S/DIV0U/DIV1, DMULS/DMULU/MUL/MULS/MULU, EXT*, SWAP*, XTRCT, CMP*, MOV forms, MOVA, MOVT, DT, CLRT/SETT, CLRMAC, NOP, the branches' arithmetic). The ones that change are listed in isa.md (TRAPA, RTE, SLEEP, LDC/STC SR and VBR, everything that touches memory, TAS.B). Options: - **(a) Import sh2-python's handler mixins** (`_arith.py`, `_logic.py`, `_shift.py`, ...) into the SH-4 class through a pinned dependency. Saves perhaps 100 small methods. Costs: the SH-4 core's correctness would depend on another chip's file layout and its private bus methods (`_read8`, `_tas8`, `_enter`), a fix for one chip could silently change the other, and readers of sh4-python would have to read sh2-python to see what `ADDC` does. The x-rust tier would inherit the coupling. - **(b) Write the SH-4 handlers anew from SM section 9**, in the same style and the same subset, and use sh2-python as a **differential detector**: for the shared, memory-free forms, run both cores' handlers on the same random register states and require identical results. A cheap test that catches transcription slips in both directions (an SH-2 bug found this way goes upstream to sh2-python, as the family's "fixes start upstream" rule says). - **(c) Extract a shared `superh-isa` core** that both import. A new repository, a refactor of a finished, gated core, and an interface (the bus methods, exception entry) that the two chips do not share. Not justified by about 100 three-line methods. **Recommendation: (b)** (open decision 3). The overlap's value is as a judge, not as code to share. ## 2. The generated decode table sh2-python's method: a hand-checked transcription of PM table A.50 (`tests/pm_a50.py`, extracted by `scripts/extract_pm_tables.py`), a generator (`scripts/gen_decode.py`) that adds the handler name, the pipeline class from PM table 7.2 and the defining section, and a test that regenerates `_decode.py` and checks that every one of the 65,536 encodings decodes to at most one form. For the SH-4 the method transfers whole; the data does not: - input: SM section 7's code tables (234 forms) joined row for row with SM table 8.3 (234 rows: group, issue rate, latency, pattern, lock stage/start/cycles); - key: (opcode, FPSCR.PR, FPSCR.SZ), not the opcode alone (isa.md); - the uniqueness test runs over 2^18 keys, and also checks that every SH-2 row's pattern appears with the same mnemonic (the overlap above, as a test); - C1 (SM's FMOV XDn typo) is the kind of slip the join catches. The generator is written anew (it reads different tables, emits different columns); sh2-python's is the model to read, not a file to copy. ## 3. The handler subset and its lint The subset (sh2-python `handler-subset:docs/handler-subset.md`): one handler per decode row, signature `(self, op)`, named state only, bus access only through named bus methods, one exception-entry primitive, integer arithmetic only, no loops, no floats, no dynamic dispatch, no hidden state; `tests/test_handler_subset.py` checks it on the syntax tree and proves it can fail with seeded faults. The JIT proposal (decisions J2/J3) asks every CPU core to keep such a subset so a cached interpreter or a generated Rust interpreter can read handlers as data. What the SH-4 needs beyond the SH-2's subset: - **Floating point without floats.** The SH-2 subset forbids floats, and the SH-4 should keep that rule: FPU registers are 32-bit integers (bit patterns), and arithmetic goes through pure integer helpers (a soft-float in the subset: unpack, operate on integer significands, round once by FPSCR.RM, pack, set cause bits). This is also what bit exactness needs (fpu.md: double rounding, FMAC's single rounding, FIPR/FTRV), and what a Rust transcription can reproduce exactly. - **Mode-dependent rows.** A handler belongs to one (opcode, PR, SZ) row, so it never branches on PR or SZ itself; the decoder picks the row. - **More named state**: banks (selected through one accessor, since SR.RB and SR.MD pick the bank), SSR/SPC/SGR/DBR, FPSCR, FPUL, FR/XF. - **More bus methods**: translated reads and writes (the MMU in the method, not in the handler), `pref`, `ocbi`/`ocbp`/`ocbwb`, `movca`, the SQ write path, `tas8`. - **Timing stays out of handlers**, as in the SH-2 subset: the issue model of timing.md reads the decode row's group, latency and lock columns. - **PyPy's 80-attribute limit** (family memory): the SH-4's state is larger than the SH-2's (sh2-python's chip already holds 91 attributes, past the limit); keep the register file in arrays and the modules as separate objects from the start. The lint is written anew for the SH-4's names (the SH-2 lint hard-codes sh2-python's state and methods), keeping the same structure: walk each handler's syntax tree, allow only the subset, check effects against the row's class, prove failure with seeded faults. **Recommendation**: adopt the subset from the first handler (open decision 4), extended as above, with the lint at rung 0, as vr4300-python's brief recommends for the VR4300. ## 4. What else carries over - The Hitachi house style both manuals share (section-cited docstrings, `M32`, `_s32`). - The tooling shape: `debug.py`, `trace.py`, `console.py`, `python -m sh4_python`, `ChipState` that round-trips into a fresh chip (the sh2-rust lesson, 2026-11-25). - The on-chip modules are different chips: the SH7750's BSC, DMAC, TMU, INTC, SCIF share names with the SH7604's but not register maps or behaviour; nothing there is reusable. - The MAME oracle recipe's traps (sh2-python docs/mame-oracle.md: `-nodrc`, frame-count exits from Lua) apply unchanged (docs/mame-oracle.md here).