# dma_stop: a hardware test for C12 (Hades-Tests `dma-start-delay` test 2) Hades' test 2 enables an immediate one-halfword DMA0 that copies TM0CNT_L, writes `DMA0CNT = 0` on the next statement, reads the timer (t1), runs one pass of `while (DMA0CNT & enable)` and reads the timer again (t2). On hardware t2 is later than on this board, NanoBoyAdvance and mGBA by **2 cycles in IWRAM and 12 in EWRAM and ROM (with or without prefetch)**: two sequential ARM opcode fetches in each region. The DMA's own copied value and t1 already agree. This ROM finds what causes those two fetches (docs/undocumented-behavior.md, C12). ## The probes Every ARM probe is Hades' test 2 **instruction for instruction** (its devkitARM `-O0` code, disassembled from `Hades-Tests/roms/dma-start-delay.gba`: the same literal loads, `MOV r2,#0x80000001`, 32-bit stores and the `LDR/CMP/BLT` loop), from the timer start to t2, with **one** thing changed. Timer 0 runs at one tick per cycle, with timer 1 cascaded to flag a wrap. Each probe runs three times, each time started at scanline 40 with interrupts off. | ID | Changed from Hades | Code in | WAITCNT | | --- | --- | --- | --- | | HIW | nothing (Hades' IWRAM test 2) | IWRAM | 0003 | | HEW | nothing (EWRAM) | EWRAM | 0003 | | HRN | nothing (ROM, no prefetch) | ROM | 0003 | | HRP | nothing (ROM, prefetch) | ROM | 4003 | | L0I, L0R | t2 taken right after t1's store | IWRAM, ROM | 0003 | | L1I, L1R | t2 taken after the loop's literal load | IWRAM, ROM | 0003 | | L2I, L2R | t2 taken after the loop's `LDR` of DMA0CNT | IWRAM, ROM | 0003 | | RKI, RKR | the loop reads KEYINPUT instead of DMA0CNT (same cost) | IWRAM, ROM | 0003 | | R1I | the loop reads DMA1CNT instead of DMA0CNT | IWRAM | 0003 | | NWI, NWR | neither the enable nor the disable is written (both go to SIODATA32) | IWRAM, ROM | 0003 | | EOI | the enable is written, the disable is not (it goes to SIODATA32) | IWRAM | 0003 | | KTI | enable and disable on consecutive instructions (operands preloaded) | IWRAM | 0003 | | K01, K02, K04, K08, K32 | 1, 2, 4, 8, 32 `MOV R0,R0` between enable and disable | IWRAM | 0003 | | VBI | the enable is VBlank-timed (0x90000001), so no transfer can happen | IWRAM | 0003 | | C1I, C2I, C3I | DMA1, DMA2, DMA3 instead of DMA0 | IWRAM | 0003 | | TBI, TBR | the same sequence in Thumb | IWRAM, ROM | 0003 | | WSR | Hades' ROM test with WS0 sequential 1 wait (S = 2 per halfword) | ROM | 0013 | | WNR | Hades' ROM test with WS0 first access 2 waits (N = 3 per halfword) | ROM | 000B | WAITCNT bits 0-1 = 3 keep SRAM at 8 waits; otherwise 0003 is the BIOS's hand-over default that Hades runs with (WS0 4/2 waits). One code fetch costs 1 cycle in IWRAM (ARM or Thumb), 6 for an ARM fetch from EWRAM or ROM at these settings, 3 for a Thumb fetch from ROM, and 4 for an ARM fetch from ROM under WSR. ## The hypotheses and what each predicts "Extra" is how much later t2 is on hardware than on this board (the board's value is in the next section). Hades' hardware results fix the four H rows: HIW +2, HEW +12, HRN +12, HRP +12. - **H1, the read.** The first read of the *stopped channel's own* DMAxCNT after the CPU's disable write costs two code fetches. - **H2, the write.** The disable write costs two code fetches, charged after t1 whatever the loop reads. - **H3, a window.** The effect needs the disable to land within a few cycles of the transfer (the gap rows fall to 0 as the gap grows); without H3 the gap does not matter. - **H4, no transfer needed.** Any enable followed by a disable does it, even when no transfer happened (VBI, and KTI if the transfer is cancelled). - **H5, DMA0 only.** Channels 1-3 do not show it. - **Fetch-sized.** Whatever the trigger, the cost is two *sequential* code fetches: it scales with the S time of the code region (TBR, WSR), not with N (WNR). | ID | H1 read | H2 write | notes | | --- | --- | --- | --- | | HIW / HEW / HRN / HRP | +2 / +12 / +12 / +12 | same | Hades' hardware values; the controls | | L0I / L0R | 0 / 0 | +2 / +12, or 0 if charged later | where the extra first appears locates it | | L1I / L1R | 0 / 0 | as L0, or the extra appears here | | | L2I / L2R | +2 / +12 | +2 / +12 | H1: appears exactly at the read | | RKI / RKR | 0 / 0 | +2 / +12 | **H1 against H2** | | R1I | 0 | +2 | H1 needs the same channel's register | | NWI / NWR | 0 / 0 | 0 / 0 | no writes: must be 0 under every hypothesis | | EOI | 0 | 0 | no CPU disable: 0 under H1 and H2 (the DMA clears the bit itself) | | KTI | +2 if SLOT shows a transfer; under H4 also if not | same | NBA and this board cancel the transfer here, mGBA does not: hardware settles that too | | K01-K32 | +2 at every gap; under H3 falling to 0 | same | | | VBI | 0; +2 under H4 | 0; +2 under H4 | no transfer can happen | | C1I, C2I, C3I | +2 each; 0 under H5 | same | | | TBI / TBR | +2 / +6 | +2 / +6 | fetch-sized: two Thumb fetches | | WSR | +8 | +8 | fetch-sized: two ARM S fetches at 4; +14 if it were two N fetches | | WNR | +12 | +12 | S unchanged; N changes | ## What the referees and this board show Retail BIOS, `python tests/hw/dma_stop/run_referees.py`. SLOT is the timer value the DMA copied (---- when it never ran); D = t2 - t1. All values hex. All three runs agreed for every probe on every referee. The board and NanoBoyAdvance 1.8.3 agree on every value. mGBA agrees on D for every probe; its SLOT is one cycle off in either direction, and in most IWRAM probes its t1 and t2 are one cycle later too. mGBA also lets the KTI transfer run where the other two cancel it. | ID | board and NBA: SLOT t1 t2 D | mGBA: SLOT t1 t2 D | Hades' hardware (H rows) | | --- | --- | --- | --- | | HIW | 0016 0020 0030 0010 | 0017 0021 0031 0010 | 0016 0020 **0032** | | HEW | 005B 0085 00C2 003D | 005A 0085 00C2 003D | 005B 0085 **00CE** | | HRN | 0073 00A7 00F2 004B | 0072 00A7 00F2 004B | 0073 00A7 **00FE** | | HRP | 0067 0098 00D8 0040 | 0066 0098 00D8 0040 | 0067 0098 **00E4** | | L0I | 0016 0020 0028 0008 | 0017 0021 0029 0008 | | | L1I | 0016 0020 002B 000B | 0017 0021 002C 000B | | | L2I | 0016 0020 002E 000E | 0017 0021 002F 000E | | | L0R | 0073 00A7 00CB 0024 | 0072 00A7 00CB 0024 | | | L1R | 0073 00A7 00DC 0035 | 0072 00A7 00DC 0035 | | | L2R | 0073 00A7 00E6 003F | 0072 00A7 00E6 003F | | | RKI | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | RKR | 0073 00A7 00F2 004B | 0072 00A7 00F2 004B | | | R1I | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | NWI | ---- 001D 002D 0010 | ---- 001D 002D 0010 | | | NWR | ---- 00A3 00EE 004B | ---- 00A3 00EE 004B | | | EOI | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | KTI | ---- 001D 002D 0010 | 001A 001D 002D 0010 | | | K01 | 0016 0022 0032 0010 | 0015 0022 0032 0010 | | | K02 | 0016 0023 0033 0010 | 0015 0023 0033 0010 | | | K04 | 0016 0025 0035 0010 | 0015 0025 0035 0010 | | | K08 | 0016 0029 0039 0010 | 0015 0029 0039 0010 | | | K32 | 0016 0041 0051 0010 | 0015 0041 0051 0010 | | | VBI | ---- 001D 002D 0010 | ---- 001D 002D 0010 | | | C1I | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | C2I | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | C3I | 0016 0020 0030 0010 | 0017 0021 0031 0010 | | | TBI | 0018 0022 0032 0010 | 0019 0023 0033 0010 | | | TBR | 0060 0088 00BE 0036 | 005F 0088 00BE 0036 | | | WSR | 0063 0090 00D0 0040 | 0062 0090 00D0 0040 | | | WNR | 005B 0085 00C2 003D | 005A 0085 00C2 003D | | On the four H rows NanoBoyAdvance and this board reproduce exactly the values they show on Hades' own ROM (SLOT and t1 equal to hardware, t2 short by 2 or 12), so the probes reproduce the Hades test. Screenshots: `referee-mgba-p1..3.png`, `referee-nba-p1..3.png` (the board has no picture until rung 3; its values come from SRAM). ## Running it on a GBA 1. Copy `dma_stop.gba` to a flash cart. If the cart asks for a save type, choose **SRAM** (the ROM also carries the `SRAM_V113` tag). 2. Boot it normally on a GBA, GBA SP or Game Boy Player, through the Nintendo logo: not the cart's skip-BIOS or soft-reset mode, and not a DS or 3DS (they run GBA code with different timing). 3. The probes finish within a second of the logo. The screen then shows page 1 of 3; pages turn every three seconds, or press **A** to turn at once. **Photograph all three pages.** 4. Optional: copy the `.sav` back and run `python tests/hw/dma_stop/run_referees.py --decode .sav`. ### Reading the screen Each row is `ID SLOT T1 T2 D`, all hex: the probe ID (table above), the timer value the DMA copied (`----` in cyan: the DMA never ran), t1, t2, and D = t2 - t1. A **grey** row with `RUNNING` is a probe that never finished (the machine hung in it). A **yellow** row means the three runs disagreed (the SRAM record keeps the first run's values). `OV` at the right means timer 0 wrapped; it should never appear. ### SRAM layout Offset 0: `DMAS`, layout version (1), probe count, then `D` once every probe finished. From 0x10, one 16-byte record per probe: ID (4 bytes, NUL-padded), SLOT, t1, t2, timer 1 (all little-endian u16), status (`R` done, `P` pending), runs agreed (1/0), WAITCNT (u16). ## Building `python tests/hw/dma_stop/build.py` assembles `crt0.s`, `probes.s` and `main.c` with the Arm GNU Toolchain 14.3 that `scripts/fetch_references.py` pins, writes the logo and header check, and produces `dma_stop.gba` (9,216 bytes). Intermediates go to `build/` (ignored). ## Hardware result To be filled in from the photos or the `.sav`. | ID | SLOT | t1 | t2 | D | extra over the board | | --- | --- | --- | --- | --- | --- | | | | | | | | Winner: