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" |
25 | #include "patch.h" |
b67ef287 |
26 | |
27 | struct patch |
28 | { |
29 | unsigned int addr; |
30 | unsigned short data; |
31 | }; |
32 | |
33 | struct patch_inst *PicoPatches = NULL; |
34 | int PicoPatchCount = 0; |
35 | |
36 | static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899"; |
37 | |
38 | /* genie_decode |
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. |
45 | * |
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. |
48 | */ |
49 | |
50 | static void genie_decode(const char* code, struct patch* result) |
51 | { |
52 | int i = 0, n; |
53 | char* x; |
54 | |
55 | for(; i < 8; ++i) |
56 | { |
57 | /* If strchr returns NULL, we were given a bad character */ |
58 | if(!(x = strchr(genie_chars, code[i]))) |
59 | { |
60 | result->addr = -1; result->data = -1; |
61 | return; |
62 | } |
63 | n = (x - genie_chars) >> 1; |
64 | /* Now, based on which character this is, fit it into the result */ |
65 | switch(i) |
66 | { |
67 | case 0: |
68 | /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */ |
69 | result->data |= n << 3; |
70 | break; |
71 | case 1: |
72 | /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */ |
73 | result->data |= n >> 2; |
74 | result->addr |= (n & 3) << 14; |
75 | break; |
76 | case 2: |
77 | /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */ |
78 | result->addr |= n << 9; |
79 | break; |
80 | case 3: |
81 | /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */ |
82 | result->addr |= (n & 0xF) << 20 | (n >> 4) << 8; |
83 | break; |
84 | case 4: |
85 | /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */ |
86 | result->data |= (n & 1) << 12; |
87 | result->addr |= (n >> 1) << 16; |
88 | break; |
89 | case 5: |
90 | /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */ |
91 | result->data |= (n & 1) << 15 | (n >> 1) << 8; |
92 | break; |
93 | case 6: |
94 | /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */ |
95 | result->data |= (n >> 3) << 13; |
96 | result->addr |= (n & 7) << 5; |
97 | break; |
98 | case 7: |
99 | /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */ |
100 | result->addr |= n; |
101 | break; |
102 | } |
103 | /* Go around again */ |
104 | } |
105 | return; |
106 | } |
107 | |
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- |
112 | * tuated already. */ |
113 | |
114 | static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf"; |
115 | |
116 | static void hex_decode(const char *code, struct patch *result) |
117 | { |
118 | char *x; |
119 | int i; |
120 | /* 6 digits for address */ |
121 | for(i = 0; i < 6; ++i) |
122 | { |
123 | if(!(x = strchr(hex_chars, code[i]))) |
124 | { |
125 | result->addr = result->data = -1; |
126 | return; |
127 | } |
128 | result->addr = (result->addr << 4) | ((x - hex_chars) >> 1); |
129 | } |
130 | /* 4 digits for data */ |
131 | for(i = 6; i < 10; ++i) |
132 | { |
133 | if(!(x = strchr(hex_chars, code[i]))) |
134 | { |
135 | result->addr = result->data = -1; |
136 | return; |
137 | } |
138 | result->data = (result->data << 4) | ((x - hex_chars) >> 1); |
139 | } |
140 | } |
141 | |
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 |
144 | * decoder. */ |
145 | static void decode(const char* code, struct patch* result) |
146 | { |
147 | int len = strlen(code), i, j; |
148 | char code_to_pass[16], *x; |
149 | const char *ad, *da; |
150 | int adl, dal; |
151 | |
152 | /* Initialize the result */ |
153 | result->addr = result->data = 0; |
154 | |
155 | /* Just assume 8 char long string to be Game Genie code */ |
156 | if (len == 8) |
157 | { |
158 | genie_decode(code, result); |
159 | return; |
160 | } |
161 | |
162 | /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie |
163 | * code. */ |
164 | if(len == 9 && code[4] == '-') |
165 | { |
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); |
177 | return; |
178 | } |
179 | |
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; |
185 | |
186 | /* If a section is empty or too long, toss it */ |
187 | if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code; |
188 | |
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]; |
192 | |
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]; |
196 | |
197 | code_to_pass[10] = '\0'; |
198 | |
199 | /* Decode and goodbye */ |
200 | hex_decode(code_to_pass, result); |
201 | return; |
202 | |
203 | bad_code: |
204 | |
205 | /* AGH! Invalid code! */ |
206 | result->data = result->addr = -1; |
207 | return; |
208 | } |
209 | |
210 | |
211 | |
eff55556 |
212 | unsigned int PicoRead16(unsigned int a); |
b67ef287 |
213 | void PicoWrite16(unsigned int a, unsigned short d); |
214 | |
215 | |
ee2a3bdf |
216 | /* avoid dependency on newer glibc */ |
217 | static int isspace_(int c) |
218 | { |
219 | return (0x09 <= c && c <= 0x0d) || c == ' '; |
220 | } |
221 | |
b67ef287 |
222 | void PicoPatchUnload(void) |
223 | { |
224 | if (PicoPatches != NULL) |
225 | { |
226 | free(PicoPatches); |
227 | PicoPatches = NULL; |
228 | } |
229 | PicoPatchCount = 0; |
230 | } |
231 | |
232 | int PicoPatchLoad(const char *fname) |
233 | { |
234 | FILE *f; |
235 | char buff[256]; |
236 | struct patch pt; |
237 | int array_len = 0; |
238 | |
239 | PicoPatchUnload(); |
240 | |
241 | f = fopen(fname, "r"); |
242 | if (f == NULL) |
243 | { |
244 | return -1; |
245 | } |
246 | |
247 | while (fgets(buff, sizeof(buff), f)) |
248 | { |
249 | int llen, clen; |
250 | |
251 | llen = strlen(buff); |
252 | for (clen = 0; clen < llen; clen++) |
ee2a3bdf |
253 | if (isspace_(buff[clen])) |
254 | break; |
b67ef287 |
255 | buff[clen] = 0; |
256 | |
257 | if (clen > 11 || clen < 8) |
258 | continue; |
259 | |
260 | decode(buff, &pt); |
261 | if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1) |
262 | continue; |
263 | |
264 | /* code was good, add it */ |
265 | if (array_len < PicoPatchCount + 1) |
266 | { |
267 | void *ptr; |
268 | array_len *= 2; |
269 | array_len++; |
270 | ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0])); |
271 | if (ptr == NULL) break; |
272 | PicoPatches = ptr; |
273 | } |
274 | strcpy(PicoPatches[PicoPatchCount].code, buff); |
275 | /* strip */ |
276 | for (clen++; clen < llen; clen++) |
ee2a3bdf |
277 | if (!isspace_(buff[clen])) |
278 | break; |
b67ef287 |
279 | for (llen--; llen > 0; llen--) |
ee2a3bdf |
280 | if (!isspace_(buff[llen])) |
281 | break; |
b67ef287 |
282 | buff[llen+1] = 0; |
283 | strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51); |
284 | PicoPatches[PicoPatchCount].name[51] = 0; |
285 | PicoPatches[PicoPatchCount].active = 0; |
286 | PicoPatches[PicoPatchCount].addr = pt.addr; |
287 | PicoPatches[PicoPatchCount].data = pt.data; |
288 | PicoPatches[PicoPatchCount].data_old = 0; |
289 | PicoPatchCount++; |
290 | // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data, |
291 | // PicoPatches[PicoPatchCount-1].name); |
292 | } |
293 | fclose(f); |
294 | |
295 | return 0; |
296 | } |
297 | |
298 | /* to be called when the Rom is loaded and byteswapped */ |
299 | void PicoPatchPrepare(void) |
300 | { |
301 | int i; |
302 | |
303 | for (i = 0; i < PicoPatchCount; i++) |
304 | { |
305 | PicoPatches[i].addr &= ~1; |
306 | PicoPatches[i].data_old = PicoRead16(PicoPatches[i].addr); |
307 | if (strstr(PicoPatches[i].name, "AUTO")) |
308 | PicoPatches[i].active = 1; |
309 | } |
310 | } |
311 | |
312 | void PicoPatchApply(void) |
313 | { |
314 | int i, u; |
315 | unsigned int addr; |
316 | |
317 | for (i = 0; i < PicoPatchCount; i++) |
318 | { |
319 | addr = PicoPatches[i].addr; |
320 | if (addr < Pico.romsize) |
321 | { |
322 | if (PicoPatches[i].active) |
323 | *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data; |
324 | else { |
325 | // if current addr is not patched by older patch, write back original val |
326 | for (u = 0; u < i; u++) |
327 | if (PicoPatches[u].addr == addr) break; |
328 | if (u == i) |
329 | *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old; |
330 | } |
331 | // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr, |
332 | // *(unsigned short *)(Pico.rom + addr)); |
333 | } |
334 | else |
335 | { |
336 | /* RAM or some other weird patch */ |
337 | if (PicoPatches[i].active) |
338 | PicoWrite16(addr, PicoPatches[i].data); |
339 | } |
340 | } |
341 | } |
342 | |