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 :)
34 struct patch_inst *PicoPatches = NULL;
35 int PicoPatchCount = 0;
37 static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
40 * This function converts a Game Genie code to an address:data pair.
41 * The code is given as an 8-character string, like "BJX0SA1C". It need not
42 * be null terminated, since only the first 8 characters are taken. It is
43 * assumed that the code is already made of valid characters, i.e. there are no
44 * Q's, U's, or symbols. If such a character is
45 * encountered, the function will return with a warning on stderr.
47 * The resulting address:data pair is returned in the struct patch pointed to
48 * by result. If an error results, both the address and data will be set to -1.
51 static void genie_decode_md(const char* code, struct patch* result)
58 /* Skip i=4; it's going to be the separating hyphen */
61 /* If strchr returns NULL, we were given a bad character */
62 if(!(x = strchr(genie_chars_md, code[i])))
64 result->addr = -1; result->data = -1;
67 n = (x - genie_chars_md) >> 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_md(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 = 7; i < 11; ++i)
137 if(!(x = strchr(hex_chars, code[i])))
140 result->addr = result->data = -1;
143 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
147 void genie_decode_ms(const char *code, struct patch *result)
151 /* 2 digits for data */
154 if(!(x = strchr(hex_chars, code[i])))
156 result->addr = result->data = -1;
159 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
161 /* 4 digits for address */
164 /* 4th character is hyphen and can be skipped*/
166 if(!(x = strchr(hex_chars, code[i])))
168 result->addr = result->data = -1;
171 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
173 /* Correct the address */
174 result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
175 /* Optional: 3 digits for comp */
179 if (i==9) continue; /* 2nd character is ignored */
180 if(!(x = strchr(hex_chars, code[i])))
182 result->addr = result->data = -1;
185 result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
187 /* Correct the comp */
188 result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
192 void ar_decode_ms(const char *code, struct patch *result){
195 /* 2 digits of padding*/
196 /* 4 digits for address */
199 /* 5th character is hyphen and can be skipped*/
201 if(!(x = strchr(hex_chars, code[i])))
203 result->addr = result->data = -1;
206 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
208 /* 2 digits for data */
211 if(!(x = strchr(hex_chars, code[i])))
213 result->addr = result->data = -1;
216 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
220 void fusion_ram_decode(const char *code, struct patch *result){
223 /* 4 digits for address */
226 if(!(x = strchr(hex_chars, code[i])))
228 result->addr = result->data = -1;
231 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
234 /* 2 digits for data */
237 if(!(x = strchr(hex_chars, code[i])))
239 result->addr = result->data = -1;
242 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
246 void fusion_rom_decode(const char *code, struct patch *result){
249 /* 2 digits for comp */
252 if(!(x = strchr(hex_chars, code[i])))
254 result->addr = result->data = -1;
257 result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
259 /* 4 digits for address */
262 if(!(x = strchr(hex_chars, code[i])))
264 result->addr = result->data = -1;
267 result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
269 /* 2 digits for data */
272 if(!(x = strchr(hex_chars, code[i])))
274 result->addr = result->data = -1;
277 result->data = (result->data << 4) | ((x - hex_chars) >> 1);
281 /* THIS is the function you call from the MegaDrive or whatever. This figures
282 * out whether it's a genie or hex code, depunctuates it, and calls the proper
284 void decode(const char* code, struct patch* result)
286 int len = strlen(code);
288 /* Initialize the result */
289 result->addr = result->data = result->comp = 0;
291 if(!(PicoAHW & PAHW_SMS))
296 if(len == 9 && code[4] == '-')
298 genie_decode_md(code, result);
303 else if(len >=9 && code[6] == ':')
305 hex_decode_md(code, result);
316 if(len == 11 && code[3] == '-' && code[7] == '-')
318 genie_decode_ms(code, result);
322 else if(len == 9 && code[4] == '-')
324 ar_decode_ms(code, result);
328 else if(len == 7 && code[4] == ':')
330 fusion_ram_decode(code, result);
334 else if(len == 9 && code[6] == ':')
336 fusion_rom_decode(code, result);
344 //Convert RAM address space to Genesis location.
345 if (result->addr>=0xC000)
346 result->addr= 0xFF0000 | (0x1FFF & result->addr);
352 result->data = result->addr = -1;
358 unsigned int PicoRead16(unsigned int a);
359 void PicoWrite16(unsigned int a, unsigned short d);
360 extern unsigned short m68k_read16(unsigned int a);
361 extern void m68k_write16(unsigned int a, unsigned short d);
362 extern char PicoRead8_z80(unsigned short a);
363 extern void PicoWrite8_z80(unsigned short a, char d);
365 void PicoPatchUnload(void)
367 if (PicoPatches != NULL)
375 int PicoPatchLoad(const char *fname)
384 f = fopen(fname, "r");
390 while (fgets(buff, sizeof(buff), f))
395 for (clen = 0; clen < llen; clen++)
396 if (isspace_(buff[clen]))
400 if (clen > 11 || clen < 8)
404 if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
407 /* code was good, add it */
408 if (array_len < PicoPatchCount + 1)
413 ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
414 if (ptr == NULL) break;
417 strcpy(PicoPatches[PicoPatchCount].code, buff);
419 for (clen++; clen < llen; clen++)
420 if (!isspace_(buff[clen]))
422 for (llen--; llen > 0; llen--)
423 if (!isspace_(buff[llen]))
426 strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
427 PicoPatches[PicoPatchCount].name[51] = 0;
428 PicoPatches[PicoPatchCount].active = 0;
429 PicoPatches[PicoPatchCount].addr = pt.addr;
430 PicoPatches[PicoPatchCount].data = pt.data;
431 PicoPatches[PicoPatchCount].data_old = 0;
433 // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
434 // PicoPatches[PicoPatchCount-1].name);
441 /* to be called when the Rom is loaded and byteswapped */
442 void PicoPatchPrepare(void)
447 for (i = 0; i < PicoPatchCount; i++)
449 addr=PicoPatches[i].addr;
451 if (addr < Pico.romsize)
452 PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
455 if(!(PicoAHW & PAHW_SMS))
456 PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
458 PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
460 if (strstr(PicoPatches[i].name, "AUTO"))
461 PicoPatches[i].active = 1;
465 void PicoPatchApply(void)
470 for (i = 0; i < PicoPatchCount; i++)
472 addr = PicoPatches[i].addr;
474 if (addr < Pico.romsize)
476 if (PicoPatches[i].active)
478 if (!(PicoAHW & PAHW_SMS))
479 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
480 else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
481 *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
485 // if current addr is not patched by older patch, write back original val
486 for (u = 0; u < i; u++)
487 if (PicoPatches[u].addr == addr) break;
490 if (!(PicoAHW & PAHW_SMS))
491 *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
493 *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
496 // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
497 // *(unsigned short *)(Pico.rom + addr));
501 if (PicoPatches[i].active)
503 if (!(PicoAHW & PAHW_SMS))
504 m68k_write16(addr,PicoPatches[i].data);
506 PicoWrite8_z80(addr,PicoPatches[i].data);
510 // if current addr is not patched by older patch, write back original val
511 for (u = 0; u < i; u++)
512 if (PicoPatches[u].addr == addr) break;
515 if (!(PicoAHW & PAHW_SMS))
516 m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
518 PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);