### whole module """PSR transfer: MRS and MSR (DS 4.6), one cycle each like data processing (TRM 6.5). MSR writes the fields its mask names (``c`` = bits 7-0, ``x`` = 15-8, ``s`` = 23-16, ``f`` = 31-24; ARMv4T defines only ``c`` and ``f``, and NBA writes the others as given). In User mode only the flags can change. Writing the control field changes mode at once, and bit 4 of the mode always reads 1 (U15). MSR to the SPSR in a mode without one is ignored and MRS of it reads the CPSR (U16). MSR can set T; the DS says software "must never" (U16). """ from __future__ import annotations from arm7tdmi_python._alu import ror32 from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import USR class PSRMixin(CoreMixin): def _arm_mrs(self, op: int) -> None: """MRS -- Rd = CPSR, or the SPSR with bit 22 (DS 4.6, U16).""" value = self._get_spsr() if op & (1 << 22) else self.cpsr self._set_reg((op >> 12) & 0xF, value) self.next_fetch_seq = True self.r[15] = (self.r[15] + 4) & MASK32 def _arm_msr(self, op: int) -> None: """MSR -- CPSR or SPSR fields <- Rm or a rotated immediate (DS 4.6, U15, U16).""" mask = 0 for bit, field in ((16, 0x000000FF), (17, 0x0000FF00), (18, 0x00FF0000), (19, 0xFF000000)): if op & (1 << bit): mask |= field immediate = op & (1 << 25) value = ror32(op & 0xFF, ((op >> 8) & 0xF) * 2) if immediate else self._reg(op & 0xF) if not op & (1 << 22): if self.cpsr & 0x1F == USR: mask &= 0xFF000000 # User mode may change only the flags (DS 4.6) if mask & 0xFF: value |= 0x10 # bit 4 of the mode is always 1 on the ARM7TDMI (U15) self._switch_mode(value & 0x1F) self.cpsr = (self.cpsr & ~mask) | (value & mask) elif self._spsr_bank is not None and not self._mode_invalid: self.spsr[self._spsr_bank] = (self._get_spsr() & ~mask) | (value & mask) self.next_fetch_seq = True self.r[15] = (self.r[15] + 4) & MASK32