# SPDX-License-Identifier: MIT """The channels docs/domains.md declares, checked against the code. The Mega Drive side reaches the CD only through megadrive-python's sockets on ``MegaCD``; the sub side's units never name the Mega Drive board; the CDC's DMA reaches the sub side's memories only through ``MegaCD.cdc_dma_*``; the gate array reaches the drive only through its host registers. 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" / "segacd_python" #: The sub side's units: on the sub CPU's clock, with no view of the Mega Drive. SUB_SIDE = ("gatearray.py", "cdc.py", "cdd.py", "graphics.py", "pcm.py", "memory.py") #: The CDC's DMA channel into the memories (megacd.py). CDC_DMA = {"cdc_dma_word_ram_held", "cdc_dma_prg_held", "cdc_dma_pcm", "cdc_dma_prg", "cdc_dma_word_ram"} # fmt: skip #: The drive's host channel as the gate array uses it (cdd.py). CDD_HOST = {"control", "set_control", "set_fader", "status_word", "command_word", "write_command", "subcode_address", "subcode_word", "reset_host"} # fmt: skip def _tree(name: str) -> ast.Module: return ast.parse((SRC / name).read_text(encoding="utf-8")) def _reach(tree: ast.Module, owner: str) -> set[str]: """Attributes taken of ``owner`` or ``self.owner``.""" found = set() for node in ast.walk(tree): if not isinstance(node, ast.Attribute): continue value = node.value if (isinstance(value, ast.Name) and value.id == owner) or ( isinstance(value, ast.Attribute) and value.attr == owner ): found.add(node.attr) return found def test_sub_side_never_names_the_mega_drive(): for name in SUB_SIDE: tree = _tree(name) for node in ast.walk(tree): if isinstance(node, ast.ImportFrom) and node.module: assert not node.module.startswith("megadrive_python"), (name, node.module) if isinstance(node, ast.Attribute): assert node.attr not in {"board", "game_cartridge"}, f"{name}:{node.lineno}" def test_cdc_dma_reaches_memory_only_through_the_device_calls(): assert _reach(_tree("cdc.py"), "device") == CDC_DMA def test_gate_array_reaches_the_drive_only_through_its_host_registers(): assert _reach(_tree("gatearray.py"), "cdd") <= CDD_HOST def test_the_declared_calls_exist(): from segacd_python.cdd import CDD from segacd_python.megacd import MegaCD for name in CDC_DMA: assert callable(getattr(MegaCD, name)), name for name in CDD_HOST: assert callable(getattr(CDD, name)), name def test_cdd_host_registers(): from segacd_python.cdd import CDD cdd = CDD() cdd.write_command(8, 0x0305, True, True) assert cdd.command_word(8) == 0x0305 assert cdd.new_command == cdd.command # nibble 9 written: the command is sent cdd.status[2], cdd.status[3] = 0xA, 0xB assert cdd.status_word(2) == 0x0A0B