# SPDX-License-Identifier: MIT """Rung 0: the opcode map, 256 opcodes x 4 ALT states x B (BK2 chapter 9, fullsnes; C2, U4). The expected map below is transcribed independently from fullsnes' opcode tables ("CPU MOV/ ALU/JMP and Prefix Opcodes") and BK2 2-2-6..2-2-8, then the U4 fallback is applied by hand for the empty cells. """ from __future__ import annotations import pytest from gsu_python._decode import TABLE, decode, mode_index from gsu_python.disasm import disassemble_bytes # opcode range -> (none, ALT1, ALT2, ALT3) as fullsnes prints them; None = no such form. MAP = [ (0x00, 0x00, ("STOP", None, None, None)), (0x01, 0x01, ("NOP", None, None, None)), (0x02, 0x02, ("CACHE", None, None, None)), (0x03, 0x03, ("LSR", None, None, None)), (0x04, 0x04, ("ROL", None, None, None)), (0x05, 0x0F, ("Bcc", None, None, None)), (0x30, 0x3B, ("STW", "STB", None, None)), (0x3C, 0x3C, ("LOOP", None, None, None)), (0x3D, 0x3D, ("ALT1", None, None, None)), (0x3E, 0x3E, ("ALT2", None, None, None)), (0x3F, 0x3F, ("ALT3", None, None, None)), (0x40, 0x4B, ("LDW", "LDB", None, None)), (0x4C, 0x4C, ("PLOT", "RPIX", None, None)), (0x4D, 0x4D, ("SWAP", None, None, None)), (0x4E, 0x4E, ("COLOR", "CMODE", None, None)), (0x4F, 0x4F, ("NOT", None, None, None)), (0x50, 0x5F, ("ADD", "ADC", "ADD#", "ADC#")), (0x60, 0x6F, ("SUB", "SBC", "SUB#", "CMP")), (0x70, 0x70, ("MERGE", None, None, None)), (0x71, 0x7F, ("AND", "BIC", "AND#", "BIC#")), (0x80, 0x8F, ("MULT", "UMULT", "MULT#", "UMULT#")), (0x90, 0x90, ("SBK", None, None, None)), (0x91, 0x94, ("LINK", None, None, None)), (0x95, 0x95, ("SEX", None, None, None)), (0x96, 0x96, ("ASR", "DIV2", None, None)), (0x97, 0x97, ("ROR", None, None, None)), (0x98, 0x9D, ("JMP", "LJMP", None, None)), (0x9E, 0x9E, ("LOB", None, None, None)), (0x9F, 0x9F, ("FMULT", "LMULT", None, None)), (0xA0, 0xAF, ("IBT", "LMS", "SMS", None)), (0xC0, 0xC0, ("HIB", None, None, None)), (0xC1, 0xCF, ("OR", "XOR", "OR#", "XOR#")), (0xD0, 0xDE, ("INC", None, None, None)), (0xDF, 0xDF, ("GETC", None, "RAMB", "ROMB")), (0xE0, 0xEE, ("DEC", None, None, None)), (0xEF, 0xEF, ("GETB", "GETBH", "GETBL", "GETBS")), (0xF0, 0xFF, ("IWT", "LM", "SM", None)), ] # Our keys in fullsnes' names. NAMES = { "branch": "Bcc", "add_i": "ADD#", "adc_i": "ADC#", "sub_i": "SUB#", "and_i": "AND#", "bic_i": "BIC#", "mult_i": "MULT#", "umult_i": "UMULT#", "or_i": "OR#", "xor_i": "XOR#", } # fmt: skip def name_of(key: str) -> str: return NAMES.get(key, key.removesuffix("_r").upper()) def expected(opcode: int, alt: int, b: bool) -> str: if 0x10 <= opcode <= 0x1F: return "MOVE" if b else "TO" if 0x20 <= opcode <= 0x2F: return "WITH" if 0xB0 <= opcode <= 0xBF: return "MOVES" if b else "FROM" for first, last, forms in MAP: if first <= opcode <= last: # U4: ALT1/ALT2 fall back to none; ALT3 to ALT1, then none. order = {0: (0,), 1: (1, 0), 2: (2, 0), 3: (3, 1, 0)}[alt] return next(forms[i] for i in order if forms[i] is not None) raise AssertionError(hex(opcode)) @pytest.mark.parametrize("mode", range(8)) def test_all_cells(mode): """Every one of the 2,048 cells against the independent transcription.""" alt, b = mode & 3, bool(mode & 4) for opcode in range(256): assert name_of(TABLE[mode][opcode].key) == expected(opcode, alt, b), hex(opcode) def test_u4_fallback_cells(): """fullsnes "Ignored Prefixes": the cells start-here.md names.""" assert decode(0x4C, 3, False).key == "rpix" # 3F 4C assert decode(0x96, 2, False).key == "asr" # 3E 96 assert decode(0xA0, 3, False).key == "lms" # 3F A0 assert decode(0xD0, 1, False).key == "inc" # 3D D0 assert decode(0xF5, 3, False).key == "lm" assert decode(0xDF, 1, False).key == "getc" def test_c2_branch_order(): """C2: 06 is BGE and 07 BLT (fullsnes, every emulator's execution); BK2 prints them exchanged. The decode gives the condition index; disasm names it.""" assert [i.text.split()[0] for i in disassemble_bytes(bytes([5, 0, 6, 0, 7, 0, 0xF, 0]))] == [ "BRA", "BGE", "BLT", "BVS", ] def test_prefix_persistence_set(): """U3: only TO/FROM/WITH, ALT1-3 and the branches keep the prefix state.""" keepers = {TABLE[m][op].key for m in range(8) for op in range(256) if TABLE[m][op].keeps_prefix} assert keepers == {"to", "from", "with", "alt1", "alt2", "alt3", "branch"} assert not decode(0x15, 0, True).keeps_prefix # MOVE resets assert not decode(0xB5, 0, True).keeps_prefix # MOVES resets assert not decode(0x01, 0, False).keeps_prefix # NOP resets def test_lengths(): """Operand bytes: branches, IBT, LMS, SMS one; IWT, LM, SM two (BK2 2-2-6).""" assert decode(0x05, 0, False).length == 2 assert decode(0xA3, 0, False).length == 2 assert decode(0xA3, 1, False).length == 2 # LMS assert decode(0xA3, 2, False).length == 2 # SMS assert decode(0xF3, 0, False).length == 3 assert decode(0xF3, 1, False).length == 3 # LM assert decode(0xF3, 2, False).length == 3 # SM assert decode(0x50, 2, False).length == 1 # ADD #n is the nibble def test_mode_index_matches_sfr_packing(): for alt in range(4): for b in (False, True): sfr = alt << 8 | (0x1000 if b else 0) assert mode_index(alt, b) == (sfr >> 8 & 3 | sfr >> 10 & 4)