# SPDX-License-Identifier: MIT """Rung 5: MAME lockstep, the quick part. MAME 0.285 is an emulator (tier 5): **a detector**. Its traces of four machines are replayed through the core (tests/mame_replay.py); the whole traces are ``scripts/run_mame_replay.py``, whose numbers are in docs/mame-oracle.md. Each kind of difference found is listed there with the higher-tier source that decides it; this test holds the list: a new kind of difference fails. Traces are recorded by ``scripts/mame_trace.py --bus`` into ``tests/mame_traces`` (never committed); a driver without one is skipped. """ import re import pytest from mame_replay import DRIVERS, TRACE_HINT, has_bus_trace, replay from w65c02_python import G65SC02 INSTRUCTIONS = 150_000 #: The kinds of difference the full replays found, each decided by a higher tier #: (docs/mame-oracle.md, "What the lockstep found"). EXPLAINED = ( # C19: MAME's base CMOS device takes 7 cycles for the abs,X shifts within a page; # DS Table 7-1, GDS Tables IV and V and CAP (both parts, every execution) say 6. re.compile(r"cycles of \$[1357]E (ASL|ROL|LSR|ROR): core 6, MAME 7"), # The Lynx's CPUSLEEP ($FD91): MAME holds the CPU while Suzy draws and counts the # held cycles on the CPU's counter. The board's time, not the instruction's. re.compile(r"cycles of \$9C STZ: core 4, MAME \d+"), ) @pytest.mark.parametrize("driver", sorted(DRIVERS)) def test_the_start_of_each_trace_agrees_but_for_the_explained_differences(driver: str) -> None: if not has_bus_trace(driver): pytest.skip(TRACE_HINT) report = replay(driver, INSTRUCTIONS) assert report.instructions + sum(report.interrupts.values()) == INSTRUCTIONS unexplained = { category: report.examples[category] for category in report.differences if not any(pattern.fullmatch(category) for pattern in EXPLAINED) } assert not unexplained assert report.agreed >= INSTRUCTIONS - 100 class WrongCompareCarry(G65SC02): """A seeded fault: CMP, CPX and CPY set C on greater-than, not greater-or-equal.""" def _compare(self, register: int, operand: int) -> None: super()._compare(register, operand) if register == operand: self.p &= ~0x01 def test_a_seeded_fault_shows_in_the_replay() -> None: """The detector can detect: a wrong CMP carry differs from MAME's registers.""" if not has_bus_trace("apple2ee"): pytest.skip(TRACE_HINT) report = replay("apple2ee", INSTRUCTIONS, row=WrongCompareCarry) assert report.differences["registers after $C4 CPY"] > 10