reset behavior changed, Puggsy detection added
[picodrive.git] / platform / common / emu.c
index 936665b..14cc52a 100644 (file)
@@ -41,6 +41,7 @@ char noticeMsg[64];
 int state_slot = 0;\r
 int config_slot = 0, config_slot_current = 0;\r
 char lastRomFile[512];\r
+int kb_combo_keys = 0, kb_combo_acts = 0;      // keys and actions which need button combos\r
 \r
 unsigned char *movie_data = NULL;\r
 static int movie_size = 0;\r
@@ -88,8 +89,8 @@ static void get_ext(char *file, char *ext)
        strlwr_(ext);\r
 }\r
 \r
-char *biosfiles_us[] = { "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210" };\r
-char *biosfiles_eu[] = { "eu_mcd2_9306", "eu_mcd2_9303",   "eu_mcd1_9210" };\r
+char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };\r
+char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303"   };\r
 char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r
 \r
 int emu_findBios(int region, char **bios_file)\r
@@ -232,18 +233,20 @@ static int extract_text(char *dest, unsigned char *src, int len, int swab)
 char *emu_makeRomId(void)\r
 {\r
        static char id_string[3+0x11+0x11+0x30+16];\r
-       int pos;\r
+       int pos, swab = 1;\r
 \r
-       if (PicoMCD & 1)\r
-            strcpy(id_string, "CD|");\r
+       if (PicoMCD & 1) {\r
+               strcpy(id_string, "CD|");\r
+               swab = 0;\r
+       }\r
        else strcpy(id_string, "MD|");\r
        pos = 3;\r
 \r
-       pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, 1); // serial\r
+       pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial\r
        id_string[pos] = '|'; pos++;\r
-       pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, 1); // region\r
+       pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region\r
        id_string[pos] = '|'; pos++;\r
-       pos += extract_text(id_string + pos, id_header + 0x50, 0x30, 1); // overseas name\r
+       pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name\r
        id_string[pos] = 0;\r
 \r
        return id_string;\r
@@ -275,7 +278,8 @@ int emu_ReloadRom(void)
                free(movie_data);\r
                movie_data = 0;\r
        }\r
-       if (!strcmp(ext, ".gmv")) {\r
+       if (!strcmp(ext, ".gmv"))\r
+       {\r
                // check for both gmv and rom\r
                int dummy;\r
                FILE *movie_file = fopen(romFileName, "rb");\r
@@ -328,6 +332,7 @@ int emu_ReloadRom(void)
        cd_state = emu_cdCheck(&cd_region);\r
        if (cd_state > 0)\r
        {\r
+               PicoMCD |= 1;\r
                // valid CD image, check for BIOS..\r
 \r
                // we need to have config loaded at this point\r
@@ -341,10 +346,10 @@ int emu_ReloadRom(void)
                }\r
                if (!emu_findBios(cd_region, &used_rom_name)) {\r
                        // bios_help() ?\r
+                       PicoMCD &= ~1;\r
                        return 0;\r
                }\r
 \r
-               PicoMCD |= 1;\r
                get_ext(used_rom_name, ext);\r
        }\r
        else\r
@@ -429,7 +434,7 @@ int emu_ReloadRom(void)
                        } else {\r
                                PicoRegionOverride = 4;\r
                        }\r
-                       PicoReset(0);\r
+                       PicoReset();\r
                        // TODO: bits 6 & 5\r
                }\r
                movie_data[0x18+30] = 0;\r
@@ -476,6 +481,18 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext)
 }\r
 \r
 \r
+static void make_config_cfg(char *cfg)\r
+{\r
+       strncpy(cfg, PicoConfigFile, 511);\r
+       if (config_slot != 0)\r
+       {\r
+               char *p = strrchr(cfg, '.');\r
+               if (p == NULL) p = cfg + strlen(cfg);\r
+               sprintf(p, ".%i.cfg", config_slot);\r
+       }\r
+       cfg[511] = 0;\r
+}\r
+\r
 int emu_ReadConfig(int game, int no_defaults)\r
 {\r
        char cfg[512];\r
@@ -486,14 +503,7 @@ int emu_ReadConfig(int game, int no_defaults)
        {\r
                if (!no_defaults)\r
                        emu_setDefaultConfig();\r
-               strncpy(cfg, PicoConfigFile, 511);\r
-               if (config_slot != 0)\r
-               {\r
-                       char *p = strrchr(cfg, '.');\r
-                       if (p == NULL) p = cfg + strlen(cfg);\r
-                       sprintf(p, ".%i.cfg", config_slot);\r
-               }\r
-               cfg[511] = 0;\r
+               make_config_cfg(cfg);\r
                ret = config_readsect(cfg, NULL);\r
        }\r
        else\r
@@ -506,14 +516,25 @@ int emu_ReadConfig(int game, int no_defaults)
                else strcpy(cfg,  "game.cfg");\r
 \r
                ret = -1;\r
-               if (config_havesect(cfg, sect)) {\r
+               if (config_havesect(cfg, sect))\r
+               {\r
+                       // read user's config\r
+                       int vol = currentConfig.volume;\r
                        emu_setDefaultConfig();\r
                        ret = config_readsect(cfg, sect);\r
+                       currentConfig.volume = vol; // make vol global (bah)\r
+               }\r
+               else\r
+               {\r
+                       // read global config, and apply game_def.cfg on top\r
+                       make_config_cfg(cfg);\r
+                       config_readsect(cfg, NULL);\r
+                       ret = config_readsect("game_def.cfg", sect);\r
                }\r
 \r
                if (ret != 0)\r
                {\r
-                       // fall back to old\r
+                       // fall back to old game specific cfg\r
                        char extbuf[16];\r
                        if (config_slot != 0)\r
                                sprintf(extbuf, ".%i.pbcfg", config_slot);\r
@@ -538,7 +559,7 @@ int emu_ReadConfig(int game, int no_defaults)
                                PsndRate = currentConfig.s_PsndRate;\r
                                PicoRegionOverride = currentConfig.s_PicoRegion;\r
                                PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
-                               PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
+                               // PicoCDBuffers = currentConfig.s_PicoCDBuffers; // ignore in this case\r
                        }\r
                }\r
                else\r
@@ -651,6 +672,47 @@ void emu_textOut16(int x, int y, const char *text)
        }\r
 }\r
 \r
+void emu_findKeyBindCombos(void)\r
+{\r
+       int act, u;\r
+\r
+       // find out which keys and actions are combos\r
+       kb_combo_keys = kb_combo_acts = 0;\r
+       for (act = 0; act < 32; act++)\r
+       {\r
+               int keyc = 0, keyc2 = 0;\r
+               if (act == 16 || act == 17) continue; // player2 flag\r
+               if (act > 17)\r
+               {\r
+                       for (u = 0; u < 32; u++)\r
+                               if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;\r
+               }\r
+               else\r
+               {\r
+                       for (u = 0; u < 32; u++)\r
+                               if ((currentConfig.KeyBinds[u] & 0x30000) == 0 && // pl. 1\r
+                                       (currentConfig.KeyBinds[u] & (1 << act))) keyc++;\r
+                       for (u = 0; u < 32; u++)\r
+                               if ((currentConfig.KeyBinds[u] & 0x30000) == 1 && // pl. 2\r
+                                       (currentConfig.KeyBinds[u] & (1 << act))) keyc2++;\r
+                       if (keyc2 > keyc) keyc = keyc2;\r
+               }\r
+               if (keyc > 1)\r
+               {\r
+                       // loop again and mark those keys and actions as combo\r
+                       for (u = 0; u < 32; u++)\r
+                       {\r
+                               if (currentConfig.KeyBinds[u] & (1 << act)) {\r
+                                       kb_combo_keys |= 1 << u;\r
+                                       kb_combo_acts |= 1 << act;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       // printf("combo keys/acts: %08x %08x\n", kb_combo_keys, kb_combo_acts);\r
+}\r
+\r
 \r
 void emu_updateMovie(void)\r
 {\r
@@ -876,3 +938,4 @@ int emu_SaveLoadGame(int load, int sram)
                return ret;\r
        }\r
 }\r
+\r