1 // This is part of Pico Library
\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
7 // For commercial use, separate licencing terms must be obtained.
\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
16 static char *rom_exts[] = { "bin", "gen", "smd", "iso" };
\r
18 void (*PicoCartUnloadHook)(void) = NULL;
\r
20 void (*PicoCartLoadProgressCB)(int percent) = NULL;
\r
21 void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c
\r
23 static void PicoCartDetect(void);
\r
26 typedef struct _cso_struct
\r
28 unsigned char in_buff[2*2048];
\r
29 unsigned char out_buff[2048];
\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
37 unsigned char align;
\r
38 unsigned char reserved[2];
\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
48 static int uncompress2(void *dest, int destLen, void *source, int sourceLen)
\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
58 stream.zalloc = NULL;
\r
59 stream.zfree = NULL;
\r
61 err = inflateInit2(&stream, -15);
\r
62 if (err != Z_OK) return err;
\r
64 err = inflate(&stream, Z_FINISH);
\r
65 if (err != Z_STREAM_END) {
\r
66 inflateEnd(&stream);
\r
69 //*destLen = stream.total_out;
\r
71 return inflateEnd(&stream);
\r
74 pm_file *pm_open(const char *path)
\r
76 pm_file *file = NULL;
\r
80 if (path == NULL) return NULL;
\r
82 if (strlen(path) < 5) ext = NULL; // no ext
\r
83 else ext = path + strlen(path) - 3;
\r
85 if (ext && strcasecmp(ext, "zip") == 0)
\r
87 struct zipent *zipentry;
\r
92 zipfile = openzip(path);
\r
94 if (zipfile != NULL)
\r
96 /* search for suitable file (right extension or large enough file) */
\r
97 while ((zipentry = readzip(zipfile)) != NULL)
\r
99 if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
\r
100 if (strlen(zipentry->name) < 5) continue;
\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
107 /* zipfile given, but nothing found suitable for us inside */
\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
115 file = malloc(sizeof(*file));
\r
116 if (file == NULL) goto zip_failed;
\r
117 file->file = zipfile;
\r
119 file->size = zipentry->uncompressed_size;
\r
120 file->type = PMT_ZIP;
\r
126 zipfile->fp = NULL; // gzclose() closed it
\r
132 else if (ext && strcasecmp(ext, "cso") == 0)
\r
134 cso_struct *cso = NULL, *tmp = NULL;
\r
136 f = fopen(path, "rb");
\r
140 /* we use our own buffering */
\r
141 setvbuf(f, NULL, _IONBF, 0);
\r
143 cso = malloc(sizeof(*cso));
\r
147 if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
\r
150 if (strncmp(cso->header.magic, "CISO", 4) != 0) {
\r
151 elprintf(EL_STATUS, "cso: bad header");
\r
155 if (cso->header.block_size != 2048) {
\r
156 elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
\r
160 size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
\r
161 tmp = realloc(cso, size);
\r
165 elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
\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
174 cso->fpos_in = ftell(f);
\r
176 cso->block_in_buff = -1;
\r
177 file = malloc(sizeof(*file));
\r
178 if (file == NULL) goto cso_failed;
\r
181 file->size = cso->header.total_bytes;
\r
182 file->type = PMT_CSO;
\r
186 if (cso != NULL) free(cso);
\r
187 if (f != NULL) fclose(f);
\r
191 /* not a zip, treat as uncompressed file */
\r
192 f = fopen(path, "rb");
\r
193 if (f == NULL) return NULL;
\r
195 /* we use our own buffering */
\r
196 setvbuf(f, NULL, _IONBF, 0);
\r
198 file = malloc(sizeof(*file));
\r
199 if (file == NULL) {
\r
203 fseek(f, 0, SEEK_END);
\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
213 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
\r
217 if (stream->type == PMT_UNCOMPRESSED)
\r
219 ret = fread(ptr, 1, bytes, stream->file);
\r
221 else if (stream->type == PMT_ZIP)
\r
223 gzFile gf = stream->param;
\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
231 else if (stream->type == PMT_CSO)
\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
243 out_offs = cso->fpos_out&0x7ff;
\r
244 if (out_offs == 0 && bytes >= 2048)
\r
246 else tmp_dst = cso->out_buff;
\r
248 read_pos = (index&0x7fffffff) << cso->header.align;
\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
257 read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
\r
258 if (block != cso->block_in_buff)
\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
268 cso->block_in_buff = block;
\r
270 rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);
\r
272 elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
\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
286 cso->fpos_out += rret;
\r
290 index_end = cso->index[block+1];
\r
299 int pm_seek(pm_file *stream, long offset, int whence)
\r
301 if (stream->type == PMT_UNCOMPRESSED)
\r
303 fseek(stream->file, offset, whence);
\r
304 return ftell(stream->file);
\r
306 else if (stream->type == PMT_ZIP)
\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
313 return gzseek((gzFile) stream->param, offset, whence);
\r
315 else if (stream->type == PMT_CSO)
\r
317 cso_struct *cso = stream->param;
\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
324 return cso->fpos_out;
\r
330 int pm_close(pm_file *fp)
\r
334 if (fp == NULL) return EOF;
\r
336 if (fp->type == PMT_UNCOMPRESSED)
\r
340 else if (fp->type == PMT_ZIP)
\r
342 ZIP *zipfile = fp->file;
\r
343 gzclose((gzFile) fp->param);
\r
344 zipfile->fp = NULL; // gzclose() closed it
\r
347 else if (fp->type == PMT_CSO)
\r
360 void Byteswap(unsigned char *data,int len)
\r
364 if (len<2) return; // Too short
\r
368 unsigned short *pd=(unsigned short *)(data+i);
\r
369 int value=*pd; // Get 2 bytes
\r
371 value=(value<<8)|(value>>8); // Byteswap it
\r
372 *pd=(unsigned short)value; // Put 2b ytes
\r
378 // Interleve a 16k block and byteswap
\r
379 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\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
387 // Decode a SMD file
\r
388 static int DecodeSmd(unsigned char *data,int len)
\r
390 unsigned char *temp=NULL;
\r
393 temp=(unsigned char *)malloc(0x4000);
\r
394 if (temp==NULL) return 1;
\r
395 memset(temp,0,0x4000);
\r
397 // Interleve each 16k block and shift down by 0x200:
\r
398 for (i=0; i+0x4200<=len; i+=0x4000)
\r
400 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
401 memcpy(data+i,temp,0x4000); // Copy back in
\r
408 static unsigned char *cd_realloc(void *old, int filesize)
\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
416 static unsigned char *PicoCartAlloc(int filesize)
\r
419 unsigned char *rom;
\r
421 if (PicoAHW & PAHW_MCD) return cd_realloc(NULL, filesize);
\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
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
435 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
\r
437 unsigned char *rom=NULL; int size, bytes_read;
\r
438 if (f==NULL) return 1;
\r
441 if (size <= 0) return 1;
\r
442 size=(size+3)&~3; // Round up to a multiple of 4
\r
444 // Allocate space for the rom plus padding
\r
445 rom=PicoCartAlloc(size);
\r
447 elprintf(EL_STATUS, "out of memory (wanted %i)", size);
\r
451 if (PicoCartLoadProgressCB != NULL)
\r
453 // read ROM in blocks, just for fun
\r
455 unsigned char *p = rom;
\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
464 PicoCartLoadProgressCB(bytes_read * 100 / size);
\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
476 // maybe we are loading MegaCD BIOS?
\r
477 if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
\r
478 !strncmp((char *)rom+0x128, "BOOT", 4))) {
\r
479 PicoAHW |= PAHW_MCD;
\r
480 rom = cd_realloc(rom, size);
\r
484 if (size >= 0x4200 && (size&0x3fff)==0x200 &&
\r
485 ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
\r
486 DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
\r
488 else Byteswap(rom,size); // Just byteswap
\r
490 if (prom) *prom=rom;
\r
491 if (psize) *psize=size;
\r
496 // Insert a cartridge:
\r
497 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
499 // notaz: add a 68k "jump one op back" opcode to the end of ROM.
\r
500 // This will hang the emu, but will prevent nasty crashes.
\r
501 // note: 4 bytes are padded to every ROM
\r
503 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
\r
506 Pico.romsize=romsize;
\r
513 if (PicoCartUnloadHook != NULL) {
\r
514 PicoCartUnloadHook();
\r
515 PicoCartUnloadHook = NULL;
\r
518 PicoAHW &= PAHW_MCD;
\r
520 PicoMemResetHooks();
\r
521 PicoDmaHook = NULL;
\r
522 PicoResetHook = NULL;
\r
523 PicoLineHook = NULL;
\r
524 PicoLoadStateHook = NULL;
\r
525 carthw_chunks = NULL;
\r
529 if (!(PicoAHW & PAHW_MCD))
\r
532 // setup correct memory map for loaded ROM
\r
533 // call PicoMemReset again due to possible memmap change
\r
536 elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);
\r
538 case PAHW_SVP: PicoMemSetup(); break;
\r
539 case PAHW_MCD: PicoMemSetupCD(); break;
\r
540 case PAHW_PICO: PicoMemSetupPico(); break;
\r
548 int PicoCartUnload(void)
\r
550 if (Pico.rom != NULL) {
\r
557 static int rom_strcmp(int rom_offset, const char *s1)
\r
559 int i, len = strlen(s1);
\r
560 const char *s_rom = (const char *)Pico.rom + rom_offset;
\r
561 for (i = 0; i < len; i++)
\r
562 if (s1[i] != s_rom[i^1])
\r
567 static int name_cmp(const char *name)
\r
569 return rom_strcmp(0x150, name);
\r
573 * various cart-specific things, which can't be handled by generic code
\r
574 * (maybe I should start using CRC for this stuff?)
\r
576 static void PicoCartDetect(void)
\r
578 int sram_size = 0, csum;
\r
579 Pico.m.sram_reg = 0;
\r
581 csum = PicoRead32(0x18c) & 0xffff;
\r
583 if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
\r
585 if (Pico.rom[0x1B2] & 0x40)
\r
588 SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games
\r
589 SRam.end = PicoRead32(0x1B8);
\r
590 sram_size = 0x2000;
\r
591 Pico.m.sram_reg |= 4;
\r
594 SRam.start = PicoRead32(0x1B4) & ~0xff;
\r
595 SRam.end = PicoRead32(0x1B8) | 1;
\r
596 sram_size = SRam.end - SRam.start + 1;
\r
598 SRam.start &= ~0xff000000;
\r
599 SRam.end &= ~0xff000000;
\r
600 Pico.m.sram_reg |= 0x10; // SRAM was detected
\r
602 if (sram_size <= 0)
\r
604 // some games may have bad headers, like S&K and Sonic3
\r
605 // note: majority games use 0x200000 as starting address, but there are some which
\r
606 // use something else (0x300000 by HardBall '95). Luckily they have good headers.
\r
607 SRam.start = 0x200000;
\r
608 SRam.end = 0x203FFF;
\r
609 sram_size = 0x004000;
\r
612 // this game actually doesn't have SRAM, but some weird protection
\r
613 if (rom_strcmp(0x120, "PUGGSY") == 0)
\r
615 SRam.start = SRam.end = sram_size = 0;
\r
620 SRam.data = (unsigned char *) calloc(sram_size, 1);
\r
621 if (SRam.data == NULL) return;
\r
625 // set EEPROM defaults, in case it gets detected
\r
626 SRam.eeprom_type = 0; // 7bit (24C01)
\r
627 SRam.eeprom_abits = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in
\r
628 SRam.eeprom_bit_cl = 1;
\r
629 SRam.eeprom_bit_in = 0;
\r
630 SRam.eeprom_bit_out= 0;
\r
632 // some known EEPROM data (thanks to EkeEke)
\r
633 if (name_cmp("COLLEGE SLAM") == 0 ||
\r
634 name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)
\r
636 SRam.eeprom_type = 3;
\r
637 SRam.eeprom_abits = 2;
\r
638 SRam.eeprom_bit_cl = 0;
\r
640 else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||
\r
641 name_cmp("NFL QUARTERBACK CLUB") == 0)
\r
643 SRam.eeprom_type = 2;
\r
644 SRam.eeprom_abits = 2;
\r
645 SRam.eeprom_bit_cl = 0;
\r
647 else if (name_cmp("NBA JAM") == 0)
\r
649 SRam.eeprom_type = 2;
\r
650 SRam.eeprom_bit_out = 1;
\r
651 SRam.eeprom_abits = 0;
\r
653 else if (name_cmp("NHLPA HOCKEY '93") == 0 ||
\r
654 name_cmp("NHLPA Hockey '93") == 0 ||
\r
655 name_cmp("RINGS OF POWER") == 0)
\r
657 SRam.start = SRam.end = 0x200000;
\r
658 Pico.m.sram_reg = 0x14;
\r
659 SRam.eeprom_abits = 0;
\r
660 SRam.eeprom_bit_cl = 6;
\r
661 SRam.eeprom_bit_in = 7;
\r
662 SRam.eeprom_bit_out= 7;
\r
664 else if ( name_cmp("MICRO MACHINES II") == 0 ||
\r
665 (name_cmp(" ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}
\r
666 (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))
\r
668 SRam.start = 0x300000;
\r
669 SRam.end = 0x380001;
\r
670 Pico.m.sram_reg = 0x14;
\r
671 SRam.eeprom_type = 2;
\r
672 SRam.eeprom_abits = 0;
\r
673 SRam.eeprom_bit_cl = 1;
\r
674 SRam.eeprom_bit_in = 0;
\r
675 SRam.eeprom_bit_out= 7;
\r
679 else if (name_cmp("Virtua Racing") == 0 ||
\r
680 name_cmp("VIRTUA RACING") == 0)
\r
686 else if (rom_strcmp(0x100, "SEGA PICO") == 0 ||
\r
687 rom_strcmp(0x100, "IMA IKUNOUJYUKU") == 0) // what is that supposed to mean?
\r
692 // Detect 12-in-1 mapper
\r
693 else if ((name_cmp("ROBOCOP 3") == 0 && Pico.romsize == 0x200000) ||
\r
694 (rom_strcmp(0x160, "FLICKY") == 0 && Pico.romsize >= 0x200000) ||
\r
695 (name_cmp(" SHOVE IT!") == 0 && Pico.romsize >= 0x200000) ||
\r
696 (name_cmp("MS PACMAN") == 0 && Pico.romsize >= 0x200000)) // bad dump?
\r
698 carthw_12in1_startup();
\r
702 else if (Pico.romsize == 512*1024 && (
\r
703 rom_strcmp(0x94, "THE EARTH DEFEND") == 0 ||
\r
704 rom_strcmp(0xfe, "WISEGAME 11-03-1993") == 0 || // Funny World
\r
705 rom_strcmp(0x95, "MALLET LEGEND ") == 0)) // Whac-A-Critter
\r
707 carthw_realtec_startup();
\r
710 // Some games malfunction if SRAM is not filled with 0xff
\r
711 if (name_cmp("DINO DINI'S SOCCER") == 0 ||
\r
712 name_cmp("MICRO MACHINES II") == 0)
\r
714 memset(SRam.data, 0xff, sram_size);
\r
717 // Unusual region 'code'
\r
718 if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
\r
719 *(int *) (Pico.rom+0x1f0) = 0x20204520;
\r