idle loop detection (Cyclone only, with debug stuff)
[picodrive.git] / Pico / Cart.c
index cf33580..e65b509 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
+static void PicoCartDetect(void);\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
@@ -48,7 +101,7 @@ pm_file *pm_open(const char *path)
 \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
+          if (strcasecmp(ext, rom_exts[i]) == 0) goto found_rom_zip;\r
       }\r
 \r
       /* zipfile given, but nothing found suitable for us inside */\r
@@ -76,6 +129,64 @@ 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
+    /* we use our own buffering */\r
+    setvbuf(f, NULL, _IONBF, 0);\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
@@ -95,6 +206,7 @@ zip_failed:
   file->size  = ftell(f);\r
   file->type  = PMT_UNCOMPRESSED;\r
   fseek(f, 0, SEEK_SET);\r
+\r
   return file;\r
 }\r
 \r
@@ -116,6 +228,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 +312,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 +344,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 +408,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
@@ -229,14 +418,13 @@ static unsigned char *PicoCartAlloc(int filesize)
   int alloc_size;\r
   unsigned char *rom;\r
 \r
-  if (PicoMCD & 1) return cd_realloc(NULL, filesize);\r
+  if (PicoAHW & PAHW_MCD) return cd_realloc(NULL, filesize);\r
 \r
   alloc_size=filesize+0x7ffff;\r
   if((filesize&0x3fff)==0x200) alloc_size-=0x200;\r
   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 +444,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,19 +468,23 @@ 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
 \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
-    PicoMCD |= 1;\r
+  if (!(PicoAHW & PAHW_MCD) && size == 0x20000 && (!strncmp((char *)rom+0x124, "BOOT", 4) ||\r
+       !strncmp((char *)rom+0x128, "BOOT", 4))) {\r
+    PicoAHW |= PAHW_MCD;\r
     rom = cd_realloc(rom, size);\r
   }\r
 \r
   // Check for SMD:\r
-  if ((size&0x3fff)==0x200) { DecodeSmd(rom,size); size-=0x200; } // Decode and byteswap SMD\r
+  if (size >= 0x4200 && (size&0x3fff)==0x200 &&\r
+      ((rom[0x2280] == 'S' && rom[0x280] == 'E') || (rom[0x280] == 'S' && rom[0x2281] == 'E'))) {\r
+    DecodeSmd(rom,size); size-=0x200; // Decode and byteswap SMD\r
+  }\r
   else Byteswap(rom,size); // Just byteswap\r
 \r
   if (prom)  *prom=rom;\r
@@ -301,51 +493,90 @@ 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
   // This will hang the emu, but will prevent nasty crashes.\r
   // note: 4 bytes are padded to every ROM\r
-  if(rom != NULL)\r
+  if (rom != NULL)\r
     *(unsigned long *)(rom+romsize) = 0xFFFE4EFA; // 4EFA FFFE byteswapped\r
 \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
+  if (SRam.data) {\r
+    free(SRam.data);\r
+    SRam.data = NULL;\r
+  }\r
+\r
+  if (PicoCartUnloadHook != NULL) {\r
+    PicoCartUnloadHook();\r
+    PicoCartUnloadHook = NULL;\r
+  }\r
+\r
+  PicoAHW &= PAHW_MCD;\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
+  if (!(PicoAHW & PAHW_MCD))\r
     PicoCartDetect();\r
 \r
-  return PicoReset(1);\r
+  // setup correct memory map for loaded ROM\r
+  // call PicoMemReset again due to possible memmap change\r
+  switch (PicoAHW) {\r
+    default:\r
+      elprintf(EL_STATUS|EL_ANOMALY, "starting in unknown hw configuration: %x", PicoAHW);\r
+    case 0:\r
+    case PAHW_SVP:  PicoMemSetup(); break;\r
+    case PAHW_MCD:  PicoMemSetupCD(); break;\r
+    case PAHW_PICO: PicoMemSetupPico(); break;\r
+  }\r
+  PicoMemReset();\r
+\r
+  PicoPower();\r
+  return 0;\r
 }\r
 \r
-int PicoUnloadCart(unsigned char* romdata)\r
+int PicoCartUnload(void)\r
 {\r
-  free(romdata);\r
+  if (Pico.rom != NULL) {\r
+    SekFinishIdleDet();\r
+    free(Pico.rom);\r
+    Pico.rom=NULL;\r
+  }\r
   return 0;\r
 }\r
 \r
-static int name_cmp(const char *name)\r
+static int rom_strcmp(int rom_offset, const char *s1)\r
 {\r
-  int i, len = strlen(name);\r
-  const char *name_rom = (const char *)Pico.rom+0x150;\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 (name[i] != name_rom[i^1])\r
+    if (s1[i] != s_rom[i^1])\r
       return 1;\r
   return 0;\r
 }\r
 \r
-/* various cart-specific things, which can't be handled by generic code */\r
-void PicoCartDetect(void)\r
+static int name_cmp(const char *name)\r
+{\r
+  return rom_strcmp(0x150, name);\r
+}\r
+\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
+static 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
   csum = PicoRead32(0x18c) & 0xffff;\r
@@ -361,10 +592,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
@@ -377,10 +610,16 @@ void PicoCartDetect(void)
     sram_size  = 0x004000;\r
   }\r
 \r
+  // this game actually doesn't have SRAM, but some weird protection\r
+  if (rom_strcmp(0x120, "PUGGSY") == 0)\r
+  {\r
+    SRam.start = SRam.end = sram_size = 0;\r
+  }\r
+\r
   if (sram_size)\r
   {\r
     SRam.data = (unsigned char *) calloc(sram_size, 1);\r
-    if(!SRam.data) return;\r
+    if (SRam.data == NULL) return;\r
   }\r
   SRam.changed = 0;\r
 \r
@@ -425,7 +664,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
@@ -437,9 +676,53 @@ 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
+  // Pico\r
+  else if (rom_strcmp(0x100, "SEGA PICO") == 0 ||\r
+           rom_strcmp(0x100, "IMA IKUNOUJYUKU") == 0) // what is that supposed to mean?\r
+  {\r
+    PicoInitPico();\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
+    (name_cmp(" SHOVE IT!") == 0 && Pico.romsize >= 0x200000) ||\r
+    (name_cmp("MS PACMAN") == 0 && Pico.romsize >= 0x200000)) // bad dump?\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
+  // Radica mapper\r
+  else if (name_cmp("KID CHAMELEON") == 0 && Pico.romsize > 0x100000)\r
+  {\r
+    carthw_radica_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 || rom_strcmp(0x1f0, "Europe") == 0)\r
+    *(int *) (Pico.rom+0x1f0) = 0x20204520;\r
 }\r
 \r