# SPDX-License-Identifier: MIT """Single data swap: SWP and SWPB (DS 4.12). S+2N+I (timing.md table 6-14): the prefetch, a locked N read at Rn, a locked N write of Rm to the same address, then an internal cycle at pc+3L while the data reaches Rd; the next fetch is S (merged IS, C1). The address is driven as computed; a misaligned word read rotates like LDR's (U1). Rm is read before the swap, so Rd = Rm stores Rm's old value (DS 4.12.2). The operands are read after the PC advances, so R15 (which DS 4.12.4 forbids) reads pc+12 (OBSERVED, corpus v1). """ from __future__ import annotations from arm7tdmi_python._alu import ror32 from arm7tdmi_python._core import MASK32, CoreMixin class SwapMixin(CoreMixin): def _arm_swp(self, op: int) -> None: """SWP/SWPB -- Rd = [Rn]; [Rn] = Rm, word or byte, read and write locked (DS 4.12).""" byte = op & (1 << 22) self.r[15] = (self.r[15] + 4) & MASK32 address = self._reg((op >> 16) & 0xF) # Rn in the first cycle, Rm in the read cycle if byte: value = self._read(address, 8, lock=True) self._write(address, 8, self._reg(op & 0xF), lock=True) else: value = ror32(self._read(address, 32, lock=True), (address & 3) * 8) self._write(address, 32, self._reg(op & 0xF), lock=True) self._idle(self.r[15]) rd = (op >> 12) & 0xF self._set_reg(rd, value) self.next_fetch_seq = True if rd == 15: self._refill() # DS 4.12.4 forbids it; the corpus refills (OBSERVED corpus v1)