# SPDX-License-Identifier: MIT """Disassembly in NEC's syntax (handoff-brief decision 9), from the same field extraction the core uses. An OP or RT word is a list of parts joined by `` | ``: the ALU operation (``NOP``, ``INC ACCA``, ``ADD ACCB,RAM``), the move in the data sheet's destination-first order (``MOV @DR,A``, DS p.36 "MOV @DR, xxx"), then ``DPINC``/``DPDEC``/``DPCLR``, ``M1``-``MF``, ``RPDEC`` and ``RET`` when present. A move whose source and destination are both 0 is omitted (``MOV @NON,NON`` changes nothing); one with destination ``@NON`` and another source is kept, because reading ``DR`` there still sets RQM. JP prints the branch and its effective target (``JRQM 004h``; ``HJMP``/``HCALL`` with bit 13 set); LD prints ``LDI @SR,0400h`` (DS p.37 "LDI @SR, Imm"). Unknown branch codes print as ``JP? 1FFh,004h``: they do nothing. """ from __future__ import annotations from collections.abc import Sequence from dataclasses import dataclass from upd7725_python import _fields as f @dataclass(frozen=True, slots=True) class Instruction: """One decoded word.""" address: int word: int kind: int # f.OP, f.RT, f.JP, f.LD parts: tuple[str, ...] @property def text(self) -> str: return " | ".join(self.parts) @property def mnemonic(self) -> str: return self.parts[0].split(" ", 1)[0] def jump_target(word: int) -> int: """The address a JP word names: NA plus the uPD96050's bank bits (U11), and bit 13 for HJMP/HCALL. A conditional jump keeps the current PC bit 13 at run time (not shown).""" target = f.bank(word) << 11 | f.na(word) if f.brch(word) in (f.HJMP, f.HCALL): target |= 0x2000 return target def disassemble(word: int, address: int = 0) -> Instruction: """Decode one 24-bit word.""" kind = f.kind(word) if kind in (f.OP, f.RT): operation = f.alu(word) accumulator = f.ACC[f.asl(word)] if operation == 0: parts = ["NOP"] elif operation in f.BINARY_ALU: parts = [f"{f.ALU[operation]} {accumulator},{f.P_SELECT[f.p_select(word)]}"] else: parts = [f"{f.ALU[operation]} {accumulator}"] if f.src(word) or f.dst(word): parts.append(f"MOV {f.DST[f.dst(word)]},{f.SRC[f.src(word)]}") if f.dpl(word): parts.append(f.DPL[f.dpl(word)]) if f.dphm(word): parts.append(f"M{f.dphm(word):X}") if f.rpdcr(word): parts.append("RPDEC") if kind == f.RT: parts.append("RET") return Instruction(address, word, kind, tuple(parts)) if kind == f.JP: name = f.BRCH.get(f.brch(word)) if name is None: text = f"JP? {f.brch(word):03X}h,{jump_target(word):03X}h" elif f.brch(word) == f.JMPSO: text = "JMPSO" else: text = f"{name} {jump_target(word):03X}h" return Instruction(address, word, kind, (text,)) return Instruction(address, word, kind, (f"LDI {f.DST[f.dst(word)]},{f.immediate(word):04X}h",)) def disassemble_program( program: Sequence[int], start: int = 0, count: int | None = None ) -> list[Instruction]: """Decode ``count`` words (default: to the end) from ``start``.""" end = len(program) if count is None else min(len(program), start + count) return [disassemble(program[address], address) for address in range(start, end)] def format_instruction(instruction: Instruction) -> str: return f"{instruction.address:04X} {instruction.word:06X} {instruction.text}" __all__ = [ "Instruction", "disassemble", "disassemble_program", "format_instruction", "jump_target", ]