# SPDX-License-Identifier: MIT """The disassembler against anomie's syntax, and against MAME's ``spc700ds`` on the same trace.""" import re from pathlib import Path import pytest from spc700_python._opcodes import OPCODES from spc700_python.disasm import Instruction, disassemble, disassemble_bytes from spc700_python.replay import replay_file ROOT = Path(__file__).resolve().parents[1] TRACE = ROOT / "tests" / "mame_traces" / "spctest" / "spc.trace" LOG = TRACE.with_name("error.log") IPL = ROOT / "reference" / "build" / "iplrom.bin" @pytest.mark.parametrize( "data,address,text", [ (b"\x00", 0, "NOP"), (b"\x01", 0, "TCALL 0"), (b"\xf1", 0, "TCALL 15"), (b"\x02\x12", 0, "SET1 $12.0"), (b"\xe2\x12", 0, "SET1 $12.7"), (b"\x03\x12\x02", 0x100, "BBS $12.0, $0105"), (b"\x04\x12", 0, "OR A, $12"), (b"\x05\x34\x12", 0, "OR A, !$1234"), (b"\x07\x12", 0, "OR A, [$12+X]"), (b"\x08\x12", 0, "OR A, #$12"), (b"\x09\x34\x12", 0, "OR $12, $34"), # stored source first (b"\x0a\x34\x52", 0, "OR1 C, $1234.2"), (b"\x2a\x34\x52", 0, "OR1 C, /$1234.2"), (b"\xca\x34\x52", 0, "MOV1 $1234.2, C"), (b"\x17\x12", 0, "OR A, [$12]+Y"), (b"\x18\x34\x12", 0, "OR $12, #$34"), # stored immediate first (b"\x1f\x34\x12", 0, "JMP [!$1234+X]"), (b"\x2f\xfe", 0x300, "BRA $0300"), (b"\x4f\x80", 0, "PCALL $80"), (b"\x6e\x12\xfd", 0x300, "DBNZ $12, $0300"), (b"\xba\x12", 0, "MOVW YA, $12"), (b"\xcf", 0, "MUL YA"), (b"\x9e", 0, "DIV YA, X"), (b"\xaf", 0, "MOV (X)+, A"), (b"\xd9\x12", 0, "MOV $12+Y, X"), (b"\xde\x12\x00", 0, "CBNE $12+X, $0003"), ], ) def test_text(data, address, text): assert disassemble_bytes(data, address).text == text def test_every_opcode_decodes_at_its_length_and_wraps_pc(): for opcode, entry in enumerate(OPCODES): memory = {0xFFFF: opcode, 0x0000: 0x12, 0x0001: 0x34} instruction = disassemble(memory.__getitem__, 0xFFFF) assert instruction.length == entry.length assert instruction.mnemonic == entry.mnemonic def test_instruction_is_validated(): with pytest.raises(ValueError): Instruction(0x10000, b"\x00", "NOP") with pytest.raises(ValueError): Instruction(0, b"", "NOP") with pytest.raises(ValueError): disassemble_bytes(b"\x05\x34") def to_mame(text: str) -> str: """Our text in the shape of MAME's ``spc700ds``, in lowercase with no spaces or ``$``. MAME's differences are syntax: RETI for RET1, parentheses for brackets, no ``!``, DAA, NOT1 and ADDW/SUBW/CMPW with their fixed operands left out. """ word, _, operands = text.partition(" ") word = {"RET1": "RETI"}.get(word, word) if word in ("DAA", "NOT1"): operands = "" if word in ("ADDW", "SUBW", "CMPW"): operands = operands.removeprefix("YA, ") return normalise(f"{word} {operands}") def normalise(text: str) -> str: text = text.lower().translate(str.maketrans("[]", "()", "!$ ")) return text @pytest.mark.skipif(not (TRACE.exists() and LOG.exists()), reason="needs mame_trace.py --io") @pytest.mark.skipif(not IPL.exists(), reason="run scripts/build_referees.py first") @pytest.mark.slow def test_against_mame_on_the_spctest_trace(): """Detector tier: MAME's ``spc700ds`` on the same trace: every instruction the replay runs is disassembled by both, and all the forms in spctest compare.""" with TRACE.open(encoding="latin-1") as stream: lines = [line for line in stream if re.match(r"[0-9A-F]{4}: ", line)] seen: set[str] = set() def observe(position, state, host): text = disassemble(host.peek, state.pc).text if text in seen: return seen.add(text) theirs = normalise(lines[position].split(": ", 1)[1].split("//")[0].strip()) assert to_mame(text) == theirs, f"{state.pc:04X} {text!r} against {lines[position]!r}" result = replay_file(LOG, IPL.read_bytes(), observer=observe) assert result.ok assert len(seen) > 2000