use system's zlib
[picodrive.git] / pico / cart.c
index 42d0817..9dce38d 100644 (file)
@@ -1,17 +1,16 @@
-// This is part of Pico Library\r
-\r
-// (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
-\r
+/*\r
+ * PicoDrive\r
+ * (c) Copyright Dave, 2004\r
+ * (C) notaz, 2006-2010\r
+ *\r
+ * This work is licensed under the terms of MAME license.\r
+ * See COPYING file in the top-level directory.\r
+ */\r
 \r
 #include "pico_int.h"\r
-#include "../zlib/zlib.h"\r
 #include "../cpu/debug.h"\r
 #include "../unzip/unzip.h"\r
-#include "../unzip/unzip_stream.h"\r
+#include <zlib.h>\r
 \r
 \r
 static int rom_alloc_size;\r
@@ -23,6 +22,8 @@ void (*PicoCartMemSetup)(void);
 void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
 void (*PicoCDLoadProgressCB)(const char *fname, int percent) = NULL; // handled in Pico/cd/cd_file.c\r
 \r
+int PicoGameLoaded;\r
+\r
 static void PicoCartDetect(const char *carthw_cfg);\r
 \r
 /* cso struct */\r
@@ -48,7 +49,7 @@ typedef struct _cso_struct
 }\r
 cso_struct;\r
 \r
-static int uncompress2(void *dest, int destLen, void *source, int sourceLen)\r
+static int uncompress_buf(void *dest, int destLen, void *source, int sourceLen)\r
 {\r
     z_stream stream;\r
     int err;\r
@@ -98,6 +99,7 @@ pm_file *pm_open(const char *path)
     return NULL;\r
 \r
   ext = get_ext(path);\r
+#ifndef NO_ZLIB\r
   if (strcasecmp(ext, "zip") == 0)\r
   {\r
     struct zipent *zipentry;\r
@@ -147,7 +149,9 @@ zip_failed:
       return NULL;\r
     }\r
   }\r
-  else if (strcasecmp(ext, "cso") == 0)\r
+  else\r
+#endif\r
+  if (strcasecmp(ext, "cso") == 0)\r
   {\r
     cso_struct *cso = NULL, *tmp = NULL;\r
     int size;\r
@@ -155,7 +159,7 @@ zip_failed:
     if (f == NULL)\r
       goto cso_failed;\r
 \r
-#ifndef __EPOC32__\r
+#ifdef __GP2X__\r
     /* we use our own buffering */\r
     setvbuf(f, NULL, _IONBF, 0);\r
 #endif\r
@@ -225,7 +229,7 @@ cso_failed:
   strncpy(file->ext, ext, sizeof(file->ext) - 1);\r
   fseek(f, 0, SEEK_SET);\r
 \r
-#ifndef __EPOC32__ // makes things worse on Symbian\r
+#ifdef __GP2X__\r
   if (file->size > 0x400000)\r
     /* we use our own buffering */\r
     setvbuf(f, NULL, _IONBF, 0);\r
@@ -242,6 +246,7 @@ size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
   {\r
     ret = fread(ptr, 1, bytes, stream->file);\r
   }\r
+#ifndef NO_ZLIB\r
   else if (stream->type == PMT_ZIP)\r
   {\r
     gzFile gf = stream->param;\r
@@ -252,6 +257,7 @@ size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
       /* we must reset stream pointer or else next seek/read fails */\r
       gzrewind(gf);\r
   }\r
+#endif\r
   else if (stream->type == PMT_CSO)\r
   {\r
     cso_struct *cso = stream->param;\r
@@ -291,7 +297,7 @@ size_t pm_read(void *ptr, size_t bytes, pm_file *stream)
           }\r
           cso->block_in_buff = block;\r
         }\r
-        rret = uncompress2(tmp_dst, 2048, cso->in_buff, read_len);\r
+        rret = uncompress_buf(tmp_dst, 2048, cso->in_buff, read_len);\r
         if (rret != 0) {\r
           elprintf(EL_STATUS, "cso: uncompress failed @ %08x with %i", read_pos, rret);\r
           break;\r
@@ -327,6 +333,7 @@ int pm_seek(pm_file *stream, long offset, int whence)
     fseek(stream->file, offset, whence);\r
     return ftell(stream->file);\r
   }\r
+#ifndef NO_ZLIB\r
   else if (stream->type == PMT_ZIP)\r
   {\r
     if (PicoMessage != NULL && offset > 6*1024*1024) {\r
@@ -336,6 +343,7 @@ int pm_seek(pm_file *stream, long offset, int whence)
     }\r
     return gzseek((gzFile) stream->param, offset, whence);\r
   }\r
+#endif\r
   else if (stream->type == PMT_CSO)\r
   {\r
     cso_struct *cso = stream->param;\r
@@ -361,6 +369,7 @@ int pm_close(pm_file *fp)
   {\r
     fclose(fp->file);\r
   }\r
+#ifndef NO_ZLIB\r
   else if (fp->type == PMT_ZIP)\r
   {\r
     ZIP *zipfile = fp->file;\r
@@ -368,6 +377,7 @@ int pm_close(pm_file *fp)
     zipfile->fp = NULL; // gzclose() closed it\r
     closezip(zipfile);\r
   }\r
+#endif\r
   else if (fp->type == PMT_CSO)\r
   {\r
     free(fp->param);\r
@@ -439,6 +449,9 @@ static unsigned char *PicoCartAlloc(int filesize, int is_sms)
     if (filesize > (1 << s))\r
       s++;\r
     rom_alloc_size = 1 << s;\r
+    // be sure we can cover all address space\r
+    if (rom_alloc_size < 0x10000)\r
+      rom_alloc_size = 0x10000;\r
   }\r
   else {\r
     // make alloc size at least sizeof(mcd_state),\r
@@ -455,7 +468,7 @@ static unsigned char *PicoCartAlloc(int filesize, int is_sms)
 \r
   // Allocate space for the rom plus padding\r
   // use special address for 32x dynarec\r
-  rom = plat_mmap(0x02000000, rom_alloc_size);\r
+  rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);\r
   return rom;\r
 }\r
 \r
@@ -499,7 +512,7 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
     bytes_read = pm_read(rom,size,f); // Load up the rom\r
   if (bytes_read <= 0) {\r
     elprintf(EL_STATUS, "read failed");\r
-    free(rom);\r
+    plat_munmap(rom, rom_alloc_size);\r
     return 3;\r
   }\r
 \r
@@ -589,6 +602,18 @@ int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_
   else\r
     PicoPower();\r
 \r
+  PicoGameLoaded = 1;\r
+  return 0;\r
+}\r
+\r
+int PicoCartResize(int newsize)\r
+{\r
+  void *tmp = plat_mremap(Pico.rom, rom_alloc_size, newsize);\r
+  if (tmp == NULL)\r
+    return -1;\r
+\r
+  Pico.rom = tmp;\r
+  rom_alloc_size = newsize;\r
   return 0;\r
 }\r
 \r
@@ -607,6 +632,7 @@ void PicoCartUnload(void)
     plat_munmap(Pico.rom, rom_alloc_size);\r
     Pico.rom = NULL;\r
   }\r
+  PicoGameLoaded = 0;\r
 }\r
 \r
 static unsigned int rom_crc32(void)\r
@@ -625,6 +651,8 @@ static int rom_strcmp(int rom_offset, const char *s1)
 {\r
   int i, len = strlen(s1);\r
   const char *s_rom = (const char *)Pico.rom;\r
+  if (rom_offset + len > Pico.romsize)\r
+    return 0;\r
   for (i = 0; i < len; i++)\r
     if (s1[i] != s_rom[(i + rom_offset) ^ 1])\r
       return 1;\r
@@ -691,21 +719,45 @@ static int is_expr(const char *expr, char **pr)
   return 1;\r
 }\r
 \r
+#include "carthw_cfg.c"\r
+\r
 static void parse_carthw(const char *carthw_cfg, int *fill_sram)\r
 {\r
   int line = 0, any_checks_passed = 0, skip_sect = 0;\r
+  const char *s, *builtin = builtin_carthw_cfg;\r
   int tmp, rom_crc = 0;\r
   char buff[256], *p, *r;\r
   FILE *f;\r
 \r
   f = fopen(carthw_cfg, "r");\r
-  if (f == NULL) {\r
+  if (f == NULL)\r
+    f = fopen("pico/carthw.cfg", "r");\r
+  if (f == NULL)\r
     elprintf(EL_STATUS, "couldn't open carthw.cfg!");\r
-    return;\r
-  }\r
 \r
-  while ((p = fgets(buff, sizeof(buff), f)))\r
+  for (;;)\r
   {\r
+    if (f != NULL) {\r
+      p = fgets(buff, sizeof(buff), f);\r
+      if (p == NULL)\r
+        break;\r
+    }\r
+    else {\r
+      if (*builtin == 0)\r
+        break;\r
+      for (s = builtin; *s != 0 && *s != '\n'; s++)\r
+        ;\r
+      while (*s == '\n')\r
+        s++;\r
+      tmp = s - builtin;\r
+      if (tmp > sizeof(buff) - 1)\r
+        tmp = sizeof(buff) - 1;\r
+      memcpy(buff, builtin, tmp);\r
+      buff[tmp] = 0;\r
+      p = buff;\r
+      builtin = s;\r
+    }\r
+\r
     line++;\r
     p = sskip(p);\r
     if (*p == 0 || *p == '#')\r
@@ -809,6 +861,8 @@ static void parse_carthw(const char *carthw_cfg, int *fill_sram)
         carthw_realtec_startup();\r
       else if (strcmp(p, "radica_mapper") == 0)\r
         carthw_radica_startup();\r
+      else if (strcmp(p, "piersolar_mapper") == 0)\r
+        carthw_pier_startup();\r
       else if (strcmp(p, "prot_lk3") == 0)\r
         carthw_prot_lk3_startup();\r
       else {\r
@@ -853,10 +907,13 @@ static void parse_carthw(const char *carthw_cfg, int *fill_sram)
         SRam.flags &= ~SRF_EEPROM;\r
       else if (strcmp(p, "filled_sram") == 0)\r
         *fill_sram = 1;\r
+      else if (strcmp(p, "force_6btn") == 0)\r
+        PicoQuirks |= PQUIRK_FORCE_6BTN;\r
       else {\r
         elprintf(EL_STATUS, "carthw:%d: unsupported prop: %s", line, p);\r
         goto bad_nomsg;\r
       }\r
+      elprintf(EL_STATUS, "game prop: %s", p);\r
       continue;\r
     }\r
     else if (is_expr("eeprom_type", &p)) {\r
@@ -914,7 +971,9 @@ no_checks:
     skip_sect = 1;\r
     continue;\r
   }\r
-  fclose(f);\r
+\r
+  if (f != NULL)\r
+    fclose(f);\r
 }\r
 \r
 /*\r
@@ -979,3 +1038,4 @@ static void PicoCartDetect(const char *carthw_cfg)
     *(int *) (Pico.rom + 0x1f0) = 0x20204520;\r
 }\r
 \r
+// vim:shiftwidth=2:expandtab\r