"""Every instruction form the game uses, in Unicorn and as lifted C: a64-kit's tests/insn_test.py with the Switch 2 additions. Differences from running the vendored test as is: - the Switch 2 forms translate (insn.install()), and RETAx are returns (not sampled, like RET); - Unicorn runs its "max" CPU model, which has the ARMv8.1+ forms (the default Cortex-A72 rejects LSE, CAS, LDAPR); - the harness is built with -mcx16 (CASP on 128 bits); - PAC and MTE forms are left out: here they are the identity by design (see insn.py), not the hardware's result; - memory operands based on x29 / x30 get their scratch address too: capstone names them fp / lr, which insn_test.mem_regs does not parse, so those cases ran on a random (often tiny) address that the harness does not reset between cases (a false LDADD [x29] failure, 2026-10-31). Usage: python -I nx2/test_insn.py [--modules main,sdk,...] [--per 6] [--states 12] [--only lse] (--modules: every code module of the exefs but rtld by default) Needs the module images of nso.py in /modules (made on first use). """ import os import subprocess import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # (python -I leaves the script folder off) import kit import insn insn.install() sys.path.insert(0, os.path.join(kit.KIT, "tests")) import insn_test # noqa: E402 import toolchain # noqa: E402 import unicorn # noqa: E402 from unicorn import arm64_const as A # noqa: E402 # (the game: this runs the lifted code of a real dump against Unicorn) RUN_ALL_SKIP = { "linux": "needs the game's modules (work/dump) and Unicorn", "windows": "needs the game's modules (work/dump) and Unicorn", } class _Unicorn: """insn_test's view of the unicorn module: every engine it makes runs the ARMv8.x "max" CPU""" UcError = unicorn.UcError UC_ARCH_ARM64 = unicorn.UC_ARCH_ARM64 UC_MODE_ARM = unicorn.UC_MODE_ARM @staticmethod def Uc(arch, mode): uc = unicorn.Uc(arch, mode) uc.ctl_set_cpu_model(A.UC_CPU_ARM64_MAX) return uc ONLY = {"lse": lambda w: (w & 0x3F200C00) == 0x38200000 or (w & 0x3FA07C00) == 0x08A07C00 or (w & 0xBFA07C00) == 0x08207C00, "nx2": lambda w: insn.emit(w) is not None} def mem_regs(w): md = insn_test.capstone.Cs(insn_test.capstone.CS_ARCH_ARM64, insn_test.capstone.CS_MODE_ARM) md.detail = True ins = next(md.disasm(insn_test.struct.pack("