Patch.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. //#include <stdio.h>
  24. //#include <string.h>
  25. #include <ctype.h>
  26. #include "PicoInt.h"
  27. #include "Patch.h"
  28. struct patch
  29. {
  30. unsigned int addr;
  31. unsigned short data;
  32. };
  33. struct patch_inst *PicoPatches = NULL;
  34. int PicoPatchCount = 0;
  35. static char genie_chars[] = "AaBbCcDdEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz0O1I2233445566778899";
  36. /* genie_decode
  37. * This function converts a Game Genie code to an address:data pair.
  38. * The code is given as an 8-character string, like "BJX0SA1C". It need not
  39. * be null terminated, since only the first 8 characters are taken. It is
  40. * assumed that the code is already made of valid characters, i.e. there are no
  41. * Q's, U's, or symbols. If such a character is
  42. * encountered, the function will return with a warning on stderr.
  43. *
  44. * The resulting address:data pair is returned in the struct patch pointed to
  45. * by result. If an error results, both the address and data will be set to -1.
  46. */
  47. static void genie_decode(const char* code, struct patch* result)
  48. {
  49. int i = 0, n;
  50. char* x;
  51. for(; i < 8; ++i)
  52. {
  53. /* If strchr returns NULL, we were given a bad character */
  54. if(!(x = strchr(genie_chars, code[i])))
  55. {
  56. result->addr = -1; result->data = -1;
  57. return;
  58. }
  59. n = (x - genie_chars) >> 1;
  60. /* Now, based on which character this is, fit it into the result */
  61. switch(i)
  62. {
  63. case 0:
  64. /* ____ ____ ____ ____ ____ ____ : ____ ____ ABCD E___ */
  65. result->data |= n << 3;
  66. break;
  67. case 1:
  68. /* ____ ____ DE__ ____ ____ ____ : ____ ____ ____ _ABC */
  69. result->data |= n >> 2;
  70. result->addr |= (n & 3) << 14;
  71. break;
  72. case 2:
  73. /* ____ ____ __AB CDE_ ____ ____ : ____ ____ ____ ____ */
  74. result->addr |= n << 9;
  75. break;
  76. case 3:
  77. /* BCDE ____ ____ ___A ____ ____ : ____ ____ ____ ____ */
  78. result->addr |= (n & 0xF) << 20 | (n >> 4) << 8;
  79. break;
  80. case 4:
  81. /* ____ ABCD ____ ____ ____ ____ : ___E ____ ____ ____ */
  82. result->data |= (n & 1) << 12;
  83. result->addr |= (n >> 1) << 16;
  84. break;
  85. case 5:
  86. /* ____ ____ ____ ____ ____ ____ : E___ ABCD ____ ____ */
  87. result->data |= (n & 1) << 15 | (n >> 1) << 8;
  88. break;
  89. case 6:
  90. /* ____ ____ ____ ____ CDE_ ____ : _AB_ ____ ____ ____ */
  91. result->data |= (n >> 3) << 13;
  92. result->addr |= (n & 7) << 5;
  93. break;
  94. case 7:
  95. /* ____ ____ ____ ____ ___A BCDE : ____ ____ ____ ____ */
  96. result->addr |= n;
  97. break;
  98. }
  99. /* Go around again */
  100. }
  101. return;
  102. }
  103. /* "Decode" an address/data pair into a structure. This is for "012345:ABCD"
  104. * type codes. You're more likely to find Genie codes circulating around, but
  105. * there's a chance you could come on to one of these. Which is nice, since
  106. * they're MUCH easier to implement ;) Once again, the input should be depunc-
  107. * tuated already. */
  108. static char hex_chars[] = "00112233445566778899AaBbCcDdEeFf";
  109. static void hex_decode(const char *code, struct patch *result)
  110. {
  111. char *x;
  112. int i;
  113. /* 6 digits for address */
  114. for(i = 0; i < 6; ++i)
  115. {
  116. if(!(x = strchr(hex_chars, code[i])))
  117. {
  118. result->addr = result->data = -1;
  119. return;
  120. }
  121. result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
  122. }
  123. /* 4 digits for data */
  124. for(i = 6; i < 10; ++i)
  125. {
  126. if(!(x = strchr(hex_chars, code[i])))
  127. {
  128. result->addr = result->data = -1;
  129. return;
  130. }
  131. result->data = (result->data << 4) | ((x - hex_chars) >> 1);
  132. }
  133. }
  134. /* THIS is the function you call from the MegaDrive or whatever. This figures
  135. * out whether it's a genie or hex code, depunctuates it, and calls the proper
  136. * decoder. */
  137. static void decode(const char* code, struct patch* result)
  138. {
  139. int len = strlen(code), i, j;
  140. char code_to_pass[16], *x;
  141. const char *ad, *da;
  142. int adl, dal;
  143. /* Initialize the result */
  144. result->addr = result->data = 0;
  145. /* Just assume 8 char long string to be Game Genie code */
  146. if (len == 8)
  147. {
  148. genie_decode(code, result);
  149. return;
  150. }
  151. /* If it's 9 chars long and the 5th is a hyphen, we have a Game Genie
  152. * code. */
  153. if(len == 9 && code[4] == '-')
  154. {
  155. /* Remove the hyphen and pass to genie_decode */
  156. code_to_pass[0] = code[0];
  157. code_to_pass[1] = code[1];
  158. code_to_pass[2] = code[2];
  159. code_to_pass[3] = code[3];
  160. code_to_pass[4] = code[5];
  161. code_to_pass[5] = code[6];
  162. code_to_pass[6] = code[7];
  163. code_to_pass[7] = code[8];
  164. code_to_pass[8] = '\0';
  165. genie_decode(code_to_pass, result);
  166. return;
  167. }
  168. /* Otherwise, we assume it's a hex code.
  169. * Find the colon so we know where address ends and data starts. If there's
  170. * no colon, then we haven't a code at all! */
  171. if(!(x = strchr(code, ':'))) goto bad_code;
  172. ad = code; da = x + 1; adl = x - code; dal = len - adl - 1;
  173. /* If a section is empty or too long, toss it */
  174. if(adl == 0 || adl > 6 || dal == 0 || dal > 4) goto bad_code;
  175. /* Pad the address with zeros, then fill it with the value */
  176. for(i = 0; i < (6 - adl); ++i) code_to_pass[i] = '0';
  177. for(j = 0; i < 6; ++i, ++j) code_to_pass[i] = ad[j];
  178. /* Do the same for data */
  179. for(i = 6; i < (10 - dal); ++i) code_to_pass[i] = '0';
  180. for(j = 0; i < 10; ++i, ++j) code_to_pass[i] = da[j];
  181. code_to_pass[10] = '\0';
  182. /* Decode and goodbye */
  183. hex_decode(code_to_pass, result);
  184. return;
  185. bad_code:
  186. /* AGH! Invalid code! */
  187. result->data = result->addr = -1;
  188. return;
  189. }
  190. unsigned int PicoRead16(unsigned int a);
  191. void PicoWrite16(unsigned int a, unsigned short d);
  192. void PicoPatchUnload(void)
  193. {
  194. if (PicoPatches != NULL)
  195. {
  196. free(PicoPatches);
  197. PicoPatches = NULL;
  198. }
  199. PicoPatchCount = 0;
  200. }
  201. int PicoPatchLoad(const char *fname)
  202. {
  203. FILE *f;
  204. char buff[256];
  205. struct patch pt;
  206. int array_len = 0;
  207. PicoPatchUnload();
  208. f = fopen(fname, "r");
  209. if (f == NULL)
  210. {
  211. return -1;
  212. }
  213. while (fgets(buff, sizeof(buff), f))
  214. {
  215. int llen, clen;
  216. llen = strlen(buff);
  217. for (clen = 0; clen < llen; clen++)
  218. if (isspace(buff[clen])) break;
  219. buff[clen] = 0;
  220. if (clen > 11 || clen < 8)
  221. continue;
  222. decode(buff, &pt);
  223. if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
  224. continue;
  225. /* code was good, add it */
  226. if (array_len < PicoPatchCount + 1)
  227. {
  228. void *ptr;
  229. array_len *= 2;
  230. array_len++;
  231. ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
  232. if (ptr == NULL) break;
  233. PicoPatches = ptr;
  234. }
  235. strcpy(PicoPatches[PicoPatchCount].code, buff);
  236. /* strip */
  237. for (clen++; clen < llen; clen++)
  238. if (!isspace(buff[clen])) break;
  239. for (llen--; llen > 0; llen--)
  240. if (!isspace(buff[llen])) break;
  241. buff[llen+1] = 0;
  242. strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
  243. PicoPatches[PicoPatchCount].name[51] = 0;
  244. PicoPatches[PicoPatchCount].active = 0;
  245. PicoPatches[PicoPatchCount].addr = pt.addr;
  246. PicoPatches[PicoPatchCount].data = pt.data;
  247. PicoPatches[PicoPatchCount].data_old = 0;
  248. PicoPatchCount++;
  249. // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
  250. // PicoPatches[PicoPatchCount-1].name);
  251. }
  252. fclose(f);
  253. return 0;
  254. }
  255. /* to be called when the Rom is loaded and byteswapped */
  256. void PicoPatchPrepare(void)
  257. {
  258. int i;
  259. for (i = 0; i < PicoPatchCount; i++)
  260. {
  261. PicoPatches[i].addr &= ~1;
  262. PicoPatches[i].data_old = PicoRead16(PicoPatches[i].addr);
  263. if (strstr(PicoPatches[i].name, "AUTO"))
  264. PicoPatches[i].active = 1;
  265. }
  266. }
  267. void PicoPatchApply(void)
  268. {
  269. int i, u;
  270. unsigned int addr;
  271. for (i = 0; i < PicoPatchCount; i++)
  272. {
  273. addr = PicoPatches[i].addr;
  274. if (addr < Pico.romsize)
  275. {
  276. if (PicoPatches[i].active)
  277. *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
  278. else {
  279. // if current addr is not patched by older patch, write back original val
  280. for (u = 0; u < i; u++)
  281. if (PicoPatches[u].addr == addr) break;
  282. if (u == i)
  283. *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
  284. }
  285. // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
  286. // *(unsigned short *)(Pico.rom + addr));
  287. }
  288. else
  289. {
  290. /* RAM or some other weird patch */
  291. if (PicoPatches[i].active)
  292. PicoWrite16(addr, PicoPatches[i].data);
  293. }
  294. }
  295. }