From a3f602f17c4a5a1c2c7d524b5abac51f35758e8d Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Sun, 3 Sep 2017 14:13:01 +0200 Subject: [PATCH] begin save.s decomp, thanks revo! --- asm/battle_4.s | 2 +- asm/battle_records.s | 10 +- asm/battle_setup.s | 48 +- asm/berry_blender.s | 4 +- asm/clear_save_data_screen.s | 2 +- asm/contest_link_80F57C4.s | 8 +- asm/evolution_scene.s | 4 +- asm/field_control_avatar.s | 4 +- asm/field_player_avatar.s | 2 +- asm/hall_of_fame.s | 8 +- asm/intro.s | 2 +- asm/item_use.s | 2 +- asm/link.s | 14 +- asm/load_save.s | 12 +- asm/mauville_old_man.s | 2 +- asm/mystery_event_menu.s | 2 +- asm/reset_rtc_screen.s | 2 +- asm/rom4.s | 16 +- asm/rom6.s | 10 +- asm/rom_80C6FA0.s | 4 +- asm/rom_8158B30.s | 2 +- asm/rom_81700F8.s | 2 +- asm/rom_8184DA4.s | 8 +- asm/rom_81BE66C.s | 2 +- asm/roulette.s | 2 +- asm/safari_zone.s | 2 +- asm/save.s | 1748 +--------------------------------- asm/save_failed_screen.s | 14 +- asm/scrcmd.s | 2 +- asm/secret_base.s | 4 +- asm/shop.s | 2 +- asm/slot_machine.s | 2 +- asm/start_menu.s | 8 +- asm/trade.s | 4 +- asm/trainer_card.s | 8 +- asm/tv.s | 6 +- asm/weather.s | 2 +- asm/wild_encounter.s | 2 +- data/save.s | 2 +- include/save.h | 92 ++ ld_script.txt | 1 + src/save.c | 830 ++++++++++++++++ sym_common.txt | 16 +- 43 files changed, 1054 insertions(+), 1865 deletions(-) create mode 100644 include/save.h create mode 100644 src/save.c diff --git a/asm/battle_4.s b/asm/battle_4.s index 32156cb5f..b5d15859a 100644 --- a/asm/battle_4.s +++ b/asm/battle_4.s @@ -15702,7 +15702,7 @@ atk60_cmd60: @ 804E868 ldr r0, =gBattlescriptCurrInstr ldr r0, [r0] ldrb r0, [r0, 0x1] - bl sav12_xor_increment + bl IncrementGameStat _0804E882: ldr r1, =gBattlescriptCurrInstr ldr r0, [r1] diff --git a/asm/battle_records.s b/asm/battle_records.s index 516c4259e..ae210d9d8 100644 --- a/asm/battle_records.s +++ b/asm/battle_records.s @@ -259,12 +259,12 @@ _0813C152: movs r4, 0x19 _0813C154: adds r0, r4, 0 - bl sub_80847F8 + bl GetGameStat ldr r1, =0x0000270e cmp r0, r1 bhi _0813C166 adds r0, r4, 0 - bl sav12_xor_increment + bl IncrementGameStat _0813C166: pop {r4} pop {r0} @@ -465,7 +465,7 @@ sub_813C2F4: @ 813C2F4 sub sp, 0xC ldr r4, =gStringVar1 movs r0, 0x17 - bl sub_80847F8 + bl GetGameStat adds r1, r0, 0 adds r0, r4, 0 movs r2, 0 @@ -473,7 +473,7 @@ sub_813C2F4: @ 813C2F4 bl ConvertIntToDecimalStringN ldr r4, =gStringVar2 movs r0, 0x18 - bl sub_80847F8 + bl GetGameStat adds r1, r0, 0 adds r0, r4, 0 movs r2, 0 @@ -481,7 +481,7 @@ sub_813C2F4: @ 813C2F4 bl ConvertIntToDecimalStringN ldr r4, =gStringVar3 movs r0, 0x19 - bl sub_80847F8 + bl GetGameStat adds r1, r0, 0 adds r0, r4, 0 movs r2, 0 diff --git a/asm/battle_setup.s b/asm/battle_setup.s index 287bd719d..1093fa1cb 100644 --- a/asm/battle_setup.s +++ b/asm/battle_setup.s @@ -136,9 +136,9 @@ _080B06F2: movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r4} @@ -166,9 +166,9 @@ sub_80B072C: @ 80B072C movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -217,9 +217,9 @@ sub_80B07B4: @ 80B07B4 movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -236,9 +236,9 @@ task_add_01_battle_start_with_music_and_stats: @ 80B0804 movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x9 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80B1234 pop {r0} bx r0 @@ -267,9 +267,9 @@ _080B0846: bl task_add_01_battle_start _080B0856: movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x9 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80B1234 pop {r0} bx r0 @@ -315,9 +315,9 @@ sub_80B08A8: @ 80B08A8 movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -342,9 +342,9 @@ sub_80B08EC: @ 80B08EC movs r1, 0 bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -444,9 +444,9 @@ _080B09F4: bl task_add_01_battle_start _080B09FE: movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r4} @@ -482,9 +482,9 @@ _080B0A50: bl task_add_01_battle_start _080B0A5A: movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -540,9 +540,9 @@ _080B0AD6: ldr r1, =0x000001df bl task_add_01_battle_start movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 pop {r0} @@ -1392,9 +1392,9 @@ sub_80B11A8: @ 80B11A8 bl prev_quest_postbuffer_cursor_backup_reset bl overworld_poison_timer_set movs r0, 0x7 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x8 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80EECC8 bl sub_80B1218 _080B11F0: @@ -1418,7 +1418,7 @@ sub_80B1204: @ 80B1204 sub_80B1218: @ 80B1218 push {lr} movs r0, 0x8 - bl sub_80847F8 + bl GetGameStat movs r1, 0x3C bl __umodsi3 cmp r0, 0 @@ -1433,7 +1433,7 @@ _080B122E: sub_80B1234: @ 80B1234 push {lr} movs r0, 0x9 - bl sub_80847F8 + bl GetGameStat movs r1, 0x14 bl __umodsi3 cmp r0, 0 diff --git a/asm/berry_blender.s b/asm/berry_blender.s index 8b79ead6d..c63b72647 100644 --- a/asm/berry_blender.s +++ b/asm/berry_blender.s @@ -5050,12 +5050,12 @@ _08082292: cmp r0, 0 bne _080822A8 movs r0, 0x22 - bl sav12_xor_increment + bl IncrementGameStat b _08082458 .pool _080822A8: movs r0, 0x21 - bl sav12_xor_increment + bl IncrementGameStat b _08082458 _080822B0: ldr r6, =gUnknown_020322A4 diff --git a/asm/clear_save_data_screen.s b/asm/clear_save_data_screen.s index ab0d54a03..9eff4a775 100644 --- a/asm/clear_save_data_screen.s +++ b/asm/clear_save_data_screen.s @@ -126,7 +126,7 @@ sub_817ADC0: @ 817ADC0 adds r4, r0, 0 lsls r4, 24 lsrs r4, 24 - bl calls_flash_erase_block + bl ClearSaveData adds r0, r4, 0 bl DestroyTask ldr r0, =sub_817AFD4 diff --git a/asm/contest_link_80F57C4.s b/asm/contest_link_80F57C4.s index 28288dea5..851ec8709 100644 --- a/asm/contest_link_80F57C4.s +++ b/asm/contest_link_80F57C4.s @@ -543,7 +543,7 @@ _080F5D2A: cmp r0, 0 bne _080F5D58 movs r0, 0x23 - bl sav12_xor_increment + bl IncrementGameStat ldr r1, =gSpecialVar_0x8005 movs r0, 0x8 strh r0, [r1] @@ -580,7 +580,7 @@ _080F5D58: bl VarSet bl sub_8076D5C movs r0, 0x1 - bl save_game_when_memory_present + bl TrySavingData bl sav2_gender2_inplace_and_xFE adds r0, r5, 0 adds r1, r4, 0 @@ -662,7 +662,7 @@ _080F5E2E: .pool _080F5E78: movs r0, 0x24 - bl sav12_xor_increment + bl IncrementGameStat ldr r1, =gUnknown_02039F20 ldr r0, =gUnknown_02039F25 ldrb r0, [r0] @@ -671,7 +671,7 @@ _080F5E78: cmp r0, 0 bne _080F5E92 movs r0, 0x25 - bl sav12_xor_increment + bl IncrementGameStat _080F5E92: ldr r0, =gUnknown_02039F2E ldrb r0, [r0] diff --git a/asm/evolution_scene.s b/asm/evolution_scene.s index fb2ba7262..f1214c557 100644 --- a/asm/evolution_scene.s +++ b/asm/evolution_scene.s @@ -1661,7 +1661,7 @@ _0813E99E: movs r1, 0x3 bl pokedex_flag_operation movs r0, 0xE - bl sav12_xor_increment + bl IncrementGameStat b _0813F1A4 .pool _0813EA1C: @@ -2845,7 +2845,7 @@ _0813F4F2: movs r1, 0x3 bl pokedex_flag_operation movs r0, 0xE - bl sav12_xor_increment + bl IncrementGameStat b _0813FCC4 .pool _0813F570: diff --git a/asm/field_control_avatar.s b/asm/field_control_avatar.s index 56b6ef940..fd55e94ad 100644 --- a/asm/field_control_avatar.s +++ b/asm/field_control_avatar.s @@ -228,7 +228,7 @@ _0809C070: cmp r0, 0 beq _0809C096 movs r0, 0x5 - bl sav12_xor_increment + bl IncrementGameStat bl increment_var_x4026_on_birth_island_modulo_100 mov r0, sp adds r1, r4, 0 @@ -1395,7 +1395,7 @@ _0809CA48: cmp r0, 0 beq _0809CA64 movs r0, 0xD - bl sav12_xor_increment + bl IncrementGameStat ldr r0, =gUnknown_08291FC0 bl script_env_1_execute_new_script b _0809CB28 diff --git a/asm/field_player_avatar.s b/asm/field_player_avatar.s index 7706be947..e88eaa650 100644 --- a/asm/field_player_avatar.s +++ b/asm/field_player_avatar.s @@ -993,7 +993,7 @@ _0808B0E4: cmp r0, 0 beq _0808B108 movs r0, 0x2B - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0x6 b _0808B152 _0808B108: diff --git a/asm/hall_of_fame.s b/asm/hall_of_fame.s index 2bbb0ed4a..1f7a56418 100644 --- a/asm/hall_of_fame.s +++ b/asm/hall_of_fame.s @@ -519,16 +519,16 @@ sub_81739C4: @ 81739C4 push {r4,r5,lr} lsls r0, 24 lsrs r4, r0, 24 - ldr r1, =gUnknown_03006214 + ldr r1, =gGameContinueCallback ldr r0, =sub_81736D8 str r0, [r1] movs r0, 0x3 - bl save_game_when_memory_present + bl TrySavingData lsls r0, 24 lsrs r0, 24 cmp r0, 0xFF bne _08173A34 - ldr r0, =gUnknown_030061FC + ldr r0, =gDamagedSaveSectors ldr r0, [r0] cmp r0, 0 beq _08173A34 @@ -1651,7 +1651,7 @@ _081743BC: _081743C6: strh r0, [r1, 0x8] movs r0, 0xA - bl sub_80847F8 + bl GetGameStat ldr r2, =gTasks adds r1, r5, r6 lsls r1, 3 diff --git a/asm/intro.s b/asm/intro.s index 71b97da03..54baf2038 100644 --- a/asm/intro.s +++ b/asm/intro.s @@ -309,7 +309,7 @@ c2_copyright_1: @ 816CEAC lsrs r0, 16 bl InitSaveBlockPointersWithRandomOffset bl sub_808447C - bl sub_8152680 + bl ResetSaveCounters movs r0, 0 bl sub_81534D0 ldr r0, =gUnknown_03006210 diff --git a/asm/item_use.s b/asm/item_use.s index 5e50e434d..4f9e9e65a 100644 --- a/asm/item_use.s +++ b/asm/item_use.s @@ -536,7 +536,7 @@ ItemUseOutOfBattle_Itemfinder: @ 80FD490 lsls r4, 24 lsrs r4, 24 movs r0, 0x27 - bl sav12_xor_increment + bl IncrementGameStat ldr r1, =gUnknown_0203A0F4 ldr r0, =ItemUseOnFieldCB_Itemfinder str r0, [r1] diff --git a/asm/link.s b/asm/link.s index 88b2f5243..17ed8d718 100644 --- a/asm/link.s +++ b/asm/link.s @@ -622,7 +622,7 @@ _08009956: cmp r0, 0 beq _08009968 movs r0, 0x1 - bl save_game_when_memory_present + bl TrySavingData _08009968: ldrh r1, [r4, 0x2E] movs r0, 0x4 @@ -20827,7 +20827,7 @@ _08013FFC: movs r2, 0x64 bl memcpy movs r0, 0x32 - bl sav12_xor_increment + bl IncrementGameStat bl sub_800A5B4 b _0801405A .pool @@ -29860,7 +29860,7 @@ _08018EC0: .pool _08018ECC: movs r0, 0 - bl save_game_when_memory_present + bl TrySavingData b _08018EEC _08018ED4: ldr r0, =gUnknown_085EF5E0 @@ -33410,7 +33410,7 @@ _0801AB1E: cmp r4, r6 ble _0801AB1E movs r0, 0x32 - bl sav12_xor_increment + bl IncrementGameStat bl calc_player_party_count ldr r0, =gUnknown_02038BCA movs r2, 0xC0 @@ -38746,7 +38746,7 @@ _0801D6D8: b _0801D7FE _0801D6EA: ldrb r0, [r2, 0x4] - bl sub_80847F8 + bl GetGameStat adds r2, r0, 0 adds r0, r4, 0 movs r1, 0x12 @@ -41252,7 +41252,7 @@ _0801EB04: bne _0801EBC2 bl sub_8076D5C movs r0, 0 - bl save_game_when_memory_present + bl TrySavingData ldr r0, =gUnknown_02022C84 ldr r1, [r0] movs r0, 0x8 @@ -52525,7 +52525,7 @@ sub_8024578: @ 8024578 adds r4, r0, 0 movs r5, 0 movs r0, 0x33 - bl sav12_xor_increment + bl IncrementGameStat strb r5, [r4, 0xD] movs r1, 0 strh r5, [r4, 0x10] diff --git a/asm/load_save.s b/asm/load_save.s index 8cb8ee993..205333800 100644 --- a/asm/load_save.s +++ b/asm/load_save.s @@ -398,23 +398,23 @@ _08076E7A: .pool thumb_func_end save_deserialize_npcs - thumb_func_start save_serialize_game -save_serialize_game: @ 8076EAC + 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 save_serialize_game + thumb_func_end SaveSerializedGame - thumb_func_start save_deserialize_game -save_deserialize_game: @ 8076EBC + 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 save_deserialize_game + thumb_func_end LoadSerializedGame thumb_func_start copy_bags_and_unk_data_from_save_blocks copy_bags_and_unk_data_from_save_blocks: @ 8076ECC diff --git a/asm/mauville_old_man.s b/asm/mauville_old_man.s index fb6378bcc..ef08dc65f 100644 --- a/asm/mauville_old_man.s +++ b/asm/mauville_old_man.s @@ -1775,7 +1775,7 @@ sub_8120E74: @ 8120E74 bne _08120E80 movs r0, 0 _08120E80: - bl sub_80847F8 + bl GetGameStat pop {r1} bx r1 thumb_func_end sub_8120E74 diff --git a/asm/mystery_event_menu.s b/asm/mystery_event_menu.s index 125f9c71b..003fa80f5 100644 --- a/asm/mystery_event_menu.s +++ b/asm/mystery_event_menu.s @@ -484,7 +484,7 @@ _08178D74: cmp r0, 0 bne _08178E34 movs r0, 0 - bl save_game_when_memory_present + bl TrySavingData b _08178E34 .pool _08178DBC: diff --git a/asm/reset_rtc_screen.s b/asm/reset_rtc_screen.s index 06fba72d5..f4904a147 100644 --- a/asm/reset_rtc_screen.s +++ b/asm/reset_rtc_screen.s @@ -1269,7 +1269,7 @@ _0809F340: .pool _0809F398: movs r0, 0 - bl save_game_when_memory_present + bl TrySavingData lsls r0, 24 lsrs r0, 24 cmp r0, 0x1 diff --git a/asm/rom4.s b/asm/rom4.s index 9f2ac2252..498d8acac 100644 --- a/asm/rom4.s +++ b/asm/rom4.s @@ -155,16 +155,16 @@ _080847AC: bx r0 thumb_func_end sub_80847A8 - thumb_func_start sav12_xor_increment -@ void sav12_xor_increment(u8 a1) -sav12_xor_increment: @ 80847C4 + thumb_func_start IncrementGameStat +@ void IncrementGameStat(u8 a1) +IncrementGameStat: @ 80847C4 push {r4,lr} lsls r0, 24 lsrs r4, r0, 24 cmp r4, 0x33 bhi _080847EC adds r0, r4, 0 - bl sub_80847F8 + bl GetGameStat adds r1, r0, 0 ldr r0, =0x00fffffe cmp r1, r0 @@ -182,10 +182,10 @@ _080847EC: pop {r0} bx r0 .pool - thumb_func_end sav12_xor_increment + thumb_func_end IncrementGameStat - thumb_func_start sub_80847F8 -sub_80847F8: @ 80847F8 + thumb_func_start GetGameStat +GetGameStat: @ 80847F8 push {lr} lsls r0, 24 lsrs r1, r0, 24 @@ -210,7 +210,7 @@ _08084828: _0808482A: pop {r1} bx r1 - thumb_func_end sub_80847F8 + thumb_func_end GetGameStat thumb_func_start sav12_xor_set sav12_xor_set: @ 8084830 diff --git a/asm/rom6.s b/asm/rom6.s index be0170dae..766aa8fba 100644 --- a/asm/rom6.s +++ b/asm/rom6.s @@ -330,7 +330,7 @@ oei_rocksmash: @ 81356E4 strh r0, [r1, 0x18] strh r2, [r1, 0x1A] movs r0, 0x13 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0 pop {r1} bx r1 @@ -4055,7 +4055,7 @@ _08137760: bl FlagSet _0813776C: movs r0, 0x1 - bl sub_80847F8 + bl GetGameStat cmp r0, 0 bne _0813778E ldr r0, =gSaveBlock2Ptr @@ -4132,7 +4132,7 @@ _0813780A: cmp r7, 0x1 bne _0813785A movs r0, 0x2A - bl sav12_xor_increment + bl IncrementGameStat ldr r0, =0x0000089b bl FlagSet mov r3, sp @@ -7336,7 +7336,7 @@ sub_8139200: @ 8139200 sub_8139228: @ 8139228 push {lr} movs r0, 0x20 - bl sub_80847F8 + bl GetGameStat lsls r0, 16 lsrs r0, 16 pop {r1} @@ -7501,7 +7501,7 @@ sub_8139370: @ 8139370 push {r4,lr} sub sp, 0x4 movs r0, 0x2A - bl sav12_xor_increment + bl IncrementGameStat ldr r0, =0x0000089b bl FlagSet movs r1, 0x1 diff --git a/asm/rom_80C6FA0.s b/asm/rom_80C6FA0.s index 8a56841e9..00433b1c6 100644 --- a/asm/rom_80C6FA0.s +++ b/asm/rom_80C6FA0.s @@ -24469,7 +24469,7 @@ sub_80D3A6C: @ 80D3A6C strh r0, [r1, 0x18] strh r2, [r1, 0x1A] movs r0, 0x12 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0 pop {r1} bx r1 @@ -24507,7 +24507,7 @@ sub_80D3ABC: @ 80D3ABC strh r0, [r1, 0x18] strh r2, [r1, 0x1A] movs r0, 0x12 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0 pop {r1} bx r1 diff --git a/asm/rom_8158B30.s b/asm/rom_8158B30.s index bb1f1b99b..dfd4b13ee 100644 --- a/asm/rom_8158B30.s +++ b/asm/rom_8158B30.s @@ -23687,7 +23687,7 @@ _081650DC: cmp r0, 0 beq _08165132 movs r0, 0x2A - bl sav12_xor_increment + bl IncrementGameStat mov r0, r8 cmp r0, 0x1 ble _08165110 diff --git a/asm/rom_81700F8.s b/asm/rom_81700F8.s index 124d12bf3..115b9d0cb 100644 --- a/asm/rom_81700F8.s +++ b/asm/rom_81700F8.s @@ -31,7 +31,7 @@ sub_81700F8: @ 81700F8 lsrs r0, 16 bl InitSaveBlockPointersWithRandomOffset bl sub_808447C - bl sub_8152680 + bl ResetSaveCounters movs r0, 0 bl sub_81534D0 ldr r0, =gUnknown_03006210 diff --git a/asm/rom_8184DA4.s b/asm/rom_8184DA4.s index ecb2d596d..3a8ad2d0b 100644 --- a/asm/rom_8184DA4.s +++ b/asm/rom_8184DA4.s @@ -33562,7 +33562,7 @@ sub_8196D74: @ 8196D74 bl __divsi3 mov r8, r0 movs r0, 0x9 - bl sub_80847F8 + bl GetGameStat eors r4, r5 eors r5, r0 adds r4, r5 @@ -60482,7 +60482,7 @@ _081A4C4E: bl copy_player_party_from_sav1 bl sub_8076D5C movs r0, 0x1 - bl save_game_when_memory_present + bl TrySavingData bl sav2_gender2_inplace_and_xFE strb r5, [r4] adds r4, r7, 0 @@ -65191,7 +65191,7 @@ sub_81A7418: @ 81A7418 strb r0, [r1] bl save_serialize_map movs r0, 0x1 - bl save_game_when_memory_present + bl TrySavingData pop {r4} pop {r0} bx r0 @@ -68828,7 +68828,7 @@ sub_81A9134: @ 81A9134 strb r0, [r1] bl save_serialize_map movs r0, 0x1 - bl save_game_when_memory_present + bl TrySavingData pop {r4} pop {r0} bx r0 diff --git a/asm/rom_81BE66C.s b/asm/rom_81BE66C.s index 529434c9c..808a7b661 100644 --- a/asm/rom_81BE66C.s +++ b/asm/rom_81BE66C.s @@ -25858,7 +25858,7 @@ sub_81CBE50: @ 81CBE50 lsls r0, 16 lsrs r4, r0, 16 movs r0, 0x9 - bl sub_80847F8 + bl GetGameStat adds r1, r0, 0 ldr r0, =0x0001869f cmp r1, r0 diff --git a/asm/roulette.s b/asm/roulette.s index a0edead88..5332c4a46 100644 --- a/asm/roulette.s +++ b/asm/roulette.s @@ -2559,7 +2559,7 @@ sub_8141800: @ 8141800 cmp r0, 0 beq _08141888 movs r0, 0x1D - bl sub_80847F8 + bl GetGameStat ldrh r1, [r4, 0x1E] adds r1, 0x1 strh r1, [r4, 0x1E] diff --git a/asm/safari_zone.s b/asm/safari_zone.s index 618ce44e2..32e80e23d 100644 --- a/asm/safari_zone.s +++ b/asm/safari_zone.s @@ -41,7 +41,7 @@ ResetSafariZoneFlag: @ 80FC0C4 sub_80FC0D4: @ 80FC0D4 push {lr} movs r0, 0x11 - bl sav12_xor_increment + bl IncrementGameStat bl SetSafariZoneFlag bl sub_80FC244 ldr r1, =gUnknown_0203A04C diff --git a/asm/save.s b/asm/save.s index 004514594..7047c3c25 100644 --- a/asm/save.s +++ b/asm/save.s @@ -5,1746 +5,12 @@ .text - thumb_func_start calls_flash_erase_block -calls_flash_erase_block: @ 8152650 - push {r4,r5,lr} - movs r4, 0 - ldr r5, =EraseFlashSector -_08152656: - ldr r1, [r5] - adds r0, r4, 0 - bl _call_via_r1 - adds r0, r4, 0 - adds r0, 0x10 - lsls r0, 16 - lsrs r0, 16 - ldr r1, [r5] - bl _call_via_r1 - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, 0xF - bls _08152656 - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end calls_flash_erase_block - - thumb_func_start sub_8152680 -sub_8152680: @ 8152680 - ldr r0, =gUnknown_03006200 - movs r1, 0 - str r1, [r0] - ldr r0, =gUnknown_030061F0 - strh r1, [r0] - ldr r0, =gUnknown_030061FC - str r1, [r0] - bx lr - .pool - thumb_func_end sub_8152680 - - thumb_func_start sub_815269C -sub_815269C: @ 815269C - push {r4,lr} - lsls r0, 24 - lsrs r0, 24 - lsls r1, 24 - lsrs r3, r1, 24 - movs r4, 0 - cmp r0, 0x1 - beq _081526D0 - cmp r0, 0x1 - bgt _081526B6 - cmp r0, 0 - beq _081526BC - b _081526F4 -_081526B6: - cmp r0, 0x2 - beq _081526E4 - b _081526F4 -_081526BC: - ldr r2, =gUnknown_030061FC - movs r1, 0x1 - lsls r1, r3 - ldr r0, [r2] - orrs r0, r1 - str r0, [r2] - b _081526F4 - .pool -_081526D0: - ldr r2, =gUnknown_030061FC - adds r1, r0, 0 - lsls r1, r3 - ldr r0, [r2] - bics r0, r1 - str r0, [r2] - b _081526F4 - .pool -_081526E4: - ldr r0, =gUnknown_030061FC - movs r1, 0x1 - lsls r1, r3 - ldr r0, [r0] - ands r0, r1 - cmp r0, 0 - beq _081526F4 - movs r4, 0x1 -_081526F4: - adds r0, r4, 0 - pop {r4} - pop {r1} - bx r1 - .pool - thumb_func_end sub_815269C - - thumb_func_start save_write_to_flash -save_write_to_flash: @ 8152700 - push {r4-r7,lr} - adds r7, r1, 0 - lsls r0, 16 - lsrs r2, r0, 16 - ldr r1, =gUnknown_03006204 - ldr r0, =gUnknown_0203ABBC - str r0, [r1] - ldr r0, =0x0000ffff - cmp r2, r0 - beq _08152730 - adds r0, r2, 0 - adds r1, r7, 0 - bl sub_81527A0 - lsls r0, 24 - lsrs r5, r0, 24 - b _08152782 - .pool -_08152730: - ldr r1, =gUnknown_030061F8 - ldr r5, =gUnknown_030061F0 - ldrh r0, [r5] - strh r0, [r1] - ldr r1, =gUnknown_030061F4 - ldr r6, =gUnknown_03006200 - ldr r4, [r6] - str r4, [r1] - adds r0, 0x1 - strh r0, [r5] - ldrh r0, [r5] - movs r1, 0xE - bl __umodsi3 - strh r0, [r5] - adds r4, 0x1 - str r4, [r6] - movs r5, 0x1 - movs r4, 0 -_08152756: - adds r0, r4, 0 - adds r1, r7, 0 - bl sub_81527A0 - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, 0xD - bls _08152756 - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _08152782 - movs r5, 0xFF - ldr r1, =gUnknown_030061F0 - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r1] - ldr r1, =gUnknown_03006200 - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r1] -_08152782: - adds r0, r5, 0 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end save_write_to_flash - - thumb_func_start sub_81527A0 -sub_81527A0: @ 81527A0 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - adds r4, r1, 0 - lsls r0, 16 - lsrs r6, r0, 16 - ldr r0, =gUnknown_030061F0 - ldrh r0, [r0] - adds r0, r6, r0 - lsls r0, 16 - lsrs r5, r0, 16 - adds r0, r5, 0 - movs r1, 0xE - bl __umodsi3 - lsls r0, 16 - lsrs r5, r0, 16 - ldr r2, =gUnknown_03006200 - ldr r1, [r2] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 1 - adds r0, r5, r0 - lsls r0, 16 - lsrs r5, r0, 16 - lsls r0, r6, 3 - adds r0, r4 - ldr r1, [r0] - mov r10, r1 - ldrh r4, [r0, 0x4] - movs r3, 0 - mov r9, r2 - ldr r2, =gUnknown_03006204 - mov r12, r2 - mov r8, r12 - movs r2, 0 - ldr r1, =0x00000fff -_081527F2: - mov r7, r8 - ldr r0, [r7] - adds r0, r3 - strb r2, [r0] - adds r0, r3, 0x1 - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, r1 - bls _081527F2 - mov r0, r12 - ldr r1, [r0] - ldr r2, =0x00000ff4 - adds r0, r1, r2 - strh r6, [r0] - ldr r3, =0x00000ff8 - adds r2, r1, r3 - ldr r0, =0x08012025 - str r0, [r2] - ldr r6, =0x00000ffc - adds r1, r6 - mov r7, r9 - ldr r0, [r7] - str r0, [r1] - movs r3, 0 - lsls r5, 24 - cmp r3, r4 - bcs _08152840 - mov r2, r12 -_0815282A: - ldr r1, [r2] - adds r1, r3 - mov r6, r10 - adds r0, r6, r3 - ldrb r0, [r0] - strb r0, [r1] - adds r0, r3, 0x1 - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, r4 - bcc _0815282A -_08152840: - mov r0, r10 - adds r1, r4, 0 - bl sub_8153164 - ldr r1, =gUnknown_03006204 - ldr r1, [r1] - ldr r7, =0x00000ff6 - adds r2, r1, r7 - strh r0, [r2] - lsrs r0, r5, 24 - bl sub_8152908 - lsls r0, 24 - lsrs r0, 24 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_81527A0 - - thumb_func_start sub_8152890 -sub_8152890: @ 8152890 - push {r4-r7,lr} - adds r5, r1, 0 - lsls r0, 24 - lsrs r7, r0, 24 - lsls r2, 16 - lsrs r2, 16 - ldr r4, =gUnknown_0203ABBC - movs r3, 0 - movs r6, 0 - ldr r1, =0x00000fff -_081528A4: - adds r0, r4, r3 - strb r6, [r0] - adds r0, r3, 0x1 - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, r1 - bls _081528A4 - ldr r0, =0x00000ff8 - adds r1, r4, r0 - ldr r0, =0x08012025 - str r0, [r1] - movs r3, 0 - cmp r3, r2 - bcs _081528D2 -_081528C0: - adds r1, r4, r3 - adds r0, r5, r3 - ldrb r0, [r0] - strb r0, [r1] - adds r0, r3, 0x1 - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, r2 - bcc _081528C0 -_081528D2: - adds r0, r5, 0 - adds r1, r2, 0 - bl sub_8153164 - ldr r2, =0x00000ff4 - adds r1, r4, r2 - strh r0, [r1] - adds r0, r7, 0 - adds r1, r4, 0 - bl sub_8152908 - lsls r0, 24 - lsrs r0, 24 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152890 - - thumb_func_start sub_8152908 -sub_8152908: @ 8152908 - push {r4,lr} - lsls r0, 24 - lsrs r4, r0, 24 - adds r0, r4, 0 - bl ProgramFlashSectorAndVerify - cmp r0, 0 - bne _08152924 - movs r0, 0x1 - adds r1, r4, 0 - bl sub_815269C - movs r0, 0x1 - b _0815292E -_08152924: - movs r0, 0 - adds r1, r4, 0 - bl sub_815269C - movs r0, 0xFF -_0815292E: - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_8152908 - - thumb_func_start sub_8152934 -sub_8152934: @ 8152934 - push {r4-r6,lr} - ldr r1, =gUnknown_03006204 - ldr r0, =gUnknown_0203ABBC - str r0, [r1] - ldr r1, =gUnknown_030061F8 - ldr r5, =gUnknown_030061F0 - ldrh r0, [r5] - strh r0, [r1] - ldr r1, =gUnknown_030061F4 - ldr r6, =gUnknown_03006200 - ldr r4, [r6] - str r4, [r1] - adds r0, 0x1 - strh r0, [r5] - ldrh r0, [r5] - movs r1, 0xE - bl __umodsi3 - strh r0, [r5] - adds r4, 0x1 - str r4, [r6] - ldr r1, =gUnknown_03006208 - movs r0, 0 - strh r0, [r1] - ldr r1, =gUnknown_030061FC - movs r0, 0 - str r0, [r1] - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152934 - - thumb_func_start sub_8152990 -sub_8152990: @ 8152990 - ldr r1, =gUnknown_03006204 - ldr r0, =gUnknown_0203ABBC - str r0, [r1] - ldr r1, =gUnknown_030061F8 - ldr r0, =gUnknown_030061F0 - ldrh r0, [r0] - strh r0, [r1] - ldr r1, =gUnknown_030061F4 - ldr r0, =gUnknown_03006200 - ldr r0, [r0] - str r0, [r1] - ldr r1, =gUnknown_03006208 - movs r0, 0 - strh r0, [r1] - ldr r1, =gUnknown_030061FC - movs r0, 0 - str r0, [r1] - bx lr - .pool - thumb_func_end sub_8152990 - - thumb_func_start sub_81529D4 -sub_81529D4: @ 81529D4 - push {r4,r5,lr} - lsls r0, 16 - lsrs r0, 16 - ldr r4, =gUnknown_03006208 - ldrh r2, [r4] - subs r0, 0x1 - cmp r2, r0 - bge _08152A28 - movs r5, 0x1 - adds r0, r2, 0 - bl sub_81527A0 - ldrh r0, [r4] - adds r0, 0x1 - strh r0, [r4] - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _08152A2A - movs r5, 0xFF - ldr r1, =gUnknown_030061F0 - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r1] - ldr r1, =gUnknown_03006200 - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r1] - b _08152A2A - .pool -_08152A28: - movs r5, 0xFF -_08152A2A: - adds r0, r5, 0 - pop {r4,r5} - pop {r1} - bx r1 - thumb_func_end sub_81529D4 - - thumb_func_start sub_8152A34 -sub_8152A34: @ 8152A34 - push {r4,lr} - lsls r0, 16 - movs r4, 0x1 - ldr r2, =0xffff0000 - adds r0, r2 - lsrs r0, 16 - bl calls_flash_erase_block_2 - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _08152A5E - movs r4, 0xFF - ldr r1, =gUnknown_030061F0 - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r1] - ldr r1, =gUnknown_03006200 - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r1] -_08152A5E: - adds r0, r4, 0 - pop {r4} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152A34 - - thumb_func_start calls_flash_erase_block_2 -calls_flash_erase_block_2: @ 8152A80 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - adds r4, r1, 0 - lsls r0, 16 - lsrs r6, r0, 16 - ldr r0, =gUnknown_030061F0 - ldrh r0, [r0] - adds r0, r6, r0 - lsls r0, 16 - lsrs r5, r0, 16 - adds r0, r5, 0 - movs r1, 0xE - bl __umodsi3 - lsls r0, 16 - lsrs r5, r0, 16 - ldr r2, =gUnknown_03006200 - ldr r1, [r2] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 1 - adds r0, r5, r0 - lsls r0, 16 - lsrs r5, r0, 16 - lsls r0, r6, 3 - adds r0, r4 - ldr r1, [r0] - mov r10, r1 - ldrh r3, [r0, 0x4] - movs r4, 0 - mov r9, r2 - ldr r2, =gUnknown_03006204 - mov r12, r2 - mov r8, r12 - movs r2, 0 - ldr r1, =0x00000fff -_08152AD2: - mov r7, r8 - ldr r0, [r7] - adds r0, r4 - strb r2, [r0] - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, r1 - bls _08152AD2 - mov r0, r12 - ldr r1, [r0] - ldr r2, =0x00000ff4 - adds r0, r1, r2 - strh r6, [r0] - ldr r6, =0x00000ff8 - adds r2, r1, r6 - ldr r0, =0x08012025 - str r0, [r2] - ldr r7, =0x00000ffc - adds r1, r7 - mov r2, r9 - ldr r0, [r2] - str r0, [r1] - movs r4, 0 - lsls r6, r5, 24 - mov r8, r6 - cmp r4, r3 - bcs _08152B22 - mov r2, r12 -_08152B0C: - ldr r1, [r2] - adds r1, r4 - mov r7, r10 - adds r0, r7, r4 - ldrb r0, [r0] - strb r0, [r1] - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, r3 - bcc _08152B0C -_08152B22: - mov r0, r10 - adds r1, r3, 0 - bl sub_8153164 - ldr r1, =gUnknown_03006204 - ldr r1, [r1] - ldr r2, =0x00000ff6 - adds r1, r2 - strh r0, [r1] - ldr r0, =EraseFlashSector - ldr r1, [r0] - adds r0, r5, 0 - bl _call_via_r1 - movs r6, 0x1 - movs r4, 0 - ldr r7, =0x00000ff7 - mov r9, r7 - ldr r7, =ProgramFlashByte - b _08152B82 - .pool -_08152B7C: - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 -_08152B82: - cmp r4, r9 - bhi _08152BA0 - ldr r0, =gUnknown_03006204 - ldr r0, [r0] - adds r0, r4 - ldrb r2, [r0] - ldr r3, [r7] - adds r0, r5, 0 - adds r1, r4, 0 - bl _call_via_r3 - lsls r0, 16 - cmp r0, 0 - beq _08152B7C - movs r6, 0xFF -_08152BA0: - cmp r6, 0xFF - bne _08152BB0 - mov r0, r8 - lsrs r1, r0, 24 - b _08152C08 - .pool -_08152BB0: - movs r6, 0x1 - movs r4, 0 - ldr r1, =ProgramFlashByte - mov r9, r1 - ldr r7, =0x00000ff9 - b _08152BCA - .pool -_08152BC4: - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 -_08152BCA: - cmp r4, 0x6 - bhi _08152BEC - adds r1, r4, r7 - ldr r0, =gUnknown_03006204 - ldr r0, [r0] - adds r0, r4, r0 - adds r0, r7 - ldrb r2, [r0] - mov r0, r9 - ldr r3, [r0] - adds r0, r5, 0 - bl _call_via_r3 - lsls r0, 16 - cmp r0, 0 - beq _08152BC4 - movs r6, 0xFF -_08152BEC: - cmp r6, 0xFF - beq _08152C04 - mov r2, r8 - lsrs r1, r2, 24 - movs r0, 0x1 - bl sub_815269C - movs r0, 0x1 - b _08152C10 - .pool -_08152C04: - mov r6, r8 - lsrs r1, r6, 24 -_08152C08: - movs r0, 0 - bl sub_815269C - movs r0, 0xFF -_08152C10: - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - thumb_func_end calls_flash_erase_block_2 - - thumb_func_start sav12_xor_get -sav12_xor_get: @ 8152C20 - push {r4-r6,lr} - lsls r0, 16 - ldr r6, =gUnknown_030061F0 - lsrs r0, 16 - ldrh r1, [r6] - adds r0, r1 - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r4, 0 - movs r1, 0xE - bl __umodsi3 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r5, =gUnknown_03006200 - ldr r1, [r5] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 1 - adds r0, r4, r0 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r0, =ProgramFlashByte - ldr r1, =0x00000ff8 - ldr r3, [r0] - adds r0, r4, 0 - movs r2, 0x25 - bl _call_via_r3 - lsls r0, 16 - cmp r0, 0 - bne _08152C84 - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0x1 - bl sub_815269C - movs r0, 0x1 - b _08152C9C - .pool -_08152C84: - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0 - bl sub_815269C - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r6] - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r5] - movs r0, 0xFF -_08152C9C: - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sav12_xor_get - - thumb_func_start sub_8152CAC -sub_8152CAC: @ 8152CAC - push {r4-r6,lr} - lsls r0, 16 - ldr r6, =gUnknown_030061F0 - lsrs r0, 16 - ldrh r1, [r6] - adds r0, r1 - subs r0, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r4, 0 - movs r1, 0xE - bl __umodsi3 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r5, =gUnknown_03006200 - ldr r1, [r5] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 1 - adds r0, r4, r0 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r3, =ProgramFlashByte - ldr r1, =0x00000ff8 - ldr r0, =gUnknown_03006204 - ldr r0, [r0] - adds r0, r1 - ldrb r2, [r0] - ldr r3, [r3] - adds r0, r4, 0 - bl _call_via_r3 - lsls r0, 16 - cmp r0, 0 - bne _08152D1C - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0x1 - bl sub_815269C - movs r0, 0x1 - b _08152D34 - .pool -_08152D1C: - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0 - bl sub_815269C - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r6] - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r5] - movs r0, 0xFF -_08152D34: - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152CAC - - thumb_func_start sub_8152D44 -sub_8152D44: @ 8152D44 - push {r4-r6,lr} - lsls r0, 16 - ldr r6, =gUnknown_030061F0 - lsrs r0, 16 - ldrh r1, [r6] - adds r0, r1 - subs r0, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - adds r0, r4, 0 - movs r1, 0xE - bl __umodsi3 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r5, =gUnknown_03006200 - ldr r1, [r5] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 1 - adds r0, r4, r0 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r0, =ProgramFlashByte - ldr r1, =0x00000ff8 - ldr r3, [r0] - adds r0, r4, 0 - movs r2, 0x25 - bl _call_via_r3 - lsls r0, 16 - cmp r0, 0 - bne _08152DA8 - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0x1 - bl sub_815269C - movs r0, 0x1 - b _08152DC0 - .pool -_08152DA8: - lsls r1, r4, 24 - lsrs r1, 24 - movs r0, 0 - bl sub_815269C - ldr r0, =gUnknown_030061F8 - ldrh r0, [r0] - strh r0, [r6] - ldr r0, =gUnknown_030061F4 - ldr r0, [r0] - str r0, [r5] - movs r0, 0xFF -_08152DC0: - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152D44 - - thumb_func_start sub_8152DD0 -sub_8152DD0: @ 8152DD0 - push {r4-r6,lr} - adds r6, r1, 0 - lsls r0, 16 - lsrs r4, r0, 16 - ldr r1, =gUnknown_03006204 - ldr r0, =gUnknown_0203ABBC - str r0, [r1] - ldr r0, =0x0000ffff - cmp r4, r0 - beq _08152DF4 - movs r5, 0xFF - b _08152E06 - .pool -_08152DF4: - adds r0, r6, 0 - bl sub_8152EC8 - lsls r0, 24 - lsrs r5, r0, 24 - adds r0, r4, 0 - adds r1, r6, 0 - bl sub_8152E10 -_08152E06: - adds r0, r5, 0 - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end sub_8152DD0 - - thumb_func_start sub_8152E10 -sub_8152E10: @ 8152E10 - push {r4-r7,lr} - mov r7, r8 - push {r7} - mov r8, r1 - ldr r0, =gUnknown_03006200 - ldr r1, [r0] - movs r0, 0x1 - ands r1, r0 - lsls r0, r1, 3 - subs r0, r1 - lsls r0, 17 - lsrs r7, r0, 16 - movs r5, 0 - ldr r6, =gUnknown_03006204 -_08152E2C: - adds r0, r5, r7 - lsls r0, 24 - lsrs r0, 24 - ldr r1, [r6] - bl sub_815314C - ldr r0, [r6] - ldr r1, =0x00000ff4 - adds r0, r1 - ldrh r1, [r0] - cmp r1, 0 - bne _08152E48 - ldr r0, =gUnknown_030061F0 - strh r5, [r0] -_08152E48: - ldr r0, [r6] - lsls r1, 3 - mov r2, r8 - adds r4, r1, r2 - ldrh r1, [r4, 0x4] - bl sub_8153164 - lsls r0, 16 - lsrs r3, r0, 16 - ldr r2, [r6] - ldr r1, =0x00000ff8 - adds r0, r2, r1 - ldr r1, [r0] - ldr r0, =0x08012025 - adds r5, 0x1 - cmp r1, r0 - bne _08152E98 - ldr r1, =0x00000ff6 - adds r0, r2, r1 - ldrh r0, [r0] - cmp r0, r3 - bne _08152E98 - movs r2, 0 - ldrh r0, [r4, 0x4] - cmp r2, r0 - bcs _08152E98 - adds r3, r4, 0 - ldr r4, =gUnknown_03006204 -_08152E80: - ldr r1, [r3] - adds r1, r2 - ldr r0, [r4] - adds r0, r2 - ldrb r0, [r0] - strb r0, [r1] - adds r0, r2, 0x1 - lsls r0, 16 - lsrs r2, r0, 16 - ldrh r1, [r3, 0x4] - cmp r2, r1 - bcc _08152E80 -_08152E98: - lsls r0, r5, 16 - lsrs r5, r0, 16 - cmp r5, 0xD - bls _08152E2C - movs r0, 0x1 - pop {r3} - mov r8, r3 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152E10 - - thumb_func_start sub_8152EC8 -sub_8152EC8: @ 8152EC8 - push {r4-r7,lr} - mov r7, r10 - mov r6, r9 - mov r5, r8 - push {r5-r7} - sub sp, 0x4 - mov r10, r0 - movs r0, 0 - mov r8, r0 - mov r9, r0 - movs r6, 0 - movs r5, 0 - movs r4, 0 - ldr r7, =gUnknown_03006204 -_08152EE4: - lsls r0, r4, 24 - lsrs r0, 24 - ldr r1, [r7] - bl sub_815314C - ldr r2, [r7] - ldr r1, =0x00000ff8 - adds r0, r2, r1 - ldr r1, [r0] - ldr r0, =0x08012025 - cmp r1, r0 - bne _08152F34 - movs r5, 0x1 - ldr r3, =0x00000ff4 - adds r0, r2, r3 - ldrh r0, [r0] - lsls r0, 3 - add r0, r10 - ldrh r1, [r0, 0x4] - adds r0, r2, 0 - bl sub_8153164 - lsls r0, 16 - lsrs r2, r0, 16 - ldr r1, [r7] - ldr r3, =0x00000ff6 - adds r0, r1, r3 - ldrh r0, [r0] - cmp r0, r2 - bne _08152F34 - ldr r2, =0x00000ffc - adds r0, r1, r2 - ldr r0, [r0] - mov r8, r0 - subs r3, 0x2 - adds r1, r3 - adds r0, r5, 0 - ldrh r1, [r1] - lsls r0, r1 - orrs r6, r0 -_08152F34: - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, 0xD - bls _08152EE4 - cmp r5, 0 - beq _08152F70 - ldr r0, =0x00003fff - movs r1, 0xFF - str r1, [sp] - cmp r6, r0 - bne _08152F74 - movs r2, 0x1 - str r2, [sp] - b _08152F74 - .pool -_08152F70: - movs r3, 0 - str r3, [sp] -_08152F74: - movs r6, 0 - movs r5, 0 - movs r4, 0 - ldr r7, =gUnknown_03006204 -_08152F7C: - adds r0, r4, 0 - adds r0, 0xE - lsls r0, 24 - lsrs r0, 24 - ldr r1, [r7] - bl sub_815314C - ldr r2, [r7] - ldr r1, =0x00000ff8 - adds r0, r2, r1 - ldr r1, [r0] - ldr r0, =0x08012025 - cmp r1, r0 - bne _08152FD0 - movs r5, 0x1 - ldr r3, =0x00000ff4 - adds r0, r2, r3 - ldrh r0, [r0] - lsls r0, 3 - add r0, r10 - ldrh r1, [r0, 0x4] - adds r0, r2, 0 - bl sub_8153164 - lsls r0, 16 - lsrs r2, r0, 16 - ldr r1, [r7] - ldr r3, =0x00000ff6 - adds r0, r1, r3 - ldrh r0, [r0] - cmp r0, r2 - bne _08152FD0 - ldr r2, =0x00000ffc - adds r0, r1, r2 - ldr r0, [r0] - mov r9, r0 - subs r3, 0x2 - adds r1, r3 - adds r0, r5, 0 - ldrh r1, [r1] - lsls r0, r1 - orrs r6, r0 -_08152FD0: - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, 0xD - bls _08152F7C - cmp r5, 0 - beq _08153008 - ldr r0, =0x00003fff - movs r1, 0xFF - cmp r6, r0 - bne _0815300A - movs r1, 0x1 - b _0815300A - .pool -_08153008: - movs r1, 0 -_0815300A: - ldr r0, [sp] - cmp r0, 0x1 - bne _0815307C - cmp r1, 0x1 - bne _0815306C - movs r0, 0x1 - negs r0, r0 - cmp r8, r0 - bne _08153022 - mov r1, r9 - cmp r1, 0 - beq _0815302C -_08153022: - mov r2, r8 - cmp r2, 0 - bne _08153050 - cmp r9, r0 - bne _08153050 -_0815302C: - mov r1, r8 - adds r1, 0x1 - mov r0, r9 - adds r0, 0x1 - cmp r1, r0 - bcs _08153044 - ldr r0, =gUnknown_03006200 - mov r3, r9 - b _08153064 - .pool -_08153044: - ldr r0, =gUnknown_03006200 - mov r1, r8 - str r1, [r0] - b _08153094 - .pool -_08153050: - cmp r8, r9 - bcs _08153060 - ldr r0, =gUnknown_03006200 - mov r2, r9 - str r2, [r0] - b _08153094 - .pool -_08153060: - ldr r0, =gUnknown_03006200 - mov r3, r8 -_08153064: - str r3, [r0] - b _08153094 - .pool -_0815306C: - ldr r0, =gUnknown_03006200 - mov r2, r8 - str r2, [r0] - cmp r1, 0xFF - beq _0815308C - b _08153094 - .pool -_0815307C: - cmp r1, 0x1 - bne _08153098 - ldr r0, =gUnknown_03006200 - mov r3, r9 - str r3, [r0] - ldr r0, [sp] - cmp r0, 0xFF - bne _08153094 -_0815308C: - movs r0, 0xFF - b _081530C4 - .pool -_08153094: - movs r0, 0x1 - b _081530C4 -_08153098: - ldr r2, [sp] - cmp r2, 0 - bne _081530B8 - cmp r1, 0 - bne _081530B8 - ldr r0, =gUnknown_03006200 - str r1, [r0] - ldr r0, =gUnknown_030061F0 - strh r1, [r0] - movs r0, 0 - b _081530C4 - .pool -_081530B8: - ldr r0, =gUnknown_03006200 - movs r1, 0 - str r1, [r0] - ldr r0, =gUnknown_030061F0 - strh r1, [r0] - movs r0, 0x2 -_081530C4: - add sp, 0x4 - pop {r3-r5} - mov r8, r3 - mov r9, r4 - mov r10, r5 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8152EC8 - - thumb_func_start sub_81530DC -sub_81530DC: @ 81530DC - push {r4-r6,lr} - adds r6, r1, 0 - lsls r0, 24 - lsrs r0, 24 - lsls r2, 16 - lsrs r4, r2, 16 - ldr r5, =gUnknown_0203ABBC - adds r1, r5, 0 - bl sub_815314C - ldr r1, =0x00000ff8 - adds r0, r5, r1 - ldr r1, [r0] - ldr r0, =0x08012025 - cmp r1, r0 - bne _08153144 - adds r0, r5, 0 - adds r1, r4, 0 - bl sub_8153164 - lsls r0, 16 - lsrs r0, 16 - ldr r2, =0x00000ff4 - adds r1, r5, r2 - ldrh r1, [r1] - cmp r1, r0 - bne _08153140 - movs r2, 0 - cmp r2, r4 - bcs _0815312A -_08153118: - adds r1, r6, r2 - adds r0, r5, r2 - ldrb r0, [r0] - strb r0, [r1] - adds r0, r2, 0x1 - lsls r0, 16 - lsrs r2, r0, 16 - cmp r2, r4 - bcc _08153118 -_0815312A: - movs r0, 0x1 - b _08153146 - .pool -_08153140: - movs r0, 0x2 - b _08153146 -_08153144: - movs r0, 0 -_08153146: - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end sub_81530DC - - thumb_func_start sub_815314C -sub_815314C: @ 815314C - push {lr} - adds r2, r1, 0 - lsls r0, 24 - lsrs r0, 24 - movs r3, 0x80 - lsls r3, 5 - movs r1, 0 - bl ReadFlash - movs r0, 0x1 - pop {r1} - bx r1 - thumb_func_end sub_815314C - - thumb_func_start sub_8153164 -sub_8153164: @ 8153164 - push {r4,lr} - adds r4, r0, 0 - lsls r1, 16 - movs r2, 0 - movs r3, 0 - lsrs r1, 18 - cmp r2, r1 - bcs _08153182 -_08153174: - ldm r4!, {r0} - adds r2, r0 - adds r0, r3, 0x1 - lsls r0, 16 - lsrs r3, r0, 16 - cmp r3, r1 - bcc _08153174 -_08153182: - lsrs r0, r2, 16 - adds r0, r2 - lsls r0, 16 - lsrs r0, 16 - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_8153164 - - thumb_func_start sub_8153190 -sub_8153190: @ 8153190 - push {r4,r5,lr} - ldr r3, =gUnknown_03006220 - ldr r0, =gSaveBlock2Ptr - ldr r2, =gUnknown_085CDC00 - ldrh r1, [r2] - ldr r0, [r0] - adds r0, r1 - str r0, [r3] - ldrh r0, [r2, 0x2] - strh r0, [r3, 0x4] - ldr r5, =gSaveBlock1Ptr - adds r3, 0x8 - adds r2, 0x4 - movs r4, 0x3 -_081531AC: - ldrh r0, [r2] - ldr r1, [r5] - adds r1, r0 - str r1, [r3] - ldrh r0, [r2, 0x2] - strh r0, [r3, 0x4] - adds r3, 0x8 - adds r2, 0x4 - subs r4, 0x1 - cmp r4, 0 - bge _081531AC - movs r4, 0x5 - ldr r1, =gUnknown_03006220 - ldr r5, =gUnknown_03005D94 - ldr r0, =gUnknown_085CDC00 - adds r3, r1, 0 - adds r3, 0x28 - adds r2, r0, 0 - adds r2, 0x14 -_081531D2: - ldrh r0, [r2] - ldr r1, [r5] - adds r1, r0 - str r1, [r3] - ldrh r0, [r2, 0x2] - strh r0, [r3, 0x4] - adds r3, 0x8 - adds r2, 0x4 - adds r4, 0x1 - cmp r4, 0xD - ble _081531D2 - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8153190 - - thumb_func_start calls_flash_erase_block_3 -calls_flash_erase_block_3: @ 8153204 - push {r4-r6,lr} - lsls r0, 24 - lsrs r4, r0, 24 - ldr r1, =gUnknown_0203CF5C - ldr r6, [r1] - movs r0, 0 - str r0, [r1] - bl sub_8153190 - cmp r4, 0x5 - bhi _081532AC - lsls r0, r4, 2 - ldr r1, =_0815322C - adds r0, r1 - ldr r0, [r0] - mov pc, r0 - .pool - .align 2, 0 -_0815322C: - .4byte _081532AC - .4byte _081532C4 - .4byte _081532C4 - .4byte _0815325A - .4byte _081532F8 - .4byte _08153244 -_08153244: - movs r4, 0x1C - ldr r5, =EraseFlashSector -_08153248: - ldr r1, [r5] - adds r0, r4, 0 - bl _call_via_r1 - adds r0, r4, 0x1 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x1F - bls _08153248 -_0815325A: - movs r0, 0xA - bl sub_80847F8 - ldr r1, =0x000003e6 - cmp r0, r1 - bhi _0815326C - movs r0, 0xA - bl sav12_xor_increment -_0815326C: - bl save_serialize_game - ldr r0, =0x0000ffff - ldr r1, =gUnknown_03006220 - bl save_write_to_flash - ldr r4, =0x0201c000 - movs r5, 0xF8 - lsls r5, 4 - movs r0, 0x1C - adds r1, r4, 0 - adds r2, r5, 0 - bl sub_8152890 - adds r4, r5 - movs r0, 0x1D - adds r1, r4, 0 - adds r2, r5, 0 - bl sub_8152890 - b _0815331A - .pool -_081532AC: - bl save_serialize_game - ldr r0, =0x0000ffff - ldr r1, =gUnknown_03006220 - bl save_write_to_flash - b _0815331A - .pool -_081532C4: - bl save_serialize_game - movs r4, 0 -_081532CA: - adds r0, r4, 0 - ldr r1, =gUnknown_03006220 - bl calls_flash_erase_block_2 - adds r0, r4, 0x1 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x4 - bls _081532CA - movs r4, 0 -_081532DE: - adds r0, r4, 0 - ldr r1, =gUnknown_03006220 - bl sav12_xor_get - adds r0, r4, 0x1 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x4 - bls _081532DE - b _0815331A - .pool -_081532F8: - movs r4, 0x1C - ldr r5, =EraseFlashSector -_081532FC: - ldr r1, [r5] - adds r0, r4, 0 - bl _call_via_r1 - adds r0, r4, 0x1 - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x1F - bls _081532FC - bl save_serialize_game - ldr r0, =0x0000ffff - ldr r1, =gUnknown_03006220 - bl save_write_to_flash -_0815331A: - ldr r0, =gUnknown_0203CF5C - str r6, [r0] - movs r0, 0 - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end calls_flash_erase_block_3 - - thumb_func_start save_game_when_memory_present -save_game_when_memory_present: @ 8153338 - push {r4,r5,lr} - lsls r0, 24 - lsrs r5, r0, 24 - ldr r0, =gFlashMemoryPresent - ldr r4, [r0] - cmp r4, 0x1 - bne _0815335A - adds r0, r5, 0 - bl calls_flash_erase_block_3 - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _08153370 - adds r0, r5, 0 - bl fullscreen_save_activate -_0815335A: - ldr r1, =gUnknown_03006294 - movs r0, 0xFF - strh r0, [r1] - movs r0, 0xFF - b _08153376 - .pool -_08153370: - ldr r0, =gUnknown_03006294 - strh r4, [r0] - movs r0, 0x1 -_08153376: - pop {r4,r5} - pop {r1} - bx r1 - .pool - thumb_func_end save_game_when_memory_present - - thumb_func_start sub_8153380 -sub_8153380: @ 8153380 - push {lr} - ldr r0, =gFlashMemoryPresent - ldr r0, [r0] - cmp r0, 0x1 - bne _081533A4 - bl sub_8153190 - bl save_serialize_game - ldr r0, =gUnknown_03006220 - bl sub_8152934 - movs r0, 0 - b _081533A6 - .pool -_081533A4: - movs r0, 0x1 -_081533A6: - pop {r1} - bx r1 - thumb_func_end sub_8153380 - - thumb_func_start sub_81533AC -sub_81533AC: @ 81533AC - push {r4,lr} - ldr r1, =gUnknown_03006220 - movs r0, 0xE - bl sub_81529D4 - lsls r0, 24 - lsrs r4, r0, 24 - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _081533C8 - movs r0, 0 - bl fullscreen_save_activate -_081533C8: - cmp r4, 0xFF - beq _081533D8 - movs r0, 0 - b _081533DA - .pool -_081533D8: - movs r0, 0x1 -_081533DA: - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_81533AC - - thumb_func_start sub_81533E0 -sub_81533E0: @ 81533E0 - push {lr} - ldr r1, =gUnknown_03006220 - movs r0, 0xE - bl sub_8152A34 - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _081533F8 - movs r0, 0 - bl fullscreen_save_activate -_081533F8: - movs r0, 0 - pop {r1} - bx r1 - .pool - thumb_func_end sub_81533E0 - - thumb_func_start sub_8153408 -sub_8153408: @ 8153408 - push {lr} - ldr r1, =gUnknown_03006220 - movs r0, 0xE - bl sub_8152CAC - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _08153420 - movs r0, 0 - bl fullscreen_save_activate -_08153420: - movs r0, 0 - pop {r1} - bx r1 - .pool - thumb_func_end sub_8153408 - - thumb_func_start sub_8153430 -sub_8153430: @ 8153430 - push {r4,lr} - ldr r0, =gFlashMemoryPresent - ldr r0, [r0] - cmp r0, 0x1 - bne _0815346C - bl sub_8153190 - bl save_serialize_game - ldr r4, =gUnknown_03006220 - adds r0, r4, 0 - bl sub_8152990 - ldr r0, =gUnknown_03006208 - ldrh r0, [r0] - adds r0, 0x1 - lsls r0, 16 - lsrs r0, 16 - adds r1, r4, 0 - bl sub_8152A34 - movs r0, 0 - b _0815346E - .pool -_0815346C: - movs r0, 0x1 -_0815346E: - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_8153430 - - thumb_func_start sub_8153474 -sub_8153474: @ 8153474 - push {r4-r6,lr} - movs r6, 0 - ldr r0, =gUnknown_03006208 - ldrh r1, [r0] - adds r1, 0x1 - strh r1, [r0] - lsls r0, r1, 16 - lsrs r5, r0, 16 - cmp r5, 0x4 - bhi _081534A8 - adds r0, r1, 0x1 - lsls r0, 16 - lsrs r0, 16 - ldr r4, =gUnknown_03006220 - adds r1, r4, 0 - bl sub_8152A34 - adds r0, r5, 0 - adds r1, r4, 0 - bl sub_8152D44 - b _081534B2 - .pool -_081534A8: - ldr r1, =gUnknown_03006220 - adds r0, r5, 0 - bl sub_8152D44 - movs r6, 0x1 -_081534B2: - ldr r0, =gUnknown_030061FC - ldr r0, [r0] - cmp r0, 0 - beq _081534C0 - movs r0, 0x1 - bl fullscreen_save_activate -_081534C0: - adds r0, r6, 0 - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8153474 - - thumb_func_start sub_81534D0 -sub_81534D0: @ 81534D0 - push {r4-r6,lr} - lsls r0, 24 - lsrs r4, r0, 24 - ldr r0, =gFlashMemoryPresent - ldr r0, [r0] - cmp r0, 0x1 - beq _081534F0 - ldr r1, =gUnknown_03006210 - movs r0, 0x4 - strh r0, [r1] - movs r0, 0xFF - b _08153550 - .pool -_081534F0: - bl sub_8153190 - cmp r4, 0 - beq _081534FC - cmp r4, 0x3 - beq _08153528 -_081534FC: - ldr r0, =0x0000ffff - ldr r1, =gUnknown_03006220 - bl sub_8152DD0 - lsls r0, 24 - lsrs r4, r0, 24 - bl save_deserialize_game - ldr r0, =gUnknown_03006210 - strh r4, [r0] - ldr r1, =gUnknown_03006214 - movs r0, 0 - str r0, [r1] - b _0815354E - .pool -_08153528: - ldr r5, =0x0201c000 - movs r6, 0xF8 - lsls r6, 4 - movs r0, 0x1C - adds r1, r5, 0 - adds r2, r6, 0 - bl sub_81530DC - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0x1 - bne _0815354E - adds r1, r5, r6 - movs r0, 0x1D - adds r2, r6, 0 - bl sub_81530DC - lsls r0, 24 - lsrs r4, r0, 24 -_0815354E: - adds r0, r4, 0 -_08153550: - pop {r4-r6} - pop {r1} - bx r1 - .pool - thumb_func_end sub_81534D0 + thumb_func_start sub_815355C sub_815355C: @ 815355C push {r4-r7,lr} - ldr r5, =gUnknown_03006204 + ldr r5, =gFastSaveSection ldr r0, =gUnknown_0203ABBC str r0, [r5] adds r7, r0, 0 @@ -1764,10 +30,10 @@ _0815357C: adds r0, r1 b _081535CA _0815358C: - bl sub_8153190 - ldr r0, =gUnknown_03006220 - bl sub_8152EC8 - ldr r0, =gUnknown_03006200 + bl UpdateSaveAddresses + ldr r0, =gRamSaveSectionLocations + bl GetSaveValidStatus + ldr r0, =gSaveCounter ldr r1, [r0] ands r1, r4 lsls r0, r1, 3 @@ -1780,7 +46,7 @@ _081535A6: lsls r0, 24 lsrs r0, 24 ldr r1, [r5] - bl sub_815314C + bl DoReadFlashWholeSection ldr r0, [r5] ldr r1, =0x00000ff4 adds r0, r1 diff --git a/asm/save_failed_screen.s b/asm/save_failed_screen.s index 963db4988..bbc8ad67a 100644 --- a/asm/save_failed_screen.s +++ b/asm/save_failed_screen.s @@ -44,8 +44,8 @@ sub_8178F44: @ 8178F44 .pool thumb_func_end sub_8178F44 - thumb_func_start fullscreen_save_activate -fullscreen_save_activate: @ 8178F90 + thumb_func_start DoSaveFailedScreen +DoSaveFailedScreen: @ 8178F90 push {r4,lr} adds r4, r0, 0 lsls r4, 24 @@ -66,7 +66,7 @@ fullscreen_save_activate: @ 8178F90 pop {r0} bx r0 .pool - thumb_func_end fullscreen_save_activate + thumb_func_end DoSaveFailedScreen thumb_func_start sub_8178FC8 sub_8178FC8: @ 8178FC8 @@ -325,7 +325,7 @@ sub_8179288: @ 8179288 ldr r0, =gUnknown_0203BCFE movs r1, 0x1 strh r1, [r0] - ldr r1, =gUnknown_030061FC + ldr r1, =gDamagedSaveSectors ldr r0, [r1] cmp r0, 0 beq _081792EC @@ -346,7 +346,7 @@ _0817929C: bl sub_8178F44 ldr r0, =gUnknown_0203BCFC ldrb r0, [r0] - bl calls_flash_erase_block_3 + bl HandleSavingData ldr r0, [r6] cmp r0, 0 beq _081792DC @@ -381,7 +381,7 @@ _0817931C: ldrb r0, [r0] movs r1, 0x11 bl FillWindowPixelBuffer - ldr r0, =gUnknown_03006214 + ldr r0, =gGameContinueCallback ldr r0, [r0] cmp r0, 0 bne _08179370 @@ -488,7 +488,7 @@ sub_8179428: @ 8179428 lsrs r4, r0, 24 cmp r4, 0 bne _0817944E - ldr r5, =gUnknown_03006214 + ldr r5, =gGameContinueCallback ldr r0, [r5] cmp r0, 0 bne _08179448 diff --git a/asm/scrcmd.s b/asm/scrcmd.s index e549ab035..d22965e54 100644 --- a/asm/scrcmd.s +++ b/asm/scrcmd.s @@ -1297,7 +1297,7 @@ sC3_unknown: @ 8099C58 adds r1, 0x1 str r1, [r0, 0x8] adds r0, r2, 0 - bl sav12_xor_increment + bl IncrementGameStat movs r0, 0 pop {r1} bx r1 diff --git a/asm/secret_base.s b/asm/secret_base.s index 731277d52..f0bdf9acd 100644 --- a/asm/secret_base.s +++ b/asm/secret_base.s @@ -1927,7 +1927,7 @@ sub_80E9A90: @ 80E9A90 sub_80E9AC0: @ 80E9AC0 push {lr} movs r0, 0x14 - bl sav12_xor_increment + bl IncrementGameStat bl sub_80E9A90 pop {r0} bx r0 @@ -2018,7 +2018,7 @@ sub_80E9B70: @ 80E9B70 push {r4-r6,lr} bl sub_80E9AD0 movs r0, 0x14 - bl sav12_xor_increment + bl IncrementGameStat ldr r5, =gSaveBlock1Ptr ldr r0, [r5] ldr r4, =0x00001aaa diff --git a/asm/shop.s b/asm/shop.s index deda193ba..0655f1851 100644 --- a/asm/shop.s +++ b/asm/shop.s @@ -2453,7 +2453,7 @@ BuyMenuSubtractMoney: @ 80E0F88 lsls r0, 24 lsrs r6, r0, 24 movs r0, 0x26 - bl sav12_xor_increment + bl IncrementGameStat ldr r5, =gSaveBlock1Ptr ldr r0, [r5] movs r4, 0x92 diff --git a/asm/slot_machine.s b/asm/slot_machine.s index 5e263d0fa..688cefb36 100644 --- a/asm/slot_machine.s +++ b/asm/slot_machine.s @@ -1387,7 +1387,7 @@ sub_812B01C: @ 812B01C cmp r0, 0 beq _0812B044 movs r0, 0x1C - bl sav12_xor_increment + bl IncrementGameStat _0812B044: ldr r2, [r4] ldrh r1, [r2, 0x8] diff --git a/asm/start_menu.s b/asm/start_menu.s index 27b6c07ca..79509d957 100644 --- a/asm/start_menu.s +++ b/asm/start_menu.s @@ -864,7 +864,7 @@ StartMenu_Pokedex: @ 809FBB4 .pool _0809FBCC: movs r0, 0x29 - bl sav12_xor_increment + bl IncrementGameStat bl play_some_sound bl RemoveExtraStartMenuWindows bl overworld_free_bg_tilemaps @@ -1656,14 +1656,14 @@ sub_80A0234: @ 80A0234 sub_80A024C: @ 80A024C push {r4,lr} movs r0, 0 - bl sav12_xor_increment + bl IncrementGameStat bl sub_81A9E90 ldr r4, =gUnknown_020322D4 ldrb r0, [r4] cmp r0, 0x1 bne _080A0274 movs r0, 0x4 - bl save_game_when_memory_present + bl TrySavingData lsls r0, 24 lsrs r1, r0, 24 movs r0, 0 @@ -1672,7 +1672,7 @@ sub_80A024C: @ 80A024C .pool _080A0274: movs r0, 0 - bl save_game_when_memory_present + bl TrySavingData lsls r0, 24 lsrs r1, r0, 24 _080A027E: diff --git a/asm/trade.s b/asm/trade.s index 3dd436713..0feabafcb 100644 --- a/asm/trade.s +++ b/asm/trade.s @@ -13829,7 +13829,7 @@ _0807EDC0: cmp r0, 0 bne _0807EDCE movs r0, 0x15 - bl sav12_xor_increment + bl IncrementGameStat _0807EDCE: ldr r0, =gLinkVSyncDisabled ldrb r0, [r0] @@ -14676,7 +14676,7 @@ _0807F50A: movs r2, 0 bl sub_807F1A8 movs r0, 0x15 - bl sav12_xor_increment + bl IncrementGameStat bl sub_8153380 ldr r0, =gUnknown_020322A0 ldr r0, [r0] diff --git a/asm/trainer_card.s b/asm/trainer_card.s index 253da5e78..e8ee8b154 100644 --- a/asm/trainer_card.s +++ b/asm/trainer_card.s @@ -803,7 +803,7 @@ sav12_xor_get_clamped_above: @ 80C2DE4 adds r4, r1, 0 lsls r0, 24 lsrs r0, 24 - bl sub_80847F8 + bl GetGameStat cmp r0, r4 bls _080C2DF6 adds r0, r4, 0 @@ -852,7 +852,7 @@ _080C2E38: sub_80C2E40: @ 80C2E40 push {r4,lr} movs r0, 0xA - bl sub_80847F8 + bl GetGameStat negs r1, r0 orrs r1, r0 lsrs r4, r1, 31 @@ -943,10 +943,10 @@ sub_80C2EC4: @ 80C2EC4 ldrb r0, [r1, 0x10] strh r0, [r5, 0x12] movs r0, 0x1 - bl sub_80847F8 + bl GetGameStat adds r4, r0, 0 movs r0, 0xA - bl sub_80847F8 + bl GetGameStat cmp r0, 0 bne _080C2EF6 movs r4, 0 diff --git a/asm/tv.s b/asm/tv.s index e4fc54486..1b74eef92 100644 --- a/asm/tv.s +++ b/asm/tv.s @@ -748,7 +748,7 @@ GabbyAndTyAfterInterview: @ 80EC448 adds r0, r2 strb r1, [r0] movs r0, 0x6 - bl sav12_xor_increment + bl IncrementGameStat pop {r4-r6} pop {r0} bx r0 @@ -1208,7 +1208,7 @@ sub_80EC8A4: @ 80EC8A4 movs r1, 0x18 bl sub_80EF910 movs r0, 0x5 - bl sub_80847F8 + bl GetGameStat strh r0, [r4, 0x6] movs r0, 0x19 strb r0, [r4] @@ -3238,7 +3238,7 @@ sub_80EDA80: @ 80EDA80 ldrh r0, [r5, 0x2] strh r0, [r4, 0x2] movs r0, 0x5 - bl sub_80847F8 + bl GetGameStat ldrh r1, [r5, 0x6] subs r0, r1 strh r0, [r4, 0x6] diff --git a/asm/weather.s b/asm/weather.s index 802a5bc96..7bb483e4a 100644 --- a/asm/weather.s +++ b/asm/weather.s @@ -7810,7 +7810,7 @@ sub_80AEFDC: @ 80AEFDC bne _080AEFF6 _080AEFF0: movs r0, 0x28 - bl sav12_xor_increment + bl IncrementGameStat _080AEFF6: pop {r0} bx r0 diff --git a/asm/wild_encounter.s b/asm/wild_encounter.s index ad10a869e..ee654745c 100644 --- a/asm/wild_encounter.s +++ b/asm/wild_encounter.s @@ -1882,7 +1882,7 @@ _080B5764: lsrs r4, r0, 16 _080B5784: movs r0, 0xC - bl sav12_xor_increment + bl IncrementGameStat adds r0, r4, 0 bl sub_80EDA3C bl sub_80B0698 diff --git a/data/save.s b/data/save.s index 760773e88..29b750b3a 100644 --- a/data/save.s +++ b/data/save.s @@ -3,5 +3,5 @@ .section .rodata -gUnknown_085CDC00:: @ 85CDC00 +gSaveSectionOffsets:: @ 85CDC00 .incbin "baserom.gba", 0x5cdc00, 0x38 diff --git a/include/save.h b/include/save.h new file mode 100644 index 000000000..4b13acf61 --- /dev/null +++ b/include/save.h @@ -0,0 +1,92 @@ +#ifndef GUARD_SAVE_H +#define GUARD_SAVE_H + +struct SaveSectionLocation +{ + void *data; + u16 size; +}; + +struct SaveSection +{ + u8 data[0xFF4]; + u16 id; + u16 checksum; + u32 security; + u32 counter; +}; // size is 0x1000 + +// headless save section? +struct UnkSaveSection +{ + u8 data[0xFF4]; + u32 security; +}; // size is 0xFF8 + +struct SaveSectionOffsets +{ + u16 toAdd; + u16 size; +}; + +// Emerald changes this definition to be the sectors per slot. +#define NUM_SECTORS_PER_SLOT 16 // move to save.h + +#define UNKNOWN_CHECK_VALUE 0x8012025 + +// SetDamagedSectorBits states +enum +{ + ENABLE, + DISABLE, + CHECK // unused +}; + +// Do save types +enum +{ + NORMAL_SAVE, + LINK_SAVE, + //EREADER_SAVE, // depreciated in Emerald + LINK2_SAVE, // unknown 2nd link save + HOF_SAVE, + DIFFERENT_FILE_SAVE, + HOF_DELETE_SAVE // unused +}; + +void ClearSaveData(void); +void ResetSaveCounters(void); +//bool32 ManipulateSectorBits(u8 op, u8 bit); +//u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *a2); +u8 HandleWriteSector(u16, const struct SaveSectionLocation *); +//u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size); +u8 TryWriteSector(u8, u8 *); +//u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location); +//u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location); +//u8 sub_812550C(u16 a1, const struct SaveSectionLocation *location); +u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location); +u8 ClearSaveData_2(u16, const struct SaveSectionLocation *location); +//u8 sub_8125758(u16 a1, const struct SaveSectionLocation *location); +//u8 sub_81257F0(u16 a1, const struct SaveSectionLocation *location); +//u8 sub_812587C(u16 a1, const struct SaveSectionLocation *location); +u8 sub_8152E10(u16, const struct SaveSectionLocation *location); +u8 GetSaveValidStatus(const struct SaveSectionLocation *location); +//u8 sub_8125B88(u8 a1, u8 *data, u16 size); +u8 DoReadFlashWholeSection(u8, struct SaveSection *); +u16 CalculateChecksum(void *, u16); +//u8 HandleSavingData(u8 saveType); +//u8 TrySavingData(u8 saveType); +//u8 sub_8125D80(void); +//bool8 sub_8125DA8(void); +//u8 sub_8125DDC(void); +//u8 sub_8125E04(void); +//u8 sub_8125E2C(void); +//bool8 sub_8125E6C(void); +//u8 sub_8125EC8(u8 a1); +//bool8 unref_sub_8125F4C(struct UnkSaveSection *a1); +//u8 unref_sub_8125FA0(void); +//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); + +#endif diff --git a/ld_script.txt b/ld_script.txt index d71092981..10fe3192a 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -180,6 +180,7 @@ SECTIONS { asm/rom_8151534.o(.text); asm/roulette_util.o(.text); asm/cable_car_util.o(.text); + src/save.o(.text); asm/save.o(.text); asm/mystery_event_script.o(.text); asm/field_effect_helpers.o(.text); diff --git a/src/save.c b/src/save.c new file mode 100644 index 000000000..c6560ee0f --- /dev/null +++ b/src/save.c @@ -0,0 +1,830 @@ +#include "global.h" +#include "gba/flash_internal.h" +#include "save.h" +#include "game_stat.h" + +extern u32 gSaveCounter; +extern u16 gLastWrittenSector; +extern u32 gDamagedSaveSectors; +extern u16 gLastKnownGoodSector; +extern u32 gLastSaveCounter; +extern u16 gUnknown_03006208; +extern struct SaveSection *gFastSaveSection; +extern struct SaveSection *gUnknown_0203ABBC; + +bool32 ProgramFlashSectorAndVerify(u8 sector, u8 *data); +void ReadFlash(u8 secotr, u32 arg1, u8* data, u32 size); + +void ClearSaveData(void) +{ + u16 i; + + for (i = 0; i < NUM_SECTORS_PER_SLOT; i++) + { + EraseFlashSector(i); + EraseFlashSector(i + NUM_SECTORS_PER_SLOT); // clear slot 2. + } +} + +void ResetSaveCounters(void) +{ + gSaveCounter = 0; + gLastWrittenSector = 0; + gDamagedSaveSectors = 0; +} + +bool32 SetDamagedSectorBits(u8 op, u8 bit) +{ + bool32 retVal = FALSE; + + switch (op) + { + case ENABLE: + gDamagedSaveSectors |= (1 << bit); + break; + case DISABLE: + gDamagedSaveSectors &= ~(1 << bit); + break; + case CHECK: // unused + if (gDamagedSaveSectors & (1 << bit)) + retVal = TRUE; + break; + } + + return retVal; +} + +u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) +{ + u32 retVal; + u16 i; + + gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; + + if (a1 != 0xFFFF) // for link + { + retVal = HandleWriteSector(a1, location); + } + else + { + gLastKnownGoodSector = gLastWrittenSector; // backup the current written sector before attempting to write. + gLastSaveCounter = gSaveCounter; + gLastWrittenSector++; + gLastWrittenSector = gLastWrittenSector % 0xE; // array count save sector locations + gSaveCounter++; + retVal = 1; + + for (i = 0; i < 0xE; i++) + HandleWriteSector(i, location); + + if (gDamagedSaveSectors != 0) // skip the damaged sector. + { + retVal = 0xFF; + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + } + } + + return retVal; +} + +u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) +{ + u16 i; + u16 sector; + u8 *data; + u16 size; + + sector = a1 + gLastWrittenSector; + sector %= 0xE; + sector += 0xE * (gSaveCounter % 2); + + data = location[a1].data; + size = location[a1].size; + + // clear save section. + for (i = 0; i < sizeof(struct SaveSection); i++) + ((char *)gFastSaveSection)[i] = 0; + + gFastSaveSection->id = a1; + gFastSaveSection->security = UNKNOWN_CHECK_VALUE; + gFastSaveSection->counter = gSaveCounter; + + for (i = 0; i < size; i++) + gFastSaveSection->data[i] = data[i]; + + gFastSaveSection->checksum = CalculateChecksum(data, size); + return TryWriteSector(sector, gFastSaveSection->data); +} + +u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) +{ + u16 i; + struct SaveSection *section = (struct SaveSection *)&gUnknown_0203ABBC; + + for (i = 0; i < sizeof(struct SaveSection); i++) + ((char *)section)[i] = 0; + + section->security = UNKNOWN_CHECK_VALUE; + + for (i = 0; i < size; i++) + section->data[i] = data[i]; + + section->id = CalculateChecksum(data, size); // though this appears to be incorrect, it might be some sector checksum instead of a whole save checksum and only appears to be relevent to HOF data, if used. + return TryWriteSector(sector, section->data); +} + +u8 TryWriteSector(u8 sector, u8 *data) +{ + if (ProgramFlashSectorAndVerify(sector, data) != 0) // is damaged? + { + SetDamagedSectorBits(ENABLE, sector); // set damaged sector bits. + return 0xFF; + } + else + { + SetDamagedSectorBits(DISABLE, sector); // unset damaged sector bits. it's safe now. + return 1; + } +} + +u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused +{ + gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; + gLastKnownGoodSector = gLastWrittenSector; + gLastSaveCounter = gSaveCounter; + gLastWrittenSector++; + gLastWrittenSector = gLastWrittenSector % 0xE; + gSaveCounter++; + gUnknown_03006208 = 0; + gDamagedSaveSectors = 0; + return 0; +} + +u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ever called once, and gSaveBlock2 is passed to this function. location is unused +{ + gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; + gLastKnownGoodSector = gLastWrittenSector; + gLastSaveCounter = gSaveCounter; + gUnknown_03006208 = 0; + gDamagedSaveSectors = 0; + return 0; +} + +u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location) +{ + u8 retVal; + + if (gUnknown_03006208 < a1 - 1) + { + retVal = 1; + HandleWriteSector(gUnknown_03006208, location); + gUnknown_03006208++; + if (gDamagedSaveSectors) + { + retVal = 0xFF; + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + } + } + else + { + retVal = 0xFF; + } + + return retVal; +} + +u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location) +{ + u8 retVal = 1; + + ClearSaveData_2(a1 - 1, location); + + if (gDamagedSaveSectors) + { + retVal = 0xFF; + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + } + return retVal; +} + +u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location) +{ + u16 i; + u16 sector; + u8 *data; + u16 size; + u8 status; + + sector = a1 + gLastWrittenSector; + sector %= 0xE; + sector += 0xE * (gSaveCounter % 2); + + data = location[a1].data; + size = location[a1].size; + + // clear temp save section. + for (i = 0; i < sizeof(struct SaveSection); i++) + ((char *)gFastSaveSection)[i] = 0; + + gFastSaveSection->id = a1; + gFastSaveSection->security = UNKNOWN_CHECK_VALUE; + gFastSaveSection->counter = gSaveCounter; + + // set temp section's data. + for (i = 0; i < size; i++) + gFastSaveSection->data[i] = data[i]; + + // calculate checksum. + gFastSaveSection->checksum = CalculateChecksum(data, size); + + EraseFlashSector(sector); + + status = 1; + + for (i = 0; i < sizeof(struct UnkSaveSection); i++) + { + if (ProgramFlashByte(sector, i, ((u8 *)gFastSaveSection)[i])) + { + status = 0xFF; + break; + } + } + + if (status == 0xFF) + { + SetDamagedSectorBits(ENABLE, sector); + return 0xFF; + } + else + { + status = 1; + + for (i = 0; i < 7; i++) + { + if (ProgramFlashByte(sector, 0xFF9 + i, ((u8 *)gFastSaveSection)[0xFF9 + i])) + { + status = 0xFF; + break; + } + } + + if (status == 0xFF) + { + SetDamagedSectorBits(ENABLE, sector); + return 0xFF; + } + else + { + SetDamagedSectorBits(DISABLE, sector); + return 1; + } + } +} + +u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location) +{ + u16 sector; + + sector = a1 + gLastWrittenSector; // no sub 1? + sector %= 0xE; + sector += 0xE * (gSaveCounter % 2); + + if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) + { + // sector is damaged, so enable the bit in gDamagedSaveSectors and restore the last written sector and save counter. + SetDamagedSectorBits(ENABLE, sector); + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + return 0xFF; + } + else + { + SetDamagedSectorBits(DISABLE, sector); + return 1; + } +} + +u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location) +{ + u16 sector; + + sector = a1 + gLastWrittenSector - 1; + sector %= 0xE; + sector += 0xE * (gSaveCounter % 2); + + if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), ((u8 *)gFastSaveSection)[sizeof(struct UnkSaveSection)])) + { + // sector is damaged, so enable the bit in gDamagedSaveSectors and restore the last written sector and save counter. + SetDamagedSectorBits(ENABLE, sector); + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + return 0xFF; + } + else + { + SetDamagedSectorBits(DISABLE, sector); + return 1; + } +} + +u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location) +{ + u16 sector; + + sector = a1 + gLastWrittenSector - 1; // no sub 1? + sector %= 0xE; + sector += 0xE * (gSaveCounter % 2); + + if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) + { + // sector is damaged, so enable the bit in gDamagedSaveSectors and restore the last written sector and save counter. + SetDamagedSectorBits(ENABLE, sector); + gLastWrittenSector = gLastKnownGoodSector; + gSaveCounter = gLastSaveCounter; + return 0xFF; + } + else + { + SetDamagedSectorBits(DISABLE, sector); + return 1; + } +} + +u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location) +{ + u8 retVal; + gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; + if (a1 != 0xFFFF) + { + retVal = 0xFF; + } + else + { + retVal = GetSaveValidStatus(location); + sub_8152E10(0xFFFF, location); + } + + return retVal; +} + +u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location) +{ + u16 i; + u16 checksum; + u16 v3 = 0xE * (gSaveCounter % 2); + u16 id; + + for (i = 0; i < 0xE; i++) + { + DoReadFlashWholeSection(i + v3, gFastSaveSection); + id = gFastSaveSection->id; + if (id == 0) + gLastWrittenSector = i; + checksum = CalculateChecksum(gFastSaveSection->data, location[id].size); + if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE + && gFastSaveSection->checksum == checksum) + { + u16 j; + for (j = 0; j < location[id].size; j++) + ((u8 *)location[id].data)[j] = gFastSaveSection->data[j]; + } + } + + return 1; +} + +u8 GetSaveValidStatus(const struct SaveSectionLocation *location) +{ + u16 i; + u16 checksum; + u32 saveSlot1Counter = 0; + u32 saveSlot2Counter = 0; + u32 slotCheckField = 0; + bool8 securityPassed = FALSE; + u8 saveSlot1Status; + u8 saveSlot2Status; + + // check save slot 1. + for (i = 0; i < 0xE; i++) + { + DoReadFlashWholeSection(i, gFastSaveSection); + if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) + { + securityPassed = TRUE; + checksum = CalculateChecksum(gFastSaveSection->data, location[gFastSaveSection->id].size); + if (gFastSaveSection->checksum == checksum) + { + saveSlot1Counter = gFastSaveSection->counter; + slotCheckField |= 1 << gFastSaveSection->id; + } + } + } + + if (securityPassed) + { + if (slotCheckField == 0x3FFF) + saveSlot1Status = 1; + else + saveSlot1Status = 255; + } + else + { + saveSlot1Status = 0; + } + + slotCheckField = 0; + securityPassed = FALSE; + + // check save slot 2. + for (i = 0; i < 0xE; i++) + { + DoReadFlashWholeSection(i + 0xE, gFastSaveSection); + if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) + { + securityPassed = TRUE; + checksum = CalculateChecksum(gFastSaveSection->data, location[gFastSaveSection->id].size); + if (gFastSaveSection->checksum == checksum) + { + saveSlot2Counter = gFastSaveSection->counter; + slotCheckField |= 1 << gFastSaveSection->id; + } + } + } + + if (securityPassed) + { + if (slotCheckField == 0x3FFF) + saveSlot2Status = 1; + else + saveSlot2Status = 255; + } + else + { + saveSlot2Status = 0; + } + + if (saveSlot1Status == 1 && saveSlot2Status == 1) + { + if ((saveSlot1Counter == -1 && saveSlot2Counter == 0) || (saveSlot1Counter == 0 && saveSlot2Counter == -1)) + { + if ((unsigned)(saveSlot1Counter + 1) < (unsigned)(saveSlot2Counter + 1)) + { + gSaveCounter = saveSlot2Counter; + } + else + { + gSaveCounter = saveSlot1Counter; + } + } + else + { + if (saveSlot1Counter < saveSlot2Counter) + { + gSaveCounter = saveSlot2Counter; + } + else + { + gSaveCounter = saveSlot1Counter; + } + } + return 1; + } + + if (saveSlot1Status == 1) + { + gSaveCounter = saveSlot1Counter; + if (saveSlot2Status == 255) + return 255; + return 1; + } + + if (saveSlot2Status == 1) + { + gSaveCounter = saveSlot2Counter; + if (saveSlot1Status == 255) + return 255; + return 1; + } + + if (saveSlot1Status == 0 && saveSlot2Status == 0) + { + gSaveCounter = 0; + gLastWrittenSector = 0; + return 0; + } + + gSaveCounter = 0; + gLastWrittenSector = 0; + return 2; +} + +u8 sub_81530DC(u8 a1, u8 *data, u16 size) +{ + u16 i; + struct SaveSection *section = (struct SaveSection *)&gUnknown_0203ABBC; + DoReadFlashWholeSection(a1, section); + if (section->security == UNKNOWN_CHECK_VALUE) + { + u16 checksum = CalculateChecksum(section->data, size); + if (section->id == checksum) + { + for (i = 0; i < size; i++) + data[i] = section->data[i]; + return 1; + } + else + { + return 2; + } + } + else + { + return 0; + } +} + +u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section) +{ + ReadFlash(sector, 0, section->data, sizeof(struct SaveSection)); + return 1; +} + +u16 CalculateChecksum(void *data, u16 size) +{ + u16 i; + u32 checksum = 0; + + for (i = 0; i < (size / 4); i++) + checksum += *((u32 *)data)++; + + return ((checksum >> 16) + checksum); +} + +extern struct SaveSectionOffsets gSaveSectionOffsets[0xE]; // gSaveSectionOffsets +extern struct SaveSectionLocation gRamSaveSectionLocations[0xE]; // gRamSaveSectionLocations +extern void *gUnknown_03005D94; + +#ifdef NONMATCHING +// the initial allocation of the pointer and toAdd variable doesnt match up with the original function. however, forcing it is impossible since gRamSaveSectionLocations is loaded first. +void UpdateSaveAddresses(void) +{ + int i; + gRamSaveSectionLocations[0].data = gSaveBlock2Ptr + gSaveSectionOffsets[0].toAdd; + gRamSaveSectionLocations[0].size = gSaveSectionOffsets[0].size; + + for(i = 1; i < 5; i++) + { + gRamSaveSectionLocations[i].data = gSaveBlock1Ptr + gSaveSectionOffsets[i].toAdd; + gRamSaveSectionLocations[i].size = gSaveSectionOffsets[i].size; + } + + for(i = 5; i < 14; i++) + { + gRamSaveSectionLocations[i].data = gUnknown_03005D94 + gSaveSectionOffsets[i].toAdd; + gRamSaveSectionLocations[i].size = gSaveSectionOffsets[i].size; + } +} +#else +__attribute__((naked)) +void UpdateSaveAddresses(void) +{ + asm(".syntax unified\n\ + push {r4,r5,lr}\n\ + ldr r3, =gRamSaveSectionLocations\n\ + ldr r0, =gSaveBlock2Ptr\n\ + ldr r2, =gSaveSectionOffsets\n\ + ldrh r1, [r2]\n\ + ldr r0, [r0]\n\ + adds r0, r1\n\ + str r0, [r3]\n\ + ldrh r0, [r2, 0x2]\n\ + strh r0, [r3, 0x4]\n\ + ldr r5, =gSaveBlock1Ptr\n\ + adds r3, 0x8\n\ + adds r2, 0x4\n\ + movs r4, 0x3\n\ +_081531AC:\n\ + ldrh r0, [r2]\n\ + ldr r1, [r5]\n\ + adds r1, r0\n\ + str r1, [r3]\n\ + ldrh r0, [r2, 0x2]\n\ + strh r0, [r3, 0x4]\n\ + adds r3, 0x8\n\ + adds r2, 0x4\n\ + subs r4, 0x1\n\ + cmp r4, 0\n\ + bge _081531AC\n\ + movs r4, 0x5\n\ + ldr r1, =gRamSaveSectionLocations\n\ + ldr r5, =gUnknown_03005D94\n\ + ldr r0, =gSaveSectionOffsets\n\ + adds r3, r1, 0\n\ + adds r3, 0x28\n\ + adds r2, r0, 0\n\ + adds r2, 0x14\n\ +_081531D2:\n\ + ldrh r0, [r2]\n\ + ldr r1, [r5]\n\ + adds r1, r0\n\ + str r1, [r3]\n\ + ldrh r0, [r2, 0x2]\n\ + strh r0, [r3, 0x4]\n\ + adds r3, 0x8\n\ + adds r2, 0x4\n\ + adds r4, 0x1\n\ + cmp r4, 0xD\n\ + ble _081531D2\n\ + pop {r4,r5}\n\ + pop {r0}\n\ + bx r0\n\ + .pool\n\ + .syntax divided"); +} +#endif + +extern u32 GetGameStat(u8 index); // rom4 +extern void IncrementGameStat(u8 index); // rom4 +extern void SaveSerializedGame(void); // load_save +extern u32 gUnknown_0203CF5C; + +u8 HandleSavingData(u8 saveType) +{ + u8 i; + u32 backupVar = gUnknown_0203CF5C; + u8 *tempAddr; + + gUnknown_0203CF5C = 0; + UpdateSaveAddresses(); + switch (saveType) + { + case HOF_DELETE_SAVE: // deletes HOF before overwriting HOF completely. unused + for (i = 0xE * 2 + 0; i < 32; i++) + EraseFlashSector(i); + case HOF_SAVE: // hall of fame. + if (GetGameStat(GAME_STAT_ENTERED_HOF) < 999) + IncrementGameStat(GAME_STAT_ENTERED_HOF); + SaveSerializedGame(); + save_write_to_flash(0xFFFF, gRamSaveSectionLocations); + tempAddr = (u8 *)0x201C000; // FIXME: make this a label. + HandleWriteSectorNBytes(0x1C, tempAddr, 0xF80); + HandleWriteSectorNBytes(0x1D, tempAddr + 0xF80, 0xF80); + break; + case NORMAL_SAVE: // normal save. also called by overwriting your own save. + default: + SaveSerializedGame(); + save_write_to_flash(0xFFFF, gRamSaveSectionLocations); + break; + case LINK_SAVE: // _081532C4 + case LINK2_SAVE: + SaveSerializedGame(); + for(i = 0; i < 5; i++) + ClearSaveData_2(i, gRamSaveSectionLocations); + for(i = 0; i < 5; i++) + sav12_xor_get(i, gRamSaveSectionLocations); + break; + // support for Ereader was removed in Emerald. + /* + case EREADER_SAVE: // used in mossdeep "game corner" before/after battling old man e-reader trainer + SaveSerializedGame(); + save_write_to_flash(0, gRamSaveSectionLocations); + break; + */ + case DIFFERENT_FILE_SAVE: + for (i = (0xE * 2 + 0); i < 32; i++) + EraseFlashSector(i); // erase HOF. + SaveSerializedGame(); + save_write_to_flash(0xFFFF, gRamSaveSectionLocations); + break; + } + gUnknown_0203CF5C = backupVar; + return 0; +} + +extern u32 gFlashMemoryPresent; +extern void DoSaveFailedScreen(u8); // save_failed_screen +extern u16 gUnknown_03006294; + +u8 TrySavingData(u8 saveType) // TrySave +{ + if(gFlashMemoryPresent == TRUE) + { + HandleSavingData(saveType); + if(gDamagedSaveSectors) + DoSaveFailedScreen(saveType); + else + goto OK; // really? + } + gUnknown_03006294 = 0xFF; + return 0xFF; + +OK: + gUnknown_03006294 = 1; + return 1; +} + +u8 sub_8153380(void) // trade.s save +{ + if (gFlashMemoryPresent != TRUE) + return 1; + UpdateSaveAddresses(); + SaveSerializedGame(); + RestoreSaveBackupVarsAndIncrement(gRamSaveSectionLocations); + return 0; +} + +bool8 sub_81533AC(void) // trade.s save +{ + u8 retVal = sub_81529D4(0xE, gRamSaveSectionLocations); + if (gDamagedSaveSectors) + DoSaveFailedScreen(0); + if (retVal == 0xFF) + return 1; + else + return 0; +} + +u8 sub_81533E0(void) // trade.s save +{ + sub_8152A34(0xE, gRamSaveSectionLocations); + if (gDamagedSaveSectors) + DoSaveFailedScreen(0); + return 0; +} + +u8 sub_8153408(void) // trade.s save +{ + sub_8152CAC(0xE, gRamSaveSectionLocations); + if (gDamagedSaveSectors) + DoSaveFailedScreen(0); + return 0; +} + +u8 sub_8153430(void) +{ + if (gFlashMemoryPresent != TRUE) + return 1; + + UpdateSaveAddresses(); + SaveSerializedGame(); + RestoreSaveBackupVars(gRamSaveSectionLocations); + sub_8152A34(gUnknown_03006208 + 1, gRamSaveSectionLocations); + return 0; +} + +bool8 sub_8153474(void) +{ + u8 retVal = FALSE; + u16 val = ++gUnknown_03006208; + if (val <= 4) + { + sub_8152A34(gUnknown_03006208 + 1, gRamSaveSectionLocations); + sub_8152D44(val, gRamSaveSectionLocations); + } + else + { + sub_8152D44(val, gRamSaveSectionLocations); + retVal = TRUE; + } + if (gDamagedSaveSectors) + DoSaveFailedScreen(1); + return retVal; +} + +extern u16 gUnknown_03006210; +extern void LoadSerializedGame(void); // load_save +extern void (*gGameContinueCallback)(void); +extern u8 gDecompressionBuffer[]; + +u8 sub_81534D0(u8 a1) +{ + u8 result; + + if (gFlashMemoryPresent != TRUE) + { + gUnknown_03006210 = 4; + return 0xFF; + } + + UpdateSaveAddresses(); + switch (a1) + { + case 0: + default: + result = sub_8152DD0(0xFFFF, gRamSaveSectionLocations); + LoadSerializedGame(); + gUnknown_03006210 = result; + gGameContinueCallback = 0; + break; + case 3: + result = sub_81530DC(0x1C, gDecompressionBuffer, 0xF80); + if(result == 1) + result = sub_81530DC(0x1D, gDecompressionBuffer + 0xF80, 0xF80); + break; + } + + return result; +} diff --git a/sym_common.txt b/sym_common.txt index c38c6b1d8..2b6145b75 100644 --- a/sym_common.txt +++ b/sym_common.txt @@ -392,22 +392,22 @@ gUnknown_030061E8: @ 30061E8 gUnknown_030061EC: @ 30061EC .space 0x4 -gUnknown_030061F0: @ 30061F0 +gLastWrittenSector: @ 30061F0 .space 0x4 -gUnknown_030061F4: @ 30061F4 +gLastSaveCounter: @ 30061F4 .space 0x4 -gUnknown_030061F8: @ 30061F8 +gLastKnownGoodSector: @ 30061F8 .space 0x4 -gUnknown_030061FC: @ 30061FC +gDamagedSaveSectors: @ 30061FC .space 0x4 -gUnknown_03006200: @ 3006200 +gSaveCounter: @ 3006200 .space 0x4 -gUnknown_03006204: @ 3006204 +gFastSaveSection: @ 3006204 .space 0x4 gUnknown_03006208: @ 3006208 @@ -416,10 +416,10 @@ gUnknown_03006208: @ 3006208 gUnknown_03006210: @ 3006210 .space 0x4 -gUnknown_03006214: @ 3006214 +gGameContinueCallback: @ 3006214 .space 0xC -gUnknown_03006220: @ 3006220 +gRamSaveSectionLocations: @ 3006220 .space 0x74 gUnknown_03006294: @ 3006294