move ram save variables to C

This commit is contained in:
DizzyEggg 2017-09-03 15:39:33 +02:00
parent a3f602f17c
commit ddf85539c1
7 changed files with 117 additions and 212 deletions

View File

@ -6,156 +6,6 @@
.text .text
thumb_func_start sub_815355C
sub_815355C: @ 815355C
push {r4-r7,lr}
ldr r5, =gFastSaveSection
ldr r0, =gUnknown_0203ABBC
str r0, [r5]
adds r7, r0, 0
ldr r0, =gFlashMemoryPresent
ldr r4, [r0]
cmp r4, 0x1
beq _0815358C
b _081535C8
.pool
_0815357C:
ldrb r1, [r7, 0xA]
ldrb r0, [r7, 0xB]
adds r1, r0
ldrb r0, [r7, 0xC]
adds r1, r0
ldrb r0, [r7, 0xD]
adds r0, r1
b _081535CA
_0815358C:
bl UpdateSaveAddresses
ldr r0, =gRamSaveSectionLocations
bl GetSaveValidStatus
ldr r0, =gSaveCounter
ldr r1, [r0]
ands r1, r4
lsls r0, r1, 3
subs r0, r1
lsls r0, 17
lsrs r6, r0, 16
movs r4, 0
_081535A6:
adds r0, r4, r6
lsls r0, 24
lsrs r0, 24
ldr r1, [r5]
bl DoReadFlashWholeSection
ldr r0, [r5]
ldr r1, =0x00000ff4
adds r0, r1
ldrh r0, [r0]
cmp r0, 0
beq _0815357C
adds r0, r4, 0x1
lsls r0, 16
lsrs r4, r0, 16
cmp r4, 0xD
bls _081535A6
_081535C8:
movs r0, 0
_081535CA:
pop {r4-r7}
pop {r1}
bx r1
.pool
thumb_func_end sub_815355C
thumb_func_start sub_81535DC
sub_81535DC: @ 81535DC
push {r4-r6,lr}
adds r6, r1, 0
lsls r0, 24
lsrs r1, r0, 24
movs r2, 0xE2
lsls r2, 24
adds r0, r2
lsrs r0, 24
cmp r0, 0x1
bhi _08153608
ldr r4, =gUnknown_0203ABBC
movs r3, 0x80
lsls r3, 5
adds r0, r1, 0
movs r1, 0
adds r2, r4, 0
bl ReadFlash
ldr r1, [r4]
ldr r0, =0x0000b39d
cmp r1, r0
beq _08153614
_08153608:
movs r0, 0xFF
b _0815362A
.pool
_08153614:
movs r3, 0
ldr r5, =0x00000ffb
adds r2, r4, 0x4
_0815361A:
adds r0, r6, r3
adds r1, r2, r3
ldrb r1, [r1]
strb r1, [r0]
adds r3, 0x1
cmp r3, r5
ble _0815361A
movs r0, 0x1
_0815362A:
pop {r4-r6}
pop {r1}
bx r1
.pool
thumb_func_end sub_81535DC
thumb_func_start sub_8153634
sub_8153634: @ 8153634
push {r4-r7,lr}
adds r5, r1, 0
lsls r0, 24
lsrs r6, r0, 24
movs r1, 0xE2
lsls r1, 24
adds r0, r1
lsrs r0, 24
cmp r0, 0x1
bhi _08153680
ldr r7, =gUnknown_0203ABBC
ldr r0, =0x0000b39d
adds r3, r7, 0
stm r3!, {r0}
movs r2, 0
ldr r4, =0x00000ffb
_08153654:
adds r0, r3, r2
adds r1, r5, r2
ldrb r1, [r1]
strb r1, [r0]
adds r2, 0x1
cmp r2, r4
ble _08153654
adds r0, r6, 0
adds r1, r7, 0
bl ProgramFlashSectorAndVerify
cmp r0, 0
bne _08153680
movs r0, 0x1
b _08153682
.pool
_08153680:
movs r0, 0xFF
_08153682:
pop {r4-r7}
pop {r1}
bx r1
thumb_func_end sub_8153634
thumb_func_start sub_8153688 thumb_func_start sub_8153688
sub_8153688: @ 8153688 sub_8153688: @ 8153688
push {r4,lr} push {r4,lr}

View File

@ -594,7 +594,7 @@ sub_8179514: @ 8179514
push {r4,lr} push {r4,lr}
lsls r0, 16 lsls r0, 16
lsrs r0, 16 lsrs r0, 16
ldr r4, =gUnknown_0203ABBC ldr r4, =gSaveDataBuffer
movs r3, 0x80 movs r3, 0x80
lsls r3, 5 lsls r3, 5
movs r1, 0 movs r1, 0

10
common_syms/save.txt Normal file
View File

@ -0,0 +1,10 @@
gLastWrittenSector
gLastSaveCounter
gLastKnownGoodSector
gDamagedSaveSectors
gSaveCounter
gFastSaveSection
gUnknown_03006208
gSaveUnusedVar
gUnknown_03006210
gGameContinueCallback

View File

@ -89,4 +89,4 @@ u16 CalculateChecksum(void *, u16);
//u8 unref_sub_8126068(u8 sector, u8 *data, u32 size); //u8 unref_sub_8126068(u8 sector, u8 *data, u32 size);
//u8 unref_sub_8126080(u8 sector, u8 *data); //u8 unref_sub_8126080(u8 sector, u8 *data);
#endif #endif // GUARD_SAVE_H

View File

@ -3,17 +3,31 @@
#include "save.h" #include "save.h"
#include "game_stat.h" #include "game_stat.h"
extern u32 gSaveCounter; extern struct SaveSectionOffsets gSaveSectionOffsets[0xE];
extern u16 gLastWrittenSector; extern struct SaveSectionLocation gRamSaveSectionLocations[0xE];
extern u32 gDamagedSaveSectors; extern void *gUnknown_03005D94;
extern u16 gLastKnownGoodSector; extern u8 gDecompressionBuffer[];
extern u32 gLastSaveCounter; extern u32 gFlashMemoryPresent;
extern u16 gUnknown_03006208; extern u16 gUnknown_03006294;
extern struct SaveSection *gFastSaveSection;
extern struct SaveSection *gUnknown_0203ABBC;
bool32 ProgramFlashSectorAndVerify(u8 sector, u8 *data); extern void DoSaveFailedScreen(u8); // save_failed_screen
void ReadFlash(u8 secotr, u32 arg1, u8* data, u32 size); extern void LoadSerializedGame(void); // load_save
extern bool32 ProgramFlashSectorAndVerify(u8 sector, u8 *data);
extern void ReadFlash(u8 sector, u32 arg1, void* data, u32 size);
// iwram common
u16 gLastWrittenSector;
u32 gLastSaveCounter;
u16 gLastKnownGoodSector;
u32 gDamagedSaveSectors;
u32 gSaveCounter;
struct SaveSection *gFastSaveSection;
u16 gUnknown_03006208;
u16 gSaveUnusedVar;
u16 gUnknown_03006210;
void (*gGameContinueCallback)(void);
EWRAM_DATA struct SaveSection gSaveDataBuffer = {0};
void ClearSaveData(void) void ClearSaveData(void)
{ {
@ -59,7 +73,7 @@ u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location)
u32 retVal; u32 retVal;
u16 i; u16 i;
gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; gFastSaveSection = &gSaveDataBuffer;
if (a1 != 0xFFFF) // for link if (a1 != 0xFFFF) // for link
{ {
@ -120,7 +134,7 @@ u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location)
u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size)
{ {
u16 i; u16 i;
struct SaveSection *section = (struct SaveSection *)&gUnknown_0203ABBC; struct SaveSection *section = &gSaveDataBuffer;
for (i = 0; i < sizeof(struct SaveSection); i++) for (i = 0; i < sizeof(struct SaveSection); i++)
((char *)section)[i] = 0; ((char *)section)[i] = 0;
@ -150,7 +164,7 @@ u8 TryWriteSector(u8 sector, u8 *data)
u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused
{ {
gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; gFastSaveSection = &gSaveDataBuffer;
gLastKnownGoodSector = gLastWrittenSector; gLastKnownGoodSector = gLastWrittenSector;
gLastSaveCounter = gSaveCounter; gLastSaveCounter = gSaveCounter;
gLastWrittenSector++; gLastWrittenSector++;
@ -163,7 +177,7 @@ u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location
u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ever called once, and gSaveBlock2 is passed to this function. location is unused 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; gFastSaveSection = &gSaveDataBuffer;
gLastKnownGoodSector = gLastWrittenSector; gLastKnownGoodSector = gLastWrittenSector;
gLastSaveCounter = gSaveCounter; gLastSaveCounter = gSaveCounter;
gUnknown_03006208 = 0; gUnknown_03006208 = 0;
@ -356,7 +370,7 @@ u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location)
u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location) u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location)
{ {
u8 retVal; u8 retVal;
gFastSaveSection = (struct SaveSection *)&gUnknown_0203ABBC; gFastSaveSection = &gSaveDataBuffer;
if (a1 != 0xFFFF) if (a1 != 0xFFFF)
{ {
retVal = 0xFF; retVal = 0xFF;
@ -524,7 +538,7 @@ u8 GetSaveValidStatus(const struct SaveSectionLocation *location)
u8 sub_81530DC(u8 a1, u8 *data, u16 size) u8 sub_81530DC(u8 a1, u8 *data, u16 size)
{ {
u16 i; u16 i;
struct SaveSection *section = (struct SaveSection *)&gUnknown_0203ABBC; struct SaveSection *section = &gSaveDataBuffer;
DoReadFlashWholeSection(a1, section); DoReadFlashWholeSection(a1, section);
if (section->security == UNKNOWN_CHECK_VALUE) if (section->security == UNKNOWN_CHECK_VALUE)
{ {
@ -563,17 +577,13 @@ u16 CalculateChecksum(void *data, u16 size)
return ((checksum >> 16) + checksum); return ((checksum >> 16) + checksum);
} }
extern struct SaveSectionOffsets gSaveSectionOffsets[0xE]; // gSaveSectionOffsets
extern struct SaveSectionLocation gRamSaveSectionLocations[0xE]; // gRamSaveSectionLocations
extern void *gUnknown_03005D94;
#ifdef NONMATCHING #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. // 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) void UpdateSaveAddresses(void)
{ {
int i; int i = 0;
gRamSaveSectionLocations[0].data = gSaveBlock2Ptr + gSaveSectionOffsets[0].toAdd; gRamSaveSectionLocations[i].data = gSaveBlock2Ptr + gSaveSectionOffsets[0].toAdd;
gRamSaveSectionLocations[0].size = gSaveSectionOffsets[0].size; gRamSaveSectionLocations[i].size = gSaveSectionOffsets[0].size;
for(i = 1; i < 5; i++) for(i = 1; i < 5; i++)
{ {
@ -704,10 +714,6 @@ u8 HandleSavingData(u8 saveType)
return 0; return 0;
} }
extern u32 gFlashMemoryPresent;
extern void DoSaveFailedScreen(u8); // save_failed_screen
extern u16 gUnknown_03006294;
u8 TrySavingData(u8 saveType) // TrySave u8 TrySavingData(u8 saveType) // TrySave
{ {
if(gFlashMemoryPresent == TRUE) if(gFlashMemoryPresent == TRUE)
@ -794,11 +800,6 @@ bool8 sub_8153474(void)
return retVal; 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 sub_81534D0(u8 a1)
{ {
u8 result; u8 result;
@ -828,3 +829,69 @@ u8 sub_81534D0(u8 a1)
return result; return result;
} }
u16 sub_815355C(void)
{
u16 i, v3;
struct SaveSection* savSection;
savSection = gFastSaveSection = &gSaveDataBuffer;
if (gFlashMemoryPresent != 1)
return 0;
UpdateSaveAddresses();
GetSaveValidStatus(gRamSaveSectionLocations);
v3 = 0xE * (gSaveCounter % 2);
for (i = 0; i < 14; i++)
{
DoReadFlashWholeSection(i + v3, gFastSaveSection);
if (gFastSaveSection->id == 0)
return savSection->data[10] +
savSection->data[11] +
savSection->data[12] +
savSection->data[13];
}
return 0;
}
u32 sub_81535DC(u8 sector, u8* dst)
{
s32 i;
s32 size;
u8* savData;
if (sector != 30 && sector != 31)
return 0xFF;
ReadFlash(sector, 0, &gSaveDataBuffer, sizeof(struct SaveSection));
if (*(u32*)(&gSaveDataBuffer.data[0]) != 0xB39D)
return 0xFF;
// copies whole save section except u32 counter
i = 0;
size = 0xFFB;
savData = &gSaveDataBuffer.data[4];
for (; i <= size; i++)
dst[i] = savData[i];
return 1;
}
u32 sub_8153634(u8 sector, u8* src)
{
s32 i;
s32 size;
u8* savData;
void* savDataBuffer;
if (sector != 30 && sector != 31)
return 0xFF;
savDataBuffer = &gSaveDataBuffer;
*(u32*)(savDataBuffer) = 0xB39D;
// copies whole save section except u32 counter
i = 0;
size = 0xFFB;
savData = &gSaveDataBuffer.data[4];
for (; i <= size; i++)
savData[i] = src[i];
if (ProgramFlashSectorAndVerify(sector, savDataBuffer) != 0)
return 0xFF;
return 1;
}

View File

@ -392,32 +392,9 @@ gUnknown_030061E8: @ 30061E8
gUnknown_030061EC: @ 30061EC gUnknown_030061EC: @ 30061EC
.space 0x4 .space 0x4
gLastWrittenSector: @ 30061F0 .include"save.o"
.space 0x4
.space 0x8
gLastSaveCounter: @ 30061F4
.space 0x4
gLastKnownGoodSector: @ 30061F8
.space 0x4
gDamagedSaveSectors: @ 30061FC
.space 0x4
gSaveCounter: @ 3006200
.space 0x4
gFastSaveSection: @ 3006204
.space 0x4
gUnknown_03006208: @ 3006208
.space 0x8
gUnknown_03006210: @ 3006210
.space 0x4
gGameContinueCallback: @ 3006214
.space 0xC
gRamSaveSectionLocations: @ 3006220 gRamSaveSectionLocations: @ 3006220
.space 0x74 .space 0x74

View File

@ -1653,8 +1653,9 @@ gUnknown_0203ABB5: @ 203ABB5
gUnknown_0203ABB8: @ 203ABB8 gUnknown_0203ABB8: @ 203ABB8
.space 0x4 .space 0x4
gUnknown_0203ABBC: @ 203ABBC .include "src/save.o"
.space 0x1004
.space 0x4 /*unused var?*/
gUnknown_0203BBC0: @ 203BBC0 gUnknown_0203BBC0: @ 203BBC0
.space 0x74 .space 0x74