finish load_save.c

This commit is contained in:
ProjectRevoTPP 2017-11-22 21:06:30 -05:00
parent adba0bacfc
commit 9644f44982
4 changed files with 183 additions and 583 deletions

View File

@ -1,573 +0,0 @@
.include "asm/macros.inc"
.include "constants/constants.inc"
.syntax unified
.text
thumb_func_start sav2_x1_query_bit1
sav2_x1_query_bit1: @ 8076D24
ldr r0, =gSaveBlock2Ptr
ldr r0, [r0]
ldrb r1, [r0, 0x9]
movs r0, 0x1
ands r0, r1
bx lr
.pool
thumb_func_end sav2_x1_query_bit1
thumb_func_start sav2_x9_clear_bit1
sav2_x9_clear_bit1: @ 8076D34
ldr r0, =gSaveBlock2Ptr
ldr r2, [r0]
ldrb r1, [r2, 0x9]
movs r0, 0xFE
ands r0, r1
strb r0, [r2, 0x9]
bx lr
.pool
thumb_func_end sav2_x9_clear_bit1
thumb_func_start sub_8076D48
sub_8076D48: @ 8076D48
ldr r0, =gSaveBlock2Ptr
ldr r2, [r0]
ldrb r1, [r2, 0x9]
movs r0, 0x1
orrs r0, r1
strb r0, [r2, 0x9]
bx lr
.pool
thumb_func_end sub_8076D48
thumb_func_start sub_8076D5C
sub_8076D5C: @ 8076D5C
push {lr}
movs r0, 0
bl sub_8084FAC
ldr r0, =gSaveBlock2Ptr
ldr r2, [r0]
ldrb r1, [r2, 0x9]
movs r0, 0x1
orrs r0, r1
strb r0, [r2, 0x9]
pop {r0}
bx r0
.pool
thumb_func_end sub_8076D5C
thumb_func_start sav2_gender2_inplace_and_xFE
sav2_gender2_inplace_and_xFE: @ 8076D78
ldr r0, =gSaveBlock2Ptr
ldr r2, [r0]
ldrb r1, [r2, 0x9]
movs r0, 0xFE
ands r0, r1
strb r0, [r2, 0x9]
bx lr
.pool
thumb_func_end sav2_gender2_inplace_and_xFE
thumb_func_start copy_player_party_to_sav1
@ void copy_player_party_to_sav1()
copy_player_party_to_sav1: @ 8076D8C
push {r4,lr}
ldr r0, =gSaveBlock1Ptr
ldr r0, [r0]
ldr r1, =gPlayerPartyCount
ldrb r1, [r1]
movs r2, 0x8D
lsls r2, 2
adds r0, r2
strb r1, [r0]
movs r4, 0
_08076DA0:
ldr r0, =gSaveBlock1Ptr
ldr r0, [r0]
movs r1, 0x64
muls r1, r4
adds r0, r1
movs r2, 0x8E
lsls r2, 2
adds r0, r2
ldr r2, =gPlayerParty
adds r1, r2
movs r2, 0x64
bl memcpy
adds r4, 0x1
cmp r4, 0x5
ble _08076DA0
pop {r4}
pop {r0}
bx r0
.pool
thumb_func_end copy_player_party_to_sav1
thumb_func_start copy_player_party_from_sav1
copy_player_party_from_sav1: @ 8076DD4
push {r4,r5,lr}
ldr r1, =gPlayerPartyCount
ldr r0, =gSaveBlock1Ptr
ldr r0, [r0]
movs r2, 0x8D
lsls r2, 2
adds r0, r2
ldrb r0, [r0]
strb r0, [r1]
movs r4, 0
ldr r5, =gPlayerParty
_08076DEA:
movs r0, 0x64
adds r2, r4, 0
muls r2, r0
adds r0, r2, r5
ldr r1, =gSaveBlock1Ptr
ldr r1, [r1]
adds r1, r2
movs r2, 0x8E
lsls r2, 2
adds r1, r2
movs r2, 0x64
bl memcpy
adds r4, 0x1
cmp r4, 0x5
ble _08076DEA
pop {r4,r5}
pop {r0}
bx r0
.pool
thumb_func_end copy_player_party_from_sav1
thumb_func_start save_serialize_npcs
save_serialize_npcs: @ 8076E1C
push {r4-r7,lr}
mov r7, r8
push {r7}
ldr r0, =gSaveBlock1Ptr
mov r12, r0
movs r5, 0xA3
lsls r5, 4
mov r8, r5
ldr r4, =gMapObjects
movs r3, 0
movs r2, 0xF
_08076E32:
mov r6, r12
ldr r1, [r6]
adds r1, r3
add r1, r8
adds r0, r4, 0
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
adds r4, 0x24
adds r3, 0x24
subs r2, 0x1
cmp r2, 0
bge _08076E32
pop {r3}
mov r8, r3
pop {r4-r7}
pop {r0}
bx r0
.pool
thumb_func_end save_serialize_npcs
thumb_func_start save_deserialize_npcs
save_deserialize_npcs: @ 8076E64
push {r4-r7,lr}
mov r7, r8
push {r7}
ldr r0, =gSaveBlock1Ptr
mov r12, r0
movs r5, 0xA3
lsls r5, 4
mov r8, r5
ldr r4, =gMapObjects
movs r3, 0
movs r2, 0xF
_08076E7A:
mov r6, r12
ldr r1, [r6]
adds r1, r3
adds r0, r4, 0
add r1, r8
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
adds r4, 0x24
adds r3, 0x24
subs r2, 0x1
cmp r2, 0
bge _08076E7A
pop {r3}
mov r8, r3
pop {r4-r7}
pop {r0}
bx r0
.pool
thumb_func_end save_deserialize_npcs
thumb_func_start SaveSerializedGame
SaveSerializedGame: @ 8076EAC
push {lr}
bl copy_player_party_to_sav1
bl save_serialize_npcs
pop {r0}
bx r0
thumb_func_end SaveSerializedGame
thumb_func_start LoadSerializedGame
LoadSerializedGame: @ 8076EBC
push {lr}
bl copy_player_party_from_sav1
bl save_deserialize_npcs
pop {r0}
bx r0
thumb_func_end LoadSerializedGame
thumb_func_start copy_bags_and_unk_data_from_save_blocks
copy_bags_and_unk_data_from_save_blocks: @ 8076ECC
push {r4-r7,lr}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
movs r2, 0
ldr r0, =gUnknown_02032180
mov r12, r0
ldr r1, =gSaveBlock2Ptr
mov r10, r1
ldr r5, =gSaveBlock1Ptr
movs r3, 0xAC
lsls r3, 3
ldr r4, =gUnknown_02031C58
_08076EE8:
lsls r0, r2, 2
ldr r1, [r5]
adds r1, r3
adds r1, r0
ldr r0, [r1]
stm r4!, {r0}
adds r2, 0x1
cmp r2, 0x1D
ble _08076EE8
movs r2, 0
ldr r0, =gUnknown_02031C58
ldr r5, =gSaveBlock1Ptr
movs r3, 0xBB
lsls r3, 3
adds r4, r0, 0
adds r4, 0x78
_08076F08:
lsls r0, r2, 2
ldr r1, [r5]
adds r1, r3
adds r1, r0
ldr r0, [r1]
stm r4!, {r0}
adds r2, 0x1
cmp r2, 0x1D
ble _08076F08
movs r2, 0
ldr r0, =gUnknown_02031C58
ldr r5, =gSaveBlock1Ptr
movs r3, 0xCA
lsls r3, 3
adds r4, r0, 0
adds r4, 0xF0
_08076F28:
lsls r0, r2, 2
ldr r1, [r5]
adds r1, r3
adds r1, r0
ldr r0, [r1]
stm r4!, {r0}
adds r2, 0x1
cmp r2, 0xF
ble _08076F28
movs r2, 0
ldr r0, =gUnknown_02031C58
ldr r5, =gSaveBlock1Ptr
movs r6, 0x98
lsls r6, 1
adds r4, r0, r6
movs r3, 0xD2
lsls r3, 3
_08076F4A:
lsls r0, r2, 2
ldr r1, [r5]
adds r1, r3
adds r1, r0
ldr r0, [r1]
stm r4!, {r0}
adds r2, 0x1
cmp r2, 0x3F
ble _08076F4A
movs r2, 0
ldr r0, =gUnknown_02031C58
ldr r5, =gSaveBlock1Ptr
movs r7, 0x8C
lsls r7, 2
adds r4, r0, r7
movs r3, 0xF2
lsls r3, 3
_08076F6C:
lsls r0, r2, 2
ldr r1, [r5]
adds r1, r3
adds r1, r0
ldr r0, [r1]
stm r4!, {r0}
adds r2, 0x1
cmp r2, 0x2D
ble _08076F6C
ldr r0, =gUnknown_02031C58
ldr r1, =gSaveBlock1Ptr
mov r8, r1
movs r5, 0xBA
lsls r5, 2
adds r4, r0, r5
movs r3, 0
ldr r6, =0x00002be0
mov r9, r6
movs r2, 0xF
_08076F92:
mov r7, r8
ldr r1, [r7]
adds r1, r3
adds r0, r4, 0
add r1, r9
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
ldm r1!, {r5-r7}
stm r0!, {r5-r7}
adds r4, 0x24
adds r3, 0x24
subs r2, 0x1
cmp r2, 0
bge _08076F92
mov r1, r10
ldr r0, [r1]
adds r0, 0xAC
ldr r0, [r0]
mov r5, r12
str r0, [r5]
pop {r3-r5}
mov r8, r3
mov r9, r4
mov r10, r5
pop {r4-r7}
pop {r0}
bx r0
.pool
thumb_func_end copy_bags_and_unk_data_from_save_blocks
thumb_func_start copy_bags_and_unk_data_to_save_blocks
copy_bags_and_unk_data_to_save_blocks: @ 8076FE0
push {r4-r7,lr}
mov r7, r10
mov r6, r9
mov r5, r8
push {r5-r7}
movs r2, 0
ldr r0, =gSaveBlock2Ptr
mov r8, r0
ldr r1, =gUnknown_02032180
mov r12, r1
ldr r5, =gSaveBlock1Ptr
movs r3, 0xAC
lsls r3, 3
ldr r4, =gUnknown_02031C58
_08076FFC:
ldr r0, [r5]
lsls r1, r2, 2
adds r0, r3
adds r0, r1
ldm r4!, {r1}
str r1, [r0]
adds r2, 0x1
cmp r2, 0x1D
ble _08076FFC
movs r2, 0
ldr r5, =gSaveBlock1Ptr
movs r4, 0xBB
lsls r4, 3
ldr r0, =gUnknown_02031C58
adds r3, r0, 0
adds r3, 0x78
_0807701C:
ldr r0, [r5]
lsls r1, r2, 2
adds r0, r4
adds r0, r1
ldm r3!, {r1}
str r1, [r0]
adds r2, 0x1
cmp r2, 0x1D
ble _0807701C
movs r2, 0
ldr r5, =gSaveBlock1Ptr
movs r4, 0xCA
lsls r4, 3
ldr r0, =gUnknown_02031C58
adds r3, r0, 0
adds r3, 0xF0
_0807703C:
ldr r0, [r5]
lsls r1, r2, 2
adds r0, r4
adds r0, r1
ldm r3!, {r1}
str r1, [r0]
adds r2, 0x1
cmp r2, 0xF
ble _0807703C
movs r2, 0
ldr r5, =gSaveBlock1Ptr
movs r4, 0xD2
lsls r4, 3
ldr r0, =gUnknown_02031C58
movs r6, 0x98
lsls r6, 1
adds r3, r0, r6
_0807705E:
ldr r0, [r5]
lsls r1, r2, 2
adds r0, r4
adds r0, r1
ldm r3!, {r1}
str r1, [r0]
adds r2, 0x1
cmp r2, 0x3F
ble _0807705E
movs r2, 0
ldr r5, =gSaveBlock1Ptr
movs r4, 0xF2
lsls r4, 3
ldr r0, =gUnknown_02031C58
movs r7, 0x8C
lsls r7, 2
adds r3, r0, r7
_08077080:
ldr r0, [r5]
lsls r1, r2, 2
adds r0, r4
adds r0, r1
ldm r3!, {r1}
str r1, [r0]
adds r2, 0x1
cmp r2, 0x2D
ble _08077080
ldr r0, =gSaveBlock1Ptr
mov r9, r0
ldr r1, =0x00002be0
mov r10, r1
ldr r4, =gUnknown_02031C58
movs r3, 0
movs r2, 0xF
_080770A0:
mov r5, r9
ldr r1, [r5]
adds r1, r3
add r1, r10
movs r6, 0xBA
lsls r6, 2
adds r0, r4, r6
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
ldm r0!, {r5-r7}
stm r1!, {r5-r7}
adds r4, 0x24
adds r3, 0x24
subs r2, 0x1
cmp r2, 0
bge _080770A0
mov r7, r8
ldr r0, [r7]
adds r0, 0xAC
ldr r4, [r0]
mov r2, r12
ldr r1, [r2]
str r1, [r0]
adds r0, r4, 0
bl ApplyNewEncyprtionKeyToBagItems
ldr r0, [r7]
adds r0, 0xAC
str r4, [r0]
pop {r3-r5}
mov r8, r3
mov r9, r4
mov r10, r5
pop {r4-r7}
pop {r0}
bx r0
.pool
thumb_func_end copy_bags_and_unk_data_to_save_blocks
thumb_func_start ApplyNewEncyprtionKeyToHword
ApplyNewEncyprtionKeyToHword: @ 8077100
ldr r2, =gSaveBlock2Ptr
ldr r2, [r2]
adds r2, 0xAC
ldr r2, [r2]
ldrh r3, [r0]
eors r2, r3
eors r2, r1
strh r2, [r0]
bx lr
.pool
thumb_func_end ApplyNewEncyprtionKeyToHword
thumb_func_start ApplyNewEncyprtionKeyToWord
ApplyNewEncyprtionKeyToWord: @ 8077118
ldr r2, =gSaveBlock2Ptr
ldr r3, [r2]
adds r3, 0xAC
ldr r2, [r0]
ldr r3, [r3]
eors r2, r3
eors r2, r1
str r2, [r0]
bx lr
.pool
thumb_func_end ApplyNewEncyprtionKeyToWord
thumb_func_start ApplyNewEncyprtionKeyToAllEncryptedData
ApplyNewEncyprtionKeyToAllEncryptedData: @ 8077130
push {r4,r5,lr}
adds r4, r0, 0
bl ApplyNewEncyprtionKeyToGameStats
adds r0, r4, 0
bl ApplyNewEncyprtionKeyToBagItems_
adds r0, r4, 0
bl ApplyNewEncyprtionKeyToBerryPowder
ldr r5, =gSaveBlock1Ptr
ldr r0, [r5]
movs r1, 0x92
lsls r1, 3
adds r0, r1
adds r1, r4, 0
bl ApplyNewEncyprtionKeyToWord
ldr r0, [r5]
ldr r1, =0x00000494
adds r0, r1
adds r1, r4, 0
bl ApplyNewEncyprtionKeyToHword
pop {r4,r5}
pop {r0}
bx r0
.pool
thumb_func_end ApplyNewEncyprtionKeyToAllEncryptedData
.align 2, 0 @ Don't pad with nop.

View File

@ -83,7 +83,6 @@ SECTIONS {
asm/smokescreen.o(.text);
asm/pokeball.o(.text);
src/load_save.o(.text);
asm/load_save.o(.text);
asm/trade.o(.text);
src/berry_blender.o(.text);
asm/berry_blender.o(.text);

View File

@ -10,11 +10,27 @@ extern void* gUnknown_0203CF5C;
extern bool16 IdentifyFlash(void);
extern void SetBagItemsPointers(void);
extern void SetDecorationInventoriesPointers(void);
extern void ApplyNewEncyprtionKeyToGameStats(u32 key);
extern void ApplyNewEncyprtionKeyToBagItems(u32 newKey);
extern void ApplyNewEncyprtionKeyToBagItems_(u32 key);
extern void ApplyNewEncyprtionKeyToBerryPowder(u32 key);
extern void sub_8084FAC(int unused);
void ApplyNewEncyprtionKeyToAllEncryptedData(u32 encryptionKey);
// this is probably wrong or misleading due to it being used in ResetHeap...
extern void InitHeap(void *pointer, u32 size);
#define SAVEBLOCK_MOVE_RANGE 128
struct LoadedSaveData
{
/*0x0000*/ struct ItemSlot items[30];
/*0x0078*/ struct ItemSlot keyItems[30];
/*0x00F0*/ struct ItemSlot pokeBalls[16];
/*0x0130*/ struct ItemSlot TMsHMs[64];
/*0x0230*/ struct ItemSlot berries[46];
/*0x02E8*/ struct MailStruct mail[16];
};
EWRAM_DATA struct SaveBlock2 gSaveblock2 = {0};
EWRAM_DATA u8 gSaveblock2_DMA[SAVEBLOCK_MOVE_RANGE] = {0};
@ -24,7 +40,10 @@ EWRAM_DATA u8 gSaveblock1_DMA[SAVEBLOCK_MOVE_RANGE] = {0};
EWRAM_DATA struct PokemonStorage gPokemonStorage = {0};
EWRAM_DATA u8 gSaveblock3_DMA[SAVEBLOCK_MOVE_RANGE] = {0};
extern void InitHeap(void *pointer, u32 size);
EWRAM_DATA struct LoadedSaveData gLoadedSaveData = {0};
EWRAM_DATA u32 gLastEncryptionKey = {0};
void ApplyNewEncyprtionKeyToAllEncryptedData(u32 encryptionKey);
void CheckForFlashMemory(void)
{
@ -233,3 +252,164 @@ void MoveSaveBlocks_ResetHeap(void)
.syntax divided\n");
}
#endif
u8 sav2_x1_query_bit1(void)
{
return gSaveBlock2Ptr->specialSaveWarp & 1;
}
void sav2_x9_clear_bit1(void)
{
gSaveBlock2Ptr->specialSaveWarp &= ~1;
}
void sub_8076D48(void)
{
gSaveBlock2Ptr->specialSaveWarp |= 1;
}
void sub_8076D5C(void)
{
sub_8084FAC(0);
gSaveBlock2Ptr->specialSaveWarp |= 1;
}
void sav2_gender2_inplace_and_xFE(void)
{
gSaveBlock2Ptr->specialSaveWarp &= ~1;
}
void copy_player_party_to_sav1(void) // SavePlayerParty
{
int i;
gSaveBlock1Ptr->playerPartyCount = gPlayerPartyCount;
for (i = 0; i < 6; i++)
gSaveBlock1Ptr->playerParty[i] = gPlayerParty[i];
}
void copy_player_party_from_sav1(void) // LoadPlayerParty
{
int i;
gPlayerPartyCount = gSaveBlock1Ptr->playerPartyCount;
for (i = 0; i < 6; i++)
gPlayerParty[i] = gSaveBlock1Ptr->playerParty[i];
}
void save_serialize_npcs(void) // SaveMapObjects
{
int i;
for (i = 0; i < 16; i++)
gSaveBlock1Ptr->mapObjects[i] = gMapObjects[i];
}
void save_deserialize_npcs(void) // LoadMapObjects
{
int i;
for (i = 0; i < 16; i++)
gMapObjects[i] = gSaveBlock1Ptr->mapObjects[i];
}
void SaveSerializedGame(void)
{
copy_player_party_to_sav1();
save_serialize_npcs();
}
void LoadSerializedGame(void)
{
copy_player_party_from_sav1();
save_deserialize_npcs();
}
void copy_bags_and_unk_data_from_save_blocks(void)
{
int i;
// load player items.
for (i = 0; i < 30; i++)
gLoadedSaveData.items[i] = gSaveBlock1Ptr->bagPocket_Items[i];
// load player key items.
for (i = 0; i < 30; i++)
gLoadedSaveData.keyItems[i] = gSaveBlock1Ptr->bagPocket_KeyItems[i];
// load player pokeballs.
for (i = 0; i < 16; i++)
gLoadedSaveData.pokeBalls[i] = gSaveBlock1Ptr->bagPocket_PokeBalls[i];
// load player TMs and HMs.
for (i = 0; i < 64; i++)
gLoadedSaveData.TMsHMs[i] = gSaveBlock1Ptr->bagPocket_TMHM[i];
// load player berries.
for (i = 0; i < 46; i++)
gLoadedSaveData.berries[i] = gSaveBlock1Ptr->bagPocket_Berries[i];
// load mail.
for (i = 0; i < 16; i++)
gLoadedSaveData.mail[i] = gSaveBlock1Ptr->mail[i];
gLastEncryptionKey = gSaveBlock2Ptr->encryptionKey;
}
void copy_bags_and_unk_data_to_save_blocks(void)
{
int i;
u32 encryptionKeyBackup;
// save player items.
for (i = 0; i < 30; i++)
gSaveBlock1Ptr->bagPocket_Items[i] = gLoadedSaveData.items[i];
// save player key items.
for (i = 0; i < 30; i++)
gSaveBlock1Ptr->bagPocket_KeyItems[i] = gLoadedSaveData.keyItems[i];
// save player pokeballs.
for (i = 0; i < 16; i++)
gSaveBlock1Ptr->bagPocket_PokeBalls[i] = gLoadedSaveData.pokeBalls[i];
// save player TMs and HMs.
for (i = 0; i < 64; i++)
gSaveBlock1Ptr->bagPocket_TMHM[i] = gLoadedSaveData.TMsHMs[i];
// save player berries.
for (i = 0; i < 46; i++)
gSaveBlock1Ptr->bagPocket_Berries[i] = gLoadedSaveData.berries[i];
// save mail.
for (i = 0; i < 16; i++)
gSaveBlock1Ptr->mail[i] = gLoadedSaveData.mail[i];
encryptionKeyBackup = gSaveBlock2Ptr->encryptionKey;
gSaveBlock2Ptr->encryptionKey = gLastEncryptionKey;
ApplyNewEncyprtionKeyToBagItems(encryptionKeyBackup);
gSaveBlock2Ptr->encryptionKey = encryptionKeyBackup; // updated twice?
}
void ApplyNewEncyprtionKeyToHword(u16 *hWord, u32 newKey)
{
*hWord ^= gSaveBlock2Ptr->encryptionKey;
*hWord ^= newKey;
}
void ApplyNewEncyprtionKeyToWord(u32 *word, u32 newKey)
{
*word ^= gSaveBlock2Ptr->encryptionKey;
*word ^= newKey;
}
void ApplyNewEncyprtionKeyToAllEncryptedData(u32 encryptionKey)
{
ApplyNewEncyprtionKeyToGameStats(encryptionKey);
ApplyNewEncyprtionKeyToBagItems_(encryptionKey);
ApplyNewEncyprtionKeyToBerryPowder(encryptionKey);
ApplyNewEncyprtionKeyToWord(&gSaveBlock1Ptr->money, encryptionKey);
ApplyNewEncyprtionKeyToHword(&gSaveBlock1Ptr->coins, encryptionKey);
}

View File

@ -547,12 +547,6 @@ gUnknown_020249B4: @ 20249B4
.include "src/daycare.o"
.include "src/load_save.o"
gUnknown_02031C58: @ 2031C58
.space 0x528
gUnknown_02032180: @ 2032180
.space 0x4
gUnknown_02032184: @ 2032184
.space 0x4