# SPDX-License-Identifier: MIT """Software interrupts, the undefined instruction, and the coprocessor instructions. SWI (DS 4.13, 5.17) is 2S+N: the prefetch, then the refill at vector 0x08 in Supervisor mode; R14_svc is the address of the next instruction (TRM table 2-3). The undefined instruction and every coprocessor instruction no coprocessor answers (DS 4.14, 4.15-4.18) take the undefined trap, 2S+N+I: the prefetch, an internal cycle at pc+2L while the core waits for a coprocessor that never answers, then the refill at vector 0x04 in Undefined mode; R14_und is the address of the next instruction (TRM table 6-21, C5, user decision 5). CP14 is answered on the ARM7TDMI: MRC and MCR to it do not trap (U17; alyosha-tas ``arm/Coprocessor_14``, run on a Game Boy Player, C6). MRC is S+C+I and writes Rd with the opcode the prefetch read (bits 31:28 to N Z C V when Rd is R15); MCR is S+C and changes nothing; its next fetch is N (TRM tables 6-19, 6-20). CDP, LDC and STC to CP14 still trap (HYPOTHESIS; the corpus agrees, OBSERVED corpus v1). """ from __future__ import annotations from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import SVC, UND CP14 = 14 class ExceptionMixin(CoreMixin): def _undefined(self, length: int) -> None: """The undefined trap; ``length`` is the instruction's size in bytes (TRM 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 vector 0x08 (DS 4.13, table 6-15).""" self._enter_exception(0x08, SVC, self.r[15] - 4) def _arm_undefined(self, op: int) -> None: """UNDEFINED -- the undefined trap (DS 4.16, C5).""" self._undefined(4) def _arm_cdp(self, op: int) -> None: """CDP -- no coprocessor answers: the undefined trap (DS 4.14, C5).""" self._undefined(4) def _arm_ldc(self, op: int) -> None: """LDC -- no coprocessor answers: the undefined trap (DS 4.15, C5).""" self._undefined(4) def _arm_stc(self, op: int) -> None: """STC -- no coprocessor answers: the undefined trap (DS 4.15, C5).""" self._undefined(4) def _arm_mrc(self, op: int) -> None: """MRC -- CP14: Rd = the prefetched opcode; others: the undefined trap (U17, C6).""" if (op >> 8) & 0xF != CP14: self._undefined(4) return self.r[15] = (self.r[15] + 4) & MASK32 self._coprocessor_cycle(self.r[15]) self._idle(self.r[15]) value = self.pipeline[1] rd = (op >> 12) & 0xF if rd == 15: self.cpsr = (self.cpsr & 0x0FFFFFFF) | (value & 0xF0000000) else: self._set_reg(rd, value) self.next_fetch_seq = True # merged IS (C1) def _arm_mcr(self, op: int) -> None: """MCR -- CP14: nothing changes; others: the undefined trap (U17, C6).""" if (op >> 8) & 0xF != CP14: self._undefined(4) return self.r[15] = (self.r[15] + 4) & MASK32 self._coprocessor_cycle(self.r[15]) self.next_fetch_seq = False def _thumb_swi(self, op: int) -> None: """SWI -- enter Supervisor mode in ARM state at vector 0x08 (DS 5.17, table 6-15).""" self._enter_exception(0x08, SVC, self.r[15] - 2) def _thumb_undefined(self, op: int) -> None: """UNDEFINED -- the undefined trap, also for 0xDExx and 0xE8xx (DS 5.0, C7, C8).""" self._undefined(2)