From d4eb5af332ce518baa552c886caa0bf3b2864f75 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 11 Jul 2017 19:19:49 -0400 Subject: Major reformatting, cleaning up black magic --- gba/source/gamedata.c | 56 ++-- gba/source/gamedata.h | 7 + gba/source/libSave.c | 596 ------------------------------------ gba/source/libSave.h | 27 -- gba/source/link.c | 23 +- gba/source/link.h | 9 +- gba/source/main.c | 485 +++++------------------------ source/encoding.c | 9 + source/encoding.h | 6 + source/link.c | 6 + source/link.h | 6 + source/main.c | 825 ++++++++++++-------------------------------------- 12 files changed, 348 insertions(+), 1707 deletions(-) delete mode 100644 gba/source/libSave.c delete mode 100644 gba/source/libSave.h diff --git a/gba/source/gamedata.c b/gba/source/gamedata.c index 8e63232..6868b2b 100644 --- a/gba/source/gamedata.c +++ b/gba/source/gamedata.c @@ -1,9 +1,9 @@ /* - * Pokemon Gen III Data Extractor by hatkirby 2017. + * Copyright (C) 2017 hatkirby + * Copyright (C) 2017 slipstream/RoL * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. - * */ #include "gamedata.h" @@ -245,9 +245,10 @@ bool initSaveData( } /// --- FR/LG --- - // In FR/LG, the function that initialises the save-block pointers to default does not set up saveblock3. - // Which will need to be set up before loading the save if we want boxed Pokémon to not disappear. - // Oh, and loadsave() offset is different between FR and LG... + // In FR/LG, the function that initialises the save-block pointers to + // default does not set up saveblock3. Which will need to be set up before + // loading the save if we want boxed Pokémon to not disappear. Oh, and + // loadsave() offset is different between FR and LG... case 'DRPB': // FireRed German case 'DGPB': // LeafGreen German @@ -376,12 +377,14 @@ bool initSaveData( { // LeafGreen v1.1 Japanese is undumped. // Therefore, it is unsupported. - // I will make guesses at the offsets in the comments, but I will not actually implement them until LeafGreen v1.1 is dumped. + // I will make guesses at the offsets in the comments, but I will + // not actually implement them until LeafGreen v1.1 is dumped. return false; } - loadsave = (void(*)(char)) 0x80db529; // potential LG1.1 address: 0x80db4fd + loadsave = (void(*)(char)) 0x80db529; + // potential LG1.1 address: 0x80db4fd //mainloop = (void(*)()) 0x8000417; //titlemid = 0x8078987; //load_pokemon = (void(*)()) 0x804b9c5; @@ -399,8 +402,10 @@ bool initSaveData( } /// --- Emerald --- - // In Emerald, the saveblock pointer that isn't set up is saveblock1 (in FR/LG it was saveblock3). - // The initial save loading code after the copyright screen is also updated, now it sets up ASLR/crypto here before loading the save. + // In Emerald, the saveblock pointer that isn't set up is saveblock1 (in + // FR/LG it was saveblock3). The initial save loading code after the + // copyright screen is also updated, now it sets up ASLR/crypto here before + // loading the save. case 'DEPB': // Emerald German { @@ -493,42 +498,15 @@ bool initSaveData( } loadsave(0); -// sendS32(-1); + // now the save is loaded, we can do what we want with the loaded blocks. - // first, we're going to want to decrypt the parts that are crypted, if applicable. + // first, we're going to want to decrypt the parts that are crypted, if + // applicable. decryptSaveStructures(gSaveBlock1,gSaveBlock2,gSaveBlock3); *SaveBlock1 = gSaveBlock1; *SaveBlock2 = gSaveBlock2; *SaveBlock3 = gSaveBlock3; - /* - // time to call the payload. - payload(gSaveBlock1,gSaveBlock2,gSaveBlock3); - // Now, we better call the function that sets the pokemon-related stuff from the structure elements of the loaded save again. - // Just in case the payload did something with that. - load_pokemon(); - // In FR/LG/Emerald, just returning to the game is unwise. - // The game reloads the savefile. - // In FR/LG, this is done at the title screen after setting ASLR/saveblock-crypto up. (probably because at initial save-load, SaveBlock3 ptr isn't set up lol) - // So, better bypass the title screen and get the game to return directly to the Continue/New Game screen. - // In Emerald, the save reload happens after the Continue option was chosen, so we have no choice but to bypass everything and get the game to go straight to the overworld. - // Easiest way to do this is to call into the middle of the function we want, using an ASM wrapper to set up the stack. - // Here goes... - if (titlemid) { - // Function reserves an extra 4 bytes of stack space in FireRed/LeafGreen, and none in Emerald. - call_into_middle_of_titlescreen_func(titlemid,(GAME_EM ? 0 : 4)); - } - // Now we've done what we want, time to return to the game. - // Can't just return, the game will reload the save. - // So let's just call the main-loop directly ;) - // turn the sound back on before we head back to the game - *(vu16 *)(REG_BASE + 0x84) = 0x8f; - // re-enable interrupts - REG_IME = 1; - mainloop(); - // Anything past here will not be executed. - return 0; - */ return true; } diff --git a/gba/source/gamedata.h b/gba/source/gamedata.h index 99dfa8e..4d0a2a9 100644 --- a/gba/source/gamedata.h +++ b/gba/source/gamedata.h @@ -1,3 +1,10 @@ +/* + * Copyright (C) 2017 hatkirby + * Copyright (C) 2017 slipstream/RoL + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #ifndef _GAMEDATA_H_ #define _GAMEDATA_H_ diff --git a/gba/source/libSave.c b/gba/source/libSave.c deleted file mode 100644 index e3bda1d..0000000 --- a/gba/source/libSave.c +++ /dev/null @@ -1,596 +0,0 @@ -/* - libSave - Cartridge backup memory save routines. To use, call the required - routine with a pointer to an appropriately sized array of data to - be read from or written to the cartridge. - Data types are from wintermute's gba_types.h libgba library. - Original file from SendSave by Chishm -*/ - -#include -#include -#include -#include - -#define EEPROM_ADDRESS (0xDFFFF00) -#define REG_EEPROM *(vu16 *)(EEPROM_ADDRESS) -#define REG_DMA3CNT_H *(vu16 *)(REG_BASE + 0x0de) -#define REG_WAITCNT *(vu16 *)(REG_BASE + 0x204) - -//----------------------------------------------------------------------- -// Common EEPROM Routines -//----------------------------------------------------------------------- - -void EEPROM_SendPacket( u16* packet, int size ) -{ - REG_WAITCNT = (REG_WAITCNT & 0xF8FF) | 0x0300; - REG_DMA3SAD = (u32)packet; - REG_DMA3DAD = EEPROM_ADDRESS; - REG_DMA3CNT = 0x80000000 + size; - while((REG_DMA3CNT_H & 0x8000) != 0) ; -} - -void EEPROM_ReceivePacket( u16* packet, int size ) -{ - REG_WAITCNT = (REG_WAITCNT & 0xF8FF) | 0x0300; - REG_DMA3SAD = EEPROM_ADDRESS; - REG_DMA3DAD = (u32)packet; - REG_DMA3CNT = 0x80000000 + size; - while((REG_DMA3CNT_H & 0x8000) != 0) ; -} - -//----------------------------------------------------------------------- -// Routines for 512B EEPROM -//----------------------------------------------------------------------- - -void EEPROM_Read_512B( volatile u8 offset, u8* dest ) // dest must point to 8 bytes -{ - u16 packet[68]; - u8* out_pos; - u16* in_pos; - u8 out_byte; - int byte, bit; - - memset( packet, 0, 68*2 ); - - // Read request - packet[0] = 1; - packet[1] = 1; - - // 6 bits eeprom address (MSB first) - packet[2] = (offset>>5)&1; - packet[3] = (offset>>4)&1; - packet[4] = (offset>>3)&1; - packet[5] = (offset>>2)&1; - packet[6] = (offset>>1)&1; - packet[7] = (offset)&1; - - // End of request - packet[8] = 0; - - // Do transfers - EEPROM_SendPacket( packet, 9 ); - memset( packet, 0, 68*2 ); - EEPROM_ReceivePacket( packet, 68 ); - - // Extract data - in_pos = &packet[4]; - out_pos = dest; - for( byte = 7; byte >= 0; --byte ) - { - out_byte = 0; - for( bit = 7; bit >= 0; --bit ) - { -// out_byte += (*in_pos++)<>5)&1; - packet[3] = (offset>>4)&1; - packet[4] = (offset>>3)&1; - packet[5] = (offset>>2)&1; - packet[6] = (offset>>1)&1; - packet[7] = (offset)&1; - - // Extract data - in_pos = source; - out_pos = &packet[8]; - for( byte = 7; byte >= 0; --byte ) - { - in_byte = *in_pos++; - for( bit = 7; bit >= 0; --bit ) - { - *out_pos++ = (in_byte>>bit)&1; - } - } - - // End of request - packet[72] = 0; - - // Do transfers - EEPROM_SendPacket( packet, 73 ); - - // Wait for EEPROM to finish (should timeout after 10 ms) - while( (REG_EEPROM & 1) == 0 ); -} - -//--------------------------------------------------------------------------------- -void GetSave_EEPROM_512B(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u8 x; - u32 sleep; - - for (x=0;x<64;++x){ - EEPROM_Read_512B(x,&data[x*8]); - for(sleep=0;sleep<512000;sleep++); - } -} - -//--------------------------------------------------------------------------------- -void PutSave_EEPROM_512B(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u8 x; - u32 sleep; - - for (x=0;x<64;x++){ - EEPROM_Write_512B(x,&data[x*8]); - for(sleep=0;sleep<512000;sleep++); - } -} -//----------------------------------------------------------------------- -// Routines for 8KB EEPROM -//----------------------------------------------------------------------- - -void EEPROM_Read_8KB( volatile u16 offset, u8* dest ) // dest must point to 8 bytes -{ - u16 packet[68]; - u8* out_pos; - u16* in_pos; - u8 out_byte; - int byte, bit; - - memset( packet, 0, 68*2 ); - - // Read request - packet[0] = 1; - packet[1] = 1; - - // 14 bits eeprom address (MSB first) - packet[2] = (offset>>13)&1; - packet[3] = (offset>>12)&1; - packet[4] = (offset>>11)&1; - packet[5] = (offset>>10)&1; - packet[6] = (offset>>9)&1; - packet[7] = (offset>>8)&1; - packet[8] = (offset>>7)&1; - packet[9] = (offset>>6)&1; - packet[10] = (offset>>5)&1; - packet[11] = (offset>>4)&1; - packet[12] = (offset>>3)&1; - packet[13] = (offset>>2)&1; - packet[14] = (offset>>1)&1; - packet[15] = (offset)&1; - - // End of request - packet[16] = 0; - - // Do transfers - EEPROM_SendPacket( packet, 17 ); - memset( packet, 0, 68*2 ); - EEPROM_ReceivePacket( packet, 68 ); - - // Extract data - in_pos = &packet[4]; - out_pos = dest; - for( byte = 7; byte >= 0; --byte ) - { - out_byte = 0; - for( bit = 7; bit >= 0; --bit ) - { -// out_byte += (*in_pos++)<>13)&1; - packet[3] = (offset>>12)&1; - packet[4] = (offset>>11)&1; - packet[5] = (offset>>10)&1; - packet[6] = (offset>>9)&1; - packet[7] = (offset>>8)&1; - packet[8] = (offset>>7)&1; - packet[9] = (offset>>6)&1; - packet[10] = (offset>>5)&1; - packet[11] = (offset>>4)&1; - packet[12] = (offset>>3)&1; - packet[13] = (offset>>2)&1; - packet[14] = (offset>>1)&1; - packet[15] = (offset)&1; - - // Extract data - in_pos = source; - out_pos = &packet[16]; - for( byte = 7; byte >= 0; --byte ) - { - in_byte = *in_pos++; - for( bit = 7; bit >= 0; --bit ) - { - *out_pos++ = (in_byte>>bit)&1; - } - } - - // End of request - packet[80] = 0; - - // Do transfers - EEPROM_SendPacket( packet, 81 ); - - // Wait for EEPROM to finish (should timeout after 10 ms) - while( (REG_EEPROM & 1) == 0 ); -} - -//--------------------------------------------------------------------------------- -void GetSave_EEPROM_8KB(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u16 x; - u32 sleep; - - for (x=0;x<1024;x++){ - EEPROM_Read_8KB(x,&data[x*8]); - for(sleep=0;sleep<512000;sleep++); - } - -} - -//--------------------------------------------------------------------------------- -void PutSave_EEPROM_8KB(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u16 x; - u32 sleep; - - for (x=0;x<1024;x++){ - EEPROM_Write_8KB(x,&data[x*8]); - for(sleep=0;sleep<512000;sleep++); - } -} - -//--------------------------------------------------------------------------------- -void GetSave_SRAM_32KB(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u8 *sram= (u8*) 0x0E000000; - volatile u16 x; - - for (x = 0; x < 0x8000; ++x) - { - data[x] = sram[x]; - } - -} - -//--------------------------------------------------------------------------------- -void PutSave_SRAM_32KB(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u8 *sram= (u8*) 0x0E000000; - volatile u16 x; - - for (x = 0; x < 0x8000; ++x) - { - sram[x] = data[x]; - } -} - -//--------------------------------------------------------------------------------- -void GetSave_FLASH_64KB(u8* data) -//--------------------------------------------------------------------------------- -{ - volatile u8 *sram= (u8*) 0x0E000000; - volatile u32 x; - - for (x = 0; x < 0x10000; ++x) - { - data[x] = sram[x]; - } -} - -//--------------------------------------------------------------------------------- -void PutSave_FLASH_64KB(u8* foo) -//--------------------------------------------------------------------------------- -{ - volatile u8 *fctrl0 = (u8*) 0xE005555; - volatile u8 *fctrl1 = (u8*) 0xE002AAA; - volatile u8 *fctrl2 = (u8*) 0xE000000; - - //init flash - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0x90; - *fctrl2 = 0xF0; - - //erase chip - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0x80; - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0x10; - - //wait for erase done - u8 val1; - u8 val2; - val1 = *fctrl2; - val2 = *fctrl2; - while (val1 != val2) { - val1 = *fctrl2; - val2 = *fctrl2; - } - val1 = *fctrl2; - val2 = *fctrl2; - while (val1 != val2) { - val1 = *fctrl2; - val2 = *fctrl2; - } - - volatile u8 *data = fctrl2; - u32 i; - //write data - for (i=0; i<65536; i++) { - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0xA0; - data [i] = foo [ i ]; - val1 = data [ i ]; - val2 = data [ i ]; - - while (val1 != val2) { - val1 = data [ i ]; - val2 = data [ i ]; - } - val1 = data [ i ]; - val2 = data [ i ]; - while (val1 != val2) { - val1 = data [ i ]; - val2 = data [ i ]; - } - val1 = data [ i ]; - val2 = data [ i ]; - while (val1 != val2) { - val1 = data [ i ]; - val2 = data [ i ]; - } - } -} - -//--------------------------------------------------------------------------------- -void GetSave_FLASH_128KB(u8* data) -//--------------------------------------------------------------------------------- -{ - const u32 size = 0x10000; - - volatile u8 *fctrl0 = (u8*) 0xE005555; - volatile u8 *fctrl1 = (u8*) 0xE002AAA; - volatile u8 *fctrl2 = (u8*) 0xE000000; - volatile u32 i; - volatile u8 *sram= (u8*) 0x0E000000; - - //init flash - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0x90; - *fctrl2 = 0xF0; - - // read first bank - *fctrl0 = 0xAA; - *fctrl1 = 0x55; - *fctrl0 = 0xB0; - *fctrl2 = 0x00; - - for (i=0; i=0;x--){ - switch (pak[x]) { - case 0x53414C46: - if (pak[x+1] == 0x5F4D3148){ - return 0x20000; // FLASH_128KB - } else if ((pak[x+1] & 0x0000FFFF) == 0x00005F48){ - return 0x10000; // FLASH_64KB - } else if (pak[x+1] == 0x32313548){ - return 0x10000; // FLASH_64KB - } - break; - case 0x52504545: - if ((pak[x+1] & 0x00FFFFFF) == 0x005F4D4F){ - GetSave_EEPROM_8KB(data); - for(i = 8; i < 0x800; i += 8) { - if(memcmp(data, data+i, 8) != 0) - return 0x2000; // EEPROM_8KB - } - return 0x200; // EEPROM_512B - } - break; - case 0x4D415253: - if ((pak[x+1] & 0x000000FF) == 0x0000005F){ - return 0x8000; // SRAM_32KB - } - } - } - return 0; -} - diff --git a/gba/source/libSave.h b/gba/source/libSave.h deleted file mode 100644 index 5ecf822..0000000 --- a/gba/source/libSave.h +++ /dev/null @@ -1,27 +0,0 @@ - - -//--------------------------------------------------------------------------------- -#ifdef __cplusplus -extern "C" { -#endif -//--------------------------------------------------------------------------------- - -void GetSave_EEPROM_512B(u8* data); -void PutSave_EEPROM_512B(u8* data); -void GetSave_EEPROM_8KB(u8* data); -void PutSave_EEPROM_8KB(u8* data); -void GetSave_SRAM_32KB(u8* data); -void PutSave_SRAM_32KB(u8* data); -void GetSave_FLASH_64KB(u8* data); -void PutSave_FLASH_64KB(u8* foo); -void GetSave_FLASH_128KB(u8* data); -void PutSave_FLASH_128KB(u8* foo); - -u32 SaveSize(u8* data, s32 gamesize); - - -//--------------------------------------------------------------------------------- -#ifdef __cplusplus -} // extern "C" -#endif -//--------------------------------------------------------------------------------- diff --git a/gba/source/link.c b/gba/source/link.c index e695622..c84e44d 100644 --- a/gba/source/link.c +++ b/gba/source/link.c @@ -1,13 +1,20 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #include "link.h" #define JOY_WRITE 2 #define JOY_READ 4 #define JOY_RW 6 -void waitForWriteAccess() +void initializeLink() { - //while ((REG_HS_CTRL & JOY_READ) == 0); - while ((REG_HS_CTRL & JOY_WRITE) == 0); + REG_HS_CTRL |= JOY_RW; + REG_JOYTR = 0; + while ((REG_HS_CTRL & JOY_WRITE) == 0); REG_HS_CTRL |= JOY_RW; } @@ -23,19 +30,9 @@ void waitForAck() void sendS32(s32 val) { REG_JOYTR = val; - //waitForWriteAccess(); } void sendU32(u32 val) { REG_JOYTR = val; - //waitForWriteAccess(); -} - -u32 recieveU32() -{ - while ((REG_HS_CTRL & JOY_WRITE) == 0); - REG_HS_CTRL |= JOY_RW; - return REG_JOYRE; } - diff --git a/gba/source/link.h b/gba/source/link.h index f18b38a..08fd998 100644 --- a/gba/source/link.h +++ b/gba/source/link.h @@ -1,12 +1,17 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #ifndef _LINK_H_ #define _LINK_H_ #include -void waitForWriteAccess(); +void initializeLink(); void waitForAck(); void sendS32(s32 val); void sendU32(u32 val); -u32 recieveU32(); #endif diff --git a/gba/source/main.c b/gba/source/main.c index 94d4c2b..9f97324 100644 --- a/gba/source/main.c +++ b/gba/source/main.c @@ -1,435 +1,110 @@ /* - * Copyright (C) 2016 FIX94 + * Copyright (C) 2017 hatkirby * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ #include -#include -#include -#include "libSave.h" #include "gamedata.h" #include "link.h" -#define REG_WAITCNT *(vu16 *)(REG_BASE + 0x204) -#define JOY_WRITE 2 -#define JOY_READ 4 -#define JOY_RW 6 - -u8 save_data[0x20000] __attribute__ ((section (".sbss"))); - -s32 getGameSize(void) -{ - if(*(vu32*)(0x08000004) != 0x51AEFF24) - return -1; - s32 i; - for(i = (1<<20); i < (1<<25); i<<=1) - { - vu16 *rompos = (vu16*)(0x08000000+i); - int j; - bool romend = true; - for(j = 0; j < 0x1000; j++) - { - if(rompos[j] != j) - { - romend = false; - break; - } - } - if(romend) break; - } - return i; -} - - -// === (from tonc_memdef.h) =========================================== - -// --- REG_DISPCNT defines --- -#define DCNT_MODE0 0x0000 -#define DCNT_MODE1 0x0001 -#define DCNT_MODE2 0x0002 -#define DCNT_MODE3 0x0003 -#define DCNT_MODE4 0x0004 -#define DCNT_MODE5 0x0005 -// layers -#define DCNT_BG0 0x0100 -#define DCNT_BG1 0x0200 -#define DCNT_BG2 0x0400 -#define DCNT_BG3 0x0800 -#define DCNT_OBJ 0x1000 -typedef u16 COLOR; -#define MEM_VRAM 0x06000000 -#define SCREEN_WIDTH 240 -#define vid_mem ((u16*)MEM_VRAM) -static inline void m3_plot(int x, int y, COLOR clr) -{ vid_mem[y*SCREEN_WIDTH+x]= clr; } -static inline COLOR RGB15(u32 red, u32 green, u32 blue) -{ return red | (green<<5) | (blue<<10); } -void plot_sqr(int x, int y, COLOR clr) -{ - /*for (int j=0;j<8; j++) - { - for (int i=0;i<8; i++) - { - vid_mem[(y*8+j+32)*SCREEN_WIDTH+x*8+i+32] = clr; - } - } - vid_mem[(y*8+1+32)*SCREEN_WIDTH+x*8+1+32] = RGB15(31,31,31);*/ -} -void m3_fill(COLOR clr) -{ - /*int ii; - u32 *dst= (u32*)vid_mem; - u32 wd= (clr<<16) | clr; - - for(ii=0; iirs.playerName; - } else if (GAME_FRLG) - { - trainerName = SaveBlock2->frlg.playerName; - } else if (GAME_EM) - { - trainerName = SaveBlock2->e.playerName; - } + return 0; + } - u32 tn1 = - (trainerName[0] << 24) - | (trainerName[1] << 16) - | (trainerName[2] << 8) - | (trainerName[3]); + sendS32(1); + waitForAck(); - u32 tn2 = - (trainerName[4] << 24) - | (trainerName[5] << 16) - | (trainerName[6] << 8) - | (trainerName[7]); + // Send trainer name. + u8* trainerName = 0; - sendU32(tn1); - waitForAck(); + if (GAME_RS) + { + trainerName = SaveBlock2->rs.playerName; + } else if (GAME_FRLG) + { + trainerName = SaveBlock2->frlg.playerName; + } else if (GAME_EM) + { + trainerName = SaveBlock2->e.playerName; + } - sendU32(tn2); - waitForAck(); + u32 tn1 = + (trainerName[0] << 24) + | (trainerName[1] << 16) + | (trainerName[2] << 8) + | (trainerName[3]); - // Send trainer ID. - - u8* trainerId = 0; - if (GAME_RS) - { - trainerId = SaveBlock2->rs.playerTrainerId; - } else if (GAME_FRLG) - { - trainerId = SaveBlock2->frlg.playerTrainerId; - } else if (GAME_EM) - { - trainerId = SaveBlock2->e.playerTrainerId; - } + u32 tn2 = + (trainerName[4] << 24) + | (trainerName[5] << 16) + | (trainerName[6] << 8) + | (trainerName[7]); - u32 tti = - (trainerId[1] << 8) - | (trainerId[0]); + sendU32(tn1); + waitForAck(); - //iprintf("sending trainer id %ld\n", tti); - sendU32(tti); - waitForAck(); + sendU32(tn2); + waitForAck(); - // Restart, because we're just testing. - sendS32(0); - //continue; - break; + // Send trainer ID. + u8* trainerId = 0; + if (GAME_RS) + { + trainerId = SaveBlock2->rs.playerTrainerId; + } else if (GAME_FRLG) + { + trainerId = SaveBlock2->frlg.playerTrainerId; + } else if (GAME_EM) + { + trainerId = SaveBlock2->e.playerTrainerId; + } -/* + u32 tti = + (trainerId[1] << 8) + | (trainerId[0]); + sendU32(tti); + waitForAck(); - //game in, send header - for(i = 0; i < 0xC0; i+=4) - { - REG_JOYTR = *(vu32*)(0x08000000+i); - while((REG_HS_CTRL&JOY_READ) == 0) ; - REG_HS_CTRL |= JOY_RW; - } - REG_JOYTR = 0; - //wait for other side to choose - while((REG_HS_CTRL&JOY_WRITE) == 0) ; - REG_HS_CTRL |= JOY_RW; - u32 choseval = REG_JOYRE; - if(choseval == 0) - { - REG_JOYTR = 0; - continue; //nothing to read - } - else if(choseval == 1) - { - //disable interrupts - u32 prevIrqMask = REG_IME; - REG_IME = 0; - //dump the game - for(i = 0; i < gamesize; i+=4) - { - REG_JOYTR = *(vu32*)(0x08000000+i); - while((REG_HS_CTRL&JOY_READ) == 0) ; - REG_HS_CTRL |= JOY_RW; - } - //restore interrupts - REG_IME = prevIrqMask; - } - else if(choseval == 2) - { - //disable interrupts - u32 prevIrqMask = REG_IME; - REG_IME = 0; - //backup save - switch (savesize){ - case 0x200: - GetSave_EEPROM_512B(save_data); - break; - case 0x2000: - GetSave_EEPROM_8KB(save_data); - break; - case 0x8000: - GetSave_SRAM_32KB(save_data); - break; - case 0x10000: - GetSave_FLASH_64KB(save_data); - break; - case 0x20000: - GetSave_FLASH_128KB(save_data); - break; - default: - break; - } - //restore interrupts - REG_IME = prevIrqMask; - //say gc side we read it - REG_JOYTR = savesize; - //wait for a cmd receive for safety - while((REG_HS_CTRL&JOY_WRITE) == 0) ; - REG_HS_CTRL |= JOY_RW; - //send the save - for(i = 0; i < savesize; i+=4) - { - REG_JOYTR = *(vu32*)(save_data+i); - while((REG_HS_CTRL&JOY_READ) == 0) ; - REG_HS_CTRL |= JOY_RW; - } - } - else if(choseval == 3 || choseval == 4) - { - REG_JOYTR = savesize; - if(choseval == 3) - { - //receive the save - for(i = 0; i < savesize; i+=4) - { - while((REG_HS_CTRL&JOY_WRITE) == 0) ; - REG_HS_CTRL |= JOY_RW; - *(vu32*)(save_data+i) = REG_JOYRE; - } - } - else - { - //clear the save - for(i = 0; i < savesize; i+=4) - *(vu32*)(save_data+i) = 0; - } - //disable interrupts - u32 prevIrqMask = REG_IME; - REG_IME = 0; - //write it - switch (savesize){ - case 0x200: - PutSave_EEPROM_512B(save_data); - break; - case 0x2000: - PutSave_EEPROM_8KB(save_data); - break; - case 0x8000: - PutSave_SRAM_32KB(save_data); - break; - case 0x10000: - PutSave_FLASH_64KB(save_data); - break; - case 0x20000: - PutSave_FLASH_128KB(save_data); - break; - default: - break; - } - //restore interrupts - REG_IME = prevIrqMask; - //say gc side we're done - REG_JOYTR = 0; - //wait for a cmd receive for safety - while((REG_HS_CTRL&JOY_WRITE) == 0) ; - REG_HS_CTRL |= JOY_RW; - } - REG_JOYTR = 0; - } - } else if(REG_HS_CTRL&JOY_WRITE) - { - REG_HS_CTRL |= JOY_RW; - u32 choseval = REG_JOYRE; - if(choseval == 5) - { - //disable interrupts - u32 prevIrqMask = REG_IME; - REG_IME = 0; - //dump BIOS - for (i = 0; i < 0x4000; i+=4) - { - // the lower bits are inaccurate, so just get it four times :) - u32 a = MidiKey2Freq((WaveData *)(i-4), 180-12, 0) * 2; - u32 b = MidiKey2Freq((WaveData *)(i-3), 180-12, 0) * 2; - u32 c = MidiKey2Freq((WaveData *)(i-2), 180-12, 0) * 2; - u32 d = MidiKey2Freq((WaveData *)(i-1), 180-12, 0) * 2; - REG_JOYTR = ((a>>24<<24) | (d>>24<<16) | (c>>24<<8) | (b>>24)); - while((REG_HS_CTRL&JOY_READ) == 0) ; - REG_HS_CTRL |= JOY_RW; - } - //restore interrupts - REG_IME = prevIrqMask; - } - REG_JOYTR = 0; - }*/ - Halt(); - } + // Halt(); } - - diff --git a/source/encoding.c b/source/encoding.c index a69fc7e..7b70df2 100644 --- a/source/encoding.c +++ b/source/encoding.c @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + * + * Gen 3 proprietary encoding information from Bulbapedia: + * https://bulbapedia.bulbagarden.net/wiki/Character_encoding_in_Generation_III + */ #include "encoding.h" const char charmap[] = { diff --git a/source/encoding.h b/source/encoding.h index be99895..4dc3b1f 100644 --- a/source/encoding.h +++ b/source/encoding.h @@ -1,3 +1,9 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #ifndef ENCODING_H_95547384 #define ENCODING_H_95547384 diff --git a/source/link.c b/source/link.c index 7229191..4178293 100644 --- a/source/link.c +++ b/source/link.c @@ -1,3 +1,9 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #include "link.h" u32 waitForButtons(u32 mask) diff --git a/source/link.h b/source/link.h index 4e536b4..24a60b5 100644 --- a/source/link.h +++ b/source/link.h @@ -1,3 +1,9 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ #ifndef _LINK_H_ #define _LINK_H_ diff --git a/source/main.c b/source/main.c index dfcdc3b..8a9e672 100644 --- a/source/main.c +++ b/source/main.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2017 hatkirby + * Copyright (C) 2017 slipstream/RoL * Copyright (C) 2016 FIX94 * * This software may be modified and distributed under the terms @@ -7,15 +8,10 @@ */ #include #include -#include +#include #include #include -#include -#include -#include -#include -#include -#include +#include #include "link.h" #include "encoding.h" @@ -31,9 +27,8 @@ void printmain() printf("\x1b[2J"); printf("\x1b[37m"); printf("Pokemon Gen III Data Extractor by hatkirby\n"); + printf("Based on gba-gen3multiboot by slipstream/RoL\n"); printf("Based on GBA Link Cable Dumper v1.6 by FIX94\n"); - printf("Save Support based on SendSave by Chishm\n"); - printf("Save Structure based on gba-gen3multiboot by slipstream/RoL\n"); } u8 *resbuf,*cmdbuf; @@ -49,98 +44,24 @@ void acb(s32 res, u32 val) { resval = val; } -/* -unsigned int docrc(u32 crc, u32 val) -{ - int i; - for (i = 0; i < 0x20; i++) - { - if ((crc ^ val) & 1) - { - crc>>=1; - crc^=0xa1c1; - } else { - crc>>=1; - } - - val>>=1; - } - return crc; -}*/ -unsigned int docrc(u32 crc,u32 val) { +unsigned int docrc(u32 crc,u32 val) +{ u32 result; result = val ^ crc; - for (int i = 0; i < 0x20; i++) { - if (result & 1) { + for (int i = 0; i < 0x20; i++) + { + if (result & 1) + { result >>= 1; result ^= 0xA1C1; - } else result >>= 1; + } else { + result >>= 1; + } } - return result; -} - -void fixFName(char *str) -{ - u8 i = 0; - for (i = 0; i < strlen(str); ++i) - { - if (str[i] < 0x20 || str[i] > 0x7F) - { - str[i] = '_'; - } else { - switch (str[i]) - { - case '\\': - case '/': - case ':': - case '*': - case '?': - case '\"': - case '<': - case '>': - case '|': - str[i] = '_'; - break; - default: - break; - } - } - } -} - -unsigned int calckey(unsigned int size) -{ - unsigned int ret = 0; - size= (size - 0x200) >> 3; - int res1 = (size & 0x3F80) << 1; - res1 |= (size & 0x4000) << 2; - res1 |= (size & 0x7F); - res1 |= 0x380000; - int res2 = res1; - res1 = res2 >> 0x10; - int res3 = res2 >> 8; - res3 += res1; - res3 += res2; - res3 <<= 24; - res3 |= res2; - res3 |= 0x80808080; - - if ((res3 & 0x200) == 0) - { - ret |= (((res3) & 0xFF) ^ 0x4B) << 24; - ret |= (((res3>>8) & 0xFF) ^ 0x61) << 16; - ret |= (((res3>>16) & 0xFF) ^ 0x77) << 8; - ret |= (((res3>>24) & 0xFF) ^ 0x61); - } else { - ret |= (((res3) & 0xFF) ^ 0x73) << 24; - ret |= (((res3>>8) & 0xFF) ^ 0x65) << 16; - ret |= (((res3>>16) & 0xFF) ^ 0x64) << 8; - ret |= (((res3>>24) & 0xFF) ^ 0x6F); - } - return ret; + return result; } void doreset() @@ -170,17 +91,6 @@ void endproc() exit(0); } -void fsleep(int i) -{ - sleep(i); - /*PAD_ScanPads(); - if (PAD_ButtonsDown(0) & PAD_BUTTON_START) - { - getstatus(); - endproc(); - }*/ -} - u32 recv() { memset(resbuf,0,32); @@ -208,29 +118,6 @@ void send(u32 msg) while (transval == 0); } -bool dirExists(const char *path) -{ - DIR *dir = opendir(path); - if (dir) - { - closedir(dir); - - return true; - } - - return false; -} - -void createFile(const char *path, size_t size) -{ - int fd = open(path, O_WRONLY | O_CREAT); - if (fd >= 0) - { - ftruncate(fd, size); - close(fd); - } -} - void warnError(char *msg) { puts(msg); @@ -248,64 +135,98 @@ void fatalError(char *msg) exit(0); } -u32 genKeyA() { +u32 genKeyA() +{ u32 retries = 0; - while (true) { + + for (;;) + { u32 key = 0; - if (retries > 32) { + + if (retries > 32) + { key = 0xDD654321; } else { key = (rand() & 0x00ffffff) | 0xDD000000; } + u32 unk = (key % 2 != 0); u32 v12 = key; - for (u32 v13 = 1; v13 < 32; v13++) { + for (u32 v13 = 1; v13 < 32; v13++) + { v12 >>= 1; unk += (v12 % 2 != 0); } - if ((unk >= 10 && unk <= 24)) { - if (retries > 4) printf("KeyA retries = %ld",retries); - printf("KeyA = 0x%08lx\n",key); + + if ((unk >= 10 && unk <= 24)) + { + if (retries > 4) + { + printf("KeyA retries = %ld", retries); + } + + printf("KeyA = 0x%08lx\n", key); + return key; } + retries++; } } -u32 checkKeyB(u32 KeyBRaw) { - if ((KeyBRaw & 0xFF) != 0xEE) { - printf("Invalid KeyB - lowest 8 bits should be 0xEE, actually 0x%02x\n",((u8)(KeyBRaw))); +u32 checkKeyB(u32 KeyBRaw) +{ + if ((KeyBRaw & 0xFF) != 0xEE) + { + printf("Invalid KeyB - lowest 8 bits should be 0xEE, actually 0x%02x\n", + ((u8)(KeyBRaw))); + return 0; } + u32 KeyB = KeyBRaw & 0xffffff00; u32 val = KeyB; u32 unk = (val < 0); - for (u32 i = 1; i < 24; i++) { + for (u32 i = 1; i < 24; i++) + { val <<= 1; unk += (val < 0); } - if (unk > 14) { - printf("Invalid KeyB - high 24 bits bad: 0x%08lx\n",KeyB); + + if (unk > 14) + { + printf("Invalid KeyB - high 24 bits bad: 0x%08lx\n", KeyB); + return 0; } - printf("Valid KeyB: 0x%08lx\n",KeyB); + + printf("Valid KeyB: 0x%08lx\n", KeyB); + return KeyB; } -u32 deriveKeyC(u32 keyCderive, u32 kcrc) { +u32 deriveKeyC(u32 keyCderive, u32 kcrc) +{ u32 keyc = 0; u32 keyCi = 0; - do { + + do + { u32 v5 = 0x1000000 * keyCi - 1; u32 keyCattempt = docrc(kcrc,v5); - //printf("i = %d; keyCderive = %08x; keyCattempt = %08x\n",keyCi,keyCderive,keyCattempt); - if (keyCderive == keyCattempt) { + + if (keyCderive == keyCattempt) + { keyc = v5; + printf("Found keyC: %08lx\n",keyc); + return keyc; } + keyCi++; } while (keyCi < 256); + return keyc; } @@ -315,12 +236,13 @@ u32 getMsg() while (val == 0) { val = __builtin_bswap32(recv()); - fsleep(1); + sleep(1); } + send(0); - while (recv()!=0) {fsleep(1);}; + while (recv()!=0) {sleep(1);}; send(0); - + return val; } @@ -345,33 +267,17 @@ int main(int argc, char *argv[]) PAD_Init(); cmdbuf = memalign(32,32); resbuf = memalign(32,32); - u8 *testdump = memalign(32,0x400000); - if(!testdump) return 0; - /*if (!fatInitDefault()) + for (;;) { printmain(); - fatalError("ERROR: No usable device found to write dumped files to!"); - } - mkdir("/dumps", S_IREAD | S_IWRITE); - if (!dirExists("/dumps")) - { - printmain(); - fatalError("ERROR: Could not create dumps folder, make sure you have a supported device connected!"); - }*/ - - int i; - while(1) - { - printmain(); - printf("Press A to begin, press Start to quit.\n"); if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_START) & PAD_BUTTON_START) { endproc(); } - + printf("Waiting for a GBA in port 2...\n"); resval = 0; @@ -401,63 +307,86 @@ int main(int argc, char *argv[]) if (resval & SI_GBA) { - printf("GBA Found! Waiting on BIOS\n"); + printf("GBA Found! Waiting on BIOS\n"); + resbuf[2]=0; - //u32 oldresult = 0; - //u32 newresult = 0; + // wait for the BIOS to hand over to the game - do { + do + { doreset(); - } while (!(resbuf[1] > 4)); + printf("BIOS handed over to game, waiting on game\n"); + do { doreset(); - } while((resbuf[0] != 0) || !(resbuf[2]&0x10)); + } while ((resbuf[0] != 0) || !(resbuf[2] & 0x10)); + // receive the game-code from GBA side. u32 gamecode = recv(); + printf("Ready, sending multiboot ROM\n"); + unsigned int sendsize = ((gba_mb_gba_size+7)&~7); + // generate KeyA unsigned int ourkey = genKeyA(); + //printf("Our Key: %08x\n", ourkey); - printf("Sending game code that we got: 0x%08lx\n",__builtin_bswap32(gamecode)); + printf("Sending game code that we got: 0x%08lx\n", + __builtin_bswap32(gamecode)); + // send the game code back, then KeyA. send(__builtin_bswap32(gamecode)); send(ourkey); - // get KeyB from GBA, check it to make sure its valid, then xor with KeyA to derive the initial CRC value and the sessionkey. + + // get KeyB from GBA, check it to make sure its valid, then xor with KeyA + // to derive the initial CRC value and the sessionkey. u32 sessionkeyraw = 0; - do { + do + { sessionkeyraw = recv(); } while (sessionkeyraw == gamecode); + sessionkeyraw = checkKeyB(__builtin_bswap32(sessionkeyraw)); if (sessionkeyraw == 0) { warnError("Cannot continue.\n"); - + continue; } - + u32 sessionkey = sessionkeyraw ^ ourkey; u32 kcrc = sessionkey; printf("start kCRC=%08lx\n",kcrc); + sessionkey = (sessionkey*0x6177614b)+1; + // send hacked up send-size in uint32s u32 hackedupsize = (sendsize >> 3) - 1; + printf("Sending hacked up size 0x%08lx\n",hackedupsize); send(hackedupsize); + //unsigned int fcrc = 0x00bb; - // send over multiboot binary header, in the clear until the end of the nintendo logo. - // GBA checks this, if nintendo logo does not match the one in currently inserted cart's ROM, it will not accept any more data. - for(i = 0; i < 0xA0; i+=4) { + // send over multiboot binary header, in the clear until the end of the + // nintendo logo. GBA checks this, if nintendo logo does not match the + // one in currently inserted cart's ROM, it will not accept any more data. + for (int i = 0; i < 0xA0; i+=4) + { vu32 rom_dword = *(vu32*)(gba_mb_gba+i); send(__builtin_bswap32(rom_dword)); } + printf("\n"); printf("Header done! Sending ROM...\n"); - // Add each uint32 of the multiboot image to the checksum, encrypt the uint32 with the session key, increment the session key, send the encrypted uint32. - for(i = 0xA0; i < sendsize; i+=4) + + // Add each uint32 of the multiboot image to the checksum, encrypt the + // uint32 with the session key, increment the session key, send the + // encrypted uint32. + for (int i = 0xA0; i < sendsize; i+=4) { u32 dec = ( (((gba_mb_gba[i+3]) << 24) & 0xff000000) | @@ -465,490 +394,136 @@ int main(int argc, char *argv[]) (((gba_mb_gba[i+1]) << 8) & 0x0000ff00) | (((gba_mb_gba[i]) << 0) & 0x000000ff) ); + u32 enc = (dec - kcrc) ^ sessionkey; - kcrc=docrc(kcrc,dec); - sessionkey = (sessionkey*0x6177614B)+1; + kcrc = docrc(kcrc,dec); + sessionkey = (sessionkey * 0x6177614B) + 1; //enc^=((~(i+(0x20<<20)))+1); //enc^=0x6f646573;//0x20796220; + send(enc); } + //fcrc |= (sendsize<<16); printf("ROM done! CRC: %08lx\n", kcrc); //get crc back (unused) + // Get KeyC derivation material from GBA (eventually) u32 keyCderive = 0; - do { + do + { keyCderive = recv(); } while (keyCderive <= 0xfeffffff); + keyCderive = __builtin_bswap32(keyCderive); keyCderive >>= 8; - printf("KeyC derivation material: %08lx\n",keyCderive); - // (try to) find the KeyC, using the checksum of the multiboot image, and the derivation material that GBA sent to us + printf("KeyC derivation material: %08lx\n",keyCderive); + // (try to) find the KeyC, using the checksum of the multiboot image, and + // the derivation material that GBA sent to us u32 keyc = deriveKeyC(keyCderive,kcrc); if (keyc == 0) - { - printf("Could not find keyC - kcrc=0x%08lx\n",kcrc); - warnError("Cannot continue.\n"); - continue; - } + { + printf("Could not find keyC - kcrc=0x%08lx\n",kcrc); + warnError("Cannot continue.\n"); - // derive the boot key from the found KeyC, and send to GBA. if this is not correct, GBA will not jump to the multiboot image it was sent. + continue; + } + + // derive the boot key from the found KeyC, and send to GBA. if this is + // not correct, GBA will not jump to the multiboot image it was sent. u32 bootkey = docrc(0xBB,keyc) | 0xbb000000; printf("BootKey = 0x%08lx\n",bootkey); - send(bootkey); - /* - printf("GBA Found! Waiting on BIOS...\n"); - resbuf[2]=0; - while (!(resbuf[2] & 0x10)) - { - doreset(); - getstatus(); - } + send(bootkey); + sleep(2); - printf("Ready, sending extractor.\n"); + printf("Waiting for GBA...\n"); + while (recv() != 0) {sleep(1);}; + send(0); - unsigned int sendsize = ((gba_mb_gba_size + 7) & ~7); - unsigned int ourkey = calckey(sendsize); + VIDEO_WaitVSync(); - //get current sessionkey - u32 sessionkeyraw = recv(); - u32 sessionkey = __builtin_bswap32(sessionkeyraw ^ 0x7365646F); + // Get game + // -1 - unsupported game + // 1 - Ruby + // 2 - Sapphire + // 3 - FireRed + // 4 - LeafGreen + // 5 - Emerald + u32 gameId = getMsg(); + if (gameId == -1) + { + warnError("ERROR: Unsupported GBA game inserted!\n"); - //send over our own key - send(__builtin_bswap32(ourkey)); - unsigned int fcrc = 0x15a0; + continue; + } - //send over gba header - for(i = 0; i < 0xC0; i+=4) + printf("\nPokemon "); + switch (gameId) { - send(__builtin_bswap32(*(vu32*)(gba_mb_gba+i))); + case 1: printf("Ruby"); break; + case 2: printf("Sapphire"); break; + case 3: printf("FireRed"); break; + case 4: printf("LeafGreen"); break; + case 5: printf("Emerald"); break; } - for (i = 0xC0; i < sendsize; i+=4) + printf("\n"); + VIDEO_WaitVSync(); + + u32 isValid = getMsg(); + if (isValid == -1) { - u32 enc = ( - (gba_mb_gba[i+3] << 24) - | (gba_mb_gba[i+2] << 16) - | (gba_mb_gba[i+1] << 8) - | (gba_mb_gba[i])); - - fcrc = docrc(fcrc,enc); - sessionkey = (sessionkey * 0x6177614B) + 1; - enc ^= sessionkey; - enc ^= ((~(i + (0x20 << 20))) + 1); - enc ^= 0x20796220; - - send(enc); - } + warnError("ERROR: Unsupported game version inserted!\n"); - fcrc |= (sendsize<<16); + continue; + } - //send over CRC - sessionkey = (sessionkey * 0x6177614B) + 1; - fcrc ^= sessionkey; - fcrc ^= ((~(i + (0x20 << 20))) + 1); - fcrc ^= 0x20796220; + // Get trainer name + u8 trainerName[8]; - send(fcrc); + u32 tnd = getMsg(); + trainerName[0] = (tnd & 0xFF000000) >> 24; + trainerName[1] = (tnd & 0x00FF0000) >> 16; + trainerName[2] = (tnd & 0x0000FF00) >> 8; + trainerName[3] = (tnd & 0x000000FF); - //get crc back (unused) - recv(); - printf("Done!\n"); - sleep(2); + tnd = getMsg(); + trainerName[4] = (tnd & 0xFF000000) >> 24; + trainerName[5] = (tnd & 0x00FF0000) >> 16; + trainerName[6] = (tnd & 0x0000FF00) >> 8; + trainerName[7] = (tnd & 0x000000FF); - //hm - while (1) - { - printmain(); - printf("Press A once you have a GBA Game inserted.\n \n"); + // Get trainer ID + u32 trainerId = getMsg(); - PAD_ScanPads(); - VIDEO_WaitVSync(); - u32 btns = PAD_ButtonsDown(0); - if (btns & PAD_BUTTON_START) - { - endproc(); - } else if (btns & PAD_BUTTON_A) - {*/ - sleep(2); - //recv(); - - //if (recv() == 0) //ready - //{ - {{ - printf("Waiting for GBA...\n"); - while (recv() != 0) {fsleep(1);}; - send(0); - - VIDEO_WaitVSync(); - - /*int gbasize = 0; - while(gbasize == 0) - { - gbasize = __builtin_bswap32(recv()); - } - - send(0); //got gbasize - while (recv()!=0) {sleep(1);}; - - //u32 savesize = __builtin_bswap32(recv()); - //send(0); //got savesize - - if (gbasize == -1) - { - warnError("ERROR: No (Valid) GBA Card inserted!\n"); - - continue; - }*/ - - // Get game - // -1 - unsupported game - // 1 - Ruby - // 2 - Sapphire - // 3 - FireRed - // 4 - LeafGreen - // 5 - Emerald - u32 gameId = 0; - while (gameId == 0) - { - gameId = __builtin_bswap32(recv()); - fsleep(1); - } - - send(0); - while (recv()!=0) {fsleep(1);}; - send(0); - //sleep(1); - - if (gameId == -1) - { - warnError("ERROR: Unsupported GBA game inserted!\n"); - - continue; - } - - printf("\nPokemon "); - switch (gameId) - { - case 1: printf("Ruby"); break; - case 2: printf("Sapphire"); break; - case 3: printf("FireRed"); break; - case 4: printf("LeafGreen"); break; - case 5: printf("Emerald"); break; - } - - printf("\n"); - VIDEO_WaitVSync(); - - u32 isValid = 0; - while (isValid == 0) - { - isValid = __builtin_bswap32(recv()); - fsleep(1); - } - - if (isValid == -1) - { - //send(0); - - warnError("ERROR: Unsupported game version inserted!\n"); - - continue; - } - - send(0); - while (recv()!=0) {fsleep(1);}; - send(0); - //sleep(1); - - // Get trainer name - u8 trainerName[8]; - - u32 tnd = getMsg(); - //send(0); - trainerName[0] = (tnd & 0xFF000000) >> 24; - trainerName[1] = (tnd & 0x00FF0000) >> 16; - trainerName[2] = (tnd & 0x0000FF00) >> 8; - trainerName[3] = (tnd & 0x000000FF); - - tnd = getMsg(); - //send(0); - trainerName[4] = (tnd & 0xFF000000) >> 24; - trainerName[5] = (tnd & 0x00FF0000) >> 16; - trainerName[6] = (tnd & 0x0000FF00) >> 8; - trainerName[7] = (tnd & 0x000000FF); - - // Get trainer ID - u32 trainerId = 0; - while (trainerId == 0) - { - trainerId = __builtin_bswap32(recv()); - fsleep(1); - } - send(0); - while (recv()!=0) {fsleep(1);}; - send(0); - //sleep(1); - - printf("Trainer: "); - - for (int i = 0; i < 8; i++) - { - if (trainerName[i] == 0xFF) - { - break; - } else { - printf("%c", debugGen3Decode(trainerName[i])); - } - } - - printf(" (%ld)\n", trainerId); - - //continue; - - // Wait for confirmation. - printf("Press A to import the data from this game.\n"); - printf("Press B to cancel.\n"); - VIDEO_WaitVSync(); - - if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_B) & PAD_BUTTON_B) - { - continue; - } + printf("Trainer: "); + for (int i = 0; i < 8; i++) + { + if (trainerName[i] == 0xFF) + { + break; + } else { + printf("%c", debugGen3Decode(trainerName[i])); + } + } + printf(" (%ld)\n", trainerId); + // Wait for confirmation. + printf("Press A to import the data from this game.\n"); + printf("Press B to cancel.\n"); + VIDEO_WaitVSync(); -/* - //get rom header - for(i = 0; i < 0xC0; i+=4) - *(vu32*)(testdump+i) = recv(); - //print out all the info from the game - printf("Game Name: %.12s\n",(char*)(testdump+0xA0)); - printf("Game ID: %.4s\n",(char*)(testdump+0xAC)); - printf("Company ID: %.2s\n",(char*)(testdump+0xB0)); - printf("ROM Size: %02.02f MB\n",((float)(gbasize/1024))/1024.f); - if(savesize > 0) - printf("Save Size: %02.02f KB\n \n",((float)(savesize))/1024.f); - else - printf("No Save File\n \n"); - //generate file paths - char gamename[64]; - sprintf(gamename,"/dumps/%.12s [%.4s%.2s].gba", - (char*)(testdump+0xA0),(char*)(testdump+0xAC),(char*)(testdump+0xB0)); - fixFName(gamename+7); //fix name behind "/dumps/" - char savename[64]; - sprintf(savename,"/dumps/%.12s [%.4s%.2s].sav", - (char*)(testdump+0xA0),(char*)(testdump+0xAC),(char*)(testdump+0xB0)); - fixFName(savename+7); //fix name behind "/dumps/" - //let the user choose the option - printf("Press A to dump this game, it will take about %i minutes.\n",gbasize/1024/1024*3/2); - printf("Press B if you want to cancel dumping this game.\n"); - if(savesize > 0) - { - printf("Press Y to backup this save file.\n"); - printf("Press X to restore this save file.\n"); - printf("Press Z to clear the save file on the GBA Cartridge.\n\n"); - } - else - printf("\n"); - - int command = 0; - while(1) - { - PAD_ScanPads(); - VIDEO_WaitVSync(); - u32 btns = PAD_ButtonsDown(0); - if(btns&PAD_BUTTON_START) - endproc(); - else if(btns&PAD_BUTTON_A) - { - command = 1; - break; - } - else if(btns&PAD_BUTTON_B) - break; - else if(savesize > 0) - { - if(btns&PAD_BUTTON_Y) - { - command = 2; - break; - } - else if(btns&PAD_BUTTON_X) - { - command = 3; - break; - } - else if(btns&PAD_TRIGGER_Z) - { - command = 4; - break; - } - } - } - if(command == 1) - { - FILE *f = fopen(gamename,"rb"); - if(f) - { - fclose(f); - command = 0; - warnError("ERROR: Game already dumped!\n"); - } - } - else if(command == 2) - { - FILE *f = fopen(savename,"rb"); - if(f) - { - fclose(f); - command = 0; - warnError("ERROR: Save already backed up!\n"); - } - } - else if(command == 3) - { - size_t readsize = 0; - FILE *f = fopen(savename,"rb"); - if(f) - { - fseek(f,0,SEEK_END); - readsize = ftell(f); - if(readsize != savesize) - { - command = 0; - warnError("ERROR: Save has the wrong size, aborting restore!\n"); - } - else - { - rewind(f); - fread(testdump,readsize,1,f); - } - fclose(f); - } - else - { - command = 0; - warnError("ERROR: No Save to restore!\n"); - } - } - send(command); - //let gba prepare - sleep(1); - if(command == 0) - continue; - else if(command == 1) - { - //create base file with size - printf("Preparing file...\n"); - createFile(gamename,gbasize); - FILE *f = fopen(gamename,"wb"); - if(!f) - fatalError("ERROR: Could not create file! Exit..."); - printf("Dumping...\n"); - u32 bytes_read = 0; - while(gbasize > 0) - { - int toread = (gbasize > 0x400000 ? 0x400000 : gbasize); - int j; - for(j = 0; j < toread; j+=4) - { - *(vu32*)(testdump+j) = recv(); - bytes_read+=4; - if((bytes_read&0xFFFF) == 0) - printf("\r%02.02f MB done",(float)(bytes_read/1024)/1024.f); - } - fwrite(testdump,toread,1,f); - gbasize -= toread; - } - printf("\nClosing file\n"); - fclose(f); - printf("Game dumped!\n"); - sleep(5); - } - else if(command == 2) - { - //create base file with size - printf("Preparing file...\n"); - createFile(savename,savesize); - FILE *f = fopen(savename,"wb"); - if(!f) - fatalError("ERROR: Could not create file! Exit..."); - printf("Waiting for GBA\n"); - VIDEO_WaitVSync(); - u32 readval = 0; - while(readval != savesize) - readval = __builtin_bswap32(recv()); - send(0); //got savesize - printf("Receiving...\n"); - for(i = 0; i < savesize; i+=4) - *(vu32*)(testdump+i) = recv(); - printf("Writing save...\n"); - fwrite(testdump,savesize,1,f); - fclose(f); - printf("Save backed up!\n"); - sleep(5); - } - else if(command == 3 || command == 4) - { - u32 readval = 0; - while(readval != savesize) - readval = __builtin_bswap32(recv()); - if(command == 3) - { - printf("Sending save\n"); - VIDEO_WaitVSync(); - for(i = 0; i < savesize; i+=4) - send(__builtin_bswap32(*(vu32*)(testdump+i))); - } - printf("Waiting for GBA\n"); - while(recv() != 0) - VIDEO_WaitVSync(); - printf(command == 3 ? "Save restored!\n" : "Save cleared!\n"); - send(0); - sleep(5); - }*/ - } - } - /*else if(btns&PAD_BUTTON_Y) - { - const char *biosname = "/dumps/gba_bios.bin"; - FILE *f = fopen(biosname,"rb"); - if(f) - { - fclose(f); - warnError("ERROR: BIOS already backed up!\n"); - } - else - { - //create base file with size - printf("Preparing file...\n"); - createFile(biosname,0x4000); - f = fopen(biosname,"wb"); - if(!f) - fatalError("ERROR: Could not create file! Exit..."); - //send over bios dump command - send(5); - //the gba might still be in a loop itself - sleep(1); - //lets go! - printf("Dumping...\n"); - for(i = 0; i < 0x4000; i+=4) - *(vu32*)(testdump+i) = recv(); - fwrite(testdump,0x4000,1,f); - printf("Closing file\n"); - fclose(f); - printf("BIOS dumped!\n"); - sleep(5); - } - }*/ - } -// } -// } + if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_B) & PAD_BUTTON_B) + { + continue; + } + } } + return 0; } -- cgit 1.4.1