patch.c 13 KB

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