MD.ino: Move some globals to getCartInfo_MD locals.

Also, bypass sdBuffer when it was the directory copied to another buffer.
Also, factorise yet another pair of loops copying rom name.
This frees 59 bytes of global ram space.
This commit is contained in:
Vincent Pelletier 2022-10-24 06:51:06 +00:00
parent 3c3b399d6a
commit 10061beaf5

View File

@ -103,10 +103,6 @@ static unsigned long cartSizeLockon;
static unsigned long cartSizeSonic2 = 262144; static unsigned long cartSizeSonic2 = 262144;
static word chksumLockon; static word chksumLockon;
static word chksumSonic2 = 0x0635; static word chksumSonic2 = 0x0635;
static char romNameLockon[12];
static char id[15];
static char idLockon[15];
static char labelLockon[17];
/****************************************** /******************************************
Configuration Configuration
@ -671,6 +667,23 @@ void dataIn_MD() {
/****************************************** /******************************************
MEGA DRIVE functions MEGA DRIVE functions
*****************************************/ *****************************************/
byte copyToRomName_MD(char *output, const byte *input, byte length) {
byte myLength = 0;
for (byte i = 0; i < 48; i++) {
if (
(
(input[i] >= '0' && input[i] <= '9') ||
(input[i] >= 'A' && input[i] <= 'z')
) && myLength < length
) {
output[myLength++] = input[i];
}
}
return myLength;
}
void getCartInfo_MD() { void getCartInfo_MD() {
// Set control // Set control
dataIn_MD(); dataIn_MD();
@ -709,6 +722,7 @@ void getCartInfo_MD() {
// Sonic & Knuckles Check // Sonic & Knuckles Check
SnKmode = 0; SnKmode = 0;
if (chksum == 0xDFB3) { if (chksum == 0xDFB3) {
char id[15];
// Get ID // Get ID
for (byte c = 0; c < 14; c += 2) { for (byte c = 0; c < 14; c += 2) {
@ -718,15 +732,13 @@ void getCartInfo_MD() {
byte hiByte = myWord >> 8; byte hiByte = myWord >> 8;
// write to buffer // write to buffer
sdBuffer[c] = hiByte; id[c] = hiByte;
sdBuffer[c + 1] = loByte; id[c + 1] = loByte;
}
for (int i = 0; i < 14; i++) {
id[i] = char(sdBuffer[i]);
} }
//Sonic & Knuckles ID:GM MK-1563 -00 //Sonic & Knuckles ID:GM MK-1563 -00
if (!strcmp("GM MK-1563 -00", id)) { if (!strcmp("GM MK-1563 -00", id)) {
char labelLockon[17];
// Get labelLockon // Get labelLockon
for (byte c = 0; c < 16; c += 2) { for (byte c = 0; c < 16; c += 2) {
@ -736,15 +748,13 @@ void getCartInfo_MD() {
byte hiByte = myWord >> 8; byte hiByte = myWord >> 8;
// write to buffer // write to buffer
sdBuffer[c] = hiByte; labelLockon[c] = hiByte;
sdBuffer[c + 1] = loByte; labelLockon[c + 1] = loByte;
}
for (int i = 0; i < 16; i++) {
labelLockon[i] = char(sdBuffer[i]);
} }
// check Lock-on game presence // check Lock-on game presence
if (!(strcmp("SEGA MEGA DRIVE ", labelLockon) & strcmp("SEGA GENESIS ", labelLockon))) { if (!(strcmp("SEGA MEGA DRIVE ", labelLockon) & strcmp("SEGA GENESIS ", labelLockon))) {
char idLockon[15];
// Lock-on cart checksum // Lock-on cart checksum
chksumLockon = readWord_MD(0x1000C7); chksumLockon = readWord_MD(0x1000C7);
@ -759,11 +769,8 @@ void getCartInfo_MD() {
byte hiByte = myWord >> 8; byte hiByte = myWord >> 8;
// write to buffer // write to buffer
sdBuffer[c] = hiByte; idLockon[c] = hiByte;
sdBuffer[c + 1] = loByte; idLockon[c + 1] = loByte;
}
for (int i = 0; i < 14; i++) {
idLockon[i] = char(sdBuffer[i]);
} }
if (!(strncmp("GM 00001009-0", idLockon, 13) & strncmp("GM 00004049-0", idLockon, 13))) { if (!(strncmp("GM 00001009-0", idLockon, 13) & strncmp("GM 00004049-0", idLockon, 13))) {
@ -968,16 +975,11 @@ void getCartInfo_MD() {
sdBuffer[c] = hiByte; sdBuffer[c] = hiByte;
sdBuffer[c + 1] = loByte; sdBuffer[c + 1] = loByte;
} }
byte myLength = 0; romName[copyToRomName_MD(romName, sdBuffer, sizeof(romName) - 1)] = 0;
for (unsigned int i = 0; i < 48; i++) {
if (((char(sdBuffer[i]) >= 48 && char(sdBuffer[i]) <= 57) || (char(sdBuffer[i]) >= 65 && char(sdBuffer[i]) <= 122)) && myLength < 15) {
romName[myLength] = char(sdBuffer[i]);
myLength++;
}
}
//Get Lock-on cart name //Get Lock-on cart name
if (SnKmode >= 2) { if (SnKmode >= 2) {
char romNameLockon[12];
//Change romName //Change romName
strcpy(romName, "SnK_"); strcpy(romName, "SnK_");
@ -992,13 +994,7 @@ void getCartInfo_MD() {
sdBuffer[c] = hiByte; sdBuffer[c] = hiByte;
sdBuffer[c + 1] = loByte; sdBuffer[c + 1] = loByte;
} }
byte myLength = 0; romNameLockon[copyToRomName_MD(romNameLockon, sdBuffer, sizeof(romNameLockon) - 1)] = 0;
for (unsigned int i = 0; i < 48; i++) {
if (((char(sdBuffer[i]) >= 48 && char(sdBuffer[i]) <= 57) || (char(sdBuffer[i]) >= 65 && char(sdBuffer[i]) <= 122)) && myLength < 11) {
romNameLockon[myLength] = char(sdBuffer[i]);
myLength++;
}
}
switch (SnKmode) { switch (SnKmode) {
case 2: strcat(romName, "SONIC1"); break; case 2: strcat(romName, "SONIC1"); break;