patch.c 13 KB

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