From bcc9eda077d079ea5005d60ac58f9fe0d5105abe Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 10 Aug 2009 14:22:50 +0000 Subject: [PATCH] sram bugfix + savestate refactoring git-svn-id: file:///home/notaz/opt/svn/PicoDrive@744 be3aeb3a-fb24-0410-a615-afba39da0efa --- pico/area.c | 184 ++++++++++++++++++++++++++++++----------- pico/pico.h | 17 +--- pico/pico_int.h | 11 +++ platform/common/emu.c | 68 ++------------- platform/common/emu.h | 1 - platform/common/menu.c | 26 +----- 6 files changed, 164 insertions(+), 143 deletions(-) diff --git a/pico/area.c b/pico/area.c index 876320d6..8a14d558 100644 --- a/pico/area.c +++ b/pico/area.c @@ -8,6 +8,7 @@ #include "pico_int.h" +#include // ym2612 #include "sound/ym2612.h" @@ -32,16 +33,18 @@ void (*PicoLoadStateHook)(void) = NULL; // Scan one variable and callback -static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction) +static int ScanVar(void *data,int len,char *name,void *PmovFile,int is_write) { int ret = 0; - if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile); - if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile); + if (is_write) + ret = areaWrite(data,1,len,PmovFile); + else + ret = areaRead (data,1,len,PmovFile); return (ret != len); } -#define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction); -#define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction); +#define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,is_write); +#define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,is_write); // Pack the cpu into a common format: PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub) @@ -115,7 +118,7 @@ PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub) } // Scan the contents of the virtual machine's memory for saving or loading -static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile) +static int PicoAreaScan(int is_write, unsigned int ver, void *PmovFile) { void *ym2612_regs; unsigned char cpu[0x60]; @@ -124,59 +127,57 @@ static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile) memset(&cpu,0,sizeof(cpu)); memset(&cpu_z80,0,sizeof(cpu_z80)); + Pico.m.scanline=0; ym2612_regs = YM2612GetRegs(); - if (PmovAction&4) - { - Pico.m.scanline=0; - - // Scan all the memory areas: - SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram) - - // Pack, scan and unpack the cpu data: - if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0); - //PicoMemInit(); - SCAN_VAR(cpu,"cpu") - if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0); - - SCAN_VAR(Pico.m ,"misc") - SCAN_VAR(Pico.video,"video") - - if (PicoOpt&7) { - if((PmovAction&3)==1) z80_pack(cpu_z80); - ret = SCAN_VAR(cpu_z80,"cpu_z80") - // do not unpack if we fail to load z80 state - if((PmovAction&3)==2) { - if(ret) z80_reset(); - else z80_unpack(cpu_z80); - } - } - if (PicoOpt&3) - ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff - if (PicoOpt&1) { - if((PmovAction&3)==1) ym2612_pack_state(); - ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line - if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs - } + // Scan all the memory areas: + SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram) + + // Pack, scan and unpack the cpu data: + if (is_write) + PicoAreaPackCpu(cpu, 0); + SCAN_VAR(cpu,"cpu") + if (!is_write) + PicoAreaUnpackCpu(cpu, 0); + + SCAN_VAR(Pico.m ,"misc") + SCAN_VAR(Pico.video,"video") + + if (is_write) + z80_pack(cpu_z80); + ret = SCAN_VAR(cpu_z80,"cpu_z80") + // do not unpack if we fail to load z80 state + if (!is_write) { + if (ret) z80_reset(); + else z80_unpack(cpu_z80); } + ScanVar(sn76496_regs, 28*4, "SN76496state", PmovFile, is_write); + if (is_write) + ym2612_pack_state(); + ret = ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, is_write); // regs + addr line + if (!is_write && !ret) + ym2612_unpack_state(); + return 0; } // --------------------------------------------------------------------------- // Helper code to save/load to a file handle +// XXX: error checking // Save or load the state from PmovFile: -int PmovState(int PmovAction, void *PmovFile) +static int PmovState(int is_write, void *PmovFile) { int minimum=0; unsigned char head[32]; if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL) { - if (PmovAction&1) return PicoCdSaveState(PmovFile); - if (PmovAction&2) { + if (is_write) + return PicoCdSaveState(PmovFile); + else { int ret = PicoCdLoadState(PmovFile); if (PicoLoadStateHook) PicoLoadStateHook(); return ret; @@ -186,7 +187,6 @@ int PmovState(int PmovAction, void *PmovFile) memset(head,0,sizeof(head)); // Find out minimal compatible version: - //PicoAreaScan(PmovAction&0xc,&minimum); minimum = 0x0021; memcpy(head,"Pico",4); @@ -194,14 +194,106 @@ int PmovState(int PmovAction, void *PmovFile) *(unsigned int *)(head+0xc)=minimum; // Scan header: - if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile); - if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile); + if (is_write) + areaWrite(head,1,sizeof(head),PmovFile); + else + areaRead (head,1,sizeof(head),PmovFile); // Scan memory areas: - PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile); + PicoAreaScan(is_write, *(unsigned int *)(head+0x8), PmovFile); + + if (!is_write && PicoLoadStateHook) + PicoLoadStateHook(); - if ((PmovAction&2) && PicoLoadStateHook) PicoLoadStateHook(); + return 0; +} + +static size_t gzRead2(void *p, size_t _size, size_t _n, void *file) +{ + return gzread(file, p, _n); +} +static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file) +{ + return gzwrite(file, p, _n); +} + +static void set_cbs(int gz) +{ + if (gz) { + areaRead = gzRead2; + areaWrite = gzWrite2; + areaEof = (areaeof *) gzeof; + areaSeek = (areaseek *) gzseek; + areaClose = (areaclose *) gzclose; + } else { + areaRead = (arearw *) fread; + areaWrite = (arearw *) fwrite; + areaEof = (areaeof *) feof; + areaSeek = (areaseek *) fseek; + areaClose = (areaclose *) fclose; + } +} + +int PicoState(const char *fname, int is_save) +{ + void *afile = NULL; + int ret; + + if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) + { + if ( (afile = gzopen(fname, is_save ? "wb" : "rb")) ) { + set_cbs(1); + if (is_save) + gzsetparams(afile, 9, Z_DEFAULT_STRATEGY); + } + } + else + { + if ( (afile = fopen(fname, is_save ? "wb" : "rb")) ) { + set_cbs(0); + } + } + + if (afile == NULL) + return -1; + + ret = PmovState(is_save, afile); + areaClose(afile); + if (!is_save) + Pico.m.dirtyPal=1; + + return ret; +} + +int PicoStateLoadVDP(const char *fname) +{ + void *afile = NULL; + if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) + { + if ( (afile = gzopen(fname, "rb")) ) + set_cbs(1); + } + else + { + if ( (afile = fopen(fname, "rb")) ) + set_cbs(0); + } + if (afile == NULL) + return -1; + + if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL) { + PicoCdLoadStateGfx(afile); + } else { + areaSeek(afile, 0x10020, SEEK_SET); // skip header and RAM in state file + areaRead(Pico.vram, 1, sizeof(Pico.vram), afile); + areaSeek(afile, 0x2000, SEEK_CUR); + areaRead(Pico.cram, 1, sizeof(Pico.cram), afile); + areaRead(Pico.vsram, 1, sizeof(Pico.vsram), afile); + areaSeek(afile, 0x221a0, SEEK_SET); + areaRead(&Pico.video, 1, sizeof(Pico.video), afile); + } + areaClose(afile); return 0; } diff --git a/pico/pico.h b/pico/pico.h index 4940796d..ba7349e3 100644 --- a/pico/pico.h +++ b/pico/pico.h @@ -98,21 +98,12 @@ typedef struct } picohw_state; extern picohw_state PicoPicohw; -// Area.c -typedef size_t (arearw)(void *p, size_t _size, size_t _n, void *file); -typedef size_t (areaeof)(void *file); -typedef int (areaseek)(void *file, long offset, int whence); -typedef int (areaclose)(void *file); -// Save or load the state from PmovFile: -int PmovState(int PmovAction, void *PmovFile); // &1=for reading &2=for writing &4=volatile &8=non-volatile -extern arearw *areaRead; // external read and write function pointers for -extern arearw *areaWrite; // gzip save state ability -extern areaeof *areaEof; -extern areaseek *areaSeek; -extern areaclose *areaClose; +// area.c +int PicoState(const char *fname, int is_save); +int PicoStateLoadVDP(const char *fname); extern void (*PicoStateProgressCB)(const char *str); -// cd/Area.c +// cd/area.c int PicoCdLoadStateGfx(void *file); // cd/buffering.c diff --git a/pico/pico_int.h b/pico/pico_int.h index 9630399a..df3a6612 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -388,6 +388,17 @@ typedef struct { extern carthw_state_chunk *carthw_chunks; #define CHUNK_CARTHW 64 +// area.c +typedef size_t (arearw)(void *p, size_t _size, size_t _n, void *file); +typedef size_t (areaeof)(void *file); +typedef int (areaseek)(void *file, long offset, int whence); +typedef int (areaclose)(void *file); +extern arearw *areaRead; // external read and write function pointers for +extern arearw *areaWrite; // gzip save state ability +extern areaeof *areaEof; +extern areaseek *areaSeek; +extern areaclose *areaClose; + // Cart.c extern void (*PicoCartUnloadHook)(void); diff --git a/platform/common/emu.c b/platform/common/emu.c index 2e813d3d..08dbe0bc 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -1,4 +1,4 @@ -// (c) Copyright 2006-2007 notaz, All rights reserved. +// (c) Copyright 2006-2009 notaz, All rights reserved. // Free for non-commercial use. // For commercial use, separate licencing terms must be obtained. @@ -22,7 +22,6 @@ #include #include #include -#include #define STATUS_MSG_TIMEOUT 2000 @@ -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"); } + strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1); + rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0; + rom_loaded = 1; + // load SRAM for this ROM if (currentConfig.EmuOpt & EOPT_EN_SRAM) emu_save_load_game(1, 1); - strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1); - rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0; - rom_loaded = 1; return 1; fail2: @@ -766,18 +766,6 @@ void update_movie(void) } } - -static size_t gzRead2(void *p, size_t _size, size_t _n, void *file) -{ - return gzread(file, p, _n); -} - - -static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file) -{ - return gzwrite(file, p, _n); -} - static int try_ropen_file(const char *fname) { FILE *f; @@ -842,23 +830,6 @@ int emu_check_save_file(int slot) return emu_get_save_fname(1, 0, slot) ? 1 : 0; } -void emu_setSaveStateCbs(int gz) -{ - if (gz) { - areaRead = gzRead2; - areaWrite = gzWrite2; - areaEof = (areaeof *) gzeof; - areaSeek = (areaseek *) gzseek; - areaClose = (areaclose *) gzclose; - } else { - areaRead = (arearw *) fread; - areaWrite = (arearw *) fwrite; - areaEof = (areaeof *) feof; - areaSeek = (areaseek *) fseek; - areaClose = (areaclose *) fclose; - } -} - int emu_save_load_game(int load, int sram) { int ret = 0; @@ -929,34 +900,13 @@ int emu_save_load_game(int load, int sram) } else { - void *PmovFile = NULL; - if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) - { - if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) { - emu_setSaveStateCbs(1); - if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY); - } - } - else - { - if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) { - emu_setSaveStateCbs(0); - } - } - if(PmovFile) { - ret = PmovState(load ? 6 : 5, PmovFile); - areaClose(PmovFile); - PmovFile = 0; - if (load) Pico.m.dirtyPal=1; + ret = PicoState(saveFname, !load); + if (!ret) { #ifndef NO_SYNC - else sync(); + if (!load) sync(); #endif - } - else ret = -1; - if (!ret) emu_status_msg(load ? "STATE LOADED" : "STATE SAVED"); - else - { + } else { emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED"); ret = -1; } diff --git a/platform/common/emu.h b/platform/common/emu.h index f08d12ac..59fa7a1e 100644 --- a/platform/common/emu.h +++ b/platform/common/emu.h @@ -113,7 +113,6 @@ int emu_write_config(int game); char *emu_get_save_fname(int load, int is_sram, int slot); int emu_check_save_file(int slot); -void emu_setSaveStateCbs(int gz); void emu_text_out8 (int x, int y, const char *text); void emu_text_out16(int x, int y, const char *text); diff --git a/platform/common/menu.c b/platform/common/menu.c index 18c4a3fb..a81e1bc8 100644 --- a/platform/common/menu.c +++ b/platform/common/menu.c @@ -20,7 +20,6 @@ #include #include -#include #define array_size(x) (sizeof(x) / sizeof(x[0])) @@ -971,7 +970,7 @@ static void draw_savestate_bg(int slot) struct PicoVideo tmp_pv; unsigned short tmp_cram[0x40]; unsigned short tmp_vsram[0x40]; - void *tmp_vram, *file; + void *tmp_vram; char *fname; fname = emu_get_save_fname(1, 0, slot); @@ -985,28 +984,7 @@ static void draw_savestate_bg(int slot) memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram)); memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video)); - if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) { - file = gzopen(fname, "rb"); - emu_setSaveStateCbs(1); - } else { - file = fopen(fname, "rb"); - emu_setSaveStateCbs(0); - } - - if (file) { - if (PicoAHW & PAHW_MCD) { - PicoCdLoadStateGfx(file); - } else { - areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file - areaRead(Pico.vram, 1, sizeof(Pico.vram), file); - areaSeek(file, 0x2000, SEEK_CUR); - areaRead(Pico.cram, 1, sizeof(Pico.cram), file); - areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file); - areaSeek(file, 0x221a0, SEEK_SET); - areaRead(&Pico.video, 1, sizeof(Pico.video), file); - } - areaClose(file); - } + PicoStateLoadVDP(fname); /* do a frame and fetch menu bg */ pemu_forced_frame(POPT_EN_SOFTSCALE); -- 2.39.5