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