# SPDX-License-Identifier: MIT """Rung 4: the replay machinery, on made-up lines, and on MAME's real traces when present. Emulator-derived: a detector. The traces are recorded by ``scripts/mame_trace.py --io`` into tests/mame_traces/ (gitignored); the tests over them are ``slow`` and skip without them. """ from pathlib import Path import pytest from spc700_python.replay import Line, parse_trace, replay, replay_file ROOT = Path(__file__).resolve().parents[1] TRACES = ROOT / "tests" / "mame_traces" IPL_FILE = ROOT / "reference" / "build" / "iplrom.bin" #: IPL-shaped program: the replay starts at $FFC0 from the IPL ROM. IPL = bytes.fromhex("e4f4 c4f5 2ffa").ljust(64, b"\x00") # MOV A,$F4; MOV $F5,A; BRA -6 LOG = """\ [:snsslot] something MAME says FFC0 E4 0 0 0 0 0 100 R F4 7 101 FFC2 C4 7 0 0 0 0 106 W F5 7 110 FFC4 2F 7 0 0 0 0 114 FFC0 E4 7 0 0 0 0 122 R F4 8 123 FFC2 C4 8 0 0 0 0 128 """.splitlines() def test_parse_trace(): lines = list(parse_trace(LOG)) assert [line.pc for line in lines] == [0xFFC0, 0xFFC2, 0xFFC4, 0xFFC0, 0xFFC2] assert lines[0].io == [("R", 0xF4, 7)] assert lines[1].io == [("W", 0xF5, 7)] def test_replay_agrees(): result = replay(parse_trace(LOG), IPL) assert result.ok assert result.compared == 4 assert (result.io_reads, result.io_writes) == (2, 1) assert result.unlogged_dummy_reads == 1 # MOV $F5,A reads $F5 first; MAME's core does not def test_replay_reports_a_register_difference(): lines = list(parse_trace(LOG)) lines[2].a = 9 divergence = replay(lines, IPL).divergence assert divergence is not None assert "A = 7, MAME 9" in divergence.describe() def test_replay_reports_a_cycle_difference(): lines = list(parse_trace(LOG)) lines[1].cycles += 2 divergence = replay(lines, IPL).divergence assert divergence is not None assert any("cycles" in problem for problem in divergence.problems) def test_replay_reports_a_different_write(): lines = list(parse_trace(LOG)) lines[1].io = [("W", 0xF5, 8)] divergence = replay(lines, IPL).divergence assert divergence is not None assert "core wrote $00F5=$07" in divergence.problems[0] def test_replay_reports_a_read_mame_did_not_log(): lines = list(parse_trace(LOG)) lines[0].io = [] assert replay(lines, IPL).divergence is not None def test_replay_drops_mames_second_read_of_an_adc_operand(): ipl = bytes.fromhex("98 04 f2 2ffb").ljust(64, b"\x00") # ADC $F2,#$04; BRA -5 log = [ "FFC0 98 0 0 0 0 0 100", "R F2 1 101", "R F2 1 101", "W F2 5 101", "FFC3 2F 0 0 0 0 0 110", "FFC0 98 0 0 0 0 0 118", ] result = replay(parse_trace(log), ipl) assert result.ok, result.divergence assert result.repeated_reads == 1 def test_replay_needs_a_line(): with pytest.raises(ValueError): replay(iter(()), IPL) def test_line_has_the_trace_fields(): line = Line(1, 2, 3, 4, 5, 6, 7, 8) assert (line.pc, line.opcode, line.cycles, line.io) == (1, 2, 8, []) LOGS = sorted(p for p in TRACES.glob("*/error.log") if b"\nR " in p.read_bytes()[: 1 << 24]) @pytest.mark.slow @pytest.mark.skipif(not IPL_FILE.exists(), reason="run scripts/build_referees.py first") @pytest.mark.parametrize("log", LOGS, ids=[p.parent.name for p in LOGS]) def test_real_code(log): result = replay_file(log, IPL_FILE.read_bytes()) assert result.ok, result.divergence.describe() assert result.compared > 1_000_000