Merge pull request #127 from libretro/master

[libretro] merge cheat support changes
This commit is contained in:
ekeeke 2017-03-28 11:38:22 +02:00 committed by GitHub
commit e0e10c5375

View File

@ -1190,14 +1190,13 @@ static uint32_t decode_cheat(char *string, int index)
uint32_t address = 0; uint32_t address = 0;
uint16_t data = 0; uint16_t data = 0;
uint8_t ref = 0; uint8_t ref = 0;
/* 16-bit Game Genie code (ABCD-EFGH) */
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD){
//If system is Genesis-based
//Game-Genie
if ((strlen(string) >= 9) && (string[4] == '-')) if ((strlen(string) >= 9) && (string[4] == '-'))
{ {
/* 16-bit system only */
if ((system_hw & SYSTEM_PBC) != SYSTEM_MD)
{
return 0;
}
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
if (i == 4) string++; if (i == 4) string++;
@ -1238,14 +1237,36 @@ static uint32_t decode_cheat(char *string, int index)
/* code length */ /* code length */
len = 9; len = 9;
} }
/* 8-bit Game Genie code (DDA-AAA-XXX) */
else if ((strlen(string) >= 11) && (string[3] == '-') && (string[7] == '-')) //Patch and PAR
else if ((strlen(string) >=9) && (string[6] == ':'))
{ {
/* 8-bit system only */ /* decode 24-bit address */
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD) for (i=0; i<6; i++)
{ {
return 0; p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
address |= (n << ((5 - i) * 4));
} }
/* decode 16-bit data */
string++;
for (i=0; i<4; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) break;
n = (p - arvalidchars) & 0xF;
data |= (n << ((3 - i) * 4));
}
/* code length */
len = 11;
}
} else {
//If System is Master-based
//Game Genie
if ((strlen(string) >=7) && (string[3] == '-'))
{
/* decode 8-bit data */ /* decode 8-bit data */
for (i=0; i<2; i++) for (i=0; i<2; i++)
{ {
@ -1269,13 +1290,9 @@ static uint32_t decode_cheat(char *string, int index)
n = (p - arvalidchars) & 0xF; n = (p - arvalidchars) & 0xF;
n ^= 0xF; /* bits inversion */ n ^= 0xF; /* bits inversion */
address |= (n << 12); address |= (n << 12);
/* RAM address are also supported */ /* Optional: decode reference 8-bit data */
if (address >= 0xC000) if (*string=='-')
{ {
/* convert to 24-bit Work RAM address */
address = 0xFF0000 | (address & 0x1FFF);
}
/* decode reference 8-bit data */
for (i=0; i<2; i++) for (i=0; i<2; i++)
{ {
string++; /* skip separator and 2nd digit */ string++; /* skip separator and 2nd digit */
@ -1286,44 +1303,76 @@ static uint32_t decode_cheat(char *string, int index)
} }
ref = (ref >> 2) | ((ref & 0x03) << 6); /* 2-bit right rotation */ ref = (ref >> 2) | ((ref & 0x03) << 6); /* 2-bit right rotation */
ref ^= 0xBA; /* XOR */ ref ^= 0xBA; /* XOR */
/* update old data value */
cheatlist[index].old = ref;
/* code length */
len = 11;
}
/* Action Replay code */
else if (string[6] == ':')
{
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD)
{
/* 16-bit code (AAAAAA:DDDD) */
if (strlen(string) < 11) return 0;
/* decode 24-bit address */
for (i=0; i<6; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
address |= (n << ((5 - i) * 4));
}
/* decode 16-bit data */
string++;
for (i=0; i<4; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
data |= (n << ((3 - i) * 4));
}
/* code length */ /* code length */
len = 11; len = 11;
} }
else else
{ {
/* 8-bit code (xxAAAA:DD) */ /* code length */
if (strlen(string) < 9) return 0; len = 7;
/* decode 16-bit address */ }
}
//Action Replay
else if ((strlen(string) >=9) && (string[4] == '-')){
string+=2; string+=2;
/* decode 16-bit address */
for (i=0; i<4; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
address |= (n << ((3 - i) * 4));
if (i==1) string++;
}
/* decode 8-bit data */
for (i=0; i<2; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
data |= (n << ((1 - i) * 4));
}
/* code length */
len = 9;
}
//Fusion RAM
else if ((strlen(string) >=7) && (string[4] == ':'))
{
/* decode 16-bit address */
for (i=0; i<4; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
address |= (n << ((3 - i) * 4));
}
/* decode 8-bit data */
string++;
for (i=0; i<2; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
data |= (n << ((1 - i) * 4));
}
/* code length */
len = 7;
}
//Fusion ROM
else if ((strlen(string) >=9) && (string[6] == ':'))
{
/* decode reference 8-bit data */
for (i=0; i<2; i++)
{
p = strchr (arvalidchars, *string++);
if (p == NULL) return 0;
n = (p - arvalidchars) & 0xF;
ref |= (n << ((1 - i) * 4));
}
/* decode 16-bit address */
for (i=0; i<4; i++) for (i=0; i<4; i++)
{ {
p = strchr (arvalidchars, *string++); p = strchr (arvalidchars, *string++);
@ -1331,10 +1380,6 @@ static uint32_t decode_cheat(char *string, int index)
n = (p - arvalidchars) & 0xF; n = (p - arvalidchars) & 0xF;
address |= (n << ((3 - i) * 4)); address |= (n << ((3 - i) * 4));
} }
/* ROM addresses are not supported */
if (address < 0xC000) return 0;
/* convert to 24-bit Work RAM address */
address = 0xFF0000 | (address & 0x1FFF);
/* decode 8-bit data */ /* decode 8-bit data */
string++; string++;
for (i=0; i<2; i++) for (i=0; i<2; i++)
@ -1347,6 +1392,11 @@ static uint32_t decode_cheat(char *string, int index)
/* code length */ /* code length */
len = 9; len = 9;
} }
/* convert to 24-bit Work RAM address */
if (address >= 0xC000)
{
address = 0xFF0000 | (address & 0x1FFF);
}
} }
/* Valid code found ? */ /* Valid code found ? */
if (len) if (len)
@ -1354,6 +1404,7 @@ static uint32_t decode_cheat(char *string, int index)
/* update cheat address & data values */ /* update cheat address & data values */
cheatlist[index].address = address; cheatlist[index].address = address;
cheatlist[index].data = data; cheatlist[index].data = data;
cheatlist[index].old = ref;
} }
/* return code length (0 = invalid) */ /* return code length (0 = invalid) */
return len; return len;
@ -1497,8 +1548,8 @@ void ROMCheatUpdate(void)
/* get current banked ROM address */ /* get current banked ROM address */
ptr = &z80_readmap[(cheatlist[index].address) >> 10][cheatlist[index].address & 0x03FF]; ptr = &z80_readmap[(cheatlist[index].address) >> 10][cheatlist[index].address & 0x03FF];
/* check if reference matches original ROM data */ /* check if reference exists and matches original ROM data */
if (((uint8_t)cheatlist[index].old) == *ptr) if (!cheatlist[index].old || ((uint8_t)cheatlist[index].old) == *ptr)
{ {
/* patch data */ /* patch data */
*ptr = cheatlist[index].data; *ptr = cheatlist[index].data;
@ -1892,11 +1943,23 @@ void retro_cheat_reset(void)
void retro_cheat_set(unsigned index, bool enabled, const char *code) void retro_cheat_set(unsigned index, bool enabled, const char *code)
{ {
char codeCopy[256];
char *buff;
/* Avoid crashing when giving empty input */
if (code=='\0') return;
/* clear existing ROM patches */ /* clear existing ROM patches */
clear_cheats(); clear_cheats();
/* Detect and split multiline cheats */
strcpy(codeCopy,code);
buff = strtok(codeCopy,"+");
while (buff != NULL)
{
/* interpret code and check if this is a valid cheat code */ /* interpret code and check if this is a valid cheat code */
if (decode_cheat((char *)code, maxcheats)) if (decode_cheat((char *)buff, maxcheats))
{ {
int i; int i;
@ -1918,6 +1981,8 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code)
maxcheats++; maxcheats++;
} }
} }
buff = strtok(NULL,"+");
}
/* apply ROM patches */ /* apply ROM patches */
apply_cheats(); apply_cheats();