psp fixes, gamma
[picodrive.git] / Pico / Cart.c
CommitLineData
cc68a136 1// This is part of Pico Library\r
2\r
3// (c) Copyright 2004 Dave, All rights reserved.\r
6cadc2da 4// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r
cc68a136 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
83bd0b76 11#include "../zlib/zlib.h"\r
12#include "../unzip/unzip.h"\r
13#include "../unzip/unzip_stream.h"\r
14\r
7336a99a 15\r
83bd0b76 16static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
17\r
a9b3ffd3 18void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
4b167c12 19void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c\r
a9b3ffd3 20\r
83bd0b76 21\r
22pm_file *pm_open(const char *path)\r
23{\r
24 pm_file *file = NULL;\r
25 const char *ext;\r
26 FILE *f;\r
27\r
28 if (path == NULL) return NULL;\r
29\r
30 if (strlen(path) < 5) ext = NULL; // no ext\r
31 else ext = path + strlen(path) - 3;\r
32\r
33 if (ext && strcasecmp(ext, "zip") == 0)\r
34 {\r
35 struct zipent *zipentry;\r
36 gzFile gzf = NULL;\r
37 ZIP *zipfile;\r
38 int i;\r
39\r
40 zipfile = openzip(path);\r
41\r
42 if (zipfile != NULL)\r
43 {\r
44 /* search for suitable file (right extension or large enough file) */\r
45 while ((zipentry = readzip(zipfile)) != NULL)\r
46 {\r
47 if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;\r
48 if (strlen(zipentry->name) < 5) continue;\r
49\r
50 ext = zipentry->name+strlen(zipentry->name)-3;\r
51 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
d0ae0cb4 52 if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
83bd0b76 53 }\r
54\r
55 /* zipfile given, but nothing found suitable for us inside */\r
56 goto zip_failed;\r
57\r
58found_rom_zip:\r
59 /* try to convert to gzip stream, so we could use standard gzio functions from zlib */\r
60 gzf = zip2gz(zipfile, zipentry);\r
61 if (gzf == NULL) goto zip_failed;\r
62\r
63 file = malloc(sizeof(*file));\r
64 if (file == NULL) goto zip_failed;\r
65 file->file = zipfile;\r
66 file->param = gzf;\r
67 file->size = zipentry->uncompressed_size;\r
68 file->type = PMT_ZIP;\r
69 return file;\r
70\r
71zip_failed:\r
72 if (gzf) {\r
73 gzclose(gzf);\r
74 zipfile->fp = NULL; // gzclose() closed it\r
75 }\r
76 closezip(zipfile);\r
77 return NULL;\r
78 }\r
79 }\r
80\r
81 /* not a zip, treat as uncompressed file */\r
82 f = fopen(path, "rb");\r
83 if (f == NULL) return NULL;\r
84\r
7336a99a 85 /* we use our own buffering */\r
86 setvbuf(f, NULL, _IONBF, 0);\r
87\r
83bd0b76 88 file = malloc(sizeof(*file));\r
89 if (file == NULL) {\r
90 fclose(f);\r
91 return NULL;\r
92 }\r
93 fseek(f, 0, SEEK_END);\r
94 file->file = f;\r
95 file->param = NULL;\r
96 file->size = ftell(f);\r
97 file->type = PMT_UNCOMPRESSED;\r
98 fseek(f, 0, SEEK_SET);\r
99 return file;\r
100}\r
101\r
102size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
103{\r
104 int ret;\r
105\r
106 if (stream->type == PMT_UNCOMPRESSED)\r
107 {\r
108 ret = fread(ptr, 1, bytes, stream->file);\r
109 }\r
110 else if (stream->type == PMT_ZIP)\r
111 {\r
112 gzFile gf = stream->param;\r
113 int err;\r
114 ret = gzread(gf, ptr, bytes);\r
115 err = gzerror2(gf);\r
116 if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))\r
117 /* we must reset stream pointer or else next seek/read fails */\r
118 gzrewind(gf);\r
119 }\r
120 else\r
121 ret = 0;\r
122\r
123 return ret;\r
124}\r
125\r
126int pm_seek(pm_file *stream, long offset, int whence)\r
127{\r
128 if (stream->type == PMT_UNCOMPRESSED)\r
129 {\r
7336a99a 130 fseek(stream->file, offset, whence);\r
131 return ftell(stream->file);\r
83bd0b76 132 }\r
133 else if (stream->type == PMT_ZIP)\r
134 {\r
66fdc0f0 135 if (PicoMessage != NULL && offset > 6*1024*1024) {\r
136 long pos = gztell((gzFile) stream->param);\r
137 if (offset < pos || offset - pos > 6*1024*1024)\r
138 PicoMessage("Decompressing data...");\r
139 }\r
83bd0b76 140 return gzseek((gzFile) stream->param, offset, whence);\r
141 }\r
142 else\r
143 return -1;\r
144}\r
145\r
146int pm_close(pm_file *fp)\r
147{\r
148 int ret = 0;\r
149\r
150 if (fp == NULL) return EOF;\r
151\r
152 if (fp->type == PMT_UNCOMPRESSED)\r
153 {\r
154 fclose(fp->file);\r
155 }\r
156 else if (fp->type == PMT_ZIP)\r
157 {\r
158 ZIP *zipfile = fp->file;\r
159 gzclose((gzFile) fp->param);\r
160 zipfile->fp = NULL; // gzclose() closed it\r
161 closezip(zipfile);\r
162 }\r
163 else\r
164 ret = EOF;\r
165\r
166 free(fp);\r
167 return ret;\r
168}\r
169\r
cc68a136 170\r
171void Byteswap(unsigned char *data,int len)\r
172{\r
173 int i=0;\r
174\r
175 if (len<2) return; // Too short\r
176\r
177 do\r
178 {\r
179 unsigned short *pd=(unsigned short *)(data+i);\r
180 int value=*pd; // Get 2 bytes\r
181\r
182 value=(value<<8)|(value>>8); // Byteswap it\r
183 *pd=(unsigned short)value; // Put 2b ytes\r
184 i+=2;\r
185 }\r
186 while (i+2<=len);\r
187}\r
188\r
189// Interleve a 16k block and byteswap\r
190static int InterleveBlock(unsigned char *dest,unsigned char *src)\r
191{\r
192 int i=0;\r
193 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd\r
194 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even\r
195 return 0;\r
196}\r
197\r
198// Decode a SMD file\r
199static int DecodeSmd(unsigned char *data,int len)\r
200{\r
201 unsigned char *temp=NULL;\r
202 int i=0;\r
203\r
204 temp=(unsigned char *)malloc(0x4000);\r
205 if (temp==NULL) return 1;\r
206 memset(temp,0,0x4000);\r
207\r
208 // Interleve each 16k block and shift down by 0x200:\r
209 for (i=0; i+0x4200<=len; i+=0x4000)\r
210 {\r
211 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer\r
212 memcpy(data+i,temp,0x4000); // Copy back in\r
213 }\r
214\r
215 free(temp);\r
216 return 0;\r
217}\r
218\r
bf098bc5 219static unsigned char *cd_realloc(void *old, int filesize)\r
220{\r
221 unsigned char *rom;\r
222 dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));\r
223 rom=realloc(old, sizeof(mcd_state));\r
224 if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);\r
225 return rom;\r
226}\r
227\r
cc68a136 228static unsigned char *PicoCartAlloc(int filesize)\r
229{\r
230 int alloc_size;\r
231 unsigned char *rom;\r
232\r
bf098bc5 233 if (PicoMCD & 1) return cd_realloc(NULL, filesize);\r
cc68a136 234\r
235 alloc_size=filesize+0x7ffff;\r
236 if((filesize&0x3fff)==0x200) alloc_size-=0x200;\r
237 alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently\r
238 if((filesize&0x3fff)==0x200) alloc_size+=0x200;\r
239 else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection\r
240 //dprintf("alloc_size: %x\n", alloc_size);\r
241\r
242 // Allocate space for the rom plus padding\r
243 rom=(unsigned char *)malloc(alloc_size);\r
244 if(rom) memset(rom+alloc_size-0x80000,0,0x80000);\r
245 return rom;\r
246}\r
247\r
83bd0b76 248int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
cc68a136 249{\r
a9b3ffd3 250 unsigned char *rom=NULL; int size, bytes_read;\r
cc68a136 251 if (f==NULL) return 1;\r
252\r
83bd0b76 253 size=f->size;\r
cc68a136 254 if (size <= 0) return 1;\r
bf098bc5 255 size=(size+3)&~3; // Round up to a multiple of 4\r
cc68a136 256\r
257 // Allocate space for the rom plus padding\r
258 rom=PicoCartAlloc(size);\r
7336a99a 259 if (rom==NULL) {\r
c6196c0f 260 elprintf(EL_STATUS, "out of memory (wanted %i)", size);\r
7336a99a 261 return 1;\r
262 }\r
cc68a136 263\r
a9b3ffd3 264 if (PicoCartLoadProgressCB != NULL)\r
265 {\r
266 // read ROM in blocks, just for fun\r
267 int ret;\r
268 unsigned char *p = rom;\r
269 bytes_read=0;\r
270 do\r
271 {\r
272 int todo = size - bytes_read;\r
273 if (todo > 256*1024) todo = 256*1024;\r
274 ret = pm_read(p,todo,f);\r
275 bytes_read += ret;\r
276 p += ret;\r
277 PicoCartLoadProgressCB(bytes_read * 100 / size);\r
278 }\r
279 while (ret > 0);\r
280 }\r
281 else\r
282 bytes_read = pm_read(rom,size,f); // Load up the rom\r
283 if (bytes_read <= 0) {\r
c6196c0f 284 elprintf(EL_STATUS, "read failed");\r
a9b3ffd3 285 free(rom);\r
286 return 1;\r
287 }\r
bf098bc5 288\r
289 // maybe we are loading MegaCD BIOS?\r
290 if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
291 PicoMCD |= 1;\r
292 rom = cd_realloc(rom, size);\r
293 }\r
cc68a136 294\r
295 // Check for SMD:\r
296 if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
297 else Byteswap(rom,size); // Just byteswap\r
298\r
299 if (prom) *prom=rom;\r
300 if (psize) *psize=size;\r
301\r
302 return 0;\r
303}\r
304\r
305// Insert/remove a cartridge:\r
306int PicoCartInsert(unsigned char *rom,unsigned int romsize)\r
307{\r
308 // notaz: add a 68k "jump one op back" opcode to the end of ROM.\r
309 // This will hang the emu, but will prevent nasty crashes.\r
310 // note: 4 bytes are padded to every ROM\r
311 if(rom != NULL)\r
312 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
313\r
cc68a136 314 Pico.rom=rom;\r
315 Pico.romsize=romsize;\r
316\r
1dceadae 317 // setup correct memory map for loaded ROM\r
318 if (PicoMCD & 1)\r
319 PicoMemSetupCD();\r
320 else PicoMemSetup();\r
321 PicoMemReset();\r
322\r
323 if (!(PicoMCD & 1))\r
324 PicoCartDetect();\r
325\r
cc68a136 326 return PicoReset(1);\r
327}\r
328\r
329int PicoUnloadCart(unsigned char* romdata)\r
330{\r
331 free(romdata);\r
332 return 0;\r
333}\r
334\r
1f4e9f14 335static int rom_strcmp(int rom_offset, const char *s1)\r
1dceadae 336{\r
1f4e9f14 337 int i, len = strlen(s1);\r
338 const char *s_rom = (const char *)Pico.rom + rom_offset;\r
1dceadae 339 for (i = 0; i < len; i++)\r
1f4e9f14 340 if (s1[i] != s_rom[i^1])\r
1dceadae 341 return 1;\r
342 return 0;\r
343}\r
344\r
1f4e9f14 345static int name_cmp(const char *name)\r
346{\r
347 return rom_strcmp(0x150, name);\r
348}\r
349\r
1dceadae 350/* various cart-specific things, which can't be handled by generic code */\r
351void PicoCartDetect(void)\r
352{\r
353 int sram_size = 0, csum;\r
354 if(SRam.data) free(SRam.data); SRam.data=0;\r
355 Pico.m.sram_reg = 0;\r
356\r
357 csum = PicoRead32(0x18c) & 0xffff;\r
358\r
359 if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')\r
360 {\r
361 if (Pico.rom[0x1B2] & 0x40)\r
362 {\r
363 // EEPROM\r
364 SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games\r
365 SRam.end = PicoRead32(0x1B8);\r
366 sram_size = 0x2000;\r
367 Pico.m.sram_reg |= 4;\r
368 } else {\r
369 // normal SRAM\r
370 SRam.start = PicoRead32(0x1B4) & 0xFFFF00;\r
371 SRam.end = PicoRead32(0x1B8) | 1;\r
372 sram_size = SRam.end - SRam.start + 1;\r
373 }\r
374 Pico.m.sram_reg |= 0x10; // SRAM was detected\r
375 }\r
376 if (sram_size <= 0)\r
377 {\r
378 // some games may have bad headers, like S&K and Sonic3\r
379 // note: majority games use 0x200000 as starting address, but there are some which\r
380 // use something else (0x300000 by HardBall '95). Luckily they have good headers.\r
381 SRam.start = 0x200000;\r
382 SRam.end = 0x203FFF;\r
383 sram_size = 0x004000;\r
384 }\r
385\r
386 if (sram_size)\r
387 {\r
388 SRam.data = (unsigned char *) calloc(sram_size, 1);\r
389 if(!SRam.data) return;\r
390 }\r
391 SRam.changed = 0;\r
392\r
393 // set EEPROM defaults, in case it gets detected\r
394 SRam.eeprom_type = 0; // 7bit (24C01)\r
395 SRam.eeprom_abits = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in\r
396 SRam.eeprom_bit_cl = 1;\r
397 SRam.eeprom_bit_in = 0;\r
398 SRam.eeprom_bit_out= 0;\r
399\r
400 // some known EEPROM data (thanks to EkeEke)\r
401 if (name_cmp("COLLEGE SLAM") == 0 ||\r
402 name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)\r
403 {\r
404 SRam.eeprom_type = 3;\r
405 SRam.eeprom_abits = 2;\r
406 SRam.eeprom_bit_cl = 0;\r
407 }\r
408 else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||\r
409 name_cmp("NFL QUARTERBACK CLUB") == 0)\r
410 {\r
411 SRam.eeprom_type = 2;\r
412 SRam.eeprom_abits = 2;\r
413 SRam.eeprom_bit_cl = 0;\r
414 }\r
415 else if (name_cmp("NBA JAM") == 0)\r
416 {\r
417 SRam.eeprom_type = 2;\r
418 SRam.eeprom_bit_out = 1;\r
419 SRam.eeprom_abits = 0;\r
420 }\r
421 else if (name_cmp("NHLPA HOCKEY '93") == 0 ||\r
422 name_cmp("NHLPA Hockey '93") == 0 ||\r
423 name_cmp("RINGS OF POWER") == 0)\r
424 {\r
425 SRam.start = SRam.end = 0x200000;\r
426 Pico.m.sram_reg = 0x14;\r
427 SRam.eeprom_abits = 0;\r
428 SRam.eeprom_bit_cl = 6;\r
429 SRam.eeprom_bit_in = 7;\r
430 SRam.eeprom_bit_out= 7;\r
431 }\r
432 else if ( name_cmp("MICRO MACHINES II") == 0 ||\r
433 (name_cmp(" ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}\r
434 (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))\r
435 {\r
436 SRam.start = 0x300000;\r
437 SRam.end = 0x380001;\r
438 Pico.m.sram_reg = 0x14;\r
439 SRam.eeprom_type = 2;\r
440 SRam.eeprom_abits = 0;\r
441 SRam.eeprom_bit_cl = 1;\r
442 SRam.eeprom_bit_in = 0;\r
443 SRam.eeprom_bit_out= 7;\r
444 }\r
445\r
446 // Some games malfunction if SRAM is not filled with 0xff\r
447 if (name_cmp("DINO DINI'S SOCCER") == 0 ||\r
448 name_cmp("MICRO MACHINES II") == 0)\r
449 memset(SRam.data, 0xff, sram_size);\r
1f4e9f14 450\r
451 // Unusual region 'code'\r
452 if (rom_strcmp(0x1f0, "EUROPE") == 0)\r
453 *(int *) (Pico.rom+0x1f0) = 0x20204520;\r
1dceadae 454}\r
455\r