### module docstring """The core's machinery: registers and banks, the bus, the pipeline, exceptions, ``step()``. Every instruction handler in the family modules builds on these. The rules they encode come from the TRM (ARM DDI 0029G) and the data sheet (DS, ARM DDI 0029E); where the documents are silent the behaviour is NanoBoyAdvance's, which the SingleStepTests corpus was generated from, and the docs item that says so is cited. **R15 during an instruction.** As in the TRM's pipeline (DS 3.4), when an instruction starts executing R15 holds its address plus 2L (L = 4 in ARM state, 2 in Thumb): that is the address of cycle 1's prefetch. Handlers advance R15 by L at the point the hardware's PC advances, which is what makes R15 read as plus 12 after the first cycle (U8, U9, U12). """ ### CoreMixin._reg def _reg(self, n: int) -> int: """Rn as an ARM-state instruction reads it. For two cycles after a user-bank LDM^ in a privileged mode, a banked register reads as the OR of its own and the user copy (U7, the LDM^ bus glitch). """ value = self.r[n] if self._conflict_cycles and 8 <= n < 15: value |= self._bank[BANK_USER][n - 8] return value ### CoreMixin._set_reg def _set_reg(self, n: int, value: int) -> None: """Rn <- value. In an invalid mode the banked registers ignore writes (U15); in the LDM^ glitch window the user copy is written too (U7).""" banked = 8 <= n < 15 if self._conflict_cycles and banked: self._bank[BANK_USER][n - 8] = value if not self._mode_invalid or not banked: self.r[n] = value ### CoreMixin._switch_mode def _switch_mode(self, new_mode: int) -> None: """Change CPSR[4:0] and swap the banked registers (TRM 2.6, figure 2-3). A mode value that names no mode reads R8-R14 as zero and ignores writes to them until a valid mode returns; this is NBA's model, not a measurement (U15). """ old = bank_of(self.cpsr) new = bank_of(new_mode) self.cpsr = (self.cpsr & ~MODE_BITS) | (new_mode & MODE_BITS) self._spsr_bank = None if new == BANK_USER else new if old == new: return r, bank = self.r, self._bank if old == BANK_FIQ: bank[BANK_FIQ][0:5] = r[8:13] r[8:13] = bank[BANK_USER][0:5] elif new == BANK_FIQ: bank[BANK_USER][0:5] = r[8:13] r[8:13] = bank[BANK_FIQ][0:5] bank[old][5] = r[13] bank[old][6] = r[14] if new != BANK_INVALID: r[13] = bank[new][5] r[14] = bank[new][6] self._mode_invalid = False else: r[8:15] = [0] * 7 self.spsr[BANK_INVALID] = 0 self._mode_invalid = True ### CoreMixin._get_spsr def _get_spsr(self) -> int: """The current mode's SPSR. Bit 4 always reads 1 on the ARM7TDMI (U15). In User and System mode there is no SPSR and the CPSR is read instead (U13, U16).""" value = (self.cpsr if self._spsr_bank is None else self.spsr[self._spsr_bank]) | 0x10 if self._conflict_cycles: value |= self.cpsr # NBA's theory for the LDM^ glitch window (U7) return value ### CoreMixin._refill def _refill(self) -> None: """Flush the pipeline after a write to R15: N at the target, S at target+L (DS 3.4). The fetch addresses have R15's low bits cleared (bits 1:0 in ARM state, bit 0 in Thumb: C3); R15 itself keeps them (U20). The refill also latches the new I and F masks (U14). """ if self.cpsr & THUMB: target = self.r[15] & ~1 self.pipeline[0] = self._fetch(target, 16, False) self.pipeline[1] = self._fetch((target + 2) & MASK32, 16, True) self.r[15] = (self.r[15] + 4) & MASK32 else: target = self.r[15] & ~3 self.pipeline[0] = self._fetch(target, 32, False) self.pipeline[1] = self._fetch((target + 4) & MASK32, 32, True) self.r[15] = (self.r[15] + 8) & MASK32 self.next_fetch_seq = True self._irq_masked = bool(self.cpsr & MASK_I) self._fiq_masked = bool(self.cpsr & MASK_F) ### CoreMixin._execute_arm def _execute_arm(self) -> None: op = self.pipeline[0] self.r[15] &= ~1 # NBA clears bit 0 of R15 at every instruction (U20) self.pipeline[0] = self.pipeline[1] self.pipeline[1] = self._fetch(self.r[15] & ~3, 32, self.next_fetch_seq) if condition_passes(op >> 28, self.cpsr): self._arm_table[((op >> 16) & 0xFF0) | ((op >> 4) & 0xF)](op) else: # An instruction whose condition fails costs its prefetch only (TRM table 6-22). self.next_fetch_seq = True self.r[15] = (self.r[15] + 4) & MASK32