Restore support for short GG cheats.
[picodrive.git] / pico / patch.c
1 /* Decode a Game Genie code into an M68000 address/data pair.
2  * The Game Genie code is made of the characters
3  * ABCDEFGHJKLMNPRSTVWXYZ0123456789 (notice the missing I, O, Q and U).
4  * Where A = 00000, B = 00001, C = 00010, ... , on to 9 = 11111.
5  *
6  * These come out to a very scrambled bit pattern like this:
7  * (SCRA-MBLE is just an example)
8  *
9  *   S     C     R     A  -  M     B     L     E
10  * 01111 00010 01110 00000 01011 00001 01010 00100
11  * ijklm nopIJ KLMNO PABCD EFGHd efgha bcQRS TUVWX
12  *
13  * Our goal is to rearrange that to this:
14  *
15  * 0000 0101 1001 1100 0100 0100 : 1011 0000 0111 1000
16  * ABCD EFGH IJKL MNOP QRST UVWX : abcd efgh ijkl mnop
17  *
18  * which in Hexadecimal is 059C44:B078. Simple, huh? ;)
19  *
20  * So, then, we dutifully change memory location 059C44 to B078!
21  * (of course, that's handled by a different source file :)
22  */
23
24 #include "pico_int.h"
25 #include "memory.h"
26 #include "patch.h"
27
28 struct patch
29 {
30         unsigned int addr;
31         unsigned short data;
32         unsigned char comp;
33 };
34
35 struct patch_inst *PicoPatches = NULL;
36 int PicoPatchCount = 0;
37
38 static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
39
40 /* genie_decode
41  * This function converts a Game Genie code to an address:data pair.
42  * The code is given as an 8-character string, like "BJX0SA1C". It need not
43  * be null terminated, since only the first 8 characters are taken. It is
44  * assumed that the code is already made of valid characters, i.e. there are no
45  * Q's, U's, or symbols. If such a character is
46  * encountered, the function will return with a warning on stderr.
47  *
48  * The resulting address:data pair is returned in the struct patch pointed to
49  * by result. If an error results, both the address and data will be set to -1.
50  */
51
52 static void genie_decode_md(const char* code, struct patch* result)
53 {
54   int i = 0, n;
55   char* x;
56
57   for(; i < 9; ++i)
58   {
59     /* Skip i=4; it's going to be the separating hyphen */
60     if (i==4) continue;
61
62     /* If strchr returns NULL, we were given a bad character */
63     if(!(x = strchr(genie_chars_md, code[i])))
64     {
65       result->addr = -1; result->data = -1;
66       return;
67     }
68     n = (x - genie_chars_md) >> 1;
69     /* Now, based on which character this is, fit it into the result */
70     switch(i)
71     {
72     case 0:
73       /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
74       result->data |= n << 3;
75       break;
76     case 1:
77       /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
78       result->data |= n >> 2;
79       result->addr |= (n & 3) << 14;
80       break;
81     case 2:
82       /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
83       result->addr |= n << 9;
84       break;
85     case 3:
86       /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
87       result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
88       break;
89     case 5:
90       /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
91       result->data |= (n & 1) << 12;
92       result->addr |= (n >> 1) << 16;
93       break;
94     case 6:
95       /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
96       result->data |= (n & 1) << 15 | (n >> 1) << 8;
97       break;
98     case 7:
99       /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
100       result->data |= (n >> 3) << 13;
101       result->addr |= (n & 7) << 5;
102       break;
103     case 8:
104       /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
105       result->addr |= n;
106       break;
107     }
108     /* Go around again */
109   }
110   return;
111 }
112
113 /* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
114  * type codes. You're more likely to find Genie codes circulating around, but
115  * there's a chance you could come on to one of these. Which is nice, since
116  * they're MUCH easier to implement ;) Once again, the input should be depunc-
117  * tuated already. */
118
119 static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
120
121 static void hex_decode_md(const char *code, struct patch *result)
122 {
123   char *x;
124   int i;
125   /* 6 digits for address */
126   for(i = 0; i < 6; ++i)
127   {
128     if(!(x = strchr(hex_chars, code[i])))
129     {
130       result->addr = result->data = -1;
131       return;
132     }
133     result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
134   }
135   /* 4 digits for data */
136   for(i = 7; i < 11; ++i)
137   {
138     if(!(x = strchr(hex_chars, code[i])))
139     {
140       if (i==8) break;
141       result->addr = result->data = -1;
142       return;
143     }
144     result->data = (result->data << 4) | ((x - hex_chars) >> 1);
145   }
146 }
147
148 void genie_decode_ms(const char *code, struct patch *result)
149 {
150   char *x;
151   int i;
152   /* 2 digits for data */
153   for(i=0;i<2;++i)
154   {
155     if(!(x = strchr(hex_chars, code[i])))
156     {
157       result->addr = result->data = -1;
158       return;
159     }
160     result->data = (result->data << 4) | ((x - hex_chars) >> 1);
161   }
162   /* 4 digits for address */
163   for(i=2;i<7;++i)
164   {
165     /* 4th character is hyphen and can be skipped*/
166     if (i==3) continue;
167     if(!(x = strchr(hex_chars, code[i])))
168     {
169       result->addr = result->data = -1;
170       return;
171     }
172     result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
173   }
174   /* Correct the address */
175   result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
176   /* Optional: 3 digits for comp */
177   if (code[8]=='-'){
178     for(i=8;i<11;++i)
179     {
180       if (i==9) continue; /* 2nd character is ignored */
181       if(!(x = strchr(hex_chars, code[i])))
182       {
183         result->addr = result->data = -1;
184         return;
185       }
186       result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
187     }
188     /* Correct the comp */
189     result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
190   }
191 }
192
193 void ar_decode_ms(const char *code, struct patch *result){
194   char *x;
195   int i;
196   /* 2 digits of padding*/
197   /* 4 digits for address */
198   for(i=2;i<7;++i)
199   {
200     /* 5th character is hyphen and can be skipped*/
201     if (i==4) continue;
202     if(!(x = strchr(hex_chars, code[i])))
203     {
204       result->addr = result->data = -1;
205       return;
206     }
207     result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
208   }
209   /* 2 digits for data */
210   for(i=7;i<9;++i)
211   {
212     if(!(x = strchr(hex_chars, code[i])))
213     {
214       result->addr = result->data = -1;
215       return;
216     }
217     result->data = (result->data << 4) | ((x - hex_chars) >> 1);
218   }
219 }
220
221 void fusion_ram_decode(const char *code, struct patch *result){
222   char *x;
223   int i;
224   /* 4 digits for address */
225   for(i=0;i<4;++i)
226   {
227     if(!(x = strchr(hex_chars, code[i])))
228     {
229       result->addr = result->data = -1;
230       return;
231     }
232     result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
233   }
234   /* Skip the ':' */
235   /* 2 digits for data */
236   for(i=5;i<7;++i)
237   {
238     if(!(x = strchr(hex_chars, code[i])))
239     {
240       result->addr = result->data = -1;
241       return;
242     }
243     result->data = (result->data << 4) | ((x - hex_chars) >> 1);
244   }
245 }
246
247 void fusion_rom_decode(const char *code, struct patch *result){
248   char *x;
249   int i;
250   /* 2 digits for comp */
251   for(i=0;i<2;++i)
252   {
253     if(!(x = strchr(hex_chars, code[i])))
254     {
255       result->addr = result->data = -1;
256       return;
257     }
258     result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
259   }
260   /* 4 digits for address */
261   for(i=2;i<6;++i)
262   {
263     if(!(x = strchr(hex_chars, code[i])))
264     {
265       result->addr = result->data = -1;
266       return;
267     }
268     result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
269   }
270   /* 2 digits for data */
271   for(i=7;i<9;++i)
272   {
273     if(!(x = strchr(hex_chars, code[i])))
274     {
275       result->addr = result->data = -1;
276       return;
277     }
278     result->data = (result->data << 4) | ((x - hex_chars) >> 1);
279   }
280 }
281
282 /* THIS is the function you call from the MegaDrive or whatever. This figures
283  * out whether it's a genie or hex code, depunctuates it, and calls the proper
284  * decoder. */
285 void decode(const char* code, struct patch* result)
286 {
287   int len = strlen(code);
288
289   /* Initialize the result */
290   result->addr = result->data = result->comp = 0;
291
292   if(!(PicoAHW & PAHW_SMS))
293   {
294     //If Genesis
295
296     //Game Genie
297     if(len == 9 && code[4] == '-')
298     {
299       genie_decode_md(code, result);
300       return;
301     }
302
303     //Master
304     else if(len >=9 && code[6] == ':')
305     {
306       hex_decode_md(code, result);
307     }
308
309     else
310     {
311       goto bad_code;
312     }
313   } else {
314     //If Master System
315
316     //Genie
317     if(len >= 7 && code[3] == '-')
318     {
319       genie_decode_ms(code, result);
320     }
321
322     //AR
323     else if(len == 9 && code[4] == '-')
324     {
325       ar_decode_ms(code, result);
326     }
327
328     //Fusion RAM
329     else if(len == 7 && code[4] == ':')
330     {
331       fusion_ram_decode(code, result);
332     }
333
334     //Fusion ROM
335     else if(len == 9 && code[6] == ':')
336     {
337       fusion_rom_decode(code, result);
338     }
339
340     else
341     {
342       goto bad_code;
343     }
344
345     //Convert RAM address space to Genesis location.
346     if (result->addr>=0xC000)
347       result->addr= 0xFF0000 | (0x1FFF & result->addr);
348   }
349
350   return;
351
352   bad_code:
353   result->data = result->addr = -1;
354   return;
355 }
356
357 void PicoPatchUnload(void)
358 {
359         if (PicoPatches != NULL)
360         {
361                 free(PicoPatches);
362                 PicoPatches = NULL;
363         }
364         PicoPatchCount = 0;
365 }
366
367 int PicoPatchLoad(const char *fname)
368 {
369         FILE *f;
370         char buff[256];
371         struct patch pt;
372         int array_len = 0;
373
374         PicoPatchUnload();
375
376         f = fopen(fname, "r");
377         if (f == NULL)
378         {
379                 return -1;
380         }
381
382         while (fgets(buff, sizeof(buff), f))
383         {
384                 int llen, clen;
385
386                 llen = strlen(buff);
387                 for (clen = 0; clen < llen; clen++)
388                         if (isspace_(buff[clen]))
389                                 break;
390                 buff[clen] = 0;
391
392                 if (clen > 11 || clen < 8)
393                         continue;
394
395                 decode(buff, &pt);
396                 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
397                         continue;
398
399                 /* code was good, add it */
400                 if (array_len < PicoPatchCount + 1)
401                 {
402                         void *ptr;
403                         array_len *= 2;
404                         array_len++;
405                         ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
406                         if (ptr == NULL) break;
407                         PicoPatches = ptr;
408                 }
409                 strcpy(PicoPatches[PicoPatchCount].code, buff);
410                 /* strip */
411                 for (clen++; clen < llen; clen++)
412                         if (!isspace_(buff[clen]))
413                                 break;
414                 for (llen--; llen > 0; llen--)
415                         if (!isspace_(buff[llen]))
416                                 break;
417                 buff[llen+1] = 0;
418                 strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
419                 PicoPatches[PicoPatchCount].name[51] = 0;
420                 PicoPatches[PicoPatchCount].active = 0;
421                 PicoPatches[PicoPatchCount].addr = pt.addr;
422                 PicoPatches[PicoPatchCount].data = pt.data;
423                 PicoPatches[PicoPatchCount].data_old = 0;
424                 PicoPatchCount++;
425                 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
426                 //      PicoPatches[PicoPatchCount-1].name);
427         }
428         fclose(f);
429
430         return 0;
431 }
432
433 /* to be called when the Rom is loaded and byteswapped */
434 void PicoPatchPrepare(void)
435 {
436   int i;
437   int addr;
438
439   for (i = 0; i < PicoPatchCount; i++)
440   {
441     addr=PicoPatches[i].addr;
442     addr &= ~1;
443     if (addr < Pico.romsize)
444       PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
445     else
446     {
447       if(!(PicoAHW & PAHW_SMS))
448         PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
449       else
450         ; // wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
451     }
452     if (strstr(PicoPatches[i].name, "AUTO"))
453       PicoPatches[i].active = 1;
454   }
455 }
456
457 void PicoPatchApply(void)
458 {
459   int i, u;
460   unsigned int addr;
461
462   for (i = 0; i < PicoPatchCount; i++)
463   {
464     addr = PicoPatches[i].addr;
465
466     if (addr < Pico.romsize)
467     {
468       if (PicoPatches[i].active)
469       {
470         if (!(PicoAHW & PAHW_SMS))
471           *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
472         else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
473           *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
474       }
475       else
476       {
477         // if current addr is not patched by older patch, write back original val
478         for (u = 0; u < i; u++)
479           if (PicoPatches[u].addr == addr) break;
480         if (u == i)
481         {
482           if (!(PicoAHW & PAHW_SMS))
483             *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
484           else
485             *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
486         }
487       }
488       // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
489       //        *(unsigned short *)(Pico.rom + addr));
490     }
491     else
492     {
493       if (PicoPatches[i].active)
494       {
495         if (!(PicoAHW & PAHW_SMS))
496           m68k_write16(addr,PicoPatches[i].data);
497         else
498           ;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
499       }
500       else
501       {
502         // if current addr is not patched by older patch, write back original val
503         for (u = 0; u < i; u++)
504           if (PicoPatches[u].addr == addr) break;
505         if (u == i)
506         {
507           if (!(PicoAHW & PAHW_SMS))
508             m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
509           else
510             ;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
511         }
512       }
513     }
514   }
515 }
516