Skip to content

Commit

Permalink
port: refactor gbcRomCheck a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Sep 2, 2024
1 parent 2550b5a commit 2a5c3a3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion port/include/romdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ u8 *romdataSegGetData(const char *segName);
u8 *romdataSegGetDataEnd(const char *segName);
u32 romdataSegGetSize(const char *segName);

void gbcRomCheck(void);
s32 romdataCheckGbcRom(void);

#endif
3 changes: 2 additions & 1 deletion port/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int main(int argc, const char **argv)
inputInit();
audioInit();
romdataInit();
gbcRomCheck();

g_ValidGbcRomFound = romdataCheckGbcRom();

gameInit();

Expand Down
53 changes: 29 additions & 24 deletions port/src/romdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include "system.h"
#include "preprocess.h"
#include "platform.h"
#ifndef PLATFORM_N64
#include "bss.h"
#endif

/**
* asset files and ROM segments can be replaced by optional external files,
Expand Down Expand Up @@ -392,43 +389,51 @@ s32 romdataInit(void)
return 0;
}

void gbcRomCheck(void)
static inline bool romdataCheckGbcRomContents(const u8 *gbcRomFile, const u32 gbcRomSize)
{
u32 gbcRomSize;
u8* gbcRomFile = fsFileLoad(GBC_ROM_NAME, &gbcRomSize);

if (!gbcRomFile) {
return;
}

if (gbcRomSize != GBC_ROM_SIZE) {
free(gbcRomFile);
return;
return false;
}

// ROM title

if (memcmp(gbcRomFile + 0x134, "PerfDark VPDE", 15) != 0) {
free(gbcRomFile);
return;
return false;
}

// Licensee code

if (memcmp(gbcRomFile + 0x144, "4Y", 2) != 0) {
free(gbcRomFile);
return;
return false;
}

// Header and global checksums

if (gbcRomFile[0x14D] != 0xA1 || gbcRomFile[0x14E] != 0xAD || gbcRomFile[0x14F] != 0x0F) {
free(gbcRomFile);
return;
return false;
}

return true;
}

s32 romdataCheckGbcRom(void)
{
if (fsFileSize(GBC_ROM_NAME) < 0) {
// bail early if it doesn't exist to avoid generating error messages
return false;
}

u32 gbcRomSize = 0;
u8 *gbcRomFile = fsFileLoad(GBC_ROM_NAME, &gbcRomSize);
if (!gbcRomFile) {
return false;
}

const bool ret = romdataCheckGbcRomContents(gbcRomFile, gbcRomSize);
sysMemFree(gbcRomFile);

if (ret) {
sysLogPrintf(LOG_NOTE, "romdataCheckGbcRom: valid GBC rom found");
}

g_validGbcRomFound = true;
free(gbcRomFile);
return ret;
}

s32 romdataFileGetSize(s32 fileNum)
Expand Down
4 changes: 2 additions & 2 deletions src/game/pak.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ bool var80075d14 = true;
#endif

#ifndef PLATFORM_N64
bool g_validGbcRomFound = false;
bool g_ValidGbcRomFound = false;
#endif

u32 pakGetBlockSize(s8 device)
Expand Down Expand Up @@ -5997,7 +5997,7 @@ bool gbpakIsAnyPerfectDark(void)

return false;
#else
return g_validGbcRomFound;
return g_ValidGbcRomFound;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/include/bss.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ extern struct chrdata *g_MpBotChrPtrs[MAX_BOTS];
extern s32 g_JpnMaxCacheItems;
extern s32 var8009d370jf;
#ifndef PLATFORM_N64
extern bool g_validGbcRomFound;
extern bool g_ValidGbcRomFound;
#endif

#endif

0 comments on commit 2a5c3a3

Please sign in to comment.