sram bugfix + savestate refactoring
[picodrive.git] / platform / common / emu.c
index 7de2aa9..08dbe0b 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006-2007 notaz, All rights reserved.\r
+// (c) Copyright 2006-2009 notaz, All rights reserved.\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
@@ -22,7 +22,6 @@
 #include <pico/pico_int.h>\r
 #include <pico/patch.h>\r
 #include <pico/cd/cue.h>\r
-#include <zlib/zlib.h>\r
 \r
 \r
 #define STATUS_MSG_TIMEOUT 2000\r
@@ -407,8 +406,8 @@ int emu_reload_rom(char *rom_fname)
                // valid CD image, check for BIOS..\r
 \r
                // we need to have config loaded at this point\r
-               ret = emu_read_config(1, 1);\r
-               if (!ret) emu_read_config(0, 1);\r
+               ret = emu_read_config(1, 0);\r
+               if (!ret) emu_read_config(0, 0);\r
                cfg_loaded = 1;\r
 \r
                if (PicoRegionOverride) {\r
@@ -465,8 +464,8 @@ int emu_reload_rom(char *rom_fname)
        if (!(PicoAHW & PAHW_MCD))\r
                memcpy(id_header, rom_data + 0x100, sizeof(id_header));\r
        if (!cfg_loaded) {\r
-               ret = emu_read_config(1, 1);\r
-               if (!ret) emu_read_config(0, 1);\r
+               ret = emu_read_config(1, 0);\r
+               if (!ret) emu_read_config(0, 0);\r
        }\r
 \r
        lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r
@@ -516,13 +515,14 @@ int emu_reload_rom(char *rom_fname)
                emu_status_msg(Pico.m.pal ? "PAL SYSTEM / 50 FPS" : "NTSC SYSTEM / 60 FPS");\r
        }\r
 \r
+       strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r
+       rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r
+       rom_loaded = 1;\r
+\r
        // load SRAM for this ROM\r
        if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r
                emu_save_load_game(1, 1);\r
 \r
-       strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r
-       rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r
-       rom_loaded = 1;\r
        return 1;\r
 \r
 fail2:\r
@@ -613,10 +613,11 @@ int emu_read_config(int game, int no_defaults)
        char cfg[512];\r
        int ret;\r
 \r
+       if (!no_defaults)\r
+               emu_set_defconfig();\r
+\r
        if (!game)\r
        {\r
-               if (!no_defaults)\r
-                       emu_set_defconfig();\r
                make_config_cfg(cfg);\r
                ret = config_readsect(cfg, NULL);\r
        }\r
@@ -643,7 +644,8 @@ int emu_read_config(int game, int no_defaults)
                        // 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
+                       emu_make_path(cfg, "game_def.cfg", sizeof(cfg));\r
+                       ret = config_readsect(cfg, sect);\r
                }\r
 \r
                if (ret == 0)\r
@@ -764,18 +766,6 @@ void update_movie(void)
        }\r
 }\r
 \r
-\r
-static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
-{\r
-       return gzread(file, p, _n);\r
-}\r
-\r
-\r
-static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
-{\r
-       return gzwrite(file, p, _n);\r
-}\r
-\r
 static int try_ropen_file(const char *fname)\r
 {\r
        FILE *f;\r
@@ -795,7 +785,8 @@ char *emu_get_save_fname(int load, int is_sram, int slot)
 \r
        if (is_sram)\r
        {\r
-               romfname_ext(saveFname, (PicoAHW&1) ? "brm"PATH_SEP : "srm"PATH_SEP, (PicoAHW&1) ? ".brm" : ".srm");\r
+               romfname_ext(saveFname, (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP,\r
+                               (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r
                if (load) {\r
                        if (try_ropen_file(saveFname)) return saveFname;\r
                        // try in current dir..\r
@@ -839,23 +830,6 @@ int emu_check_save_file(int slot)
        return emu_get_save_fname(1, 0, slot) ? 1 : 0;\r
 }\r
 \r
-void emu_setSaveStateCbs(int gz)\r
-{\r
-       if (gz) {\r
-               areaRead  = gzRead2;\r
-               areaWrite = gzWrite2;\r
-               areaEof   = (areaeof *) gzeof;\r
-               areaSeek  = (areaseek *) gzseek;\r
-               areaClose = (areaclose *) gzclose;\r
-       } else {\r
-               areaRead  = (arearw *) fread;\r
-               areaWrite = (arearw *) fwrite;\r
-               areaEof   = (areaeof *) feof;\r
-               areaSeek  = (areaseek *) fseek;\r
-               areaClose = (areaclose *) fclose;\r
-       }\r
-}\r
-\r
 int emu_save_load_game(int load, int sram)\r
 {\r
        int ret = 0;\r
@@ -926,34 +900,13 @@ int emu_save_load_game(int load, int sram)
        }\r
        else\r
        {\r
-               void *PmovFile = NULL;\r
-               if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)\r
-               {\r
-                       if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
-                               emu_setSaveStateCbs(1);\r
-                               if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
-                               emu_setSaveStateCbs(0);\r
-                       }\r
-               }\r
-               if(PmovFile) {\r
-                       ret = PmovState(load ? 6 : 5, PmovFile);\r
-                       areaClose(PmovFile);\r
-                       PmovFile = 0;\r
-                       if (load) Pico.m.dirtyPal=1;\r
+               ret = PicoState(saveFname, !load);\r
+               if (!ret) {\r
 #ifndef NO_SYNC\r
-                       else sync();\r
+                       if (!load) sync();\r
 #endif\r
-               }\r
-               else    ret = -1;\r
-               if (!ret)\r
                        emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");\r
-               else\r
-               {\r
+               } else {\r
                        emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");\r
                        ret = -1;\r
                }\r
@@ -987,9 +940,14 @@ void emu_set_fastforward(int set_on)
        }\r
 }\r
 \r
-static void emu_msg_tray_open(void)\r
+static void emu_tray_open(void)\r
 {\r
-       emu_status_msg("CD tray opened");\r
+       engineState = PGS_TrayMenu;\r
+}\r
+\r
+static void emu_tray_close(void)\r
+{\r
+       emu_status_msg("CD tray closed.");\r
 }\r
 \r
 void emu_reset_game(void)\r
@@ -1211,8 +1169,8 @@ void emu_init(void)
 \r
        PicoInit();\r
        PicoMessage = plat_status_msg_busy_next;\r
-       PicoMCDopenTray = emu_msg_tray_open;\r
-       PicoMCDcloseTray = menu_loop_tray;\r
+       PicoMCDopenTray = emu_tray_open;\r
+       PicoMCDcloseTray = emu_tray_close;\r
 }\r
 \r
 void emu_finish(void)\r
@@ -1275,9 +1233,6 @@ void emu_loop(void)
        if (PicoAHW & PAHW_MCD)\r
                PicoCDBufferInit();\r
 \r
-       if (currentConfig.EmuOpt & EOPT_PSYNC)\r
-               plat_video_wait_vsync();\r
-\r
        pemu_loop_prep();\r
 \r
        timestamp_fps = get_ticks();\r
@@ -1285,6 +1240,8 @@ void emu_loop(void)
 \r
        frames_done = frames_shown = pframes_done = 0;\r
 \r
+       plat_video_wait_vsync();\r
+\r
        /* loop with resync every 1 sec. */\r
        while (engineState == PGS_Running)\r
        {\r
@@ -1413,12 +1370,9 @@ void emu_loop(void)
                        if (diff < diff_lim)\r
                        {\r
                                // we are too fast\r
-                               if (currentConfig.EmuOpt & EOPT_PSYNC) {\r
-                                       if (diff_lim - diff > target_frametime/2)\r
-                                               plat_wait_till_us(timestamp_base + target_frametime/4);\r
+                               plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);\r
+                               if (currentConfig.EmuOpt & EOPT_VSYNC)\r
                                        plat_video_wait_vsync();\r
-                               } else\r
-                                       plat_wait_till_us(timestamp_base + diff_lim);\r
                        }\r
                }\r
 \r
@@ -1429,9 +1383,6 @@ void emu_loop(void)
 \r
        emu_set_fastforward(0);\r
 \r
-       if (PicoAHW & PAHW_MCD)\r
-               PicoCDBufferFree();\r
-\r
        // save SRAM\r
        if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
                plat_status_msg_busy_first("Writing SRAM/BRAM...");\r
@@ -1439,9 +1390,11 @@ void emu_loop(void)
                SRam.changed = 0;\r
        }\r
 \r
-       // do menu background to be sure it's right\r
-       pemu_forced_frame(POPT_EN_SOFTSCALE);\r
-\r
        pemu_loop_end();\r
+\r
+       // pemu_loop_end() might want to do 1 frame for bg image,\r
+       // so free CD buffer here\r
+       if (PicoAHW & PAHW_MCD)\r
+               PicoCDBufferFree();\r
 }\r
 \r