X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=fceu.git;a=blobdiff_plain;f=state.c;h=deacbbfde361bc36d85d755d2f06998570e4eee1;hp=c349cff3b634368e42e10fab343768f902f45299;hb=0bb3fe122854be9af6c3024fef9f503d27bc63a1;hpb=d0f72c9ab3d6acf18f236970c1c359fafa3e1ce7 diff --git a/state.c b/state.c index c349cff..deacbbf 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; - count = x = 0; - while (sf[x++].v) count++; + 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); +} + +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; @@ -462,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;