# DMA, timers, interrupts, the maths unit, keys and power control Source: GBATEK "DS System and Built-in Peripherals" (DMA Transfers, Timers, Interrupts, Maths, Keypad, Power Control). DOC unless marked. Much here is "same as GBA" in GBATEK's words; gba-python (`../gba-python`) specifies and judges the GBA forms (its `dma.py`, `timers.py`, `irq.py`, `keypad.py` and docs), and the DS versions are written again in nds-python with those as the reading list, not imported (boards do not import boards; the chip rule keeps these in each board). ## DMA (four channels per CPU) - **ARM9**: word count 21 bits, addresses 0-0FFFFFFEh on every channel; start modes: immediate, V-blank, H-blank (paused in V-blank), start of display, main memory display, DS slot, GBA slot, **geometry FIFO**; four 32-bit **fill registers** (40000E0h-40000EFh) as fixed sources for memory fills. - **ARM7**: GBA-like limits; modes: immediate, V-blank, DS slot, and wireless (channels 0 and 2) or GBA slot (1 and 3). - DMA cannot reach the ARM9's TCMs; the ARM9 keeps running during DMA only while it stays in TCM and cache (GBATEK "NDS9 Cache, Writebuffer, DTCM, and ITCM"). - Main RAM to main RAM makes every access nonsequential ("Sequential Main Memory DMA"). - Plus 16 sound channel fetches and 2 capture writers on the ARM7 side, "priority unknown" (U1). ## Timers (four per CPU) "Same as GBA, except F = 33.513982 MHz (for both NDS9 and NDS7)": prescalers F/1, F/64, F/256, F/1024, cascade, IRQ. Unlike the GBA, sound has its own timers, so timers do not drive audio. ## Interrupts IME (4000208h), IE (4000210h), IF (4000214h), 32 bits, on each CPU. Sources: V-blank, H-blank, V-count, timers 0-3, RTC (ARM7), DMA 0-3, keypad, GBA slot, IPC sync, IPC send empty, IPC receive not empty, DS slot transfer done, DS slot IREQ_MC, **geometry FIFO** (ARM9, level-triggered), **screens unfolding** (ARM7, the hinge), SPI (ARM7), wireless (ARM7). The ARM9's IRQ vector is in its BIOS at FFFF0018h and jumps through [DTCM+3FFCh]; the ARM7's through [03FFFFFCh] (GBATEK "DS Memory Maps"). ## Maths unit (ARM9 only) - **Divider** (DIVCNT 4000280h): 32/32, 64/32, 64/64 signed, busy for 18, 34, 34 bus cycles; restarts on any write to its registers; divide-by-zero and -MAX/-1 results defined (GBATEK "DS Maths"). - **Square root** (SQRTCNT 40002B0h): 32- or 64-bit unsigned, busy 13 bus cycles in either mode; reading the results takes more because of the ARM9's nonsequential penalty (GBATEK). - rockwrestler checks results (not timings) of both. ## Keys KEYINPUT/KEYCNT on both CPUs as on the GBA (10 buttons, keypad IRQ). EXTKEYIN (ARM7 4000136h): X, Y, debug button, pen down (the TSC's /PENIRQ), hinge closed. The hinge raises IF bit 22 on the ARM7. ## Power control POWCNT1 (ARM9 4000304h): LCDs, 2D engine A, 3D rendering, 3D geometry, 2D engine B, and which engine drives the top screen (bit 15). POWCNT2 (ARM7 4000304h): sound speakers (initially on), wireless (initially off; off also disables WIFIWAITCNT and the wireless ports). HALTCNT (ARM7 4000301h): halt, sleep, GBA mode. The power management device on SPI (peripherals.md) controls backlights, amplifier and power-off. ## Unknowns (U) - U1 Priority among the eight DMA channels, the sound fetches and capture, and the CPUs on main RAM (memory-map.md U2). - U2 DMA start latency after the trigger (gba-python measured the GBA's; the DS's is undocumented). - U3 Divider and square-root cycle counts: GBATEK's figures, unchecked by any test ROM we have (rockwrestler checks results only). - U4 Disabled engines: GBATEK says their ports become read-only and their palette memory "read-only-zero-filled"; what the screen shows and what reads of the other registers return is not said.