forked from libretro/picodrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.c
521 lines (475 loc) · 13.1 KB
/
patch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* Decode a Game Genie code into an M68000 address/data pair.
* The Game Genie code is made of the characters
* ABCDEFGHJKLMNPRSTVWXYZ0123456789 (notice the missing I, O, Q and U).
* Where A = 00000, B = 00001, C = 00010, ... , on to 9 = 11111.
*
* These come out to a very scrambled bit pattern like this:
* (SCRA-MBLE is just an example)
*
* S C R A - M B L E
* 01111 00010 01110 00000 01011 00001 01010 00100
* ijklm nopIJ KLMNO PABCD EFGHd efgha bcQRS TUVWX
*
* Our goal is to rearrange that to this:
*
* 0000 0101 1001 1100 0100 0100 : 1011 0000 0111 1000
* ABCD EFGH IJKL MNOP QRST UVWX : abcd efgh ijkl mnop
*
* which in Hexadecimal is 059C44:B078. Simple, huh? ;)
*
* So, then, we dutifully change memory location 059C44 to B078!
* (of course, that's handled by a different source file :)
*/
#include "pico_int.h"
#include "memory.h"
#include "patch.h"
#ifdef USE_LIBRETRO_VFS
#include "file_stream_transforms.h"
#endif
struct patch
{
unsigned int addr;
unsigned short data;
unsigned char comp;
};
struct patch_inst *PicoPatches = NULL;
int PicoPatchCount = 0;
static char genie_chars_md[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
/* genie_decode
* This function converts a Game Genie code to an address:data pair.
* The code is given as an 8-character string, like "BJX0SA1C". It need not
* be null terminated, since only the first 8 characters are taken. It is
* assumed that the code is already made of valid characters, i.e. there are no
* Q's, U's, or symbols. If such a character is
* encountered, the function will return with a warning on stderr.
*
* The resulting address:data pair is returned in the struct patch pointed to
* by result. If an error results, both the address and data will be set to -1.
*/
static void genie_decode_md(const char* code, struct patch* result)
{
int i = 0, n;
char* x;
for(; i < 9; ++i)
{
/* Skip i=4; it's going to be the separating hyphen */
if (i==4) continue;
/* If strchr returns NULL, we were given a bad character */
if(!(x = strchr(genie_chars_md, code[i])))
{
result->addr = -1; result->data = -1;
return;
}
n = (x - genie_chars_md) >> 1;
/* Now, based on which character this is, fit it into the result */
switch(i)
{
case 0:
/* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
result->data |= n << 3;
break;
case 1:
/* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
result->data |= n >> 2;
result->addr |= (n & 3) << 14;
break;
case 2:
/* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
result->addr |= n << 9;
break;
case 3:
/* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
break;
case 5:
/* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
result->data |= (n & 1) << 12;
result->addr |= (n >> 1) << 16;
break;
case 6:
/* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
result->data |= (n & 1) << 15 | (n >> 1) << 8;
break;
case 7:
/* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
result->data |= (n >> 3) << 13;
result->addr |= (n & 7) << 5;
break;
case 8:
/* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
result->addr |= n;
break;
}
/* Go around again */
}
return;
}
/* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
* type codes. You're more likely to find Genie codes circulating around, but
* there's a chance you could come on to one of these. Which is nice, since
* they're MUCH easier to implement ;) Once again, the input should be depunc-
* tuated already. */
static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
static void hex_decode_md(const char *code, struct patch *result)
{
char *x;
int i;
/* 6 digits for address */
for(i = 0; i < 6; ++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
}
/* 4 digits for data */
for(i = 7; i < 11; ++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
if (i==8) break;
result->addr = result->data = -1;
return;
}
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
}
}
void genie_decode_ms(const char *code, struct patch *result)
{
char *x;
int i;
/* 2 digits for data */
for(i=0;i<2;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
}
/* 4 digits for address */
for(i=2;i<7;++i)
{
/* 4th character is hyphen and can be skipped*/
if (i==3) continue;
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
}
/* Correct the address */
result->addr = ((result->addr >> 4) | (result->addr << 12 & 0xF000)) ^ 0xF000;
/* Optional: 3 digits for comp */
if (code[7]=='-')
{
for(i=8;i<11;++i)
{
if (i==9) continue; /* 2nd character is ignored */
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
}
/* Correct the comp */
result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
}
}
void ar_decode_ms(const char *code, struct patch *result){
char *x;
int i;
/* 2 digits of padding*/
/* 4 digits for address */
for(i=2;i<7;++i)
{
/* 5th character is hyphen and can be skipped*/
if (i==4) continue;
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
}
/* 2 digits for data */
for(i=7;i<9;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
}
}
void fusion_ram_decode(const char *code, struct patch *result){
char *x;
int i;
/* 4 digits for address */
for(i=0;i<4;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
}
/* Skip the ':' */
/* 2 digits for data */
for(i=5;i<7;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
}
}
void fusion_rom_decode(const char *code, struct patch *result){
char *x;
int i;
/* 2 digits for comp */
for(i=0;i<2;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
}
/* 4 digits for address */
for(i=2;i<6;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
}
/* 2 digits for data */
for(i=7;i<9;++i)
{
if(!(x = strchr(hex_chars, code[i])))
{
result->addr = result->data = -1;
return;
}
result->data = (result->data << 4) | ((x - hex_chars) >> 1);
}
}
/* THIS is the function you call from the MegaDrive or whatever. This figures
* out whether it's a genie or hex code, depunctuates it, and calls the proper
* decoder. */
void decode(const char* code, struct patch* result)
{
int len = strlen(code);
/* Initialize the result */
result->addr = result->data = result->comp = 0;
if(!(PicoIn.AHW & PAHW_SMS))
{
//If Genesis
//Game Genie
if(len == 9 && code[4] == '-')
{
genie_decode_md(code, result);
return;
}
//Master
else if(len >=9 && code[6] == ':')
{
hex_decode_md(code, result);
}
else
{
goto bad_code;
}
} else {
//If Master System
//Genie
if(len >= 7 && code[3] == '-')
{
genie_decode_ms(code, result);
}
//AR
else if(len == 9 && code[4] == '-')
{
ar_decode_ms(code, result);
}
//Fusion RAM
else if(len == 7 && code[4] == ':')
{
fusion_ram_decode(code, result);
}
//Fusion ROM
else if(len == 9 && code[6] == ':')
{
fusion_rom_decode(code, result);
}
else
{
goto bad_code;
}
//Convert RAM address space to Genesis location.
if (result->addr>=0xC000)
result->addr= 0xFF0000 | (0x1FFF & result->addr);
}
return;
bad_code:
result->data = result->addr = -1;
return;
}
void PicoPatchUnload(void)
{
if (PicoPatches != NULL)
{
free(PicoPatches);
PicoPatches = NULL;
}
PicoPatchCount = 0;
}
int PicoPatchLoad(const char *fname)
{
FILE *f;
char buff[256];
struct patch pt;
int array_len = 0;
PicoPatchUnload();
f = fopen(fname, "r");
if (f == NULL)
{
return -1;
}
while (fgets(buff, sizeof(buff), f))
{
int llen, clen;
llen = strlen(buff);
for (clen = 0; clen < llen; clen++)
if (isspace_(buff[clen]))
break;
buff[clen] = 0;
if (clen > 11 || clen < 8)
continue;
decode(buff, &pt);
if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
continue;
/* code was good, add it */
if (array_len < PicoPatchCount + 1)
{
void *ptr;
array_len *= 2;
array_len++;
ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
if (ptr == NULL) break;
PicoPatches = ptr;
}
strcpy(PicoPatches[PicoPatchCount].code, buff);
/* strip */
for (clen++; clen < llen; clen++)
if (!isspace_(buff[clen]))
break;
for (llen--; llen > 0; llen--)
if (!isspace_(buff[llen]))
break;
buff[llen+1] = 0;
strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
PicoPatches[PicoPatchCount].name[51] = 0;
PicoPatches[PicoPatchCount].active = 0;
PicoPatches[PicoPatchCount].addr = pt.addr;
PicoPatches[PicoPatchCount].data = pt.data;
PicoPatches[PicoPatchCount].data_old = 0;
PicoPatchCount++;
// fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
// PicoPatches[PicoPatchCount-1].name);
}
fclose(f);
return 0;
}
/* to be called when the Rom is loaded and byteswapped */
void PicoPatchPrepare(void)
{
int i;
int addr;
for (i = 0; i < PicoPatchCount; i++)
{
addr=PicoPatches[i].addr;
addr &= ~1;
if (addr < Pico.romsize)
PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
else
{
if(!(PicoIn.AHW & PAHW_SMS))
PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
else
;// wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
}
if (strstr(PicoPatches[i].name, "AUTO"))
PicoPatches[i].active = 1;
}
}
void PicoPatchApply(void)
{
int i, u;
unsigned int addr;
for (i = 0; i < PicoPatchCount; i++)
{
addr = PicoPatches[i].addr;
if (addr < Pico.romsize)
{
if (PicoPatches[i].active)
{
if (!(PicoIn.AHW & PAHW_SMS))
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
}
else
{
// if current addr is not patched by older patch, write back original val
for (u = 0; u < i; u++)
if (PicoPatches[u].addr == addr) break;
if (u == i)
{
if (!(PicoIn.AHW & PAHW_SMS))
*(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
else
*(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
}
}
// fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
// *(unsigned short *)(Pico.rom + addr));
}
else
{
if (PicoPatches[i].active)
{
if (!(PicoIn.AHW & PAHW_SMS))
m68k_write16(addr,PicoPatches[i].data);
else
;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
}
else
{
// if current addr is not patched by older patch, write back original val
for (u = 0; u < i; u++)
if (PicoPatches[u].addr == addr) break;
if (u == i)
{
if (!(PicoIn.AHW & PAHW_SMS))
m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
else
;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
}
}
}
}
}