# SPDX-License-Identifier: MIT """Flow of control: branches, jumps, the stack, flags, BRK, interrupts, reset, WAI and STP. Sequences are 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). """ from sixfiveohtwo.frame import IRQ_LINE, NMI_LATCH, NO_POLL from w65c02_python._core import B, C, D, I, N, U, V, Z # 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. VECTOR_NMI = 0xFFFA VECTOR_RESET = 0xFFFC VECTOR_IRQ = 0xFFFE # IRQ and BRK (DS Table 3-1) class ControlMixin: """Branches, jumps, stack, flags, BRK, interrupt entry, reset, WAI, STP.""" #: U1, U2: whether reset's three stack cycles write PCH, PCL and P (the #: G65SC12: CAP; GDS Table V) or only read (the R65C02: CAP). # MEASURED for the two captured parts; the default of a new row (False) is # HYPOTHESIS (U1). reset_writes_stack = False # -- branches ----------------------------------------------------------------- def _branch(self, taken: int) -> None: """A conditional branch: 2 cycles, 3 taken, 4 taken across a page (CAP: `D0` 2/3/4).""" # MEASURED (CAP); DOC (DS Table 6-4 for the conditions). offset = self._fetch() if taken: self._extra += 1 self._take_branch(offset) def _take_branch(self, offset: int) -> None: """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). """ # Cycles and addresses: MEASURED (CAP); SST's differ (C16, OBSERVED). # No early poll: HYPOTHESIS (U5). pc = self.pc self._fetch_discard() target = (pc + offset - ((offset & 0x80) << 1)) & 0xFFFF if (target ^ pc) & 0xFF00: self._fetch_discard() self._extra += 1 self.pc = target def _op_bpl(self) -> None: """BPL -- branch if N = 0 (DS Table 6-4).""" self._branch(not self.p & N) def _op_bmi(self) -> None: """BMI -- branch if N = 1 (DS Table 6-4).""" self._branch(self.p & N) def _op_bvc(self) -> None: """BVC -- branch if V = 0 (DS Table 6-4).""" self._branch(not self.p & V) def _op_bvs(self) -> None: """BVS -- branch if V = 1 (DS Table 6-4).""" self._branch(self.p & V) def _op_bcc(self) -> None: """BCC -- branch if C = 0 (DS Table 6-4).""" self._branch(not self.p & C) def _op_bcs(self) -> None: """BCS -- branch if C = 1 (DS Table 6-4).""" self._branch(self.p & C) def _op_bne(self) -> None: """BNE -- branch if Z = 0 (DS Table 6-4).""" self._branch(not self.p & Z) def _op_beq(self) -> None: """BEQ -- branch if Z = 1 (DS Table 6-4).""" self._branch(self.p & Z) def _op_bra(self) -> None: """BRA -- branch always: 3 cycles, 4 across a page (DS Table 6-4; CAP: `80` 3/4).""" self._take_branch(self._fetch()) # -- jumps and subroutines ---------------------------------------------------- # BRA and JMP abs: DOC (DS Table 6-4) and MEASURED (CAP: `80` 3/4, `4C` 3). def _op_jmp_abs(self) -> None: """JMP abs -- jump (DS Table 6-4).""" self.pc = self._fetch_word() def _op_jmp_ind(self) -> None: """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).""" 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). self._read((self.pc - 1) & 0xFFFF) low = self._read(pointer) self.pc = self._read((pointer + 1) & 0xFFFF) << 8 | low def _op_jmp_abxind(self) -> None: """JMP (abs,X) -- jump through a pointer at abs + X (DS Table 6-4; CAP: `7C` 6).""" base = self._fetch_word() # MEASURED (CAP); SST's dummy address differs (C17, OBSERVED). self._read((self.pc - 1) & 0xFFFF) # CAP: the operand's high byte again (SST: the low, C17) pointer = (base + self.x) & 0xFFFF low = self._read(pointer) self.pc = self._read((pointer + 1) & 0xFFFF) << 8 | low def _op_jsr(self) -> None: """JSR -- push the address of its last byte and jump (DS Table 6-4; CAP: 6).""" # MEASURED (CAP: cycles, the stack read, the bytes pushed). low = self._fetch() self._read(0x0100 | self.s) # a dropped read of the stack, as on the NMOS part (CAP) pc = self.pc # the address of the instruction's third byte self._push(pc >> 8) self._push(pc & 0xFF) self.pc = self._fetch() << 8 | low def _op_rts(self) -> None: """RTS -- pull the return address and continue after it (DS Table 6-4; CAP: 6).""" # MEASURED (CAP). self._fetch_discard() self._read(0x0100 | self.s) # the stack before the increment (CAP) low = self._pull() self.pc = self._pull() << 8 | low self._fetch() # the byte JSR's address pointed at, dropped; PC steps past it def _op_rti(self) -> None: """RTI -- pull P, then PC (DS Table 6-4; CAP: 6).""" # MEASURED (CAP). self._fetch_discard() self._read(0x0100 | self.s) # as RTS (CAP) self.p = (self._pull() | U) & ~B # bit 5 and B are not flip-flops low = self._pull() self.pc = self._pull() << 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). def _op_pha(self) -> None: """PHA -- push A (DS Table 6-4).""" self._fetch_discard() self._push(self.a) def _op_phx(self) -> None: """PHX -- push X (DS Table 6-4; CL section 3).""" self._fetch_discard() self._push(self.x) def _op_phy(self) -> None: """PHY -- push Y (DS Table 6-4; CL section 3).""" self._fetch_discard() self._push(self.y) def _op_php(self) -> None: """PHP -- push P with B and bit 5 set (DS Table 6-4).""" self._fetch_discard() self._push(self.p | B | U) def _op_pla(self) -> None: """PLA -- pull A (DS Table 6-4).""" self._fetch_discard() self._read(0x0100 | self.s) self.a = self._set_nz(self._pull()) def _op_plx(self) -> None: """PLX -- pull X (DS Table 6-4; CL section 3).""" self._fetch_discard() self._read(0x0100 | self.s) self.x = self._set_nz(self._pull()) def _op_ply(self) -> None: """PLY -- pull Y (DS Table 6-4; CL section 3).""" self._fetch_discard() self._read(0x0100 | self.s) self.y = self._set_nz(self._pull()) def _op_plp(self) -> None: """PLP -- pull P; the new I counts from the next instruction's poll (DS Table 6-4).""" # Cycles: MEASURED (CAP). When the new I takes effect: HYPOTHESIS (U5). self._fetch_discard() self._read(0x0100 | self.s) self._polled_i = self.p & I self.p = (self._pull() | U) & ~B # -- flags -------------------------------------------------------------------- # DOC (DS Table 6-4); MEASURED (CAP: two cycles, both reads; Dormann's tests). def _op_clc(self) -> None: """CLC -- clear C (DS Table 6-4).""" self._fetch_discard() self.p &= ~C def _op_sec(self) -> None: """SEC -- set C (DS Table 6-4).""" self._fetch_discard() self.p |= C def _op_cld(self) -> None: """CLD -- clear D (DS Table 6-4).""" self._fetch_discard() self.p &= ~D def _op_sed(self) -> None: """SED -- set D (DS Table 6-4).""" self._fetch_discard() self.p |= D def _op_clv(self) -> None: """CLV -- clear V (DS Table 6-4).""" self._fetch_discard() self.p &= ~V def _op_cli(self) -> None: """CLI -- clear I; a pending IRQ is taken after the next instruction (DS Table 6-4; LT `cpu` test 1).""" # TEST ROM (LT `cpu` test 1, expectations from real Lynxes); DOC (DS Table 6-4). self._fetch_discard() self._polled_i = self.p & I # this instruction's poll saw the old I (U5) self.p &= ~I def _op_sei(self) -> None: """SEI -- set I; an IRQ already polled is still taken (DS Table 6-4; U5).""" # HYPOTHESIS (U5: the frame's NMOS rule). self._fetch_discard() self._polled_i = self.p & I self.p |= I # -- BRK, interrupts, reset ---------------------------------------------------- def _skip_poll(self) -> None: """The sequence that just ran does not poll: an instruction runs before any interrupt.""" self._polled = NO_POLL self._deferred = None def _op_brk(self) -> 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).""" # 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() # the signature byte: read and skipped pc = self.pc self._push(pc >> 8) self._push(pc & 0xFF) 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). low = self._read(VECTOR_IRQ) self.pc = self._read(VECTOR_IRQ + 1) << 8 | low self._skip_poll() # U5: the frame's rule def _enter_interrupt(self, kind: int) -> int: """Take an NMI or IRQ at a boundary: seven cycles (CAP: 9,563 + 9,264 entries, all 7, cycles 3 to 5 writes).""" # 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._polled_i = None 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 = self.fetch = True self._read(pc) self.sync = False self._read(pc) self.fetch = False if kind == NMI_LATCH: self._requests &= ~NMI_LATCH # the edge is used up vector = VECTOR_NMI else: vector = VECTOR_IRQ self._push(pc >> 8) self._push(pc & 0xFF) 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) low = self._read(vector) self.pc = self._read(vector + 1) << 8 | low self._skip_poll() return 7 def _accept_reset(self) -> int: """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. """ # 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._reset_pending = False self.halted = self.waiting = self.stopped = False self._polled_i = None pc = self.pc self.sync = self.fetch = True # CAP: SYNC is high in the first of the seven self._read(pc) # U4: the addresses of these two reads are not captured self.sync = False self._read(pc) self.fetch = False if self.reset_writes_stack: self._push(pc >> 8) self._push(pc & 0xFF) self._push(self.p | U) # CAP: the G65SC12 wrote $AF, B clear else: for _ in range(3): self._read(0x0100 | self.s) self.s = (self.s - 1) & 0xFF self.p = (self.p | I) & ~D # DS Table 7-1 "Decimal flag" low = self._read(VECTOR_RESET) self.pc = self._read(VECTOR_RESET + 1) << 8 | low self._skip_poll() return 7 # -- WAI and STP (the W65C02S row) ------------------------------------------- def _op_wai(self) -> None: """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").""" # 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 not self._requests & (IRQ_LINE | NMI_LATCH): self.waiting = True self.halted = True def _op_stp(self) -> None: """STP -- stop the clock until reset: three cycles (DS Table 6-4; GF).""" # DOC (DS; GF's bench notes); no capture executes STP. self._fetch_discard() self._fetch_discard() self.stopped = True self.halted = True def _halted(self) -> int: """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. """ # 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 and self._requests & (IRQ_LINE | NMI_LATCH): self.waiting = False self.halted = False if self._deferred is not None: self._confirm() # the request was made in a waiting cycle: it stands self._polled = None return self.step() self._fetch_discard() return 1