# sh2-rust > **Lineage:** sh2-python → **sh2-rust** · > fixes start in sh2-python and flow down by merges · > [family charter](https://github.com/alewman/emulator-lineage/blob/main/docs/charter.md) A Rust **Hitachi SH7604 (SH-2)** core, the CPU of the 32X and the Saturn with its on-chip cache, BSC (ordinary and synchronous DRAM, refresh, the write buffer), INTC, DMAC, DIVU, FRT, WDT, SCI, UBC and power-down, transcribed module for module from [sh2-python](https://github.com/alewman/sh2-python) at `8824075` and proven equivalent to it: every bus cycle (address, value, direction, width, kind, the state it starts on), every step's states, registers and state, and every pipeline-scheduler event identical. Tier `x-rust` of the charter: "matches the Python" is what this repository proves. How well the Python matches real SH-2s (the manuals, Dunkley's CPS3 measurements, the 32xspd photograph) is the reference's `docs/validation.md`; nothing here is a hardware claim. 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 Against sh2-python `8824075` (rung 6 last run in full at `0d61a66`), the gate `scripts/ladder.sh` (numbers, commands and commits in [docs/status.md](docs/status.md)): | Rung | What | Result | | --- | --- | --- | | 0 | the decode table regenerated; every table entry of all 65,536 encodings, 30 classes, 512 on-chip addresses | identical | | 1 | the reference's 16,600-case corpus natively against its expected results, and each case's bus CRC | 16,600 / 16,600; identical | | 2 | four MAME 0.285 32X traces (129 M instructions) replayed to the end (two also with the cache on); the first 2,000,000 instructions of each in bus lockstep | the reference's counts exactly; identical | | 3 | every PM section 7 figure of the reference's tests (as drawn and real) and its other pipeline programs | 268 scenarios identical (5 of them restore half way into a fresh chip) | | 4 | the write buffer, the CCR purge, 32xspd's loop, 160 DN loops and the purge values | 168 scenarios identical | | 5 | seeded fuzz (every form, every module, SDRAM and refresh, DMA, interrupts, resets, save states) and 43 edge scenarios | 80 fuzz + 20 fresh-chip fuzz + 64 edge scenarios identical (DMA address errors among them) | | 6 | 93 seeded one-line faults (at `0d61a66`), and the repin's 19 new or changed (at `8824075`) | 89 of 89 counted caught (4 equivalent); 19 of 19 | ## Speed (one core, this transcription, not optimised) The reference's harness_speed loop at load ~80 on 32 cores (docs/status.md has the table): **30-47 M SH-2 instructions/s natively** (23-46 M on the 32X's SDRAM bus), **20-27 M as wasm32 under Node**. The 32X's pair needs at most 46 M/s (two CPUs at 23 MHz, one instruction a state at best) and about 11-35 M/s by the bench's states per instruction (docs/status.md): natively one core covers it, in the browser it is at the edge before the rest of the board. Cleverness belongs in a future `sh2-rust-fast` (then a cached interpreter in a turbo tier for the browser, if needed). ## Running ```sh scripts/ladder.sh # the gate: rungs 0-5 and the speed report FAULTS=1 scripts/ladder.sh # and rung 6 scripts/speed.sh # instructions per second, native and wasm ``` `scripts/env.sh` names the reference (`SH2_PYTHON`, default `../sh2-python`, at the pin with 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`).