add common function to open create folder, print message and open file. Remove duplicate code

This commit is contained in:
smesgr9000 2024-05-26 22:20:47 +02:00
parent 78bf1462d0
commit 363d18b542
28 changed files with 61 additions and 272 deletions

View File

@ -296,13 +296,7 @@ boolean checkE7(uint16_t bank) {
}
void readROM_2600() {
createFolder("ATARI", "ROM", romName, "a26");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("ATARI", "ROM", romName, "a26");
// ROM Start 0xF000
// Address A12-A0 = 0x1000 = 1 0000 0000 0000 = 4KB

View File

@ -216,16 +216,9 @@ void readSegment_5200(uint16_t startaddr, uint16_t endaddr) {
//******************************************
void readROM_5200() {
createFolder("5200", "ROM", romName, "a52");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("5200", "ROM", romName, "a52");
// 5200 A13-A0 = 10 0000 0000 0000
switch (a5200mapper) {
case 0: // Standard 4KB/8KB/16KB/32KB
// Lower Half of 32K is at 0x4000

View File

@ -342,13 +342,7 @@ void bankSwitch_7800(uint16_t addr) {
//******************************************
void readROM_7800() {
createFolder("7800", "ROM", romName, "a78");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("7800", "ROM", romName, "a78");
switch (a7800mapper) {
case 0: // Standard 16K/32K/48K [7816/7832/7848]

View File

@ -150,14 +150,7 @@ void readSegment_ARC(uint16_t startaddr, uint16_t endaddr) {
}
void readROM_ARC() {
createFolder("ARC", "ROM", romName, "bin");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("ARC", "ROM", romName, "bin");
readSegment_ARC(0x0000, 0x0800); // 2K
if (arcsize > 0) {

View File

@ -364,13 +364,7 @@ void readPorts_C64() {
// GAME HIGH/EXROM LOW: ROML = $8000
void readROM_C64() {
createFolder("C64", "ROM", romName, "bin");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("C64", "ROM", romName, "bin");
switch (c64mapper) {
case 0: // Normal (4K/8K/16K) & Ultimax (8K/16K)

View File

@ -162,13 +162,7 @@ void readSegment_COL(uint32_t startaddr, uint32_t endaddr) {
}
void readROM_COL() {
createFolder("COL", "ROM", romName, "col");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("COL", "ROM", romName, "col");
// RESET ALL CS PINS HIGH (DISABLE)
PORTH |= (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);

View File

@ -692,6 +692,15 @@ void printAndIncrementFolder(bool displayClear = false) {
EEPROM_writeAnything(0, foldern);
}
void createFolderAndOpenFile(const char* system, const char* subfolder, const char* gameName, const char* fileSuffix) {
createFolder(system, subfolder, gameName, fileSuffix);
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
}
// move file pointer to first game line with matching letter. If no match is found the last database entry is selected
void seek_first_letter_in_database(FsFile& database, byte myLetter) {
char gamename_str[3];

View File

@ -392,14 +392,7 @@ uint8_t readData_FAIRCHILD() {
}
void readROM_FAIRCHILD() {
createFolder("FAIRCHILD", "ROM", romName, "bin");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("FAIRCHILD", "ROM", romName, "bin");
unsigned long cartsize = FAIRCHILD[fairchildsize] * 0x400;
uint8_t blocks = cartsize / 0x200;

View File

@ -1107,14 +1107,7 @@ void getCartInfo_GB() {
// Read ROM
void readROM_GB() {
// Get name, add extension and convert to char array for sd lib
createFolder("GB", "ROM", romName, "gb");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("GB", "ROM", romName, "gb");
word endAddress = 0x7FFF;
word romAddress = 0;
@ -1454,14 +1447,7 @@ unsigned long verifySRAM_GB() {
// Read SRAM + FLASH save data of MBC6
void readSRAMFLASH_MBC6_GB() {
// Get name, add extension and convert to char array for sd lib
createFolder("GB", "SAVE", romName, "sav");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("GB", "SAVE", romName, "sav");
//Initialize progress bar
uint32_t processedProgressBar = 0;
@ -2524,14 +2510,7 @@ bool writeCFI_GB() {
// Read Pelican GBC Device - All Brainboys, MonsterBrains, Codebreakers
void readPelican_GB() {
// Get name, add extension and convert to char array for sd lib
createFolder("GB", "ROM", "Pelican", "GB");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("GB", "ROM", "Pelican", "GB");
word finalAddress = 0x3FFF;
word startAddress = 0x2000;

View File

@ -821,14 +821,7 @@ void getCartInfo_GBA() {
// Dump ROM
void readROM_GBA() {
// Get name, add extension and convert to char array for sd lib
createFolder("GBA", "ROM", romName, "gba");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("GBA", "ROM", romName, "gba");
//Initialize progress bar
uint32_t processedProgressBar = 0;
@ -1045,14 +1038,8 @@ void readFRAM_GBA(unsigned long framSize) {
PORTH &= ~((1 << 0) | (1 << 6));
// Get name, add extension and convert to char array for sd lib
createFolder("GBA", "SAVE", romName, "srm");
createFolderAndOpenFile("GBA", "SAVE", romName, "srm");
printAndIncrementFolder();
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
for (unsigned long currAddress = 0; currAddress < framSize; currAddress += 512) {
for (int c = 0; c < 512; c++) {
// Pull OE_SRAM(PH6) HIGH

View File

@ -268,15 +268,7 @@ void readRAM_GPC() {
controlIn_SNES();
// Get name, add extension and convert to char array for sd lib
createFolder("SNES", "ROM", "GPC4M", "sfc");
//clear the screen
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("SNES", "ROM", "GPC4M", "sfc");
// Read Banks
for (int currBank = 0xC0; currBank < 0xC8; currBank++) {

View File

@ -303,13 +303,7 @@ void readSegment_INTV(uint32_t startaddr, uint32_t endaddr) {
// MODIFIED READ ROUTINE FOR ALL 10 MAPPERS
void readROM_INTV() {
createFolder("INTV", "ROM", romName, "int");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("INTV", "ROM", romName, "int");
switch (intvmapper) {
case 0: //default mattel up to 32K (8K/12K/16K/24K/32K)

View File

@ -522,13 +522,7 @@ void getCartInfo_LOOPY() {
void readROM_LOOPY() {
dataIn_LOOPY();
createFolder("LOOPY", "ROM", romName, "bin");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("LOOPY", "ROM", romName, "bin");
draw_progressbar(0, cartSize);

View File

@ -1424,14 +1424,7 @@ void readROM_MD() {
dataIn_MD();
// Get name, add extension and convert to char array for sd lib
createFolder("MD", "ROM", romName, "BIN");
printAndIncrementFolder(true);
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("MD", "ROM", romName, "BIN");
byte buffer[1024] = { 0 };
@ -2872,14 +2865,7 @@ void readRealtec_MD() {
dataIn_MD();
// Get name, add extension and convert to char array for sd lib
createFolder("MD", "ROM", romName, "MD");
printAndIncrementFolder(true);
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("MD", "ROM", romName, "MD");
// Realtec Registers
writeWord_MD(0x201000, 4); // Number of 128K Blocks 0x402000 (0x201000)

View File

@ -343,13 +343,7 @@ void readROM_MSX() {
println_Msg(F("ROM SIZE 0K"));
display_Update();
} else {
createFolder("MSX", "ROM", romName, "bin");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("MSX", "ROM", romName, "bin");
switch (msxmapper) {
case 0: // No Mapper

View File

@ -4194,13 +4194,7 @@ void resetGameshark_N64() {
// Read rom and save to the SD card
void backupGameshark_N64() {
createFolder("N64", "ROM", "GameShark", "z64");
printAndIncrementFolder(true);
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("N64", "ROM", "GameShark", "z64");
for (unsigned long currByte = romBase + 0xEC00000; currByte < (romBase + 0xEC00000 + flashSize); currByte += 512) {
// Blink led
@ -4563,13 +4557,7 @@ void resetXplorer_N64() {
// Read rom and save to the SD card
void backupXplorer_N64() {
// create a new folder
createFolder("N64", "ROM", "XPLORER64", "z64");
printAndIncrementFolder(true);
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("N64", "ROM", "XPLORER64", "z64");
for (unsigned long currByte = 0x10400000; currByte <= 0x1043FFFF; currByte += 512) {
// Blink led

View File

@ -711,14 +711,7 @@ bool selectMapping(FsFile& database) {
void read_NES(const char* fileSuffix, const byte* header, const uint8_t headersize, const boolean renamerom) {
// Get name, add extension and convert to char array for sd lib
createFolder("NES", "ROM", romName, fileSuffix);
printAndIncrementFolder(true);
// Open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("NES", "ROM", romName, fileSuffix);
//Initialize progress bar
uint32_t processedProgressBar = 0;

View File

@ -212,14 +212,7 @@ void bankSwitch_ODY2(uint16_t addr, uint8_t data) {
}
void readROM_ODY2() {
createFolder("ODY2", "ROM", romName, "bin");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("ODY2", "ROM", romName, "bin");
if (ody2mapper == 1) { // A10 CONNECTED
// Videopac 31: Musician

View File

@ -239,13 +239,7 @@ void writeData_POKE(uint32_t addr, uint8_t data) {
//******************************************
void readROM_POKE() {
createFolder("POKE", "ROM", romName, "min");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("POKE", "ROM", romName, "min");
// read rom
uint32_t progress = 0;

View File

@ -647,37 +647,30 @@ void readSRAM_SMS() {
} else {
system = "SMS";
}
createFolder(system, "SAVE", romName, "sav");
createFolderAndOpenFile(system, "SAVE", romName, "sav");
printAndIncrementFolder(true);
// Write the whole 32KB
// When there is only 8KB of SRAM, the contents should be duplicated
word bankSize = 16 * 1024UL;
for (byte currBank = 0x0; currBank < 2; currBank++) {
writeByte_SMS(0xFFFC, 0x08 | (currBank << 2));
// Create file on sd card
if (myFile.open(fileName, O_RDWR | O_CREAT)) {
// Write the whole 32KB
// When there is only 8KB of SRAM, the contents should be duplicated
word bankSize = 16 * 1024UL;
for (byte currBank = 0x0; currBank < 2; currBank++) {
writeByte_SMS(0xFFFC, 0x08 | (currBank << 2));
// Blink led
blinkLED();
// Blink led
blinkLED();
// Read 16KB from slot 2 which starts at 0x8000
for (word currBuffer = 0; currBuffer < bankSize; currBuffer += 512) {
// Fill SD buffer
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_SMS(0x8000 + currBuffer + currByte);
}
myFile.write(sdBuffer, 512);
// Read 16KB from slot 2 which starts at 0x8000
for (word currBuffer = 0; currBuffer < bankSize; currBuffer += 512) {
// Fill SD buffer
for (int currByte = 0; currByte < 512; currByte++) {
sdBuffer[currByte] = readByte_SMS(0x8000 + currBuffer + currByte);
}
myFile.write(sdBuffer, 512);
}
// Close file
myFile.close();
print_STR(press_button_STR, 1);
display_Update();
} else {
print_FatalError(sd_error_STR);
}
// Close file
myFile.close();
print_STR(press_button_STR, 1);
display_Update();
}
//**********************************************

View File

@ -1291,14 +1291,7 @@ void readROM_SNES() {
controlIn_SNES();
// Get name, add extension and convert to char array for sd lib
createFolder("SNES", "ROM", romName, "sfc");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("SNES", "ROM", romName, "sfc");
//Dump Derby Stallion '96 (Japan) and Sound Novel Tsukuru (Japan) - Actual Size is 24Mb
if ((romType == LO) && (numBanks == 96) && ((strcmp("CC86", checksumStr) == 0) || (strcmp("A77B", checksumStr) == 0))) {

View File

@ -167,14 +167,7 @@ void readSlot(bool cartSlot) {
// Read ST rom to SD card
void readRom_ST(unsigned int bankStart, unsigned int bankEnd) {
// create a new folder to save rom file
createFolder("ST", "ROM", "SUFAMI_TURBO", "st");
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("ST", "ROM", "SUFAMI_TURBO", "st");
// Read specified banks
readLoRomBanks(bankStart + 0x80, bankEnd + 0x80, &myFile);

View File

@ -148,11 +148,7 @@ void suprAcanMenu() {
static void readROM_Acan() {
uint32_t crc32 = 0xffffffff;
createFolder("/ACAN", "ROM", "rom", "bin");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("/ACAN", "ROM", "rom", "bin");
draw_progressbar(0, cartSize);
@ -180,11 +176,7 @@ static void readROM_Acan() {
}
static void readSRAM_Acan() {
createFolder("/ACAN", "SAVE", "save", "bin");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("/ACAN", "SAVE", "save", "bin");
dataIn_MD();
for (uint32_t i = 0; i < 0x10000; i += 1024) {
@ -265,11 +257,7 @@ static void verifySRAM_Acan() {
}
static void readUM6650() {
createFolder("/ACAN", "UM6650", "UM6650", "bin");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("/ACAN", "UM6650", "UM6650", "bin");
for (uint16_t i = 0; i < 256; i++) {
dataOut_MD();

View File

@ -413,15 +413,7 @@ void readROM_SV() {
controlIn_SNES();
// Get name, add extension and convert to char array for sd lib
createFolder("SNES", "ROM", "MEMPACK", "bs");
//clear the screen
printAndIncrementFolder(true);
//open file on sd card
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(create_file_STR);
}
createFolderAndOpenFile("SNES", "ROM", "MEMPACK", "bs");
// Read Banks
for (int currBank = 0x40; currBank < 0x50; currBank++) {

View File

@ -410,13 +410,7 @@ void getCartInfo_VB() {
void readROM_VB() {
dataIn_VB();
createFolder("VBOY", "ROM", romName, "vb");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
print_FatalError(sd_error_STR);
}
createFolderAndOpenFile("VBOY", "ROM", romName, "vb");
word d = 0;
uint32_t progress = 0;

View File

@ -196,13 +196,7 @@ void readSegment_VECTREX(uint16_t startaddr, uint16_t endaddr) {
//******************************************
void readROM_VECTREX() {
createFolder("VECTREX", "ROM", romName, "vec");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(sd_error_STR);
createFolderAndOpenFile("VECTREX", "ROM", romName, "vec");
PB6_DISABLE; // PB6 LOW - Switch Bank

View File

@ -553,12 +553,7 @@ static uint16_t readROM_WS(char *outPathBuf, size_t bufferSize) {
static void readSRAM_WS() {
// generate fullname of rom file
createFolder("WS", "SAVE", romName, "save");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("WS", "SAVE", romName, "save");
uint32_t bank_size = (sramSize << 7);
uint16_t end_bank = (bank_size >> 16); // 64KB per bank
@ -685,12 +680,7 @@ static void writeSRAM_WS() {
static void readEEPROM_WS() {
// generate fullname of eep file
createFolder("WS", "SAVE", romName, "eep");
printAndIncrementFolder(true);
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("WS", "SAVE", romName, "eep");
uint32_t eepromSize = (sramSize << 7);
uint32_t bufSize = (eepromSize < 512 ? eepromSize : 512);

View File

@ -185,13 +185,7 @@ uint8_t readByte_WSV(uint32_t addr) {
//******************************************
void readROM_WSV() {
createFolder("WSV", "ROM", romName, "sv");
printAndIncrementFolder(true);
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
createFolderAndOpenFile("WSV", "ROM", romName, "sv");
// start reading rom
dataIn_WSV();