From 509a8aac3d4a01a254afffcaf51b65d64887ffe4 Mon Sep 17 00:00:00 2001 From: Madhav Kanbur Date: Sun, 10 Jan 2021 14:34:16 +0530 Subject: [PATCH] NDSCart : Abstract out common code in LoadROM() Signed-off-by: Madhav Kanbur --- src/NDSCart.cpp | 70 ++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index 07652882..7395d045 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -885,46 +885,15 @@ void DecryptSecureArea(u8* out) } } - -bool LoadROM(const char* path, const char* sram, bool direct) +static bool LoadROMCommon(u32 filelength, const char *sram, bool direct) { - // TODO: streaming mode? for really big ROMs or systems with limited RAM - // for now we're lazy - // also TODO: validate what we're loading!! - - FILE* f = Platform::OpenFile(path, "rb"); - if (!f) - { - return false; - } - - NDS::Reset(); - - fseek(f, 0, SEEK_END); - u32 len = (u32)ftell(f); - - CartROMSize = 0x200; - while (CartROMSize < len) - CartROMSize <<= 1; - u32 gamecode; - fseek(f, 0x0C, SEEK_SET); - fread(&gamecode, 4, 1, f); + memcpy(&gamecode, CartROM + 0x0C, 4); printf("Game code: %c%c%c%c\n", gamecode&0xFF, (gamecode>>8)&0xFF, (gamecode>>16)&0xFF, gamecode>>24); - u8 unitcode; - fseek(f, 0x12, SEEK_SET); - fread(&unitcode, 1, 1, f); + u8 unitcode = CartROM[0x12]; CartIsDSi = (unitcode & 0x02) != 0; - CartROM = new u8[CartROMSize]; - memset(CartROM, 0, CartROMSize); - fseek(f, 0, SEEK_SET); - fread(CartROM, 1, len, f); - - fclose(f); - //CartROM = f; - ROMListEntry romparams; if (!ReadROMParams(gamecode, &romparams)) { @@ -941,7 +910,7 @@ bool LoadROM(const char* path, const char* sram, bool direct) else printf("ROM entry: %08X %08X\n", romparams.ROMSize, romparams.SaveMemType); - if (romparams.ROMSize != len) printf("!! bad ROM size %d (expected %d) rounded to %d\n", len, romparams.ROMSize, CartROMSize); + if (romparams.ROMSize != filelength) printf("!! bad ROM size %d (expected %d) rounded to %d\n", filelength, romparams.ROMSize, CartROMSize); // generate a ROM ID // note: most games don't check the actual value @@ -1026,6 +995,37 @@ bool LoadROM(const char* path, const char* sram, bool direct) return true; } +bool LoadROM(const char* path, const char* sram, bool direct) +{ + // TODO: streaming mode? for really big ROMs or systems with limited RAM + // for now we're lazy + // also TODO: validate what we're loading!! + + FILE* f = Platform::OpenFile(path, "rb"); + if (!f) + { + return false; + } + + NDS::Reset(); + + fseek(f, 0, SEEK_END); + u32 len = (u32)ftell(f); + + CartROMSize = 0x200; + while (CartROMSize < len) + CartROMSize <<= 1; + + CartROM = new u8[CartROMSize]; + memset(CartROM, 0, CartROMSize); + fseek(f, 0, SEEK_SET); + fread(CartROM, 1, len, f); + + fclose(f); + + return LoadROMCommon(len, sram, direct); +} + void RelocateSave(const char* path, bool write) { // herp derp