# SPDX-License-Identifier: MIT """The manufacturers' tables, transcribed, for rung 0 (tests/test_tables.py). Transcribed by hand from the pinned documents in ``reference/docs`` (never committed), each in its document's own layout, so that the core's opcode table (``w65c02_python._dispatch``, laid out by opcode) is held against independent readings. Tier 2 throughout, except ``CL_EXTRA`` (tier 3). - ``DS_MATRIX``: W65C02S data sheet (8 April 2022), Table 5-2 "W65C02S OpCode Matrix", p. 22: one line per high nibble, sixteen cells, each "MNEMONIC mode" in the data sheet's notation, ``-`` for an empty cell. - ``DS_NOPS``: the same, Table 7-1 "Microprocessor Operational Enhancements", p. 30: bytes and cycles of the undefined opcodes. ``DS_BRK_BYTES``: Table 4-1 note 5. - ``GDS_TABLE_IV``: GTE/CMD G65SCXXX family data sheet, Table IV "Operational Codes, Execution Time, and Memory Requirements", p. 2-10 (a scan, read from the page image): one line per mnemonic, each entry "column OP n #" with n the cycles and # the bytes. This is the 178-opcode part. Its notes: (1) add 1 to n if a page boundary is crossed, except STA and STZ; (2) add 1 to n if a branch occurs to the same page, 2 to a different page; (3) add 1 to n in decimal mode; (4) accumulator addressing is included in implied. - ``GDS_NOPS``: the same data sheet, Table V: the NOPs of a part without bit instructions (``X2``: 2 bytes, 2 cycles; ``X3, X7, XB, XF``: 1, 1; ...). - ``CL_EXTRA``: Clark and Laughton, "65C02 Opcodes", sections 4 and 5: bytes and cycles of the instructions Table IV does not have. """ DS_MATRIX = """ BRK s | ORA (zp,x) | - | - | TSB zp | ORA zp | ASL zp | RMB0 zp | PHP s | ORA # | ASL A | - | TSB a | ORA a | ASL a | BBR0 r BPL r | ORA (zp),y | ORA (zp) | - | TRB zp | ORA zp,x | ASL zp,x | RMB1 zp | CLC i | ORA a,y | INC A | - | TRB a | ORA a,x | ASL a,x | BBR1 r JSR a | AND (zp,x) | - | - | BIT zp | AND zp | ROL zp | RMB2 zp | PLP s | AND # | ROL A | - | BIT a | AND a | ROL a | BBR2 r BMI r | AND (zp),y | AND (zp) | - | BIT zp,x | AND zp,x | ROL zp,x | RMB3 zp | SEC i | AND a,y | DEC A | - | BIT a,x | AND a,x | ROL a,x | BBR3 r RTI s | EOR (zp,x) | - | - | - | EOR zp | LSR zp | RMB4 zp | PHA s | EOR # | LSR A | - | JMP a | EOR a | LSR a | BBR4 r BVC r | EOR (zp),y | EOR (zp) | - | - | EOR zp,x | LSR zp,x | RMB5 zp | CLI i | EOR a,y | PHY s | - | - | EOR a,x | LSR a,x | BBR5 r RTS s | ADC (zp,x) | - | - | STZ zp | ADC zp | ROR zp | RMB6 zp | PLA s | ADC # | ROR A | - | JMP (a) | ADC a | ROR a | BBR6 r BVS r | ADC (zp),y | ADC (zp) | - | STZ zp,x | ADC zp,x | ROR zp,x | RMB7 zp | SEI i | ADC a,y | PLY s | - | JMP (a,x) | ADC a,x | ROR a,x | BBR7 r BRA r | STA (zp,x) | - | - | STY zp | STA zp | STX zp | SMB0 zp | DEY i | BIT # | TXA i | - | STY a | STA a | STX a | BBS0 r BCC r | STA (zp),y | STA (zp) | - | STY zp,x | STA zp,x | STX zp,y | SMB1 zp | TYA i | STA a,y | TXS i | - | STZ a | STA a,x | STZ a,x | BBS1 r LDY # | LDA (zp,x) | LDX # | - | LDY zp | LDA zp | LDX zp | SMB2 zp | TAY i | LDA # | TAX i | - | LDY a | LDA a | LDX a | BBS2 r BCS r | LDA (zp),y | LDA (zp) | - | LDY zp,x | LDA zp,x | LDX zp,y | SMB3 zp | CLV i | LDA a,y | TSX i | - | LDY a,x | LDA a,x | LDX a,y | BBS3 r CPY # | CMP (zp,x) | - | - | CPY zp | CMP zp | DEC zp | SMB4 zp | INY i | CMP # | DEX i | WAI i | CPY a | CMP a | DEC a | BBS4 r BNE r | CMP (zp),y | CMP (zp) | - | - | CMP zp,x | DEC zp,x | SMB5 zp | CLD i | CMP a,y | PHX s | STP i | - | CMP a,x | DEC a,x | BBS5 r CPX # | SBC (zp,x) | - | - | CPX zp | SBC zp | INC zp | SMB6 zp | INX i | SBC # | NOP i | - | CPX a | SBC a | INC a | BBS6 r BEQ r | SBC (zp),y | SBC (zp) | - | - | SBC zp,x | INC zp,x | SMB7 zp | SED i | SBC a,y | PLX s | - | - | SBC a,x | INC a,x | BBS7 r """ #: DS Table 4-1 note 5: "The BRK instruction ... is a 2 byte instruction." DS_BRK_BYTES = 2 #: DS Table 7-1, "Execution of invalid OpCodes": opcode -> (bytes, cycles). Its #: row "X3, 0B-BB, EB, FB" is every x3, and every xB except CB and DB (WAI, STP). DS_NOPS = { **{opcode: (2, 2) for opcode in (0x02, 0x22, 0x42, 0x62, 0x82, 0xC2, 0xE2)}, **{high << 4 | 0x03: (1, 1) for high in range(16)}, **{high << 4 | 0x0B: (1, 1) for high in range(16) if high not in (0xC, 0xD)}, 0x44: (2, 3), 0x54: (2, 4), 0xD4: (2, 4), 0xF4: (2, 4), 0x5C: (3, 8), 0xDC: (3, 4), 0xFC: (3, 4), } #: GDS Table V: "X2: 2, 2", "X3, X7, XB, XF: 1, 1", and the rest as DS. On a #: part with 178 opcodes; X2 means the seven undefined opcodes of that column. GDS_NOPS = { **DS_NOPS, **{high << 4 | low: (1, 1) for high in range(16) for low in (0x03, 0x07, 0x0B, 0x0F)}, } # The columns of GDS Table IV, in its order, as the names used below. GDS_COLUMNS = ( "imm", "abs", "zp", "imp", "(ind,x)", "(ind),y", "zpg,x", "abs,x", "abs,y", "rel", "ind", "zpg,y", ) # fmt: skip GDS_TABLE_IV = """ ADC: imm 69 2 2; abs 6D 4 3; zp 65 3 2; (ind,x) 61 6 2; (ind),y 71 5 2; zpg,x 75 4 2; abs,x 7D 4 3; abs,y 79 4 3; ind 72 5 2 AND: imm 29 2 2; abs 2D 4 3; zp 25 3 2; (ind,x) 21 6 2; (ind),y 31 5 2; zpg,x 35 4 2; abs,x 3D 4 3; abs,y 39 4 3; ind 32 5 2 ASL: abs 0E 6 3; zp 06 5 2; imp 0A 2 1; zpg,x 16 6 2; abs,x 1E 6 3 BCC: rel 90 2 2 BCS: rel B0 2 2 BEQ: rel F0 2 2 BIT: imm 89 2 2; abs 2C 4 3; zp 24 3 2; zpg,x 34 4 2; abs,x 3C 4 3 BMI: rel 30 2 2 BNE: rel D0 2 2 BPL: rel 10 2 2 BRA: rel 80 2 2 BRK: imp 00 7 1 BVC: rel 50 2 2 BVS: rel 70 2 2 CLC: imp 18 2 1 CLD: imp D8 2 1 CLI: imp 58 2 1 CLV: imp B8 2 1 CMP: imm C9 2 2; abs CD 4 3; zp C5 3 2; (ind,x) C1 6 2; (ind),y D1 5 2; zpg,x D5 4 2; abs,x DD 4 3; abs,y D9 4 3; ind D2 5 2 CPX: imm E0 2 2; abs EC 4 3; zp E4 3 2 CPY: imm C0 2 2; abs CC 4 3; zp C4 3 2 DEC: abs CE 6 3; zp C6 5 2; imp 3A 2 1; zpg,x D6 6 2; abs,x DE 6 3 DEX: imp CA 2 1 DEY: imp 88 2 1 EOR: imm 49 2 2; abs 4D 4 3; zp 45 3 2; (ind,x) 41 6 2; (ind),y 51 5 2; zpg,x 55 4 2; abs,x 5D 4 3; abs,y 59 4 3; ind 52 5 2 INC: abs EE 6 3; zp E6 5 2; imp 1A 2 1; zpg,x F6 6 2; abs,x FE 6 3 INX: imp E8 2 1 INY: imp C8 2 1 JMP: abs 4C 3 3; (ind,x) 7C 6 3; ind 6C 6 3 JSR: abs 20 6 3 LDA: imm A9 2 2; abs AD 4 3; zp A5 3 2; (ind,x) A1 6 2; (ind),y B1 5 2; zpg,x B5 4 2; abs,x BD 4 3; abs,y B9 4 3; ind B2 5 2 LDX: imm A2 2 2; abs AE 4 3; zp A6 3 2; abs,y BE 4 3; zpg,y B6 4 2 LDY: imm A0 2 2; abs AC 4 3; zp A4 3 2; zpg,x B4 4 2; abs,x BC 4 3 LSR: abs 4E 6 3; zp 46 5 2; imp 4A 2 1; zpg,x 56 6 2; abs,x 5E 6 3 NOP: imp EA 2 1 ORA: imm 09 2 2; abs 0D 4 3; zp 05 3 2; (ind,x) 01 6 2; (ind),y 11 5 2; zpg,x 15 4 2; abs,x 1D 4 3; abs,y 19 4 3; ind 12 5 2 PHA: imp 48 3 1 PHP: imp 08 3 1 PHX: imp DA 3 1 PHY: imp 5A 3 1 PLA: imp 68 4 1 PLP: imp 28 4 1 PLX: imp FA 4 1 PLY: imp 7A 4 1 ROL: abs 2E 6 3; zp 26 5 2; imp 2A 2 1; zpg,x 36 6 2; abs,x 3E 6 3 ROR: abs 6E 6 3; zp 66 5 2; imp 6A 2 1; zpg,x 76 6 2; abs,x 7E 6 3 RTI: imp 40 6 1 RTS: imp 60 6 1 SBC: imm E9 2 2; abs ED 4 3; zp E5 3 2; (ind,x) E1 6 2; (ind),y F1 5 2; zpg,x F5 4 2; abs,x FD 4 3; abs,y F9 4 3; ind F2 5 2 SEC: imp 38 2 1 SED: imp F8 2 1 SEI: imp 78 2 1 STA: abs 8D 4 3; zp 85 3 2; (ind,x) 81 6 2; (ind),y 91 6 2; zpg,x 95 4 2; abs,x 9D 5 3; abs,y 99 5 3; ind 92 5 2 STX: abs 8E 4 3; zp 86 3 2; zpg,y 96 4 2 STY: abs 8C 4 3; zp 84 3 2; zpg,x 94 4 2 STZ: abs 9C 4 3; zp 64 3 2; zpg,x 74 4 2; abs,x 9E 5 3 TAX: imp AA 2 1 TAY: imp A8 2 1 TRB: abs 1C 6 3; zp 14 5 2 TSB: abs 0C 6 3; zp 04 5 2 TSX: imp BA 2 1 TXA: imp 8A 2 1 TXS: imp 9A 2 1 TYA: imp 98 2 1 """ #: CL sections 4 and 5: mnemonic family -> (bytes, cycles). BBR and BBS: "5 a", #: more when the branch is taken. CL_EXTRA = { "RMB": (2, 5), "SMB": (2, 5), "BBR": (3, 5), "BBS": (3, 5), "WAI": (1, 3), "STP": (1, 3), } def ds_matrix() -> dict[int, tuple[str, str] | None]: """``DS_MATRIX`` as opcode -> (mnemonic, mode), or None for an empty cell.""" cells: dict[int, tuple[str, str] | None] = {} lines = DS_MATRIX.strip().splitlines() assert len(lines) == 16 for high, line in enumerate(lines): row = [cell.strip() for cell in line.split("|")] assert len(row) == 16, line for low, cell in enumerate(row): cells[high << 4 | low] = None if cell == "-" else tuple(cell.split()) return cells def gds_table_iv() -> dict[int, tuple[str, str, int, int]]: """``GDS_TABLE_IV`` as opcode -> (mnemonic, column, cycles, bytes).""" table: dict[int, tuple[str, str, int, int]] = {} for line in GDS_TABLE_IV.strip().splitlines(): mnemonic, entries = line.split(": ") for entry in entries.split("; "): column, opcode, cycles, size = entry.split() assert column in GDS_COLUMNS, entry assert int(opcode, 16) not in table, entry table[int(opcode, 16)] = (mnemonic, column, int(cycles), int(size)) return table