2021-01-27 08:05:25 +01:00
|
|
|
|
//******************************************
|
2022-07-23 11:04:17 +02:00
|
|
|
|
// SF MEMORY MODULE
|
2021-01-27 08:05:25 +01:00
|
|
|
|
//******************************************
|
2024-03-02 17:26:35 +01:00
|
|
|
|
#ifdef ENABLE_SFM
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
SF Memory Clock Source
|
|
|
|
|
******************************************/
|
|
|
|
|
// The clock signal for the SF Memory cassette
|
|
|
|
|
// is generated with the Adafruit Clock Generator
|
|
|
|
|
// or a similar external clock source
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
Variables
|
|
|
|
|
*****************************************/
|
|
|
|
|
// SF Memory status
|
|
|
|
|
byte sfmReady = 0;
|
|
|
|
|
|
|
|
|
|
// Arrays that hold game info
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// int gameSize[8];
|
|
|
|
|
// int saveSize[8];
|
2021-01-27 08:05:25 +01:00
|
|
|
|
byte gameAddress[8];
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// byte gameVersion[8];
|
|
|
|
|
// boolean hirom[8];
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
Menu
|
|
|
|
|
*****************************************/
|
|
|
|
|
// SFM menu items
|
|
|
|
|
static const char sfmMenuItem1[] PROGMEM = "Game Menu";
|
|
|
|
|
static const char sfmMenuItem2[] PROGMEM = "Flash Menu";
|
2024-03-02 17:26:35 +01:00
|
|
|
|
static const char* const menuOptionsSFM[] PROGMEM = { sfmMenuItem1, sfmMenuItem2, FSTRING_RESET };
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// SFM flash menu items
|
|
|
|
|
static const char sfmFlashMenuItem1[] PROGMEM = "Read Flash";
|
|
|
|
|
static const char sfmFlashMenuItem2[] PROGMEM = "Write Flash";
|
|
|
|
|
static const char sfmFlashMenuItem3[] PROGMEM = "Print Mapping";
|
|
|
|
|
static const char sfmFlashMenuItem4[] PROGMEM = "Read Mapping";
|
|
|
|
|
static const char sfmFlashMenuItem5[] PROGMEM = "Write Mapping";
|
|
|
|
|
static const char sfmFlashMenuItem6[] PROGMEM = "Back";
|
2022-10-13 09:49:03 +02:00
|
|
|
|
static const char* const menuOptionsSFMFlash[] PROGMEM = { sfmFlashMenuItem1, sfmFlashMenuItem2, sfmFlashMenuItem3, sfmFlashMenuItem4, sfmFlashMenuItem5, sfmFlashMenuItem6 };
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// SFM game menu items
|
|
|
|
|
static const char sfmGameMenuItem2[] PROGMEM = "Read Game";
|
|
|
|
|
static const char sfmGameMenuItem4[] PROGMEM = "Switch Game";
|
2024-03-02 17:26:35 +01:00
|
|
|
|
static const char* const menuOptionsSFMGame[] PROGMEM = { FSTRING_READ_SAVE, sfmGameMenuItem2, FSTRING_WRITE_SAVE, sfmGameMenuItem4, FSTRING_RESET };
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
void sfmMenu() {
|
|
|
|
|
// create menu with title and 3 options to choose from
|
|
|
|
|
unsigned char mainMenu;
|
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
|
convertPgm(menuOptionsSFM, 3);
|
|
|
|
|
mainMenu = question_box(F("SF Memory"), menuOptions, 3, 0);
|
|
|
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
2022-10-13 09:49:03 +02:00
|
|
|
|
switch (mainMenu) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Game menu
|
|
|
|
|
case 0:
|
|
|
|
|
sfmGameMenu();
|
|
|
|
|
break;
|
2024-06-30 21:31:54 +02:00
|
|
|
|
#ifdef CORE_SFM_FLASH
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Flash menu
|
|
|
|
|
case 1:
|
2024-03-02 17:26:35 +01:00
|
|
|
|
mode = CORE_SFM_FLASH;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
break;
|
2024-06-30 21:31:54 +02:00
|
|
|
|
#endif
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Reset
|
|
|
|
|
case 2:
|
|
|
|
|
resetArduino();
|
|
|
|
|
break;
|
2024-06-30 21:31:54 +02:00
|
|
|
|
default:
|
|
|
|
|
print_MissingModule();
|
|
|
|
|
break;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sfmGameMenu() {
|
2022-10-24 08:16:04 +02:00
|
|
|
|
char menuOptionsSFMGames[8][20];
|
|
|
|
|
byte numGames;
|
|
|
|
|
boolean hasMenu;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Switch to hirom all
|
|
|
|
|
if (send_SFM(0x04) == 0x2A) {
|
|
|
|
|
delay(300);
|
|
|
|
|
|
|
|
|
|
// Fill arrays with data
|
2022-10-24 08:16:04 +02:00
|
|
|
|
getGames(menuOptionsSFMGames, &hasMenu, &numGames);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
if (hasMenu) {
|
|
|
|
|
// Create menu with title and numGames options to choose from
|
|
|
|
|
unsigned char gameSubMenu;
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
|
|
|
|
gameSubMenu = question_box(F("Select Game"), menuOptionsSFMGames, numGames, 0);
|
|
|
|
|
|
|
|
|
|
// Switch to game
|
|
|
|
|
send_SFM(gameSubMenu + 0x80);
|
|
|
|
|
delay(200);
|
|
|
|
|
// Check for successfull switch
|
|
|
|
|
byte timeout = 0;
|
|
|
|
|
while (readBank_SFM(0, 0x2400) != 0x7D) {
|
|
|
|
|
delay(200);
|
|
|
|
|
// Try again
|
|
|
|
|
send_SFM(gameSubMenu + 0x80);
|
|
|
|
|
delay(200);
|
|
|
|
|
timeout++;
|
|
|
|
|
// Abort, something is wrong
|
|
|
|
|
if (timeout == 5) {
|
|
|
|
|
display_Clear();
|
|
|
|
|
print_Msg(F("Game "));
|
|
|
|
|
print_Msg(gameSubMenu + 0x80, HEX);
|
|
|
|
|
println_Msg(F(" Timeout"));
|
|
|
|
|
println_Msg(readBank_SFM(0, 0x2400), HEX);
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_EMPTY));
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Powercycle SFM cart"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Copy gameCode to romName in case of japanese chars in romName
|
2022-10-24 08:16:04 +02:00
|
|
|
|
strcpy(romName, menuOptionsSFMGames[gameSubMenu + 1]);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Print info
|
|
|
|
|
getCartInfo_SFM();
|
2024-03-02 17:26:35 +01:00
|
|
|
|
mode = CORE_SFM_GAME;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// No menu so switch to only game
|
|
|
|
|
// Switch to game
|
|
|
|
|
send_SFM(0x80);
|
|
|
|
|
delay(200);
|
|
|
|
|
|
|
|
|
|
// Copy gameCode to romName in case of japanese chars in romName
|
2022-10-24 08:16:04 +02:00
|
|
|
|
strcpy(romName, menuOptionsSFMGames[0]);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Print info
|
|
|
|
|
getCartInfo_SFM();
|
2024-03-02 17:26:35 +01:00
|
|
|
|
mode = CORE_SFM_GAME;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("Switch to HiRom failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sfmGameOptions() {
|
|
|
|
|
// create menu with title and 5 options to choose from
|
|
|
|
|
unsigned char gameSubMenu;
|
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
|
convertPgm(menuOptionsSFMGame, 5);
|
|
|
|
|
gameSubMenu = question_box(F("SFM Game Menu"), menuOptions, 5, 0);
|
|
|
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
2022-10-13 09:49:03 +02:00
|
|
|
|
switch (gameSubMenu) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Read sram
|
|
|
|
|
case 0:
|
|
|
|
|
display_Clear();
|
|
|
|
|
// Change working dir to root
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
readSRAM();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Read rom
|
|
|
|
|
case 1:
|
|
|
|
|
display_Clear();
|
|
|
|
|
// Change working dir to root
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
readROM_SFM();
|
|
|
|
|
compare_checksum();
|
2022-12-11 03:49:07 +01:00
|
|
|
|
compareCRC("snes.txt", 0, 1, 0);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Write sram
|
|
|
|
|
case 2:
|
|
|
|
|
display_Clear();
|
|
|
|
|
// Change working dir to root
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
writeSRAM(1);
|
|
|
|
|
unsigned long wrErrors;
|
|
|
|
|
wrErrors = verifySRAM();
|
|
|
|
|
if (wrErrors == 0) {
|
|
|
|
|
println_Msg(F("Verified OK"));
|
|
|
|
|
display_Update();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(error_STR, 0);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
print_Msg(wrErrors);
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(_bytes_STR, 1);
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(did_not_verify_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Switch game
|
|
|
|
|
case 3:
|
|
|
|
|
sfmGameMenu();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
|
case 4:
|
|
|
|
|
resetArduino();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (gameSubMenu != 3) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_EMPTY));
|
2022-10-21 15:59:06 +02:00
|
|
|
|
// Prints string out of the common strings array either with or without newline
|
|
|
|
|
print_STR(press_button_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 17:26:35 +01:00
|
|
|
|
#ifdef ENABLE_FLASH
|
2021-01-27 08:05:25 +01:00
|
|
|
|
void sfmFlashMenu() {
|
|
|
|
|
// create menu with title and 6 options to choose from
|
|
|
|
|
unsigned char flashSubMenu;
|
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
|
convertPgm(menuOptionsSFMFlash, 6);
|
|
|
|
|
flashSubMenu = question_box(F("SFM Flash Menu"), menuOptions, 6, 0);
|
|
|
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
2022-10-13 09:49:03 +02:00
|
|
|
|
switch (flashSubMenu) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Read Flash
|
|
|
|
|
case 0:
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Reset to root directory
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
|
|
|
|
|
// Reset to HIROM ALL
|
|
|
|
|
romType = 1;
|
|
|
|
|
print_Msg(F("Switch to HiRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (send_SFM(0x04) == 0x2A) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Reset flash
|
|
|
|
|
resetFlash_SFM(0xC0);
|
|
|
|
|
resetFlash_SFM(0xE0);
|
|
|
|
|
|
|
|
|
|
flashSize = 4194304;
|
|
|
|
|
numBanks = 64;
|
|
|
|
|
|
|
|
|
|
// Get name, add extension and convert to char array for sd lib
|
|
|
|
|
EEPROM_readAnything(0, foldern);
|
|
|
|
|
sprintf(fileName, "SFM%d", foldern);
|
|
|
|
|
strcat(fileName, ".bin");
|
|
|
|
|
sd.mkdir("NP", true);
|
|
|
|
|
sd.chdir("NP");
|
|
|
|
|
// write new folder number back to eeprom
|
|
|
|
|
foldern = foldern + 1;
|
|
|
|
|
EEPROM_writeAnything(0, foldern);
|
|
|
|
|
|
|
|
|
|
// Read flash
|
|
|
|
|
readFlash_SFM();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("Switch to HiRom failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Write Flash
|
|
|
|
|
case 1:
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Print warning
|
|
|
|
|
println_Msg(F("Attention"));
|
|
|
|
|
println_Msg(F("This will erase your"));
|
|
|
|
|
println_Msg(F("NP Cartridge."));
|
|
|
|
|
println_Msg("");
|
2022-10-21 15:59:06 +02:00
|
|
|
|
// Prints string out of the common strings array either with or without newline
|
|
|
|
|
print_STR(press_button_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
wait();
|
|
|
|
|
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
filePath[0] = '\0';
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
// Launch file browser
|
|
|
|
|
fileBrowser(F("Select 4MB file"));
|
|
|
|
|
display_Clear();
|
|
|
|
|
sprintf(filePath, "%s/%s", filePath, fileName);
|
|
|
|
|
flashSize = 2097152;
|
|
|
|
|
numBanks = 32;
|
|
|
|
|
println_Msg(F("Writing 1st rom"));
|
|
|
|
|
display_Update();
|
|
|
|
|
// Program 1st flashrom
|
|
|
|
|
write_SFM(0xC0, 0);
|
|
|
|
|
display_Clear();
|
|
|
|
|
println_Msg(F("Writing 2nd rom"));
|
|
|
|
|
display_Update();
|
|
|
|
|
// Program 2nd flashrom
|
|
|
|
|
write_SFM(0xE0, 2097152);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Print mapping
|
|
|
|
|
case 2:
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Reset to root directory
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
|
|
|
|
|
// Reset to HIROM ALL
|
|
|
|
|
romType = 1;
|
|
|
|
|
print_Msg(F("Switch to HiRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (send_SFM(0x04) == 0x2A) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
idFlash_SFM(0xC0);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
idFlash_SFM(0xE0);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Reset flash
|
|
|
|
|
resetFlash_SFM(0xC0);
|
|
|
|
|
resetFlash_SFM(0xE0);
|
|
|
|
|
delay(100);
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
printMapping();
|
|
|
|
|
resetFlash_SFM(0xC0);
|
|
|
|
|
resetFlash_SFM(0xE0);
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Read mapping
|
|
|
|
|
case 3:
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Reset to root directory
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
|
|
|
|
|
// Reset to HIROM ALL
|
|
|
|
|
romType = 1;
|
|
|
|
|
print_Msg(F("Switch to HiRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (send_SFM(0x04) == 0x2A) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
idFlash_SFM(0xC0);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
idFlash_SFM(0xE0);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Reset flash
|
|
|
|
|
resetFlash_SFM(0xC0);
|
|
|
|
|
resetFlash_SFM(0xE0);
|
|
|
|
|
delay(100);
|
|
|
|
|
readMapping();
|
|
|
|
|
resetFlash_SFM(0xC0);
|
|
|
|
|
resetFlash_SFM(0xE0);
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Write mapping
|
|
|
|
|
case 4:
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Print warning
|
|
|
|
|
println_Msg(F("Attention"));
|
|
|
|
|
println_Msg(F("This will erase your"));
|
|
|
|
|
println_Msg(F("NP Cartridge."));
|
|
|
|
|
println_Msg("");
|
2022-10-21 15:59:06 +02:00
|
|
|
|
// Prints string out of the common strings array either with or without newline
|
|
|
|
|
print_STR(press_button_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
wait();
|
|
|
|
|
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Reset to root directory
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
|
|
|
|
|
// Erase mapping
|
|
|
|
|
eraseMapping(0xD0);
|
|
|
|
|
eraseMapping(0xE0);
|
|
|
|
|
print_Msg(F("Blankcheck..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (blankcheckMapping_SFM()) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(F("Nope"));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear screen
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
|
|
// Clear filepath
|
|
|
|
|
filePath[0] = '\0';
|
|
|
|
|
|
|
|
|
|
// Reset to root directory
|
|
|
|
|
sd.chdir("/");
|
|
|
|
|
|
|
|
|
|
// Launch file browser
|
|
|
|
|
fileBrowser(F("Select MAP file"));
|
|
|
|
|
display_Clear();
|
|
|
|
|
sprintf(filePath, "%s/%s", filePath, fileName);
|
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Write mapping
|
|
|
|
|
writeMapping_SFM(0xD0, 0);
|
|
|
|
|
writeMapping_SFM(0xE0, 256);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Go back
|
|
|
|
|
case 5:
|
2024-03-02 17:26:35 +01:00
|
|
|
|
mode = CORE_SFM;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (flashSubMenu != 5) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_EMPTY));
|
2022-10-21 15:59:06 +02:00
|
|
|
|
// Prints string out of the common strings array either with or without newline
|
|
|
|
|
print_STR(press_button_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Read the games from the menu area
|
2022-10-28 15:02:51 +02:00
|
|
|
|
void getGames(char gameCode[8][20], boolean* hasMenu, byte* numGames) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Set data pins to input
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Check if menu is present
|
2022-10-24 08:16:04 +02:00
|
|
|
|
*hasMenu = true;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
byte menuString[] = { 0x4D, 0x45, 0x4E, 0x55, 0x20, 0x50, 0x52, 0x4F, 0x47, 0x52, 0x41, 0x4D };
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
|
if (menuString[i] != readBank_SFM(0xC0, 0x7FC0 + i)) {
|
2022-10-24 08:16:04 +02:00
|
|
|
|
*hasMenu = false;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 08:16:04 +02:00
|
|
|
|
if (*hasMenu) {
|
|
|
|
|
*numGames = 0;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Count number of games
|
|
|
|
|
for (word i = 0x0000; i < 0xE000; i += 0x2000) {
|
2022-10-24 08:16:04 +02:00
|
|
|
|
if (readBank_SFM(0xC6, i) == *numGames)
|
|
|
|
|
(*numGames)++;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get game info
|
2022-10-24 08:16:04 +02:00
|
|
|
|
for (int i = 0; i < *numGames; i++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Read starting address and size
|
|
|
|
|
gameAddress[i] = 0xC0 + readBank_SFM(0xC6, i * 0x2000 + 0x01) * 0x8;
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// gameSize[i] = readBank_SFM(0xC6, i * 0x2000 + 0x03) * 128;
|
|
|
|
|
// saveSize[i] = readBank_SFM(0xC6, i * 0x2000 + 0x05) / 8;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
//check if hirom
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// if (readBank_SFM(gameAddress[i], 0xFFD5) == 0x31) {
|
|
|
|
|
// hirom[i] = true;
|
|
|
|
|
// } else if (readBank_SFM(gameAddress[i], 0xFFD5) == 0x21) {
|
|
|
|
|
// hirom[i] = true;
|
|
|
|
|
// } else {
|
|
|
|
|
// hirom[i] = false;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (hirom[i]) {
|
|
|
|
|
// gameVersion[i] = readBank_SFM(gameAddress[i], 0xFFDB);
|
|
|
|
|
// } else {
|
|
|
|
|
// gameVersion[i] = readBank_SFM(gameAddress[i], 0x7FDB);
|
|
|
|
|
// }
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Read game code
|
|
|
|
|
byte myByte = 0;
|
|
|
|
|
byte myLength = 0;
|
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
|
myByte = readBank_SFM(0xC6, i * 0x2000 + 0x07 + j);
|
|
|
|
|
// Remove funny characters
|
2022-10-24 06:38:56 +02:00
|
|
|
|
if (((myByte >= ',' && myByte <= '9') || (myByte >= 'A' && myByte <= 'z')) && myLength < 9) {
|
|
|
|
|
gameCode[i][myLength] = myByte;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
myLength++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// End char array in case game code is less than 9 chars
|
|
|
|
|
gameCode[i][myLength] = '\0';
|
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-24 06:38:56 +02:00
|
|
|
|
word base;
|
2022-10-24 08:16:04 +02:00
|
|
|
|
*numGames = 1;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
//check if hirom
|
|
|
|
|
if (readBank_SFM(0xC0, 0xFFD5) == 0x31) {
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// hirom[0] = true;
|
2022-10-24 06:38:56 +02:00
|
|
|
|
base = 0xFF00;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// hirom[0] = false;
|
2022-10-24 06:38:56 +02:00
|
|
|
|
base = 0x7F00;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// gameVersion[0] = readBank_SFM(0xC0, base + 0xDB);
|
2022-10-24 06:38:56 +02:00
|
|
|
|
gameCode[0][0] = 'G';
|
|
|
|
|
gameCode[0][1] = 'A';
|
|
|
|
|
gameCode[0][2] = 'M';
|
|
|
|
|
gameCode[0][3] = 'E';
|
|
|
|
|
gameCode[0][4] = '-';
|
|
|
|
|
gameCode[0][5] = readBank_SFM(0xC0, base + 0xB2);
|
|
|
|
|
gameCode[0][6] = readBank_SFM(0xC0, base + 0xB3);
|
|
|
|
|
gameCode[0][7] = readBank_SFM(0xC0, base + 0xB4);
|
|
|
|
|
gameCode[0][8] = readBank_SFM(0xC0, base + 0xB5);
|
|
|
|
|
gameCode[0][9] = '\0';
|
2022-10-29 03:16:53 +02:00
|
|
|
|
// gameSize[0] = 1 << (readBank_SFM(0xC0, base + 0xD7) - 7);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
Setup
|
|
|
|
|
*****************************************/
|
|
|
|
|
void setup_SFM() {
|
2023-06-26 21:25:54 +02:00
|
|
|
|
// Request 5V
|
2023-06-26 12:04:00 +02:00
|
|
|
|
setVoltage(VOLTS_SET_5V);
|
|
|
|
|
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Set cicrstPin(PG1) to Output
|
|
|
|
|
DDRG |= (1 << 1);
|
|
|
|
|
// Output a high signal to disable snesCIC
|
|
|
|
|
PORTG |= (1 << 1);
|
|
|
|
|
// Set cichstPin(PG0) to Input
|
|
|
|
|
DDRG &= ~(1 << 0);
|
|
|
|
|
|
|
|
|
|
// Set Address Pins to Output
|
|
|
|
|
//A0-A7
|
|
|
|
|
DDRF = 0xFF;
|
|
|
|
|
//A8-A15
|
|
|
|
|
DDRK = 0xFF;
|
|
|
|
|
//BA0-BA7
|
|
|
|
|
DDRL = 0xFF;
|
|
|
|
|
|
|
|
|
|
// Set Control Pins to Output RST(PH0) CS(PH3) WR(PH5) RD(PH6)
|
|
|
|
|
DDRH |= (1 << 0) | (1 << 3) | (1 << 5) | (1 << 6);
|
|
|
|
|
// Switch RST(PH0) and WR(PH5) to HIGH
|
|
|
|
|
PORTH |= (1 << 0) | (1 << 5);
|
|
|
|
|
// Switch CS(PH3) and RD(PH6) to LOW
|
|
|
|
|
PORTH &= ~((1 << 3) | (1 << 6));
|
|
|
|
|
|
|
|
|
|
// Set IRQ(PH4) to Input
|
|
|
|
|
DDRH &= ~(1 << 4);
|
|
|
|
|
// Activate Internal Pullup Resistors
|
|
|
|
|
//PORTH |= (1 << 4);
|
|
|
|
|
|
|
|
|
|
// Set Data Pins (D0-D7) to Input
|
|
|
|
|
DDRC = 0x00;
|
|
|
|
|
// Enable Internal Pullups
|
|
|
|
|
//PORTC = 0xFF;
|
|
|
|
|
|
|
|
|
|
// Unused pins
|
|
|
|
|
// Set CPU Clock(PH1) to Output
|
|
|
|
|
DDRH |= (1 << 1);
|
|
|
|
|
//PORTH &= ~(1 << 1);
|
|
|
|
|
|
|
|
|
|
// Adafruit Clock Generator
|
2022-02-27 06:20:33 +01:00
|
|
|
|
initializeClockOffset();
|
2021-10-14 09:53:07 +02:00
|
|
|
|
|
2021-11-18 14:55:50 +01:00
|
|
|
|
if (i2c_found) {
|
|
|
|
|
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
|
|
|
|
|
clockgen.set_pll(SI5351_PLL_FIXED, SI5351_PLLB);
|
|
|
|
|
clockgen.set_freq(2147727200ULL, SI5351_CLK0);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
2021-11-18 14:55:50 +01:00
|
|
|
|
// start outputting master clock
|
|
|
|
|
clockgen.output_enable(SI5351_CLK1, 0);
|
|
|
|
|
clockgen.output_enable(SI5351_CLK2, 0);
|
|
|
|
|
clockgen.output_enable(SI5351_CLK0, 1);
|
|
|
|
|
}
|
2024-03-02 17:26:35 +01:00
|
|
|
|
#ifdef ENABLE_CLOCKGEN
|
2021-11-18 14:55:50 +01:00
|
|
|
|
else {
|
|
|
|
|
display_Clear();
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Clock Generator not found"));
|
2021-11-18 14:55:50 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Wait until all is stable
|
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
|
|
// Switch to HiRom All
|
|
|
|
|
byte timeout = 0;
|
|
|
|
|
send_SFM(0x04);
|
|
|
|
|
delay(200);
|
|
|
|
|
while (readBank_SFM(0, 0x2400) != 0x2A) {
|
|
|
|
|
delay(100);
|
|
|
|
|
// Try again
|
|
|
|
|
send_SFM(0x04);
|
|
|
|
|
delay(100);
|
|
|
|
|
timeout++;
|
|
|
|
|
// Abort, something is wrong
|
|
|
|
|
if (timeout == 5) {
|
|
|
|
|
println_Msg(F("Hirom All Timeout"));
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_EMPTY));
|
|
|
|
|
println_Msg(FS(FSTRING_EMPTY));
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Powercycle SFM cart"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
I/O Functions
|
|
|
|
|
*****************************************/
|
|
|
|
|
// Switch control pins to write
|
|
|
|
|
void controlOut_SFM() {
|
|
|
|
|
// Switch RD(PH6) and WR(PH5) to HIGH
|
|
|
|
|
PORTH |= (1 << 6) | (1 << 5);
|
|
|
|
|
// Switch CS(PH3) to LOW
|
|
|
|
|
PORTH &= ~(1 << 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch control pins to read
|
|
|
|
|
void controlIn_SFM() {
|
|
|
|
|
// Switch WR(PH5) to HIGH
|
|
|
|
|
PORTH |= (1 << 5);
|
|
|
|
|
// Switch CS(PH3) and RD(PH6) to LOW
|
|
|
|
|
PORTH &= ~((1 << 3) | (1 << 6));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
Low level functions
|
|
|
|
|
*****************************************/
|
|
|
|
|
// Write one byte of data to a location specified by bank and address, 00:0000
|
|
|
|
|
void writeBank_SFM(byte myBank, word myAddress, byte myData) {
|
|
|
|
|
PORTL = myBank;
|
|
|
|
|
PORTF = myAddress & 0xFF;
|
|
|
|
|
PORTK = (myAddress >> 8) & 0xFF;
|
|
|
|
|
PORTC = myData;
|
|
|
|
|
|
|
|
|
|
// Arduino running at 16Mhz -> one nop = 62.5ns
|
|
|
|
|
// Wait till output is stable
|
2022-10-13 09:49:03 +02:00
|
|
|
|
__asm__("nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t");
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Switch WR(PH5) to LOW
|
|
|
|
|
PORTH &= ~(1 << 5);
|
|
|
|
|
|
|
|
|
|
// Leave WR low for at least 60ns
|
2022-10-13 09:49:03 +02:00
|
|
|
|
__asm__("nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t");
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Switch WR(PH5) to HIGH
|
|
|
|
|
PORTH |= (1 << 5);
|
|
|
|
|
|
|
|
|
|
// Leave WR high for at least 50ns
|
2022-10-13 09:49:03 +02:00
|
|
|
|
__asm__("nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t");
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read one byte of data from a location specified by bank and address, 00:0000
|
|
|
|
|
byte readBank_SFM(byte myBank, word myAddress) {
|
|
|
|
|
PORTL = myBank;
|
|
|
|
|
PORTF = myAddress & 0xFF;
|
|
|
|
|
PORTK = (myAddress >> 8) & 0xFF;
|
|
|
|
|
|
|
|
|
|
// Arduino running at 16Mhz -> one nop = 62.5ns -> 1000ns total
|
2022-10-13 09:49:03 +02:00
|
|
|
|
__asm__("nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t"
|
|
|
|
|
"nop\n\t");
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Read
|
|
|
|
|
byte tempByte = PINC;
|
|
|
|
|
return tempByte;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
SNES ROM Functions
|
|
|
|
|
******************************************/
|
|
|
|
|
void getCartInfo_SFM() {
|
|
|
|
|
// Print start page
|
|
|
|
|
if (checkcart_SFM() == 0) {
|
|
|
|
|
// Checksum either corrupt or 0000
|
|
|
|
|
errorLvl = 1;
|
2024-05-26 22:43:41 +02:00
|
|
|
|
rgbLed(red_color);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Clear();
|
|
|
|
|
println_Msg(F("ERROR"));
|
|
|
|
|
println_Msg(F("Rom header corrupt"));
|
|
|
|
|
println_Msg(F("or missing"));
|
|
|
|
|
display_Update();
|
|
|
|
|
wait();
|
|
|
|
|
// Wait() clears errors but in this case we still have an error
|
|
|
|
|
errorLvl = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display_Clear();
|
2024-05-30 08:00:13 +02:00
|
|
|
|
print_Msg(FS(FSTRING_NAME));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(romName);
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_SPACE));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
print_Msg(F("Version: 1."));
|
|
|
|
|
println_Msg(romVersion);
|
|
|
|
|
|
2024-05-30 08:00:13 +02:00
|
|
|
|
print_Msg(FS(FSTRING_CHECKSUM));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(checksumStr);
|
|
|
|
|
|
2024-05-28 21:11:48 +02:00
|
|
|
|
print_Msg(FS(FSTRING_SIZE));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
print_Msg(romSize);
|
|
|
|
|
println_Msg(F("Mbit "));
|
|
|
|
|
|
|
|
|
|
print_Msg(F("Type: "));
|
|
|
|
|
if (romType == 1)
|
|
|
|
|
println_Msg(F("HiROM"));
|
|
|
|
|
else if (romType == 0)
|
|
|
|
|
println_Msg(F("LoROM"));
|
|
|
|
|
else
|
|
|
|
|
println_Msg(romType);
|
|
|
|
|
|
|
|
|
|
print_Msg(F("Banks: "));
|
|
|
|
|
println_Msg(numBanks);
|
|
|
|
|
|
|
|
|
|
print_Msg(F("Sram: "));
|
|
|
|
|
print_Msg(sramSize);
|
|
|
|
|
println_Msg(F("Kbit"));
|
2022-10-21 15:59:06 +02:00
|
|
|
|
// Prints string out of the common strings array either with or without newline
|
|
|
|
|
print_STR(press_button_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
// Wait for user input
|
|
|
|
|
wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read header information
|
|
|
|
|
boolean checkcart_SFM() {
|
|
|
|
|
// set control to read
|
|
|
|
|
dataIn();
|
|
|
|
|
|
2022-08-22 17:49:19 +02:00
|
|
|
|
// Read ROM header
|
|
|
|
|
byte snesHeader[80] = { 0 };
|
|
|
|
|
for (byte c = 0; c < 80; c++) {
|
|
|
|
|
snesHeader[c] = readBank_SFM(0, 0xFFB0 + c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate CRC32 of header
|
|
|
|
|
char crcStr[9];
|
2022-10-23 14:26:40 +02:00
|
|
|
|
sprintf(crcStr, "%08lX", calculateCRC(snesHeader, 80));
|
2022-08-22 17:49:19 +02:00
|
|
|
|
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Get Checksum as string
|
|
|
|
|
sprintf(checksumStr, "%02X%02X", readBank_SFM(0, 65503), readBank_SFM(0, 65502));
|
|
|
|
|
|
|
|
|
|
romType = readBank_SFM(0, 0xFFD5);
|
|
|
|
|
if ((romType >> 5) != 1) { // Detect invalid romType byte due to too long ROM name (22 chars)
|
2022-10-13 09:49:03 +02:00
|
|
|
|
romType = 0; // LoROM // Krusty's Super Fun House (U) 1.0 & Contra 3 (U)
|
|
|
|
|
} else {
|
|
|
|
|
romType &= 1; // Must be LoROM or HiROM
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check RomSize
|
|
|
|
|
byte romSizeExp = readBank_SFM(0, 65495) - 7;
|
|
|
|
|
romSize = 1;
|
|
|
|
|
while (romSizeExp--)
|
|
|
|
|
romSize *= 2;
|
|
|
|
|
|
|
|
|
|
numBanks = (long(romSize) * 1024 * 1024 / 8) / (32768 + (long(romType) * 32768));
|
|
|
|
|
|
2022-08-22 17:49:19 +02:00
|
|
|
|
//Check SD card for alt config, pass CRC32 of snesHeader but filter out 0000 and FFFF checksums
|
|
|
|
|
if (!(strcmp(checksumStr, "0000") == 0) && !(strcmp(checksumStr, "FFFF") == 0)) {
|
|
|
|
|
checkAltConf(crcStr);
|
|
|
|
|
}
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Get name
|
|
|
|
|
byte myByte = 0;
|
|
|
|
|
byte myLength = 0;
|
|
|
|
|
for (unsigned int i = 65472; i < 65492; i++) {
|
|
|
|
|
myByte = readBank_SFM(0, i);
|
|
|
|
|
if (((char(myByte) >= 48 && char(myByte) <= 57) || (char(myByte) >= 65 && char(myByte) <= 122)) && myLength < 15) {
|
|
|
|
|
romName[myLength] = char(myByte);
|
|
|
|
|
myLength++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If name consists out of all japanese characters use game code
|
|
|
|
|
if (myLength == 0) {
|
|
|
|
|
// Get rom code
|
|
|
|
|
romName[0] = 'S';
|
|
|
|
|
romName[1] = 'H';
|
|
|
|
|
romName[2] = 'V';
|
|
|
|
|
romName[3] = 'C';
|
|
|
|
|
romName[4] = '-';
|
|
|
|
|
for (unsigned int i = 0; i < 4; i++) {
|
|
|
|
|
myByte = readBank_SFM(0, 0xFFB2 + i);
|
|
|
|
|
if (((char(myByte) >= 48 && char(myByte) <= 57) || (char(myByte) >= 65 && char(myByte) <= 122)) && myLength < 4) {
|
|
|
|
|
romName[myLength + 5] = char(myByte);
|
|
|
|
|
myLength++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (myLength == 0) {
|
|
|
|
|
// Rom code unknown
|
|
|
|
|
romName[0] = 'U';
|
|
|
|
|
romName[1] = 'N';
|
|
|
|
|
romName[2] = 'K';
|
|
|
|
|
romName[3] = 'N';
|
|
|
|
|
romName[4] = 'O';
|
|
|
|
|
romName[5] = 'W';
|
|
|
|
|
romName[6] = 'N';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read sramSizeExp
|
|
|
|
|
byte sramSizeExp = readBank_SFM(0, 0xFFD8);
|
|
|
|
|
|
|
|
|
|
// Calculate sramSize
|
|
|
|
|
if (sramSizeExp != 0) {
|
|
|
|
|
sramSizeExp = sramSizeExp + 3;
|
|
|
|
|
sramSize = 1;
|
|
|
|
|
while (sramSizeExp--)
|
|
|
|
|
sramSize *= 2;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
sramSize = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ROM Version
|
|
|
|
|
romVersion = readBank_SFM(0, 65499);
|
|
|
|
|
|
|
|
|
|
// Test if checksum is equal to reverse checksum
|
2022-10-13 09:49:03 +02:00
|
|
|
|
if (((word(readBank_SFM(0, 65500)) + (word(readBank_SFM(0, 65501)) * 256)) + (word(readBank_SFM(0, 65502)) + (word(readBank_SFM(0, 65503)) * 256))) == 65535) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
if (strcmp("0000", checksumStr) == 0) {
|
|
|
|
|
return 0;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Either rom checksum is wrong or no cart is inserted
|
|
|
|
|
else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read rom to SD card
|
|
|
|
|
void readROM_SFM() {
|
|
|
|
|
// Set control
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Get name, add extension and convert to char array for sd lib
|
2024-05-12 15:37:11 +02:00
|
|
|
|
createFolder("NP", "ROM", romName, "sfc");
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
//clear the screen
|
|
|
|
|
display_Clear();
|
2022-10-31 15:41:29 +01:00
|
|
|
|
print_Msg(F("Creating folder "));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(folder);
|
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// write new folder number back to eeprom
|
|
|
|
|
foldern = foldern + 1;
|
|
|
|
|
EEPROM_writeAnything(0, foldern);
|
|
|
|
|
|
|
|
|
|
//open file on sd card
|
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(create_file_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if LoROM or HiROM...
|
|
|
|
|
if (romType == 0) {
|
|
|
|
|
println_Msg(F("Dumping LoRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Read up to 96 banks starting at bank 0×00.
|
2022-11-01 08:43:42 +01:00
|
|
|
|
for (word currBank = 0; currBank < numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Dump the bytes to SD 512B at a time
|
|
|
|
|
for (long currByte = 32768; currByte < 65536; currByte += 512) {
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
sdBuffer[c] = readBank_SFM(currBank, currByte + c);
|
|
|
|
|
}
|
|
|
|
|
myFile.write(sdBuffer, 512);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Dump High-type ROM
|
|
|
|
|
else {
|
|
|
|
|
println_Msg(F("Dumping HiRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
|
2022-10-31 15:41:29 +01:00
|
|
|
|
for (word currBank = 192; currBank < (numBanks + 192); currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (long currByte = 0; currByte < 65536; currByte += 512) {
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
sdBuffer[c] = readBank_SFM(currBank, currByte + c);
|
|
|
|
|
}
|
|
|
|
|
myFile.write(sdBuffer, 512);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
|
|
|
|
|
|
|
|
|
// Signal end of process
|
|
|
|
|
print_Msg(F("Saved as "));
|
|
|
|
|
println_Msg(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
29F1601 flashrom functions (NP)
|
|
|
|
|
*****************************************/
|
|
|
|
|
// Reset the MX29F1601 flashrom, startbank is 0xC0 for first and 0xE0 for second flashrom
|
|
|
|
|
void resetFlash_SFM(int startBank) {
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
|
|
|
|
|
// Reset command sequence
|
|
|
|
|
if (romType) {
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xf0);
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0, 0x8000 + 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xf0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print flashrom manufacturer and device ID
|
|
|
|
|
void idFlash_SFM(int startBank) {
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
|
|
|
|
|
if (romType) {
|
|
|
|
|
// ID command sequence
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x90);
|
|
|
|
|
|
|
|
|
|
// Set data pins to input again
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the two id bytes into a string
|
2022-10-22 02:47:50 +02:00
|
|
|
|
flashid = readBank_SFM(startBank, 0x00) << 8;
|
|
|
|
|
flashid |= readBank_SFM(startBank, 0x02);
|
|
|
|
|
sprintf(flashid_str, "%04x", flashid);
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0, 0x8000 + 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0x90);
|
|
|
|
|
|
|
|
|
|
// Set data pins to input again
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the two id bytes into a string
|
2022-10-22 02:47:50 +02:00
|
|
|
|
flashid = readBank_SFM(0, 0x8000) << 8;
|
|
|
|
|
flashid |= readBank_SFM(0, 0x8000 + 0x02);
|
|
|
|
|
sprintf(flashid_str, "%04x", flashid);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the flashroms by reading a file from the SD card, pos defines where in the file the reading/writing should start
|
|
|
|
|
void writeFlash_SFM(int startBank, uint32_t pos) {
|
|
|
|
|
display_Clear();
|
|
|
|
|
print_Msg(F("Writing Bank 0x"));
|
2022-10-31 15:41:29 +01:00
|
|
|
|
println_Msg(startBank, HEX);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
|
if (myFile.open(filePath, O_READ)) {
|
|
|
|
|
|
|
|
|
|
// Seek to a new position in the file
|
|
|
|
|
if (pos != 0)
|
|
|
|
|
myFile.seekCur(pos);
|
|
|
|
|
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
|
|
|
|
|
if (romType) {
|
2022-10-31 15:41:29 +01:00
|
|
|
|
//Initialize progress bar
|
|
|
|
|
uint32_t processedProgressBar = 0;
|
|
|
|
|
uint32_t totalProgressBar = numBanks * 0x10000;
|
|
|
|
|
draw_progressbar(0, totalProgressBar);
|
|
|
|
|
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Write hirom
|
2022-10-31 15:41:29 +01:00
|
|
|
|
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Fill SDBuffer with 1 page at a time then write it repeat until all bytes are written
|
|
|
|
|
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 128) {
|
|
|
|
|
myFile.read(sdBuffer, 128);
|
|
|
|
|
// Write command sequence
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xa0);
|
|
|
|
|
|
|
|
|
|
for (byte c = 0; c < 128; c++) {
|
|
|
|
|
|
|
|
|
|
// Write one byte of data
|
|
|
|
|
writeBank_SFM(currBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
|
|
|
|
|
if (c == 127) {
|
|
|
|
|
// Write the last byte twice or else it won't write at all
|
|
|
|
|
writeBank_SFM(currBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Wait until write is finished
|
|
|
|
|
busyCheck_SFM(startBank);
|
|
|
|
|
}
|
2022-10-31 15:41:29 +01:00
|
|
|
|
// update progress bar
|
|
|
|
|
processedProgressBar += 0x10000;
|
|
|
|
|
draw_progressbar(processedProgressBar, totalProgressBar);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-31 15:41:29 +01:00
|
|
|
|
//Initialize progress bar
|
|
|
|
|
uint32_t processedProgressBar = 0;
|
|
|
|
|
uint32_t totalProgressBar = numBanks * 0x8000;
|
|
|
|
|
draw_progressbar(0, totalProgressBar);
|
|
|
|
|
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// Write lorom
|
2022-11-01 08:43:42 +01:00
|
|
|
|
for (word currBank = 0; currBank < numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0x8000; currByte < 0x10000; currByte += 128) {
|
|
|
|
|
myFile.read(sdBuffer, 128);
|
|
|
|
|
// Write command sequence
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0, 0x8000 + 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xa0);
|
|
|
|
|
|
|
|
|
|
for (byte c = 0; c < 128; c++) {
|
|
|
|
|
// Write one byte of data
|
|
|
|
|
writeBank_SFM(currBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
|
|
|
|
|
if (c == 127) {
|
|
|
|
|
// Write the last byte twice or else it won't write at all
|
|
|
|
|
writeBank_SFM(currBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Wait until write is finished
|
|
|
|
|
busyCheck_SFM(startBank);
|
|
|
|
|
}
|
2022-10-31 15:41:29 +01:00
|
|
|
|
// update progress bar
|
|
|
|
|
processedProgressBar += 0x8000;
|
|
|
|
|
draw_progressbar(processedProgressBar, totalProgressBar);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
|
|
|
|
println_Msg("");
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(open_file_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delay between write operations based on status register
|
|
|
|
|
void busyCheck_SFM(byte startBank) {
|
|
|
|
|
// Set data pins to input
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input and therefore pull CE low and latch status register content
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read register
|
|
|
|
|
readBank_SFM(startBank, 0x0000);
|
|
|
|
|
|
|
|
|
|
// Read D7 while D7 = 0
|
|
|
|
|
//1 = B00000001, 1 << 7 = B10000000, PINC = B1XXXXXXX (X = don't care), & = bitwise and
|
|
|
|
|
while (!(PINC & (1 << 7))) {
|
|
|
|
|
// CE or OE must be toggled with each subsequent status read or the
|
|
|
|
|
// completion of a program or erase operation will not be evident.
|
|
|
|
|
// Switch RD(PH6) to HIGH
|
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
|
|
|
|
|
|
// one nop ~62.5ns
|
|
|
|
|
__asm__("nop\n\t");
|
|
|
|
|
|
|
|
|
|
// Switch RD(PH6) to LOW
|
|
|
|
|
PORTH &= ~(1 << 6);
|
|
|
|
|
|
|
|
|
|
// one nop ~62.5ns
|
|
|
|
|
__asm__("nop\n\t");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Erase the flashrom to 0xFF
|
|
|
|
|
void eraseFlash_SFM(int startBank) {
|
|
|
|
|
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
|
|
|
|
|
if (romType) {
|
|
|
|
|
// Erase command sequence
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x80);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x10);
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0, 0x8000 + 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0x80);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0, 0x8000 + 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(1, 0x8000 + 0x1555L * 2, 0x10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for erase to complete
|
|
|
|
|
busyCheck_SFM(startBank);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if an erase succeeded, return 1 if blank and 0 if not
|
|
|
|
|
byte blankcheck_SFM(int startBank) {
|
|
|
|
|
|
|
|
|
|
// Set data pins to input again
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
byte blank = 1;
|
|
|
|
|
if (romType) {
|
2022-10-31 15:41:29 +01:00
|
|
|
|
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0; currByte < 0x10000; currByte++) {
|
|
|
|
|
if (readBank_SFM(currBank, currByte) != 0xFF) {
|
2022-10-13 09:49:03 +02:00
|
|
|
|
currBank = startBank + numBanks;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
blank = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-11-01 08:43:42 +01:00
|
|
|
|
for (word currBank = 0; currBank < numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0x8000; currByte < 0x10000; currByte++) {
|
|
|
|
|
if (readBank_SFM(currBank, currByte) != 0xFF) {
|
|
|
|
|
currBank = numBanks;
|
|
|
|
|
blank = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return blank;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if a write succeeded, returns 0 if all is ok and number of errors if not
|
|
|
|
|
unsigned long verifyFlash_SFM(int startBank, uint32_t pos) {
|
2022-10-13 09:49:03 +02:00
|
|
|
|
unsigned long verified = 0;
|
2021-01-27 08:05:25 +01:00
|
|
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
|
if (myFile.open(filePath, O_READ)) {
|
|
|
|
|
|
|
|
|
|
// Set file starting position
|
|
|
|
|
myFile.seekCur(pos);
|
|
|
|
|
|
|
|
|
|
// Set data pins to input
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
if (romType) {
|
2022-10-31 15:41:29 +01:00
|
|
|
|
for (word currBank = startBank; currBank < startBank + numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 512) {
|
|
|
|
|
// Fill SDBuffer
|
|
|
|
|
myFile.read(sdBuffer, 512);
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
if (readBank_SFM(currBank, currByte + c) != sdBuffer[c]) {
|
|
|
|
|
verified++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-11-01 08:43:42 +01:00
|
|
|
|
for (word currBank = 0; currBank < numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0x8000; currByte < 0x10000; currByte += 512) {
|
|
|
|
|
// Fill SDBuffer
|
|
|
|
|
myFile.read(sdBuffer, 512);
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
if (readBank_SFM(currBank, currByte + c) != sdBuffer[c]) {
|
|
|
|
|
verified++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
// SD Error
|
|
|
|
|
verified = 999999;
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("Can't open file on SD"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
// Return 0 if verified ok, or number of errors
|
|
|
|
|
return verified;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read flashroms and save them to the SD card
|
|
|
|
|
void readFlash_SFM() {
|
|
|
|
|
// Set data pins to input
|
|
|
|
|
dataIn();
|
|
|
|
|
// Set control pins to input
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
print_Msg(F("Saving as NP/"));
|
|
|
|
|
print_Msg(fileName);
|
|
|
|
|
println_Msg(F("..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(create_file_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
if (romType) {
|
2022-10-31 15:41:29 +01:00
|
|
|
|
for (word currBank = 0xC0; currBank < 0xC0 + numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0; currByte < 0x10000; currByte += 512) {
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
sdBuffer[c] = readBank_SFM(currBank, currByte + c);
|
|
|
|
|
}
|
|
|
|
|
myFile.write(sdBuffer, 512);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-11-01 08:43:42 +01:00
|
|
|
|
for (word currBank = 0; currBank < numBanks; currBank++) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (unsigned long currByte = 0x8000; currByte < 0x10000; currByte += 512) {
|
|
|
|
|
for (int c = 0; c < 512; c++) {
|
|
|
|
|
sdBuffer[c] = readBank_SFM(currBank, currByte + c);
|
|
|
|
|
}
|
|
|
|
|
myFile.write(sdBuffer, 512);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
|
|
|
|
println_Msg("");
|
|
|
|
|
println_Msg(F("Finished reading"));
|
|
|
|
|
display_Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display protected sectors/banks as 0xc2 and unprotected as 0x00
|
|
|
|
|
void readSectorProtection_SFM(byte startBank) {
|
|
|
|
|
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataOut();
|
|
|
|
|
|
|
|
|
|
// Display Sector Protection Status
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x90);
|
|
|
|
|
|
|
|
|
|
// Configure control pins
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
// Set data pins to output
|
|
|
|
|
dataIn();
|
|
|
|
|
display_Clear();
|
|
|
|
|
for (int i = 0; i <= 0x1F; i++) {
|
|
|
|
|
print_Msg(F("Sector: 0x"));
|
|
|
|
|
print_Msg(startBank + i, HEX);
|
|
|
|
|
print_Msg(F(" Sector Protect: 0x"));
|
|
|
|
|
println_Msg(readBank_SFM(startBank + i, 0x04), HEX);
|
|
|
|
|
}
|
|
|
|
|
display_Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the current mapping from the hidden "page buffer" and print it
|
|
|
|
|
void printMapping() {
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset to defaults
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x38);
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0xd0);
|
|
|
|
|
// Read Extended Status Register (GSR and PSR)
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x71);
|
|
|
|
|
// Page Buffer Swap
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x72);
|
|
|
|
|
// Read Page Buffer
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x75);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the mapping out of the first chip
|
|
|
|
|
char buffer[3];
|
|
|
|
|
|
2022-10-22 10:25:37 +02:00
|
|
|
|
for (unsigned int currByte = 0xFF00; currByte < 0xFF50; currByte += 10) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
for (int c = 0; c < 10; c++) {
|
2022-10-13 09:49:03 +02:00
|
|
|
|
itoa(readBank_SFM(0xC0, currByte + c), buffer, 16);
|
2022-10-22 10:25:37 +02:00
|
|
|
|
for (size_t i = 0; i < 2 - strlen(buffer); i++) {
|
2022-06-16 17:17:16 +02:00
|
|
|
|
print_Msg(F("0"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
// Now print the significant bits
|
|
|
|
|
print_Msg(buffer);
|
|
|
|
|
}
|
|
|
|
|
println_Msg("");
|
|
|
|
|
}
|
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xC0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xE0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the current mapping from the hidden "page buffer"
|
|
|
|
|
void readMapping() {
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset to defaults
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x38);
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0xd0);
|
|
|
|
|
// Read Extended Status Register (GSR and PSR)
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x71);
|
|
|
|
|
// Page Buffer Swap
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x72);
|
|
|
|
|
// Read Page Buffer
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x75);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Get name, add extension and convert to char array for sd lib
|
|
|
|
|
EEPROM_readAnything(0, foldern);
|
|
|
|
|
sprintf(fileName, "NP%d", foldern);
|
|
|
|
|
strcat(fileName, ".MAP");
|
|
|
|
|
sd.mkdir("NP", true);
|
|
|
|
|
sd.chdir("NP");
|
|
|
|
|
|
|
|
|
|
// write new folder number back to eeprom
|
|
|
|
|
foldern = foldern + 1;
|
|
|
|
|
EEPROM_writeAnything(0, foldern);
|
|
|
|
|
|
|
|
|
|
//open file on sd card
|
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(sd_error_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the mapping info out of the 1st chip
|
|
|
|
|
for (unsigned long currByte = 0xFF00; currByte <= 0xFFFF; currByte++) {
|
|
|
|
|
myFile.write(readBank_SFM(0xC0, currByte));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset to defaults
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x38);
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0xd0);
|
|
|
|
|
// Read Extended Status Register (GSR and PSR)
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x71);
|
|
|
|
|
// Page Buffer Swap
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x72);
|
|
|
|
|
// Read Page Buffer
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x75);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the mapping info out of the 1st chip
|
|
|
|
|
for (unsigned long currByte = 0xFF00; currByte <= 0xFFFF; currByte++) {
|
|
|
|
|
myFile.write(readBank_SFM(0xE0, currByte));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xC0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xE0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Signal end of process
|
|
|
|
|
print_Msg(F("Saved to NP/"));
|
|
|
|
|
println_Msg(fileName);
|
|
|
|
|
display_Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void eraseMapping(byte startBank) {
|
|
|
|
|
|
|
|
|
|
if (unlockHirom()) {
|
|
|
|
|
// Get ID
|
|
|
|
|
idFlash_SFM(startBank);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
resetFlash_SFM(startBank);
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Prepare to erase/write Page Buffer
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x77);
|
|
|
|
|
// Erase Page Buffer
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xe0);
|
|
|
|
|
|
|
|
|
|
// Wait until complete
|
|
|
|
|
busyCheck_SFM(startBank);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Unlock failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the current mapping is all 0xFF
|
|
|
|
|
byte blankcheckMapping_SFM() {
|
|
|
|
|
byte blank = 1;
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset to defaults
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x38);
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0xd0);
|
|
|
|
|
// Read Extended Status Register (GSR and PSR)
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x71);
|
|
|
|
|
// Page Buffer Swap
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x72);
|
|
|
|
|
// Read Page Buffer
|
|
|
|
|
writeBank_SFM(0xC0, 0x0000, 0x75);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the mapping info out of the 1st chip
|
|
|
|
|
for (unsigned long currByte = 0xFF00; currByte <= 0xFFFF; currByte++) {
|
|
|
|
|
if (readBank_SFM(0xC0, currByte) != 0xFF) {
|
|
|
|
|
blank = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset to defaults
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x38);
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0xd0);
|
|
|
|
|
// Read Extended Status Register (GSR and PSR)
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x71);
|
|
|
|
|
// Page Buffer Swap
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x72);
|
|
|
|
|
// Read Page Buffer
|
|
|
|
|
writeBank_SFM(0xE0, 0x0000, 0x75);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read the mapping info out of the 1st chip
|
|
|
|
|
for (unsigned long currByte = 0xFF00; currByte <= 0xFFFF; currByte++) {
|
|
|
|
|
if (readBank_SFM(0xE0, currByte) != 0xFF) {
|
|
|
|
|
blank = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xC0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xC0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Reset Flash
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(0xE0, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(0xE0, 0x5555L * 2, 0xf0);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
return blank;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeMapping_SFM(byte startBank, uint32_t pos) {
|
|
|
|
|
|
|
|
|
|
if (unlockHirom()) {
|
|
|
|
|
// Get ID
|
|
|
|
|
idFlash_SFM(startBank);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
resetFlash_SFM(startBank);
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
|
if (myFile.open(filePath, O_READ)) {
|
|
|
|
|
|
|
|
|
|
// Seek to a new position in the file
|
|
|
|
|
if (pos != 0)
|
|
|
|
|
myFile.seekCur(pos);
|
|
|
|
|
|
|
|
|
|
// Write to Page Buffer
|
|
|
|
|
for (unsigned long currByte = 0xFF00; currByte < 0xFFFF; currByte += 128) {
|
|
|
|
|
// Prepare to erase/write Page Buffer
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x77);
|
|
|
|
|
// Write Page Buffer Command
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0xaa);
|
|
|
|
|
writeBank_SFM(startBank, 0x2AAAL * 2, 0x55);
|
|
|
|
|
writeBank_SFM(startBank, 0x5555L * 2, 0x99);
|
|
|
|
|
|
|
|
|
|
myFile.read(sdBuffer, 128);
|
|
|
|
|
|
|
|
|
|
for (byte c = 0; c < 128; c++) {
|
|
|
|
|
writeBank_SFM(startBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
// Write last byte twice
|
|
|
|
|
if (c == 127) {
|
|
|
|
|
writeBank_SFM(startBank, currByte + c, sdBuffer[c]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
busyCheck_SFM(startBank);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the file:
|
|
|
|
|
myFile.close();
|
|
|
|
|
println_Msg("");
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_Error(F("Can't open file on SD"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Unlock failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
|
SF Memory functions
|
|
|
|
|
*****************************************/
|
|
|
|
|
// Switch to HiRom All and unlock Write Protection
|
|
|
|
|
boolean unlockHirom() {
|
|
|
|
|
romType = 1;
|
|
|
|
|
print_Msg(F("Switch to HiRom..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (send_SFM(0x04) == 0x2A) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
// Unlock Write Protection
|
|
|
|
|
print_Msg(F("Enable Write..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
send_SFM(0x02);
|
|
|
|
|
if (readBank_SFM(0, 0x2401) == 0x4) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
return 1;
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(F("failed"));
|
|
|
|
|
display_Update();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(F("failed"));
|
|
|
|
|
display_Update();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send a command to the MX15001 chip
|
|
|
|
|
byte send_SFM(byte command) {
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
// Write command
|
|
|
|
|
writeBank_SFM(0, 0x2400, 0x09);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read status
|
|
|
|
|
sfmReady = readBank_SFM(0, 0x2400);
|
|
|
|
|
|
|
|
|
|
// Switch to write
|
|
|
|
|
dataOut();
|
|
|
|
|
controlOut_SFM();
|
|
|
|
|
|
|
|
|
|
writeBank_SFM(0, 0x2401, 0x28);
|
|
|
|
|
writeBank_SFM(0, 0x2401, 0x84);
|
|
|
|
|
|
|
|
|
|
// NP_CMD_06h, send this only if above read has returned 7Dh, not if it's already returning 2Ah
|
|
|
|
|
if (sfmReady == 0x7D) {
|
|
|
|
|
writeBank_SFM(0, 0x2400, 0x06);
|
|
|
|
|
writeBank_SFM(0, 0x2400, 0x39);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the command
|
|
|
|
|
writeBank_SFM(0, 0x2400, command);
|
|
|
|
|
|
|
|
|
|
// Switch to read
|
|
|
|
|
dataIn();
|
|
|
|
|
controlIn_SFM();
|
|
|
|
|
|
|
|
|
|
// Read status
|
|
|
|
|
sfmReady = readBank_SFM(0, 0x2400);
|
|
|
|
|
return sfmReady;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function will erase and program the NP cart from a 4MB file off the SD card
|
|
|
|
|
void write_SFM(int startBank, uint32_t pos) {
|
|
|
|
|
// Switch NP cart's mapping
|
|
|
|
|
if (unlockHirom()) {
|
|
|
|
|
// Get ID
|
|
|
|
|
idFlash_SFM(startBank);
|
2022-10-22 02:47:50 +02:00
|
|
|
|
if (flashid == 0xc2f3) {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
print_Msg(F("Flash ID: "));
|
2022-10-22 02:47:50 +02:00
|
|
|
|
println_Msg(flashid_str);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
resetFlash_SFM(startBank);
|
|
|
|
|
delay(1000);
|
|
|
|
|
// Erase flash
|
|
|
|
|
print_Msg(F("Blankcheck..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (blankcheck_SFM(startBank)) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2021-01-27 08:05:25 +01:00
|
|
|
|
println_Msg(F("Nope"));
|
|
|
|
|
display_Clear();
|
|
|
|
|
print_Msg(F("Erasing..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
eraseFlash_SFM(startBank);
|
|
|
|
|
resetFlash_SFM(startBank);
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(done_STR, 1);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
print_Msg(F("Blankcheck..."));
|
|
|
|
|
display_Update();
|
|
|
|
|
if (blankcheck_SFM(startBank)) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Could not erase flash"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Write flash
|
|
|
|
|
writeFlash_SFM(startBank, pos);
|
|
|
|
|
|
|
|
|
|
// Reset flash
|
|
|
|
|
resetFlash_SFM(startBank);
|
|
|
|
|
|
|
|
|
|
// Checking for errors
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(verifying_STR, 0);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
|
|
|
|
writeErrors = verifyFlash_SFM(startBank, pos);
|
|
|
|
|
if (writeErrors == 0) {
|
2024-03-02 17:26:35 +01:00
|
|
|
|
println_Msg(FS(FSTRING_OK));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
display_Update();
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(error_STR, 0);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
print_Msg(writeErrors);
|
2022-10-21 15:59:06 +02:00
|
|
|
|
print_STR(_bytes_STR, 1);
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(did_not_verify_STR);
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Error: Wrong Flash ID"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
2022-10-13 09:49:03 +02:00
|
|
|
|
} else {
|
2022-10-30 03:21:01 +01:00
|
|
|
|
print_FatalError(F("Unlock failed"));
|
2021-01-27 08:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//******************************************
|
|
|
|
|
// End of File
|
2022-10-28 15:02:51 +02:00
|
|
|
|
//******************************************
|