sn76496: simplify writes
[picodrive.git] / pico / cart.c
CommitLineData
cff531af 1/*\r
2 * PicoDrive\r
3 * (c) Copyright Dave, 2004\r
4 * (C) notaz, 2006-2010\r
5 *\r
6 * This work is licensed under the terms of MAME license.\r
7 * See COPYING file in the top-level directory.\r
8 */\r
cc68a136 9\r
efcba75f 10#include "pico_int.h"\r
a2b8c5a5 11#include "../cpu/debug.h"\r
83bd0b76 12#include "../unzip/unzip.h"\r
325ee167 13#include <zlib.h>\r
83bd0b76 14\r
7336a99a 15\r
b081408f 16static int rom_alloc_size;\r
3e49ffd0 17static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };\r
83bd0b76 18\r
45f2f245 19void (*PicoCartUnloadHook)(void);\r
20void (*PicoCartMemSetup)(void);\r
e807ac75 21\r
a9b3ffd3 22void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
b2451ac6 23void (*PicoCDLoadProgressCB)(const char *fname, int percent) = NULL; // handled in Pico/cd/cd_file.c\r
a9b3ffd3 24\r
4c2e3554 25int PicoGameLoaded;\r
26\r
ae87bffa 27static void PicoCartDetect(const char *carthw_cfg);\r
d6114368 28\r
71de3cd9 29/* cso struct */\r
30typedef struct _cso_struct\r
31{\r
32 unsigned char in_buff[2*2048];\r
33 unsigned char out_buff[2048];\r
34 struct {\r
35 char magic[4];\r
36 unsigned int unused;\r
37 unsigned int total_bytes;\r
38 unsigned int total_bytes_high; // ignored here\r
39 unsigned int block_size; // 10h\r
40 unsigned char ver;\r
41 unsigned char align;\r
42 unsigned char reserved[2];\r
43 } header;\r
44 unsigned int fpos_in; // input file read pointer\r
45 unsigned int fpos_out; // pos in virtual decompressed file\r
46 int block_in_buff; // block which we have read in in_buff\r
47 int pad;\r
48 int index[0];\r
49}\r
50cso_struct;\r
51\r
ad949800 52static int uncompress_buf(void *dest, int destLen, void *source, int sourceLen)\r
71de3cd9 53{\r
54 z_stream stream;\r
55 int err;\r
56\r
57 stream.next_in = (Bytef*)source;\r
58 stream.avail_in = (uInt)sourceLen;\r
59 stream.next_out = dest;\r
60 stream.avail_out = (uInt)destLen;\r
61\r
62 stream.zalloc = NULL;\r
63 stream.zfree = NULL;\r
64\r
65 err = inflateInit2(&stream, -15);\r
66 if (err != Z_OK) return err;\r
67\r
68 err = inflate(&stream, Z_FINISH);\r
69 if (err != Z_STREAM_END) {\r
70 inflateEnd(&stream);\r
71 return err;\r
72 }\r
73 //*destLen = stream.total_out;\r
74\r
75 return inflateEnd(&stream);\r
76}\r
83bd0b76 77\r
87b0845f 78static const char *get_ext(const char *path)\r
79{\r
80 const char *ext;\r
81 if (strlen(path) < 4)\r
82 return ""; // no ext\r
83\r
84 // allow 2 or 3 char extensions for now\r
85 ext = path + strlen(path) - 2;\r
86 if (ext[-1] != '.') ext--;\r
87 if (ext[-1] != '.')\r
88 return "";\r
89 return ext;\r
90}\r
91\r
b4c2331e 92struct zip_file {\r
93 pm_file file;\r
94 ZIP *zip;\r
95 struct zipent *entry;\r
96 z_stream stream;\r
97 unsigned char inbuf[16384];\r
98 long start;\r
99 unsigned int pos;\r
100};\r
101\r
83bd0b76 102pm_file *pm_open(const char *path)\r
103{\r
104 pm_file *file = NULL;\r
105 const char *ext;\r
106 FILE *f;\r
107\r
87b0845f 108 if (path == NULL)\r
109 return NULL;\r
83bd0b76 110\r
87b0845f 111 ext = get_ext(path);\r
112 if (strcasecmp(ext, "zip") == 0)\r
83bd0b76 113 {\r
b4c2331e 114 struct zip_file *zfile = NULL;\r
83bd0b76 115 struct zipent *zipentry;\r
83bd0b76 116 ZIP *zipfile;\r
b4c2331e 117 int i, ret;\r
83bd0b76 118\r
119 zipfile = openzip(path);\r
83bd0b76 120 if (zipfile != NULL)\r
121 {\r
122 /* search for suitable file (right extension or large enough file) */\r
123 while ((zipentry = readzip(zipfile)) != NULL)\r
124 {\r
87b0845f 125 ext = get_ext(zipentry->name);\r
83bd0b76 126\r
87b0845f 127 if (zipentry->uncompressed_size >= 32*1024)\r
128 goto found_rom_zip;\r
3e49ffd0 129\r
83bd0b76 130 for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
87b0845f 131 if (strcasecmp(ext, rom_exts[i]) == 0)\r
132 goto found_rom_zip;\r
83bd0b76 133 }\r
134\r
135 /* zipfile given, but nothing found suitable for us inside */\r
136 goto zip_failed;\r
137\r
138found_rom_zip:\r
b4c2331e 139 zfile = calloc(1, sizeof(*zfile));\r
140 if (zfile == NULL)\r
141 goto zip_failed;\r
142 ret = seekcompresszip(zipfile, zipentry);\r
143 if (ret != 0)\r
144 goto zip_failed;\r
145 ret = inflateInit2(&zfile->stream, -15);\r
146 if (ret != Z_OK) {\r
147 elprintf(EL_STATUS, "zip: inflateInit2 %d", ret);\r
148 goto zip_failed;\r
149 }\r
150 zfile->zip = zipfile;\r
151 zfile->entry = zipentry;\r
152 zfile->start = ftell(zipfile->fp);\r
153 zfile->file.file = zfile;\r
154 zfile->file.size = zipentry->uncompressed_size;\r
155 zfile->file.type = PMT_ZIP;\r
156 strncpy(zfile->file.ext, ext, sizeof(zfile->file.ext) - 1);\r
157 return &zfile->file;\r
83bd0b76 158\r
159zip_failed:\r
83bd0b76 160 closezip(zipfile);\r
b4c2331e 161 free(zfile);\r
83bd0b76 162 return NULL;\r
163 }\r
164 }\r
87b0845f 165 else if (strcasecmp(ext, "cso") == 0)\r
71de3cd9 166 {\r
167 cso_struct *cso = NULL, *tmp = NULL;\r
168 int size;\r
169 f = fopen(path, "rb");\r
170 if (f == NULL)\r
171 goto cso_failed;\r
172\r
274fcc35 173#ifdef __GP2X__\r
2d2247c2 174 /* we use our own buffering */\r
175 setvbuf(f, NULL, _IONBF, 0);\r
f8af9634 176#endif\r
2d2247c2 177\r
71de3cd9 178 cso = malloc(sizeof(*cso));\r
179 if (cso == NULL)\r
180 goto cso_failed;\r
181\r
182 if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))\r
183 goto cso_failed;\r
184\r
185 if (strncmp(cso->header.magic, "CISO", 4) != 0) {\r
186 elprintf(EL_STATUS, "cso: bad header");\r
187 goto cso_failed;\r
188 }\r
189\r
190 if (cso->header.block_size != 2048) {\r
191 elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);\r
192 goto cso_failed;\r
193 }\r
194\r
195 size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);\r
196 tmp = realloc(cso, size);\r
197 if (tmp == NULL)\r
198 goto cso_failed;\r
199 cso = tmp;\r
200 elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);\r
201\r
202 size -= sizeof(*cso); // index size\r
203 if (fread(cso->index, 1, size, f) != size) {\r
204 elprintf(EL_STATUS, "cso: premature EOF");\r
205 goto cso_failed;\r
206 }\r
207\r
208 // all ok\r
209 cso->fpos_in = ftell(f);\r
210 cso->fpos_out = 0;\r
211 cso->block_in_buff = -1;\r
87b0845f 212 file = calloc(1, sizeof(*file));\r
71de3cd9 213 if (file == NULL) goto cso_failed;\r
214 file->file = f;\r
215 file->param = cso;\r
216 file->size = cso->header.total_bytes;\r
217 file->type = PMT_CSO;\r
218 return file;\r
219\r
220cso_failed:\r
221 if (cso != NULL) free(cso);\r
222 if (f != NULL) fclose(f);\r
223 return NULL;\r
224 }\r
83bd0b76 225\r
226 /* not a zip, treat as uncompressed file */\r
227 f = fopen(path, "rb");\r
228 if (f == NULL) return NULL;\r
229\r
87b0845f 230 file = calloc(1, sizeof(*file));\r
83bd0b76 231 if (file == NULL) {\r
232 fclose(f);\r
233 return NULL;\r
234 }\r
235 fseek(f, 0, SEEK_END);\r
236 file->file = f;\r
237 file->param = NULL;\r
238 file->size = ftell(f);\r
239 file->type = PMT_UNCOMPRESSED;\r
87b0845f 240 strncpy(file->ext, ext, sizeof(file->ext) - 1);\r
83bd0b76 241 fseek(f, 0, SEEK_SET);\r
d6114368 242\r
274fcc35 243#ifdef __GP2X__\r
f8af9634 244 if (file->size > 0x400000)\r
245 /* we use our own buffering */\r
246 setvbuf(f, NULL, _IONBF, 0);\r
247#endif\r
248\r
83bd0b76 249 return file;\r
250}\r
251\r
252size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
253{\r
254 int ret;\r
255\r
256 if (stream->type == PMT_UNCOMPRESSED)\r
257 {\r
258 ret = fread(ptr, 1, bytes, stream->file);\r
259 }\r
260 else if (stream->type == PMT_ZIP)\r
261 {\r
b4c2331e 262 struct zip_file *z = stream->file;\r
263\r
264 if (z->entry->compression_method == 0) {\r
265 int ret = fread(ptr, 1, bytes, z->zip->fp);\r
266 z->pos += ret;\r
267 return ret;\r
268 }\r
269\r
270 z->stream.next_out = ptr;\r
271 z->stream.avail_out = bytes;\r
272 while (z->stream.avail_out != 0) {\r
273 if (z->stream.avail_in == 0) {\r
274 z->stream.avail_in = fread(z->inbuf, 1, sizeof(z->inbuf), z->zip->fp);\r
275 if (z->stream.avail_in == 0)\r
276 break;\r
277 z->stream.next_in = z->inbuf;\r
278 }\r
279 ret = inflate(&z->stream, Z_NO_FLUSH);\r
280 if (ret == Z_STREAM_END)\r
281 break;\r
282 if (ret != Z_OK) {\r
283 elprintf(EL_STATUS, "zip: inflate: %d", ret);\r
284 return 0;\r
285 }\r
286 }\r
287 z->pos += bytes - z->stream.avail_out;\r
288 return bytes - z->stream.avail_out;\r
83bd0b76 289 }\r
71de3cd9 290 else if (stream->type == PMT_CSO)\r
291 {\r
292 cso_struct *cso = stream->param;\r
293 int read_pos, read_len, out_offs, rret;\r
294 int block = cso->fpos_out >> 11;\r
295 int index = cso->index[block];\r
296 int index_end = cso->index[block+1];\r
297 unsigned char *out = ptr, *tmp_dst;\r
298\r
299 ret = 0;\r
300 while (bytes != 0)\r
301 {\r
302 out_offs = cso->fpos_out&0x7ff;\r
303 if (out_offs == 0 && bytes >= 2048)\r
304 tmp_dst = out;\r
305 else tmp_dst = cso->out_buff;\r
306\r
307 read_pos = (index&0x7fffffff) << cso->header.align;\r
308\r
309 if (index < 0) {\r
310 if (read_pos != cso->fpos_in)\r
311 fseek(stream->file, read_pos, SEEK_SET);\r
312 rret = fread(tmp_dst, 1, 2048, stream->file);\r
313 cso->fpos_in = read_pos + rret;\r
314 if (rret != 2048) break;\r
315 } else {\r
316 read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;\r
317 if (block != cso->block_in_buff)\r
318 {\r
319 if (read_pos != cso->fpos_in)\r
ff6b7429 320 fseek(stream->file, read_pos, SEEK_SET);\r
71de3cd9 321 rret = fread(cso->in_buff, 1, read_len, stream->file);\r
322 cso->fpos_in = read_pos + rret;\r
323 if (rret != read_len) {\r
324 elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);\r
325 break;\r
326 }\r
327 cso->block_in_buff = block;\r
328 }\r
ad949800 329 rret = uncompress_buf(tmp_dst, 2048, cso->in_buff, read_len);\r
71de3cd9 330 if (rret != 0) {\r
331 elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);\r
332 break;\r
333 }\r
334 }\r
335\r
336 rret = 2048;\r
337 if (out_offs != 0 || bytes < 2048) {\r
338 //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);\r
339 if (bytes < rret) rret = bytes;\r
340 if (2048 - out_offs < rret) rret = 2048 - out_offs;\r
341 memcpy(out, tmp_dst + out_offs, rret);\r
342 }\r
343 ret += rret;\r
344 out += rret;\r
345 cso->fpos_out += rret;\r
346 bytes -= rret;\r
347 block++;\r
348 index = index_end;\r
349 index_end = cso->index[block+1];\r
350 }\r
351 }\r
83bd0b76 352 else\r
353 ret = 0;\r
354\r
355 return ret;\r
356}\r
357\r
358int pm_seek(pm_file *stream, long offset, int whence)\r
359{\r
360 if (stream->type == PMT_UNCOMPRESSED)\r
361 {\r
7336a99a 362 fseek(stream->file, offset, whence);\r
363 return ftell(stream->file);\r
83bd0b76 364 }\r
365 else if (stream->type == PMT_ZIP)\r
366 {\r
b4c2331e 367 struct zip_file *z = stream->file;\r
368 unsigned int pos = z->pos;\r
369 int ret;\r
370\r
371 switch (whence)\r
372 {\r
373 case SEEK_CUR: pos += offset; break;\r
374 case SEEK_SET: pos = offset; break;\r
375 case SEEK_END: pos = stream->size - offset; break;\r
376 }\r
377 if (z->entry->compression_method == 0) {\r
378 ret = fseek(z->zip->fp, z->start + pos, SEEK_SET);\r
379 if (ret == 0)\r
380 return (z->pos = pos);\r
381 return -1;\r
382 }\r
383 offset = pos - z->pos;\r
384 if (pos < z->pos) {\r
385 // full decompress from the start\r
386 fseek(z->zip->fp, z->start, SEEK_SET);\r
387 z->stream.avail_in = 0;\r
388 z->stream.next_in = z->inbuf;\r
389 inflateReset(&z->stream);\r
390 z->pos = 0;\r
391 offset = pos;\r
392 }\r
393\r
394 if (PicoMessage != NULL && offset > 4 * 1024 * 1024)\r
395 PicoMessage("Decompressing data...");\r
396\r
397 while (offset > 0) {\r
398 char buf[16 * 1024];\r
399 size_t l = offset > sizeof(buf) ? sizeof(buf) : offset;\r
400 ret = pm_read(buf, l, stream);\r
401 if (ret != l)\r
402 break;\r
403 offset -= l;\r
66fdc0f0 404 }\r
b4c2331e 405 return z->pos;\r
83bd0b76 406 }\r
71de3cd9 407 else if (stream->type == PMT_CSO)\r
408 {\r
409 cso_struct *cso = stream->param;\r
410 switch (whence)\r
411 {\r
412 case SEEK_CUR: cso->fpos_out += offset; break;\r
413 case SEEK_SET: cso->fpos_out = offset; break;\r
414 case SEEK_END: cso->fpos_out = cso->header.total_bytes - offset; break;\r
415 }\r
416 return cso->fpos_out;\r
417 }\r
83bd0b76 418 else\r
419 return -1;\r
420}\r
421\r
422int pm_close(pm_file *fp)\r
423{\r
424 int ret = 0;\r
425\r
426 if (fp == NULL) return EOF;\r
427\r
428 if (fp->type == PMT_UNCOMPRESSED)\r
429 {\r
430 fclose(fp->file);\r
431 }\r
432 else if (fp->type == PMT_ZIP)\r
433 {\r
b4c2331e 434 struct zip_file *z = fp->file;\r
435 inflateEnd(&z->stream);\r
436 closezip(z->zip);\r
83bd0b76 437 }\r
71de3cd9 438 else if (fp->type == PMT_CSO)\r
439 {\r
440 free(fp->param);\r
441 fclose(fp->file);\r
442 }\r
83bd0b76 443 else\r
444 ret = EOF;\r
445\r
446 free(fp);\r
447 return ret;\r
448}\r
449\r
83ff19ec 450// byteswap, data needs to be int aligned, src can match dst\r
451void Byteswap(void *dst, const void *src, int len)\r
cc68a136 452{\r
83ff19ec 453 const unsigned int *ps = src;\r
454 unsigned int *pd = dst;\r
455 int i, m;\r
cc68a136 456\r
83ff19ec 457 if (len < 2)\r
458 return;\r
cc68a136 459\r
83ff19ec 460 m = 0x00ff00ff;\r
461 for (i = 0; i < len / 4; i++) {\r
462 unsigned int t = ps[i];\r
463 pd[i] = ((t & m) << 8) | ((t & ~m) >> 8);\r
cc68a136 464 }\r
cc68a136 465}\r
466\r
467// Interleve a 16k block and byteswap\r
468static int InterleveBlock(unsigned char *dest,unsigned char *src)\r
469{\r
470 int i=0;\r
471 for (i=0;i<0x2000;i++) dest[(i<<1) ]=src[ i]; // Odd\r
472 for (i=0;i<0x2000;i++) dest[(i<<1)+1]=src[0x2000+i]; // Even\r
473 return 0;\r
474}\r
475\r
476// Decode a SMD file\r
477static int DecodeSmd(unsigned char *data,int len)\r
478{\r
479 unsigned char *temp=NULL;\r
480 int i=0;\r
481\r
482 temp=(unsigned char *)malloc(0x4000);\r
483 if (temp==NULL) return 1;\r
484 memset(temp,0,0x4000);\r
485\r
486 // Interleve each 16k block and shift down by 0x200:\r
487 for (i=0; i+0x4200<=len; i+=0x4000)\r
488 {\r
489 InterleveBlock(temp,data+0x200+i); // Interleve 16k to temporary buffer\r
490 memcpy(data+i,temp,0x4000); // Copy back in\r
491 }\r
492\r
493 free(temp);\r
494 return 0;\r
495}\r
496\r
87b0845f 497static unsigned char *PicoCartAlloc(int filesize, int is_sms)\r
cc68a136 498{\r
cc68a136 499 unsigned char *rom;\r
500\r
87b0845f 501 if (is_sms) {\r
502 // make size power of 2 for easier banking handling\r
503 int s = 0, tmp = filesize;\r
504 while ((tmp >>= 1) != 0)\r
505 s++;\r
506 if (filesize > (1 << s))\r
507 s++;\r
b081408f 508 rom_alloc_size = 1 << s;\r
b4db550e 509 // be sure we can cover all address space\r
510 if (rom_alloc_size < 0x10000)\r
511 rom_alloc_size = 0x10000;\r
87b0845f 512 }\r
513 else {\r
b081408f 514 // make alloc size at least sizeof(mcd_state),\r
515 // in case we want to switch to CD mode\r
516 if (filesize < sizeof(mcd_state))\r
517 filesize = sizeof(mcd_state);\r
518\r
87b0845f 519 // align to 512K for memhandlers\r
b081408f 520 rom_alloc_size = (filesize + 0x7ffff) & ~0x7ffff;\r
87b0845f 521 }\r
cc68a136 522\r
b081408f 523 if (rom_alloc_size - filesize < 4)\r
524 rom_alloc_size += 4; // padding for out-of-bound exec protection\r
cc68a136 525\r
526 // Allocate space for the rom plus padding\r
b081408f 527 // use special address for 32x dynarec\r
e743be20 528 rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);\r
cc68a136 529 return rom;\r
530}\r
531\r
3e49ffd0 532int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)\r
cc68a136 533{\r
87b0845f 534 unsigned char *rom;\r
535 int size, bytes_read;\r
cc68a136 536\r
87b0845f 537 if (f == NULL)\r
538 return 1;\r
539\r
540 size = f->size;\r
cc68a136 541 if (size <= 0) return 1;\r
87b0845f 542 size = (size+3)&~3; // Round up to a multiple of 4\r
cc68a136 543\r
544 // Allocate space for the rom plus padding\r
87b0845f 545 rom = PicoCartAlloc(size, is_sms);\r
546 if (rom == NULL) {\r
c6196c0f 547 elprintf(EL_STATUS, "out of memory (wanted %i)", size);\r
f8af9634 548 return 2;\r
7336a99a 549 }\r
cc68a136 550\r
a9b3ffd3 551 if (PicoCartLoadProgressCB != NULL)\r
552 {\r
553 // read ROM in blocks, just for fun\r
554 int ret;\r
555 unsigned char *p = rom;\r
556 bytes_read=0;\r
557 do\r
558 {\r
559 int todo = size - bytes_read;\r
560 if (todo > 256*1024) todo = 256*1024;\r
561 ret = pm_read(p,todo,f);\r
562 bytes_read += ret;\r
563 p += ret;\r
564 PicoCartLoadProgressCB(bytes_read * 100 / size);\r
565 }\r
566 while (ret > 0);\r
567 }\r
568 else\r
569 bytes_read = pm_read(rom,size,f); // Load up the rom\r
570 if (bytes_read <= 0) {\r
c6196c0f 571 elprintf(EL_STATUS, "read failed");\r
01685eef 572 plat_munmap(rom, rom_alloc_size);\r
f8af9634 573 return 3;\r
a9b3ffd3 574 }\r
bf098bc5 575\r
3e49ffd0 576 if (!is_sms)\r
577 {\r
578 // maybe we are loading MegaCD BIOS?\r
579 if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||\r
580 !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
581 PicoAHW |= PAHW_MCD;\r
3e49ffd0 582 }\r
cc68a136 583\r
3e49ffd0 584 // Check for SMD:\r
87b0845f 585 if (size >= 0x4200 && (size&0x3fff) == 0x200 &&\r
3e49ffd0 586 ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {\r
87b0845f 587 elprintf(EL_STATUS, "SMD format detected.");\r
3e49ffd0 588 DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD\r
589 }\r
83ff19ec 590 else Byteswap(rom, rom, size); // Just byteswap\r
21395e38 591 }\r
87b0845f 592 else\r
593 {\r
594 if (size >= 0x4200 && (size&0x3fff) == 0x200) {\r
595 elprintf(EL_STATUS, "SMD format detected.");\r
596 // at least here it's not interleaved\r
597 size -= 0x200;\r
598 memmove(rom, rom + 0x200, size);\r
599 }\r
600 }\r
cc68a136 601\r
87b0845f 602 if (prom) *prom = rom;\r
603 if (psize) *psize = size;\r
cc68a136 604\r
605 return 0;\r
606}\r
607\r
f8ef8ff7 608// Insert a cartridge:\r
ae87bffa 609int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg)\r
cc68a136 610{\r
611 // notaz: add a 68k "jump one op back" opcode to the end of ROM.\r
612 // This will hang the emu, but will prevent nasty crashes.\r
613 // note: 4 bytes are padded to every ROM\r
d6114368 614 if (rom != NULL)\r
cc68a136 615 *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
616\r
cc68a136 617 Pico.rom=rom;\r
618 Pico.romsize=romsize;\r
619\r
d6114368 620 if (SRam.data) {\r
621 free(SRam.data);\r
622 SRam.data = NULL;\r
623 }\r
624\r
e807ac75 625 if (PicoCartUnloadHook != NULL) {\r
626 PicoCartUnloadHook();\r
627 PicoCartUnloadHook = NULL;\r
628 }\r
a2b8c5a5 629 pdb_cleanup();\r
e807ac75 630\r
3e49ffd0 631 PicoAHW &= PAHW_MCD|PAHW_SMS;\r
602133e1 632\r
45f2f245 633 PicoCartMemSetup = NULL;\r
f8ef8ff7 634 PicoDmaHook = NULL;\r
635 PicoResetHook = NULL;\r
017512f2 636 PicoLineHook = NULL;\r
fad24893 637 PicoLoadStateHook = NULL;\r
945c2fdc 638 carthw_chunks = NULL;\r
f8ef8ff7 639\r
3e49ffd0 640 if (!(PicoAHW & (PAHW_MCD|PAHW_SMS)))\r
ae87bffa 641 PicoCartDetect(carthw_cfg);\r
f8ef8ff7 642\r
1dceadae 643 // setup correct memory map for loaded ROM\r
9037e45d 644 switch (PicoAHW) {\r
645 default:\r
646 elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);\r
647 case 0:\r
648 case PAHW_SVP: PicoMemSetup(); break;\r
649 case PAHW_MCD: PicoMemSetupCD(); break;\r
650 case PAHW_PICO: PicoMemSetupPico(); break;\r
3e49ffd0 651 case PAHW_SMS: PicoMemSetupMS(); break;\r
9037e45d 652 }\r
1dceadae 653\r
45f2f245 654 if (PicoCartMemSetup != NULL)\r
655 PicoCartMemSetup();\r
656\r
3e49ffd0 657 if (PicoAHW & PAHW_SMS)\r
658 PicoPowerMS();\r
659 else\r
660 PicoPower();\r
661\r
4c2e3554 662 PicoGameLoaded = 1;\r
1cb1584b 663 return 0;\r
cc68a136 664}\r
665\r
a736af3e 666int PicoCartResize(int newsize)\r
667{\r
668 void *tmp = plat_mremap(Pico.rom, rom_alloc_size, newsize);\r
669 if (tmp == NULL)\r
670 return -1;\r
671\r
672 Pico.rom = tmp;\r
673 rom_alloc_size = newsize;\r
674 return 0;\r
675}\r
676\r
2aa27095 677void PicoCartUnload(void)\r
cc68a136 678{\r
3e49ffd0 679 if (PicoCartUnloadHook != NULL) {\r
680 PicoCartUnloadHook();\r
681 PicoCartUnloadHook = NULL;\r
682 }\r
683\r
5e49c3a8 684 if (PicoAHW & PAHW_32X)\r
685 PicoUnload32x();\r
686\r
a12b1b29 687 if (Pico.rom != NULL) {\r
053fd9b4 688 SekFinishIdleDet();\r
b081408f 689 plat_munmap(Pico.rom, rom_alloc_size);\r
690 Pico.rom = NULL;\r
a12b1b29 691 }\r
4c2e3554 692 PicoGameLoaded = 0;\r
cc68a136 693}\r
694\r
000f5335 695static unsigned int rom_crc32(void)\r
696{\r
697 unsigned int crc;\r
698 elprintf(EL_STATUS, "caclulating CRC32..");\r
699\r
700 // have to unbyteswap for calculation..\r
83ff19ec 701 Byteswap(Pico.rom, Pico.rom, Pico.romsize);\r
000f5335 702 crc = crc32(0, Pico.rom, Pico.romsize);\r
83ff19ec 703 Byteswap(Pico.rom, Pico.rom, Pico.romsize);\r
000f5335 704 return crc;\r
705}\r
706\r
1f4e9f14 707static int rom_strcmp(int rom_offset, const char *s1)\r
1dceadae 708{\r
1f4e9f14 709 int i, len = strlen(s1);\r
45f2f245 710 const char *s_rom = (const char *)Pico.rom;\r
a76fad41 711 if (rom_offset + len > Pico.romsize)\r
712 return 0;\r
1dceadae 713 for (i = 0; i < len; i++)\r
45f2f245 714 if (s1[i] != s_rom[(i + rom_offset) ^ 1])\r
1dceadae 715 return 1;\r
716 return 0;\r
717}\r
718\r
af37bca8 719static unsigned int rom_read32(int addr)\r
720{\r
721 unsigned short *m = (unsigned short *)(Pico.rom + addr);\r
722 return (m[0] << 16) | m[1];\r
723}\r
724\r
45f2f245 725static char *sskip(char *s)\r
726{\r
727 while (*s && isspace_(*s))\r
728 s++;\r
729 return s;\r
730}\r
731\r
732static void rstrip(char *s)\r
733{\r
734 char *p;\r
735 for (p = s + strlen(s) - 1; p >= s; p--)\r
736 if (isspace_(*p))\r
737 *p = 0;\r
738}\r
739\r
000f5335 740static int parse_3_vals(char *p, int *val0, int *val1, int *val2)\r
741{\r
742 char *r;\r
743 *val0 = strtoul(p, &r, 0);\r
744 if (r == p)\r
745 goto bad;\r
746 p = sskip(r);\r
747 if (*p++ != ',')\r
748 goto bad;\r
749 *val1 = strtoul(p, &r, 0);\r
750 if (r == p)\r
751 goto bad;\r
752 p = sskip(r);\r
753 if (*p++ != ',')\r
754 goto bad;\r
755 *val2 = strtoul(p, &r, 0);\r
756 if (r == p)\r
757 goto bad;\r
758\r
759 return 1;\r
760bad:\r
761 return 0;\r
762}\r
763\r
45f2f245 764static int is_expr(const char *expr, char **pr)\r
765{\r
766 int len = strlen(expr);\r
767 char *p = *pr;\r
768\r
769 if (strncmp(expr, p, len) != 0)\r
770 return 0;\r
771 p = sskip(p + len);\r
772 if (*p != '=')\r
773 return 0; // wrong or malformed\r
774\r
775 *pr = sskip(p + 1);\r
776 return 1;\r
777}\r
778\r
005651ce 779#include "carthw_cfg.c"\r
780\r
ae87bffa 781static void parse_carthw(const char *carthw_cfg, int *fill_sram)\r
45f2f245 782{\r
783 int line = 0, any_checks_passed = 0, skip_sect = 0;\r
005651ce 784 const char *s, *builtin = builtin_carthw_cfg;\r
000f5335 785 int tmp, rom_crc = 0;\r
45f2f245 786 char buff[256], *p, *r;\r
787 FILE *f;\r
788\r
ae87bffa 789 f = fopen(carthw_cfg, "r");\r
005651ce 790 if (f == NULL)\r
791 f = fopen("pico/carthw.cfg", "r");\r
792 if (f == NULL)\r
ae87bffa 793 elprintf(EL_STATUS, "couldn't open carthw.cfg!");\r
45f2f245 794\r
005651ce 795 for (;;)\r
45f2f245 796 {\r
005651ce 797 if (f != NULL) {\r
798 p = fgets(buff, sizeof(buff), f);\r
799 if (p == NULL)\r
800 break;\r
801 }\r
802 else {\r
803 if (*builtin == 0)\r
804 break;\r
805 for (s = builtin; *s != 0 && *s != '\n'; s++)\r
806 ;\r
807 while (*s == '\n')\r
808 s++;\r
809 tmp = s - builtin;\r
810 if (tmp > sizeof(buff) - 1)\r
811 tmp = sizeof(buff) - 1;\r
812 memcpy(buff, builtin, tmp);\r
813 buff[tmp] = 0;\r
814 p = buff;\r
815 builtin = s;\r
816 }\r
817\r
45f2f245 818 line++;\r
819 p = sskip(p);\r
820 if (*p == 0 || *p == '#')\r
821 continue;\r
822\r
823 if (*p == '[') {\r
824 any_checks_passed = 0;\r
825 skip_sect = 0;\r
826 continue;\r
827 }\r
828 \r
829 if (skip_sect)\r
830 continue;\r
831\r
832 /* look for checks */\r
833 if (is_expr("check_str", &p))\r
834 {\r
835 int offs;\r
836 offs = strtoul(p, &r, 0);\r
837 if (offs < 0 || offs > Pico.romsize) {\r
838 elprintf(EL_STATUS, "carthw:%d: check_str offs out of range: %d\n", line, offs);\r
839 goto bad;\r
840 }\r
841 p = sskip(r);\r
842 if (*p != ',')\r
843 goto bad;\r
844 p = sskip(p + 1);\r
845 if (*p != '"')\r
846 goto bad;\r
847 p++;\r
848 r = strchr(p, '"');\r
849 if (r == NULL)\r
850 goto bad;\r
851 *r = 0;\r
852\r
853 if (rom_strcmp(offs, p) == 0)\r
854 any_checks_passed = 1;\r
855 else\r
856 skip_sect = 1;\r
857 continue;\r
858 }\r
859 else if (is_expr("check_size_gt", &p))\r
860 {\r
861 int size;\r
862 size = strtoul(p, &r, 0);\r
863 if (r == p || size < 0)\r
864 goto bad;\r
865\r
866 if (Pico.romsize > size)\r
867 any_checks_passed = 1;\r
868 else\r
869 skip_sect = 1;\r
870 continue;\r
871 }\r
872 else if (is_expr("check_csum", &p))\r
873 {\r
874 int csum;\r
875 csum = strtoul(p, &r, 0);\r
876 if (r == p || (csum & 0xffff0000))\r
877 goto bad;\r
878\r
879 if (csum == (rom_read32(0x18c) & 0xffff))\r
880 any_checks_passed = 1;\r
881 else\r
882 skip_sect = 1;\r
883 continue;\r
884 }\r
000f5335 885 else if (is_expr("check_crc32", &p))\r
886 {\r
887 unsigned int crc;\r
888 crc = strtoul(p, &r, 0);\r
889 if (r == p)\r
890 goto bad;\r
891\r
892 if (rom_crc == 0)\r
893 rom_crc = rom_crc32();\r
894 if (crc == rom_crc)\r
895 any_checks_passed = 1;\r
896 else\r
897 skip_sect = 1;\r
898 continue;\r
899 }\r
45f2f245 900\r
901 /* now time for actions */\r
902 if (is_expr("hw", &p)) {\r
903 if (!any_checks_passed)\r
904 goto no_checks;\r
905 rstrip(p);\r
906\r
907 if (strcmp(p, "svp") == 0)\r
908 PicoSVPStartup();\r
909 else if (strcmp(p, "pico") == 0)\r
910 PicoInitPico();\r
000f5335 911 else if (strcmp(p, "prot") == 0)\r
912 carthw_sprot_startup();\r
913 else if (strcmp(p, "ssf2_mapper") == 0)\r
914 carthw_ssf2_startup();\r
45f2f245 915 else if (strcmp(p, "x_in_1_mapper") == 0)\r
916 carthw_Xin1_startup();\r
917 else if (strcmp(p, "realtec_mapper") == 0)\r
918 carthw_realtec_startup();\r
919 else if (strcmp(p, "radica_mapper") == 0)\r
920 carthw_radica_startup();\r
30f0fdd4 921 else if (strcmp(p, "piersolar_mapper") == 0)\r
922 carthw_pier_startup();\r
000f5335 923 else if (strcmp(p, "prot_lk3") == 0)\r
924 carthw_prot_lk3_startup();\r
45f2f245 925 else {\r
926 elprintf(EL_STATUS, "carthw:%d: unsupported mapper: %s", line, p);\r
927 skip_sect = 1;\r
928 }\r
929 continue;\r
930 }\r
931 if (is_expr("sram_range", &p)) {\r
932 int start, end;\r
933\r
934 if (!any_checks_passed)\r
935 goto no_checks;\r
936 rstrip(p);\r
937\r
938 start = strtoul(p, &r, 0);\r
939 if (r == p)\r
940 goto bad;\r
941 p = sskip(r);\r
942 if (*p != ',')\r
943 goto bad;\r
944 p = sskip(p + 1);\r
945 end = strtoul(p, &r, 0);\r
946 if (r == p)\r
947 goto bad;\r
948 if (((start | end) & 0xff000000) || start > end) {\r
949 elprintf(EL_STATUS, "carthw:%d: bad sram_range: %08x - %08x", line, start, end);\r
950 goto bad_nomsg;\r
951 }\r
952 SRam.start = start;\r
953 SRam.end = end;\r
954 continue;\r
955 }\r
956 else if (is_expr("prop", &p)) {\r
957 if (!any_checks_passed)\r
958 goto no_checks;\r
959 rstrip(p);\r
960\r
961 if (strcmp(p, "no_sram") == 0)\r
962 SRam.flags &= ~SRF_ENABLED;\r
963 else if (strcmp(p, "no_eeprom") == 0)\r
964 SRam.flags &= ~SRF_EEPROM;\r
965 else if (strcmp(p, "filled_sram") == 0)\r
966 *fill_sram = 1;\r
a76fad41 967 else if (strcmp(p, "force_6btn") == 0)\r
968 PicoQuirks |= PQUIRK_FORCE_6BTN;\r
45f2f245 969 else {\r
970 elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);\r
971 goto bad_nomsg;\r
972 }\r
a76fad41 973 elprintf(EL_STATUS, "game prop: %s", p);\r
45f2f245 974 continue;\r
975 }\r
976 else if (is_expr("eeprom_type", &p)) {\r
977 int type;\r
978 if (!any_checks_passed)\r
979 goto no_checks;\r
980 rstrip(p);\r
981\r
982 type = strtoul(p, &r, 0);\r
983 if (r == p || type < 0)\r
984 goto bad;\r
985 SRam.eeprom_type = type;\r
986 SRam.flags |= SRF_EEPROM;\r
987 continue;\r
988 }\r
989 else if (is_expr("eeprom_lines", &p)) {\r
990 int scl, sda_in, sda_out;\r
991 if (!any_checks_passed)\r
992 goto no_checks;\r
993 rstrip(p);\r
994\r
000f5335 995 if (!parse_3_vals(p, &scl, &sda_in, &sda_out))\r
45f2f245 996 goto bad;\r
000f5335 997 if (scl < 0 || scl > 15 || sda_in < 0 || sda_in > 15 ||\r
998 sda_out < 0 || sda_out > 15)\r
45f2f245 999 goto bad;\r
1000\r
1001 SRam.eeprom_bit_cl = scl;\r
1002 SRam.eeprom_bit_in = sda_in;\r
1003 SRam.eeprom_bit_out= sda_out;\r
1004 continue;\r
1005 }\r
000f5335 1006 else if ((tmp = is_expr("prot_ro_value16", &p)) || is_expr("prot_rw_value16", &p)) {\r
1007 int addr, mask, val;\r
1008 if (!any_checks_passed)\r
1009 goto no_checks;\r
1010 rstrip(p);\r
1011\r
1012 if (!parse_3_vals(p, &addr, &mask, &val))\r
1013 goto bad;\r
1014\r
1015 carthw_sprot_new_location(addr, mask, val, tmp ? 1 : 0);\r
1016 continue;\r
1017 }\r
45f2f245 1018\r
1019\r
1020bad:\r
1021 elprintf(EL_STATUS, "carthw:%d: unrecognized expression: %s", line, buff);\r
1022bad_nomsg:\r
1023 skip_sect = 1;\r
1024 continue;\r
1025\r
1026no_checks:\r
1027 elprintf(EL_STATUS, "carthw:%d: command without any checks before it: %s", line, buff);\r
1028 skip_sect = 1;\r
1029 continue;\r
1030 }\r
005651ce 1031\r
1032 if (f != NULL)\r
1033 fclose(f);\r
45f2f245 1034}\r
1035\r
a12b1b29 1036/*\r
1037 * various cart-specific things, which can't be handled by generic code\r
a12b1b29 1038 */\r
ae87bffa 1039static void PicoCartDetect(const char *carthw_cfg)\r
1dceadae 1040{\r
000f5335 1041 int fill_sram = 0;\r
1dceadae 1042\r
45f2f245 1043 memset(&SRam, 0, sizeof(SRam));\r
1dceadae 1044 if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')\r
1045 {\r
45f2f245 1046 SRam.start = rom_read32(0x1B4) & ~0xff000001; // align\r
1047 SRam.end = (rom_read32(0x1B8) & ~0xff000000) | 1;\r
1dceadae 1048 if (Pico.rom[0x1B2] & 0x40)\r
1dceadae 1049 // EEPROM\r
45f2f245 1050 SRam.flags |= SRF_EEPROM;\r
1051 SRam.flags |= SRF_ENABLED;\r
1dceadae 1052 }\r
45f2f245 1053 if (SRam.end == 0 || SRam.start > SRam.end)\r
1dceadae 1054 {\r
1055 // some games may have bad headers, like S&K and Sonic3\r
1056 // note: majority games use 0x200000 as starting address, but there are some which\r
1057 // use something else (0x300000 by HardBall '95). Luckily they have good headers.\r
1058 SRam.start = 0x200000;\r
1059 SRam.end = 0x203FFF;\r
45f2f245 1060 SRam.flags |= SRF_ENABLED;\r
1dceadae 1061 }\r
1dceadae 1062\r
1063 // set EEPROM defaults, in case it gets detected\r
1064 SRam.eeprom_type = 0; // 7bit (24C01)\r
1dceadae 1065 SRam.eeprom_bit_cl = 1;\r
1066 SRam.eeprom_bit_in = 0;\r
1067 SRam.eeprom_bit_out= 0;\r
1068\r
ae87bffa 1069 if (carthw_cfg != NULL)\r
1070 parse_carthw(carthw_cfg, &fill_sram);\r
9037e45d 1071\r
45f2f245 1072 if (SRam.flags & SRF_ENABLED)\r
757f8dae 1073 {\r
45f2f245 1074 if (SRam.flags & SRF_EEPROM)\r
1075 SRam.size = 0x2000;\r
1076 else\r
1077 SRam.size = SRam.end - SRam.start + 1;\r
757f8dae 1078\r
45f2f245 1079 SRam.data = calloc(SRam.size, 1);\r
1080 if (SRam.data == NULL)\r
1081 SRam.flags &= ~SRF_ENABLED;\r
a12b1b29 1082\r
45f2f245 1083 if (SRam.eeprom_type == 1) // 1 == 0 in PD EEPROM code\r
1084 SRam.eeprom_type = 0;\r
0b35350d 1085 }\r
1086\r
45f2f245 1087 if ((SRam.flags & SRF_ENABLED) && fill_sram)\r
757f8dae 1088 {\r
45f2f245 1089 elprintf(EL_STATUS, "SRAM fill");\r
1090 memset(SRam.data, 0xff, SRam.size);\r
757f8dae 1091 }\r
1f4e9f14 1092\r
1093 // Unusual region 'code'\r
453d2a6e 1094 if (rom_strcmp(0x1f0, "EUROPE") == 0 || rom_strcmp(0x1f0, "Europe") == 0)\r
45f2f245 1095 *(int *) (Pico.rom + 0x1f0) = 0x20204520;\r
1dceadae 1096}\r
1097\r
005651ce 1098// vim:shiftwidth=2:expandtab\r