diff --git a/zsnes/src/initc.c b/zsnes/src/initc.c index 78d09efd..cca38bf7 100755 --- a/zsnes/src/initc.c +++ b/zsnes/src/initc.c @@ -80,10 +80,10 @@ extern unsigned char spcon; void procexecloop() { - curexecstate &= 0xFFFFFF00; + curexecstate &= 0xFFFFFF00; - if (spcon) { curexecstate += 3; } - else { curexecstate += 1; } + if (spcon) { curexecstate += 3; } + else { curexecstate += 1; } } void Debug_WriteString(char *str) @@ -1807,14 +1807,31 @@ unsigned int showinfogui() extern unsigned int nmiprevaddrl, nmiprevaddrh, nmirept, nmiprevline, nmistatus; extern unsigned int spcnumread, spchalted; extern unsigned char NextLineCache, sramsavedis, sndrot, regsbackup[3019]; -extern unsigned char yesoutofmemory; +extern unsigned char yesoutofmemory, fnames[512]; void SetupROM(); void initsnes(); void outofmemfix(); void GUIDoReset(); -void powercycle() +bool loadSRAM(char *sramname) +{ + FILE *sramfp; + int sramsize; + + if ((sramfp = fopen(sramname, "rb"))) + { + fseek(sramfp, 0, SEEK_END); + sramsize = ftell(sramfp); + rewind(sramfp); + if (sramsize) { fread(sram, 1, sramsize, sramfp); } + fclose(sramfp); + return (true); + } + else { return(false); } +} + +void powercycle(bool sramload) { memset(sram, 0xFF, 8192*4); clearSPCRAM(); @@ -1829,6 +1846,7 @@ void powercycle() NextLineCache = 0; curexecstate = 1; + if (sramload) { loadSRAM(fnames+1); } asm_call(SetupROM); asm_call(initsnes); diff --git a/zsnes/src/zmovie.c b/zsnes/src/zmovie.c index 31880010..1f7867a7 100644 --- a/zsnes/src/zmovie.c +++ b/zsnes/src/zmovie.c @@ -49,13 +49,15 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. extern unsigned int versionNumber, CRC32, cur_zst_size; extern unsigned int JoyAOrig, JoyBOrig, JoyCOrig, JoyDOrig, JoyEOrig; extern unsigned int MsgCount, MessageOn; -extern unsigned char MovieStartMethod, GUIReset, ReturnFromSPCStall; -extern unsigned char MovieProcessing, *Msgptr; +extern unsigned char MovieStartMethod, GUIReset, ReturnFromSPCStall, GUIQuit; +extern unsigned char MovieProcessing, *Msgptr, fnamest[512]; +extern unsigned char CMovieExt; extern bool romispal; bool firstloop; void GUIDoReset(); -void powercycle(); +void powercycle(bool); +void zst_sram_load(FILE *); void zst_save(FILE *, bool); bool zst_load(FILE *); @@ -556,14 +558,14 @@ static void zmv_create(char *filename) case zmv_sm_zst: break; case zmv_sm_power: - powercycle(); + powercycle(true); break; case zmv_sm_reset: GUIReset = 1; asm_call(GUIDoReset); ReturnFromSPCStall = 0; break; - } + } zst_save(zmv_vars.fp, false); zmv_vars.filename = (char *)malloc(filename_len+1); //+1 for null @@ -712,6 +714,8 @@ static bool zmv_open(char *filename) } + MovieStartMethod = (unsigned char)zmv_vars.header.zmv_flag.start_method; + if (zmv_vars.header.zsnes_version != (versionNumber & 0xFFFF)) { zst_load(zmv_vars.fp); @@ -725,23 +729,22 @@ static bool zmv_open(char *filename) zst_load(zmv_vars.fp); break; case zmv_sm_power: - powercycle(); - fseek(zmv_vars.fp, zmv_vars.header.zst_size, SEEK_CUR); + powercycle(false); + zst_sram_load(zmv_vars.fp); break; case zmv_sm_reset: GUIReset = 1; asm_call(GUIDoReset); ReturnFromSPCStall = 0; - fseek(zmv_vars.fp, zmv_vars.header.zst_size, SEEK_CUR); + zst_sram_load(zmv_vars.fp); break; } + zmv_open_vars.input_start_pos = ftell(zmv_vars.fp); Msgptr = "MOVIE STARTED."; } - + firstloop = true; - - zmv_open_vars.input_start_pos = ftell(zmv_vars.fp); fseek(zmv_vars.fp, -(EXT_CHAP_COUNT_END_DIST), SEEK_END); zmv_open_vars.external_chapter_count = fread2(zmv_vars.fp); @@ -1314,6 +1317,7 @@ static void MovieSub_Close() { if (MovieSub.fp) { + MessageOn = 0; fclose(MovieSub.fp); MovieSub.fp = 0; } @@ -1390,6 +1394,14 @@ MovieProcessing */ +extern bool SRAMState, SloMo50; +bool PrevSRAMState; +extern unsigned int statefileloc; +extern unsigned char ComboCounter, MovieRecordWinVal, RewindStates; +char MovieFrameStr[10]; +void SRAMChdir(); +void ChangetoLOADdir(); + void MovieInsertChapter() { switch (MovieProcessing) @@ -1452,9 +1464,6 @@ void MovieSeekBehind() MessageOn = MsgCount; } -extern bool SRAMState; -bool PrevSRAMState; - void Replay() { if (zmv_replay()) @@ -1486,19 +1495,12 @@ void Replay() } } -extern bool SloMo50; -extern unsigned char ComboCounter; - void ProcessMovies() { if (MovieProcessing == 2) { zmv_record(SloMo50 ? true : false, ComboCounter); } else { Replay(); } } -// The following will maybe end up in guic.c once we get it started. -// It came from guiwindp.inc and gui.asm, after all -extern unsigned char MovieRecordWinVal; - void SkipMovie() { MovieRecordWinVal = 0; @@ -1513,6 +1515,7 @@ void MovieStop() case 1: zmv_replay_finished(); MovieSub_Close(); + SRAMState = PrevSRAMState; break; case 2: @@ -1527,27 +1530,19 @@ void MovieStop() zmv_dealloc_rewind_buffer(); MovieProcessing = 0; - SRAMState = PrevSRAMState; } else { firstloop = false; } } -extern unsigned int statefileloc; -extern unsigned char GUIQuit, fnamest[512], CMovieExt; -extern unsigned char RewindStates; - -void SRAMChdir(); -void ChangetoLOADdir(); - void MoviePlay() { unsigned char FileExt[4]; + PrevSRAMState = SRAMState; + SRAMState = true; + if (!MovieProcessing) { - PrevSRAMState = SRAMState; - SRAMState = true; - GUIQuit = 2; memcpy(FileExt, &fnamest[statefileloc-3], 4); memcpy(&fnamest[statefileloc-3], ".zmv", 4); @@ -1602,9 +1597,6 @@ void MovieRecord() if (!(tempfhandle = fopen(fnamest+1,"rb"))) { - PrevSRAMState = SRAMState; - SRAMState = true; - zmv_create(fnamest+1); zmv_alloc_rewind_buffer(RewindStates); MovieProcessing = 2; @@ -1618,12 +1610,11 @@ void MovieRecord() } asm_call(ChangetoLOADdir); + memcpy (&fnamest[statefileloc-3], FileExt, 4); } } -char MovieFrameStr[10]; - void GetMovieFrameStr() { *MovieFrameStr = 0; diff --git a/zsnes/src/zstate.c b/zsnes/src/zstate.c index 8cb4bb53..0a04a2a2 100755 --- a/zsnes/src/zstate.c +++ b/zsnes/src/zstate.c @@ -84,11 +84,11 @@ static unsigned int zst_version; static void copy_state_data(unsigned char *buffer, void (*copy_func)(unsigned char **, void *, size_t), bool read) { copy_snes_data(&buffer, copy_func); - + //WRAM (128k), VRAM (64k) copy_func(&buffer, wramdata, 8192*16); copy_func(&buffer, vram, 4096*16); - + if (spcon) { copy_spc_data(&buffer, copy_func); @@ -97,18 +97,18 @@ static void copy_state_data(unsigned char *buffer, void (*copy_func)(unsigned ch copy_func(&buffer, &echoon0, PHdspsave2); } } - + if (C4Enable) - { + { copy_func(&buffer, C4Ram, 2048*4); } - + if (SFXEnable) { copy_func(&buffer, sfxramdata, 8192*16); copy_func(&buffer, &SfxR0, PHnum2writesfxreg); } - + if (SA1Enable) { copy_func(&buffer, &SA1Mode, PHnum2writesa1reg); @@ -120,7 +120,7 @@ static void copy_state_data(unsigned char *buffer, void (*copy_func)(unsigned ch copy_func(&buffer, &sa1dmaptr, 2*4); } } - + if (DSP1Type && !loading_old_state) { copy_func(&buffer, &DSP1COp, 70+128); @@ -142,9 +142,9 @@ static void copy_state_data(unsigned char *buffer, void (*copy_func)(unsigned ch } if (SETAEnable) - { + { copy_func(&buffer, setaramdata, 256*16); - + //Todo: copy the SetaCmdEnable? For completeness we should do it //but currently we ignore it anyway. } @@ -154,16 +154,16 @@ static void copy_state_data(unsigned char *buffer, void (*copy_func)(unsigned ch copy_func(&buffer, romdata+0x510000, 65536); copy_func(&buffer, &SPCMultA, PHnum2writespc7110reg); } - + if (!loading_old_state) { copy_extra_data(&buffer, copy_func); - + if (!loading_state_no_sram) { copy_func(&buffer, sram, ramsize); } - + if (buffer) //Not to a file, i.e. rewind { copy_func(&buffer, &tempesi, 4); @@ -226,7 +226,7 @@ void RestoreCVFrame() if (RewindPos != RewindEarliestPos) { unsigned char *RewindBufferPos; - + if (!RewindPos) { RewindPos = RewindStates; @@ -236,12 +236,12 @@ void RestoreCVFrame() RewindBufferPos = StateBackup + RewindPos*rewind_state_size; printf("Restoring rewind in slot #%u\n", RewindPos); copy_state_data(RewindBufferPos, memcpyrinc, true); - + //Clear Cache Check memset(vidmemch2, 1, sizeof(vidmemch2)); memset(vidmemch4, 1, sizeof(vidmemch4)); memset(vidmemch8, 1, sizeof(vidmemch8)); - + RewindTimer = 60*3; } } @@ -262,7 +262,7 @@ void MultipleFrameBack(unsigned int i) { break; } - } + } } void SetupRewindBuffer() @@ -296,7 +296,7 @@ void BackupCVFrame() } RewindTimer = ActualRewindFrames; } - + void RestoreCVFrame() { unsigned char *RewindBufferPos = StateBackup + PBackupPos*rewind_state_size; @@ -305,7 +305,7 @@ void RestoreCVFrame() if (MovieProcessing) { zmv_rewind_load(PBackupPos, MovieProcessing == 1 ? true : false); - } + } RewindTimer = ActualRewindFrames; } @@ -317,9 +317,9 @@ void SetupRewindBuffer() RewindStates = 16; StateBackup = 0; StateBackup = (unsigned char *)malloc(rewind_state_size*RewindStates); - if (!StateBackup) - { - asm_call(outofmemory); + if (!StateBackup) + { + asm_call(outofmemory); } } @@ -493,7 +493,7 @@ void repackfunct() if (block && 0xC0) { Voice0Status = Voice1Status = Voice2Status = Voice3Status = 0; - Voice4Status = Voice5Status = Voice6Status = Voice7Status = 0; + Voice4Status = Voice5Status = Voice6Status = Voice7Status = 0; } NoiseInc = (((NoiseSpeeds[(block & 0x1F)] * dspPAdj) >> 17) & 0xFFFFFFFF); @@ -656,7 +656,7 @@ static const char zst_header_old[] = "ZSNES Save State File V0.6\x1a\x3c"; static const char zst_header_cur[] = "ZSNES Save State File V143\x1a\x8f"; void calculate_state_sizes() -{ +{ state_size = 0; zst_version = 143; copy_state_data(0, state_size_tally, false); @@ -691,12 +691,12 @@ void zst_save(FILE *fp, bool Thumbnail) SfxRomBuffer -= SfxCROM; SfxLastRamAdr -= SfxRAMMem; } - + if (SA1Enable) { SaveSA1(); //Convert SA-1 stuff to standard, non displacement format } - + fhandle = fp; //Set global file handle copy_state_data(0, write_save_state_data, false); @@ -710,20 +710,20 @@ void zst_save(FILE *fp, bool Thumbnail) { RestoreSA1(); //Convert back SA-1 stuff } - + if (Thumbnail) { CapturePicture(); fwrite(PrevPicture, 1, 64*56*sizeof(unsigned short), fp); } - + ResetOffset(); ResetState(); } void statesaver() { - //Save State code + //Save State code #ifdef __LINUX__ SRAMChdir(); #endif @@ -734,11 +734,11 @@ void statesaver() if (mzt_save(fnamest+1, (cbitmode && !NoPictureSave) ? true : false, false)) { Msgptr = "RR STATE SAVED."; - MessageOn = MsgCount; + MessageOn = MsgCount; } return; - } - + } + //'Auto increment savestate slot' code if (AutoIncSaveSlot) { @@ -763,13 +763,13 @@ void statesaver() } clim(); - + if ((fhandle = fopen(fnamest+1,"wb"))) - { + { zst_save(fhandle, (cbitmode && !NoPictureSave) ? true : false); - + fclose(fhandle); - + //Display message on the screen, 'STATE X SAVED.' if (fnamest[statefileloc] == 't') { @@ -782,7 +782,7 @@ void statesaver() Msgptr = txtsavemsg; MessageOn = MsgCount; - } + } else { //Display message on the screen, 'UNABLE TO SAVE.' @@ -913,9 +913,9 @@ static void read_save_state_data(unsigned char **dest, void *data, size_t len) bool zst_load(FILE *fp) { - char zst_header_check[sizeof(zst_header_cur)-1]; + char zst_header_check[sizeof(zst_header_cur)-1]; zst_version = 0; - + Totalbyteloaded += fread(zst_header_check, 1, sizeof(zst_header_check), fp); if (!memcmp(zst_header_check, zst_header_cur, sizeof(zst_header_check)-2)) { @@ -925,14 +925,14 @@ bool zst_load(FILE *fp) { zst_version = 60; //v0.60 - v1.42 } // the -2 means we only check the text - trust me, that's ok - + if (!zst_version) { return(false); } //Pre v0.60 saves are no longer loaded - + load_save_size = 0; fhandle = fp; //Set global file handle copy_state_data(0, read_save_state_data, true); Totalbyteloaded += load_save_size; - + if (SFXEnable) { SfxCPB = SfxMemTable[(SfxPBR & 0xFF)]; @@ -949,10 +949,10 @@ bool zst_load(FILE *fp) All UpdateBanks() seems to do is break Oshaberi Parodius... The C port is still present, just commented out */ - //UpdateBanks(); + //UpdateBanks(); SA1UpdateDPageC(); } - + if (SDD1Enable) { UpdateBanksSDD1(); @@ -969,10 +969,29 @@ bool zst_load(FILE *fp) ResetOffset(); ResetState(); procexecloop(); - + return(true); } +void zst_sram_load(FILE *fp) +{ + fseek(fp, sizeof(zst_header_cur)-1 + PH65816regsize + 199635, SEEK_CUR); + if (spcon) { fseek(fp, PHspcsave + PHdspsave + sizeof(DSPMem), SEEK_CUR); } + if (C4Enable) { fseek(fp, 8192, SEEK_CUR); } + if (SFXEnable) { fseek(fp, PHnum2writesfxreg + 131072, SEEK_CUR); } + if (SA1Enable) + { + fseek(fp, PHnum2writesa1reg, SEEK_CUR); + fread(SA1RAMArea, 1, 131072, fp); // SA-1 sram + fseek(fp, 15, SEEK_CUR); + } + if (DSP1Type) { fseek(fp, 2874, SEEK_CUR); } + if (SETAEnable) { fread(setaramdata, 1, 4096, fp); } // SETA sram + if (SPC7110Enable) { fseek(fp, PHnum2writespc7110reg + 65536, SEEK_CUR); } + fseek(fp, 227, SEEK_CUR); + if (ramsize) { fread(sram, 1, ramsize, fp); } // normal sram +} + void stateloader (unsigned char *statename, unsigned char keycheck, unsigned char xfercheck) { #ifdef __LINUX__ @@ -982,7 +1001,7 @@ void stateloader (unsigned char *statename, unsigned char keycheck, unsigned cha if (keycheck) { unsigned char statevalue; - + pressed[1] = 0; pressed[KeyLoadState] = 2; multchange = 1; @@ -1001,19 +1020,19 @@ void stateloader (unsigned char *statename, unsigned char keycheck, unsigned cha txtconvmsg[6] = statevalue; txtnfndmsg[21] = statevalue; } - + switch (MovieProcessing) { bool mzt_load(char *, bool); - + case 1: if (mzt_load(statename, true)) { Msgptr = "RR STATE LOADED."; - MessageOn = MsgCount; + MessageOn = MsgCount; } return; - + case 2: if (mzt_load(statename, false)) { @@ -1021,15 +1040,15 @@ void stateloader (unsigned char *statename, unsigned char keycheck, unsigned cha MessageOn = MsgCount; } return; - } - + } + clim(); - + //Actual state loading code if ((fhandle = fopen(statename,"rb")) != NULL) { if (xfercheck) { Totalbyteloaded = 0; } - + if (zst_load(fhandle)) { Msgptr = txtloadmsg; // 'STATE X LOADED.' @@ -1038,14 +1057,14 @@ void stateloader (unsigned char *statename, unsigned char keycheck, unsigned cha { Msgptr = txtconvmsg; // 'STATE X TOO OLD.' } - + fclose(fhandle); } else { Msgptr = txtnfndmsg; // 'UNABLE TO LOAD STATE X.' } - + stim(); if (keycheck)