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