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
21 pm_file *pm_open(const char *path)
\r
23 pm_file *file = NULL;
\r
27 if (path == NULL) return NULL;
\r
29 if (strlen(path) < 5) ext = NULL; // no ext
\r
30 else ext = path + strlen(path) - 3;
\r
32 if (ext && strcasecmp(ext, "zip") == 0)
\r
34 struct zipent *zipentry;
\r
39 zipfile = openzip(path);
\r
41 if (zipfile != NULL)
\r
43 /* search for suitable file (right extension or large enough file) */
\r
44 while ((zipentry = readzip(zipfile)) != NULL)
\r
46 if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
\r
47 if (strlen(zipentry->name) < 5) continue;
\r
49 ext = zipentry->name+strlen(zipentry->name)-3;
\r
50 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
\r
51 if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
\r
54 /* zipfile given, but nothing found suitable for us inside */
\r
58 /* try to convert to gzip stream, so we could use standard gzio functions from zlib */
\r
59 gzf = zip2gz(zipfile, zipentry);
\r
60 if (gzf == NULL) goto zip_failed;
\r
62 file = malloc(sizeof(*file));
\r
63 if (file == NULL) goto zip_failed;
\r
64 file->file = zipfile;
\r
66 file->size = zipentry->uncompressed_size;
\r
67 file->type = PMT_ZIP;
\r
73 zipfile->fp = NULL; // gzclose() closed it
\r
80 /* not a zip, treat as uncompressed file */
\r
81 f = fopen(path, "rb");
\r
82 if (f == NULL) return NULL;
\r
84 /* we use our own buffering */
\r
85 setvbuf(f, NULL, _IONBF, 0);
\r
87 file = malloc(sizeof(*file));
\r
92 fseek(f, 0, SEEK_END);
\r
95 file->size = ftell(f);
\r
96 file->type = PMT_UNCOMPRESSED;
\r
97 fseek(f, 0, SEEK_SET);
\r
101 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
\r
105 if (stream->type == PMT_UNCOMPRESSED)
\r
107 ret = fread(ptr, 1, bytes, stream->file);
\r
109 else if (stream->type == PMT_ZIP)
\r
111 gzFile gf = stream->param;
\r
113 ret = gzread(gf, ptr, bytes);
\r
114 err = gzerror2(gf);
\r
115 if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
\r
116 /* we must reset stream pointer or else next seek/read fails */
\r
125 int pm_seek(pm_file *stream, long offset, int whence)
\r
127 if (stream->type == PMT_UNCOMPRESSED)
\r
129 fseek(stream->file, offset, whence);
\r
130 return ftell(stream->file);
\r
132 else if (stream->type == PMT_ZIP)
\r
134 if (PicoMessage != NULL && offset > 6*1024*1024) {
\r
135 long pos = gztell((gzFile) stream->param);
\r
136 if (offset < pos || offset - pos > 6*1024*1024)
\r
137 PicoMessage("Decompressing data...");
\r
139 return gzseek((gzFile) stream->param, offset, whence);
\r
145 int pm_close(pm_file *fp)
\r
149 if (fp == NULL) return EOF;
\r
151 if (fp->type == PMT_UNCOMPRESSED)
\r
155 else if (fp->type == PMT_ZIP)
\r
157 ZIP *zipfile = fp->file;
\r
158 gzclose((gzFile) fp->param);
\r
159 zipfile->fp = NULL; // gzclose() closed it
\r
170 void Byteswap(unsigned char *data,int len)
\r
174 if (len<2) return; // Too short
\r
178 unsigned short *pd=(unsigned short *)(data+i);
\r
179 int value=*pd; // Get 2 bytes
\r
181 value=(value<<8)|(value>>8); // Byteswap it
\r
182 *pd=(unsigned short)value; // Put 2b ytes
\r
188 // Interleve a 16k block and byteswap
\r
189 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
192 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
193 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
197 // Decode a SMD file
\r
198 static int DecodeSmd(unsigned char *data,int len)
\r
200 unsigned char *temp=NULL;
\r
203 temp=(unsigned char *)malloc(0x4000);
\r
204 if (temp==NULL) return 1;
\r
205 memset(temp,0,0x4000);
\r
207 // Interleve each 16k block and shift down by 0x200:
\r
208 for (i=0; i+0x4200<=len; i+=0x4000)
\r
210 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
211 memcpy(data+i,temp,0x4000); // Copy back in
\r
218 static unsigned char *cd_realloc(void *old, int filesize)
\r
220 unsigned char *rom;
\r
221 dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));
\r
222 rom=realloc(old, sizeof(mcd_state));
\r
223 if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);
\r
227 static unsigned char *PicoCartAlloc(int filesize)
\r
230 unsigned char *rom;
\r
232 if (PicoMCD & 1) return cd_realloc(NULL, filesize);
\r
234 alloc_size=filesize+0x7ffff;
\r
235 if((filesize&0x3fff)==0x200) alloc_size-=0x200;
\r
236 alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
\r
237 if((filesize&0x3fff)==0x200) alloc_size+=0x200;
\r
238 else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
\r
239 //dprintf("alloc_size: %x\n", alloc_size);
\r
241 // Allocate space for the rom plus padding
\r
242 rom=(unsigned char *)malloc(alloc_size);
\r
243 if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
\r
247 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
\r
249 unsigned char *rom=NULL; int size, bytes_read;
\r
250 if (f==NULL) return 1;
\r
253 if (size <= 0) return 1;
\r
254 size=(size+3)&~3; // Round up to a multiple of 4
\r
256 // Allocate space for the rom plus padding
\r
257 rom=PicoCartAlloc(size);
\r
259 printf("out of memory (wanted %i)\n", size);
\r
263 if (PicoCartLoadProgressCB != NULL)
\r
265 // read ROM in blocks, just for fun
\r
267 unsigned char *p = rom;
\r
271 int todo = size - bytes_read;
\r
272 if (todo > 256*1024) todo = 256*1024;
\r
273 ret = pm_read(p,todo,f);
\r
276 PicoCartLoadProgressCB(bytes_read * 100 / size);
\r
281 bytes_read = pm_read(rom,size,f); // Load up the rom
\r
282 if (bytes_read <= 0) {
\r
283 printf("read failed\n");
\r
288 // maybe we are loading MegaCD BIOS?
\r
289 if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {
\r
291 rom = cd_realloc(rom, size);
\r
295 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
\r
296 else Byteswap(rom,size); // Just byteswap
\r
298 if (prom) *prom=rom;
\r
299 if (psize) *psize=size;
\r
304 // Insert/remove a cartridge:
\r
305 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
307 // notaz: add a 68k "jump one op back" opcode to the end of ROM.
\r
308 // This will hang the emu, but will prevent nasty crashes.
\r
309 // note: 4 bytes are padded to every ROM
\r
311 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
\r
315 Pico.romsize=romsize;
\r
317 return PicoReset(1);
\r
320 int PicoUnloadCart(unsigned char* romdata)
\r