From e83f16af870ad0da25419793cee6a717cf5500dc Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 20 Dec 2017 06:00:58 +0100 Subject: [PATCH] Cleanup savestate code --- libretro.cpp | 4 +- mednafen/hw_misc/arcade_card/arcade_card.cpp | 4 +- mednafen/pce_fast/huc6280.cpp | 2 +- mednafen/pce_fast/input.cpp | 2 +- mednafen/pce_fast/pcecd.cpp | 4 +- mednafen/pce_fast/pcecd_drive.cpp | 2 +- mednafen/pce_fast/psg.cpp | 4 +- mednafen/pce_fast/vdc.cpp | 4 +- mednafen/state.cpp | 90 +++++++++----------- mednafen/state.h | 22 ++--- 10 files changed, 59 insertions(+), 79 deletions(-) diff --git a/libretro.cpp b/libretro.cpp index 62ef62a..7fd0992 100644 --- a/libretro.cpp +++ b/libretro.cpp @@ -409,7 +409,7 @@ int StateAction(StateMem *sm, int load, int data_only) // if(BaseRAM[i] != 0xFF) // printf("%d %02x\n", i, BaseRAM[i]); - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN"); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false); ret &= HuC6280_StateAction(sm, load, data_only); ret &= VDC_StateAction(sm, load, data_only); @@ -759,7 +759,7 @@ int HuC_StateAction(StateMem *sm, int load, int data_only) SFVAR(HuCSF2Latch), SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "HuC"); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "HuC", false); if(load) HuCSF2Latch &= 0x3; diff --git a/mednafen/hw_misc/arcade_card/arcade_card.cpp b/mednafen/hw_misc/arcade_card/arcade_card.cpp index 541323d..c8d81ca 100644 --- a/mednafen/hw_misc/arcade_card/arcade_card.cpp +++ b/mednafen/hw_misc/arcade_card/arcade_card.cpp @@ -302,7 +302,7 @@ int ArcadeCard::StateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, ACUsedRegs, "ArcadeCardUsed")) + if(!MDFNSS_StateAction(sm, load, data_only, ACUsedRegs, "ArcadeCardUsed", false)) return(0); SFORMAT ACStateRegs[] = @@ -325,7 +325,7 @@ int ArcadeCard::StateAction(StateMem *sm, int load, int data_only) SFARRAY(ACRAM, ACRAMUsed ? 0x200000 : 0x0), SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, ACStateRegs, "ArcadeCard"); + int ret = MDFNSS_StateAction(sm, load, data_only, ACStateRegs, "ArcadeCard", false); return(ret); diff --git a/mednafen/pce_fast/huc6280.cpp b/mednafen/pce_fast/huc6280.cpp index d3df3c7..91d0c4d 100644 --- a/mednafen/pce_fast/huc6280.cpp +++ b/mednafen/pce_fast/huc6280.cpp @@ -763,7 +763,7 @@ int HuC6280_StateAction(StateMem *sm, int load, int data_only) SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, SFCPU, "CPU"); + int ret = MDFNSS_StateAction(sm, load, data_only, SFCPU, "CPU", false); if(load) { diff --git a/mednafen/pce_fast/input.cpp b/mednafen/pce_fast/input.cpp index a6f21dc..b035952 100644 --- a/mednafen/pce_fast/input.cpp +++ b/mednafen/pce_fast/input.cpp @@ -229,7 +229,7 @@ int INPUT_StateAction(StateMem *sm, int load, int data_only) SFVAR(read_index), SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "JOY"); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "JOY", false); return(ret); } diff --git a/mednafen/pce_fast/pcecd.cpp b/mednafen/pce_fast/pcecd.cpp index 11b9622..2098cce 100644 --- a/mednafen/pce_fast/pcecd.cpp +++ b/mednafen/pce_fast/pcecd.cpp @@ -899,7 +899,7 @@ static int ADPCM_StateAction(StateMem *sm, int load, int data_only) SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "APCM"); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "APCM", false); if(load) { MSM5205.SetSample(ad_sample); @@ -932,7 +932,7 @@ int PCECD_StateAction(StateMem *sm, int load, int data_only) SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "PECD"); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "PECD", false); ret &= PCECD_Drive_StateAction(sm, load, data_only, "CDRM"); ret &= ADPCM_StateAction(sm, load, data_only); diff --git a/mednafen/pce_fast/pcecd_drive.cpp b/mednafen/pce_fast/pcecd_drive.cpp index 8abc1ad..694cc9f 100644 --- a/mednafen/pce_fast/pcecd_drive.cpp +++ b/mednafen/pce_fast/pcecd_drive.cpp @@ -1299,7 +1299,7 @@ int PCECD_Drive_StateAction(StateMem * sm, int load, int data_only, const char * SFEND }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, sname); + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, sname, false); if(load) { diff --git a/mednafen/pce_fast/psg.cpp b/mednafen/pce_fast/psg.cpp index 44b8ece..926bd72 100644 --- a/mednafen/pce_fast/psg.cpp +++ b/mednafen/pce_fast/psg.cpp @@ -608,7 +608,7 @@ int PCEFast_PSG::StateAction(StateMem *sm, int load, int data_only) SFEND }; tmpstr[3] = '0' + ch; - ret &= MDFNSS_StateAction(sm, load, data_only, CH_StateRegs, tmpstr); + ret &= MDFNSS_StateAction(sm, load, data_only, CH_StateRegs, tmpstr, false); } SFORMAT PSG_StateRegs[] = @@ -624,7 +624,7 @@ int PCEFast_PSG::StateAction(StateMem *sm, int load, int data_only) SFEND }; - ret &= MDFNSS_StateAction(sm, load, data_only, PSG_StateRegs, "PSG"); + ret &= MDFNSS_StateAction(sm, load, data_only, PSG_StateRegs, "PSG", false); if(load) { diff --git a/mednafen/pce_fast/vdc.cpp b/mednafen/pce_fast/vdc.cpp index 99303c9..50ec64c 100644 --- a/mednafen/pce_fast/vdc.cpp +++ b/mednafen/pce_fast/vdc.cpp @@ -1093,7 +1093,7 @@ int VDC_StateAction(StateMem *sm, int load, int data_only) }; - int ret = MDFNSS_StateAction(sm, load, data_only, VCE_StateRegs, "VCE"); + int ret = MDFNSS_StateAction(sm, load, data_only, VCE_StateRegs, "VCE", false); int chip = 0; { @@ -1155,7 +1155,7 @@ int VDC_StateAction(StateMem *sm, int load, int data_only) SFEND }; - ret &= MDFNSS_StateAction(sm, load, data_only, VDC_StateRegs, "VDC0"); + ret &= MDFNSS_StateAction(sm, load, data_only, VDC_StateRegs, "VDC0", false); if(load) { diff --git a/mednafen/state.cpp b/mednafen/state.cpp index cf8f7ff..9f5dd83 100644 --- a/mednafen/state.cpp +++ b/mednafen/state.cpp @@ -19,8 +19,6 @@ #include -#include - #include "mednafen.h" #include "driver.h" #include "general.h" @@ -375,69 +373,60 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) } /* This function is called by the game driver(NES, GB, GBA) to save a state. */ -static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, std::vector §ions) +static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDescriptor *section) { StateMem *st = (StateMem*)st_p; - std::vector::iterator section; if(load) { + char sname[32]; + + int found = 0; + uint32_t tmp_size; + uint32_t total = 0; + + while(smem_read(st, (uint8_t *)sname, 32) == 32) { - char sname[32]; + if(smem_read32le(st, &tmp_size) != 4) + return(0); - for(section = sections.begin(); section != sections.end(); section++) + total += tmp_size + 32 + 4; + + // Yay, we found the section + if(!strncmp(sname, section->name, 32)) { - int found = 0; - uint32_t tmp_size; - uint32_t total = 0; - - while(smem_read(st, (uint8_t *)sname, 32) == 32) + if(!ReadStateChunk(st, section->sf, tmp_size)) { - if(smem_read32le(st, &tmp_size) != 4) - return(0); - - total += tmp_size + 32 + 4; - - // Yay, we found the section - if(!strncmp(sname, section->name, 32)) - { - if(!ReadStateChunk(st, section->sf, tmp_size)) - { - printf("Error reading chunk: %s\n", section->name); - return(0); - } - found = 1; - break; - } - else - { - if(smem_seek(st, tmp_size, SEEK_CUR) < 0) - { - puts("Chunk seek failure"); - return(0); - } - } - } - if(smem_seek(st, -total, SEEK_CUR) < 0) - { - puts("Reverse seek error"); + printf("Error reading chunk: %s\n", section->name); return(0); } - if(!found && !section->optional) // Not found. We are sad! + found = 1; + break; + } + else + { + if(smem_seek(st, tmp_size, SEEK_CUR) < 0) { - printf("Section missing: %.32s\n", section->name); + puts("Chunk seek failure"); return(0); } } } + if(smem_seek(st, -total, SEEK_CUR) < 0) + { + puts("Reverse seek error"); + return(0); + } + if(!found && !section->optional) // Not found. We are sad! + { + printf("Section missing: %.32s\n", section->name); + return(0); + } } else { - for(section = sections.begin(); section != sections.end(); section++) - { - if(!WriteStateChunk(st, section->name, section->sf)) - return(0); - } + if(!WriteStateChunk(st, section->name, section->sf)) + return(0); } return(1); @@ -445,11 +434,14 @@ static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, std: int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional) { + SSDescriptor love; StateMem *st = (StateMem*)st_p; - std::vector love; - love.push_back(SSDescriptor(sf, name, optional)); - return(MDFNSS_StateAction_internal(st, load, 0, love)); + love.sf = sf; + love.name = name; + love.optional = optional; + + return(MDFNSS_StateAction_internal(st, load, 0, &love)); } int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*) diff --git a/mednafen/state.h b/mednafen/state.h index 9e03414..5ff6f37 100644 --- a/mednafen/state.h +++ b/mednafen/state.h @@ -90,25 +90,13 @@ INLINE uint32_t SF_FORCE_D(double *) { return(0); } #define SFEND { 0, 0, 0, 0 } // State-Section Descriptor -class SSDescriptor +struct SSDescriptor { - public: - SSDescriptor(SFORMAT *n_sf, const char *n_name, bool n_optional = 0) - { - sf = n_sf; - name = n_name; - optional = n_optional; - } - ~SSDescriptor(void) - { - - } - - SFORMAT *sf; - const char *name; - bool optional; + SFORMAT *sf; + const char *name; + bool optional; }; -int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional = 0); +int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional); #endif