# SPDX-License-Identifier: MIT """Branches, jumps, LOOP, LINK, NOP and STOP. Every write of R15 is a jump with a delay slot (U1): the byte after the jump is already in the pipeline and runs before the target. The branches ``05-0F`` take their displacement from the pipeline (R15 then points past it) and add it when taken; they are the only opcodes besides the prefixes that keep ALT1/ALT2/B/Sreg/Dreg (U3, ``_decode.KEEPS_PREFIX``). """ from __future__ import annotations from gsu_python._sfr import CY, GO, IRQ, OV, S, Z _SZ = ~(S | Z) class JumpMixin: r: list[int] sfr: int sreg: int pbr: int cbr: int cfgr: int por: int pipeline: int _r15_written: bool _previous_wrote_r15: bool #: C1. True (default): STOP leaves R15 = STOP + 2 (the byte it prefetched, + 1: ares #: ``instructions.cpp`` l.2-10 with ``main``; fullsnes' opcode table; OBSERVED on MAME #: 0.285). False: BK2 2-5-3's table, "Address of STOP instruction + 1" (and jump #: target + 1 after a jump, as both). STOP_R15_PLUS_TWO = True #: C11. False (default): STOP sets SFR.IRQ only when CFGR bit 7 does not mask the IRQ #: (ares ``instructions.cpp`` l.3-6; MAME 0.285 replays). True: always (fullsnes, hedged). IRQ_FLAG_WHEN_MASKED = False def _taken(self, condition: int) -> bool: """Branch conditions in opcode order 05-0F (BK2 9.14-9.26; C2: 06 = BGE, 07 = BLT).""" sfr = self.sfr if condition == 0: # BRA return True if condition <= 2: # BGE / BLT: (S xor OV) = 0 / 1 less = bool(sfr & S) != bool(sfr & OV) return less if condition == 2 else not less bit = (Z, Z, S, S, CY, CY, OV, OV)[condition - 3] # BNE BPL BCC BVC (conditions 3, 5, 7, 9) branch on a clear flag, the others on a set one on_clear = condition & 1 return bool(sfr & bit) != bool(on_clear) def _op_branch(self, n: int) -> None: """Bcc e: R15 = (address of the byte after the displacement) + e when taken.""" displacement = self._pipe() if self._taken(n): offset = displacement - 0x100 if displacement & 0x80 else displacement self._set(15, (self.r[15] + offset) & 0xFFFF) def _op_jmp(self, n: int) -> None: """JMP Rn (BK2 9.44): R15 = Rn.""" self._set(15, self.r[n]) def _op_ljmp(self, n: int) -> None: """LJMP Rn (BK2 9.49 with C3's operands as fullsnes and the emulators): PBR = Rn, R15 = Sreg; CBR = R15 AND FFF0h and every cache line invalid (BK2 2-6-11).""" target = self.r[self.sreg] self.pbr = self.r[n] & 0x7F self._set(15, target) self.cbr = target & 0xFFF0 self._flush_cache() def _op_loop(self, n: int) -> None: """LOOP (BK2 9.54; U31): R12 = R12 - 1, S and Z from it; R15 = R13 unless zero.""" value = (self.r[12] - 1) & 0xFFFF self._set(12, value) self.sfr = (self.sfr & _SZ) | (S if value & 0x8000 else 0) | (0 if value else Z) if value: self._set(15, self.r[13]) def _op_link(self, n: int) -> None: """LINK #n (BK2 9.48; U2): R11 = R15 + n (R15 = the address of the next opcode).""" self._set(11, (self.r[15] + n) & 0xFFFF) def _op_nop(self, n: int) -> None: """NOP (BK2 9.68): clears the prefixes like any other opcode (U3).""" def _op_stop(self, n: int) -> None: """STOP (BK2 9.84; 2-5-3): GO = 0; SFR.IRQ = 1 unless CFGR masks it (C11); the pipeline holds NOP, so a restart begins with a NOP (ares ``instructions.cpp`` l.8; MAME). The byte after STOP was already fetched (U30). R15: C1.""" masked = bool(self.cfgr & 0x80) if not masked or self.IRQ_FLAG_WHEN_MASKED: self.sfr |= IRQ self.sfr &= ~GO self.pipeline = 0x01 if not self.STOP_R15_PLUS_TWO and not self._previous_wrote_r15: self._r15_written = True # BK2: leave R15 = STOP + 1 __all__ = ["JumpMixin"]