| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <ctype.h> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "mempatcher.h" |
| 25 | #include "settings.h" |
| 26 | |
| 27 | #ifdef _WIN32 |
| 28 | #include "msvc_compat.h" |
| 29 | #endif |
| 30 | |
| 31 | static uint8 **RAMPtrs = NULL; |
| 32 | static uint32 PageSize; |
| 33 | static uint32 NumPages; |
| 34 | |
| 35 | typedef struct __CHEATF |
| 36 | { |
| 37 | char *name; |
| 38 | char *conditions; |
| 39 | |
| 40 | uint32 addr; |
| 41 | uint64 val; |
| 42 | uint64 compare; |
| 43 | |
| 44 | unsigned int length; |
| 45 | bool bigendian; |
| 46 | unsigned int icount; /* Instance count */ |
| 47 | char type; /* 'R' for replace, 'S' for substitute(GG), 'C' for substitute with compare */ |
| 48 | int status; |
| 49 | } CHEATF; |
| 50 | |
| 51 | static std::vector<CHEATF> cheats; |
| 52 | static bool CheatsActive = true; |
| 53 | |
| 54 | static std::vector<SUBCHEAT> SubCheats[8]; |
| 55 | |
| 56 | static void RebuildSubCheats(void) |
| 57 | { |
| 58 | std::vector<CHEATF>::iterator chit; |
| 59 | |
| 60 | for(int x = 0; x < 8; x++) |
| 61 | SubCheats[x].clear(); |
| 62 | |
| 63 | if(!CheatsActive) return; |
| 64 | |
| 65 | for(chit = cheats.begin(); chit != cheats.end(); chit++) |
| 66 | { |
| 67 | if(chit->status && chit->type != 'R') |
| 68 | { |
| 69 | for(unsigned int x = 0; x < chit->length; x++) |
| 70 | { |
| 71 | SUBCHEAT tmpsub; |
| 72 | unsigned int shiftie; |
| 73 | |
| 74 | if(chit->bigendian) |
| 75 | shiftie = (chit->length - 1 - x) * 8; |
| 76 | else |
| 77 | shiftie = x * 8; |
| 78 | |
| 79 | tmpsub.addr = chit->addr + x; |
| 80 | tmpsub.value = (chit->val >> shiftie) & 0xFF; |
| 81 | if(chit->type == 'C') |
| 82 | tmpsub.compare = (chit->compare >> shiftie) & 0xFF; |
| 83 | else |
| 84 | tmpsub.compare = -1; |
| 85 | SubCheats[(chit->addr + x) & 0x7].push_back(tmpsub); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | extern "C" bool MDFNMP_Init(uint32 ps, uint32 numpages) |
| 92 | { |
| 93 | PageSize = ps; |
| 94 | NumPages = numpages; |
| 95 | |
| 96 | RAMPtrs = (uint8 **)calloc(numpages, sizeof(uint8 *)); |
| 97 | |
| 98 | CheatsActive = MDFN_GetSettingB("cheats"); |
| 99 | return(1); |
| 100 | } |
| 101 | |
| 102 | extern "C" void MDFNMP_Kill(void) |
| 103 | { |
| 104 | if(RAMPtrs) |
| 105 | { |
| 106 | free(RAMPtrs); |
| 107 | RAMPtrs = NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | extern "C" void MDFNMP_AddRAM(uint32 size, uint32 A, uint8 *RAM) |
| 112 | { |
| 113 | uint32 AB = A / PageSize; |
| 114 | |
| 115 | size /= PageSize; |
| 116 | |
| 117 | for(unsigned int x = 0; x < size; x++) |
| 118 | { |
| 119 | RAMPtrs[AB + x] = RAM; |
| 120 | if(RAM) /* Don't increment the RAM pointer if we're passed a NULL pointer */ |
| 121 | RAM += PageSize; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | extern "C" void MDFNMP_InstallReadPatches(void) { } |
| 126 | void MDFNMP_RemoveReadPatches(void) { } |
| 127 | |
| 128 | /* This function doesn't allocate any memory for "name" */ |
| 129 | static int AddCheatEntry(char *name, char *conditions, uint32 addr, uint64 val, uint64 compare, int status, char type, unsigned int length, bool bigendian) |
| 130 | { |
| 131 | CHEATF temp; |
| 132 | |
| 133 | memset(&temp, 0, sizeof(CHEATF)); |
| 134 | |
| 135 | temp.name=name; |
| 136 | temp.conditions = conditions; |
| 137 | temp.addr=addr; |
| 138 | temp.val=val; |
| 139 | temp.status=status; |
| 140 | temp.compare=compare; |
| 141 | temp.length = length; |
| 142 | temp.bigendian = bigendian; |
| 143 | temp.type=type; |
| 144 | |
| 145 | cheats.push_back(temp); |
| 146 | return(1); |
| 147 | } |
| 148 | |
| 149 | extern "C" void MDFN_LoadGameCheats(void *override_ptr) |
| 150 | { |
| 151 | RebuildSubCheats(); |
| 152 | } |
| 153 | |
| 154 | extern "C" void MDFN_FlushGameCheats(int nosave) |
| 155 | { |
| 156 | std::vector<CHEATF>::iterator chit; |
| 157 | |
| 158 | for(chit = cheats.begin(); chit != cheats.end(); chit++) |
| 159 | { |
| 160 | free(chit->name); |
| 161 | if(chit->conditions) |
| 162 | free(chit->conditions); |
| 163 | } |
| 164 | cheats.clear(); |
| 165 | |
| 166 | RebuildSubCheats(); |
| 167 | } |
| 168 | |
| 169 | int MDFNI_AddCheat(const char *name, uint32 addr, uint64 val, uint64 compare, char type, unsigned int length, bool bigendian) |
| 170 | { |
| 171 | char *t; |
| 172 | |
| 173 | if(!(t = strdup(name))) |
| 174 | return(0); |
| 175 | |
| 176 | if(!AddCheatEntry(t, NULL, addr,val,compare,1,type, length, bigendian)) |
| 177 | { |
| 178 | free(t); |
| 179 | return(0); |
| 180 | } |
| 181 | |
| 182 | MDFNMP_RemoveReadPatches(); |
| 183 | RebuildSubCheats(); |
| 184 | MDFNMP_InstallReadPatches(); |
| 185 | |
| 186 | return(1); |
| 187 | } |
| 188 | |
| 189 | int MDFNI_DelCheat(uint32 which) |
| 190 | { |
| 191 | free(cheats[which].name); |
| 192 | cheats.erase(cheats.begin() + which); |
| 193 | |
| 194 | MDFNMP_RemoveReadPatches(); |
| 195 | RebuildSubCheats(); |
| 196 | MDFNMP_InstallReadPatches(); |
| 197 | |
| 198 | return(1); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | Condition format(ws = white space): |
| 203 | |
| 204 | <variable size><ws><endian><ws><address><ws><operation><ws><value> |
| 205 | [,second condition...etc.] |
| 206 | |
| 207 | Value should be unsigned integer, hex(with a 0x prefix) or |
| 208 | base-10. |
| 209 | |
| 210 | Operations: |
| 211 | >= |
| 212 | <= |
| 213 | > |
| 214 | < |
| 215 | == |
| 216 | != |
| 217 | & Result of AND between two values is nonzero |
| 218 | !& Result of AND between two values is zero |
| 219 | ^ same, XOR |
| 220 | !^ |
| 221 | | same, OR |
| 222 | !| |
| 223 | |
| 224 | Full example: |
| 225 | |
| 226 | 2 L 0xADDE == 0xDEAD, 1 L 0xC000 == 0xA0 |
| 227 | |
| 228 | */ |
| 229 | |
| 230 | static bool TestConditions(const char *string) |
| 231 | { |
| 232 | char address[64]; |
| 233 | char operation[64]; |
| 234 | char value[64]; |
| 235 | char endian; |
| 236 | unsigned int bytelen; |
| 237 | bool passed = 1; |
| 238 | |
| 239 | while(sscanf(string, "%u %c %63s %63s %63s", &bytelen, &endian, address, operation, value) == 5 && passed) |
| 240 | { |
| 241 | uint64 v_value; |
| 242 | uint64 value_at_address; |
| 243 | |
| 244 | if(value[0] == '0' && value[1] == 'x') |
| 245 | v_value = strtoull(value + 2, NULL, 16); |
| 246 | else |
| 247 | v_value = strtoull(value, NULL, 0); |
| 248 | |
| 249 | value_at_address = 0; |
| 250 | |
| 251 | if(!strcmp(operation, ">=")) |
| 252 | { |
| 253 | if(!(value_at_address >= v_value)) |
| 254 | passed = 0; |
| 255 | } |
| 256 | else if(!strcmp(operation, "<=")) |
| 257 | { |
| 258 | if(!(value_at_address <= v_value)) |
| 259 | passed = 0; |
| 260 | } |
| 261 | else if(!strcmp(operation, ">")) |
| 262 | { |
| 263 | if(!(value_at_address > v_value)) |
| 264 | passed = 0; |
| 265 | } |
| 266 | else if(!strcmp(operation, "<")) |
| 267 | { |
| 268 | if(!(value_at_address < v_value)) |
| 269 | passed = 0; |
| 270 | } |
| 271 | else if(!strcmp(operation, "==")) |
| 272 | { |
| 273 | if(!(value_at_address == v_value)) |
| 274 | passed = 0; |
| 275 | } |
| 276 | else if(!strcmp(operation, "!=")) |
| 277 | { |
| 278 | if(!(value_at_address != v_value)) |
| 279 | passed = 0; |
| 280 | } |
| 281 | else if(!strcmp(operation, "&")) |
| 282 | { |
| 283 | if(!(value_at_address & v_value)) |
| 284 | passed = 0; |
| 285 | } |
| 286 | else if(!strcmp(operation, "!&")) |
| 287 | { |
| 288 | if(value_at_address & v_value) |
| 289 | passed = 0; |
| 290 | } |
| 291 | else if(!strcmp(operation, "^")) |
| 292 | { |
| 293 | if(!(value_at_address ^ v_value)) |
| 294 | passed = 0; |
| 295 | } |
| 296 | else if(!strcmp(operation, "!^")) |
| 297 | { |
| 298 | if(value_at_address ^ v_value) |
| 299 | passed = 0; |
| 300 | } |
| 301 | else if(!strcmp(operation, "|")) |
| 302 | { |
| 303 | if(!(value_at_address | v_value)) |
| 304 | passed = 0; |
| 305 | } |
| 306 | else if(!strcmp(operation, "!|")) |
| 307 | { |
| 308 | if(value_at_address | v_value) |
| 309 | passed = 0; |
| 310 | } |
| 311 | string = strchr(string, ','); |
| 312 | if(!string) |
| 313 | break; |
| 314 | string++; |
| 315 | } |
| 316 | |
| 317 | return(passed); |
| 318 | } |
| 319 | |
| 320 | extern "C" void MDFNMP_ApplyPeriodicCheats(void) |
| 321 | { |
| 322 | std::vector<CHEATF>::iterator chit; |
| 323 | |
| 324 | if(!CheatsActive) |
| 325 | return; |
| 326 | |
| 327 | for(chit = cheats.begin(); chit != cheats.end(); chit++) |
| 328 | { |
| 329 | if(chit->status && chit->type == 'R') |
| 330 | { |
| 331 | if(!chit->conditions || TestConditions(chit->conditions)) |
| 332 | for(unsigned int x = 0; x < chit->length; x++) |
| 333 | { |
| 334 | uint32 page = ((chit->addr + x) / PageSize) % NumPages; |
| 335 | if(RAMPtrs[page]) |
| 336 | { |
| 337 | uint64 tmpval = chit->val; |
| 338 | |
| 339 | if(chit->bigendian) |
| 340 | tmpval >>= (chit->length - 1 - x) * 8; |
| 341 | else |
| 342 | tmpval >>= x * 8; |
| 343 | |
| 344 | RAMPtrs[page][(chit->addr + x) % PageSize] = tmpval; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | 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) |
| 352 | { |
| 353 | std::vector<CHEATF>::iterator chit; |
| 354 | |
| 355 | for(chit = cheats.begin(); chit != cheats.end(); chit++) |
| 356 | { |
| 357 | if(!callb(chit->name, chit->addr, chit->val, chit->compare, chit->status, chit->type, chit->length, chit->bigendian, data)) break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | int MDFNI_GetCheat(uint32 which, char **name, uint32 *a, uint64 *v, uint64 *compare, int *s, char *type, unsigned int *length, bool *bigendian) |
| 362 | { |
| 363 | CHEATF *next = &cheats[which]; |
| 364 | |
| 365 | if(name) |
| 366 | *name=next->name; |
| 367 | if(a) |
| 368 | *a=next->addr; |
| 369 | if(v) |
| 370 | *v=next->val; |
| 371 | if(s) |
| 372 | *s=next->status; |
| 373 | if(compare) |
| 374 | *compare=next->compare; |
| 375 | if(type) |
| 376 | *type=next->type; |
| 377 | if(length) |
| 378 | *length = next->length; |
| 379 | if(bigendian) |
| 380 | *bigendian = next->bigendian; |
| 381 | return(1); |
| 382 | } |
| 383 | |
| 384 | int MDFNI_DecodePAR(const char *str, uint32 *a, uint8 *v, uint8 *c, char *type) |
| 385 | { |
| 386 | int boo[4]; |
| 387 | if(strlen(str)!=8) return(0); |
| 388 | |
| 389 | sscanf(str,"%02x%02x%02x%02x",boo,boo+1,boo+2,boo+3); |
| 390 | |
| 391 | *c = 0; |
| 392 | |
| 393 | if(1) |
| 394 | { |
| 395 | *a=(boo[3]<<8)|(boo[2]+0x7F); |
| 396 | *v=0; |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | *v=boo[3]; |
| 401 | *a=boo[2]|(boo[1]<<8); |
| 402 | } |
| 403 | |
| 404 | *type = 'S'; |
| 405 | return(1); |
| 406 | } |
| 407 | |
| 408 | /* name can be NULL if the name isn't going to be changed. */ |
| 409 | int MDFNI_SetCheat(uint32 which, const char *name, uint32 a, uint64 v, uint64 compare, int s, char type, unsigned int length, bool bigendian) |
| 410 | { |
| 411 | CHEATF *next = &cheats[which]; |
| 412 | |
| 413 | if(name) |
| 414 | { |
| 415 | char *t; |
| 416 | |
| 417 | if(!(t=(char *)realloc(next->name,strlen(name)+1))) |
| 418 | return(0); |
| 419 | next->name=t; |
| 420 | strcpy(next->name,name); |
| 421 | } |
| 422 | next->addr=a; |
| 423 | next->val=v; |
| 424 | next->status=s; |
| 425 | next->compare=compare; |
| 426 | next->type=type; |
| 427 | next->length = length; |
| 428 | next->bigendian = bigendian; |
| 429 | |
| 430 | RebuildSubCheats(); |
| 431 | |
| 432 | return(1); |
| 433 | } |
| 434 | |
| 435 | /* Convenience function. */ |
| 436 | int MDFNI_ToggleCheat(uint32 which) |
| 437 | { |
| 438 | cheats[which].status = !cheats[which].status; |
| 439 | RebuildSubCheats(); |
| 440 | |
| 441 | return(cheats[which].status); |
| 442 | } |
| 443 | |
| 444 | static void SettingChanged(const char *name) |
| 445 | { |
| 446 | MDFNMP_RemoveReadPatches(); |
| 447 | |
| 448 | CheatsActive = MDFN_GetSettingB("cheats"); |
| 449 | |
| 450 | RebuildSubCheats(); |
| 451 | |
| 452 | MDFNMP_InstallReadPatches(); |
| 453 | } |