"""The opcode map: handler, addressing mode, mnemonic, operand form and base cycles. ``OPCODES`` is the fullest part, the W65C02S: 212 documented opcodes (DS Table 5-2) and 44 undefined ones, every one a NOP of the length and cycle count DS Table 7-1 gives. The other rows are this table with cells replaced (:func:`opcodes_for`): a part without the bit instructions has a one-byte, one-cycle NOP at each ``x7`` and ``xF`` (GDS Table V, VDS Table 2, NDS; CAP on the G65SC12; against SST, C2), and a part without ``WAI`` and ``STP`` has one at ``$CB`` and ``$DB`` (GDS, VDS, NDS, RDS; not captured, U3). - ``handler``: the ``_op_`` method. - ``mode``: the method that makes the addressing cycles, or ``None`` when the handler makes its own (implied, the stack, branches, jumps, the bit instructions, the NOPs). ``rd_`` returns the operand's value, ``ea_`` its address; ``_fixed`` is the indexed form that always takes the extra cycle (stores; ``INC`` and ``DEC abs,X``, C11). - ``mnemonic`` and ``form``: for the disassembler; ``FORM_BYTES`` gives the instruction length. - ``cycles``: the count with D clear, no page crossed and no branch taken (DS Tables 4-1 and 7-1; CAP's measured counts in docs/timing.md), which rung 1 confirms by running the instruction. ``step()`` adds what the instruction adds (``_extra``). Rung 0 (tests/test_tables.py) holds every row against a transcription of the data sheets' tables and against the counts measured on real chips. """ from collections.abc import Callable Row = tuple[str, str | None, str, str, int] #: opcode: (handler, mode, mnemonic, form, base cycles), for the W65C02S. OPCODES: dict[int, Row] = { 0x00: ("brk", None, "BRK", "imm", 7), 0x01: ("ora", "rd_idx", "ORA", "idx", 6), 0x02: ("nop_imm", None, "NOP", "imm", 2), 0x03: ("nop_one_cycle", None, "NOP", "imp", 1), 0x04: ("tsb", "ea_zp", "TSB", "zp", 5), 0x05: ("ora", "rd_zp", "ORA", "zp", 3), 0x06: ("asl", "ea_zp", "ASL", "zp", 5), 0x07: ("rmb", None, "RMB0", "zp", 5), 0x08: ("php", None, "PHP", "imp", 3), 0x09: ("ora", "rd_imm", "ORA", "imm", 2), 0x0A: ("asl_a", None, "ASL", "acc", 2), 0x0B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x0C: ("tsb", "ea_abs", "TSB", "abs", 6), 0x0D: ("ora", "rd_abs", "ORA", "abs", 4), 0x0E: ("asl", "ea_abs", "ASL", "abs", 6), 0x0F: ("bbr", None, "BBR0", "zprel", 5), 0x10: ("bpl", None, "BPL", "rel", 2), 0x11: ("ora", "rd_idy", "ORA", "idy", 5), 0x12: ("ora", "rd_ind", "ORA", "zpind", 5), 0x13: ("nop_one_cycle", None, "NOP", "imp", 1), 0x14: ("trb", "ea_zp", "TRB", "zp", 5), 0x15: ("ora", "rd_zpx", "ORA", "zpx", 4), 0x16: ("asl", "ea_zpx", "ASL", "zpx", 6), 0x17: ("rmb", None, "RMB1", "zp", 5), 0x18: ("clc", None, "CLC", "imp", 2), 0x19: ("ora", "rd_aby", "ORA", "aby", 4), 0x1A: ("inc_a", None, "INC", "acc", 2), 0x1B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x1C: ("trb", "ea_abs", "TRB", "abs", 6), 0x1D: ("ora", "rd_abx", "ORA", "abx", 4), 0x1E: ("asl", "ea_abx", "ASL", "abx", 6), 0x1F: ("bbr", None, "BBR1", "zprel", 5), 0x20: ("jsr", None, "JSR", "abs", 6), 0x21: ("and", "rd_idx", "AND", "idx", 6), 0x22: ("nop_imm", None, "NOP", "imm", 2), 0x23: ("nop_one_cycle", None, "NOP", "imp", 1), 0x24: ("bit", "rd_zp", "BIT", "zp", 3), 0x25: ("and", "rd_zp", "AND", "zp", 3), 0x26: ("rol", "ea_zp", "ROL", "zp", 5), 0x27: ("rmb", None, "RMB2", "zp", 5), 0x28: ("plp", None, "PLP", "imp", 4), 0x29: ("and", "rd_imm", "AND", "imm", 2), 0x2A: ("rol_a", None, "ROL", "acc", 2), 0x2B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x2C: ("bit", "rd_abs", "BIT", "abs", 4), 0x2D: ("and", "rd_abs", "AND", "abs", 4), 0x2E: ("rol", "ea_abs", "ROL", "abs", 6), 0x2F: ("bbr", None, "BBR2", "zprel", 5), 0x30: ("bmi", None, "BMI", "rel", 2), 0x31: ("and", "rd_idy", "AND", "idy", 5), 0x32: ("and", "rd_ind", "AND", "zpind", 5), 0x33: ("nop_one_cycle", None, "NOP", "imp", 1), 0x34: ("bit", "rd_zpx", "BIT", "zpx", 4), 0x35: ("and", "rd_zpx", "AND", "zpx", 4), 0x36: ("rol", "ea_zpx", "ROL", "zpx", 6), 0x37: ("rmb", None, "RMB3", "zp", 5), 0x38: ("sec", None, "SEC", "imp", 2), 0x39: ("and", "rd_aby", "AND", "aby", 4), 0x3A: ("dec_a", None, "DEC", "acc", 2), 0x3B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x3C: ("bit", "rd_abx", "BIT", "abx", 4), 0x3D: ("and", "rd_abx", "AND", "abx", 4), 0x3E: ("rol", "ea_abx", "ROL", "abx", 6), 0x3F: ("bbr", None, "BBR3", "zprel", 5), 0x40: ("rti", None, "RTI", "imp", 6), 0x41: ("eor", "rd_idx", "EOR", "idx", 6), 0x42: ("nop_imm", None, "NOP", "imm", 2), 0x43: ("nop_one_cycle", None, "NOP", "imp", 1), 0x44: ("nop_zp", None, "NOP", "zp", 3), 0x45: ("eor", "rd_zp", "EOR", "zp", 3), 0x46: ("lsr", "ea_zp", "LSR", "zp", 5), 0x47: ("rmb", None, "RMB4", "zp", 5), 0x48: ("pha", None, "PHA", "imp", 3), 0x49: ("eor", "rd_imm", "EOR", "imm", 2), 0x4A: ("lsr_a", None, "LSR", "acc", 2), 0x4B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x4C: ("jmp_abs", None, "JMP", "abs", 3), 0x4D: ("eor", "rd_abs", "EOR", "abs", 4), 0x4E: ("lsr", "ea_abs", "LSR", "abs", 6), 0x4F: ("bbr", None, "BBR4", "zprel", 5), 0x50: ("bvc", None, "BVC", "rel", 2), 0x51: ("eor", "rd_idy", "EOR", "idy", 5), 0x52: ("eor", "rd_ind", "EOR", "zpind", 5), 0x53: ("nop_one_cycle", None, "NOP", "imp", 1), 0x54: ("nop_zpx", None, "NOP", "zpx", 4), 0x55: ("eor", "rd_zpx", "EOR", "zpx", 4), 0x56: ("lsr", "ea_zpx", "LSR", "zpx", 6), 0x57: ("rmb", None, "RMB5", "zp", 5), 0x58: ("cli", None, "CLI", "imp", 2), 0x59: ("eor", "rd_aby", "EOR", "aby", 4), 0x5A: ("phy", None, "PHY", "imp", 3), 0x5B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x5C: ("nop_5c", None, "NOP", "abs", 8), 0x5D: ("eor", "rd_abx", "EOR", "abx", 4), 0x5E: ("lsr", "ea_abx", "LSR", "abx", 6), 0x5F: ("bbr", None, "BBR5", "zprel", 5), 0x60: ("rts", None, "RTS", "imp", 6), 0x61: ("adc", "rd_idx", "ADC", "idx", 6), 0x62: ("nop_imm", None, "NOP", "imm", 2), 0x63: ("nop_one_cycle", None, "NOP", "imp", 1), 0x64: ("stz", "ea_zp", "STZ", "zp", 3), 0x65: ("adc", "rd_zp", "ADC", "zp", 3), 0x66: ("ror", "ea_zp", "ROR", "zp", 5), 0x67: ("rmb", None, "RMB6", "zp", 5), 0x68: ("pla", None, "PLA", "imp", 4), 0x69: ("adc", "rd_imm", "ADC", "imm", 2), 0x6A: ("ror_a", None, "ROR", "acc", 2), 0x6B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x6C: ("jmp_ind", None, "JMP", "ind", 6), 0x6D: ("adc", "rd_abs", "ADC", "abs", 4), 0x6E: ("ror", "ea_abs", "ROR", "abs", 6), 0x6F: ("bbr", None, "BBR6", "zprel", 5), 0x70: ("bvs", None, "BVS", "rel", 2), 0x71: ("adc", "rd_idy", "ADC", "idy", 5), 0x72: ("adc", "rd_ind", "ADC", "zpind", 5), 0x73: ("nop_one_cycle", None, "NOP", "imp", 1), 0x74: ("stz", "ea_zpx", "STZ", "zpx", 4), 0x75: ("adc", "rd_zpx", "ADC", "zpx", 4), 0x76: ("ror", "ea_zpx", "ROR", "zpx", 6), 0x77: ("rmb", None, "RMB7", "zp", 5), 0x78: ("sei", None, "SEI", "imp", 2), 0x79: ("adc", "rd_aby", "ADC", "aby", 4), 0x7A: ("ply", None, "PLY", "imp", 4), 0x7B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x7C: ("jmp_abxind", None, "JMP", "abxind", 6), 0x7D: ("adc", "rd_abx", "ADC", "abx", 4), 0x7E: ("ror", "ea_abx", "ROR", "abx", 6), 0x7F: ("bbr", None, "BBR7", "zprel", 5), 0x80: ("bra", None, "BRA", "rel", 3), 0x81: ("sta", "ea_idx", "STA", "idx", 6), 0x82: ("nop_imm", None, "NOP", "imm", 2), 0x83: ("nop_one_cycle", None, "NOP", "imp", 1), 0x84: ("sty", "ea_zp", "STY", "zp", 3), 0x85: ("sta", "ea_zp", "STA", "zp", 3), 0x86: ("stx", "ea_zp", "STX", "zp", 3), 0x87: ("smb", None, "SMB0", "zp", 5), 0x88: ("dey", None, "DEY", "imp", 2), 0x89: ("bit_imm", None, "BIT", "imm", 2), 0x8A: ("txa", None, "TXA", "imp", 2), 0x8B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x8C: ("sty", "ea_abs", "STY", "abs", 4), 0x8D: ("sta", "ea_abs", "STA", "abs", 4), 0x8E: ("stx", "ea_abs", "STX", "abs", 4), 0x8F: ("bbs", None, "BBS0", "zprel", 5), 0x90: ("bcc", None, "BCC", "rel", 2), 0x91: ("sta", "ea_idy_fixed", "STA", "idy", 6), 0x92: ("sta", "ea_ind", "STA", "zpind", 5), 0x93: ("nop_one_cycle", None, "NOP", "imp", 1), 0x94: ("sty", "ea_zpx", "STY", "zpx", 4), 0x95: ("sta", "ea_zpx", "STA", "zpx", 4), 0x96: ("stx", "ea_zpy", "STX", "zpy", 4), 0x97: ("smb", None, "SMB1", "zp", 5), 0x98: ("tya", None, "TYA", "imp", 2), 0x99: ("sta", "ea_aby_fixed", "STA", "aby", 5), 0x9A: ("txs", None, "TXS", "imp", 2), 0x9B: ("nop_one_cycle", None, "NOP", "imp", 1), 0x9C: ("stz", "ea_abs", "STZ", "abs", 4), 0x9D: ("sta", "ea_abx_fixed", "STA", "abx", 5), 0x9E: ("stz", "ea_abx_fixed", "STZ", "abx", 5), 0x9F: ("bbs", None, "BBS1", "zprel", 5), 0xA0: ("ldy", "rd_imm", "LDY", "imm", 2), 0xA1: ("lda", "rd_idx", "LDA", "idx", 6), 0xA2: ("ldx", "rd_imm", "LDX", "imm", 2), 0xA3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xA4: ("ldy", "rd_zp", "LDY", "zp", 3), 0xA5: ("lda", "rd_zp", "LDA", "zp", 3), 0xA6: ("ldx", "rd_zp", "LDX", "zp", 3), 0xA7: ("smb", None, "SMB2", "zp", 5), 0xA8: ("tay", None, "TAY", "imp", 2), 0xA9: ("lda", "rd_imm", "LDA", "imm", 2), 0xAA: ("tax", None, "TAX", "imp", 2), 0xAB: ("nop_one_cycle", None, "NOP", "imp", 1), 0xAC: ("ldy", "rd_abs", "LDY", "abs", 4), 0xAD: ("lda", "rd_abs", "LDA", "abs", 4), 0xAE: ("ldx", "rd_abs", "LDX", "abs", 4), 0xAF: ("bbs", None, "BBS2", "zprel", 5), 0xB0: ("bcs", None, "BCS", "rel", 2), 0xB1: ("lda", "rd_idy", "LDA", "idy", 5), 0xB2: ("lda", "rd_ind", "LDA", "zpind", 5), 0xB3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xB4: ("ldy", "rd_zpx", "LDY", "zpx", 4), 0xB5: ("lda", "rd_zpx", "LDA", "zpx", 4), 0xB6: ("ldx", "rd_zpy", "LDX", "zpy", 4), 0xB7: ("smb", None, "SMB3", "zp", 5), 0xB8: ("clv", None, "CLV", "imp", 2), 0xB9: ("lda", "rd_aby", "LDA", "aby", 4), 0xBA: ("tsx", None, "TSX", "imp", 2), 0xBB: ("nop_one_cycle", None, "NOP", "imp", 1), 0xBC: ("ldy", "rd_abx", "LDY", "abx", 4), 0xBD: ("lda", "rd_abx", "LDA", "abx", 4), 0xBE: ("ldx", "rd_aby", "LDX", "aby", 4), 0xBF: ("bbs", None, "BBS3", "zprel", 5), 0xC0: ("cpy", "rd_imm", "CPY", "imm", 2), 0xC1: ("cmp", "rd_idx", "CMP", "idx", 6), 0xC2: ("nop_imm", None, "NOP", "imm", 2), 0xC3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xC4: ("cpy", "rd_zp", "CPY", "zp", 3), 0xC5: ("cmp", "rd_zp", "CMP", "zp", 3), 0xC6: ("dec", "ea_zp", "DEC", "zp", 5), 0xC7: ("smb", None, "SMB4", "zp", 5), 0xC8: ("iny", None, "INY", "imp", 2), 0xC9: ("cmp", "rd_imm", "CMP", "imm", 2), 0xCA: ("dex", None, "DEX", "imp", 2), 0xCB: ("wai", None, "WAI", "imp", 3), 0xCC: ("cpy", "rd_abs", "CPY", "abs", 4), 0xCD: ("cmp", "rd_abs", "CMP", "abs", 4), 0xCE: ("dec", "ea_abs", "DEC", "abs", 6), 0xCF: ("bbs", None, "BBS4", "zprel", 5), 0xD0: ("bne", None, "BNE", "rel", 2), 0xD1: ("cmp", "rd_idy", "CMP", "idy", 5), 0xD2: ("cmp", "rd_ind", "CMP", "zpind", 5), 0xD3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xD4: ("nop_zpx", None, "NOP", "zpx", 4), 0xD5: ("cmp", "rd_zpx", "CMP", "zpx", 4), 0xD6: ("dec", "ea_zpx", "DEC", "zpx", 6), 0xD7: ("smb", None, "SMB5", "zp", 5), 0xD8: ("cld", None, "CLD", "imp", 2), 0xD9: ("cmp", "rd_aby", "CMP", "aby", 4), 0xDA: ("phx", None, "PHX", "imp", 3), 0xDB: ("stp", None, "STP", "imp", 3), 0xDC: ("nop_abs", None, "NOP", "abs", 4), 0xDD: ("cmp", "rd_abx", "CMP", "abx", 4), 0xDE: ("dec", "ea_abx_fixed", "DEC", "abx", 7), 0xDF: ("bbs", None, "BBS5", "zprel", 5), 0xE0: ("cpx", "rd_imm", "CPX", "imm", 2), 0xE1: ("sbc", "rd_idx", "SBC", "idx", 6), 0xE2: ("nop_imm", None, "NOP", "imm", 2), 0xE3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xE4: ("cpx", "rd_zp", "CPX", "zp", 3), 0xE5: ("sbc", "rd_zp", "SBC", "zp", 3), 0xE6: ("inc", "ea_zp", "INC", "zp", 5), 0xE7: ("smb", None, "SMB6", "zp", 5), 0xE8: ("inx", None, "INX", "imp", 2), 0xE9: ("sbc", "rd_imm", "SBC", "imm", 2), 0xEA: ("nop", None, "NOP", "imp", 2), 0xEB: ("nop_one_cycle", None, "NOP", "imp", 1), 0xEC: ("cpx", "rd_abs", "CPX", "abs", 4), 0xED: ("sbc", "rd_abs", "SBC", "abs", 4), 0xEE: ("inc", "ea_abs", "INC", "abs", 6), 0xEF: ("bbs", None, "BBS6", "zprel", 5), 0xF0: ("beq", None, "BEQ", "rel", 2), 0xF1: ("sbc", "rd_idy", "SBC", "idy", 5), 0xF2: ("sbc", "rd_ind", "SBC", "zpind", 5), 0xF3: ("nop_one_cycle", None, "NOP", "imp", 1), 0xF4: ("nop_zpx", None, "NOP", "zpx", 4), 0xF5: ("sbc", "rd_zpx", "SBC", "zpx", 4), 0xF6: ("inc", "ea_zpx", "INC", "zpx", 6), 0xF7: ("smb", None, "SMB7", "zp", 5), 0xF8: ("sed", None, "SED", "imp", 2), 0xF9: ("sbc", "rd_aby", "SBC", "aby", 4), 0xFA: ("plx", None, "PLX", "imp", 4), 0xFB: ("nop_one_cycle", None, "NOP", "imp", 1), 0xFC: ("nop_abs", None, "NOP", "abs", 4), 0xFD: ("sbc", "rd_abx", "SBC", "abx", 4), 0xFE: ("inc", "ea_abx_fixed", "INC", "abx", 7), 0xFF: ("bbs", None, "BBS7", "zprel", 5), } #: What a part has in place of an instruction it lacks: one byte, one cycle. ONE_CYCLE_NOP: Row = ("nop_one_cycle", None, "NOP", "imp", 1) #: The 32 bit-instruction opcodes (``RMB``, ``SMB``, ``BBR``, ``BBS``): columns 7 and F. BIT_OPCODES = tuple(opcode for opcode in range(256) if opcode & 0x0F in (0x07, 0x0F)) #: ``WAI`` and ``STP``. WAI_STP_OPCODES = (0xCB, 0xDB) #: Instruction length in bytes by operand form. ``BRK`` is listed as ``imm``: #: it is a two-byte instruction (DS Table 4-1 note 5). FORM_BYTES = { "imp": 1, "acc": 1, "imm": 2, "zp": 2, "zpx": 2, "zpy": 2, "abs": 3, "abx": 3, "aby": 3, "ind": 3, "idx": 2, "idy": 2, "zpind": 2, "abxind": 3, "rel": 2, "zprel": 3, } # fmt: skip Entry = tuple[Callable[..., None], Callable[..., int] | None, int] def opcodes_for(row: type) -> dict[int, Row]: """The 256 rows of one part: ``OPCODES`` with the cells its settings replace. ``row`` is a CPU class (or anything with ``bit_instructions`` and ``wai_stp``). """ table = dict(OPCODES) if not row.bit_instructions: for opcode in BIT_OPCODES: table[opcode] = ONE_CYCLE_NOP # C2: CAP on the G65SC12, GDS, VDS, NDS; SST disagrees if not row.wai_stp: for opcode in WAI_STP_OPCODES: table[opcode] = ONE_CYCLE_NOP # U3: GDS, VDS, NDS "XB: 1 byte, 1 cycle"; not captured return table def build_table(cls: type) -> list[Entry]: """The 256 (handler, mode, base cycles) entries of ``cls``, as unbound functions.""" table = [] rows = opcodes_for(cls) for opcode in range(256): handler, mode, _mnemonic, _form, cycles = rows[opcode] table.append( ( getattr(cls, f"_op_{handler}"), None if mode is None else getattr(cls, f"_{mode}"), cycles, ) ) return table