Add support for single-line cheats and PAR codes.
[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 };
33
34 struct patch_inst *PicoPatches = NULL;
35 int PicoPatchCount = 0;
36
37 static char genie_chars[] = "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(const char* code, struct patch* result)
52 {
53   int i = 0, n;
54   char* x;
55
56   for(; i < 8; ++i)
57   {
58     /* If strchr returns NULL, we were given a bad character */
59     if(!(x = strchr(genie_chars, code[i])))
60     {
61       result->addr = -1; result->data = -1;
62       return;
63     }
64     n = (x - genie_chars) >> 1;
65     /* Now, based on which character this is, fit it into the result */
66     switch(i)
67     {
68     case 0:
69       /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
70       result->data |= n << 3;
71       break;
72     case 1:
73       /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
74       result->data |= n >> 2;
75       result->addr |= (n & 3) << 14;
76       break;
77     case 2:
78       /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
79       result->addr |= n << 9;
80       break;
81     case 3:
82       /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
83       result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
84       break;
85     case 4:
86       /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
87       result->data |= (n & 1) << 12;
88       result->addr |= (n >> 1) << 16;
89       break;
90     case 5:
91       /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
92       result->data |= (n & 1) << 15 | (n >> 1) << 8;
93       break;
94     case 6:
95       /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
96       result->data |= (n >> 3) << 13;
97       result->addr |= (n & 7) << 5;
98       break;
99     case 7:
100       /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
101       result->addr |= n;
102       break;
103     }
104     /* Go around again */
105   }
106   return;
107 }
108
109 /* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
110  * type codes. You're more likely to find Genie codes circulating around, but
111  * there's a chance you could come on to one of these. Which is nice, since
112  * they're MUCH easier to implement ;) Once again, the input should be depunc-
113  * tuated already. */
114
115 static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
116
117 static void hex_decode(const char *code, struct patch *result)
118 {
119   char *x;
120   int i;
121   /* 6 digits for address */
122   for(i = 0; i < 6; ++i)
123     {
124       if(!(x = strchr(hex_chars, code[i])))
125       {
126         result->addr = result->data = -1;
127         return;
128       }
129       result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
130     }
131   /* 4 digits for data */
132   for(i = 6; i < 10; ++i)
133     {
134       if(!(x = strchr(hex_chars, code[i])))
135       {
136         result->addr = result->data = -1;
137         return;
138       }
139       result->data = (result->data << 4) | ((x - hex_chars) >> 1);
140     }
141 }
142
143 /* THIS is the function you call from the MegaDrive or whatever. This figures
144  * out whether it's a genie or hex code, depunctuates it, and calls the proper
145  * decoder. */
146 void decode(const char* code, struct patch* result)
147 {
148   int len = strlen(code), i, j;
149   char code_to_pass[16], *x;
150   const char *ad, *da;
151   int adl, dal;
152
153   /* Initialize the result */
154   result->addr = result->data = 0;
155
156   /* Just assume 8 char long string to be Game Genie code */
157   if (len == 8)
158   {
159     genie_decode(code, result);
160     return;
161   }
162
163   /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
164    * code. */
165     if(len == 9 && code[4] == '-')
166     {
167       /* Remove the hyphen and pass to genie_decode */
168       code_to_pass[0] = code[0];
169       code_to_pass[1] = code[1];
170       code_to_pass[2] = code[2];
171       code_to_pass[3] = code[3];
172       code_to_pass[4] = code[5];
173       code_to_pass[5] = code[6];
174       code_to_pass[6] = code[7];
175       code_to_pass[7] = code[8];
176       code_to_pass[8] = '\0';
177       genie_decode(code_to_pass, result);
178       return;
179     }
180
181   /* Otherwise, we assume it's a hex code.
182    * Find the colon so we know where address ends and data starts. If there's
183    * no colon, then we haven't a code at all! */
184   if(!(x = strchr(code, ':'))) goto bad_code;
185   ad = code; da = x + 1; adl = x - code; dal = len - adl - 1;
186
187   /* If a section is empty or too long, toss it */
188   if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
189
190   /* Pad the address with zeros, then fill it with the value */
191   for(i = 0; i < (6 - adl); ++i) code_to_pass[i] = '0';
192   for(j = 0; i < 6; ++i, ++j) code_to_pass[i] = ad[j];
193
194   /* Do the same for data */
195   for(i = 6; i < (10 - dal); ++i) code_to_pass[i] = '0';
196   for(j = 0; i < 10; ++i, ++j) code_to_pass[i] = da[j];
197
198   code_to_pass[10] = '\0';
199
200   /* Decode and goodbye */
201   hex_decode(code_to_pass, result);
202   return;
203
204 bad_code:
205
206   /* AGH! Invalid code! */
207   result->data = result->addr = -1;
208   return;
209 }
210
211
212
213 unsigned int PicoRead16(unsigned int a);
214 void PicoWrite16(unsigned int a, unsigned short d);
215
216
217 void PicoPatchUnload(void)
218 {
219         if (PicoPatches != NULL)
220         {
221                 free(PicoPatches);
222                 PicoPatches = NULL;
223         }
224         PicoPatchCount = 0;
225 }
226
227 int PicoPatchLoad(const char *fname)
228 {
229         FILE *f;
230         char buff[256];
231         struct patch pt;
232         int array_len = 0;
233
234         PicoPatchUnload();
235
236         f = fopen(fname, "r");
237         if (f == NULL)
238         {
239                 return -1;
240         }
241
242         while (fgets(buff, sizeof(buff), f))
243         {
244                 int llen, clen;
245
246                 llen = strlen(buff);
247                 for (clen = 0; clen < llen; clen++)
248                         if (isspace_(buff[clen]))
249                                 break;
250                 buff[clen] = 0;
251
252                 if (clen > 11 || clen < 8)
253                         continue;
254
255                 decode(buff, &pt);
256                 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
257                         continue;
258
259                 /* code was good, add it */
260                 if (array_len < PicoPatchCount + 1)
261                 {
262                         void *ptr;
263                         array_len *= 2;
264                         array_len++;
265                         ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
266                         if (ptr == NULL) break;
267                         PicoPatches = ptr;
268                 }
269                 strcpy(PicoPatches[PicoPatchCount].code, buff);
270                 /* strip */
271                 for (clen++; clen < llen; clen++)
272                         if (!isspace_(buff[clen]))
273                                 break;
274                 for (llen--; llen > 0; llen--)
275                         if (!isspace_(buff[llen]))
276                                 break;
277                 buff[llen+1] = 0;
278                 strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
279                 PicoPatches[PicoPatchCount].name[51] = 0;
280                 PicoPatches[PicoPatchCount].active = 0;
281                 PicoPatches[PicoPatchCount].addr = pt.addr;
282                 PicoPatches[PicoPatchCount].data = pt.data;
283                 PicoPatches[PicoPatchCount].data_old = 0;
284                 PicoPatchCount++;
285                 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
286                 //      PicoPatches[PicoPatchCount-1].name);
287         }
288         fclose(f);
289
290         return 0;
291 }
292
293 /* to be called when the Rom is loaded and byteswapped */
294 void PicoPatchPrepare(void)
295 {
296         int i;
297
298         for (i = 0; i < PicoPatchCount; i++)
299         {
300                 PicoPatches[i].addr &= ~1;
301                 if (PicoPatches[i].addr < Pico.romsize)
302                         PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + PicoPatches[i].addr);
303                 else
304                         PicoPatches[i].data_old = (unsigned short) m68k_read16(PicoPatches[i].addr);
305                 if (strstr(PicoPatches[i].name, "AUTO"))
306                         PicoPatches[i].active = 1;
307         }
308 }
309
310 void PicoPatchApply(void)
311 {
312         int i, u;
313         unsigned int addr;
314
315         for (i = 0; i < PicoPatchCount; i++)
316         {
317                 addr = PicoPatches[i].addr;
318                 if (addr < Pico.romsize)
319                 {
320                         if (PicoPatches[i].active)
321                                 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
322                         else {
323                                 // if current addr is not patched by older patch, write back original val
324                                 for (u = 0; u < i; u++)
325                                         if (PicoPatches[u].addr == addr) break;
326                                 if (u == i)
327                                         *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
328                         }
329                         // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
330                         //      *(unsigned short *)(Pico.rom + addr));
331                 }
332                 else
333                 {
334                         if (PicoPatches[i].active)
335                                 m68k_write16(PicoPatches[i].addr,PicoPatches[i].data);
336                         else {
337                                 // if current addr is not patched by older patch, write back original val
338                                 for (u = 0; u < i; u++)
339                                         if (PicoPatches[u].addr == addr) break;
340                                 if (u == i)
341                                         m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
342                         }
343                 }
344         }
345 }
346