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.
6 * These come out to a very scrambled bit pattern like this:
7 * (SCRA-MBLE is just an example)
10 * 01111 00010 01110 00000 01011 00001 01010 00100
11 * ijklm nopIJ KLMNO PABCD EFGHd efgha bcQRS TUVWX
13 * Our goal is to rearrange that to this:
15 * 0000 0101 1001 1100 0100 0100 : 1011 0000 0111 1000
16 * ABCD EFGH IJKL MNOP QRST UVWX : abcd efgh ijkl mnop
18 * which in Hexadecimal is 059C44:B078. Simple, huh? ;)
20 * So, then, we dutifully change memory location 059C44 to B078!
21 * (of course, that's handled by a different source file :)
37 struct patch_inst *PicoPatches = NULL;
38 int PicoPatchCount = 0;
40 static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
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.
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.
54 static void genie_decode(const char* code, struct patch* result)
61 /* If strchr returns NULL, we were given a bad character */
62 if(!(x = strchr(genie_chars, code[i])))
64 result->addr = -1; result->data = -1;
67 n = (x - genie_chars) >> 1;
68 /* Now, based on which character this is, fit it into the result */
72 /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
73 result->data |= n << 3;
76 /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
77 result->data |= n >> 2;
78 result->addr |= (n & 3) << 14;
81 /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
82 result->addr |= n << 9;
85 /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
86 result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
89 /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
90 result->data |= (n & 1) << 12;
91 result->addr |= (n >> 1) << 16;
94 /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
95 result->data |= (n & 1) << 15 | (n >> 1) << 8;
98 /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
99 result->data |= (n >> 3) << 13;
100 result->addr |= (n & 7) << 5;
103 /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
107 /* Go around again */
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-
118 static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
120 static void hex_decode(const char *code, struct patch *result)
124 /* 6 digits for address */
125 for(i = 0; i < 6; ++i)
127 if(!(x = strchr(hex_chars, code[i])))
129 result->addr = result->data = -1;
132 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
134 /* 4 digits for data */
135 for(i = 6; i < 10; ++i)
137 if(!(x = strchr(hex_chars, code[i])))
139 result->addr = result->data = -1;
142 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
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
149 static void decode(const char* code, struct patch* result)
151 int len = strlen(code), i, j;
152 char code_to_pass[16], *x;
156 /* Initialize the result */
157 result->addr = result->data = 0;
159 /* Just assume 8 char long string to be Game Genie code */
162 genie_decode(code, result);
166 /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
168 if(len == 9 && code[4] == '-')
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);
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;
190 /* If a section is empty or too long, toss it */
191 if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
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];
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];
201 code_to_pass[10] = '\0';
203 /* Decode and goodbye */
204 hex_decode(code_to_pass, result);
209 /* AGH! Invalid code! */
210 result->data = result->addr = -1;
216 unsigned int PicoRead16(unsigned int a);
217 void PicoWrite16(unsigned int a, unsigned short d);
220 void PicoPatchUnload(void)
222 if (PicoPatches != NULL)
230 int PicoPatchLoad(const char *fname)
239 f = fopen(fname, "r");
245 while (fgets(buff, sizeof(buff), f))
250 for (clen = 0; clen < llen; clen++)
251 if (isspace(buff[clen])) break;
254 if (clen > 11 || clen < 8)
258 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
261 /* code was good, add it */
262 if (array_len < PicoPatchCount + 1)
267 ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
268 if (ptr == NULL) break;
271 strcpy(PicoPatches[PicoPatchCount].code, buff);
273 for (clen++; clen < llen; clen++)
274 if (!isspace(buff[clen])) break;
275 for (llen--; llen > 0; llen--)
276 if (!isspace(buff[llen])) break;
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;
285 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
286 // PicoPatches[PicoPatchCount-1].name);
293 /* to be called when the Rom is loaded and byteswapped */
294 void PicoPatchPrepare(void)
298 for (i = 0; i < PicoPatchCount; i++)
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;
307 void PicoPatchApply(void)
312 for (i = 0; i < PicoPatchCount; i++)
314 addr = PicoPatches[i].addr;
315 if (addr < Pico.romsize)
317 if (PicoPatches[i].active)
318 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
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;
324 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
326 // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
327 // *(unsigned short *)(Pico.rom + addr));
331 /* RAM or some other weird patch */
332 if (PicoPatches[i].active)
333 PicoWrite16(addr, PicoPatches[i].data);