Debian Codegraph @

mednafen/mempatcher.cpp

raw symbols SCIP
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 "mednafen.h"
19
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <vector>
24
25 #include <boolean.h>
26
27 #include "general.h"
28 #include "mempatcher.h"
29
30 #ifdef _WIN32
31 #include "msvc_compat.h"
32 #endif
33
34 static uint8 **RAMPtrs = NULL;
35 static uint32 PageSize;
36 static uint32 NumPages;
37
38 typedef struct __CHEATF
39 {
40 char *name;
41 char *conditions;
42
43 uint32 addr;
44 uint64 val;
45 uint64 compare;
46
47 unsigned int length;
48 bool bigendian;
49 unsigned int icount; // Instance count
50 char type; /* 'R' for replace, 'S' for substitute(GG), 'C' for substitute with compare */
51 int status;
52 } CHEATF;
53
54 static std::vector<CHEATF> cheats;
55 static int savecheats;
56 static uint32 resultsbytelen = 1;
57 static bool resultsbigendian = 0;
58 static bool CheatsActive = true;
59
60 bool SubCheatsOn = 0;
61 std::vector<SUBCHEAT> SubCheats[8];
62
63 static void RebuildSubCheats(void)
64 {
65 std::vector<CHEATF>::iterator chit;
66
67 SubCheatsOn = 0;
68 for(int x = 0; x < 8; x++)
69 SubCheats[x].clear();
70
71 if(!CheatsActive) return;
72
73 for(chit = cheats.begin(); chit != cheats.end(); chit++)
74 {
75 if(chit->status && chit->type != 'R')
76 {
77 for(unsigned int x = 0; x < chit->length; x++)
78 {
79 SUBCHEAT tmpsub;
80 unsigned int shiftie;
81
82 if(chit->bigendian)
83 shiftie = (chit->length - 1 - x) * 8;
84 else
85 shiftie = x * 8;
86
87 tmpsub.addr = chit->addr + x;
88 tmpsub.value = (chit->val >> shiftie) & 0xFF;
89 if(chit->type == 'C')
90 tmpsub.compare = (chit->compare >> shiftie) & 0xFF;
91 else
92 tmpsub.compare = -1;
93 SubCheats[(chit->addr + x) & 0x7].push_back(tmpsub);
94 SubCheatsOn = 1;
95 }
96 }
97 }
98 }
99
100 bool MDFNMP_Init(uint32 ps, uint32 numpages)
101 {
102 PageSize = ps;
103 NumPages = numpages;
104
105 RAMPtrs = (uint8 **)calloc(numpages, sizeof(uint8 *));
106
107 CheatsActive = MDFN_GetSettingB("cheats");
108 return(1);
109 }
110
111 void MDFNMP_Kill(void)
112 {
113 if(RAMPtrs)
114 {
115 free(RAMPtrs);
116 RAMPtrs = NULL;
117 }
118 }
119
120
121 void MDFNMP_AddRAM(uint32 size, uint32 A, uint8 *RAM)
122 {
123 uint32 AB = A / PageSize;
124
125 size /= PageSize;
126
127 for(unsigned int x = 0; x < size; x++)
128 {
129 RAMPtrs[AB + x] = RAM;
130 if(RAM) // Don't increment the RAM pointer if we're passed a NULL pointer
131 RAM += PageSize;
132 }
133 }
134
135 void MDFNMP_InstallReadPatches(void)
136 {
137 if(!CheatsActive) return;
138
139 std::vector<SUBCHEAT>::iterator chit;
140
141 for(unsigned int x = 0; x < 8; x++)
142 for(chit = SubCheats[x].begin(); chit != SubCheats[x].end(); chit++)
143 {
144 if(MDFNGameInfo->InstallReadPatch)
145 MDFNGameInfo->InstallReadPatch(chit->addr);
146 }
147 }
148
149 void MDFNMP_RemoveReadPatches(void)
150 {
151 if(MDFNGameInfo->RemoveReadPatches)
152 MDFNGameInfo->RemoveReadPatches();
153 }
154
155 /* This function doesn't allocate any memory for "name" */
156 static int AddCheatEntry(char *name, char *conditions, uint32 addr, uint64 val, uint64 compare, int status, char type, unsigned int length, bool bigendian)
157 {
158 CHEATF temp;
159
160 memset(&temp, 0, sizeof(CHEATF));
161
162 temp.name=name;
163 temp.conditions = conditions;
164 temp.addr=addr;
165 temp.val=val;
166 temp.status=status;
167 temp.compare=compare;
168 temp.length = length;
169 temp.bigendian = bigendian;
170 temp.type=type;
171
172 cheats.push_back(temp);
173 return(1);
174 }
175
176 void MDFN_LoadGameCheats(void *override_ptr)
177 {
178 RebuildSubCheats();
179 }
180
181 void MDFN_FlushGameCheats(int nosave)
182 {
183 std::vector<CHEATF>::iterator chit;
184
185 for(chit = cheats.begin(); chit != cheats.end(); chit++)
186 {
187 free(chit->name);
188 if(chit->conditions)
189 free(chit->conditions);
190 }
191 cheats.clear();
192
193 RebuildSubCheats();
194 }
195
196 int MDFNI_AddCheat(const char *name, uint32 addr, uint64 val, uint64 compare, char type, unsigned int length, bool bigendian)
197 {
198 char *t;
199
200 if(!(t = strdup(name)))
201 {
202 /* Error allocating memory for cheat data. */
203 return(0);
204 }
205
206 if(!AddCheatEntry(t, NULL, addr,val,compare,1,type, length, bigendian))
207 {
208 free(t);
209 return(0);
210 }
211
212 savecheats = 1;
213
214 MDFNMP_RemoveReadPatches();
215 RebuildSubCheats();
216 MDFNMP_InstallReadPatches();
217
218 return(1);
219 }
220
221 int MDFNI_DelCheat(uint32 which)
222 {
223 free(cheats[which].name);
224 cheats.erase(cheats.begin() + which);
225
226 savecheats=1;
227
228 MDFNMP_RemoveReadPatches();
229 RebuildSubCheats();
230 MDFNMP_InstallReadPatches();
231
232 return(1);
233 }
234
235 /*
236 Condition format(ws = white space):
237
238 <variable size><ws><endian><ws><address><ws><operation><ws><value>
239 [,second condition...etc.]
240
241 Value should be unsigned integer, hex(with a 0x prefix) or
242 base-10.
243
244 Operations:
245 >=
246 <=
247 >
248 <
249 ==
250 !=
251 & // Result of AND between two values is nonzero
252 !& // Result of AND between two values is zero
253 ^ // same, XOR
254 !^
255 | // same, OR
256 !|
257
258 Full example:
259
260 2 L 0xADDE == 0xDEAD, 1 L 0xC000 == 0xA0
261
262 */
263
264 static bool TestConditions(const char *string)
265 {
266 char address[64];
267 char operation[64];
268 char value[64];
269 char endian;
270 unsigned int bytelen;
271 bool passed = 1;
272
273 //printf("TR: %s\n", string);
274 while(sscanf(string, "%u %c %63s %63s %63s", &bytelen, &endian, address, operation, value) == 5 && passed)
275 {
276 uint32 v_address;
277 uint64 v_value;
278 uint64 value_at_address;
279
280 if(address[0] == '0' && address[1] == 'x')
281 v_address = strtoul(address + 2, NULL, 16);
282 else
283 v_address = strtoul(address, NULL, 10);
284
285 if(value[0] == '0' && value[1] == 'x')
286 v_value = strtoull(value + 2, NULL, 16);
287 else
288 v_value = strtoull(value, NULL, 0);
289
290 value_at_address = 0;
291 for(unsigned int x = 0; x < bytelen; x++)
292 {
293 unsigned int shiftie;
294
295 if(endian == 'B')
296 shiftie = (bytelen - 1 - x) * 8;
297 else
298 shiftie = x * 8;
299 value_at_address |= MDFNGameInfo->MemRead(v_address + x) << shiftie;
300 }
301
302 //printf("A: %08x, V: %08llx, VA: %08llx, OP: %s\n", v_address, v_value, value_at_address, operation);
303 if(!strcmp(operation, ">="))
304 {
305 if(!(value_at_address >= v_value))
306 passed = 0;
307 }
308 else if(!strcmp(operation, "<="))
309 {
310 if(!(value_at_address <= v_value))
311 passed = 0;
312 }
313 else if(!strcmp(operation, ">"))
314 {
315 if(!(value_at_address > v_value))
316 passed = 0;
317 }
318 else if(!strcmp(operation, "<"))
319 {
320 if(!(value_at_address < v_value))
321 passed = 0;
322 }
323 else if(!strcmp(operation, "=="))
324 {
325 if(!(value_at_address == v_value))
326 passed = 0;
327 }
328 else if(!strcmp(operation, "!="))
329 {
330 if(!(value_at_address != v_value))
331 passed = 0;
332 }
333 else if(!strcmp(operation, "&"))
334 {
335 if(!(value_at_address & v_value))
336 passed = 0;
337 }
338 else if(!strcmp(operation, "!&"))
339 {
340 if(value_at_address & v_value)
341 passed = 0;
342 }
343 else if(!strcmp(operation, "^"))
344 {
345 if(!(value_at_address ^ v_value))
346 passed = 0;
347 }
348 else if(!strcmp(operation, "!^"))
349 {
350 if(value_at_address ^ v_value)
351 passed = 0;
352 }
353 else if(!strcmp(operation, "|"))
354 {
355 if(!(value_at_address | v_value))
356 passed = 0;
357 }
358 else if(!strcmp(operation, "!|"))
359 {
360 if(value_at_address | v_value)
361 passed = 0;
362 }
363 else
364 puts("Invalid operation");
365 string = strchr(string, ',');
366 if(string == NULL)
367 break;
368 else
369 string++;
370 //printf("Foo: %s\n", string);
371 }
372
373 return(passed);
374 }
375
376 void MDFNMP_ApplyPeriodicCheats(void)
377 {
378 std::vector<CHEATF>::iterator chit;
379
380
381 if(!CheatsActive)
382 return;
383
384 //TestConditions("2 L 0x1F00F5 == 0xDEAD");
385 //if(TestConditions("1 L 0x1F0058 > 0")) //, 1 L 0xC000 == 0x01"));
386 for(chit = cheats.begin(); chit != cheats.end(); chit++)
387 {
388 if(chit->status && chit->type == 'R')
389 {
390 if(!chit->conditions || TestConditions(chit->conditions))
391 for(unsigned int x = 0; x < chit->length; x++)
392 {
393 uint32 page = ((chit->addr + x) / PageSize) % NumPages;
394 if(RAMPtrs[page])
395 {
396 uint64 tmpval = chit->val;
397
398 if(chit->bigendian)
399 tmpval >>= (chit->length - 1 - x) * 8;
400 else
401 tmpval >>= x * 8;
402
403 RAMPtrs[page][(chit->addr + x) % PageSize] = tmpval;
404 }
405 }
406 }
407 }
408 }
409
410
411 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)
412 {
413 std::vector<CHEATF>::iterator chit;
414
415 for(chit = cheats.begin(); chit != cheats.end(); chit++)
416 {
417 if(!callb(chit->name, chit->addr, chit->val, chit->compare, chit->status, chit->type, chit->length, chit->bigendian, data)) break;
418 }
419 }
420
421 int MDFNI_GetCheat(uint32 which, char **name, uint32 *a, uint64 *v, uint64 *compare, int *s, char *type, unsigned int *length, bool *bigendian)
422 {
423 CHEATF *next = &cheats[which];
424
425 if(name)
426 *name=next->name;
427 if(a)
428 *a=next->addr;
429 if(v)
430 *v=next->val;
431 if(s)
432 *s=next->status;
433 if(compare)
434 *compare=next->compare;
435 if(type)
436 *type=next->type;
437 if(length)
438 *length = next->length;
439 if(bigendian)
440 *bigendian = next->bigendian;
441 return(1);
442 }
443
444 static uint8 CharToNibble(char thechar)
445 {
446 const char lut[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
447
448 thechar = toupper(thechar);
449
450 for(int x = 0; x < 16; x++)
451 if(lut[x] == thechar)
452 return(x);
453
454 return(0xFF);
455 }
456
457 bool MDFNI_DecodeGBGG(const char *instr, uint32 *a, uint8 *v, uint8 *c, char *type)
458 {
459 char str[10];
460 int len;
461
462 for(int x = 0; x < 9; x++)
463 {
464 while(*instr && CharToNibble(*instr) == 255)
465 instr++;
466 if(!(str[x] = *instr)) break;
467 instr++;
468 }
469 str[9] = 0;
470
471 len = strlen(str);
472
473 if(len != 9 && len != 6)
474 return(0);
475
476 uint32 tmp_address;
477 uint8 tmp_value;
478 uint8 tmp_compare = 0;
479
480 tmp_address = (CharToNibble(str[5]) << 12) | (CharToNibble(str[2]) << 8) | (CharToNibble(str[3]) << 4) | (CharToNibble(str[4]) << 0);
481 tmp_address ^= 0xF000;
482 tmp_value = (CharToNibble(str[0]) << 4) | (CharToNibble(str[1]) << 0);
483
484 if(len == 9)
485 {
486 tmp_compare = (CharToNibble(str[6]) << 4) | (CharToNibble(str[8]) << 0);
487 tmp_compare = (tmp_compare >> 2) | ((tmp_compare << 6) & 0xC0);
488 tmp_compare ^= 0xBA;
489 }
490
491 *a = tmp_address;
492 *v = tmp_value;
493
494 if(len == 9)
495 {
496 *c = tmp_compare;
497 *type = 'C';
498 }
499 else
500 {
501 *c = 0;
502 *type = 'S';
503 }
504
505 return(1);
506 }
507
508 static int GGtobin(char c)
509 {
510 static char lets[16]={'A','P','Z','L','G','I','T','Y','E','O','X','U','K','S','V','N'};
511 int x;
512
513 for(x=0;x<16;x++)
514 if(lets[x] == toupper(c)) return(x);
515 return(0);
516 }
517
518 /* Returns 1 on success, 0 on failure. Sets *a,*v,*c. */
519 int MDFNI_DecodeGG(const char *str, uint32 *a, uint8 *v, uint8 *c, char *type)
520 {
521 uint16 A;
522 uint8 V,C;
523 uint8 t;
524 int s;
525
526 A=0x8000;
527 V=0;
528 C=0;
529
530 s=strlen(str);
531 if(s!=6 && s!=8) return(0);
532
533 t=GGtobin(*str++);
534 V|=(t&0x07);
535 V|=(t&0x08)<<4;
536
537 t=GGtobin(*str++);
538 V|=(t&0x07)<<4;
539 A|=(t&0x08)<<4;
540
541 t=GGtobin(*str++);
542 A|=(t&0x07)<<4;
543 //if(t&0x08) return(0); /* 8-character code?! */
544
545 t=GGtobin(*str++);
546 A|=(t&0x07)<<12;
547 A|=(t&0x08);
548
549 t=GGtobin(*str++);
550 A|=(t&0x07);
551 A|=(t&0x08)<<8;
552
553 if(s==6)
554 {
555 t=GGtobin(*str++);
556 A|=(t&0x07)<<8;
557 V|=(t&0x08);
558
559 *a=A;
560 *v=V;
561 *type = 'S';
562 *c = 0;
563 }
564 else
565 {
566 t=GGtobin(*str++);
567 A|=(t&0x07)<<8;
568 C|=(t&0x08);
569
570 t=GGtobin(*str++);
571 C|=(t&0x07);
572 C|=(t&0x08)<<4;
573
574 t=GGtobin(*str++);
575 C|=(t&0x07)<<4;
576 V|=(t&0x08);
577 *a=A;
578 *v=V;
579 *c=C;
580 *type = 'C';
581 }
582
583 return(1);
584 }
585
586 int MDFNI_DecodePAR(const char *str, uint32 *a, uint8 *v, uint8 *c, char *type)
587 {
588 int boo[4];
589 if(strlen(str)!=8) return(0);
590
591 sscanf(str,"%02x%02x%02x%02x",boo,boo+1,boo+2,boo+3);
592
593 *c = 0;
594
595 if(1)
596 {
597 *a=(boo[3]<<8)|(boo[2]+0x7F);
598 *v=0;
599 }
600 else
601 {
602 *v=boo[3];
603 *a=boo[2]|(boo[1]<<8);
604 }
605
606 *type = 'S';
607 return(1);
608 }
609
610 /* name can be NULL if the name isn't going to be changed. */
611 int MDFNI_SetCheat(uint32 which, const char *name, uint32 a, uint64 v, uint64 compare, int s, char type, unsigned int length, bool bigendian)
612 {
613 CHEATF *next = &cheats[which];
614
615 if(name)
616 {
617 char *t;
618
619 if((t=(char *)realloc(next->name,strlen(name+1))))
620 {
621 next->name=t;
622 strcpy(next->name,name);
623 }
624 else
625 return(0);
626 }
627 next->addr=a;
628 next->val=v;
629 next->status=s;
630 next->compare=compare;
631 next->type=type;
632 next->length = length;
633 next->bigendian = bigendian;
634
635 RebuildSubCheats();
636 savecheats=1;
637
638 return(1);
639 }
640
641 /* Convenience function. */
642 int MDFNI_ToggleCheat(uint32 which)
643 {
644 cheats[which].status = !cheats[which].status;
645 savecheats = 1;
646 RebuildSubCheats();
647
648 return(cheats[which].status);
649 }
650
651 static void SettingChanged(const char *name)
652 {
653 MDFNMP_RemoveReadPatches();
654
655 CheatsActive = MDFN_GetSettingB("cheats");
656
657 RebuildSubCheats();
658
659 MDFNMP_InstallReadPatches();
660 }
661
662
663 MDFNSetting MDFNMP_Settings[] =
664 {
665 { "cheats", MDFNSF_NOFLAGS, "Enable cheats.", NULL, MDFNST_BOOL, "1", NULL, NULL, NULL, SettingChanged },
666 { NULL}
667 };