Rendered from docs/CODES.md in the repository.
The DUO's own menu can start a game with codes, but typing them in on the GBA is slow and the DUO
arrives with none. duo_codes.py does the same job from the PC: paste codes into a text file, check
them, and start the game with them. It uses Datel's own routines, so the codes behave exactly as they
would from Datel's menu.
python duo_codes.py decrypt codes.txt # each code decrypted, with what it does
python duo_codes.py check codes.txt --game AXVE # master code present? hook right for the game?
python duo_codes.py make --game AXVE --rev 1 # ready-made Ruby/Sapphire codes (money, coins)
python duo_codes.py master 0x08000430 # a master code for a hook
python duo_codes.py hook game.gba # find a game's master-code hook in your own ROM
python duo_codes.py launch codes.txt # dry run: build the launch program
python duo_codes.py launch codes.txt --write-block # start the game on the DUO with the codes
A codes file is plain text. Each code is one line of two 8-digit hex words, and a line without a code
names the codes under it. # starts a comment. Codes can be encrypted (as published for the AR MAX
and AR V3) or raw. The tool tells them apart by the master code or ID code at the top; --raw or
--encrypted says which if that doesn't work.
Decryption: Datel's own
All checked against Datel's 1.06 firmware (see AR_ENGINE.md):
- Cipher: AR MAX/V3 codes are TEA with 32 rounds. The decrypt routine is at 0x0800C488.
- Default keys:
7AA9648F 7FAE6994 C0EFAAD5 42712C57. Datel's 0x0800C440 builds them from two seed bytes (0x02000010, 0x02000014), both zero by default. It writes them to 0x02000020. - DEADFACE codes: a code that decrypts to address
DEADFACEsets the seeds from its value (byte 1 and byte 0) and makes new keys for the codes after it. - The keys come from two 256-byte tables in Datel's image: 0x0800EF4C, and the IWRAM copy at 0x03002138.
duo_codes.pyreads those tables from your ownfirmware/build/fw.binat run time; they aren't in the repository.- The generator matches Datel's routine, run in an emulator, for every seed pair tested.
- ID codes (value
001DC0DE, address = the game code) only name the game, so they're left out of the list. - Test vectors, Datel's published Emerald codes:
D8BAE4D9 4864DCE5→C40005EC 00008401, the master code (hook 0x080005EC, type 1).A86CDBA5 19BA49B3→45455042 001DC0DE, the ID code (BPEE).
Master codes and hooks
Every list starts with a master code. Its address word is 0xC4000000 | hook. In its value word, the
low byte is the hook type, bits 8–14 are codes per call and bit 15 is a flag. Datel's own codes use
00008401.
The hook is where the game reads the buttons (ReadKeys). The same instructions appear in every Gen 3 game, so one byte pattern finds the hook in any of these ROMs:
| Game | Hook |
|---|---|
| Emerald (all languages) | 0x080005EC |
| FireRed (BPRE rev 0) | 0x080005F0 |
| LeafGreen (BPGE rev 1) | 0x08000604 |
| Ruby/Sapphire USA rev 1 (AXVE/AXPE) | 0x08000430 |
| Ruby USA rev 2 (AXVE, from the cartridge in session 39) | 0x08000430 |
| Ruby Japan (AXVJ) | 0x08000434 |
| Ruby Spain, Germany, France, Italy | 0x08000564 |
Other revisions can move the hook. duo_codes.py hook game.gba finds it in your own ROM, and a lab
read of the cartridge's first few KB finds it on the cartridge itself (session 39).
Codes made from the game's memory
Ruby and Sapphire keep their save blocks at fixed addresses (gSaveBlock1 = 0x02025734). So a few useful codes can be written straight from pret's symbols, without hunting for published ones:
| Code | Raw | What it does |
|---|---|---|
| Max money | 04225BC4 000F423F |
32-bit write of 999,999 to 0x02025BC4 (SaveBlock1 + 0x490) |
| Max coins | 02225BC8 0000270F |
16-bit write of 9,999 to 0x02025BC8 (SaveBlock1 + 0x494) |
A raw code's address keeps bits 0–19 and moves the top nibble into bits 20–23 (0x02025BC4 → 0x00225BC4). Bits 25–26 are the size (0 = 8-bit, 1 = 16-bit, 2 = 32-bit) and bits 27–29 the type (0 = write). In an emulator, Datel's real engine applies both codes.
Launching
launch builds a small program for the Reckless firmware, run from RAM with command 0x23. It does two
things:
- It copies Datel's engine from its image in the DUO's flash (0x0802E510) to IWRAM 0x03002244, where Datel's start-up would have put it. In server mode it isn't there.
- It calls Datel's start-with-codes routine (0x0800C5F8) with the decrypted list, at most 200 lines.
From there it's Datel's own code:
- It writes the 8 KB code block at DUO flash 0x08020000: trampoline at +0x20, engine at +0x400, codes at +0x9C0. Datel's menu rewrites this same block every time it starts a game with codes.
- It sets the patch slots so the hook reads
BL 0x08000020. - It starts the game.
The launch writes that one block of the DUO's flash and nothing else. The cartridge save isn't touched.
The tool runs a dry run unless you give it --write-block, and then asks you to type YES.
On hardware (session 40, 28 November), Ruby USA rev 2 started from the PC with the master code and max money, and the game showed 999,999.
Tests (python tests/test_codes.py) cover:
- the published vectors;
- DEADFACE against Datel's routine;
- Datel's engine applying the ready-made codes;
- the whole launch on the simulated DUO. Datel's routine writes its engine and the codes into the block and patches the hook.