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