# SPDX-License-Identifier: MIT """U16: an MSR that sets T leaves two ARM words in the pipeline (arm7tdmi-rust's report). ARM's documents forbid it (DS 3.8, DS 4.6, TRM 2.7.2: unpredictable), so what the silicon does is unknown; the core's rule is a HYPOTHESIS (docs/undocumented-behavior.md U16): each prefetched word is decoded as its bits 15:0, the halfword a Thumb fetch at that word's address would take from the bus (TRM table 3-7, little-endian). Before the rule the next step raised IndexError on ``_thumb_table[op >> 6]``. """ from __future__ import annotations from arm7tdmi_python import ARM7TDMI, CODE, CPUState BASE = 0x08000000 MSR_CPSR_FC_R0 = 0xE129F000 # MSR CPSR_fc, r0 def test_the_prefetched_arm_words_run_as_their_low_halfwords(): memory = { BASE + 4: 0xDEAD2001, # bits 15:0: MOVS r0, #1 BASE + 8: 0xBEEF3102, # bits 15:0: ADDS r1, #2 BASE + 12: 0x46C02205, # MOVS r2, #5 at +12, then MOV r8, r8 at +14 (little-endian) BASE + 16: 0x46C046C0, } log = [] def read(address, width, access): log.append((address, width, access & CODE)) word = memory.get(address & ~3, 0) return (word >> (8 * (address & 2))) & 0xFFFF if width == 16 else word cpu = ARM7TDMI(read, lambda *a: None) regs = [0] * 16 regs[0] = 0x33 # Supervisor, T set regs[15] = BASE + 8 pipeline = [MSR_CPSR_FC_R0, memory[BASE + 4]] cpu.restore_state(CPUState(registers=regs, cpsr=0x13, pipeline=pipeline)) cpu.step() # the MSR: its cycle-1 fetch is still ARM's word at +8 assert cpu.cpsr == 0x33 and cpu.pipeline == [memory[BASE + 4], memory[BASE + 8]] for _ in range(3): cpu.step() assert (cpu.r[0], cpu.r[1], cpu.r[2]) == (1, 2, 5) # The fetches after the MSR are Thumb halfwords from R15 = +12 on (OBSERVED, corpus v1). assert log == [(BASE + 8, 32, CODE), (BASE + 12, 16, CODE), (BASE + 14, 16, CODE), (BASE + 16, 16, CODE)] # fmt: skip def test_the_report_repro_no_longer_raises(): cpu = ARM7TDMI(lambda a, w, acc: 0xE1A00000, lambda *a: None) regs = [0] * 16 regs[0] = 0x33 regs[15] = 0x08000008 cpu.restore_state(CPUState(registers=regs, cpsr=0x13, pipeline=[MSR_CPSR_FC_R0, 0xE1A00000])) cpu.step() cpu.step() # bits 15:0 of MOV r0, r0 (ARM): H'0000, LSLS r0, r0, #0 assert cpu.cpsr & 0x20 and cpu.r[15] == 0x0800000E