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 "pico_int.h"
\r
11 #include "../zlib/zlib.h"
\r
12 #include "../unzip/unzip.h"
\r
13 #include "../unzip/unzip_stream.h"
\r
16 static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };
\r
18 void (*PicoCartUnloadHook)(void) = NULL;
\r
20 void (*PicoCartLoadProgressCB)(int percent) = NULL;
\r
21 void (*PicoCDLoadProgressCB)(const char *fname, 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) - 2;
\r
103 if (ext[-1] != '.') ext--;
\r
104 if (ext[-1] != '.') ext--;
\r
106 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
\r
107 if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
\r
110 /* zipfile given, but nothing found suitable for us inside */
\r
114 /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
\r
115 gzf = zip2gz(zipfile, zipentry);
\r
116 if (gzf == NULL) goto zip_failed;
\r
118 file = malloc(sizeof(*file));
\r
119 if (file == NULL) goto zip_failed;
\r
120 file->file = zipfile;
\r
122 file->size = zipentry->uncompressed_size;
\r
123 file->type = PMT_ZIP;
\r
129 zipfile->fp = NULL; // gzclose() closed it
\r
135 else if (ext && strcasecmp(ext, "cso") == 0)
\r
137 cso_struct *cso = NULL, *tmp = NULL;
\r
139 f = fopen(path, "rb");
\r
144 /* we use our own buffering */
\r
145 setvbuf(f, NULL, _IONBF, 0);
\r
148 cso = malloc(sizeof(*cso));
\r
152 if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))
\r
155 if (strncmp(cso->header.magic, "CISO", 4) != 0) {
\r
156 elprintf(EL_STATUS, "cso: bad header");
\r
160 if (cso->header.block_size != 2048) {
\r
161 elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);
\r
165 size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);
\r
166 tmp = realloc(cso, size);
\r
170 elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);
\r
172 size -= sizeof(*cso); // index size
\r
173 if (fread(cso->index, 1, size, f) != size) {
\r
174 elprintf(EL_STATUS, "cso: premature EOF");
\r
179 cso->fpos_in = ftell(f);
\r
181 cso->block_in_buff = -1;
\r
182 file = malloc(sizeof(*file));
\r
183 if (file == NULL) goto cso_failed;
\r
186 file->size = cso->header.total_bytes;
\r
187 file->type = PMT_CSO;
\r
191 if (cso != NULL) free(cso);
\r
192 if (f != NULL) fclose(f);
\r
196 /* not a zip, treat as uncompressed file */
\r
197 f = fopen(path, "rb");
\r
198 if (f == NULL) return NULL;
\r
200 file = malloc(sizeof(*file));
\r
201 if (file == NULL) {
\r
205 fseek(f, 0, SEEK_END);
\r
207 file->param = NULL;
\r
208 file->size = ftell(f);
\r
209 file->type = PMT_UNCOMPRESSED;
\r
210 fseek(f, 0, SEEK_SET);
\r
212 #ifndef __EPOC32__ // makes things worse on Symbian
\r
213 if (file->size > 0x400000)
\r
214 /* we use our own buffering */
\r
215 setvbuf(f, NULL, _IONBF, 0);
\r
221 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
\r
225 if (stream->type == PMT_UNCOMPRESSED)
\r
227 ret = fread(ptr, 1, bytes, stream->file);
\r
229 else if (stream->type == PMT_ZIP)
\r
231 gzFile gf = stream->param;
\r
233 ret = gzread(gf, ptr, bytes);
\r
234 err = gzerror2(gf);
\r
235 if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
\r
236 /* we must reset stream pointer or else next seek/read fails */
\r
239 else if (stream->type == PMT_CSO)
\r
241 cso_struct *cso = stream->param;
\r
242 int read_pos, read_len, out_offs, rret;
\r
243 int block = cso->fpos_out >> 11;
\r
244 int index = cso->index[block];
\r
245 int index_end = cso->index[block+1];
\r
246 unsigned char *out = ptr, *tmp_dst;
\r
251 out_offs = cso->fpos_out&0x7ff;
\r
252 if (out_offs == 0 && bytes >= 2048)
\r
254 else tmp_dst = cso->out_buff;
\r
256 read_pos = (index&0x7fffffff) << cso->header.align;
\r
259 if (read_pos != cso->fpos_in)
\r
260 fseek(stream->file, read_pos, SEEK_SET);
\r
261 rret = fread(tmp_dst, 1, 2048, stream->file);
\r
262 cso->fpos_in = read_pos + rret;
\r
263 if (rret != 2048) break;
\r
265 read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;
\r
266 if (block != cso->block_in_buff)
\r
268 if (read_pos != cso->fpos_in)
\r
269 fseek(stream->file, read_pos, SEEK_SET);
\r
270 rret = fread(cso->in_buff, 1, read_len, stream->file);
\r
271 cso->fpos_in = read_pos + rret;
\r
272 if (rret != read_len) {
\r
273 elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);
\r
276 cso->block_in_buff = block;
\r
278 rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);
\r
280 elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);
\r
286 if (out_offs != 0 || bytes < 2048) {
\r
287 //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);
\r
288 if (bytes < rret) rret = bytes;
\r
289 if (2048 - out_offs < rret) rret = 2048 - out_offs;
\r
290 memcpy(out, tmp_dst + out_offs, rret);
\r
294 cso->fpos_out += rret;
\r
298 index_end = cso->index[block+1];
\r
307 int pm_seek(pm_file *stream, long offset, int whence)
\r
309 if (stream->type == PMT_UNCOMPRESSED)
\r
311 fseek(stream->file, offset, whence);
\r
312 return ftell(stream->file);
\r
314 else if (stream->type == PMT_ZIP)
\r
316 if (PicoMessage != NULL && offset > 6*1024*1024) {
\r
317 long pos = gztell((gzFile) stream->param);
\r
318 if (offset < pos || offset - pos > 6*1024*1024)
\r
319 PicoMessage("Decompressing data...");
\r
321 return gzseek((gzFile) stream->param, offset, whence);
\r
323 else if (stream->type == PMT_CSO)
\r
325 cso_struct *cso = stream->param;
\r
328 case SEEK_CUR: cso->fpos_out += offset; break;
\r
329 case SEEK_SET: cso->fpos_out = offset; break;
\r
330 case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;
\r
332 return cso->fpos_out;
\r
338 int pm_close(pm_file *fp)
\r
342 if (fp == NULL) return EOF;
\r
344 if (fp->type == PMT_UNCOMPRESSED)
\r
348 else if (fp->type == PMT_ZIP)
\r
350 ZIP *zipfile = fp->file;
\r
351 gzclose((gzFile) fp->param);
\r
352 zipfile->fp = NULL; // gzclose() closed it
\r
355 else if (fp->type == PMT_CSO)
\r
368 void Byteswap(unsigned char *data,int len)
\r
372 if (len<2) return; // Too short
\r
376 unsigned short *pd=(unsigned short *)(data+i);
\r
377 int value=*pd; // Get 2 bytes
\r
379 value=(value<<8)|(value>>8); // Byteswap it
\r
380 *pd=(unsigned short)value; // Put 2b ytes
\r
386 // Interleve a 16k block and byteswap
\r
387 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
390 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
391 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
395 // Decode a SMD file
\r
396 static int DecodeSmd(unsigned char *data,int len)
\r
398 unsigned char *temp=NULL;
\r
401 temp=(unsigned char *)malloc(0x4000);
\r
402 if (temp==NULL) return 1;
\r
403 memset(temp,0,0x4000);
\r
405 // Interleve each 16k block and shift down by 0x200:
\r
406 for (i=0; i+0x4200<=len; i+=0x4000)
\r
408 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
409 memcpy(data+i,temp,0x4000); // Copy back in
\r
416 static unsigned char *cd_realloc(void *old, int filesize)
\r
418 unsigned char *rom;
\r
419 rom=realloc(old, sizeof(mcd_state));
\r
420 if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);
\r
424 static unsigned char *PicoCartAlloc(int filesize)
\r
427 unsigned char *rom;
\r
429 if (PicoAHW & PAHW_MCD) return cd_realloc(NULL, filesize);
\r
431 alloc_size=filesize+0x7ffff;
\r
432 if((filesize&0x3fff)==0x200) alloc_size-=0x200;
\r
433 alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
\r
434 if((filesize&0x3fff)==0x200) alloc_size+=0x200;
\r
435 else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
\r
437 // Allocate space for the rom plus padding
\r
438 rom=(unsigned char *)malloc(alloc_size);
\r
439 if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
\r
443 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
\r
445 unsigned char *rom=NULL; int size, bytes_read;
\r
446 if (f==NULL) return 1;
\r
449 if (size <= 0) return 1;
\r
450 size=(size+3)&~3; // Round up to a multiple of 4
\r
452 // Allocate space for the rom plus padding
\r
453 rom=PicoCartAlloc(size);
\r
455 elprintf(EL_STATUS, "out of memory (wanted %i)", size);
\r
459 if (PicoCartLoadProgressCB != NULL)
\r
461 // read ROM in blocks, just for fun
\r
463 unsigned char *p = rom;
\r
467 int todo = size - bytes_read;
\r
468 if (todo > 256*1024) todo = 256*1024;
\r
469 ret = pm_read(p,todo,f);
\r
472 PicoCartLoadProgressCB(bytes_read * 100 / size);
\r
477 bytes_read = pm_read(rom,size,f); // Load up the rom
\r
478 if (bytes_read <= 0) {
\r
479 elprintf(EL_STATUS, "read failed");
\r
486 // maybe we are loading MegaCD BIOS?
\r
487 if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||
\r
488 !strncmp((char *)rom+0x128, "BOOT", 4))) {
\r
489 PicoAHW |= PAHW_MCD;
\r
490 rom = cd_realloc(rom, size);
\r
494 if (size >= 0x4200 && (size&0x3fff)==0x200 &&
\r
495 ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {
\r
496 DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD
\r
498 else Byteswap(rom,size); // Just byteswap
\r
501 if (prom) *prom=rom;
\r
502 if (psize) *psize=size;
\r
507 // Insert a cartridge:
\r
508 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
510 // notaz: add a 68k "jump one op back" opcode to the end of ROM.
\r
511 // This will hang the emu, but will prevent nasty crashes.
\r
512 // note: 4 bytes are padded to every ROM
\r
514 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
\r
517 Pico.romsize=romsize;
\r
524 if (PicoCartUnloadHook != NULL) {
\r
525 PicoCartUnloadHook();
\r
526 PicoCartUnloadHook = NULL;
\r
529 PicoAHW &= PAHW_MCD|PAHW_SMS;
\r
531 PicoMemResetHooks();
\r
532 PicoDmaHook = NULL;
\r
533 PicoResetHook = NULL;
\r
534 PicoLineHook = NULL;
\r
535 PicoLoadStateHook = NULL;
\r
536 carthw_chunks = NULL;
\r
540 if (!(PicoAHW & (PAHW_MCD|PAHW_SMS)))
\r
543 // setup correct memory map for loaded ROM
\r
544 // call PicoMemReset again due to possible memmap change
\r
547 elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);
\r
549 case PAHW_SVP: PicoMemSetup(); break;
\r
550 case PAHW_MCD: PicoMemSetupCD(); break;
\r
551 case PAHW_PICO: PicoMemSetupPico(); break;
\r
552 case PAHW_SMS: PicoMemSetupMS(); break;
\r
556 if (PicoAHW & PAHW_SMS)
\r
564 void PicoCartUnload(void)
\r
566 if (PicoCartUnloadHook != NULL) {
\r
567 PicoCartUnloadHook();
\r
568 PicoCartUnloadHook = NULL;
\r
571 if (Pico.rom != NULL) {
\r
572 SekFinishIdleDet();
\r
578 static int rom_strcmp(int rom_offset, const char *s1)
\r
580 int i, len = strlen(s1);
\r
581 const char *s_rom = (const char *)Pico.rom + rom_offset;
\r
582 for (i = 0; i < len; i++)
\r
583 if (s1[i] != s_rom[i^1])
\r
588 static int name_cmp(const char *name)
\r
590 return rom_strcmp(0x150, name);
\r
594 * various cart-specific things, which can't be handled by generic code
\r
595 * (maybe I should start using CRC for this stuff?)
\r
597 static void PicoCartDetect(void)
\r
599 int sram_size = 0, csum;
\r
600 Pico.m.sram_reg = 0;
\r
602 csum = PicoRead32(0x18c) & 0xffff;
\r
604 if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
\r
606 if (Pico.rom[0x1B2] & 0x40)
\r
609 SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games
\r
610 SRam.end = PicoRead32(0x1B8);
\r
611 sram_size = 0x2000;
\r
612 Pico.m.sram_reg |= 4;
\r
615 SRam.start = PicoRead32(0x1B4) & ~0xff;
\r
616 SRam.end = PicoRead32(0x1B8) | 1;
\r
617 sram_size = SRam.end - SRam.start + 1;
\r
619 SRam.start &= ~0xff000000;
\r
620 SRam.end &= ~0xff000000;
\r
621 Pico.m.sram_reg |= 0x10; // SRAM was detected
\r
623 if (sram_size <= 0)
\r
625 // some games may have bad headers, like S&K and Sonic3
\r
626 // note: majority games use 0x200000 as starting address, but there are some which
\r
627 // use something else (0x300000 by HardBall '95). Luckily they have good headers.
\r
628 SRam.start = 0x200000;
\r
629 SRam.end = 0x203FFF;
\r
630 sram_size = 0x004000;
\r
633 // this game actually doesn't have SRAM, but some weird protection
\r
634 if (rom_strcmp(0x120, "PUGGSY") == 0)
\r
636 SRam.start = SRam.end = sram_size = 0;
\r
641 SRam.data = (unsigned char *) calloc(sram_size, 1);
\r
642 if (SRam.data == NULL) return;
\r
646 // set EEPROM defaults, in case it gets detected
\r
647 SRam.eeprom_type = 0; // 7bit (24C01)
\r
648 SRam.eeprom_abits = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in
\r
649 SRam.eeprom_bit_cl = 1;
\r
650 SRam.eeprom_bit_in = 0;
\r
651 SRam.eeprom_bit_out= 0;
\r
653 // some known EEPROM data (thanks to EkeEke)
\r
654 if (name_cmp("COLLEGE SLAM") == 0 ||
\r
655 name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)
\r
657 SRam.eeprom_type = 3;
\r
658 SRam.eeprom_abits = 2;
\r
659 SRam.eeprom_bit_cl = 0;
\r
661 else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||
\r
662 name_cmp("NFL QUARTERBACK CLUB") == 0)
\r
664 SRam.eeprom_type = 2;
\r
665 SRam.eeprom_abits = 2;
\r
666 SRam.eeprom_bit_cl = 0;
\r
668 else if (name_cmp("NBA JAM") == 0)
\r
670 SRam.eeprom_type = 2;
\r
671 SRam.eeprom_bit_out = 1;
\r
672 SRam.eeprom_abits = 0;
\r
674 else if (name_cmp("NHLPA HOCKEY '93") == 0 ||
\r
675 name_cmp("NHLPA Hockey '93") == 0 ||
\r
676 name_cmp("RINGS OF POWER") == 0)
\r
678 SRam.start = SRam.end = 0x200000;
\r
679 Pico.m.sram_reg = 0x14;
\r
680 SRam.eeprom_abits = 0;
\r
681 SRam.eeprom_bit_cl = 6;
\r
682 SRam.eeprom_bit_in = 7;
\r
683 SRam.eeprom_bit_out= 7;
\r
685 else if ( name_cmp("MICRO MACHINES II") == 0 ||
\r
686 (name_cmp(" ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}
\r
687 (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))
\r
689 SRam.start = 0x300000;
\r
690 SRam.end = 0x380001;
\r
691 Pico.m.sram_reg = 0x14;
\r
692 SRam.eeprom_type = 2;
\r
693 SRam.eeprom_abits = 0;
\r
694 SRam.eeprom_bit_cl = 1;
\r
695 SRam.eeprom_bit_in = 0;
\r
696 SRam.eeprom_bit_out= 7;
\r
700 else if (name_cmp("Virtua Racing") == 0 ||
\r
701 name_cmp("VIRTUA RACING") == 0)
\r
707 else if (rom_strcmp(0x100, "SEGA PICO") == 0 ||
\r
708 rom_strcmp(0x100, "IMA IKUNOUJYUKU") == 0) // what is that supposed to mean?
\r
713 // Detect 12-in-1 mapper
\r
714 else if ((name_cmp("ROBOCOP 3") == 0 && Pico.romsize == 0x200000) ||
\r
715 (rom_strcmp(0x160, "FLICKY") == 0 && Pico.romsize >= 0x200000) ||
\r
716 (name_cmp(" SHOVE IT!") == 0 && Pico.romsize >= 0x200000) ||
\r
717 (name_cmp("MS PACMAN") == 0 && Pico.romsize >= 0x200000) || // bad dump?
\r
718 (name_cmp("ALIEN 3") == 0 && Pico.romsize == 0x200000))
\r
720 carthw_12in1_startup();
\r
724 else if (Pico.romsize == 512*1024 && (
\r
725 rom_strcmp(0x94, "THE EARTH DEFEND") == 0 ||
\r
726 rom_strcmp(0xfe, "WISEGAME 11-03-1993") == 0 || // Funny World
\r
727 rom_strcmp(0x95, "MALLET LEGEND ") == 0)) // Whac-A-Critter
\r
729 carthw_realtec_startup();
\r
733 else if (name_cmp("KID CHAMELEON") == 0 && Pico.romsize > 0x100000)
\r
735 carthw_radica_startup();
\r
738 // Some games malfunction if SRAM is not filled with 0xff
\r
739 if (name_cmp("DINO DINI'S SOCCER") == 0 ||
\r
740 name_cmp("MICRO MACHINES II") == 0)
\r
742 memset(SRam.data, 0xff, sram_size);
\r
745 // Unusual region 'code'
\r
746 if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)
\r
747 *(int *) (Pico.rom+0x1f0) = 0x20204520;
\r