From cd99d80e424afdc312ad960835811254c8adea02 Mon Sep 17 00:00:00 2001 From: profi200 Date: Mon, 15 Jun 2020 18:59:09 +0200 Subject: [PATCH] Detect homebrew and force SRAM save type. Bug fixes. --- include/types.h | 1 + source/arm11/hardware/lgy.c | 22 +++++++++++++++------- source/arm9/hardware/lgy.c | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/include/types.h b/include/types.h index a16bf8a..0380364 100644 --- a/include/types.h +++ b/include/types.h @@ -30,6 +30,7 @@ #define ALIGN(a) __attribute__((aligned(a))) // Use alignas() instead. #define NAKED __attribute__((naked)) +#define NOINLINE __attribute__((noinline)) #define PACKED __attribute__((packed)) #define TARGET_ARM __attribute__((target("arm"))) #define TARGET_THUMB __attribute__((target("thumb"))) diff --git a/source/arm11/hardware/lgy.c b/source/arm11/hardware/lgy.c index b0f39e4..39d40cb 100644 --- a/source/arm11/hardware/lgy.c +++ b/source/arm11/hardware/lgy.c @@ -39,7 +39,7 @@ static void lgySleepIrqHandler(u32 intSource) } } -static Result loadRom(const char *const path, u32 *const rsOut) +static Result loadGbaRom(const char *const path, u32 *const rsOut) { Result res; FHandle f; @@ -57,7 +57,7 @@ static Result loadRom(const char *const path, u32 *const rsOut) { *rsOut = romSize; // Pad ROM area with "open bus" value. - memset((void*)(ROM_LOC + romSize), 0xFFFFFFFFu, romSize); + memset((void*)(ROM_LOC + romSize), 0xFFFFFFFFu, MAX_ROM_SIZE - romSize); } } else res = RES_ROM_TOO_BIG; @@ -71,9 +71,15 @@ static Result loadRom(const char *const path, u32 *const rsOut) // Code based on: https://github.com/Gericom/GBARunner2/blob/master/arm9/source/save/Save.vram.cpp static u16 tryDetectSaveType(u32 romSize) { - // TODO: Homebrew detection (always SRAM). + const u32 *romPtr = (u32*)ROM_LOC; + if(romPtr[0xAC / 4] == 0) // If Game Code all zeros --> Homebrew. + { + debug_printf("Detected homebrew. Using SRAM save type.\n"); + return SAVE_TYPE_SRAM_256k; + } + + romPtr += 0xE4 / 4; // Skip headers. u16 saveType = SAVE_TYPE_NONE; - const u32 *romPtr = (u32*)(ROM_LOC + 0xE4u); // Skip headers. for(; romPtr < (u32*)(ROM_LOC + romSize); romPtr++) { u32 tmp = *romPtr; @@ -129,14 +135,16 @@ static u16 tryDetectSaveType(u32 romSize) if(memcmp(romPtr, str, strlen(str)) == 0) { - debug_printf("Detected save type '%s'.\n", str); saveType = tmpSaveType; - break; + debug_printf("Detected save type '%s'.\n", str); + goto saveTypeFound; } } } } +saveTypeFound: + return saveType; } @@ -156,7 +164,7 @@ Result LGY_prepareGbaMode(bool gbaBios, const char *const romPath, const char *c { // Load the ROM image. u32 romSize; - Result res = loadRom(romPath, &romSize); + Result res = loadGbaRom(romPath, &romSize); if(res != RES_OK) return res; // Try to detect the save type. diff --git a/source/arm9/hardware/lgy.c b/source/arm9/hardware/lgy.c index 1625280..b87a822 100644 --- a/source/arm9/hardware/lgy.c +++ b/source/arm9/hardware/lgy.c @@ -49,7 +49,7 @@ static void setupBiosOverlay(bool gbaBios) iomemcpy(REGs_LGY_A7_VECTOR, (u32*)_overlay_stub, (u32)_overlay_stub_size); NDMA_copy((u32*)ARM7_STUB_LOC9, _arm7_stub_start, (u32)_arm7_stub_size); - // Patch swi 0x10 (RegisterRamReset) to swi 0x26 (HardReset). + // Patch swi 0x01 (RegisterRamReset) to swi 0x26 (HardReset). if(gbaBios) *((u8*)_arm7_stub_swi) = 0x26; }