# SPDX-License-Identifier: MIT """The opcode map: 256 opcodes x 4 ALT states x B, to an instruction form (rung 0). Sources: BK2 2-2-6..2-2-8 and chapter 9 (manufacturer), fullsnes "CPU MOV/ALU/JMP and Prefix Opcodes" (research); BGE/BLT as fullsnes (C2: BK2 prints them exchanged), LJMP's operands as fullsnes (C3, in the handler). ares ``instruction.cpp`` decodes the same map. **U4, the fallback.** An ALT prefix on an opcode that has no such form is ignored: ALT1 and ALT2 fall back to the plain form, ALT3 to the ALT1 form and then to the plain form (fullsnes "Ignored Prefixes": "ALT3 does reportedly mirror to ALT1"; ares's handlers test the bits one at a time, which gives exactly this in every cell). **B** (set by WITH) only changes ``1n`` (TO -> MOVE) and ``Bn`` (FROM -> MOVES); every other opcode ignores it. A form is ``(key, n, keeps_prefix, length)``: the handler key (``core.GSU._op_``), the register or immediate number in the opcode's low nibble (or the LINK count, or the branch condition index), whether the opcode leaves ALT1/ALT2/B/Sreg/Dreg alone (the prefixes and the branches ``05-0F``: U3; every other opcode, NOP included, clears them after it runs), and its length in bytes counting the opcode but not a prefix. """ from __future__ import annotations from typing import NamedTuple class Form(NamedTuple): key: str n: int keeps_prefix: bool length: int #: Mnemonic of each handler key in BK2's syntax (disasm.py formats the operands). MNEMONICS = { "stop": "STOP", "nop": "NOP", "cache": "CACHE", "lsr": "LSR", "rol": "ROL", "branch": "B", "to": "TO", "move": "MOVE", "with": "WITH", "stw": "STW", "stb": "STB", "loop": "LOOP", "alt1": "ALT1", "alt2": "ALT2", "alt3": "ALT3", "ldw": "LDW", "ldb": "LDB", "plot": "PLOT", "rpix": "RPIX", "swap": "SWAP", "color": "COLOR", "cmode": "CMODE", "not": "NOT", "add_r": "ADD", "adc_r": "ADC", "add_i": "ADD", "adc_i": "ADC", "sub_r": "SUB", "sbc_r": "SBC", "sub_i": "SUB", "cmp_r": "CMP", "merge": "MERGE", "and_r": "AND", "bic_r": "BIC", "and_i": "AND", "bic_i": "BIC", "mult_r": "MULT", "umult_r": "UMULT", "mult_i": "MULT", "umult_i": "UMULT", "sbk": "SBK", "link": "LINK", "sex": "SEX", "asr": "ASR", "div2": "DIV2", "ror": "ROR", "jmp": "JMP", "ljmp": "LJMP", "lob": "LOB", "fmult": "FMULT", "lmult": "LMULT", "ibt": "IBT", "lms": "LMS", "sms": "SMS", "from": "FROM", "moves": "MOVES", "hib": "HIB", "or_r": "OR", "xor_r": "XOR", "or_i": "OR", "xor_i": "XOR", "inc": "INC", "getc": "GETC", "ramb": "RAMB", "romb": "ROMB", "dec": "DEC", "getb": "GETB", "getbh": "GETBH", "getbl": "GETBL", "getbs": "GETBS", "iwt": "IWT", "lm": "LM", "sm": "SM", } # fmt: skip #: Branch conditions ``05-0F`` in opcode order (C2: ``06`` is BGE, ``07`` is BLT). BRANCHES = ("BRA", "BGE", "BLT", "BNE", "BEQ", "BPL", "BMI", "BCC", "BCS", "BVC", "BVS") # Prefix-free opcodes that take their ALT forms from this map: opcode -> {alt: key}. # Only the forms that exist are listed; the fallback (U4) fills the rest. _ALT_FORMS: dict[int, dict[int, str]] = {} def _forms(first: int, last: int, **by_alt: str) -> None: alts = {"none": 0, "alt1": 1, "alt2": 2, "alt3": 3} for opcode in range(first, last + 1): _ALT_FORMS[opcode] = {alts[name]: key for name, key in by_alt.items()} _forms(0x00, 0x00, none="stop") _forms(0x01, 0x01, none="nop") _forms(0x02, 0x02, none="cache") _forms(0x03, 0x03, none="lsr") _forms(0x04, 0x04, none="rol") _forms(0x05, 0x0F, none="branch") _forms(0x30, 0x3B, none="stw", alt1="stb") _forms(0x3C, 0x3C, none="loop") _forms(0x3D, 0x3D, none="alt1") _forms(0x3E, 0x3E, none="alt2") _forms(0x3F, 0x3F, none="alt3") _forms(0x40, 0x4B, none="ldw", alt1="ldb") _forms(0x4C, 0x4C, none="plot", alt1="rpix") _forms(0x4D, 0x4D, none="swap") _forms(0x4E, 0x4E, none="color", alt1="cmode") _forms(0x4F, 0x4F, none="not") _forms(0x50, 0x5F, none="add_r", alt1="adc_r", alt2="add_i", alt3="adc_i") _forms(0x60, 0x6F, none="sub_r", alt1="sbc_r", alt2="sub_i", alt3="cmp_r") _forms(0x70, 0x70, none="merge") _forms(0x71, 0x7F, none="and_r", alt1="bic_r", alt2="and_i", alt3="bic_i") _forms(0x80, 0x8F, none="mult_r", alt1="umult_r", alt2="mult_i", alt3="umult_i") _forms(0x90, 0x90, none="sbk") _forms(0x91, 0x94, none="link") _forms(0x95, 0x95, none="sex") _forms(0x96, 0x96, none="asr", alt1="div2") _forms(0x97, 0x97, none="ror") _forms(0x98, 0x9D, none="jmp", alt1="ljmp") _forms(0x9E, 0x9E, none="lob") _forms(0x9F, 0x9F, none="fmult", alt1="lmult") _forms(0xA0, 0xAF, none="ibt", alt1="lms", alt2="sms") _forms(0xC0, 0xC0, none="hib") _forms(0xC1, 0xCF, none="or_r", alt1="xor_r", alt2="or_i", alt3="xor_i") _forms(0xD0, 0xDE, none="inc") _forms(0xDF, 0xDF, none="getc", alt2="ramb", alt3="romb") _forms(0xE0, 0xEE, none="dec") _forms(0xEF, 0xEF, none="getb", alt1="getbh", alt2="getbl", alt3="getbs") _forms(0xF0, 0xFF, none="iwt", alt1="lm", alt2="sm") #: Handler keys that leave the prefix state alone (U3): the prefixes and the branches. KEEPS_PREFIX = frozenset({"to", "from", "with", "alt1", "alt2", "alt3", "branch"}) #: Bytes after the opcode (operands), per handler key. OPERAND_BYTES = {"branch": 1, "ibt": 1, "lms": 1, "sms": 1, "iwt": 2, "lm": 2, "sm": 2} def _fallback(forms: dict[int, str], alt: int) -> str: """U4: the form an ALT state selects, falling back as fullsnes describes.""" order = {0: (0,), 1: (1, 0), 2: (2, 0), 3: (3, 1, 0)}[alt] for candidate in order: if candidate in forms: return forms[candidate] raise AssertionError("every opcode has a plain form") def decode(opcode: int, alt: int, b: bool) -> Form: """The form ``opcode`` executes as with ALT state ``alt`` (0-3) and the B flag ``b``.""" if not 0 <= opcode <= 0xFF or not 0 <= alt <= 3: raise ValueError("opcode must be a byte and alt 0-3") n = opcode & 0xF if 0x10 <= opcode <= 0x1F: key = "move" if b else "to" elif 0x20 <= opcode <= 0x2F: key = "with" elif 0xB0 <= opcode <= 0xBF: key = "moves" if b else "from" else: key = _fallback(_ALT_FORMS[opcode], alt) if key == "branch": n = opcode - 0x05 elif key == "link": n = opcode - 0x90 return Form(key, n, key in KEEPS_PREFIX, 1 + OPERAND_BYTES.get(key, 0)) def mode_index(alt: int, b: bool) -> int: """The row of :data:`TABLE` for an ALT state and the B flag: SFR bits 9-8 and 12 packed as ``alt | b << 2`` (the core computes it as ``sfr >> 8 & 3 | sfr >> 10 & 4``).""" return alt | (4 if b else 0) #: TABLE[mode_index(alt, b)][opcode] -> Form, all 8 x 256 combinations. TABLE: tuple[tuple[Form, ...], ...] = tuple( tuple(decode(opcode, mode & 3, bool(mode & 4)) for opcode in range(256)) for mode in range(8) ) __all__ = [ "BRANCHES", "KEEPS_PREFIX", "MNEMONICS", "OPERAND_BYTES", "TABLE", "Form", "decode", "mode_index", ]