# The instruction set and its decode ## The source of the decode table SM section 7 (tables 7.3-7.11, "Fixed-Point Transfer" through "Floating-Point Control") lists every form with its operation, its 16-bit code pattern, whether it is privileged and its effect on T. SM table 8.3 ("Execution Cycles") gives each form its issue group, issue rate, latency, execution pattern (figure 8.2's numbers) and lock stages. These two tables are to sh4-python what PM table A.50 and table 7.2 are to sh2-python: the input of a generator (`scripts/gen_decode.py`, to be written) that emits `_decode.py`, one row per form, the page cited per row. HM section 7 repeats the code tables (HM 7.x) and is the cross-check: where SM and HM differ, the row records both (C1 below). A first parse this phase (a throwaway script over `reference/text/sh4-software-manual.txt`, matching lines that end in a 16-character code pattern; not committed) found **234 forms** in SM section 7; SM table 8.3 numbers its rows 1-234, so the two tables agree on the count. The generator must re-derive the count from a deliberate transcription (the sh2-python method: a hand-checked table module, then the generator), not from a regex over `pdftotext` output: two-line rows and the layout of the FPU tables are where a parse like this one goes wrong. ## The decode depends on mode bits Unlike the SH-2's, the SH-4's decode is not a function of the opcode alone (SM 6, 7): - **FPSCR.PR** selects single or double precision for the same encodings: `FADD FRm,FRn` (1111nnnnmmmm0000) is `FADD DRm,DRn` when PR=1 (the DRn form's pattern, 1111nnn0mmm00000, is a subset). Likewise FSUB, FMUL, FDIV, FCMP, FSQRT, FABS, FNEG, FLOAT, FTRC, and the conversions FCNVSD/FCNVDS exist only with PR=1. - **FPSCR.SZ** selects single moves or pair moves for the FMOV encodings: `FMOV FRm,FRn` (1111nnnnmmmm1100) moves a pair (DRm/XDm to DRn/XDn) when SZ=1; the memory forms move 32 or 64 bits. - **FPSCR.FR** selects which bank is FR and which is XF (it changes the register file mapping, not the decode). - **SR.MD** decides whether a privileged instruction runs or raises a general illegal instruction exception; **SR.FD** whether an FPU instruction runs or raises an FPU disable exception (general or slot form, table 5.2 codes 800h and 820h). So the generated table is keyed by (opcode, PR, SZ): 2^18 entries, or a 16-bit table whose FPU rows carry a PR/SZ sub-table. The SingleStepTests corpus is organised the same way: 233 files named `_sz_pr

.json.bin` (206 at sz0/pr0, 16 at sz1/pr0, 11 at sz0/pr1), 500 cases each. Combinations the manual leaves undefined (PR=1 with SZ=1; FIPR, FTRV, FMAC, FSRRA-like forms with PR=1) are U3 in the handoff brief. ## Overlap with the SH-2 (sh2-python's `_decode.py`) Compared by code pattern against sh2-python's 142 generated forms (2026-11-26, sh2-python `main` at `8824075`): - **138 SH-2 forms have an identical code pattern** in SM section 7, and the other 4 (`BRAF`, `BSRF`, `JMP @`, `JSR @`) differ only in the register field's letter (PM writes `mmmm`, SM writes `nnnn`): **all 142 SH-2 encodings mean the same instruction on the SH-4**. - **92 forms are SH-4 only** (SH-3 and SH-4 additions): LDTLB, CLRS/SETS, the banked register moves (`LDC/STC Rm_BANK`, 16 forms with their `.L` variants), SSR/SPC/SGR/DBR moves, PREF, OCBI, OCBP, OCBWB, MOVCA.L, SHAD/SHLD, and the FPU (FPUL/FPSCR moves and 60 or so floating-point forms). - Same encoding is not same semantics. The SH-2 forms whose behaviour changes on the SH-4: `TRAPA` (writes TRA, vectors through VBR+100h with code 160h instead of the SH-2's vector table); `RTE` (restores SR from SSR and PC from SPC, not from the stack); `SLEEP` (the SH-4's power-down modes, HM 9); `LDC/STC SR` (the new MD, RB, BL, FD bits, privileged); `LDC/STC GBR, VBR` (VBR privileged); every memory access (translated by the MMU, cached by the SH-4's caches, address errors by the SH-4's rules); `MAC.W`, `MAC.L` (the S bit's saturation is the same rule; to confirm against SM 9); `TAS.B` (on the SH-4 a read-modify-write that purges the O-cache line: SM 9, to read in full). - Timing shares nothing: the SH-2's pipeline classes (PM table 7.2) and sh2-python's stage scheduler do not apply; SM table 8.3 replaces them. What this means for reuse is in [reuse-sh2.md](reuse-sh2.md). ## Families and their corners (to read in full before rung 1) - **Delay slots**: the SH-2 rules (a branch in a slot is a slot illegal instruction; PC- relative forms in a slot use the branch target's PC) carry over (SM 7, "delayed branch"). New: an FPU instruction in a slot with SR.FD=1 is the *slot* FPU disable exception (820h). - **Banked registers**: with SR.MD=1 and SR.RB=1, R0-R7 name BANK1; exceptions set RB=1 and MD=1, so handlers run on BANK1 (SM 2). `LDC Rm,Rn_BANK` reaches the other bank. - **PREF**: to the store-queue area it starts an SQ burst; elsewhere a cache line fill (HM 4.3.9, 4.7.3). It is in the LS group and does not stall. - **OCBI / OCBP / OCBWB / MOVCA.L**: the cache-control instructions; MOVCA.L allocates a line without fetching it (HM 4.3). Games use MOVCA.L and OCBWB to stream into VRAM and RAM; they make cache state visible to software. - **SHAD / SHLD**: dynamic shifts, SH-3 additions (the SH-2 lacks them). - **FSCA and FSRRA are SH-4A instructions** (not in the SM tables for the SH-4). Some emulators implement them for the Dreamcast; this core will not unless a judge shows the SH7091 decodes them (U4). ## Contradictions found in the instruction tables - C1. SM table 7.10 prints `FMOV @(R0,Rm),DRn` twice, once with the code `1111nnn1mmmm0110`; HM's table (and SM's own section 9 and appendix tables) give that code as `FMOV @(R0,Rm),XDn`. A transcription error in SM section 7: the generator takes HM's reading and cites both.