From 34e7f90d706c8e5865e17b7cd9a7c0ca401e6ded Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Fri, 2 Jul 2021 08:11:44 +0200 Subject: [PATCH 1/6] Extracts user interaction from writeSRAM_GB() and writeFlash_GB() to allow for more flexibility --- Cart_Reader/GB.ino | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index b9f9be8..032444b 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -115,6 +115,8 @@ void gbMenu() { if (sramEndAddress > 0) { // Change working dir to root sd.chdir("/"); + filePath[0] = '\0'; + fileBrowser(F("Select sav file")); writeSRAM_GB(); unsigned long wrErrors; wrErrors = verifySRAM_GB(); @@ -138,6 +140,11 @@ void gbMenu() { // Change working dir to root sd.chdir("/"); //MBC3 + // Launch filebrowser + filePath[0] = '\0'; + sd.chdir("/"); + fileBrowser(F("Select file")); + display_Clear(); writeFlash_GB(3); // Reset wait(); @@ -148,6 +155,11 @@ void gbMenu() { // Change working dir to root sd.chdir("/"); //MBC5 + // Launch filebrowser + filePath[0] = '\0'; + sd.chdir("/"); + fileBrowser(F("Select file")); + display_Clear(); writeFlash_GB(5); // Reset wait(); @@ -652,10 +664,6 @@ void readSRAM_GB() { void writeSRAM_GB() { // Does cartridge have SRAM if (sramEndAddress > 0) { - - filePath[0] = '\0'; - sd.chdir("/"); - fileBrowser(F("Select sav file")); // Create filepath sprintf(filePath, "%s/%s", filePath, fileName); @@ -766,12 +774,6 @@ unsigned long verifySRAM_GB() { // A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks // A14-A21 connected to MBC5 void writeFlash_GB(byte MBC) { - // Launch filebrowser - filePath[0] = '\0'; - sd.chdir("/"); - fileBrowser(F("Select file")); - display_Clear(); - // Create filepath sprintf(filePath, "%s/%s", filePath, fileName); From f554adccce575fc02b0f7ea1d22714651691bd81 Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Fri, 2 Jul 2021 09:36:24 +0200 Subject: [PATCH 2/6] Combines "Flash MBC3" and "Flash MBC5"; adds "Flash cart and write latest save" --- Cart_Reader/GB.ino | 187 +++++++++++++++++++++++---------------------- 1 file changed, 96 insertions(+), 91 deletions(-) diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index 032444b..26b05db 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -28,8 +28,8 @@ static const char* const menuOptionsGBx[] PROGMEM = {gbxMenuItem1, gbxMenuItem2, static const char GBMenuItem1[] PROGMEM = "Read Rom"; static const char GBMenuItem2[] PROGMEM = "Read Save"; static const char GBMenuItem3[] PROGMEM = "Write Save"; -static const char GBMenuItem4[] PROGMEM = "Flash MBC3 cart"; -static const char GBMenuItem5[] PROGMEM = "Flash MBC5 cart"; +static const char GBMenuItem4[] PROGMEM = "Flash Cart"; +static const char GBMenuItem5[] PROGMEM = "Flash Cart and Save"; static const char GBMenuItem6[] PROGMEM = "Reset"; static const char* const menuOptionsGB[] PROGMEM = {GBMenuItem1, GBMenuItem2, GBMenuItem3, GBMenuItem4, GBMenuItem5, GBMenuItem6}; @@ -145,7 +145,7 @@ void gbMenu() { sd.chdir("/"); fileBrowser(F("Select file")); display_Clear(); - writeFlash_GB(3); + writeFlash_GB(); // Reset wait(); resetArduino(); @@ -160,7 +160,50 @@ void gbMenu() { sd.chdir("/"); fileBrowser(F("Select file")); display_Clear(); - writeFlash_GB(5); + writeFlash_GB(); + getCartInfo_GB(); + // Does cartridge have SRAM + if (sramEndAddress > 0) { + // Remove file name ending + int pos=-1; + while (fileName[++pos]!='\0') { + if (fileName[pos]=='.') { + fileName[pos]='\0'; + break; + } + } + sprintf(filePath, "/GB/SAVE/%s/", fileName); + if (sd.exists(filePath)) { + println_Msg(F("Save folder found")); + EEPROM_readAnything(0, foldern); + for (int i=foldern;i>=0;i--) { + sprintf(filePath, "/GB/SAVE/%s/%d/%s.SAV", fileName, i, fileName); + if (sd.exists(filePath)) { + print_Msg(F("Save slot ")); + print_Msg(i); + println_Msg(F(" found.")); + sprintf(filePath, "/GB/SAVE/%s/%d", fileName, i); + sprintf(fileName, "%s.SAV", fileName); + writeSRAM_GB(); + unsigned long wrErrors; + wrErrors = verifySRAM_GB(); + if (wrErrors == 0) { + println_Msg(F("Verified OK")); + display_Update(); + } + else { + print_Msg(F("Error: ")); + print_Msg(wrErrors); + println_Msg(F(" bytes ")); + print_Error(F("did not verify."), false); + } + } + } + } + } + else { + print_Error(F("Cart has no Sram"), false); + } // Reset wait(); resetArduino(); @@ -773,7 +816,7 @@ unsigned long verifySRAM_GB() { // Write 29F032 flashrom // A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks // A14-A21 connected to MBC5 -void writeFlash_GB(byte MBC) { +void writeFlash_GB() { // Create filepath sprintf(filePath, "%s/%s", filePath, fileName); @@ -851,11 +894,20 @@ void writeFlash_GB(byte MBC) { display_Update(); } else { + print_Msg(F("Flash ID: ")); + println_Msg(flashid); + println_Msg(F("S29GL032M90TCIR4")); + print_Msg(F("Banks: ")); + print_Msg(romBanks); + println_Msg(F("/256")); + display_Update(); + } + /*else { print_Msg(F("Flash ID: ")); println_Msg(flashid); display_Update(); print_Error(F("Unknown flashrom"), true); - } + }*/ dataOut(); // Reset flash @@ -916,104 +968,57 @@ void writeFlash_GB(byte MBC) { } } - if (MBC == 3) { - println_Msg(F("Writing flash MBC3")); - display_Update(); + println_Msg(F("Writing flash MBC3/MBC5")); + display_Update(); - // Write flash - dataOut(); + // Write flash + dataOut(); - uint16_t currAddr = 0; - uint16_t endAddr = 0x3FFF; + uint16_t currAddr = 0; + uint16_t endAddr = 0x3FFF; - for (int currBank = 0; currBank < romBanks; currBank++) { - // Blink led - PORTB ^= (1 << 4); + for (int currBank = 0; currBank < romBanks; currBank++) { + // Blink led + PORTB ^= (1 << 4); - // Set ROM bank - writeByte_GB(0x2100, currBank); + // Set ROM bank + writeByte_GB(0x2100, currBank); + // 0x2A8000 fix + writeByte_GB(0x4000, 0x0); - if (currBank > 0) { - currAddr = 0x4000; - endAddr = 0x7FFF; - } - - while (currAddr <= endAddr) { - myFile.read(sdBuffer, 512); - - for (int currByte = 0; currByte < 512; currByte++) { - // Write command sequence - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0xa0); - // Write current byte - writeByte_GB(currAddr + currByte, sdBuffer[currByte]); - - // Set data pins to input - dataIn(); - - // Setting CS(PH3) and OE/RD(PH6) LOW - PORTH &= ~((1 << 3) | (1 << 6)); - - // Busy check - while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) { - } - - // Switch CS(PH3) and OE/RD(PH6) to HIGH - PORTH |= (1 << 3) | (1 << 6); - - // Set data pins to output - dataOut(); - } - currAddr += 512; - } + if (currBank > 0) { + currAddr = 0x4000; + endAddr = 0x7FFF; } - } - else if (MBC == 5) { - println_Msg(F("Writing flash MBC5")); - display_Update(); + while (currAddr <= endAddr) { + myFile.read(sdBuffer, 512); - // Write flash - dataOut(); + for (int currByte = 0; currByte < 512; currByte++) { + // Write command sequence + writeByte_GB(0x555, 0xaa); + writeByte_GB(0x2aa, 0x55); + writeByte_GB(0x555, 0xa0); + // Write current byte + writeByte_GB(currAddr + currByte, sdBuffer[currByte]); - for (int currBank = 0; currBank < romBanks; currBank++) { - // Blink led - PORTB ^= (1 << 4); + // Set data pins to input + dataIn(); - // Set ROM bank - writeByte_GB(0x2000, currBank); - // 0x2A8000 fix - writeByte_GB(0x4000, 0x0); + // Setting CS(PH3) and OE/RD(PH6) LOW + PORTH &= ~((1 << 3) | (1 << 6)); - for (unsigned int currAddr = 0x4000; currAddr < 0x7FFF; currAddr += 512) { - myFile.read(sdBuffer, 512); - - for (int currByte = 0; currByte < 512; currByte++) { - // Write command sequence - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0xa0); - // Write current byte - writeByte_GB(currAddr + currByte, sdBuffer[currByte]); - - // Set data pins to input - dataIn(); - - // Setting CS(PH3) and OE/RD(PH6) LOW - PORTH &= ~((1 << 3) | (1 << 6)); - - // Busy check - while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) { - } - - // Switch CS(PH3) and OE/RD(PH6) to HIGH - PORTH |= (1 << 3) | (1 << 6); - - // Set data pins to output - dataOut(); + // Busy check + while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) { } + + // Switch CS(PH3) and OE/RD(PH6) to HIGH + PORTH |= (1 << 3) | (1 << 6); + + // Set data pins to output + dataOut(); } + currAddr += 512; } } From 233b89fc91ebcfbeeb1e44d830c854b60b911613 Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Fri, 2 Jul 2021 14:18:56 +0200 Subject: [PATCH 3/6] Now reports when no save has been found on "Flash ROM and save" (cherry picked from commit 9cf556d981b218c1bd7f56b30cc6afada593a0cf) --- Cart_Reader/GB.ino | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index 26b05db..b212506 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -173,15 +173,16 @@ void gbMenu() { } } sprintf(filePath, "/GB/SAVE/%s/", fileName); + bool saveFound=false; if (sd.exists(filePath)) { - println_Msg(F("Save folder found")); EEPROM_readAnything(0, foldern); for (int i=foldern;i>=0;i--) { sprintf(filePath, "/GB/SAVE/%s/%d/%s.SAV", fileName, i, fileName); if (sd.exists(filePath)) { - print_Msg(F("Save slot ")); + print_Msg(F("Save number ")); print_Msg(i); println_Msg(F(" found.")); + saveFound=true; sprintf(filePath, "/GB/SAVE/%s/%d", fileName, i); sprintf(fileName, "%s.SAV", fileName); writeSRAM_GB(); @@ -197,9 +198,13 @@ void gbMenu() { println_Msg(F(" bytes ")); print_Error(F("did not verify."), false); } + break; } } } + if (!saveFound) { + println_Msg(F("Error: No save found.")); + } } else { print_Error(F("Cart has no Sram"), false); From 16af53b613b4714a0607edcfa7750ab5eb28be27 Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Wed, 7 Jul 2021 13:48:16 +0200 Subject: [PATCH 4/6] Possible fix for flashing MBC5. Shouldn't affect MBC3. --- Cart_Reader/GB.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index b212506..bc3c2b6 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -987,7 +987,8 @@ void writeFlash_GB() { PORTB ^= (1 << 4); // Set ROM bank - writeByte_GB(0x2100, currBank); + writeByte_GB(0x2100, currBank); //MBC5 + writeByte_GB(0x2000, currBank); //MBC3 // 0x2A8000 fix writeByte_GB(0x4000, 0x0); From 12b3c7ebe0512d466308f6ff25803d2ede92774d Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Wed, 21 Jul 2021 22:46:34 +0200 Subject: [PATCH 5/6] Fixes flashing MBC3; flash device id detection replaced by feature detection, adding compatibility for many repro flash carts. --- Cart_Reader/Cart_Reader.ino | 3 + Cart_Reader/GB.ino | 232 ++++++++++++++++++++++++------------ 2 files changed, 158 insertions(+), 77 deletions(-) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index be25732..8f7fe1c 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -219,6 +219,9 @@ char flashid[5]; char vendorID[5]; unsigned long fileSize; unsigned long sramBase; +unsigned long flashBanks; +bool flashX16Mode; +bool flashSwitchLastBits; // Variable to count errors unsigned long writeErrors; diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index bc3c2b6..ed264d7 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -145,7 +145,14 @@ void gbMenu() { sd.chdir("/"); fileBrowser(F("Select file")); display_Clear(); - writeFlash_GB(); + identifyFlash_GB(); + if (!writeFlash_GB()) { + display_Clear(); + println_Msg(F("Flashing failed, time out!")); + println_Msg(F("Press button...")); + display_Update(); + wait(); + } // Reset wait(); resetArduino(); @@ -160,7 +167,15 @@ void gbMenu() { sd.chdir("/"); fileBrowser(F("Select file")); display_Clear(); - writeFlash_GB(); + identifyFlash_GB(); + if (!writeFlash_GB()) { + display_Clear(); + println_Msg(F("Flashing failed, time out!")); + println_Msg(F("Press button...")); + display_Update(); + wait(); + resetArduino(); + } getCartInfo_GB(); // Does cartridge have SRAM if (sramEndAddress > 0) { @@ -818,10 +833,120 @@ unsigned long verifySRAM_GB() { } } +/* + * Flash chips can either be in x8 mode or x16 mode and sometimes the two + * least significant bits on flash cartridges' data lines are swapped. + * This function reads a byte and compensates for the differences. + * This is only necessary for commands to the flash, not for data read from the flash, the MBC or SRAM. + * + * address needs to be the x8 mode address of the flash register that should be read. +*/ +byte readByteCompensated(int address) { + byte data = readByte_GB(address >> (flashX16Mode ? 1 : 0)); + if (flashSwitchLastBits) { + return (data & 0b11111100) | ((data<<1) & 0b10) | ((data>>1) & 0b01); + } + return data; +} + +/* + * Flash chips can either be in x8 mode or x16 mode and sometimes the two + * least significant bits on flash cartridges' data lines are swapped. + * This function writes a byte and compensates for the differences. + * This is only necessary for commands to the flash, not for data written to the flash, the MBC or SRAM. + * . + * address needs to be the x8 mode address of the flash register that should be read. +*/ +byte writeByteCompensated(int address, byte data) { + if (flashSwitchLastBits) { + data = (data & 0b11111100) | ((data<<1) & 0b10) | ((data>>1) & 0b01); + } + writeByte_GB(address >> (flashX16Mode ? 1 : 0), data); +} + +void startCFIMode(boolean x16Mode) { + if (x16Mode) { + writeByte_GB(0x555, 0xf0); //x16 mode reset command + delay(500); + writeByte_GB(0x555, 0xf0); //Double reset to get out of possible Autoselect + CFI mode + delay(500); + writeByte_GB(0x55, 0x98); //x16 CFI Query command + } else { + writeByte_GB(0xAAA, 0xf0); //x8 mode reset command + delay(100); + writeByte_GB(0xAAA, 0xf0); //Double reset to get out of possible Autoselect + CFI mode + delay(100); + writeByte_GB(0xAA, 0x98); //x8 CFI Query command + } +} + +/* Identify the different flash chips. + * Sets the global variables flashBanks, flashX16Mode and flashSwitchLastBits +*/ +void identifyFlash_GB() { + // Reset flash + display_Clear(); + dataOut(); + writeByte_GB(0x6000, 0); // Set ROM Mode + writeByte_GB(0x2000, 0); // Set Bank to 0 + writeByte_GB(0x3000, 0); + + startCFIMode(false); // Trying x8 mode first + + dataIn_GB(); + display_Clear(); + // Try x8 mode first + char cfiQRYx8[7]; + char cfiQRYx16[7]; + sprintf(cfiQRYx8, "%02X%02X%02X", readByte_GB(0x20), readByte_GB(0x22), readByte_GB(0x24)); + sprintf(cfiQRYx16, "%02X%02X%02X", readByte_GB(0x10), readByte_GB(0x11), readByte_GB(0x12)); // some devices use x8-style CFI Query command even though they are in x16 command mode + if (strcmp(cfiQRYx8, "515259")==0) { // QRY in x8 mode + println_Msg(F("Normal CFI x8 Mode")); + flashX16Mode = false; + flashSwitchLastBits = false; + } else if (strcmp(cfiQRYx8, "52515A")==0) { // QRY in x8 mode with switched last bit + println_Msg(F("Switched CFI x8 Mode")); + flashX16Mode = false; + flashSwitchLastBits = true; + } else if (strcmp(cfiQRYx16, "515259")==0) { // QRY in x16 mode + println_Msg(F("Normal CFI x16 Mode")); + flashX16Mode = true; + flashSwitchLastBits = false; + } else if (strcmp(cfiQRYx16, "52515A")==0) { // QRY in x16 mode with switched last bit + println_Msg(F("Switched CFI x16 Mode")); + flashX16Mode = true; + flashSwitchLastBits = true; + } else { + startCFIMode(true); // Try x16 mode next + sprintf(cfiQRYx16, "%02X%02X%02X", readByte_GB(0x10), readByte_GB(0x11), readByte_GB(0x12)); + if (strcmp(cfiQRYx16, "515259")==0) { // QRY in x16 mode + println_Msg(F("Normal CFI x16 Mode")); + flashX16Mode = true; + flashSwitchLastBits = false; + } else if (strcmp(cfiQRYx16, "52515A")==0) { // QRY in x16 mode with switched last bit + println_Msg(F("Switched CFI x16 Mode")); + flashX16Mode = true; + flashSwitchLastBits = true; + } else { + println_Msg(F("CFI Query failed!")); + display_Update(); + wait(); + return 0; + } + } + dataIn_GB(); + flashBanks = 1<<(readByteCompensated(0x4E) - 14);// - flashX16Mode); + dataOut(); + + // Reset flash + writeByteCompensated(0xAAA, 0xf0); + delay(100); +} + // Write 29F032 flashrom // A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks // A14-A21 connected to MBC5 -void writeFlash_GB() { +bool writeFlash_GB() { // Create filepath sprintf(filePath, "%s/%s", filePath, fileName); @@ -850,69 +975,8 @@ void writeFlash_GB() { delay(100); // Reset flash - writeByte_GB(0x555, 0xf0); + writeByteCompensated(0xAAA, 0xf0); delay(100); - - // ID command sequence - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0x90); - - dataIn_GB(); - - // Read the two id bytes into a string - sprintf(flashid, "%02X%02X", readByte_GB(0), readByte_GB(1)); - - if (strcmp(flashid, "04D4") == 0) { - println_Msg(F("MBM29F033C")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/256")); - display_Update(); - } - else if (strcmp(flashid, "0141") == 0) { - println_Msg(F("AM29F032B")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/256")); - display_Update(); - } - else if (strcmp(flashid, "01AD") == 0) { - println_Msg(F("AM29F016B")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/128")); - display_Update(); - } - else if (strcmp(flashid, "04AD") == 0) { - println_Msg(F("AM29F016D")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/128")); - display_Update(); - } - else if (strcmp(flashid, "01D5") == 0) { - println_Msg(F("AM29F080B")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/64")); - display_Update(); - } - else { - print_Msg(F("Flash ID: ")); - println_Msg(flashid); - println_Msg(F("S29GL032M90TCIR4")); - print_Msg(F("Banks: ")); - print_Msg(romBanks); - println_Msg(F("/256")); - display_Update(); - } - /*else { - print_Msg(F("Flash ID: ")); - println_Msg(flashid); - display_Update(); - print_Error(F("Unknown flashrom"), true); - }*/ dataOut(); // Reset flash @@ -923,12 +987,12 @@ void writeFlash_GB() { display_Update(); // Erase flash - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0x80); - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0x10); + writeByteCompensated(0xAAA, 0xaa); + writeByteCompensated(0x555, 0x55); + writeByteCompensated(0xAAA, 0x80); + writeByteCompensated(0xAAA, 0xaa); + writeByteCompensated(0x555, 0x55); + writeByteCompensated(0xAAA, 0x10); dataIn_GB(); @@ -987,8 +1051,7 @@ void writeFlash_GB() { PORTB ^= (1 << 4); // Set ROM bank - writeByte_GB(0x2100, currBank); //MBC5 - writeByte_GB(0x2000, currBank); //MBC3 + writeByte_GB(0x2100, currBank); // 0x2A8000 fix writeByte_GB(0x4000, 0x0); @@ -1002,9 +1065,10 @@ void writeFlash_GB() { for (int currByte = 0; currByte < 512; currByte++) { // Write command sequence - writeByte_GB(0x555, 0xaa); - writeByte_GB(0x2aa, 0x55); - writeByte_GB(0x555, 0xa0); + writeByteCompensated(0xAAA, 0xaa); + writeByteCompensated(0x555, 0x55); + writeByteCompensated(0xAAA, 0xa0); + // Write current byte writeByte_GB(currAddr + currByte, sdBuffer[currByte]); @@ -1015,7 +1079,19 @@ void writeFlash_GB() { PORTH &= ~((1 << 3) | (1 << 6)); // Busy check + short i=0; while ((PINC & 0x80) != (sdBuffer[currByte] & 0x80)) { + i++; + if (i>500) { + if (currAddr<0x4000) { // This happens when trying to flash an MBC5 as if it was an MBC3. Retry to flash as MBC5, starting from last successfull byte. + currByte--; + currAddr+=0x4000; + endAddr = 0x7FFF; + break; + } else { // If a timeout happens while trying to flash MBC5-style, flashing failed. + return false; + } + } } // Switch CS(PH3) and OE/RD(PH6) to HIGH @@ -1031,6 +1107,7 @@ void writeFlash_GB() { // Set data pins to input again dataIn_GB(); + display_Clear(); println_Msg(F("Verifying")); display_Update(); @@ -1096,10 +1173,11 @@ void writeFlash_GB() { println_Msg(F("Can't open file")); display_Update(); } + return true; } #endif //****************************************** // End of File -//****************************************** +//****************************************** \ No newline at end of file From 78d6e9dde33583e03626aa88395e6a0a0fdc79aa Mon Sep 17 00:00:00 2001 From: Dakkaron Date: Wed, 21 Jul 2021 23:02:45 +0200 Subject: [PATCH 6/6] Adds romBanks <= flashBanks check --- Cart_Reader/GB.ino | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Cart_Reader/GB.ino b/Cart_Reader/GB.ino index ed264d7..7d83d7f 100644 --- a/Cart_Reader/GB.ino +++ b/Cart_Reader/GB.ino @@ -946,6 +946,7 @@ void identifyFlash_GB() { // Write 29F032 flashrom // A0-A13 directly connected to cart edge -> 16384(0x0-0x3FFF) bytes per bank -> 256(0x0-0xFF) banks // A14-A21 connected to MBC5 +// identifyFlash_GB() needs to be run before this! bool writeFlash_GB() { // Create filepath sprintf(filePath, "%s/%s", filePath, fileName); @@ -965,6 +966,27 @@ bool writeFlash_GB() { romBanks = 2 << romSize; } + if (romBanks<=flashBanks) { + print_Msg(F("Using ")); + print_Msg(romBanks); + print_Msg(F("/")); + print_Msg(flashBanks); + println_Msg(F(" Banks")); + display_Update(); + } else { + println_Msg(F("Error: Flash has too few banks!")); + print_Msg(F("Has ")); + print_Msg(flashBanks); + println_Msg(F(" banks,") + print_Msg(F("but needs ")); + print_Msg(romBanks); + println_Msg(F(".")); + println_Msg(F("Press button...")); + display_Update(); + wait(); + resetArduino(); + } + // Set data pins to output dataOut(); @@ -1037,7 +1059,7 @@ bool writeFlash_GB() { } } - println_Msg(F("Writing flash MBC3/MBC5")); + println_Msg(F("Writing flash MBC3/5")); display_Update(); // Write flash