bugfixes, new config system and messed code for it
[libpicofe.git] / common / emu.c
index 9d573bf..936665b 100644 (file)
@@ -40,6 +40,7 @@ int rom_loaded = 0;
 char noticeMsg[64];\r
 int state_slot = 0;\r
 int config_slot = 0, config_slot_current = 0;\r
+char lastRomFile[512];\r
 \r
 unsigned char *movie_data = NULL;\r
 static int movie_size = 0;\r
@@ -139,6 +140,7 @@ int emu_findBios(int region, char **bios_file)
 }\r
 \r
 /* check if the name begins with BIOS name */\r
+/*\r
 static int emu_isBios(const char *name)\r
 {\r
        int i;\r
@@ -150,8 +152,9 @@ static int emu_isBios(const char *name)
                if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r
        return 0;\r
 }\r
+*/\r
 \r
-static unsigned char scd_id_header[0x100];\r
+static unsigned char id_header[0x100];\r
 \r
 /* checks if romFileName points to valid MegaCD image\r
  * if so, checks for suitable BIOS */\r
@@ -177,7 +180,7 @@ int emu_cdCheck(int *pregion)
        }\r
 \r
        pm_seek(cd_f, (type == 1) ? 0x100 : 0x110, SEEK_SET);\r
-       pm_read(scd_id_header, sizeof(scd_id_header), cd_f);\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
@@ -229,21 +232,11 @@ 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
-       unsigned char *id_header;\r
        int pos;\r
 \r
-       if (Pico.rom == NULL) {\r
-               id_string[0] = 0;\r
-               return id_string;\r
-       }\r
-\r
-       if (PicoMCD & 1) {\r
-               id_header = scd_id_header;\r
-               strcpy(id_string, "CD|");\r
-       } else {\r
-               id_header = Pico.rom + 0x100;\r
-               strcpy(id_string, "MD|");\r
-       }\r
+       if (PicoMCD & 1)\r
+            strcpy(id_string, "CD|");\r
+       else strcpy(id_string, "MD|");\r
        pos = 3;\r
 \r
        pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, 1); // serial\r
@@ -253,7 +246,6 @@ char *emu_makeRomId(void)
        pos += extract_text(id_string + pos, id_header + 0x50, 0x30, 1); // overseas name\r
        id_string[pos] = 0;\r
 \r
-       printf("id_string: %s\n", id_string);\r
        return id_string;\r
 }\r
 \r
@@ -391,6 +383,8 @@ int emu_ReloadRom(void)
        }\r
 \r
        // load config for this ROM (do this before insert to get correct region)\r
+       if (!(PicoMCD&1))\r
+               memcpy(id_header, rom_data + 0x100, sizeof(id_header));\r
        if (!cfg_loaded) {\r
                ret = emu_ReadConfig(1, 1);\r
                if (!ret) emu_ReadConfig(0, 1);\r
@@ -418,13 +412,6 @@ int emu_ReloadRom(void)
 \r
        menu_romload_end();\r
 \r
-       if (!emu_isBios(romFileName))\r
-       {\r
-               // emu_ReadConfig() might have messed currentConfig.lastRomFile\r
-               strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);\r
-               currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;\r
-       }\r
-\r
        if (PicoPatches) {\r
                PicoPatchPrepare();\r
                PicoPatchApply();\r
@@ -463,6 +450,8 @@ int emu_ReloadRom(void)
        if (currentConfig.EmuOpt & 1)\r
                emu_SaveLoadGame(1, 1);\r
 \r
+       strncpy(lastRomFile, romFileName, sizeof(lastRomFile)-1);\r
+       lastRomFile[sizeof(lastRomFile)-1] = 0;\r
        rom_loaded = 1;\r
        return 1;\r
 }\r
@@ -509,14 +498,18 @@ int emu_ReadConfig(int game, int no_defaults)
        }\r
        else\r
        {\r
-               if (!no_defaults)\r
-                       emu_setDefaultConfig();\r
+               char *sect = emu_makeRomId();\r
 \r
                // try new .cfg way\r
                if (config_slot != 0)\r
                     sprintf(cfg, "game.%i.cfg", config_slot);\r
                else strcpy(cfg,  "game.cfg");\r
-               ret = config_readsect(cfg, emu_makeRomId());\r
+\r
+               ret = -1;\r
+               if (config_havesect(cfg, sect)) {\r
+                       emu_setDefaultConfig();\r
+                       ret = config_readsect(cfg, sect);\r
+               }\r
 \r
                if (ret != 0)\r
                {\r
@@ -532,7 +525,9 @@ int emu_ReadConfig(int game, int no_defaults)
                                f = fopen(cfg, "rb");\r
                        }\r
                        if (f) {\r
-                               int bread = fread(&currentConfig, 1, sizeof(currentConfig), f);\r
+                               int bread;\r
+                               fseek(f, 512, SEEK_SET); // skip unused lrom buffer\r
+                               bread = fread(&currentConfig, 1, sizeof(currentConfig), f);\r
                                lprintf("emu_ReadConfig: %s %s\n", cfg, bread > 0 ? "(ok)" : "(failed)");\r
                                fclose(f);\r
                                ret = 0;\r
@@ -548,7 +543,7 @@ int emu_ReadConfig(int game, int no_defaults)
                }\r
                else\r
                {\r
-                       lprintf("loaded cf from game sect\n");\r
+                       lprintf("loaded cfg from sect \"%s\"\n", sect);\r
                }\r
        }\r
 \r
@@ -593,10 +588,12 @@ int emu_WriteConfig(int is_game)
                     sprintf(cfg, "game.%i.cfg", config_slot);\r
                else strcpy(cfg,  "game.cfg");\r
                game_sect = emu_makeRomId();\r
+               lprintf("emu_WriteConfig: sect \"%s\"\n", game_sect);\r
        }\r
 \r
        lprintf("emu_WriteConfig: %s ", cfg);\r
        ret = config_writesect(cfg, game_sect);\r
+       if (write_lrom) config_writelrom(cfg);\r
 #ifndef NO_SYNC\r
        sync();\r
 #endif\r