# SPDX-License-Identifier: MIT """A console run frame by frame, built fresh or loaded from a save state: what ``scripts/run_32x.py`` and ``python -m sega32x_python`` share. :meth:`Session.start` builds the System with the 32X (``console.build``) or loads one (``savestate.py``); :meth:`Session.sound` decides the FM and the sample rate (the FM is off unless sound is wanted: it is the slowest chip); :meth:`Session.run_frame` runs one frame and counts it; the three pictures are the composite on the 32X's grid (``composite.rectangle``), the 32X VDP's alone, and the Mega Drive's own. """ from __future__ import annotations import sys import time from pathlib import Path from megadrive_python.cartridge import load_image from megadrive_python.system import System from . import savestate from .bootrom import BootROMs from .composite import rectangle from .console import build, cartridge_for from .slot import Sega32X class Session: """``system`` with ``device`` in its slot, ``frame`` frames run so far.""" def __init__(self, system: System, device: Sega32X, frame: int = 0) -> None: self.system = system self.device = device self.frame = frame @classmethod def start( cls, cartridge: str | Path, bios_dir: str | Path, *, machine: str = "genesis", load_state: str | Path | None = None, any_board: bool = False, ) -> Session: """A new console with ``cartridge`` in the 32X, or the one saved in ``load_state`` (made from the same cartridge and boot ROMs: :func:`savestate.check`). A loaded state carries its own machine; ``machine`` is then not used.""" image = load_image(cartridge) boot_roms = BootROMs.load(bios_dir) if load_state is None: system, device = build(machine, cartridge_for(image), boot_roms) return cls(system, device) began = time.time() system, device, header = savestate.load( load_state, cartridge_for(image), boot_roms=boot_roms, any_board=any_board ) print(f"[state] loaded {load_state} at frame {header.frame} in " f"{time.time() - began:.2f} s", file=sys.stderr, flush=True) # fmt: skip return cls(system, device, header.frame) def sound(self, *, fm: bool, rate: int | None = None) -> None: """The FM on or off, and the host sample rate (None: the System's). A loaded state has the FM as it was saved; turning it on or off then makes the sound differ from a straight run.""" chip = self.system.board.fm if self.frame and chip.enabled != fm: print(f"[state] the FM was {'on' if chip.enabled else 'off'} when saved; now " f"{'on' if fm else 'off'}: the sound will not match a straight run", file=sys.stderr, flush=True) # fmt: skip chip.enabled = fm if rate is not None: self.system.audio.sample_rate = rate def run_frame(self) -> None: """One frame (megadrive-python's ``Lockup`` passes through).""" self.system.run_frame() self.frame += 1 def save(self, path: str | Path) -> int: """Save the machine to ``path`` (its size in bytes), reporting size and time.""" began = time.time() size = savestate.save(path, self.system, self.device, frame=self.frame) print(f"[state] saved {path} at frame {self.frame}: {size:,} bytes in " f"{time.time() - began:.2f} s", file=sys.stderr, flush=True) # fmt: skip return size # -- pictures ----------------------------------------------------------------------- def picture(self) -> list[list[int]]: """The picture that leaves the console (socket 7's composite), on the 32X's grid.""" return rectangle([list(row) for row in self.system.vdp.renderer.frame_rgb()]) def picture_32x(self) -> list[list[int]]: """The 32X VDP's own frame, 320 wide.""" return self.device.vdp.frame_rgb() def picture_md(self) -> list[list[int]]: """The Mega Drive's own frame: the renderer's rows with socket 7 ignored.""" renderer = self.system.vdp.renderer picture, renderer.picture = renderer.picture, None rows = renderer.frame_rgb() renderer.picture = picture return rectangle(rows) __all__ = ["Session"]