// SPDX-License-Identifier: MIT //! Flow of control: branches, jumps, the stack, flags, BRK, interrupts, reset, //! WAI and STP (`_control.py`). //! //! Sequences are the reference's docs/timing.md's. The interrupt requests and //! the poll are the frame's (`sixfiveohtwo::frame`): requests are sampled at //! the end of an instruction's second-to-last cycle; `CLI`, `SEI` and `PLP` //! change I after their poll; `BRK`, interrupt entries and reset do not poll. //! Nothing above emulator tier describes the CMOS part's poll points (U5); one //! measured point agrees with the frame's rule (LT `cpu` test 1: on real Lynxes //! `CLI` admits a pending IRQ only after the following instruction). //! //! Differences forced by Rust: the frame's underscored attributes are fields of //! [`sixfiveohtwo::frame::Frame`] (`_requests` is `frame.requests`, `_polled` //! `frame.polled`, `_confirm()` is `frame.access()`, `halted` is //! `frame.halted`). `_accept_reset`, `_enter_interrupt` and `_halted` are //! `reset_sequence`, `enter_interrupt_sequence` and `halted_step` here, called //! by the `Part` hooks in `cpu.rs` (as 6502-rust names them), so the inherent //! methods do not shadow the trait's. `reset_writes_stack` is a field of the //! [`crate::Row`]. use crate::core::{Bus, Cmos65c02, B, C, D, I, N, U, V, Z}; use sixfiveohtwo::frame::{Part, IRQ_LINE, NMI_LATCH, NO_POLL}; // Evidence tags (emulator-lineage docs/methodology.md). The poll rules above // are HYPOTHESIS (U5: the NMOS frame's rule, carried over; a capture with // interrupts timed against each instruction kind would settle them), agreeing // with what evidence reaches them: TEST ROM (LT `cpu` test 1, for CLI) and // CAP's 65,772 interrupt entries replayed in lockstep (MEASURED, but they do // not exercise every poll point). // DOC: DS Table 3-1. pub const VECTOR_NMI: u16 = 0xFFFA; pub const VECTOR_RESET: u16 = 0xFFFC; pub const VECTOR_IRQ: u16 = 0xFFFE; // IRQ and BRK (DS Table 3-1) impl Cmos65c02 { // -- branches ----------------------------------------------------------------- /// A conditional branch: 2 cycles, 3 taken, 4 taken across a page (CAP: `D0` 2/3/4). fn branch(&mut self, taken: bool) { // MEASURED (CAP); DOC (DS Table 6-4 for the conditions). let offset = self.fetch_byte(); if taken { self.extra += 1; self.take_branch(offset); } } /// The cycles of a branch that is taken. /// /// A dropped read of the next opcode's address, and across a page the /// same read again (CAP, both parts: the two captured bytes are equal; /// SST reads the old page with the target's low byte, C16). There is no /// NMOS-style early poll (U5). fn take_branch(&mut self, offset: u8) { // Cycles and addresses: MEASURED (CAP); SST's differ (C16, OBSERVED). // No early poll: HYPOTHESIS (U5). let pc = self.pc; self.fetch_discard(); let target = pc.wrapping_add(offset as i8 as u16); if (target ^ pc) & 0xFF00 != 0 { self.fetch_discard(); self.extra += 1; } self.pc = target; } /// BPL -- branch if N = 0 (DS Table 6-4). pub(crate) fn op_bpl(&mut self) { self.branch(self.p & N == 0); } /// BMI -- branch if N = 1 (DS Table 6-4). pub(crate) fn op_bmi(&mut self) { self.branch(self.p & N != 0); } /// BVC -- branch if V = 0 (DS Table 6-4). pub(crate) fn op_bvc(&mut self) { self.branch(self.p & V == 0); } /// BVS -- branch if V = 1 (DS Table 6-4). pub(crate) fn op_bvs(&mut self) { self.branch(self.p & V != 0); } /// BCC -- branch if C = 0 (DS Table 6-4). pub(crate) fn op_bcc(&mut self) { self.branch(self.p & C == 0); } /// BCS -- branch if C = 1 (DS Table 6-4). pub(crate) fn op_bcs(&mut self) { self.branch(self.p & C != 0); } /// BNE -- branch if Z = 0 (DS Table 6-4). pub(crate) fn op_bne(&mut self) { self.branch(self.p & Z == 0); } /// BEQ -- branch if Z = 1 (DS Table 6-4). pub(crate) fn op_beq(&mut self) { self.branch(self.p & Z != 0); } /// BRA -- branch always: 3 cycles, 4 across a page (DS Table 6-4; CAP: `80` 3/4). pub(crate) fn op_bra(&mut self) { let offset = self.fetch_byte(); self.take_branch(offset); } // -- jumps and subroutines ---------------------------------------------------- // BRA and JMP abs: DOC (DS Table 6-4) and MEASURED (CAP: `80` 3/4, `4C` 3). /// JMP abs -- jump (DS Table 6-4). pub(crate) fn op_jmp_abs(&mut self) { self.pc = self.fetch_word(); } /// JMP (abs) -- jump through a pointer; the pointer's high byte comes from the next /// address even across a page, at the cost of a cycle (DS Table 7-1; CAP: 6). pub(crate) fn op_jmp_ind(&mut self) { let pointer = self.fetch_word(); // The cycle the fix costs comes first: the operand's high byte is read // again, then the pointer (CAP, both parts; SST reads the pointer, the // NMOS part's wrong high address, then the right one, C17). MEASURED (CAP); DOC (DS 7-1). let low = self.read(pointer) as u16; self.read((pointer & 0xFF00) | (pointer.wrapping_add(1) & 0x00FF)); self.pc = ((self.read(pointer.wrapping_add(1)) as u16) << 8) | low; } /// JMP (abs,X) -- jump through a pointer at abs + X (DS Table 6-4; CAP: `7C` 6). pub(crate) fn op_jmp_abxind(&mut self) { let base = self.fetch_word(); // MEASURED (CAP); SST's dummy address differs (C17, OBSERVED). self.read(self.pc.wrapping_sub(1)); // CAP: the operand's high byte again (SST: the low, C17) let pointer = base.wrapping_add(self.x as u16); let low = self.read(pointer) as u16; self.pc = ((self.read(pointer.wrapping_add(1)) as u16) << 8) | low; } /// JSR -- push the address of its last byte and jump (DS Table 6-4; CAP: 6). pub(crate) fn op_jsr(&mut self) { // MEASURED (CAP: cycles, the stack read, the bytes pushed). let low = self.fetch_byte() as u16; self.read(0x0100 | self.s as u16); // a dropped read of the stack, as on the NMOS part (CAP) let pc = self.pc; // the address of the instruction's third byte self.push((pc >> 8) as u8); self.push(pc as u8); self.pc = ((self.fetch_byte() as u16) << 8) | low; } /// RTS -- pull the return address and continue after it (DS Table 6-4; CAP: 6). pub(crate) fn op_rts(&mut self) { // MEASURED (CAP). self.fetch_discard(); self.read(0x0100 | self.s as u16); // the stack before the increment (CAP) let low = self.pull() as u16; self.pc = ((self.pull() as u16) << 8) | low; self.fetch_byte(); // the byte JSR's address pointed at, dropped; PC steps past it } /// RTI -- pull P, then PC (DS Table 6-4; CAP: 6). pub(crate) fn op_rti(&mut self) { // MEASURED (CAP). self.fetch_discard(); self.read(0x0100 | self.s as u16); // as RTS (CAP) self.p = (self.pull() | U) & !B; // bit 5 and B are not flip-flops let low = self.pull() as u16; self.pc = ((self.pull() as u16) << 8) | low; } // -- pushes and pulls --------------------------------------------------------- // A push is 3 cycles and a pull 4 (CAP: `48` 3, `68` 4): the opcode, a // dropped read of the next byte, [pull: a dropped read of the stack], the // stack access. MEASURED (CAP); PHX PHY PLX PLY are DOC (DS, CL section 3) // and MEASURED (CAP) too. PHP pushes B and bit 5 set: DOC (DS), MEASURED (CAP). /// PHA -- push A (DS Table 6-4). pub(crate) fn op_pha(&mut self) { self.fetch_discard(); self.push(self.a); } /// PHX -- push X (DS Table 6-4; CL section 3). pub(crate) fn op_phx(&mut self) { self.fetch_discard(); self.push(self.x); } /// PHY -- push Y (DS Table 6-4; CL section 3). pub(crate) fn op_phy(&mut self) { self.fetch_discard(); self.push(self.y); } /// PHP -- push P with B and bit 5 set (DS Table 6-4). pub(crate) fn op_php(&mut self) { self.fetch_discard(); self.push(self.p | B | U); } /// PLA -- pull A (DS Table 6-4). pub(crate) fn op_pla(&mut self) { self.fetch_discard(); self.read(0x0100 | self.s as u16); let value = self.pull(); self.a = self.set_nz(value); } /// PLX -- pull X (DS Table 6-4; CL section 3). pub(crate) fn op_plx(&mut self) { self.fetch_discard(); self.read(0x0100 | self.s as u16); let value = self.pull(); self.x = self.set_nz(value); } /// PLY -- pull Y (DS Table 6-4; CL section 3). pub(crate) fn op_ply(&mut self) { self.fetch_discard(); self.read(0x0100 | self.s as u16); let value = self.pull(); self.y = self.set_nz(value); } /// PLP -- pull P; the new I counts from the next instruction's poll (DS Table 6-4). pub(crate) fn op_plp(&mut self) { // Cycles: MEASURED (CAP). When the new I takes effect: HYPOTHESIS (U5). self.fetch_discard(); self.read(0x0100 | self.s as u16); self.frame.polled_i = Some(self.p & I); self.p = (self.pull() | U) & !B; } // -- flags -------------------------------------------------------------------- // DOC (DS Table 6-4); MEASURED (CAP: two cycles, both reads; Dormann's tests). /// CLC -- clear C (DS Table 6-4). pub(crate) fn op_clc(&mut self) { self.fetch_discard(); self.p &= !C; } /// SEC -- set C (DS Table 6-4). pub(crate) fn op_sec(&mut self) { self.fetch_discard(); self.p |= C; } /// CLD -- clear D (DS Table 6-4). pub(crate) fn op_cld(&mut self) { self.fetch_discard(); self.p &= !D; } /// SED -- set D (DS Table 6-4). pub(crate) fn op_sed(&mut self) { self.fetch_discard(); self.p |= D; } /// CLV -- clear V (DS Table 6-4). pub(crate) fn op_clv(&mut self) { self.fetch_discard(); self.p &= !V; } /// CLI -- clear I; a pending IRQ is taken after the next instruction (DS Table 6-4; LT /// `cpu` test 1). pub(crate) fn op_cli(&mut self) { // TEST ROM (LT `cpu` test 1, expectations from real Lynxes); DOC (DS Table 6-4). self.fetch_discard(); self.frame.polled_i = Some(self.p & I); // this instruction's poll saw the old I (U5) self.p &= !I; } /// SEI -- set I; an IRQ already polled is still taken (DS Table 6-4; U5). pub(crate) fn op_sei(&mut self) { // HYPOTHESIS (U5: the frame's NMOS rule). self.fetch_discard(); self.frame.polled_i = Some(self.p & I); self.p |= I; } // -- BRK, interrupts, reset ---------------------------------------------------- /// The sequence that just ran does not poll: an instruction runs before any interrupt. fn skip_poll(&mut self) { self.frame.polled = Some(NO_POLL); self.frame.deferred = None; } /// BRK -- push PC + 2 and P with B set, clear D, set I, jump through `$FFFE` (DS Table 6-4; /// DS Table 4-1 note 5: two bytes). pub(crate) fn op_brk(&mut self) { // DOC only (DS Tables 4-1, 6-4, 7-1; CL section 6): no capture executes // BRK (timing.md), so its bus cycles are also OBSERVED (SST) but not MEASURED. self.fetch_byte(); // the signature byte: read and skipped let pc = self.pc; self.push((pc >> 8) as u8); self.push(pc as u8); self.push(self.p | B | U); // D is cleared on the 65C02 (DS Table 7-1 "Decimal flag"; CL section 6). self.p = (self.p | I) & !D; // A pending NMI or IRQ does not take over the vector: "BRK is executed, // and then interrupt is executed" (DS Table 7-1; C6). let low = self.read(VECTOR_IRQ) as u16; self.pc = ((self.read(VECTOR_IRQ + 1) as u16) << 8) | low; self.skip_poll(); // U5: the frame's rule } /// Take an NMI or IRQ at a boundary: seven cycles (CAP: 9,563 + 9,264 entries, all 7, /// cycles 3 to 5 writes). pub(crate) fn enter_interrupt_sequence(&mut self, kind: u8) -> u32 { // MEASURED (CAP): the cycles, SYNC in the first, the bytes pushed (B // clear), D cleared; DOC (DS Table 7-1, CL section 6) agrees. self.frame.polled_i = None; let pc = self.pc; // The chip fetches the next opcode with SYNC high and drops it (CAP: // the entries are counted from a SYNC cycle), then reads PC again. self.sync = true; self.fetch = true; self.read(pc); self.sync = false; self.read(pc); self.fetch = false; let vector = if kind == NMI_LATCH { self.frame.requests &= !NMI_LATCH; // the edge is used up VECTOR_NMI } else { VECTOR_IRQ }; self.push((pc >> 8) as u8); self.push(pc as u8); self.push(self.p | U); // B clear: what tells an IRQ from a BRK (CL section 6) self.p = (self.p | I) & !D; // D cleared by interrupts too (DS Table 7-1) let low = self.read(vector) as u16; self.pc = ((self.read(vector.wrapping_add(1)) as u16) << 8) | low; self.skip_poll(); 7 } /// The reset sequence: seven cycles (CAP, both parts; DS 3.11; six in GDS, VDS, NDS: C4). /// /// Two reads at PC, three stack cycles that leave S three lower, the /// vector. The stack cycles read on the R65C02 and write PCH, PCL and P /// on the G65SC12 (CAP; GDS Table V), by `reset_writes_stack`. I is /// set and D cleared; no other register is initialised (DS 3.11). Reset /// ends WAI and STP. pub(crate) fn reset_sequence(&mut self) -> u32 { // Seven cycles, SYNC, the stack cycles: MEASURED (CAP, both parts); // the six of GDS, VDS, NDS are DOC and overruled (C4). The addresses of // the first two reads: HYPOTHESIS (U4: the PC, which no capture shows). // D cleared and I set: DOC (DS 3.11, Table 7-1; the captures cannot see // the flags at reset). Ending WAI and STP: DOC (DS 3.4, 3.10). self.frame.reset_pending = false; self.frame.halted = false; self.waiting = false; self.stopped = false; self.frame.polled_i = None; let pc = self.pc; self.sync = true; // CAP: SYNC is high in the first of the seven self.fetch = true; self.read(pc); // U4: the addresses of these two reads are not captured self.sync = false; self.read(pc); self.fetch = false; if self.row.reset_writes_stack { self.push((pc >> 8) as u8); self.push(pc as u8); self.push(self.p | U); // CAP: the G65SC12 wrote $AF, B clear } else { for _ in 0..3 { self.read(0x0100 | self.s as u16); self.s = self.s.wrapping_sub(1); } } self.p = (self.p | I) & !D; // DS Table 7-1 "Decimal flag" let low = self.read(VECTOR_RESET) as u16; self.pc = ((self.read(VECTOR_RESET + 1) as u16) << 8) | low; self.skip_poll(); 7 } // -- WAI and STP (the W65C02S row) ------------------------------------------- /// WAI -- wait for an interrupt: three cycles, then RDY is held low until IRQ, NMI or /// reset (DS 3.4, 3.10; GF: three cycles, "the next opcode repeated"). pub(crate) fn op_wai(&mut self) { // DOC (DS 3.4, 3.10; GF's bench notes); no capture executes WAI. self.fetch_discard(); self.fetch_discard(); // GF: with IRQ already low, WAI is a plain three-cycle instruction. if self.frame.requests & (IRQ_LINE | NMI_LATCH) == 0 { self.waiting = true; self.frame.halted = true; } } /// STP -- stop the clock until reset: three cycles (DS Table 6-4; GF). pub(crate) fn op_stp(&mut self) { // DOC (DS; GF's bench notes); no capture executes STP. self.fetch_discard(); self.fetch_discard(); self.stopped = true; self.frame.halted = true; } /// A step while WAI waits or STP has stopped: one read of the next opcode's address (GF). /// /// An IRQ (whatever I says) or an NMI ends WAI. The step that finds the /// request is itself the interrupt entry, or with IRQ and I set the next /// instruction, with no vectoring (DS 3.4; GW). How many cycles the real /// chip takes to wake is U6. pub(crate) fn halted_step(&mut self) -> u32 { // The waiting cycle's read: DOC (GF). Ending WAI on IRQ whatever I says: // DOC (DS 3.4; GW). Wake timing: HYPOTHESIS (U6: a capture of a W65C02S // waking from WAI would settle it). if self.waiting && self.frame.requests & (IRQ_LINE | NMI_LATCH) != 0 { self.waiting = false; self.frame.halted = false; if self.frame.deferred.is_some() { self.frame.access(); // the request was made in a waiting cycle: it stands } self.frame.polled = None; return Part::step(self); } self.fetch_discard(); 1 } }