bugfixes, new scaling, double ym upd at 940
[picodrive.git] / Pico / Cart.c
1 // This is part of Pico Library\r
2 \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
6 \r
7 // For commercial use, separate licencing terms must be obtained.\r
8 \r
9 \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
14 \r
15 \r
16 static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
17 \r
18 \r
19 pm_file *pm_open(const char *path)\r
20 {\r
21   pm_file *file = NULL;\r
22   const char *ext;\r
23   FILE *f;\r
24 \r
25   if (path == NULL) return NULL;\r
26 \r
27   if (strlen(path) < 5) ext = NULL; // no ext\r
28   else ext = path + strlen(path) - 3;\r
29 \r
30   if (ext && strcasecmp(ext, "zip") == 0)\r
31   {\r
32     struct zipent *zipentry;\r
33     gzFile gzf = NULL;\r
34     ZIP *zipfile;\r
35     int i;\r
36 \r
37     zipfile = openzip(path);\r
38 \r
39     if (zipfile != NULL)\r
40     {\r
41       /* search for suitable file (right extension or large enough file) */\r
42       while ((zipentry = readzip(zipfile)) != NULL)\r
43       {\r
44         if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;\r
45         if (strlen(zipentry->name) < 5) continue;\r
46 \r
47         ext = zipentry->name+strlen(zipentry->name)-3;\r
48         for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
49           if (!strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
50       }\r
51 \r
52       /* zipfile given, but nothing found suitable for us inside */\r
53       goto zip_failed;\r
54 \r
55 found_rom_zip:\r
56       /* try to convert to gzip stream, so we could use standard gzio functions from zlib */\r
57       gzf = zip2gz(zipfile, zipentry);\r
58       if (gzf == NULL)  goto zip_failed;\r
59 \r
60       file = malloc(sizeof(*file));\r
61       if (file == NULL) goto zip_failed;\r
62       file->file  = zipfile;\r
63       file->param = gzf;\r
64       file->size  = zipentry->uncompressed_size;\r
65       file->type  = PMT_ZIP;\r
66       return file;\r
67 \r
68 zip_failed:\r
69       if (gzf) {\r
70         gzclose(gzf);\r
71         zipfile->fp = NULL; // gzclose() closed it\r
72       }\r
73       closezip(zipfile);\r
74       return NULL;\r
75     }\r
76   }\r
77 \r
78   /* not a zip, treat as uncompressed file */\r
79   f = fopen(path, "rb");\r
80   if (f == NULL) return NULL;\r
81 \r
82   /* we use our own buffering */\r
83   setvbuf(f, NULL, _IONBF, 0);\r
84 \r
85   file = malloc(sizeof(*file));\r
86   if (file == NULL) {\r
87     fclose(f);\r
88     return NULL;\r
89   }\r
90   fseek(f, 0, SEEK_END);\r
91   file->file  = f;\r
92   file->param = NULL;\r
93   file->size  = ftell(f);\r
94   file->type  = PMT_UNCOMPRESSED;\r
95   fseek(f, 0, SEEK_SET);\r
96   return file;\r
97 }\r
98 \r
99 size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
100 {\r
101   int ret;\r
102 \r
103   if (stream->type == PMT_UNCOMPRESSED)\r
104   {\r
105     ret = fread(ptr, 1, bytes, stream->file);\r
106   }\r
107   else if (stream->type == PMT_ZIP)\r
108   {\r
109     gzFile gf = stream->param;\r
110     int err;\r
111     ret = gzread(gf, ptr, bytes);\r
112     err = gzerror2(gf);\r
113     if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))\r
114       /* we must reset stream pointer or else next seek/read fails */\r
115       gzrewind(gf);\r
116   }\r
117   else\r
118     ret = 0;\r
119 \r
120   return ret;\r
121 }\r
122 \r
123 int pm_seek(pm_file *stream, long offset, int whence)\r
124 {\r
125   if (stream->type == PMT_UNCOMPRESSED)\r
126   {\r
127     fseek(stream->file, offset, whence);\r
128     return ftell(stream->file);\r
129   }\r
130   else if (stream->type == PMT_ZIP)\r
131   {\r
132     if (PicoMessage != NULL && offset > 6*1024*1024) {\r
133       long pos = gztell((gzFile) stream->param);\r
134       if (offset < pos || offset - pos > 6*1024*1024)\r
135         PicoMessage("Decompressing data...");\r
136     }\r
137     return gzseek((gzFile) stream->param, offset, whence);\r
138   }\r
139   else\r
140     return -1;\r
141 }\r
142 \r
143 int pm_close(pm_file *fp)\r
144 {\r
145   int ret = 0;\r
146 \r
147   if (fp == NULL) return EOF;\r
148 \r
149   if (fp->type == PMT_UNCOMPRESSED)\r
150   {\r
151     fclose(fp->file);\r
152   }\r
153   else if (fp->type == PMT_ZIP)\r
154   {\r
155     ZIP *zipfile = fp->file;\r
156     gzclose((gzFile) fp->param);\r
157     zipfile->fp = NULL; // gzclose() closed it\r
158     closezip(zipfile);\r
159   }\r
160   else\r
161     ret = EOF;\r
162 \r
163   free(fp);\r
164   return ret;\r
165 }\r
166 \r
167 \r
168 void Byteswap(unsigned char *data,int len)\r
169 {\r
170   int i=0;\r
171 \r
172   if (len<2) return; // Too short\r
173 \r
174   do\r
175   {\r
176     unsigned short *pd=(unsigned short *)(data+i);\r
177     int value=*pd; // Get 2 bytes\r
178 \r
179     value=(value<<8)|(value>>8); // Byteswap it\r
180     *pd=(unsigned short)value; // Put 2b ytes\r
181     i+=2;\r
182   }\r
183   while (i+2<=len);\r
184 }\r
185 \r
186 // Interleve a 16k block and byteswap\r
187 static int InterleveBlock(unsigned char *dest,unsigned char *src)\r
188 {\r
189   int i=0;\r
190   for (i=0;i<0x2000;i++) dest[(i<<1)  ]=src[       i]; // Odd\r
191   for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even\r
192   return 0;\r
193 }\r
194 \r
195 // Decode a SMD file\r
196 static int DecodeSmd(unsigned char *data,int len)\r
197 {\r
198   unsigned char *temp=NULL;\r
199   int i=0;\r
200 \r
201   temp=(unsigned char *)malloc(0x4000);\r
202   if (temp==NULL) return 1;\r
203   memset(temp,0,0x4000);\r
204 \r
205   // Interleve each 16k block and shift down by 0x200:\r
206   for (i=0; i+0x4200<=len; i+=0x4000)\r
207   {\r
208     InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer\r
209     memcpy(data+i,temp,0x4000); // Copy back in\r
210   }\r
211 \r
212   free(temp);\r
213   return 0;\r
214 }\r
215 \r
216 static unsigned char *cd_realloc(void *old, int filesize)\r
217 {\r
218   unsigned char *rom;\r
219   dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));\r
220   rom=realloc(old, sizeof(mcd_state));\r
221   if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);\r
222   return rom;\r
223 }\r
224 \r
225 static unsigned char *PicoCartAlloc(int filesize)\r
226 {\r
227   int alloc_size;\r
228   unsigned char *rom;\r
229 \r
230   if (PicoMCD & 1) return cd_realloc(NULL, filesize);\r
231 \r
232   alloc_size=filesize+0x7ffff;\r
233   if((filesize&0x3fff)==0x200) alloc_size-=0x200;\r
234   alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently\r
235   if((filesize&0x3fff)==0x200) alloc_size+=0x200;\r
236   else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection\r
237   //dprintf("alloc_size: %x\n",  alloc_size);\r
238 \r
239   // Allocate space for the rom plus padding\r
240   rom=(unsigned char *)malloc(alloc_size);\r
241   if(rom) memset(rom+alloc_size-0x80000,0,0x80000);\r
242   return rom;\r
243 }\r
244 \r
245 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
246 {\r
247   unsigned char *rom=NULL; int size;\r
248   if (f==NULL) return 1;\r
249 \r
250   size=f->size;\r
251   if (size <= 0) return 1;\r
252   size=(size+3)&~3; // Round up to a multiple of 4\r
253 \r
254   // Allocate space for the rom plus padding\r
255   rom=PicoCartAlloc(size);\r
256   if (rom==NULL) {\r
257     printf("out of memory (wanted %i)\n", size);\r
258     return 1;\r
259   }\r
260 \r
261   pm_read(rom,size,f); // Load up the rom\r
262 \r
263   // maybe we are loading MegaCD BIOS?\r
264   if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
265     PicoMCD |= 1;\r
266     rom = cd_realloc(rom, size);\r
267   }\r
268 \r
269   // Check for SMD:\r
270   if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
271   else Byteswap(rom,size); // Just byteswap\r
272 \r
273   if (prom)  *prom=rom;\r
274   if (psize) *psize=size;\r
275 \r
276   return 0;\r
277 }\r
278 \r
279 // Insert/remove a cartridge:\r
280 int PicoCartInsert(unsigned char *rom,unsigned int romsize)\r
281 {\r
282   // notaz: add a 68k "jump one op back" opcode to the end of ROM.\r
283   // This will hang the emu, but will prevent nasty crashes.\r
284   // note: 4 bytes are padded to every ROM\r
285   if(rom != NULL)\r
286     *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
287 \r
288   SRam.resize=1;\r
289   Pico.rom=rom;\r
290   Pico.romsize=romsize;\r
291 \r
292   return PicoReset(1);\r
293 }\r
294 \r
295 int PicoUnloadCart(unsigned char* romdata)\r
296 {\r
297   free(romdata);\r
298   return 0;\r
299 }\r
300 \r