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