# SPDX-License-Identifier: MIT """The board's domains and channels as docs/domains.md declares them, checked against the code. Two domains: the S-CPU's (master clock: the 65816, DMA/HDMA, the timers, WRAM, both PPUs, the cartridge) and the APU's (its own resonator: the S-SMP and the S-DSP sharing ARAM). The one channel between them is the four-port latch pair, ``smp.CPUPorts``; the S-CPU's side reaches the APU only through ``APU.cpu_read``/``cpu_write``/``catch_up``. These tests fail when a module starts reaching across the boundary some other way (charter, "Domains and channels"). """ from __future__ import annotations import ast from pathlib import Path SRC = Path(__file__).resolve().parents[1] / "src" / "snes_python" #: The S-CPU domain's modules; none may import or name the APU's insides. CPU_SIDE = ("board.py", "dma.py", "ppu.py", "ppu_render.py", "ppu_png.py", "cartridge.py", "controllers.py", "machines.py") # fmt: skip #: The APU domain's modules; none may import the S-CPU side. APU_SIDE = ("smp.py", "spc_file.py") #: Attribute names that are the APU's own state. #: (``ports`` is not in it: the board's ``ports`` are the joypad ports.) APU_STATE = {"smp", "dsp", "aram", "from_cpu", "to_cpu", "input_ports", "output_ports"} #: The channel's S-CPU-side calls (``apu.APU`` and ``apu.PortStub`` both answer them). CPU_CALLS = {"cpu_read", "cpu_write", "catch_up"} #: What ``apu.APU`` (the APU domain's scheduler) may touch on its S-SMP. APU_REACH = {"cpu", "clock", "dsp", "ports", "cpu_read_port", "cpu_write_port"} def _tree(name: str) -> ast.Module: return ast.parse((SRC / name).read_text(encoding="utf-8")) def _imports(tree: ast.Module) -> set[str]: found: set[str] = set() for node in ast.walk(tree): if isinstance(node, ast.ImportFrom) and node.module: found.add(node.module) elif isinstance(node, ast.Import): found.update(alias.name for alias in node.names) return found def test_cpu_side_names_none_of_the_apu_state(): for name in CPU_SIDE: tree = _tree(name) for module in _imports(tree): assert not module.startswith(("snes_python.smp", "snes_python.dsp")), (name, module) for node in ast.walk(tree): if isinstance(node, ast.Attribute): assert node.attr not in APU_STATE, f"{name}:{node.lineno} .{node.attr}" def test_cpu_side_reaches_the_apu_only_through_the_channel(): calls = set() for node in ast.walk(_tree("board.py")): if ( isinstance(node, ast.Attribute) and isinstance(node.value, ast.Attribute) and node.value.attr == "apu" ): calls.add(node.attr) assert calls == CPU_CALLS def test_apu_side_imports_nothing_of_the_cpu_side(): for name in APU_SIDE: for module in _imports(_tree(name)): top = module.removeprefix("snes_python.").split(".")[0] assert f"{top}.py" not in CPU_SIDE and top != "apu", (name, module) def test_the_apu_scheduler_touches_only_its_declared_reach(): reach = set() for node in ast.walk(_tree("apu.py")): if ( isinstance(node, ast.Attribute) and isinstance(node.value, ast.Attribute) and node.value.attr == "smp" ): reach.add(node.attr) assert reach <= APU_REACH, reach - APU_REACH def test_the_ports_are_the_only_shared_state(): """The S-SMP's port latches live in ``CPUPorts`` and nowhere else on the S-SMP.""" from snes_python.smp import SMP, CPUPorts smp = SMP() assert isinstance(smp.ports, CPUPorts) assert smp.input_ports is smp.ports.from_cpu assert smp.output_ports is smp.ports.to_cpu smp.cpu_write_port(1, 0x1234) assert smp.ports.smp_read(1) == 0x34 smp.ports.smp_write(2, 0x56, smp.clock + 10) assert smp.cpu_read_port(2, smp.clock) == 0 # the store is later than the read assert smp.cpu_read_port(2, smp.clock + 10) == 0x56