"""The fast core against the pinned reference, boundary by boundary. Every shipped manifest, plus random programs with random lifecycle events, through ``validation/lockstep.py``: T-states, all 28 CPUState fields, and every bus access compared after each step. The long runs (ZEXDOC/ZEXALL segments) use the same checker from ``scripts/rung3.py``. """ from dataclasses import astuple from pathlib import Path import pytest from validation import lockstep ROOT = Path(__file__).resolve().parents[1] MANIFESTS = sorted( [ *ROOT.glob("examples/conformance/*.json"), *ROOT.glob("examples/conformance/interrupts/*.json"), ] ) @pytest.mark.parametrize("path", MANIFESTS, ids=lambda path: path.stem) def test_manifest_runs_identically(path: Path) -> None: result = lockstep.run(path) assert result.divergence is None, str(result.divergence) @pytest.mark.parametrize("seed", range(40)) def test_random_program_runs_identically(seed: int) -> None: result = lockstep.run_manifest(lockstep.random_manifest(seed, 3_000)) assert result.divergence is None, str(result.divergence) @pytest.mark.parametrize("seed", range(10)) def test_run_one_is_one_step(seed: int) -> None: """run(1) through its own loop body, boundary by boundary, events included.""" result = lockstep.run_manifest(lockstep.random_manifest(seed, 3_000), entry="run") assert result.divergence is None, str(result.divergence) @pytest.mark.parametrize("seed", range(10)) def test_run_budget_matches_stepping_to_the_same_budget(seed: int) -> None: """run(n) for random n: the same state, T-states and bus traffic as step() called until n T-states have elapsed. Random programs with interrupt requests made between run() calls.""" import random from z80_python import Z80CPU as Reference from z80_python_fast import Z80CPU as Fast rng = random.Random(seed) image = rng.randbytes(0x10000) cores = [] for core in (Reference, Fast): memory = bytearray(image) log: list = [] def read_byte(address: int, memory: bytearray = memory, log: list = log) -> int: log.append(("r", address)) return memory[address] def write_byte(address: int, value: int, memory: bytearray = memory, log: list = log): log.append(("w", address, value)) memory[address] = value cpu = core(read_byte, write_byte, read_port=lambda port: port & 0xFF) cpu.pc, cpu.sp, cpu.im = rng.randrange(0x10000), 0x8000, 1 cores.append((cpu, memory, log)) rng = random.Random(seed) # same draws for the second core rng.randbytes(0x10000) (ref, ref_memory, ref_log), (fast, fast_memory, fast_log) = cores rng = random.Random(seed * 7919) for _ in range(300): budget = rng.randrange(-5, 200) total = 0 while total < budget: total += ref.step() assert fast.run(budget) == total assert astuple(fast.capture_state()) == astuple(ref.capture_state()) assert fast_log == ref_log assert fast_memory == ref_memory if rng.random() < 0.1: vector = 0xC7 | rng.randrange(8) << 3 ref.request_maskable_interrupt(vector) fast.request_maskable_interrupt(vector)