Skip to content

Commit a08debc

Browse files
committed
memcard progress
1 parent c08da5a commit a08debc

File tree

3 files changed

+94
-27
lines changed

3 files changed

+94
-27
lines changed

decompile/General/MEMCARD/MEMCARD_00_SetIcon.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ void DECOMP_MEMCARD_SetIcon(int icon)
2121
src = &data.memcardIcon_CrashHead[0];
2222
}
2323

24-
// aligned copy, reduce loop jumps
24+
// TODO: inline memcpy asm
25+
// Aligned copy, reduce loop jumps
2526
for(i = 0; i < 0x40; i+= 4)
2627
{
2728
dst[i+0] = src[i+0];

decompile/General/MEMCARD/MEMCARD_02_ChecksumSave.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <common.h>
22

3+
#if 1 // 132
4+
// Rewrite looks like this
5+
// Potentially allowing inline optimizations
6+
// Problem: Does not use multi-frame checksum (like loading)
37
void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
48
{
59
int i;
@@ -18,4 +22,65 @@ void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
1822
// which didn't really throw anyone off at all
1923
saveBytes[len-2] = (char)(crc>>8);
2024
saveBytes[len-1] = (char)(crc);
21-
}
25+
}
26+
#endif
27+
28+
#if 0 // 156
29+
// Closer resembles the loading function
30+
void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
31+
{
32+
int i;
33+
int crc;
34+
int byteIndexEnd;
35+
int byteIndexStart;
36+
int boolFinishThisFrame = 1;
37+
38+
// Leave 2 bytes at the end,
39+
// the checksum is stored there
40+
len -= 2;
41+
byteIndexEnd = len;
42+
43+
// Option 1: Set ZERO for a one-frame load
44+
crc = 0;
45+
byteIndexStart = 0;
46+
47+
// run checksum
48+
for (i = byteIndexStart; i < byteIndexEnd; i++)
49+
{
50+
crc = MEMCARD_CRC16(crc, (int)saveBytes[i]);
51+
}
52+
53+
// finishing check
54+
crc = MEMCARD_CRC16(crc, 0);
55+
crc = MEMCARD_CRC16(crc, 0);
56+
57+
// write checksum to data (last 2 bytes),
58+
// swap endians to throw off hackers,
59+
// which didn't really throw anyone off at all
60+
saveBytes[i+0] = (char)(crc>>8);
61+
saveBytes[i+1] = (char)(crc);
62+
}
63+
#endif
64+
65+
#if 0 // 156
66+
// Original game looked like this
67+
void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
68+
{
69+
int i;
70+
int crc = 0;
71+
72+
for(i = 0; i < len-2; i++)
73+
{
74+
crc = MEMCARD_CRC16(crc, (int)saveBytes[i]);
75+
}
76+
77+
crc = MEMCARD_CRC16(crc, 0);
78+
crc = MEMCARD_CRC16(crc, 0);
79+
80+
// write checksum to data (last 2 bytes),
81+
// swap endians to throw off hackers,
82+
// which didn't really throw anyone off at all
83+
saveBytes[i+0] = (char)(crc>>8);
84+
saveBytes[i+1] = (char)(crc);
85+
}
86+
#endif

decompile/General/MEMCARD/MEMCARD_03_ChecksumLoad.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,46 @@ int DECOMP_MEMCARD_ChecksumLoad(unsigned char* saveBytes, int len)
66
int crc;
77
int byteIndexEnd;
88
int byteIndexStart;
9-
int memcardStatusFlags;
10-
11-
// load checkpoints (both zero on first call),
12-
// also group all $gp variables together, save asm
13-
crc = sdata->crc16_checkpoint_status;
14-
byteIndexStart = sdata->crc16_checkpoint_byteIndex;
15-
memcardStatusFlags = sdata->memcardStatusFlags;
9+
int boolFinishThisFrame = 1;
1610

17-
// end of memcard "before" checksum,
18-
// checksum goes in last 2 bytes
11+
// Leave 2 bytes at the end,
12+
// the checksum is stored there
1913
len -= 2;
2014
byteIndexEnd = len;
15+
16+
// Option 1: Set ZERO for a one-frame load
17+
// Option 2: Set ZERO for first-frame of multi-frame load
18+
// Option 3: Set existing checkpoint from previous frame
19+
crc = sdata->crc16_checkpoint_status;
20+
byteIndexStart = sdata->crc16_checkpoint_byteIndex;
2121

22-
// if not running full checksum in one frame
23-
if((memcardStatusFlags & 8) == 0)
22+
// if this is not a one-frame load
23+
if((sdata->memcardStatusFlags & 8) == 0)
2424
{
25-
// if more than 512 bytes remain, cap it
26-
if(byteIndexEnd > (byteIndexStart + 0x200))
25+
// if more than 512 bytes remain
26+
if (byteIndexEnd > (byteIndexStart + 0x200))
27+
{
28+
// cap to 512 bytes, and then continue next frame
2729
byteIndexEnd = (byteIndexStart + 0x200);
30+
boolFinishThisFrame = 0;
31+
}
2832
}
2933

30-
// checksum 10% of the profile
34+
// run checksum
3135
for (i = byteIndexStart; i < byteIndexEnd; i++)
3236
{
3337
crc = MEMCARD_CRC16(crc, saveBytes[i]);
3438
}
35-
36-
// at this point, 'i' equals byteIndexEnd,
37-
// but can't use 'i' cause that's too much asm
38-
39-
// save checkpoints
40-
sdata->crc16_checkpoint_status = crc;
41-
sdata->crc16_checkpoint_byteIndex = byteIndexEnd;
42-
43-
// if end is not reached
44-
if (byteIndexEnd != len)
39+
40+
// save checkpoints for next frame
41+
if (boolFinishThisFrame == 0)
42+
{
43+
sdata->crc16_checkpoint_status = crc;
44+
sdata->crc16_checkpoint_byteIndex = byteIndexEnd;
4545
return MC_RETURN_PENDING;
46+
}
4647

47-
// finalize checksum twice (dont loop)
48+
// finishing check
4849
crc = MEMCARD_CRC16(crc, saveBytes[byteIndexEnd]);
4950
crc = MEMCARD_CRC16(crc, saveBytes[byteIndexEnd+1]);
5051

0 commit comments

Comments
 (0)