Skip to content

Commit

Permalink
reuse n64 flashrom command sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
smesgr9000 committed May 30, 2024
1 parent 86059ae commit fb78a1f
Showing 1 changed file with 35 additions and 97 deletions.
132 changes: 35 additions & 97 deletions Cart_Reader/N64.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3344,17 +3344,21 @@ void resetFlashrom_N64(unsigned long flashBase) {
delay(100);
}

void sendFlashromCommand_N64(unsigned long addr, byte cmd) {
setAddress_N64(addr + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(addr + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(addr + (0x555 << 1));
writeWord_N64(cmd);
}

void idFlashrom_N64() {
// Set size to 0 if no ID is found
cartSize = 0;

// Send flashrom ID command
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0x90);
sendFlashromCommand_N64(romBase, 0x90);

// Read 1 byte vendor ID
setAddress_N64(romBase);
Expand All @@ -3374,12 +3378,7 @@ void idFlashrom_N64() {
resetFlashrom_N64(romBase);

// Test for second flashrom chip at 0x2000000 (32MB)
setAddress_N64(romBase + 0x2000000 + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + 0x2000000 + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + 0x2000000 + (0x555 << 1));
writeWord_N64(0x90);
sendFlashromCommand_N64(romBase + 0x2000000, 0x90);

char tempID[5];
setAddress_N64(romBase + 0x2000000);
Expand All @@ -3402,12 +3401,7 @@ void idFlashrom_N64() {
resetFlashrom_N64(romBase + 0x800000);

// Test for second flashrom chip at 0x800000 (8MB)
setAddress_N64(romBase + 0x800000 + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + 0x800000 + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + 0x800000 + (0x555 << 1));
writeWord_N64(0x90);
sendFlashromCommand_N64(romBase + 0x800000, 0x90);

char tempID[5];
setAddress_N64(romBase + 0x800000);
Expand All @@ -3430,12 +3424,7 @@ void idFlashrom_N64() {
resetIntel4400_N64();

// Test if second half of the flashrom might be hidden
setAddress_N64(romBase + 0x2000000 + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + 0x2000000 + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + 0x2000000 + (0x555 << 1));
writeWord_N64(0x90);
sendFlashromCommand_N64(romBase + 0x2000000, 0x90);

// Read manufacturer ID
setAddress_N64(romBase + 0x2000000);
Expand Down Expand Up @@ -3678,18 +3667,8 @@ void eraseFlashrom_N64() {
display_Update();

// Send Erase Command
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0x80);
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(romBase + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + (0x555 << 1));
writeWord_N64(0x10);
sendFlashromCommand_N64(romBase, 0x80);
sendFlashromCommand_N64(romBase, 0x10);

// Read the status register
setAddress_N64(romBase);
Expand Down Expand Up @@ -3725,19 +3704,15 @@ void eraseSector_N64(unsigned long sectorSize) {
}

// Send Erase Command
setAddress_N64(flashBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(flashBase + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(flashBase + (0x555 << 1));
writeWord_N64(0x80);
sendFlashromCommand_N64(flashBase, 0x80);
setAddress_N64(flashBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(flashBase + (0x2AA << 1));
writeWord_N64(0x55);
setAddress_N64(romBase + currSector);
writeWord_N64(0x30);


// Read the status register
setAddress_N64(romBase + currSector);
word statusReg = readWord_N64();
Expand Down Expand Up @@ -3958,13 +3933,7 @@ void writeFlashrom_N64(unsigned long sectorSize) {
// Join two bytes into one word
word currWord = ((sdBuffer[currByte] & 0xFF) << 8) | (sdBuffer[currByte + 1] & 0xFF);
// 2 unlock commands
setAddress_N64(flashBase + (0x555 << 1));
writeWord_N64(0xAA);
setAddress_N64(flashBase + (0x2AA << 1));
writeWord_N64(0x55);
// Program command
setAddress_N64(flashBase + (0x555 << 1));
writeWord_N64(0xA0);
sendFlashromCommand_N64(flashBase, 0xA0);
// Write word
setAddress_N64(romBase + currSector + currSdBuffer + currByte);
writeWord_N64(currWord);
Expand Down Expand Up @@ -4024,6 +3993,15 @@ unsigned long verifyFlashrom_N64() {
/******************************************
N64 Gameshark Flash Functions
*****************************************/
void sendFlashromGamesharkCommand_N64(uint16_t cmd) {
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(cmd);
}

void flashGameshark_N64() {
// Check flashrom ID's
unlockGSAddressRanges();
Expand Down Expand Up @@ -4138,12 +4116,7 @@ void unlockGSAddressRanges() {
void idGameshark_N64() {
flashid = 0x0;
//Send flashrom ID command
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x9090);
sendFlashromGamesharkCommand_N64(0x9090);

setAddress_N64(0x1EC00000);
// Read 1 byte vendor ID
Expand Down Expand Up @@ -4173,21 +4146,11 @@ void idGameshark_N64() {
void resetGameshark_N64() {
if (flashid == 0x0808 || flashid == 0x3535 || flashid == 0x0707) {
// Send reset command for SST 29LE010 / AMTEL AT29LV010A / SST 29EE010
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xF0F0);
sendFlashromGamesharkCommand_N64(0xF0F0);
delay(100);
} else if (flashid == 0x0404) {
// Send reset command for SST 28LF040
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xFFFF);
sendFlashromGamesharkCommand_N64(0xFFFF);
delay(300);
}
}
Expand Down Expand Up @@ -4226,18 +4189,8 @@ void eraseGameshark_N64() {

// Send chip erase to SST 29LE010 / AMTEL AT29LV010A / SST 29EE010
if (flashid == 0x0808 || flashid == 0x3535 || flashid == 0x0707) {
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x8080);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x1010);
sendFlashromGamesharkCommand_N64(0x8080);
sendFlashromGamesharkCommand_N64(0x1010);

delay(20);
}
Expand All @@ -4261,12 +4214,7 @@ void eraseGameshark_N64() {
delay(1000);

//Erase flash
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x3030);
sendFlashromGamesharkCommand_N64(0x3030);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x3030);
delay(1000);
Expand Down Expand Up @@ -4309,12 +4257,7 @@ void writeGameshark_N64() {
myFile.read(sdBuffer, 256);

//Send page write command to both flashroms
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xA0A0);
sendFlashromGamesharkCommand_N64(0xA0A0);

// Write 1 page each, one flashrom gets the low byte, the other the high byte.
for (unsigned long currByte = 0; currByte < 256; currByte += 2) {
Expand Down Expand Up @@ -4346,12 +4289,7 @@ void writeGameshark_N64() {
for (unsigned long currByte = 0; currByte < 256; currByte += 2) {

// Send byte program command
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0xAAAA);
setAddress_N64(0x1EE05554);
writeWord_N64(0x5555);
setAddress_N64(0x1EF0AAA8);
writeWord_N64(0x1010);
sendFlashromGamesharkCommand_N64(0x1010);

// Set address
setAddress_N64(romBase + 0xEC00000 + currSector + currSdBuffer + currByte);
Expand Down

0 comments on commit fb78a1f

Please sign in to comment.