// SPDX-License-Identifier: MIT //! The opcode map: handler, addressing mode, mnemonic, operand form and base //! cycles (`_dispatch.py`). //! //! `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 //! ([`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 ([`Op`]). //! - `mode`: the method that makes the addressing cycles, or `Implied` (the //! reference's `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`). //! //! The reference's 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; here rung 1 (`scripts/check_tables.py`) holds every row's table //! against the reference's `opcodes_for`. //! //! The table below is generated from the reference's `OPCODES` dict by //! `scripts/gen_dispatch.py`. Differences forced by Rust: handlers, modes and //! forms are enums where the reference has strings and bound methods, and the //! dispatch is the table and three explicit matches (the mode's value or //! address, then the handler), as 6502-rust's; the reference resolves the //! same table into unbound functions once per class (`build_table`), here once //! per CPU. use crate::core::{Bus, Cmos65c02}; use crate::cpu::Row; /// A handler, named as the reference's `_op_` methods are. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Op { Adc, And, Asl, AslA, Bbr, Bbs, Bcc, Bcs, Beq, Bit, BitImm, Bmi, Bne, Bpl, Bra, Brk, Bvc, Bvs, Clc, Cld, Cli, Clv, Cmp, Cpx, Cpy, Dec, DecA, Dex, Dey, Eor, Inc, IncA, Inx, Iny, JmpAbs, JmpAbxind, JmpInd, Jsr, Lda, Ldx, Ldy, Lsr, LsrA, Nop, Nop5c, NopAbs, NopImm, NopOneCycle, NopZp, NopZpx, Ora, Pha, Php, Phx, Phy, Pla, Plp, Plx, Ply, Rmb, Rol, RolA, Ror, RorA, Rti, Rts, Sbc, Sec, Sed, Sei, Smb, Sta, Stp, Stx, Sty, Stz, Tax, Tay, Trb, Tsb, Tsx, Txa, Txs, Tya, Wai, } /// An addressing method, the reference's `_rd_` and `_ea_`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Mode { /// The reference's `None`: the handler makes its own cycles. Implied, EaAbs, EaAbx, EaAbxFixed, EaAbyFixed, EaIdx, EaIdyFixed, EaInd, EaZp, EaZpx, EaZpy, RdAbs, RdAbx, RdAby, RdIdx, RdIdy, RdImm, RdInd, RdZp, RdZpx, RdZpy, } /// An operand form, for the instruction length and the disassembler. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Form { Abs, Abx, Abxind, Aby, Acc, Idx, Idy, Imm, Imp, Ind, Rel, Zp, Zpind, Zprel, Zpx, Zpy, } /// One row of the map: (handler, mode, mnemonic, form, base cycles). pub type OpcodeRow = (Op, Mode, &'static str, Form, u32); // Evidence for the table (tags: emulator-lineage docs/methodology.md): // - which opcodes exist, mnemonics, lengths: DOC (DS Tables 5-2 and 7-1; GDS // Tables IV and V for the 178 common opcodes); // - base cycle counts: DOC (DS Tables 4-1 and 7-1), and MEASURED (CAP) for the // 253 opcodes the two captured parts executed (rung 0: all but `$00`, // `$CB`, `$DB`); BRK's 7 is DOC (DS) only, and `$CB` WAI and `$DB` STP are // DOC (DS; GF's bench notes); // - `x7`/`xF` bit instructions: MEASURED on the R65C02 (CAP), DOC (DS, RDS); // - every count is also OBSERVED in SST (rung 1), outside C1, C2 and C3. /// opcode: (handler, mode, mnemonic, form, base cycles), for the W65C02S. #[rustfmt::skip] pub const OPCODES: [OpcodeRow; 256] = [ /* 00 */ (Op::Brk, Mode::Implied, "BRK", Form::Imm, 7), /* 01 */ (Op::Ora, Mode::RdIdx, "ORA", Form::Idx, 6), /* 02 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* 03 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 04 */ (Op::Tsb, Mode::EaZp, "TSB", Form::Zp, 5), /* 05 */ (Op::Ora, Mode::RdZp, "ORA", Form::Zp, 3), /* 06 */ (Op::Asl, Mode::EaZp, "ASL", Form::Zp, 5), /* 07 */ (Op::Rmb, Mode::Implied, "RMB0", Form::Zp, 5), /* 08 */ (Op::Php, Mode::Implied, "PHP", Form::Imp, 3), /* 09 */ (Op::Ora, Mode::RdImm, "ORA", Form::Imm, 2), /* 0A */ (Op::AslA, Mode::Implied, "ASL", Form::Acc, 2), /* 0B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 0C */ (Op::Tsb, Mode::EaAbs, "TSB", Form::Abs, 6), /* 0D */ (Op::Ora, Mode::RdAbs, "ORA", Form::Abs, 4), /* 0E */ (Op::Asl, Mode::EaAbs, "ASL", Form::Abs, 6), /* 0F */ (Op::Bbr, Mode::Implied, "BBR0", Form::Zprel, 5), /* 10 */ (Op::Bpl, Mode::Implied, "BPL", Form::Rel, 2), /* 11 */ (Op::Ora, Mode::RdIdy, "ORA", Form::Idy, 5), /* 12 */ (Op::Ora, Mode::RdInd, "ORA", Form::Zpind, 5), /* 13 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 14 */ (Op::Trb, Mode::EaZp, "TRB", Form::Zp, 5), /* 15 */ (Op::Ora, Mode::RdZpx, "ORA", Form::Zpx, 4), /* 16 */ (Op::Asl, Mode::EaZpx, "ASL", Form::Zpx, 6), /* 17 */ (Op::Rmb, Mode::Implied, "RMB1", Form::Zp, 5), /* 18 */ (Op::Clc, Mode::Implied, "CLC", Form::Imp, 2), /* 19 */ (Op::Ora, Mode::RdAby, "ORA", Form::Aby, 4), /* 1A */ (Op::IncA, Mode::Implied, "INC", Form::Acc, 2), /* 1B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 1C */ (Op::Trb, Mode::EaAbs, "TRB", Form::Abs, 6), /* 1D */ (Op::Ora, Mode::RdAbx, "ORA", Form::Abx, 4), /* 1E */ (Op::Asl, Mode::EaAbx, "ASL", Form::Abx, 6), /* 1F */ (Op::Bbr, Mode::Implied, "BBR1", Form::Zprel, 5), /* 20 */ (Op::Jsr, Mode::Implied, "JSR", Form::Abs, 6), /* 21 */ (Op::And, Mode::RdIdx, "AND", Form::Idx, 6), /* 22 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* 23 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 24 */ (Op::Bit, Mode::RdZp, "BIT", Form::Zp, 3), /* 25 */ (Op::And, Mode::RdZp, "AND", Form::Zp, 3), /* 26 */ (Op::Rol, Mode::EaZp, "ROL", Form::Zp, 5), /* 27 */ (Op::Rmb, Mode::Implied, "RMB2", Form::Zp, 5), /* 28 */ (Op::Plp, Mode::Implied, "PLP", Form::Imp, 4), /* 29 */ (Op::And, Mode::RdImm, "AND", Form::Imm, 2), /* 2A */ (Op::RolA, Mode::Implied, "ROL", Form::Acc, 2), /* 2B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 2C */ (Op::Bit, Mode::RdAbs, "BIT", Form::Abs, 4), /* 2D */ (Op::And, Mode::RdAbs, "AND", Form::Abs, 4), /* 2E */ (Op::Rol, Mode::EaAbs, "ROL", Form::Abs, 6), /* 2F */ (Op::Bbr, Mode::Implied, "BBR2", Form::Zprel, 5), /* 30 */ (Op::Bmi, Mode::Implied, "BMI", Form::Rel, 2), /* 31 */ (Op::And, Mode::RdIdy, "AND", Form::Idy, 5), /* 32 */ (Op::And, Mode::RdInd, "AND", Form::Zpind, 5), /* 33 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 34 */ (Op::Bit, Mode::RdZpx, "BIT", Form::Zpx, 4), /* 35 */ (Op::And, Mode::RdZpx, "AND", Form::Zpx, 4), /* 36 */ (Op::Rol, Mode::EaZpx, "ROL", Form::Zpx, 6), /* 37 */ (Op::Rmb, Mode::Implied, "RMB3", Form::Zp, 5), /* 38 */ (Op::Sec, Mode::Implied, "SEC", Form::Imp, 2), /* 39 */ (Op::And, Mode::RdAby, "AND", Form::Aby, 4), /* 3A */ (Op::DecA, Mode::Implied, "DEC", Form::Acc, 2), /* 3B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 3C */ (Op::Bit, Mode::RdAbx, "BIT", Form::Abx, 4), /* 3D */ (Op::And, Mode::RdAbx, "AND", Form::Abx, 4), /* 3E */ (Op::Rol, Mode::EaAbx, "ROL", Form::Abx, 6), /* 3F */ (Op::Bbr, Mode::Implied, "BBR3", Form::Zprel, 5), /* 40 */ (Op::Rti, Mode::Implied, "RTI", Form::Imp, 6), /* 41 */ (Op::Eor, Mode::RdIdx, "EOR", Form::Idx, 6), /* 42 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* 43 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 44 */ (Op::NopZp, Mode::Implied, "NOP", Form::Zp, 3), /* 45 */ (Op::Eor, Mode::RdZp, "EOR", Form::Zp, 3), /* 46 */ (Op::Lsr, Mode::EaZp, "LSR", Form::Zp, 5), /* 47 */ (Op::Rmb, Mode::Implied, "RMB4", Form::Zp, 5), /* 48 */ (Op::Pha, Mode::Implied, "PHA", Form::Imp, 3), /* 49 */ (Op::Eor, Mode::RdImm, "EOR", Form::Imm, 2), /* 4A */ (Op::LsrA, Mode::Implied, "LSR", Form::Acc, 2), /* 4B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 4C */ (Op::JmpAbs, Mode::Implied, "JMP", Form::Abs, 3), /* 4D */ (Op::Eor, Mode::RdAbs, "EOR", Form::Abs, 4), /* 4E */ (Op::Lsr, Mode::EaAbs, "LSR", Form::Abs, 6), /* 4F */ (Op::Bbr, Mode::Implied, "BBR4", Form::Zprel, 5), /* 50 */ (Op::Bvc, Mode::Implied, "BVC", Form::Rel, 2), /* 51 */ (Op::Eor, Mode::RdIdy, "EOR", Form::Idy, 5), /* 52 */ (Op::Eor, Mode::RdInd, "EOR", Form::Zpind, 5), /* 53 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 54 */ (Op::NopZpx, Mode::Implied, "NOP", Form::Zpx, 4), /* 55 */ (Op::Eor, Mode::RdZpx, "EOR", Form::Zpx, 4), /* 56 */ (Op::Lsr, Mode::EaZpx, "LSR", Form::Zpx, 6), /* 57 */ (Op::Rmb, Mode::Implied, "RMB5", Form::Zp, 5), /* 58 */ (Op::Cli, Mode::Implied, "CLI", Form::Imp, 2), /* 59 */ (Op::Eor, Mode::RdAby, "EOR", Form::Aby, 4), /* 5A */ (Op::Phy, Mode::Implied, "PHY", Form::Imp, 3), /* 5B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 5C */ (Op::Nop5c, Mode::Implied, "NOP", Form::Abs, 8), /* 5D */ (Op::Eor, Mode::RdAbx, "EOR", Form::Abx, 4), /* 5E */ (Op::Lsr, Mode::EaAbx, "LSR", Form::Abx, 6), /* 5F */ (Op::Bbr, Mode::Implied, "BBR5", Form::Zprel, 5), /* 60 */ (Op::Rts, Mode::Implied, "RTS", Form::Imp, 6), /* 61 */ (Op::Adc, Mode::RdIdx, "ADC", Form::Idx, 6), /* 62 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* 63 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 64 */ (Op::Stz, Mode::EaZp, "STZ", Form::Zp, 3), /* 65 */ (Op::Adc, Mode::RdZp, "ADC", Form::Zp, 3), /* 66 */ (Op::Ror, Mode::EaZp, "ROR", Form::Zp, 5), /* 67 */ (Op::Rmb, Mode::Implied, "RMB6", Form::Zp, 5), /* 68 */ (Op::Pla, Mode::Implied, "PLA", Form::Imp, 4), /* 69 */ (Op::Adc, Mode::RdImm, "ADC", Form::Imm, 2), /* 6A */ (Op::RorA, Mode::Implied, "ROR", Form::Acc, 2), /* 6B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 6C */ (Op::JmpInd, Mode::Implied, "JMP", Form::Ind, 6), /* 6D */ (Op::Adc, Mode::RdAbs, "ADC", Form::Abs, 4), /* 6E */ (Op::Ror, Mode::EaAbs, "ROR", Form::Abs, 6), /* 6F */ (Op::Bbr, Mode::Implied, "BBR6", Form::Zprel, 5), /* 70 */ (Op::Bvs, Mode::Implied, "BVS", Form::Rel, 2), /* 71 */ (Op::Adc, Mode::RdIdy, "ADC", Form::Idy, 5), /* 72 */ (Op::Adc, Mode::RdInd, "ADC", Form::Zpind, 5), /* 73 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 74 */ (Op::Stz, Mode::EaZpx, "STZ", Form::Zpx, 4), /* 75 */ (Op::Adc, Mode::RdZpx, "ADC", Form::Zpx, 4), /* 76 */ (Op::Ror, Mode::EaZpx, "ROR", Form::Zpx, 6), /* 77 */ (Op::Rmb, Mode::Implied, "RMB7", Form::Zp, 5), /* 78 */ (Op::Sei, Mode::Implied, "SEI", Form::Imp, 2), /* 79 */ (Op::Adc, Mode::RdAby, "ADC", Form::Aby, 4), /* 7A */ (Op::Ply, Mode::Implied, "PLY", Form::Imp, 4), /* 7B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 7C */ (Op::JmpAbxind, Mode::Implied, "JMP", Form::Abxind, 6), /* 7D */ (Op::Adc, Mode::RdAbx, "ADC", Form::Abx, 4), /* 7E */ (Op::Ror, Mode::EaAbx, "ROR", Form::Abx, 6), /* 7F */ (Op::Bbr, Mode::Implied, "BBR7", Form::Zprel, 5), /* 80 */ (Op::Bra, Mode::Implied, "BRA", Form::Rel, 3), /* 81 */ (Op::Sta, Mode::EaIdx, "STA", Form::Idx, 6), /* 82 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* 83 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 84 */ (Op::Sty, Mode::EaZp, "STY", Form::Zp, 3), /* 85 */ (Op::Sta, Mode::EaZp, "STA", Form::Zp, 3), /* 86 */ (Op::Stx, Mode::EaZp, "STX", Form::Zp, 3), /* 87 */ (Op::Smb, Mode::Implied, "SMB0", Form::Zp, 5), /* 88 */ (Op::Dey, Mode::Implied, "DEY", Form::Imp, 2), /* 89 */ (Op::BitImm, Mode::Implied, "BIT", Form::Imm, 2), /* 8A */ (Op::Txa, Mode::Implied, "TXA", Form::Imp, 2), /* 8B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 8C */ (Op::Sty, Mode::EaAbs, "STY", Form::Abs, 4), /* 8D */ (Op::Sta, Mode::EaAbs, "STA", Form::Abs, 4), /* 8E */ (Op::Stx, Mode::EaAbs, "STX", Form::Abs, 4), /* 8F */ (Op::Bbs, Mode::Implied, "BBS0", Form::Zprel, 5), /* 90 */ (Op::Bcc, Mode::Implied, "BCC", Form::Rel, 2), /* 91 */ (Op::Sta, Mode::EaIdyFixed, "STA", Form::Idy, 6), /* 92 */ (Op::Sta, Mode::EaInd, "STA", Form::Zpind, 5), /* 93 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 94 */ (Op::Sty, Mode::EaZpx, "STY", Form::Zpx, 4), /* 95 */ (Op::Sta, Mode::EaZpx, "STA", Form::Zpx, 4), /* 96 */ (Op::Stx, Mode::EaZpy, "STX", Form::Zpy, 4), /* 97 */ (Op::Smb, Mode::Implied, "SMB1", Form::Zp, 5), /* 98 */ (Op::Tya, Mode::Implied, "TYA", Form::Imp, 2), /* 99 */ (Op::Sta, Mode::EaAbyFixed, "STA", Form::Aby, 5), /* 9A */ (Op::Txs, Mode::Implied, "TXS", Form::Imp, 2), /* 9B */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* 9C */ (Op::Stz, Mode::EaAbs, "STZ", Form::Abs, 4), /* 9D */ (Op::Sta, Mode::EaAbxFixed, "STA", Form::Abx, 5), /* 9E */ (Op::Stz, Mode::EaAbxFixed, "STZ", Form::Abx, 5), /* 9F */ (Op::Bbs, Mode::Implied, "BBS1", Form::Zprel, 5), /* A0 */ (Op::Ldy, Mode::RdImm, "LDY", Form::Imm, 2), /* A1 */ (Op::Lda, Mode::RdIdx, "LDA", Form::Idx, 6), /* A2 */ (Op::Ldx, Mode::RdImm, "LDX", Form::Imm, 2), /* A3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* A4 */ (Op::Ldy, Mode::RdZp, "LDY", Form::Zp, 3), /* A5 */ (Op::Lda, Mode::RdZp, "LDA", Form::Zp, 3), /* A6 */ (Op::Ldx, Mode::RdZp, "LDX", Form::Zp, 3), /* A7 */ (Op::Smb, Mode::Implied, "SMB2", Form::Zp, 5), /* A8 */ (Op::Tay, Mode::Implied, "TAY", Form::Imp, 2), /* A9 */ (Op::Lda, Mode::RdImm, "LDA", Form::Imm, 2), /* AA */ (Op::Tax, Mode::Implied, "TAX", Form::Imp, 2), /* AB */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* AC */ (Op::Ldy, Mode::RdAbs, "LDY", Form::Abs, 4), /* AD */ (Op::Lda, Mode::RdAbs, "LDA", Form::Abs, 4), /* AE */ (Op::Ldx, Mode::RdAbs, "LDX", Form::Abs, 4), /* AF */ (Op::Bbs, Mode::Implied, "BBS2", Form::Zprel, 5), /* B0 */ (Op::Bcs, Mode::Implied, "BCS", Form::Rel, 2), /* B1 */ (Op::Lda, Mode::RdIdy, "LDA", Form::Idy, 5), /* B2 */ (Op::Lda, Mode::RdInd, "LDA", Form::Zpind, 5), /* B3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* B4 */ (Op::Ldy, Mode::RdZpx, "LDY", Form::Zpx, 4), /* B5 */ (Op::Lda, Mode::RdZpx, "LDA", Form::Zpx, 4), /* B6 */ (Op::Ldx, Mode::RdZpy, "LDX", Form::Zpy, 4), /* B7 */ (Op::Smb, Mode::Implied, "SMB3", Form::Zp, 5), /* B8 */ (Op::Clv, Mode::Implied, "CLV", Form::Imp, 2), /* B9 */ (Op::Lda, Mode::RdAby, "LDA", Form::Aby, 4), /* BA */ (Op::Tsx, Mode::Implied, "TSX", Form::Imp, 2), /* BB */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* BC */ (Op::Ldy, Mode::RdAbx, "LDY", Form::Abx, 4), /* BD */ (Op::Lda, Mode::RdAbx, "LDA", Form::Abx, 4), /* BE */ (Op::Ldx, Mode::RdAby, "LDX", Form::Aby, 4), /* BF */ (Op::Bbs, Mode::Implied, "BBS3", Form::Zprel, 5), /* C0 */ (Op::Cpy, Mode::RdImm, "CPY", Form::Imm, 2), /* C1 */ (Op::Cmp, Mode::RdIdx, "CMP", Form::Idx, 6), /* C2 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* C3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* C4 */ (Op::Cpy, Mode::RdZp, "CPY", Form::Zp, 3), /* C5 */ (Op::Cmp, Mode::RdZp, "CMP", Form::Zp, 3), /* C6 */ (Op::Dec, Mode::EaZp, "DEC", Form::Zp, 5), /* C7 */ (Op::Smb, Mode::Implied, "SMB4", Form::Zp, 5), /* C8 */ (Op::Iny, Mode::Implied, "INY", Form::Imp, 2), /* C9 */ (Op::Cmp, Mode::RdImm, "CMP", Form::Imm, 2), /* CA */ (Op::Dex, Mode::Implied, "DEX", Form::Imp, 2), /* CB */ (Op::Wai, Mode::Implied, "WAI", Form::Imp, 3), /* CC */ (Op::Cpy, Mode::RdAbs, "CPY", Form::Abs, 4), /* CD */ (Op::Cmp, Mode::RdAbs, "CMP", Form::Abs, 4), /* CE */ (Op::Dec, Mode::EaAbs, "DEC", Form::Abs, 6), /* CF */ (Op::Bbs, Mode::Implied, "BBS4", Form::Zprel, 5), /* D0 */ (Op::Bne, Mode::Implied, "BNE", Form::Rel, 2), /* D1 */ (Op::Cmp, Mode::RdIdy, "CMP", Form::Idy, 5), /* D2 */ (Op::Cmp, Mode::RdInd, "CMP", Form::Zpind, 5), /* D3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* D4 */ (Op::NopZpx, Mode::Implied, "NOP", Form::Zpx, 4), /* D5 */ (Op::Cmp, Mode::RdZpx, "CMP", Form::Zpx, 4), /* D6 */ (Op::Dec, Mode::EaZpx, "DEC", Form::Zpx, 6), /* D7 */ (Op::Smb, Mode::Implied, "SMB5", Form::Zp, 5), /* D8 */ (Op::Cld, Mode::Implied, "CLD", Form::Imp, 2), /* D9 */ (Op::Cmp, Mode::RdAby, "CMP", Form::Aby, 4), /* DA */ (Op::Phx, Mode::Implied, "PHX", Form::Imp, 3), /* DB */ (Op::Stp, Mode::Implied, "STP", Form::Imp, 3), /* DC */ (Op::NopAbs, Mode::Implied, "NOP", Form::Abs, 4), /* DD */ (Op::Cmp, Mode::RdAbx, "CMP", Form::Abx, 4), /* DE */ (Op::Dec, Mode::EaAbxFixed, "DEC", Form::Abx, 7), /* DF */ (Op::Bbs, Mode::Implied, "BBS5", Form::Zprel, 5), /* E0 */ (Op::Cpx, Mode::RdImm, "CPX", Form::Imm, 2), /* E1 */ (Op::Sbc, Mode::RdIdx, "SBC", Form::Idx, 6), /* E2 */ (Op::NopImm, Mode::Implied, "NOP", Form::Imm, 2), /* E3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* E4 */ (Op::Cpx, Mode::RdZp, "CPX", Form::Zp, 3), /* E5 */ (Op::Sbc, Mode::RdZp, "SBC", Form::Zp, 3), /* E6 */ (Op::Inc, Mode::EaZp, "INC", Form::Zp, 5), /* E7 */ (Op::Smb, Mode::Implied, "SMB6", Form::Zp, 5), /* E8 */ (Op::Inx, Mode::Implied, "INX", Form::Imp, 2), /* E9 */ (Op::Sbc, Mode::RdImm, "SBC", Form::Imm, 2), /* EA */ (Op::Nop, Mode::Implied, "NOP", Form::Imp, 2), /* EB */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* EC */ (Op::Cpx, Mode::RdAbs, "CPX", Form::Abs, 4), /* ED */ (Op::Sbc, Mode::RdAbs, "SBC", Form::Abs, 4), /* EE */ (Op::Inc, Mode::EaAbs, "INC", Form::Abs, 6), /* EF */ (Op::Bbs, Mode::Implied, "BBS6", Form::Zprel, 5), /* F0 */ (Op::Beq, Mode::Implied, "BEQ", Form::Rel, 2), /* F1 */ (Op::Sbc, Mode::RdIdy, "SBC", Form::Idy, 5), /* F2 */ (Op::Sbc, Mode::RdInd, "SBC", Form::Zpind, 5), /* F3 */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* F4 */ (Op::NopZpx, Mode::Implied, "NOP", Form::Zpx, 4), /* F5 */ (Op::Sbc, Mode::RdZpx, "SBC", Form::Zpx, 4), /* F6 */ (Op::Inc, Mode::EaZpx, "INC", Form::Zpx, 6), /* F7 */ (Op::Smb, Mode::Implied, "SMB7", Form::Zp, 5), /* F8 */ (Op::Sed, Mode::Implied, "SED", Form::Imp, 2), /* F9 */ (Op::Sbc, Mode::RdAby, "SBC", Form::Aby, 4), /* FA */ (Op::Plx, Mode::Implied, "PLX", Form::Imp, 4), /* FB */ (Op::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1), /* FC */ (Op::NopAbs, Mode::Implied, "NOP", Form::Abs, 4), /* FD */ (Op::Sbc, Mode::RdAbx, "SBC", Form::Abx, 4), /* FE */ (Op::Inc, Mode::EaAbxFixed, "INC", Form::Abx, 7), /* FF */ (Op::Bbs, Mode::Implied, "BBS7", Form::Zprel, 5), ]; /// What a part has in place of an instruction it lacks: one byte, one cycle. // MEASURED for `x7`/`xF` on the G65SC12 (CAP); DOC (GDS Table V, VDS, NDS); // for `$CB`/`$DB` DOC only (U3). pub const ONE_CYCLE_NOP: OpcodeRow = (Op::Nop, Mode::Implied, "NOP", Form::Imp, 2); /// Whether `opcode` is one of the 32 bit-instruction opcodes (`RMB`, `SMB`, /// `BBR`, `BBS`): columns 7 and F (the reference's `BIT_OPCODES`). // DOC: DS Table 5-2, RDS. pub const fn is_bit_opcode(opcode: u8) -> bool { opcode & 0x0F == 0x07 || opcode & 0x0F == 0x0F } /// `WAI` and `STP`. pub const WAI_STP_OPCODES: [u8; 2] = [0xCB, 0xDB]; // DOC: DS Table 5-2 /// Instruction length in bytes by operand form (the reference's `FORM_BYTES`). /// `BRK` is listed as `imm`: it is a two-byte instruction (DS Table 4-1 note 5). // DOC: DS Tables 4-1 and 5-2; MEASURED (CAP: the PC after each instruction). pub const fn form_bytes(form: Form) -> usize { match form { Form::Imp | Form::Acc => 1, Form::Imm | Form::Zp | Form::Zpx | Form::Zpy => 2, Form::Abs | Form::Abx | Form::Aby => 3, Form::Ind => 3, Form::Idx | Form::Idy | Form::Zpind => 2, Form::Abxind => 3, Form::Rel => 2, Form::Zprel => 3, } } /// One entry of a CPU's table: (handler, mode, base cycles). pub type Entry = (Op, Mode, u32); /// The 256 rows of one part: `OPCODES` with the cells its settings replace. pub fn opcodes_for(row: &Row) -> [OpcodeRow; 256] { let mut table = OPCODES; // `x7`/`xF` as NOPs: MEASURED (CAP, G65SC12), DOC; SST disagrees (C2, OBSERVED). // `$CB`/`$DB` as NOPs: DOC only (GDS, VDS, NDS, RDS); not captured (U3). if !row.bit_instructions { for (opcode, cell) in table.iter_mut().enumerate() { if is_bit_opcode(opcode as u8) { *cell = ONE_CYCLE_NOP; // C2: CAP on the G65SC12, GDS, VDS, NDS; SST disagrees } } } if !row.wai_stp { for opcode in WAI_STP_OPCODES { table[opcode as usize] = ONE_CYCLE_NOP; // U3: GDS, VDS, NDS "XB: 1 byte, 1 cycle"; not captured } } table } /// The 256 (handler, mode, base cycles) entries of `row`. pub fn build_table(row: &Row) -> [Entry; 256] { let rows = opcodes_for(row); let mut table = [(Op::NopOneCycle, Mode::Implied, 1); 256]; for (entry, (handler, mode, _mnemonic, _form, cycles)) in table.iter_mut().zip(rows) { *entry = (handler, mode, cycles); } table } impl Cmos65c02 { /// The operand's value, for a handler that reads it (`rd_`). #[inline] pub(crate) fn operand_value(&mut self, mode: Mode) -> u8 { match mode { Mode::RdImm => self.rd_imm(), Mode::RdZp => self.rd_zp(), Mode::RdZpx => self.rd_zpx(), Mode::RdZpy => self.rd_zpy(), Mode::RdAbs => self.rd_abs(), Mode::RdAbx => self.rd_abx(), Mode::RdAby => self.rd_aby(), Mode::RdIdx => self.rd_idx(), Mode::RdIdy => self.rd_idy(), Mode::RdInd => self.rd_ind(), _ => unreachable!("{mode:?} is not a reading mode"), } } /// The operand's address, for a handler that writes or modifies it (`ea_`). #[inline] pub(crate) fn operand_address(&mut self, mode: Mode) -> u16 { match mode { Mode::EaZp => self.ea_zp(), Mode::EaZpx => self.ea_zpx(), Mode::EaZpy => self.ea_zpy(), Mode::EaAbs => self.ea_abs(), Mode::EaAbx => self.ea_abx(), Mode::EaAbxFixed => self.ea_abx_fixed(), Mode::EaAbyFixed => self.ea_aby_fixed(), Mode::EaIdx => self.ea_idx(), Mode::EaIdyFixed => self.ea_idy_fixed(), Mode::EaInd => self.ea_ind(), _ => unreachable!("{mode:?} is not an address mode"), } } /// Run one instruction's handler after its opcode fetch (`handler(self, mode)`). #[inline] pub(crate) fn dispatch(&mut self, op: Op, mode: Mode) { match op { // -- handlers that read their operand --------------------------- Op::Adc => { let v = self.operand_value(mode); self.op_adc(v) } Op::Sbc => { let v = self.operand_value(mode); self.op_sbc(v) } Op::And => { let v = self.operand_value(mode); self.op_and(v) } Op::Ora => { let v = self.operand_value(mode); self.op_ora(v) } Op::Eor => { let v = self.operand_value(mode); self.op_eor(v) } Op::Cmp => { let v = self.operand_value(mode); self.op_cmp(v) } Op::Cpx => { let v = self.operand_value(mode); self.op_cpx(v) } Op::Cpy => { let v = self.operand_value(mode); self.op_cpy(v) } Op::Bit => { let v = self.operand_value(mode); self.op_bit(v) } Op::Lda => { let v = self.operand_value(mode); self.op_lda(v) } Op::Ldx => { let v = self.operand_value(mode); self.op_ldx(v) } Op::Ldy => { let v = self.operand_value(mode); self.op_ldy(v) } // -- handlers that write or modify their operand ---------------- Op::Sta => { let a = self.operand_address(mode); self.op_sta(a) } Op::Stx => { let a = self.operand_address(mode); self.op_stx(a) } Op::Sty => { let a = self.operand_address(mode); self.op_sty(a) } Op::Stz => { let a = self.operand_address(mode); self.op_stz(a) } Op::Asl => { let a = self.operand_address(mode); self.op_asl(a) } Op::Lsr => { let a = self.operand_address(mode); self.op_lsr(a) } Op::Rol => { let a = self.operand_address(mode); self.op_rol(a) } Op::Ror => { let a = self.operand_address(mode); self.op_ror(a) } Op::Inc => { let a = self.operand_address(mode); self.op_inc(a) } Op::Dec => { let a = self.operand_address(mode); self.op_dec(a) } Op::Tsb => { let a = self.operand_address(mode); self.op_tsb(a) } Op::Trb => { let a = self.operand_address(mode); self.op_trb(a) } // -- handlers that make their own cycles (mode None) ------------ Op::AslA => self.op_asl_a(), Op::LsrA => self.op_lsr_a(), Op::RolA => self.op_rol_a(), Op::RorA => self.op_ror_a(), Op::IncA => self.op_inc_a(), Op::DecA => self.op_dec_a(), Op::BitImm => self.op_bit_imm(), Op::Rmb => self.op_rmb(), Op::Smb => self.op_smb(), Op::Bbr => self.op_bbr(), Op::Bbs => self.op_bbs(), Op::Bpl => self.op_bpl(), Op::Bmi => self.op_bmi(), Op::Bvc => self.op_bvc(), Op::Bvs => self.op_bvs(), Op::Bcc => self.op_bcc(), Op::Bcs => self.op_bcs(), Op::Bne => self.op_bne(), Op::Beq => self.op_beq(), Op::Bra => self.op_bra(), Op::JmpAbs => self.op_jmp_abs(), Op::JmpInd => self.op_jmp_ind(), Op::JmpAbxind => self.op_jmp_abxind(), Op::Jsr => self.op_jsr(), Op::Rts => self.op_rts(), Op::Rti => self.op_rti(), Op::Pha => self.op_pha(), Op::Phx => self.op_phx(), Op::Phy => self.op_phy(), Op::Php => self.op_php(), Op::Pla => self.op_pla(), Op::Plx => self.op_plx(), Op::Ply => self.op_ply(), Op::Plp => self.op_plp(), Op::Clc => self.op_clc(), Op::Sec => self.op_sec(), Op::Cld => self.op_cld(), Op::Sed => self.op_sed(), Op::Clv => self.op_clv(), Op::Cli => self.op_cli(), Op::Sei => self.op_sei(), Op::Brk => self.op_brk(), Op::Wai => self.op_wai(), Op::Stp => self.op_stp(), Op::Tax => self.op_tax(), Op::Tay => self.op_tay(), Op::Txa => self.op_txa(), Op::Tya => self.op_tya(), Op::Tsx => self.op_tsx(), Op::Txs => self.op_txs(), Op::Inx => self.op_inx(), Op::Iny => self.op_iny(), Op::Dex => self.op_dex(), Op::Dey => self.op_dey(), Op::Nop => self.op_nop(), Op::NopOneCycle => self.op_nop_one_cycle(), Op::NopImm => self.op_nop_imm(), Op::NopZp => self.op_nop_zp(), Op::NopZpx => self.op_nop_zpx(), Op::NopAbs => self.op_nop_abs(), Op::Nop5c => self.op_nop_5c(), } } }