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 (*PicoCartLoadProgressCB)(int percent) = NULL;
\r
19 void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c
\r
22 pm_file *pm_open(const char *path)
\r
24 pm_file *file = NULL;
\r
28 if (path == NULL) return NULL;
\r
30 if (strlen(path) < 5) ext = NULL; // no ext
\r
31 else ext = path + strlen(path) - 3;
\r
33 if (ext && strcasecmp(ext, "zip") == 0)
\r
35 struct zipent *zipentry;
\r
40 zipfile = openzip(path);
\r
42 if (zipfile != NULL)
\r
44 /* search for suitable file (right extension or large enough file) */
\r
45 while ((zipentry = readzip(zipfile)) != NULL)
\r
47 if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
\r
48 if (strlen(zipentry->name) < 5) continue;
\r
50 ext = zipentry->name+strlen(zipentry->name)-3;
\r
51 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
\r
52 if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
\r
55 /* zipfile given, but nothing found suitable for us inside */
\r
59 /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
\r
60 gzf = zip2gz(zipfile, zipentry);
\r
61 if (gzf == NULL) goto zip_failed;
\r
63 file = malloc(sizeof(*file));
\r
64 if (file == NULL) goto zip_failed;
\r
65 file->file = zipfile;
\r
67 file->size = zipentry->uncompressed_size;
\r
68 file->type = PMT_ZIP;
\r
74 zipfile->fp = NULL; // gzclose() closed it
\r
81 /* not a zip, treat as uncompressed file */
\r
82 f = fopen(path, "rb");
\r
83 if (f == NULL) return NULL;
\r
85 /* we use our own buffering */
\r
86 setvbuf(f, NULL, _IONBF, 0);
\r
88 file = malloc(sizeof(*file));
\r
93 fseek(f, 0, SEEK_END);
\r
96 file->size = ftell(f);
\r
97 file->type = PMT_UNCOMPRESSED;
\r
98 fseek(f, 0, SEEK_SET);
\r
102 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
\r
106 if (stream->type == PMT_UNCOMPRESSED)
\r
108 ret = fread(ptr, 1, bytes, stream->file);
\r
110 else if (stream->type == PMT_ZIP)
\r
112 gzFile gf = stream->param;
\r
114 ret = gzread(gf, ptr, bytes);
\r
115 err = gzerror2(gf);
\r
116 if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
\r
117 /* we must reset stream pointer or else next seek/read fails */
\r
126 int pm_seek(pm_file *stream, long offset, int whence)
\r
128 if (stream->type == PMT_UNCOMPRESSED)
\r
130 fseek(stream->file, offset, whence);
\r
131 return ftell(stream->file);
\r
133 else if (stream->type == PMT_ZIP)
\r
135 if (PicoMessage != NULL && offset > 6*1024*1024) {
\r
136 long pos = gztell((gzFile) stream->param);
\r
137 if (offset < pos || offset - pos > 6*1024*1024)
\r
138 PicoMessage("Decompressing data...");
\r
140 return gzseek((gzFile) stream->param, offset, whence);
\r
146 int pm_close(pm_file *fp)
\r
150 if (fp == NULL) return EOF;
\r
152 if (fp->type == PMT_UNCOMPRESSED)
\r
156 else if (fp->type == PMT_ZIP)
\r
158 ZIP *zipfile = fp->file;
\r
159 gzclose((gzFile) fp->param);
\r
160 zipfile->fp = NULL; // gzclose() closed it
\r
171 void Byteswap(unsigned char *data,int len)
\r
175 if (len<2) return; // Too short
\r
179 unsigned short *pd=(unsigned short *)(data+i);
\r
180 int value=*pd; // Get 2 bytes
\r
182 value=(value<<8)|(value>>8); // Byteswap it
\r
183 *pd=(unsigned short)value; // Put 2b ytes
\r
189 // Interleve a 16k block and byteswap
\r
190 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
193 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
194 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
198 // Decode a SMD file
\r
199 static int DecodeSmd(unsigned char *data,int len)
\r
201 unsigned char *temp=NULL;
\r
204 temp=(unsigned char *)malloc(0x4000);
\r
205 if (temp==NULL) return 1;
\r
206 memset(temp,0,0x4000);
\r
208 // Interleve each 16k block and shift down by 0x200:
\r
209 for (i=0; i+0x4200<=len; i+=0x4000)
\r
211 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
212 memcpy(data+i,temp,0x4000); // Copy back in
\r
219 static unsigned char *cd_realloc(void *old, int filesize)
\r
221 unsigned char *rom;
\r
222 dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));
\r
223 rom=realloc(old, sizeof(mcd_state));
\r
224 if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);
\r
228 static unsigned char *PicoCartAlloc(int filesize)
\r
231 unsigned char *rom;
\r
233 if (PicoMCD & 1) return cd_realloc(NULL, filesize);
\r
235 alloc_size=filesize+0x7ffff;
\r
236 if((filesize&0x3fff)==0x200) alloc_size-=0x200;
\r
237 alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
\r
238 if((filesize&0x3fff)==0x200) alloc_size+=0x200;
\r
239 else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
\r
240 //dprintf("alloc_size: %x\n", alloc_size);
\r
242 // Allocate space for the rom plus padding
\r
243 rom=(unsigned char *)malloc(alloc_size);
\r
244 if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
\r
248 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
\r
250 unsigned char *rom=NULL; int size, bytes_read;
\r
251 if (f==NULL) return 1;
\r
254 if (size <= 0) return 1;
\r
255 size=(size+3)&~3; // Round up to a multiple of 4
\r
257 // Allocate space for the rom plus padding
\r
258 rom=PicoCartAlloc(size);
\r
260 printf("out of memory (wanted %i)\n", size);
\r
264 if (PicoCartLoadProgressCB != NULL)
\r
266 // read ROM in blocks, just for fun
\r
268 unsigned char *p = rom;
\r
272 int todo = size - bytes_read;
\r
273 if (todo > 256*1024) todo = 256*1024;
\r
274 ret = pm_read(p,todo,f);
\r
277 PicoCartLoadProgressCB(bytes_read * 100 / size);
\r
282 bytes_read = pm_read(rom,size,f); // Load up the rom
\r
283 if (bytes_read <= 0) {
\r
284 printf("read failed\n");
\r
289 // maybe we are loading MegaCD BIOS?
\r
290 if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {
\r
292 rom = cd_realloc(rom, size);
\r
296 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
\r
297 else Byteswap(rom,size); // Just byteswap
\r
299 if (prom) *prom=rom;
\r
300 if (psize) *psize=size;
\r
305 // Insert/remove a cartridge:
\r
306 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
308 // notaz: add a 68k "jump one op back" opcode to the end of ROM.
\r
309 // This will hang the emu, but will prevent nasty crashes.
\r
310 // note: 4 bytes are padded to every ROM
\r
312 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
\r
315 Pico.romsize=romsize;
\r
317 // setup correct memory map for loaded ROM
\r
320 else PicoMemSetup();
\r
323 if (!(PicoMCD & 1))
\r
326 return PicoReset(1);
\r
329 int PicoUnloadCart(unsigned char* romdata)
\r
335 static int rom_strcmp(int rom_offset, const char *s1)
\r
337 int i, len = strlen(s1);
\r
338 const char *s_rom = (const char *)Pico.rom + rom_offset;
\r
339 for (i = 0; i < len; i++)
\r
340 if (s1[i] != s_rom[i^1])
\r
345 static int name_cmp(const char *name)
\r
347 return rom_strcmp(0x150, name);
\r
350 /* various cart-specific things, which can't be handled by generic code */
\r
351 void PicoCartDetect(void)
\r
353 int sram_size = 0, csum;
\r
354 if(SRam.data) free(SRam.data); SRam.data=0;
\r
355 Pico.m.sram_reg = 0;
\r
357 csum = PicoRead32(0x18c) & 0xffff;
\r
359 if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')
\r
361 if (Pico.rom[0x1B2] & 0x40)
\r
364 SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games
\r
365 SRam.end = PicoRead32(0x1B8);
\r
366 sram_size = 0x2000;
\r
367 Pico.m.sram_reg |= 4;
\r
370 SRam.start = PicoRead32(0x1B4) & 0xFFFF00;
\r
371 SRam.end = PicoRead32(0x1B8) | 1;
\r
372 sram_size = SRam.end - SRam.start + 1;
\r
374 Pico.m.sram_reg |= 0x10; // SRAM was detected
\r
376 if (sram_size <= 0)
\r
378 // some games may have bad headers, like S&K and Sonic3
\r
379 // note: majority games use 0x200000 as starting address, but there are some which
\r
380 // use something else (0x300000 by HardBall '95). Luckily they have good headers.
\r
381 SRam.start = 0x200000;
\r
382 SRam.end = 0x203FFF;
\r
383 sram_size = 0x004000;
\r
388 SRam.data = (unsigned char *) calloc(sram_size, 1);
\r
389 if(!SRam.data) return;
\r
393 // set EEPROM defaults, in case it gets detected
\r
394 SRam.eeprom_type = 0; // 7bit (24C01)
\r
395 SRam.eeprom_abits = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in
\r
396 SRam.eeprom_bit_cl = 1;
\r
397 SRam.eeprom_bit_in = 0;
\r
398 SRam.eeprom_bit_out= 0;
\r
400 // some known EEPROM data (thanks to EkeEke)
\r
401 if (name_cmp("COLLEGE SLAM") == 0 ||
\r
402 name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)
\r
404 SRam.eeprom_type = 3;
\r
405 SRam.eeprom_abits = 2;
\r
406 SRam.eeprom_bit_cl = 0;
\r
408 else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||
\r
409 name_cmp("NFL QUARTERBACK CLUB") == 0)
\r
411 SRam.eeprom_type = 2;
\r
412 SRam.eeprom_abits = 2;
\r
413 SRam.eeprom_bit_cl = 0;
\r
415 else if (name_cmp("NBA JAM") == 0)
\r
417 SRam.eeprom_type = 2;
\r
418 SRam.eeprom_bit_out = 1;
\r
419 SRam.eeprom_abits = 0;
\r
421 else if (name_cmp("NHLPA HOCKEY '93") == 0 ||
\r
422 name_cmp("NHLPA Hockey '93") == 0 ||
\r
423 name_cmp("RINGS OF POWER") == 0)
\r
425 SRam.start = SRam.end = 0x200000;
\r
426 Pico.m.sram_reg = 0x14;
\r
427 SRam.eeprom_abits = 0;
\r
428 SRam.eeprom_bit_cl = 6;
\r
429 SRam.eeprom_bit_in = 7;
\r
430 SRam.eeprom_bit_out= 7;
\r
432 else if ( name_cmp("MICRO MACHINES II") == 0 ||
\r
433 (name_cmp(" ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}
\r
434 (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))
\r
436 SRam.start = 0x300000;
\r
437 SRam.end = 0x380001;
\r
438 Pico.m.sram_reg = 0x14;
\r
439 SRam.eeprom_type = 2;
\r
440 SRam.eeprom_abits = 0;
\r
441 SRam.eeprom_bit_cl = 1;
\r
442 SRam.eeprom_bit_in = 0;
\r
443 SRam.eeprom_bit_out= 7;
\r
446 // Some games malfunction if SRAM is not filled with 0xff
\r
447 if (name_cmp("DINO DINI'S SOCCER") == 0 ||
\r
448 name_cmp("MICRO MACHINES II") == 0)
\r
449 memset(SRam.data, 0xff, sram_size);
\r
451 // Unusual region 'code'
\r
452 if (rom_strcmp(0x1f0, "EUROPE") == 0)
\r
453 *(int *) (Pico.rom+0x1f0) = 0x20204520;
\r