From 5580dffff0bea8a04e56448814ed2257c5abf4ae Mon Sep 17 00:00:00 2001 From: sanni Date: Thu, 7 Jul 2022 23:20:45 +0200 Subject: [PATCH] Fix adding iNES header when manually selecting mapper --- Cart_Reader/Cart_Reader.ino | 57 +++++++++++++++++++++++++++++++++++-- Cart_Reader/NES.ino | 25 ++++++---------- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index c023b0a..aeb515d 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -406,6 +406,23 @@ void draw_progressbar(uint32_t processedsize, uint32_t totalsize); byte eepbit[8]; byte eeptemp; +#ifdef no-intro +// Array to hold iNES header +byte iNES_HEADER[16]; +//ID 0-3 +//ROM_size 4 +//VROM_size 5 +//ROM_type 6 +//ROM_type2 7 +//ROM_type3 8 +//Upper_ROM_VROM_size 9 +//RAM_size 10 +//VRAM_size 11 +//TV_system 12 +//VS_hardware 13 +//reserved 14, 15 +#endif + //****************************************** // CRC32 //****************************************** @@ -583,9 +600,43 @@ boolean compareCRC(char* database, char* crcString, int offset) { //if checksum search successful, rename the file and end search if (strcmp(crc_search, crcStr) == 0) { +#ifdef enable_NES + if (mode == mode_NES) { + // Rewind to iNES Header + myFile.seekSet(myFile.curPosition() - 36); + + char iNES_STR[33]; + // Read iNES header + get_line(iNES_STR, &myFile, 33); + + // Convert "4E4553" to (0x4E, 0x45, 0x53) + byte iNES_BUF[2]; + for (byte j = 0; j < 16; j++) { + sscanf(iNES_STR + j * 2, "%2X", iNES_BUF); + iNES_HEADER[j] = iNES_BUF[0]; + } + //Skip CRLF + myFile.seekSet(myFile.curPosition() + 4); + } +#endif + // Close the file: myFile.close(); + //Write iNES header +#ifdef enable_NES + if (mode == mode_NES) { + // Write iNES header + sd.chdir(folder); + if (!myFile.open(fileName, O_RDWR)) { + print_Error(F("SD Error"), true); + } + for (byte z = 0; z < 16; z++) { + myFile.write(iNES_HEADER[z]); + } + myFile.close(); + } +#endif print_Msg(F(" -> ")); println_Msg(gamename); @@ -675,6 +726,7 @@ void mainMenu() { #ifdef enable_NES case 1: + mode = mode_NES; display_Clear(); display_Update(); setup_NES(); @@ -684,7 +736,6 @@ void mainMenu() { checkStatus_NES(0); #endif nesMenu(); - mode = mode_NES; break; #endif @@ -834,6 +885,7 @@ void addonsMenu() { { #ifdef enable_NES case 0: + mode = mode_NES; display_Clear(); display_Update(); setup_NES(); @@ -843,7 +895,6 @@ void addonsMenu() { checkStatus_NES(0); #endif nesMenu(); - mode = mode_NES; break; #endif @@ -1467,7 +1518,7 @@ void convertPgm(const char* const pgmOptions[], byte numArrays) { } } -void print_Error(const __FlashStringHelper *errorMessage, boolean forceReset) { +void print_Error(const __FlashStringHelper * errorMessage, boolean forceReset) { errorLvl = 1; setColor_RGB(255, 0, 0); println_Msg(errorMessage); diff --git a/Cart_Reader/NES.ino b/Cart_Reader/NES.ino index 16421fb..f04ef27 100644 --- a/Cart_Reader/NES.ino +++ b/Cart_Reader/NES.ino @@ -375,21 +375,6 @@ void setup_NES() { Get Mapping from no-intro SD database *****************************************/ #ifdef no-intro -// Array to hold iNES header -byte iNES_HEADER[16]; -//ID 0-3 -//ROM_size 4 -//VROM_size 5 -//ROM_type 6 -//ROM_type2 7 -//ROM_type3 8 -//Upper_ROM_VROM_size 9 -//RAM_size 10 -//VRAM_size 11 -//TV_system 12 -//VS_hardware 13 -//reserved 14, 15 - // no clue (taken from fceux) uint32_t uppow2(uint32_t n) { int x; @@ -426,6 +411,10 @@ boolean getMapping() { // Filter out 0xFF checksum if (strcmp(crcStr, "BD7BC39F") == 0) { + romName[0] = 'C'; + romName[1] = 'A'; + romName[2] = 'R'; + romName[3] = 'T'; return 0; } else { @@ -451,7 +440,7 @@ boolean getMapping() { // Skip over semicolon myFile.seekSet(myFile.curPosition() + 1); - // Read CRC32 of first 16 bytes + // Read CRC32 of first 512 bytes sprintf(crc_search, "%c", myFile.read()); for (byte i = 0; i < 7; i++) { sprintf(tempStr2, "%c", myFile.read()); @@ -565,6 +554,10 @@ boolean getMapping() { // File searched until end but nothing found if (strcmp(crc_search, crcStr) != 0) { println_Msg(F("Not found")); + romName[0] = 'C'; + romName[1] = 'A'; + romName[2] = 'R'; + romName[3] = 'T'; return 0; } }