Pokémon GBA Save Format
In this page I hope to explain how save data is handled in GBA versions of Pokemon.
Data is split into blocks of 4096 bytes. Each block has a signature within the last 12 bytes that provides details of it such as block ID, save ID, checksum and a fixed value of 0x08012025 for validation.
All shorts/ints are stored in little endian.
Signature Structure: - unsigned char BLOCK ID - unsigned char {unknown} - unsigned short CHECKSUM - unsigned int VALIDATION CODE - unsigned int SAVE ID
Two files are stored - the current save and the previous save. If the current save is corrupt, the previous save is loaded instead, although I'm not certain how/when these saves are updated. The file overwritten when saving is always the oldest, so the most recent will jump between the two files.
The two files are split into 14 blocks and stored back to back. What you will see when you look at the files is 14 blocks of file 1 and then 14 blocks of file 2. The starting block will always vary but they will always be in order following that (blocks 00, 01, 02, etc will follow on from block 13).
To produce the save file as it is loaded into game memory, you must first gather the blocks that are most recent - so pick the blocks with the highest save ID - and then pull together the first 3968 bytes from each block in order (the number of bytes may vary - not sure).
The first few bits of info you'll find in the file are the player's trainer name and Trainer ID, as well as the so-called 'secret ID'. Further on you'll find the player's team and PC pokémon. These follow a fairly simple format that has been partially documented on Bulbapedia.
Data Crystal has a text conversion chart.
There is an additional block used by the Hall of Fame PC after the main 2 save files.
Various Knowledge
Here's a list of things I do know about the unpacked save data:
- The first 8 bytes are the trainer name
- The LSB of byte 0x09 is the trainer's gender- 0 for male, 1 for female
- Bytes 0x0A-0x0D are the trainer's ID, first short is the trainer ID, the full int is used as an encryption key for various things
- The pokémon in the party begin at offset 0x11B8 in R/S/E and 0x0FB8 in FrLg
- PC Boxes start at 0x4D84 - 30boxes x 30 pokémon x 80 bytes
- Box names are at 0xD0C4 - 9 characters each
- Inventory starts at offset 0x1418 in R/S/E and 0x1218 in FrLg
- Item quantities are encrypted in FrLg & Emerald but not in R/S - the encryption key (short) is at offset 0xAC in E and 0xF20 in FrLg
- All item data is stored in one long list with zeros for empty items (although even empty item slots have their quantities XOR'd)
- Bag structure is simply short itemtype, short quantity - in E/FrLg the quantity is XOR'd with the bag encryption key
Things I would really like to know
- How to determine which version/region your save file is from (I've seen that Pokemon Reader 2 did this)
- How the R/S berry patch is stored/executed - I'm assuming it's a very short ASM routine that Mods the RTC's counter so that R/S can read it properly
Special thanks to loadingNOW, Kyoufu Kawa and all contributers to Bulbapedia's GBA data structures pages.
If you have any questions or information that you think I would find to be useful, please contact me at pokemonresearch@furlocks-forest.net.