# SPDX-License-Identifier: MIT """JP: unconditional jumps and calls, the conditional jumps, and the uPD96050's extras. DS p.23-24 (BRCH and NA), fullsnes "JP Instructions" (the uPD96050: JP bits 1-0 are NA bits 12-11, conditional jumps keep PC bit 13, ``HJMP``/``HCALL`` set it, ``JMP``/``CALL`` clear it, ``JMPSO`` jumps to SO; U11). On the uPD7725 the same arithmetic runs and the 11-bit PC drops the extra bits; the three uPD96050 codes execute on both chips as in every emulator (C8). Any BRCH value not in the table does nothing: PC simply moves on (every emulator). """ from __future__ import annotations from upd7725_python._alu import FLAG_C, FLAG_OV0, FLAG_OV1, FLAG_S0, FLAG_S1, FLAG_Z from upd7725_python._fields import CALL, HCALL, HJMP, JMP, JMPSO from upd7725_python._op import SR_RQM # Conditional codes 080h-0BFh: bits 5-1 name the condition and bit 0 of the even codes (bit 1 # of BRCH) the sense; JDPLx use bits 1-0 (DS p.23). Each entry: (flag register, bit) by code. _FLAG_CONDITIONS = { 0x080: ("a", FLAG_C, 0), # JNCA 0x082: ("a", FLAG_C, 1), # JCA 0x084: ("b", FLAG_C, 0), # JNCB 0x086: ("b", FLAG_C, 1), # JCB 0x088: ("a", FLAG_Z, 0), # JNZA 0x08A: ("a", FLAG_Z, 1), # JZA 0x08C: ("b", FLAG_Z, 0), # JNZB 0x08E: ("b", FLAG_Z, 1), # JZB 0x090: ("a", FLAG_OV0, 0), # JNOVA0 0x092: ("a", FLAG_OV0, 1), # JOVA0 0x094: ("b", FLAG_OV0, 0), # JNOVB0 0x096: ("b", FLAG_OV0, 1), # JOVB0 0x098: ("a", FLAG_OV1, 0), # JNOVA1 0x09A: ("a", FLAG_OV1, 1), # JOVA1 0x09C: ("b", FLAG_OV1, 0), # JNOVB1 0x09E: ("b", FLAG_OV1, 1), # JOVB1 0x0A0: ("a", FLAG_S0, 0), # JNSA0 0x0A2: ("a", FLAG_S0, 1), # JSA0 0x0A4: ("b", FLAG_S0, 0), # JNSB0 0x0A6: ("b", FLAG_S0, 1), # JSB0 0x0A8: ("a", FLAG_S1, 0), # JNSA1 0x0AA: ("a", FLAG_S1, 1), # JSA1 0x0AC: ("b", FLAG_S1, 0), # JNSB1 0x0AE: ("b", FLAG_S1, 1), # JSB1 } class JumpMixin: """JP (bits 23-22 = 10).""" pc: int sp: int dp: int so: int sr: int flaga: int flagb: int siack: int soack: int stack: list[int] _pc_mask: int _stack_mask: int def _push(self, address: int) -> None: self.stack[self.sp] = address self.sp = (self.sp + 1) & self._stack_mask # U10: a 5th (9th) CALL wraps (C5) def _jump(self, word: int) -> None: """``self.pc`` already points at the next instruction (the return address).""" code = word >> 13 & 0x1FF # NA plus, on the uPD96050, JP bits 1-0 as NA bits 12-11 (U11). target = (word & 3) << 11 | (word >> 2 & 0x7FF) if code == JMP: self.pc = target & self._pc_mask # clears PC bit 13 elif code == CALL: self._push(self.pc) self.pc = target & self._pc_mask elif code == HJMP: self.pc = (0x2000 | target) & self._pc_mask # sets PC bit 13 (uPD96050; C8) elif code == HCALL: self._push(self.pc) self.pc = (0x2000 | target) & self._pc_mask elif code == JMPSO: self.pc = self.so & self._pc_mask # the ST011's computed jumps (U16, C4) elif self._condition(code): # A conditional jump keeps PC bit 13 (fullsnes; U11). self.pc = ((self.pc & 0x2000) | target) & self._pc_mask def _condition(self, code: int) -> bool: """Whether a conditional BRCH code jumps; False for every unlisted code.""" entry = _FLAG_CONDITIONS.get(code) if entry is not None: register, bit, sense = entry flags = self.flaga if register == "a" else self.flagb return bool(flags & bit) == bool(sense) low = self.dp & 0xF if code == 0x0B0: return low == 0 # JDPL0 if code == 0x0B1: return low != 0 # JDPLN0 if code == 0x0B2: return low == 0xF # JDPLF if code == 0x0B3: return low != 0xF # JDPLNF if code == 0x0B4: return self.siack == 0 # JNSIAK: the SI acknowledge latch (C12) if code == 0x0B6: return self.siack == 1 # JSIAK if code == 0x0B8: return self.soack == 0 # JNSOAK if code == 0x0BA: return self.soack == 1 # JSOAK if code == 0x0BC: return not self.sr & SR_RQM # JNRQM if code == 0x0BE: return bool(self.sr & SR_RQM) # JRQM: the programs' host wait (`JRQM $`) return False # unknown BRCH: fall through __all__ = ["JumpMixin"]