2020-05-12 13:52:03 +02:00
|
|
|
//******************************************
|
|
|
|
// NGP MODULE
|
|
|
|
//******************************************
|
2024-03-02 17:26:35 +01:00
|
|
|
#ifdef ENABLE_NGP
|
2020-07-04 17:07:14 +02:00
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
static const char ngpMenuItem2[] PROGMEM = "Read chip info";
|
2024-03-02 17:26:35 +01:00
|
|
|
static const char* const menuOptionsNGP[] PROGMEM = { FSTRING_READ_ROM, ngpMenuItem2, FSTRING_SET_SIZE, FSTRING_RESET };
|
2020-05-12 13:52:03 +02:00
|
|
|
|
2021-08-19 17:38:16 +02:00
|
|
|
static const char ngpRomItem1[] PROGMEM = "4 Mbits / 512 KB";
|
|
|
|
static const char ngpRomItem2[] PROGMEM = "8 Mbits / 1 MB";
|
|
|
|
static const char ngpRomItem3[] PROGMEM = "16 Mbits / 2 MB";
|
|
|
|
static const char ngpRomItem4[] PROGMEM = "32 Mbits / 4 MB";
|
2022-10-13 09:49:03 +02:00
|
|
|
static const char* const ngpRomOptions[] PROGMEM = { ngpRomItem1, ngpRomItem2, ngpRomItem3, ngpRomItem4 };
|
2021-08-18 01:00:07 +02:00
|
|
|
|
2021-07-09 00:24:21 +02:00
|
|
|
char ngpRomVersion[3];
|
|
|
|
uint8_t ngpSystemType;
|
2021-08-17 01:23:33 +02:00
|
|
|
uint8_t manufacturerID;
|
|
|
|
uint8_t deviceID;
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
void setup_NGP() {
|
2023-06-26 21:25:54 +02:00
|
|
|
// Request 3.3V
|
2023-06-26 12:04:00 +02:00
|
|
|
setVoltage(VOLTS_SET_3V3);
|
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
// A0 - A7
|
|
|
|
DDRF = 0xff;
|
|
|
|
// A8 - A15
|
|
|
|
DDRK = 0xff;
|
|
|
|
// A16 - A20
|
|
|
|
DDRL = 0xff;
|
|
|
|
|
|
|
|
// D0 - D7
|
|
|
|
DDRC = 0x00;
|
|
|
|
|
|
|
|
// controls
|
|
|
|
// /CE0: PH3
|
|
|
|
// /CE1: PH0
|
|
|
|
// /OE: PH6
|
|
|
|
// /WE: PH5
|
|
|
|
// PWR: PH4
|
|
|
|
DDRH |= ((1 << 0) | (1 << 3) | (1 << 5) | (1 << 6));
|
|
|
|
DDRH &= ~(1 << 4);
|
|
|
|
|
|
|
|
PORTH |= ((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6));
|
|
|
|
|
2021-08-19 21:09:19 +02:00
|
|
|
println_Msg(F("Initializing..."));
|
|
|
|
display_Update();
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
if (getCartInfo_NGP())
|
2021-08-17 01:23:33 +02:00
|
|
|
printCartInfo_NGP();
|
2021-08-19 21:09:19 +02:00
|
|
|
else
|
2022-10-30 03:21:01 +01:00
|
|
|
print_FatalError(F("Cartridge read error"));
|
2020-05-12 13:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ngpMenu() {
|
|
|
|
uint8_t mainMenu;
|
|
|
|
|
2023-02-06 01:08:26 +01:00
|
|
|
convertPgm(menuOptionsNGP, 4);
|
|
|
|
mainMenu = question_box(F("NGP Menu"), menuOptions, 4, 0);
|
2020-05-12 13:52:03 +02:00
|
|
|
|
2021-08-18 01:00:07 +02:00
|
|
|
switch (mainMenu) {
|
2020-05-12 13:52:03 +02:00
|
|
|
case 0:
|
|
|
|
sd.chdir("/");
|
|
|
|
readROM_NGP(filePath, FILEPATH_LENGTH);
|
|
|
|
sd.chdir("/");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2021-08-26 00:52:28 +02:00
|
|
|
scanChip_NGP();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-02-06 01:08:26 +01:00
|
|
|
changeSize_NGP();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2020-05-12 13:52:03 +02:00
|
|
|
resetArduino();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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);
|
2020-05-12 13:52:03 +02:00
|
|
|
display_Update();
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getCartInfo_NGP() {
|
2022-10-13 09:49:03 +02:00
|
|
|
uint8_t* tmp;
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
// enter autoselect mode
|
|
|
|
dataOut();
|
|
|
|
writeByte_NGP(0x555, 0xaa);
|
|
|
|
writeByte_NGP(0x2aa, 0x55);
|
|
|
|
writeByte_NGP(0x555, 0x90);
|
|
|
|
|
|
|
|
dataIn();
|
|
|
|
cartSize = 0;
|
2021-10-14 09:53:07 +02:00
|
|
|
|
|
|
|
// get chip manufacturer and device IDs
|
2021-08-17 01:23:33 +02:00
|
|
|
manufacturerID = readByte_NGP(0);
|
|
|
|
deviceID = readByte_NGP(1);
|
2020-05-12 13:52:03 +02:00
|
|
|
tmp = (uint8_t*)&romSize;
|
2021-08-17 01:23:33 +02:00
|
|
|
*(tmp + 0) = deviceID;
|
|
|
|
*(tmp + 1) = manufacturerID;
|
2020-05-12 13:52:03 +02:00
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
|
2021-08-18 01:00:07 +02:00
|
|
|
switch (romSize) {
|
2023-02-06 01:08:26 +01:00
|
|
|
// 4 Mbits
|
|
|
|
case 0x98ab: // Toshiba
|
|
|
|
case 0x204c: // STMicroelectronics ?
|
2023-02-23 12:04:33 +01:00
|
|
|
cartSize = 524288;
|
|
|
|
break;
|
2023-02-06 01:08:26 +01:00
|
|
|
|
|
|
|
// 8 Mbits
|
|
|
|
case 0x982c: // Toshiba
|
|
|
|
case 0xec2c: // Samsung
|
2023-02-23 12:04:33 +01:00
|
|
|
cartSize = 1048576;
|
|
|
|
break;
|
2023-02-06 01:08:26 +01:00
|
|
|
|
|
|
|
// 16 Mbits
|
|
|
|
case 0x982f: // Toshiba
|
|
|
|
case 0xec2f: // Samsung
|
|
|
|
case 0x4c7: // Fujitsu (FlashMasta USB)
|
2023-02-23 12:04:33 +01:00
|
|
|
cartSize = 2097152;
|
|
|
|
break;
|
2023-02-06 01:08:26 +01:00
|
|
|
|
|
|
|
// detection error (no cart inserted or hw problem)
|
|
|
|
case 0xffff:
|
2023-02-23 12:04:33 +01:00
|
|
|
return false;
|
|
|
|
break;
|
2020-05-12 13:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset to read mode
|
|
|
|
dataOut();
|
2021-08-17 01:23:33 +02:00
|
|
|
writeByte_NGP(0x0, 0xf0);
|
2020-05-12 13:52:03 +02:00
|
|
|
|
2021-08-17 01:23:33 +02:00
|
|
|
// confirm NGP cart recognition
|
2021-08-19 17:38:16 +02:00
|
|
|
dataIn();
|
2020-05-12 13:52:03 +02:00
|
|
|
for (uint32_t addr = 0; addr < 28; addr++)
|
|
|
|
sdBuffer[addr] = readByte_NGP(addr);
|
|
|
|
if (memcmp_P(sdBuffer, PSTR("COPYRIGHT BY SNK CORPORATION"), 28) != 0 && memcmp_P(sdBuffer, PSTR(" LICENSED BY SNK CORPORATION"), 28) != 0)
|
|
|
|
return false;
|
|
|
|
|
2021-08-17 01:23:33 +02:00
|
|
|
// get app ID
|
|
|
|
snprintf(cartID, 5, "%02X%02X", readByte_NGP(0x21), readByte_NGP(0x20));
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2021-08-19 17:38:16 +02:00
|
|
|
// force rom size to 32 Mbits for few titles
|
2022-10-13 09:49:03 +02:00
|
|
|
if (strcmp(cartID, "0060") == 0 || strcmp(cartID, "0061") == 0 || strcmp(cartID, "0069") == 0)
|
2021-08-18 01:00:07 +02:00
|
|
|
cartSize = 4194304;
|
2021-10-14 09:53:07 +02:00
|
|
|
|
|
|
|
// get app version
|
2021-08-17 01:23:33 +02:00
|
|
|
snprintf(ngpRomVersion, 3, "%02X", readByte_NGP(0x22));
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2021-08-17 01:23:33 +02:00
|
|
|
// get app system compatibility
|
|
|
|
ngpSystemType = readByte_NGP(0x23);
|
|
|
|
|
|
|
|
// get app name
|
2023-02-13 13:58:39 +01:00
|
|
|
for (uint32_t i = 0; i < 17; i++) {
|
2020-05-12 13:52:03 +02:00
|
|
|
romName[i] = readByte_NGP(0x24 + i);
|
|
|
|
|
2023-02-23 12:04:33 +01:00
|
|
|
// replace '/' chars in game name to avoid path errors
|
2023-02-13 13:58:39 +01:00
|
|
|
if (romName[i] == '/')
|
|
|
|
romName[i] = '_';
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:23:33 +02:00
|
|
|
void printCartInfo_NGP() {
|
2020-05-12 13:52:03 +02:00
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
println_Msg(F("NGP Cart Info"));
|
|
|
|
|
2021-08-17 01:23:33 +02:00
|
|
|
print_Msg(F("Name: "));
|
2020-05-12 13:52:03 +02:00
|
|
|
println_Msg(romName);
|
|
|
|
|
2022-07-18 11:24:23 +02:00
|
|
|
print_Msg(F("App ID: "));
|
2020-05-12 13:52:03 +02:00
|
|
|
println_Msg(cartID);
|
|
|
|
|
2022-07-18 11:24:23 +02:00
|
|
|
print_Msg(F("App version: "));
|
2020-05-12 13:52:03 +02:00
|
|
|
println_Msg(ngpRomVersion);
|
|
|
|
|
2021-07-09 00:24:21 +02:00
|
|
|
print_Msg(F("System: "));
|
2021-08-17 01:23:33 +02:00
|
|
|
if (ngpSystemType == 0x0)
|
2021-08-01 23:33:59 +02:00
|
|
|
println_Msg(F("NGPMonochrome"));
|
2021-08-17 01:23:33 +02:00
|
|
|
else if (ngpSystemType == 0x10)
|
2021-08-01 23:33:59 +02:00
|
|
|
println_Msg(F("NGPColor"));
|
2021-07-09 00:24:21 +02:00
|
|
|
else
|
2021-08-01 23:33:59 +02:00
|
|
|
println_Msg(F("Unknown"));
|
2021-07-09 00:24:21 +02:00
|
|
|
|
2022-07-18 11:24:23 +02:00
|
|
|
print_Msg(F("ROM Size: "));
|
2021-08-18 01:00:07 +02:00
|
|
|
if (cartSize == 0) {
|
2021-08-17 01:23:33 +02:00
|
|
|
println_Msg(F("Unknown"));
|
2022-10-13 09:49:03 +02:00
|
|
|
} else {
|
2021-08-17 01:23:33 +02:00
|
|
|
print_Msg((cartSize >> 17));
|
|
|
|
println_Msg(F(" Mbits"));
|
|
|
|
}
|
2021-08-01 23:33:59 +02:00
|
|
|
|
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);
|
2020-05-12 13:52:03 +02:00
|
|
|
display_Update();
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:49:03 +02:00
|
|
|
void readROM_NGP(char* outPathBuf, size_t bufferSize) {
|
2023-02-06 01:08:26 +01:00
|
|
|
// Set rom size manually if chip ID is unknown
|
|
|
|
if (cartSize == 0)
|
|
|
|
changeSize_NGP();
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
// generate fullname of rom file
|
2024-05-12 15:37:11 +02:00
|
|
|
createFolder("NGP", "ROM", romName, "ngp");
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
// filling output file path to buffer
|
|
|
|
if (outPathBuf != NULL && bufferSize > 0)
|
|
|
|
snprintf(outPathBuf, bufferSize, "%s/%s", folder, fileName);
|
|
|
|
|
|
|
|
display_Clear();
|
2024-05-25 14:07:08 +02:00
|
|
|
printAndIncrementFolder();
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
// open file on sdcard
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT))
|
2022-10-30 03:21:01 +01:00
|
|
|
print_FatalError(create_file_STR);
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
// back to read mode
|
|
|
|
dataOut();
|
|
|
|
writeByte_NGP(0x0, 0xf0);
|
|
|
|
|
2021-08-18 01:00:07 +02:00
|
|
|
// read rom
|
2020-05-12 13:52:03 +02:00
|
|
|
dataIn();
|
2022-07-18 11:24:23 +02:00
|
|
|
uint32_t progress = 0;
|
|
|
|
draw_progressbar(0, cartSize);
|
2020-05-12 13:52:03 +02:00
|
|
|
for (uint32_t addr = 0; addr < cartSize; addr += 512) {
|
|
|
|
// blink LED
|
|
|
|
if ((addr & ((1 << 14) - 1)) == 0)
|
2021-10-26 17:13:42 +02:00
|
|
|
blinkLED();
|
2020-05-12 13:52:03 +02:00
|
|
|
|
2021-08-18 01:00:07 +02:00
|
|
|
// read block
|
2020-05-12 13:52:03 +02:00
|
|
|
for (uint32_t i = 0; i < 512; i++)
|
|
|
|
sdBuffer[i] = readByte_NGP(addr + i);
|
|
|
|
|
|
|
|
myFile.write(sdBuffer, 512);
|
2022-07-18 11:24:23 +02:00
|
|
|
progress += 512;
|
|
|
|
draw_progressbar(progress, cartSize);
|
2020-05-12 13:52:03 +02:00
|
|
|
}
|
2022-07-23 11:04:17 +02:00
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
myFile.close();
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
void scanChip_NGP() {
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
// generate name of report file
|
2024-05-12 15:37:11 +02:00
|
|
|
createFolder("NGP", "ROM", romName, "txt");
|
2021-08-26 00:52:28 +02:00
|
|
|
|
|
|
|
print_Msg(F("Saving chip report to "));
|
|
|
|
print_Msg(folder);
|
|
|
|
println_Msg(F("/..."));
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
// open file on sdcard
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT))
|
2022-10-30 03:21:01 +01:00
|
|
|
print_FatalError(create_file_STR);
|
2021-08-26 00:52:28 +02:00
|
|
|
|
|
|
|
// write new folder number back to EEPROM
|
|
|
|
foldern++;
|
|
|
|
EEPROM_writeAnything(0, foldern);
|
|
|
|
|
|
|
|
// write software info to report file
|
2022-07-18 11:24:23 +02:00
|
|
|
myFile.println("Name: " + String(romName));
|
|
|
|
myFile.println("App ID: " + String(cartID));
|
|
|
|
myFile.println("App version: " + String(ngpRomVersion));
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("");
|
|
|
|
|
|
|
|
// write chip info to report file
|
2021-10-14 09:53:07 +02:00
|
|
|
myFile.println("Chip manufacturer ID : 0x" + String(manufacturerID, HEX));
|
|
|
|
myFile.println("Chip device ID : 0x" + String(deviceID, HEX));
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("");
|
|
|
|
|
2021-10-14 09:53:07 +02:00
|
|
|
if (cartSize == 0)
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("Cart size unknown");
|
|
|
|
else {
|
|
|
|
// enter autoselect mode
|
|
|
|
dataOut();
|
|
|
|
writeByte_NGP(0x555, 0xaa);
|
|
|
|
writeByte_NGP(0x2aa, 0x55);
|
|
|
|
writeByte_NGP(0x555, 0x90);
|
|
|
|
|
|
|
|
dataIn();
|
|
|
|
uint32_t addrMax;
|
|
|
|
uint8_t sectorID = 0;
|
|
|
|
|
2022-07-18 11:24:23 +02:00
|
|
|
// For 32Mbits carts, skip the 2nd 16Mbits chip
|
2021-10-14 09:53:07 +02:00
|
|
|
if (cartSize == 4194304) {
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("Warning: this cart is 32Mbits. Only the first 16Mbits chip will be scanned.");
|
|
|
|
myFile.println("");
|
|
|
|
addrMax = 2097152;
|
2022-10-13 09:49:03 +02:00
|
|
|
} else
|
2021-08-26 00:52:28 +02:00
|
|
|
addrMax = cartSize;
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("Sector | Start address | Status");
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
// browse sectors
|
2021-10-14 09:53:07 +02:00
|
|
|
for (uint32_t addr = 0; addr < addrMax; addr += 0x1000) {
|
|
|
|
|
2022-10-13 09:49:03 +02:00
|
|
|
if ((addr % 0x10000 == 0) || (addr == addrMax - 0x8000) || (addr == addrMax - 0x6000) || (addr == addrMax - 0x4000)) {
|
2021-10-14 09:53:07 +02:00
|
|
|
|
|
|
|
myFile.print("#" + String(sectorID) + " | 0x" + String(addr, HEX) + " | ");
|
2021-08-26 00:52:28 +02:00
|
|
|
|
|
|
|
// check the protection status
|
2021-10-14 09:53:07 +02:00
|
|
|
if (readByte_NGP(addr + 0x2) == 0)
|
2021-08-26 00:52:28 +02:00
|
|
|
myFile.println("unprotected");
|
|
|
|
else
|
|
|
|
myFile.println("protected");
|
2021-10-14 09:53:07 +02:00
|
|
|
|
2021-08-26 00:52:28 +02:00
|
|
|
sectorID += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
myFile.close();
|
|
|
|
writeByte_NGP(0x00, 0xf0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 01:08:26 +01:00
|
|
|
void changeSize_NGP() {
|
|
|
|
unsigned char ngpRomMenu;
|
|
|
|
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
convertPgm(ngpRomOptions, 4);
|
|
|
|
ngpRomMenu = question_box(F("Select ROM size"), menuOptions, 4, 0);
|
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
|
|
|
switch (ngpRomMenu) {
|
|
|
|
case 0: cartSize = 524288; break;
|
|
|
|
case 1: cartSize = 1048576; break;
|
|
|
|
case 2: cartSize = 2097152; break;
|
|
|
|
case 3: cartSize = 4194304; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:52:03 +02:00
|
|
|
void writeByte_NGP(uint32_t addr, uint8_t data) {
|
|
|
|
PORTF = addr & 0xff;
|
|
|
|
PORTK = (addr >> 8) & 0xff;
|
|
|
|
PORTL = (addr >> 16) & 0x1f;
|
|
|
|
PORTC = data;
|
|
|
|
|
|
|
|
// which chip to select
|
|
|
|
// 0x000000 - 0x1fffff -> /CE0
|
|
|
|
// 0x200000 - 0x3fffff -> /CE1
|
|
|
|
data = (addr & 0x00200000 ? (1 << 0) : (1 << 3));
|
|
|
|
|
|
|
|
PORTH &= ~data;
|
|
|
|
PORTH &= ~(1 << 5);
|
|
|
|
NOP;
|
|
|
|
|
|
|
|
PORTH |= data;
|
|
|
|
PORTH |= (1 << 5);
|
2022-10-13 09:49:03 +02:00
|
|
|
NOP;
|
|
|
|
NOP;
|
2020-05-12 13:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t readByte_NGP(uint32_t addr) {
|
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
PORTF = addr & 0xff;
|
|
|
|
PORTK = (addr >> 8) & 0xff;
|
|
|
|
PORTL = (addr >> 16) & 0x1f;
|
|
|
|
|
|
|
|
// which chip to select
|
|
|
|
// 0x000000 - 0x1fffff -> /CE0
|
|
|
|
// 0x200000 - 0x3fffff -> /CE1
|
|
|
|
data = (addr & 0x00200000 ? (1 << 0) : (1 << 3));
|
|
|
|
|
|
|
|
PORTH &= ~data;
|
|
|
|
PORTH &= ~(1 << 6);
|
2022-10-13 09:49:03 +02:00
|
|
|
NOP;
|
|
|
|
NOP;
|
|
|
|
NOP;
|
2020-05-12 13:52:03 +02:00
|
|
|
|
|
|
|
data = PINC;
|
|
|
|
|
|
|
|
PORTH |= data;
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2020-07-04 17:07:14 +02:00
|
|
|
|
2021-07-09 00:24:21 +02:00
|
|
|
#endif
|
2022-09-25 11:09:54 +02:00
|
|
|
//******************************************
|
|
|
|
// End of File
|
2023-02-06 01:08:26 +01:00
|
|
|
//******************************************
|