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