# SPDX-License-Identifier: MIT """Opcode decoding: exact bytes first, then bit patterns, mapped to handler names. A pattern is eight characters, ``0`` and ``1`` for fixed bits and ``x`` for a field the handler decodes itself. The map's columns repeat (docs/start-here.md "Instruction set"): low nibble $1 is TCALL, $2 SET1/CLR1, $3 BBS/BBC, and the ALU block OR AND EOR CMP ADC SBC fills columns $4-$9 of rows $0-$B, its operation in the top three bits. Rows $C-$F of those columns are MOV, so they are in :data:`EXACT`, which wins. The mnemonic text of every opcode is in generated ``_opcodes.py``. """ from __future__ import annotations #: Opcodes outside the repeating columns: opcode -> handler method name. EXACT: dict[int, str] = { # column 0, even rows: implied 0x00: "_op_nop", 0x20: "_op_clrp", 0x40: "_op_setp", 0x60: "_op_clrc", 0x80: "_op_setc", 0xA0: "_op_ei", 0xC0: "_op_di", 0xE0: "_op_clrv", # columns 4-9, rows C-F: MOV and the X/Y compare-immediate 0xC4: "_op_mov_d_a", 0xC5: "_op_mov_abs_a", 0xC6: "_op_mov_ix_a", 0xC7: "_op_mov_dxi_a", 0xC8: "_op_cmp_x_imm", 0xC9: "_op_mov_abs_x", 0xD4: "_op_mov_dx_a", 0xD5: "_op_mov_absx_a", 0xD6: "_op_mov_absy_a", 0xD7: "_op_mov_diy_a", 0xD8: "_op_mov_d_x", 0xD9: "_op_mov_dy_x", 0xE4: "_op_mov_a_d", 0xE5: "_op_mov_a_abs", 0xE6: "_op_mov_a_ix", 0xE7: "_op_mov_a_dxi", 0xE8: "_op_mov_a_imm", 0xE9: "_op_mov_x_abs", 0xF4: "_op_mov_a_dx", 0xF5: "_op_mov_a_absx", 0xF6: "_op_mov_a_absy", 0xF7: "_op_mov_a_diy", 0xF8: "_op_mov_x_d", 0xF9: "_op_mov_x_dy", # column A 0x0A: "_op_or1", 0x2A: "_op_or1", 0x4A: "_op_and1", 0x6A: "_op_and1", 0x8A: "_op_eor1", 0xAA: "_op_mov1_c_bit", 0xCA: "_op_mov1_bit_c", 0xEA: "_op_not1", 0x1A: "_op_decw", 0x3A: "_op_incw", 0x5A: "_op_cmpw", 0x7A: "_op_addw", 0x9A: "_op_subw", 0xBA: "_op_movw_ya_d", 0xDA: "_op_movw_d_ya", 0xFA: "_op_mov_dd_ds", # columns B and C, rows C-F 0xCB: "_op_mov_d_y", 0xCC: "_op_mov_abs_y", 0xDB: "_op_mov_dx_y", 0xDC: "_op_dec_y", 0xEB: "_op_mov_y_d", 0xEC: "_op_mov_y_abs", 0xFB: "_op_mov_y_dx", 0xFC: "_op_inc_y", # column D 0x8D: "_op_mov_y_imm", 0xAD: "_op_cmp_y_imm", 0xCD: "_op_mov_x_imm", 0xED: "_op_notc", 0x1D: "_op_dec_x", 0x3D: "_op_inc_x", 0x5D: "_op_mov_x_a", 0x7D: "_op_mov_a_x", 0x9D: "_op_mov_x_sp", 0xBD: "_op_mov_sp_x", 0xDD: "_op_mov_a_y", 0xFD: "_op_mov_y_a", # column E 0x0E: "_op_tset1", 0x2E: "_op_cbne_d", 0x4E: "_op_tclr1", 0x6E: "_op_dbnz_d", 0x1E: "_op_cmp_x_abs", 0x3E: "_op_cmp_x_d", 0x5E: "_op_cmp_y_abs", 0x7E: "_op_cmp_y_d", 0x9E: "_op_div", 0xBE: "_op_das", 0xDE: "_op_cbne_dx", 0xFE: "_op_dbnz_y", # column F 0x0F: "_op_brk", 0x1F: "_op_jmp_absx", 0x2F: "_op_bra", 0x3F: "_op_call", 0x4F: "_op_pcall", 0x5F: "_op_jmp_abs", 0x6F: "_op_ret", 0x7F: "_op_ret1", 0x8F: "_op_mov_d_imm", 0x9F: "_op_xcn", 0xAF: "_op_mov_ixp_a", 0xBF: "_op_mov_a_ixp", 0xCF: "_op_mul", 0xDF: "_op_daa", 0xEF: "_op_sleep", 0xFF: "_op_stop", } #: (pattern, handler method name), for whatever :data:`EXACT` leaves. RULES: tuple[tuple[str, str], ...] = ( ("xxx10000", "_op_branch"), # BPL BMI BVC BVS BCC BCS BNE BEQ ("xxxx0001", "_op_tcall"), ("xxxx0010", "_op_set1_clr1"), ("xxxx0011", "_op_bbs_bbc"), ("xxx00100", "_op_alu_a_d"), ("xxx00101", "_op_alu_a_abs"), ("xxx00110", "_op_alu_a_ix"), ("xxx00111", "_op_alu_a_dxi"), ("xxx01000", "_op_alu_a_imm"), ("xxx01001", "_op_alu_dd_ds"), ("xxx10100", "_op_alu_a_dx"), ("xxx10101", "_op_alu_a_absx"), ("xxx10110", "_op_alu_a_absy"), ("xxx10111", "_op_alu_a_diy"), ("xxx11000", "_op_alu_d_imm"), ("xxx11001", "_op_alu_ix_iy"), ("xxx01011", "_op_shift_d"), # ASL ROL LSR ROR DEC INC, by the top three bits ("xxx11011", "_op_shift_dx"), ("xxx01100", "_op_shift_abs"), ("xxx11100", "_op_shift_a"), ("xxx01101", "_op_push"), ("xxx01110", "_op_pop"), ) def matches(pattern: str, opcode: int) -> bool: """Whether ``opcode`` fits an eight-character bit pattern.""" bits = f"{opcode:08b}" return all(p in ("x", b) for p, b in zip(pattern, bits, strict=True)) def build_table() -> tuple[str, ...]: """The handler method name of each of the 256 opcodes; fails if one has no rule.""" names = [] for opcode in range(256): name = EXACT.get(opcode) if name is None: for pattern, candidate in RULES: if matches(pattern, opcode): name = candidate break else: raise ValueError(f"no decode rule for opcode ${opcode:02X}") names.append(name) return tuple(names) HANDLER_NAMES = build_table() __all__ = ["EXACT", "HANDLER_NAMES", "RULES", "build_table", "matches"]