# SPDX-License-Identifier: MIT """Jumps, calls, returns, skips, the stack and HALT. PC is 8 bits, a word within the 256-word cache page that holds program bank PB (U2). A jump or call loads PC from the low byte; a **far** one also sets PB = P, the page register, after which the cache lookup at the next fetch finds or loads that page (C5: byuu's model, ares ``instructions.cpp`` l.174-186, ISC). A taken JMP, JSR or RTS costs 2 extra clocks, a taken SKIP 1 (U1: ares ``step(2)`` in ``instructionJMP``/``JSR``/``RTS``). Contested here: **C4** the stack (``STACK_MODEL``), **C17** which bits make a jump far (``FAR_SELECT``). """ from __future__ import annotations STACK_DEPTH = 8 class FlowMixin: #: C4. "ares" (default; byuu's model, ares ``instructions.cpp`` l.1-25 ``push``/``pull``): #: an 8-entry shift register of PB:PC; a push drops the oldest entry, a pull from an #: empty stack gives 0 (PB 0, PC 0). "ring-observed": what a closed-box Mesen2 run #: shows (OBSERVED, ``scripts/cx4_probe.py stack``): ten nested calls keep the last #: eight return addresses, and the ninth return after eight comes back to the newest #: one again, so the eight entries form a ring that a push overwrites and a pull wraps. #: The program's deepest nesting decides nothing (validation.md). STACK_MODEL = "ares" #: C17. "bit9" (default; byuu's model, ares ``instruction.cpp`` l.8-15, ``0000 10f.``): #: bit 9 makes a jump or call far, bit 8 is ignored. "either-observed": a closed-box #: Mesen2 run takes a jump with only bit 8 set (09xxh) as far (OBSERVED, #: ``scripts/cx4_probe.py far``). The shipped program never sets bit 8 in a jump. FAR_SELECT = "bit9" # ----- the stack ------------------------------------------------------------------------ def _push(self) -> None: entry = self.pb << 8 | self.pc stack = self.stack if self.STACK_MODEL == "ring-observed": stack[self.sp] = entry # the ring: write at the pointer, then step it self.sp = (self.sp + 1) % STACK_DEPTH return stack[1:] = stack[:-1] # shift every entry down; the oldest falls off the end stack[0] = entry def _pull(self) -> None: stack = self.stack if self.STACK_MODEL == "ring-observed": self.sp = (self.sp - 1) % STACK_DEPTH # step back, then read entry = stack[self.sp] else: entry = stack[0] stack[:-1] = stack[1:] stack[-1] = 0 self.pb = entry >> 8 & 0x7FFF self.pc = entry & 0xFF def _far(self, word: int) -> bool: if self.FAR_SELECT == "bit9": return bool(word & 0x200) return bool(word & 0x300) # "either-observed": bit 8 or bit 9 def _condition(self, index: int) -> bool: """0 always, 1 EQ (Z), 2 GE (C), 3 MI (N), 4 VS (V).""" if index == 0: return True if index == 1: return self.z if index == 2: return self.c if index == 3: return self.n return self.v # ----- JMP, JSR, RTS -------------------------------------------------------------------- def _op_jmp(self, word: int) -> None: if not self._condition((word >> 10) - 2): return if self._far(word): self.pb = self.p self.pc = word & 0xFF self._tick(2) def _op_jsr(self, word: int) -> None: if not self._condition((word >> 10) - 10): return self._push() # PC has already advanced: the return address is the next word if self._far(word): self.pb = self.p self.pc = word & 0xFF self._tick(2) def _op_rts(self, word: int) -> None: self._pull() self._tick(2) # ----- SKIP: bits 9-8 pick V, C, Z, N; bit 0 is the value that skips ----------------- def _op_skip(self, word: int) -> None: flag = (self.v, self.c, self.z, self.n)[word >> 8 & 3] if flag != bool(word & 1): return self._advance() # the skipped word is never fetched, but PC can wrap a page (U2) self._tick(1) # ----- HALT and the halted state (U13) -------------------------------------------------- def _op_halt(self, word: int) -> None: self._halt() def _halt(self) -> None: """Stop; with the IRQ enabled ($7F51 bit 0 = 0), raise /IRQ and the IRQ flag (byuu's glue, ares ``hitachidsp.cpp`` l.31 ``HitachiDSP::halt``).""" self.halted = True if not self.irq_disabled: self.irq_flag = True self.irq_line = True __all__ = ["STACK_DEPTH", "FlowMixin"]