# SPDX-License-Identifier: MIT """The drawing switches (``VDP.rendering``, ``VDP.undrawn_frame``) gate only the pixels. A frame run undrawn must be the same frame as one drawn: every 68000 and Z80 step at the same clock and PC, and the whole state but the picture (``state.machine_state``) equal at every frame's end; and a frame drawn after undrawn ones (``undrawn_frame``, which the VDP clears as the next frame begins) is the picture a run drawing every frame shows. Found downstream on 2026-11-23 (docs/polish.md): switching drawing off also skipped line $1FF's sprite scan and never set the sprite collision flag; and a frame drawn after an undrawn one lost its first lines when the step that ended the undrawn frame had run them. ``board-tour.bin`` and ``h32-sprites.bin`` need no ROM but never read the collision flag set. Earthworm Jim (``$NO_INTRO_DIR``) reads it set from frame 83 of a cold boot, and the test checks that it did; Overdrive 2 (the fetched test ROMs) has a frame whose last step runs 13 lines of the next, and the test checks that one came before a drawn frame. Both skip without their ROM. """ from __future__ import annotations import os import zipfile from pathlib import Path import pytest from megadrive_python import state from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from megadrive_python.vdp import VDP EXAMPLES = Path(__file__).parent / "examples" COLLIDER = "Sega - Mega Drive - Genesis/Earthworm Jim (USA).zip" def _system(image: bytes, machine: str = "genesis") -> System: system = System(MACHINES[machine], Cartridge.from_image(image)) system.board.fm.enabled = False # speed: the FM is not what this tests return system def _frame(system: System, draw: bool) -> list[tuple]: """One frame, drawn or not (``undrawn_frame``): every step's clocks and PCs.""" system.vdp.undrawn_frame = not draw out = [] frame = system.vdp.frame while system.vdp.frame == frame: system.step() out.append((system.mclk, system.m68k.PC, system.z80_mclk, system.z80.pc)) return out def _same_runs(image: bytes, frames: int, every: int, machine: str = "genesis") -> int: """A run drawing every frame against one drawing every ``every``-th frame; returns how many drawn frames began part way down, their first lines run by the step that ended the undrawn frame before.""" drawn, sparse = _system(image, machine), _system(image, machine) late = 0 for n in range(frames): shown = n % every == every - 1 if shown and sparse.vdp.v != 0: late += 1 assert _frame(sparse, shown) == _frame(drawn, True), f"frame {n}: the steps part" assert state.machine_state(sparse) == state.machine_state(drawn), f"frame {n}" if shown and n >= every: # The frame finished at this boundary was drawn whole in both runs. assert sparse.vdp.renderer.frame() == drawn.vdp.renderer.frame(), f"frame {n}" return late @pytest.mark.parametrize("name", ["board-tour.bin", "h32-sprites.bin"]) def test_an_undrawn_frame_is_the_same_frame(name: str) -> None: _same_runs((EXAMPLES / name).read_bytes(), frames=12, every=3) def test_drawing_off_for_a_whole_run() -> None: """``rendering`` off throughout: the same steps and machine state, no picture.""" image = (EXAMPLES / "board-tour.bin").read_bytes() drawn, blind = _system(image), _system(image) blind.vdp.rendering = False for n in range(8): assert _frame(blind, True) == _frame(drawn, True), f"frame {n}" assert state.machine_state(blind) == state.machine_state(drawn), f"frame {n}" assert not any(any(row) for row in blind.vdp.renderer.pixels) def test_a_frame_begun_by_the_last_step_of_an_undrawn_one() -> None: path = Path(__file__).parent / "test_roms" / "titan" / "titan-overdrive2.zip" if not path.exists(): pytest.skip("needs tests/test_roms/titan (scripts/fetch_references.py)") with zipfile.ZipFile(path) as archive: name = next(n for n in archive.namelist() if n.lower().endswith((".bin", ".md"))) image = archive.read(name) assert _same_runs(image, frames=220, every=2, machine="megadrive") > 0 def _collider() -> bytes: root = os.environ.get("NO_INTRO_DIR") path = Path(root) / COLLIDER if root else None if path is None or not path.exists(): pytest.skip(f"needs $NO_INTRO_DIR/{COLLIDER}") with zipfile.ZipFile(path) as archive: return archive.read(archive.namelist()[0]) def test_the_collision_flag_with_drawing_off(monkeypatch: pytest.MonkeyPatch) -> None: image = _collider() seen = [] read_word = VDP.read_word def watched(self: VDP, offset: int, mclk: int) -> int: value = read_word(self, offset, mclk) if (offset >> 2) & 7 == 1 and value & 0x20 and self.undrawn_frame: seen.append(self.frame) return value monkeypatch.setattr(VDP, "read_word", watched) _same_runs(image, frames=120, every=3) assert seen, "the game never read the collision flag set in an undrawn frame"