"""Write src/access_timing.rs from the reference's access-timing table. The table is data the reference derives from the SingleStepTests corpus (z80-python scripts/derive_access_timing.py, z80_python/_access_timing.py). This port does not re-derive it: it transcribes the pinned reference's table byte for byte, and rung 7 (scripts/rung7.sh) proves instruction_timing() answers as the reference's does for every instruction. Usage: external/z80-python/.venv/bin/python scripts/gen_access_timing.py """ import subprocess from pathlib import Path from z80_python._access_timing import TABLE ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "src" / "access_timing.rs" KINDS = {"read": "Read", "write": "Write", "in": "In", "out": "Out"} def main() -> None: commit = subprocess.run( ["git", "-C", str(ROOT / "external" / "z80-python"), "rev-parse", "--short", "HEAD"], check=True, capture_output=True, text=True, ).stdout.strip() lines = [ f"//! GENERATED by scripts/gen_access_timing.py from z80-python {commit}'s", "//! `_access_timing.py`. Do not edit.", "//!", "//! Where each memory and I/O access of each instruction falls; see `timing`.", "//! Key: the opcode bytes (prefixes and opcode, not operands or displacement),", "//! sorted, for a binary search. Value: the accesses, `(kind, t)` for every", "//! access the instruction can make, in order; and the shapes, `(t_states,", "//! accesses made)` for each way the instruction can execute.", "", "use crate::timing::AccessKind::{self, In, Out, Read, Write};", "", "pub(crate) type Entry = (", " &'static [u8],", " &'static [(AccessKind, u8)],", " &'static [(u8, u8)],", ");", "", "#[rustfmt::skip]", "pub(crate) static TABLE: &[Entry] = &[", ] for key in sorted(TABLE): accesses, shapes = TABLE[key] key_text = ", ".join(f"0x{b:02X}" for b in key) access_text = ", ".join(f"({KINDS[kind]}, {t})" for kind, t in accesses) shape_text = ", ".join(f"({t_states}, {made})" for t_states, made in shapes) lines.append(f" (&[{key_text}], &[{access_text}], &[{shape_text}]),") lines.append("];") OUT.write_text("\n".join(lines) + "\n", encoding="utf-8") print(f"{OUT.relative_to(ROOT)}: {len(TABLE)} entries from z80-python {commit}") if __name__ == "__main__": main()