# SPDX-License-Identifier: MIT """A saved state continues exactly as the run it came from (charter, the observable contract: "save states that load into a fresh console and continue identically"). mcd-verificator runs from power-on; after frame ``SAVE_AT`` the console is saved as ``python -m segacd_python --save-state`` saves it (segacd_python.state, on megadrive-python's state version 2); the straight run goes on to ``LAST``, and so do a console built afresh in this process with the state loaded and one built in a fresh process (where nothing built at import or kept in a module survives). The whole state's digest (savestate_check.py) must match right after the load and after every frame. Frames 450-600 hold the CDC FLAGS, CDC DMA2 and CDC DMA3 rows (docs/build-status.md), so the drive, the decoder's transfers, both 68000s and the gate array's interrupts are all moving. With ``SEGACD_DISC`` set to a disc (a .cue or a zip; Sonic CD (USA) is the one the gate uses) the drive holds it; without, the drive is empty. Some minutes under PyPy (the digests cost more than the frames): ``pytest -m slow tests/test_save_state.py``.""" from __future__ import annotations import json import subprocess import sys import pytest from savestate_check import ( ROOT, VERIFICATOR, build_machine, digest, disc_path, first_divergence, load, run_digests, run_frame, save, ) SAVE_AT = 450 LAST = 600 pytestmark = [ pytest.mark.slow, pytest.mark.skipif(not VERIFICATOR.exists(), reason="run scripts/fetch_references.py"), ] def test_saved_state_continues_identically(tmp_path) -> None: disc = disc_path() system, device = build_machine(disc) for _frame in range(1, SAVE_AT + 1): run_frame(system, device) state = tmp_path / f"state.f{SAVE_AT:05d}" save(state, system, SAVE_AT) at_save = digest(system) fresh_out = tmp_path / "fresh.json" helper = ROOT / "tests/savestate_check.py" fresh = subprocess.Popen( # runs beside the in-process halves [sys.executable, str(helper), str(state), str(SAVE_AT), str(LAST), str(fresh_out)] ) copy_system, copy_device, frame = load(state, disc) assert frame == SAVE_AT same = [digest(copy_system)] straight = [at_save, *run_digests(system, device, SAVE_AT + 1, LAST)] same += run_digests(copy_system, copy_device, SAVE_AT + 1, LAST) assert fresh.wait(timeout=3600) == 0 loaded = json.loads(fresh_out.read_text()) where = f"(disc: {disc or 'none'})" assert first_divergence(straight, same, SAVE_AT) is None, ( f"same-process load {where}: {first_divergence(straight, same, SAVE_AT)}" ) assert first_divergence(straight, loaded, SAVE_AT) is None, ( f"fresh-process load {where}: {first_divergence(straight, loaded, SAVE_AT)}" )