# SPDX-License-Identifier: MIT """Rung 0: every instruction class's bus schedule equals docs/timing.md (TRM chapter 6). Each case below is one row group of a timing.md table, written as (type, address, width) per cycle with the table's symbols: ``pc`` the instruction's address, ``alu`` the computed address or target, ``pc'`` a value loaded into R15, ``Xn`` a vector. Type is N, S, I or C as converted in timing.md (each row carries its own type), and ``next`` is the type of the following instruction's cycle 1, the core's ``next_fetch_seq``. Cycle 1 ("prev") is S here because each case starts with ``next_fetch_seq`` set. This is manufacturer documentation, the TRM's; where the core follows the TRM against NBA and the corpus (merged IS, C1; the undefined trap's I cycle, C5) these tables are the judge. """ from __future__ import annotations import pytest from arm7tdmi_python import ARM7TDMI, CODE, LOCK, SEQ, CPUState PC_ARM = 0x08000100 PC_THUMB = 0x08000200 SYS, THUMB = 0x1F, 0x20 def run(op: int, regs: dict[int, int] | None = None, *, thumb: bool = False, cpsr: int = SYS, memory: dict[int, int] | None = None, irq: bool = False): # fmt: skip """Run one instruction (or an IRQ entry); return its cycles and the next fetch's type.""" memory = memory or {} log: list[tuple[str, int, int | None, int]] = [] cpu: ARM7TDMI def read(address: int, width: int, access: int) -> int: log.append(("S" if access & SEQ else "N", address, width, access)) return memory.get(address, 0) def write(address: int, width: int, value: int, access: int) -> None: log.append(("S" if access & SEQ else "N", address, width, access)) def idle(address: int) -> None: log.append(("C" if cpu.coprocessor_cycle else "I", address, None, 0)) cpu = ARM7TDMI(read, write, idle) pc = PC_THUMB if thumb else PC_ARM registers = [0] * 16 for n, value in (regs or {}).items(): registers[n] = value registers[15] = pc + (4 if thumb else 8) cpu.restore_state( CPUState( registers=registers, cpsr=cpsr | (THUMB if thumb else 0), pipeline=[op, 0], next_fetch_seq=True, ) ) cpu.irq = irq cycles = cpu.step() assert cycles == len(log) return log, "S" if cpu.next_fetch_seq else "N" def cycles(log): return [(kind, address, width) for kind, address, width, _ in log] A = PC_ARM T = PC_THUMB ALU = 0x03000040 # a data address TARGET = 0x08000400 # a branch target / loaded PC ARM_CASES = { # Table 6-1: B and BL: prefetch, N at alu, S at alu+4; next S. "B": (0xEA00003E, {}, [("S", A + 8, 32), ("N", A + 8 + 0xF8, 32), ("S", A + 12 + 0xF8, 32)], "S"), "BL": (0xEB00003E, {}, [("S", A + 8, 32), ("N", A + 8 + 0xF8, 32), ("S", A + 12 + 0xF8, 32)], "S"), # Table 6-3: BX to Thumb: new width from cycle 2. "BX to Thumb": (0xE12FFF10, {0: TARGET | 1}, [("S", A + 8, 32), ("N", TARGET, 16), ("S", TARGET + 2, 16)], "S"), "BX to ARM": (0xE12FFF10, {0: TARGET}, [("S", A + 8, 32), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), # Table 6-4: data processing. "ADD normal": (0xE0810002, {}, [("S", A + 8, 32)], "S"), "MOV pc": (0xE1A0F001, {1: TARGET}, [("S", A + 8, 32), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), "ADD shift by Rs": (0xE0810312, {}, [("S", A + 8, 32), ("I", A + 12, None)], "S"), "MOV pc shift by Rs": (0xE1A0F311, {1: TARGET}, [("S", A + 8, 32), ("I", A + 12, None), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), "MRS": (0xE10F0000, {}, [("S", A + 8, 32)], "S"), "MSR": (0xE129F000, {0: SYS}, [("S", A + 8, 32)], "S"), # Table 6-9: loads. S+N+I; next S at pc+12 (merged IS). "LDR": (0xE5910000, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("I", A + 12, None)], "S"), "LDRB": (0xE5D10000, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 8), ("I", A + 12, None)], "S"), "LDRH": (0xE1D100B0, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 16), ("I", A + 12, None)], "S"), "LDRSB": (0xE1D100D0, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 8), ("I", A + 12, None)], "S"), "LDR pc": (0xE591F000, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("I", A + 12, None), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), # Table 6-11: stores. S+N; next N. "STR": (0xE5810000, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32)], "N"), "STRB": (0xE5C10000, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 8)], "N"), "STRH": (0xE1C100B0, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 16)], "N"), # Table 6-12: LDM. N then S per register, I at pc+12; with PC, then N, S. "LDM 1": (0xE8910001, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("I", A + 12, None)], "S"), "LDM 3": (0xE8910007, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("S", ALU + 4, 32), ("S", ALU + 8, 32), ("I", A + 12, None)], "S"), "LDM with pc": (0xE8918001, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("S", ALU + 4, 32), ("I", A + 12, None), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), "LDMDB": (0xE9110003, {1: ALU}, [("S", A + 8, 32), ("N", ALU - 8, 32), ("S", ALU - 4, 32), ("I", A + 12, None)], "S"), # Table 6-13: STM. (n-1)S+2N; next N. "STM 3": (0xE8810007, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("S", ALU + 4, 32), ("S", ALU + 8, 32)], "N"), # Table 6-14: SWP. S+2N+I, locked read and write. "SWP": (0xE1010092, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 32), ("N", ALU, 32), ("I", A + 12, None)], "S"), "SWPB": (0xE1410092, {1: ALU}, [("S", A + 8, 32), ("N", ALU, 8), ("N", ALU, 8), ("I", A + 12, None)], "S"), # Table 6-15: SWI. 2S+N at the vector. "SWI": (0xEF000000, {}, [("S", A + 8, 32), ("N", 0x08, 32), ("S", 0x0C, 32)], "S"), # Table 6-21: undefined and absent coprocessor: 2S+N+I, I at pc+8 (C5). "undefined": (0xE7F000F0, {}, [("S", A + 8, 32), ("I", A + 8, None), ("N", 0x04, 32), ("S", 0x08, 32)], "S"), "CDP": (0xEE000000, {}, [("S", A + 8, 32), ("I", A + 8, None), ("N", 0x04, 32), ("S", 0x08, 32)], "S"), "LDC": (0xED900000, {}, [("S", A + 8, 32), ("I", A + 8, None), ("N", 0x04, 32), ("S", 0x08, 32)], "S"), "MRC p15": (0xEE100F10, {}, [("S", A + 8, 32), ("I", A + 8, None), ("N", 0x04, 32), ("S", 0x08, 32)], "S"), # Tables 6-19, 6-20: CP14 register transfers (U17). "MRC p14": (0xEE100E10, {}, [("S", A + 8, 32), ("C", A + 12, None), ("I", A + 12, None)], "S"), "MCR p14": (0xEE010E10, {}, [("S", A + 8, 32), ("C", A + 12, None)], "N"), # Table 6-22: an instruction whose condition fails. "unexecuted": (0x00810002, {}, [("S", A + 8, 32)], "S"), } # fmt: skip @pytest.mark.parametrize("name", sorted(ARM_CASES)) def test_arm_schedule(name): op, regs, expected, after = ARM_CASES[name] memory = {ALU: TARGET, ALU + 4: TARGET} log, next_type = run(op, regs, memory=memory) assert cycles(log) == expected assert next_type == after @pytest.mark.parametrize(("rs", "m"), [(0x7F, 1), (0xFFFFFF80, 1), (0x1234, 2), (0xFF001234, 3), (0x12345678, 4)]) # fmt: skip def test_multiply_schedules(rs, m): """Tables 6-5 to 6-8: MUL m I cycles at pc+12; MLA one more, the first at pc+8; UMULL and SMULL m+1, the accumulating forms m+2; m by the significant bytes of Rs (TRM 6.20).""" regs = {1: 3, 2: rs} prefetch = [("S", A + 8, 32)] log, after = run(0xE0000291, regs) # MUL r0, r1, r2 assert cycles(log) == prefetch + [("I", A + 12, None)] * m and after == "S" log, _ = run(0xE0203291, regs) # MLA r0, r1, r2, r3 assert cycles(log) == prefetch + [("I", A + 8, None)] + [("I", A + 12, None)] * m log, _ = run(0xE0C54291, regs) # SMULL r4, r5, r1, r2 assert cycles(log) == prefetch + [("I", A + 12, None)] * (m + 1) log, _ = run(0xE0E54291, regs) # SMLAL r4, r5, r1, r2 assert cycles(log) == prefetch + [("I", A + 12, None)] * (m + 2) def test_umull_terminates_only_on_zeros(): """UMULL stops early only when the upper bits are zero (DS 4.8.3, U11).""" log, _ = run(0xE0854291, {1: 3, 2: 0xFFFFFF80}) # UMULL r4, r5, r1, r2 assert len(log) == 1 + 5 THUMB_CASES = { "LSL imm": (0x0048, {}, [("S", T + 4, 16)], "S"), "LSL by register": (0x4088, {}, [("S", T + 4, 16), ("I", T + 6, None)], "S"), "LDR imm": (0x6808, {1: ALU}, [("S", T + 4, 16), ("N", ALU, 32), ("I", T + 6, None)], "S"), "STR imm": (0x6008, {1: ALU}, [("S", T + 4, 16), ("N", ALU, 32)], "N"), "LDRH imm": (0x8808, {1: ALU}, [("S", T + 4, 16), ("N", ALU, 16), ("I", T + 6, None)], "S"), "B": (0xE010, {}, [("S", T + 4, 16), ("N", T + 4 + 0x20, 16), ("S", T + 6 + 0x20, 16)], "S"), "BEQ not taken": (0xD010, {}, [("S", T + 4, 16)], "S"), "BL prefix": (0xF000, {}, [("S", T + 4, 16)], "S"), "BX to ARM": (0x4708, {1: TARGET}, [("S", T + 4, 16), ("N", TARGET, 32), ("S", TARGET + 4, 32)], "S"), "MOV pc": (0x468F, {1: TARGET}, [("S", T + 4, 16), ("N", TARGET, 16), ("S", TARGET + 2, 16)], "S"), "PUSH 2": (0xB403, {13: ALU}, [("S", T + 4, 16), ("N", ALU - 8, 32), ("S", ALU - 4, 32)], "N"), "POP 2": (0xBC03, {13: ALU}, [("S", T + 4, 16), ("N", ALU, 32), ("S", ALU + 4, 32), ("I", T + 6, None)], "S"), "POP pc": (0xBD00, {13: ALU}, [("S", T + 4, 16), ("N", ALU, 32), ("I", T + 6, None), ("N", TARGET, 16), ("S", TARGET + 2, 16)], "S"), "SWI": (0xDF00, {}, [("S", T + 4, 16), ("N", 0x08, 32), ("S", 0x0C, 32)], "S"), "undefined DExx": (0xDE00, {}, [("S", T + 4, 16), ("I", T + 4, None), ("N", 0x04, 32), ("S", 0x08, 32)], "S"), } # fmt: skip @pytest.mark.parametrize("name", sorted(THUMB_CASES)) def test_thumb_schedule(name): op, regs, expected, after = THUMB_CASES[name] log, next_type = run(op, regs, thumb=True, memory={ALU: TARGET}) assert cycles(log) == expected assert next_type == after def test_thumb_bl_suffix(): """Table 6-2: the second half is 2S+N: its prefetch at pc+6 (pc of the first half), then the refill at LR + offset.""" log, _ = run(0xF810, {14: TARGET}, thumb=True) assert cycles(log) == [("S", T + 4, 16), ("N", TARGET + 0x20, 16), ("S", TARGET + 0x22, 16)] @pytest.mark.parametrize("thumb", [False, True]) def test_irq_entry(thumb): """Table 6-15 for IRQ: cycle 1 fetches (discarded), then N at 0x18, S at 0x1C.""" log, after = run(0, thumb=thumb, cpsr=SYS, irq=True) first = ("S", T + 4, 16) if thumb else ("S", A + 8, 32) assert cycles(log) == [first, ("N", 0x18, 32), ("S", 0x1C, 32)] and after == "S" @pytest.mark.parametrize("line", ["irq", "fiq"]) def test_entry_masks_the_still_high_line(line): """After IRQ (FIQ) entry the line is still high, but the entry set I (and F): the next step runs the instruction at the vector, not a second entry (U14, TRM 2.8.4-2.8.5).""" vector = 0x18 if line == "irq" else 0x1C fetched: list[int] = [] def read(address: int, width: int, access: int) -> int: fetched.append(address) return 0xE1A00000 # MOV R0, R0 cpu = ARM7TDMI(read, lambda *a: None, lambda a: None) registers = [0] * 16 registers[15] = PC_ARM + 8 cpu.restore_state(CPUState(registers=registers, cpsr=SYS, pipeline=[0xE1A00000] * 2, next_fetch_seq=True)) # fmt: skip setattr(cpu, line, True) cpu.step() assert cpu.pending_interrupt() is None fetched.clear() cpu.step() # the vector's first instruction: one fetch, two words past it assert fetched == [vector + 8] and cpu.cpsr & 0x1F == (0x12 if line == "irq" else 0x11) @pytest.mark.parametrize( ("cpsr", "msr", "taken"), [ (SYS | 0x80, 0xE321F01F, True), # clears I: the IRQ is taken right after it (SYS, 0xE321F09F, True), # sets I: still taken right after it (one instruction late) (SYS | 0x80, 0xE321F09F, False), # I set before and after: masked ], ) def test_irq_mask_change(cpsr, msr, taken): """U14: a line is masked only if I was set when the instruction started and is still set when it ends (TEST ROM: alyosha-tas psr for setting; irq/IRQ_sub and IRQ_sub_slow with Nintendo's BIOS for clearing).""" def read(address: int, width: int, access: int) -> int: cpu.irq = True # the line rises during the MSR's first cycle return 0xE1A00000 # MOV R0, R0 cpu = ARM7TDMI(read, lambda *a: None, lambda a: None) registers = [0] * 16 registers[15] = PC_ARM + 8 cpu.restore_state(CPUState(registers=registers, cpsr=cpsr, pipeline=[msr, 0xE1A00000], next_fetch_seq=True)) # fmt: skip cpu.step() # the MSR assert (cpu.pending_interrupt() == 0x12) is taken cpu.step() assert (cpu.cpsr & 0x1F == 0x12) is taken def test_access_bits(): """Fetches carry CODE; SWP's read and write carry LOCK (TRM table 6-14, C10).""" log, _ = run(0xE1010092, {1: ALU}) assert [access for *_, access in log] == [CODE | SEQ, LOCK, LOCK, 0]