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