X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=fceu.git;a=blobdiff_plain;f=state.c;h=d711d9c19266f5acdd997de8707531296755e6fd;hp=c349cff3b634368e42e10fab343768f902f45299;hb=refs%2Fheads%2Fmaster;hpb=971a1d07d2f5b5a6f991ed5712275c04b933734f diff --git a/state.c b/state.c index c349cff..d711d9c 100644 --- a/state.c +++ b/state.c @@ -52,6 +52,7 @@ static int SFEXINDEX; static int stateversion; extern SFORMAT FCEUPPU_STATEINFO[]; // 3 +extern SFORMAT FCEUSND_STATEINFO[]; extern SFORMAT FCEUCTRL_STATEINFO[]; // 4 SFORMAT SFCPU[]={ // 1 @@ -82,169 +83,128 @@ SFORMAT SFCPUC[]={ // 2 extern uint16 TempAddrT,RefreshAddrT; -SFORMAT SFSND[]={ - { &fhcnt, 4|RLSB,"FHCN"}, - { &fcnt, 1, "FCNT"}, - { PSG, 14, "PSG"}, - { &PSG[0x15], 1, "P15"}, - { &PSG[0x17], 1, "P17"}, - { decvolume, 3, "DECV"}, - { &sqnon, 1, "SQNO"}, - { &nreg, 2|RLSB, "NREG"}, - { &trimode, 1, "TRIM"}, - { &tricoop, 1, "TRIC"}, - { sweepon, 2, "SWEE"}, - { &curfreq[0], 4|RLSB,"CRF1"}, - { &curfreq[1], 4|RLSB,"CRF2"}, - { SweepCount, 2,"SWCT"}, - { DecCountTo1, 3,"DCT1"}, - { &PCMBitIndex, 1,"PBIN"}, - { &PCMAddressIndex, 4|RLSB, "PAIN"}, - { &PCMSizeIndex, 4|RLSB, "PSIN"}, - { 0, } -}; +static int SubWrite(FILE *st, SFORMAT *sf) +{ + 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; + } -int WriteStateChunk(FILE *st, int type, SFORMAT *sf) -{ - int bsize, count; - int x; + 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++; + } - count = x = 0; - while (sf[x++].v) count++; + 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; @@ -273,13 +233,15 @@ for(;;) break; case 3:if(!ReadStateChunk(st,FCEUPPU_STATEINFO,size)) ret=0;break; case 4:if(!ReadStateChunk(st,FCEUCTRL_STATEINFO,size)) ret=0;break; - case 5:if(!ReadStateChunk(st,SFSND,size)) ret=0;break; + case 5:if(!ReadStateChunk(st,FCEUSND_STATEINFO,size)) ret=0;break; case 0x10:if(!ReadStateChunk(st,SFMDATA,size)) ret=0;break; default:printf("ReadStateChunks: unknown chunk: %i\n", t); if(fseek(st,size,SEEK_CUR)<0) goto endo;break; } } endo: + if(ret) + FCEUSND_LoadState(stateversion); return ret; } @@ -306,20 +268,21 @@ void SaveState(void) 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); #ifdef ASM_6502 asmcpu_pack(); #endif + FCEUSND_SaveState(); totalsize=WriteStateChunk(st,1,SFCPU); totalsize+=WriteStateChunk(st,2,SFCPUC); totalsize+=WriteStateChunk(st,3,FCEUPPU_STATEINFO); totalsize+=WriteStateChunk(st,4,FCEUCTRL_STATEINFO); - totalsize+=WriteStateChunk(st,5,SFSND); + totalsize+=WriteStateChunk(st,5,FCEUSND_STATEINFO); if(SPreSave) SPreSave(); @@ -462,8 +425,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;