support for zipped ISOs

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@65 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
notaz 2007-03-10 23:50:03 +00:00
parent b67ef287e7
commit 83bd0b76ab
17 changed files with 413 additions and 156 deletions

View file

@ -63,11 +63,13 @@ int PicoAreaPackCpu(unsigned char *cpu, int is_sub)
#endif
#ifdef EMU_M68K
m68ki_cpu_core *context = is_sub ? &PicoS68kCPU : &PicoM68kCPU;
memcpy(cpu,context->dar,0x40);
pc=context->pc;
void *oldcontext = m68ki_cpu_p;
m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
memcpy(cpu,m68ki_cpu_p->dar,0x40);
pc=m68ki_cpu_p->pc;
*(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
*(unsigned int *)(cpu+0x48)=context->sp[0];
*(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[0];
m68k_set_context(oldcontext);
#endif
*(unsigned int *)(cpu+0x40)=pc;
@ -94,11 +96,13 @@ int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
#endif
#ifdef EMU_M68K
m68ki_cpu_core *context = is_sub ? &PicoS68kCPU : &PicoM68kCPU;
memcpy(context->dar,cpu,0x40);
context->pc=*(unsigned int *)(cpu+0x40);
void *oldcontext = m68ki_cpu_p;
m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
memcpy(m68ki_cpu_p->dar,cpu,0x40);
m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
context->sp[0]=*(unsigned int *)(cpu+0x48);
m68ki_cpu_p->sp[0]=*(unsigned int *)(cpu+0x48);
m68k_set_context(oldcontext);
#endif
return 0;
}

View file

@ -8,6 +8,152 @@
#include "PicoInt.h"
#include "../zlib/zlib.h"
#include "../unzip/unzip.h"
#include "../unzip/unzip_stream.h"
static char *rom_exts[] = { "bin", "gen", "smd", "iso" };
pm_file *pm_open(const char *path)
{
pm_file *file = NULL;
const char *ext;
FILE *f;
if (path == NULL) return NULL;
if (strlen(path) < 5) ext = NULL; // no ext
else ext = path + strlen(path) - 3;
if (ext && strcasecmp(ext, "zip") == 0)
{
struct zipent *zipentry;
gzFile gzf = NULL;
ZIP *zipfile;
int i;
zipfile = openzip(path);
if (zipfile != NULL)
{
/* search for suitable file (right extension or large enough file) */
while ((zipentry = readzip(zipfile)) != NULL)
{
if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;
if (strlen(zipentry->name) < 5) continue;
ext = zipentry->name+strlen(zipentry->name)-3;
for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)
if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;
}
/* zipfile given, but nothing found suitable for us inside */
goto zip_failed;
found_rom_zip:
/* try to convert to gzip stream, so we could use standard gzio functions from zlib */
gzf = zip2gz(zipfile, zipentry);
if (gzf == NULL) goto zip_failed;
file = malloc(sizeof(*file));
if (file == NULL) goto zip_failed;
file->file = zipfile;
file->param = gzf;
file->size = zipentry->uncompressed_size;
file->type = PMT_ZIP;
return file;
zip_failed:
if (gzf) {
gzclose(gzf);
zipfile->fp = NULL; // gzclose() closed it
}
closezip(zipfile);
return NULL;
}
}
/* not a zip, treat as uncompressed file */
f = fopen(path, "rb");
if (f == NULL) return NULL;
file = malloc(sizeof(*file));
if (file == NULL) {
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_END);
file->file = f;
file->param = NULL;
file->size = ftell(f);
file->type = PMT_UNCOMPRESSED;
fseek(f, 0, SEEK_SET);
return file;
}
size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
{
int ret;
if (stream->type == PMT_UNCOMPRESSED)
{
ret = fread(ptr, 1, bytes, stream->file);
}
else if (stream->type == PMT_ZIP)
{
gzFile gf = stream->param;
int err;
ret = gzread(gf, ptr, bytes);
err = gzerror2(gf);
if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))
/* we must reset stream pointer or else next seek/read fails */
gzrewind(gf);
}
else
ret = 0;
return ret;
}
int pm_seek(pm_file *stream, long offset, int whence)
{
if (stream->type == PMT_UNCOMPRESSED)
{
return fseek(stream->file, offset, whence);
}
else if (stream->type == PMT_ZIP)
{
return gzseek((gzFile) stream->param, offset, whence);
}
else
return -1;
}
int pm_close(pm_file *fp)
{
int ret = 0;
if (fp == NULL) return EOF;
if (fp->type == PMT_UNCOMPRESSED)
{
fclose(fp->file);
}
else if (fp->type == PMT_ZIP)
{
ZIP *zipfile = fp->file;
gzclose((gzFile) fp->param);
zipfile->fp = NULL; // gzclose() closed it
closezip(zipfile);
}
else
ret = EOF;
free(fp);
return ret;
}
void Byteswap(unsigned char *data,int len)
{
@ -86,12 +232,12 @@ static unsigned char *PicoCartAlloc(int filesize)
return rom;
}
int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
{
unsigned char *rom=NULL; int size;
if (f==NULL) return 1;
fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);
size=f->size;
if (size <= 0) return 1;
size=(size+3)&~3; // Round up to a multiple of 4
@ -99,7 +245,7 @@ int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
rom=PicoCartAlloc(size);
if (rom==NULL) return 1; // { fclose(f); return 1; }
fread(rom,1,size,f); // Load up the rom
pm_read(rom,size,f); // Load up the rom
// maybe we are loading MegaCD BIOS?
if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {
@ -139,69 +285,3 @@ int PicoUnloadCart(unsigned char* romdata)
return 0;
}
#ifdef _UNZIP_SUPPORT
// notaz
#include "../unzip/unzip.h"
// nearly same as PicoCartLoad, but works with zipfiles
int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize)
{
unsigned char *rom=0;
struct zipent* zipentry;
int size;
ZIP *zipfile = openzip(fname);
if(!zipfile) return 1;
// find first bin or smd
while((zipentry = readzip(zipfile)) != 0)
{
char *ext;
if(strlen(zipentry->name) < 5) continue;
ext = zipentry->name+strlen(zipentry->name)-4;
if(!strcasecmp(ext, ".bin") || !strcasecmp(ext, ".smd") || !strcasecmp(ext, ".gen")) break;
}
if(!zipentry) {
closezip(zipfile);
return 4; // no roms
}
size = zipentry->uncompressed_size;
size=(size+3)&~3; // Round up to a multiple of 4
// Allocate space for the rom plus padding
rom=PicoCartAlloc(size);
if (rom==NULL) { closezip(zipfile); return 2; }
if(readuncompresszip(zipfile, zipentry, (char *)rom) != 0) {
free(rom);
rom = 0;
closezip(zipfile);
return 5; // unzip failed
}
closezip(zipfile);
// maybe we are loading MegaCD BIOS?
if (!(PicoMCD&1) && size == 0x20000 &&
(!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {
PicoMCD |= 1;
rom = cd_realloc(rom, size);
}
// Check for SMD:
if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
else Byteswap(rom,size); // Just byteswap
if (prom) *prom=rom;
if (psize) *psize=size;
return 0;
}
#endif

View file

@ -62,10 +62,24 @@ extern areaclose *areaClose;
extern void (*PicoStateProgressCB)(const char *str);
// Cart.c
int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize);
typedef enum
{
PMT_UNCOMPRESSED = 0,
PMT_ZIP
} pm_type;
typedef struct
{
void *file; /* file handle */
void *param; /* additional file related field */
unsigned int size; /* size */
pm_type type;
} pm_file;
pm_file *pm_open(const char *path);
size_t pm_read(void *ptr, size_t bytes, pm_file *stream);
int pm_seek(pm_file *stream, long offset, int whence);
int pm_close(pm_file *fp);
int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize);
int PicoCartInsert(unsigned char *rom,unsigned int romsize);
// notaz
int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize);
void Byteswap(unsigned char *data,int len);
// anotherguest
int PicoUnloadCart(unsigned char* romdata);

View file

@ -257,7 +257,7 @@ void z80_write(unsigned char data, unsigned short a);
void z80_write16(unsigned short data, unsigned short a);
// cd/Memory.c
void PicoMemSetupCD();
void PicoMemSetupCD(void);
unsigned char PicoReadCD8 (unsigned int a);
unsigned short PicoReadCD16(unsigned int a);
unsigned int PicoReadCD32(unsigned int a);

View file

@ -243,7 +243,8 @@ int PicoCdLoadState(void *file)
/* after load events */
if (Pico_mcd->s68k_regs[3]&4) // 1M mode?
wram_2M_to_1M(Pico_mcd->word_ram2M);
mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset);
if (Pico_mcd->m.audio_track > 0 && Pico_mcd->m.audio_track < Pico_mcd->TOC.Last_Track)
mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset);
// restore hint vector
*(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector;

View file

@ -16,11 +16,10 @@ void FILE_End(void)
int Load_ISO(const char *iso_name, int is_bin)
{
struct stat file_stat;
int i, j, num_track, Cur_LBA, index, ret, iso_name_len;
_scd_track *Tracks = Pico_mcd->TOC.Tracks;
FILE *tmp_file;
char tmp_name[1024], tmp_ext[10];
pm_file *pmf;
static char *exts[] = {
"%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
"%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
@ -33,16 +32,7 @@ int Load_ISO(const char *iso_name, int is_bin)
Tracks[0].ftype = is_bin ? TYPE_BIN : TYPE_ISO;
ret = stat(iso_name, &file_stat);
if (ret != 0) return -1;
Tracks[0].Length = file_stat.st_size;
if (Tracks[0].ftype == TYPE_ISO) Tracks[0].Length >>= 11; // size in sectors
else Tracks[0].Length /= 2352; // size in sectors
Tracks[0].F = fopen(iso_name, "rb");
Tracks[0].F = pmf = pm_open(iso_name);
if (Tracks[0].F == NULL)
{
Tracks[0].ftype = 0;
@ -50,11 +40,9 @@ int Load_ISO(const char *iso_name, int is_bin)
return -1;
}
if (Tracks[0].ftype == TYPE_ISO) fseek(Tracks[0].F, 0x100, SEEK_SET);
else fseek(Tracks[0].F, 0x110, SEEK_SET);
// fread(buf, 1, 0x200, Tracks[0].F);
fseek(Tracks[0].F, 0, SEEK_SET);
if (Tracks[0].ftype == TYPE_ISO)
Tracks[0].Length = pmf->size >>= 11; // size in sectors
else Tracks[0].Length = pmf->size /= 2352;
Tracks[0].MSF.M = 0; // minutes
Tracks[0].MSF.S = 2; // seconds
@ -64,7 +52,6 @@ int Load_ISO(const char *iso_name, int is_bin)
Cur_LBA = Tracks[0].Length; // Size in sectors
strcpy(tmp_name, iso_name);
iso_name_len = strlen(iso_name);
for (num_track = 2, i = 0; i < 100; i++)
@ -72,6 +59,7 @@ int Load_ISO(const char *iso_name, int is_bin)
for(j = 0; j < sizeof(exts)/sizeof(char *); j++)
{
int ext_len;
FILE *tmp_file;
sprintf(tmp_ext, exts[j], i);
ext_len = strlen(tmp_ext);
@ -88,8 +76,8 @@ int Load_ISO(const char *iso_name, int is_bin)
if (tmp_file)
{
// float fs;
int fs;
struct stat file_stat;
index = num_track - 1;
ret = stat(tmp_name, &file_stat);
@ -143,13 +131,13 @@ void Unload_ISO(void)
if (Pico_mcd == NULL) return;
for(i = 0; i < 100; i++)
if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F);
for(i = 1; i < 100; i++)
{
if (Pico_mcd->TOC.Tracks[i].F) fclose(Pico_mcd->TOC.Tracks[i].F);
Pico_mcd->TOC.Tracks[i].F = NULL;
Pico_mcd->TOC.Tracks[i].Length = 0;
Pico_mcd->TOC.Tracks[i].ftype = 0;
}
memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
}
@ -208,11 +196,11 @@ int FILE_Read_One_LBA_CDC(void)
Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA
Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF;
memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], &Pico_mcd->cdc.HEAD, 4);
*(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N;
//memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048);
fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
fread(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 1, 2048, Pico_mcd->TOC.Tracks[0].F);
pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET);
pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
#ifdef DEBUG_CD
cdprintf("Read -> WA = %d Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);

View file

@ -3,8 +3,6 @@
#include "cd_file.h"
#include <stdio.h> // FILE
#ifdef __cplusplus
extern "C" {
#endif
@ -37,7 +35,7 @@ typedef struct
_msf MSF;
//
char ftype; // TYPE_ISO, TYPE_BIN, TYPE_MP3
FILE *F;
void *F;
int Length;
short KBtps; // kbytes per sec for mp3s (bitrate / 1000 / 8)
short pad;
@ -94,9 +92,6 @@ int Open_Tray_CDD_cD(void);
int CDD_Def(void);
//void Write_CD_Audio(short *Buf, int rate, int channel, int lenght);
//void Update_CD_Audio(int **Buf, int lenght);
#ifdef __cplusplus
};