X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=fceu.git;a=blobdiff_plain;f=state.c;h=ab2f0c92492ffcea10c244cb7e931e208a5ddfe9;hp=c50223dbb9fbd45214b25481e259ff2f6a0555c5;hb=eec2623f6183dd0f9494b99065a16bf90f2a1ccf;hpb=d97315ac0bca825d2d50a44453bc5652946e2c67 diff --git a/state.c b/state.c index c50223d..ab2f0c9 100644 --- a/state.c +++ b/state.c @@ -106,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); + + #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); +} - count = x = 0; - while (sf[x++].v) count++; +static int WriteStateChunk(FILE *st, int type, SFORMAT *sf) +{ + int bsize; fputc(type,st); - for(x=bsize=0;xv) { - fwrite(sf[x].desc,1,4,st); - write32(sf[x].s&(~RLSB),st); - #ifdef LSB_FIRST - fwrite((uint8 *)sf[x].v,1,sf[x].s&(~RLSB),st); - #else + 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); + read32le(&tsize,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; @@ -289,6 +272,7 @@ extern int geniestage; void SaveState(void) { FILE *st=NULL; + char *fname; TempAddrT=TempAddr; RefreshAddrT=RefreshAddr; @@ -299,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); @@ -386,6 +372,7 @@ int FCEUSS_LoadFP(FILE *st, int make_backup) void LoadState(void) { FILE *st=NULL; + char *fname; if(geniestage==1) { @@ -393,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; @@ -420,6 +416,7 @@ void CheckStates(void) SaveStateStatus[ssel]=0; } } +#endif void SaveStateRefresh(void) { @@ -448,8 +445,7 @@ void AddExState(void *v, uint32 s, int type, char *desc) strcpy(SFMDATA[SFEXINDEX].desc,desc); } else -// SFMDATA[SFEXINDEX].desc=0; - return; // do not support recursive save structures + SFMDATA[SFEXINDEX].desc=0; SFMDATA[SFEXINDEX].v=v; SFMDATA[SFEXINDEX].s=s; if(type) SFMDATA[SFEXINDEX].s|=RLSB;