beetle-wswan-libretro/mednafen/mempatcher.cpp

454 lines
9.9 KiB
C++
Raw Permalink Normal View History

2012-06-03 15:48:14 +00:00
/* Mednafen - Multi-system Emulator
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2020-09-18 19:02:48 +00:00
#include <stdio.h>
2020-09-23 00:35:12 +00:00
#include <stdlib.h>
2012-06-03 15:48:14 +00:00
#include <string.h>
#include <ctype.h>
#include <vector>
#include "mempatcher.h"
2020-09-23 00:35:12 +00:00
#include "settings.h"
2012-06-03 15:48:14 +00:00
2012-11-10 19:44:37 +00:00
#ifdef _WIN32
#include "msvc_compat.h"
#endif
2012-06-03 15:48:14 +00:00
static uint8 **RAMPtrs = NULL;
static uint32 PageSize;
static uint32 NumPages;
typedef struct __CHEATF
{
char *name;
char *conditions;
uint32 addr;
uint64 val;
uint64 compare;
unsigned int length;
bool bigendian;
2022-04-09 04:06:09 +00:00
unsigned int icount; /* Instance count */
2012-06-03 15:48:14 +00:00
char type; /* 'R' for replace, 'S' for substitute(GG), 'C' for substitute with compare */
int status;
} CHEATF;
static std::vector<CHEATF> cheats;
2016-06-07 11:04:25 +00:00
static bool CheatsActive = true;
2012-06-03 15:48:14 +00:00
2022-12-14 18:10:45 +00:00
static std::vector<SUBCHEAT> SubCheats[8];
2012-06-03 15:48:14 +00:00
static void RebuildSubCheats(void)
{
std::vector<CHEATF>::iterator chit;
for(int x = 0; x < 8; x++)
SubCheats[x].clear();
if(!CheatsActive) return;
for(chit = cheats.begin(); chit != cheats.end(); chit++)
{
if(chit->status && chit->type != 'R')
{
for(unsigned int x = 0; x < chit->length; x++)
{
SUBCHEAT tmpsub;
unsigned int shiftie;
if(chit->bigendian)
shiftie = (chit->length - 1 - x) * 8;
else
shiftie = x * 8;
tmpsub.addr = chit->addr + x;
tmpsub.value = (chit->val >> shiftie) & 0xFF;
if(chit->type == 'C')
tmpsub.compare = (chit->compare >> shiftie) & 0xFF;
else
tmpsub.compare = -1;
SubCheats[(chit->addr + x) & 0x7].push_back(tmpsub);
}
}
}
}
2020-09-23 01:01:21 +00:00
extern "C" bool MDFNMP_Init(uint32 ps, uint32 numpages)
2012-06-03 15:48:14 +00:00
{
PageSize = ps;
NumPages = numpages;
RAMPtrs = (uint8 **)calloc(numpages, sizeof(uint8 *));
CheatsActive = MDFN_GetSettingB("cheats");
return(1);
}
2020-09-23 01:01:21 +00:00
extern "C" void MDFNMP_Kill(void)
2012-06-03 15:48:14 +00:00
{
if(RAMPtrs)
{
free(RAMPtrs);
RAMPtrs = NULL;
}
}
extern "C" void MDFNMP_AddRAM(uint32 size, uint32 A, uint8 *RAM)
2012-06-03 15:48:14 +00:00
{
uint32 AB = A / PageSize;
size /= PageSize;
for(unsigned int x = 0; x < size; x++)
{
RAMPtrs[AB + x] = RAM;
2022-04-09 04:06:09 +00:00
if(RAM) /* Don't increment the RAM pointer if we're passed a NULL pointer */
2012-06-03 15:48:14 +00:00
RAM += PageSize;
}
}
2020-12-10 01:35:47 +00:00
extern "C" void MDFNMP_InstallReadPatches(void) { }
void MDFNMP_RemoveReadPatches(void) { }
2012-06-03 15:48:14 +00:00
/* This function doesn't allocate any memory for "name" */
static int AddCheatEntry(char *name, char *conditions, uint32 addr, uint64 val, uint64 compare, int status, char type, unsigned int length, bool bigendian)
{
2015-07-29 14:27:29 +00:00
CHEATF temp;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
memset(&temp, 0, sizeof(CHEATF));
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
temp.name=name;
temp.conditions = conditions;
temp.addr=addr;
temp.val=val;
temp.status=status;
temp.compare=compare;
temp.length = length;
temp.bigendian = bigendian;
temp.type=type;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
cheats.push_back(temp);
return(1);
2012-06-03 15:48:14 +00:00
}
2020-09-23 01:01:21 +00:00
extern "C" void MDFN_LoadGameCheats(void *override_ptr)
2012-06-03 15:48:14 +00:00
{
2015-07-23 15:57:44 +00:00
RebuildSubCheats();
2012-06-03 15:48:14 +00:00
}
2020-09-23 01:01:21 +00:00
extern "C" void MDFN_FlushGameCheats(int nosave)
2012-06-03 15:48:14 +00:00
{
2015-07-29 14:27:29 +00:00
std::vector<CHEATF>::iterator chit;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
for(chit = cheats.begin(); chit != cheats.end(); chit++)
2012-06-03 15:48:14 +00:00
{
2015-07-29 14:27:29 +00:00
free(chit->name);
if(chit->conditions)
free(chit->conditions);
2012-06-03 15:48:14 +00:00
}
2015-07-29 14:27:29 +00:00
cheats.clear();
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
RebuildSubCheats();
2012-06-03 15:48:14 +00:00
}
int MDFNI_AddCheat(const char *name, uint32 addr, uint64 val, uint64 compare, char type, unsigned int length, bool bigendian)
{
2015-07-23 15:57:44 +00:00
char *t;
2012-06-03 15:48:14 +00:00
2015-07-23 15:57:44 +00:00
if(!(t = strdup(name)))
return(0);
2012-06-03 15:48:14 +00:00
2015-07-23 15:57:44 +00:00
if(!AddCheatEntry(t, NULL, addr,val,compare,1,type, length, bigendian))
{
free(t);
return(0);
}
2012-06-03 15:48:14 +00:00
2015-07-23 15:57:44 +00:00
MDFNMP_RemoveReadPatches();
RebuildSubCheats();
MDFNMP_InstallReadPatches();
2012-06-03 15:48:14 +00:00
2015-07-23 15:57:44 +00:00
return(1);
2012-06-03 15:48:14 +00:00
}
int MDFNI_DelCheat(uint32 which)
{
2015-07-29 14:27:29 +00:00
free(cheats[which].name);
cheats.erase(cheats.begin() + which);
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
MDFNMP_RemoveReadPatches();
RebuildSubCheats();
MDFNMP_InstallReadPatches();
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
return(1);
2012-06-03 15:48:14 +00:00
}
/*
Condition format(ws = white space):
<variable size><ws><endian><ws><address><ws><operation><ws><value>
[,second condition...etc.]
Value should be unsigned integer, hex(with a 0x prefix) or
base-10.
Operations:
>=
<=
>
<
==
!=
2022-04-09 04:06:09 +00:00
& Result of AND between two values is nonzero
!& Result of AND between two values is zero
^ same, XOR
2012-06-03 15:48:14 +00:00
!^
2022-04-09 04:06:09 +00:00
| same, OR
2012-06-03 15:48:14 +00:00
!|
Full example:
2 L 0xADDE == 0xDEAD, 1 L 0xC000 == 0xA0
*/
static bool TestConditions(const char *string)
{
2015-07-29 14:27:29 +00:00
char address[64];
char operation[64];
char value[64];
char endian;
unsigned int bytelen;
bool passed = 1;
2012-06-03 15:48:14 +00:00
2016-06-06 00:03:25 +00:00
while(sscanf(string, "%u %c %63s %63s %63s", &bytelen, &endian, address, operation, value) == 5 && passed)
2015-07-29 14:27:29 +00:00
{
uint64 v_value;
uint64 value_at_address;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
if(value[0] == '0' && value[1] == 'x')
v_value = strtoull(value + 2, NULL, 16);
else
v_value = strtoull(value, NULL, 0);
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
value_at_address = 0;
2016-08-09 06:12:59 +00:00
2015-07-29 14:27:29 +00:00
if(!strcmp(operation, ">="))
{
if(!(value_at_address >= v_value))
passed = 0;
}
else if(!strcmp(operation, "<="))
{
if(!(value_at_address <= v_value))
passed = 0;
}
else if(!strcmp(operation, ">"))
{
if(!(value_at_address > v_value))
passed = 0;
}
else if(!strcmp(operation, "<"))
{
if(!(value_at_address < v_value))
passed = 0;
}
else if(!strcmp(operation, "=="))
{
if(!(value_at_address == v_value))
passed = 0;
}
else if(!strcmp(operation, "!="))
{
if(!(value_at_address != v_value))
passed = 0;
}
else if(!strcmp(operation, "&"))
{
if(!(value_at_address & v_value))
passed = 0;
}
else if(!strcmp(operation, "!&"))
{
if(value_at_address & v_value)
passed = 0;
}
else if(!strcmp(operation, "^"))
{
if(!(value_at_address ^ v_value))
passed = 0;
}
else if(!strcmp(operation, "!^"))
{
if(value_at_address ^ v_value)
passed = 0;
}
else if(!strcmp(operation, "|"))
{
if(!(value_at_address | v_value))
passed = 0;
}
else if(!strcmp(operation, "!|"))
{
if(value_at_address | v_value)
passed = 0;
}
string = strchr(string, ',');
2022-04-09 04:06:09 +00:00
if(!string)
2015-07-29 14:27:29 +00:00
break;
2022-04-09 04:06:09 +00:00
string++;
2012-06-03 15:48:14 +00:00
}
2015-07-29 14:27:29 +00:00
return(passed);
2012-06-03 15:48:14 +00:00
}
2020-09-23 01:01:21 +00:00
extern "C" void MDFNMP_ApplyPeriodicCheats(void)
2012-06-03 15:48:14 +00:00
{
2015-07-29 14:27:29 +00:00
std::vector<CHEATF>::iterator chit;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
if(!CheatsActive)
return;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
for(chit = cheats.begin(); chit != cheats.end(); chit++)
{
if(chit->status && chit->type == 'R')
{
if(!chit->conditions || TestConditions(chit->conditions))
for(unsigned int x = 0; x < chit->length; x++)
{
uint32 page = ((chit->addr + x) / PageSize) % NumPages;
if(RAMPtrs[page])
{
uint64 tmpval = chit->val;
if(chit->bigendian)
tmpval >>= (chit->length - 1 - x) * 8;
else
tmpval >>= x * 8;
RAMPtrs[page][(chit->addr + x) % PageSize] = tmpval;
}
}
}
}
2012-06-03 15:48:14 +00:00
}
2015-07-29 14:27:29 +00:00
void MDFNI_ListCheats(int (*callb)(char *name, uint32 a, uint64 v, uint64 compare, int s, char type, unsigned int length, bool bigendian, void *data), void *data)
2012-06-03 15:48:14 +00:00
{
2015-07-29 14:27:29 +00:00
std::vector<CHEATF>::iterator chit;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
for(chit = cheats.begin(); chit != cheats.end(); chit++)
{
if(!callb(chit->name, chit->addr, chit->val, chit->compare, chit->status, chit->type, chit->length, chit->bigendian, data)) break;
}
2012-06-03 15:48:14 +00:00
}
2015-07-29 14:27:29 +00:00
int MDFNI_GetCheat(uint32 which, char **name, uint32 *a, uint64 *v, uint64 *compare, int *s, char *type, unsigned int *length, bool *bigendian)
2012-06-03 15:48:14 +00:00
{
2015-07-29 14:27:29 +00:00
CHEATF *next = &cheats[which];
if(name)
*name=next->name;
if(a)
*a=next->addr;
if(v)
*v=next->val;
if(s)
*s=next->status;
if(compare)
*compare=next->compare;
if(type)
*type=next->type;
if(length)
*length = next->length;
if(bigendian)
*bigendian = next->bigendian;
return(1);
2012-06-03 15:48:14 +00:00
}
int MDFNI_DecodePAR(const char *str, uint32 *a, uint8 *v, uint8 *c, char *type)
{
2015-07-29 14:27:29 +00:00
int boo[4];
if(strlen(str)!=8) return(0);
2012-06-03 15:48:14 +00:00
2016-06-06 00:03:25 +00:00
sscanf(str,"%02x%02x%02x%02x",boo,boo+1,boo+2,boo+3);
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
*c = 0;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
if(1)
{
*a=(boo[3]<<8)|(boo[2]+0x7F);
*v=0;
}
else
{
*v=boo[3];
*a=boo[2]|(boo[1]<<8);
}
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
*type = 'S';
return(1);
2012-06-03 15:48:14 +00:00
}
/* name can be NULL if the name isn't going to be changed. */
int MDFNI_SetCheat(uint32 which, const char *name, uint32 a, uint64 v, uint64 compare, int s, char type, unsigned int length, bool bigendian)
{
2015-07-29 14:27:29 +00:00
CHEATF *next = &cheats[which];
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
if(name)
{
char *t;
2012-06-03 15:48:14 +00:00
2021-03-24 13:29:34 +00:00
if(!(t=(char *)realloc(next->name,strlen(name)+1)))
2015-07-29 14:27:29 +00:00
return(0);
2021-03-24 13:29:34 +00:00
next->name=t;
strcpy(next->name,name);
2015-07-29 14:27:29 +00:00
}
next->addr=a;
next->val=v;
next->status=s;
next->compare=compare;
next->type=type;
next->length = length;
next->bigendian = bigendian;
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
RebuildSubCheats();
2012-06-03 15:48:14 +00:00
2015-07-29 14:27:29 +00:00
return(1);
2012-06-03 15:48:14 +00:00
}
/* Convenience function. */
int MDFNI_ToggleCheat(uint32 which)
{
cheats[which].status = !cheats[which].status;
RebuildSubCheats();
return(cheats[which].status);
}
static void SettingChanged(const char *name)
{
MDFNMP_RemoveReadPatches();
CheatsActive = MDFN_GetSettingB("cheats");
RebuildSubCheats();
MDFNMP_InstallReadPatches();
}