lowercasing filenames, part3
[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 <stdio.h>
25 //#include <string.h>
26 #include <ctype.h>
27
28 #include "pico_int.h"
29 #include "patch.h"
30
31 struct patch
32 {
33         unsigned int addr;
34         unsigned short data;
35 };
36
37 struct patch_inst *PicoPatches = NULL;
38 int PicoPatchCount = 0;
39
40 static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
41
42 /* genie_decode
43  * This function converts a Game Genie code to an address:data pair.
44  * The code is given as an 8-character string, like "BJX0SA1C". It need not
45  * be null terminated, since only the first 8 characters are taken. It is
46  * assumed that the code is already made of valid characters, i.e. there are no
47  * Q's, U's, or symbols. If such a character is
48  * encountered, the function will return with a warning on stderr.
49  *
50  * The resulting address:data pair is returned in the struct patch pointed to
51  * by result. If an error results, both the address and data will be set to -1.
52  */
53
54 static void genie_decode(const char* code, struct patch* result)
55 {
56   int i = 0, n;
57   char* x;
58
59   for(; i < 8; ++i)
60   {
61     /* If strchr returns NULL, we were given a bad character */
62     if(!(x = strchr(genie_chars, code[i])))
63     {
64       result->addr = -1; result->data = -1;
65       return;
66     }
67     n = (x - genie_chars) >> 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 4:
89       /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
90       result->data |= (n & 1) << 12;
91       result->addr |= (n >> 1) << 16;
92       break;
93     case 5:
94       /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
95       result->data |= (n & 1) << 15 | (n >> 1) << 8;
96       break;
97     case 6:
98       /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
99       result->data |= (n >> 3) << 13;
100       result->addr |= (n & 7) << 5;
101       break;
102     case 7:
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(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 = 6; i < 10; ++i)
136     {
137       if(!(x = strchr(hex_chars, code[i])))
138       {
139         result->addr = result->data = -1;
140         return;
141       }
142       result->data = (result->data << 4) | ((x - hex_chars) >> 1);
143     }
144 }
145
146 /* THIS is the function you call from the MegaDrive or whatever. This figures
147  * out whether it's a genie or hex code, depunctuates it, and calls the proper
148  * decoder. */
149 static void decode(const char* code, struct patch* result)
150 {
151   int len = strlen(code), i, j;
152   char code_to_pass[16], *x;
153   const char *ad, *da;
154   int adl, dal;
155
156   /* Initialize the result */
157   result->addr = result->data = 0;
158
159   /* Just assume 8 char long string to be Game Genie code */
160   if (len == 8)
161   {
162     genie_decode(code, result);
163     return;
164   }
165
166   /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
167    * code. */
168     if(len == 9 && code[4] == '-')
169     {
170       /* Remove the hyphen and pass to genie_decode */
171       code_to_pass[0] = code[0];
172       code_to_pass[1] = code[1];
173       code_to_pass[2] = code[2];
174       code_to_pass[3] = code[3];
175       code_to_pass[4] = code[5];
176       code_to_pass[5] = code[6];
177       code_to_pass[6] = code[7];
178       code_to_pass[7] = code[8];
179       code_to_pass[8] = '\0';
180       genie_decode(code_to_pass, result);
181       return;
182     }
183
184   /* Otherwise, we assume it's a hex code.
185    * Find the colon so we know where address ends and data starts. If there's
186    * no colon, then we haven't a code at all! */
187   if(!(x = strchr(code, ':'))) goto bad_code;
188   ad = code; da = x + 1; adl = x - code; dal = len - adl - 1;
189
190   /* If a section is empty or too long, toss it */
191   if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
192
193   /* Pad the address with zeros, then fill it with the value */
194   for(i = 0; i < (6 - adl); ++i) code_to_pass[i] = '0';
195   for(j = 0; i < 6; ++i, ++j) code_to_pass[i] = ad[j];
196
197   /* Do the same for data */
198   for(i = 6; i < (10 - dal); ++i) code_to_pass[i] = '0';
199   for(j = 0; i < 10; ++i, ++j) code_to_pass[i] = da[j];
200
201   code_to_pass[10] = '\0';
202
203   /* Decode and goodbye */
204   hex_decode(code_to_pass, result);
205   return;
206
207 bad_code:
208
209   /* AGH! Invalid code! */
210   result->data = result->addr = -1;
211   return;
212 }
213
214
215
216 unsigned int PicoRead16(unsigned int a);
217 void PicoWrite16(unsigned int a, unsigned short d);
218
219
220 void PicoPatchUnload(void)
221 {
222         if (PicoPatches != NULL)
223         {
224                 free(PicoPatches);
225                 PicoPatches = NULL;
226         }
227         PicoPatchCount = 0;
228 }
229
230 int PicoPatchLoad(const char *fname)
231 {
232         FILE *f;
233         char buff[256];
234         struct patch pt;
235         int array_len = 0;
236
237         PicoPatchUnload();
238
239         f = fopen(fname, "r");
240         if (f == NULL)
241         {
242                 return -1;
243         }
244
245         while (fgets(buff, sizeof(buff), f))
246         {
247                 int llen, clen;
248
249                 llen = strlen(buff);
250                 for (clen = 0; clen < llen; clen++)
251                         if (isspace(buff[clen])) break;
252                 buff[clen] = 0;
253
254                 if (clen > 11 || clen < 8)
255                         continue;
256
257                 decode(buff, &pt);
258                 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
259                         continue;
260
261                 /* code was good, add it */
262                 if (array_len < PicoPatchCount + 1)
263                 {
264                         void *ptr;
265                         array_len *= 2;
266                         array_len++;
267                         ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
268                         if (ptr == NULL) break;
269                         PicoPatches = ptr;
270                 }
271                 strcpy(PicoPatches[PicoPatchCount].code, buff);
272                 /* strip */
273                 for (clen++; clen < llen; clen++)
274                         if (!isspace(buff[clen])) break;
275                 for (llen--; llen > 0; llen--)
276                         if (!isspace(buff[llen])) 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                 PicoPatches[i].data_old = PicoRead16(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                         /* RAM or some other weird patch */
332                         if (PicoPatches[i].active)
333                                 PicoWrite16(addr, PicoPatches[i].data);
334                 }
335         }
336 }
337