From d4c5f6ec5b9e3cbb8d7a270398ed66f363d84b9c Mon Sep 17 00:00:00 2001 From: Vincent Pelletier Date: Sat, 29 Oct 2022 00:11:33 +0000 Subject: GBA.ino: Get rid of calcChecksumStr as a global Frees 150 bytes of program space and 5 bytes of global ram space. --- Cart_Reader/GBA.ino | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'Cart_Reader/GBA.ino') diff --git a/Cart_Reader/GBA.ino b/Cart_Reader/GBA.ino index 2163a01..a824096 100644 --- a/Cart_Reader/GBA.ino +++ b/Cart_Reader/GBA.ino @@ -6,7 +6,6 @@ /****************************************** Variables *****************************************/ -char calcChecksumStr[5]; boolean readType; /****************************************** @@ -569,6 +568,16 @@ void writeByte_GBA(unsigned long myAddress, byte myData) { /****************************************** GBA ROM Functions *****************************************/ +// Compute the checksum of rom header +// "header" must contain at least the rom's first 188 bytes +byte checksumHeader_GBA(const byte *header) { + byte result = 0x00; + for (byte n = 0xA0; n < 0xBD; n++) { + result -= header[n]; + } + return result - 0x19; +} + // Read info out of rom header void getCartInfo_GBA() { char saveTypeStr[14]; @@ -760,22 +769,21 @@ void getCartInfo_GBA() { // Get ROM version romVersion = sdBuffer[0xBC]; - // Get Checksum as string - sprintf(checksumStr, "%02X", sdBuffer[0xBD]); - // Calculate Checksum - int calcChecksum = 0x00; - for (int n = 0xA0; n < 0xBD; n++) { - calcChecksum -= sdBuffer[n]; - } - calcChecksum = (calcChecksum - 0x19) & 0xFF; - // Turn into string - sprintf(calcChecksumStr, "%02X", calcChecksum); + byte calcChecksum = checksumHeader_GBA(sdBuffer); + + // Convert checksum from header into string + // (used in compare_checksum_GBA... it should just exchange an integer + // instead) + sprintf(checksumStr, "%02X", sdBuffer[0xBD]); // Compare checksum - if (strcmp(calcChecksumStr, checksumStr) != 0) { + if (sdBuffer[0xBD] != calcChecksum) { + char calcChecksumStr[3]; display_Clear(); print_Msg(F("Result: ")); + // Turn into string + sprintf(calcChecksumStr, "%02X", calcChecksum); println_Msg(calcChecksumStr); print_Error(F("Checksum Error"), false); println_Msg(F("")); @@ -912,15 +920,9 @@ boolean compare_checksum_GBA() { myFile.read(sdBuffer, 512); myFile.close(); - // Calculate Checksum - int calcChecksum = 0x00; - for (int n = 0xA0; n < 0xBD; n++) { - calcChecksum -= sdBuffer[n]; - } - calcChecksum = (calcChecksum - 0x19) & 0xFF; - - // Turn into string - sprintf(calcChecksumStr, "%02X", calcChecksum); + // Calculate Checksum and turn into string + char calcChecksumStr[3]; + sprintf(calcChecksumStr, "%02X", checksumHeader_GBA(sdBuffer)); print_Msg(calcChecksumStr); if (strcmp(calcChecksumStr, checksumStr) == 0) { -- cgit v1.2.3