"""Rung 0: the core's opcode map is the datasheet's (DS Table 5-4). tests/datasheet.py transcribes Table 5-4 as mnemonic and mode for each opcode; every entry of _dispatch.OPCODES must name that mnemonic's handler and a mode that computes that addressing mode. """ import pytest from datasheet import OPCODES as TABLE from w65c816._dispatch import OPCODES from w65c816.cpu import W65C816 # Table 5-6's mode names, and the _ea_ methods that compute each. MODES = { "a": {"abs"}, "a,x": {"abx", "abx_w"}, "a,y": {"aby", "aby_w"}, "al": {"long"}, "al,x": {"long_x"}, "d": {"dp"}, "d,x": {"dpx"}, "d,y": {"dpy"}, "(d,x)": {"idx"}, "(d)": {"ind"}, "(d),y": {"idy", "idy_w"}, "[d]": {"ind_long"}, "[d],y": {"ind_long_y"}, "d,s": {"sr"}, "(d,s),y": {"sr_y"}, "#": {"imm_m", "imm_x", None}, } # fmt: skip OWN_CYCLES = {"i", "s", "r", "rl", "xyc", "(a)", "(a,x)", "A"} # handlers make their own @pytest.mark.parametrize("opcode", range(256), ids=lambda opcode: f"{opcode:02x}") def test_opcode_is_the_datasheets(opcode: int) -> None: mnemonic, mode = TABLE[opcode] handler, ea = OPCODES[opcode] assert handler.split("_")[0] == mnemonic.lower(), (handler, mnemonic) if mode in OWN_CYCLES or (mnemonic in {"JMP", "JML", "JSR", "JSL"}): assert ea is None else: assert ea in MODES[mode], (ea, mode) if mode == "#" and ea is not None: index = mnemonic in {"LDX", "LDY", "CPX", "CPY"} assert ea == ("imm_x" if index else "imm_m") def test_every_handler_and_mode_exists() -> None: for handler, ea in OPCODES.values(): assert callable(getattr(W65C816, f"_op_{handler}")) if ea is not None: assert callable(getattr(W65C816, f"_ea_{ea}"))