"""Exact capture resets preserve their CPU-clock phase across frame boundaries.""" import pytest from board_frames import main, reset_boundary, run_reset_frame from run_vscroll_latch import _w, nuked_md_cartridge from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System def system(): board = System( MACHINES["nuked-md"], Cartridge(nuked_md_cartridge(_w(0x46FC, 0x2700, 0x4E71, 0x60FC))), ) board.board.fm.enabled = False return board @pytest.mark.parametrize("at", [1000, 1001, 2_000_000]) def test_exact_reset_uses_the_requested_clock_not_the_next_instruction(at): probe = system() boundary = reset_boundary(probe, at) assert boundary <= at < probe.mclk expected = boundary + -(-(at - boundary) // 7) * 7 capture = system() pending = True frames = 0 while pending: previous = capture.vdp.frame pending = run_reset_frame(capture, at, boundary) assert capture.vdp.frame > previous frames += 1 assert frames < 5 assert capture.board.cart_refresh_due % 896 == expected % 896 if at == 2_000_000: assert frames > 1 def test_exact_reset_on_an_instruction_boundary(): probe = system() at = probe.mclk assert reset_boundary(probe, at) == at capture = system() assert not run_reset_frame(capture, at, at) assert capture.board.cart_refresh_due % 896 == at % 896 def test_exact_reset_rejects_a_past_time(): probe = system() with pytest.raises(ValueError, match="precedes"): reset_boundary(probe, probe.mclk - 1) def test_exact_reset_rejects_a_divergent_probe(): capture = system() with pytest.raises(RuntimeError, match="passed"): run_reset_frame(capture, capture.mclk + 100, capture.mclk - 1) def test_exact_flag_requires_a_reset_time(monkeypatch): monkeypatch.setattr("sys.argv", ["board_frames.py", "rom.bin", "out", "--reset-exact"]) with pytest.raises(SystemExit) as error: main() assert error.value.code == 2