// SPDX-License-Identifier: MIT //! Single data transfers: LDR, STR, LDRB, STRB, LDRH, STRH, LDRSB, LDRSH and Thumb formats 6-11. //! //! Transcribed from `arm7tdmi_python/_transfer.py`. //! //! A load is S+N+I: the prefetch, the data read (N) at the address as computed, and an internal //! cycle at pc+3L while the data reaches Rd; the next fetch is S at pc+3L (merged IS, C1). A store //! is S+N, and the next fetch is N (timing.md tables 6-9, 6-11). The core drives the address with //! its low bits as computed and never aligns it: the board's memory ignores them (U3). Loads fix //! up what comes back: a misaligned word rotates (U1), a misaligned halfword rotates by 8, and a //! misaligned signed halfword is the sign-extended byte at that address (U2, C4). //! //! Write-back happens after the data cycle; a load into the base register wins over write-back //! (U6). A write-back into R15 is "must not" (DS 4.9); the core refills there as for every other //! write to R15, and the corpus's cases are excluded from the gate (C2). use crate::alu::{ror32, shift_by_immediate}; use crate::core::{Bus, ARM7TDMI}; pub const WORD: u32 = 0; pub const BYTE: u32 = 1; pub const HALF: u32 = 2; pub const SIGNED_BYTE: u32 = 3; pub const SIGNED_HALF: u32 = 4; #[inline] pub fn sign_extend_from(value: u32, bits: u32) -> u32 { if value >> (bits - 1) & 1 != 0 { value.wrapping_sub(1 << bits) } else { value } } impl ARM7TDMI { /// One N data read of `kind` at `address`, as the register receives it. #[inline] fn load(&mut self, address: u32, kind: u32) -> u32 { if kind == WORD { // A misaligned word is the aligned word rotated right by 8 x address[1:0] (U1). return ror32(self.read(address, 32, false, false), (address & 3) * 8); } if kind == BYTE { return self.read(address, 8, false, false); } if kind == SIGNED_BYTE { return sign_extend_from(self.read(address, 8, false, false), 8); } let value = self.read(address, 16, false, false); if kind == HALF { // A misaligned halfword is rotated right by 8 within the 32-bit result (U2). return if address & 1 != 0 { ror32(value, 8) } else { value }; } // A misaligned signed halfword is the sign-extended byte at that address, read with // the instruction's width 16 (U2, C4). if address & 1 != 0 { sign_extend_from(value >> 8, 8) } else { sign_extend_from(value, 16) } } #[inline] fn store(&mut self, address: u32, width: u32, value: u32) { self.write(address, width, value, false, false); } // ------------------------------------------------------------------ ARM /// (transfer address, base register, signed offset) of an ARM transfer; advances the PC /// past the address cycle (DS 4.9.1). The offset is returned as the 32-bit value the /// reference's signed offset wraps to. #[inline] fn single_address(&mut self, op: u32, offset: u32) -> (u32, usize, u32) { let rn = ((op >> 16) & 0xF) as usize; let base = self.reg(rn); self.r[15] = self.r[15].wrapping_add(4); let mut offset = offset; if op & (1 << 23) == 0 { offset = offset.wrapping_neg(); } let address = if op & (1 << 24) != 0 { base.wrapping_add(offset) } else { base }; (address, rn, offset) } /// Write-back (W set, or always when post-indexed); returns whether R15 was written. #[inline] fn write_back(&mut self, op: u32, rn: usize, offset: u32) -> bool { if op & (1 << 21) != 0 || op & (1 << 24) == 0 { let value = self.reg(rn).wrapping_add(offset); self.set_reg(rn, value); return rn == 15; } false } fn arm_load(&mut self, op: u32, offset: u32, kind: u32) { let rd = ((op >> 12) & 0xF) as usize; let (address, rn, offset) = self.single_address(op, offset); let user = op & (1 << 24) == 0 && op & (1 << 21) != 0; // LDRT: nTRANS low for the data cycle self.user_access = user; let value = self.load(address, kind); self.user_access = false; let pc_written = self.write_back(op, rn, offset); self.idle(self.r[15]); // data to Rd: I cycle at pc+3L self.set_reg(rd, value); self.next_fetch_seq = true; // merged IS (C1) if rd == 15 || pc_written { self.refill(); } } fn arm_store(&mut self, op: u32, offset: u32, width: u32) { let rd = ((op >> 12) & 0xF) as usize; let (address, rn, offset) = self.single_address(op, offset); let user = op & (1 << 24) == 0 && op & (1 << 21) != 0; // STRT self.user_access = user; self.store(address, width, self.r[rd]); // read in cycle 2 (U7); R15 is pc+12 (U8) self.user_access = false; let pc_written = self.write_back(op, rn, offset); self.next_fetch_seq = false; // two N cycles back to back (TRM 3.3.1) if pc_written { self.refill(); } } /// The 12-bit immediate, or Rm shifted by an immediate (DS 4.9.1; never by a register). #[inline] fn single_offset(&self, op: u32) -> u32 { if op & (1 << 25) == 0 { return op & 0xFFF; } let (value, _) = shift_by_immediate( (op >> 5) & 3, self.reg((op & 0xF) as usize), (op >> 7) & 0x1F, self.carry(), ); value } /// LDR -- Rd = word at the address, rotated if misaligned; T form is LDRT (DS 4.9, U1). pub(crate) fn arm_ldr(&mut self, op: u32) { let offset = self.single_offset(op); self.arm_load(op, offset, WORD); } /// LDRB -- Rd = byte at the address, zero-extended; T form is LDRBT (DS 4.9). pub(crate) fn arm_ldrb(&mut self, op: u32) { let offset = self.single_offset(op); self.arm_load(op, offset, BYTE); } /// STR -- word at the address = Rd; T form is STRT (DS 4.9, U3, U8). pub(crate) fn arm_str(&mut self, op: u32) { let offset = self.single_offset(op); self.arm_store(op, offset, 32); } /// STRB -- byte at the address = Rd[7:0]; T form is STRBT (DS 4.9). pub(crate) fn arm_strb(&mut self, op: u32) { let offset = self.single_offset(op); self.arm_store(op, offset, 8); } /// An 8-bit immediate split across bits 11:8 and 3:0, or Rm (DS 4.10). #[inline] fn halfword_offset(&self, op: u32) -> u32 { if op & (1 << 22) != 0 { return (op & 0xF) | ((op >> 4) & 0xF0); } self.reg((op & 0xF) as usize) } /// LDRH -- Rd = halfword, zero-extended; misaligned rotates by 8 (DS 4.10, U2). pub(crate) fn arm_ldrh(&mut self, op: u32) { let offset = self.halfword_offset(op); self.arm_load(op, offset, HALF); } /// LDRSB -- Rd = byte, sign-extended (DS 4.10). pub(crate) fn arm_ldrsb(&mut self, op: u32) { let offset = self.halfword_offset(op); self.arm_load(op, offset, SIGNED_BYTE); } /// LDRSH -- Rd = halfword, sign-extended; misaligned is the signed byte (DS 4.10, C4). pub(crate) fn arm_ldrsh(&mut self, op: u32) { let offset = self.halfword_offset(op); self.arm_load(op, offset, SIGNED_HALF); } /// STRH -- halfword at the address = Rd[15:0] (DS 4.10). pub(crate) fn arm_strh(&mut self, op: u32) { let offset = self.halfword_offset(op); self.arm_store(op, offset, 16); } /// The ARMv5TE doubleword encodings on the ARM7TDMI (U21): no data cycle, the base /// written back as a halfword transfer would, `idle_cycles` internal cycles at pc+12. /// /// No document says what ARMv4 does here (GBATEK: "supported on ARMv5TE and up only") /// and the corpus has no such case. OBSERVED, NanoBoyAdvance 1.8.3 run as a closed box on /// 98 cases each made from the corpus's STRH cases with bits 6:5 patched: no bus /// access, two internal cycles (LDRD) or one (STRD), registers as STRH leaves them less /// the store. HYPOTHESIS for hardware; a GBA test ROM would settle it. fn doubleword_encoding(&mut self, op: u32, idle_cycles: u32) { let offset = self.halfword_offset(op); let (_, rn, offset) = self.single_address(op, offset); for _ in 0..idle_cycles { self.idle(self.r[15]); } self.next_fetch_seq = true; // merged IS (C1) if self.write_back(op, rn, offset) { self.refill(); } } /// LDRD -- not on ARMv4: write-back and two internal cycles, no load (U21, C2). pub(crate) fn arm_ldrd(&mut self, op: u32) { self.doubleword_encoding(op, 2); } /// STRD -- not on ARMv4: write-back and one internal cycle, no store (U21, C2). pub(crate) fn arm_strd(&mut self, op: u32) { self.doubleword_encoding(op, 1); } // ------------------------------------------------------------------ Thumb #[inline] fn thumb_load(&mut self, rd: usize, address: u32, kind: u32) { self.r[15] = self.r[15].wrapping_add(2); let value = self.load(address, kind); self.idle(self.r[15]); // I cycle at pc+3L self.r[rd] = value; self.next_fetch_seq = true; // merged IS (C1) } #[inline] fn thumb_store(&mut self, rd: usize, address: u32, width: u32) { self.r[15] = self.r[15].wrapping_add(2); self.store(address, width, self.r[rd]); self.next_fetch_seq = false; } #[inline] fn reg_address(&self, op: u32) -> u32 { self.r[((op >> 3) & 7) as usize].wrapping_add(self.r[((op >> 6) & 7) as usize]) } /// LDR Rd,[PC,#Imm] -- format 6: address (PC AND NOT 2) + Word8 x 4 (DS 5.6). pub(crate) fn thumb_ldr_pc(&mut self, op: u32) { let address = (self.r[15] & !2).wrapping_add((op & 0xFF) << 2); self.thumb_load(((op >> 8) & 7) as usize, address, WORD); } /// STR Rd,[Rb,Ro] -- format 7 (DS 5.7). pub(crate) fn thumb_str_reg(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.reg_address(op), 32); } /// STRB Rd,[Rb,Ro] -- format 7 (DS 5.7). pub(crate) fn thumb_strb_reg(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.reg_address(op), 8); } /// LDR Rd,[Rb,Ro] -- format 7; misaligned rotates (DS 5.7, U1). pub(crate) fn thumb_ldr_reg(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.reg_address(op), WORD); } /// LDRB Rd,[Rb,Ro] -- format 7 (DS 5.7). pub(crate) fn thumb_ldrb_reg(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.reg_address(op), BYTE); } /// STRH Rd,[Rb,Ro] -- format 8 (DS 5.8). pub(crate) fn thumb_strh_reg(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.reg_address(op), 16); } /// LDRH Rd,[Rb,Ro] -- format 8; misaligned rotates by 8 (DS 5.8, U2). pub(crate) fn thumb_ldrh_reg(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.reg_address(op), HALF); } /// LDSB Rd,[Rb,Ro] -- format 8, LDRSB (DS 5.8). pub(crate) fn thumb_ldsb_reg(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.reg_address(op), SIGNED_BYTE); } /// LDSH Rd,[Rb,Ro] -- format 8, LDRSH; misaligned is the signed byte (DS 5.8, C4). pub(crate) fn thumb_ldsh_reg(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.reg_address(op), SIGNED_HALF); } #[inline] fn imm_address(&self, op: u32, scale: u32) -> u32 { self.r[((op >> 3) & 7) as usize].wrapping_add(((op >> 6) & 0x1F) * scale) } /// STR Rd,[Rb,#Imm] -- format 9, offset x 4 (DS 5.9). pub(crate) fn thumb_str_imm(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.imm_address(op, 4), 32); } /// LDR Rd,[Rb,#Imm] -- format 9, offset x 4 (DS 5.9, U1). pub(crate) fn thumb_ldr_imm(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.imm_address(op, 4), WORD); } /// STRB Rd,[Rb,#Imm] -- format 9 (DS 5.9). pub(crate) fn thumb_strb_imm(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.imm_address(op, 1), 8); } /// LDRB Rd,[Rb,#Imm] -- format 9 (DS 5.9). pub(crate) fn thumb_ldrb_imm(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.imm_address(op, 1), BYTE); } /// STRH Rd,[Rb,#Imm] -- format 10, offset x 2 (DS 5.10). pub(crate) fn thumb_strh_imm(&mut self, op: u32) { self.thumb_store((op & 7) as usize, self.imm_address(op, 2), 16); } /// LDRH Rd,[Rb,#Imm] -- format 10, offset x 2 (DS 5.10, U2). pub(crate) fn thumb_ldrh_imm(&mut self, op: u32) { self.thumb_load((op & 7) as usize, self.imm_address(op, 2), HALF); } /// STR Rd,[SP,#Imm] -- format 11, Word8 x 4 (DS 5.11). pub(crate) fn thumb_str_sp(&mut self, op: u32) { self.thumb_store( ((op >> 8) & 7) as usize, self.r[13].wrapping_add((op & 0xFF) << 2), 32, ); } /// LDR Rd,[SP,#Imm] -- format 11, Word8 x 4 (DS 5.11, U1). pub(crate) fn thumb_ldr_sp(&mut self, op: u32) { self.thumb_load( ((op >> 8) & 7) as usize, self.r[13].wrapping_add((op & 0xFF) << 2), WORD, ); } }