bugfix/improvement
[picodrive.git] / Pico / Cart.c
index d3e7743..a9fe6ec 100644 (file)
 // This is part of Pico Library\r
 \r
 // (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006 notaz, 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
 #include "PicoInt.h"\r
+#include "../zlib/zlib.h"\r
+#include "../unzip/unzip.h"\r
+#include "../unzip/unzip_stream.h"\r
+\r
+\r
+static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
+\r
+void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
+\r
+\r
+pm_file *pm_open(const char *path)\r
+{\r
+  pm_file *file = NULL;\r
+  const char *ext;\r
+  FILE *f;\r
+\r
+  if (path == NULL) return NULL;\r
+\r
+  if (strlen(path) < 5) ext = NULL; // no ext\r
+  else ext = path + strlen(path) - 3;\r
+\r
+  if (ext && strcasecmp(ext, "zip") == 0)\r
+  {\r
+    struct zipent *zipentry;\r
+    gzFile gzf = NULL;\r
+    ZIP *zipfile;\r
+    int i;\r
+\r
+    zipfile = openzip(path);\r
+\r
+    if (zipfile != NULL)\r
+    {\r
+      /* search for suitable file (right extension or large enough file) */\r
+      while ((zipentry = readzip(zipfile)) != NULL)\r
+      {\r
+        if (zipentry->uncompressed_size >= 128*1024) goto found_rom_zip;\r
+        if (strlen(zipentry->name) < 5) continue;\r
+\r
+        ext = zipentry->name+strlen(zipentry->name)-3;\r
+        for (i = 0; i < sizeof(rom_exts)/sizeof(rom_exts[0]); i++)\r
+          if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
+      }\r
+\r
+      /* zipfile given, but nothing found suitable for us inside */\r
+      goto zip_failed;\r
+\r
+found_rom_zip:\r
+      /* try to convert to gzip stream, so we could use standard gzio functions from zlib */\r
+      gzf = zip2gz(zipfile, zipentry);\r
+      if (gzf == NULL)  goto zip_failed;\r
+\r
+      file = malloc(sizeof(*file));\r
+      if (file == NULL) goto zip_failed;\r
+      file->file  = zipfile;\r
+      file->param = gzf;\r
+      file->size  = zipentry->uncompressed_size;\r
+      file->type  = PMT_ZIP;\r
+      return file;\r
+\r
+zip_failed:\r
+      if (gzf) {\r
+        gzclose(gzf);\r
+        zipfile->fp = NULL; // gzclose() closed it\r
+      }\r
+      closezip(zipfile);\r
+      return NULL;\r
+    }\r
+  }\r
+\r
+  /* not a zip, treat as uncompressed file */\r
+  f = fopen(path, "rb");\r
+  if (f == NULL) return NULL;\r
+\r
+  /* we use our own buffering */\r
+  setvbuf(f, NULL, _IONBF, 0);\r
+\r
+  file = malloc(sizeof(*file));\r
+  if (file == NULL) {\r
+    fclose(f);\r
+    return NULL;\r
+  }\r
+  fseek(f, 0, SEEK_END);\r
+  file->file  = f;\r
+  file->param = NULL;\r
+  file->size  = ftell(f);\r
+  file->type  = PMT_UNCOMPRESSED;\r
+  fseek(f, 0, SEEK_SET);\r
+  return file;\r
+}\r
+\r
+size_t pm_read(void *ptr, size_t bytes, pm_file *stream)\r
+{\r
+  int ret;\r
+\r
+  if (stream->type == PMT_UNCOMPRESSED)\r
+  {\r
+    ret = fread(ptr, 1, bytes, stream->file);\r
+  }\r
+  else if (stream->type == PMT_ZIP)\r
+  {\r
+    gzFile gf = stream->param;\r
+    int err;\r
+    ret = gzread(gf, ptr, bytes);\r
+    err = gzerror2(gf);\r
+    if (ret > 0 && (err == Z_DATA_ERROR || err == Z_STREAM_END))\r
+      /* we must reset stream pointer or else next seek/read fails */\r
+      gzrewind(gf);\r
+  }\r
+  else\r
+    ret = 0;\r
+\r
+  return ret;\r
+}\r
+\r
+int pm_seek(pm_file *stream, long offset, int whence)\r
+{\r
+  if (stream->type == PMT_UNCOMPRESSED)\r
+  {\r
+    fseek(stream->file, offset, whence);\r
+    return ftell(stream->file);\r
+  }\r
+  else if (stream->type == PMT_ZIP)\r
+  {\r
+    if (PicoMessage != NULL && offset > 6*1024*1024) {\r
+      long pos = gztell((gzFile) stream->param);\r
+      if (offset < pos || offset - pos > 6*1024*1024)\r
+        PicoMessage("Decompressing data...");\r
+    }\r
+    return gzseek((gzFile) stream->param, offset, whence);\r
+  }\r
+  else\r
+    return -1;\r
+}\r
+\r
+int pm_close(pm_file *fp)\r
+{\r
+  int ret = 0;\r
+\r
+  if (fp == NULL) return EOF;\r
+\r
+  if (fp->type == PMT_UNCOMPRESSED)\r
+  {\r
+    fclose(fp->file);\r
+  }\r
+  else if (fp->type == PMT_ZIP)\r
+  {\r
+    ZIP *zipfile = fp->file;\r
+    gzclose((gzFile) fp->param);\r
+    zipfile->fp = NULL; // gzclose() closed it\r
+    closezip(zipfile);\r
+  }\r
+  else\r
+    ret = EOF;\r
+\r
+  free(fp);\r
+  return ret;\r
+}\r
+\r
 \r
 void Byteswap(unsigned char *data,int len)\r
 {\r
@@ -86,20 +244,46 @@ static unsigned char *PicoCartAlloc(int filesize)
   return rom;\r
 }\r
 \r
-int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)\r
+int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)\r
 {\r
-  unsigned char *rom=NULL; int size;\r
+  unsigned char *rom=NULL; int size, bytes_read;\r
   if (f==NULL) return 1;\r
 \r
-  fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);\r
+  size=f->size;\r
   if (size <= 0) return 1;\r
   size=(size+3)&~3; // Round up to a multiple of 4\r
 \r
   // Allocate space for the rom plus padding\r
   rom=PicoCartAlloc(size);\r
-  if (rom==NULL) return 1; // { fclose(f); return 1; }\r
+  if (rom==NULL) {\r
+    printf("out of memory (wanted %i)\n", size);\r
+    return 1;\r
+  }\r
 \r
-  fread(rom,1,size,f); // Load up the rom\r
+  if (PicoCartLoadProgressCB != NULL)\r
+  {\r
+    // read ROM in blocks, just for fun\r
+    int ret;\r
+    unsigned char *p = rom;\r
+    bytes_read=0;\r
+    do\r
+    {\r
+      int todo = size - bytes_read;\r
+      if (todo > 256*1024) todo = 256*1024;\r
+      ret = pm_read(p,todo,f);\r
+      bytes_read += ret;\r
+      p += ret;\r
+      PicoCartLoadProgressCB(bytes_read * 100 / size);\r
+    }\r
+    while (ret > 0);\r
+  }\r
+  else\r
+    bytes_read = pm_read(rom,size,f); // Load up the rom\r
+  if (bytes_read <= 0) {\r
+    printf("read failed\n");\r
+    free(rom);\r
+    return 1;\r
+  }\r
 \r
   // maybe we are loading MegaCD BIOS?\r
   if (!(PicoMCD&1) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
@@ -126,10 +310,18 @@ int PicoCartInsert(unsigned char *rom,unsigned int romsize)
   if(rom != NULL)\r
     *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
 \r
-  SRam.resize=1;\r
   Pico.rom=rom;\r
   Pico.romsize=romsize;\r
 \r
+  // setup correct memory map for loaded ROM\r
+  if (PicoMCD & 1)\r
+       PicoMemSetupCD();\r
+  else PicoMemSetup();\r
+  PicoMemReset();\r
+\r
+  if (!(PicoMCD & 1))\r
+    PicoCartDetect();\r
+\r
   return PicoReset(1);\r
 }\r
 \r
@@ -139,69 +331,124 @@ int PicoUnloadCart(unsigned char* romdata)
   return 0;\r
 }\r
 \r
-\r
-#ifdef _UNZIP_SUPPORT\r
-\r
-// notaz\r
-#include "../unzip/unzip.h"\r
-\r
-// nearly same as PicoCartLoad, but works with zipfiles\r
-int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize)\r
+static int rom_strcmp(int rom_offset, const char *s1)\r
 {\r
-       unsigned char *rom=0;\r
-       struct zipent* zipentry;\r
-       int size;\r
-       ZIP *zipfile = openzip(fname);\r
-\r
-       if(!zipfile) return 1;\r
-\r
-       // find first bin or smd\r
-       while((zipentry = readzip(zipfile)) != 0)\r
-       {\r
-               char *ext;\r
-               if(strlen(zipentry->name) < 5) continue;\r
-               ext = zipentry->name+strlen(zipentry->name)-4;\r
-\r
-               if(!strcasecmp(ext, ".bin") || !strcasecmp(ext, ".smd") || !strcasecmp(ext, ".gen")) break;\r
-       }\r
-\r
-       if(!zipentry) {\r
-               closezip(zipfile);\r
-               return 4; // no roms\r
-       }\r
-\r
-       size = zipentry->uncompressed_size;\r
-\r
-       size=(size+3)&~3; // Round up to a multiple of 4\r
+  int i, len = strlen(s1);\r
+  const char *s_rom = (const char *)Pico.rom + rom_offset;\r
+  for (i = 0; i < len; i++)\r
+    if (s1[i] != s_rom[i^1])\r
+      return 1;\r
+  return 0;\r
+}\r
 \r
-       // Allocate space for the rom plus padding\r
-       rom=PicoCartAlloc(size);\r
-       if (rom==NULL) { closezip(zipfile); return 2; }\r
+static int name_cmp(const char *name)\r
+{\r
+  return rom_strcmp(0x150, name);\r
+}\r
 \r
-       if(readuncompresszip(zipfile, zipentry, (char *)rom) != 0) {\r
-               free(rom);\r
-               rom = 0;\r
-               closezip(zipfile);\r
-               return 5; // unzip failed\r
-       }\r
+/* various cart-specific things, which can't be handled by generic code */\r
+void PicoCartDetect(void)\r
+{\r
+  int sram_size = 0, csum;\r
+  if(SRam.data) free(SRam.data); SRam.data=0;\r
+  Pico.m.sram_reg = 0;\r
 \r
-       closezip(zipfile);\r
+  csum = PicoRead32(0x18c) & 0xffff;\r
 \r
-        // maybe we are loading MegaCD BIOS?\r
-        if (!(PicoMCD&1) && size == 0x20000 &&\r
-                       (!strncmp((char *)rom+0x124, "BOOT", 4) || !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
-               PicoMCD |= 1;\r
-               rom = cd_realloc(rom, size);\r
-        }\r
+  if (Pico.rom[0x1B1] == 'R' && Pico.rom[0x1B0] == 'A')\r
+  {\r
+    if (Pico.rom[0x1B2] & 0x40)\r
+    {\r
+      // EEPROM\r
+      SRam.start = PicoRead32(0x1B4) & ~1; // zero address is used for clock by some games\r
+      SRam.end   = PicoRead32(0x1B8);\r
+      sram_size  = 0x2000;\r
+      Pico.m.sram_reg |= 4;\r
+    } else {\r
+      // normal SRAM\r
+      SRam.start = PicoRead32(0x1B4) & 0xFFFF00;\r
+      SRam.end   = PicoRead32(0x1B8) | 1;\r
+      sram_size  = SRam.end - SRam.start + 1;\r
+    }\r
+    Pico.m.sram_reg |= 0x10; // SRAM was detected\r
+  }\r
+  if (sram_size <= 0)\r
+  {\r
+    // some games may have bad headers, like S&K and Sonic3\r
+    // note: majority games use 0x200000 as starting address, but there are some which\r
+    // use something else (0x300000 by HardBall '95). Luckily they have good headers.\r
+    SRam.start = 0x200000;\r
+    SRam.end   = 0x203FFF;\r
+    sram_size  = 0x004000;\r
+  }\r
 \r
-       // Check for SMD:\r
-       if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
-       else Byteswap(rom,size); // Just byteswap\r
+  if (sram_size)\r
+  {\r
+    SRam.data = (unsigned char *) calloc(sram_size, 1);\r
+    if(!SRam.data) return;\r
+  }\r
+  SRam.changed = 0;\r
+\r
+  // set EEPROM defaults, in case it gets detected\r
+  SRam.eeprom_type   = 0; // 7bit (24C01)\r
+  SRam.eeprom_abits  = 3; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ in\r
+  SRam.eeprom_bit_cl = 1;\r
+  SRam.eeprom_bit_in = 0;\r
+  SRam.eeprom_bit_out= 0;\r
+\r
+  // some known EEPROM data (thanks to EkeEke)\r
+  if (name_cmp("COLLEGE SLAM") == 0 ||\r
+      name_cmp("FRANK THOMAS BIGHURT BASEBAL") == 0)\r
+  {\r
+    SRam.eeprom_type = 3;\r
+    SRam.eeprom_abits = 2;\r
+    SRam.eeprom_bit_cl = 0;\r
+  }\r
+  else if (name_cmp("NBA JAM TOURNAMENT EDITION") == 0 ||\r
+           name_cmp("NFL QUARTERBACK CLUB") == 0)\r
+  {\r
+    SRam.eeprom_type = 2;\r
+    SRam.eeprom_abits = 2;\r
+    SRam.eeprom_bit_cl = 0;\r
+  }\r
+  else if (name_cmp("NBA JAM") == 0)\r
+  {\r
+    SRam.eeprom_type = 2;\r
+    SRam.eeprom_bit_out = 1;\r
+    SRam.eeprom_abits = 0;\r
+  }\r
+  else if (name_cmp("NHLPA HOCKEY '93") == 0 ||\r
+           name_cmp("NHLPA Hockey '93") == 0 ||\r
+           name_cmp("RINGS OF POWER") == 0)\r
+  {\r
+    SRam.start = SRam.end = 0x200000;\r
+    Pico.m.sram_reg = 0x14;\r
+    SRam.eeprom_abits = 0;\r
+    SRam.eeprom_bit_cl = 6;\r
+    SRam.eeprom_bit_in = 7;\r
+    SRam.eeprom_bit_out= 7;\r
+  }\r
+  else if ( name_cmp("MICRO MACHINES II") == 0 ||\r
+           (name_cmp("        ") == 0 && // Micro Machines {Turbo Tournament '96, Military - It's a Blast!}\r
+           (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))\r
+  {\r
+    SRam.start = 0x300000;\r
+    SRam.end   = 0x380001;\r
+    Pico.m.sram_reg = 0x14;\r
+    SRam.eeprom_type = 2;\r
+    SRam.eeprom_abits = 0;\r
+    SRam.eeprom_bit_cl = 1;\r
+    SRam.eeprom_bit_in = 0;\r
+    SRam.eeprom_bit_out= 7;\r
+  }\r
 \r
-       if (prom)  *prom=rom;\r
-       if (psize) *psize=size;\r
+  // Some games malfunction if SRAM is not filled with 0xff\r
+  if (name_cmp("DINO DINI'S SOCCER") == 0 ||\r
+      name_cmp("MICRO MACHINES II") == 0)\r
+    memset(SRam.data, 0xff, sram_size);\r
 \r
-       return 0;\r
+  // Unusual region 'code'\r
+  if (rom_strcmp(0x1f0, "EUROPE") == 0)\r
+    *(int *) (Pico.rom+0x1f0) = 0x20204520;\r
 }\r
 \r
-#endif\r