From 5fe51b66cda4f250d562ce57f4694aec7e8a9c3b Mon Sep 17 00:00:00 2001 From: RocketRobz Date: Mon, 13 May 2019 14:05:13 -0600 Subject: [PATCH] Load cheats from `cheatData.bin` --- retail/arm9/include/configuration.h | 2 -- retail/arm9/include/nds_loader_arm9.h | 2 +- retail/arm9/source/conf_sd.cpp | 33 ------------------- retail/arm9/source/main.cpp | 13 +++++--- retail/arm9/source/nds_loader_arm9.c | 25 ++------------ retail/bootloader/include/hook.h | 1 + retail/bootloader/source/arm7/hook_arm7.c | 9 +++-- retail/bootloader/source/arm7/load_crt0.s | 3 ++ retail/bootloader/source/arm7/main.arm7.c | 2 ++ .../arm7/source/card_engine_header.s | 4 +-- .../common/include/cardengine_header_arm7.h | 7 +--- retail/common/include/cheat_engine.h | 13 -------- retail/common/include/load_crt0.h | 1 + 13 files changed, 27 insertions(+), 88 deletions(-) diff --git a/retail/arm9/include/configuration.h b/retail/arm9/include/configuration.h index 05aa7464d..0f26e63a6 100644 --- a/retail/arm9/include/configuration.h +++ b/retail/arm9/include/configuration.h @@ -31,8 +31,6 @@ typedef struct configuration { bool dldiPatchNds; int argc; const char** argv; //const char* argv[ARG_MAX]; - u32* cheat_data; //u32 cheat_data[CHEAT_DATA_MAX_LEN] - u32 cheat_data_len; u32 backlightMode; } configuration; diff --git a/retail/arm9/include/nds_loader_arm9.h b/retail/arm9/include/nds_loader_arm9.h index eb2ad74be..fb10536dd 100644 --- a/retail/arm9/include/nds_loader_arm9.h +++ b/retail/arm9/include/nds_loader_arm9.h @@ -31,7 +31,7 @@ extern "C" //#define LOAD_DEFAULT_NDS 0 -void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 patchOffsetCacheCluster, u32 fatTableCluster, configuration* conf); +void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 fatTableCluster, configuration* conf); #ifdef __cplusplus } diff --git a/retail/arm9/source/conf_sd.cpp b/retail/arm9/source/conf_sd.cpp index 16bca09d9..76ba4cd76 100644 --- a/retail/arm9/source/conf_sd.cpp +++ b/retail/arm9/source/conf_sd.cpp @@ -138,39 +138,6 @@ static void load_conf(configuration* conf, const char* fn) { iniGetKey(IniData, IniCount, &Key); conf->logging = (bool)strtol(Key.Data, NULL, 0); - // Cheat data - Key.Data = (char*)""; - Key.Name = (char*)"CHEAT_DATA"; - iniGetKey(IniData, IniCount, &Key); - conf->cheat_data = (u32*)malloc(CHEAT_DATA_MAX_SIZE); - conf->cheat_data_len = 0; - char* str = strdup(Key.Data); - char* cheat = strtok(str, " "); - if (cheat != NULL) - printf("Cheat data present\n"); - while (cheat != NULL) { - if (!checkCheatDataLen(conf->cheat_data_len)) { - printf("Cheat data size limit reached, the cheats are ignored!\n"); - toncset(conf->cheat_data, 0, conf->cheat_data_len*sizeof(u32)); //cheats.clear(); - conf->cheat_data_len = 0; - break; - } - printf(cheat); - nocashMessage(cheat); - printf(" "); - - conf->cheat_data[conf->cheat_data_len] = strtoul(cheat, NULL, 16); - - nocashMessage(tohex(conf->cheat_data[conf->cheat_data_len])); - printf(" "); - - ++conf->cheat_data_len; - - cheat = strtok(NULL, " "); - } - free(str); - realloc(conf->cheat_data, conf->cheat_data_len*sizeof(u32)); - // Backlight mode Key.Data = (char*)""; Key.Name = (char*)"BACKLIGHT_MODE"; diff --git a/retail/arm9/source/main.cpp b/retail/arm9/source/main.cpp index a992361ac..9bb4e5c46 100644 --- a/retail/arm9/source/main.cpp +++ b/retail/arm9/source/main.cpp @@ -154,8 +154,6 @@ static inline void debugConf(configuration* conf) { //dbg_printf("dldiPatchNds: %s\n", btoa(conf->dldiPatchNds)); //dbg_printf("argc: %lu\n", conf->argc); //const char** argv; - //u32 cheat_data[CHEAT_DATA_MAX_LEN]; - dbg_printf("cheat_data_len: %lu\n", conf->cheat_data_len); dbg_printf("backlightMode: %lX\n", conf->backlightMode); } @@ -337,9 +335,11 @@ static int runNdsFile(configuration* conf) { struct stat st; struct stat stSav; + struct stat stCheat; struct stat stPatchOffsetCache; struct stat stFatTable; u32 clusterSav = 0; + u32 clusterCheat = 0; u32 clusterPatchOffsetCache = 0; u32 clusterFatTable = 0; char filePath[PATH_MAX]; @@ -354,6 +354,12 @@ static int runNdsFile(configuration* conf) { clusterSav = stSav.st_ino; } + std::string cheatFilePath = "sd:/_nds/nds-bootstrap/cheatData.bin"; + + if (stat(cheatFilePath.c_str(), &stCheat) >= 0) { + clusterCheat = stCheat.st_ino; + } + std::string romFilename = ReplaceAll(conf->ndsPath, ".nds", ".bin"); const size_t last_slash_idx = romFilename.find_last_of("/"); if (std::string::npos != last_slash_idx) @@ -398,7 +404,7 @@ static int runNdsFile(configuration* conf) { fread(load_bin, 1, loaderSize, bootloaderBin); fclose(bootloaderBin); - runNds(load_bin, loaderSize, st.st_ino, clusterSav, clusterPatchOffsetCache, clusterFatTable, conf); + runNds(load_bin, loaderSize, st.st_ino, clusterSav, clusterCheat, clusterPatchOffsetCache, clusterFatTable, conf); return 0; } @@ -422,7 +428,6 @@ int main(int argc, char** argv) { free(conf->ndsPath); free(conf->savPath); free(conf->argv); - free(conf->cheat_data); free(conf); stop(); diff --git a/retail/arm9/source/nds_loader_arm9.c b/retail/arm9/source/nds_loader_arm9.c index 6a71f3cd3..149710366 100644 --- a/retail/arm9/source/nds_loader_arm9.c +++ b/retail/arm9/source/nds_loader_arm9.c @@ -181,26 +181,7 @@ int loadArgs(int argc, const char** argv) { return true; } -int loadCheatData(u32* cheat_data, u32 cheat_data_len) { - nocashMessage("loadCheatData"); - - cardengineArm7* ce7 = (cardengineArm7*)0x027E0000; - nocashMessage("ce7"); - nocashMessage(tohex((u32)ce7)); - - u32* ce7_cheat_data = getCheatData(ce7); - nocashMessage("ce7_cheat_data"); - nocashMessage(tohex((u32)ce7_cheat_data)); - - //tonccpy(ce7_cheat_data, cheat_data, 32768); - tonccpy(ce7_cheat_data, cheat_data, cheat_data_len*sizeof(u32)); - - ce7->cheat_data_len = cheat_data_len; - - return true; -} - -void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 patchOffsetCacheCluster, u32 fatTableCluster, configuration* conf) { +void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 cheatCluster, u32 patchOffsetCacheCluster, u32 fatTableCluster, configuration* conf) { nocashMessage("runNds"); irqDisable(IRQ_ALL); @@ -229,6 +210,7 @@ void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u3 lc0->saveFileCluster = saveCluster; lc0->romSize = conf->romSize; lc0->saveSize = conf->saveSize; + lc0->cheatFileCluster = cheatCluster; lc0->patchOffsetCacheFileCluster = patchOffsetCacheCluster; lc0->fatTableFileCluster = fatTableCluster; lc0->language = conf->language; @@ -245,9 +227,6 @@ void runNds(const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u3 lc0->preciseVolumeControl = conf->preciseVolumeControl; lc0->logging = conf->logging; - loadCheatData(conf->cheat_data, conf->cheat_data_len); - free(conf->cheat_data); - free(conf); nocashMessage("irqDisable(IRQ_ALL);"); diff --git a/retail/bootloader/include/hook.h b/retail/bootloader/include/hook.h index 3d6dc6206..fa7a1570c 100644 --- a/retail/bootloader/include/hook.h +++ b/retail/bootloader/include/hook.h @@ -34,6 +34,7 @@ int hookNdsRetailArm7( const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 fileCluster, + u32 cheatFileCluster, u32 language, u32 dsiMode, // SDK5 u32 ROMinRAM, diff --git a/retail/bootloader/source/arm7/hook_arm7.c b/retail/bootloader/source/arm7/hook_arm7.c index db5739961..936275c13 100644 --- a/retail/bootloader/source/arm7/hook_arm7.c +++ b/retail/bootloader/source/arm7/hook_arm7.c @@ -93,6 +93,7 @@ int hookNdsRetailArm7( const tNDSHeader* ndsHeader, const module_params_t* moduleParams, u32 fileCluster, + u32 cheatFileCluster, u32 language, u32 dsiMode, // SDK 5 u32 ROMinRAM, @@ -189,9 +190,6 @@ int hookNdsRetailArm7( ce7->gameSoftReset = gameSoftReset; ce7->preciseVolumeControl = preciseVolumeControl; - u32* ce7_cheat_data = getCheatData(ce7); - endCheatData(ce7_cheat_data, &ce7->cheat_data_len); - *vblankHandler = ce7->patches->vblankHandler; if (!ROMinRAM) { *timer0Handler = ce7->patches->timer0Handler; @@ -201,6 +199,11 @@ int hookNdsRetailArm7( *ipcSyncHandler = ce7->patches->fifoHandler; } + aFile cheatFile = getFileFromCluster(cheatFileCluster); + if (cheatFile.firstCluster != CLUSTER_FREE) { + fileRead(ce7->cheat_data_offset, cheatFile, 0, 0x8000, -1); + } + dbg_printf("ERR_NONE\n"); return ERR_NONE; } diff --git a/retail/bootloader/source/arm7/load_crt0.s b/retail/bootloader/source/arm7/load_crt0.s index fb6bbfe58..777de9158 100644 --- a/retail/bootloader/source/arm7/load_crt0.s +++ b/retail/bootloader/source/arm7/load_crt0.s @@ -32,6 +32,7 @@ .global saveFileCluster .global romSize .global saveSize + .global cheatFileCluster .global patchOffsetCacheFileCluster .global fatTableFileCluster .global language @@ -76,6 +77,8 @@ romSize: .word 0x00000000 @ .nds file size saveSize: .word 0x00000000 @ .sav file size +cheatFileCluster: + .word 0x00000000 patchOffsetCacheFileCluster: .word 0x00000000 fatTableFileCluster: diff --git a/retail/bootloader/source/arm7/main.arm7.c b/retail/bootloader/source/arm7/main.arm7.c index 5286b611e..171552fe7 100644 --- a/retail/bootloader/source/arm7/main.arm7.c +++ b/retail/bootloader/source/arm7/main.arm7.c @@ -84,6 +84,7 @@ extern u32 initDisc; extern u32 saveFileCluster; extern u32 romSize; extern u32 saveSize; +extern u32 cheatFileCluster; extern u32 patchOffsetCacheFileCluster; extern u32 fatTableFileCluster; extern u32 language; @@ -845,6 +846,7 @@ int arm7_main(void) { ndsHeader, moduleParams, romFile->firstCluster, + cheatFileCluster, language, dsiModeConfirmed, ROMinRAM, diff --git a/retail/cardengine/arm7/source/card_engine_header.s b/retail/cardengine/arm7/source/card_engine_header.s index 1e49ccaf9..16d7dcd27 100644 --- a/retail/cardengine/arm7/source/card_engine_header.s +++ b/retail/cardengine/arm7/source/card_engine_header.s @@ -66,9 +66,7 @@ gameSoftReset: preciseVolumeControl: .word 0x00000000 cheat_data_offset: - .word cheat_data - patches_offset -cheat_data_len: - .word 0x00000000 + .word cheat_data @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ diff --git a/retail/common/include/cardengine_header_arm7.h b/retail/common/include/cardengine_header_arm7.h index b3b525a0d..d9b54e6c3 100644 --- a/retail/common/include/cardengine_header_arm7.h +++ b/retail/common/include/cardengine_header_arm7.h @@ -76,13 +76,8 @@ typedef struct cardengineArm7 { u32 romread_LED; u32 gameSoftReset; u32 preciseVolumeControl; - u32 cheat_data_offset; //u32* cheat_data; - u32 cheat_data_len; + u32* cheat_data_offset; //u32* cheat_data; } __attribute__ ((__packed__)) cardengineArm7; -inline u32* getCheatData(const cardengineArm7* ce7) { - return (u32*)((u32)ce7 + ce7->cheat_data_offset); -} - #endif // CARDENGINE_HEADER_ARM7_H diff --git a/retail/common/include/cheat_engine.h b/retail/common/include/cheat_engine.h index 150e69f02..c13d7b97b 100644 --- a/retail/common/include/cheat_engine.h +++ b/retail/common/include/cheat_engine.h @@ -4,17 +4,4 @@ #define CHEAT_DATA_MAX_SIZE (32 * 1024) // 32KiB #define CHEAT_DATA_MAX_LEN (CHEAT_DATA_MAX_SIZE / sizeof(u32)) -inline bool checkCheatDataLen(u32 cheat_data_len) { - return (cheat_data_len + 2 <= CHEAT_DATA_MAX_LEN); -} - -inline void endCheatData(u32* cheat_data, u32* cheat_data_len_ptr) { - if (*cheat_data_len_ptr + 2 > CHEAT_DATA_MAX_LEN) { // Not necessarily needed - return; - } - cheat_data[*cheat_data_len_ptr] = 0xCF000000; - cheat_data[*cheat_data_len_ptr + 1] = 0x00000000; - *cheat_data_len_ptr += 2; -} - #endif // CHEAT_ENGINE_H \ No newline at end of file diff --git a/retail/common/include/load_crt0.h b/retail/common/include/load_crt0.h index 217cdf5c0..2f706a239 100644 --- a/retail/common/include/load_crt0.h +++ b/retail/common/include/load_crt0.h @@ -17,6 +17,7 @@ typedef struct loadCrt0 { u32 saveFileCluster; u32 romSize; u32 saveSize; + u32 cheatFileCluster; u32 patchOffsetCacheFileCluster; u32 fatTableFileCluster; u32 language; //u8