// SPDX-License-Identifier: MIT //! Control flow, the implied flag instructions, and the two that never finish //! (`_control.py`). //! //! Taken branches add two internal cycles (the reference's docs/start-here.md //! "Control flow"). Where anomie's cycle doc and SingleStepTests order a call's //! or a return's cycles differently (C6) or a test-and-branch's (C7), the corpus //! is followed: it has the most specific claim, and ares (ISC) orders them the //! same way. The docs say which cycles are only as good as an emulator. //! //! Differences forced by Rust: none beyond the integer types. use crate::core::{pack, Bus, Spc700}; use crate::flags::{FLAG_B, FLAG_C, FLAG_H, FLAG_I, FLAG_N, FLAG_P, FLAG_V, FLAG_Z}; /// The flag each pair of conditional branches tests, by `op >> 6`. pub const BRANCH_FLAG: [u8; 4] = [FLAG_N, FLAG_V, FLAG_C, FLAG_Z]; pub const BRK_VECTOR: u16 = 0xFFDE; /// TCALL n reads the word at $FFDE - 2n: TCALL 0 shares BRK's vector. pub const TCALL_BASE: u16 = 0xFFDE; pub const UPPER_PAGE: u16 = 0xFF00; impl Spc700 { /// A branch taken: two internal cycles, then PC moves by the signed offset. fn taken(&mut self, offset: u8) { self.idle(); self.idle(); self.pc = self.pc.wrapping_add(offset as i8 as i16 as u16); } // ----- branches ------------------------------------------------------------ /// BPL/BMI/BVC/BVS/BCC/BCS/BNE/BEQ r -- 2 cycles, 4 taken (timing.md branch). pub(crate) fn op_branch(&mut self, op: u8) { let offset = self.fetch(); if (self.psw & BRANCH_FLAG[(op >> 6) as usize] != 0) == (op & 0x20 != 0) { self.taken(offset); } } /// BRA r -- always taken (timing.md branch). pub(crate) fn op_bra(&mut self, _op: u8) { let offset = self.fetch(); self.taken(offset); } /// BBS/BBC d.b,r -- test a direct-page bit, then branch (timing.md BBS/BBC). pub(crate) fn op_bbs_bbc(&mut self, op: u8) { let offset = self.fetch() as u32; let value = self.load(offset); self.idle(); let offset = self.fetch(); if (value >> (op >> 5) & 1 != 0) != (op & 0x10 != 0) { self.taken(offset); } } /// CBNE d,r -- branch if A differs from a direct-page byte; no flags (timing.md CBNE). pub(crate) fn op_cbne_d(&mut self, _op: u8) { let offset = self.fetch() as u32; let value = self.load(offset); self.idle(); let offset = self.fetch(); if self.a != value { self.taken(offset); } } /// CBNE d+X,r -- branch if A differs from an indexed byte (timing.md CBNE d+X, C7). pub(crate) fn op_cbne_dx(&mut self, _op: u8) { let offset = self.direct_x(); let value = self.load(offset); self.idle(); // C7: SST; anomie 31c calls its own order here "guesswork" let offset = self.fetch(); if self.a != value { self.taken(offset); } } /// DBNZ d,r -- an RMW: the write happens whether or not it branches (timing.md DBNZ d). pub(crate) fn op_dbnz_d(&mut self, _op: u8) { let target = self.fetch() as u32; let value = self.load(target).wrapping_sub(1); self.store(target, value); let offset = self.fetch(); if value != 0 { self.taken(offset); } } /// DBNZ Y,r -- decrement Y, branch if not zero; no flags (timing.md DBNZ Y, C7). pub(crate) fn op_dbnz_y(&mut self, _op: u8) { self.read_dummy(self.pc); // C7: SST; anomie 31e reads the offset here instead self.idle(); let offset = self.fetch(); self.y = self.y.wrapping_sub(1); if self.y != 0 { self.taken(offset); } } // ----- jumps, calls, returns --------------------------------------------------- /// JMP !a -- absolute jump (timing.md JMP !a). pub(crate) fn op_jmp_abs(&mut self, _op: u8) { self.pc = self.fetch_word(); } /// JMP [!a+X] -- through the word at a+X (timing.md JMP [!a+X]). pub(crate) fn op_jmp_absx(&mut self, _op: u8) { let address = self.absolute_indexed(self.x); let low = self.read(address); self.pc = pack(self.read(address.wrapping_add(1)), low); } /// CALL !a -- pushes the next instruction's address (U14, timing.md CALL, C6). pub(crate) fn op_call(&mut self, _op: u8) { let target = self.fetch_word(); self.idle(); self.push_pc(); self.idle(); self.idle(); self.pc = target; } /// PCALL u -- call $FF00 + u (U14, timing.md PCALL, C6). pub(crate) fn op_pcall(&mut self, _op: u8) { let target = UPPER_PAGE | self.fetch() as u16; self.idle(); self.push_pc(); self.idle(); self.pc = target; } /// TCALL n -- call through the word at $FFDE - 2n (U14, timing.md TCALL, C6). pub(crate) fn op_tcall(&mut self, op: u8) { self.read_dummy(self.pc); self.idle(); self.push_pc(); self.idle(); let vector = TCALL_BASE - 2 * (op >> 4) as u16; let low = self.read(vector); self.pc = pack(self.read(vector + 1), low); } /// BRK -- push PC and PSW, set B, clear I, jump through $FFDE (U7, timing.md BRK, C6). pub(crate) fn op_brk(&mut self, _op: u8) { self.read_dummy(self.pc); // C6: SST and ares make this a dummy read of PC; anomie has IO self.push_pc(); self.push(self.psw); self.idle(); let low = self.read(BRK_VECTOR); self.pc = pack(self.read(BRK_VECTOR + 1), low); self.psw = self.psw & !FLAG_I | FLAG_B; // U7: after the push, so the pushed B is the old one } /// RET -- pull PC, low byte first (U14, timing.md RET, C6). pub(crate) fn op_ret(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.pull_pc(); } /// RET1 -- pull PSW, all eight bits, then PC (U7, timing.md RET1, C6). pub(crate) fn op_ret1(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.psw = self.pull(); self.pull_pc(); } // ----- implied flag instructions and NOP ---------------------------------------- /// NOP -- the dummy read of PC is the instruction's only other cycle (C2). pub(crate) fn op_nop(&mut self, _op: u8) { self.read_dummy(self.pc); } /// CLRP -- direct page $00xx (timing.md implied, register). pub(crate) fn op_clrp(&mut self, _op: u8) { self.read_dummy(self.pc); self.psw &= !FLAG_P; } /// SETP -- direct page $01xx (timing.md implied, register). pub(crate) fn op_setp(&mut self, _op: u8) { self.read_dummy(self.pc); self.psw |= FLAG_P; } /// CLRC -- clear C (timing.md implied, register). pub(crate) fn op_clrc(&mut self, _op: u8) { self.read_dummy(self.pc); self.psw &= !FLAG_C; } /// SETC -- set C (timing.md implied, register). pub(crate) fn op_setc(&mut self, _op: u8) { self.read_dummy(self.pc); self.psw |= FLAG_C; } /// NOTC -- complement C (timing.md implied, 3 cycles). pub(crate) fn op_notc(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.psw ^= FLAG_C; } /// CLRV -- clear V and H (timing.md implied, register). pub(crate) fn op_clrv(&mut self, _op: u8) { self.read_dummy(self.pc); self.psw &= !(FLAG_V | FLAG_H); } /// EI -- set I, which does nothing else here (U11, timing.md implied, 3 cycles). pub(crate) fn op_ei(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.psw |= FLAG_I; } /// DI -- clear I, which does nothing else here (U11, timing.md implied, 3 cycles). pub(crate) fn op_di(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.psw &= !FLAG_I; } // ----- the two that never finish ------------------------------------------------- /// SLEEP -- wait for an interrupt that never comes: stopped until reset (U11, C5). pub(crate) fn op_sleep(&mut self, _op: u8) { self.stopped = true; } /// STOP -- stop the clock: stopped until reset (U11, C5). pub(crate) fn op_stop(&mut self, _op: u8) { self.stopped = true; } }