"""M1's exit test: the 2-second no-cartridge MAME trace replayed in lockstep, and seeded faults that show the replay can fail. The trace is made by ``scripts/mame_trace.py coleco --seconds 2`` into the git-ignored ``tests/mame_traces/coleco-nocart/`` (docs/mame-oracle.md); the tests skip without it. """ from __future__ import annotations import pytest from conftest import TRACES from colecovision_python import Board, mame_options from colecovision_python.mame_log import lockstep, read_log NOCART = TRACES / "coleco-nocart" / "error.log" pytestmark = pytest.mark.skipif(not NOCART.exists(), reason="no MAME trace (scripts/mame_trace.py)") def replay(board, limit=None): return lockstep(board, read_log(NOCART), limit=limit) def test_bios_two_seconds_lockstep(bios): """945,993 instructions, 17,821 port writes, 1 port read, 0 differences.""" board = Board(bios, options=mame_options(), io_log=[]) result = replay(board) assert result.differences == 0, result.first_difference assert (result.instructions, result.port_writes, result.port_reads) == (945_993, 17_821, 1) PALCART = TRACES / "colecop-nocart" / "error.log" @pytest.mark.skipif(not PALCART.exists(), reason="no MAME colecop trace") def test_pal_bios_two_seconds_lockstep(pal_bios): """951,253 instructions, 17,821 port writes, 1 port read, 0 differences.""" from colecovision_python import PAL board = Board(pal_bios, machine=PAL, options=mame_options(), io_log=[]) result = lockstep(board, read_log(PALCART)) assert result.differences == 0, result.first_difference assert (result.instructions, result.port_writes, result.port_reads) == (951_253, 17_821, 1) # ---------------------------------------------------------------------- seeded faults class NoPrefixWait(Board): """Fault: prefix bytes do not get the M1 wait.""" def _read_m1(self, address, kind): value = super()._read_m1(address, kind) if value in (0xDD, 0xFD, 0xED, 0xCB): self._m1 -= 1 return value class UnmirroredRam(Board): """Fault: RAM only at $6000-$63FF, as MAME's map would read without its mirror.""" def _build_readers(self): readers = super()._build_readers() ram = self.ram readers[3] = lambda address: ram[address - 0x6000] if address < 0x6400 else 0xFF return readers def write_byte(self, address, value): if 0x6400 <= address < 0x8000: return super().write_byte(address, value) class NoFrameFlag(Board): """Fault: the VDP is never run, so F never sets before the BIOS's one status read.""" def _catch_up(self, clock): self.vdp_clock = clock @pytest.mark.parametrize( ("make", "limit"), [ (lambda bios: Board(bios, options=mame_options(ready_wait=27), io_log=[]), 1_000), (lambda bios: Board(bios, options=mame_options(ready_wait=29), io_log=[]), 1_000), (lambda bios: NoPrefixWait(bios, options=mame_options(), io_log=[]), 200_000), (lambda bios: UnmirroredRam(bios, options=mame_options(), io_log=[]), 200_000), (lambda bios: Board(bios, options=mame_options(power_on_cpu={}), io_log=[]), 10), (lambda bios: NoFrameFlag(bios, options=mame_options(), io_log=[]), None), ], ids=["ready-27", "ready-29", "no-prefix-wait", "ram-unmirrored", "power-on-zeros", "no-frame-flag"], ) def test_seeded_fault_is_caught(bios, make, limit): result = replay(make(bios), limit=limit) assert result.differences == 1, "the fault went unseen"