# The DS cartridge: protocol, KEY1 and KEY2, saves; and the GBA slot Source: GBATEK "DS Cartridges, Encryption, Firmware" (Header, Secure Area, Protocol, Backup, NAND, I/O Ports, Encryption by Gamecode/Idcode (KEY1), Encryption by Random Seed (KEY2), Cart Infrared, GBA Slot accessories). DOC unless marked. Census figures from census.md. ## The slot and its registers The DS slot is a serial 8-bit bus (ROM) plus a separate 1-bit SPI bus (save chip), owned by one CPU at a time (EXMEMCNT bit 11). Registers (on the owner's side): AUXSPICNT 40001A0h (slot enable, ROM or SPI mode, SPI baud and hold, transfer-ready IRQ), AUXSPIDATA 40001A2h, **ROMCTRL 40001A4h** (KEY1 gap lengths, KEY2 enables and seed apply, data-ready, block size 0/200h-4000h/4 bytes, clock 6.7 or 4.2 MHz = bus / 5 or / 8, reset release, write direction, start/busy), the 8-byte command at 40001A8h, the KEY2 seeds at 40001B0h-40001BBh, and the data port 4100010h (read by the CPU or by DMA in DS slot mode). The transfer-done IRQ is IF bit 19; IREQ_MC is bit 20. **Timing is observable**: the data-ready bit and DMA requests come at a rate set by the clock bit and gap lengths, so how long a block takes is part of what software sees (games stream data while running). ROMCTRL words in the census: 00416657h (1T-ROM, slower) 3,567 headers, 00416017h 3,543, 00586000h (MROM, faster) 489. GBATEK warns some carts declare 1T-ROM values but hold MROM chips (chip ID bit 31 tells). ## The protocol (three phases) 1. **Unencrypted**: 9Fh dummy, 00h get header (200h bytes; 1000h on DSi), 90h get chip ID, 3Ch activate KEY1 (with random parameters). 2. **KEY1-encrypted commands** (the secure-area phase): 4xh activate KEY2, 1xh chip ID, 2xh get secure-area block (4 KB, blocks 4-7 for 4000h-7FFFh), Axh enter main data mode; commands are Blowfish-encrypted with a key derived from the **gamecode** and the 1048h-byte table in the **ARM7 BIOS** (00000030h-00001077h); the replies are KEY2- encrypted. Each KEY1 command is preceded (chip ID bit 31 = 0) or followed (bit 31 = 1) by the header's secure-area delay. 3. **KEY2 main data**: B7h read (200h bytes from addresses 8000h up), B8h chip ID; KEY2 is a pair of 39-bit LFSR-like registers seeded from the command's random value and the header's seed select (all 7,626 DS images use seed select 0), XORed into commands and data. GBATEK: B7h "can access only 8000h and up", and retail cartridges "silently redirect address below 4000h to addr=8000h+(addr AND 1FFh)" (what 4000h-7FFFh returns to B7h is U3). ## The secure area and the dumps The first 2 KB of the secure area (4000h-47FFh) carry a second KEY1 layer and start with the ID "encryObj"; the BIOS decrypts, checks the ID and overwrites it with E7FFDEFFh x2 (on failure, the whole 2 KB). **7,457 of the 7,626 No-Intro "Decrypted" images hold that destroyed ID**: they are the secure area as RAM held it after a boot, not as the cartridge sends it. So: - **A cartridge model that serves a BIOS boot** must re-create the cartridge's view: restore "encryObj", re-encrypt the first 2 KB with KEY1 (the inverse of the BIOS's decryption, GBATEK gives both directions), and encrypt replies with KEY2. That needs the ARM7 BIOS's key table, i.e. the player's BIOS. - **A direct boot** (the board loads the ARM9 and ARM7 binaries from the image per the header, sets the post-boot state the BIOS and firmware leave, and jumps) needs no encryption for the boot itself, but games that later read the secure area or issue KEY1 commands (anti-piracy checks) still see the cartridge. The 121 zero-filled secure areas (devkit style) and 32 without a secure area boot through either path without re-encryption. ## Saves (the separate SPI bus) EEPROM 0.5 KB (9-bit addresses), 8 KB and 64 KB (16-bit), 128 KB (24-bit); FLASH 256 KB, 512 KB, 1 MB, 8 MB (one title); FRAM 8 KB and 32 KB (GBATEK "DS Cartridge Backup", with command sets per type). Status register WIP/WEL/WP bits; FLASH page program and erase take time (busy bit). Infrared cartridges (48 "I" gamecodes in the census: the Pokémon pedometer games and others) put an IR chip on the same SPI bus, with a 00h prefix byte for the FLASH; NAND cartridges (some "U" gamecodes) store saves in a NAND area of the ROM chip itself (header 094h-097h, commands B2h/B5h/D6h and friends). **The save type is not in the header or the No-Intro DAT** (census.md). Options: detect it from the commands the game sends (GBATEK's per-type command differences make most types distinguishable after a few accesses, but not sizes in every case); a database keyed by gamecode (third-party lists exist; each needs a licence check); or the player chooses. Open decision in handoff-brief.md. ## The GBA slot in NDS mode An address window (08000000h-0AFFFFFFh) with programmable access times (EXMEMCNT), owned by one CPU, used by DS games for accessories: the Rumble Pak and the slider with rumble, the Memory Expansion Pak (RAM), the Guitar Hero grip, the Motion Pack, the paddle and others GBATEK lists, and by games that read a GBA cartridge (Pokémon "dual-slot" bonuses). An empty slot returns open bus (memory-map.md). Accessories are late and each its own small device behind a socket. ## Unknowns (U) and contradictions (C) - U1 The exact transfer timing per byte and per gap (GBATEK gives the clock and gap lengths; the data-ready edge timing per word is not given). - U2 Chip ID values per cartridge type (needed for anti-piracy checks and the 1T-ROM/MROM difference); the dumps do not carry them. - U3 What a game sees if it reads the secure area again after boot (the cartridge would refuse KEY1 commands outside that phase? GBATEK: commands are phase-specific). - U4 Save type and size per title (above). - U5 NAND cartridge command set and timing (GBATEK marks several "whatever"). - C1 GBATEK's header table says the ARM9 ROM offset is "4000h and up", but 32 DS images have it at 8000h or above (no secure area) and GBATEK's own "Avoiding Secure Area Encryption" describes those layouts: not a contradiction of behaviour, a reminder that the header table states Nintendo's convention, not the hardware's limit.