# SPDX-License-Identifier: MIT """The disassembler: fixed encodings, every encoding of both sets, and the core as a check. The text of each fixed case follows the data sheet's syntax (docs/disassembly.md). The whole-space tests make sure every encoding disassembles and that the disassembler names the handler the core runs; the execution tests check the addresses ours writes in comments against the addresses the core actually drives. The comparison with mGBA, MAME and objdump, which are detectors, and the round trip through GNU as are ``scripts/disasm_judge.py`` (it needs the referee build and gba-python's pinned binutils). """ from __future__ import annotations import random import pytest from arm7tdmi_python import ARM7TDMI, CODE from arm7tdmi_python._tables import ARM_HANDLERS, THUMB_HANDLERS from arm7tdmi_python.disasm import Instruction, disassemble, disassemble_arm, disassemble_thumb ARM_CASES = { 0xE0912003: "ADDS R2, R1, R3", 0x10912003: "ADDNES R2, R1, R3", 0xE12FFF1E: "BX LR", 0xE10F0000: "MRS R0, CPSR", 0xE14F0000: "MRS R0, SPSR", 0xE129F001: "MSR CPSR_fc, R1", 0xE328F20F: "MSR CPSR_f, #0xF0000000", 0xE3A004FF: "MOV R0, #0xFF000000", 0xE3A00EFF: "MOV R0, #0xFF0", 0xE3A00F3F: "MOV R0, #0x3F, 30", # 0xFC, but not the smallest rotation: carry differs 0xE3B00C3F: "MOVS R0, #0x3F00", 0xE3A0003F: "MOV R0, #0x3F", 0xE3A00E03: "MOV R0, #0x3, 28", 0xE3A00CFF: "MOV R0, #0xFF00", 0xE1A00061: "MOV R0, R1, RRX", 0xE1A00021: "MOV R0, R1, LSR #32", 0xE1A00041: "MOV R0, R1, ASR #32", 0xE1A00211: "MOV R0, R1, LSL R2", 0xE1B0F00E: "MOVS PC, LR", 0xE13FF000: "TEQP PC, R0", 0xE3500000: "CMP R0, #0x0", 0xE0010392: "MUL R1, R2, R3", 0xE0214392: "MLA R1, R2, R3, R4", 0xE0A01392: "UMLAL R1, R0, R2, R3", 0xE0D01392: "SMULLS R1, R0, R2, R3", 0xE1020091: "SWP R0, R1, [R2]", 0xE1420091: "SWPB R0, R1, [R2]", 0xE59F1004: "LDR R1, [PC, #0x4]", 0xE5901000: "LDR R1, [R0]", 0xE5101004: "LDR R1, [R0, #-0x4]", 0xE5B01004: "LDR R1, [R0, #0x4]!", 0xE4901004: "LDR R1, [R0], #0x4", 0xE4B01004: "LDRT R1, [R0], #0x4", 0xE5D01001: "LDRB R1, [R0, #0x1]", 0xE4F01001: "LDRBT R1, [R0], #0x1", 0xE7901102: "LDR R1, [R0, R2, LSL #2]", 0xE7101002: "LDR R1, [R0, -R2]", 0x05801000: "STREQ R1, [R0]", 0x05C01000: "STREQB R1, [R0]", 0xE1C020B4: "STRH R2, [R0, #0x4]", 0xE1D020F2: "LDRSH R2, [R0, #0x2]", 0xE19020D1: "LDRSB R2, [R0, R1]", 0x01D020B0: "LDREQH R2, [R0]", 0xE1C020D0: "LDRD R2, [R0]", 0xE8BD8FF0: "LDMIA SP!, {R4-R11, PC}", 0xE92D4FF0: "STMDB SP!, {R4-R11, LR}", 0xE8D08003: "LDMIA R0, {R0, R1, PC}^", 0xE9900000: "LDMIB R0, {}", 0xEB000010: "BL 0x08000048", 0xEAFFFFFE: "B 0x08000000", 0x0AFFFFFD: "BEQ 0x07FFFFFC", 0xEF000006: "SWI 0x6", 0xEF060000: "SWI 0x60000", 0xEE100E10: "MRC p14, 0, R0, c0, c0", 0xEE010E30: "MCR p14, 0, R0, c1, c0, 1", 0xEE012103: "CDP p1, 0, c2, c1, c3, 0", 0xED901104: "LDC p1, c1, [R0, #0x10]", 0xEC901104: "LDC p1, c1, [R0], #0x10", # post-indexed LDC/STC writes back only with W = 1, written "!" (DS 4.15) 0xECB01104: "LDC p1, c1, [R0], #0x10!", 0xEC201104: "STC p1, c1, [R0], #-0x10!", 0xEC901100: "LDC p1, c1, [R0], #0x0", 0xED401104: "STCL p1, c1, [R0, #-0x10]", # a post-indexed zero offset stays visible: [R1] alone is the pre-indexed encoding 0xE4912000: "LDR R2, [R1], #0x0", 0xE5912000: "LDR R2, [R1]", 0xE4B12000: "LDRT R2, [R1], #0x0", 0xE0D120B0: "LDRH R2, [R1], #0x0", 0xE6000010: "UND", 0xF5D0F000: "LDRNVB PC, [R0]", 0xE28F0010: "ADD R0, PC, #0x10", } THUMB_CASES = { 0x0000: "LSL R0, R0, #0", 0x0088: "LSL R0, R1, #2", 0x0800: "LSR R0, R0, #32", 0x1000: "ASR R0, R0, #32", 0x1888: "ADD R0, R1, R2", 0x1C08: "ADD R0, R1, #0", 0x1E48: "SUB R0, R1, #1", 0x2001: "MOV R0, #0x1", 0x28FF: "CMP R0, #0xFF", 0x4148: "ADC R0, R1", 0x4248: "NEG R0, R1", 0x4348: "MUL R0, R1", 0x4687: "MOV PC, R0", 0x4640: "MOV R0, R8", 0x460A: "MOV R2, R1", 0x4485: "ADD SP, R0", 0x4770: "BX LR", 0x4780: "BX R0", 0x4801: "LDR R0, [PC, #0x4]", 0x5088: "STR R0, [R1, R2]", 0x5E8A: "LDRSH R2, [R1, R2]", 0x568A: "LDRSB R2, [R1, R2]", 0x6048: "STR R0, [R1, #0x4]", 0x7848: "LDRB R0, [R1, #0x1]", 0x8888: "LDRH R0, [R1, #0x4]", 0x9001: "STR R0, [SP, #0x4]", 0xA001: "ADD R0, PC, #0x4", 0xA801: "ADD R0, SP, #0x4", 0xB002: "ADD SP, #0x8", 0xB082: "ADD SP, #-0x8", 0xB5F0: "PUSH {R4-R7, LR}", 0xBDF0: "POP {R4-R7, PC}", 0xB400: "PUSH {}", 0xC803: "LDMIA R0!, {R0, R1}", 0xD0FE: "BEQ 0x08000000", 0xDDFE: "BLE 0x08000000", 0xDF06: "SWI 0x6", 0xE7FE: "B 0x08000000", 0xDE00: "UND #0x0", 0xE800: "UND #0x0", 0xB100: "UND", 0xF000: "BL.HI #0x0", 0xF810: "BL.LO #0x10", } @pytest.mark.parametrize("word", sorted(ARM_CASES), ids=lambda w: f"{w:08X}") def test_arm_text(word): assert disassemble_arm(word, 0x08000000).text == ARM_CASES[word] @pytest.mark.parametrize("halfword", sorted(THUMB_CASES), ids=lambda h: f"{h:04X}") def test_thumb_text(halfword): assert disassemble_thumb(halfword, 0x08000000).text == THUMB_CASES[halfword] def test_comments_name_addresses_and_docs_items(): assert disassemble_arm(0xE59F1004, 0x08000000).comment == "=[0x0800000C]" assert disassemble_arm(0xE28F0010, 0x08000000).comment == "=0x08000018" assert disassemble_thumb(0x4801, 0x08000002).comment == "=[0x08000008]" assert disassemble_thumb(0xA001, 0x08000002).comment == "=0x08000008" assert "U21" in disassemble_arm(0xE1C020D0, 0).comment assert "U17" in disassemble_arm(0xEE100E10, 0).comment assert "undefined trap" in disassemble_arm(0xEE100110, 0).comment # MRC p1 assert "C7" in disassemble_thumb(0xDE00, 0).comment assert "C8" in disassemble_thumb(0xE800, 0).comment assert "U19" in disassemble_thumb(0x4780, 0).comment # BX with H1 = 1 assert "U19" in disassemble_thumb(0x460A, 0).comment # MOV of two low registers assert "U19" in disassemble_arm(0xF5D0F000, 0).comment # condition NV assert "nonstandard" in disassemble_arm(0xE12FFF0E & ~0x100, 0).comment # BX, SBO clear for nonstandard in (0xE30F0000, 0xE1120091, 0xE0400391, 0xE0011392): assert "nonstandard" in disassemble_arm(nonstandard, 0).comment # MRS, SWP, MUL x2 assert disassemble_arm(0xE0912003, 0).comment == "" listing = disassemble_arm(0xE59F1004, 0x08000000).listing assert listing.startswith("LDR R1, [PC, #0x4]") and listing.endswith("; =[0x0800000C]") def test_branch_targets(): assert disassemble_arm(0xEB000010, 0x08000000).target == 0x08000048 assert disassemble_arm(0xEAFFFFFE, 0x08000000).target == 0x08000000 assert disassemble_thumb(0xE7FE, 0x08000000).target == 0x08000000 assert disassemble_thumb(0xD0FE, 0x08000000).target == 0x08000000 assert disassemble_arm(0xE0912003, 0).target is None def test_thumb_bl_pair_combines(): pair = disassemble_thumb(0xF000, 0x08000000, 0xF810) assert pair.text == "BL 0x08000024" and pair.size == 4 and pair.target == 0x08000024 backwards = disassemble_thumb(0xF7FF, 0x08000100, 0xFFFE) # offset -4 assert backwards.text == "BL 0x08000100" assert disassemble_thumb(0xF000, 0x08000000, 0x2001).text == "BL.HI #0x0" def test_disassemble_reads_memory_and_aligns(): memory = bytearray(16) memory[0:4] = (0xE0912003).to_bytes(4, "little") memory[4:8] = (0xF810F000).to_bytes(4, "little") # F000 then F810 memory[8:10] = (0x4770).to_bytes(2, "little") arm = disassemble(memory.__getitem__, 2, thumb=False) assert arm.address == 0 and arm.text == "ADDS R2, R1, R3" pair = disassemble(memory.__getitem__, 4, thumb=True) assert pair.text == "BL 0x00000028" and pair.next_address == 8 assert disassemble(memory.__getitem__, 9, thumb=True).text == "BX LR" with pytest.raises(ValueError): disassemble(lambda address: 256, 0, thumb=False) def test_instruction_validates(): with pytest.raises(ValueError): Instruction(-1, b"\0\0", "NOP") with pytest.raises(ValueError): Instruction(0, b"\0", "NOP") with pytest.raises(ValueError): Instruction(0, b"\0\0", "") def test_every_thumb_encoding_disassembles(): for halfword in range(0x10000): ins = disassemble_thumb(halfword, 0x08000000) assert ins.size == 2 and ins.thumb and ins.mnemonic if THUMB_HANDLERS[halfword >> 6] == "_thumb_undefined": assert ins.mnemonic == "UND" def test_every_arm_index_disassembles_and_names_the_core_handler(): """Each of the 4,096 decode indexes, 8 random words each, under every condition: the mnemonic is the family the core's handler implements.""" rng = random.Random(4096) families = { "_arm_ldm": "LDM", "_arm_stm": "STM", "_arm_b": "B", "_arm_bl": "BL", "_arm_bx": "BX", "_arm_swi": "SWI", "_arm_undefined": "UND", "_arm_mrs": "MRS", "_arm_msr": "MSR", "_arm_swp": "SWP", "_arm_cdp": "CDP", "_arm_ldc": "LDC", "_arm_stc": "STC", "_arm_mrc": "MRC", "_arm_mcr": "MCR", } # fmt: skip for index in range(4096): handler = ARM_HANDLERS[index] for cond in range(16): word = rng.getrandbits(32) & ~0xFFF000F0 word |= (cond << 28) | ((index >> 4) << 20) | ((index & 0xF) << 4) ins = disassemble_arm(word, 0x08000000) assert ins.size == 4 and not ins.thumb family = families.get(handler, handler[5:8].upper()) assert ins.mnemonic.startswith(family), (hex(word), handler, ins.text) # ---------------------------------------------------------------------- against the core class FlatBus: def __init__(self, program: bytes, base: int) -> None: self.memory = bytearray(0x1000) self.memory[base : base + len(program)] = program self.data_reads: list[int] = [] def read(self, address: int, width: int, access: int) -> int: if not access & CODE: self.data_reads.append(address) address &= 0xFFF return int.from_bytes(self.memory[address : address + width // 8], "little") def write(self, address: int, width: int, value: int, access: int) -> None: pass def _run_one(program: bytes, base: int, thumb: bool) -> FlatBus: bus = FlatBus(program, base) cpu = ARM7TDMI(bus.read, bus.write) state = cpu.capture_state() state.cpsr = 0x1F | (0x20 if thumb else 0) cpu.restore_state(state) cpu.start_at(base) bus.data_reads.clear() cpu.step() return bus @pytest.mark.parametrize("word", [0xE59F1004, 0xE51F1010, 0xE1DF20B2, 0xE15F20F6]) def test_arm_pc_relative_comment_is_the_address_the_core_reads(word): ins = disassemble_arm(word, 0x100) bus = _run_one(word.to_bytes(4, "little"), 0x100, thumb=False) assert ins.comment == f"=[0x{bus.data_reads[0]:08X}]" @pytest.mark.parametrize("address", [0x100, 0x102]) def test_thumb_pc_relative_comment_is_the_address_the_core_reads(address): ins = disassemble_thumb(0x4801, address) bus = _run_one((0x4801).to_bytes(2, "little"), address, thumb=True) assert ins.comment == f"=[0x{bus.data_reads[0]:08X}]"