aboutsummaryrefslogtreecommitdiffhomepage
path: root/Cart_Reader/GBA.ino
diff options
context:
space:
mode:
authorVincent Pelletier <[email protected]>2022-10-29 00:11:33 +0000
committerVincent Pelletier <[email protected]>2022-10-29 07:16:37 +0000
commitd4c5f6ec5b9e3cbb8d7a270398ed66f363d84b9c (patch)
treecaeb96f2e6b2cd3c8a43ce1973c012867302cd58 /Cart_Reader/GBA.ino
parent3bb63d77e370e528078bf8fd7908115768741b54 (diff)
downloadcartreader-d4c5f6ec5b9e3cbb8d7a270398ed66f363d84b9c.tar.gz
cartreader-d4c5f6ec5b9e3cbb8d7a270398ed66f363d84b9c.zip
GBA.ino: Get rid of calcChecksumStr as a global
Frees 150 bytes of program space and 5 bytes of global ram space.
Diffstat (limited to 'Cart_Reader/GBA.ino')
-rw-r--r--Cart_Reader/GBA.ino44
1 files changed, 23 insertions, 21 deletions
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) {