X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2FCart.c;h=1cf8dec79f45963c0fb0d20ef258d2fffe980315;hb=32826a1a22fd3e6203310bba855d8c2b6f3c403a;hp=076247385520d55897ef525d1efdcc32f6767fec;hpb=83bd0b76aba19ff62368cfee76089e15579e3b7c;p=picodrive.git diff --git a/Pico/Cart.c b/Pico/Cart.c index 0762473..1cf8dec 100644 --- a/Pico/Cart.c +++ b/Pico/Cart.c @@ -1,7 +1,7 @@ // This is part of Pico Library // (c) Copyright 2004 Dave, All rights reserved. -// (c) Copyright 2006 notaz, All rights reserved. +// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas // Free for non-commercial use. // For commercial use, separate licencing terms must be obtained. @@ -12,8 +12,11 @@ #include "../unzip/unzip.h" #include "../unzip/unzip_stream.h" + static char *rom_exts[] = { "bin", "gen", "smd", "iso" }; +void (*PicoCartLoadProgressCB)(int percent) = NULL; + pm_file *pm_open(const char *path) { @@ -78,6 +81,9 @@ zip_failed: f = fopen(path, "rb"); if (f == NULL) return NULL; + /* we use our own buffering */ + setvbuf(f, NULL, _IONBF, 0); + file = malloc(sizeof(*file)); if (file == NULL) { fclose(f); @@ -120,10 +126,16 @@ int pm_seek(pm_file *stream, long offset, int whence) { if (stream->type == PMT_UNCOMPRESSED) { - return fseek(stream->file, offset, whence); + fseek(stream->file, offset, whence); + return ftell(stream->file); } else if (stream->type == PMT_ZIP) { + if (PicoMessage != NULL && offset > 6*1024*1024) { + long pos = gztell((gzFile) stream->param); + if (offset < pos || offset - pos > 6*1024*1024) + PicoMessage("Decompressing data..."); + } return gzseek((gzFile) stream->param, offset, whence); } else @@ -234,7 +246,7 @@ static unsigned char *PicoCartAlloc(int filesize) int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize) { - unsigned char *rom=NULL; int size; + unsigned char *rom=NULL; int size, bytes_read; if (f==NULL) return 1; size=f->size; @@ -243,9 +255,35 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize) // Allocate space for the rom plus padding rom=PicoCartAlloc(size); - if (rom==NULL) return 1; // { fclose(f); return 1; } + if (rom==NULL) { + printf("out of memory (wanted %i)\n", size); + return 1; + } - pm_read(rom,size,f); // Load up the rom + if (PicoCartLoadProgressCB != NULL) + { + // read ROM in blocks, just for fun + int ret; + unsigned char *p = rom; + bytes_read=0; + do + { + int todo = size - bytes_read; + if (todo > 256*1024) todo = 256*1024; + ret = pm_read(p,todo,f); + bytes_read += ret; + p += ret; + PicoCartLoadProgressCB(bytes_read * 100 / size); + } + while (ret > 0); + } + else + bytes_read = pm_read(rom,size,f); // Load up the rom + if (bytes_read <= 0) { + printf("read failed\n"); + free(rom); + return 1; + } // maybe we are loading MegaCD BIOS? if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {