.cue support, Pico stubs
[libpicofe.git] / common / emu.c
index 1bbdf35..e88859b 100644 (file)
@@ -18,6 +18,7 @@
 \r
 #include <Pico/PicoInt.h>\r
 #include <Pico/Patch.h>\r
+#include <Pico/cd/cue.h>\r
 #include <zlib/zlib.h>\r
 \r
 #if   defined(__GP2X__)\r
@@ -163,12 +164,24 @@ int emu_cdCheck(int *pregion)
 {\r
        unsigned char buf[32];\r
        pm_file *cd_f;\r
-       int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe\r
-       char ext[5];\r
+       int region = 4; // 1: Japan, 4: US, 8: Europe\r
+       char ext[5], *fname = romFileName;\r
+       cue_track_type type = CT_UNKNOWN;\r
+       cue_data_t *cue_data = NULL;\r
 \r
        get_ext(romFileName, ext);\r
+       if (strcasecmp(ext, ".cue") == 0) {\r
+               cue_data = cue_parse(romFileName);\r
+               if (cue_data != NULL) {\r
+                       fname = cue_data->tracks[1].fname;\r
+                       type  = cue_data->tracks[1].type;\r
+               }\r
+       }\r
+\r
+       cd_f = pm_open(fname);\r
+       if (cue_data != NULL)\r
+               cue_destroy(cue_data);\r
 \r
-       cd_f = pm_open(romFileName);\r
        if (!cd_f) return 0; // let the upper level handle this\r
 \r
        if (pm_read(buf, 32, cd_f) != 32) {\r
@@ -176,18 +189,27 @@ int emu_cdCheck(int *pregion)
                return 0;\r
        }\r
 \r
-       if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1;       // Sega CD (ISO)\r
-       if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2;       // Sega CD (BIN)\r
-       if (type == 0) {\r
+       if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {\r
+               if (type && type != CT_ISO)\r
+                       elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r
+               type = CT_ISO;       // Sega CD (ISO)\r
+       }\r
+       if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {\r
+               if (type && type != CT_BIN)\r
+                       elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r
+               type = CT_BIN;       // Sega CD (BIN)\r
+       }\r
+\r
+       if (type == CT_UNKNOWN) {\r
                pm_close(cd_f);\r
                return 0;\r
        }\r
 \r
-       pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);\r
+       pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);\r
        pm_read(id_header, sizeof(id_header), cd_f);\r
 \r
        /* it seems we have a CD image here. Try to detect region now.. */\r
-       pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
+       pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r
        pm_read(buf, 1, cd_f);\r
        pm_close(cd_f);\r
 \r
@@ -195,7 +217,7 @@ int emu_cdCheck(int *pregion)
        if (buf[0] == 0xa1) region = 1; // JAP\r
 \r
        lprintf("detected %s Sega/Mega CD image with %s region\n",\r
-               type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
+               type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
 \r
        if (pregion != NULL) *pregion = region;\r
 \r