b67ef287 |
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 | |
efcba75f |
24 | #include "pico_int.h" |
8655fd04 |
25 | #include "memory.h" |
efcba75f |
26 | #include "patch.h" |
b67ef287 |
27 | |
28 | struct patch |
29 | { |
126eb5f4 |
30 | unsigned int addr; |
31 | unsigned short data; |
32 | unsigned char comp; |
b67ef287 |
33 | }; |
34 | |
35 | struct patch_inst *PicoPatches = NULL; |
36 | int PicoPatchCount = 0; |
37 | |
ed4a2193 |
38 | static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899"; |
b67ef287 |
39 | |
40 | /* genie_decode |
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. |
47 | * |
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. |
50 | */ |
51 | |
ed4a2193 |
52 | static void genie_decode_md(const char* code, struct patch* result) |
b67ef287 |
53 | { |
54 | int i = 0, n; |
55 | char* x; |
56 | |
ed4a2193 |
57 | for(; i < 9; ++i) |
b67ef287 |
58 | { |
ed4a2193 |
59 | /* Skip i=4; it's going to be the separating hyphen */ |
60 | if (i==4) continue; |
61 | |
b67ef287 |
62 | /* If strchr returns NULL, we were given a bad character */ |
ed4a2193 |
63 | if(!(x = strchr(genie_chars_md, code[i]))) |
b67ef287 |
64 | { |
65 | result->addr = -1; result->data = -1; |
66 | return; |
67 | } |
ed4a2193 |
68 | n = (x - genie_chars_md) >> 1; |
b67ef287 |
69 | /* Now, based on which character this is, fit it into the result */ |
70 | switch(i) |
71 | { |
72 | case 0: |
73 | /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */ |
74 | result->data |= n << 3; |
75 | break; |
76 | case 1: |
77 | /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */ |
78 | result->data |= n >> 2; |
79 | result->addr |= (n & 3) << 14; |
80 | break; |
81 | case 2: |
82 | /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */ |
83 | result->addr |= n << 9; |
84 | break; |
85 | case 3: |
86 | /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */ |
87 | result->addr |= (n & 0xF) << 20 | (n >> 4) << 8; |
88 | break; |
ed4a2193 |
89 | case 5: |
b67ef287 |
90 | /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */ |
91 | result->data |= (n & 1) << 12; |
92 | result->addr |= (n >> 1) << 16; |
93 | break; |
ed4a2193 |
94 | case 6: |
b67ef287 |
95 | /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */ |
96 | result->data |= (n & 1) << 15 | (n >> 1) << 8; |
97 | break; |
ed4a2193 |
98 | case 7: |
b67ef287 |
99 | /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */ |
100 | result->data |= (n >> 3) << 13; |
101 | result->addr |= (n & 7) << 5; |
102 | break; |
ed4a2193 |
103 | case 8: |
b67ef287 |
104 | /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */ |
105 | result->addr |= n; |
106 | break; |
107 | } |
108 | /* Go around again */ |
109 | } |
110 | return; |
111 | } |
112 | |
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- |
117 | * tuated already. */ |
118 | |
119 | static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf"; |
120 | |
ed4a2193 |
121 | static void hex_decode_md(const char *code, struct patch *result) |
b67ef287 |
122 | { |
123 | char *x; |
124 | int i; |
125 | /* 6 digits for address */ |
126 | for(i = 0; i < 6; ++i) |
ed4a2193 |
127 | { |
128 | if(!(x = strchr(hex_chars, code[i]))) |
b67ef287 |
129 | { |
ed4a2193 |
130 | result->addr = result->data = -1; |
131 | return; |
b67ef287 |
132 | } |
ed4a2193 |
133 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
134 | } |
b67ef287 |
135 | /* 4 digits for data */ |
ed4a2193 |
136 | for(i = 7; i < 11; ++i) |
137 | { |
138 | if(!(x = strchr(hex_chars, code[i]))) |
139 | { |
140 | if (i==8) break; |
141 | result->addr = result->data = -1; |
142 | return; |
143 | } |
144 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
145 | } |
146 | } |
147 | |
148 | void genie_decode_ms(const char *code, struct patch *result) |
149 | { |
150 | char *x; |
151 | int i; |
152 | /* 2 digits for data */ |
153 | for(i=0;i<2;++i) |
154 | { |
155 | if(!(x = strchr(hex_chars, code[i]))) |
156 | { |
157 | result->addr = result->data = -1; |
158 | return; |
159 | } |
160 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
161 | } |
162 | /* 4 digits for address */ |
163 | for(i=2;i<7;++i) |
164 | { |
165 | /* 4th character is hyphen and can be skipped*/ |
166 | if (i==3) continue; |
167 | if(!(x = strchr(hex_chars, code[i]))) |
168 | { |
169 | result->addr = result->data = -1; |
170 | return; |
171 | } |
172 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
173 | } |
174 | /* Correct the address */ |
175 | result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000; |
176 | /* Optional: 3 digits for comp */ |
126eb5f4 |
177 | if (code[7]=='-') |
178 | { |
ed4a2193 |
179 | for(i=8;i<11;++i) |
b67ef287 |
180 | { |
ed4a2193 |
181 | if (i==9) continue; /* 2nd character is ignored */ |
b67ef287 |
182 | if(!(x = strchr(hex_chars, code[i]))) |
183 | { |
126eb5f4 |
184 | result->addr = result->data = -1; |
185 | return; |
b67ef287 |
186 | } |
ed4a2193 |
187 | result->comp = (result->comp << 4) | ((x - hex_chars) >> 1); |
188 | } |
189 | /* Correct the comp */ |
190 | result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA; |
191 | } |
192 | } |
193 | |
194 | void ar_decode_ms(const char *code, struct patch *result){ |
195 | char *x; |
196 | int i; |
197 | /* 2 digits of padding*/ |
198 | /* 4 digits for address */ |
199 | for(i=2;i<7;++i) |
200 | { |
201 | /* 5th character is hyphen and can be skipped*/ |
202 | if (i==4) continue; |
203 | if(!(x = strchr(hex_chars, code[i]))) |
204 | { |
205 | result->addr = result->data = -1; |
206 | return; |
207 | } |
208 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
209 | } |
210 | /* 2 digits for data */ |
211 | for(i=7;i<9;++i) |
212 | { |
213 | if(!(x = strchr(hex_chars, code[i]))) |
214 | { |
215 | result->addr = result->data = -1; |
216 | return; |
217 | } |
218 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
219 | } |
220 | } |
221 | |
222 | void fusion_ram_decode(const char *code, struct patch *result){ |
223 | char *x; |
224 | int i; |
225 | /* 4 digits for address */ |
226 | for(i=0;i<4;++i) |
227 | { |
228 | if(!(x = strchr(hex_chars, code[i]))) |
229 | { |
230 | result->addr = result->data = -1; |
231 | return; |
232 | } |
233 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
234 | } |
235 | /* Skip the ':' */ |
236 | /* 2 digits for data */ |
237 | for(i=5;i<7;++i) |
238 | { |
239 | if(!(x = strchr(hex_chars, code[i]))) |
240 | { |
241 | result->addr = result->data = -1; |
242 | return; |
243 | } |
244 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
245 | } |
246 | } |
247 | |
248 | void fusion_rom_decode(const char *code, struct patch *result){ |
249 | char *x; |
250 | int i; |
251 | /* 2 digits for comp */ |
252 | for(i=0;i<2;++i) |
253 | { |
254 | if(!(x = strchr(hex_chars, code[i]))) |
255 | { |
256 | result->addr = result->data = -1; |
257 | return; |
258 | } |
259 | result->comp = (result->comp << 4) | ((x - hex_chars) >> 1); |
260 | } |
261 | /* 4 digits for address */ |
262 | for(i=2;i<6;++i) |
263 | { |
264 | if(!(x = strchr(hex_chars, code[i]))) |
265 | { |
266 | result->addr = result->data = -1; |
267 | return; |
b67ef287 |
268 | } |
ed4a2193 |
269 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
270 | } |
271 | /* 2 digits for data */ |
272 | for(i=7;i<9;++i) |
273 | { |
274 | if(!(x = strchr(hex_chars, code[i]))) |
275 | { |
276 | result->addr = result->data = -1; |
277 | return; |
278 | } |
279 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
280 | } |
b67ef287 |
281 | } |
282 | |
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 |
285 | * decoder. */ |
8655fd04 |
286 | void decode(const char* code, struct patch* result) |
b67ef287 |
287 | { |
ed4a2193 |
288 | int len = strlen(code); |
b67ef287 |
289 | |
290 | /* Initialize the result */ |
ed4a2193 |
291 | result->addr = result->data = result->comp = 0; |
b67ef287 |
292 | |
93f9619e |
293 | if(!(PicoIn.AHW & PAHW_SMS)) |
b67ef287 |
294 | { |
ed4a2193 |
295 | //If Genesis |
b67ef287 |
296 | |
ed4a2193 |
297 | //Game Genie |
b67ef287 |
298 | if(len == 9 && code[4] == '-') |
299 | { |
ed4a2193 |
300 | genie_decode_md(code, result); |
b67ef287 |
301 | return; |
302 | } |
303 | |
ed4a2193 |
304 | //Master |
305 | else if(len >=9 && code[6] == ':') |
306 | { |
307 | hex_decode_md(code, result); |
308 | } |
309 | |
310 | else |
311 | { |
312 | goto bad_code; |
313 | } |
314 | } else { |
315 | //If Master System |
b67ef287 |
316 | |
ed4a2193 |
317 | //Genie |
9a570a67 |
318 | if(len >= 7 && code[3] == '-') |
ed4a2193 |
319 | { |
320 | genie_decode_ms(code, result); |
321 | } |
b67ef287 |
322 | |
ed4a2193 |
323 | //AR |
324 | else if(len == 9 && code[4] == '-') |
325 | { |
326 | ar_decode_ms(code, result); |
327 | } |
b67ef287 |
328 | |
ed4a2193 |
329 | //Fusion RAM |
330 | else if(len == 7 && code[4] == ':') |
331 | { |
332 | fusion_ram_decode(code, result); |
333 | } |
b67ef287 |
334 | |
ed4a2193 |
335 | //Fusion ROM |
336 | else if(len == 9 && code[6] == ':') |
337 | { |
338 | fusion_rom_decode(code, result); |
339 | } |
b67ef287 |
340 | |
ed4a2193 |
341 | else |
342 | { |
343 | goto bad_code; |
344 | } |
345 | |
346 | //Convert RAM address space to Genesis location. |
347 | if (result->addr>=0xC000) |
348 | result->addr= 0xFF0000 | (0x1FFF & result->addr); |
349 | } |
b67ef287 |
350 | |
ed4a2193 |
351 | return; |
b67ef287 |
352 | |
ed4a2193 |
353 | bad_code: |
b67ef287 |
354 | result->data = result->addr = -1; |
355 | return; |
356 | } |
357 | |
b67ef287 |
358 | void PicoPatchUnload(void) |
359 | { |
126eb5f4 |
360 | if (PicoPatches != NULL) |
361 | { |
362 | free(PicoPatches); |
363 | PicoPatches = NULL; |
364 | } |
365 | PicoPatchCount = 0; |
b67ef287 |
366 | } |
367 | |
368 | int PicoPatchLoad(const char *fname) |
369 | { |
126eb5f4 |
370 | FILE *f; |
371 | char buff[256]; |
372 | struct patch pt; |
373 | int array_len = 0; |
374 | |
375 | PicoPatchUnload(); |
376 | |
377 | f = fopen(fname, "r"); |
378 | if (f == NULL) |
379 | { |
380 | return -1; |
381 | } |
382 | |
383 | while (fgets(buff, sizeof(buff), f)) |
384 | { |
385 | int llen, clen; |
386 | |
387 | llen = strlen(buff); |
388 | for (clen = 0; clen < llen; clen++) |
389 | if (isspace_(buff[clen])) |
390 | break; |
391 | buff[clen] = 0; |
392 | |
393 | if (clen > 11 || clen < 8) |
394 | continue; |
395 | |
396 | decode(buff, &pt); |
397 | if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1) |
398 | continue; |
399 | |
400 | /* code was good, add it */ |
401 | if (array_len < PicoPatchCount + 1) |
402 | { |
403 | void *ptr; |
404 | array_len *= 2; |
405 | array_len++; |
406 | ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0])); |
407 | if (ptr == NULL) break; |
408 | PicoPatches = ptr; |
409 | } |
410 | strcpy(PicoPatches[PicoPatchCount].code, buff); |
411 | /* strip */ |
412 | for (clen++; clen < llen; clen++) |
413 | if (!isspace_(buff[clen])) |
414 | break; |
415 | for (llen--; llen > 0; llen--) |
416 | if (!isspace_(buff[llen])) |
417 | break; |
418 | buff[llen+1] = 0; |
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; |
425 | PicoPatchCount++; |
426 | // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data, |
427 | // PicoPatches[PicoPatchCount-1].name); |
428 | } |
429 | fclose(f); |
430 | |
431 | return 0; |
b67ef287 |
432 | } |
433 | |
434 | /* to be called when the Rom is loaded and byteswapped */ |
435 | void PicoPatchPrepare(void) |
436 | { |
126eb5f4 |
437 | int i; |
438 | int addr; |
439 | |
440 | for (i = 0; i < PicoPatchCount; i++) |
441 | { |
442 | addr=PicoPatches[i].addr; |
443 | addr &= ~1; |
444 | if (addr < Pico.romsize) |
91ea9406 |
445 | PicoPatches[i].data_old = *(u16 *)(Pico.rom + addr); |
ed4a2193 |
446 | else |
126eb5f4 |
447 | { |
93f9619e |
448 | if(!(PicoIn.AHW & PAHW_SMS)) |
91ea9406 |
449 | PicoPatches[i].data_old = (u16) m68k_read16(addr); |
126eb5f4 |
450 | else |
451 | ;// wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr); |
452 | } |
453 | if (strstr(PicoPatches[i].name, "AUTO")) |
454 | PicoPatches[i].active = 1; |
455 | } |
b67ef287 |
456 | } |
457 | |
458 | void PicoPatchApply(void) |
459 | { |
126eb5f4 |
460 | int i, u; |
461 | unsigned int addr; |
b67ef287 |
462 | |
126eb5f4 |
463 | for (i = 0; i < PicoPatchCount; i++) |
464 | { |
465 | addr = PicoPatches[i].addr; |
ed4a2193 |
466 | |
126eb5f4 |
467 | if (addr < Pico.romsize) |
ed4a2193 |
468 | { |
126eb5f4 |
469 | if (PicoPatches[i].active) |
470 | { |
93f9619e |
471 | if (!(PicoIn.AHW & PAHW_SMS)) |
91ea9406 |
472 | *(u16 *)(Pico.rom + addr) = PicoPatches[i].data; |
126eb5f4 |
473 | else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr)) |
474 | *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data; |
475 | } |
476 | else |
477 | { |
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; |
481 | if (u == i) |
482 | { |
93f9619e |
483 | if (!(PicoIn.AHW & PAHW_SMS)) |
91ea9406 |
484 | *(u16 *)(Pico.rom + addr) = PicoPatches[i].data_old; |
126eb5f4 |
485 | else |
486 | *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old; |
487 | } |
488 | } |
ed4a2193 |
489 | // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr, |
91ea9406 |
490 | // *(u16 *)(Pico.rom + addr)); |
ed4a2193 |
491 | } |
492 | else |
493 | { |
126eb5f4 |
494 | if (PicoPatches[i].active) |
495 | { |
93f9619e |
496 | if (!(PicoIn.AHW & PAHW_SMS)) |
126eb5f4 |
497 | m68k_write16(addr,PicoPatches[i].data); |
498 | else |
499 | ;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data); |
500 | } |
501 | else |
502 | { |
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; |
506 | if (u == i) |
507 | { |
93f9619e |
508 | if (!(PicoIn.AHW & PAHW_SMS)) |
126eb5f4 |
509 | m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old); |
510 | else |
511 | ;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old); |
512 | } |
513 | } |
ed4a2193 |
514 | } |
126eb5f4 |
515 | } |
b67ef287 |
516 | } |
517 | |