cart.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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 <cpu/debug.h>
  11. #ifdef USE_LIBRETRO_VFS
  12. #include "file_stream_transforms.h"
  13. #endif
  14. #if defined(USE_LIBCHDR)
  15. #include "libchdr/chd.h"
  16. #include "libchdr/cdrom.h"
  17. #endif
  18. #include <unzip/unzip.h>
  19. #include <zlib.h>
  20. static int rom_alloc_size;
  21. static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };
  22. void (*PicoCartUnloadHook)(void);
  23. void (*PicoCartMemSetup)(void);
  24. void (*PicoCartLoadProgressCB)(int percent) = NULL;
  25. void (*PicoCDLoadProgressCB)(const char *fname, int percent) = NULL; // handled in Pico/cd/cd_file.c
  26. int PicoGameLoaded;
  27. static void PicoCartDetect(const char *carthw_cfg);
  28. /* cso struct */
  29. typedef struct _cso_struct
  30. {
  31. unsigned char in_buff[2*2048];
  32. unsigned char out_buff[2048];
  33. struct {
  34. char magic[4];
  35. unsigned int unused;
  36. unsigned int total_bytes;
  37. unsigned int total_bytes_high; // ignored here
  38. unsigned int block_size; // 10h
  39. unsigned char ver;
  40. unsigned char align;
  41. unsigned char reserved[2];
  42. } header;
  43. unsigned int fpos_in; // input file read pointer
  44. unsigned int fpos_out; // pos in virtual decompressed file
  45. int block_in_buff; // block which we have read in in_buff
  46. int pad;
  47. int index[0];
  48. }
  49. cso_struct;
  50. static int uncompress_buf(void *dest, int destLen, void *source, int sourceLen)
  51. {
  52. z_stream stream;
  53. int err;
  54. stream.next_in = (Bytef*)source;
  55. stream.avail_in = (uInt)sourceLen;
  56. stream.next_out = dest;
  57. stream.avail_out = (uInt)destLen;
  58. stream.zalloc = NULL;
  59. stream.zfree = NULL;
  60. err = inflateInit2(&stream, -15);
  61. if (err != Z_OK) return err;
  62. err = inflate(&stream, Z_FINISH);
  63. if (err != Z_STREAM_END) {
  64. inflateEnd(&stream);
  65. return err;
  66. }
  67. //*destLen = stream.total_out;
  68. return inflateEnd(&stream);
  69. }
  70. static const char *get_ext(const char *path)
  71. {
  72. const char *ext;
  73. if (strlen(path) < 4)
  74. return ""; // no ext
  75. // allow 2 or 3 char extensions for now
  76. ext = path + strlen(path) - 2;
  77. if (ext[-1] != '.') ext--;
  78. if (ext[-1] != '.')
  79. return "";
  80. return ext;
  81. }
  82. struct zip_file {
  83. pm_file file;
  84. ZIP *zip;
  85. struct zipent *entry;
  86. z_stream stream;
  87. unsigned char inbuf[16384];
  88. long start;
  89. unsigned int pos;
  90. };
  91. #if defined(USE_LIBCHDR)
  92. struct chd_struct {
  93. pm_file file;
  94. int fpos;
  95. int sectorsize;
  96. chd_file *chd;
  97. int unitbytes;
  98. int hunkunits;
  99. u8 *hunk;
  100. int hunknum;
  101. };
  102. #endif
  103. pm_file *pm_open(const char *path)
  104. {
  105. pm_file *file = NULL;
  106. const char *ext;
  107. FILE *f;
  108. if (path == NULL)
  109. return NULL;
  110. ext = get_ext(path);
  111. if (strcasecmp(ext, "zip") == 0)
  112. {
  113. struct zip_file *zfile = NULL;
  114. struct zipent *zipentry;
  115. ZIP *zipfile;
  116. int i, ret;
  117. zipfile = openzip(path);
  118. if (zipfile != NULL)
  119. {
  120. /* search for suitable file (right extension or large enough file) */
  121. while ((zipentry = readzip(zipfile)) != NULL)
  122. {
  123. ext = get_ext(zipentry->name);
  124. if (zipentry->uncompressed_size >= 32*1024)
  125. goto found_rom_zip;
  126. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  127. if (strcasecmp(ext, rom_exts[i]) == 0)
  128. goto found_rom_zip;
  129. }
  130. /* zipfile given, but nothing found suitable for us inside */
  131. goto zip_failed;
  132. found_rom_zip:
  133. zfile = calloc(1, sizeof(*zfile));
  134. if (zfile == NULL)
  135. goto zip_failed;
  136. ret = seekcompresszip(zipfile, zipentry);
  137. if (ret != 0)
  138. goto zip_failed;
  139. ret = inflateInit2(&zfile->stream, -15);
  140. if (ret != Z_OK) {
  141. elprintf(EL_STATUS, "zip: inflateInit2 %d", ret);
  142. goto zip_failed;
  143. }
  144. zfile->zip = zipfile;
  145. zfile->entry = zipentry;
  146. zfile->start = ftell(zipfile->fp);
  147. zfile->file.file = zfile;
  148. zfile->file.size = zipentry->uncompressed_size;
  149. zfile->file.type = PMT_ZIP;
  150. strncpy(zfile->file.ext, ext, sizeof(zfile->file.ext) - 1);
  151. return &zfile->file;
  152. zip_failed:
  153. closezip(zipfile);
  154. free(zfile);
  155. return NULL;
  156. }
  157. }
  158. else if (strcasecmp(ext, "cso") == 0)
  159. {
  160. cso_struct *cso = NULL, *tmp = NULL;
  161. int size;
  162. f = fopen(path, "rb");
  163. if (f == NULL)
  164. goto cso_failed;
  165. #ifdef __GP2X__
  166. /* we use our own buffering */
  167. setvbuf(f, NULL, _IONBF, 0);
  168. #endif
  169. cso = malloc(sizeof(*cso));
  170. if (cso == NULL)
  171. goto cso_failed;
  172. if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
  173. goto cso_failed;
  174. cso->header.block_size = CPU_LE4(cso->header.block_size);
  175. cso->header.total_bytes = CPU_LE4(cso->header.total_bytes);
  176. cso->header.total_bytes_high = CPU_LE4(cso->header.total_bytes_high);
  177. if (strncmp(cso->header.magic, "CISO", 4) != 0) {
  178. elprintf(EL_STATUS, "cso: bad header");
  179. goto cso_failed;
  180. }
  181. if (cso->header.block_size != 2048) {
  182. elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
  183. goto cso_failed;
  184. }
  185. size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
  186. tmp = realloc(cso, size);
  187. if (tmp == NULL)
  188. goto cso_failed;
  189. cso = tmp;
  190. elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
  191. size -= sizeof(*cso); // index size
  192. if (fread(cso->index, 1, size, f) != size) {
  193. elprintf(EL_STATUS, "cso: premature EOF");
  194. goto cso_failed;
  195. }
  196. // all ok
  197. cso->fpos_in = ftell(f);
  198. cso->fpos_out = 0;
  199. cso->block_in_buff = -1;
  200. file = calloc(1, sizeof(*file));
  201. if (file == NULL) goto cso_failed;
  202. file->file = f;
  203. file->param = cso;
  204. file->size = cso->header.total_bytes;
  205. file->type = PMT_CSO;
  206. return file;
  207. cso_failed:
  208. if (cso != NULL) free(cso);
  209. if (f != NULL) fclose(f);
  210. return NULL;
  211. }
  212. #if defined(USE_LIBCHDR)
  213. else if (strcasecmp(ext, "chd") == 0)
  214. {
  215. struct chd_struct *chd = NULL;
  216. chd_file *cf = NULL;
  217. const chd_header *head;
  218. if (chd_open(path, CHD_OPEN_READ, NULL, &cf) != CHDERR_NONE)
  219. goto chd_failed;
  220. // sanity check
  221. head = chd_get_header(cf);
  222. if ((head->hunkbytes == 0) || (head->hunkbytes % CD_FRAME_SIZE))
  223. goto chd_failed;
  224. chd = calloc(1, sizeof(*chd));
  225. if (chd == NULL)
  226. goto chd_failed;
  227. chd->hunk = (u8 *)malloc(head->hunkbytes);
  228. if (!chd->hunk)
  229. goto chd_failed;
  230. chd->chd = cf;
  231. chd->unitbytes = head->unitbytes;
  232. chd->hunkunits = head->hunkbytes / head->unitbytes;
  233. chd->sectorsize = CD_MAX_SECTOR_DATA; // default to RAW mode
  234. chd->fpos = 0;
  235. chd->hunknum = -1;
  236. chd->file.file = chd;
  237. chd->file.type = PMT_CHD;
  238. // subchannel data is skipped, remove it from total size
  239. chd->file.size = chd_get_header(cf)->logicalbytes / CD_FRAME_SIZE * CD_MAX_SECTOR_DATA;
  240. strncpy(chd->file.ext, ext, sizeof(chd->file.ext) - 1);
  241. return &chd->file;
  242. chd_failed:
  243. /* invalid CHD file */
  244. if (chd != NULL) free(chd);
  245. if (cf != NULL) chd_close(cf);
  246. return NULL;
  247. }
  248. #endif
  249. /* not a zip, treat as uncompressed file */
  250. f = fopen(path, "rb");
  251. if (f == NULL) return NULL;
  252. file = calloc(1, sizeof(*file));
  253. if (file == NULL) {
  254. fclose(f);
  255. return NULL;
  256. }
  257. fseek(f, 0, SEEK_END);
  258. file->file = f;
  259. file->param = NULL;
  260. file->size = ftell(f);
  261. file->type = PMT_UNCOMPRESSED;
  262. strncpy(file->ext, ext, sizeof(file->ext) - 1);
  263. fseek(f, 0, SEEK_SET);
  264. #ifdef __GP2X__
  265. if (file->size > 0x400000)
  266. /* we use our own buffering */
  267. setvbuf(f, NULL, _IONBF, 0);
  268. #endif
  269. return file;
  270. }
  271. void pm_sectorsize(int length, pm_file *stream)
  272. {
  273. // CHD reading needs to know how much binary data is in one data sector(=unit)
  274. #if defined(USE_LIBCHDR)
  275. if (stream->type == PMT_CHD) {
  276. struct chd_struct *chd = stream->file;
  277. chd->sectorsize = length;
  278. if (chd->sectorsize > chd->unitbytes)
  279. elprintf(EL_STATUS|EL_ANOMALY, "cd: sector size %d too large for unit %d", chd->sectorsize, chd->unitbytes);
  280. }
  281. #endif
  282. }
  283. #if defined(USE_LIBCHDR)
  284. static size_t _pm_read_chd(void *ptr, size_t bytes, pm_file *stream, int is_audio)
  285. {
  286. int ret = 0;
  287. if (stream->type == PMT_CHD) {
  288. struct chd_struct *chd = stream->file;
  289. // calculate sector and offset in sector
  290. int sectsz = is_audio ? CD_MAX_SECTOR_DATA : chd->sectorsize;
  291. int sector = chd->fpos / sectsz;
  292. int offset = chd->fpos - (sector * sectsz);
  293. // calculate hunk and sector offset in hunk
  294. int hunknum = sector / chd->hunkunits;
  295. int hunksec = sector - (hunknum * chd->hunkunits);
  296. int hunkofs = hunksec * chd->unitbytes;
  297. while (bytes != 0) {
  298. // data left in current sector
  299. int len = sectsz - offset;
  300. // update hunk cache if needed
  301. if (hunknum != chd->hunknum) {
  302. chd_read(chd->chd, hunknum, chd->hunk);
  303. chd->hunknum = hunknum;
  304. }
  305. if (len > bytes)
  306. len = bytes;
  307. #ifdef CPU_IS_LE
  308. if (is_audio) {
  309. // convert big endian audio samples
  310. u16 *dst = ptr, v;
  311. u8 *src = chd->hunk + hunkofs + offset;
  312. int i;
  313. for (i = 0; i < len; i += 4) {
  314. v = *src++ << 8; *dst++ = v | *src++;
  315. v = *src++ << 8; *dst++ = v | *src++;
  316. }
  317. } else
  318. #endif
  319. memcpy(ptr, chd->hunk + hunkofs + offset, len);
  320. // house keeping
  321. ret += len;
  322. chd->fpos += len;
  323. bytes -= len;
  324. // no need to advance internals if there's no more data to read
  325. if (bytes) {
  326. ptr += len;
  327. offset = 0;
  328. sector ++;
  329. hunksec ++;
  330. hunkofs += chd->unitbytes;
  331. if (hunksec >= chd->hunkunits) {
  332. hunksec = 0;
  333. hunkofs = 0;
  334. hunknum ++;
  335. }
  336. }
  337. }
  338. }
  339. return ret;
  340. }
  341. #endif
  342. size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
  343. {
  344. int ret;
  345. if (stream->type == PMT_UNCOMPRESSED)
  346. {
  347. ret = fread(ptr, 1, bytes, stream->file);
  348. }
  349. else if (stream->type == PMT_ZIP)
  350. {
  351. struct zip_file *z = stream->file;
  352. if (z->entry->compression_method == 0) {
  353. int ret = fread(ptr, 1, bytes, z->zip->fp);
  354. z->pos += ret;
  355. return ret;
  356. }
  357. z->stream.next_out = ptr;
  358. z->stream.avail_out = bytes;
  359. while (z->stream.avail_out != 0) {
  360. if (z->stream.avail_in == 0) {
  361. z->stream.avail_in = fread(z->inbuf, 1, sizeof(z->inbuf), z->zip->fp);
  362. if (z->stream.avail_in == 0)
  363. break;
  364. z->stream.next_in = z->inbuf;
  365. }
  366. ret = inflate(&z->stream, Z_NO_FLUSH);
  367. if (ret == Z_STREAM_END)
  368. break;
  369. if (ret != Z_OK) {
  370. elprintf(EL_STATUS, "zip: inflate: %d", ret);
  371. return 0;
  372. }
  373. }
  374. z->pos += bytes - z->stream.avail_out;
  375. return bytes - z->stream.avail_out;
  376. }
  377. else if (stream->type == PMT_CSO)
  378. {
  379. cso_struct *cso = stream->param;
  380. int read_pos, read_len, out_offs, rret;
  381. int block = cso->fpos_out >> 11;
  382. int index = cso->index[block];
  383. int index_end = cso->index[block+1];
  384. unsigned char *out = ptr, *tmp_dst;
  385. ret = 0;
  386. while (bytes != 0)
  387. {
  388. out_offs = cso->fpos_out&0x7ff;
  389. if (out_offs == 0 && bytes >= 2048)
  390. tmp_dst = out;
  391. else tmp_dst = cso->out_buff;
  392. read_pos = (index&0x7fffffff) << cso->header.align;
  393. if (index < 0) {
  394. if (read_pos != cso->fpos_in)
  395. fseek(stream->file, read_pos, SEEK_SET);
  396. rret = fread(tmp_dst, 1, 2048, stream->file);
  397. cso->fpos_in = read_pos + rret;
  398. if (rret != 2048) break;
  399. } else {
  400. read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
  401. if (block != cso->block_in_buff)
  402. {
  403. if (read_pos != cso->fpos_in)
  404. fseek(stream->file, read_pos, SEEK_SET);
  405. rret = fread(cso->in_buff, 1, read_len, stream->file);
  406. cso->fpos_in = read_pos + rret;
  407. if (rret != read_len) {
  408. elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);
  409. break;
  410. }
  411. cso->block_in_buff = block;
  412. }
  413. rret = uncompress_buf(tmp_dst, 2048, cso->in_buff, read_len);
  414. if (rret != 0) {
  415. elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
  416. break;
  417. }
  418. }
  419. rret = 2048;
  420. if (out_offs != 0 || bytes < 2048) {
  421. //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);
  422. if (bytes < rret) rret = bytes;
  423. if (2048 - out_offs < rret) rret = 2048 - out_offs;
  424. memcpy(out, tmp_dst + out_offs, rret);
  425. }
  426. ret += rret;
  427. out += rret;
  428. cso->fpos_out += rret;
  429. bytes -= rret;
  430. block++;
  431. index = index_end;
  432. index_end = cso->index[block+1];
  433. }
  434. }
  435. #if defined(USE_LIBCHDR)
  436. else if (stream->type == PMT_CHD)
  437. {
  438. ret = _pm_read_chd(ptr, bytes, stream, 0);
  439. }
  440. #endif
  441. else
  442. ret = 0;
  443. return ret;
  444. }
  445. size_t pm_read_audio(void *ptr, size_t bytes, pm_file *stream)
  446. {
  447. #if !(CPU_IS_LE)
  448. if (stream->type == PMT_UNCOMPRESSED)
  449. {
  450. // convert little endian audio samples from WAV file
  451. int ret = pm_read(ptr, bytes, stream);
  452. u16 *dst = ptr, v;
  453. u8 *src = ptr;
  454. int i;
  455. for (i = 0; i < ret; i += 4) {
  456. v = *src++; *dst++ = v | (*src++ << 8);
  457. v = *src++; *dst++ = v | (*src++ << 8);
  458. }
  459. return ret;
  460. }
  461. else
  462. #endif
  463. #if defined(USE_LIBCHDR)
  464. if (stream->type == PMT_CHD)
  465. {
  466. return _pm_read_chd(ptr, bytes, stream, 1);
  467. }
  468. #endif
  469. return pm_read(ptr, bytes, stream);
  470. }
  471. int pm_seek(pm_file *stream, long offset, int whence)
  472. {
  473. if (stream->type == PMT_UNCOMPRESSED)
  474. {
  475. fseek(stream->file, offset, whence);
  476. return ftell(stream->file);
  477. }
  478. else if (stream->type == PMT_ZIP)
  479. {
  480. struct zip_file *z = stream->file;
  481. unsigned int pos = z->pos;
  482. int ret;
  483. switch (whence)
  484. {
  485. case SEEK_CUR: pos += offset; break;
  486. case SEEK_SET: pos = offset; break;
  487. case SEEK_END: pos = stream->size - offset; break;
  488. }
  489. if (z->entry->compression_method == 0) {
  490. ret = fseek(z->zip->fp, z->start + pos, SEEK_SET);
  491. if (ret == 0)
  492. return (z->pos = pos);
  493. return -1;
  494. }
  495. offset = pos - z->pos;
  496. if (pos < z->pos) {
  497. // full decompress from the start
  498. fseek(z->zip->fp, z->start, SEEK_SET);
  499. z->stream.avail_in = 0;
  500. z->stream.next_in = z->inbuf;
  501. inflateReset(&z->stream);
  502. z->pos = 0;
  503. offset = pos;
  504. }
  505. if (PicoIn.osdMessage != NULL && offset > 4 * 1024 * 1024)
  506. PicoIn.osdMessage("Decompressing data...");
  507. while (offset > 0) {
  508. char buf[16 * 1024];
  509. size_t l = offset > sizeof(buf) ? sizeof(buf) : offset;
  510. ret = pm_read(buf, l, stream);
  511. if (ret != l)
  512. break;
  513. offset -= l;
  514. }
  515. return z->pos;
  516. }
  517. else if (stream->type == PMT_CSO)
  518. {
  519. cso_struct *cso = stream->param;
  520. switch (whence)
  521. {
  522. case SEEK_CUR: cso->fpos_out += offset; break;
  523. case SEEK_SET: cso->fpos_out = offset; break;
  524. case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;
  525. }
  526. return cso->fpos_out;
  527. }
  528. #if defined(USE_LIBCHDR)
  529. else if (stream->type == PMT_CHD)
  530. {
  531. struct chd_struct *chd = stream->file;
  532. switch (whence)
  533. {
  534. case SEEK_CUR: chd->fpos += offset; break;
  535. case SEEK_SET: chd->fpos = offset; break;
  536. case SEEK_END: chd->fpos = stream->size - offset; break;
  537. }
  538. return chd->fpos;
  539. }
  540. #endif
  541. else
  542. return -1;
  543. }
  544. int pm_close(pm_file *fp)
  545. {
  546. int ret = 0;
  547. if (fp == NULL) return EOF;
  548. if (fp->type == PMT_UNCOMPRESSED)
  549. {
  550. fclose(fp->file);
  551. }
  552. else if (fp->type == PMT_ZIP)
  553. {
  554. struct zip_file *z = fp->file;
  555. inflateEnd(&z->stream);
  556. closezip(z->zip);
  557. }
  558. else if (fp->type == PMT_CSO)
  559. {
  560. free(fp->param);
  561. fclose(fp->file);
  562. }
  563. #if defined(USE_LIBCHDR)
  564. else if (fp->type == PMT_CHD)
  565. {
  566. struct chd_struct *chd = fp->file;
  567. chd_close(chd->chd);
  568. if (chd->hunk)
  569. free(chd->hunk);
  570. }
  571. #endif
  572. else
  573. ret = EOF;
  574. free(fp);
  575. return ret;
  576. }
  577. // byteswap, data needs to be int aligned, src can match dst
  578. void Byteswap(void *dst, const void *src, int len)
  579. {
  580. #if CPU_IS_LE
  581. const unsigned int *ps = src;
  582. unsigned int *pd = dst;
  583. int i, m;
  584. if (len < 2)
  585. return;
  586. m = 0x00ff00ff;
  587. for (i = 0; i < len / 4; i++) {
  588. unsigned int t = ps[i];
  589. pd[i] = ((t & m) << 8) | ((t & ~m) >> 8);
  590. }
  591. #endif
  592. }
  593. // Interleve a 16k block and byteswap
  594. static int InterleveBlock(unsigned char *dest,unsigned char *src)
  595. {
  596. int i=0;
  597. for (i=0;i<0x2000;i++) dest[(i<<1)+MEM_BE2(1)]=src[ i]; // Odd
  598. for (i=0;i<0x2000;i++) dest[(i<<1)+MEM_BE2(0)]=src[0x2000+i]; // Even
  599. return 0;
  600. }
  601. // Decode a SMD file
  602. static int DecodeSmd(unsigned char *data,int len)
  603. {
  604. unsigned char *temp=NULL;
  605. int i=0;
  606. temp=(unsigned char *)malloc(0x4000);
  607. if (temp==NULL) return 1;
  608. memset(temp,0,0x4000);
  609. // Interleve each 16k block and shift down by 0x200:
  610. for (i=0; i+0x4200<=len; i+=0x4000)
  611. {
  612. InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
  613. memcpy(data+i,temp,0x4000); // Copy back in
  614. }
  615. free(temp);
  616. return 0;
  617. }
  618. static unsigned char *PicoCartAlloc(int filesize, int is_sms)
  619. {
  620. unsigned char *rom;
  621. if (is_sms) {
  622. // make size power of 2 for easier banking handling
  623. int s = 0, tmp = filesize;
  624. while ((tmp >>= 1) != 0)
  625. s++;
  626. if (filesize > (1 << s))
  627. s++;
  628. rom_alloc_size = 1 << s;
  629. // be sure we can cover all address space
  630. if (rom_alloc_size < 0x10000)
  631. rom_alloc_size = 0x10000;
  632. }
  633. else {
  634. // make alloc size at least sizeof(mcd_state),
  635. // in case we want to switch to CD mode
  636. if (filesize < sizeof(mcd_state))
  637. filesize = sizeof(mcd_state);
  638. // align to 512K for memhandlers
  639. rom_alloc_size = (filesize + 0x7ffff) & ~0x7ffff;
  640. }
  641. if (rom_alloc_size - filesize < 4)
  642. rom_alloc_size += 4; // padding for out-of-bound exec protection
  643. // Allocate space for the rom plus padding
  644. // use special address for 32x dynarec
  645. rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);
  646. return rom;
  647. }
  648. int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
  649. {
  650. unsigned char *rom;
  651. int size, bytes_read;
  652. if (f == NULL)
  653. return 1;
  654. size = f->size;
  655. if (size <= 0) return 1;
  656. size = (size+3)&~3; // Round up to a multiple of 4
  657. // Allocate space for the rom plus padding
  658. rom = PicoCartAlloc(size, is_sms);
  659. if (rom == NULL) {
  660. elprintf(EL_STATUS, "out of memory (wanted %i)", size);
  661. return 2;
  662. }
  663. if (PicoCartLoadProgressCB != NULL)
  664. {
  665. // read ROM in blocks, just for fun
  666. int ret;
  667. unsigned char *p = rom;
  668. bytes_read=0;
  669. do
  670. {
  671. int todo = size - bytes_read;
  672. if (todo > 256*1024) todo = 256*1024;
  673. ret = pm_read(p,todo,f);
  674. bytes_read += ret;
  675. p += ret;
  676. PicoCartLoadProgressCB(bytes_read * 100 / size);
  677. }
  678. while (ret > 0);
  679. }
  680. else
  681. bytes_read = pm_read(rom,size,f); // Load up the rom
  682. if (bytes_read <= 0) {
  683. elprintf(EL_STATUS, "read failed");
  684. plat_munmap(rom, rom_alloc_size);
  685. return 3;
  686. }
  687. if (!is_sms)
  688. {
  689. // maybe we are loading MegaCD BIOS?
  690. if (!(PicoIn.AHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
  691. !strncmp((char *)rom+0x128, "BOOT", 4))) {
  692. PicoIn.AHW |= PAHW_MCD;
  693. }
  694. // Check for SMD:
  695. if (size >= 0x4200 && (size&0x3fff) == 0x200 &&
  696. ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
  697. elprintf(EL_STATUS, "SMD format detected.");
  698. DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
  699. }
  700. else Byteswap(rom, rom, size); // Just byteswap
  701. }
  702. else
  703. {
  704. if (size >= 0x4200 && (size&0x3fff) == 0x200) {
  705. elprintf(EL_STATUS, "SMD format detected.");
  706. // at least here it's not interleaved
  707. size -= 0x200;
  708. memmove(rom, rom + 0x200, size);
  709. }
  710. }
  711. if (prom) *prom = rom;
  712. if (psize) *psize = size;
  713. return 0;
  714. }
  715. // Insert a cartridge:
  716. int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg)
  717. {
  718. // notaz: add a 68k "jump one op back" opcode to the end of ROM.
  719. // This will hang the emu, but will prevent nasty crashes.
  720. // note: 4 bytes are padded to every ROM
  721. if (rom != NULL)
  722. *(unsigned long *)(rom+romsize) = CPU_BE2(0x4EFAFFFE);
  723. Pico.rom=rom;
  724. Pico.romsize=romsize;
  725. if (Pico.sv.data) {
  726. free(Pico.sv.data);
  727. Pico.sv.data = NULL;
  728. }
  729. if (PicoCartUnloadHook != NULL) {
  730. PicoCartUnloadHook();
  731. PicoCartUnloadHook = NULL;
  732. }
  733. pdb_cleanup();
  734. PicoIn.AHW &= PAHW_MCD|PAHW_SMS;
  735. PicoCartMemSetup = NULL;
  736. PicoDmaHook = NULL;
  737. PicoResetHook = NULL;
  738. PicoLineHook = NULL;
  739. PicoLoadStateHook = NULL;
  740. carthw_chunks = NULL;
  741. if (!(PicoIn.AHW & (PAHW_MCD|PAHW_SMS)))
  742. PicoCartDetect(carthw_cfg);
  743. // setup correct memory map for loaded ROM
  744. switch (PicoIn.AHW) {
  745. default:
  746. elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoIn.AHW);
  747. case 0:
  748. case PAHW_SVP: PicoMemSetup(); break;
  749. case PAHW_MCD: PicoMemSetupCD(); break;
  750. case PAHW_PICO: PicoMemSetupPico(); break;
  751. case PAHW_SMS: PicoMemSetupMS(); break;
  752. }
  753. if (PicoCartMemSetup != NULL)
  754. PicoCartMemSetup();
  755. if (PicoIn.AHW & PAHW_SMS)
  756. PicoPowerMS();
  757. else
  758. PicoPower();
  759. PicoGameLoaded = 1;
  760. return 0;
  761. }
  762. int PicoCartResize(int newsize)
  763. {
  764. void *tmp = plat_mremap(Pico.rom, rom_alloc_size, newsize);
  765. if (tmp == NULL)
  766. return -1;
  767. Pico.rom = tmp;
  768. rom_alloc_size = newsize;
  769. return 0;
  770. }
  771. void PicoCartUnload(void)
  772. {
  773. if (PicoCartUnloadHook != NULL) {
  774. PicoCartUnloadHook();
  775. PicoCartUnloadHook = NULL;
  776. }
  777. if (PicoIn.AHW & PAHW_32X)
  778. PicoUnload32x();
  779. if (Pico.rom != NULL) {
  780. SekFinishIdleDet();
  781. plat_munmap(Pico.rom, rom_alloc_size);
  782. Pico.rom = NULL;
  783. }
  784. PicoGameLoaded = 0;
  785. }
  786. static unsigned int rom_crc32(void)
  787. {
  788. unsigned int crc;
  789. elprintf(EL_STATUS, "caclulating CRC32..");
  790. // have to unbyteswap for calculation..
  791. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  792. crc = crc32(0, Pico.rom, Pico.romsize);
  793. Byteswap(Pico.rom, Pico.rom, Pico.romsize);
  794. return crc;
  795. }
  796. static int rom_strcmp(int rom_offset, const char *s1)
  797. {
  798. int i, len = strlen(s1);
  799. const char *s_rom = (const char *)Pico.rom;
  800. if (rom_offset + len > Pico.romsize)
  801. return 0;
  802. for (i = 0; i < len; i++)
  803. if (s1[i] != s_rom[MEM_BE2(i + rom_offset)])
  804. return 1;
  805. return 0;
  806. }
  807. static unsigned int rom_read32(int addr)
  808. {
  809. unsigned short *m = (unsigned short *)(Pico.rom + addr);
  810. return (m[0] << 16) | m[1];
  811. }
  812. static char *sskip(char *s)
  813. {
  814. while (*s && isspace_(*s))
  815. s++;
  816. return s;
  817. }
  818. static void rstrip(char *s)
  819. {
  820. char *p;
  821. for (p = s + strlen(s) - 1; p >= s; p--)
  822. if (isspace_(*p))
  823. *p = 0;
  824. }
  825. static int parse_3_vals(char *p, int *val0, int *val1, int *val2)
  826. {
  827. char *r;
  828. *val0 = strtoul(p, &r, 0);
  829. if (r == p)
  830. goto bad;
  831. p = sskip(r);
  832. if (*p++ != ',')
  833. goto bad;
  834. *val1 = strtoul(p, &r, 0);
  835. if (r == p)
  836. goto bad;
  837. p = sskip(r);
  838. if (*p++ != ',')
  839. goto bad;
  840. *val2 = strtoul(p, &r, 0);
  841. if (r == p)
  842. goto bad;
  843. return 1;
  844. bad:
  845. return 0;
  846. }
  847. static int is_expr(const char *expr, char **pr)
  848. {
  849. int len = strlen(expr);
  850. char *p = *pr;
  851. if (strncmp(expr, p, len) != 0)
  852. return 0;
  853. p = sskip(p + len);
  854. if (*p != '=')
  855. return 0; // wrong or malformed
  856. *pr = sskip(p + 1);
  857. return 1;
  858. }
  859. #include "carthw_cfg.c"
  860. static void parse_carthw(const char *carthw_cfg, int *fill_sram,
  861. int *hw_detected)
  862. {
  863. int line = 0, any_checks_passed = 0, skip_sect = 0;
  864. const char *s, *builtin = builtin_carthw_cfg;
  865. int tmp, rom_crc = 0;
  866. char buff[256], *p, *r;
  867. FILE *f;
  868. f = fopen(carthw_cfg, "r");
  869. if (f == NULL)
  870. f = fopen("pico/carthw.cfg", "r");
  871. if (f == NULL)
  872. elprintf(EL_STATUS, "couldn't open carthw.cfg!");
  873. for (;;)
  874. {
  875. if (f != NULL) {
  876. p = fgets(buff, sizeof(buff), f);
  877. if (p == NULL)
  878. break;
  879. }
  880. else {
  881. if (*builtin == 0)
  882. break;
  883. for (s = builtin; *s != 0 && *s != '\n'; s++)
  884. ;
  885. while (*s == '\n')
  886. s++;
  887. tmp = s - builtin;
  888. if (tmp > sizeof(buff) - 1)
  889. tmp = sizeof(buff) - 1;
  890. memcpy(buff, builtin, tmp);
  891. buff[tmp] = 0;
  892. p = buff;
  893. builtin = s;
  894. }
  895. line++;
  896. p = sskip(p);
  897. if (*p == 0 || *p == '#')
  898. continue;
  899. if (*p == '[') {
  900. any_checks_passed = 0;
  901. skip_sect = 0;
  902. continue;
  903. }
  904. if (skip_sect)
  905. continue;
  906. /* look for checks */
  907. if (is_expr("check_str", &p))
  908. {
  909. int offs;
  910. offs = strtoul(p, &r, 0);
  911. if (offs < 0 || offs > Pico.romsize) {
  912. elprintf(EL_STATUS, "carthw:%d: check_str offs out of range: %d\n", line, offs);
  913. goto bad;
  914. }
  915. p = sskip(r);
  916. if (*p != ',')
  917. goto bad;
  918. p = sskip(p + 1);
  919. if (*p != '"')
  920. goto bad;
  921. p++;
  922. r = strchr(p, '"');
  923. if (r == NULL)
  924. goto bad;
  925. *r = 0;
  926. if (rom_strcmp(offs, p) == 0)
  927. any_checks_passed = 1;
  928. else
  929. skip_sect = 1;
  930. continue;
  931. }
  932. else if (is_expr("check_size_gt", &p))
  933. {
  934. int size;
  935. size = strtoul(p, &r, 0);
  936. if (r == p || size < 0)
  937. goto bad;
  938. if (Pico.romsize > size)
  939. any_checks_passed = 1;
  940. else
  941. skip_sect = 1;
  942. continue;
  943. }
  944. else if (is_expr("check_csum", &p))
  945. {
  946. int csum;
  947. csum = strtoul(p, &r, 0);
  948. if (r == p || (csum & 0xffff0000))
  949. goto bad;
  950. if (csum == (rom_read32(0x18c) & 0xffff))
  951. any_checks_passed = 1;
  952. else
  953. skip_sect = 1;
  954. continue;
  955. }
  956. else if (is_expr("check_crc32", &p))
  957. {
  958. unsigned int crc;
  959. crc = strtoul(p, &r, 0);
  960. if (r == p)
  961. goto bad;
  962. if (rom_crc == 0)
  963. rom_crc = rom_crc32();
  964. if (crc == rom_crc)
  965. any_checks_passed = 1;
  966. else
  967. skip_sect = 1;
  968. continue;
  969. }
  970. /* now time for actions */
  971. if (is_expr("hw", &p)) {
  972. if (!any_checks_passed)
  973. goto no_checks;
  974. *hw_detected = 1;
  975. rstrip(p);
  976. if (strcmp(p, "svp") == 0)
  977. PicoSVPStartup();
  978. else if (strcmp(p, "pico") == 0)
  979. PicoInitPico();
  980. else if (strcmp(p, "prot") == 0)
  981. carthw_sprot_startup();
  982. else if (strcmp(p, "ssf2_mapper") == 0)
  983. carthw_ssf2_startup();
  984. else if (strcmp(p, "x_in_1_mapper") == 0)
  985. carthw_Xin1_startup();
  986. else if (strcmp(p, "realtec_mapper") == 0)
  987. carthw_realtec_startup();
  988. else if (strcmp(p, "radica_mapper") == 0)
  989. carthw_radica_startup();
  990. else if (strcmp(p, "piersolar_mapper") == 0)
  991. carthw_pier_startup();
  992. else if (strcmp(p, "prot_lk3") == 0)
  993. carthw_prot_lk3_startup();
  994. else {
  995. elprintf(EL_STATUS, "carthw:%d: unsupported mapper: %s", line, p);
  996. skip_sect = 1;
  997. *hw_detected = 0;
  998. }
  999. continue;
  1000. }
  1001. if (is_expr("sram_range", &p)) {
  1002. int start, end;
  1003. if (!any_checks_passed)
  1004. goto no_checks;
  1005. rstrip(p);
  1006. start = strtoul(p, &r, 0);
  1007. if (r == p)
  1008. goto bad;
  1009. p = sskip(r);
  1010. if (*p != ',')
  1011. goto bad;
  1012. p = sskip(p + 1);
  1013. end = strtoul(p, &r, 0);
  1014. if (r == p)
  1015. goto bad;
  1016. if (((start | end) & 0xff000000) || start > end) {
  1017. elprintf(EL_STATUS, "carthw:%d: bad sram_range: %08x - %08x", line, start, end);
  1018. goto bad_nomsg;
  1019. }
  1020. Pico.sv.start = start;
  1021. Pico.sv.end = end;
  1022. continue;
  1023. }
  1024. else if (is_expr("prop", &p)) {
  1025. if (!any_checks_passed)
  1026. goto no_checks;
  1027. rstrip(p);
  1028. if (strcmp(p, "no_sram") == 0)
  1029. Pico.sv.flags &= ~SRF_ENABLED;
  1030. else if (strcmp(p, "no_eeprom") == 0)
  1031. Pico.sv.flags &= ~SRF_EEPROM;
  1032. else if (strcmp(p, "filled_sram") == 0)
  1033. *fill_sram = 1;
  1034. else if (strcmp(p, "force_6btn") == 0)
  1035. PicoIn.quirks |= PQUIRK_FORCE_6BTN;
  1036. else {
  1037. elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);
  1038. goto bad_nomsg;
  1039. }
  1040. elprintf(EL_STATUS, "game prop: %s", p);
  1041. continue;
  1042. }
  1043. else if (is_expr("eeprom_type", &p)) {
  1044. int type;
  1045. if (!any_checks_passed)
  1046. goto no_checks;
  1047. rstrip(p);
  1048. type = strtoul(p, &r, 0);
  1049. if (r == p || type < 0)
  1050. goto bad;
  1051. Pico.sv.eeprom_type = type;
  1052. Pico.sv.flags |= SRF_EEPROM;
  1053. continue;
  1054. }
  1055. else if (is_expr("eeprom_lines", &p)) {
  1056. int scl, sda_in, sda_out;
  1057. if (!any_checks_passed)
  1058. goto no_checks;
  1059. rstrip(p);
  1060. if (!parse_3_vals(p, &scl, &sda_in, &sda_out))
  1061. goto bad;
  1062. if (scl < 0 || scl > 15 || sda_in < 0 || sda_in > 15 ||
  1063. sda_out < 0 || sda_out > 15)
  1064. goto bad;
  1065. Pico.sv.eeprom_bit_cl = scl;
  1066. Pico.sv.eeprom_bit_in = sda_in;
  1067. Pico.sv.eeprom_bit_out= sda_out;
  1068. continue;
  1069. }
  1070. else if ((tmp = is_expr("prot_ro_value16", &p)) || is_expr("prot_rw_value16", &p)) {
  1071. int addr, mask, val;
  1072. if (!any_checks_passed)
  1073. goto no_checks;
  1074. rstrip(p);
  1075. if (!parse_3_vals(p, &addr, &mask, &val))
  1076. goto bad;
  1077. carthw_sprot_new_location(addr, mask, val, tmp ? 1 : 0);
  1078. continue;
  1079. }
  1080. bad:
  1081. elprintf(EL_STATUS, "carthw:%d: unrecognized expression: %s", line, buff);
  1082. bad_nomsg:
  1083. skip_sect = 1;
  1084. continue;
  1085. no_checks:
  1086. elprintf(EL_STATUS, "carthw:%d: command without any checks before it: %s", line, buff);
  1087. skip_sect = 1;
  1088. continue;
  1089. }
  1090. if (f != NULL)
  1091. fclose(f);
  1092. }
  1093. /*
  1094. * various cart-specific things, which can't be handled by generic code
  1095. */
  1096. static void PicoCartDetect(const char *carthw_cfg)
  1097. {
  1098. int carthw_detected = 0;
  1099. int fill_sram = 0;
  1100. memset(&Pico.sv, 0, sizeof(Pico.sv));
  1101. if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
  1102. {
  1103. Pico.sv.start = rom_read32(0x1B4) & ~0xff000001; // align
  1104. Pico.sv.end = (rom_read32(0x1B8) & ~0xff000000) | 1;
  1105. if (Pico.rom[0x1B2] & 0x40)
  1106. // EEPROM
  1107. Pico.sv.flags |= SRF_EEPROM;
  1108. Pico.sv.flags |= SRF_ENABLED;
  1109. }
  1110. if (Pico.sv.end == 0 || Pico.sv.start > Pico.sv.end)
  1111. {
  1112. // some games may have bad headers, like S&K and Sonic3
  1113. // note: majority games use 0x200000 as starting address, but there are some which
  1114. // use something else (0x300000 by HardBall '95). Luckily they have good headers.
  1115. Pico.sv.start = 0x200000;
  1116. Pico.sv.end = 0x203FFF;
  1117. Pico.sv.flags |= SRF_ENABLED;
  1118. }
  1119. // set EEPROM defaults, in case it gets detected
  1120. Pico.sv.eeprom_type = 0; // 7bit (24C01)
  1121. Pico.sv.eeprom_bit_cl = 1;
  1122. Pico.sv.eeprom_bit_in = 0;
  1123. Pico.sv.eeprom_bit_out= 0;
  1124. if (carthw_cfg != NULL)
  1125. parse_carthw(carthw_cfg, &fill_sram, &carthw_detected);
  1126. // assume the standard mapper for large roms
  1127. if (!carthw_detected && Pico.romsize > 0x400000)
  1128. carthw_ssf2_startup();
  1129. if (Pico.sv.flags & SRF_ENABLED)
  1130. {
  1131. if (Pico.sv.flags & SRF_EEPROM)
  1132. Pico.sv.size = 0x2000;
  1133. else
  1134. Pico.sv.size = Pico.sv.end - Pico.sv.start + 1;
  1135. Pico.sv.data = calloc(Pico.sv.size, 1);
  1136. if (Pico.sv.data == NULL)
  1137. Pico.sv.flags &= ~SRF_ENABLED;
  1138. if (Pico.sv.eeprom_type == 1) // 1 == 0 in PD EEPROM code
  1139. Pico.sv.eeprom_type = 0;
  1140. }
  1141. if ((Pico.sv.flags & SRF_ENABLED) && fill_sram)
  1142. {
  1143. elprintf(EL_STATUS, "SRAM fill");
  1144. memset(Pico.sv.data, 0xff, Pico.sv.size);
  1145. }
  1146. // Unusual region 'code'
  1147. if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
  1148. *(int *) (Pico.rom + 0x1f0) = 0x20204520;
  1149. }
  1150. // vim:shiftwidth=2:expandtab