# SPDX-License-Identifier: MIT AND ISC """The instruction encoding: one table of the 256 high bytes, shared by the CPU and disasm. Every instruction is one 16-bit word (start-here.md "Instruction encoding"; byuu's decode table, ares ``instruction.cpp`` (ISC), cited, not copied). Bits 15-10 select one of 64 operation groups, bits 9-8 a sub-field (a shift, a destination, a flag, a byte lane or the far bit) and bits 7-0 an operand. :data:`OPS` gives, for each high byte (bits 15-8), the operation key the CPU dispatches on and the sub-field already decoded, so the low byte is the only thing a handler still reads. :func:`classify` names a word the way byuu's table does (the rung 0 check over all 65,536 words), :func:`decode` splits it into named fields for the disassembler, the debugger and the trace. """ from __future__ import annotations from dataclasses import dataclass #: The shift sub-field: A, A<<1, A<<8, A<<16 (U7; fullsnes A*2, A*100h, A*10000h). SHIFTS = (0, 1, 8, 16) #: Conditions of JMP/JSR by bits 12-10 of the word (high byte >> 2 & 7): always, then the #: flag that must be set. ares: 08 JMP, 0C EQ (Z), 10 GE (C), 14 MI (N), 18 VS (V). CONDITIONS = ("", "EQ", "GE", "MI", "VS") #: SKIP's flag by bits 9-8 (ares; fullsnes has "?" for 0: C10). SKIP_FLAGS = ("V", "C", "Z", "N") #: Register operand codes (start-here.md "Registers"; U5): name for the disassembler. REGISTER_NAMES: dict[int, str] = { 0x00: "A", 0x01: "MH", 0x02: "ML", 0x03: "MDR", 0x08: "ROM", 0x0C: "RAM", 0x13: "MAR", 0x1C: "DPR", 0x20: "PC", 0x28: "P", 0x2E: "BUS", # an external access with the ROM wait (U10) 0x2F: "BUSRAM", # the same with the RAM wait } #: The sixteen constant registers 50h-5Fh (U6; fullsnes, ares ``registers.cpp`` l.26-41 identical). CONSTANTS = ( 0x000000, 0xFFFFFF, 0x00FF00, 0xFF0000, 0x00FFFF, 0xFFFF00, 0x800000, 0x7FFFFF, 0x008000, 0x007FFF, 0xFF7FFF, 0xFFFF7F, 0x010000, 0xFEFFFF, 0x000100, 0x00FEFF, ) def register_name(code: int) -> str: """The operand's name: bit 7 is ignored, 70h-7Fh mirror R0-R15 (U5).""" code &= 0x7F if code >= 0x60: return f"R{code & 0xF}" if code >= 0x50: return f"#${CONSTANTS[code & 0xF]:06X}" return REGISTER_NAMES.get(code, f"?{code:02X}") def _build() -> tuple[tuple[str, int], ...]: """(operation key, sub-field) for each high byte; the key names a CPU handler.""" ops: list[tuple[str, int]] = [("nop", 0)] * 256 for high in range(256): group, sub = high >> 2, high & 3 if group in (0x02, 0x03, 0x04, 0x05, 0x06): # JMP, JMP EQ/GE/MI/VS: sub = far bit ops[high] = ("jmp", group - 0x02) elif group == 0x07: ops[high] = ("wait", 0) elif group == 0x09: ops[high] = ("skip", sub) elif group in (0x0A, 0x0B, 0x0C, 0x0D, 0x0E): # JSR family ops[high] = ("jsr", group - 0x0A) elif group == 0x0F: ops[high] = ("rts", 0) elif group == 0x10: ops[high] = ("inc_mar", 0) elif group == 0x12: ops[high] = ("cmpr_reg", sub) elif group == 0x13: ops[high] = ("cmpr_imm", sub) elif group == 0x14: ops[high] = ("cmp_reg", sub) elif group == 0x15: ops[high] = ("cmp_imm", sub) elif high == 0x59: ops[high] = ("sxb", 0) elif high == 0x5A: ops[high] = ("sxw", 0) elif group == 0x18: # LD A/MDR/MAR, reg and LD P, Rn ops[high] = ("ld_p_reg", 0) if sub == 3 else ("ld_reg", sub) elif group == 0x19: ops[high] = ("ld_imm", sub) elif group == 0x1A and sub < 3: ops[high] = ("rdram_a", sub) elif group == 0x1B and sub < 3: ops[high] = ("rdram_imm", sub) elif group == 0x1C: ops[high] = ("rdrom_a", 0) elif group == 0x1D: ops[high] = ("rdrom_imm", sub) elif high == 0x7C: ops[high] = ("ld_pl", 0) elif high == 0x7D: ops[high] = ("ld_ph", 0) elif group == 0x20: ops[high] = ("add_reg", sub) elif group == 0x21: ops[high] = ("add_imm", sub) elif group == 0x22: ops[high] = ("subr_reg", sub) elif group == 0x23: ops[high] = ("subr_imm", sub) elif group == 0x24: ops[high] = ("sub_reg", sub) elif group == 0x25: ops[high] = ("sub_imm", sub) elif group == 0x26: ops[high] = ("mul_reg", 0) elif group == 0x27: ops[high] = ("mul_imm", 0) elif group in (0x28, 0x2A, 0x2C, 0x2E): # XNOR, XOR, AND, OR with a register ops[high] = (("xnor", "xor", "and", "or")[(group - 0x28) >> 1] + "_reg", sub) elif group in (0x29, 0x2B, 0x2D, 0x2F): ops[high] = (("xnor", "xor", "and", "or")[(group - 0x29) >> 1] + "_imm", sub) elif 0x30 <= group <= 0x37: # SHR, ASR, ROR, SHL; even = register, odd = immediate name = ("shr", "asr", "ror", "shl")[(group - 0x30) >> 1] ops[high] = (name + ("_imm" if group & 1 else "_reg"), 0) elif high in (0xE0, 0xE1): ops[high] = ("st", sub) # 0: ST reg,A; 1: ST reg,MDR elif group == 0x3A and sub < 3: ops[high] = ("wrram_a", sub) elif group == 0x3B and sub < 3: ops[high] = ("wrram_imm", sub) elif group == 0x3C: ops[high] = ("swap", 0) elif group == 0x3E: ops[high] = ("clear", 0) elif group == 0x3F: ops[high] = ("halt", 0) # everything else stays a NOP: 00-07, 20-23, 44-47, 58, 5B, 5C-5F, 6B, 6F, 78-7B, # 7E-7F, E2-E7, EB, EF, F4-F7 (ares binds them all to NOP) return tuple(ops) #: high byte -> (operation key, sub-field) OPS = _build() def classify(word: int) -> str: """The word's operation in byuu's table's terms (ares ``instruction.cpp`` comments), e.g. ``JMP EQ``, ``SKIP C``, ``ADD A<> 8 & 0xFF] if key in ("jmp", "jsr"): condition = CONDITIONS[sub] return key.upper() + (f" {condition}" if condition else "") if key == "skip": return f"SKIP {SKIP_FLAGS[sub]}" return _CLASS_NAMES[key][sub] if key in _CLASS_NAMES else _SIMPLE_NAMES[key] _SIMPLE_NAMES = { "nop": "NOP", "wait": "WAIT", "rts": "RTS", "inc_mar": "INC MAR", "cmpr_reg": "CMPR A< Fields: word &= 0xFFFF key, sub = OPS[word >> 8] low = word & 0xFF if key in ("jmp", "jsr"): return Fields(word, key, sub, target=low, far=bool(word & 0x200), condition=CONDITIONS[sub]) stem = key.rsplit("_", 1)[0] shift = SHIFTS[sub] if stem in _SHIFTED else None if key in _REG_KEYS: return Fields(word, key, sub, shift=shift, register=low & 0x7F) if key in ("ld_p_reg", "swap"): return Fields(word, key, sub, register=0x60 | (low & 0xF)) if key.endswith("_imm") and key.startswith(("shr", "asr", "ror", "shl")): return Fields(word, key, sub, immediate=low & 0x1F) if key == "rdrom_imm": return Fields(word, key, sub, immediate=word & 0x3FF) if key == "ld_ph": return Fields(word, key, sub, immediate=low & 0x7F) if key in ("rdram_a", "wrram_a"): return Fields(word, key, sub, lane=sub) if key in ("rdram_imm", "wrram_imm"): return Fields(word, key, sub, lane=sub, immediate=low) if key == "skip": return Fields(word, key, sub, immediate=low & 1) if key.endswith("_imm") or key == "ld_pl": return Fields(word, key, sub, shift=shift, immediate=low) return Fields(word, key, sub) __all__ = [ "CONDITIONS", "CONSTANTS", "OPS", "REGISTER_NAMES", "SHIFTS", "SKIP_FLAGS", "Fields", "classify", "decode", "register_name", ]