reverted useless threaded buffering code
[picodrive.git] / Pico / Cart.c
index a9fe6ec..25bb961 100644 (file)
 \r
 static char *rom_exts[] = { "bin", "gen", "smd", "iso" };\r
 \r
+void (*PicoCartUnloadHook)(void) = NULL;\r
+\r
 void (*PicoCartLoadProgressCB)(int percent) = NULL;\r
+void (*PicoCDLoadProgressCB)(int percent) = NULL; // handled in Pico/cd/cd_file.c\r
+\r
+/* cso struct */\r
+typedef struct _cso_struct\r
+{\r
+  unsigned char in_buff[2*2048];\r
+  unsigned char out_buff[2048];\r
+  struct {\r
+    char          magic[4];\r
+    unsigned int  unused;\r
+    unsigned int  total_bytes;\r
+    unsigned int  total_bytes_high; // ignored here\r
+    unsigned int  block_size;  // 10h\r
+    unsigned char ver;\r
+    unsigned char align;\r
+    unsigned char reserved[2];\r
+  } header;\r
+  unsigned int  fpos_in;  // input file read pointer\r
+  unsigned int  fpos_out; // pos in virtual decompressed file\r
+  int block_in_buff;      // block which we have read in in_buff\r
+  int pad;\r
+  int index[0];\r
+}\r
+cso_struct;\r
+\r
+static int uncompress2(void *dest, int destLen, void *source, int sourceLen)\r
+{\r
+    z_stream stream;\r
+    int err;\r
+\r
+    stream.next_in = (Bytef*)source;\r
+    stream.avail_in = (uInt)sourceLen;\r
+    stream.next_out = dest;\r
+    stream.avail_out = (uInt)destLen;\r
 \r
+    stream.zalloc = NULL;\r
+    stream.zfree = NULL;\r
+\r
+    err = inflateInit2(&stream, -15);\r
+    if (err != Z_OK) return err;\r
+\r
+    err = inflate(&stream, Z_FINISH);\r
+    if (err != Z_STREAM_END) {\r
+        inflateEnd(&stream);\r
+        return err;\r
+    }\r
+    //*destLen = stream.total_out;\r
+\r
+    return inflateEnd(&stream);\r
+}\r
 \r
 pm_file *pm_open(const char *path)\r
 {\r
@@ -76,6 +127,61 @@ zip_failed:
       return NULL;\r
     }\r
   }\r
+  else if (ext && strcasecmp(ext, "cso") == 0)\r
+  {\r
+    cso_struct *cso = NULL, *tmp = NULL;\r
+    int size;\r
+    f = fopen(path, "rb");\r
+    if (f == NULL)\r
+      goto cso_failed;\r
+\r
+    cso = malloc(sizeof(*cso));\r
+    if (cso == NULL)\r
+      goto cso_failed;\r
+\r
+    if (fread(&cso->header, 1, sizeof(cso->header), f) != sizeof(cso->header))\r
+      goto cso_failed;\r
+\r
+    if (strncmp(cso->header.magic, "CISO", 4) != 0) {\r
+      elprintf(EL_STATUS, "cso: bad header");\r
+      goto cso_failed;\r
+    }\r
+\r
+    if (cso->header.block_size != 2048) {\r
+      elprintf(EL_STATUS, "cso: bad block size (%u)", cso->header.block_size);\r
+      goto cso_failed;\r
+    }\r
+\r
+    size = ((cso->header.total_bytes >> 11) + 1)*4 + sizeof(*cso);\r
+    tmp = realloc(cso, size);\r
+    if (tmp == NULL)\r
+      goto cso_failed;\r
+    cso = tmp;\r
+    elprintf(EL_STATUS, "allocated %i bytes for CSO struct", size);\r
+\r
+    size -= sizeof(*cso); // index size\r
+    if (fread(cso->index, 1, size, f) != size) {\r
+      elprintf(EL_STATUS, "cso: premature EOF");\r
+      goto cso_failed;\r
+    }\r
+\r
+    // all ok\r
+    cso->fpos_in = ftell(f);\r
+    cso->fpos_out = 0;\r
+    cso->block_in_buff = -1;\r
+    file = malloc(sizeof(*file));\r
+    if (file == NULL) goto cso_failed;\r
+    file->file  = f;\r
+    file->param = cso;\r
+    file->size  = cso->header.total_bytes;\r
+    file->type  = PMT_CSO;\r
+    return file;\r
+\r
+cso_failed:\r
+    if (cso != NULL) free(cso);\r
+    if (f != NULL) fclose(f);\r
+    return NULL;\r
+  }\r
 \r
   /* not a zip, treat as uncompressed file */\r
   f = fopen(path, "rb");\r
@@ -116,6 +222,68 @@ 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
+  else if (stream->type == PMT_CSO)\r
+  {\r
+    cso_struct *cso = stream->param;\r
+    int read_pos, read_len, out_offs, rret;\r
+    int block = cso->fpos_out >> 11;\r
+    int index = cso->index[block];\r
+    int index_end = cso->index[block+1];\r
+    unsigned char *out = ptr, *tmp_dst;\r
+\r
+    ret = 0;\r
+    while (bytes != 0)\r
+    {\r
+      out_offs = cso->fpos_out&0x7ff;\r
+      if (out_offs == 0 && bytes >= 2048)\r
+           tmp_dst = out;\r
+      else tmp_dst = cso->out_buff;\r
+\r
+      read_pos = (index&0x7fffffff) << cso->header.align;\r
+\r
+      if (index < 0) {\r
+        if (read_pos != cso->fpos_in)\r
+          fseek(stream->file, read_pos, SEEK_SET);\r
+        rret = fread(tmp_dst, 1, 2048, stream->file);\r
+        cso->fpos_in = read_pos + rret;\r
+        if (rret != 2048) break;\r
+      } else {\r
+        read_len = (((index_end&0x7fffffff) << cso->header.align) - read_pos) & 0xfff;\r
+        if (block != cso->block_in_buff)\r
+        {\r
+          if (read_pos != cso->fpos_in)\r
+            fseek(stream->file, read_pos, SEEK_SET);\r
+          rret = fread(cso->in_buff, 1, read_len, stream->file);\r
+          cso->fpos_in = read_pos + rret;\r
+          if (rret != read_len) {\r
+            elprintf(EL_STATUS, "cso: read failed @ %08x", read_pos);\r
+            break;\r
+          }\r
+          cso->block_in_buff = block;\r
+        }\r
+        rret = uncompress2(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
+        }\r
+      }\r
+\r
+      rret = 2048;\r
+      if (out_offs != 0 || bytes < 2048) {\r
+        //elprintf(EL_STATUS, "cso: unaligned/nonfull @ %08x, offs=%i, len=%u", cso->fpos_out, out_offs, bytes);\r
+        if (bytes < rret) rret = bytes;\r
+        if (2048 - out_offs < rret) rret = 2048 - out_offs;\r
+        memcpy(out, tmp_dst + out_offs, rret);\r
+      }\r
+      ret += rret;\r
+      out += rret;\r
+      cso->fpos_out += rret;\r
+      bytes -= rret;\r
+      block++;\r
+      index = index_end;\r
+      index_end = cso->index[block+1];\r
+    }\r
+  }\r
   else\r
     ret = 0;\r
 \r
@@ -138,6 +306,17 @@ int pm_seek(pm_file *stream, long offset, int whence)
     }\r
     return gzseek((gzFile) stream->param, offset, whence);\r
   }\r
+  else if (stream->type == PMT_CSO)\r
+  {\r
+    cso_struct *cso = stream->param;\r
+    switch (whence)\r
+    {\r
+      case SEEK_CUR: cso->fpos_out += offset; break;\r
+      case SEEK_SET: cso->fpos_out  = offset; break;\r
+      case SEEK_END: cso->fpos_out  = cso->header.total_bytes - offset; break;\r
+    }\r
+    return cso->fpos_out;\r
+  }\r
   else\r
     return -1;\r
 }\r
@@ -159,6 +338,11 @@ int pm_close(pm_file *fp)
     zipfile->fp = NULL; // gzclose() closed it\r
     closezip(zipfile);\r
   }\r
+  else if (fp->type == PMT_CSO)\r
+  {\r
+    free(fp->param);\r
+    fclose(fp->file);\r
+  }\r
   else\r
     ret = EOF;\r
 \r
@@ -218,7 +402,6 @@ static int DecodeSmd(unsigned char *data,int len)
 static unsigned char *cd_realloc(void *old, int filesize)\r
 {\r
   unsigned char *rom;\r
-  dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));\r
   rom=realloc(old, sizeof(mcd_state));\r
   if (rom) memset(rom+0x20000, 0, sizeof(mcd_state)-0x20000);\r
   return rom;\r
@@ -236,7 +419,6 @@ static unsigned char *PicoCartAlloc(int filesize)
   alloc_size&=~0x7ffff; // use alloc size of multiples of 512K, so that memhandlers could be set up more efficiently\r
   if((filesize&0x3fff)==0x200) alloc_size+=0x200;\r
   else if(alloc_size-filesize < 4) alloc_size+=4; // padding for out-of-bound exec protection\r
-  //dprintf("alloc_size: %x\n",  alloc_size);\r
 \r
   // Allocate space for the rom plus padding\r
   rom=(unsigned char *)malloc(alloc_size);\r
@@ -256,7 +438,7 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
   // Allocate space for the rom plus padding\r
   rom=PicoCartAlloc(size);\r
   if (rom==NULL) {\r
-    printf("out of memory (wanted %i)\n", size);\r
+    elprintf(EL_STATUS, "out of memory (wanted %i)", size);\r
     return 1;\r
   }\r
 \r
@@ -280,7 +462,7 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
   else\r
     bytes_read = pm_read(rom,size,f); // Load up the rom\r
   if (bytes_read <= 0) {\r
-    printf("read failed\n");\r
+    elprintf(EL_STATUS, "read failed");\r
     free(rom);\r
     return 1;\r
   }\r
@@ -301,7 +483,7 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize)
   return 0;\r
 }\r
 \r
-// Insert/remove a cartridge:\r
+// Insert a cartridge:\r
 int PicoCartInsert(unsigned char *rom,unsigned int romsize)\r
 {\r
   // notaz: add a 68k "jump one op back" opcode to the end of ROM.\r
@@ -313,21 +495,39 @@ int PicoCartInsert(unsigned char *rom,unsigned int romsize)
   Pico.rom=rom;\r
   Pico.romsize=romsize;\r
 \r
+  if (PicoCartUnloadHook != NULL) {\r
+    PicoCartUnloadHook();\r
+    PicoCartUnloadHook = NULL;\r
+  }\r
+\r
+  PicoMemResetHooks();\r
+  PicoDmaHook = NULL;\r
+  PicoResetHook = NULL;\r
+  PicoLineHook = NULL;\r
+  PicoLoadStateHook = NULL;\r
+  carthw_chunks = NULL;\r
+\r
+  PicoMemReset();\r
+\r
+  if (!(PicoMCD & 1))\r
+    PicoCartDetect();\r
+\r
   // setup correct memory map for loaded ROM\r
+  // call PicoMemReset again due to possible memmap change\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
-int PicoUnloadCart(unsigned char* romdata)\r
+int PicoCartUnload(void)\r
 {\r
-  free(romdata);\r
+  if (Pico.rom != NULL) {\r
+    free(Pico.rom);\r
+    Pico.rom=NULL;\r
+  }\r
   return 0;\r
 }\r
 \r
@@ -346,7 +546,10 @@ static int name_cmp(const char *name)
   return rom_strcmp(0x150, name);\r
 }\r
 \r
-/* various cart-specific things, which can't be handled by generic code */\r
+/*\r
+ * various cart-specific things, which can't be handled by generic code\r
+ * (maybe I should start using CRC for this stuff?)\r
+ */\r
 void PicoCartDetect(void)\r
 {\r
   int sram_size = 0, csum;\r
@@ -366,10 +569,12 @@ void PicoCartDetect(void)
       Pico.m.sram_reg |= 4;\r
     } else {\r
       // normal SRAM\r
-      SRam.start = PicoRead32(0x1B4) & 0xFFFF00;\r
+      SRam.start = PicoRead32(0x1B4) & ~0xff;\r
       SRam.end   = PicoRead32(0x1B8) | 1;\r
       sram_size  = SRam.end - SRam.start + 1;\r
     }\r
+    SRam.start &= ~0xff000000;\r
+    SRam.end   &= ~0xff000000;\r
     Pico.m.sram_reg |= 0x10; // SRAM was detected\r
   }\r
   if (sram_size <= 0)\r
@@ -430,7 +635,7 @@ void PicoCartDetect(void)
   }\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
+           (csum == 0x165e || csum == 0x168b || csum == 0xCEE0 || csum == 0x2C41)))\r
   {\r
     SRam.start = 0x300000;\r
     SRam.end   = 0x380001;\r
@@ -442,10 +647,35 @@ void PicoCartDetect(void)
     SRam.eeprom_bit_out= 7;\r
   }\r
 \r
+  // SVP detection\r
+  else if (name_cmp("Virtua Racing") == 0 ||\r
+           name_cmp("VIRTUA RACING") == 0)\r
+  {\r
+    PicoSVPStartup();\r
+  }\r
+\r
+  // Detect 12-in-1 mapper\r
+  else if ((name_cmp("ROBOCOP 3") == 0 && Pico.romsize == 0x200000) ||\r
+    (rom_strcmp(0x160, "FLICKY") == 0 && Pico.romsize >= 0x200000))\r
+  {\r
+    carthw_12in1_startup();\r
+  }\r
+\r
+  // Realtec mapper\r
+  else if (Pico.romsize == 512*1024 && (\r
+    rom_strcmp(0x94, "THE EARTH DEFEND") == 0 ||\r
+    rom_strcmp(0xfe, "WISEGAME 11-03-1993") == 0 || // Funny World\r
+    rom_strcmp(0x95, "MALLET LEGEND ") == 0)) // Whac-A-Critter\r
+  {\r
+    carthw_realtec_startup();\r
+  }\r
+\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
+  {\r
     memset(SRam.data, 0xff, sram_size);\r
+  }\r
 \r
   // Unusual region 'code'\r
   if (rom_strcmp(0x1f0, "EUROPE") == 0)\r