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