remove some dead code
[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   printf("CHEAT: code[8]==%c\n",code[8]);
177   if (code[7]=='-'){
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
358
359 unsigned int PicoRead16(unsigned int a);
360 void PicoWrite16(unsigned int a, unsigned short d);
361 extern unsigned short m68k_read16(unsigned int a);
362 extern void m68k_write16(unsigned int a, unsigned short d);
363 extern char PicoRead8_z80(unsigned short a);
364 extern void PicoWrite8_z80(unsigned short a, char d);
365
366 void PicoPatchUnload(void)
367 {
368    if (PicoPatches != NULL)
369    {
370       free(PicoPatches);
371       PicoPatches = NULL;
372    }
373    PicoPatchCount = 0;
374 }
375
376 int PicoPatchLoad(const char *fname)
377 {
378    FILE *f;
379    char buff[256];
380    struct patch pt;
381    int array_len = 0;
382
383    PicoPatchUnload();
384
385    f = fopen(fname, "r");
386    if (f == NULL)
387    {
388       return -1;
389    }
390
391    while (fgets(buff, sizeof(buff), f))
392    {
393       int llen, clen;
394
395       llen = strlen(buff);
396       for (clen = 0; clen < llen; clen++)
397          if (isspace_(buff[clen]))
398             break;
399       buff[clen] = 0;
400
401       if (clen > 11 || clen < 8)
402          continue;
403
404       decode(buff, &pt);
405       if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
406          continue;
407
408       /* code was good, add it */
409       if (array_len < PicoPatchCount + 1)
410       {
411          void *ptr;
412          array_len *= 2;
413          array_len++;
414          ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
415          if (ptr == NULL) break;
416          PicoPatches = ptr;
417       }
418       strcpy(PicoPatches[PicoPatchCount].code, buff);
419       /* strip */
420       for (clen++; clen < llen; clen++)
421          if (!isspace_(buff[clen]))
422             break;
423       for (llen--; llen > 0; llen--)
424          if (!isspace_(buff[llen]))
425             break;
426       buff[llen+1] = 0;
427       strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
428       PicoPatches[PicoPatchCount].name[51] = 0;
429       PicoPatches[PicoPatchCount].active = 0;
430       PicoPatches[PicoPatchCount].addr = pt.addr;
431       PicoPatches[PicoPatchCount].data = pt.data;
432       PicoPatches[PicoPatchCount].data_old = 0;
433       PicoPatchCount++;
434       // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
435       // PicoPatches[PicoPatchCount-1].name);
436    }
437    fclose(f);
438
439    return 0;
440 }
441
442 /* to be called when the Rom is loaded and byteswapped */
443 void PicoPatchPrepare(void)
444 {
445    int i;
446    int addr;
447
448    for (i = 0; i < PicoPatchCount; i++)
449    {
450       addr=PicoPatches[i].addr;
451       addr &= ~1;
452       if (addr < Pico.romsize)
453          PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
454       else
455       {
456          if(!(PicoAHW & PAHW_SMS))
457             PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
458          else
459             PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
460       }
461       if (strstr(PicoPatches[i].name, "AUTO"))
462          PicoPatches[i].active = 1;
463    }
464 }
465
466 void PicoPatchApply(void)
467 {
468    int i, u;
469    unsigned int addr;
470
471    for (i = 0; i < PicoPatchCount; i++)
472    {
473       addr = PicoPatches[i].addr;
474
475       if (addr < Pico.romsize)
476       {
477          if (PicoPatches[i].active)
478          {
479             if (!(PicoAHW & PAHW_SMS))
480                *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
481             else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
482                *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
483          }
484          else
485          {
486             // if current addr is not patched by older patch, write back original val
487             for (u = 0; u < i; u++)
488                if (PicoPatches[u].addr == addr) break;
489             if (u == i)
490             {
491                if (!(PicoAHW & PAHW_SMS))
492                   *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
493                else
494                   *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
495             }
496          }
497       // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
498       // *(unsigned short *)(Pico.rom + addr));
499       }
500       else
501       {
502          if (PicoPatches[i].active)
503          {
504             if (!(PicoAHW & PAHW_SMS))
505               m68k_write16(addr,PicoPatches[i].data);
506             else
507               PicoWrite8_z80(addr,PicoPatches[i].data);
508          }
509          else
510          {
511             // if current addr is not patched by older patch, write back original val
512             for (u = 0; u < i; u++)
513                if (PicoPatches[u].addr == addr) break;
514             if (u == i)
515             {
516                if (!(PicoAHW & PAHW_SMS))
517                   m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
518               else
519                 PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
520             }
521          }
522       }
523    }
524 }
525