"""Readability contract for the instruction core, enforced the way correctness is. Following 6502-python's and z80-python's tests/test_readability.py: every opcode handler must be findable by the mnemonic a 65816 programmer would grep for, must live in the module that owns its instruction group, and must say where its rule comes from. Checked from the source with :mod:`ast`, without importing it: 1. Every ``_op_*`` method's docstring headline is ``MNEMONIC[ form] -- text (DS Table 5-5 p. N).``: the page of WDC's W65C816S datasheet (2024-03-13, pinned in docs/validation.md) whose Table 5-5, "Operation, Operation Codes, and Status Register", gives that instruction's operation. 2. The page is the right one, as the datasheet prints it. 3. The handler lives in the module that owns that mnemonic, is named after it, and every one of the 92 mnemonics has a handler. """ import ast import re from pathlib import Path import pytest SRC = Path(__file__).resolve().parents[1] / "src" / "w65c816" #: DS Table 5-5: the page each mnemonic's row is printed on. TABLE_5_5 = { mnemonic: page for page, mnemonics in { 32: "ADC AND ASL BCC BCS BEQ BIT BMI BNE BPL BRA BRK BRL BVC BVS CLC CLD CLI CLV " "CMP COP CPX CPY DEC DEX DEY EOR INC INX INY JML JMP JSL JSR LDA", 33: "LDX LDY LSR MVN MVP NOP ORA PEA PEI PER PHA PHB PHD PHK PHP PHX PHY PLA PLB " "PLD PLP PLX PLY REP ROL ROR RTI RTL RTS SBC SEC SED SEI SEP STA STP STX", 34: "STY STZ TAX TAY TCD TCS TDC TRB TSB TSC TSX TXA TXS TXY TYA TYX WAI WDM XBA XCE", }.items() for mnemonic in mnemonics.split() } OWNERS: dict[str, frozenset[str]] = { module: frozenset(mnemonics.split()) for module, mnemonics in { "_alu.py": "ADC SBC AND ORA EOR CMP CPX CPY BIT TSB TRB", "_loads.py": "LDA LDX LDY STA STX STY STZ TAX TAY TXA TYA TXY TYX TSX TXS TCS TSC " "TCD TDC XBA XCE INX INY DEX DEY", "_shifts.py": "ASL LSR ROL ROR INC DEC", "_block.py": "MVN MVP", "_control.py": "BPL BMI BVC BVS BCC BCS BNE BEQ BRA BRL JMP JML JSR JSL RTS RTL RTI " "PHA PHX PHY PHP PHB PHK PHD PLA PLX PLY PLP PLB PLD PEA PEI PER CLC SEC CLD SED " "CLV CLI SEI REP SEP NOP WDM WAI STP BRK COP", }.items() } HEADLINE = re.compile(r"^(?P[A-Z]{3})(?P
[^-]*?) -- (?P.+)$") CITATION = re.compile(r"\(DS Table 5-5 p\. (?P\d+)\)\.$") def handlers() -> list[tuple[str, ast.FunctionDef]]: found = [] for path in sorted(SRC.glob("_*.py")): tree = ast.parse(path.read_text(encoding="utf-8")) for cls in (node for node in tree.body if isinstance(node, ast.ClassDef)): found += [ (path.name, node) for node in cls.body if isinstance(node, ast.FunctionDef) and node.name.startswith("_op_") ] assert found return found HANDLERS = handlers() def headline(node: ast.FunctionDef) -> str: docstring = ast.get_docstring(node) or "" return " ".join(docstring.split("\n\n")[0].split()) @pytest.mark.parametrize(("module", "node"), HANDLERS, ids=[node.name for _, node in HANDLERS]) def test_handler_headline_names_mnemonic_and_cites_its_own_page( module: str, node: ast.FunctionDef ) -> None: line = headline(node) match = HEADLINE.match(line) assert match, f"{node.name}: headline must read 'MNEMONIC -- text (SOURCE).', got {line!r}" mnemonic = match["mnemonic"] citation = CITATION.search(line) assert citation, f"{node.name}: headline must end with (DS Table 5-5 p. N)." assert int(citation["page"]) == TABLE_5_5[mnemonic], ( f"{node.name}: {mnemonic} is on DS p. {TABLE_5_5[mnemonic]}" ) @pytest.mark.parametrize(("module", "node"), HANDLERS, ids=[node.name for _, node in HANDLERS]) def test_handler_lives_in_its_module_and_is_named_after_its_mnemonic( module: str, node: ast.FunctionDef ) -> None: mnemonic = HEADLINE.match(headline(node))["mnemonic"] assert mnemonic in OWNERS[module], f"{node.name}: {mnemonic} belongs elsewhere" assert node.name.removeprefix("_op_").split("_")[0] == mnemonic.lower() def test_every_mnemonic_has_a_handler_and_no_handler_is_defined_twice() -> None: names = [node.name for _, node in HANDLERS] assert len(names) == len(set(names)), "a duplicate would be shadowed by the MRO" claimed = {HEADLINE.match(headline(node))["mnemonic"] for _, node in HANDLERS} assert claimed == set(TABLE_5_5) assert len(TABLE_5_5) == 92 assert set().union(*OWNERS.values()) == set(TABLE_5_5)