Cart.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // This is part of Pico Library
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #include "PicoInt.h"
  7. #include "../zlib/zlib.h"
  8. #include "../unzip/unzip.h"
  9. #include "../unzip/unzip_stream.h"
  10. static char *rom_exts[] = { "bin", "gen", "smd", "iso" };
  11. void (*PicoCartUnloadHook)(void) = NULL;
  12. void (*PicoCartLoadProgressCB)(int percent) = NULL;
  13. void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c
  14. static void PicoCartDetect(void);
  15. /* cso struct */
  16. typedef struct _cso_struct
  17. {
  18. unsigned char in_buff[2*2048];
  19. unsigned char out_buff[2048];
  20. struct {
  21. char magic[4];
  22. unsigned int unused;
  23. unsigned int total_bytes;
  24. unsigned int total_bytes_high; // ignored here
  25. unsigned int block_size; // 10h
  26. unsigned char ver;
  27. unsigned char align;
  28. unsigned char reserved[2];
  29. } header;
  30. unsigned int fpos_in; // input file read pointer
  31. unsigned int fpos_out; // pos in virtual decompressed file
  32. int block_in_buff; // block which we have read in in_buff
  33. int pad;
  34. int index[0];
  35. }
  36. cso_struct;
  37. static int uncompress2(void *dest, int destLen, void *source, int sourceLen)
  38. {
  39. z_stream stream;
  40. int err;
  41. stream.next_in = (Bytef*)source;
  42. stream.avail_in = (uInt)sourceLen;
  43. stream.next_out = dest;
  44. stream.avail_out = (uInt)destLen;
  45. stream.zalloc = NULL;
  46. stream.zfree = NULL;
  47. err = inflateInit2(&stream, -15);
  48. if (err != Z_OK) return err;
  49. err = inflate(&stream, Z_FINISH);
  50. if (err != Z_STREAM_END) {
  51. inflateEnd(&stream);
  52. return err;
  53. }
  54. //*destLen = stream.total_out;
  55. return inflateEnd(&stream);
  56. }
  57. pm_file *pm_open(const char *path)
  58. {
  59. pm_file *file = NULL;
  60. const char *ext;
  61. FILE *f;
  62. if (path == NULL) return NULL;
  63. if (strlen(path) < 5) ext = NULL; // no ext
  64. else ext = path + strlen(path) - 3;
  65. if (ext && strcasecmp(ext, "zip") == 0)
  66. {
  67. struct zipent *zipentry;
  68. gzFile gzf = NULL;
  69. ZIP *zipfile;
  70. int i;
  71. zipfile = openzip(path);
  72. if (zipfile != NULL)
  73. {
  74. /* search for suitable file (right extension or large enough file) */
  75. while ((zipentry = readzip(zipfile)) != NULL)
  76. {
  77. if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
  78. if (strlen(zipentry->name) < 5) continue;
  79. ext = zipentry->name+strlen(zipentry->name)-3;
  80. for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
  81. if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
  82. }
  83. /* zipfile given, but nothing found suitable for us inside */
  84. goto zip_failed;
  85. found_rom_zip:
  86. /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
  87. gzf = zip2gz(zipfile, zipentry);
  88. if (gzf == NULL) goto zip_failed;
  89. file = malloc(sizeof(*file));
  90. if (file == NULL) goto zip_failed;
  91. file->file = zipfile;
  92. file->param = gzf;
  93. file->size = zipentry->uncompressed_size;
  94. file->type = PMT_ZIP;
  95. return file;
  96. zip_failed:
  97. if (gzf) {
  98. gzclose(gzf);
  99. zipfile->fp = NULL; // gzclose() closed it
  100. }
  101. closezip(zipfile);
  102. return NULL;
  103. }
  104. }
  105. else if (ext && strcasecmp(ext, "cso") == 0)
  106. {
  107. cso_struct *cso = NULL, *tmp = NULL;
  108. int size;
  109. f = fopen(path, "rb");
  110. if (f == NULL)
  111. goto cso_failed;
  112. /* we use our own buffering */
  113. setvbuf(f, NULL, _IONBF, 0);
  114. cso = malloc(sizeof(*cso));
  115. if (cso == NULL)
  116. goto cso_failed;
  117. if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
  118. goto cso_failed;
  119. if (strncmp(cso->header.magic, "CISO", 4) != 0) {
  120. elprintf(EL_STATUS, "cso: bad header");
  121. goto cso_failed;
  122. }
  123. if (cso->header.block_size != 2048) {
  124. elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
  125. goto cso_failed;
  126. }
  127. size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
  128. tmp = realloc(cso, size);
  129. if (tmp == NULL)
  130. goto cso_failed;
  131. cso = tmp;
  132. elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
  133. size -= sizeof(*cso); // index size
  134. if (fread(cso->index, 1, size, f) != size) {
  135. elprintf(EL_STATUS, "cso: premature EOF");
  136. goto cso_failed;
  137. }
  138. // all ok
  139. cso->fpos_in = ftell(f);
  140. cso->fpos_out = 0;
  141. cso->block_in_buff = -1;
  142. file = malloc(sizeof(*file));
  143. if (file == NULL) goto cso_failed;
  144. file->file = f;
  145. file->param = cso;
  146. file->size = cso->header.total_bytes;
  147. file->type = PMT_CSO;
  148. return file;
  149. cso_failed:
  150. if (cso != NULL) free(cso);
  151. if (f != NULL) fclose(f);
  152. return NULL;
  153. }
  154. /* not a zip, treat as uncompressed file */
  155. f = fopen(path, "rb");
  156. if (f == NULL) return NULL;
  157. /* we use our own buffering */
  158. setvbuf(f, NULL, _IONBF, 0);
  159. file = malloc(sizeof(*file));
  160. if (file == NULL) {
  161. fclose(f);
  162. return NULL;
  163. }
  164. fseek(f, 0, SEEK_END);
  165. file->file = f;
  166. file->param = NULL;
  167. file->size = ftell(f);
  168. file->type = PMT_UNCOMPRESSED;
  169. fseek(f, 0, SEEK_SET);
  170. return file;
  171. }
  172. size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
  173. {
  174. int ret;
  175. if (stream->type == PMT_UNCOMPRESSED)
  176. {
  177. ret = fread(ptr, 1, bytes, stream->file);
  178. }
  179. else if (stream->type == PMT_ZIP)
  180. {
  181. gzFile gf = stream->param;
  182. int err;
  183. ret = gzread(gf, ptr, bytes);
  184. err = gzerror2(gf);
  185. if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
  186. /* we must reset stream pointer or else next seek/read fails */
  187. gzrewind(gf);
  188. }
  189. else if (stream->type == PMT_CSO)
  190. {
  191. cso_struct *cso = stream->param;
  192. int read_pos, read_len, out_offs, rret;
  193. int block = cso->fpos_out >> 11;
  194. int index = cso->index[block];
  195. int index_end = cso->index[block+1];
  196. unsigned char *out = ptr, *tmp_dst;
  197. ret = 0;
  198. while (bytes != 0)
  199. {
  200. out_offs = cso->fpos_out&0x7ff;
  201. if (out_offs == 0 && bytes >= 2048)
  202. tmp_dst = out;
  203. else tmp_dst = cso->out_buff;
  204. read_pos = (index&0x7fffffff) << cso->header.align;
  205. if (index < 0) {
  206. if (read_pos != cso->fpos_in)
  207. fseek(stream->file, read_pos, SEEK_SET);
  208. rret = fread(tmp_dst, 1, 2048, stream->file);
  209. cso->fpos_in = read_pos + rret;
  210. if (rret != 2048) break;
  211. } else {
  212. read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
  213. if (block != cso->block_in_buff)
  214. {
  215. if (read_pos != cso->fpos_in)
  216. fseek(stream->file, read_pos, SEEK_SET);
  217. rret = fread(cso->in_buff, 1, read_len, stream->file);
  218. cso->fpos_in = read_pos + rret;
  219. if (rret != read_len) {
  220. elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);
  221. break;
  222. }
  223. cso->block_in_buff = block;
  224. }
  225. rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);
  226. if (rret != 0) {
  227. elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
  228. break;
  229. }
  230. }
  231. rret = 2048;
  232. if (out_offs != 0 || bytes < 2048) {
  233. //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);
  234. if (bytes < rret) rret = bytes;
  235. if (2048 - out_offs < rret) rret = 2048 - out_offs;
  236. memcpy(out, tmp_dst + out_offs, rret);
  237. }
  238. ret += rret;
  239. out += rret;
  240. cso->fpos_out += rret;
  241. bytes -= rret;
  242. block++;
  243. index = index_end;
  244. index_end = cso->index[block+1];
  245. }
  246. }
  247. else
  248. ret = 0;
  249. return ret;
  250. }
  251. int pm_seek(pm_file *stream, long offset, int whence)
  252. {
  253. if (stream->type == PMT_UNCOMPRESSED)
  254. {
  255. fseek(stream->file, offset, whence);
  256. return ftell(stream->file);
  257. }
  258. else if (stream->type == PMT_ZIP)
  259. {
  260. if (PicoMessage != NULL && offset > 6*1024*1024) {
  261. long pos = gztell((gzFile) stream->param);
  262. if (offset < pos || offset - pos > 6*1024*1024)
  263. PicoMessage("Decompressing data...");
  264. }
  265. return gzseek((gzFile) stream->param, offset, whence);
  266. }
  267. else if (stream->type == PMT_CSO)
  268. {
  269. cso_struct *cso = stream->param;
  270. switch (whence)
  271. {
  272. case SEEK_CUR: cso->fpos_out += offset; break;
  273. case SEEK_SET: cso->fpos_out = offset; break;
  274. case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;
  275. }
  276. return cso->fpos_out;
  277. }
  278. else
  279. return -1;
  280. }
  281. int pm_close(pm_file *fp)
  282. {
  283. int ret = 0;
  284. if (fp == NULL) return EOF;
  285. if (fp->type == PMT_UNCOMPRESSED)
  286. {
  287. fclose(fp->file);
  288. }
  289. else if (fp->type == PMT_ZIP)
  290. {
  291. ZIP *zipfile = fp->file;
  292. gzclose((gzFile) fp->param);
  293. zipfile->fp = NULL; // gzclose() closed it
  294. closezip(zipfile);
  295. }
  296. else if (fp->type == PMT_CSO)
  297. {
  298. free(fp->param);
  299. fclose(fp->file);
  300. }
  301. else
  302. ret = EOF;
  303. free(fp);
  304. return ret;
  305. }
  306. void Byteswap(unsigned char *data,int len)
  307. {
  308. int i=0;
  309. if (len<2) return; // Too short
  310. do
  311. {
  312. unsigned short *pd=(unsigned short *)(data+i);
  313. int value=*pd; // Get 2 bytes
  314. value=(value<<8)|(value>>8); // Byteswap it
  315. *pd=(unsigned short)value; // Put 2b ytes
  316. i+=2;
  317. }
  318. while (i+2<=len);
  319. }
  320. // Interleve a 16k block and byteswap
  321. static int InterleveBlock(unsigned char *dest,unsigned char *src)
  322. {
  323. int i=0;
  324. for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
  325. for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
  326. return 0;
  327. }
  328. // Decode a SMD file
  329. static int DecodeSmd(unsigned char *data,int len)
  330. {
  331. unsigned char *temp=NULL;
  332. int i=0;
  333. temp=(unsigned char *)malloc(0x4000);
  334. if (temp==NULL) return 1;
  335. memset(temp,0,0x4000);
  336. // Interleve each 16k block and shift down by 0x200:
  337. for (i=0; i+0x4200<=len; i+=0x4000)
  338. {
  339. InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
  340. memcpy(data+i,temp,0x4000); // Copy back in
  341. }
  342. free(temp);
  343. return 0;
  344. }
  345. static unsigned char *cd_realloc(void *old, int filesize)
  346. {
  347. unsigned char *rom;
  348. rom=realloc(old, sizeof(mcd_state));
  349. if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);
  350. return rom;
  351. }
  352. static unsigned char *PicoCartAlloc(int filesize)
  353. {
  354. int alloc_size;
  355. unsigned char *rom;
  356. if (PicoAHW & PAHW_MCD) return cd_realloc(NULL, filesize);
  357. alloc_size=filesize+0x7ffff;
  358. if((filesize&0x3fff)==0x200) alloc_size-=0x200;
  359. alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
  360. if((filesize&0x3fff)==0x200) alloc_size+=0x200;
  361. else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
  362. // Allocate space for the rom plus padding
  363. rom=(unsigned char *)malloc(alloc_size);
  364. if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
  365. return rom;
  366. }
  367. int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
  368. {
  369. unsigned char *rom=NULL; int size, bytes_read;
  370. if (f==NULL) return 1;
  371. size=f->size;
  372. if (size <= 0) return 1;
  373. size=(size+3)&~3; // Round up to a multiple of 4
  374. // Allocate space for the rom plus padding
  375. rom=PicoCartAlloc(size);
  376. if (rom==NULL) {
  377. elprintf(EL_STATUS, "out of memory (wanted %i)", size);
  378. return 1;
  379. }
  380. if (PicoCartLoadProgressCB != NULL)
  381. {
  382. // read ROM in blocks, just for fun
  383. int ret;
  384. unsigned char *p = rom;
  385. bytes_read=0;
  386. do
  387. {
  388. int todo = size - bytes_read;
  389. if (todo > 256*1024) todo = 256*1024;
  390. ret = pm_read(p,todo,f);
  391. bytes_read += ret;
  392. p += ret;
  393. PicoCartLoadProgressCB(bytes_read * 100 / size);
  394. }
  395. while (ret > 0);
  396. }
  397. else
  398. bytes_read = pm_read(rom,size,f); // Load up the rom
  399. if (bytes_read <= 0) {
  400. elprintf(EL_STATUS, "read failed");
  401. free(rom);
  402. return 1;
  403. }
  404. // maybe we are loading MegaCD BIOS?
  405. if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
  406. !strncmp((char *)rom+0x128, "BOOT", 4))) {
  407. PicoAHW |= PAHW_MCD;
  408. rom = cd_realloc(rom, size);
  409. }
  410. // Check for SMD:
  411. if (size >= 0x4200 && (size&0x3fff)==0x200 &&
  412. ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
  413. DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
  414. }
  415. else Byteswap(rom,size); // Just byteswap
  416. if (prom) *prom=rom;
  417. if (psize) *psize=size;
  418. return 0;
  419. }
  420. // Insert a cartridge:
  421. int PicoCartInsert(unsigned char *rom,unsigned int romsize)
  422. {
  423. // notaz: add a 68k "jump one op back" opcode to the end of ROM.
  424. // This will hang the emu, but will prevent nasty crashes.
  425. // note: 4 bytes are padded to every ROM
  426. if (rom != NULL)
  427. *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
  428. Pico.rom=rom;
  429. Pico.romsize=romsize;
  430. if (SRam.data) {
  431. free(SRam.data);
  432. SRam.data = NULL;
  433. }
  434. if (PicoCartUnloadHook != NULL) {
  435. PicoCartUnloadHook();
  436. PicoCartUnloadHook = NULL;
  437. }
  438. PicoAHW &= PAHW_MCD;
  439. PicoMemResetHooks();
  440. PicoDmaHook = NULL;
  441. PicoResetHook = NULL;
  442. PicoLineHook = NULL;
  443. PicoLoadStateHook = NULL;
  444. carthw_chunks = NULL;
  445. PicoMemReset();
  446. if (!(PicoAHW & PAHW_MCD))
  447. PicoCartDetect();
  448. // setup correct memory map for loaded ROM
  449. // call PicoMemReset again due to possible memmap change
  450. switch (PicoAHW) {
  451. default:
  452. elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);
  453. case 0:
  454. case PAHW_SVP: PicoMemSetup(); break;
  455. case PAHW_MCD: PicoMemSetupCD(); break;
  456. case PAHW_PICO: PicoMemSetupPico(); break;
  457. }
  458. PicoMemReset();
  459. PicoPower();
  460. return 0;
  461. }
  462. void PicoCartUnload(void)
  463. {
  464. if (Pico.rom != NULL) {
  465. SekFinishIdleDet();
  466. free(Pico.rom);
  467. Pico.rom=NULL;
  468. }
  469. }
  470. static int rom_strcmp(int rom_offset, const char *s1)
  471. {
  472. int i, len = strlen(s1);
  473. const char *s_rom = (const char *)Pico.rom + rom_offset;
  474. for (i = 0; i < len; i++)
  475. if (s1[i] != s_rom[i^1])
  476. return 1;
  477. return 0;
  478. }
  479. static int name_cmp(const char *name)
  480. {
  481. return rom_strcmp(0x150, name);
  482. }
  483. /*
  484. * various cart-specific things, which can't be handled by generic code
  485. * (maybe I should start using CRC for this stuff?)
  486. */
  487. static void PicoCartDetect(void)
  488. {
  489. int sram_size = 0, csum;
  490. Pico.m.sram_reg = 0;
  491. csum = PicoRead32(0x18c) & 0xffff;
  492. if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
  493. {
  494. if (Pico.rom[0x1B2] & 0x40)
  495. {
  496. // EEPROM
  497. SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games
  498. SRam.end = PicoRead32(0x1B8);
  499. sram_size = 0x2000;
  500. Pico.m.sram_reg |= 4;
  501. } else {
  502. // normal SRAM
  503. SRam.start = PicoRead32(0x1B4) & ~0xff;
  504. SRam.end = PicoRead32(0x1B8) | 1;
  505. sram_size = SRam.end - SRam.start + 1;
  506. }
  507. SRam.start &= ~0xff000000;
  508. SRam.end &= ~0xff000000;
  509. Pico.m.sram_reg |= 0x10; // SRAM was detected
  510. }
  511. if (sram_size <= 0)
  512. {
  513. // some games may have bad headers, like S&K and Sonic3
  514. // note: majority games use 0x200000 as starting address, but there are some which
  515. // use something else (0x300000 by HardBall '95). Luckily they have good headers.
  516. SRam.start = 0x200000;
  517. SRam.end = 0x203FFF;
  518. sram_size = 0x004000;
  519. }
  520. // this game actually doesn't have SRAM, but some weird protection
  521. if (rom_strcmp(0x120, "PUGGSY") == 0)
  522. {
  523. SRam.start = SRam.end = sram_size = 0;
  524. }
  525. if (sram_size)
  526. {
  527. SRam.data = (unsigned char *) calloc(sram_size, 1);
  528. if (SRam.data == NULL) return;
  529. }
  530. SRam.changed = 0;
  531. // set EEPROM defaults, in case it gets detected
  532. SRam.eeprom_type = 0; // 7bit (24C01)
  533. SRam.eeprom_abits = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in
  534. SRam.eeprom_bit_cl = 1;
  535. SRam.eeprom_bit_in = 0;
  536. SRam.eeprom_bit_out= 0;
  537. // some known EEPROM data (thanks to EkeEke)
  538. if (name_cmp("COLLEGE SLAM") == 0 ||
  539. name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)
  540. {
  541. SRam.eeprom_type = 3;
  542. SRam.eeprom_abits = 2;
  543. SRam.eeprom_bit_cl = 0;
  544. }
  545. else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||
  546. name_cmp("NFL QUARTERBACK CLUB") == 0)
  547. {
  548. SRam.eeprom_type = 2;
  549. SRam.eeprom_abits = 2;
  550. SRam.eeprom_bit_cl = 0;
  551. }
  552. else if (name_cmp("NBA JAM") == 0)
  553. {
  554. SRam.eeprom_type = 2;
  555. SRam.eeprom_bit_out = 1;
  556. SRam.eeprom_abits = 0;
  557. }
  558. else if (name_cmp("NHLPA HOCKEY '93") == 0 ||
  559. name_cmp("NHLPA Hockey '93") == 0 ||
  560. name_cmp("RINGS OF POWER") == 0)
  561. {
  562. SRam.start = SRam.end = 0x200000;
  563. Pico.m.sram_reg = 0x14;
  564. SRam.eeprom_abits = 0;
  565. SRam.eeprom_bit_cl = 6;
  566. SRam.eeprom_bit_in = 7;
  567. SRam.eeprom_bit_out= 7;
  568. }
  569. else if ( name_cmp("MICRO MACHINES II") == 0 ||
  570. (name_cmp(" ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}
  571. (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))
  572. {
  573. SRam.start = 0x300000;
  574. SRam.end = 0x380001;
  575. Pico.m.sram_reg = 0x14;
  576. SRam.eeprom_type = 2;
  577. SRam.eeprom_abits = 0;
  578. SRam.eeprom_bit_cl = 1;
  579. SRam.eeprom_bit_in = 0;
  580. SRam.eeprom_bit_out= 7;
  581. }
  582. // SVP detection
  583. else if (name_cmp("Virtua Racing") == 0 ||
  584. name_cmp("VIRTUA RACING") == 0)
  585. {
  586. PicoSVPStartup();
  587. }
  588. // Pico
  589. else if (rom_strcmp(0x100, "SEGA PICO") == 0 ||
  590. rom_strcmp(0x100, "IMA IKUNOUJYUKU") == 0) // what is that supposed to mean?
  591. {
  592. PicoInitPico();
  593. }
  594. // Detect 12-in-1 mapper
  595. else if ((name_cmp("ROBOCOP 3") == 0 && Pico.romsize == 0x200000) ||
  596. (rom_strcmp(0x160, "FLICKY") == 0 && Pico.romsize >= 0x200000) ||
  597. (name_cmp(" SHOVE IT!") == 0 && Pico.romsize >= 0x200000) ||
  598. (name_cmp("MS PACMAN") == 0 && Pico.romsize >= 0x200000)) // bad dump?
  599. {
  600. carthw_12in1_startup();
  601. }
  602. // Realtec mapper
  603. else if (Pico.romsize == 512*1024 && (
  604. rom_strcmp(0x94, "THE EARTH DEFEND") == 0 ||
  605. rom_strcmp(0xfe, "WISEGAME 11-03-1993") == 0 || // Funny World
  606. rom_strcmp(0x95, "MALLET LEGEND ") == 0)) // Whac-A-Critter
  607. {
  608. carthw_realtec_startup();
  609. }
  610. // Radica mapper
  611. else if (name_cmp("KID CHAMELEON") == 0 && Pico.romsize > 0x100000)
  612. {
  613. carthw_radica_startup();
  614. }
  615. // Some games malfunction if SRAM is not filled with 0xff
  616. if (name_cmp("DINO DINI'S SOCCER") == 0 ||
  617. name_cmp("MICRO MACHINES II") == 0)
  618. {
  619. memset(SRam.data, 0xff, sram_size);
  620. }
  621. // Unusual region 'code'
  622. if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
  623. *(int *) (Pico.rom+0x1f0) = 0x20204520;
  624. }