#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Generate src/arm7tdmi_python/_tables.py: the ARM and Thumb decode tables. ARM is indexed by the 12 bits ``bits[27:20]:bits[7:4]`` (4,096 entries), Thumb by ``bits[15:6]`` (1,024 entries), as every referee does (docs/start-here.md). Each entry names the handler method. The rules follow the data sheet's encoding diagrams (DS figure 4-1 and chapter 5); where an index is ambiguous within those bits, the order of the checks is the one the SingleStepTests corpus (MIT, run as data) passes with, recorded here as OBSERVED: - BX is ``0x12`` with bits 7:4 = ``0001``, whatever Rn and Rd hold (bits 19:8 are not in the index). - Multiply and swap are bits 7:4 = ``1001`` (bit 24 clear and set); the other ``1xx1`` patterns are halfword and signed transfers. - Data processing with S clear and opcode 8-11 is a PSR transfer: MRS with bit 21 clear, MSR with it set. - Thumb ``0xDExx`` (condition 1110) and ``0xE8xx`` are undefined (user decision 4), as is every other encoding the formats leave out. python scripts/gen_tables.py # rewrite the module python scripts/gen_tables.py --check # exit 1 if the module is out of date """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TARGET = ROOT / "src" / "arm7tdmi_python" / "_tables.py" DATA_OPS = [ "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc", "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn", ] THUMB_ALU = [ "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror", "tst", "neg", "cmp", "cmn", "orr", "mul", "bic", "mvn", ] def arm_handler(index: int) -> str: """The handler for ARM index ``bits[27:20]:bits[7:4]``.""" high, low = index >> 4, index & 0xF # bits 27:20, bits 7:4 family = high >> 5 # bits 27:25 if family in (0b000, 0b001): immediate = family == 0b001 if not immediate and high == 0x12 and low == 0b0001: return "_arm_bx" if not immediate and low == 0b1001: if high & 0x10: # bit 24 return "_arm_swp" if high & 0x08: # bit 23: long signed, accumulate = high & 0x04, high & 0x02 return f"_arm_{'s' if signed else 'u'}{'mlal' if accumulate else 'mull'}" return "_arm_mla" if high & 0x02 else "_arm_mul" if not immediate and low & 0b1001 == 0b1001: load = high & 0x01 sh = (low >> 1) & 3 if load: return ("", "_arm_ldrh", "_arm_ldrsb", "_arm_ldrsh")[sh] return ("", "_arm_strh", "_arm_ldrd", "_arm_strd")[sh] opcode = (high >> 1) & 0xF set_flags = high & 0x01 if not set_flags and 8 <= opcode <= 11: return "_arm_msr" if high & 0x02 else "_arm_mrs" return f"_arm_{DATA_OPS[opcode]}" if family in (0b010, 0b011): if family == 0b011 and low & 1: return "_arm_undefined" load, byte = high & 0x01, high & 0x04 return f"_arm_{'ldr' if load else 'str'}{'b' if byte else ''}" if family == 0b100: return "_arm_ldm" if high & 0x01 else "_arm_stm" if family == 0b101: return "_arm_bl" if high & 0x10 else "_arm_b" if family == 0b110: return "_arm_ldc" if high & 0x01 else "_arm_stc" if high & 0x10: # 1111: SWI return "_arm_swi" if low & 1: return "_arm_mrc" if high & 0x01 else "_arm_mcr" return "_arm_cdp" def thumb_handler(index: int) -> str: """The handler for Thumb index ``bits[15:6]``.""" op = index << 6 if op & 0xF800 < 0x1800: return ("_thumb_lsl_imm", "_thumb_lsr_imm", "_thumb_asr_imm")[(op >> 11) & 3] if op & 0xF800 == 0x1800: return "_thumb_sub3" if op & 0x0200 else "_thumb_add3" if op & 0xE000 == 0x2000: return ("_thumb_mov_imm", "_thumb_cmp_imm", "_thumb_add_imm", "_thumb_sub_imm")[ (op >> 11) & 3 ] if op & 0xFC00 == 0x4000: return f"_thumb_{THUMB_ALU[(op >> 6) & 0xF]}" if op & 0xFC00 == 0x4400: return ("_thumb_add_hi", "_thumb_cmp_hi", "_thumb_mov_hi", "_thumb_bx")[(op >> 8) & 3] if op & 0xF800 == 0x4800: return "_thumb_ldr_pc" if op & 0xF200 == 0x5000: return ("_thumb_str_reg", "_thumb_strb_reg", "_thumb_ldr_reg", "_thumb_ldrb_reg")[ (op >> 10) & 3 ] if op & 0xF200 == 0x5200: return ("_thumb_strh_reg", "_thumb_ldsb_reg", "_thumb_ldrh_reg", "_thumb_ldsh_reg")[ (op >> 10) & 3 ] if op & 0xE000 == 0x6000: return ("_thumb_str_imm", "_thumb_ldr_imm", "_thumb_strb_imm", "_thumb_ldrb_imm")[ (op >> 11) & 3 ] if op & 0xF000 == 0x8000: return "_thumb_ldrh_imm" if op & 0x0800 else "_thumb_strh_imm" if op & 0xF000 == 0x9000: return "_thumb_ldr_sp" if op & 0x0800 else "_thumb_str_sp" if op & 0xF000 == 0xA000: return "_thumb_add_sp_rel" if op & 0x0800 else "_thumb_add_pc_rel" if op & 0xFF00 == 0xB000: return "_thumb_add_sp" if op & 0xF600 == 0xB400: return "_thumb_pop" if op & 0x0800 else "_thumb_push" if op & 0xF000 == 0xC000: return "_thumb_ldmia" if op & 0x0800 else "_thumb_stmia" if op & 0xF000 == 0xD000: condition = (op >> 8) & 0xF if condition == 0xF: return "_thumb_swi" if condition == 0xE: return "_thumb_undefined" # C7: provisional, user decision 4 return "_thumb_bcc" if op & 0xF800 == 0xE000: return "_thumb_b" if op & 0xF000 == 0xF000: return "_thumb_bl_suffix" if op & 0x0800 else "_thumb_bl_prefix" return "_thumb_undefined" # includes 0xE8xx (C8) and the unused B-space formats def render() -> str: arm = [arm_handler(i) for i in range(4096)] thumb = [thumb_handler(i) for i in range(1024)] lines = [ "# SPDX-License-Identifier: MIT", '"""The ARM and Thumb decode tables. Generated by scripts/gen_tables.py: do not edit.', "", "``ARM_HANDLERS[bits[27:20] << 4 | bits[7:4]]`` and ``THUMB_HANDLERS[bits[15:6]]`` name", "the handler method for every encoding; the rules are in the script's docstring.", '"""', "", "ARM_HANDLERS = (", ] for start in range(0, 4096, 16): names = ", ".join(f'"{n}"' for n in arm[start : start + 16]) lines.append(f" # 0x{start:03X}\n {names},") lines += [")", "", "THUMB_HANDLERS = ("] for start in range(0, 1024, 16): names = ", ".join(f'"{n}"' for n in thumb[start : start + 16]) lines.append(f" # 0x{start << 6:04X}\n {names},") lines.append(")") return "\n".join(lines) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--check", action="store_true", help="fail if the module is stale") args = parser.parse_args() text = render() if args.check: if TARGET.read_text(encoding="utf-8") != text: print(f"{TARGET.relative_to(ROOT)} is out of date", file=sys.stderr) return 1 return 0 TARGET.write_text(text, encoding="utf-8") print(f"wrote {TARGET.relative_to(ROOT)}") return 0 if __name__ == "__main__": sys.exit(main())