# SPDX-License-Identifier: MIT """The channels docs/domains.md declares, checked against the code. The SH-2 pair is one domain, and ``sh2side.py`` is the only module that touches a chip's state (its clock, skew, waits, DREQ and IRL lines): the 68000's side (``slot.py``), the PWM and the VDP reach the pair through ``SH2Side``'s named calls. The only call into the Mega Drive board's own state from the 32X side is ``Sega32X.video_lines_to`` (socket 6). A test fails when a module starts reaching across some other way (charter, "Domains and channels"). """ from __future__ import annotations import ast from pathlib import Path SRC = Path(__file__).resolve().parents[1] / "src" / "sega32x_python" #: A chip's own state: only sh2side.py may name these. CHIP_STATE = {"chips", "skew", "wait", "dreq0", "dreq1", "irl", "rv_blocked"} #: What the other modules may call on ``SH2Side`` (``sh2`` / ``device.sh2``). SH2_CALLS = { # running "running", "catch_up", "set_run", "rewire", "instructions", "on_step", # interrupts in (the 68000's side, the VDP, the PWM) "assert_source", "clear_source", "set_mask", "masks", "has_source", "raise_pwm", "raise_hint", "raise_vint", # DREQ, waits and time "set_dreq0", "set_dreq1", "hold", "state", "cycle_end", "oldest", "fifo_status", # the cartridge, against the 68000 "m68k_cartridge", } # fmt: skip #: Host operations that may see everything (whole-machine save and load). HOST = {"savestate.py"} def _tree(path: Path) -> ast.Module: return ast.parse(path.read_text(encoding="utf-8")) def _modules() -> list[Path]: return sorted(p for p in SRC.glob("*.py") if p.name != "sh2side.py" and p.name not in HOST) def test_only_sh2side_touches_a_chip(): for path in _modules(): for node in ast.walk(_tree(path)): if isinstance(node, ast.Attribute): assert node.attr not in CHIP_STATE, f"{path.name}:{node.lineno} .{node.attr}" def test_the_pair_is_reached_through_its_named_calls(): used = set() for path in _modules(): for node in ast.walk(_tree(path)): if isinstance(node, ast.Attribute) and ( (isinstance(node.value, ast.Attribute) and node.value.attr == "sh2") or (isinstance(node.value, ast.Name) and node.value.id == "sh2") ): used.add(node.attr) assert used <= SH2_CALLS, used - SH2_CALLS def test_one_call_into_the_mega_drive_vdp(): """``board.vdp`` appears once in the 32X's code: socket 6's ``video_lines_to``.""" def on_board(node: ast.AST) -> bool: if not isinstance(node, ast.Attribute) or node.attr != "vdp": return False value = node.value if isinstance(value, ast.Name): return value.id == "board" return isinstance(value, ast.Attribute) and value.attr == "board" found = [ (path.name, node.lineno) for path in sorted(SRC.glob("*.py")) for node in ast.walk(_tree(path)) if on_board(node) ] assert len(found) == 1 and found[0][0] == "slot.py", found def test_the_declared_calls_exist(): from sega32x_python.sh2side import SH2Side from sega32x_python.slot import Sega32X attributes = {"running", "instructions", "on_step", "masks"} for name in SH2_CALLS - attributes: assert callable(getattr(SH2Side, name)), name device_calls = ( "video_lines_to", "shared_cartridge", "sh2_register_read", "sh2_register_write", "sh2_framebuffer_read", "sh2_framebuffer_write", ) for name in device_calls: assert callable(getattr(Sega32X, name)), name