# z80-rust-fast A fast Rust Z80 instruction core, forked from [z80-rust](https://github.com/alewman/z80-rust) and kept provably equivalent to [z80-python](https://github.com/alewman/z80-python) at every processor boundary: boundary kind, T-states, instruction bytes, and all 28 `CpuState` fields. z80-rust is a module-for-module transcription of the reference, deliberately kept readable beside the Python and deliberately not fast. This repository starts from that transcription (commit `43c5122`, the first commit here that touches `src/`) and changes how the core does things without changing what it does. The conformance ladder is the definition of "what it does": a change that fails a rung is not an optimization, and every change is one commit with the measurement that justifies it. Where it stands (2026-09-08): ZEXALL's 5,764,169,474 instructions in **16.8 s** with `cargo build --release` (344 M instructions/s, a 2.8 GHz Z80) and 15.1 s with the optional PGO build, from 69.3 s as forked on the same measure (60.1 s once code placement is controlled for). Every change is one commit with its measurement, in the table under "Speed", and the whole six-rung ladder passes (table under "Certification"). The same commit runs ZEXALL in 15.7 s, and 14.5 s with PGO, on a Ryzen 7 9800X3D; that machine has its own section under "Speed". The library keeps the crate name `z80_rust`, so a host written for z80-rust (`use z80_rust::{Bus, Z80}`) compiles against this crate unchanged. The `Bus` trait, `Z80`, `step()`, `capture_state()`/`restore_state()`, the lifecycle requests, and `Fault` are the frozen surface; so are the trace producer, the manifests, and the rung scripts. **Merged with z80-rust at z80-python `b62891b` (2026-10-12).** z80-rust's repin (the EI delay fix, RESET clearing I and R, the opt-in CMOS `OUT (C),0`, and `timing::instruction_timing` with its generated table, which the Mega Drive board uses to arbitrate the Z80's bus) was merged into this fork as one merge commit. All seven rungs pass on it, rung 3 included (see "Certification"). The speed numbers below were measured before the merge, which touches no hot path except one comparison in `step()`. ## Certification Pinned oracles, as `docs/conformance.md` in z80-python requires: | What | Version | | --- | --- | | z80-python (reference core and conformance kit) | main at commit `b59c80b`, which was `b62891b` before z80-python's history rewrite (after 0.4.0: the EI delay fix, RESET clearing I and R, the opt-in CMOS `OUT (C),0`, and the access-timing table; the commit megadrive-python pins). Earlier pins: `cab1598`, `530cad3` (now `73dd7ab`, `6c8d0db`) | | Trace schema | version 1 | | SingleStepTests/z80 corpus | revision `ebe1875d48f374bcfd4b505d8eb8ee751568b5f7` | | raxoft/z80test | release 1.2a | | FUSE Z80 core tests | release 1.6.0 (`fuse-1.6.0.tar.gz`, SHA-256 `3a8fedf2…047096`) | **History rewrite (2026-11-19).** z80-python's history was rewritten to drop GPL test data and a document that named the author; this repository's and z80-rust's were rewritten for the author name only. Every hash changed and no tree did, so every result below still holds. Hashes in this README and in `docs/` written before then are the old ones: z80-python `b62891b` is now `b59c80b`; this repository's `c24701d` (the megadrive-rust-fast pin) is now `6c53d14`, `5015d98` (the galaga pin) is now `4c369cc`, and tag `v0.1.0` was recreated on the rewritten commit. Ladder status, each rung reproduced with the script named (`scripts/`) on Linux x86_64 (i9-13900K) with rustc 1.93.1, CPython 3.14.4 and PyPy 7.3.20: | Rung | What | Result | Last run at | Script | | --- | --- | --- | --- | --- | | 1 | The three example manifests diff clean against the reference | `traces are identical` x3 | the `b62891b` merge | `rung1.sh` | | 2 | SingleStepTests, 1,604 files: registers, RAM, port order, T-states, and on every case the kinds and order of the accesses `step()` makes against the access-timing table | `TOTAL: 1604000 passed, 0 failed, 0 not implemented / 1604000 cases` | the `b62891b` merge | `rung2.sh` | | 3 | ZEXALL and ZEXDOC in lockstep against the reference, 116 segments of 50,000,000 records each | `zexall: every segment identical` and `zexdoc: every segment identical`, 5,764,169,474 records each, at the merge (2026-10-13) and before it at `2a5eac5` | the `b62891b` merge | `rung3.sh` | | 4 | z80test natively: `z80full`, `z80ccf`, `z80memptr` | all three `Result: all tests passed.` | the `b62891b` merge | `rung4.sh` | | 5 | The ten interrupt scenarios as manifests with events, plus three for the behaviour `b62891b` changed: `ei-ei-rearms-delay`, `ei-di-leaves-delay-zero`, `reset-clears-i-r` | all thirteen `traces are identical` | the `b62891b` merge | `rung5.sh` | | 6 | FUSE 1.6.0's core test set, 1,356 cases, six explained divergences pinned | `1350 agree, 6 expected divergences, 0 unexpected` | the `b62891b` merge | `rung6.sh` | | 7 | `timing::instruction_timing` answers as the reference's does, for every form the lookup distinguishes | `instruction_timing: 6180 forms, every answer identical` | the `b62891b` merge | `rung7.sh` | Before the merge, rungs 1, 2, 4, 5 and 6 were re-run clean on every commit that touched `src/` or the build profile, and rung 3 passed at `2a5eac5` (every source-level optimization) and at the fork point. CI (`.github/workflows/ci.yml`) reproduces rungs 1, 2, 5, 6 and 7 on every push, plus `cargo fmt --check`, `cargo clippy -D warnings`, and `cargo test`. ### Reproducing ```text scripts/fetch_z80python.sh # z80-python at b62891b into external/, with a venv scripts/fetch_test_vectors.sh # SingleStepTests/z80 at the pinned revision into external/ scripts/rung1.sh scripts/rung2.sh # place zexall.com / zexdoc.com in conformance/zex/ (SHA-256 in conformance/README.md) JOBS=30 scripts/rung3.sh zexall path/to/pypy3 JOBS=30 scripts/rung3.sh zexdoc path/to/pypy3 scripts/rung4.sh path/to/z80test-1.2a # directory holding the .tap files scripts/rung5.sh scripts/fetch_fuse_tests.sh # FUSE 1.6.0 z80/tests into external/ scripts/rung6.sh scripts/bench.sh # the speed number, pinned to one core target/release/z80-bench bench/ix-loop.json # secondary workloads, see bench/README.md scripts/rung7.sh ``` ## Speed The measure is `z80-bench`: a plain `step()` loop over ZEXALL with the `cpm-minimal` traps, no trace records and no state capture, 5,764,169,474 instructions and 46,734,975,782 T-states. `scripts/bench.sh` builds release and runs it pinned to one logical CPU (`CPU`, default 4, a P-core on the i9-13900K; the E-cores are about 1.45x slower on this loop and an unpinned run may land on either). **Code placement matters as much as small optimizations here.** Two builds whose `step()` code was byte-identical differed by 9% (7.50 s against 8.17 s per 1,000,000,000 instructions) because an unrelated function changed size and moved the hot code 0x5e0 bytes. With LLVM's `-align-all-nofallthru-blocks=5` or `=6` (branch-target blocks on 32- or 64-byte boundaries) the same two builds agree within 1-3%, in both directions. So `scripts/bench.sh` builds three layouts, the default and those two, runs each, and prints the minimum; the minimum over layouts is the figure that says whether a change helped, and the default-build figure is kept because it is what `cargo build --release` produces. Run-to-run noise on one binary is about 0.2%. Since the alignment commit the default build is the 64-byte-aligned layout and the sweep is `default`, `noalign` (the flag overridden away, the layout LLVM picks on its own) and `nf5`. Measurements taken while rung 3's 22 PyPy processes were running were 10-30% slower and are not in the table; the rows from `6fb0786` on were taken with those processes paused. | Commit | Change | Default build | Min over layouts | M instructions/s (min) | | --- | --- | --- | --- | --- | | `52192d1` | Baseline: the transcription as forked | 69.3 s | 60.1 s (nf6; 62.3 nf5) | 95.8 | | `d27af51` | Main dispatch: one exhaustive `match` over the opcode byte, prefixes included, instead of the if-chain | 43.0 s | 43.0 s (default; 43.2 nf6, 45.8 nf5) | 134.0 | | `5814b1f` | DD/FD dispatch: the same `match` for the byte after the prefix (`bench/ix-loop.json` 3.86 s to 2.99 s) | 43.3 s | 43.0 s (nf5; 43.6 nf6) | 134.1 | | `4f0dbe7` | ED dispatch: the same `match` for the byte after ED (`bench/ed-loop.json` 2.27 s to 2.04 s) | 47.1 s | 43.2 s (nf6; 43.6 nf5) | 133.3 | | `a19edfc` | Lifecycle requests as one byte: `step()` tests RESET, NMI, INT with one load and takes a cold path only when a line is up | 43.7 s | 40.6 s (nf6; 41.9 nf5) | 141.9 | | `60b843c` | Fetched bytes in an inline 8-byte buffer; a `Vec` only for prefix runs longer than that | 38.6 s | 38.6 s (default; 40.3 nf6, 41.4 nf5) | 149.2 | | `94bd13a` | Dispatch arms expanded to one per opcode with the opcode as a literal argument (`tools/expand_dispatch.py`) | 38.6 s | 37.8 s (nf5; 38.8 nf6) | 152.3 | | `dc1e28b` | `#[inline]` on the conformance host's `Bus` methods and the CP/M trap check, which were real calls from the binaries' crate on every read and every step | 21.3 s | 20.5 s (nf6; 22.3 nf5) | 281.0 | | `653281c` | 8-bit ALU and CB rotate flags composed as one byte from a compile-time S/Z/X/Y/parity table instead of five to seven setter calls | 20.0 s | 19.8 s (nf6 19.75, nf5 19.76) | 291.8 | | `2a5eac5` | The remaining flag writers the same way: ADC/SBC HL, ADD HL/IX/IY, RLCA/RRCA/RLA/RRA, BIT, IN r,(C), RRD/RLD, LD A,I/R (no measurable ZEXALL change) | 20.5 s | 19.7 s (nf6 19.69, nf5 19.72) | 292.8 | | `6fb0786` | Build profile: `lto = "fat"`, `codegen-units = 1` | 17.9 s | 17.0 s (nf6; 17.8 nf5) | 338.8 | | `c5f04b5` | 64-byte branch-target alignment becomes the default build (`.cargo/config.toml`); sweep is now default / noalign / nf5 | 16.8 s | 16.8 s (default; 17.0 nf5, 17.5 noalign) | 343.8 | | (optional) | `scripts/pgo.sh`: profile-guided build trained on ZEXALL and the two loops, output under `target/pgo-use/` | 15.1 s | 15.1 s (one layout) | 382.8 | ### A second machine: Ryzen 7 9800X3D Every number above is from the build machine (i9-13900K). The same commit `85fb856` was re-measured on a Ryzen 7 9800X3D on 2026-09-08 to separate what the core does from what that one machine does. Eight Zen 5 cores, no P/E split, so `CPU` picks a full core either way; Linux x86_64 under WSL2 (Ubuntu 24.04), `scripts/bench.sh` unchanged, `RUNS=2`, CPU 4. Each figure is the minimum over the three layouts, as above: | Build | i9-13900K | 9800X3D | | --- | --- | --- | | `cargo build --release`, rustc 1.93.1 | 16.8 s | 16.28 s (354.1 M instructions/s) | | `cargo build --release`, rustc 1.98.1 | not measured | 15.66 s (368.2 M instructions/s) | | the same, Windows-native (`x86_64-pc-windows-gnu`, `scripts/bench.ps1`) | not applicable | 15.71 s (367.0 M instructions/s) | | `scripts/pgo.sh` | 15.1 s | 14.53 s (396.5 M instructions/s, a 3.22 GHz Z80) | Three things this says: 1. **The layout sensitivity is a property of the build machine, not of the core.** It is the reason `.cargo/config.toml` forces 64-byte branch-target alignment and the reason every commit above is measured under three layouts. On Zen 5 the spread across `default`, `noalign` and `nf5` is 1.6% at rustc 1.93.1 and 1.7% at 1.98.1 -- run-to-run noise -- and the winning layout is a different one in each sweep (`noalign`, then `default`, then `nf5` on Windows). So the alignment flag buys nothing measurable there, at about 6% code size; it stays the default because the i9 is the build machine, where it is worth 4%. 2. **The compiler moves this as much as the CPU does.** rustc 1.93.1 to 1.98.1 is 16.28 s to 15.66 s, 3.8%, against 3.1% for the CPU change with the compiler held at 1.93.1. A cross-machine comparison that does not pin rustc is not measuring the core. 3. **WSL2 costs nothing on this loop.** Windows-native 15.71 s against WSL 15.66 s, 0.3% apart, so the WSL figures stand as Linux figures. PGO is reported from interleaved runs, not from `scripts/pgo.sh`'s own timed run: that one, taken immediately after the optimized build, read 15.75 s, and three default/PGO pairs run alternately afterwards put PGO at 14.53-14.60 s against 15.61-15.87 s for the default build. The secondary workloads at this commit, rustc 1.98.1: `bench/ix-loop.json` 1.279 s (234.6 M instructions/s), `bench/ed-loop.json` 1.072 s (279.9 M instructions/s); the ix and ed figures in "Optimizations" below are from earlier commits on the i9 and are not comparable with these. No rung was reproduced on this machine, so the certification above is unchanged and still rests on the i9 runs. Every bench run there did reproduce the exact 5,764,169,474 instructions and 46,734,975,782 T-states and stop on `cpm_exit`, which says the core did the same work, but that is a consistency check and not a rung: WSL on that machine has no outbound network, so the `fetch_*` scripts and the reference venv cannot be built. ### Profile of the baseline `perf` is not available on the build machine (`perf_event_paranoid` = 4), so `tools/sample-profile.py` samples the instruction pointer of the running bench over ptrace at 2 ms and attributes each sample with `nm` and `addr2line -i`. A full ZEXALL run under it (33,840 samples, 813 distinct addresses) at `52192d1`: | Self time | Innermost inlined frame | What it is | | --- | --- | --- | | 22.2% | `execute_main` | the if-chain over the unprefixed opcode | | 12.8% | `handle_cpm_trap` | the two PC compares in the bench loop (inlined into `main`) | | 10.8% | `step` | reset/NMI/INT checks, `halted`, `ei_delay` | | 8.3% | `z80_bench::main` | the loop itself | | 7.9% | `usize::from(u16)` | the memory index in `read_byte`, i.e. the load and its bounds check | | 3.8% | `read_operand_byte` | | | 3.8% | `ConformanceHost::read_byte` | | | 2.6% | `op_ld_r_r` | | | 2.4% | `decode_and_execute` | the prefix tests before `execute_main` | | 2.1% | `Vec::push` | `note_fetched`, the per-byte trace bookkeeping | | 2.0% | `can_accept_maskable_interrupt` | | | 1.4% each | `pop_word`, `read_pair`, `fetch_byte`, `push_word`, `inc_r` | | By outlined function, `step` holds 43.9% (nearly every handler is inlined into it), `handle_cpm_trap` 12.8%, `read_byte` 11.1%, `main` 8.3%, `read_operand_word` 5.5%, `op_ld_r_r` 2.9%, `alu_a` 2.1%. The flag setters are individually small (`set_h` 0.6%, `set_z` 0.4%) but are spread over every ALU handler. What this says: dispatch shape first (the if-chain and the prefix tests are a quarter of the time), then the per-step lifecycle checks, then the fetched-bytes bookkeeping; the flag computation is diffuse and comes after. ### Optimizations Each row above is one commit; its message states what the profile showed, what changed, and the number before and after. Rungs 1, 2, 4, 5, and 6 were re-run clean on each. 1. **Main dispatch as a jump table.** `execute_main` was the reference's if-chain, up to forty tests per instruction and 22% of self time; it is now one `match` over all 256 opcode bytes with no `_` arm, the CB/ED/ DD/FD prefix tests folded in as arms, each arm calling the same handler with the same argument. `execute_cb` likewise. 69.3 s to 43.0 s in the default build; 60.1 s to 43.0 s with layouts controlled. 2. **DD/FD dispatch as a jump table.** `execute_index_opcode` was a sixty-test if-chain; now one exhaustive `match`. ZEXALL has too few prefixed instructions to move (43.2 s, within noise), so the change is measured on `bench/ix-loop.json`, a loop of five IX instructions and a JR: 3.86 s to 2.99 s, 78 to 100 M instructions/s. 3. **ED dispatch as a jump table.** `execute_ed` was a `match` on the block instructions followed by an if-chain; now one `match` whose `_` arm is the reference's ED no-op rule. `bench/ed-loop.json` (NEG, ADC HL,BC, LD A,I, LD A,R, JR): 2.27 s to 2.04 s. The default build of ZEXALL got 9% slower (43.3 s to 47.1 s) and this is the change that exposed the layout sensitivity described above: `step()` was byte-identical and had moved; layout-controlled, 43.2 s against the previous commit's 43.0 s. 4. **Lifecycle lines as one byte.** `step()` began with three tests (`reset_pending`, `non_maskable_interrupt_pending`, `iff1 && ei_delay == 0 && pending_maskable_interrupt.is_some()`), 11% of self time with `can_accept_maskable_interrupt` in the profile. The three requests are now bits of one `pending_lines` byte plus the INT vector byte; `step()` tests the byte and calls a `#[cold]` function that applies the reference's priority order only when something is pending. `capture_state`/`restore_state` and the request/clear API translate, so the 28 `CpuState` fields are unchanged. 43.2 s to 40.6 s. 5. **Fetched bytes inline.** Every fetched byte was pushed to a `Vec` (2% in `Vec::push`, plus the `clear` per instruction). The bytes now go to an 8-byte array with a length; only a DD/FD run longer than eight bytes moves to a `Vec`, on a `#[cold]` path. `last_instruction_bytes()` returns the same slice either way, and `tests/fetched_bytes.rs` covers the spill, since no manifest records an instruction longer than five bytes. 40.6 s to 38.6 s. 6. **One arm per opcode.** The grouped arms (`0x40..=0x75 | 0x77..=0x7F => self.op_ld_r_r(opcode)`) share one body, so the handler decodes the register fields at run time. `tools/expand_dispatch.py` rewrote every table so each opcode has its own arm with the opcode as a literal (`0x41 => self.op_ld_r_r(0x41)`), the mapping otherwise untouched. 38.6 s to 37.8 s; `ix-loop` 2.99 s to 2.42 s, `ed-loop` 2.04 s to about 1.8 s. The larger gain needs the handlers inlined so the literal folds, which is the next change. 7. **Forcing the handlers inline was a regression (not committed).** `#[inline(always)]` on the 28 opcode-parameterized handlers so the literal would fold: `execute_main` grew to 5.8 KB and was no longer inlined into `step`, and ZEXALL went from 37.8 s to 39.0 s, `ix-loop` from 2.42 s to 2.76 s. Reverted; recorded so it is not tried again the same way. 8. **Inline the host.** The re-profile at `94bd13a` showed `ConformanceHost::read_byte` (14.5%), `handle_cpm_trap` (13%) and `write_byte` (3.5%) as *outlined* functions: they are non-generic functions in the library crate, and the bench, `z80-trace`, and the rung binaries are separate crates that cannot inline them without a hint. Every memory read and every step was a call. `#[inline]` on the four `Bus` methods and on the trap test (its BDOS body moved to a `#[cold]` function) changes nothing the host does. 37.8 s to 20.5 s; `ix-loop` 2.42 s to 1.77 s, `ed-loop` 1.8 s to 1.4 s; `z80-trace --no-trace` over ZEXALL 151 s (z80-rust) to 128 s. A host in its own crate never paid this, so the gain is real for the kit's binaries and for the numbers in this table, but not for every embedding. For a core-only comparison the baseline `52192d1` was re-measured with the same two attributes applied: 47.7 s default, **40.1 s** min over layouts (nf6; 42.1 nf5). Against that, the core changes alone are 40.1 s to 20.5 s at this row. 9. **Flags as one byte.** `add`, `sub`, `cp`, `and`, `or`, `xor`, `inc`, `dec` and the eight CB rotates/shifts each wrote F through five to seven `Flags` setters (read-modify-write each, plus a three-step parity fold). Each now builds F in one expression: a `const` table `SZP[256]` holds S, Z, X, Y and parity for every result byte, and C, H, N and overflow are ORed in. Every primitive still writes exactly the flags the setter sequence wrote and preserves the rest (`inc`/`dec` keep C). Rung 2's 1,604,000 vectors and z80full are the check. 20.5 s to 19.8 s. 10. **The rest of the flag writers.** `add16`/`sub16`, `ADD HL,rr` and `ADD IX/IY,rr` (which preserve S, Z, PV), the accumulator rotates (same), `BIT` (preserves C), `IN r,(C)`, `RRD`/`RLD`, and `LD A,I`/ `LD A,R` (preserve C) composed the same way. ZEXALL barely runs them: 19.75 s to 19.69 s, within noise. Done for consistency, so every F write in the core is one assignment with its preserved bits named. 11. **Fat LTO and one codegen unit.** The first build-profile change, after the source-level work so each earlier number stands on the default profile. 19.7 s to 17.0 s. 12. **64-byte branch-target alignment as the default build.** `.cargo/config.toml` now sets `-C llvm-args=-align-all-nofallthru-blocks=6`, the layout that was best or equal-best on every commit above, so `cargo build --release` gives the measured layout rather than a lucky or unlucky one. Code size grows about 6%. `scripts/bench.sh` now sweeps `default` (aligned), `noalign` (the flag overridden away) and `nf5`. Quiet measurement (rung 3's PyPy processes paused, nothing else running): aligned default 16.8 s, `noalign` 17.5 s, `nf5` 17.0 s. 13. **`target-cpu=native` was not adopted.** On a 1,500,000,000-instruction slice with LTO: 8% slower than the default without alignment, 2% faster with it, i.e. inside the layout noise, and it would make the binaries non-portable. Not used. 14. **Profile-guided optimization, as an optional build.** `scripts/pgo.sh` instruments, trains on ZEXALL and the two secondary loops, merges the profile with the toolchain's `llvm-profdata`, and rebuilds into `target/pgo-use/`. Same quiet conditions: **15.1 s** (15.06, 15.11), 382 M instructions/s, a 3.1 GHz Z80; `ix-loop` 1.53 s to 0.98 s, `ed-loop` 1.21 s to 0.86 s. The default build does not use it, because a committed profile is tied to one compiler version. ## Using the core ```rust use z80_rust::{Bus, Z80}; struct Flat([u8; 0x10000]); impl Bus for Flat { fn read_byte(&mut self, addr: u16) -> u8 { self.0[usize::from(addr)] } fn write_byte(&mut self, addr: u16, value: u8) { self.0[usize::from(addr)] = value; } fn read_port(&mut self, _addr: u16) -> u8 { 0xFF } fn write_port(&mut self, _addr: u16, _value: u8) {} } let mut cpu = Z80::new(Flat([0; 0x10000])); cpu.bus.0[..2].copy_from_slice(&[0x3E, 0x2A]); // LD A,0x2A let t_states = cpu.step().unwrap(); assert_eq!((t_states, cpu.a), (7, 0x2A)); ``` `step()` services an asserted RESET, a latched NMI, or an acceptable maskable interrupt before fetching, exactly as the reference's `step()` does; `request_*` / `clear_*` are the lifecycle API; `capture_state()` and `restore_state()` move the complete processor state. `step()` returns `Err(Fault)` in exactly the situation where the reference raises `NotImplementedError` (IM 0 with a non-RST vector byte) and leaves the state as the reference leaves it. ### Features - `std` (default): the trace producer (`trace`), the conformance kit (`conformance`), serde, the binaries, and the rung 1 test. - Without it (`cargo build --no-default-features`) the crate is `no_std` plus `alloc` and contains the instruction core alone, for the WebAssembly build that follows this project. ### Binaries - `z80-bench [manifest] [--limit N] [--show-output]`: the speed measure above. - `z80-trace `: run a conformance manifest and stream its trace (`docs/trace-schema.md` in z80-python) to stdout or `--out`; `--checkpoint-every N` splits a long run into resumable segments. - `z80-vectors `: the SingleStepTests runner (rung 2). - `z80-z80test ...`: the z80test runner (rung 4). - `z80-fuse `: the FUSE core-test runner (rung 6). ## Stated limitations Inherited from the reference and z80-rust, unchanged: - **IM 0 accepts only the eight RST opcodes.** Both parent cores stop with an error for a non-RST byte and leave the request pending; no oracle in the ladder exercises it, and supporting it changes the trace schema. - Bus-level timing, memory contention, WAIT states, interrupt-acknowledge callbacks, and daisy chains are outside both cores' claims, as `docs/conformance.md` in the reference says. `step()` does not count T-states as it goes; where in an instruction each access falls is answered beside the core, from a table (below). ## Access timing `timing::instruction_timing(bytes)` takes an instruction's bytes, as a host peeks them at PC before `step()`, and returns every memory and I/O access the instruction can make, in order, each with its T-state offset, plus each way it can execute (`(t_states, accesses_made)`). It is for boards that arbitrate a shared bus, such as the Mega Drive's. It transcribes `z80_python/timing.py`; the table (`src/access_timing.rs`) is generated from the reference's by `scripts/gen_access_timing.py`, and the reference derives it from the SingleStepTests corpus's per-T-state bus samples, so its tier is emulator-derived. Interrupt acknowledge cycles and the HALT idle cycle are not covered. The behaviour fixes at `b62891b`, each from the reference: - **EI re-arms the delay** ([z80-python#18](https://github.com/alewman/z80-python/pull/18)): the delay ends as the instruction after EI starts, so `EI; EI` takes a pending interrupt only after the next instruction (Young 5.5), and `EI; DI` leaves `ei_delay` 0 rather than the -1 the reference's `CPUState` rejects. - **RESET clears I and R** (Zilog UM0080, "RESET"; found by megadrive-python, whose Sonic 3 sound driver reads R after a board reset). - **`Z80::cmos_out_c_zero`** (default `false`): the undocumented `OUT (C),0` drives 0xFF, as CMOS parts such as the Game Gear's are reported to. Reported, not hardware-captured. The rest of `cab1598..b62891b` is internal to the reference (the callable bus, the speed rungs' one-table dispatch and collapsed IX/IY handlers, tooling, documentation) and changes no behaviour; this port keeps its explicit if-chain dispatch and handler names, which still read side by side with the reference's rules and citations. Binaries: - `z80-trace `: run a conformance manifest and stream its trace (`docs/trace-schema.md`) to stdout or `--out`. - `z80-vectors `: the SingleStepTests runner. - `z80-z80test ...`: the z80test runner. - `z80-timing`: reads instruction bytes in hex on stdin and prints `instruction_timing` for each (rung 7). - `z80-fuse `: the FUSE core-test runner (rung 6). FUSE's expectations are emulator-derived; six cases disagree with the hardware-derived oracles, for reasons z80-python's `docs/validation.md` records, and the runner requires exactly those six to diverge. ## Prefix runs At `cab1598` the reference gained hardware-faithful handling of runs of DD/FD prefixes and of DD/FD before ED ([z80-python#5](https://github.com/alewman/z80-python/pull/5): each stray prefix is a 4-T-state M1 that bumps R, the last one decides IX or IY, an ED after a prefix runs the ED instruction unchanged, and no interrupt is accepted inside the run). `src/index_dispatch.rs` transcribes it, and the bytes an instruction occupies are now unbounded, since the run is. The rule is documentation-derived (Sean Young, *The Undocumented Z80 Documented* v0.91, sections 3.7 and 6.1, chapter 5) and has no hardware-captured vector; z80-python's `docs/validation.md` states that tier, and this port inherits it. `Fault::UnhandledIndexOpcode` is now unreachable and kept only as the table's completeness guard. ## Notes for the reference Three things this port turned up went upstream and are merged: [z80-python#3](https://github.com/alewman/z80-python/pull/3) (`CPUState` has 28 fields where the docs said 29, and the interrupt-scenario manifests in `conformance/interrupts/` shipped as `examples/conformance/interrupts/` with their reference traces), [#4](https://github.com/alewman/z80-python/pull/4) (the reference's CI workflow had failed to parse since before this work), and [#5](https://github.com/alewman/z80-python/pull/5) (prefix runs, above). ## Where the brief is [docs/handoff-brief.md](docs/handoff-brief.md) is the prompt this repository is built from, verbatim, and [docs/build-record.md](docs/build-record.md) is what happened when it was run. The transcription's own brief and record are [docs/z80-rust-handoff-brief.md](docs/z80-rust-handoff-brief.md) and [docs/z80-rust-build-record.md](docs/z80-rust-build-record.md). ## License MIT, like both parents. The SingleStepTests corpus (MIT), the ZEX exercisers (GPL-2.0), z80test (MIT), and FUSE's tests (GPL-2.0) are external and not bundled.