# SPDX-License-Identifier: MIT """Rung 4: Mesen2 2.1.1 recordings replayed in lockstep (detector tier, never the judge). Each window under ``tests/mesen_traces//`` (``scripts/record_mesen_windows.py``; gitignored, recorded from BYOR cartridges) is replayed from power-on: every S-CPU read must match and every traced instruction's registers must match, except where a C entry says the models differ. With the defaults (byuu's model, ares, on every C entry) the only difference Mesen2 shows is C18 ($7F51 sets the IRQ flag in Mesen2): status reads with bit 1 set. With ``IRQ_DISABLE_SETS_FLAG = True`` nothing differs. """ from __future__ import annotations from pathlib import Path import pytest from hg51b_python import HG51B from hg51b_python.replay import replay TRACES = Path(__file__).parent / "mesen_traces" WINDOWS = { "x2-boot": "retail", "x2-test": "retail", "x2-title": "retail", "x2-play": "retail", "x3-title": "x3", } class Mesen2Irq(HG51B): IRQ_DISABLE_SETS_FLAG = True def _recording(name: str) -> Path: path = TRACES / name if not (path / "host.txt.gz").exists(): pytest.skip(f"no recording: python -I scripts/record_mesen_windows.py {name}") return path @pytest.mark.slow @pytest.mark.parametrize("name", sorted(WINDOWS)) def test_replay_matches_mesen2(name: str, cartridge) -> None: path = _recording(name) report = replay(cartridge(WINDOWS[name]), path, core=Mesen2Irq) assert report.instructions_compared > 1000, report.summary() assert not report.divergences, report.summary() assert not report.read_mismatches, report.summary() assert not report.run_length_mismatches, report.summary() @pytest.mark.slow def test_defaults_differ_only_by_c18(cartridge) -> None: path = _recording("x2-boot") report = replay(cartridge("retail"), path) assert not report.divergences assert report.read_mismatches assert all(core ^ mesen == 0x02 for _, _, core, mesen, _ in report.read_mismatches)