// SPDX-License-Identifier: MIT //! Branches, jumps, calls, returns, the stack, flags, BRK and COP, interrupts, //! reset, WAI, STP (`_control.py`). //! //! Rows are Table 5-7's. Emulation mode keeps the 6502's instructions' stack in //! page one and lets the new ones (JSL, JSR (a,x), PEA, PEI, PER, PHD, PLD, PLB, //! RTL) step a 16-bit S, then forces SH back to $01 (U2, C5). Interrupts, BRK //! and COP clear D, set I and PBR = 0, and push PBR only in native mode (U8). //! //! Differences forced by Rust: the vector dictionaries are `match`es; the //! frame's underscored attributes (`_requests`, `_polled`, `_polled_i`, //! `_deferred`, `halted`) are the public fields of 6502-rust's [`Frame`]; //! `_boundary_interrupt`'s override is [`W65C816::boundary_abort`], called //! from the `Part` implementation in `cpu.rs`. `request_abort` exists twice: //! on the CPU, and as [`request_abort`] on a [`Frame`], for a bus callback. use crate::core::{ nz16, nz8, Bus, C, D, DATA, I, IO, M, N, OPCODE, PROGRAM, V, VECTOR, W65C816, X, Z, }; use crate::loads::Index; use sixfiveohtwo::frame::{Frame, IRQ_LINE, NMI_LATCH, NO_POLL}; pub const ABORT_LATCH: u8 = 0x08; // a request bit beside the frame's IRQ_LINE and NMI_LATCH // DS Tables 5-2 and 5-3 (p. 30): emulation and native vectors, bank 0. pub fn vector_emulation(kind: u8) -> u32 { match kind { NMI_LATCH => 0xFFFA, IRQ_LINE => 0xFFFE, ABORT_LATCH => 0xFFF8, _ => unreachable!("no vector for request {kind:#x}"), } } pub fn vector_native(kind: u8) -> u32 { match kind { NMI_LATCH => 0xFFEA, IRQ_LINE => 0xFFEE, ABORT_LATCH => 0xFFE8, _ => unreachable!("no vector for request {kind:#x}"), } } pub const VECTOR_RESET: u32 = 0xFFFC; pub const VECTOR_BRK: [u32; 2] = [0xFFE6, 0xFFFE]; // native, emulation pub const VECTOR_COP: [u32; 2] = [0xFFE4, 0xFFF4]; /// Latch an ABORT request on a frame: taken at the next boundary, before NMI /// and IRQ (the reference's `request_abort`, for a bus callback). pub fn request_abort(frame: &mut Frame) { frame.request(frame.requests | ABORT_LATCH); } impl W65C816 { // -- branches (rows 20, 21) ------------------------------------------ fn branch(&mut self, taken: bool) { let offset = self.fetch() as u32; if !taken { return; } self.io_operand(); // note 5 let pc = self.pc as u32; let target = (pc + offset).wrapping_sub((offset & 0x80) << 1) & 0xFFFF; if self.e != 0 && (target ^ pc) & 0xFF00 != 0 { self.io_operand(); // note 6: emulation mode only } self.pc = target as u16; } /// BPL -- branch if N = 0 (DS Table 5-5 p. 32). pub(crate) fn op_bpl(&mut self) { self.branch(self.p & N == 0); } /// BMI -- branch if N = 1 (DS Table 5-5 p. 32). pub(crate) fn op_bmi(&mut self) { self.branch(self.p & N != 0); } /// BVC -- branch if V = 0 (DS Table 5-5 p. 32). pub(crate) fn op_bvc(&mut self) { self.branch(self.p & V == 0); } /// BVS -- branch if V = 1 (DS Table 5-5 p. 32). pub(crate) fn op_bvs(&mut self) { self.branch(self.p & V != 0); } /// BCC -- branch if C = 0 (DS Table 5-5 p. 32). pub(crate) fn op_bcc(&mut self) { self.branch(self.p & C == 0); } /// BCS -- branch if C = 1 (DS Table 5-5 p. 32). pub(crate) fn op_bcs(&mut self) { self.branch(self.p & C != 0); } /// BNE -- branch if Z = 0 (DS Table 5-5 p. 32). pub(crate) fn op_bne(&mut self) { self.branch(self.p & Z == 0); } /// BEQ -- branch if Z = 1 (DS Table 5-5 p. 32). pub(crate) fn op_beq(&mut self) { self.branch(self.p & Z != 0); } /// BRA -- branch always (DS Table 5-5 p. 32). pub(crate) fn op_bra(&mut self) { self.branch(true); } /// BRL -- branch always, 16-bit offset, within the bank (DS Table 5-5 p. 32). pub(crate) fn op_brl(&mut self) { let offset = self.fetch_word(); self.io_operand(); self.pc = self.pc.wrapping_add(offset); } // -- jumps (rows 1b, 2a, 3a, 3b, 4b) -------------------------------------- /// JMP a -- AA -> PC (DS Table 5-5 p. 32). pub(crate) fn op_jmp(&mut self) { self.pc = self.fetch_word(); } /// JML al -- AAB,AA -> PBR,PC (DS Table 5-5 p. 32). pub(crate) fn op_jml(&mut self) { let address = self.fetch_word(); self.pbr = self.fetch(); self.pc = address; } /// JMP (a) -- the pointer is in bank 0, no NMOS page bug (DS Table 5-5 p. 32). pub(crate) fn op_jmp_ind(&mut self) { let pointer = self.fetch_word() as u32; let low = self.cycle_read(pointer, DATA) as u16; self.pc = (self.cycle_read((pointer + 1) & 0xFFFF, DATA) as u16) << 8 | low; } /// JML [a] -- a three-byte pointer in bank 0 -> PBR,PC (DS Table 5-5 p. 32). pub(crate) fn op_jml_ind(&mut self) { let pointer = self.fetch_word() as u32; let low = self.cycle_read(pointer, DATA) as u16; let high = self.cycle_read((pointer + 1) & 0xFFFF, DATA) as u16; self.pbr = self.cycle_read((pointer + 2) & 0xFFFF, DATA); self.pc = high << 8 | low; } fn pointer_in_program_bank(&mut self, base: u32) -> u16 { // C1: the datasheet marks these two reads VPA (program), the corpus // VDA; the core follows the datasheet. The pointer wraps in PBR. let bank = (self.pbr as u32) << 16; let low = self.cycle_read(bank | (base + self.x as u32) & 0xFFFF, PROGRAM) as u16; (self.cycle_read(bank | (base + self.x as u32 + 1) & 0xFFFF, PROGRAM) as u16) << 8 | low } /// JMP (a,x) -- the pointer is in the program bank (DS Table 5-5 p. 32). pub(crate) fn op_jmp_idx(&mut self) { let base = self.fetch_word() as u32; self.io_operand(); self.pc = self.pointer_in_program_bank(base); } // -- calls and returns (rows 1c, 2b, 4c, 22g-i) ----------------------- /// JSR a -- push PC - 1, then AA -> PC (DS Table 5-5 p. 32). pub(crate) fn op_jsr(&mut self) { let address = self.fetch_word(); self.io_operand(); let pc = self.pc.wrapping_sub(1); // the address of the operand's last byte self.push((pc >> 8) as u8); self.push(pc as u8); self.pc = address; } /// JSR (a,x) -- push, then jump through a pointer in the program bank /// (DS Table 5-5 p. 32). pub(crate) fn op_jsr_idx(&mut self) { let low = self.fetch() as u32; let pc = self.pc; // the operand's high byte, its last: the address JSR pushes self.push_new((pc >> 8) as u8); self.push_new(pc as u8); let base = (self.fetch() as u32) << 8 | low; self.io_operand(); self.fix_stack(); self.pc = self.pointer_in_program_bank(base); } /// JSL al -- push PBR and PC - 1, then AAB,AA -> PBR,PC (DS Table 5-5 p. 32). pub(crate) fn op_jsl(&mut self) { let address = self.fetch_word(); let s = self.s; self.cycle_write(s as u32, self.pbr, DATA); self.io(s as u32, IO); self.s = s.wrapping_sub(1); let bank = self.fetch(); let pc = self.pc.wrapping_sub(1); self.push_new((pc >> 8) as u8); self.push_new(pc as u8); self.fix_stack(); self.pbr = bank; self.pc = address; } /// RTS -- pull PC, then PC + 1 -> PC (DS Table 5-5 p. 33). pub(crate) fn op_rts(&mut self) { self.io_pc(); self.io_pc(); let low = self.pull() as u16; let pc = (self.pull() as u16) << 8 | low; self.io(self.s as u32, IO); self.pc = pc.wrapping_add(1); } /// RTL -- pull PC and PBR, then PC + 1 -> PC (DS Table 5-5 p. 33). pub(crate) fn op_rtl(&mut self) { self.io_pc(); self.io_pc(); let low = self.pull_new() as u16; let high = self.pull_new() as u16; self.pbr = self.pull_new(); self.fix_stack(); self.pc = (high << 8 | low).wrapping_add(1); } /// RTI -- pull P, PC and (native mode) PBR (DS Table 5-5 p. 33). pub(crate) fn op_rti(&mut self) { self.io_pc(); self.io_pc(); let p = self.pull(); self.set_p(p); let low = self.pull() as u16; self.pc = (self.pull() as u16) << 8 | low; if self.e == 0 { self.pbr = self.pull(); } } // -- the stack (rows 22b-f) ------------------------------------------- fn push_value(&mut self, value: u16, wide: bool, new: bool) { if wide { if new { self.push_new((value >> 8) as u8); } else { self.push((value >> 8) as u8); } } if new { self.push_new(value as u8); } else { self.push(value as u8); } } fn pull_value(&mut self, wide: bool, new: bool) -> u16 { let pull = |cpu: &mut Self| if new { cpu.pull_new() } else { cpu.pull() }; let low = pull(self) as u16; if wide { (pull(self) as u16) << 8 | low } else { low } } /// PHA -- push A (DS Table 5-5 p. 33). pub(crate) fn op_pha(&mut self) { self.io_pc(); self.push_value(self.a, self.p & M == 0, false); } /// PHX -- push X (DS Table 5-5 p. 33). pub(crate) fn op_phx(&mut self) { self.io_pc(); self.push_value(self.x, self.p & X == 0, false); } /// PHY -- push Y (DS Table 5-5 p. 33). pub(crate) fn op_phy(&mut self) { self.io_pc(); self.push_value(self.y, self.p & X == 0, false); } /// PHP -- push P (DS Table 5-5 p. 33). pub(crate) fn op_php(&mut self) { self.io_pc(); self.push(self.p); } /// PHB -- push DBR (DS Table 5-5 p. 33). pub(crate) fn op_phb(&mut self) { self.io_pc(); self.push(self.dbr); } /// PHK -- push PBR (DS Table 5-5 p. 33). pub(crate) fn op_phk(&mut self) { self.io_pc(); self.push(self.pbr); } /// PHD -- push D, 16 bits (DS Table 5-5 p. 33). pub(crate) fn op_phd(&mut self) { self.io_pc(); self.push_value(self.d, true, true); self.fix_stack(); } /// PLA -- pull A (DS Table 5-5 p. 33). pub(crate) fn op_pla(&mut self) { self.io_pc(); self.io_pc(); let value = self.pull_value(self.p & M == 0, false); self.set_accumulator(value as u32); } /// PLX -- pull X (DS Table 5-5 p. 33). pub(crate) fn op_plx(&mut self) { self.io_pc(); self.io_pc(); let value = self.pull_value(self.p & X == 0, false); self.set_index(Index::X, value as u32); } /// PLY -- pull Y (DS Table 5-5 p. 33). pub(crate) fn op_ply(&mut self) { self.io_pc(); self.io_pc(); let value = self.pull_value(self.p & X == 0, false); self.set_index(Index::Y, value as u32); } /// PLP -- pull P (DS Table 5-5 p. 33). pub(crate) fn op_plp(&mut self) { self.io_pc(); self.io_pc(); self.frame.polled_i = Some(self.p & I); // the poll has already happened, on the old I let p = self.pull(); self.set_p(p); } /// PLB -- pull DBR, stepping S in 16 bits in emulation mode (C5) /// (DS Table 5-5 p. 33). pub(crate) fn op_plb(&mut self) { self.io_pc(); self.io_pc(); let dbr = self.pull_new(); self.fix_stack(); self.dbr = dbr; self.p = (self.p & !(N | Z)) | nz8(dbr as u32); } /// PLD -- pull D, 16 bits (DS Table 5-5 p. 33). pub(crate) fn op_pld(&mut self) { self.io_pc(); self.io_pc(); let d = self.pull_value(true, true); self.fix_stack(); self.d = d; self.p = (self.p & !(N | Z)) | nz16(d as u32); } /// PEA -- push a 16-bit immediate (DS Table 5-5 p. 33). pub(crate) fn op_pea(&mut self) { let value = self.fetch_word(); self.push_value(value, true, true); self.fix_stack(); } /// PEI -- push the 16-bit word at D + d, unwrapped (U1) (DS Table 5-5 p. 33). pub(crate) fn op_pei(&mut self) { let base = self.d as u32 + self.direct_offset(); let low = self.cycle_read(base & 0xFFFF, DATA) as u16; let value = (self.cycle_read((base + 1) & 0xFFFF, DATA) as u16) << 8 | low; self.push_value(value, true, true); self.fix_stack(); } /// PER -- push PC + a 16-bit offset (DS Table 5-5 p. 33). pub(crate) fn op_per(&mut self) { let offset = self.fetch_word(); self.io_operand(); self.push_value(self.pc.wrapping_add(offset), true, true); self.fix_stack(); } // -- flags (rows 18, 19a) ------------------------------------------------ /// P as an instruction loads it: emulation mode keeps M and X set; X = 1 clears XH, YH. pub(crate) fn set_p(&mut self, p: u8) { let mut p = p; if self.e != 0 { p |= M | X; } if p & X != 0 { self.x &= 0xFF; self.y &= 0xFF; } self.p = p; } /// CLC -- 0 -> C (DS Table 5-5 p. 32). pub(crate) fn op_clc(&mut self) { self.io_pc(); self.p &= !C; } /// SEC -- 1 -> C (DS Table 5-5 p. 33). pub(crate) fn op_sec(&mut self) { self.io_pc(); self.p |= C; } /// CLD -- 0 -> D (DS Table 5-5 p. 32). pub(crate) fn op_cld(&mut self) { self.io_pc(); self.p &= !D; } /// SED -- 1 -> D (DS Table 5-5 p. 33). pub(crate) fn op_sed(&mut self) { self.io_pc(); self.p |= D; } /// CLV -- 0 -> V (DS Table 5-5 p. 32). pub(crate) fn op_clv(&mut self) { self.io_pc(); self.p &= !V; } /// CLI -- 0 -> I, after this instruction's interrupt poll (DS Table 5-5 p. 32). pub(crate) fn op_cli(&mut self) { self.io_pc(); self.frame.polled_i = Some(self.p & I); self.p &= !I; } /// SEI -- 1 -> I, after this instruction's interrupt poll (DS Table 5-5 p. 33). pub(crate) fn op_sei(&mut self) { self.io_pc(); self.frame.polled_i = Some(self.p & I); self.p |= I; } /// REP -- clear the P bits set in the operand (DS Table 5-5 p. 33). pub(crate) fn op_rep(&mut self) { let mask = self.fetch(); self.io_operand(); // note 8: always three cycles self.frame.polled_i = Some(self.p & I); self.set_p(self.p & !mask); } /// SEP -- set the P bits set in the operand (DS Table 5-5 p. 33). pub(crate) fn op_sep(&mut self) { let mask = self.fetch(); self.io_operand(); self.frame.polled_i = Some(self.p & I); self.set_p(self.p | mask); } // -- no-ops, WAI, STP ---------------------------------------------------- /// NOP -- no operation (DS Table 5-5 p. 33). pub(crate) fn op_nop(&mut self) { self.io_pc(); } /// WDM -- reserved two-byte no-op; its byte is fetched (C2, bsnes) (DS Table 5-5 p. 34). pub(crate) fn op_wdm(&mut self) { self.fetch(); } /// WAI -- wait for an interrupt request (DS Table 5-5 p. 34). pub(crate) fn op_wai(&mut self) { self.io_pc(); self.io_pc(); self.waiting = true; self.frame.halted = true; } /// STP -- stop the clock until reset (DS Table 5-5 p. 33). pub(crate) fn op_stp(&mut self) { self.io_pc(); self.io_pc(); self.stopped = true; self.frame.halted = true; } /// A step while WAI waits or STP has stopped: one IO cycle at PBR,PC. /// /// An NMI, IRQ (even with I = 1) or ABORT ends WAI; the step that sees it /// is one more cycle, a program read at PBR,PC whose byte is dropped /// (bsnes was read to make it an IO cycle, HYPOTHESIS; a 5A22 console /// times it by the address, U11), and the next boundary takes the /// interrupt if I allows, else runs the instruction after WAI. pub(crate) fn halted_step(&mut self) -> u32 { self.cycles = 0; if self.waiting && self.frame.requests & (NMI_LATCH | IRQ_LINE | ABORT_LATCH) != 0 { self.waiting = false; self.frame.halted = false; self.cycle_read((self.pbr as u32) << 16 | self.pc as u32, PROGRAM); return self.cycles; } self.io_pc(); self.cycles } // -- BRK, COP, interrupts, reset (rows 22a, 22j) ------------------------- fn interrupt(&mut self, vector: u32, pushed_p: u8) { if self.e == 0 { self.push(self.pbr); // note 7: native mode only } let pc = self.pc; self.push((pc >> 8) as u8); self.push(pc as u8); self.push(pushed_p); self.p = (self.p | I) & !D; self.pbr = 0; let low = self.cycle_read(vector, VECTOR) as u16; self.pc = (self.cycle_read(vector + 1, VECTOR) as u16) << 8 | low; } /// BRK -- push PC + 2 and P, jump through the BRK vector (DS Table 5-5 p. 32). pub(crate) fn op_brk(&mut self) { self.fetch(); // the signature byte self.interrupt(VECTOR_BRK[self.e as usize], self.p); } /// COP -- push PC + 2 and P, jump through the COP vector (DS Table 5-5 p. 32). pub(crate) fn op_cop(&mut self) { self.fetch(); self.interrupt(VECTOR_COP[self.e as usize], self.p); } /// The ABORT half of `_boundary_interrupt`'s override: ABORT is a further /// request bit, taken before NMI and IRQ, but not at a boundary whose last /// step did not poll (reset, an interrupt entry). `None`: ask the frame. pub(crate) fn boundary_abort(&self) -> Option { let polled = self.frame.polled; if self.frame.requests & ABORT_LATCH != 0 && (polled.is_none() || polled.unwrap() & NO_POLL == 0) { return Some(ABORT_LATCH); } None } /// Take NMI, IRQ or ABORT at a boundary: row 22a, B clear in emulation mode. pub(crate) fn enter_interrupt_sequence(&mut self, kind: u8) -> u32 { self.cycles = 0; self.frame.polled_i = None; let address = (self.pbr as u32) << 16 | self.pc as u32; self.cycle_read(address, OPCODE); // fetched and discarded self.io(address, IO); self.frame.requests &= !(kind & (NMI_LATCH | ABORT_LATCH)); let vector = if self.e != 0 { vector_emulation(kind) } else { vector_native(kind) }; let pushed = if self.e != 0 { self.p & !X } else { self.p }; self.interrupt(vector, pushed); // The entry's last cycle is not a poll: nothing is polled, and the // handler's first instruction runs before anything else is taken // (HYPOTHESIS: read from bsnes's GPL source before the 2026-11-14 rule; // for review, 2026-11-24). self.frame.polled = Some(NO_POLL); self.frame.deferred = None; self.cycles } /// Latch an ABORT request: taken at the next boundary, before NMI and IRQ. pub fn request_abort(&mut self) { request_abort(&mut self.frame); } /// The reset sequence: row 22a with the stack cycles as reads (note 10, U12). pub(crate) fn reset_sequence(&mut self) -> u32 { self.cycles = 0; self.frame.reset_pending = false; self.frame.halted = false; self.waiting = false; self.stopped = false; self.frame.polled_i = None; self.e = 1; self.p = (self.p | M | X | I) & !D; self.x &= 0xFF; self.y &= 0xFF; self.d = 0; self.dbr = 0; self.pbr = 0; self.s = 0x0100 | (self.s & 0xFF); let address = self.pc as u32; self.cycle_read(address, OPCODE); self.io(address, IO); for _ in 0..3 { self.cycle_read(self.s as u32, DATA); self.s = 0x0100 | (self.s.wrapping_sub(1) & 0xFF); } let low = self.cycle_read(VECTOR_RESET, VECTOR) as u16; self.pc = (self.cycle_read(VECTOR_RESET + 1, VECTOR) as u16) << 8 | low; self.frame.requests &= !ABORT_LATCH; self.frame.polled = Some(NO_POLL); self.frame.deferred = None; self.cycles } }