From d5df5fd6edde78ca92bd3c124b134936db4e848c Mon Sep 17 00:00:00 2001 From: Madhav Kanbur Date: Mon, 11 Jan 2021 20:34:56 +0530 Subject: [PATCH] GBACart : Abstract out common code in LoadROM() Signed-off-by: Madhav Kanbur --- src/GBACart.cpp | 51 ++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/GBACart.cpp b/src/GBACart.cpp index 4d44d3a1..8baf2f71 100644 --- a/src/GBACart.cpp +++ b/src/GBACart.cpp @@ -620,6 +620,32 @@ void DoSavestate(Savestate* file) if (HasSolarSensor) GBACart_SolarSensor::DoSavestate(file); } +static void LoadROMCommon(const char *sram) +{ + char gamecode[5] = { '\0' }; + memcpy(&gamecode, CartROM + 0xAC, 4); + printf("Game code: %s\n", gamecode); + + for (int i = 0; i < sizeof(SOLAR_SENSOR_GAMECODES)/sizeof(SOLAR_SENSOR_GAMECODES[0]); i++) + { + if (strcmp(gamecode, SOLAR_SENSOR_GAMECODES[i]) == 0) HasSolarSensor = true; + } + + if (HasSolarSensor) + { + printf("GBA solar sensor support detected!\n"); + } + + CartCRC = CRC32(CartROM, CartROMSize); + printf("ROM CRC32: %08X\n", CartCRC); + + CartInserted = true; + + // save + printf("Save file: %s\n", sram); + GBACart_SRAM::LoadSave(sram); +} + bool LoadROM(const char* path, const char* sram) { FILE* f = Platform::OpenFile(path, "rb"); @@ -640,36 +666,13 @@ bool LoadROM(const char* path, const char* sram) while (CartROMSize < len) CartROMSize <<= 1; - char gamecode[5] = { '\0' }; - fseek(f, 0xAC, SEEK_SET); - fread(&gamecode, 1, 4, f); - printf("Game code: %s\n", gamecode); - - for (int i = 0; i < sizeof(SOLAR_SENSOR_GAMECODES)/sizeof(SOLAR_SENSOR_GAMECODES[0]); i++) - { - if (strcmp(gamecode, SOLAR_SENSOR_GAMECODES[i]) == 0) HasSolarSensor = true; - } - - if (HasSolarSensor) - { - printf("GBA solar sensor support detected!\n"); - } - CartROM = new u8[CartROMSize]; memset(CartROM, 0, CartROMSize); fseek(f, 0, SEEK_SET); fread(CartROM, 1, len, f); - fclose(f); - CartCRC = CRC32(CartROM, CartROMSize); - printf("ROM CRC32: %08X\n", CartCRC); - - CartInserted = true; - - // save - printf("Save file: %s\n", sram); - GBACart_SRAM::LoadSave(sram); + LoadROMCommon(sram); return true; }