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 :)
27 static char genie_chars
[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
30 * This function converts a Game Genie code to an address:data pair.
31 * The code is given as an 8-character string, like "BJX0SA1C". It need not
32 * be null terminated, since only the first 8 characters are taken. It is
33 * assumed that the code is already made of valid characters, i.e. there are no
34 * Q's, U's, or symbols. If such a character is
35 * encountered, the function will return with a warning on stderr.
37 * The resulting address:data pair is returned in the struct patch pointed to
38 * by result. If an error results, both the address and data will be set to -1.
41 static void genie_decode(const char* code
, struct patch
* result
)
48 /* If strchr returns NULL, we were given a bad character */
49 if(!(x
= strchr(genie_chars
, code
[i
])))
51 result
->addr
= -1; result
->data
= -1;
54 n
= (x
- genie_chars
) >> 1;
55 /* Now, based on which character this is, fit it into the result */
59 /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
60 result
->data
|= n
<< 3;
63 /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
64 result
->data
|= n
>> 2;
65 result
->addr
|= (n
& 3) << 14;
68 /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
69 result
->addr
|= n
<< 9;
72 /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
73 result
->addr
|= (n
& 0xF) << 20 | (n
>> 4) << 8;
76 /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
77 result
->data
|= (n
& 1) << 12;
78 result
->addr
|= (n
>> 1) << 16;
81 /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
82 result
->data
|= (n
& 1) << 15 | (n
>> 1) << 8;
85 /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
86 result
->data
|= (n
>> 3) << 13;
87 result
->addr
|= (n
& 7) << 5;
90 /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
99 /* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
100 * type codes. You're more likely to find Genie codes circulating around, but
101 * there's a chance you could come on to one of these. Which is nice, since
102 * they're MUCH easier to implement ;) Once again, the input should be depunc-
105 static char hex_chars
[] = "00112233445566778899AaBbCcDdEeFf";
107 static void hex_decode(const char *code
, struct patch
*result
)
111 /* 6 digits for address */
112 for(i
= 0; i
< 6; ++i
)
114 if(!(x
= strchr(hex_chars
, code
[i
])))
116 result
->addr
= result
->data
= -1;
119 result
->addr
= (result
->addr
<< 4) | ((x
- hex_chars
) >> 1);
121 /* 4 digits for data */
122 for(i
= 6; i
< 10; ++i
)
124 if(!(x
= strchr(hex_chars
, code
[i
])))
126 result
->addr
= result
->data
= -1;
129 result
->data
= (result
->data
<< 4) | ((x
- hex_chars
) >> 1);
133 /* THIS is the function you call from the MegaDrive or whatever. This figures
134 * out whether it's a genie or hex code, depunctuates it, and calls the proper
136 void decode(const char* code
, struct patch
* result
)
138 int len
= strlen(code
), i
, j
;
139 char code_to_pass
[16], *x
;
143 /* Initialize the result */
144 result
->addr
= result
->data
= 0;
146 /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
148 if(len
== 9 && code
[4] == '-')
150 /* Remove the hyphen and pass to genie_decode */
151 code_to_pass
[0] = code
[0];
152 code_to_pass
[1] = code
[1];
153 code_to_pass
[2] = code
[2];
154 code_to_pass
[3] = code
[3];
155 code_to_pass
[4] = code
[5];
156 code_to_pass
[5] = code
[6];
157 code_to_pass
[6] = code
[7];
158 code_to_pass
[7] = code
[8];
159 code_to_pass
[8] = '\0';
160 genie_decode(code_to_pass
, result
);
163 /* Otherwise, we assume it's a hex code.
164 * Find the colon so we know where address ends and data starts. If there's
165 * no colon, then we haven't a code at all! */
166 if(!(x
= strchr(code
, ':'))) goto bad_code
;
167 ad
= code
; da
= x
+ 1; adl
= x
- code
; dal
= len
- adl
- 1;
168 /* If a section is empty or too long, toss it */
169 if(adl
== 0 || adl
> 6 || dal
== 0 || dal
> 4) goto bad_code
;
170 /* Pad the address with zeros, then fill it with the value */
171 for(i
= 0; i
< (6 - adl
); ++i
) code_to_pass
[i
] = '0';
172 for(j
= 0; i
< 6; ++i
, ++j
) code_to_pass
[i
] = ad
[j
];
173 /* Do the same for data */
174 for(i
= 6; i
< (10 - dal
); ++i
) code_to_pass
[i
] = '0';
175 for(j
= 0; i
< 10; ++i
, ++j
) code_to_pass
[i
] = da
[j
];
176 code_to_pass
[10] = '\0';
177 /* Decode and goodbye */
178 hex_decode(code_to_pass
, result
);
182 /* AGH! Invalid code! */
183 result
->data
= result
->addr
= -1;