# SPDX-License-Identifier: MIT """Save states (segacd_python.state) in a few seconds: a state loaded into a freshly built console is the same console and runs on the same, and a state is refused up front for another BIOS, disc, megadrive-python state version or console. The long check, a run continued for 150 frames in this process and in a fresh one, is test_save_state.py.""" from __future__ import annotations import json import struct from pathlib import Path import pytest from megadrive_python import state as md from savestate_check import VERIFICATOR from segacd_python import state from segacd_python.console import build from segacd_python.disc import Disc from segacd_python.ramcart import RAMCartridge needs_verificator = pytest.mark.skipif( not VERIFICATOR.exists(), reason="run scripts/fetch_references.py" ) def tiny_disc(folder: Path, fill: int) -> Disc: """A one-track data disc of 32 sectors, every byte ``fill``.""" (folder / "t.bin").write_bytes(bytes([fill]) * 2352 * 32) cue = folder / "t.cue" cue.write_text('FILE "t.bin" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\n') return Disc.from_cue(cue) def console(bios: bytes | None = None, disc: Disc | None = None) -> tuple[object, object]: bios = VERIFICATOR.read_bytes() if bios is None else bios return build("genesis", bios, RAMCartridge(), disc=disc) def run(system: object, device: object, frames: int) -> None: for _ in range(frames): system.run_frame() # type: ignore[attr-defined] system.board.fm.take_samples() # type: ignore[attr-defined] system.board.psg.take_samples() # type: ignore[attr-defined] for source in device.audio_sources: # type: ignore[attr-defined] source.take_samples(system.mclk) # type: ignore[attr-defined] def test_the_borrowed_parts_of_megadrive_python_are_there() -> None: """state.py builds on these private names at the pinned megadrive-python.""" assert md.VERSION == 2 assert isinstance(md.MODULES, tuple) for name in ("_Encoder", "_Decoder", "_header", "check_header", "FORMAT", "HOST"): assert hasattr(md, name), name assert ("megadrive_python.vdp.VDP", "undrawn_frame") in md.HOST @needs_verificator def test_a_loaded_state_is_the_same_console_and_runs_on_the_same(tmp_path) -> None: disc = tiny_disc(tmp_path, 0) system, device = console(disc=disc) run(system, device, 40) # the sub CPU is out of reset and running by now assert device.sub_running data = state.save_state(system, {"frame": 40}) other_system, other_device = console(disc=tiny_disc(tmp_path, 0)) assert state.load_state(other_system, data) == {"frame": 40} assert state.machine_state(other_system) == state.machine_state(system) run(system, device, 8) run(other_system, other_device, 8) assert state.machine_state(other_system) == state.machine_state(system) assert other_device.sub.capture_state() == device.sub.capture_state() assert md.MODULES == ("megadrive_python", "ym2612_python", "sn76489_python", "ssp1601_python") @needs_verificator def test_the_drawing_switches_are_the_hosts(tmp_path) -> None: system, device = console() run(system, device, 3) system.vdp.rendering = False data = state.save_state(system) other, _ = console() state.load_state(other, data) assert other.vdp.rendering is True # the loading console keeps its own @needs_verificator def test_states_are_refused_up_front(tmp_path) -> None: system, device = console(disc=tiny_disc(tmp_path, 0)) run(system, device, 2) data = state.save_state(system) with pytest.raises(state.StateError, match="another BIOS"): state.load_state(console(bios=bytes(0x20000), disc=tiny_disc(tmp_path, 0))[0], data) other = tmp_path / "other" other.mkdir() with pytest.raises(state.StateError, match="another disc"): state.load_state(console(disc=tiny_disc(other, 1))[0], data) with pytest.raises(state.StateError, match="another disc"): state.load_state(console()[0], data) with pytest.raises(state.StateError, match="not a segacd-python"): state.load_state(console(disc=tiny_disc(tmp_path, 0))[0], b"garbage" + data) # A state of another megadrive-python state version. (length,) = struct.unpack_from(">I", data, 8) header = json.loads(data[12 : 12 + length]) header["megadrive"]["version"] = 1 text = json.dumps(header).encode() old = data[:8] + struct.pack(">I", len(text)) + text + data[12 + length :] with pytest.raises(state.StateError, match="megadrive-python state version 1"): state.load_state(console(disc=tiny_disc(tmp_path, 0))[0], old) def test_a_console_without_the_cd_is_not_ours() -> None: from megadrive_python.machine import MACHINES from megadrive_python.system import System with pytest.raises(state.StateError, match="no Sega CD"): state.save_state(System(MACHINES["genesis"], None)) def test_a_disc_is_named_by_its_tracks_and_system_area(tmp_path) -> None: first = tmp_path / "a" second = tmp_path / "b" first.mkdir() second.mkdir() assert state.disc_identity(tiny_disc(first, 0)) == state.disc_identity(tiny_disc(second, 0)) assert state.disc_identity(tiny_disc(second, 7)) != state.disc_identity(tiny_disc(first, 0)) assert state.disc_identity(None) == ""