# sh2-rust-fast > **Lineage:** sh2-python → sh2-rust → **sh2-rust-fast** · > fixes start in sh2-python and flow down by merges · > [family charter](https://github.com/alewman/emulator-lineage/blob/main/docs/charter.md) The speed tier (`x-rust-fast`) of the family's **Hitachi SH7604 (SH-2)** core: a fork of [sh2-rust](../sh2-rust) (remote `upstream`, forked at `fcb9dae`, which transcribes [sh2-python](../sh2-python) at `8824075`), changed only where a change keeps **every bus cycle identical** to sh2-rust and wins on every code layout (`scripts/ab.sh`). What did not pay is in [docs/not-kept.md](docs/not-kept.md); the current numbers are in [docs/status.md](docs/status.md). Hardware behaviour is never changed here: a bug found here goes to sh2-python first, with a repro, and comes down by merges. Nothing here is a hardware claim: "matches sh2-rust" is what this repository proves. The crate is `sh2`. ```rust use sh2::{Bus, BusContext, Options, Sh7604}; struct Ram(Vec); impl Bus for Ram { fn read(&mut self, address: u32, width: u32, _kind: u32, cx: &mut BusContext) -> u32 { cx.wait += 0; // the board's WAIT-pin states for this cycle let a = (address & 0xFFFF) as usize; (0..width / 8).fold(0, |v, i| (v << 8) | self.0[a + i as usize] as u32) } fn write(&mut self, address: u32, width: u32, value: u32, _kind: u32, _cx: &mut BusContext) { let a = (address & 0xFFFF) as usize; for i in 0..width / 8 { self.0[a + i as usize] = (value >> (width - 8 * (i + 1))) as u8; } } } let mut chip = Sh7604::new(Ram(vec![0; 0x10000]), Options::default()); chip.reset(true); // PC and R15 from vectors 0 and 1, through read let states = chip.step(); // one instruction or exception sequence ``` ## What Rust forced (everything else is the reference's names, order and comments) - The reference's mixins are `impl` blocks of one struct, in files of the same names; Python's private `_name` is a private `name`. The dispatch table is a generated match (`src/dispatch.rs`), the decode table generated from the reference's (`src/decode.rs`, `scripts/gen_decode.py`). - The board's two callables are the `Bus` trait; inside a callback the board sees the chip's clock, wait, `cycle_states` and pins through a `BusContext` (copied back after). The optional callables (`observe`, `on_ftoa`, ...) are `Bus` methods; the replaceable `read_onchip`/`write_onchip` are three hook methods. - Modules that reach back into the chip (`chip.intc.update()`, `chip._stall`) are chip methods named after their module (`frt_read8`); the base classes' byte and longword splits are helpers in `src/module.rs`. - Two seams replace methods the reference's tests and replay patch: `free_fetch` (`chip._place`, its "as drawn" figures) and `dmac_run_disabled` (`chip.dmac.run`). - Python's unbounded integers: `i64` clocks and slots, `i128` in the divider. The reference's `debug.py`, `trace.py`, `console.py` and `__main__` are tools, not behaviour, and are not transcribed (the lockstep has its own trace format). ## Certification The fork's gate is sh2-rust's ladder (`scripts/ladder.sh`: rungs 0-5 against sh2-python `8824075`'s cached traces) plus **rung U** (`scripts/upstream.sh`: every scenario family and extra fuzz seeds, and longer MAME prefixes, byte-identical to sh2-rust itself). At `61c7689` (numbers, commands and what is owed in [docs/status.md](docs/status.md)): | Rung | What | Result | | --- | --- | --- | | 0 | every table entry of all 65,536 encodings, 30 classes, 512 on-chip addresses | identical | | 1 | sh2-python's 16,600-case corpus against its expected results, and each case's bus CRC | 16,600 / 16,600; identical | | 2 | four MAME 0.285 32X traces, the first 2,000,000 instructions of each in bus lockstep (the full replays with `FULL=1`, not run in the fork) | 4 / 4 identical | | 3, 4, 5 | the pipeline figures, the hardware-number programs, seeded fuzz and edge scenarios | 268, 168, 114 + 50 identical | | U | sh2-rust `fcb9dae` on the same and more scenarios, and 4 M-instruction MAME prefixes | 900 / 900 and 4 / 4 identical | | 6 | seeded faults: sh2-rust's 111 and 14 on the fork's decode record and plan arrays | 120 / 120 counted caught, 5 equivalent noted | ## Speed One packed decode record per opcode (`67652c5`) is the change that paid: **x1.05-x1.11 natively across three code layouts and x1.116 as wasm32 under Node** over sh2-rust, pinned to one E-core (25-28 → 28-32 M SH-2 instructions/s natively, 18-21 → 21-23 M/s as wasm; on an unpinned P-core about 50-64 M/s natively and 38-45 M/s as wasm). The 32X's pair needs about 11-35 M/s. Removing allocations, inlining the scheduler and a **cached interpreter** were measured and not kept ([docs/not-kept.md](docs/not-kept.md)); the cached interpreter, exact but about 10 % slower, is on the branch `cached-interpreter` with its tests and seeded faults, and [docs/cached-interpreter.md](docs/cached-interpreter.md) is what it means for the JIT proposal: with every fetch a visible bus cycle, a block can save only the decode. ## Running ```sh scripts/ladder.sh # the gate: rungs 0-5 and the speed report scripts/upstream.sh # rung U: against sh2-rust itself scripts/ab.sh [BASE] # a speed claim: BASE against the working tree, three layouts scripts/ab-wasm.sh [BASE] # the same as wasm32 under Node scripts/speed.sh # instructions per second, native and wasm ``` `scripts/env.sh` names the reference (`SH2_PYTHON`: a private clone at the pin in `external/sh2-python-pin` when present, else `../sh2-python`; its PyPy venv) and the MAME traces (`SH2_MAME_TRACES`). Requests to the reference: [docs/requests.md](docs/requests.md). Provenance: [docs/provenance.md](docs/provenance.md). ## Licence MIT (see `LICENSE`, `NOTICE`).