ISO loading, menus, LEDs
[picodrive.git] / Pico / Cart.c
index 18461a2..d3e7743 100644 (file)
@@ -57,18 +57,21 @@ static int DecodeSmd(unsigned char *data,int len)
   return 0;\r
 }\r
 \r
+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
+}\r
+\r
 static unsigned char *PicoCartAlloc(int filesize)\r
 {\r
   int alloc_size;\r
   unsigned char *rom;\r
 \r
-  if (PicoMCD & 1) {\r
-    dprintf("sizeof(mcd_state): %i", sizeof(mcd_state));\r
-    if (filesize > 0x20000) return NULL; // invalid BIOS\r
-    rom=(unsigned char *)malloc(sizeof(mcd_state));\r
-    if (rom) memset(rom, 0, sizeof(mcd_state));\r
-    return rom;\r
-  }\r
+  if (PicoMCD & 1) return cd_realloc(NULL, filesize);\r
 \r
   alloc_size=filesize+0x7ffff;\r
   if((filesize&0x3fff)==0x200) alloc_size-=0x200;\r
@@ -90,19 +93,19 @@ int PicoCartLoad(FILE *f,unsigned char **prom,unsigned int *psize)
 \r
   fseek(f,0,SEEK_END); size=ftell(f); fseek(f,0,SEEK_SET);\r
   if (size <= 0) return 1;\r
-  if (PicoMCD & 1) {\r
-    if (size > 0x20000) return 1; // invalid BIOS\r
-    size = 0xe0000;\r
-  } else {\r
-    size=(size+3)&~3; // Round up to a multiple of 4\r
-  }\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
 \r
   fread(rom,1,size,f); // Load up the rom\r
-  // fclose(f); // this is confusing. From now on, caller should close it, because it opened this.\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
+    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
@@ -184,6 +187,13 @@ int CartLoadZip(const char *fname, unsigned char **prom, unsigned int *psize)
 \r
        closezip(zipfile);\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
+\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