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 :)
35 struct patch_inst *PicoPatches = NULL;
36 int PicoPatchCount = 0;
38 static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
41 * This function converts a Game Genie code to an address:data pair.
42 * The code is given as an 8-character string, like "BJX0SA1C". It need not
43 * be null terminated, since only the first 8 characters are taken. It is
44 * assumed that the code is already made of valid characters, i.e. there are no
45 * Q's, U's, or symbols. If such a character is
46 * encountered, the function will return with a warning on stderr.
48 * The resulting address:data pair is returned in the struct patch pointed to
49 * by result. If an error results, both the address and data will be set to -1.
52 static void genie_decode_md(const char* code, struct patch* result)
59 /* Skip i=4; it's going to be the separating hyphen */
62 /* If strchr returns NULL, we were given a bad character */
63 if(!(x = strchr(genie_chars_md, code[i])))
65 result->addr = -1; result->data = -1;
68 n = (x - genie_chars_md) >> 1;
69 /* Now, based on which character this is, fit it into the result */
73 /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
74 result->data |= n << 3;
77 /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
78 result->data |= n >> 2;
79 result->addr |= (n & 3) << 14;
82 /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
83 result->addr |= n << 9;
86 /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
87 result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
90 /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
91 result->data |= (n & 1) << 12;
92 result->addr |= (n >> 1) << 16;
95 /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
96 result->data |= (n & 1) << 15 | (n >> 1) << 8;
99 /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
100 result->data |= (n >> 3) << 13;
101 result->addr |= (n & 7) << 5;
104 /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
108 /* Go around again */
113 /* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
114 * type codes. You're more likely to find Genie codes circulating around, but
115 * there's a chance you could come on to one of these. Which is nice, since
116 * they're MUCH easier to implement ;) Once again, the input should be depunc-
119 static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
121 static void hex_decode_md(const char *code, struct patch *result)
125 /* 6 digits for address */
126 for(i = 0; i < 6; ++i)
128 if(!(x = strchr(hex_chars, code[i])))
130 result->addr = result->data = -1;
133 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
135 /* 4 digits for data */
136 for(i = 7; i < 11; ++i)
138 if(!(x = strchr(hex_chars, code[i])))
141 result->addr = result->data = -1;
144 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
148 void genie_decode_ms(const char *code, struct patch *result)
152 /* 2 digits for data */
155 if(!(x = strchr(hex_chars, code[i])))
157 result->addr = result->data = -1;
160 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
162 /* 4 digits for address */
165 /* 4th character is hyphen and can be skipped*/
167 if(!(x = strchr(hex_chars, code[i])))
169 result->addr = result->data = -1;
172 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
174 /* Correct the address */
175 result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
176 /* Optional: 3 digits for comp */
181 if (i==9) continue; /* 2nd character is ignored */
182 if(!(x = strchr(hex_chars, code[i])))
184 result->addr = result->data = -1;
187 result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
189 /* Correct the comp */
190 result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
194 void ar_decode_ms(const char *code, struct patch *result){
197 /* 2 digits of padding*/
198 /* 4 digits for address */
201 /* 5th character is hyphen and can be skipped*/
203 if(!(x = strchr(hex_chars, code[i])))
205 result->addr = result->data = -1;
208 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
210 /* 2 digits for data */
213 if(!(x = strchr(hex_chars, code[i])))
215 result->addr = result->data = -1;
218 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
222 void fusion_ram_decode(const char *code, struct patch *result){
225 /* 4 digits for address */
228 if(!(x = strchr(hex_chars, code[i])))
230 result->addr = result->data = -1;
233 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
236 /* 2 digits for data */
239 if(!(x = strchr(hex_chars, code[i])))
241 result->addr = result->data = -1;
244 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
248 void fusion_rom_decode(const char *code, struct patch *result){
251 /* 2 digits for comp */
254 if(!(x = strchr(hex_chars, code[i])))
256 result->addr = result->data = -1;
259 result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
261 /* 4 digits for address */
264 if(!(x = strchr(hex_chars, code[i])))
266 result->addr = result->data = -1;
269 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
271 /* 2 digits for data */
274 if(!(x = strchr(hex_chars, code[i])))
276 result->addr = result->data = -1;
279 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
283 /* THIS is the function you call from the MegaDrive or whatever. This figures
284 * out whether it's a genie or hex code, depunctuates it, and calls the proper
286 void decode(const char* code, struct patch* result)
288 int len = strlen(code);
290 /* Initialize the result */
291 result->addr = result->data = result->comp = 0;
293 if(!(PicoIn.AHW & PAHW_SMS))
298 if(len == 9 && code[4] == '-')
300 genie_decode_md(code, result);
305 else if(len >=9 && code[6] == ':')
307 hex_decode_md(code, result);
318 if(len >= 7 && code[3] == '-')
320 genie_decode_ms(code, result);
324 else if(len == 9 && code[4] == '-')
326 ar_decode_ms(code, result);
330 else if(len == 7 && code[4] == ':')
332 fusion_ram_decode(code, result);
336 else if(len == 9 && code[6] == ':')
338 fusion_rom_decode(code, result);
346 //Convert RAM address space to Genesis location.
347 if (result->addr>=0xC000)
348 result->addr= 0xFF0000 | (0x1FFF & result->addr);
354 result->data = result->addr = -1;
358 void PicoPatchUnload(void)
360 if (PicoPatches != NULL)
368 int PicoPatchLoad(const char *fname)
377 f = fopen(fname, "r");
383 while (fgets(buff, sizeof(buff), f))
388 for (clen = 0; clen < llen; clen++)
389 if (isspace_(buff[clen]))
393 if (clen > 11 || clen < 8)
397 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
400 /* code was good, add it */
401 if (array_len < PicoPatchCount + 1)
406 ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
407 if (ptr == NULL) break;
410 strcpy(PicoPatches[PicoPatchCount].code, buff);
412 for (clen++; clen < llen; clen++)
413 if (!isspace_(buff[clen]))
415 for (llen--; llen > 0; llen--)
416 if (!isspace_(buff[llen]))
419 strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
420 PicoPatches[PicoPatchCount].name[51] = 0;
421 PicoPatches[PicoPatchCount].active = 0;
422 PicoPatches[PicoPatchCount].addr = pt.addr;
423 PicoPatches[PicoPatchCount].data = pt.data;
424 PicoPatches[PicoPatchCount].data_old = 0;
426 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
427 // PicoPatches[PicoPatchCount-1].name);
434 /* to be called when the Rom is loaded and byteswapped */
435 void PicoPatchPrepare(void)
440 for (i = 0; i < PicoPatchCount; i++)
442 addr=PicoPatches[i].addr;
444 if (addr < Pico.romsize)
445 PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
448 if(!(PicoIn.AHW & PAHW_SMS))
449 PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
451 ;// wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
453 if (strstr(PicoPatches[i].name, "AUTO"))
454 PicoPatches[i].active = 1;
458 void PicoPatchApply(void)
463 for (i = 0; i < PicoPatchCount; i++)
465 addr = PicoPatches[i].addr;
467 if (addr < Pico.romsize)
469 if (PicoPatches[i].active)
471 if (!(PicoIn.AHW & PAHW_SMS))
472 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
473 else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
474 *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
478 // if current addr is not patched by older patch, write back original val
479 for (u = 0; u < i; u++)
480 if (PicoPatches[u].addr == addr) break;
483 if (!(PicoIn.AHW & PAHW_SMS))
484 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
486 *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
489 // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
490 // *(unsigned short *)(Pico.rom + addr));
494 if (PicoPatches[i].active)
496 if (!(PicoIn.AHW & PAHW_SMS))
497 m68k_write16(addr,PicoPatches[i].data);
499 ;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
503 // if current addr is not patched by older patch, write back original val
504 for (u = 0; u < i; u++)
505 if (PicoPatches[u].addr == addr) break;
508 if (!(PicoIn.AHW & PAHW_SMS))
509 m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
511 ;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);