"""Single data transfers: LDR, STR, LDRB, STRB, LDRH, STRH, LDRSB, LDRSH and Thumb formats 6-11. 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). """ from __future__ import annotations from arm7tdmi_python._alu import ror32, shift_by_immediate from arm7tdmi_python._core import MASK32, CoreMixin WORD, BYTE, HALF, SIGNED_BYTE, SIGNED_HALF = range(5) def sign_extend_from(value: int, bits: int) -> int: return (value - (1 << bits)) & MASK32 if value >> (bits - 1) & 1 else value class TransferMixin(CoreMixin): def _load(self, address: int, kind: int) -> int: """One N data read of ``kind`` at ``address``, as the register receives it.""" address &= MASK32 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), (address & 3) * 8) if kind == BYTE: return self._read(address, 8) if kind == SIGNED_BYTE: return sign_extend_from(self._read(address, 8), 8) value = self._read(address, 16) if kind == HALF: # A misaligned halfword is rotated right by 8 within the 32-bit result (U2). return ror32(value, 8) if address & 1 else value # A misaligned signed halfword is the sign-extended byte at that address, read with # the instruction's width 16 (U2, C4). return sign_extend_from(value >> 8, 8) if address & 1 else sign_extend_from(value, 16) def _store(self, address: int, width: int, value: int) -> None: self._write(address, width, value) # ------------------------------------------------------------------ ARM def _single_address(self, op: int, offset: int) -> tuple[int, int, int]: """(transfer address, base register, signed offset) of an ARM transfer; advances the PC past the address cycle (DS 4.9.1).""" rn = (op >> 16) & 0xF base = self._reg(rn) self.r[15] = (self.r[15] + 4) & MASK32 if not op & (1 << 23): offset = -offset address = base + offset if op & (1 << 24) else base return address & MASK32, rn, offset def _write_back(self, op: int, rn: int, offset: int) -> bool: """Write-back (W set, or always when post-indexed); returns whether R15 was written.""" if op & (1 << 21) or not op & (1 << 24): self._set_reg(rn, (self._reg(rn) + offset) & MASK32) return rn == 15 return False def _arm_load(self, op: int, offset: int, kind: int) -> None: rd = (op >> 12) & 0xF address, rn, offset = self._single_address(op, offset) user = not op & (1 << 24) and op & (1 << 21) # LDRT: nTRANS low for the data cycle self._user_access = bool(user) value = self._load(address, kind) self._user_access = False 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 or pc_written: self._refill() def _arm_store(self, op: int, offset: int, width: int) -> None: rd = (op >> 12) & 0xF address, rn, offset = self._single_address(op, offset) user = not op & (1 << 24) and op & (1 << 21) # STRT self._user_access = bool(user) self._store(address, width, self._reg(rd)) # R15 stores pc+12 (U8) self._user_access = False 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() def _single_offset(self, op: int) -> int: """The 12-bit immediate, or Rm shifted by an immediate (DS 4.9.1; never by a register).""" if not op & (1 << 25): return op & 0xFFF value, _ = shift_by_immediate( (op >> 5) & 3, self._reg(op & 0xF), (op >> 7) & 0x1F, self._carry() ) return value def _arm_ldr(self, op: int) -> None: """LDR -- Rd = word at the address, rotated if misaligned; T form is LDRT (DS 4.9, U1).""" self._arm_load(op, self._single_offset(op), WORD) def _arm_ldrb(self, op: int) -> None: """LDRB -- Rd = byte at the address, zero-extended; T form is LDRBT (DS 4.9).""" self._arm_load(op, self._single_offset(op), BYTE) def _arm_str(self, op: int) -> None: """STR -- word at the address = Rd; T form is STRT (DS 4.9, U3, U8).""" self._arm_store(op, self._single_offset(op), 32) def _arm_strb(self, op: int) -> None: """STRB -- byte at the address = Rd[7:0]; T form is STRBT (DS 4.9).""" self._arm_store(op, self._single_offset(op), 8) def _halfword_offset(self, op: int) -> int: """An 8-bit immediate split across bits 11:8 and 3:0, or Rm (DS 4.10).""" if op & (1 << 22): return (op & 0xF) | ((op >> 4) & 0xF0) return self._reg(op & 0xF) def _arm_ldrh(self, op: int) -> None: """LDRH -- Rd = halfword, zero-extended; misaligned rotates by 8 (DS 4.10, U2).""" self._arm_load(op, self._halfword_offset(op), HALF) def _arm_ldrsb(self, op: int) -> None: """LDRSB -- Rd = byte, sign-extended (DS 4.10).""" self._arm_load(op, self._halfword_offset(op), SIGNED_BYTE) def _arm_ldrsh(self, op: int) -> None: """LDRSH -- Rd = halfword, sign-extended; misaligned is the signed byte (DS 4.10, C4).""" self._arm_load(op, self._halfword_offset(op), SIGNED_HALF) def _arm_strh(self, op: int) -> None: """STRH -- halfword at the address = Rd[15:0] (DS 4.10).""" self._arm_store(op, self._halfword_offset(op), 16) def _arm_ldrd(self, op: int) -> None: """LDRD -- ARMv5's encoding (SH = 10, L = 0): on the ARM7TDMI no transfer, two I cycles around the write-back; NBA's model of the ARM7TDMI-S, no hardware evidence (U21).""" address, rn, offset = self._single_address(op, self._halfword_offset(op)) self._idle(address) pc_written = self._write_back(op, rn, offset) self._idle(self.r[15]) self.next_fetch_seq = True if pc_written: self._refill() def _arm_strd(self, op: int) -> None: """STRD -- ARMv5's encoding (SH = 11, L = 0): on the ARM7TDMI no transfer, one I cycle and the write-back; NBA's model, no hardware evidence (U21).""" address, rn, offset = self._single_address(op, self._halfword_offset(op)) self._idle(address) pc_written = self._write_back(op, rn, offset) self.next_fetch_seq = True if pc_written: self._refill() # ------------------------------------------------------------------ Thumb def _thumb_load(self, rd: int, address: int, kind: int) -> None: self.r[15] = (self.r[15] + 2) & MASK32 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) def _thumb_store(self, rd: int, address: int, width: int) -> None: self.r[15] = (self.r[15] + 2) & MASK32 self._store(address & MASK32, width, self.r[rd]) self.next_fetch_seq = False def _reg_address(self, op: int) -> int: return self.r[(op >> 3) & 7] + self.r[(op >> 6) & 7] def _thumb_ldr_pc(self, op: int) -> None: """LDR Rd,[PC,#Imm] -- format 6: address (PC AND NOT 2) + Word8 x 4 (DS 5.6).""" address = (self.r[15] & ~2) + ((op & 0xFF) << 2) self._thumb_load((op >> 8) & 7, address, WORD) def _thumb_str_reg(self, op: int) -> None: """STR Rd,[Rb,Ro] -- format 7 (DS 5.7).""" self._thumb_store(op & 7, self._reg_address(op), 32) def _thumb_strb_reg(self, op: int) -> None: """STRB Rd,[Rb,Ro] -- format 7 (DS 5.7).""" self._thumb_store(op & 7, self._reg_address(op), 8) def _thumb_ldr_reg(self, op: int) -> None: """LDR Rd,[Rb,Ro] -- format 7; misaligned rotates (DS 5.7, U1).""" self._thumb_load(op & 7, self._reg_address(op), WORD) def _thumb_ldrb_reg(self, op: int) -> None: """LDRB Rd,[Rb,Ro] -- format 7 (DS 5.7).""" self._thumb_load(op & 7, self._reg_address(op), BYTE) def _thumb_strh_reg(self, op: int) -> None: """STRH Rd,[Rb,Ro] -- format 8 (DS 5.8).""" self._thumb_store(op & 7, self._reg_address(op), 16) def _thumb_ldrh_reg(self, op: int) -> None: """LDRH Rd,[Rb,Ro] -- format 8; misaligned rotates by 8 (DS 5.8, U2).""" self._thumb_load(op & 7, self._reg_address(op), HALF) def _thumb_ldsb_reg(self, op: int) -> None: """LDSB Rd,[Rb,Ro] -- format 8, LDRSB (DS 5.8).""" self._thumb_load(op & 7, self._reg_address(op), SIGNED_BYTE) def _thumb_ldsh_reg(self, op: int) -> None: """LDSH Rd,[Rb,Ro] -- format 8, LDRSH; misaligned is the signed byte (DS 5.8, C4).""" self._thumb_load(op & 7, self._reg_address(op), SIGNED_HALF) def _imm_address(self, op: int, scale: int) -> int: return self.r[(op >> 3) & 7] + ((op >> 6) & 0x1F) * scale def _thumb_str_imm(self, op: int) -> None: """STR Rd,[Rb,#Imm] -- format 9, offset x 4 (DS 5.9).""" self._thumb_store(op & 7, self._imm_address(op, 4), 32) def _thumb_ldr_imm(self, op: int) -> None: """LDR Rd,[Rb,#Imm] -- format 9, offset x 4 (DS 5.9, U1).""" self._thumb_load(op & 7, self._imm_address(op, 4), WORD) def _thumb_strb_imm(self, op: int) -> None: """STRB Rd,[Rb,#Imm] -- format 9 (DS 5.9).""" self._thumb_store(op & 7, self._imm_address(op, 1), 8) def _thumb_ldrb_imm(self, op: int) -> None: """LDRB Rd,[Rb,#Imm] -- format 9 (DS 5.9).""" self._thumb_load(op & 7, self._imm_address(op, 1), BYTE) def _thumb_strh_imm(self, op: int) -> None: """STRH Rd,[Rb,#Imm] -- format 10, offset x 2 (DS 5.10).""" self._thumb_store(op & 7, self._imm_address(op, 2), 16) def _thumb_ldrh_imm(self, op: int) -> None: """LDRH Rd,[Rb,#Imm] -- format 10, offset x 2 (DS 5.10, U2).""" self._thumb_load(op & 7, self._imm_address(op, 2), HALF) def _thumb_str_sp(self, op: int) -> None: """STR Rd,[SP,#Imm] -- format 11, Word8 x 4 (DS 5.11).""" self._thumb_store((op >> 8) & 7, self.r[13] + ((op & 0xFF) << 2), 32) def _thumb_ldr_sp(self, op: int) -> None: """LDR Rd,[SP,#Imm] -- format 11, Word8 x 4 (DS 5.11, U1).""" self._thumb_load((op >> 8) & 7, self.r[13] + ((op & 0xFF) << 2), WORD)