patch.c 8.7 KB

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