# SPDX-License-Identifier: MIT """Processor modes, register banks and the CPSR's bits (TRM 2.6-2.7, DS 3.7-3.8).""" USR = 0x10 FIQ = 0x11 IRQ = 0x12 SVC = 0x13 ABT = 0x17 UND = 0x1B SYS = 0x1F # Bank indices. The order of FIQ..UND is the SingleStepTests corpus's SPSR order. BANK_USER = 0 # User and System share it; it holds R8-R14 of those modes BANK_FIQ = 1 # R8-R14_fiq BANK_SVC = 2 # R13-R14_svc (slots 5 and 6 of a bank are R13 and R14) BANK_ABT = 3 BANK_IRQ = 4 BANK_UND = 5 BANK_INVALID = 6 # a mode value that names no mode: see _core.py "Mode values that name no mode" BANK_COUNT = 7 BANK_OF_MODE = { USR: BANK_USER, SYS: BANK_USER, FIQ: BANK_FIQ, IRQ: BANK_IRQ, SVC: BANK_SVC, ABT: BANK_ABT, UND: BANK_UND, } FLAG_N = 1 << 31 FLAG_Z = 1 << 30 FLAG_C = 1 << 29 FLAG_V = 1 << 28 MASK_I = 1 << 7 # IRQ disable MASK_F = 1 << 6 # FIQ disable THUMB = 1 << 5 # T: Thumb state MODE_BITS = 0x1F def bank_of(mode: int) -> int: """The register bank a mode value selects; BANK_INVALID for one that names no mode.""" return BANK_OF_MODE.get(mode & MODE_BITS, BANK_INVALID) def condition_passes(condition: int, cpsr: int) -> bool: """Whether condition field ``condition`` (bits 31:28) passes with flags ``cpsr`` (DS 4.2, table 4-2). The sixteenth code, 1111 (NV), is "reserved, must not be used" in the data sheet; ARMv4 defines it as never. The core never executes it, which is what the SingleStepTests corpus shows for every NV case (OBSERVED, corpus v1). """ n = cpsr >> 31 & 1 z = cpsr >> 30 & 1 c = cpsr >> 29 & 1 v = cpsr >> 28 & 1 if condition == 0x0: # EQ return z == 1 if condition == 0x1: # NE return z == 0 if condition == 0x2: # CS return c == 1 if condition == 0x3: # CC return c == 0 if condition == 0x4: # MI return n == 1 if condition == 0x5: # PL return n == 0 if condition == 0x6: # VS return v == 1 if condition == 0x7: # VC return v == 0 if condition == 0x8: # HI return c == 1 and z == 0 if condition == 0x9: # LS return c == 0 or z == 1 if condition == 0xA: # GE return n == v if condition == 0xB: # LT return n != v if condition == 0xC: # GT return z == 0 and n == v if condition == 0xD: # LE return z == 1 or n != v return condition == 0xE # AL; NV never