// SPDX-License-Identifier: MIT //! Single data swap: SWP and SWPB (DS 4.12). //! //! Transcribed from `arm7tdmi_python/_swap.py`. //! //! 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). use crate::alu::ror32; use crate::core::{Bus, ARM7TDMI}; impl ARM7TDMI { /// SWP/SWPB -- Rd = [Rn]; [Rn] = Rm, word or byte, read and write locked (DS 4.12). pub(crate) fn arm_swp(&mut self, op: u32) { let byte = op & (1 << 22) != 0; self.r[15] = self.r[15].wrapping_add(4); let address = self.reg(((op >> 16) & 0xF) as usize); // Rn in the first cycle, Rm in the read cycle let value; if byte { value = self.read(address, 8, false, true); let rm = self.reg((op & 0xF) as usize); self.write(address, 8, rm, false, true); } else { value = ror32(self.read(address, 32, false, true), (address & 3) * 8); let rm = self.reg((op & 0xF) as usize); self.write(address, 32, rm, false, true); } self.idle(self.r[15]); let rd = ((op >> 12) & 0xF) as usize; 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) } } }