cart.c 27 KB

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