X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=fceu.git;a=blobdiff_plain;f=state.c;h=ab2f0c92492ffcea10c244cb7e931e208a5ddfe9;hp=e306edab5d04a45fac08e93cae3ba237230d962b;hb=eec2623f6183dd0f9494b99065a16bf90f2a1ccf;hpb=890e37ba2b8ea1c7593dc05926d7431e3bd00bfb diff --git a/state.c b/state.c index e306eda..ab2f0c9 100644 --- a/state.c +++ b/state.c @@ -43,7 +43,11 @@ #include "memory.h" #include "ppu.h" -static SFORMAT SFMDATA[64]; +static void (*SPreSave)(void) = 0; +static void (*SPostSave)(void) = 0; + +#define SFMDATA_SIZE (64) +static SFORMAT SFMDATA[SFMDATA_SIZE]; static int SFEXINDEX; static int stateversion; @@ -102,145 +106,128 @@ SFORMAT SFSND[]={ -int WriteStateChunk(FILE *st, int type, SFORMAT *sf) +static int SubWrite(FILE *st, SFORMAT *sf) { - int bsize, count; - int x; + uint32 acc=0; + + while(sf->v) + { + if(sf->s==~0) /* Link to another struct. */ + { + uint32 tmp; + + if(!(tmp=SubWrite(st,(SFORMAT *)sf->v))) + return(0); + acc+=tmp; + sf++; + continue; + } + + acc+=8; /* Description + size */ + acc+=sf->s&(~RLSB); + + if(st) /* Are we writing or calculating the size of this block? */ + { + fwrite(sf->desc,1,4,st); + write32le(sf->s&(~RLSB),st); - count = x = 0; - while (sf[x++].v) count++; + #ifndef LSB_FIRST + if(sf->s&RLSB) + FlipByteOrder(sf->v,sf->s&(~RLSB)); + #endif + + fwrite((uint8 *)sf->v,1,sf->s&(~RLSB),st); + /* Now restore the original byte order. */ + #ifndef LSB_FIRST + if(sf->s&RLSB) + FlipByteOrder(sf->v,sf->s&(~RLSB)); + #endif + } + sf++; + } + + return(acc); +} + +static int WriteStateChunk(FILE *st, int type, SFORMAT *sf) +{ + int bsize; fputc(type,st); - for(x=bsize=0;xv) + { + if(sf->s==~0) /* Link to another SFORMAT structure. */ { - int z; - if(sf[x].s&RLSB) + SFORMAT *tmp; + if((tmp= CheckS((SFORMAT *)sf->v, tsize, desc) )) + return(tmp); + sf++; + continue; + } + if(!memcmp(desc,sf->desc,4)) { - for(z=(sf[x].s&(~RLSB))-1;z>=0;z--) + if(tsize!=(sf->s&(~RLSB))) { - fputc(*(uint8*)sf[x].v,st); + printf("ReadStateChunk: sect \"%c%c%c%c\" has wrong size\n", desc[0], desc[1], desc[2], desc[3]); + return(0); } + return(sf); } - else - fwrite((uint8 *)sf[x].v,1,sf[x].s&(~RLSB),st); - } - #endif + sf++; } - return (bsize+5); + return(0); } -int ReadStateChunk(FILE *st, SFORMAT *sf, int size) +static int ReadStateChunk(FILE *st, SFORMAT *sf, int size) { - uint8 tmpyo[16]; - int bsize, count; - int x; + //if(scan_chunks) + // return fseek(st,size,SEEK_CUR) == 0; - // recalculate count ourselves - count = x = 0; - while (sf[x++].v) count++; + SFORMAT *tmp; + int temp; + temp=ftell(st); - for(x=bsize=0;x=53) - bsize+=count<<3; - else + while(ftell(st)=53) - { - int temp; - temp=ftell(st); - - while(ftell(st)v,1,tmp->s&(~RLSB),st); - for(x=0;x=0;z--) - *(uint8*)sf[x].v=fgetc(st); - } - else - #endif - { - fread((uint8 *)sf[x].v,1,sf[x].s&(~RLSB),st); - } - goto bloo; - } - } - printf("ReadStateChunk: sect \"%c%c%c%c\" not handled\n", toa[0], toa[1], toa[2], toa[3]); - nkayo: - fseek(st,tsize,SEEK_CUR); - bloo:; - } // while(...) - } // >=53 - else - { - for(x=0;x=0;z--) - { - *(uint8*)sf[x].v=fgetc(st); - } - else - fread((uint8 *)sf[x].v,1,sf[x].s&(~RLSB),st); + #ifndef LSB_FIRST + if(tmp->s&RLSB) + FlipByteOrder(tmp->v,tmp->s&(~RLSB)); #endif } - } - if(stateversion<56) - { - for(x=0;x<16;x++) - #ifdef LSB_FIRST - mapbyte1[x]=mapbyte1[x<<1]; - #else - mapbyte1[x]=mapbyte1[(x<<1)+1]; - #endif - memcpy(mapbyte3,tmpyo,16); - } + else + { + fseek(st,tsize,SEEK_CUR); + printf("ReadStateChunk: sect \"%c%c%c%c\" not handled\n", toa[0], toa[1], toa[2], toa[3]); + } + } // while(...) return 1; } + static int ReadStateChunks(FILE *st) { int t; @@ -285,6 +272,7 @@ extern int geniestage; void SaveState(void) { FILE *st=NULL; + char *fname; TempAddrT=TempAddr; RefreshAddrT=RefreshAddr; @@ -295,13 +283,15 @@ void SaveState(void) return; } - st=fopen(FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0),"wb"); + fname = FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0); + st=fopen(fname,"wb"); + free(fname); if(st!=NULL) { - static uint32 totalsize; - static uint8 header[16]="FCS"; - memset(header+4,0,13); + uint32 totalsize; + uint8 header[16]="FCS"; + memset(header+4,0,sizeof(header)-4); header[3]=VERSION_NUMERIC; fwrite(header,1,16,st); @@ -313,7 +303,11 @@ void SaveState(void) totalsize+=WriteStateChunk(st,3,FCEUPPU_STATEINFO); totalsize+=WriteStateChunk(st,4,FCEUCTRL_STATEINFO); totalsize+=WriteStateChunk(st,5,SFSND); + + + if(SPreSave) SPreSave(); totalsize+=WriteStateChunk(st,0x10,SFMDATA); + if(SPostSave) SPostSave(); fseek(st,4,SEEK_SET); write32(totalsize,st); @@ -378,6 +372,7 @@ int FCEUSS_LoadFP(FILE *st, int make_backup) void LoadState(void) { FILE *st=NULL; + char *fname; if(geniestage==1) { @@ -385,15 +380,24 @@ void LoadState(void) return; } - st=fopen(FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0),"rb"); + fname = FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0); + st=fopen(fname,"rb"); + free(fname); + if (st) { FCEUSS_LoadFP(st, 0); fclose(st); } + else + { + FCEU_DispMessage("State %d load error (no file).",CurrentState); + SaveStateStatus[CurrentState]=0; + } } char SaveStateStatus[10]; +#if 0 // leaks memory void CheckStates(void) { FILE *st=NULL; @@ -412,33 +416,54 @@ void CheckStates(void) SaveStateStatus[ssel]=0; } } +#endif void SaveStateRefresh(void) { SaveStateStatus[0]=-1; } -void ResetExState(void) +void ResetExState(void (*PreSave)(void), void (*PostSave)(void)) { int x; for(x=0;x