Skip to content

Commit 7f2bf82

Browse files
committed
MEMCARD Refactor
1 parent a0c5e09 commit 7f2bf82

File tree

13 files changed

+247
-427
lines changed

13 files changed

+247
-427
lines changed

decompile/General/MEMCARD/MEMCARD_00_SetIcon.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,5 @@
22

33
void DECOMP_MEMCARD_SetIcon(int icon)
44
{
5-
int* dst;
6-
int* src;
7-
int i;
8-
9-
// overwrite psyqHand
10-
dst = &data.memcardIcon_PsyqHand[0];
11-
12-
// if ghost
13-
if(icon == 0)
14-
{
15-
src = &data.memcardIcon_Ghost[0];
16-
}
17-
18-
// if crash head
19-
else
20-
{
21-
src = &data.memcardIcon_CrashHead[0];
22-
}
23-
24-
// TODO: inline memcpy asm
25-
// Aligned copy, reduce loop jumps
26-
for(i = 0; i < 0x40; i+= 4)
27-
{
28-
dst[i+0] = src[i+0];
29-
dst[i+1] = src[i+1];
30-
dst[i+2] = src[i+2];
31-
dst[i+3] = src[i+3];
32-
}
5+
// -- deprecated
336
}
Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,14 @@
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)
7-
void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
8-
{
9-
int i;
10-
int crc = 0;
11-
int nextByte;
12-
13-
for(i = 0; i < len; i++)
14-
{
15-
nextByte = 0;
16-
if(i < len-2) nextByte = (int)saveBytes[i];
17-
crc = MEMCARD_CRC16(crc, nextByte);
18-
}
19-
20-
// write checksum to data (last 2 bytes),
21-
// swap endians to throw off hackers,
22-
// which didn't really throw anyone off at all
23-
saveBytes[len-2] = (char)(crc>>8);
24-
saveBytes[len-1] = (char)(crc);
25-
}
26-
#endif
3+
int MEMCARD_NewFunc_AsyncCRC(unsigned char* saveBytes, int len);
274

28-
#if 0 // 156
29-
// Closer resembles the loading function
30-
void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
5+
int DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
316
{
32-
int i;
33-
int crc;
34-
int byteIndexEnd;
35-
int byteIndexStart;
36-
int boolFinishThisFrame = 1;
7+
int ret = MEMCARD_NewFunc_AsyncCRC(saveBytes, len);
8+
if (ret == MC_RETURN_PENDING)
9+
return ret;
3710

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-
}
11+
int crc = sdata->crc16_checkpoint_status;
5212

5313
// finishing check
5414
crc = MEMCARD_CRC16(crc, 0);
@@ -57,30 +17,8 @@ void DECOMP_MEMCARD_ChecksumSave(unsigned char* saveBytes, int len)
5717
// write checksum to data (last 2 bytes),
5818
// swap endians to throw off hackers,
5919
// 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);
20+
saveBytes[len-2] = (char)(crc>>8);
21+
saveBytes[len-1] = (char)(crc);
7922

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);
23+
return MC_RETURN_IOE;
8524
}
86-
#endif

decompile/General/MEMCARD/MEMCARD_03_ChecksumLoad.c

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,18 @@
11
#include <common.h>
22

3+
int MEMCARD_NewFunc_AsyncCRC(unsigned char* saveBytes, int len);
4+
35
int DECOMP_MEMCARD_ChecksumLoad(unsigned char* saveBytes, int len)
46
{
5-
int i;
6-
int crc;
7-
int byteIndexEnd;
8-
int byteIndexStart;
9-
int boolFinishThisFrame = 1;
7+
int ret = MEMCARD_NewFunc_AsyncCRC(saveBytes, len);
8+
if (ret == MC_RETURN_PENDING)
9+
return ret;
1010

11-
// Leave 2 bytes at the end,
12-
// the checksum is stored there
13-
len -= 2;
14-
byteIndexEnd = len;
11+
int crc = sdata->crc16_checkpoint_status;
1512

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;
21-
22-
// if this is not a one-frame load
23-
if((sdata->memcardStatusFlags & 8) == 0)
24-
{
25-
// if more than 512 bytes remain
26-
if (byteIndexEnd > (byteIndexStart + 0x200))
27-
{
28-
// cap to 512 bytes, and then continue next frame
29-
byteIndexEnd = (byteIndexStart + 0x200);
30-
boolFinishThisFrame = 0;
31-
}
32-
}
33-
34-
// run checksum
35-
for (i = byteIndexStart; i < byteIndexEnd; i++)
36-
{
37-
crc = MEMCARD_CRC16(crc, saveBytes[i]);
38-
}
39-
40-
// save checkpoints for next frame
41-
if (boolFinishThisFrame == 0)
42-
{
43-
sdata->crc16_checkpoint_status = crc;
44-
sdata->crc16_checkpoint_byteIndex = byteIndexEnd;
45-
return MC_RETURN_PENDING;
46-
}
47-
4813
// finishing check
49-
crc = MEMCARD_CRC16(crc, saveBytes[byteIndexEnd]);
50-
crc = MEMCARD_CRC16(crc, saveBytes[byteIndexEnd+1]);
14+
crc = MEMCARD_CRC16(crc, saveBytes[len-2]);
15+
crc = MEMCARD_CRC16(crc, saveBytes[len-1]);
5116

5217
// Will return one of these:
5318
// 0: MC_RETURN_IOE

decompile/General/MEMCARD/MEMCARD_12_NewTask.c

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

33
// NOTE: Always returns 0, should it become a void fn?
4-
int DECOMP_MEMCARD_NewTask(int slotIdx, char *name, uint8_t *ptrMemcard, int memcardFileSize)
4+
int DECOMP_MEMCARD_NewTask(int slotIdx, char *name, uint8_t *ptrMemcard, int memcardFileSize, int unused)
55
{
66
sdata->memcardSlot = slotIdx;
77

0 commit comments

Comments
 (0)