Add support for the Game Boy MBC6 mapper (save write)

This commit is contained in:
Lesserkuma 2022-09-28 18:52:52 +02:00
parent f7d9051b95
commit 9d42ce0964

View File

@ -310,7 +310,7 @@ void gbMenu() {
// Change working dir to root // Change working dir to root
sd.chdir("/"); sd.chdir("/");
if (romType == 32) if (romType == 32)
readSRAMFLASH_GB_MBC6(); readSRAMFLASH_MBC6_GB();
else else
readSRAM_GB(); readSRAM_GB();
} }
@ -328,6 +328,11 @@ void gbMenu() {
sd.chdir("/"); sd.chdir("/");
filePath[0] = '\0'; filePath[0] = '\0';
fileBrowser(F("Select sav file")); fileBrowser(F("Select sav file"));
if (romType == 32) {
writeSRAMFLASH_MBC6_GB();
}
else {
writeSRAM_GB(); writeSRAM_GB();
unsigned long wrErrors; unsigned long wrErrors;
wrErrors = verifySRAM_GB(); wrErrors = verifySRAM_GB();
@ -342,6 +347,7 @@ void gbMenu() {
print_Error(F("did not verify."), false); print_Error(F("did not verify."), false);
} }
} }
}
else { else {
print_Error(F("No save or unsupported type"), false); print_Error(F("No save or unsupported type"), false);
} }
@ -1268,8 +1274,8 @@ unsigned long verifySRAM_GB() {
} }
} }
// Read SRAM + FLASH save data of MBC6 // Read MBC6 (SRAM + FLASH) save data
void readSRAMFLASH_GB_MBC6() { void readSRAMFLASH_MBC6_GB() {
// Get name, add extension and convert to char array for sd lib // Get name, add extension and convert to char array for sd lib
strcpy(fileName, romName); strcpy(fileName, romName);
strcat(fileName, ".sav"); strcat(fileName, ".sav");
@ -1329,14 +1335,13 @@ void readSRAMFLASH_GB_MBC6() {
writeByte_GB(0x2800, 0x08); writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08); writeByte_GB(0x3800, 0x08);
word romAddress = 0x4000; // Switch FLASH banks
for (byte currBank = 0; currBank < 128; currBank++) { for (byte currBank = 0; currBank < 128; currBank++) {
writeByte_GB(0x2800, 8); word romAddress = 0x4000;
writeByte_GB(0x3800, 8);
writeByte_GB(0x2000, currBank); writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank); writeByte_GB(0x3000, currBank);
romAddress = 0x4000;
// Read banks and save to SD // Read banks and save to SD
while (romAddress <= 0x5FFF) { while (romAddress <= 0x5FFF) {
for (int i = 0; i < 512; i++) { for (int i = 0; i < 512; i++) {
@ -1364,32 +1369,16 @@ void readSRAMFLASH_GB_MBC6() {
display_Update(); display_Update();
} }
// Read SRAM + FLASH save data of MBC6 // Write MBC6 (SRAM + FLASH) save data
void readSRAMFLASH_GB_MBC6() { void writeSRAMFLASH_MBC6_GB() {
// Get name, add extension and convert to char array for sd lib // Create filepath
strcpy(fileName, romName); sprintf(filePath, "%s/%s", filePath, fileName);
strcat(fileName, ".sav");
// create a new folder for the save file
EEPROM_readAnything(0, foldern);
sprintf(folder, "GB/SAVE/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
display_Clear();
print_Msg(F("Saving to "));
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// write new folder number back to eeprom
foldern = foldern + 1;
EEPROM_writeAnything(0, foldern);
//open file on sd card //open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) { if (myFile.open(filePath, O_READ)) {
print_Error(F("SD Error"), true); display_Clear();
} println_Msg(F("Writing MBC6 save..."));
display_Update();
//Initialize progress bar //Initialize progress bar
uint32_t processedProgressBar = 0; uint32_t processedProgressBar = 0;
@ -1404,16 +1393,14 @@ void readSRAMFLASH_GB_MBC6() {
writeByte_GB(0x0400, currBank); writeByte_GB(0x0400, currBank);
writeByte_GB(0x0800, currBank); writeByte_GB(0x0800, currBank);
// Read SRAM // Write SRAM
for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress += 64) { for (word sramAddress = 0xA000; sramAddress <= lastByte; sramAddress++) {
for (byte i = 0; i < 64; i++) { writeByteSRAM_GB(sramAddress, myFile.read());
sdBuffer[i] = readByteSRAM_GB(sramAddress + i);
} }
myFile.write(sdBuffer, 64);
processedProgressBar += 64; processedProgressBar += (lastByte + 1) - 0xA000;
draw_progressbar(processedProgressBar, totalProgressBar); draw_progressbar(processedProgressBar, totalProgressBar);
} }
}
// Disable SRAM // Disable SRAM
writeByte_GB(0x0000, 0x00); writeByte_GB(0x0000, 0x00);
@ -1421,26 +1408,88 @@ void readSRAMFLASH_GB_MBC6() {
// Enable flash save memory (map to ROM) // Enable flash save memory (map to ROM)
writeByte_GB(0x1000, 0x01); writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x01); writeByte_GB(0x0C00, 0x01);
writeByte_GB(0x1000, 0x00); writeByte_GB(0x1000, 0x01);
writeByte_GB(0x2800, 0x08); writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08); writeByte_GB(0x3800, 0x08);
word romAddress = 0x4000;
for (byte currBank = 0; currBank < 128; currBank++) { for (byte currBank = 0; currBank < 128; currBank++) {
writeByte_GB(0x2800, 8); word romAddress = 0x4000;
writeByte_GB(0x3800, 8);
// Erase FLASH sector
if (((processedProgressBar - 0x8000) % 0x20000) == 0) {
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, 0x01);
writeByte_GB(0x3000, 0x02);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7555, 0x80);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank); writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank); writeByte_GB(0x3000, currBank);
writeByte_GB(0x4000, 0x30);
romAddress = 0x4000; byte lives = 100;
// Read banks and save to SD while (1) {
while (romAddress <= 0x5FFF) { byte sr = readByte_GB(0x4000);
for (int i = 0; i < 512; i++) { if (sr == 0x80) break;
sdBuffer[i] = readByte_GB(romAddress + i); delay(1);
if (lives-- <= 0) {
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
myFile.close();
display_Clear();
print_Error(F("Error erasing FLASH sector."), true);
} }
myFile.write(sdBuffer, 512); }
romAddress += 512; }
processedProgressBar += 512; else {
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
}
// Write to FLASH
while (romAddress <= 0x5FFF) {
writeByte_GB(0x2000, 0x01);
writeByte_GB(0x3000, 0x02);
writeByte_GB(0x7555, 0xAA);
writeByte_GB(0x4AAA, 0x55);
writeByte_GB(0x7555, 0xA0);
writeByte_GB(0x2800, 0x08);
writeByte_GB(0x3800, 0x08);
writeByte_GB(0x2000, currBank);
writeByte_GB(0x3000, currBank);
for (int i = 0; i < 128; i++) {
writeByte_GB(romAddress++, myFile.read());
}
writeByte_GB(romAddress - 1, 0x00);
byte lives = 100;
while (1) {
byte sr = readByte_GB(romAddress - 1);
if (sr == 0x80) break;
delay(1);
if (lives-- <= 0) {
// Disable flash save memory
writeByte_GB(0x1000, 0x01);
writeByte_GB(0x0C00, 0x00);
writeByte_GB(0x1000, 0x00);
writeByte_GB(0x2800, 0x00);
writeByte_GB(0x3800, 0x00);
myFile.close();
display_Clear();
print_Error(F("Error writing to FLASH."), true);
}
}
writeByte_GB(romAddress - 1, 0xF0);
processedProgressBar += 128;
draw_progressbar(processedProgressBar, totalProgressBar); draw_progressbar(processedProgressBar, totalProgressBar);
} }
} }
@ -1454,10 +1503,12 @@ void readSRAMFLASH_GB_MBC6() {
// Close the file: // Close the file:
myFile.close(); myFile.close();
println_Msg(F("Save writing finished"));
// Signal end of process
println_Msg(F("OK"));
display_Update(); display_Update();
}
else {
print_Error(F("File doesnt exist"), false);
}
} }
/****************************************** /******************************************