### whole module """Exceptions an instruction raises: SWI, the undefined instruction, and the coprocessor space. The GBA has no coprocessor: CPA and CPB are tied high, so CDP, LDC, STC and MCR/MRC to any coprocessor but CP14 take the undefined-instruction trap, 2S+N+I: the prefetch, an internal cycle while the core waits for a coprocessor that never answers, then the entry (TRM table 6-21; NBA and the corpus have no I cycle, C5). CP14, the debug communications channel, is inside the core: its MRC and MCR do not trap on a GBA (alyosha-tas ``Coprocessor_14``, a hardware test; U17, C6). Thumb encodings with no ARMv4T meaning, including the conditional branch with condition 1110 (``0xDExx``) and ARMv5's BLX suffix (``0xE8xx``), take the undefined trap too. That is ARM's rule and what mGBA and ares do; NBA and the corpus branch on ``0xDExx``. It is provisional until a hardware test ROM decides (C7, C8, user decision 4). """ from __future__ import annotations from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import FLAG_C, FLAG_N, FLAG_V, FLAG_Z, SVC, UND CP14 = 14 class ExceptionMixin(CoreMixin): def _undefined(self, length: int) -> None: """The undefined trap after cycle 1: I at pc+2L, then entry at 0x04 (table 6-21).""" self._idle(self.r[15]) self._enter_exception(0x04, UND, self.r[15] - length) def _arm_swi(self, op: int) -> None: """SWI -- enter Supervisor mode at 0x08; R14_svc = next instruction (DS 4.13).""" self._enter_exception(0x08, SVC, self.r[15] - 4) def _arm_undefined(self, op: int) -> None: """UNDEFINED -- the undefined-instruction encoding: trap to 0x04 (DS 4.15, C5).""" self._undefined(4) def _arm_cdp(self, op: int) -> None: """CDP -- no coprocessor answers on the GBA: undefined trap (TRM 4.6, C5).""" self._undefined(4) def _arm_ldc(self, op: int) -> None: """LDC -- no coprocessor answers on the GBA: undefined trap (TRM 4.6, C5).""" self._undefined(4) def _arm_stc(self, op: int) -> None: """STC -- no coprocessor answers on the GBA: undefined trap (TRM 4.6, C5).""" self._undefined(4) def _arm_mrc(self, op: int) -> None: """MRC -- CP14: Rd = the value on the data bus, the opcode cycle 1 fetched; Rd = R15 sets N Z C V from its top bits; any other coprocessor traps (TRM 4.6, U17, C6).""" if (op >> 8) & 0xF != CP14: self._undefined(4) return value = self.pipeline[1] self._coprocessor_cycle(self.r[15] + 4) # C cycle at pc+12 (table 6-19) self.r[15] = (self.r[15] + 4) & MASK32 self._idle(self.r[15]) # data to Rd rd = (op >> 12) & 0xF if rd == 15: flags = FLAG_N | FLAG_Z | FLAG_C | FLAG_V self.cpsr = (self.cpsr & ~flags) | (value & flags) else: self._set_reg(rd, value) self.next_fetch_seq = True # merged IS (C1) def _arm_mcr(self, op: int) -> None: """MCR -- CP14: Rd is driven on the bus and nothing visible changes; any other coprocessor traps (TRM 4.6, U17, C6).""" if (op >> 8) & 0xF != CP14: self._undefined(4) return self._coprocessor_cycle(self.r[15] + 4) # C cycle at pc+12 (table 6-20) self.r[15] = (self.r[15] + 4) & MASK32 self.next_fetch_seq = False def _thumb_swi(self, op: int) -> None: """SWI Value8 -- format 17: enter Supervisor mode in ARM state at 0x08 (DS 5.17).""" self._enter_exception(0x08, SVC, self.r[15] - 2) def _thumb_undefined(self, op: int) -> None: """UNDEFINED -- a Thumb encoding with no ARMv4T meaning: trap to 0x04, R14_und = next instruction (DS 5.16, C7, C8).""" self._undefined(2)