Skip to content

Commit

Permalink
Load cheats from cheatData.bin
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed May 13, 2019
1 parent 2aeb4ba commit 5fe51b6
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 88 deletions.
2 changes: 0 additions & 2 deletions retail/arm9/include/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion retail/arm9/include/nds_loader_arm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
33 changes: 0 additions & 33 deletions retail/arm9/source/conf_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
13 changes: 9 additions & 4 deletions retail/arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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];
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
25 changes: 2 additions & 23 deletions retail/arm9/source/nds_loader_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);");
Expand Down
1 change: 1 addition & 0 deletions retail/bootloader/include/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions retail/bootloader/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
3 changes: 3 additions & 0 deletions retail/bootloader/source/arm7/load_crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
.global saveFileCluster
.global romSize
.global saveSize
.global cheatFileCluster
.global patchOffsetCacheFileCluster
.global fatTableFileCluster
.global language
Expand Down Expand Up @@ -76,6 +77,8 @@ romSize:
.word 0x00000000 @ .nds file size
saveSize:
.word 0x00000000 @ .sav file size
cheatFileCluster:
.word 0x00000000
patchOffsetCacheFileCluster:
.word 0x00000000
fatTableFileCluster:
Expand Down
2 changes: 2 additions & 0 deletions retail/bootloader/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -845,6 +846,7 @@ int arm7_main(void) {
ndsHeader,
moduleParams,
romFile->firstCluster,
cheatFileCluster,
language,
dsiModeConfirmed,
ROMinRAM,
Expand Down
4 changes: 1 addition & 3 deletions retail/cardengine/arm7/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ gameSoftReset:
preciseVolumeControl:
.word 0x00000000
cheat_data_offset:
.word cheat_data - patches_offset
cheat_data_len:
.word 0x00000000
.word cheat_data

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Expand Down
7 changes: 1 addition & 6 deletions retail/common/include/cardengine_header_arm7.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 0 additions & 13 deletions retail/common/include/cheat_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions retail/common/include/load_crt0.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct loadCrt0 {
u32 saveFileCluster;
u32 romSize;
u32 saveSize;
u32 cheatFileCluster;
u32 patchOffsetCacheFileCluster;
u32 fatTableFileCluster;
u32 language; //u8
Expand Down

4 comments on commit 5fe51b6

@JonArcherII
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does debug still need to be enabled for cheats to work?

@RocketRobz
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Apparently, enabling debug never made cheats work.

@ahezard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! It makes things simpler and fix bugs. Nicely done!

@RocketRobz
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Extra work needs to be done on TWLMenu++ side though.

Please sign in to comment.