/// 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::NopOneCycle, Mode::Implied, "NOP", Form::Imp, 1); /// 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(), } } }