/* gba-python C12: what makes Hades-Tests dma-start-delay test 2 two code * fetches slower on hardware than in every emulator? See README.md for the * probes, the hypotheses and what each predicts. * * Each probe runs three times, each time started at the same scanline with * interrupts off. The screen shows three pages of ten probes (they turn every * three seconds; A turns at once): ID, the DMA's copied timer value (SLOT, * ---- if the DMA never ran), t1, t2 and D = t2 - t1, all hex. A row is grey * while its probe runs (a hang leaves it grey) and yellow if the three runs * disagreed. Every result also goes to SRAM (layout in README.md). */ typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; #define REG16(a) (*(volatile u16 *)(a)) #define VRAM ((volatile u16 *)0x06000000) #define SRAM ((volatile u8 *)0x0E000000) #define DISPCNT 0x04000000 #define VCOUNT 0x04000006 #define TM1CNT_L 0x04000104 #define TM1CNT_H 0x04000106 #define KEYINPUT 0x04000130 #define IME 0x04000208 #define WAITCNT 0x04000204 /* Flash carts and emulators look for this string to give the ROM SRAM. */ const char save_type[] __attribute__((used, aligned(4))) = "SRAM_V113"; typedef void probe_fn(volatile u16 *out); extern probe_fn p_hades_iw, p_cut0_iw, p_cut1_iw, p_cut2_iw, p_readkey_iw, p_readd1_iw, p_nowrite_iw, p_enonly_iw, p_tight_iw, p_gap1_iw, p_gap2_iw, p_gap4_iw, p_gap8_iw, p_gap32_iw, p_vblank_iw, p_ch1_iw, p_ch2_iw, p_ch3_iw, p_thumb_iw, p_hades_ew, p_hades_rom, p_cut0_rom, p_cut1_rom, p_cut2_rom, p_readkey_rom, p_nowrite_rom, p_thumb_rom; /* WAITCNT values. Bits 0-1 = 3 keep SRAM at 8 waits (safe on every cart); * the rest is the BIOS's hand-over default (WS0 4/2 waits, no prefetch) * unless the probe changes it. */ #define WC 0x0003 #define WC_PREFETCH 0x4003 #define WC_WS0_S1 0x0013 /* WS0 sequential 1 wait instead of 2 */ #define WC_WS0_N2 0x000B /* WS0 first access 2 waits instead of 4 */ struct probe { char id[4]; probe_fn *fn; u16 waitcnt; }; static const struct probe probes[] = { {"HIW", p_hades_iw, WC}, {"HEW", p_hades_ew, WC}, {"HRN", p_hades_rom, WC}, {"HRP", p_hades_rom, WC_PREFETCH}, {"L0I", p_cut0_iw, WC}, {"L1I", p_cut1_iw, WC}, {"L2I", p_cut2_iw, WC}, {"L0R", p_cut0_rom, WC}, {"L1R", p_cut1_rom, WC}, {"L2R", p_cut2_rom, WC}, {"RKI", p_readkey_iw, WC}, {"RKR", p_readkey_rom, WC}, {"R1I", p_readd1_iw, WC}, {"NWI", p_nowrite_iw, WC}, {"NWR", p_nowrite_rom, WC}, {"EOI", p_enonly_iw, WC}, {"KTI", p_tight_iw, WC}, {"K01", p_gap1_iw, WC}, {"K02", p_gap2_iw, WC}, {"K04", p_gap4_iw, WC}, {"K08", p_gap8_iw, WC}, {"K32", p_gap32_iw, WC}, {"VBI", p_vblank_iw, WC}, {"C1I", p_ch1_iw, WC}, {"C2I", p_ch2_iw, WC}, {"C3I", p_ch3_iw, WC}, {"TBI", p_thumb_iw, WC}, {"TBR", p_thumb_rom, WC}, {"WSR", p_hades_rom, WC_WS0_S1}, {"WNR", p_hades_rom, WC_WS0_N2}, }; #define COUNT (sizeof probes / sizeof probes[0]) #define RUNS 3 #define PER_PAGE 10 #define PAGES ((COUNT + PER_PAGE - 1) / PER_PAGE) struct result { u16 slot, t1, t2, tm1; u8 done, stable; }; static struct result results[COUNT]; /* ---- drawing: mode 3, a 3x5 font at scale 2, 8 pixels per character ---- */ static const u16 font[] = { 0x7B6F, 0x2C97, 0x73E7, 0x73CF, 0x5BC9, 0x79CF, 0x79EF, 0x7292, 0x7BEF, 0x7BCF, /* 0-9 */ 0x2BED, 0x6BAE, 0x7927, 0x6B6E, 0x79E7, 0x79E4, 0x396B, 0x5BED, 0x7497, 0x126A, /* A-J */ 0x5BAD, 0x4927, 0x5FED, 0x6B6D, 0x2B6A, 0x6BA4, 0x2B73, 0x6BAD, 0x388E, 0x7492, /* K-T */ 0x5B6F, 0x5B6A, 0x5BFD, 0x5AAD, 0x5A92, 0x72A7, /* U-Z */ }; static u16 glyph(char c) { if (c >= '0' && c <= '9') return font[c - '0']; if (c >= 'A' && c <= 'Z') return font[c - 'A' + 10]; if (c == '-') return 0x01C0; if (c == '/') return 0x12A4; 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 int text(int x, int y, const char *s, u16 colour) { for (; *s; s++, x += 8) { u16 bits = glyph(*s); 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); } return x; } static int hex(int x, int y, u32 value, int digits, u16 colour) { char s[9]; for (int i = 0; i < digits; i++) s[i] = "0123456789ABCDEF"[(value >> ((digits - 1 - i) * 4)) & 15]; s[digits] = 0; return text(x, y, s, colour); } #define WHITE 0x7FFF #define BLACK 0x0000 #define GREY 0x4210 #define YELLOW 0x03FF #define CYAN 0x7FE0 static int row_y(u32 i) { return 16 + (i % PER_PAGE) * 14; } static void draw_row(u32 i) { int y = row_y(i); const struct result *r = &results[i]; rect(0, y - 2, 240, 14, BLACK); u16 colour = !r->done ? GREY : r->stable ? WHITE : YELLOW; text(4, y, probes[i].id, colour); if (!r->done) { text(40, y, "RUNNING", GREY); return; } if (r->slot == 0xDEAD) text(40, y, "----", CYAN); else hex(40, y, r->slot, 4, colour); hex(80, y, r->t1, 4, colour); hex(120, y, r->t2, 4, colour); hex(160, y, (u16)(r->t2 - r->t1), 4, colour); if (r->tm1) text(200, y, "OV", YELLOW); /* timer 0 wrapped: t2 is mod 10000 */ } static void draw_page(u32 page) { rect(0, 0, 240, 160, BLACK); int x = text(4, 2, "C12 DMA STOP PAGE ", WHITE); x = hex(x, 2, page + 1, 1, WHITE); x = text(x, 2, "/", WHITE); hex(x, 2, PAGES, 1, WHITE); for (u32 i = page * PER_PAGE; i < COUNT && i < (page + 1) * PER_PAGE; i++) draw_row(i); } /* ---- SRAM: "DMAS", version, count, done; then 16-byte records ---- */ static void sram16(u32 offset, u16 value) { SRAM[offset] = value; SRAM[offset + 1] = value >> 8; } static void store(u32 i) { const struct result *r = &results[i]; u32 base = 0x10 + i * 16; for (int k = 0; k < 4; k++) SRAM[base + k] = probes[i].id[k]; sram16(base + 4, r->slot); sram16(base + 6, r->t1); sram16(base + 8, r->t2); sram16(base + 10, r->tm1); SRAM[base + 12] = r->done ? 'R' : 'P'; SRAM[base + 13] = r->stable; sram16(base + 14, probes[i].waitcnt); } /* ---- running ---- */ static void wait_line(u16 line) { while (REG16(VCOUNT) == line) ; while (REG16(VCOUNT) != line) ; } static void run_once(u32 i, volatile u16 *out, u16 *tm1) { REG16(WAITCNT) = probes[i].waitcnt; REG16(TM1CNT_H) = 0; REG16(TM1CNT_L) = 0; REG16(TM1CNT_H) = 0x0084; /* timer 1 counts timer 0's overflows */ wait_line(40); /* the same start every time, far from VBlank */ probes[i].fn(out); REG16(TM1CNT_H) = 0; *tm1 = REG16(TM1CNT_L); REG16(WAITCNT) = WC; } int main(void) { REG16(IME) = 0; REG16(WAITCNT) = WC; REG16(DISPCNT) = 0x0403; /* mode 3, BG2 */ SRAM[0] = 'D'; SRAM[1] = 'M'; SRAM[2] = 'A'; SRAM[3] = 'S'; SRAM[4] = 1; /* layout version */ SRAM[5] = COUNT; SRAM[6] = 0; for (u32 i = 0; i < COUNT; i++) store(i); for (u32 i = 0; i < COUNT; i++) { if (i % PER_PAGE == 0) draw_page(i / PER_PAGE); draw_row(i); volatile u16 out[3]; u16 first[4], tm1; u8 stable = 1; for (int run = 0; run < RUNS; run++) { run_once(i, out, &tm1); u16 now[4] = {out[0], out[1], out[2], tm1}; if (run == 0) for (int k = 0; k < 4; k++) first[k] = now[k]; else for (int k = 0; k < 4; k++) stable &= now[k] == first[k]; } results[i] = (struct result){first[0], first[1], first[2], first[3], 1, stable}; draw_row(i); store(i); } SRAM[6] = 'D'; /* done */ for (u32 page = 0;; page = (page + 1) % PAGES) { draw_page(page); for (int frame = 0; frame < 180; frame++) { wait_line(160); if (!(REG16(KEYINPUT) & 1)) { /* A: next page, on release */ while (!(REG16(KEYINPUT) & 1)) ; break; } } } }