sram bugfix + savestate refactoring
[libpicofe.git] / common / emu.c
index 2e813d3..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
@@ -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
@@ -766,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
@@ -842,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
@@ -929,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