patch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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[8]){
  166. for(i=8;i<11;++i)
  167. {
  168. if (i==9) continue; /* 2nd character is ignored */
  169. if(!(x = strchr(hex_chars, code[i])))
  170. {
  171. result->addr = result->data = -1;
  172. return;
  173. }
  174. result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
  175. }
  176. /* Correct the comp */
  177. result->comp = ((result->comp >> 2) | ((result->comp << 6) & 0xC0)) ^ 0xBA;
  178. }
  179. }
  180. void ar_decode_ms(const char *code, struct patch *result){
  181. char *x;
  182. int i;
  183. /* 2 digits of padding*/
  184. /* 4 digits for address */
  185. for(i=2;i<7;++i)
  186. {
  187. /* 5th character is hyphen and can be skipped*/
  188. if (i==4) continue;
  189. if(!(x = strchr(hex_chars, code[i])))
  190. {
  191. result->addr = result->data = -1;
  192. return;
  193. }
  194. result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
  195. }
  196. /* 2 digits for data */
  197. for(i=7;i<9;++i)
  198. {
  199. if(!(x = strchr(hex_chars, code[i])))
  200. {
  201. result->addr = result->data = -1;
  202. return;
  203. }
  204. result->data = (result->data << 4) | ((x - hex_chars) >> 1);
  205. }
  206. }
  207. void fusion_ram_decode(const char *code, struct patch *result){
  208. char *x;
  209. int i;
  210. /* 4 digits for address */
  211. for(i=0;i<4;++i)
  212. {
  213. if(!(x = strchr(hex_chars, code[i])))
  214. {
  215. result->addr = result->data = -1;
  216. return;
  217. }
  218. result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
  219. }
  220. /* Skip the ':' */
  221. /* 2 digits for data */
  222. for(i=5;i<7;++i)
  223. {
  224. if(!(x = strchr(hex_chars, code[i])))
  225. {
  226. result->addr = result->data = -1;
  227. return;
  228. }
  229. result->data = (result->data << 4) | ((x - hex_chars) >> 1);
  230. }
  231. }
  232. void fusion_rom_decode(const char *code, struct patch *result){
  233. char *x;
  234. int i;
  235. /* 2 digits for comp */
  236. for(i=0;i<2;++i)
  237. {
  238. if(!(x = strchr(hex_chars, code[i])))
  239. {
  240. result->addr = result->data = -1;
  241. return;
  242. }
  243. result->comp = (result->comp << 4) | ((x - hex_chars) >> 1);
  244. }
  245. /* 4 digits for address */
  246. for(i=2;i<6;++i)
  247. {
  248. if(!(x = strchr(hex_chars, code[i])))
  249. {
  250. result->addr = result->data = -1;
  251. return;
  252. }
  253. result->addr = (result->addr << 4) | ((x - hex_chars) >> 1);
  254. }
  255. /* 2 digits for data */
  256. for(i=7;i<9;++i)
  257. {
  258. if(!(x = strchr(hex_chars, code[i])))
  259. {
  260. result->addr = result->data = -1;
  261. return;
  262. }
  263. result->data = (result->data << 4) | ((x - hex_chars) >> 1);
  264. }
  265. }
  266. /* THIS is the function you call from the MegaDrive or whatever. This figures
  267. * out whether it's a genie or hex code, depunctuates it, and calls the proper
  268. * decoder. */
  269. void decode(const char* code, struct patch* result)
  270. {
  271. int len = strlen(code);
  272. /* Initialize the result */
  273. result->addr = result->data = result->comp = 0;
  274. if(!(PicoAHW & PAHW_SMS))
  275. {
  276. //If Genesis
  277. //Game Genie
  278. if(len == 9 && code[4] == '-')
  279. {
  280. genie_decode_md(code, result);
  281. return;
  282. }
  283. //Master
  284. else if(len >=9 && code[6] == ':')
  285. {
  286. hex_decode_md(code, result);
  287. }
  288. else
  289. {
  290. goto bad_code;
  291. }
  292. } else {
  293. //If Master System
  294. //Genie
  295. if(len == 11 && code[3] == '-' && code[7] == '-')
  296. {
  297. genie_decode_ms(code, result);
  298. }
  299. //AR
  300. else if(len == 9 && code[4] == '-')
  301. {
  302. ar_decode_ms(code, result);
  303. }
  304. //Fusion RAM
  305. else if(len == 7 && code[4] == ':')
  306. {
  307. fusion_ram_decode(code, result);
  308. }
  309. //Fusion ROM
  310. else if(len == 9 && code[6] == ':')
  311. {
  312. fusion_rom_decode(code, result);
  313. }
  314. else
  315. {
  316. goto bad_code;
  317. }
  318. //Convert RAM address space to Genesis location.
  319. if (result->addr>=0xC000)
  320. result->addr= 0xFF0000 | (0x1FFF & result->addr);
  321. }
  322. return;
  323. bad_code:
  324. result->data = result->addr = -1;
  325. return;
  326. }
  327. void PicoPatchUnload(void)
  328. {
  329. if (PicoPatches != NULL)
  330. {
  331. free(PicoPatches);
  332. PicoPatches = NULL;
  333. }
  334. PicoPatchCount = 0;
  335. }
  336. int PicoPatchLoad(const char *fname)
  337. {
  338. FILE *f;
  339. char buff[256];
  340. struct patch pt;
  341. int array_len = 0;
  342. PicoPatchUnload();
  343. f = fopen(fname, "r");
  344. if (f == NULL)
  345. {
  346. return -1;
  347. }
  348. while (fgets(buff, sizeof(buff), f))
  349. {
  350. int llen, clen;
  351. llen = strlen(buff);
  352. for (clen = 0; clen < llen; clen++)
  353. if (isspace_(buff[clen]))
  354. break;
  355. buff[clen] = 0;
  356. if (clen > 11 || clen < 8)
  357. continue;
  358. decode(buff, &pt);
  359. if (pt.addr == (unsigned int)-1 || pt.data == (unsigned short)-1)
  360. continue;
  361. /* code was good, add it */
  362. if (array_len < PicoPatchCount + 1)
  363. {
  364. void *ptr;
  365. array_len *= 2;
  366. array_len++;
  367. ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
  368. if (ptr == NULL) break;
  369. PicoPatches = ptr;
  370. }
  371. strcpy(PicoPatches[PicoPatchCount].code, buff);
  372. /* strip */
  373. for (clen++; clen < llen; clen++)
  374. if (!isspace_(buff[clen]))
  375. break;
  376. for (llen--; llen > 0; llen--)
  377. if (!isspace_(buff[llen]))
  378. break;
  379. buff[llen+1] = 0;
  380. strncpy(PicoPatches[PicoPatchCount].name, buff + clen, 51);
  381. PicoPatches[PicoPatchCount].name[51] = 0;
  382. PicoPatches[PicoPatchCount].active = 0;
  383. PicoPatches[PicoPatchCount].addr = pt.addr;
  384. PicoPatches[PicoPatchCount].data = pt.data;
  385. PicoPatches[PicoPatchCount].data_old = 0;
  386. PicoPatchCount++;
  387. // fprintf(stderr, "loaded patch #%i: %06x:%04x \"%s\"\n", PicoPatchCount-1, pt.addr, pt.data,
  388. // PicoPatches[PicoPatchCount-1].name);
  389. }
  390. fclose(f);
  391. return 0;
  392. }
  393. /* to be called when the Rom is loaded and byteswapped */
  394. void PicoPatchPrepare(void)
  395. {
  396. int i;
  397. int addr;
  398. for (i = 0; i < PicoPatchCount; i++)
  399. {
  400. addr=PicoPatches[i].addr;
  401. addr &= ~1;
  402. if (addr < Pico.romsize)
  403. PicoPatches[i].data_old = *(unsigned short *)(Pico.rom + addr);
  404. else
  405. {
  406. if(!(PicoAHW & PAHW_SMS))
  407. PicoPatches[i].data_old = (unsigned short) m68k_read16(addr);
  408. else
  409. ; // wrong: PicoPatches[i].data_old = (unsigned char) PicoRead8_z80(addr);
  410. }
  411. if (strstr(PicoPatches[i].name, "AUTO"))
  412. PicoPatches[i].active = 1;
  413. }
  414. }
  415. void PicoPatchApply(void)
  416. {
  417. int i, u;
  418. unsigned int addr;
  419. for (i = 0; i < PicoPatchCount; i++)
  420. {
  421. addr = PicoPatches[i].addr;
  422. if (addr < Pico.romsize)
  423. {
  424. if (PicoPatches[i].active)
  425. {
  426. if (!(PicoAHW & PAHW_SMS))
  427. *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data;
  428. else if (!PicoPatches[i].comp || PicoPatches[i].comp == *(char *)(Pico.rom + addr))
  429. *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data;
  430. }
  431. else
  432. {
  433. // if current addr is not patched by older patch, write back original val
  434. for (u = 0; u < i; u++)
  435. if (PicoPatches[u].addr == addr) break;
  436. if (u == i)
  437. {
  438. if (!(PicoAHW & PAHW_SMS))
  439. *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
  440. else
  441. *(char *)(Pico.rom + addr) = (char) PicoPatches[i].data_old;
  442. }
  443. }
  444. // fprintf(stderr, "patched %i: %06x:%04x\n", PicoPatches[i].active, addr,
  445. // *(unsigned short *)(Pico.rom + addr));
  446. }
  447. else
  448. {
  449. if (PicoPatches[i].active)
  450. {
  451. if (!(PicoAHW & PAHW_SMS))
  452. m68k_write16(addr,PicoPatches[i].data);
  453. else
  454. ;// wrong: PicoWrite8_z80(addr,PicoPatches[i].data);
  455. }
  456. else
  457. {
  458. // if current addr is not patched by older patch, write back original val
  459. for (u = 0; u < i; u++)
  460. if (PicoPatches[u].addr == addr) break;
  461. if (u == i)
  462. {
  463. if (!(PicoAHW & PAHW_SMS))
  464. m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
  465. else
  466. ;// wrong: PicoWrite8_z80(PicoPatches[i].addr,PicoPatches[i].data_old);
  467. }
  468. }
  469. }
  470. }
  471. }