cartreader/Cart_Reader/SUPRACAN.ino

646 lines
14 KiB
Arduino
Raw Normal View History

2022-12-10 05:29:46 +01:00
//******************************************
// Super A'can MODULE
2023-02-23 06:48:11 +01:00
// Only tested with HW3 and HW5
2022-12-10 05:29:46 +01:00
//******************************************
#ifdef ENABLE_SUPRACAN
2022-12-10 05:29:46 +01:00
/******************************************
Menu
*****************************************/
static const char acanMenuItem4[] PROGMEM = "Read UM6650";
static const char acanMenuItem5[] PROGMEM = "Write UM6650";
2023-02-23 06:48:11 +01:00
static const char acanMenuItem6[] PROGMEM = "Flash repro";
2022-12-10 05:29:46 +01:00
static const char *const menuOptionsAcan[] PROGMEM = { FSTRING_READ_ROM, FSTRING_READ_SAVE, FSTRING_WRITE_SAVE, acanMenuItem4, acanMenuItem5, FSTRING_RESET, acanMenuItem6 };
2023-02-23 06:48:11 +01:00
void setup_SuprAcan() {
// Request 5V
setVoltage(VOLTS_SET_5V);
2022-12-10 05:29:46 +01:00
// addr as output
DDRF = 0xff; // A0 - A7
DDRK = 0xff; // A8 - A15
DDRL = 0xff; // A16 - A23
// data as input
DDRC = 0xff;
DDRA = 0xff;
2023-02-23 06:48:11 +01:00
PORTC = 0x00; // disable internal pull-up
2022-12-10 05:29:46 +01:00
PORTA = 0x00;
DDRC = 0x00; // D0 - D7
DDRA = 0x00; // D8 - D15
// set /RST(PH0), /CS(PH3), C27(PH4), R/W(PH5), /AS(PH6) output
DDRH |= ((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6));
PORTH |= ((1 << 3) | (1 << 5) | (1 << 6));
PORTH &= ~((1 << 0) | (1 << 4));
// set 6619_124(PE4) input
DDRE &= ~(1 << 4);
2023-01-24 09:34:54 +01:00
// detect if flash chip exists
PORTG |= (1 << 5);
DDRG |= (1 << 5);
PORTG |= (1 << 5);
2023-02-23 06:48:11 +01:00
2023-01-24 09:34:54 +01:00
dataOut_MD();
writeWord_Acan(0xaaaa, 0xaaaa);
writeWord_Acan(0x5555, 0x5555);
writeWord_Acan(0xaaaa, 0x9090);
dataIn_MD();
eepbit[0] = readWord_Acan(0x2);
eepbit[1] = readWord_Acan(0x0);
dataOut_MD();
writeWord_Acan(0x0, 0xf0f0);
dataIn_MD();
// set /CARTIN(PG5) input
PORTG &= ~(1 << 5);
DDRG &= ~(1 << 5);
2023-02-23 06:48:11 +01:00
*((uint32_t *)(eepbit + 4)) = getFlashChipSize_Acan(*((uint16_t *)eepbit));
2023-01-24 09:34:54 +01:00
2022-12-10 05:29:46 +01:00
display_Clear();
initializeClockOffset();
// clockgen
2023-02-23 06:48:11 +01:00
if (i2c_found) {
clockgen.set_freq(1073863500ULL, SI5351_CLK1); // cpu
clockgen.set_freq(357954500ULL, SI5351_CLK2); // subcarrier
clockgen.set_freq(5369317500ULL, SI5351_CLK0); // master clock
2022-12-10 05:29:46 +01:00
clockgen.output_enable(SI5351_CLK1, 1);
clockgen.output_enable(SI5351_CLK2, 1);
clockgen.output_enable(SI5351_CLK0, 0);
// Wait for clock generator
clockgen.update_status();
delay(500);
}
#ifdef ENABLE_CLOCKGEN
2023-02-23 06:48:11 +01:00
else {
2022-12-10 05:29:46 +01:00
print_FatalError(F("Clock Generator not found"));
}
#endif
2023-02-23 06:48:11 +01:00
checkRomExist_Acan();
2022-12-10 05:29:46 +01:00
wait();
}
2023-02-23 06:48:11 +01:00
void suprAcanMenu() {
2023-01-24 09:34:54 +01:00
uint8_t mainMenu = 6;
2023-02-23 06:48:11 +01:00
if (*((uint32_t *)(eepbit + 4)) > 0)
2023-01-24 09:34:54 +01:00
mainMenu = 7;
2022-12-10 05:29:46 +01:00
2023-01-24 09:34:54 +01:00
convertPgm(menuOptionsAcan, mainMenu);
mainMenu = question_box(F("Super A'can Menu"), menuOptions, mainMenu, 0);
2022-12-10 05:29:46 +01:00
2023-02-23 06:48:11 +01:00
switch (mainMenu) {
2022-12-10 05:29:46 +01:00
case 0:
2023-02-23 06:48:11 +01:00
{
readROM_Acan();
break;
}
2022-12-10 05:29:46 +01:00
case 1:
2023-02-23 06:48:11 +01:00
{
readSRAM_Acan();
break;
}
2022-12-10 05:29:46 +01:00
case 2:
2023-02-23 06:48:11 +01:00
{
writeSRAM_Acan();
verifySRAM_Acan();
break;
}
2022-12-10 05:29:46 +01:00
case 3:
2023-02-23 06:48:11 +01:00
{
readUM6650();
break;
}
2022-12-10 05:29:46 +01:00
case 4:
2023-02-23 06:48:11 +01:00
{
writeUM6650();
verifyUM6650();
break;
}
2023-01-24 09:34:54 +01:00
case 6:
2023-02-23 06:48:11 +01:00
{
flashCart_Acan();
break;
}
2022-12-10 05:29:46 +01:00
default:
2023-02-23 06:48:11 +01:00
{
resetCart_Acan();
resetArduino();
break;
}
2022-12-10 05:29:46 +01:00
}
println_Msg(FS(FSTRING_EMPTY));
2022-12-10 05:29:46 +01:00
print_STR(press_button_STR, 1);
display_Update();
wait();
}
2023-02-23 06:48:11 +01:00
static void readROM_Acan() {
2022-12-10 05:29:46 +01:00
uint32_t crc32 = 0xffffffff;
EEPROM_readAnything(0, foldern);
2023-01-24 09:34:54 +01:00
snprintf(folder, FILEPATH_LENGTH, "/ACAN/ROM/%d", foldern);
2022-12-10 05:29:46 +01:00
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
sd.mkdir(folder, true);
sd.chdir(folder);
if (!myFile.open("rom.bin", O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
foldern++;
EEPROM_writeAnything(0, foldern);
draw_progressbar(0, cartSize);
dataIn_MD();
2023-02-23 06:48:11 +01:00
for (uint32_t addr = 0; addr < cartSize; addr += 512, draw_progressbar(addr, cartSize)) {
for (uint32_t i = 0; i < 512; i += 2) {
*((uint16_t *)(sdBuffer + i)) = readWord_Acan(addr + i);
2022-12-10 05:29:46 +01:00
UPDATE_CRC(crc32, sdBuffer[i]);
UPDATE_CRC(crc32, sdBuffer[i + 1]);
}
myFile.write(sdBuffer, 512);
if ((addr & ((1 << 14) - 1)) == 0)
blinkLED();
}
crc32 = ~crc32;
myFile.close();
print_Msg(F("CRC32: "));
print_Msg_PaddedHex32(crc32);
println_Msg(FS(FSTRING_EMPTY));
2022-12-10 05:29:46 +01:00
print_STR(done_STR, 1);
}
2023-02-23 06:48:11 +01:00
static void readSRAM_Acan() {
2022-12-10 05:29:46 +01:00
// create a new folder for storing rom file
EEPROM_readAnything(0, foldern);
2023-01-24 09:34:54 +01:00
snprintf(folder, FILEPATH_LENGTH, "/ACAN/SAVE/%d", foldern);
2022-12-10 05:29:46 +01:00
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
sd.mkdir(folder, true);
sd.chdir(folder);
if (!myFile.open("save.bin", O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
foldern++;
EEPROM_writeAnything(0, foldern);
dataIn_MD();
2023-02-23 06:48:11 +01:00
for (uint32_t i = 0; i < 0x10000; i += 1024) {
2022-12-10 05:29:46 +01:00
for (uint32_t j = 0; j < 1024; j += 2)
sdBuffer[(j >> 1)] = readWord_Acan(0xec0000 + i + j);
myFile.write(sdBuffer, 512);
}
myFile.close();
print_STR(done_STR, 1);
}
2023-02-23 06:48:11 +01:00
static void writeSRAM_Acan() {
2022-12-10 05:29:46 +01:00
filePath[0] = 0;
2023-01-24 09:34:54 +01:00
sd.chdir();
2022-12-10 05:29:46 +01:00
fileBrowser(F("Select a file"));
snprintf(filePath, FILEPATH_LENGTH, "%s/%s", filePath, fileName);
display_Clear();
2023-02-23 06:48:11 +01:00
if (!myFile.open(filePath, O_READ)) {
print_Error(FS(FSTRING_FILE_DOESNT_EXIST));
2022-12-10 05:29:46 +01:00
return;
}
print_Msg(F("Writing "));
print_Msg(filePath);
println_Msg(F("..."));
display_Update();
dataOut_MD();
2023-02-23 06:48:11 +01:00
for (uint32_t i = 0; i < 0x10000 && myFile.available(); i += 1024) {
2022-12-10 05:29:46 +01:00
myFile.read(sdBuffer, 512);
for (uint32_t j = 0; j < 1024; j += 2)
writeWord_Acan(0xec0000 + i + j, sdBuffer[(j >> 1)]);
}
myFile.close();
dataIn_MD();
print_STR(done_STR, 1);
}
2023-02-23 06:48:11 +01:00
static void verifySRAM_Acan() {
2022-12-10 05:29:46 +01:00
print_STR(verifying_STR, 0);
display_Update();
2023-02-23 06:48:11 +01:00
if (!myFile.open(filePath, O_READ)) {
print_Error(FS(FSTRING_FILE_DOESNT_EXIST));
2022-12-10 05:29:46 +01:00
return;
}
uint16_t write_errors = 0;
dataIn_MD();
2023-02-23 06:48:11 +01:00
for (uint32_t i = 0; i < 0x10000 && myFile.available(); i += 1024) {
2022-12-10 05:29:46 +01:00
myFile.read(sdBuffer, 512);
2023-02-23 06:48:11 +01:00
for (uint32_t j = 0; j < 1024; j += 2) {
2022-12-10 05:29:46 +01:00
if (readWord_Acan(0xec0000 + i + j) != sdBuffer[(j >> 1)])
write_errors++;
}
}
myFile.close();
2023-02-23 06:48:11 +01:00
if (write_errors == 0) {
2022-12-10 05:29:46 +01:00
println_Msg(F("passed"));
2023-02-23 06:48:11 +01:00
} else {
2022-12-10 05:29:46 +01:00
println_Msg(F("failed"));
print_Msg(F("Error: "));
print_Msg(write_errors);
println_Msg(F(" bytes "));
print_Error(did_not_verify_STR);
}
}
2023-02-23 06:48:11 +01:00
static void readUM6650() {
2022-12-10 05:29:46 +01:00
// create a new folder for storing rom file
EEPROM_readAnything(0, foldern);
2023-01-24 09:34:54 +01:00
snprintf(folder, sizeof(folder), "/ACAN/UM6650/%d", foldern);
2022-12-10 05:29:46 +01:00
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
sd.mkdir(folder, true);
sd.chdir(folder);
if (!myFile.open("UM6650.bin", O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
foldern++;
EEPROM_writeAnything(0, foldern);
2023-02-23 06:48:11 +01:00
for (uint16_t i = 0; i < 256; i++) {
2022-12-10 05:29:46 +01:00
dataOut_MD();
writeWord_Acan(0xeb0d03, i);
dataIn_MD();
sdBuffer[i] = readWord_Acan(0xeb0d01);
}
myFile.write(sdBuffer, 256);
myFile.close();
print_STR(done_STR, 1);
}
2023-02-23 06:48:11 +01:00
static void verifyUM6650() {
2022-12-10 05:29:46 +01:00
print_STR(verifying_STR, 0);
display_Update();
2023-02-23 06:48:11 +01:00
if (!myFile.open(filePath, O_READ)) {
print_Error(FS(FSTRING_FILE_DOESNT_EXIST));
2022-12-10 05:29:46 +01:00
return;
}
uint16_t write_errors = 0;
uint16_t len = myFile.read(sdBuffer, 256);
myFile.close();
2023-02-23 06:48:11 +01:00
for (uint16_t i = 0; i < len; i++) {
2022-12-10 05:29:46 +01:00
dataOut_MD();
writeWord_Acan(0xeb0d03, i);
dataIn_MD();
if (readWord_Acan(0xeb0d01) != sdBuffer[i])
write_errors++;
}
2023-02-23 06:48:11 +01:00
if (write_errors) {
2022-12-10 05:29:46 +01:00
println_Msg(F("failed"));
print_Msg(F("Error: "));
print_Msg(write_errors);
println_Msg(F(" bytes "));
print_Error(did_not_verify_STR);
2023-02-23 06:48:11 +01:00
} else {
2022-12-10 05:29:46 +01:00
println_Msg(F("passed"));
}
}
2023-02-23 06:48:11 +01:00
static void writeUM6650() {
2022-12-10 05:29:46 +01:00
filePath[0] = 0;
sd.chdir("/");
fileBrowser(F("Select a file"));
snprintf(filePath, FILEPATH_LENGTH, "%s/%s", filePath, fileName);
display_Clear();
2023-02-23 06:48:11 +01:00
if (!myFile.open(filePath, O_READ)) {
print_Error(FS(FSTRING_FILE_DOESNT_EXIST));
2022-12-10 05:29:46 +01:00
return;
}
uint16_t len = myFile.read(sdBuffer, 256);
myFile.close();
print_Msg(F("Writing "));
print_Msg(filePath);
println_Msg(F("..."));
display_Update();
dataOut_MD();
2023-02-23 06:48:11 +01:00
for (uint16_t i = 0; i < len; i++) {
2022-12-10 05:29:46 +01:00
writeWord_Acan(0xeb0d03, i);
writeWord_Acan(0xeb0d01, sdBuffer[i]);
delay(10); // for AT28C64B write
}
dataIn_MD();
print_STR(done_STR, 1);
}
2023-02-23 06:48:11 +01:00
static void flashCart_Acan() {
uint32_t *flash_size = (uint32_t *)(eepbit + 4);
2023-01-24 09:34:54 +01:00
filePath[0] = 0;
sd.chdir();
fileBrowser(F("Select a file"));
snprintf(filePath, FILEPATH_LENGTH, "%s/%s", filePath, fileName);
display_Clear();
2023-02-23 06:48:11 +01:00
if (!myFile.open(filePath, O_READ)) {
print_Error(FS(FSTRING_FILE_DOESNT_EXIST));
2023-01-24 09:34:54 +01:00
return;
}
print_Msg(F("Writing "));
print_Msg(filePath + 1);
println_Msg(F("..."));
display_Update();
uint32_t i, j, k, file_length = myFile.fileSize();
uint16_t data;
DDRG |= (1 << 5);
PORTG |= (1 << 5);
draw_progressbar(0, file_length);
2023-02-23 06:48:11 +01:00
for (i = 0; i < file_length; i += *flash_size) {
2023-01-24 09:34:54 +01:00
// erase chip
dataOut_MD();
writeWord_Acan(i + 0xaaaa, 0xaaaa);
writeWord_Acan(i + 0x5555, 0x5555);
writeWord_Acan(i + 0xaaaa, 0x8080);
writeWord_Acan(i + 0xaaaa, 0xaaaa);
writeWord_Acan(i + 0x5555, 0x5555);
writeWord_Acan(i + 0xaaaa, 0x1010);
dataIn_MD();
2023-02-23 06:48:11 +01:00
while (readWord_Acan(i) != 0xffff)
;
2023-01-24 09:34:54 +01:00
2023-02-23 06:48:11 +01:00
for (j = 0; j < *flash_size; j += 512) {
2023-01-24 09:34:54 +01:00
myFile.read(sdBuffer, 512);
2023-02-23 06:48:11 +01:00
for (k = 0; k < 512; k += 2) {
data = *((uint16_t *)(sdBuffer + k));
2023-01-24 09:34:54 +01:00
dataOut_MD();
writeWord_Acan(i + 0xaaaa, 0xaaaa);
writeWord_Acan(i + 0x5555, 0x5555);
writeWord_Acan(i + 0xaaaa, 0xa0a0);
writeWord_Acan(i + j + k, data);
dataIn_MD();
2023-02-23 06:48:11 +01:00
while (readWord_Acan(i + j + k) != data)
;
2023-01-24 09:34:54 +01:00
}
draw_progressbar(i + j + k, file_length);
if ((j & 0xfff) == 0)
blinkLED();
}
}
PORTG &= ~(1 << 5);
DDRG &= ~(1 << 5);
myFile.close();
print_STR(done_STR, 1);
2023-02-23 06:48:11 +01:00
checkRomExist_Acan();
}
static void checkRomExist_Acan() {
// RST to 0
PORTH &= ~(1 << 0);
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
// /RST to 1
PORTH |= (1 << 0);
cartSize = getRomSize_Acan();
romSize = cartSize >> 17;
mode = CORE_SUPRACAN;
2023-02-23 06:48:11 +01:00
if (cartSize == 0)
print_Error(F("Unable to find rom signature..."));
else {
print_Msg(F("ROM Size: "));
print_Msg(romSize);
println_Msg(F(" Mb"));
}
2023-01-24 09:34:54 +01:00
display_Update();
}
2023-02-23 06:48:11 +01:00
static uint32_t getRomSize_Acan() {
2022-12-10 05:29:46 +01:00
uint32_t addr = 0;
uint32_t crc32;
2023-02-23 06:48:11 +01:00
do {
2022-12-10 05:29:46 +01:00
// check if there is rom chip exists
// pull-up enable
DDRC = 0xff;
DDRA = 0xff;
PORTC = 0xff;
PORTA = 0xff;
DDRC = 0x00;
DDRA = 0x00;
2023-02-23 06:48:11 +01:00
*((uint16_t *)sdBuffer) = readWord_Acan(addr);
2022-12-10 05:29:46 +01:00
// pull-up disable
DDRC = 0xff;
DDRA = 0xff;
PORTC = 0x00;
PORTA = 0x00;
DDRC = 0x00;
DDRA = 0x00;
2023-02-23 06:48:11 +01:00
*((uint16_t *)(sdBuffer + 2)) = readWord_Acan(addr);
2022-12-10 05:29:46 +01:00
// should be them same if chip exists
if (sdBuffer[0] != sdBuffer[2] || sdBuffer[1] != sdBuffer[3])
break;
crc32 = 0xffffffff;
2023-02-23 06:48:11 +01:00
for (uint32_t i = 0x2000; i < 0x2080; i += 2) {
*((uint16_t *)sdBuffer) = readWord_Acan(addr + i);
2022-12-10 05:29:46 +01:00
UPDATE_CRC(crc32, sdBuffer[0]);
UPDATE_CRC(crc32, sdBuffer[1]);
}
crc32 = ~crc32;
2023-02-23 06:48:11 +01:00
if (crc32 == 0xa2bc9d7a) {
2022-12-10 05:29:46 +01:00
if (addr > 0)
break;
2023-02-23 06:48:11 +01:00
} else {
2022-12-10 05:29:46 +01:00
if (addr == 0)
break;
}
addr += 0x80000;
} while (addr < 0x800000);
return addr;
}
2023-02-23 06:48:11 +01:00
static void resetCart_Acan() {
2022-12-10 05:29:46 +01:00
// set /CS(PH3), R/W(PH5), /AS(PH6) high
// /RST(PH0) and C27(PH4) low
PORTH |= ((1 << 3) | (1 << 5) | (1 << 6));
PORTH &= ~((1 << 0) | (1 << 4));
2023-02-23 06:48:11 +01:00
if (i2c_found) {
clockgen.output_enable(SI5351_CLK1, 0); // CPU clock
clockgen.output_enable(SI5351_CLK2, 0); // CIC clock
clockgen.output_enable(SI5351_CLK0, 0); // master clock
2022-12-10 05:29:46 +01:00
}
}
2023-02-23 06:48:11 +01:00
static void writeWord_Acan(uint32_t addr, uint16_t data) {
uint8_t *ptr = (uint8_t *)&addr;
2022-12-10 05:29:46 +01:00
PORTF = *ptr++;
PORTK = *ptr++;
PORTL = *ptr;
2023-02-23 06:48:11 +01:00
if (*ptr < 0xe0) {
2022-12-10 05:29:46 +01:00
// ROM area
// /CS(PH3), C27(PH4), R/W(PH5), /AS(PH6) to L
PORTH &= ~((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6));
2023-02-23 06:48:11 +01:00
} else if (*ptr == 0xec) {
2022-12-10 05:29:46 +01:00
// save area
// /CS(PH3) to H, C27(PH4), R/W(PH5), /AS(PH6) to L
PORTH |= (1 << 3);
PORTH &= ~((1 << 4) | (1 << 5) | (1 << 6));
2023-02-23 06:48:11 +01:00
} else if (addr == 0x00eb0d03 || addr == 0x00eb0d01) {
2022-12-10 05:29:46 +01:00
// UM6650 area
// /CS(PH3), C27(PH4) to H, R/W(PH5), /AS(PH6) to L
PORTH |= ((1 << 3) | (1 << 4));
PORTH &= ~((1 << 5) | (1 << 6));
}
2023-02-23 06:48:11 +01:00
ptr = (uint8_t *)&data;
2022-12-10 05:29:46 +01:00
PORTC = *ptr++;
PORTA = *ptr;
2023-02-23 06:48:11 +01:00
NOP;
NOP;
NOP;
2022-12-10 05:29:46 +01:00
PORTH &= ~(1 << 4);
PORTH |= ((1 << 3) | (1 << 5) | (1 << 6));
}
2023-02-23 06:48:11 +01:00
static uint16_t readWord_Acan(uint32_t addr) {
uint8_t *ptr = (uint8_t *)&addr;
2022-12-10 05:29:46 +01:00
uint16_t data;
PORTF = *ptr++;
PORTK = *ptr++;
PORTL = *ptr;
2023-02-23 06:48:11 +01:00
if (*ptr < 0xe0) {
2022-12-10 05:29:46 +01:00
// ROM area
// /CS(PH3), C27(PH4), /AS(PH6) to L
PORTH &= ~((1 << 3) | (1 << 4) | (1 << 6));
2023-02-23 06:48:11 +01:00
} else if (*ptr == 0xec) {
2022-12-10 05:29:46 +01:00
// save area
// /CS(PH3) to H, C27(PH4), /AS(PH6) to L
PORTH |= (1 << 3);
PORTH &= ~((1 << 4) | (1 << 6));
2023-02-23 06:48:11 +01:00
} else if (addr == 0x00eb0d03 || addr == 0x00eb0d01) {
2022-12-10 05:29:46 +01:00
// UM6650 area
// /CS(PH3), C27(PH4) to H, /AS(PH6) to L
PORTH |= ((1 << 3) | (1 << 4));
PORTH &= ~(1 << 6);
}
2023-02-23 06:48:11 +01:00
ptr = (uint8_t *)&data;
NOP;
NOP;
NOP;
2022-12-10 05:29:46 +01:00
*ptr++ = PINC;
*ptr = PINA;
PORTH &= ~(1 << 4);
PORTH |= ((1 << 3) | (1 << 5) | (1 << 6));
return data;
}
2023-02-23 06:48:11 +01:00
static uint32_t getFlashChipSize_Acan(uint16_t chip_id) {
2023-01-24 09:34:54 +01:00
// 0x0458 (8M), 0x01ab (4M), 0x01d8 (16M)
2023-02-23 06:48:11 +01:00
switch (chip_id) {
2023-01-24 09:34:54 +01:00
case 0x01ab: return 524288;
case 0x0458: return 1048576;
case 0x01d8: return 2097152;
}
return 0;
}
2022-12-10 05:29:46 +01:00
#endif