// SPDX-License-Identifier: MIT //! Block data transfers: LDM, STM and Thumb's PUSH, POP, LDMIA and STMIA (DS 4.11, 5.14, 5.15). //! //! Transcribed from `arm7tdmi_python/_block.py`. //! //! Registers go lowest-numbered to the lowest address, always with increasing addresses: a //! decrementing form first computes the lowest address (DS 4.11.4; GBATEK "Block Data Transfer", //! "Transfer Order"). The first transfer is N and the rest S. LDM is nS+N+I: an internal cycle at //! pc+3L follows the last read, then a refill if R15 was loaded; the next fetch is S (merged IS, //! C1). STM is (n-1)S+2N, and the next fetch is N (timing.md tables 6-12, 6-13). //! //! Quirks from GBATEK "Strange Effects on Invalid Rlist's" (ARMv4), each judged by the corpus //! and, for Thumb's empty lists, by alyosha-tas's `thumb/Push_no_regs` and `Pop_no_regs` //! (run on a Game Boy Player): //! //! - An empty list transfers R15 alone and moves the base by 0x40. //! - Write-back with the base in the list: STM stores the old base when it is the first //! register of the list and the new base otherwise; LDM's loaded value wins (no write-back). //! //! With S set (DS 4.11.4): an LDM that loads R15 also copies the SPSR to the CPSR; any other //! LDM or STM transfers the User bank's registers. After such an LDM the next instruction reads //! each banked register as its own value OR'd with the User bank's (U7): alyosha-tas `LDM/*` //! ReadMe, "When LDM to user mode registers, the next register access uses the OR'd value of the //! user mode and banked register if it exists ... tested on GBP" (TEST ROM; GBATEK, "Caution": //! the following instruction "might still read R14 instead"). use crate::core::{Bus, ARM7TDMI}; use crate::modes::{bank_of, BANK_FIQ, BANK_INVALID, BANK_USER}; /// The registers of a list, lowest first (the reference's list comprehension), at most 16. struct RegisterList { registers: [usize; 16], len: usize, } impl RegisterList { fn from_bits(bits: u32, count: usize) -> Self { let mut list = RegisterList { registers: [0; 16], len: 0, }; for n in 0..count { if bits >> n & 1 != 0 { list.push(n); } } list } fn push(&mut self, n: usize) { self.registers[self.len] = n; self.len += 1; } fn contains(&self, n: usize) -> bool { self.registers[..self.len].contains(&n) } } impl ARM7TDMI { // ------------------------------------------------------------------ the User bank /// Whether Rn of the current mode is a banked copy, not the User bank's register. #[inline] fn banked_here(&self, n: usize) -> bool { let bank = bank_of(self.cpsr); if bank == BANK_FIQ { return (8..=14).contains(&n); } bank != BANK_USER && bank != BANK_INVALID && (13..=14).contains(&n) } /// Rn of the User bank, whatever the current mode (DS 4.11.4). #[inline] fn user_reg(&self, n: usize) -> u32 { if self.banked_here(n) { return self.bank[BANK_USER][n - 8]; } self.r[n] } #[inline] fn set_user_reg(&mut self, n: usize, value: u32) { if self.banked_here(n) { self.bank[BANK_USER][n - 8] = value; } else { self.r[n] = value; } } /// `reg` for the instruction after an LDM^ (U7): a banked register reads OR'd with /// the User bank's. #[inline] pub(crate) fn reg_or_user(&self, n: usize) -> u32 { let mut value = self.r[n]; if self.banked_here(n) { value |= self.bank[BANK_USER][n - 8]; } value } // ------------------------------------------------------------------ ARM fn block_transfer(&mut self, op: u32, load: bool) { let rn = ((op >> 16) & 0xF) as usize; let mut registers = RegisterList::from_bits(op, 16); let size: u32 = if registers.len != 0 { 4 * registers.len as u32 } else { 0x40 }; if registers.len == 0 { registers.push(15); } let base = self.reg(rn); let pc = self.r[15].wrapping_add(4); self.r[15] = pc; let (up, pre) = (op & (1 << 23) != 0, op & (1 << 24) != 0); let mut address; let new_base; if up { address = if pre { base.wrapping_add(4) } else { base }; new_base = base.wrapping_add(size); } else { address = if !pre { base.wrapping_sub(size).wrapping_add(4) } else { base.wrapping_sub(size) }; new_base = base.wrapping_sub(size); } let write_back = op & (1 << 21) != 0; let s_bit = op & (1 << 22) != 0; let user_bank = s_bit && !(load && registers.contains(15)); // A User bank transfer writes the base back to the User bank's Rn too ("should not be // used", GBATEK; OBSERVED, corpus v1). let mut seq = false; for index in 0..registers.len { let n = registers.registers[index]; if load { let value = self.read(address, 32, seq, false); if index == 0 && write_back { if user_bank { self.set_user_reg(rn, new_base); } else { self.set_reg(rn, new_base); } } if user_bank { self.set_user_reg(n, value); } else { self.set_reg(n, value); } } else { // Data is read after the first cycle (U7). let value = if user_bank { self.user_reg(n) } else { self.r[n] }; self.write(address, 32, value, seq, false); if index == 0 && write_back { if user_bank { self.set_user_reg(rn, new_base); } else { self.set_reg(rn, new_base); } } } address = address.wrapping_add(4); seq = true; } if !load { self.next_fetch_seq = false; return; } self.idle(pc); // at pc+3L, even when R15 was just loaded self.next_fetch_seq = true; // merged IS (C1) if registers.contains(15) { if s_bit { self.restore_cpsr_from_spsr(); } self.refill(); } else if user_bank { self.glitch_next = true; // U7 } } /// LDM -- load Rlist from consecutive words; ^ restores the CPSR or loads User /// registers (DS 4.11, U7). pub(crate) fn arm_ldm(&mut self, op: u32) { self.block_transfer(op, true); } /// STM -- store Rlist to consecutive words; ^ stores User registers (DS 4.11). pub(crate) fn arm_stm(&mut self, op: u32) { self.block_transfer(op, false); } // ------------------------------------------------------------------ Thumb /// The Thumb forms: increment after from Rb (LDMIA, STMIA, POP) or decrement before /// (PUSH), always with write-back (DS 5.14-5.15; GBATEK "THUMB Opcodes: Memory: Multiple /// Load/Store"). fn block_thumb( &mut self, rb: usize, registers: RegisterList, load: bool, push: bool, pc_bit0: bool, ) { let mut registers = registers; let size: u32 = if registers.len != 0 { 4 * registers.len as u32 } else { 0x40 }; if registers.len == 0 { registers.push(15); } let base = self.r[rb]; let pc = self.r[15].wrapping_add(2); self.r[15] = pc; let new_base = if push { base.wrapping_sub(size) } else { base.wrapping_add(size) }; let mut address = if push { new_base } else { base }; let mut seq = false; for index in 0..registers.len { let n = registers.registers[index]; if load { let value = self.read(address, 32, seq, false); if index == 0 { self.r[rb] = new_base; } self.r[n] = value; } else { self.write(address, 32, self.r[n], seq, false); if index == 0 { self.r[rb] = new_base; } } address = address.wrapping_add(4); seq = true; } if !load { self.next_fetch_seq = false; return; } self.idle(pc); // at pc+3L, even when R15 was just loaded self.next_fetch_seq = true; // merged IS (C1) if registers.contains(15) { if !pc_bit0 { self.r[15] &= !1; } self.refill(); // Thumb state is kept (DS 5.14) } } /// PUSH {Rlist}/PUSH {Rlist, LR} -- store to the full descending stack at SP (DS 5.14, /// table 6-13). pub(crate) fn thumb_push(&mut self, op: u32) { let mut registers = RegisterList::from_bits(op, 8); if op & 0x100 != 0 { registers.push(14); } self.block_thumb(13, registers, false, true, true); } /// POP {Rlist}/POP {Rlist, PC} -- load from the full descending stack at SP; PC stays in /// Thumb state (DS 5.14, table 6-12). pub(crate) fn thumb_pop(&mut self, op: u32) { let mut registers = RegisterList::from_bits(op, 8); if op & 0x100 != 0 { registers.push(15); } // POP {PC} clears bit 0 of the loaded PC; an empty list's R15 keeps it, as the // refill leaves it (U20; OBSERVED, corpus v1). self.block_thumb(13, registers, true, false, op & 0x100 == 0); } /// STMIA Rb!,{Rlist} -- store, increment after, write back (DS 5.15, table 6-13). pub(crate) fn thumb_stmia(&mut self, op: u32) { let registers = RegisterList::from_bits(op, 8); self.block_thumb(((op >> 8) & 7) as usize, registers, false, false, true); } /// LDMIA Rb!,{Rlist} -- load, increment after, write back unless Rb is loaded (DS 5.15, /// table 6-12). pub(crate) fn thumb_ldmia(&mut self, op: u32) { let registers = RegisterList::from_bits(op, 8); self.block_thumb(((op >> 8) & 7) as usize, registers, true, false, true); } }