Skip to content

Commit

Permalink
Identify more inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
cadmic committed Sep 18, 2024
1 parent f672e3b commit c593757
Showing 1 changed file with 55 additions and 109 deletions.
164 changes: 55 additions & 109 deletions src/emulator/mcardGCN.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ static s32 toggle2;
static s32 checkFailCount;
static s32 bWrite2Card;

static inline bool mcardReadyFile(MemCard* pMCard);
static inline bool mcardFinishFile(MemCard* pMCard);
static inline bool mcardReadAnywhereNoTime(MemCard* pMCard, s32 offset, s32 size, char* buffer);
static inline bool mcardWriteAnywhereNoTime(MemCard* pMCard, s32 offset, u32 size, char* buffer);
static inline bool mcardFileRelease(MemCard* pMCard);
Expand Down Expand Up @@ -193,9 +195,10 @@ static bool mcardCalculateChecksumFileBlock2(MemCard* pMCard, s32* checksum) {
return true;
}

static bool mcardSaveChecksumFileHeader(MemCard* pMCard, char* buffer, u32 unused, u32 unused2) {
static bool mcardSaveChecksumFileHeader(MemCard* pMCard, char* buffer) {
char buffer2[0x2000];
s32 checksum;
s32 pad[2];

memcpy(buffer2, pMCard->writeBuffer, 0x2000);
memcpy(pMCard->writeBuffer, buffer, 0x2000);
Expand Down Expand Up @@ -303,65 +306,39 @@ static bool mcardCheckChecksumFileHeader(MemCard* pMCard, char* buffer) {
return true;
}

static inline bool UnkInlinemCardVerifyChecksumFileHeader(MemCard* pMCard) {
if (pMCard->saveToggle == true) {
if (!mcardReadyCard(pMCard)) {
return false;
}
if (mcardGCErrorHandler(pMCard, CARDOpen(pMCard->slot, pMCard->file.name, &pMCard->file.fileInfo)) != true) {
CARDUnmount(pMCard->slot);
return false;
}
}

return true;
}

static inline bool UnkInlinemCardVerifyChecksumFileHeader3(MemCard* pMCard, char** buffer) {
if (!xlHeapFree(buffer)) {
return false;
}
if (pMCard->saveToggle == true) {
if (pMCard->file.fileInfo.chan != -1) {
CARDClose(&pMCard->file.fileInfo);
}
CARDUnmount(pMCard->slot);
}
return false;
}

static inline bool UnkInlinemCardVerifyChecksumFileHeader4(MemCard* pMCard, char** buffer) {
if (!xlHeapFree(buffer)) {
return false;
}
if (pMCard->saveToggle == true) {
if (pMCard->file.fileInfo.chan != -1) {
CARDClose(&pMCard->file.fileInfo);
}
CARDUnmount(pMCard->slot);
}
return true;
}

static bool mcardVerifyChecksumFileHeader(MemCard* pMCard) {
char* buffer;

if (!UnkInlinemCardVerifyChecksumFileHeader(pMCard)) {
if (!mcardReadyFile(pMCard)) {
return false;
}
if (!xlHeapTake(&buffer, 0x6000 | 0x30000000)) {
return false;
}

if (!mcardReadAnywhereNoTime(pMCard, 0, 0x6000, buffer)) {
return UnkInlinemCardVerifyChecksumFileHeader3(pMCard, &buffer);
if (!xlHeapFree(&buffer)) {
return false;
}
mcardFinishFile(pMCard);
return false;
}

DCInvalidateRange(buffer, 0x6000);

if (!mcardCheckChecksumFileHeader(pMCard, buffer)) {
return UnkInlinemCardVerifyChecksumFileHeader3(pMCard, &buffer);
if (!xlHeapFree(&buffer)) {
return false;
}
mcardFinishFile(pMCard);
return false;
}

return UnkInlinemCardVerifyChecksumFileHeader4(pMCard, &buffer);
if (!xlHeapFree(&buffer)) {
return false;
}
mcardFinishFile(pMCard);
return true;
}

static bool mcardPoll(MemCard* pMCard) {
Expand Down Expand Up @@ -392,6 +369,31 @@ static bool mcardPoll(MemCard* pMCard) {
static bool mcardReadyCard(MemCard* pMCard);
#pragma GLOBAL_ASM("asm/non_matchings/mcardGCN/mcardReadyCard.s")

static inline bool mcardReadyFile(MemCard* pMCard) {
if (pMCard->saveToggle == true) {
if (!mcardReadyCard(pMCard)) {
return false;
}
if (mcardGCErrorHandler(pMCard, CARDOpen(pMCard->slot, pMCard->file.name, &pMCard->file.fileInfo)) != true) {
CARDUnmount(pMCard->slot);
return false;
}
}

return true;
}

static inline bool mcardFinishFile(MemCard* pMCard) {
if (pMCard->saveToggle == true) {
if (pMCard->file.fileInfo.chan != -1) {
CARDClose(&pMCard->file.fileInfo);
}
CARDUnmount(pMCard->slot);
}

return true;
}

static inline bool mcardReadAnywhereNoTime(MemCard* pMCard, s32 offset, s32 size, char* buffer) {
if (mCard.saveToggle == true) {
if (mcardGCErrorHandler(pMCard, CARDReadAsync(&pMCard->file.fileInfo, buffer, size, offset, NULL)) != true) {
Expand Down Expand Up @@ -463,8 +465,8 @@ bool mcardWriteGameDataReset(MemCard* pMCard) {

bool mcardReInit(MemCard* pMCard) {
pMCard->saveToggle = true;
mcardGameRelease(pMCard);
mcardFileRelease(pMCard);
mcardGameRelease(pMCard);

pMCard->error = MC_E_NONE;
pMCard->slot = 0;
Expand Down Expand Up @@ -584,48 +586,21 @@ bool mcardFileErase(MemCard* pMCard) {
if (mcardPoll(pMCard) != 1) {
pMCard->pPollFunction = NULL;
pMCard->accessType = 0;
mcardGameRelease(pMCard);
pMCard->file.changedDate = 0;
memset(pMCard->file.name, 0, 0x21);
pMCard->file.numberOfGames = 0;
memset(pMCard->file.gameSize, 0, 0x10);
memset(pMCard->file.gameOffset, 0, 0x10);
memset(pMCard->file.gameName[0], 0, 0x201);
mcardFileRelease(pMCard);
CARDUnmount(pMCard->slot);
return false;
}

pMCard->accessType = 0;
pMCard->pPollFunction = NULL;
mcardGameRelease(pMCard);
pMCard->file.changedDate = 0;
memset(pMCard->file.name, 0, 0x21);
pMCard->file.numberOfGames = 0;
memset(pMCard->file.gameSize, 0, 0x10);
memset(pMCard->file.gameOffset, 0, 0x10);
memset(pMCard->file.gameName[0], 0, 0x201);
mcardFileRelease(pMCard);
CARDUnmount(pMCard->slot);
}

return true;
}
#endif

static inline bool mcardGameEraseInline(MemCard* pMCard) {
if (pMCard->saveToggle == 1) {
if (!mcardReadyCard(pMCard)) {
return false;
}

if (mcardGCErrorHandler(pMCard, CARDOpen(pMCard->slot, pMCard->file.name, &pMCard->file.fileInfo)) != 1) {
CARDUnmount(pMCard->slot);
return false;
}
}

return true;
}

bool mcardGameErase(MemCard* pMCard, s32 index) {
if (pMCard->saveToggle == 1) {
pMCard->accessType = 2;
Expand All @@ -636,61 +611,33 @@ bool mcardGameErase(MemCard* pMCard, s32 index) {
memset(pMCard->file.gameName[index], 0, sizeof(pMCard->file.gameName[index]));
pMCard->file.numberOfGames -= 1;

if (!mcardGameEraseInline(pMCard)) {
if (!mcardReadyFile(pMCard)) {
pMCard->accessType = 0;
return false;
}

if (!mcardWriteFileHeader(pMCard)) {
pMCard->accessType = 0;
if (pMCard->saveToggle == 1) {
if (pMCard->file.fileInfo.chan != -1) {
CARDClose(&pMCard->file.fileInfo);
}
CARDUnmount(pMCard->slot);
}
mcardFinishFile(pMCard);
return false;
}

pMCard->accessType = 0;

if (pMCard->saveToggle == 1) {
if (pMCard->file.fileInfo.chan != -1) {
CARDClose(&pMCard->file.fileInfo);
}
CARDUnmount(pMCard->slot);
}
mcardFinishFile(pMCard);
}

return true;
}

static inline bool mcardFileRelease(MemCard* pMCard) {
//! TODO: determine if this belongs to this function
mcardGameRelease(pMCard);

pMCard->file.changedDate = 0;
memset(pMCard->file.name, 0, ARRAY_COUNT(pMCard->file.name));
pMCard->file.numberOfGames = 0;
memset(pMCard->file.gameSize, 0, ARRAY_COUNT(pMCard->file.gameSize));
memset(pMCard->file.gameOffset, 0, ARRAY_COUNT(pMCard->file.gameOffset));
memset(pMCard->file.gameName, 0, 0x201);

if (!pMCard->bufferCreated) {
if (pMCard->file.game.buffer != NULL) {
if (!xlHeapFree((void**)&pMCard->file.game.buffer)) {
return false;
}
}

pMCard->file.game.size = 0;
memset(&pMCard->file.game.configuration, 0, sizeof(s32));
}

if ((pMCard->file.game.writtenBlocks == NULL) || xlHeapFree((void**)&pMCard->file.game.writtenBlocks)) {
pMCard->file.game.writtenConfig = 0;
pMCard->file.currentGame = 16;
}

return true;
}

bool mcardGameRelease(MemCard* pMCard) {
Expand All @@ -714,7 +661,6 @@ bool mcardGameRelease(MemCard* pMCard) {
pMCard->file.game.writtenConfig = 0;
pMCard->file.currentGame = 16;

NO_INLINE();
return true;
}

Expand Down

0 comments on commit c593757

Please sign in to comment.