# SPDX-License-Identifier: MIT """Branches, jumps, calls, returns, the stack, flags, BRK and COP, interrupts, reset, WAI, STP. 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). """ from sixfiveohtwo.frame import IRQ_LINE, NMI_LATCH, NO_POLL from w65c816._core import DATA, OPCODE, PROGRAM, VECTOR, C, D, I, M, N, V, X, Z, nz8, nz16 ABORT_LATCH = 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. VECTORS_EMULATION = {NMI_LATCH: 0xFFFA, IRQ_LINE: 0xFFFE, ABORT_LATCH: 0xFFF8} VECTORS_NATIVE = {NMI_LATCH: 0xFFEA, IRQ_LINE: 0xFFEE, ABORT_LATCH: 0xFFE8} VECTOR_RESET = 0xFFFC VECTOR_BRK = (0xFFE6, 0xFFFE) # native, emulation VECTOR_COP = (0xFFE4, 0xFFF4) class ControlMixin: """Private control-flow, stack, flag, interrupt and reset implementation.""" # -- branches (rows 20, 21) ------------------------------------------ def _branch(self, taken: int) -> None: offset = self._fetch() if not taken: return self._io_operand() # note 5 pc = self.pc target = (pc + offset - ((offset & 0x80) << 1)) & 0xFFFF if self.e and (target ^ pc) & 0xFF00: self._io_operand() # note 6: emulation mode only self.pc = target def _op_bpl(self) -> None: """BPL -- branch if N = 0 (DS Table 5-5 p. 32).""" self._branch(not self.p & N) def _op_bmi(self) -> None: """BMI -- branch if N = 1 (DS Table 5-5 p. 32).""" self._branch(self.p & N) def _op_bvc(self) -> None: """BVC -- branch if V = 0 (DS Table 5-5 p. 32).""" self._branch(not self.p & V) def _op_bvs(self) -> None: """BVS -- branch if V = 1 (DS Table 5-5 p. 32).""" self._branch(self.p & V) def _op_bcc(self) -> None: """BCC -- branch if C = 0 (DS Table 5-5 p. 32).""" self._branch(not self.p & C) def _op_bcs(self) -> None: """BCS -- branch if C = 1 (DS Table 5-5 p. 32).""" self._branch(self.p & C) def _op_bne(self) -> None: """BNE -- branch if Z = 0 (DS Table 5-5 p. 32).""" self._branch(not self.p & Z) def _op_beq(self) -> None: """BEQ -- branch if Z = 1 (DS Table 5-5 p. 32).""" self._branch(self.p & Z) def _op_bra(self) -> None: """BRA -- branch always (DS Table 5-5 p. 32).""" self._branch(True) def _op_brl(self) -> None: """BRL -- branch always, 16-bit offset, within the bank (DS Table 5-5 p. 32).""" offset = self._fetch_word() self._io_operand() self.pc = (self.pc + offset) & 0xFFFF # -- jumps (rows 1b, 2a, 3a, 3b, 4b) -------------------------------------- def _op_jmp(self) -> None: """JMP a -- AA -> PC (DS Table 5-5 p. 32).""" self.pc = self._fetch_word() def _op_jml(self) -> None: """JML al -- AAB,AA -> PBR,PC (DS Table 5-5 p. 32).""" address = self._fetch_word() self.pbr = self._fetch() self.pc = address def _op_jmp_ind(self) -> None: """JMP (a) -- the pointer is in bank 0, no NMOS page bug (DS Table 5-5 p. 32).""" pointer = self._fetch_word() low = self._cycle_read(pointer, DATA) self.pc = self._cycle_read((pointer + 1) & 0xFFFF, DATA) << 8 | low def _op_jml_ind(self) -> None: """JML [a] -- a three-byte pointer in bank 0 -> PBR,PC (DS Table 5-5 p. 32).""" pointer = self._fetch_word() low = self._cycle_read(pointer, DATA) high = self._cycle_read((pointer + 1) & 0xFFFF, DATA) self.pbr = self._cycle_read((pointer + 2) & 0xFFFF, DATA) self.pc = high << 8 | low def _pointer_in_program_bank(self, base: int) -> int: # C1: the datasheet marks these two reads VPA (program), the corpus # VDA; the core follows the datasheet. The pointer wraps in PBR. bank = self.pbr << 16 low = self._cycle_read(bank | (base + self.x) & 0xFFFF, PROGRAM) return self._cycle_read(bank | (base + self.x + 1) & 0xFFFF, PROGRAM) << 8 | low def _op_jmp_idx(self) -> None: """JMP (a,x) -- the pointer is in the program bank (DS Table 5-5 p. 32).""" base = self._fetch_word() self._io_operand() self.pc = self._pointer_in_program_bank(base) # -- calls and returns (rows 1c, 2b, 4c, 22g-i) ----------------------- def _op_jsr(self) -> None: """JSR a -- push PC - 1, then AA -> PC (DS Table 5-5 p. 32).""" address = self._fetch_word() self._io_operand() pc = (self.pc - 1) & 0xFFFF # the address of the operand's last byte self._push(pc >> 8) self._push(pc & 0xFF) self.pc = address def _op_jsr_idx(self) -> None: """JSR (a,x) -- push, then jump through a pointer in the program bank (DS Table 5-5 p. 32).""" low = self._fetch() pc = self.pc # the operand's high byte, its last: the address JSR pushes self._push_new(pc >> 8) self._push_new(pc & 0xFF) base = self._fetch() << 8 | low self._io_operand() self._fix_stack() self.pc = self._pointer_in_program_bank(base) def _op_jsl(self) -> None: """JSL al -- push PBR and PC - 1, then AAB,AA -> PBR,PC (DS Table 5-5 p. 32).""" address = self._fetch_word() s = self.s self._cycle_write(s, self.pbr) self._io(s) self.s = (s - 1) & 0xFFFF bank = self._fetch() pc = (self.pc - 1) & 0xFFFF self._push_new(pc >> 8) self._push_new(pc & 0xFF) self._fix_stack() self.pbr = bank self.pc = address def _op_rts(self) -> None: """RTS -- pull PC, then PC + 1 -> PC (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() low = self._pull() pc = self._pull() << 8 | low self._io(self.s) self.pc = (pc + 1) & 0xFFFF def _op_rtl(self) -> None: """RTL -- pull PC and PBR, then PC + 1 -> PC (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() low = self._pull_new() high = self._pull_new() self.pbr = self._pull_new() self._fix_stack() self.pc = (high << 8 | low) + 1 & 0xFFFF def _op_rti(self) -> None: """RTI -- pull P, PC and (native mode) PBR (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self._set_p(self._pull()) low = self._pull() self.pc = self._pull() << 8 | low if not self.e: self.pbr = self._pull() # -- the stack (rows 22b-f) ------------------------------------------- def _push_value(self, value: int, wide: bool, new: bool = False) -> None: push = self._push_new if new else self._push if wide: push(value >> 8) push(value & 0xFF) def _pull_value(self, wide: bool, new: bool = False) -> int: pull = self._pull_new if new else self._pull low = pull() return pull() << 8 | low if wide else low def _op_pha(self) -> None: """PHA -- push A (DS Table 5-5 p. 33).""" self._io_pc() self._push_value(self.a, not self.p & M) def _op_phx(self) -> None: """PHX -- push X (DS Table 5-5 p. 33).""" self._io_pc() self._push_value(self.x, not self.p & X) def _op_phy(self) -> None: """PHY -- push Y (DS Table 5-5 p. 33).""" self._io_pc() self._push_value(self.y, not self.p & X) def _op_php(self) -> None: """PHP -- push P (DS Table 5-5 p. 33).""" self._io_pc() self._push(self.p) def _op_phb(self) -> None: """PHB -- push DBR (DS Table 5-5 p. 33).""" self._io_pc() self._push(self.dbr) def _op_phk(self) -> None: """PHK -- push PBR (DS Table 5-5 p. 33).""" self._io_pc() self._push(self.pbr) def _op_phd(self) -> None: """PHD -- push D, 16 bits (DS Table 5-5 p. 33).""" self._io_pc() self._push_value(self.d, True, new=True) self._fix_stack() def _op_pla(self) -> None: """PLA -- pull A (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self._set_accumulator(self._pull_value(not self.p & M)) def _op_plx(self) -> None: """PLX -- pull X (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self._set_index("x", self._pull_value(not self.p & X)) def _op_ply(self) -> None: """PLY -- pull Y (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self._set_index("y", self._pull_value(not self.p & X)) def _op_plp(self) -> None: """PLP -- pull P (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self._polled_i = self.p & I # the poll has already happened, on the old I self._set_p(self._pull()) def _op_plb(self) -> None: """PLB -- pull DBR, stepping S in 16 bits in emulation mode (C5) (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() dbr = self._pull_new() self._fix_stack() self.dbr = dbr self.p = (self.p & ~(N | Z)) | nz8(dbr) def _op_pld(self) -> None: """PLD -- pull D, 16 bits (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() d = self._pull_value(True, new=True) self._fix_stack() self.d = d self.p = (self.p & ~(N | Z)) | nz16(d) def _op_pea(self) -> None: """PEA -- push a 16-bit immediate (DS Table 5-5 p. 33).""" value = self._fetch_word() self._push_value(value, True, new=True) self._fix_stack() def _op_pei(self) -> None: """PEI -- push the 16-bit word at D + d, unwrapped (U1) (DS Table 5-5 p. 33).""" base = self.d + self._direct_offset() low = self._cycle_read(base & 0xFFFF, DATA) value = self._cycle_read((base + 1) & 0xFFFF, DATA) << 8 | low self._push_value(value, True, new=True) self._fix_stack() def _op_per(self) -> None: """PER -- push PC + a 16-bit offset (DS Table 5-5 p. 33).""" offset = self._fetch_word() self._io_operand() self._push_value((self.pc + offset) & 0xFFFF, True, new=True) self._fix_stack() # -- flags (rows 18, 19a) ------------------------------------------------ def _set_p(self, p: int) -> None: """P as an instruction loads it: emulation mode keeps M and X set; X = 1 clears XH, YH.""" if self.e: p |= M | X if p & X: self.x &= 0xFF self.y &= 0xFF self.p = p def _op_clc(self) -> None: """CLC -- 0 -> C (DS Table 5-5 p. 32).""" self._io_pc() self.p &= ~C def _op_sec(self) -> None: """SEC -- 1 -> C (DS Table 5-5 p. 33).""" self._io_pc() self.p |= C def _op_cld(self) -> None: """CLD -- 0 -> D (DS Table 5-5 p. 32).""" self._io_pc() self.p &= ~D def _op_sed(self) -> None: """SED -- 1 -> D (DS Table 5-5 p. 33).""" self._io_pc() self.p |= D def _op_clv(self) -> None: """CLV -- 0 -> V (DS Table 5-5 p. 32).""" self._io_pc() self.p &= ~V def _op_cli(self) -> None: """CLI -- 0 -> I, after this instruction's interrupt poll (DS Table 5-5 p. 32).""" self._io_pc() self._polled_i = self.p & I self.p &= ~I def _op_sei(self) -> None: """SEI -- 1 -> I, after this instruction's interrupt poll (DS Table 5-5 p. 33).""" self._io_pc() self._polled_i = self.p & I self.p |= I def _op_rep(self) -> None: """REP -- clear the P bits set in the operand (DS Table 5-5 p. 33).""" mask = self._fetch() self._io_operand() # note 8: always three cycles self._polled_i = self.p & I self._set_p(self.p & ~mask) def _op_sep(self) -> None: """SEP -- set the P bits set in the operand (DS Table 5-5 p. 33).""" mask = self._fetch() self._io_operand() self._polled_i = self.p & I self._set_p(self.p | mask) # -- no-ops, WAI, STP ---------------------------------------------------- def _op_nop(self) -> None: """NOP -- no operation (DS Table 5-5 p. 33).""" self._io_pc() def _op_wdm(self) -> None: """WDM -- reserved two-byte no-op; its byte is fetched (C2, bsnes) (DS Table 5-5 p. 34).""" self._fetch() def _op_wai(self) -> None: """WAI -- wait for an interrupt request (DS Table 5-5 p. 34).""" self._io_pc() self._io_pc() self.waiting = True self.halted = True def _op_stp(self) -> None: """STP -- stop the clock until reset (DS Table 5-5 p. 33).""" self._io_pc() self._io_pc() self.stopped = True self.halted = True def _halted(self) -> int: """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. """ self._cycles = 0 if self.waiting and self._requests & (NMI_LATCH | IRQ_LINE | ABORT_LATCH): self.waiting = False self.halted = False self._cycle_read(self.pbr << 16 | self.pc, PROGRAM) return self._cycles self._io_pc() return self._cycles # -- BRK, COP, interrupts, reset (rows 22a, 22j) ------------------------- def _interrupt(self, vector: int, pushed_p: int) -> None: if not self.e: self._push(self.pbr) # note 7: native mode only pc = self.pc self._push(pc >> 8) self._push(pc & 0xFF) self._push(pushed_p) self.p = (self.p | I) & ~D self.pbr = 0 low = self._cycle_read(vector, VECTOR) self.pc = self._cycle_read(vector + 1, VECTOR) << 8 | low def _op_brk(self) -> None: """BRK -- push PC + 2 and P, jump through the BRK vector (DS Table 5-5 p. 32).""" self._fetch() # the signature byte self._interrupt(VECTOR_BRK[self.e], self.p) def _op_cop(self) -> None: """COP -- push PC + 2 and P, jump through the COP vector (DS Table 5-5 p. 32).""" self._fetch() self._interrupt(VECTOR_COP[self.e], self.p) def _boundary_interrupt(self) -> int: # 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). polled = self._polled if self._requests & ABORT_LATCH and (polled is None or not polled & NO_POLL): return ABORT_LATCH return super()._boundary_interrupt() def _enter_interrupt(self, kind: int) -> int: """Take NMI, IRQ or ABORT at a boundary: row 22a, B clear in emulation mode.""" self._cycles = 0 self._polled_i = None address = self.pbr << 16 | self.pc self._cycle_read(address, OPCODE) # fetched and discarded self._io(address) self._requests &= ~(kind & (NMI_LATCH | ABORT_LATCH)) vectors = VECTORS_EMULATION if self.e else VECTORS_NATIVE self._interrupt(vectors[kind], self.p & ~X if self.e else self.p) # 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._polled = NO_POLL self._deferred = None return self._cycles def request_abort(self) -> None: """Latch an ABORT request: taken at the next boundary, before NMI and IRQ.""" self._request(self._requests | ABORT_LATCH) def _accept_reset(self) -> int: """The reset sequence: row 22a with the stack cycles as reads (note 10, U12).""" self._cycles = 0 self._reset_pending = False self.halted = self.waiting = self.stopped = False self._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) address = self.pc self._cycle_read(address, OPCODE) self._io(address) for _ in range(3): self._cycle_read(self.s, DATA) self.s = 0x0100 | ((self.s - 1) & 0xFF) low = self._cycle_read(VECTOR_RESET, VECTOR) self.pc = self._cycle_read(VECTOR_RESET + 1, VECTOR) << 8 | low self._requests &= ~ABORT_LATCH self._polled = NO_POLL self._deferred = None return self._cycles