/* SPDX-License-Identifier: MIT */ /* Thumb 0xDExx / 0xE8xx on a real GBA: does each encoding branch, trap, or * fall through? arm7tdmi-python, user decision 4 (docs/handoff-brief.md). * * Each probe runs once from IWRAM (probes.s). Its row is drawn grey before * it runs, so a probe that hangs the machine leaves its row grey: that is * itself the result. The outcome is drawn as a coloured box with a letter, * and every record also goes to SRAM (README.md has the layout and legend). */ typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; #define REG16(a) (*(volatile u16 *)(a)) #define REG32(a) (*(volatile u32 *)(a)) #define VRAM ((volatile u16 *)0x06000000) #define SRAM ((volatile u8 *)0x0E000000) struct probe_io { u32 ret, t0, t1, r7, r6, pc, cpsr, sp; }; volatile struct probe_io probe_io; extern u32 thumb_probe(void) __attribute__((long_call)); extern u32 arm_probe(void) __attribute__((long_call)); extern void irq_unmask(void) __attribute__((long_call)); extern void irq_handler(void); extern volatile u16 thumb_slot; extern volatile u32 arm_slot; /* Flash carts and emulators look for this string to give the ROM SRAM. */ const char save_type[] __attribute__((used, aligned(4))) = "SRAM_V113"; #define LR_UND (*(volatile u32 *)0x03007FEC) /* where the retail BIOS saves R14_und */ #define LR_PAD 0x02020000u enum { THUMB, ARM }; struct probe { u32 opcode; u8 state; }; /* Rows 0-2 are controls: a Thumb NOP (MOV R8, R8), a Thumb B to the next * instruction (the timing a DEFF branch would have), and an ARM encoding * ARMv4 defines as undefined (cond 011x...1xxxx), which must trap. */ static const struct probe probes[] = { {0x46C0, THUMB}, {0xE7FF, THUMB}, {0xE7F000F0, ARM}, {0xDE00, THUMB}, {0xDE01, THUMB}, {0xDE7F, THUMB}, {0xDEFE, THUMB}, {0xDEFF, THUMB}, {0xE800, THUMB}, {0xE83F, THUMB}, {0xE840, THUMB}, {0xEFFF, THUMB}, }; #define COUNT (sizeof probes / sizeof probes[0]) /* Outcome letters. */ #define PENDING 'P' #define FELL 'F' /* executed as a no-op: the next instruction ran next */ #define BRANCH 'B' /* a taken branch (to the sled, to itself, or by timing to the next) */ #define TRAP 'T' /* the undefined-instruction trap (BIOS loop observed) */ #define OTHER 'X' /* the IRQ caught the CPU somewhere else: see PC and CPSR */ #define ODD '?' /* fell through, but its time matches neither control */ /* ---- drawing: mode 3, a 3x5 font at scale 2 ---- */ static const u16 glyphs[] = { /* 0-9 */ 0x7B6F, 0x2C97, 0x73E7, 0x73CF, 0x5BC9, 0x79CF, 0x79EF, 0x7249, 0x7BEF, 0x7BCF, /* A-F */ 0x2BED, 0x6BAE, 0x7927, 0x6B6E, 0x79E7, 0x79E4, }; static u16 glyph(char c) { if (c >= '0' && c <= '9') return glyphs[c - '0']; if (c >= 'A' && c <= 'F') return glyphs[c - 'A' + 10]; switch (c) { case 'T': return 0x7492; case 'X': return 0x5AAD; case 'P': return 0x7BE4; case '?': return 0x72C2; case '-': return 0x01C0; } return 0; } static void rect(int x, int y, int w, int h, u16 colour) { for (int j = 0; j < h; j++) for (int i = 0; i < w; i++) VRAM[(y + j) * 240 + x + i] = colour; } static void put(int x, int y, char c, u16 colour) { u16 bits = glyph(c); for (int row = 0; row < 5; row++) for (int col = 0; col < 3; col++) if (bits & (1 << (14 - row * 3 - col))) rect(x + col * 2, y + row * 2, 2, 2, colour); } static void hex(int x, int y, u32 value, int digits, u16 colour) { for (int i = digits - 1; i >= 0; i--, x += 8) put(x, y, "0123456789ABCDEF"[(value >> (i * 4)) & 15], colour); } #define WHITE 0x7FFF #define BLACK 0x0000 static u16 outcome_colour(char outcome) { switch (outcome) { case TRAP: return 0x001F; /* red */ case BRANCH: return 0x7C00; /* blue */ case FELL: return 0x03E0; /* green */ case OTHER: return 0x03FF; /* yellow */ case ODD: return 0x7C1F; /* magenta */ } return 0x4210; /* grey: pending */ } /* ---- SRAM: "TUND", version, count; then 32-byte records ---- */ static void sram_word(u32 offset, u32 value) { for (int i = 0; i < 4; i++) SRAM[offset + i] = value >> (i * 8); } struct record { u32 opcode; u8 outcome, state; u16 cycles; u32 pc, cpsr, lr_und, r7, r6, ret; }; static void store(int index, const struct record *r) { u32 base = 0x10 + index * 32; sram_word(base + 0, r->opcode); SRAM[base + 4] = r->outcome; SRAM[base + 5] = r->state; SRAM[base + 6] = r->cycles; SRAM[base + 7] = r->cycles >> 8; sram_word(base + 8, r->pc); sram_word(base + 12, r->cpsr); sram_word(base + 16, r->lr_und); sram_word(base + 20, r->r7); sram_word(base + 24, r->r6); sram_word(base + 28, r->ret); } static void draw(int index, const struct record *r) { int y = 4 + index * 13; rect(0, y - 1, 240, 12, BLACK); if (r->state == THUMB) hex(4, y, r->opcode, 4, WHITE); else hex(4, y, r->opcode, 8, WHITE); rect(74, y - 1, 20, 12, outcome_colour(r->outcome)); int light = r->outcome == FELL || r->outcome == OTHER; put(81, y, r->outcome, light ? BLACK : WHITE); if (r->outcome == PENDING) return; hex(100, y, r->cycles, 4, WHITE); hex(136, y, r->r7 > 0xFFF ? 0xFFF : r->r7, 3, WHITE); hex(168, y, r->pc, 8, WHITE); } /* ---- running ---- */ static u16 nop_cycles, branch_cycles; static void classify(struct record *r, u32 slot) { u32 ret = probe_io.ret; r->ret = ret; r->r7 = probe_io.r7; r->r6 = probe_io.r6; r->cycles = (u16)(probe_io.t1 - probe_io.t0); r->lr_und = LR_UND; r->pc = 0; r->cpsr = 0; if (ret == 1) { r->pc = slot + (r->state == THUMB ? 2 : 4); if (r->state == ARM) r->outcome = FELL; else if (r->opcode == 0x46C0) r->outcome = FELL; else if (r->opcode == 0xE7FF) r->outcome = BRANCH; else if (r->cycles == nop_cycles) r->outcome = FELL; else if (r->cycles == branch_cycles) r->outcome = BRANCH; else r->outcome = ODD; } else if (ret == 2) { /* The sled's last B (r6 = 0) is slot + 260, past any DExx target * (+258 at most). Reaching it means the fall-through B ran with R15 * two bytes behind, as NanoBoyAdvance run closed box shows. */ r->outcome = r->r6 ? BRANCH : OTHER; r->pc = slot + 4 + 2 * (128 - r->r6); /* the branch target */ } else { int thumb = (probe_io.cpsr & 0x20) != 0; r->pc = probe_io.pc; r->cpsr = probe_io.cpsr; r->cycles = 0; if (r->state == THUMB && thumb && (r->pc == slot || r->pc == slot - 2) && r->r7 >= 2) r->outcome = TRAP; else if (r->state == THUMB && thumb && r->pc == slot && r->r7 == 1) r->outcome = BRANCH; /* a branch to itself, held until the IRQ */ else if (r->state == ARM && !thumb && r->pc == slot && r->r7 == 1) r->outcome = TRAP; /* ARM: the trap returns to the probe itself */ else r->outcome = OTHER; } } int main(void) { REG16(0x04000000) = 0x0403; /* DISPCNT: mode 3, BG2 */ rect(0, 0, 240, 160, BLACK); REG16(0x04000204) = 0x4317; /* WAITCNT: SRAM 8 waits (the safe setting) */ for (u32 a = LR_PAD - 0x8000; a < LR_PAD + 0x8000; a += 4) REG32(a) = 0; /* zero sled around LR, in both states */ SRAM[0] = 'T'; SRAM[1] = 'U'; SRAM[2] = 'N'; SRAM[3] = 'D'; SRAM[4] = 1; /* layout version */ SRAM[5] = COUNT; REG16(0x04000106) = 0x0080; /* timer 1: free-running, 1 cycle per tick */ REG32(0x03007FFC) = (u32)irq_handler; REG16(0x04000200) = 0x0008; /* IE: timer 0 */ REG16(0x04000208) = 1; /* IME */ for (u32 i = 0; i < COUNT; i++) { struct record r = {probes[i].opcode, PENDING, probes[i].state, 0, 0, 0, 0, 0, 0, 0}; draw(i, &r); store(i, &r); u32 slot; LR_UND = 0; probe_io.ret = 0; irq_unmask(); if (r.state == THUMB) { thumb_slot = (u16)r.opcode; slot = (u32)&thumb_slot; thumb_probe(); } else { arm_slot = r.opcode; slot = (u32)&arm_slot; arm_probe(); } classify(&r, slot); if (i == 0) nop_cycles = r.cycles; if (i == 1) branch_cycles = r.cycles; draw(i, &r); store(i, &r); } SRAM[6] = 'D'; /* done */ for (;;) ; }