// SPDX-License-Identifier: MIT //! The NOPs: `$EA` and every undefined opcode, by class, each with its evidence //! (`_nops.py`). //! //! No undefined opcode changes a register, a flag or memory (DS Table 7-1 "All //! are NOP's"; CL section 9), but they differ in length, cycles and what they //! read (the reference's docs/timing.md, "Undefined opcodes"). //! //! Differences forced by Rust: the three settings the Python `NopMixin` holds //! as class attributes (`one_cycle_nops_poll`, `nop_5c_reads`, //! `nop_dc_fc_reads`) are fields of the [`crate::Row`]; the two string //! settings are the enums below. A one-cycle NOP's "no poll" is the same two //! assignments to the frame (`polled = NO_POLL`, `deferred = None`). use crate::core::{Bus, Cmos65c02}; use sixfiveohtwo::frame::NO_POLL; // Evidence tags (emulator-lineage docs/methodology.md): every length and // cycle count below is DOC (DS Table 7-1, GDS Table V, CL section 9) and // MEASURED (CAP) on the G65SC12 and the R65C02, except `$CB`/`$DB` as NOPs // (DOC only, U3). That no NOP changes a register, flag or memory: DOC (DS // Table 7-1) and MEASURED (CAP's lockstep). // // `one_cycle_nops_poll` (U7) is a field of the `Row`, where its tag is. /// U9: what `$5C` reads in its last five cycles (the reference's `nop_5c_reads`). // The `$FFFF` reads: MEASURED (CAP, both parts). The `$FF00 + low` read: // DOC (LK), MEASURED only where CAP's model knew the byte (U9). // "pc" is OBSERVED (MAME) and contradicted by CAP. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Nop5cReads { /// `"ff_page"`: `$FF00 + low operand byte` once, then `$FFFF` four times /// (LK, observed on a chip; CAP: the last four bytes are `$FFFF`'s on both /// captured parts). FfPage, /// `"pc"`: the next opcode's address five times (MAME's reading, which CAP /// contradicts on those two parts): kept as a setting for a part not /// captured. Pc, } /// C10: what `$DC` and `$FC` read in their fourth cycle (the reference's `nop_dc_fc_reads`). // MEASURED (CAP, in the executions it can tell); DOC (CL note i, LK). // "operand" is OBSERVED (SST) and contradicted by CAP. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum NopDcFcReads { /// `"absolute"`: the address their operand names (CL section 9 note i; LK; /// CAP agrees in the 8 executions it can tell). Absolute, /// `"operand"`: the last operand byte again (SST, which CAP contradicts in /// all 12 executions). Operand, } impl Cmos65c02 { /// NOP -- `$EA`: two cycles, the second a dropped read of the next byte (DS Table 6-4). pub(crate) fn op_nop(&mut self) { self.fetch_discard(); } /// NOP -- one byte, one cycle: `x3`, `xB`, and `x7`, `xF`, `$CB`, `$DB` where the /// row has no instruction there (DS Table 7-1; GDS Table V; CAP: `03` 1, `07` 1 on the /// G65SC12). pub(crate) fn op_nop_one_cycle(&mut self) { // The opcode fetch was the whole instruction. There is no second-to-last // cycle in which to poll, and no interrupt is taken after it (U7). // One cycle: MEASURED (CAP). No poll: DOC (CL, LK) and TEST ROM (LT test 8). if !self.row.one_cycle_nops_poll { self.frame.polled = Some(NO_POLL); self.frame.deferred = None; } } /// NOP -- two bytes, two cycles: `02 22 42 62 82 C2 E2` (DS Table 7-1; CAP: `02` 2). pub(crate) fn op_nop_imm(&mut self) { self.fetch_byte(); } /// NOP -- `$44`: two bytes, three cycles, reads the zero-page byte (DS Table 7-1; CL /// section 9 note g; CAP: 3). pub(crate) fn op_nop_zp(&mut self) { let address = self.fetch_byte() as u16; self.read(address); } /// NOP -- `54 D4 F4`: two bytes, four cycles, a `zp,X` read (DS Table 7-1; CL section 9 /// note h; CAP: `54` 4). pub(crate) fn op_nop_zpx(&mut self) { let address = self.ea_zpx(); self.read(address); } /// NOP -- `DC FC`: three bytes, four cycles, an absolute read (DS Table 7-1; CL section 9 /// note i; CAP: `DC` 4). pub(crate) fn op_nop_abs(&mut self) { let address = self.fetch_word(); if self.row.nop_dc_fc_reads == NopDcFcReads::Absolute { self.read(address); // C10: CL note i, LK and CAP } else { self.read(self.pc.wrapping_sub(1)); // the corpus's address } } /// NOP -- `$5C`: three bytes, eight cycles (DS Table 7-1; CAP: 8 on both parts; SST says /// 4, C1). pub(crate) fn op_nop_5c(&mut self) { let low = self.fetch_byte() as u16; self.fetch_byte(); if self.row.nop_5c_reads == Nop5cReads::FfPage { self.read(0xFF00 | low); // U9: LK; CAP could tell in 3 of 9 executions, and agrees for _ in 0..4 { self.read(0xFFFF); } } else { for _ in 0..5 { self.fetch_discard(); } } } }