# SPDX-License-Identifier: MIT """Block data transfers: LDM, STM and Thumb's PUSH, POP, LDMIA and STMIA (DS 4.11, 5.14, 5.15). 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"). """ from __future__ import annotations from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import BANK_FIQ, BANK_INVALID, BANK_USER, bank_of class BlockMixin(CoreMixin): # ------------------------------------------------------------------ the User bank def _banked_here(self, n: int) -> bool: """Whether Rn of the current mode is a banked copy, not the User bank's register.""" bank = bank_of(self.cpsr) if bank == BANK_FIQ: return 8 <= n <= 14 return bank not in (BANK_USER, BANK_INVALID) and 13 <= n <= 14 def _user_reg(self, n: int) -> int: """Rn of the User bank, whatever the current mode (DS 4.11.4).""" if self._banked_here(n): return self._bank[BANK_USER][n - 8] return self.r[n] def _set_user_reg(self, n: int, value: int) -> None: if self._banked_here(n): self._bank[BANK_USER][n - 8] = value & MASK32 else: self.r[n] = value & MASK32 def _reg_or_user(self, n: int) -> int: """``_reg`` for the instruction after an LDM^ (U7): a banked register reads OR'd with the User bank's.""" value = self.r[n] if self._banked_here(n): value |= self._bank[BANK_USER][n - 8] return value # ------------------------------------------------------------------ ARM def _block_transfer(self, op: int, load: bool) -> None: rn = (op >> 16) & 0xF registers = [n for n in range(16) if op >> n & 1] size = 4 * len(registers) if registers else 0x40 if not registers: registers = [15] base = self._reg(rn) self.r[15] = pc = (self.r[15] + 4) & MASK32 up, pre = op & (1 << 23), op & (1 << 24) if up: address = base + 4 if pre else base new_base = base + size else: address = base - size + 4 if not pre else base - size new_base = base - size address &= MASK32 new_base &= MASK32 write_back = bool(op & (1 << 21)) s_bit = bool(op & (1 << 22)) user_bank = s_bit and not (load and 15 in registers) # A User bank transfer writes the base back to the User bank's Rn too ("should not be # used", GBATEK; OBSERVED, corpus v1). set_base = self._set_user_reg if user_bank else self._set_reg seq = False for index, n in enumerate(registers): if load: value = self._read(address, 32, seq) if index == 0 and write_back: set_base(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). value = self._user_reg(n) if user_bank else self.r[n] self._write(address, 32, value, seq) if index == 0 and write_back: set_base(rn, new_base) address = (address + 4) & MASK32 seq = True if not 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 15 in registers: if s_bit: self._restore_cpsr_from_spsr() self._refill() elif user_bank: self._glitch_next = True # U7 def _arm_ldm(self, op: int) -> None: """LDM -- load Rlist from consecutive words; ^ restores the CPSR or loads User registers (DS 4.11, U7).""" self._block_transfer(op, load=True) def _arm_stm(self, op: int) -> None: """STM -- store Rlist to consecutive words; ^ stores User registers (DS 4.11).""" self._block_transfer(op, load=False) # ------------------------------------------------------------------ Thumb def _block_thumb( self, rb: int, registers: list[int], load: bool, push: bool, pc_bit0: bool = True ) -> None: """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").""" size = 4 * len(registers) if registers else 0x40 if not registers: registers = [15] base = self.r[rb] self.r[15] = pc = (self.r[15] + 2) & MASK32 new_base = (base - size if push else base + size) & MASK32 address = new_base if push else base seq = False for index, n in enumerate(registers): if load: value = self._read(address, 32, seq) if index == 0: self.r[rb] = new_base self.r[n] = value & MASK32 else: self._write(address, 32, self.r[n], seq) if index == 0: self.r[rb] = new_base address = (address + 4) & MASK32 seq = True if not 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 15 in registers: if not pc_bit0: self.r[15] &= ~1 self._refill() # Thumb state is kept (DS 5.14) def _thumb_push(self, op: int) -> None: """PUSH {Rlist}/PUSH {Rlist, LR} -- store to the full descending stack at SP (DS 5.14, table 6-13).""" registers = [n for n in range(8) if op >> n & 1] if op & 0x100: registers.append(14) self._block_thumb(13, registers, load=False, push=True) def _thumb_pop(self, op: int) -> None: """POP {Rlist}/POP {Rlist, PC} -- load from the full descending stack at SP; PC stays in Thumb state (DS 5.14, table 6-12).""" registers = [n for n in range(8) if op >> n & 1] if op & 0x100: registers.append(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, load=True, push=False, pc_bit0=not op & 0x100) def _thumb_stmia(self, op: int) -> None: """STMIA Rb!,{Rlist} -- store, increment after, write back (DS 5.15, table 6-13).""" self._block_thumb((op >> 8) & 7, [n for n in range(8) if op >> n & 1], False, False) def _thumb_ldmia(self, op: int) -> None: """LDMIA Rb!,{Rlist} -- load, increment after, write back unless Rb is loaded (DS 5.15, table 6-12).""" self._block_thumb((op >> 8) & 7, [n for n in range(8) if op >> n & 1], True, False)