Improve Controller Pak timing

This commit is contained in:
sanni 2022-08-18 19:23:33 +02:00
parent e4246d8310
commit 659eefda63
2 changed files with 79 additions and 47 deletions

View File

@ -4,7 +4,7 @@
This project represents a community-driven effort to provide This project represents a community-driven effort to provide
an easy to build and easy to modify cartridge dumper. an easy to build and easy to modify cartridge dumper.
Date: 17.08.2022 Date: 18.08.2022
Version: 9.5 Version: 9.5
SD lib: https://github.com/greiman/SdFat SD lib: https://github.com/greiman/SdFat

View File

@ -210,6 +210,7 @@ void n64ControllerMenu() {
display_Clear(); display_Clear();
display_Update(); display_Update();
writeMPK(); writeMPK();
delay(500);
verifyMPK(); verifyMPK();
println_Msg(F("")); println_Msg(F(""));
println_Msg(F("Press Button...")); println_Msg(F("Press Button..."));
@ -2013,23 +2014,36 @@ void readMPK() {
uint32_t totalProgressBar = (uint32_t)(0x7FFF); uint32_t totalProgressBar = (uint32_t)(0x7FFF);
draw_progressbar(0, totalProgressBar); draw_progressbar(0, totalProgressBar);
// Dummy write because first write to file takes 1 second and messes up timing
blinkLED();
myFile.write(0xFF);
myFile.rewind();
blinkLED();
// Controller paks, which all have 32kB of space, are mapped between 0x0000 0x7FFF // Controller paks, which all have 32kB of space, are mapped between 0x0000 0x7FFF
for (word i = 0x0000; i < 0x8000; i += 32) { // Read 512 byte into sdBuffer
// Read one block of the Controller Pak into array myBlock for (word currSdBuffer = 0x0000; currSdBuffer < 0x8000; currSdBuffer += 512) {
readBlock(i); // Read 32 byte block
for (word currBlock = 0; currBlock < 512; currBlock += 32) {
// Read one block of the Controller Pak into array myBlock
readBlock(currSdBuffer + currBlock);
// Delay to prevent write errors // Copy block to SdBuffer
delay(1); for (byte currByte = 0; currByte < 32; currByte++) {
sdBuffer[currBlock + currByte] = myBlock[currByte];
}
// Write block to SD card // Real N64 has about 627us pause between banks, loop takes 500us, add a bit extra delay
for (byte j = 0; j < 32; j++) { if (currBlock < 479)
myFile.write(myBlock[j]); delayMicroseconds(800);
} }
// This will take 1300us
blinkLED();
myFile.write(sdBuffer, 512);
// Blink led // Blink led
blinkLED(); blinkLED();
// Update progress bar // Update progress bar
processedProgressBar += 32; processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar); draw_progressbar(processedProgressBar, totalProgressBar);
} }
// Close the file: // Close the file:
@ -2052,40 +2066,50 @@ void writeMPK() {
uint32_t totalProgressBar = (uint32_t)(0x7FFF); uint32_t totalProgressBar = (uint32_t)(0x7FFF);
draw_progressbar(0, totalProgressBar); draw_progressbar(0, totalProgressBar);
for (word myAddress = 0x0000; myAddress < 0x8000; myAddress += 32) { for (word currSdBuffer = 0x0000; currSdBuffer < 0x8000; currSdBuffer += 512) {
// Read 32 bytes into SD buffer // Read 512 bytes into SD buffer, takes 1500us
myFile.read(sdBuffer, 32); myFile.read(sdBuffer, 512);
// Calculate the address CRC // Write 32 byte block
word myAddressCRC = addrCRC(myAddress); for (word currBlock = 0; currBlock < 512; currBlock += 32) {
// Calculate the address CRC
word myAddressCRC = addrCRC(currSdBuffer + currBlock);
// Write Controller Pak command // Copy 32 byte block from SdBuffer
unsigned char command[] = {0x03}; for (byte currByte = 0; currByte < 32; currByte++) {
// Address Command myBlock[currByte] = sdBuffer[currBlock + currByte] ;
unsigned char addressHigh[] = {(unsigned char)(myAddressCRC >> 8)}; }
unsigned char addressLow[] = {(unsigned char)(myAddressCRC & 0xff)};
// don't want interrupts getting in the way // Write Controller Pak command
noInterrupts(); unsigned char command[] = {0x03};
// Send write command // Address Command
N64_send(command, 1); unsigned char addressHigh[] = {(unsigned char)(myAddressCRC >> 8)};
// Send block number unsigned char addressLow[] = {(unsigned char)(myAddressCRC & 0xff)};
N64_send(addressHigh, 1);
N64_send(addressLow, 1); // don't want interrupts getting in the way
// Send data to write noInterrupts();
N64_send(sdBuffer, 32); // Send write command
// Send stop N64_send(command, 1);
N64_stop(); // Send block number
// Enable interrupts N64_send(addressHigh, 1);
interrupts(); N64_send(addressLow, 1);
// Send data to write
N64_send(myBlock, 32);
// Send stop
N64_stop();
// Enable interrupts
interrupts();
// Real N64 has about 627us pause between banks, add a bit extra delay
if (currBlock < 479)
delayMicroseconds(1500);
}
// Blink led // Blink led
blinkLED(); blinkLED();
// Update progress bar // Update progress bar
processedProgressBar += 32; processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar); draw_progressbar(processedProgressBar, totalProgressBar);
// Delay to prevent write errors
delay(1);
} }
// Close the file: // Close the file:
myFile.close(); myFile.close();
@ -2113,22 +2137,30 @@ void verifyMPK() {
draw_progressbar(0, totalProgressBar); draw_progressbar(0, totalProgressBar);
// Controller paks, which all have 32kB of space, are mapped between 0x0000 0x7FFF // Controller paks, which all have 32kB of space, are mapped between 0x0000 0x7FFF
for (word i = 0x0000; i < 0x8000; i += 32) { for (word currSdBuffer = 0x0000; currSdBuffer < 0x8000; currSdBuffer += 512) {
// Read one block of the Controller Pak into array myBlock // Read 512 bytes into SD buffer
readBlock(i); myFile.read(sdBuffer, 512);
// Delay to prevent read errors
delay(1); // Compare 32 byte block
// Check against file on SD card for (word currBlock = 0; currBlock < 512; currBlock += 32) {
for (byte j = 0; j < 32; j++) { // Read one block of the Controller Pak into array myBlock
if (myFile.read() != myBlock[j]) { readBlock(currSdBuffer + currBlock);
writeErrors++;
// Check against file on SD card
for (byte currByte = 0; currByte < 32; currByte++) {
if (sdBuffer[currBlock + currByte] != myBlock[currByte]) {
writeErrors++;
}
} }
// Real N64 has about 627us pause between banks, add a bit extra delay
if (currBlock < 479)
delayMicroseconds(1500);
} }
// Blink led // Blink led
blinkLED(); blinkLED();
// Update progress bar // Update progress bar
processedProgressBar += 32; processedProgressBar += 512;
draw_progressbar(processedProgressBar, totalProgressBar); draw_progressbar(processedProgressBar, totalProgressBar);
} }