From d0dfc758fc6684e765736c7aad7190328d3e145c Mon Sep 17 00:00:00 2001 From: ProjectRevoTPP Date: Fri, 6 Oct 2017 18:33:35 -0400 Subject: [PATCH] decompile save_failed_screen.c --- asm/save_failed_screen.s | 707 ----------------------------------- include/gba/flash_internal.h | 1 + include/save.h | 2 +- ld_script.txt | 2 +- src/save_failed_screen.c | 332 ++++++++++++++++ 5 files changed, 335 insertions(+), 709 deletions(-) delete mode 100644 asm/save_failed_screen.s create mode 100755 src/save_failed_screen.c diff --git a/asm/save_failed_screen.s b/asm/save_failed_screen.s deleted file mode 100644 index 182ff8954..000000000 --- a/asm/save_failed_screen.s +++ /dev/null @@ -1,707 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start sub_8178F44 -sub_8178F44: @ 8178F44 - push {r4,r5,lr} - sub sp, 0x18 - adds r5, r0, 0 - adds r4, r1, 0 - adds r3, r2, 0 - lsls r3, 24 - add r0, sp, 0x14 - movs r2, 0 - strb r2, [r0] - adds r1, r0, 0 - movs r0, 0xF - strb r0, [r1, 0x1] - movs r0, 0x3 - strb r0, [r1, 0x2] - ldr r0, =gUnknown_0203BD0E - ldrb r0, [r0] - lsls r4, 27 - lsrs r4, 24 - lsrs r3, 21 - adds r3, 0x1 - lsls r3, 24 - lsrs r3, 24 - str r2, [sp] - str r2, [sp, 0x4] - str r1, [sp, 0x8] - str r2, [sp, 0xC] - str r5, [sp, 0x10] - movs r1, 0x1 - adds r2, r4, 0 - bl AddTextPrinterParametrized2 - add sp, 0x18 - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8178F44 - - thumb_func_start DoSaveFailedScreen -DoSaveFailedScreen: @ 8178F90 - push {r4,lr} - adds r4, r0, 0 - lsls r4, 24 - lsrs r4, 24 - ldr r0, =sub_8178FDC - bl SetMainCallback2 - ldr r0, =gUnknown_0203BCFC - strh r4, [r0] - ldr r1, =gUnknown_0203BCFE - movs r2, 0 - movs r0, 0 - strh r0, [r1] - strh r0, [r1, 0x2] - ldr r0, =gUnknown_0203BD0E - strb r2, [r0] - strb r2, [r0, 0x1] - pop {r4} - pop {r0} - bx r0 - .pool - thumb_func_end DoSaveFailedScreen - - thumb_func_start sub_8178FC8 -sub_8178FC8: @ 8178FC8 - push {lr} - bl LoadOam - bl ProcessSpriteCopyRequests - bl TransferPlttBuffer - pop {r0} - bx r0 - thumb_func_end sub_8178FC8 - - thumb_func_start sub_8178FDC -sub_8178FDC: @ 8178FDC - push {r4-r7,lr} - mov r7, r8 - push {r7} - sub sp, 0xC - ldr r0, =gMain - movs r1, 0x87 - lsls r1, 3 - adds r7, r0, r1 - ldrb r0, [r7] - cmp r0, 0 - beq _08178FF8 - cmp r0, 0x1 - bne _08178FF8 - b _0817925C -_08178FF8: - movs r0, 0 - bl SetVBlankCallback - movs r0, 0 - movs r1, 0 - bl SetGpuReg - movs r0, 0xE - movs r1, 0 - bl SetGpuReg - movs r0, 0xC - movs r1, 0 - bl SetGpuReg - movs r0, 0xA - movs r1, 0 - bl SetGpuReg - movs r0, 0x8 - movs r1, 0 - bl SetGpuReg - movs r0, 0x1C - movs r1, 0 - bl SetGpuReg - movs r0, 0x1E - movs r1, 0 - bl SetGpuReg - movs r0, 0x18 - movs r1, 0 - bl SetGpuReg - movs r0, 0x1A - movs r1, 0 - bl SetGpuReg - movs r0, 0x14 - movs r1, 0 - bl SetGpuReg - movs r0, 0x16 - movs r1, 0 - bl SetGpuReg - movs r0, 0x10 - movs r1, 0 - bl SetGpuReg - movs r0, 0x12 - movs r1, 0 - bl SetGpuReg - add r1, sp, 0x4 - movs r0, 0 - strh r0, [r1] - ldr r2, =0x040000d4 - str r1, [r2] - movs r1, 0xC0 - lsls r1, 19 - str r1, [r2, 0x4] - ldr r0, =0x8100c000 - str r0, [r2, 0x8] - ldr r0, [r2, 0x8] - movs r3, 0 - mov r8, r3 - str r3, [sp, 0x8] - add r4, sp, 0x8 - str r4, [r2] - movs r0, 0xE0 - lsls r0, 19 - str r0, [r2, 0x4] - ldr r0, =0x85000100 - str r0, [r2, 0x8] - ldr r0, [r2, 0x8] - add r0, sp, 0x4 - mov r3, r8 - strh r3, [r0] - str r0, [r2] - movs r0, 0xA0 - lsls r0, 19 - str r0, [r2, 0x4] - ldr r0, =0x81000200 - str r0, [r2, 0x8] - ldr r0, [r2, 0x8] - ldr r0, =gUnknown_085B0E04 - bl LZ77UnCompVram - ldr r0, =gUnknown_085B0A80 - ldr r1, =0x06007000 - bl LZ77UnCompVram - ldr r0, =gUnknown_085B0C0C - ldr r1, =0x06007800 - bl LZ77UnCompVram - ldr r0, =gUnknown_085EFDE4 - ldr r1, =0x06010020 - bl LZ77UnCompVram - movs r0, 0 - bl ResetBgsAndClearDma3BusyFlags - ldr r1, =gUnknown_085EFD88 - movs r0, 0 - movs r2, 0x3 - bl InitBgsFromTemplates - ldr r5, =0x0201e000 - movs r0, 0 - adds r1, r5, 0 - bl SetBgTilemapBuffer - mov r0, r8 - str r0, [sp, 0x8] - ldr r2, =0x05000200 - adds r0, r4, 0 - adds r1, r5, 0 - bl CpuSet - ldr r1, =gUnknown_0850E87C - movs r2, 0x90 - lsls r2, 1 - movs r6, 0x85 - lsls r6, 2 - movs r0, 0 - adds r3, r6, 0 - bl LoadBgTiles - ldr r0, =gUnknown_085EFD94 - bl InitWindows - ldr r0, =gUnknown_085EFD9C - bl AddWindowWithoutTileMap - ldr r4, =gUnknown_0203BD0E - strb r0, [r4] - ldrb r0, [r4] - movs r1, 0x80 - lsls r1, 4 - adds r2, r5, r1 - movs r1, 0x7 - bl SetWindowAttribute - ldr r0, =gUnknown_085EFDA4 - bl AddWindowWithoutTileMap - strb r0, [r4, 0x1] - ldrb r0, [r4, 0x1] - movs r3, 0xE8 - lsls r3, 5 - adds r5, r3 - movs r1, 0x7 - adds r2, r5, 0 - bl SetWindowAttribute - bl DeactivateAllTextPrinters - bl ResetSpriteData - bl ResetTasks - bl ResetPaletteFade - ldr r0, =gUnknown_085B0A00 - movs r1, 0 - movs r2, 0x40 - bl LoadPalette - ldr r0, =gUnknown_085EFDC4 - movs r1, 0x80 - lsls r1, 1 - movs r2, 0x20 - bl LoadPalette - ldr r0, =gUnknown_0850FEFC - movs r1, 0xE0 - movs r2, 0x20 - bl LoadPalette - ldr r0, =gUnknown_0860F074 - movs r1, 0xF0 - movs r2, 0x20 - bl LoadPalette - ldrb r0, [r4] - movs r1, 0 - adds r2, r6, 0 - movs r3, 0xE - bl SetWindowBorderStyle - ldrb r0, [r4, 0x1] - movs r1, 0 - adds r2, r6, 0 - movs r3, 0xE - bl SetWindowBorderStyle - ldrb r0, [r4, 0x1] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldrb r0, [r4] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldrb r0, [r4, 0x1] - movs r1, 0x2 - bl CopyWindowToVram - ldrb r0, [r4] - movs r1, 0x1 - bl CopyWindowToVram - ldr r0, =gText_SaveFailedCheckingBackup - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 - movs r0, 0x1 - negs r0, r0 - mov r1, r8 - str r1, [sp] - movs r1, 0 - movs r2, 0x10 - movs r3, 0 - bl BeginNormalPaletteFade - movs r0, 0x1 - bl EnableInterrupts - ldr r0, =sub_8178FC8 - bl SetVBlankCallback - movs r1, 0x82 - lsls r1, 5 - movs r0, 0 - bl SetGpuReg - movs r0, 0 - bl ShowBg - movs r0, 0x2 - bl ShowBg - movs r0, 0x3 - bl ShowBg - ldrb r0, [r7] - adds r0, 0x1 - strb r0, [r7] - b _08179272 - .pool -_0817925C: - bl UpdatePaletteFade - lsls r0, 24 - cmp r0, 0 - bne _08179272 - ldr r0, =sub_8179288 - bl SetMainCallback2 - ldr r0, =sub_8179454 - bl SetVBlankCallback -_08179272: - add sp, 0xC - pop {r3} - mov r8, r3 - pop {r4-r7} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8178FDC - - thumb_func_start sub_8179288 -sub_8179288: @ 8179288 - push {r4-r6,lr} - movs r5, 0 - ldr r0, =gUnknown_0203BCFE - movs r1, 0x1 - strh r1, [r0] - ldr r1, =gDamagedSaveSectors - ldr r0, [r1] - cmp r0, 0 - beq _081792EC - adds r6, r1, 0 -_0817929C: - ldr r0, [r6] - bl sub_81795AC - lsls r0, 24 - cmp r0, 0 - bne _08179348 - ldr r4, =gUnknown_0203BD0E - ldrb r0, [r4] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gText_CheckCompleted - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 - ldr r0, =gUnknown_0203BCFC - ldrb r0, [r0] - bl HandleSavingData - ldr r0, [r6] - cmp r0, 0 - beq _081792DC - ldrb r0, [r4] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gText_SaveFailedCheckingBackup - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 -_081792DC: - adds r0, r5, 0x1 - lsls r0, 24 - lsrs r5, r0, 24 - ldr r0, [r6] - cmp r0, 0 - beq _081792EC - cmp r5, 0x2 - bls _0817929C -_081792EC: - cmp r5, 0x3 - bne _0817931C - ldr r0, =gUnknown_0203BD0E - ldrb r0, [r0] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gText_BackupMemoryDamaged - b _08179330 - .pool -_0817931C: - ldr r0, =gUnknown_0203BD0E - ldrb r0, [r0] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gGameContinueCallback - ldr r0, [r0] - cmp r0, 0 - bne _08179370 - ldr r0, =gText_SaveCompleteGameCannotContinue -_08179330: - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 - b _0817937A - .pool -_08179348: - ldr r0, =gUnknown_0203BD0E - ldrb r0, [r0] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gText_BackupMemoryDamaged - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 - ldr r0, =sub_8179390 - bl SetMainCallback2 - b _08179380 - .pool -_08179370: - ldr r0, =gText_SaveCompletePressA - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 -_0817937A: - ldr r0, =sub_81793E0 - bl SetMainCallback2 -_08179380: - pop {r4-r6} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8179288 - - thumb_func_start sub_8179390 -sub_8179390: @ 8179390 - push {lr} - ldr r1, =gUnknown_0203BCFE - movs r0, 0 - strh r0, [r1] - ldr r0, =gMain - ldrh r1, [r0, 0x2E] - movs r0, 0x1 - ands r0, r1 - cmp r0, 0 - beq _081793C4 - ldr r0, =gUnknown_0203BD0E - ldrb r0, [r0] - movs r1, 0x11 - bl FillWindowPixelBuffer - ldr r0, =gText_GamePlayCannotBeContinued - movs r1, 0x1 - movs r2, 0 - bl sub_8178F44 - ldr r0, =sub_8178FC8 - bl SetVBlankCallback - ldr r0, =sub_81793E0 - bl SetMainCallback2 -_081793C4: - pop {r0} - bx r0 - .pool - thumb_func_end sub_8179390 - - thumb_func_start sub_81793E0 -sub_81793E0: @ 81793E0 - push {lr} - sub sp, 0x4 - ldr r0, =gUnknown_0203BCFE - movs r2, 0 - strh r2, [r0] - ldr r0, =gMain - ldrh r1, [r0, 0x2E] - movs r0, 0x1 - ands r0, r1 - cmp r0, 0 - beq _08179410 - movs r0, 0x1 - negs r0, r0 - str r2, [sp] - movs r1, 0 - movs r3, 0x10 - bl BeginNormalPaletteFade - ldr r0, =sub_8178FC8 - bl SetVBlankCallback - ldr r0, =sub_8179428 - bl SetMainCallback2 -_08179410: - add sp, 0x4 - pop {r0} - bx r0 - .pool - thumb_func_end sub_81793E0 - - thumb_func_start sub_8179428 -sub_8179428: @ 8179428 - push {r4,r5,lr} - bl UpdatePaletteFade - lsls r0, 24 - lsrs r4, r0, 24 - cmp r4, 0 - bne _0817944E - ldr r5, =gGameContinueCallback - ldr r0, [r5] - cmp r0, 0 - bne _08179448 - bl DoSoftReset - b _0817944E - .pool -_08179448: - bl SetMainCallback2 - str r4, [r5] -_0817944E: - pop {r4,r5} - pop {r0} - bx r0 - thumb_func_end sub_8179428 - - thumb_func_start sub_8179454 -sub_8179454: @ 8179454 - push {r4,r5,lr} - ldr r5, =gMain - ldr r0, [r5, 0x24] - lsrs r4, r0, 3 - movs r0, 0x7 - ands r4, r0 - ldr r0, =gUnknown_085EFD80 - ldr r1, [r0, 0x4] - ldr r0, [r0] - str r0, [r5, 0x38] - str r1, [r5, 0x3C] - ldrh r1, [r5, 0x3A] - ldr r0, =0xfffffe00 - ands r0, r1 - movs r1, 0x70 - orrs r0, r1 - strh r0, [r5, 0x3A] - adds r1, r5, 0 - adds r1, 0x38 - movs r0, 0x48 - strb r0, [r1] - ldr r0, =gUnknown_0203BCFE - ldrh r0, [r0] - cmp r0, 0 - beq _081794DC - ldr r3, =gUnknown_085EFDAC - lsls r2, r4, 1 - adds r2, r4 - adds r0, r2, r3 - ldrb r4, [r0] - ldrh r1, [r5, 0x3C] - ldr r0, =0xfffffc00 - ands r0, r1 - orrs r0, r4 - strh r0, [r5, 0x3C] - adds r0, r3, 0x2 - adds r0, r2, r0 - ldrb r1, [r0] - lsls r1, 4 - adds r3, 0x1 - adds r2, r3 - ldrb r0, [r2] - lsls r0, 3 - orrs r1, r0 - adds r3, r5, 0 - adds r3, 0x3B - movs r0, 0x1F - ands r1, r0 - lsls r1, 1 - ldrb r2, [r3] - movs r0, 0x3F - negs r0, r0 - ands r0, r2 - orrs r0, r1 - strb r0, [r3] - b _081794E8 - .pool -_081794DC: - ldrh r0, [r5, 0x3C] - ldr r1, =0xfffffc00 - ands r1, r0 - movs r0, 0x1 - orrs r1, r0 - strh r1, [r5, 0x3C] -_081794E8: - ldr r0, =gUnknown_030022F8 - movs r1, 0xE0 - lsls r1, 19 - movs r2, 0x1 - bl CpuFastSet - ldr r1, =gUnknown_0203BCFE - ldrh r0, [r1, 0x2] - cmp r0, 0 - beq _08179500 - subs r0, 0x1 - strh r0, [r1, 0x2] -_08179500: - pop {r4,r5} - pop {r0} - bx r0 - .pool - thumb_func_end sub_8179454 - - thumb_func_start sub_8179514 -sub_8179514: @ 8179514 - push {r4,lr} - lsls r0, 16 - lsrs r0, 16 - ldr r4, =gSaveDataBuffer - movs r3, 0x80 - lsls r3, 5 - movs r1, 0 - adds r2, r4, 0 - bl ReadFlash - movs r1, 0 - ldr r2, =0x000003ff -_0817952C: - ldr r0, [r4] - cmp r0, 0 - beq _08179540 - movs r0, 0x1 - b _0817954E - .pool -_08179540: - adds r0, r1, 0x1 - lsls r0, 16 - lsrs r1, r0, 16 - adds r4, 0x4 - cmp r1, r2 - bls _0817952C - movs r0, 0 -_0817954E: - pop {r4} - pop {r1} - bx r1 - thumb_func_end sub_8179514 - - thumb_func_start sub_8179554 -sub_8179554: @ 8179554 - push {r4-r7,lr} - mov r7, r8 - push {r7} - lsls r0, 16 - lsrs r6, r0, 16 - movs r0, 0 - ldr r1, =0x00000fff - mov r8, r1 - ldr r7, =ProgramFlashByte -_08179566: - movs r4, 0 - adds r5, r0, 0x1 -_0817956A: - ldr r3, [r7] - adds r0, r6, 0 - adds r1, r4, 0 - movs r2, 0 - bl _call_via_r3 - adds r0, r4, 0x1 - lsls r0, 16 - lsrs r4, r0, 16 - cmp r4, r8 - bls _0817956A - adds r0, r6, 0 - bl sub_8179514 - lsls r0, 24 - lsrs r1, r0, 24 - lsls r0, r5, 16 - lsrs r0, 16 - cmp r1, 0 - beq _08179596 - cmp r0, 0x81 - bls _08179566 -_08179596: - adds r0, r1, 0 - pop {r3} - mov r8, r3 - pop {r4-r7} - pop {r1} - bx r1 - .pool - thumb_func_end sub_8179554 - - thumb_func_start sub_81795AC -sub_81795AC: @ 81795AC - push {r4-r6,lr} - adds r5, r0, 0 - movs r6, 0 -_081795B2: - movs r4, 0x1 - lsls r4, r6 - adds r0, r5, 0 - ands r0, r4 - cmp r0, 0 - beq _081795CC - adds r0, r6, 0 - bl sub_8179554 - lsls r0, 24 - cmp r0, 0 - bne _081795CC - bics r5, r4 -_081795CC: - adds r0, r6, 0x1 - lsls r0, 16 - lsrs r6, r0, 16 - cmp r6, 0x1F - bls _081795B2 - cmp r5, 0 - beq _081795DE - movs r0, 0x1 - b _081795E0 -_081795DE: - movs r0, 0 -_081795E0: - pop {r4-r6} - pop {r1} - bx r1 - thumb_func_end sub_81795AC - - .align 2, 0 @ Don't pad with nop. diff --git a/include/gba/flash_internal.h b/include/gba/flash_internal.h index 6156b6c14..ba84546aa 100644 --- a/include/gba/flash_internal.h +++ b/include/gba/flash_internal.h @@ -65,6 +65,7 @@ u16 ReadFlashId(void); void StartFlashTimer(u8 phase); void SetReadFlash1(u16 *dest); void StopFlashTimer(void); +void ReadFlash(u16 sectorNum, u32 offset, u8 *dest, u32 size); u16 WaitForFlashWrite_Common(u8 phase, u8 *addr, u8 lastData); diff --git a/include/save.h b/include/save.h index e98233c67..6c1238cd2 100644 --- a/include/save.h +++ b/include/save.h @@ -76,7 +76,7 @@ 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 HandleSavingData(u8 saveType); //u8 TrySavingData(u8 saveType); //u8 sub_8125D80(void); //bool8 sub_8125DA8(void); diff --git a/ld_script.txt b/ld_script.txt index 3170e1919..890e58a9e 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -221,7 +221,7 @@ SECTIONS { asm/diploma.o(.text); asm/berry_tag_screen.o(.text); asm/mystery_event_menu.o(.text); - asm/save_failed_screen.o(.text); + src/save_failed_screen.o(.text); asm/braille_puzzles.o(.text); asm/pokeblock_feed.o(.text); asm/clear_save_data_screen.o(.text); diff --git a/src/save_failed_screen.c b/src/save_failed_screen.c new file mode 100755 index 000000000..546342b9d --- /dev/null +++ b/src/save_failed_screen.c @@ -0,0 +1,332 @@ +#include "global.h" +#include "text.h" +#include "main.h" +#include "palette.h" +#include "gpu_regs.h" +#include "bg.h" +#include "task.h" +#include "window.h" +#include "menu.h" +#include "save.h" +#include "gba/flash_internal.h" + +#define MSG_WIN_TOP 12 +#define CLOCK_WIN_TOP (MSG_WIN_TOP - 4) + +extern void AddTextPrinterParametrized2(u8 windowId, u8 fontId, u8 x, u8 y, u8 letterSpacing, u8 lineSpacing, struct TextColor *color, u8 speed, u8 *str); + +extern void (*gGameContinueCallback)(void); + +extern u32 gDamagedSaveSectors; + +extern u16 gUnknown_0203BCFC; +extern const u8 gUnknown_085B0E04[]; +extern const u8 gUnknown_085B0A80[]; +extern const u8 gUnknown_085B0C0C[]; +extern const u8 gUnknown_085EFDE4[]; + +extern const struct OamData gUnknown_085EFD80; // sClockOamData +extern const u8 gUnknown_085EFDAC[8][3]; // sClockFrames + +extern const struct BgTemplate gUnknown_085EFD88[]; +extern const struct WindowTemplate gUnknown_085EFD94[]; +extern struct WindowTemplate gUnknown_085EFD9C; +extern struct WindowTemplate gUnknown_085EFDA4; +extern struct SaveSection gSaveDataBuffer; +extern const u32 gUnknown_0850E87C[]; + +extern const u16 gUnknown_085B0A00[]; +extern const u16 gUnknown_085EFDC4[]; +extern const u16 gUnknown_0850FEFC[]; +extern const u16 gUnknown_0860F074[]; + +extern u8 gText_SaveFailedCheckingBackup[]; +extern u8 gText_BackupMemoryDamaged[]; +extern u8 gText_CheckCompleted[]; +extern u8 gText_SaveCompleteGameCannotContinue[]; +extern u8 gText_SaveCompletePressA[]; +extern u8 gText_GamePlayCannotBeContinued[]; + +extern void sub_8179454(void); + +extern u8 gDecompressionBuffer[]; + +struct Unk203BCFC +{ + u16 unk0; + u16 unk2; +}; + +extern struct Unk203BCFC gUnknown_0203BCFE; + +struct Unk203BD0E +{ + u8 unk0; + u8 unk1; +}; + +extern struct Unk203BD0E gUnknown_0203BD0E; + +void sub_8178FDC(void); +void sub_8179288(void); +void sub_8179390(void); +void sub_81793E0(void); +bool8 sub_81795AC(u32); +void sub_8179428(void); +bool8 sub_8179514(u16 sector); + +void sub_8178F44(u8 *text, u8 var1, u8 var2) +{ + struct TextColor color; + + color.fgColor = 0; + color.bgColor = 15; + color.shadowColor = 3; + AddTextPrinterParametrized2(gUnknown_0203BD0E.unk0, 1, var1 * 8, var2 * 8 + 1, 0, 0, &color, 0, text); +} + +void DoSaveFailedScreen(u8 saveType) +{ + SetMainCallback2(sub_8178FDC); + gUnknown_0203BCFC = saveType; + gUnknown_0203BCFE.unk0 = 0; + gUnknown_0203BCFE.unk2 = 0; + gUnknown_0203BD0E.unk0 = 0; + gUnknown_0203BD0E.unk1 = 0; +} + +void sub_8178FC8(void) +{ + LoadOam(); + ProcessSpriteCopyRequests(); + TransferPlttBuffer(); +} + +void sub_8178FDC(void) +{ + switch(gMain.state) + { + case 0: + default: + SetVBlankCallback(NULL); + SetGpuReg(REG_OFFSET_DISPCNT, 0); + SetGpuReg(REG_OFFSET_BG3CNT, 0); + SetGpuReg(REG_OFFSET_BG2CNT, 0); + SetGpuReg(REG_OFFSET_BG1CNT, 0); + SetGpuReg(REG_OFFSET_BG0CNT, 0); + SetGpuReg(REG_OFFSET_BG3HOFS, 0); + SetGpuReg(REG_OFFSET_BG3VOFS, 0); + SetGpuReg(REG_OFFSET_BG2HOFS, 0); + SetGpuReg(REG_OFFSET_BG2VOFS, 0); + SetGpuReg(REG_OFFSET_BG1HOFS, 0); + SetGpuReg(REG_OFFSET_BG1VOFS, 0); + SetGpuReg(REG_OFFSET_BG0HOFS, 0); + SetGpuReg(REG_OFFSET_BG0VOFS, 0); + // how come this doesnt use the Dma manager? + DmaFill16(3, 0, VRAM, VRAM_SIZE); + DmaFill32(3, 0, OAM, OAM_SIZE); + DmaFill16(3, 0, PLTT, PLTT_SIZE); + LZ77UnCompVram(gUnknown_085B0E04, (void *)VRAM); + LZ77UnCompVram(gUnknown_085B0A80, (void *)(VRAM + 0x7000)); + LZ77UnCompVram(gUnknown_085B0C0C, (void *)(VRAM + 0x7800)); + LZ77UnCompVram(gUnknown_085EFDE4, (void *)(VRAM + 0x10020)); + ResetBgsAndClearDma3BusyFlags(0); + InitBgsFromTemplates(0, gUnknown_085EFD88, 3); + SetBgTilemapBuffer(0, (void *)&gDecompressionBuffer[0x2000]); + CpuFill32(0, &gDecompressionBuffer[0x2000], 0x800); + LoadBgTiles(0, gUnknown_0850E87C, 0x120, 0x214); + InitWindows(gUnknown_085EFD94); + // AddWindowWithoutTileMap returns a u16/integer, but the info is clobbered into a u8 here resulting in lost info. Bug? + gUnknown_0203BD0E.unk0 = AddWindowWithoutTileMap(&gUnknown_085EFD9C); + SetWindowAttribute(gUnknown_0203BD0E.unk0, 7, (u32)&gDecompressionBuffer[0x2800]); + gUnknown_0203BD0E.unk1 = AddWindowWithoutTileMap(&gUnknown_085EFDA4); + SetWindowAttribute(gUnknown_0203BD0E.unk1, 7, (u32)&gDecompressionBuffer[0x3D00]); + DeactivateAllTextPrinters(); + ResetSpriteData(); + ResetTasks(); + ResetPaletteFade(); + LoadPalette(gUnknown_085B0A00, 0, 0x40); + LoadPalette(gUnknown_085EFDC4, 0x100, 0x20); + LoadPalette(gUnknown_0850FEFC, 0xE0, 0x20); + LoadPalette(gUnknown_0860F074, 0xF0, 0x20); + SetWindowBorderStyle(gUnknown_0203BD0E.unk0, FALSE, 0x214, 0xE); + SetWindowBorderStyle(gUnknown_0203BD0E.unk1, FALSE, 0x214, 0xE); + FillWindowPixelBuffer(gUnknown_0203BD0E.unk1, 0x11); // backwards? + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + CopyWindowToVram(gUnknown_0203BD0E.unk1, 2); // again? + CopyWindowToVram(gUnknown_0203BD0E.unk0, 1); + sub_8178F44(gText_SaveFailedCheckingBackup, 1, 0); + BeginNormalPaletteFade(0xFFFFFFFF, 0, 16, 0, 0); + EnableInterrupts(1); + SetVBlankCallback(sub_8178FC8); + SetGpuReg(0, 0x1040); + ShowBg(0); + ShowBg(2); + ShowBg(3); + gMain.state++; + break; + case 1: + if(!UpdatePaletteFade()) + { + SetMainCallback2(sub_8179288); + SetVBlankCallback(sub_8179454); + } + break; + } +} + +void sub_8179288(void) +{ + u8 wipeTries = 0; + + gUnknown_0203BCFE.unk0 = TRUE; + + while (gDamagedSaveSectors != 0 && wipeTries < 3) + { + if (sub_81795AC(gDamagedSaveSectors) != FALSE) + { + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + sub_8178F44(gText_BackupMemoryDamaged, 1, 0); + SetMainCallback2(sub_8179390); + return; + } + + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + sub_8178F44(gText_CheckCompleted, 1, 0); + HandleSavingData(gUnknown_0203BCFC); + + if(gDamagedSaveSectors != 0) + { + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + sub_8178F44(gText_SaveFailedCheckingBackup, 1, 0); + } + + wipeTries++; + } + + if(wipeTries == 3) + { + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + sub_8178F44(gText_BackupMemoryDamaged, 1, 0); + } + else + { + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + + if(gGameContinueCallback == NULL) + sub_8178F44(gText_SaveCompleteGameCannotContinue, 1, 0); + else + sub_8178F44(gText_SaveCompletePressA, 1, 0); + } + + SetMainCallback2(sub_81793E0); +} + +void sub_8179390(void) +{ + gUnknown_0203BCFE.unk0 = FALSE; + + if(gMain.newKeys & A_BUTTON) + { + FillWindowPixelBuffer(gUnknown_0203BD0E.unk0, 0x11); + sub_8178F44(gText_GamePlayCannotBeContinued, 1, 0); + SetVBlankCallback(sub_8178FC8); + SetMainCallback2(sub_81793E0); + } +} + +void sub_81793E0(void) +{ + gUnknown_0203BCFE.unk0 = FALSE; + + if(gMain.newKeys & A_BUTTON) + { + BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 16, 0); + SetVBlankCallback(sub_8178FC8); + SetMainCallback2(sub_8179428); + } +} + +void sub_8179428(void) +{ + if(!UpdatePaletteFade()) + { + if(gGameContinueCallback == NULL) // no callback exists, so do a soft reset. + { + DoSoftReset(); + } + else + { + SetMainCallback2((MainCallback)gGameContinueCallback); + gGameContinueCallback = NULL; + } + } +} + +void sub_8179454(void) +{ + unsigned int n = (gMain.vblankCounter2 >> 3) & 7; + + gMain.oamBuffer[0] = gUnknown_085EFD80; + gMain.oamBuffer[0].x = 112; + gMain.oamBuffer[0].y = (CLOCK_WIN_TOP + 1) * 8;; + + if(gUnknown_0203BCFE.unk0 != FALSE) + { + gMain.oamBuffer[0].tileNum = gUnknown_085EFDAC[n][0]; + gMain.oamBuffer[0].matrixNum = (gUnknown_085EFDAC[n][2] << 4) | (gUnknown_085EFDAC[n][1] << 3); + } + else + { + gMain.oamBuffer[0].tileNum = 1; + } + + CpuFastCopy(gMain.oamBuffer, (void *)OAM, 4); + + if(gUnknown_0203BCFE.unk2) + gUnknown_0203BCFE.unk2--; +} + +bool8 sub_8179514(u16 sector) +{ + u32 *ptr = (u32 *)&gSaveDataBuffer; + u16 i; + + ReadFlash(sector, 0, (u8 *)ptr, 4096); + + for (i = 0; i < 0x400; i++, ptr++) + if (*ptr) + return TRUE; + + return FALSE; +} + +bool8 sub_8179554(u16 sector) +{ + u16 i, j; + bool8 failed = TRUE; + + for (i = 0; failed && i < 130; i++) + { + for (j = 0; j < 0x1000; j++) + ProgramFlashByte(sector, j, 0); + + failed = sub_8179514(sector); + } + + return failed; +} + +bool8 sub_81795AC(u32 sectorBits) +{ + u16 i; + + for (i = 0; i < 0x20; i++) + if ((sectorBits & (1 << i)) && !sub_8179554(i)) + sectorBits &= ~(1 << i); + + if (sectorBits == 0) + return FALSE; + else + return TRUE; +}