cart.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. #ifndef __EPOC32__
  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. #ifndef __EPOC32__ // makes things worse on Symbian
  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. for (i = 0; i < len; i++)
  555. if (s1[i] != s_rom[(i + rom_offset) ^ 1])
  556. return 1;
  557. return 0;
  558. }
  559. static unsigned int rom_read32(int addr)
  560. {
  561. unsigned short *m = (unsigned short *)(Pico.rom + addr);
  562. return (m[0] << 16) | m[1];
  563. }
  564. static char *sskip(char *s)
  565. {
  566. while (*s && isspace_(*s))
  567. s++;
  568. return s;
  569. }
  570. static void rstrip(char *s)
  571. {
  572. char *p;
  573. for (p = s + strlen(s) - 1; p >= s; p--)
  574. if (isspace_(*p))
  575. *p = 0;
  576. }
  577. static int parse_3_vals(char *p, int *val0, int *val1, int *val2)
  578. {
  579. char *r;
  580. *val0 = strtoul(p, &r, 0);
  581. if (r == p)
  582. goto bad;
  583. p = sskip(r);
  584. if (*p++ != ',')
  585. goto bad;
  586. *val1 = strtoul(p, &r, 0);
  587. if (r == p)
  588. goto bad;
  589. p = sskip(r);
  590. if (*p++ != ',')
  591. goto bad;
  592. *val2 = strtoul(p, &r, 0);
  593. if (r == p)
  594. goto bad;
  595. return 1;
  596. bad:
  597. return 0;
  598. }
  599. static int is_expr(const char *expr, char **pr)
  600. {
  601. int len = strlen(expr);
  602. char *p = *pr;
  603. if (strncmp(expr, p, len) != 0)
  604. return 0;
  605. p = sskip(p + len);
  606. if (*p != '=')
  607. return 0; // wrong or malformed
  608. *pr = sskip(p + 1);
  609. return 1;
  610. }
  611. #include "carthw_cfg.c"
  612. static void parse_carthw(const char *carthw_cfg, int *fill_sram)
  613. {
  614. int line = 0, any_checks_passed = 0, skip_sect = 0;
  615. const char *s, *builtin = builtin_carthw_cfg;
  616. int tmp, rom_crc = 0;
  617. char buff[256], *p, *r;
  618. FILE *f;
  619. f = fopen(carthw_cfg, "r");
  620. if (f == NULL)
  621. f = fopen("pico/carthw.cfg", "r");
  622. if (f == NULL)
  623. elprintf(EL_STATUS, "couldn't open carthw.cfg!");
  624. for (;;)
  625. {
  626. if (f != NULL) {
  627. p = fgets(buff, sizeof(buff), f);
  628. if (p == NULL)
  629. break;
  630. }
  631. else {
  632. if (*builtin == 0)
  633. break;
  634. for (s = builtin; *s != 0 && *s != '\n'; s++)
  635. ;
  636. while (*s == '\n')
  637. s++;
  638. tmp = s - builtin;
  639. if (tmp > sizeof(buff) - 1)
  640. tmp = sizeof(buff) - 1;
  641. memcpy(buff, builtin, tmp);
  642. buff[tmp] = 0;
  643. p = buff;
  644. builtin = s;
  645. }
  646. line++;
  647. p = sskip(p);
  648. if (*p == 0 || *p == '#')
  649. continue;
  650. if (*p == '[') {
  651. any_checks_passed = 0;
  652. skip_sect = 0;
  653. continue;
  654. }
  655. if (skip_sect)
  656. continue;
  657. /* look for checks */
  658. if (is_expr("check_str", &p))
  659. {
  660. int offs;
  661. offs = strtoul(p, &r, 0);
  662. if (offs < 0 || offs > Pico.romsize) {
  663. elprintf(EL_STATUS, "carthw:%d: check_str offs out of range: %d\n", line, offs);
  664. goto bad;
  665. }
  666. p = sskip(r);
  667. if (*p != ',')
  668. goto bad;
  669. p = sskip(p + 1);
  670. if (*p != '"')
  671. goto bad;
  672. p++;
  673. r = strchr(p, '"');
  674. if (r == NULL)
  675. goto bad;
  676. *r = 0;
  677. if (rom_strcmp(offs, p) == 0)
  678. any_checks_passed = 1;
  679. else
  680. skip_sect = 1;
  681. continue;
  682. }
  683. else if (is_expr("check_size_gt", &p))
  684. {
  685. int size;
  686. size = strtoul(p, &r, 0);
  687. if (r == p || size < 0)
  688. goto bad;
  689. if (Pico.romsize > size)
  690. any_checks_passed = 1;
  691. else
  692. skip_sect = 1;
  693. continue;
  694. }
  695. else if (is_expr("check_csum", &p))
  696. {
  697. int csum;
  698. csum = strtoul(p, &r, 0);
  699. if (r == p || (csum & 0xffff0000))
  700. goto bad;
  701. if (csum == (rom_read32(0x18c) & 0xffff))
  702. any_checks_passed = 1;
  703. else
  704. skip_sect = 1;
  705. continue;
  706. }
  707. else if (is_expr("check_crc32", &p))
  708. {
  709. unsigned int crc;
  710. crc = strtoul(p, &r, 0);
  711. if (r == p)
  712. goto bad;
  713. if (rom_crc == 0)
  714. rom_crc = rom_crc32();
  715. if (crc == rom_crc)
  716. any_checks_passed = 1;
  717. else
  718. skip_sect = 1;
  719. continue;
  720. }
  721. /* now time for actions */
  722. if (is_expr("hw", &p)) {
  723. if (!any_checks_passed)
  724. goto no_checks;
  725. rstrip(p);
  726. if (strcmp(p, "svp") == 0)
  727. PicoSVPStartup();
  728. else if (strcmp(p, "pico") == 0)
  729. PicoInitPico();
  730. else if (strcmp(p, "prot") == 0)
  731. carthw_sprot_startup();
  732. else if (strcmp(p, "ssf2_mapper") == 0)
  733. carthw_ssf2_startup();
  734. else if (strcmp(p, "x_in_1_mapper") == 0)
  735. carthw_Xin1_startup();
  736. else if (strcmp(p, "realtec_mapper") == 0)
  737. carthw_realtec_startup();
  738. else if (strcmp(p, "radica_mapper") == 0)
  739. carthw_radica_startup();
  740. else if (strcmp(p, "piersolar_mapper") == 0)
  741. carthw_pier_startup();
  742. else if (strcmp(p, "prot_lk3") == 0)
  743. carthw_prot_lk3_startup();
  744. else {
  745. elprintf(EL_STATUS, "carthw:%d: unsupported mapper: %s", line, p);
  746. skip_sect = 1;
  747. }
  748. continue;
  749. }
  750. if (is_expr("sram_range", &p)) {
  751. int start, end;
  752. if (!any_checks_passed)
  753. goto no_checks;
  754. rstrip(p);
  755. start = strtoul(p, &r, 0);
  756. if (r == p)
  757. goto bad;
  758. p = sskip(r);
  759. if (*p != ',')
  760. goto bad;
  761. p = sskip(p + 1);
  762. end = strtoul(p, &r, 0);
  763. if (r == p)
  764. goto bad;
  765. if (((start | end) & 0xff000000) || start > end) {
  766. elprintf(EL_STATUS, "carthw:%d: bad sram_range: %08x - %08x", line, start, end);
  767. goto bad_nomsg;
  768. }
  769. SRam.start = start;
  770. SRam.end = end;
  771. continue;
  772. }
  773. else if (is_expr("prop", &p)) {
  774. if (!any_checks_passed)
  775. goto no_checks;
  776. rstrip(p);
  777. if (strcmp(p, "no_sram") == 0)
  778. SRam.flags &= ~SRF_ENABLED;
  779. else if (strcmp(p, "no_eeprom") == 0)
  780. SRam.flags &= ~SRF_EEPROM;
  781. else if (strcmp(p, "filled_sram") == 0)
  782. *fill_sram = 1;
  783. else {
  784. elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);
  785. goto bad_nomsg;
  786. }
  787. continue;
  788. }
  789. else if (is_expr("eeprom_type", &p)) {
  790. int type;
  791. if (!any_checks_passed)
  792. goto no_checks;
  793. rstrip(p);
  794. type = strtoul(p, &r, 0);
  795. if (r == p || type < 0)
  796. goto bad;
  797. SRam.eeprom_type = type;
  798. SRam.flags |= SRF_EEPROM;
  799. continue;
  800. }
  801. else if (is_expr("eeprom_lines", &p)) {
  802. int scl, sda_in, sda_out;
  803. if (!any_checks_passed)
  804. goto no_checks;
  805. rstrip(p);
  806. if (!parse_3_vals(p, &scl, &sda_in, &sda_out))
  807. goto bad;
  808. if (scl < 0 || scl > 15 || sda_in < 0 || sda_in > 15 ||
  809. sda_out < 0 || sda_out > 15)
  810. goto bad;
  811. SRam.eeprom_bit_cl = scl;
  812. SRam.eeprom_bit_in = sda_in;
  813. SRam.eeprom_bit_out= sda_out;
  814. continue;
  815. }
  816. else if ((tmp = is_expr("prot_ro_value16", &p)) || is_expr("prot_rw_value16", &p)) {
  817. int addr, mask, val;
  818. if (!any_checks_passed)
  819. goto no_checks;
  820. rstrip(p);
  821. if (!parse_3_vals(p, &addr, &mask, &val))
  822. goto bad;
  823. carthw_sprot_new_location(addr, mask, val, tmp ? 1 : 0);
  824. continue;
  825. }
  826. bad:
  827. elprintf(EL_STATUS, "carthw:%d: unrecognized expression: %s", line, buff);
  828. bad_nomsg:
  829. skip_sect = 1;
  830. continue;
  831. no_checks:
  832. elprintf(EL_STATUS, "carthw:%d: command without any checks before it: %s", line, buff);
  833. skip_sect = 1;
  834. continue;
  835. }
  836. if (f != NULL)
  837. fclose(f);
  838. }
  839. /*
  840. * various cart-specific things, which can't be handled by generic code
  841. */
  842. static void PicoCartDetect(const char *carthw_cfg)
  843. {
  844. int fill_sram = 0;
  845. memset(&SRam, 0, sizeof(SRam));
  846. if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
  847. {
  848. SRam.start = rom_read32(0x1B4) & ~0xff000001; // align
  849. SRam.end = (rom_read32(0x1B8) & ~0xff000000) | 1;
  850. if (Pico.rom[0x1B2] & 0x40)
  851. // EEPROM
  852. SRam.flags |= SRF_EEPROM;
  853. SRam.flags |= SRF_ENABLED;
  854. }
  855. if (SRam.end == 0 || SRam.start > SRam.end)
  856. {
  857. // some games may have bad headers, like S&K and Sonic3
  858. // note: majority games use 0x200000 as starting address, but there are some which
  859. // use something else (0x300000 by HardBall '95). Luckily they have good headers.
  860. SRam.start = 0x200000;
  861. SRam.end = 0x203FFF;
  862. SRam.flags |= SRF_ENABLED;
  863. }
  864. // set EEPROM defaults, in case it gets detected
  865. SRam.eeprom_type = 0; // 7bit (24C01)
  866. SRam.eeprom_bit_cl = 1;
  867. SRam.eeprom_bit_in = 0;
  868. SRam.eeprom_bit_out= 0;
  869. if (carthw_cfg != NULL)
  870. parse_carthw(carthw_cfg, &fill_sram);
  871. if (SRam.flags & SRF_ENABLED)
  872. {
  873. if (SRam.flags & SRF_EEPROM)
  874. SRam.size = 0x2000;
  875. else
  876. SRam.size = SRam.end - SRam.start + 1;
  877. SRam.data = calloc(SRam.size, 1);
  878. if (SRam.data == NULL)
  879. SRam.flags &= ~SRF_ENABLED;
  880. if (SRam.eeprom_type == 1) // 1 == 0 in PD EEPROM code
  881. SRam.eeprom_type = 0;
  882. }
  883. if ((SRam.flags & SRF_ENABLED) && fill_sram)
  884. {
  885. elprintf(EL_STATUS, "SRAM fill");
  886. memset(SRam.data, 0xff, SRam.size);
  887. }
  888. // Unusual region 'code'
  889. if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
  890. *(int *) (Pico.rom + 0x1f0) = 0x20204520;
  891. }
  892. // vim:shiftwidth=2:expandtab