# SPDX-License-Identifier: MIT """Rung 0: every word of all seven programs decodes, and the decode matches MAME's. MAME 0.285's ``unidasm -arch upd7725`` (the same ``necdsp_disassembler`` the debugger's ``dasm`` uses; emulator tier, a detector) disassembles each program image. The test rebuilds MAME's text from this core's field extraction (``mame_text``) and compares every word; it also maps MAME's text to NEC's syntax and compares it with ``disasm.py``. MAME prints moves ``mov src,dst``, omits a move whose DST is 0, and omits the uPD96050's NA bank bits. """ from __future__ import annotations import shutil import subprocess import pytest from conftest import PROGRAMS, require_image from upd7725_python import _fields as f from upd7725_python.disasm import disassemble UNIDASM = shutil.which("unidasm") or shutil.which("unidasm", path="/usr/games") _MAME_BRCH = {code: name.lower() for code, name in f.BRCH.items()} _MAME_BRCH[f.JMP] = "ljmp" _MAME_BRCH[f.CALL] = "lcall" _MAME_SRC = [name.lower() for name in f.SRC] _MAME_SRC[0] = "trb" _MAME_DST = [name.lstrip("@").lower() for name in f.DST] def mame_text(word: int) -> str: """MAME's disassembly (``dasm7725.cpp``), rebuilt from this core's fields.""" kind = f.kind(word) if kind in (f.OP, f.RT): operation = f.alu(word) text = f"{f.ALU[operation].lower():<5}" if operation < 8: text += ("ram", "idb", "m", "n")[f.p_select(word)] + "," text += "ab"[f.asl(word)] if f.dst(word): text += f" | mov {_MAME_SRC[f.src(word)]},{_MAME_DST[f.dst(word)]}" if f.dpl(word): text += " | " + f.DPL[f.dpl(word)].lower() if f.dphm(word): text += f" | m{f.dphm(word):x}" if f.rpdcr(word): text += " | rpdec" if kind == f.RT: text += " | ret" return text if kind == f.JP: name = _MAME_BRCH.get(f.brch(word)) prefix = "?????? " if name is None else name + " " return f"{prefix}${f.na(word):x}" return f"ld ${f.immediate(word):x},{_MAME_DST[f.dst(word)]}" def nec_from_mame(mame: str, word: int) -> str: """MAME's text in NEC syntax, for the words where the mapping is mechanical.""" parts = mame.split(" | ") out = [] head = parts[0].split() if f.kind(word) in (f.OP, f.RT): operation = head[0].upper() operands = head[1].split(",") accumulator = "ACC" + operands[-1].upper() if operation == "NOP": out.append("NOP") elif len(operands) == 2 and f.alu(word) in f.BINARY_ALU: out.append(f"{operation} {accumulator},{operands[0].upper()}") else: out.append(f"{operation} {accumulator}") rest = parts[1:] if rest and rest[0].startswith("mov "): source, destination = rest[0][4:].split(",") source = "NON" if source == "trb" else source.upper() out.append(f"MOV @{destination.upper()},{source}") rest = rest[1:] elif f.src(word): # MAME drops a move to @NON; NEC keeps it (it may set RQM) out.append(f"MOV @NON,{f.SRC[f.src(word)]}") out.extend(part.upper() for part in rest) return " | ".join(out) if f.kind(word) == f.LD: value, destination = head[1].split(",") return f"LDI @{destination.upper()},{int(value[1:], 16):04X}h" return None # JP: NEC prints the effective target, MAME the NA field; compared by fields @pytest.mark.parametrize("name", sorted(PROGRAMS)) def test_every_word_matches_mame(name, tmp_path): if UNIDASM is None: pytest.skip("MAME's unidasm is not installed") program, _ = require_image(name) image = tmp_path / "program.bin" # the PROM layout of the program part only image.write_bytes(b"".join(word.to_bytes(3, "big") + b"\xff" for word in program)) result = subprocess.run( ["nice", "-n", "19", UNIDASM, str(image), "-arch", "upd7725"], capture_output=True, text=True, check=True, ) lines = [line for line in result.stdout.splitlines() if ": " in line] assert len(lines) == len(program) mismatches = [] for address, line in enumerate(lines): where, rest = line.split(": ", 1) raw, text = rest.split(None, 1) assert int(where, 16) == address word = program[address] assert int(raw, 16) >> 8 == word if mame_text(word) != text: mismatches.append((address, text, mame_text(word))) continue nec = nec_from_mame(text, word) if nec is not None and nec != disassemble(word, address).text: mismatches.append((address, nec, disassemble(word, address).text)) assert mismatches == [] @pytest.mark.parametrize("name", sorted(PROGRAMS)) def test_every_word_decodes(name): program, _ = require_image(name) for address, word in enumerate(program): text = disassemble(word, address).text assert text if f.kind(word) == f.JP and f.brch(word) not in f.BRCH: pytest.fail(f"{name} {address:04X}: unknown BRCH {f.brch(word):03X}h")