start new makefile, migrate to libpicofe
[picodrive.git] / pico / cart.c
index 26fb59b..b060079 100644 (file)
@@ -9,10 +9,12 @@
 \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
 \r
 \r
+static int rom_alloc_size;\r
 static const char *rom_exts[] = { "bin", "gen", "smd", "iso", "sms", "gg", "sg" };\r
 \r
 void (*PicoCartUnloadHook)(void);\r
@@ -21,7 +23,7 @@ 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
-static void PicoCartDetect(void);\r
+static void PicoCartDetect(const char *carthw_cfg);\r
 \r
 /* cso struct */\r
 typedef struct _cso_struct\r
@@ -425,22 +427,10 @@ 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
-  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, int is_sms)\r
 {\r
-  int alloc_size;\r
   unsigned char *rom;\r
 \r
-  if (PicoAHW & PAHW_MCD)\r
-    return cd_realloc(NULL, filesize);\r
-\r
   if (is_sms) {\r
     // make size power of 2 for easier banking handling\r
     int s = 0, tmp = filesize;\r
@@ -448,18 +438,27 @@ static unsigned char *PicoCartAlloc(int filesize, int is_sms)
       s++;\r
     if (filesize > (1 << s))\r
       s++;\r
-    alloc_size = 1 << 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
+    // in case we want to switch to CD mode\r
+    if (filesize < sizeof(mcd_state))\r
+      filesize = sizeof(mcd_state);\r
+\r
     // align to 512K for memhandlers\r
-    alloc_size = (filesize + 0x7ffff) & ~0x7ffff;\r
+    rom_alloc_size = (filesize + 0x7ffff) & ~0x7ffff;\r
   }\r
 \r
-  if (alloc_size - filesize < 4)\r
-    alloc_size += 4; // padding for out-of-bound exec protection\r
+  if (rom_alloc_size - filesize < 4)\r
+    rom_alloc_size += 4; // padding for out-of-bound exec protection\r
 \r
   // Allocate space for the rom plus padding\r
-  rom = calloc(alloc_size, 1);\r
+  // use special address for 32x dynarec\r
+  rom = plat_mmap(0x02000000, rom_alloc_size, 0, 0);\r
   return rom;\r
 }\r
 \r
@@ -513,7 +512,6 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
     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
@@ -541,7 +539,7 @@ int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms)
 }\r
 \r
 // Insert a cartridge:\r
-int PicoCartInsert(unsigned char *rom,unsigned int romsize)\r
+int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg)\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
@@ -561,6 +559,7 @@ int PicoCartInsert(unsigned char *rom,unsigned int romsize)
     PicoCartUnloadHook();\r
     PicoCartUnloadHook = NULL;\r
   }\r
+  pdb_cleanup();\r
 \r
   PicoAHW &= PAHW_MCD|PAHW_SMS;\r
 \r
@@ -572,7 +571,7 @@ int PicoCartInsert(unsigned char *rom,unsigned int romsize)
   carthw_chunks = NULL;\r
 \r
   if (!(PicoAHW & (PAHW_MCD|PAHW_SMS)))\r
-    PicoCartDetect();\r
+    PicoCartDetect(carthw_cfg);\r
 \r
   // setup correct memory map for loaded ROM\r
   switch (PicoAHW) {\r
@@ -596,6 +595,17 @@ int PicoCartInsert(unsigned char *rom,unsigned int romsize)
   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
 void PicoCartUnload(void)\r
 {\r
   if (PicoCartUnloadHook != NULL) {\r
@@ -608,8 +618,8 @@ void PicoCartUnload(void)
 \r
   if (Pico.rom != NULL) {\r
     SekFinishIdleDet();\r
-    free(Pico.rom);\r
-    Pico.rom=NULL;\r
+    plat_munmap(Pico.rom, rom_alloc_size);\r
+    Pico.rom = NULL;\r
   }\r
 }\r
 \r
@@ -695,16 +705,16 @@ static int is_expr(const char *expr, char **pr)
   return 1;\r
 }\r
 \r
-static void parse_carthw(int *fill_sram)\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
   int tmp, rom_crc = 0;\r
   char buff[256], *p, *r;\r
   FILE *f;\r
 \r
-  f = fopen("carthw.cfg", "r");\r
+  f = fopen(carthw_cfg, "r");\r
   if (f == NULL) {\r
-    elprintf(EL_STATUS, "couldn't open carthw.txt!");\r
+    elprintf(EL_STATUS, "couldn't open carthw.cfg!");\r
     return;\r
   }\r
 \r
@@ -924,7 +934,7 @@ no_checks:
 /*\r
  * various cart-specific things, which can't be handled by generic code\r
  */\r
-static void PicoCartDetect(void)\r
+static void PicoCartDetect(const char *carthw_cfg)\r
 {\r
   int fill_sram = 0;\r
 \r
@@ -954,7 +964,8 @@ static void PicoCartDetect(void)
   SRam.eeprom_bit_in = 0;\r
   SRam.eeprom_bit_out= 0;\r
 \r
-  parse_carthw(&fill_sram);\r
+  if (carthw_cfg != NULL)\r
+    parse_carthw(carthw_cfg, &fill_sram);\r
 \r
   if (SRam.flags & SRF_ENABLED)\r
   {\r