diff --git a/asm/link.s b/asm/link.s index 1ce6f7710..cb4b6ec2e 100644 --- a/asm/link.s +++ b/asm/link.s @@ -52672,8 +52672,8 @@ SetBerryPowder: @ 802467C .pool thumb_func_end SetBerryPowder - thumb_func_start ApplyNewEncyprtionKeyToBerryPowder -ApplyNewEncyprtionKeyToBerryPowder: @ 8024690 + thumb_func_start ApplyNewEncryptionKeyToBerryPowder +ApplyNewEncryptionKeyToBerryPowder: @ 8024690 push {lr} adds r1, r0, 0 ldr r0, =gSaveBlock2Ptr @@ -52681,11 +52681,11 @@ ApplyNewEncyprtionKeyToBerryPowder: @ 8024690 movs r2, 0xFA lsls r2, 1 adds r0, r2 - bl ApplyNewEncyprtionKeyToWord + bl ApplyNewEncryptionKeyToWord pop {r0} bx r0 .pool - thumb_func_end ApplyNewEncyprtionKeyToBerryPowder + thumb_func_end ApplyNewEncryptionKeyToBerryPowder thumb_func_start sub_80246AC sub_80246AC: @ 80246AC diff --git a/asm/load_save.s b/asm/load_save.s deleted file mode 100644 index 2b4be8e67..000000000 --- a/asm/load_save.s +++ /dev/null @@ -1,669 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - - thumb_func_start MoveSaveBlocks_ResetHeap -MoveSaveBlocks_ResetHeap: @ 8076C2C - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x8 - ldr r5, =gMain - ldr r0, [r5, 0xC] - str r0, [sp] - ldr r1, [r5, 0x10] - str r1, [sp, 0x4] - movs r0, 0 - str r0, [r5, 0xC] - str r0, [r5, 0x10] - ldr r1, =gUnknown_0203CF5C - str r0, [r1] - ldr r4, =0x02000000 - ldr r0, =gSaveBlock2Ptr - ldr r1, [r0] - ldr r6, =0x00000f2c - adds r0, r4, 0 - adds r2, r6, 0 - bl memcpy - ldr r1, =gSaveBlock1Ptr - mov r10, r1 - ldr r1, [r1] - ldr r7, =0x00003d88 - ldr r0, =0x02000f2c - adds r2, r7, 0 - bl memcpy - ldr r0, =gPokemonStoragePtr - mov r8, r0 - ldr r1, [r0] - ldr r0, =0x000083d0 - mov r9, r0 - ldr r0, =0x02004cb4 - mov r2, r9 - bl memcpy - ldrb r1, [r4, 0xA] - ldrb r0, [r4, 0xB] - adds r1, r0 - ldrb r0, [r4, 0xC] - adds r1, r0 - ldrb r0, [r4, 0xD] - adds r0, r1 - bl SetSaveBlocksPointers - ldr r1, =gSaveBlock2Ptr - ldr r0, [r1] - adds r1, r4, 0 - adds r2, r6, 0 - bl memcpy - mov r1, r10 - ldr r0, [r1] - ldr r1, =0x02000f2c - adds r2, r7, 0 - bl memcpy - mov r1, r8 - ldr r0, [r1] - ldr r1, =0x02004cb4 - mov r2, r9 - bl memcpy - movs r1, 0xE0 - lsls r1, 9 - adds r0, r4, 0 - bl InitHeap - ldr r0, [sp, 0x4] - str r0, [r5, 0x10] - ldr r1, [sp] - str r1, [r5, 0xC] - bl Random - adds r4, r0, 0 - bl Random - lsls r4, 16 - lsls r0, 16 - lsrs r0, 16 - adds r4, r0 - adds r0, r4, 0 - bl ApplyNewEncyprtionKeyToAllEncryptedData - ldr r1, =gSaveBlock2Ptr - ldr r0, [r1] - adds r0, 0xAC - str r4, [r0] - add sp, 0x8 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r0} - bx r0 - .pool - thumb_func_end MoveSaveBlocks_ResetHeap - - 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. diff --git a/asm/rom4.s b/asm/rom4.s index 8b7dec1ae..ea03b8a8d 100644 --- a/asm/rom4.s +++ b/asm/rom4.s @@ -238,8 +238,8 @@ _08084854: .pool thumb_func_end sav12_xor_set - thumb_func_start ApplyNewEncyprtionKeyToGameStats -ApplyNewEncyprtionKeyToGameStats: @ 8084864 + thumb_func_start ApplyNewEncryptionKeyToGameStats +ApplyNewEncryptionKeyToGameStats: @ 8084864 push {r4-r6,lr} adds r5, r0, 0 movs r4, 0 @@ -251,7 +251,7 @@ _0808486C: ldr r0, [r6] adds r0, r1 adds r1, r5, 0 - bl ApplyNewEncyprtionKeyToWord + bl ApplyNewEncryptionKeyToWord adds r0, r4, 0x1 lsls r0, 24 lsrs r4, r0, 24 @@ -261,7 +261,7 @@ _0808486C: pop {r0} bx r0 .pool - thumb_func_end ApplyNewEncyprtionKeyToGameStats + thumb_func_end ApplyNewEncryptionKeyToGameStats thumb_func_start CopyFieldObjectTemplatesToSav1 @ void CopyFieldObjectTemplatesToSav1() diff --git a/ld_script.txt b/ld_script.txt index 305d59b71..ebc8b9857 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -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); src/play_time.o(.text); diff --git a/src/item.c b/src/item.c index 32347623e..5cf9edf6c 100644 --- a/src/item.c +++ b/src/item.c @@ -5,7 +5,7 @@ #include "text.h" #include "event_data.h" -extern void ApplyNewEncyprtionKeyToHword(u16* hword, u32 newKey); +extern void ApplyNewEncryptionKeyToHword(u16* hword, u32 newKey); extern bool8 InBattlePyramid(void); extern const u8 gText_PokeBalls[]; @@ -45,19 +45,19 @@ void SetBagItemId(u16* slot, u16 newItemId) *slot = newItemId; } -void ApplyNewEncyprtionKeyToBagItems(u32 newKey) +void ApplyNewEncryptionKeyToBagItems(u32 newKey) { u32 pocket, item; for (pocket = 0; pocket < 5; pocket++) { for (item = 0; item < gBagPockets[pocket].capacity; item++) - ApplyNewEncyprtionKeyToHword(&(gBagPockets[pocket].itemSlots[item].quantity), newKey); + ApplyNewEncryptionKeyToHword(&(gBagPockets[pocket].itemSlots[item].quantity), newKey); } } -void ApplyNewEncyprtionKeyToBagItems_(u32 newKey) // really GF? +void ApplyNewEncryptionKeyToBagItems_(u32 newKey) // really GF? { - ApplyNewEncyprtionKeyToBagItems(newKey); + ApplyNewEncryptionKeyToBagItems(newKey); } // TODO: move those max values to defines diff --git a/src/load_save.c b/src/load_save.c index 68d978e3d..1d78bda50 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -1,21 +1,36 @@ -#include "global.h" +#include "global.h" #include "gba/flash_internal.h" #include "load_save.h" #include "main.h" #include "pokemon.h" #include "rng.h" -#include "malloc.h" extern void* gUnknown_0203CF5C; extern bool16 IdentifyFlash(void); extern void SetBagItemsPointers(void); extern void SetDecorationInventoriesPointers(void); +extern void ApplyNewEncryptionKeyToGameStats(u32 key); +extern void ApplyNewEncryptionKeyToBagItems(u32 newKey); +extern void ApplyNewEncryptionKeyToBagItems_(u32 key); +extern void ApplyNewEncryptionKeyToBerryPowder(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}; @@ -25,6 +40,11 @@ EWRAM_DATA u8 gSaveblock1_DMA[SAVEBLOCK_MOVE_RANGE] = {0}; EWRAM_DATA struct PokemonStorage gPokemonStorage = {0}; EWRAM_DATA u8 gSaveblock3_DMA[SAVEBLOCK_MOVE_RANGE] = {0}; +EWRAM_DATA struct LoadedSaveData gLoadedSaveData = {0}; +EWRAM_DATA u32 gLastEncryptionKey = {0}; + +void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey); + void CheckForFlashMemory(void) { if (!IdentifyFlash()) @@ -60,18 +80,45 @@ void SetSaveBlocksPointers(u16 offset) SetDecorationInventoriesPointers(); } +// stuff i used to try and match MoveSaveBlocks_ResetHeap struct SaveBlocksInOne { struct SaveBlock2 sav2; struct SaveBlock1 sav1; struct PokemonStorage sav3; }; + +extern struct SaveBlocksInOne gHeap; + +#define ewram_addr 0x02000000 // oh no... +#define ewram_addr2 0x02000f2c +#define ewram_addr3 0x02004cb4 + +#define eSaveBlockCopy (*(struct SaveBlocksInOne *)(ewram_addr + 0x0)) +#define eSaveBlock2Copy (*(struct SaveBlock2 *)((void*)(ewram_addr + 0x0))) +#define eSaveBlock1Copy (*(struct SaveBlock1 *)((void*)(ewram_addr + sizeof(struct SaveBlock2)))) +#define eSaveBlock3Copy (*(struct PokemonStorage *)((void*)(ewram_addr + sizeof(struct SaveBlock2) + sizeof(struct SaveBlock1)))) + +#ifdef NONMATCHING // this is one devil of a motherfucker /* + The reason MoveSaveBlocks_ResetHeap mismatches is due to incorrect memcpys. Various + things have been tried, such as: direct struct copys, ewram casts, use of defining + the addresses manually, using memcpy anyway, delayed allocation of pointers at + the start of function, as seen above and below. Scope declaration has been used to try and + reproduce the correct macro thought to be used, but nothing has worked. It is worth + noting that at this point that the compiler will delay the allocation of the save block + pointers at the beginningto be allocated later: which might matter for matching this. + Due to loading shared ewram heap areas directly, it is very likely emerald used ewram + defines for this function, but there is no known example of a matching define. + In addition, dead code might be present in the form of a runtime variable used + to fix the address of the save blocks. This has been tested and is shown to affect + the registers as well. +*/ void MoveSaveBlocks_ResetHeap(void) { void *vblankCB, *hblankCB; u32 encryptionKey; - struct SaveBlocksInOne* copiedSavs; + struct SaveBlock1 **sav1Copy = &gSaveBlock1Ptr; // r10; // save interrupt functions and turn them off vblankCB = gMain.vblankCallback; @@ -80,24 +127,22 @@ void MoveSaveBlocks_ResetHeap(void) gMain.hblankCallback = NULL; gUnknown_0203CF5C = NULL; - copiedSavs = (void*)(gHeap); - - // copy saveblocks' content - copiedSavs->sav2 = *gSaveBlock2Ptr; - copiedSavs->sav1 = *gSaveBlock1Ptr; - copiedSavs->sav3 = *gPokemonStoragePtr; + // backup the saves. + eSaveBlock1Copy = **sav1Copy; + eSaveBlock2Copy = *gSaveBlock2Ptr; + eSaveBlock3Copy = *gPokemonStoragePtr; // change saveblocks' pointers // argument is a sum of the individual trainerId bytes - SetSaveBlocksPointers(copiedSavs->sav2.playerTrainerId[0] + copiedSavs->sav2.playerTrainerId[1] + copiedSavs->sav2.playerTrainerId[2] + copiedSavs->sav2.playerTrainerId[3]); + SetSaveBlocksPointers(eSaveBlock2Copy.playerTrainerId[0] + eSaveBlock2Copy.playerTrainerId[1] + eSaveBlock2Copy.playerTrainerId[2] + eSaveBlock2Copy.playerTrainerId[3]); // restore saveblock data since the pointers changed - *gSaveBlock2Ptr = copiedSavs->sav2; - *gSaveBlock1Ptr = copiedSavs->sav1; - *gPokemonStoragePtr = copiedSavs->sav3; + **sav1Copy = eSaveBlock1Copy; + *gSaveBlock2Ptr = eSaveBlock2Copy; + *gPokemonStoragePtr = eSaveBlock3Copy; // heap was destroyed in the copying process, so reset it - InitHeap(gHeap, sizeof(gHeap)); + InitHeap((void*)(0x02000000), 0x1C000); // restore interrupt functions gMain.hblankCallback = hblankCB; @@ -105,6 +150,266 @@ void MoveSaveBlocks_ResetHeap(void) // create a new encryption key encryptionKey = (Random() << 0x10) + (Random()); - ApplyNewEncyprtionKeyToAllEncryptedData(encryptionKey); + ApplyNewEncryptionKeyToAllEncryptedData(encryptionKey); gSaveBlock2Ptr->encryptionKey = encryptionKey; -}*/ +} +#else +__attribute__((naked)) +void MoveSaveBlocks_ResetHeap(void) +{ + asm(".syntax unified\n\ + push {r4-r7,lr}\n\ + mov r7, r10\n\ + mov r6, r9\n\ + mov r5, r8\n\ + push {r5-r7}\n\ + sub sp, 0x8\n\ + ldr r5, =gMain\n\ + ldr r0, [r5, 0xC]\n\ + str r0, [sp]\n\ + ldr r1, [r5, 0x10]\n\ + str r1, [sp, 0x4]\n\ + movs r0, 0\n\ + str r0, [r5, 0xC]\n\ + str r0, [r5, 0x10]\n\ + ldr r1, =gUnknown_0203CF5C\n\ + str r0, [r1]\n\ + ldr r4, =0x02000000\n\ + ldr r0, =gSaveBlock2Ptr\n\ + ldr r1, [r0]\n\ + ldr r6, =0x00000f2c\n\ + adds r0, r4, 0\n\ + adds r2, r6, 0\n\ + bl memcpy\n\ + ldr r1, =gSaveBlock1Ptr\n\ + mov r10, r1\n\ + ldr r1, [r1]\n\ + ldr r7, =0x00003d88\n\ + ldr r0, =0x02000f2c\n\ + adds r2, r7, 0\n\ + bl memcpy\n\ + ldr r0, =gPokemonStoragePtr\n\ + mov r8, r0\n\ + ldr r1, [r0]\n\ + ldr r0, =0x000083d0\n\ + mov r9, r0\n\ + ldr r0, =0x02004cb4\n\ + mov r2, r9\n\ + bl memcpy\n\ + ldrb r1, [r4, 0xA]\n\ + ldrb r0, [r4, 0xB]\n\ + adds r1, r0\n\ + ldrb r0, [r4, 0xC]\n\ + adds r1, r0\n\ + ldrb r0, [r4, 0xD]\n\ + adds r0, r1\n\ + bl SetSaveBlocksPointers\n\ + ldr r1, =gSaveBlock2Ptr\n\ + ldr r0, [r1]\n\ + adds r1, r4, 0\n\ + adds r2, r6, 0\n\ + bl memcpy\n\ + mov r1, r10\n\ + ldr r0, [r1]\n\ + ldr r1, =0x02000f2c\n\ + adds r2, r7, 0\n\ + bl memcpy\n\ + mov r1, r8\n\ + ldr r0, [r1]\n\ + ldr r1, =0x02004cb4\n\ + mov r2, r9\n\ + bl memcpy\n\ + movs r1, 0xE0\n\ + lsls r1, 9\n\ + adds r0, r4, 0\n\ + bl InitHeap\n\ + ldr r0, [sp, 0x4]\n\ + str r0, [r5, 0x10]\n\ + ldr r1, [sp]\n\ + str r1, [r5, 0xC]\n\ + bl Random\n\ + adds r4, r0, 0\n\ + bl Random\n\ + lsls r4, 16\n\ + lsls r0, 16\n\ + lsrs r0, 16\n\ + adds r4, r0\n\ + adds r0, r4, 0\n\ + bl ApplyNewEncryptionKeyToAllEncryptedData\n\ + ldr r1, =gSaveBlock2Ptr\n\ + ldr r0, [r1]\n\ + adds r0, 0xAC\n\ + str r4, [r0]\n\ + add sp, 0x8\n\ + pop {r3-r5}\n\ + mov r8, r3\n\ + mov r9, r4\n\ + mov r10, r5\n\ + pop {r4-r7}\n\ + pop {r0}\n\ + bx r0\n\ + .pool\n\ + .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; + ApplyNewEncryptionKeyToBagItems(encryptionKeyBackup); + gSaveBlock2Ptr->encryptionKey = encryptionKeyBackup; // updated twice? +} + +void ApplyNewEncryptionKeyToHword(u16 *hWord, u32 newKey) +{ + *hWord ^= gSaveBlock2Ptr->encryptionKey; + *hWord ^= newKey; +} + +void ApplyNewEncryptionKeyToWord(u32 *word, u32 newKey) +{ + *word ^= gSaveBlock2Ptr->encryptionKey; + *word ^= newKey; +} + +void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey) +{ + ApplyNewEncryptionKeyToGameStats(encryptionKey); + ApplyNewEncryptionKeyToBagItems_(encryptionKey); + ApplyNewEncryptionKeyToBerryPowder(encryptionKey); + ApplyNewEncryptionKeyToWord(&gSaveBlock1Ptr->money, encryptionKey); + ApplyNewEncryptionKeyToHword(&gSaveBlock1Ptr->coins, encryptionKey); +} diff --git a/sym_ewram.txt b/sym_ewram.txt index 989548ed6..fda69af68 100644 --- a/sym_ewram.txt +++ b/sym_ewram.txt @@ -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