# SPDX-License-Identifier: MIT """The TIA against the die: Sim2600 traces replayed pin by pin (scripts/die_trace.py). Every pin is scored, both audio pads included. Four short console-wired traces run by default; ``pytest -m slow`` runs every trace present, tia-fpga's 22 (eleven cartridges, two wirings) and the locally recorded ones under ``tests/die_traces``. They skip when the references are not fetched (``python scripts/fetch_references.py``). """ from __future__ import annotations import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) from die_trace import load, replay # noqa: E402 TRACES = ROOT / "reference" / "src" / "tia-fpga" / "sim" / "traces" LOCAL = ROOT / "tests" / "die_traces" QUICK = ["phi2/vertical.trace", "phi2/vdel.trace", "phi2/hmove.trace", "phi2/audio.trace"] ALL = sorted( [str(p.relative_to(TRACES)) for p in TRACES.glob("**/*.trace")] if TRACES.exists() else QUICK ) # Where the board follows hardware over the die, the half clocks it differs # by, exactly: hmove_cycles' two HMOVEs on cycle 74, where seven consoles # set the comb latch and Sim2600 clears it (docs/undocumented-behavior.md C27). # hmove_resp0 has the same tie in its HMOVE-on-cycle-74 block. KNOWN_DEPARTURES = { "hmove_cycles.trace": {"lum": 230, "col": 220, "blank": 32}, "hmove_resp0.trace": {"lum": 2638, "col": 2638, "blank": 352}, } LOCAL_ALL = sorted(str(p) for p in LOCAL.glob("**/*.trace")) if LOCAL.exists() else [] def score(path: Path) -> tuple[int, dict]: if not path.exists(): pytest.skip("die traces not fetched: python scripts/fetch_references.py") scored, mismatches, _, _ = replay(load(path), warm=2000, audio=True, report=0, stop_after=None) return scored, mismatches @pytest.mark.parametrize("name", QUICK) def test_quick_traces_match_the_die(name): scored, mismatches = score(TRACES / name) assert scored > 20000 assert sum(mismatches.values()) == 0, mismatches @pytest.mark.slow @pytest.mark.parametrize("name", ALL) def test_every_trace_matches_the_die(name): _, mismatches = score(TRACES / name) assert sum(mismatches.values()) == 0, mismatches @pytest.mark.slow @pytest.mark.parametrize("path", LOCAL_ALL or ["(none recorded)"]) def test_locally_recorded_traces_match_the_die(path): if path == "(none recorded)": pytest.skip("no local die traces (scripts/die_carts.py, then Sim2600)") _, mismatches = score(Path(path)) expected = KNOWN_DEPARTURES.get(Path(path).name, {}) assert {p: n for p, n in mismatches.items() if n} == expected, mismatches