V5.2: Fix bug in GB code

This commit is contained in:
sanni 2020-06-18 14:33:21 +02:00
parent c9f5c9ac36
commit ea6722d0c5
3 changed files with 72 additions and 33 deletions

View File

@ -2,8 +2,8 @@
Cartridge Reader for Arduino Mega2560 Cartridge Reader for Arduino Mega2560
Author: sanni Author: sanni
Date: 25.05.2020 Date: 18.06.2020
Version: 5.1 Version: 5.2
SD lib: https://github.com/greiman/SdFat SD lib: https://github.com/greiman/SdFat
LCD lib: https://github.com/adafruit/Adafruit_SSD1306 LCD lib: https://github.com/adafruit/Adafruit_SSD1306
@ -43,7 +43,7 @@
**********************************************************************************/ **********************************************************************************/
#include <SdFat.h> #include <SdFat.h>
char ver[5] = "5.1"; char ver[5] = "5.2";
/****************************************** /******************************************
Options Options

View File

@ -165,21 +165,16 @@ void gbMenu() {
Setup Setup
*****************************************/ *****************************************/
void setup_GB() { void setup_GB() {
// Set RST(PH0) to Input
DDRH &= ~(1 << 0);
// Activate Internal Pullup Resistors
PORTH |= (1 << 0);
// Set Address Pins to Output // Set Address Pins to Output
//A0-A7 //A0-A7
DDRF = 0xFF; DDRF = 0xFF;
//A8-A15 //A8-A15
DDRK = 0xFF; DDRK = 0xFF;
// Set Control Pins to Output CS(PH3) WR(PH5) RD(PH6) // Set Control Pins to Output RST(PH0) CS(PH3) WR(PH5) RD(PH6)
DDRH |= (1 << 3) | (1 << 5) | (1 << 6); DDRH |= (1 << 0) | (1 << 3) | (1 << 5) | (1 << 6);
// Output a high signal on all pins, pins are active low therefore everything is disabled now // Output a high signal on all pins, pins are active low therefore everything is disabled now
PORTH |= (1 << 3) | (1 << 5) | (1 << 6); PORTH |= (1 << 0) | (1 << 3) | (1 << 5) | (1 << 6);
// Set Data Pins (D0-D7) to Input // Set Data Pins (D0-D7) to Input
DDRC = 0x00; DDRC = 0x00;
@ -299,6 +294,28 @@ byte readByte_GB(word myAddress) {
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t"); __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch RD(PH6) to LOW
PORTH &= ~(1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Read
byte tempByte = PINC;
// Switch and RD(PH6) to HIGH
PORTH |= (1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
return tempByte;
}
byte readByteSRAM_GB(word myAddress) {
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch CS(PH3) and RD(PH6) to LOW // Switch CS(PH3) and RD(PH6) to LOW
PORTH &= ~((1 << 3) | (1 << 6)); PORTH &= ~((1 << 3) | (1 << 6));
@ -575,7 +592,7 @@ void readSRAM_GB() {
for (uint16_t sramAddress = 0xA000; sramAddress <= sramEndAddress; sramAddress += 64) { for (uint16_t sramAddress = 0xA000; sramAddress <= sramEndAddress; sramAddress += 64) {
uint8_t readData[64]; uint8_t readData[64];
for (uint8_t i = 0; i < 64; i++) { for (uint8_t i = 0; i < 64; i++) {
readData[i] = readByte_GB(sramAddress + i); readData[i] = readByteSRAM_GB(sramAddress + i);
} }
myFile.write(readData, 64); myFile.write(readData, 64);
} }
@ -702,7 +719,7 @@ unsigned long verifySRAM_GB() {
//fill sdBuffer //fill sdBuffer
myFile.read(sdBuffer, 64); myFile.read(sdBuffer, 64);
for (int c = 0; c < 64; c++) { for (int c = 0; c < 64; c++) {
if (readByte_GB(sramAddress + c) != sdBuffer[c]) { if (readByteSRAM_GB(sramAddress + c) != sdBuffer[c]) {
writeErrors++; writeErrors++;
} }
} }

View File

@ -53,6 +53,28 @@ uint16_t gameMenuStartBank;
extern boolean hasMenu; extern boolean hasMenu;
extern byte numGames; extern byte numGames;
byte readByte_GBS(word myAddress) {
PORTF = myAddress & 0xFF;
PORTK = (myAddress >> 8) & 0xFF;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Switch CS(PH3) and RD(PH6) to LOW
PORTH &= ~((1 << 3) | (1 << 6));
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
// Read
byte tempByte = PINC;
// Switch CS(PH3) and RD(PH6) to HIGH
PORTH |= (1 << 3) | (1 << 6);
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
return tempByte;
}
void setup_GBSmart() void setup_GBSmart()
{ {
// take from setup_GB // take from setup_GB
@ -82,7 +104,7 @@ void setup_GBSmart()
getCartInfo_GB(); getCartInfo_GB();
for (byte i = 0; i < 0x30; i++) for (byte i = 0; i < 0x30; i++)
signature[i] = readByte_GB(0x0104 + i); signature[i] = readByte_GBS(0x0104 + i);
gameMenuStartBank = 0x02; gameMenuStartBank = 0x02;
hasMenu = true; hasMenu = true;
@ -301,7 +323,7 @@ void gbSmartGetGames()
dataIn_GB(); dataIn_GB();
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
{ {
if (readByte_GB(0x0134 + i) != menu_title[i]) if (readByte_GBS(0x0134 + i) != menu_title[i])
{ {
hasMenu = false; hasMenu = false;
break; break;
@ -322,7 +344,7 @@ void gbSmartGetGames()
// read signature // read signature
for (uint8_t j = 0x00; j < 0x30; j++) for (uint8_t j = 0x00; j < 0x30; j++)
{ {
if (readByte_GB(0x4104 + j) != signature[j]) if (readByte_GBS(0x4104 + j) != signature[j])
{ {
i += 0x02; i += 0x02;
goto gb_smart_get_game_loop_end; goto gb_smart_get_game_loop_end;
@ -331,7 +353,7 @@ void gbSmartGetGames()
for (uint8_t j = 0; j < 15; j++) for (uint8_t j = 0; j < 15; j++)
{ {
myByte = readByte_GB(0x4134 + j); myByte = readByte_GBS(0x4134 + j);
if (((char(myByte) >= 0x30 && char(myByte) <= 0x39) || if (((char(myByte) >= 0x30 && char(myByte) <= 0x39) ||
(char(myByte) >= 0x41 && char(myByte) <= 0x7a))) (char(myByte) >= 0x41 && char(myByte) <= 0x7a)))
@ -340,9 +362,9 @@ void gbSmartGetGames()
gbSmartGames[numGames].title[myLength] = 0x00; gbSmartGames[numGames].title[myLength] = 0x00;
gbSmartGames[numGames].start_bank = i; gbSmartGames[numGames].start_bank = i;
gbSmartGames[numGames].rom_type = readByte_GB(0x4147); gbSmartGames[numGames].rom_type = readByte_GBS(0x4147);
gbSmartGames[numGames].rom_size = readByte_GB(0x4148); gbSmartGames[numGames].rom_size = readByte_GBS(0x4148);
gbSmartGames[numGames].sram_size = readByte_GB(0x4149); gbSmartGames[numGames].sram_size = readByte_GBS(0x4149);
myByte = (2 << gbSmartGames[numGames].rom_size); myByte = (2 << gbSmartGames[numGames].rom_size);
i += myByte; i += myByte;
@ -357,7 +379,7 @@ gb_smart_get_game_loop_end:;
dataIn_GB(); dataIn_GB();
for (uint8_t j = 0; j < 15; j++) for (uint8_t j = 0; j < 15; j++)
{ {
myByte = readByte_GB(0x0134 + j); myByte = readByte_GBS(0x0134 + j);
if (((char(myByte) >= 0x30 && char(myByte) <= 0x39) || if (((char(myByte) >= 0x30 && char(myByte) <= 0x39) ||
(char(myByte) >= 0x41 && char(myByte) <= 0x7a))) (char(myByte) >= 0x41 && char(myByte) <= 0x7a)))
@ -366,9 +388,9 @@ gb_smart_get_game_loop_end:;
gbSmartGames[0].title[myLength] = 0x00; gbSmartGames[0].title[myLength] = 0x00;
gbSmartGames[0].start_bank = 0x00; gbSmartGames[0].start_bank = 0x00;
gbSmartGames[0].rom_type = readByte_GB(0x0147); gbSmartGames[0].rom_type = readByte_GBS(0x0147);
gbSmartGames[0].rom_size = readByte_GB(0x0148); gbSmartGames[0].rom_size = readByte_GBS(0x0148);
gbSmartGames[0].sram_size = readByte_GB(0x0149); gbSmartGames[0].sram_size = readByte_GBS(0x0149);
numGames = 1; numGames = 1;
gameMenuStartBank = 0xfe; gameMenuStartBank = 0xfe;
@ -397,7 +419,7 @@ void gbSmartReadFlash()
for (uint16_t addr = 0x0000; addr <= 0x3fff; addr += 512) for (uint16_t addr = 0x0000; addr <= 0x3fff; addr += 512)
{ {
for (uint16_t c = 0; c < 512; c++) for (uint16_t c = 0; c < 512; c++)
sdBuffer[c] = readByte_GB(addr + c); sdBuffer[c] = readByte_GBS(addr + c);
myFile.write(sdBuffer, 512); myFile.write(sdBuffer, 512);
} }
@ -412,7 +434,7 @@ void gbSmartReadFlash()
for (uint16_t addr = 0x4000; addr <= 0x7fff; addr += 512) for (uint16_t addr = 0x4000; addr <= 0x7fff; addr += 512)
{ {
for (uint16_t c = 0; c < 512; c++) for (uint16_t c = 0; c < 512; c++)
sdBuffer[c] = readByte_GB(addr + c); sdBuffer[c] = readByte_GBS(addr + c);
myFile.write(sdBuffer, 512); myFile.write(sdBuffer, 512);
} }
@ -528,7 +550,7 @@ void gbSmartWriteFlashFromMyFile(uint32_t addr)
// waiting for finishing // waiting for finishing
dataIn_GB(); dataIn_GB();
while ((readByte_GB(addr + i) & 0x80) == 0x00); while ((readByte_GBS(addr + i) & 0x80) == 0x00);
} }
// blink LED // blink LED
@ -557,7 +579,7 @@ uint32_t gbSmartVerifyFlash()
for (uint16_t c = 0; c < 512; c++) for (uint16_t c = 0; c < 512; c++)
{ {
if (readByte_GB(addr + c) != sdBuffer[c]) if (readByte_GBS(addr + c) != sdBuffer[c])
verified++; verified++;
} }
} }
@ -575,7 +597,7 @@ uint32_t gbSmartVerifyFlash()
for (uint16_t c = 0; c < 512; c++) for (uint16_t c = 0; c < 512; c++)
{ {
if (readByte_GB(addr + c) != sdBuffer[c]) if (readByte_GBS(addr + c) != sdBuffer[c])
verified++; verified++;
} }
} }
@ -598,7 +620,7 @@ byte gbSmartBlankCheckingFlash(uint8_t flash_start_bank)
dataIn_GB(); dataIn_GB();
for (uint16_t addr = 0x0000; addr <= 0x3fff; addr++) for (uint16_t addr = 0x0000; addr <= 0x3fff; addr++)
{ {
if (readByte_GB(addr) != 0xff) if (readByte_GBS(addr) != 0xff)
return 0; return 0;
} }
@ -611,7 +633,7 @@ byte gbSmartBlankCheckingFlash(uint8_t flash_start_bank)
dataIn_GB(); dataIn_GB();
for (uint16_t addr = 0x4000; addr <= 0x7fff; addr++) for (uint16_t addr = 0x4000; addr <= 0x7fff; addr++)
{ {
if (readByte_GB(addr) != 0xff) if (readByte_GBS(addr) != 0xff)
return 0; return 0;
} }
} }
@ -637,7 +659,7 @@ void gbSmartEraseFlash(uint8_t flash_start_bank)
gbSmartWriteFlashByte(0x0000, 0xd0); gbSmartWriteFlashByte(0x0000, 0xd0);
dataIn_GB(); dataIn_GB();
while ((readByte_GB(0x0000) & 0x80) == 0x00); while ((readByte_GBS(0x0000) & 0x80) == 0x00);
// blink LED // blink LED
PORTB ^= (1 << 4); PORTB ^= (1 << 4);
@ -652,7 +674,7 @@ void gbSmartEraseFlash(uint8_t flash_start_bank)
gbSmartWriteFlashByte(0x4000, 0xd0); gbSmartWriteFlashByte(0x4000, 0xd0);
dataIn_GB(); dataIn_GB();
while ((readByte_GB(0x4000) & 0x80) == 0x00); while ((readByte_GBS(0x4000) & 0x80) == 0x00);
// blink LED // blink LED
PORTB ^= (1 << 4); PORTB ^= (1 << 4);