# SPDX-License-Identifier: MIT AND BSD-3-Clause # Tables from MAME 0.285's ssp1601d.cpp (BSD-3-Clause): notice in cpu.py and in NOTICE.md. # # The mnemonic tables here (CONDITIONS, ACCUMULATOR_OPS and the register names) are taken from # MAME 0.285's src/devices/cpu/ssp1601/ssp1601d.cpp, under the BSD-3-Clause licence, copyright # holders Pierpaolo Prazzoli and Grazvydas Ignotas. Its full notice (copyright line, the three # conditions and the disclaimer) is kept in cpu.py, beside the transcription of ssp1601.cpp, and # applies here too (docs/provenance.md). """A disassembler for the SSP1601, over the same opcode map as :mod:`ssp1601_python.cpu`. The mnemonics follow MAME's ``ssp1601d.cpp`` (itself the Samsung assembler's notation, per notaz's ``svpdoc.txt``): destination first, ``(ri)`` is RAM through a pointer, ``((ri))`` program memory through a RAM word. """ from __future__ import annotations from collections.abc import Callable REGISTERS = ("-", "X", "Y", "A", "ST", "STACK", "PC", "P", "EXT0", "EXT1", "EXT2", "EXT3", "EXT4", "EXT5", "EXT6", "AL") # fmt: skip CONDITIONS = ("always", "RESERVED", "gpi0", "gpi1", "l", "z", "ov", "n", "diof", "gpi2", "gpi3", "RESERVED", "RESERVED", "RESERVED", "RESERVED", "RESERVED") # fmt: skip ACCUMULATOR_OPS = ("ror", "rol", "shr", "shl", "inc", "dec", "neg", "abs") ARITHMETIC = ("", "sub", "", "cmp", "add", "and", "or", "eor") _MODIFIER = ("", "+!", "-", "+") _FIXED = ("|00", "|01", "|10", "|11") def _pointer(op: int, high: bool = False) -> str: """``r0``-``r7`` with the opcode's modifier; r3 and r7 take the short form. The low pointer of a multiply is ``op & 3`` in the bank bit 8 selects (RAM0 for the multiplies); ``high`` is the multiply's second pointer, always in RAM1.""" if high: index, mod, bank = (op >> 4) & 3, (op >> 6) & 3, 4 else: index, mod, bank = op & 3, (op >> 2) & 3, (op >> 6) & 4 return f"r{index + bank}" + (_FIXED[mod] if index == 3 else _MODIFIER[mod]) def _condition(op: int) -> str: if op & 0xF0: return f"{CONDITIONS[(op >> 4) & 15]}={(op >> 8) & 1}" return CONDITIONS[0] def disassemble(read: Callable[[int], int], pc: int) -> tuple[str, int]: """The instruction at word address ``pc`` and its length in words.""" op = read(pc) group = op >> 9 d = REGISTERS[(op >> 4) & 15] s = REGISTERS[op & 15] arith = ARITHMETIC[op >> 13] kind = group & 0x1F if group == 0x00: if op == 0: return "nop", 1 if op & 0xFF == 0x65: return "ret", 1 return f"ld {d}, {s}", 1 if group == 0x01: return f"ld {d}, ({_pointer(op)})", 1 if group == 0x02: return f"ld ({_pointer(op)}), {d}", 1 if group == 0x03: return f"ld A, {op & 0x1FF:X}", 1 if group == 0x04: return f"ld {d}, {read(pc + 1):X}", 2 if group == 0x05: return f"ld {d}, (({_pointer(op)}))", 1 if group == 0x06: return f"ld ({_pointer(op)}), {read(pc + 1):X}", 2 if group == 0x07: return f"ld {op & 0x1FF:X}, A", 1 if group == 0x09: return f"ld {d}, {_pointer(op)}", 1 if group == 0x0A: return f"ld {_pointer(op)}, {d}", 1 if 0x0C <= group <= 0x0F: return f"ldi r{(op >> 8) & 7}, {op & 0xFF:X}", 1 if group == 0x24: return f"call {_condition(op)}, {read(pc + 1):X}", 2 if group == 0x25: return f"ld {d}, (A)", 1 if group == 0x26: return f"bra {_condition(op)}, {read(pc + 1):X}", 2 if group == 0x48: return f"mod {_condition(op)}, {ACCUMULATOR_OPS[op & 7]}", 1 if group in (0x1B, 0x4B, 0x5B): name = {0x1B: "mpys", 0x4B: "mpya", 0x5B: "mld"}[group] return f"{name} ({_pointer(op, True)}), ({_pointer(op & ~0x100)}), {(op >> 8) & 1}", 1 if arith and group >> 4 == op >> 13 and kind in (0, 1, 3, 4, 5, 9, 12): if kind == 0: return f"{arith} A, {s}", 1 if kind == 1: return f"{arith} A, ({_pointer(op)})", 1 if kind == 3: return f"{arith} A, {op & 0x1FF:X}", 1 if kind == 4: return f"{arith}i A, {read(pc + 1):X}", 2 if kind == 5: return f"{arith} A, (({_pointer(op)}))", 1 if kind == 9: return f"{arith} A, {_pointer(op)}", 1 return f"{arith}i {op & 0xFF:X}", 1 return f"OP = {op:04X}", 1