cart.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * PicoDrive
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2010
  5. *
  6. * This work is licensed under the terms of MAME license.
  7. * See COPYING file in the top-level directory.
  8. */
  9. #include "pico_int.h"
  10. #include "../zlib/zlib.h"
  11. #include "../cpu/debug.h"
  12. #include "../unzip/unzip.h"
  13. #include "../unzip/unzip_stream.h"
  14. static int rom_alloc_size;
  15. static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };
  16. void (*PicoCartUnloadHook)(void);
  17. void (*PicoCartMemSetup)(void);
  18. void (*PicoCartLoadProgressCB)(int percent) = NULL;
  19. void (*PicoCDLoadProgressCB)(const char *fname, int percent) = NULL; // handled in Pico/cd/cd_file.c
  20. int PicoGameLoaded;
  21. static void PicoCartDetect(const char *carthw_cfg);
  22. /* cso struct */
  23. typedef struct _cso_struct
  24. {
  25. unsigned char in_buff[2*2048];
  26. unsigned char out_buff[2048];
  27. struct {
  28. char magic[4];
  29. unsigned int unused;
  30. unsigned int total_bytes;
  31. unsigned int total_bytes_high; // ignored here
  32. unsigned int block_size; // 10h
  33. unsigned char ver;
  34. unsigned char align;
  35. unsigned char reserved[2];
  36. } header;
  37. unsigned int fpos_in; // input file read pointer
  38. unsigned int fpos_out; // pos in virtual decompressed file
  39. int block_in_buff; // block which we have read in in_buff
  40. int pad;
  41. int index[0];
  42. }
  43. cso_struct;
  44. static int uncompress2(void *dest, int destLen, void *source, int sourceLen)
  45. {
  46. z_stream stream;
  47. int err;
  48. stream.next_in = (Bytef*)source;
  49. stream.avail_in = (uInt)sourceLen;
  50. stream.next_out = dest;
  51. stream.avail_out = (uInt)destLen;
  52. stream.zalloc = NULL;
  53. stream.zfree = NULL;
  54. err = inflateInit2(&stream, -15);
  55. if (err != Z_OK) return err;
  56. err = inflate(&stream, Z_FINISH);
  57. if (err != Z_STREAM_END) {
  58. inflateEnd(&stream);
  59. return err;
  60. }
  61. //*destLen = stream.total_out;
  62. return inflateEnd(&stream);
  63. }
  64. static const char *get_ext(const char *path)
  65. {
  66. const char *ext;
  67. if (strlen(path) < 4)
  68. return ""; // no ext
  69. // allow 2 or 3 char extensions for now
  70. ext = path + strlen(path) - 2;
  71. if (ext[-1] != '.') ext--;
  72. if (ext[-1] != '.')
  73. return "";
  74. return ext;
  75. }
  76. pm_file *pm_open(const char *path)
  77. {
  78. pm_file *file = NULL;
  79. const char *ext;
  80. FILE *f;
  81. if (path == NULL)
  82. return NULL;
  83. ext = get_ext(path);
  84. if (strcasecmp(ext, "zip") == 0)
  85. {
  86. struct zipent *zipentry;
  87. gzFile gzf = NULL;
  88. ZIP *zipfile;
  89. int i;
  90. zipfile = openzip(path);
  91. if (zipfile != NULL)
  92. {
  93. /* search for suitable file (right extension or large enough file) */
  94. while ((zipentry = readzip(zipfile)) != NULL)
  95. {
  96. ext = get_ext(zipentry->name);
  97. if (zipentry->uncompressed_size >= 32*1024)
  98. goto found_rom_zip;
  99. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  100. if (strcasecmp(ext, rom_exts[i]) == 0)
  101. goto found_rom_zip;
  102. }
  103. /* zipfile given, but nothing found suitable for us inside */
  104. goto zip_failed;
  105. found_rom_zip:
  106. /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
  107. gzf = zip2gz(zipfile, zipentry);
  108. if (gzf == NULL) goto zip_failed;
  109. file = calloc(1, sizeof(*file));
  110. if (file == NULL) goto zip_failed;
  111. file->file = zipfile;
  112. file->param = gzf;
  113. file->size = zipentry->uncompressed_size;
  114. file->type = PMT_ZIP;
  115. strncpy(file->ext, ext, sizeof(file->ext) - 1);
  116. return file;
  117. zip_failed:
  118. if (gzf) {
  119. gzclose(gzf);
  120. zipfile->fp = NULL; // gzclose() closed it
  121. }
  122. closezip(zipfile);
  123. return NULL;
  124. }
  125. }
  126. else if (strcasecmp(ext, "cso") == 0)
  127. {
  128. cso_struct *cso = NULL, *tmp = NULL;
  129. int size;
  130. f = fopen(path, "rb");
  131. if (f == NULL)
  132. goto cso_failed;
  133. #ifdef __GP2X__
  134. /* we use our own buffering */
  135. setvbuf(f, NULL, _IONBF, 0);
  136. #endif
  137. cso = malloc(sizeof(*cso));
  138. if (cso == NULL)
  139. goto cso_failed;
  140. if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
  141. goto cso_failed;
  142. if (strncmp(cso->header.magic, "CISO", 4) != 0) {
  143. elprintf(EL_STATUS, "cso: bad header");
  144. goto cso_failed;
  145. }
  146. if (cso->header.block_size != 2048) {
  147. elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
  148. goto cso_failed;
  149. }
  150. size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
  151. tmp = realloc(cso, size);
  152. if (tmp == NULL)
  153. goto cso_failed;
  154. cso = tmp;
  155. elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
  156. size -= sizeof(*cso); // index size
  157. if (fread(cso->index, 1, size, f) != size) {
  158. elprintf(EL_STATUS, "cso: premature EOF");
  159. goto cso_failed;
  160. }
  161. // all ok
  162. cso->fpos_in = ftell(f);
  163. cso->fpos_out = 0;
  164. cso->block_in_buff = -1;
  165. file = calloc(1, sizeof(*file));
  166. if (file == NULL) goto cso_failed;
  167. file->file = f;
  168. file->param = cso;
  169. file->size = cso->header.total_bytes;
  170. file->type = PMT_CSO;
  171. return file;
  172. cso_failed:
  173. if (cso != NULL) free(cso);
  174. if (f != NULL) fclose(f);
  175. return NULL;
  176. }
  177. /* not a zip, treat as uncompressed file */
  178. f = fopen(path, "rb");
  179. if (f == NULL) return NULL;
  180. file = calloc(1, sizeof(*file));
  181. if (file == NULL) {
  182. fclose(f);
  183. return NULL;
  184. }
  185. fseek(f, 0, SEEK_END);
  186. file->file = f;
  187. file->param = NULL;
  188. file->size = ftell(f);
  189. file->type = PMT_UNCOMPRESSED;
  190. strncpy(file->ext, ext, sizeof(file->ext) - 1);
  191. fseek(f, 0, SEEK_SET);
  192. #ifdef __GP2X__
  193. if (file->size > 0x400000)
  194. /* we use our own buffering */
  195. setvbuf(f, NULL, _IONBF, 0);
  196. #endif
  197. return file;
  198. }
  199. size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
  200. {
  201. int ret;
  202. if (stream->type == PMT_UNCOMPRESSED)
  203. {
  204. ret = fread(ptr, 1, bytes, stream->file);
  205. }
  206. else if (stream->type == PMT_ZIP)
  207. {
  208. gzFile gf = stream->param;
  209. int err;
  210. ret = gzread(gf, ptr, bytes);
  211. err = gzerror2(gf);
  212. if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
  213. /* we must reset stream pointer or else next seek/read fails */
  214. gzrewind(gf);
  215. }
  216. else if (stream->type == PMT_CSO)
  217. {
  218. cso_struct *cso = stream->param;
  219. int read_pos, read_len, out_offs, rret;
  220. int block = cso->fpos_out >> 11;
  221. int index = cso->index[block];
  222. int index_end = cso->index[block+1];
  223. unsigned char *out = ptr, *tmp_dst;
  224. ret = 0;
  225. while (bytes != 0)
  226. {
  227. out_offs = cso->fpos_out&0x7ff;
  228. if (out_offs == 0 && bytes >= 2048)
  229. tmp_dst = out;
  230. else tmp_dst = cso->out_buff;
  231. read_pos = (index&0x7fffffff) << cso->header.align;
  232. if (index < 0) {
  233. if (read_pos != cso->fpos_in)
  234. fseek(stream->file, read_pos, SEEK_SET);
  235. rret = fread(tmp_dst, 1, 2048, stream->file);
  236. cso->fpos_in = read_pos + rret;
  237. if (rret != 2048) break;
  238. } else {
  239. read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
  240. if (block != cso->block_in_buff)
  241. {
  242. if (read_pos != cso->fpos_in)
  243. fseek(stream->file, read_pos, SEEK_SET);
  244. rret = fread(cso->in_buff, 1, read_len, stream->file);
  245. cso->fpos_in = read_pos + rret;
  246. if (rret != read_len) {
  247. elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);
  248. break;
  249. }
  250. cso->block_in_buff = block;
  251. }
  252. rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);
  253. if (rret != 0) {
  254. elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
  255. break;
  256. }
  257. }
  258. rret = 2048;
  259. if (out_offs != 0 || bytes < 2048) {
  260. //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);
  261. if (bytes < rret) rret = bytes;
  262. if (2048 - out_offs < rret) rret = 2048 - out_offs;
  263. memcpy(out, tmp_dst + out_offs, rret);
  264. }
  265. ret += rret;
  266. out += rret;
  267. cso->fpos_out += rret;
  268. bytes -= rret;
  269. block++;
  270. index = index_end;
  271. index_end = cso->index[block+1];
  272. }
  273. }
  274. else
  275. ret = 0;
  276. return ret;
  277. }
  278. int pm_seek(pm_file *stream, long offset, int whence)
  279. {
  280. if (stream->type == PMT_UNCOMPRESSED)
  281. {
  282. fseek(stream->file, offset, whence);
  283. return ftell(stream->file);
  284. }
  285. else if (stream->type == PMT_ZIP)
  286. {
  287. if (PicoMessage != NULL && offset > 6*1024*1024) {
  288. long pos = gztell((gzFile) stream->param);
  289. if (offset < pos || offset - pos > 6*1024*1024)
  290. PicoMessage("Decompressing data...");
  291. }
  292. return gzseek((gzFile) stream->param, offset, whence);
  293. }
  294. else if (stream->type == PMT_CSO)
  295. {
  296. cso_struct *cso = stream->param;
  297. switch (whence)
  298. {
  299. case SEEK_CUR: cso->fpos_out += offset; break;
  300. case SEEK_SET: cso->fpos_out = offset; break;
  301. case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;
  302. }
  303. return cso->fpos_out;
  304. }
  305. else
  306. return -1;
  307. }
  308. int pm_close(pm_file *fp)
  309. {
  310. int ret = 0;
  311. if (fp == NULL) return EOF;
  312. if (fp->type == PMT_UNCOMPRESSED)
  313. {
  314. fclose(fp->file);
  315. }
  316. else if (fp->type == PMT_ZIP)
  317. {
  318. ZIP *zipfile = fp->file;
  319. gzclose((gzFile) fp->param);
  320. zipfile->fp = NULL; // gzclose() closed it
  321. closezip(zipfile);
  322. }
  323. else if (fp->type == PMT_CSO)
  324. {
  325. free(fp->param);
  326. fclose(fp->file);
  327. }
  328. else
  329. ret = EOF;
  330. free(fp);
  331. return ret;
  332. }
  333. // byteswap, data needs to be int aligned, src can match dst
  334. void Byteswap(void *dst, const void *src, int len)
  335. {
  336. const unsigned int *ps = src;
  337. unsigned int *pd = dst;
  338. int i, m;
  339. if (len < 2)
  340. return;
  341. m = 0x00ff00ff;
  342. for (i = 0; i < len / 4; i++) {
  343. unsigned int t = ps[i];
  344. pd[i] = ((t & m) << 8) | ((t & ~m) >> 8);
  345. }
  346. }
  347. // Interleve a 16k block and byteswap
  348. static int InterleveBlock(unsigned char *dest,unsigned char *src)
  349. {
  350. int i=0;
  351. for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
  352. for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
  353. return 0;
  354. }
  355. // Decode a SMD file
  356. static int DecodeSmd(unsigned char *data,int len)
  357. {
  358. unsigned char *temp=NULL;
  359. int i=0;
  360. temp=(unsigned char *)malloc(0x4000);
  361. if (temp==NULL) return 1;
  362. memset(temp,0,0x4000);
  363. // Interleve each 16k block and shift down by 0x200:
  364. for (i=0; i+0x4200<=len; i+=0x4000)
  365. {
  366. InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
  367. memcpy(data+i,temp,0x4000); // Copy back in
  368. }
  369. free(temp);
  370. return 0;
  371. }
  372. static unsigned char *PicoCartAlloc(int filesize, int is_sms)
  373. {
  374. unsigned char *rom;
  375. if (is_sms) {
  376. // make size power of 2 for easier banking handling
  377. int s = 0, tmp = filesize;
  378. while ((tmp >>= 1) != 0)
  379. s++;
  380. if (filesize > (1 << s))
  381. s++;
  382. rom_alloc_size = 1 << s;
  383. // be sure we can cover all address space
  384. if (rom_alloc_size < 0x10000)
  385. rom_alloc_size = 0x10000;
  386. }
  387. else {
  388. // make alloc size at least sizeof(mcd_state),
  389. // in case we want to switch to CD mode
  390. if (filesize < sizeof(mcd_state))
  391. filesize = sizeof(mcd_state);
  392. // align to 512K for memhandlers
  393. rom_alloc_size = (filesize + 0x7ffff) & ~0x7ffff;
  394. }
  395. if (rom_alloc_size - filesize < 4)
  396. rom_alloc_size += 4; // padding for out-of-bound exec protection
  397. // Allocate space for the rom plus padding
  398. // use special address for 32x dynarec
  399. rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);
  400. return rom;
  401. }
  402. int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
  403. {
  404. unsigned char *rom;
  405. int size, bytes_read;
  406. if (f == NULL)
  407. return 1;
  408. size = f->size;
  409. if (size <= 0) return 1;
  410. size = (size+3)&~3; // Round up to a multiple of 4
  411. // Allocate space for the rom plus padding
  412. rom = PicoCartAlloc(size, is_sms);
  413. if (rom == NULL) {
  414. elprintf(EL_STATUS, "out of memory (wanted %i)", size);
  415. return 2;
  416. }
  417. if (PicoCartLoadProgressCB != NULL)
  418. {
  419. // read ROM in blocks, just for fun
  420. int ret;
  421. unsigned char *p = rom;
  422. bytes_read=0;
  423. do
  424. {
  425. int todo = size - bytes_read;
  426. if (todo > 256*1024) todo = 256*1024;
  427. ret = pm_read(p,todo,f);
  428. bytes_read += ret;
  429. p += ret;
  430. PicoCartLoadProgressCB(bytes_read * 100 / size);
  431. }
  432. while (ret > 0);
  433. }
  434. else
  435. bytes_read = pm_read(rom,size,f); // Load up the rom
  436. if (bytes_read <= 0) {
  437. elprintf(EL_STATUS, "read failed");
  438. free(rom);
  439. return 3;
  440. }
  441. if (!is_sms)
  442. {
  443. // maybe we are loading MegaCD BIOS?
  444. if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
  445. !strncmp((char *)rom+0x128, "BOOT", 4))) {
  446. PicoAHW |= PAHW_MCD;
  447. }
  448. // Check for SMD:
  449. if (size >= 0x4200 && (size&0x3fff) == 0x200 &&
  450. ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
  451. elprintf(EL_STATUS, "SMD format detected.");
  452. DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
  453. }
  454. else Byteswap(rom, rom, size); // Just byteswap
  455. }
  456. else
  457. {
  458. if (size >= 0x4200 && (size&0x3fff) == 0x200) {
  459. elprintf(EL_STATUS, "SMD format detected.");
  460. // at least here it's not interleaved
  461. size -= 0x200;
  462. memmove(rom, rom + 0x200, size);
  463. }
  464. }
  465. if (prom) *prom = rom;
  466. if (psize) *psize = size;
  467. return 0;
  468. }
  469. // Insert a cartridge:
  470. int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg)
  471. {
  472. // notaz: add a 68k "jump one op back" opcode to the end of ROM.
  473. // This will hang the emu, but will prevent nasty crashes.
  474. // note: 4 bytes are padded to every ROM
  475. if (rom != NULL)
  476. *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
  477. Pico.rom=rom;
  478. Pico.romsize=romsize;
  479. if (SRam.data) {
  480. free(SRam.data);
  481. SRam.data = NULL;
  482. }
  483. if (PicoCartUnloadHook != NULL) {
  484. PicoCartUnloadHook();
  485. PicoCartUnloadHook = NULL;
  486. }
  487. pdb_cleanup();
  488. PicoAHW &= PAHW_MCD|PAHW_SMS;
  489. PicoCartMemSetup = NULL;
  490. PicoDmaHook = NULL;
  491. PicoResetHook = NULL;
  492. PicoLineHook = NULL;
  493. PicoLoadStateHook = NULL;
  494. carthw_chunks = NULL;
  495. if (!(PicoAHW & (PAHW_MCD|PAHW_SMS)))
  496. PicoCartDetect(carthw_cfg);
  497. // setup correct memory map for loaded ROM
  498. switch (PicoAHW) {
  499. default:
  500. elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);
  501. case 0:
  502. case PAHW_SVP: PicoMemSetup(); break;
  503. case PAHW_MCD: PicoMemSetupCD(); break;
  504. case PAHW_PICO: PicoMemSetupPico(); break;
  505. case PAHW_SMS: PicoMemSetupMS(); break;
  506. }
  507. if (PicoCartMemSetup != NULL)
  508. PicoCartMemSetup();
  509. if (PicoAHW & PAHW_SMS)
  510. PicoPowerMS();
  511. else
  512. PicoPower();
  513. PicoGameLoaded = 1;
  514. return 0;
  515. }
  516. int PicoCartResize(int newsize)
  517. {
  518. void *tmp = plat_mremap(Pico.rom, rom_alloc_size, newsize);
  519. if (tmp == NULL)
  520. return -1;
  521. Pico.rom = tmp;
  522. rom_alloc_size = newsize;
  523. return 0;
  524. }
  525. void PicoCartUnload(void)
  526. {
  527. if (PicoCartUnloadHook != NULL) {
  528. PicoCartUnloadHook();
  529. PicoCartUnloadHook = NULL;
  530. }
  531. if (PicoAHW & PAHW_32X)
  532. PicoUnload32x();
  533. if (Pico.rom != NULL) {
  534. SekFinishIdleDet();
  535. plat_munmap(Pico.rom, rom_alloc_size);
  536. Pico.rom = NULL;
  537. }
  538. PicoGameLoaded = 0;
  539. }
  540. static unsigned int rom_crc32(void)
  541. {
  542. unsigned int crc;
  543. elprintf(EL_STATUS, "caclulating CRC32..");
  544. // have to unbyteswap for calculation..
  545. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  546. crc = crc32(0, Pico.rom, Pico.romsize);
  547. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  548. return crc;
  549. }
  550. static int rom_strcmp(int rom_offset, const char *s1)
  551. {
  552. int i, len = strlen(s1);
  553. const char *s_rom = (const char *)Pico.rom;
  554. if (rom_offset + len > Pico.romsize)
  555. return 0;
  556. for (i = 0; i < len; i++)
  557. if (s1[i] != s_rom[(i + rom_offset) ^ 1])
  558. return 1;
  559. return 0;
  560. }
  561. static unsigned int rom_read32(int addr)
  562. {
  563. unsigned short *m = (unsigned short *)(Pico.rom + addr);
  564. return (m[0] << 16) | m[1];
  565. }
  566. static char *sskip(char *s)
  567. {
  568. while (*s && isspace_(*s))
  569. s++;
  570. return s;
  571. }
  572. static void rstrip(char *s)
  573. {
  574. char *p;
  575. for (p = s + strlen(s) - 1; p >= s; p--)
  576. if (isspace_(*p))
  577. *p = 0;
  578. }
  579. static int parse_3_vals(char *p, int *val0, int *val1, int *val2)
  580. {
  581. char *r;
  582. *val0 = strtoul(p, &r, 0);
  583. if (r == p)
  584. goto bad;
  585. p = sskip(r);
  586. if (*p++ != ',')
  587. goto bad;
  588. *val1 = strtoul(p, &r, 0);
  589. if (r == p)
  590. goto bad;
  591. p = sskip(r);
  592. if (*p++ != ',')
  593. goto bad;
  594. *val2 = strtoul(p, &r, 0);
  595. if (r == p)
  596. goto bad;
  597. return 1;
  598. bad:
  599. return 0;
  600. }
  601. static int is_expr(const char *expr, char **pr)
  602. {
  603. int len = strlen(expr);
  604. char *p = *pr;
  605. if (strncmp(expr, p, len) != 0)
  606. return 0;
  607. p = sskip(p + len);
  608. if (*p != '=')
  609. return 0; // wrong or malformed
  610. *pr = sskip(p + 1);
  611. return 1;
  612. }
  613. #include "carthw_cfg.c"
  614. static void parse_carthw(const char *carthw_cfg, int *fill_sram)
  615. {
  616. int line = 0, any_checks_passed = 0, skip_sect = 0;
  617. const char *s, *builtin = builtin_carthw_cfg;
  618. int tmp, rom_crc = 0;
  619. char buff[256], *p, *r;
  620. FILE *f;
  621. f = fopen(carthw_cfg, "r");
  622. if (f == NULL)
  623. f = fopen("pico/carthw.cfg", "r");
  624. if (f == NULL)
  625. elprintf(EL_STATUS, "couldn't open carthw.cfg!");
  626. for (;;)
  627. {
  628. if (f != NULL) {
  629. p = fgets(buff, sizeof(buff), f);
  630. if (p == NULL)
  631. break;
  632. }
  633. else {
  634. if (*builtin == 0)
  635. break;
  636. for (s = builtin; *s != 0 && *s != '\n'; s++)
  637. ;
  638. while (*s == '\n')
  639. s++;
  640. tmp = s - builtin;
  641. if (tmp > sizeof(buff) - 1)
  642. tmp = sizeof(buff) - 1;
  643. memcpy(buff, builtin, tmp);
  644. buff[tmp] = 0;
  645. p = buff;
  646. builtin = s;
  647. }
  648. line++;
  649. p = sskip(p);
  650. if (*p == 0 || *p == '#')
  651. continue;
  652. if (*p == '[') {
  653. any_checks_passed = 0;
  654. skip_sect = 0;
  655. continue;
  656. }
  657. if (skip_sect)
  658. continue;
  659. /* look for checks */
  660. if (is_expr("check_str", &p))
  661. {
  662. int offs;
  663. offs = strtoul(p, &r, 0);
  664. if (offs < 0 || offs > Pico.romsize) {
  665. elprintf(EL_STATUS, "carthw:%d: check_str offs out of range: %d\n", line, offs);
  666. goto bad;
  667. }
  668. p = sskip(r);
  669. if (*p != ',')
  670. goto bad;
  671. p = sskip(p + 1);
  672. if (*p != '"')
  673. goto bad;
  674. p++;
  675. r = strchr(p, '"');
  676. if (r == NULL)
  677. goto bad;
  678. *r = 0;
  679. if (rom_strcmp(offs, p) == 0)
  680. any_checks_passed = 1;
  681. else
  682. skip_sect = 1;
  683. continue;
  684. }
  685. else if (is_expr("check_size_gt", &p))
  686. {
  687. int size;
  688. size = strtoul(p, &r, 0);
  689. if (r == p || size < 0)
  690. goto bad;
  691. if (Pico.romsize > size)
  692. any_checks_passed = 1;
  693. else
  694. skip_sect = 1;
  695. continue;
  696. }
  697. else if (is_expr("check_csum", &p))
  698. {
  699. int csum;
  700. csum = strtoul(p, &r, 0);
  701. if (r == p || (csum & 0xffff0000))
  702. goto bad;
  703. if (csum == (rom_read32(0x18c) & 0xffff))
  704. any_checks_passed = 1;
  705. else
  706. skip_sect = 1;
  707. continue;
  708. }
  709. else if (is_expr("check_crc32", &p))
  710. {
  711. unsigned int crc;
  712. crc = strtoul(p, &r, 0);
  713. if (r == p)
  714. goto bad;
  715. if (rom_crc == 0)
  716. rom_crc = rom_crc32();
  717. if (crc == rom_crc)
  718. any_checks_passed = 1;
  719. else
  720. skip_sect = 1;
  721. continue;
  722. }
  723. /* now time for actions */
  724. if (is_expr("hw", &p)) {
  725. if (!any_checks_passed)
  726. goto no_checks;
  727. rstrip(p);
  728. if (strcmp(p, "svp") == 0)
  729. PicoSVPStartup();
  730. else if (strcmp(p, "pico") == 0)
  731. PicoInitPico();
  732. else if (strcmp(p, "prot") == 0)
  733. carthw_sprot_startup();
  734. else if (strcmp(p, "ssf2_mapper") == 0)
  735. carthw_ssf2_startup();
  736. else if (strcmp(p, "x_in_1_mapper") == 0)
  737. carthw_Xin1_startup();
  738. else if (strcmp(p, "realtec_mapper") == 0)
  739. carthw_realtec_startup();
  740. else if (strcmp(p, "radica_mapper") == 0)
  741. carthw_radica_startup();
  742. else if (strcmp(p, "piersolar_mapper") == 0)
  743. carthw_pier_startup();
  744. else if (strcmp(p, "prot_lk3") == 0)
  745. carthw_prot_lk3_startup();
  746. else {
  747. elprintf(EL_STATUS, "carthw:%d: unsupported mapper: %s", line, p);
  748. skip_sect = 1;
  749. }
  750. continue;
  751. }
  752. if (is_expr("sram_range", &p)) {
  753. int start, end;
  754. if (!any_checks_passed)
  755. goto no_checks;
  756. rstrip(p);
  757. start = strtoul(p, &r, 0);
  758. if (r == p)
  759. goto bad;
  760. p = sskip(r);
  761. if (*p != ',')
  762. goto bad;
  763. p = sskip(p + 1);
  764. end = strtoul(p, &r, 0);
  765. if (r == p)
  766. goto bad;
  767. if (((start | end) & 0xff000000) || start > end) {
  768. elprintf(EL_STATUS, "carthw:%d: bad sram_range: %08x - %08x", line, start, end);
  769. goto bad_nomsg;
  770. }
  771. SRam.start = start;
  772. SRam.end = end;
  773. continue;
  774. }
  775. else if (is_expr("prop", &p)) {
  776. if (!any_checks_passed)
  777. goto no_checks;
  778. rstrip(p);
  779. if (strcmp(p, "no_sram") == 0)
  780. SRam.flags &= ~SRF_ENABLED;
  781. else if (strcmp(p, "no_eeprom") == 0)
  782. SRam.flags &= ~SRF_EEPROM;
  783. else if (strcmp(p, "filled_sram") == 0)
  784. *fill_sram = 1;
  785. else if (strcmp(p, "force_6btn") == 0)
  786. PicoQuirks |= PQUIRK_FORCE_6BTN;
  787. else {
  788. elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);
  789. goto bad_nomsg;
  790. }
  791. elprintf(EL_STATUS, "game prop: %s", p);
  792. continue;
  793. }
  794. else if (is_expr("eeprom_type", &p)) {
  795. int type;
  796. if (!any_checks_passed)
  797. goto no_checks;
  798. rstrip(p);
  799. type = strtoul(p, &r, 0);
  800. if (r == p || type < 0)
  801. goto bad;
  802. SRam.eeprom_type = type;
  803. SRam.flags |= SRF_EEPROM;
  804. continue;
  805. }
  806. else if (is_expr("eeprom_lines", &p)) {
  807. int scl, sda_in, sda_out;
  808. if (!any_checks_passed)
  809. goto no_checks;
  810. rstrip(p);
  811. if (!parse_3_vals(p, &scl, &sda_in, &sda_out))
  812. goto bad;
  813. if (scl < 0 || scl > 15 || sda_in < 0 || sda_in > 15 ||
  814. sda_out < 0 || sda_out > 15)
  815. goto bad;
  816. SRam.eeprom_bit_cl = scl;
  817. SRam.eeprom_bit_in = sda_in;
  818. SRam.eeprom_bit_out= sda_out;
  819. continue;
  820. }
  821. else if ((tmp = is_expr("prot_ro_value16", &p)) || is_expr("prot_rw_value16", &p)) {
  822. int addr, mask, val;
  823. if (!any_checks_passed)
  824. goto no_checks;
  825. rstrip(p);
  826. if (!parse_3_vals(p, &addr, &mask, &val))
  827. goto bad;
  828. carthw_sprot_new_location(addr, mask, val, tmp ? 1 : 0);
  829. continue;
  830. }
  831. bad:
  832. elprintf(EL_STATUS, "carthw:%d: unrecognized expression: %s", line, buff);
  833. bad_nomsg:
  834. skip_sect = 1;
  835. continue;
  836. no_checks:
  837. elprintf(EL_STATUS, "carthw:%d: command without any checks before it: %s", line, buff);
  838. skip_sect = 1;
  839. continue;
  840. }
  841. if (f != NULL)
  842. fclose(f);
  843. }
  844. /*
  845. * various cart-specific things, which can't be handled by generic code
  846. */
  847. static void PicoCartDetect(const char *carthw_cfg)
  848. {
  849. int fill_sram = 0;
  850. memset(&SRam, 0, sizeof(SRam));
  851. if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
  852. {
  853. SRam.start = rom_read32(0x1B4) & ~0xff000001; // align
  854. SRam.end = (rom_read32(0x1B8) & ~0xff000000) | 1;
  855. if (Pico.rom[0x1B2] & 0x40)
  856. // EEPROM
  857. SRam.flags |= SRF_EEPROM;
  858. SRam.flags |= SRF_ENABLED;
  859. }
  860. if (SRam.end == 0 || SRam.start > SRam.end)
  861. {
  862. // some games may have bad headers, like S&K and Sonic3
  863. // note: majority games use 0x200000 as starting address, but there are some which
  864. // use something else (0x300000 by HardBall '95). Luckily they have good headers.
  865. SRam.start = 0x200000;
  866. SRam.end = 0x203FFF;
  867. SRam.flags |= SRF_ENABLED;
  868. }
  869. // set EEPROM defaults, in case it gets detected
  870. SRam.eeprom_type = 0; // 7bit (24C01)
  871. SRam.eeprom_bit_cl = 1;
  872. SRam.eeprom_bit_in = 0;
  873. SRam.eeprom_bit_out= 0;
  874. if (carthw_cfg != NULL)
  875. parse_carthw(carthw_cfg, &fill_sram);
  876. if (SRam.flags & SRF_ENABLED)
  877. {
  878. if (SRam.flags & SRF_EEPROM)
  879. SRam.size = 0x2000;
  880. else
  881. SRam.size = SRam.end - SRam.start + 1;
  882. SRam.data = calloc(SRam.size, 1);
  883. if (SRam.data == NULL)
  884. SRam.flags &= ~SRF_ENABLED;
  885. if (SRam.eeprom_type == 1) // 1 == 0 in PD EEPROM code
  886. SRam.eeprom_type = 0;
  887. }
  888. if ((SRam.flags & SRF_ENABLED) && fill_sram)
  889. {
  890. elprintf(EL_STATUS, "SRAM fill");
  891. memset(SRam.data, 0xff, SRam.size);
  892. }
  893. // Unusual region 'code'
  894. if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
  895. *(int *) (Pico.rom + 0x1f0) = 0x20204520;
  896. }
  897. // vim:shiftwidth=2:expandtab