From 0b70db4241e940945e4d306f41145a253135dc4e Mon Sep 17 00:00:00 2001 From: sanni Date: Thu, 16 Jun 2022 15:49:57 +0200 Subject: [PATCH] Add CRC32 database for SMS --- Cart_Reader/SMS.ino | 83 +++++++++++++++++++++++++++++++++++++++++++ Cart_Reader/options.h | 2 +- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/Cart_Reader/SMS.ino b/Cart_Reader/SMS.ino index e4b9480..0cea120 100644 --- a/Cart_Reader/SMS.ino +++ b/Cart_Reader/SMS.ino @@ -68,6 +68,7 @@ void _smsMenu() { // Change working dir to root sd.chdir("/"); readROM_SMS(); + compare_checksum_sms(); #ifdef global_log save_log(); #endif @@ -592,6 +593,88 @@ void readROM_SMS() { myFile.close(); } +inline uint32_t updateCRC_SMS(uint8_t ch, uint32_t crc) { + uint32_t idx = ((crc) ^ (ch)) & 0xff; + uint32_t tab_value = pgm_read_dword(crc_32_tab + idx); + return tab_value ^ ((crc) >> 8); +} + +// Calculate rom's CRC32 from SD +uint32_t crcSMS(char* fileName, char* folder) { + if (myFile.open(fileName, O_READ)) { + uint32_t oldcrc32 = 0xFFFFFFFF; + + for (unsigned long currByte = 0; currByte < (myFile.fileSize() / 512); currByte++) { + myFile.read(sdBuffer, 512); + for (int c = 0; c < 512; c++) { + oldcrc32 = updateCRC_SMS(sdBuffer[c], oldcrc32); + } + } + // Close the file: + myFile.close(); + return ~oldcrc32; + } + else { + print_Error(F("File not found"), true); + } +} + + +void compare_checksum_sms() { +#ifdef no-intro + //CRC32 + char crcStr[9]; + sprintf(crcStr, "%08lX", crcSMS(fileName, folder)); + // Print checksum + print_Msg("CRC32: "); + print_Msg(crcStr); + + //Search for CRC32 in file + char gamename[100]; + char crc_search[9]; + + //go to root + sd.chdir(); + if (myFile.open("sms.txt", O_READ)) { + //Search for same CRC in list + while (myFile.available()) { + //Read 2 lines (game name and CRC) + get_line(gamename, &myFile, 96); + get_line(crc_search, &myFile, 9); + skip_line(&myFile); //Skip every 3rd line + + //if checksum search successful, rename the file and end search + if (strcmp(crc_search, crcStr) == 0) + { + // Close the file: + myFile.close(); + + print_Msg(" -> "); + println_Msg(gamename); + + // Rename file to no-intro + sd.chdir(folder); + if (myFile.open(fileName, O_READ)) { + myFile.rename(gamename); + // Close the file: + myFile.close(); + } + break; + } + } + if (strcmp(crc_search, crcStr) != 0) + { + println_Msg(" -> Not found"); + } + } + else { + println_Msg(" -> sms.txt not found"); + } +#else + println_Msg(""); +#endif +} + // Read SRAM and save to the SD card void readSRAM_SMS() { // Get name, add extension and convert to char array for sd lib diff --git a/Cart_Reader/options.h b/Cart_Reader/options.h index 5861195..769b0a8 100644 --- a/Cart_Reader/options.h +++ b/Cart_Reader/options.h @@ -69,7 +69,7 @@ // #define clockgen_installed //****************************************** -// GB OPTIONS +// GB, SMS database options //****************************************** // Renames ROM if found in database (slow) // #define no-intro