From 54c8580d34b61dfd1db5dd91e301dfc788e564de Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Fri, 15 Sep 2017 22:12:45 -0400 Subject: [PATCH 1/3] rom_81700F8 --- asm/rom_81700F8.s | 64 --------------------------------------------- include/gpu_regs.h | 15 +++++++++++ include/load_save.h | 1 + include/malloc.h | 3 +++ include/new_game.h | 2 ++ include/rom4.h | 15 +++++++++++ include/save.h | 4 +++ ld_script.txt | 2 +- src/rom_81700F8.c | 32 +++++++++++++++++++++++ 9 files changed, 73 insertions(+), 65 deletions(-) delete mode 100644 asm/rom_81700F8.s create mode 100644 include/gpu_regs.h create mode 100644 include/rom4.h create mode 100644 src/rom_81700F8.c diff --git a/asm/rom_81700F8.s b/asm/rom_81700F8.s deleted file mode 100644 index 45c505cd9..000000000 --- a/asm/rom_81700F8.s +++ /dev/null @@ -1,64 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start sub_81700F8 -sub_81700F8: @ 81700F8 - push {r4,r5,lr} - ldr r5, =0x04000208 - ldrh r4, [r5] - movs r0, 0 - strh r0, [r5] - movs r0, 0x1 - bl RegisterRamReset - movs r0, 0 - movs r1, 0x80 - bl ClearGpuRegBits - strh r4, [r5] - ldr r1, =gMain - ldr r0, =0x00000439 - adds r1, r0 - ldrb r2, [r1] - movs r0, 0x3 - negs r0, r0 - ands r0, r2 - strb r0, [r1] - bl sub_815355C - lsls r0, 16 - lsrs r0, 16 - bl SetSaveBlocksPointers - bl sub_808447C - bl ResetSaveCounters - movs r0, 0 - bl sub_81534D0 - ldr r0, =gSaveFileStatus - ldrh r0, [r0] - cmp r0, 0 - beq _08170148 - cmp r0, 0x2 - bne _0817014C -_08170148: - bl Sav2_ClearSetDefault -_0817014C: - ldr r0, =gSaveBlock2Ptr - ldr r0, [r0] - ldrb r0, [r0, 0x15] - lsls r0, 31 - lsrs r0, 31 - bl SetPokemonCryStereo - ldr r0, =0x02000000 - movs r1, 0xE0 - lsls r1, 9 - bl InitHeap - ldr r0, =sub_8086230 - bl SetMainCallback2 - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_81700F8 - - .align 2, 0 @ Don't pad with nop. diff --git a/include/gpu_regs.h b/include/gpu_regs.h new file mode 100644 index 000000000..2d47d602c --- /dev/null +++ b/include/gpu_regs.h @@ -0,0 +1,15 @@ +// +// Created by scott on 9/15/2017. +// + +#ifndef POKEEMERALD_GPU_REGS_H +#define POKEEMERALD_GPU_REGS_H + +// Exported type declarations + +// Exported RAM declarations + +// Exported ROM declarations +void ClearGpuRegBits(u8 regOffset, u16 mask); + +#endif //POKEEMERALD_GPU_REGS_H diff --git a/include/load_save.h b/include/load_save.h index 0fedd720b..20848e1a7 100644 --- a/include/load_save.h +++ b/include/load_save.h @@ -18,5 +18,6 @@ void SaveSerializedGame(void); void LoadSerializedGame(void); void LoadPlayerBag(void); void SavePlayerBag(void); +void SetSaveBlocksPointers(u16); #endif // GUARD_LOAD_SAVE_H diff --git a/include/malloc.h b/include/malloc.h index bd870ede3..d26d19242 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -1,8 +1,11 @@ #ifndef GUARD_MALLOC_H #define GUARD_MALLOC_H +extern u8 gHeap[]; + void *Alloc(u32 size); void *AllocZeroed(u32 size); void Free(void *pointer); +void InitHeap(void *pointer, u32 size); #endif // GUARD_MALLOC_H diff --git a/include/new_game.h b/include/new_game.h index f9ad7fcb8..060279c25 100644 --- a/include/new_game.h +++ b/include/new_game.h @@ -9,5 +9,7 @@ void ClearPokedexFlags(void); void WarpToTruck(void); void NewGameInitData(void); void ResetMiniGamesResults(void); +void sub_808447C(void); +void Sav2_ClearSetDefault(void); #endif // GUARD_NEW_GAME_H diff --git a/include/rom4.h b/include/rom4.h new file mode 100644 index 000000000..1538f5dbc --- /dev/null +++ b/include/rom4.h @@ -0,0 +1,15 @@ +// +// Created by scott on 9/15/2017. +// + +#ifndef POKEEMERALD_ROM4_H +#define POKEEMERALD_ROM4_H + +// Exported type declarations + +// Exported RAM declarations + +// Exported ROM declarations +void sub_8086230(void); + +#endif //POKEEMERALD_ROM4_H diff --git a/include/save.h b/include/save.h index 1d7fdd6e3..e98233c67 100644 --- a/include/save.h +++ b/include/save.h @@ -1,6 +1,8 @@ #ifndef GUARD_SAVE_H #define GUARD_SAVE_H +extern u16 gSaveFileStatus; + struct SaveSectionLocation { void *data; @@ -88,5 +90,7 @@ u16 CalculateChecksum(void *, u16); //u8 unref_sub_8125FF0(u8 *data, u16 size); //u8 unref_sub_8126068(u8 sector, u8 *data, u32 size); //u8 unref_sub_8126080(u8 sector, u8 *data); +u16 sub_815355C(void); +u8 sub_81534D0(u8); #endif // GUARD_SAVE_H diff --git a/ld_script.txt b/ld_script.txt index 4a5bb643a..e8c8a43ce 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -206,7 +206,7 @@ SECTIONS { asm/battle_controller_wally.o(.text); asm/player_pc.o(.text); asm/intro.o(.text); - asm/rom_81700F8.o(.text); + src/rom_81700F8.o(.text); asm/field_region_map.o(.text); asm/battle_anim_8170478.o(.text); asm/hall_of_fame.o(.text); diff --git a/src/rom_81700F8.c b/src/rom_81700F8.c new file mode 100644 index 000000000..b786ff9f8 --- /dev/null +++ b/src/rom_81700F8.c @@ -0,0 +1,32 @@ +#include "global.h" +#include "main.h" +#include "gpu_regs.h" +#include "m4a.h" +#include "load_save.h" +#include "save.h" +#include "new_game.h" +#include "rom4.h" +#include "malloc.h" + +void sub_81700F8(void) +{ + u16 imeBackup; + + imeBackup = REG_IME; + REG_IME = 0; + RegisterRamReset(0x00000001); + ClearGpuRegBits(REG_OFFSET_DISPCNT, 0x80); + REG_IME = imeBackup; + gMain.inBattle = FALSE; + SetSaveBlocksPointers(sub_815355C()); + sub_808447C(); + ResetSaveCounters(); + sub_81534D0(0); + if (gSaveFileStatus == 0 || gSaveFileStatus == 2) + { + Sav2_ClearSetDefault(); + } + SetPokemonCryStereo(gSaveBlock2Ptr->optionsSound); + InitHeap(gHeap, 0x1c000); + SetMainCallback2(sub_8086230); +} From 052c9f0234a6034b9db8fce30f779b95b8869df5 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sat, 16 Sep 2017 15:13:26 -0400 Subject: [PATCH 2/3] Fix header guards --- include/gpu_regs.h | 6 +++--- include/rom4.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/gpu_regs.h b/include/gpu_regs.h index 2d47d602c..28b97fb6e 100644 --- a/include/gpu_regs.h +++ b/include/gpu_regs.h @@ -2,8 +2,8 @@ // Created by scott on 9/15/2017. // -#ifndef POKEEMERALD_GPU_REGS_H -#define POKEEMERALD_GPU_REGS_H +#ifndef GUARD_GPU_REGS_H +#define GUARD_GPU_REGS_H // Exported type declarations @@ -12,4 +12,4 @@ // Exported ROM declarations void ClearGpuRegBits(u8 regOffset, u16 mask); -#endif //POKEEMERALD_GPU_REGS_H +#endif //GUARD_GPU_REGS_H diff --git a/include/rom4.h b/include/rom4.h index 1538f5dbc..3164c81ea 100644 --- a/include/rom4.h +++ b/include/rom4.h @@ -2,8 +2,8 @@ // Created by scott on 9/15/2017. // -#ifndef POKEEMERALD_ROM4_H -#define POKEEMERALD_ROM4_H +#ifndef GUARD_ROM4_H +#define GUARD_ROM4_H // Exported type declarations @@ -12,4 +12,4 @@ // Exported ROM declarations void sub_8086230(void); -#endif //POKEEMERALD_ROM4_H +#endif //GUARD_ROM4_H From 16f2e40576b29fd85366305b4165da3aff4aef0e Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 17 Sep 2017 23:42:44 -0400 Subject: [PATCH 3/3] Fix naming conflicts --- include/global.berry.h | 4 ---- ld_script.txt | 2 +- src/berry.c | 6 +++--- src/load_save.c | 4 ---- src/{rom_81700F8.c => reset_save_heap.c} | 0 5 files changed, 4 insertions(+), 12 deletions(-) rename src/{rom_81700F8.c => reset_save_heap.c} (100%) diff --git a/include/global.berry.h b/include/global.berry.h index 4b9ca644c..a77da20ba 100644 --- a/include/global.berry.h +++ b/include/global.berry.h @@ -46,10 +46,6 @@ struct EnigmaBerry u8 holdEffect; u8 holdEffectParam; u32 checksum; - u8 pic[(6 * 6) * TILE_SIZE_4BPP]; - u16 palette[16]; - u8 description1[45]; - u8 description2[45]; }; struct BattleEnigmaBerry diff --git a/ld_script.txt b/ld_script.txt index 78d0e6cca..19ddabab1 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -207,7 +207,7 @@ SECTIONS { asm/battle_controller_wally.o(.text); asm/player_pc.o(.text); asm/intro.o(.text); - src/rom_81700F8.o(.text); + src/reset_save_heap.o(.text); asm/field_region_map.o(.text); asm/battle_anim_8170478.o(.text); asm/hall_of_fame.o(.text); diff --git a/src/berry.c b/src/berry.c index b6eaa14a0..46b93d6ff 100644 --- a/src/berry.c +++ b/src/berry.c @@ -807,7 +807,7 @@ const struct Berry gBerries[] = // unused void ClearEnigmaBerries(void) { - CpuFill16(0, &gSaveBlock1Ptr->enigmaBerry, sizeof(gSaveBlock1Ptr->enigmaBerry)); + CpuFill16(0, &gSaveBlock1Ptr->enigmaBerry, 52 /*FIXME: sizeof(gSaveBlock1Ptr->enigmaBerry)*/); } void SetEnigmaBerry(u8 *src) @@ -815,7 +815,7 @@ void SetEnigmaBerry(u8 *src) u32 i; u8 *dest = (u8*)&gSaveBlock1Ptr->enigmaBerry; - for (i = 0; i < sizeof(gSaveBlock1Ptr->enigmaBerry); i++) + for (i = 0; i < 52 /*FIXME: sizeof(gSaveBlock1Ptr->enigmaBerry)*/; i++) dest[i] = src[i]; } @@ -827,7 +827,7 @@ u32 GetEnigmaBerryChecksum(struct EnigmaBerry *enigmaBerry) dest = (u8*)enigmaBerry; checksum = 0; - for (i = 0; i < sizeof(gSaveBlock1Ptr->enigmaBerry) - sizeof(gSaveBlock1Ptr->enigmaBerry.checksum); i++) + for (i = 0; i < 52 /*FIXME: sizeof(gSaveBlock1Ptr->enigmaBerry)*/ - sizeof(gSaveBlock1Ptr->enigmaBerry.checksum); i++) { checksum += dest[i]; } diff --git a/src/load_save.c b/src/load_save.c index 588387186..68d978e3d 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -6,15 +6,11 @@ #include "rng.h" #include "malloc.h" -extern u8 gPlayerPartyCount; -extern struct PokemonStorage* gPokemonStoragePtr; extern void* gUnknown_0203CF5C; -extern u8 gHeap[0x1C000]; extern bool16 IdentifyFlash(void); extern void SetBagItemsPointers(void); extern void SetDecorationInventoriesPointers(void); -extern void InitHeap(void *heapStart, u32 heapSize); void ApplyNewEncyprtionKeyToAllEncryptedData(u32 encryptionKey); diff --git a/src/rom_81700F8.c b/src/reset_save_heap.c similarity index 100% rename from src/rom_81700F8.c rename to src/reset_save_heap.c