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 :)
33 struct patch_inst *PicoPatches = NULL;
34 int PicoPatchCount = 0;
36 static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
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.
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.
50 static void genie_decode(const char* code, struct patch* result)
57 /* If strchr returns NULL, we were given a bad character */
58 if(!(x = strchr(genie_chars, code[i])))
60 result->addr = -1; result->data = -1;
63 n = (x - genie_chars) >> 1;
64 /* Now, based on which character this is, fit it into the result */
68 /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
69 result->data |= n << 3;
72 /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
73 result->data |= n >> 2;
74 result->addr |= (n & 3) << 14;
77 /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
78 result->addr |= n << 9;
81 /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
82 result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
85 /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
86 result->data |= (n & 1) << 12;
87 result->addr |= (n >> 1) << 16;
90 /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
91 result->data |= (n & 1) << 15 | (n >> 1) << 8;
94 /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
95 result->data |= (n >> 3) << 13;
96 result->addr |= (n & 7) << 5;
99 /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
103 /* Go around again */
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-
114 static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
116 static void hex_decode(const char *code, struct patch *result)
120 /* 6 digits for address */
121 for(i = 0; i < 6; ++i)
123 if(!(x = strchr(hex_chars, code[i])))
125 result->addr = result->data = -1;
128 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
130 /* 4 digits for data */
131 for(i = 6; i < 10; ++i)
133 if(!(x = strchr(hex_chars, code[i])))
135 result->addr = result->data = -1;
138 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
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
145 static void decode(const char* code, struct patch* result)
147 int len = strlen(code), i, j;
148 char code_to_pass[16], *x;
152 /* Initialize the result */
153 result->addr = result->data = 0;
155 /* Just assume 8 char long string to be Game Genie code */
158 genie_decode(code, result);
162 /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
164 if(len == 9 && code[4] == '-')
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);
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;
186 /* If a section is empty or too long, toss it */
187 if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
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];
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];
197 code_to_pass[10] = '\0';
199 /* Decode and goodbye */
200 hex_decode(code_to_pass, result);
205 /* AGH! Invalid code! */
206 result->data = result->addr = -1;
212 unsigned int PicoRead16(unsigned int a);
213 void PicoWrite16(unsigned int a, unsigned short d);
216 void PicoPatchUnload(void)
218 if (PicoPatches != NULL)
226 int PicoPatchLoad(const char *fname)
235 f = fopen(fname, "r");
241 while (fgets(buff, sizeof(buff), f))
246 for (clen = 0; clen < llen; clen++)
247 if (isspace_(buff[clen]))
251 if (clen > 11 || clen < 8)
255 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
258 /* code was good, add it */
259 if (array_len < PicoPatchCount + 1)
264 ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
265 if (ptr == NULL) break;
268 strcpy(PicoPatches[PicoPatchCount].code, buff);
270 for (clen++; clen < llen; clen++)
271 if (!isspace_(buff[clen]))
273 for (llen--; llen > 0; llen--)
274 if (!isspace_(buff[llen]))
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;
284 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
285 // PicoPatches[PicoPatchCount-1].name);
292 /* to be called when the Rom is loaded and byteswapped */
293 void PicoPatchPrepare(void)
297 for (i = 0; i < PicoPatchCount; i++)
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;
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));