1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "PicoInt.h"
\r
12 void Byteswap(unsigned char *data,int len)
\r
16 if (len<2) return; // Too short
\r
20 unsigned short *pd=(unsigned short *)(data+i);
\r
21 int value=*pd; // Get 2 bytes
\r
23 value=(value<<8)|(value>>8); // Byteswap it
\r
24 *pd=(unsigned short)value; // Put 2b ytes
\r
30 // Interleve a 16k block and byteswap
\r
31 static int InterleveBlock(unsigned char *dest,unsigned char *src)
\r
34 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd
\r
35 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even
\r
39 // Decode a SMD file
\r
40 static int DecodeSmd(unsigned char *data,int len)
\r
42 unsigned char *temp=NULL;
\r
45 temp=(unsigned char *)malloc(0x4000);
\r
46 if (temp==NULL) return 1;
\r
47 memset(temp,0,0x4000);
\r
49 // Interleve each 16k block and shift down by 0x200:
\r
50 for (i=0; i+0x4200<=len; i+=0x4000)
\r
52 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer
\r
53 memcpy(data+i,temp,0x4000); // Copy back in
\r
60 static unsigned char *PicoCartAlloc(int filesize)
\r
66 dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));
\r
67 if (filesize > 0x20000) return NULL; // invalid BIOS
\r
68 rom=(unsigned char *)malloc(sizeof(mcd_state));
\r
69 if (rom) memset(rom, 0, sizeof(mcd_state));
\r
73 alloc_size=filesize+0x7ffff;
\r
74 if((filesize&0x3fff)==0x200) alloc_size-=0x200;
\r
75 alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently
\r
76 if((filesize&0x3fff)==0x200) alloc_size+=0x200;
\r
77 else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection
\r
78 //dprintf("alloc_size: %x\n", alloc_size);
\r
80 // Allocate space for the rom plus padding
\r
81 rom=(unsigned char *)malloc(alloc_size);
\r
82 if(rom) memset(rom+alloc_size-0x80000,0,0x80000);
\r
86 int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
\r
88 unsigned char *rom=NULL; int size;
\r
89 if (f==NULL) return 1;
\r
91 fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);
\r
92 if (size <= 0) return 1;
\r
94 if (size > 0x20000) return 1; // invalid BIOS
\r
97 size=(size+3)&~3; // Round up to a multiple of 4
\r
100 // Allocate space for the rom plus padding
\r
101 rom=PicoCartAlloc(size);
\r
102 if (rom==NULL) return 1; // { fclose(f); return 1; }
\r
104 fread(rom,1,size,f); // Load up the rom
\r
105 // fclose(f); // this is confusing. From now on, caller should close it, because it opened this.
\r
108 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
\r
109 else Byteswap(rom,size); // Just byteswap
\r
111 if (prom) *prom=rom;
\r
112 if (psize) *psize=size;
\r
117 // Insert/remove a cartridge:
\r
118 int PicoCartInsert(unsigned char *rom,unsigned int romsize)
\r
120 // notaz: add a 68k "jump one op back" opcode to the end of ROM.
\r
121 // This will hang the emu, but will prevent nasty crashes.
\r
122 // note: 4 bytes are padded to every ROM
\r
124 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped
\r
128 Pico.romsize=romsize;
\r
130 return PicoReset(1);
\r
133 int PicoUnloadCart(unsigned char* romdata)
\r
140 #ifdef _UNZIP_SUPPORT
\r
143 #include "../unzip/unzip.h"
\r
145 // nearly same as PicoCartLoad, but works with zipfiles
\r
146 int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize)
\r
148 unsigned char *rom=0;
\r
149 struct zipent* zipentry;
\r
151 ZIP *zipfile = openzip(fname);
\r
153 if(!zipfile) return 1;
\r
155 // find first bin or smd
\r
156 while((zipentry = readzip(zipfile)) != 0)
\r
159 if(strlen(zipentry->name) < 5) continue;
\r
160 ext = zipentry->name+strlen(zipentry->name)-4;
\r
162 if(!strcasecmp(ext, ".bin") || !strcasecmp(ext, ".smd") || !strcasecmp(ext, ".gen")) break;
\r
167 return 4; // no roms
\r
170 size = zipentry->uncompressed_size;
\r
172 size=(size+3)&~3; // Round up to a multiple of 4
\r
174 // Allocate space for the rom plus padding
\r
175 rom=PicoCartAlloc(size);
\r
176 if (rom==NULL) { closezip(zipfile); return 2; }
\r
178 if(readuncompresszip(zipfile, zipentry, (char *)rom) != 0) {
\r
182 return 5; // unzip failed
\r
188 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD
\r
189 else Byteswap(rom,size); // Just byteswap
\r
191 if (prom) *prom=rom;
\r
192 if (psize) *psize=size;
\r