mirror of
https://github.com/RaySollium99/picodrive.git
synced 2025-09-05 23:37:46 -04:00
Add support for Master System cheats.
notaz: drop wrong code (PicoWrite8_z80 are 68k functions, not for sms)
This commit is contained in:
parent
8655fd0462
commit
ed4a2193f7
2 changed files with 278 additions and 107 deletions
302
pico/patch.c
302
pico/patch.c
|
@ -29,12 +29,13 @@ struct patch
|
||||||
{
|
{
|
||||||
unsigned int addr;
|
unsigned int addr;
|
||||||
unsigned short data;
|
unsigned short data;
|
||||||
|
unsigned char comp;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct patch_inst *PicoPatches = NULL;
|
struct patch_inst *PicoPatches = NULL;
|
||||||
int PicoPatchCount = 0;
|
int PicoPatchCount = 0;
|
||||||
|
|
||||||
static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
|
static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
|
||||||
|
|
||||||
/* genie_decode
|
/* genie_decode
|
||||||
* This function converts a Game Genie code to an address:data pair.
|
* This function converts a Game Genie code to an address:data pair.
|
||||||
|
@ -48,20 +49,23 @@ static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I223
|
||||||
* by result. If an error results, both the address and data will be set to -1.
|
* by result. If an error results, both the address and data will be set to -1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void genie_decode(const char* code, struct patch* result)
|
static void genie_decode_md(const char* code, struct patch* result)
|
||||||
{
|
{
|
||||||
int i = 0, n;
|
int i = 0, n;
|
||||||
char* x;
|
char* x;
|
||||||
|
|
||||||
for(; i < 8; ++i)
|
for(; i < 9; ++i)
|
||||||
{
|
{
|
||||||
|
/* Skip i=4; it's going to be the separating hyphen */
|
||||||
|
if (i==4) continue;
|
||||||
|
|
||||||
/* If strchr returns NULL, we were given a bad character */
|
/* If strchr returns NULL, we were given a bad character */
|
||||||
if(!(x = strchr(genie_chars, code[i])))
|
if(!(x = strchr(genie_chars_md, code[i])))
|
||||||
{
|
{
|
||||||
result->addr = -1; result->data = -1;
|
result->addr = -1; result->data = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
n = (x - genie_chars) >> 1;
|
n = (x - genie_chars_md) >> 1;
|
||||||
/* Now, based on which character this is, fit it into the result */
|
/* Now, based on which character this is, fit it into the result */
|
||||||
switch(i)
|
switch(i)
|
||||||
{
|
{
|
||||||
|
@ -82,21 +86,21 @@ static void genie_decode(const char* code, struct patch* result)
|
||||||
/* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
|
/* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
|
||||||
result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
|
result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 5:
|
||||||
/* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
|
/* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
|
||||||
result->data |= (n & 1) << 12;
|
result->data |= (n & 1) << 12;
|
||||||
result->addr |= (n >> 1) << 16;
|
result->addr |= (n >> 1) << 16;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 6:
|
||||||
/* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
|
/* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
|
||||||
result->data |= (n & 1) << 15 | (n >> 1) << 8;
|
result->data |= (n & 1) << 15 | (n >> 1) << 8;
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 7:
|
||||||
/* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
|
/* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
|
||||||
result->data |= (n >> 3) << 13;
|
result->data |= (n >> 3) << 13;
|
||||||
result->addr |= (n & 7) << 5;
|
result->addr |= (n & 7) << 5;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 8:
|
||||||
/* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
|
/* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
|
||||||
result->addr |= n;
|
result->addr |= n;
|
||||||
break;
|
break;
|
||||||
|
@ -114,7 +118,7 @@ static void genie_decode(const char* code, struct patch* result)
|
||||||
|
|
||||||
static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
|
static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
|
||||||
|
|
||||||
static void hex_decode(const char *code, struct patch *result)
|
static void hex_decode_md(const char *code, struct patch *result)
|
||||||
{
|
{
|
||||||
char *x;
|
char *x;
|
||||||
int i;
|
int i;
|
||||||
|
@ -129,7 +133,142 @@ static void hex_decode(const char *code, struct patch *result)
|
||||||
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
||||||
}
|
}
|
||||||
/* 4 digits for data */
|
/* 4 digits for data */
|
||||||
for(i = 6; i < 10; ++i)
|
for(i = 7; i < 11; ++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
if (i==8) break;
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void genie_decode_ms(const char *code, struct patch *result)
|
||||||
|
{
|
||||||
|
char *x;
|
||||||
|
int i;
|
||||||
|
/* 2 digits for data */
|
||||||
|
for(i=0;i<2;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* 4 digits for address */
|
||||||
|
for(i=2;i<7;++i)
|
||||||
|
{
|
||||||
|
/* 4th character is hyphen and can be skipped*/
|
||||||
|
if (i==3) continue;
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* Correct the address */
|
||||||
|
result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
|
||||||
|
/* Optional: 3 digits for comp */
|
||||||
|
if (code[8]){
|
||||||
|
for(i=8;i<11;++i)
|
||||||
|
{
|
||||||
|
if (i==9) continue; /* 2nd character is ignored */
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* Correct the comp */
|
||||||
|
result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ar_decode_ms(const char *code, struct patch *result){
|
||||||
|
char *x;
|
||||||
|
int i;
|
||||||
|
/* 2 digits of padding*/
|
||||||
|
/* 4 digits for address */
|
||||||
|
for(i=2;i<7;++i)
|
||||||
|
{
|
||||||
|
/* 5th character is hyphen and can be skipped*/
|
||||||
|
if (i==4) continue;
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* 2 digits for data */
|
||||||
|
for(i=7;i<9;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fusion_ram_decode(const char *code, struct patch *result){
|
||||||
|
char *x;
|
||||||
|
int i;
|
||||||
|
/* 4 digits for address */
|
||||||
|
for(i=0;i<4;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* Skip the ':' */
|
||||||
|
/* 2 digits for data */
|
||||||
|
for(i=5;i<7;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fusion_rom_decode(const char *code, struct patch *result){
|
||||||
|
char *x;
|
||||||
|
int i;
|
||||||
|
/* 2 digits for comp */
|
||||||
|
for(i=0;i<2;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* 4 digits for address */
|
||||||
|
for(i=2;i<6;++i)
|
||||||
|
{
|
||||||
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
|
{
|
||||||
|
result->addr = result->data = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
|
||||||
|
}
|
||||||
|
/* 2 digits for data */
|
||||||
|
for(i=7;i<9;++i)
|
||||||
{
|
{
|
||||||
if(!(x = strchr(hex_chars, code[i])))
|
if(!(x = strchr(hex_chars, code[i])))
|
||||||
{
|
{
|
||||||
|
@ -145,75 +284,76 @@ static void hex_decode(const char *code, struct patch *result)
|
||||||
* decoder. */
|
* decoder. */
|
||||||
void decode(const char* code, struct patch* result)
|
void decode(const char* code, struct patch* result)
|
||||||
{
|
{
|
||||||
int len = strlen(code), i, j;
|
int len = strlen(code);
|
||||||
char code_to_pass[16], *x;
|
|
||||||
const char *ad, *da;
|
|
||||||
int adl, dal;
|
|
||||||
|
|
||||||
/* Initialize the result */
|
/* Initialize the result */
|
||||||
result->addr = result->data = 0;
|
result->addr = result->data = result->comp = 0;
|
||||||
|
|
||||||
/* Just assume 8 char long string to be Game Genie code */
|
if(!(PicoAHW & PAHW_SMS))
|
||||||
if (len == 8)
|
|
||||||
{
|
{
|
||||||
genie_decode(code, result);
|
//If Genesis
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
|
//Game Genie
|
||||||
* code. */
|
|
||||||
if(len == 9 && code[4] == '-')
|
if(len == 9 && code[4] == '-')
|
||||||
{
|
{
|
||||||
/* Remove the hyphen and pass to genie_decode */
|
genie_decode_md(code, result);
|
||||||
code_to_pass[0] = code[0];
|
|
||||||
code_to_pass[1] = code[1];
|
|
||||||
code_to_pass[2] = code[2];
|
|
||||||
code_to_pass[3] = code[3];
|
|
||||||
code_to_pass[4] = code[5];
|
|
||||||
code_to_pass[5] = code[6];
|
|
||||||
code_to_pass[6] = code[7];
|
|
||||||
code_to_pass[7] = code[8];
|
|
||||||
code_to_pass[8] = '\0';
|
|
||||||
genie_decode(code_to_pass, result);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise, we assume it's a hex code.
|
//Master
|
||||||
* Find the colon so we know where address ends and data starts. If there's
|
else if(len >=9 && code[6] == ':')
|
||||||
* no colon, then we haven't a code at all! */
|
{
|
||||||
if(!(x = strchr(code, ':'))) goto bad_code;
|
hex_decode_md(code, result);
|
||||||
ad = code; da = x + 1; adl = x - code; dal = len - adl - 1;
|
}
|
||||||
|
|
||||||
/* If a section is empty or too long, toss it */
|
else
|
||||||
if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
|
{
|
||||||
|
goto bad_code;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//If Master System
|
||||||
|
|
||||||
/* Pad the address with zeros, then fill it with the value */
|
//Genie
|
||||||
for(i = 0; i < (6 - adl); ++i) code_to_pass[i] = '0';
|
if(len == 11 && code[3] == '-' && code[7] == '-')
|
||||||
for(j = 0; i < 6; ++i, ++j) code_to_pass[i] = ad[j];
|
{
|
||||||
|
genie_decode_ms(code, result);
|
||||||
|
}
|
||||||
|
|
||||||
/* Do the same for data */
|
//AR
|
||||||
for(i = 6; i < (10 - dal); ++i) code_to_pass[i] = '0';
|
else if(len == 9 && code[4] == '-')
|
||||||
for(j = 0; i < 10; ++i, ++j) code_to_pass[i] = da[j];
|
{
|
||||||
|
ar_decode_ms(code, result);
|
||||||
|
}
|
||||||
|
|
||||||
code_to_pass[10] = '\0';
|
//Fusion RAM
|
||||||
|
else if(len == 7 && code[4] == ':')
|
||||||
|
{
|
||||||
|
fusion_ram_decode(code, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fusion ROM
|
||||||
|
else if(len == 9 && code[6] == ':')
|
||||||
|
{
|
||||||
|
fusion_rom_decode(code, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
goto bad_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Convert RAM address space to Genesis location.
|
||||||
|
if (result->addr>=0xC000)
|
||||||
|
result->addr= 0xFF0000 | (0x1FFF & result->addr);
|
||||||
|
}
|
||||||
|
|
||||||
/* Decode and goodbye */
|
|
||||||
hex_decode(code_to_pass, result);
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
bad_code:
|
bad_code:
|
||||||
|
|
||||||
/* AGH! Invalid code! */
|
|
||||||
result->data = result->addr = -1;
|
result->data = result->addr = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int PicoRead16(unsigned int a);
|
|
||||||
void PicoWrite16(unsigned int a, unsigned short d);
|
|
||||||
|
|
||||||
|
|
||||||
void PicoPatchUnload(void)
|
void PicoPatchUnload(void)
|
||||||
{
|
{
|
||||||
if (PicoPatches != NULL)
|
if (PicoPatches != NULL)
|
||||||
|
@ -294,14 +434,21 @@ int PicoPatchLoad(const char *fname)
|
||||||
void PicoPatchPrepare(void)
|
void PicoPatchPrepare(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int addr;
|
||||||
|
|
||||||
for (i = 0; i < PicoPatchCount; i++)
|
for (i = 0; i < PicoPatchCount; i++)
|
||||||
{
|
{
|
||||||
PicoPatches[i].addr &= ~1;
|
addr=PicoPatches[i].addr;
|
||||||
if (PicoPatches[i].addr < Pico.romsize)
|
addr &= ~1;
|
||||||
PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + PicoPatches[i].addr);
|
if (addr < Pico.romsize)
|
||||||
|
PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
|
||||||
else
|
else
|
||||||
PicoPatches[i].data_old = (unsigned short) m68k_read16(PicoPatches[i].addr);
|
{
|
||||||
|
if(!(PicoAHW & PAHW_SMS))
|
||||||
|
PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
|
||||||
|
else
|
||||||
|
; // wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
|
||||||
|
}
|
||||||
if (strstr(PicoPatches[i].name, "AUTO"))
|
if (strstr(PicoPatches[i].name, "AUTO"))
|
||||||
PicoPatches[i].active = 1;
|
PicoPatches[i].active = 1;
|
||||||
}
|
}
|
||||||
|
@ -315,16 +462,28 @@ void PicoPatchApply(void)
|
||||||
for (i = 0; i < PicoPatchCount; i++)
|
for (i = 0; i < PicoPatchCount; i++)
|
||||||
{
|
{
|
||||||
addr = PicoPatches[i].addr;
|
addr = PicoPatches[i].addr;
|
||||||
|
|
||||||
if (addr < Pico.romsize)
|
if (addr < Pico.romsize)
|
||||||
{
|
{
|
||||||
if (PicoPatches[i].active)
|
if (PicoPatches[i].active)
|
||||||
|
{
|
||||||
|
if (!(PicoAHW & PAHW_SMS))
|
||||||
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
|
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
|
||||||
else {
|
else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
|
||||||
|
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// if current addr is not patched by older patch, write back original val
|
// if current addr is not patched by older patch, write back original val
|
||||||
for (u = 0; u < i; u++)
|
for (u = 0; u < i; u++)
|
||||||
if (PicoPatches[u].addr == addr) break;
|
if (PicoPatches[u].addr == addr) break;
|
||||||
if (u == i)
|
if (u == i)
|
||||||
|
{
|
||||||
|
if (!(PicoAHW & PAHW_SMS))
|
||||||
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
|
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
|
||||||
|
else
|
||||||
|
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
|
// fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
|
||||||
// *(unsigned short *)(Pico.rom + addr));
|
// *(unsigned short *)(Pico.rom + addr));
|
||||||
|
@ -332,13 +491,24 @@ void PicoPatchApply(void)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (PicoPatches[i].active)
|
if (PicoPatches[i].active)
|
||||||
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data);
|
{
|
||||||
else {
|
if (!(PicoAHW & PAHW_SMS))
|
||||||
|
m68k_write16(addr,PicoPatches[i].data);
|
||||||
|
else
|
||||||
|
;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// if current addr is not patched by older patch, write back original val
|
// if current addr is not patched by older patch, write back original val
|
||||||
for (u = 0; u < i; u++)
|
for (u = 0; u < i; u++)
|
||||||
if (PicoPatches[u].addr == addr) break;
|
if (PicoPatches[u].addr == addr) break;
|
||||||
if (u == i)
|
if (u == i)
|
||||||
|
{
|
||||||
|
if (!(PicoAHW & PAHW_SMS))
|
||||||
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
|
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
|
||||||
|
else
|
||||||
|
;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ struct patch_inst
|
||||||
unsigned int addr;
|
unsigned int addr;
|
||||||
unsigned short data;
|
unsigned short data;
|
||||||
unsigned short data_old;
|
unsigned short data_old;
|
||||||
|
unsigned char comp;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct patch_inst *PicoPatches;
|
extern struct patch_inst *PicoPatches;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue