// SPDX-License-Identifier: MIT //! PSR transfers: MRS and MSR (DS 4.6). //! //! Transcribed from `arm7tdmi_python/_psr.py`. //! //! Both take one S cycle (timing.md table 6-4). MSR's field mask is bits 19:16: `c` (bit 16) //! writes PSR[7:0], `x` (17) PSR[15:8], `s` (18) PSR[23:16], `f` (19) PSR[31:24] (DS 4.6 //! describes the flags-only and whole-register forms; the four-field mask is the corpus's //! behaviour, OBSERVED corpus v1). In User mode only the flags field of the CPSR can change //! (DS 4.6.1). A write to the CPSR's control field that changes the mode swaps the banks at once; //! a control-field write can also change T (DS 4.6 says software must not: the corpus keeps //! the written bit, OBSERVED corpus v1). With no SPSR (User, System, a mode value that names no //! mode) an SPSR write changes nothing and an SPSR read returns the CPSR (see core.rs). use crate::alu::ror32; use crate::core::{Bus, ARM7TDMI}; use crate::modes::{MODE_BITS, USR}; pub const FIELD_MASKS: [u32; 4] = [0x0000_00FF, 0x0000_FF00, 0x00FF_0000, 0xFF00_0000]; impl ARM7TDMI { /// MRS -- Rd = CPSR, or SPSR with bit 22 (DS 4.6). pub(crate) fn arm_mrs(&mut self, op: u32) { let value = if op & (1 << 22) != 0 { self.get_spsr() } else { self.cpsr }; self.set_reg(((op >> 12) & 0xF) as usize, value); self.next_fetch_seq = true; self.r[15] = self.r[15].wrapping_add(4); } /// MSR -- CPSR or SPSR fields <- Rm or a rotated immediate, by field mask (DS 4.6). pub(crate) fn arm_msr(&mut self, op: u32) { let immediate = op & (1 << 25) != 0; let value = if immediate { ror32(op & 0xFF, ((op >> 8) & 0xF) * 2) } else { self.reg((op & 0xF) as usize) }; let mut mask = 0; for (field, field_mask) in FIELD_MASKS.iter().enumerate() { if op & (1 << (16 + field)) != 0 { mask |= field_mask; } } if op & (1 << 22) != 0 { if let Some(bank) = self.spsr_bank { self.spsr[bank] = (self.spsr[bank] & !mask) | (value & mask); } } else { if self.cpsr & MODE_BITS == USR { mask &= FIELD_MASKS[3]; } let mut new = (self.cpsr & !mask) | (value & mask); if mask & 0xFF != 0 { new |= 0x10; // M4 always reads 1 (alyosha-tas psr ReadMe, tested on GBP) } if mask & MODE_BITS != 0 { self.switch_mode(new & MODE_BITS); } self.cpsr = new; } self.next_fetch_seq = true; self.r[15] = self.r[15].wrapping_add(4); } }