# SPDX-License-Identifier: MIT """Control flow, the implied flag instructions, and the two that never finish. Taken branches add two internal cycles (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. """ from __future__ import annotations from spc700_python._core import CoreMixin from spc700_python._flags import ( 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``. BRANCH_FLAG = (FLAG_N, FLAG_V, FLAG_C, FLAG_Z) BRK_VECTOR = 0xFFDE #: TCALL n reads the word at $FFDE - 2n: TCALL 0 shares BRK's vector. TCALL_BASE = 0xFFDE UPPER_PAGE = 0xFF00 class ControlMixin(CoreMixin): def _taken(self, offset: int) -> None: """A branch taken: two internal cycles, then PC moves by the signed offset.""" self._idle() self._idle() signed = offset - 0x100 if offset & 0x80 else offset self.pc = (self.pc + signed) & 0xFFFF # ----- branches ------------------------------------------------------------ def _op_branch(self, op: int) -> None: """BPL/BMI/BVC/BVS/BCC/BCS/BNE/BEQ r -- 2 cycles, 4 taken (timing.md branch).""" offset = self._fetch() if bool(self.psw & BRANCH_FLAG[op >> 6]) == bool(op & 0x20): self._taken(offset) def _op_bra(self, op: int) -> None: """BRA r -- always taken (timing.md branch).""" self._taken(self._fetch()) def _op_bbs_bbc(self, op: int) -> None: """BBS/BBC d.b,r -- test a direct-page bit, then branch (timing.md BBS/BBC).""" value = self._load(self._fetch()) self._idle() offset = self._fetch() if bool(value >> (op >> 5) & 1) != bool(op & 0x10): self._taken(offset) def _op_cbne_d(self, op: int) -> None: """CBNE d,r -- branch if A differs from a direct-page byte; no flags (timing.md CBNE).""" value = self._load(self._fetch()) self._idle() offset = self._fetch() if self.a != value: self._taken(offset) def _op_cbne_dx(self, op: int) -> None: """CBNE d+X,r -- branch if A differs from an indexed byte (timing.md CBNE d+X, C7).""" value = self._load(self._direct_x()) self._idle() # C7: SST; anomie 31c calls its own order here "guesswork" offset = self._fetch() if self.a != value: self._taken(offset) def _op_dbnz_d(self, op: int) -> None: """DBNZ d,r -- an RMW: the write happens whether or not it branches (timing.md DBNZ d).""" target = self._fetch() value = (self._load(target) - 1) & 0xFF self._store(target, value) offset = self._fetch() if value: self._taken(offset) def _op_dbnz_y(self, op: int) -> None: """DBNZ Y,r -- decrement Y, branch if not zero; no flags (timing.md DBNZ Y, C7).""" self._read_dummy(self.pc) # C7: SST; anomie 31e reads the offset here instead self._idle() offset = self._fetch() self.y = (self.y - 1) & 0xFF if self.y: self._taken(offset) # ----- jumps, calls, returns --------------------------------------------------- def _op_jmp_abs(self, op: int) -> None: """JMP !a -- absolute jump (timing.md JMP !a).""" self.pc = self._fetch_word() def _op_jmp_absx(self, op: int) -> None: """JMP [!a+X] -- through the word at a+X (timing.md JMP [!a+X]).""" address = self._absolute_indexed(self.x) low = self._read(address) self.pc = self._read((address + 1) & 0xFFFF) << 8 | low def _op_call(self, op: int) -> None: """CALL !a -- pushes the next instruction's address (U14, timing.md CALL, C6).""" target = self._fetch_word() self._idle() self._push_pc() self._idle() self._idle() self.pc = target def _op_pcall(self, op: int) -> None: """PCALL u -- call $FF00 + u (U14, timing.md PCALL, C6).""" target = UPPER_PAGE | self._fetch() self._idle() self._push_pc() self._idle() self.pc = target def _op_tcall(self, op: int) -> None: """TCALL n -- call through the word at $FFDE - 2n (U14, timing.md TCALL, C6).""" self._read_dummy(self.pc) self._idle() self._push_pc() self._idle() vector = TCALL_BASE - 2 * (op >> 4) low = self._read(vector) self.pc = self._read(vector + 1) << 8 | low def _op_brk(self, op: int) -> None: """BRK -- push PC and PSW, set B, clear I, jump through $FFDE (U7, timing.md BRK, C6).""" 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() low = self._read(BRK_VECTOR) self.pc = self._read(BRK_VECTOR + 1) << 8 | low self.psw = self.psw & ~FLAG_I | FLAG_B # U7: after the push, so the pushed B is the old one def _op_ret(self, op: int) -> None: """RET -- pull PC, low byte first (U14, timing.md RET, C6).""" self._read_dummy(self.pc) self._idle() self._pull_pc() def _op_ret1(self, op: int) -> None: """RET1 -- pull PSW, all eight bits, then PC (U7, timing.md RET1, C6).""" self._read_dummy(self.pc) self._idle() self.psw = self._pull() self._pull_pc() # ----- implied flag instructions and NOP ---------------------------------------- def _op_nop(self, op: int) -> None: """NOP -- the dummy read of PC is the instruction's only other cycle (C2).""" self._read_dummy(self.pc) def _op_clrp(self, op: int) -> None: """CLRP -- direct page $00xx (timing.md implied, register).""" self._read_dummy(self.pc) self.psw &= ~FLAG_P def _op_setp(self, op: int) -> None: """SETP -- direct page $01xx (timing.md implied, register).""" self._read_dummy(self.pc) self.psw |= FLAG_P def _op_clrc(self, op: int) -> None: """CLRC -- clear C (timing.md implied, register).""" self._read_dummy(self.pc) self.psw &= ~FLAG_C def _op_setc(self, op: int) -> None: """SETC -- set C (timing.md implied, register).""" self._read_dummy(self.pc) self.psw |= FLAG_C def _op_notc(self, op: int) -> None: """NOTC -- complement C (timing.md implied, 3 cycles).""" self._read_dummy(self.pc) self._idle() self.psw ^= FLAG_C def _op_clrv(self, op: int) -> None: """CLRV -- clear V and H (timing.md implied, register).""" self._read_dummy(self.pc) self.psw &= ~(FLAG_V | FLAG_H) def _op_ei(self, op: int) -> None: """EI -- set I, which does nothing else here (U11, timing.md implied, 3 cycles).""" self._read_dummy(self.pc) self._idle() self.psw |= FLAG_I def _op_di(self, op: int) -> None: """DI -- clear I, which does nothing else here (U11, timing.md implied, 3 cycles).""" self._read_dummy(self.pc) self._idle() self.psw &= ~FLAG_I # ----- the two that never finish ------------------------------------------------- def _op_sleep(self, op: int) -> None: """SLEEP -- wait for an interrupt that never comes: stopped until reset (U11, C5).""" self.stopped = True def _op_stop(self, op: int) -> None: """STOP -- stop the clock: stopped until reset (U11, C5).""" self.stopped = True