/********************************************************************************** Snes9x - Portable Super Nintendo Entertainment System (TM) emulator. (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com), Jerremy Koot (jkoot@snes9x.com) (c) Copyright 2002 - 2004 Matthew Kendora (c) Copyright 2002 - 2005 Peter Bortas (peter@bortas.org) (c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/) (c) Copyright 2001 - 2006 John Weidman (jweidman@slip.net) (c) Copyright 2002 - 2006 funkyass (funkyass@spam.shaw.ca), Kris Bleakley (codeviolation@hotmail.com) (c) Copyright 2002 - 2007 Brad Jorsch (anomie@users.sourceforge.net), Nach (n-a-c-h@users.sourceforge.net), zones (kasumitokoduck@yahoo.com) (c) Copyright 2006 - 2007 nitsuja BS-X C emulator code (c) Copyright 2005 - 2006 Dreamer Nom, zones C4 x86 assembler and some C emulation code (c) Copyright 2000 - 2003 _Demo_ (_demo_@zsnes.com), Nach, zsKnight (zsknight@zsnes.com) C4 C++ code (c) Copyright 2003 - 2006 Brad Jorsch, Nach DSP-1 emulator code (c) Copyright 1998 - 2006 _Demo_, Andreas Naive (andreasnaive@gmail.com) Gary Henderson, Ivar (ivar@snes9x.com), John Weidman, Kris Bleakley, Matthew Kendora, Nach, neviksti (neviksti@hotmail.com) DSP-2 emulator code (c) Copyright 2003 John Weidman, Kris Bleakley, Lord Nightmare (lord_nightmare@users.sourceforge.net), Matthew Kendora, neviksti DSP-3 emulator code (c) Copyright 2003 - 2006 John Weidman, Kris Bleakley, Lancer, z80 gaiden DSP-4 emulator code (c) Copyright 2004 - 2006 Dreamer Nom, John Weidman, Kris Bleakley, Nach, z80 gaiden OBC1 emulator code (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com), Kris Bleakley, Ported from x86 assembler to C by sanmaiwashi SPC7110 and RTC C++ emulator code (c) Copyright 2002 Matthew Kendora with research by zsKnight, John Weidman, Dark Force S-DD1 C emulator code (c) Copyright 2003 Brad Jorsch with research by Andreas Naive, John Weidman S-RTC C emulator code (c) Copyright 2001-2006 byuu, John Weidman ST010 C++ emulator code (c) Copyright 2003 Feather, John Weidman, Kris Bleakley, Matthew Kendora Super FX x86 assembler emulator code (c) Copyright 1998 - 2003 _Demo_, pagefault, zsKnight, Super FX C emulator code (c) Copyright 1997 - 1999 Ivar, Gary Henderson, John Weidman Sound DSP emulator code is derived from SNEeSe and OpenSPC: (c) Copyright 1998 - 2003 Brad Martin (c) Copyright 1998 - 2006 Charles Bilyue' SH assembler code partly based on x86 assembler code (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) 2xSaI filter (c) Copyright 1999 - 2001 Derek Liauw Kie Fa HQ2x, HQ3x, HQ4x filters (c) Copyright 2003 Maxim Stepin (maxim@hiend3d.com) Win32 GUI code (c) Copyright 2003 - 2006 blip, funkyass, Matthew Kendora, Nach, nitsuja Mac OS GUI code (c) Copyright 1998 - 2001 John Stiles (c) Copyright 2001 - 2007 zones Specific ports contains the works of other authors. See headers in individual files. Snes9x homepage: http://www.snes9x.com Permission to use, copy, modify and/or distribute Snes9x in both binary and source form, for non-commercial purposes, is hereby granted without fee, providing that this license information and copyright notice appear with all copies and any derived work. This software is provided 'as-is', without any express or implied warranty. In no event shall the authors be held liable for any damages arising from the use of this software or it's derivatives. Snes9x is freeware for PERSONAL USE only. Commercial users should seek permission of the copyright holders first. Commercial use includes, but is not limited to, charging money for Snes9x or software derived from Snes9x, including Snes9x or derivatives in commercial game bundles, and/or using Snes9x as a promotion for your commercial product. The copyright holders request that bug fixes and improvements to the code should be forwarded to them so everyone can benefit from the modifications in future versions. Super NES and Super Nintendo Entertainment System are trademarks of Nintendo Co., Limited and its subsidiary companies. **********************************************************************************/ // Input recording/playback code // (c) Copyright 2004 blip #include #ifdef HAVE_STRINGS_H #include #endif #include #include #include #if defined(__unix) || defined(__linux) || defined(__sun) || defined(__DJGPP) || defined(__MACOSX__) #include #include #include #endif #include #ifdef __WIN32__ #include #ifndef W_OK #define W_OK 2 #endif #define ftruncate chsize #endif #include "movie.h" #include "snes9x.h" #include "memmap.h" #include "cpuexec.h" #include "snapshot.h" #include "controls.h" #include "language.h" #ifdef NETPLAY_SUPPORT #include "netplay.h" #endif #define SMV_MAGIC 0x1a564d53 // SMV0x1a #define SMV_VERSION 4 #define SMV_HEADER_SIZE 64 #define SMV_EXTRAROMINFO_SIZE (3 + sizeof(uint32) + 23) #define CONTROLLER_DATA_SIZE 2 #define BUFFER_GROWTH_SIZE 4096 #define PERIPHERAL_SUPPORT #ifdef PERIPHERAL_SUPPORT #define MOUSE_DATA_SIZE 5 #define SCOPE_DATA_SIZE 6 #define JUSTIFIER_DATA_SIZE 11 #endif // HACK: reduce movie size by not storing changes that can only affect polled input in the movie for these types, because currently no port sets these types to polling #define SKIPPED_POLLING_PORT_TYPE(x) ((x==CTL_NONE)||(x==CTL_JOYPAD)||(x==CTL_MP5)) #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif enum MovieState { MOVIE_STATE_NONE=0, MOVIE_STATE_PLAY, MOVIE_STATE_RECORD }; static struct SMovie { enum MovieState State; char Filename [_MAX_PATH]; FILE* File; uint32 SaveStateOffset; uint32 ControllerDataOffset; uint32 MovieId; uint32 CurrentFrame; uint32 MaxFrame; uint32 RerecordCount; uint8 ControllersMask; uint8 Opts; uint8 SyncFlags; bool8 ReadOnly; uint32 BytesPerSample; uint8* InputBuffer; uint32 InputBufferSize; uint8* InputBufferPtr; uint32 ROMCRC32; char ROMName [23]; uint32 CurrentSample; uint32 MaxSample; uint8 PortType[2]; int8 PortIDs[2][4]; bool8 RecordedThisSession; uint32 Version; } Movie; /* // For illustration: struct MovieFileHeader { uint32 magic; // SMV0x1a uint32 version; // for Snes9x 1.51 this must be 4. In Snes9x 1.43 it was 1. uint32 uid; // used to match savestates to a particular movie... this is also the date/time of first recording uint32 rerecord_count; uint32 length_frames; uint8 flags[4]; // (ControllersMask, Opts, Reserved, SyncFlags) uint32 offset_to_savestate; // pointer to embedded savestate or SRAM uint32 offset_to_controller_data; // pointer to controller data uint32 length_samples; // number of recorded input samples, may be more than length_frames for peripheral-using games uint8 PortType[2]; // for each controller port, 0=NONE, 1=JOYPAD, 2=MOUSE, 3=SUPERSCOPE, 4=JUSTIFIER, 5=MULTITAP int8 PortIDs[2][4]; // for each port, then for each possible controller in the port, the controller ID number counting up from 0, or -1 if unplugged uint8 reserved[18]; }; // sizeof(MovieFileHeader) == 64 // after the header comes extra metadata, i.e. author info // sizeof(metadata) = (offset_to_savestate - sizeof(MovieFileHeader)) - sizeof(ExtraRomInfo); // that should be an even number of bytes because the author info consists of 2-byte characters // after the metadata comes extra info about the ROM used for recording struct ExtraRomInfo { uint8 reserved[3]; uint32 romCRC32; uint8 romName[23]; }; // sizeof(ExtraRomInfo) == 30 // after that comes the savestate or SRAM data (depending on the Opts flag) // sizeof(SaveData) <= offset_to_controller_data - offset_to_savestate // after that comes the controller data // sizeof(ControllerData) == length_samples * sizeof(InputSample) // sizeof(InputSample) == 2*(sum of bits in ControllersMask) + 5*num_mouse_ports + 6*num_superscope_ports + 11*num_justifier_ports */ // some state for peripherals etc. (playback) #ifdef PERIPHERAL_SUPPORT uint8 prevPortType[2]; int8 prevPortIDs[2][4]; bool8 prevMouseMaster; bool8 prevSuperScopeMaster; bool8 prevJustifierMaster; bool8 prevMultiPlayer5Master; #endif bool8 prevForcePal, prevPal, prevForceNTSC, delayedPrevRestore=false; bool8 prevUpAndDown, prevSoundEnvelopeHeightReading, prevFakeMuteFix, prevSoundSync, prevCPUShutdown; static int bytes_per_sample() { int i; int num_controllers; num_controllers=0; for(i=0; i<8; ++i) { if(Movie.ControllersMask & (1<>8)&0xff); ptr[2]=(uint8)((v>>16)&0xff); ptr[3]=(uint8)((v>>24)&0xff); ptr += 4; } void Write16(uint16 v, uint8*& ptr) { ptr[0]=(uint8)(v&0xff); ptr[1]=(uint8)((v>>8)&0xff); ptr += 2; } static inline void Write8(uint8 v, uint8*& ptr) { ptr[0]=(uint8)(v); ptr++; } static int read_movie_header(FILE* fd, SMovie* movie) { uint8 header[SMV_HEADER_SIZE]; if(fread(header, 1, SMV_HEADER_SIZE, fd) != SMV_HEADER_SIZE) return WRONG_FORMAT; const uint8* ptr=header; uint32 magic=Read32(ptr); if(magic!=SMV_MAGIC) return WRONG_FORMAT; uint32 version=Read32(ptr); if(version>SMV_VERSION) return WRONG_VERSION; // we can still get this basic info from v1 movies movie->MovieId=Read32(ptr); movie->RerecordCount=Read32(ptr); movie->MaxFrame=Read32(ptr); movie->Version=version; int i, p, j; for(j = 0; j < 2; j++) { if((j == 0 && version != 3) || (j == 1 && version == 3)) { movie->ControllersMask=Read8(ptr); movie->Opts=Read8(ptr); Read8(ptr); // reserved byte movie->SyncFlags=Read8(ptr); movie->SaveStateOffset=Read32(ptr); movie->ControllerDataOffset=Read32(ptr); } // not part of original v1 SMV format: if((j == 1 && version > 3) || (j == 0 && version == 3)) { movie->MaxSample=Read32(ptr); movie->PortType[0]=Read8(ptr); movie->PortType[1]=Read8(ptr); for(p=0;p<2;p++) for(i=0;i<4;i++) movie->PortIDs[p][i]=Read8(ptr); } } if(version<3) return WRONG_VERSION; // needs to be at least 1 sample per frame. So, assume that to make hex editing easier, at least for non-peripheral-using movies. if(movie->MaxSample < movie->MaxFrame) movie->MaxSample = movie->MaxFrame; ptr += 18; // reserved bytes assert(ptr-header==SMV_HEADER_SIZE); return SUCCESS; } static int read_movie_extrarominfo(FILE* fd, SMovie* movie) { if((movie->SyncFlags & MOVIE_SYNC_HASROMINFO) != 0) { fseek(fd, movie->SaveStateOffset - SMV_EXTRAROMINFO_SIZE, SEEK_SET); uint8 extraRomInfo[SMV_EXTRAROMINFO_SIZE]; if(fread(extraRomInfo, 1, SMV_EXTRAROMINFO_SIZE, fd) != SMV_EXTRAROMINFO_SIZE) return WRONG_FORMAT; const uint8* ptr=extraRomInfo; ptr ++; // zero byte ptr ++; // zero byte ptr ++; // zero byte movie->ROMCRC32=Read32(ptr); strncpy(movie->ROMName,(const char*)ptr,23); ptr += 23; } else { movie->ROMCRC32=Memory.ROMCRC32; strncpy(movie->ROMName,(const char*)Memory.ROMName,23); } return SUCCESS; } static void write_movie_header(FILE* fd, const SMovie* movie) { uint8 header[SMV_HEADER_SIZE]; uint8* ptr=header; Write32(SMV_MAGIC, ptr); Write32(SMV_VERSION, ptr); Write32(movie->MovieId, ptr); Write32(movie->RerecordCount, ptr); Write32(movie->MaxFrame, ptr); Write8(movie->ControllersMask, ptr); Write8(movie->Opts, ptr); Write8(0, ptr); // reserved byte Write8(movie->SyncFlags, ptr); Write32(movie->SaveStateOffset, ptr); Write32(movie->ControllerDataOffset, ptr); // not part of original v1 SMV format: Write32(movie->MaxSample, ptr); Write8(movie->PortType[0], ptr); Write8(movie->PortType[1], ptr); int i, p; for(p=0;p<2;p++) for(i=0;i<4;i++) Write8(movie->PortIDs[p][i], ptr); // 18 reserved bytes, could be anything, ignored by this version when reading for(i=0;i<18-4;i++) Write8(0, ptr); Write32(movie->RerecordCount/2, ptr); // why not... assert(ptr-header==SMV_HEADER_SIZE); fwrite(header, 1, SMV_HEADER_SIZE, fd); assert(!ferror(fd)); } static void write_movie_extrarominfo(FILE* fd, const SMovie* movie) { if((movie->SyncFlags & MOVIE_SYNC_HASROMINFO) != 0) // should be true... { uint8 extraRomInfo [SMV_EXTRAROMINFO_SIZE]; uint8* ptr = extraRomInfo; *ptr++=0; // zero byte *ptr++=0; // zero byte *ptr++=0; // zero byte Write32(movie->ROMCRC32, ptr); strncpy((char*)ptr,movie->ROMName,23); ptr += 23; fwrite(extraRomInfo, 1, SMV_EXTRAROMINFO_SIZE, fd); assert(!ferror(fd)); } } static void flush_movie() { if(!Movie.File) return; fseek(Movie.File, 0, SEEK_SET); write_movie_header(Movie.File, &Movie); fseek(Movie.File, Movie.ControllerDataOffset, SEEK_SET); fwrite(Movie.InputBuffer, 1, Movie.BytesPerSample*(Movie.MaxSample+1), Movie.File); assert(!ferror(Movie.File)); } static void store_previous_settings() { #ifdef PERIPHERAL_SUPPORT for(int i=0; i<2; i++){ enum controllers pt; S9xGetController(i, &pt, &prevPortIDs[i][0],&prevPortIDs[i][1],&prevPortIDs[i][2],&prevPortIDs[i][3]); prevPortType[i] = (uint8) pt; } prevMouseMaster = Settings.MouseMaster; prevSuperScopeMaster = Settings.SuperScopeMaster; prevJustifierMaster = Settings.JustifierMaster; prevMultiPlayer5Master = Settings.MultiPlayer5Master; #endif if(!delayedPrevRestore) { prevPal = Settings.PAL; prevCPUShutdown = Settings.ShutdownMaster; } delayedPrevRestore = false; prevForcePal = Settings.ForcePAL; prevForceNTSC = Settings.ForceNTSC; prevUpAndDown = Settings.UpAndDown; prevSoundEnvelopeHeightReading = Settings.SoundEnvelopeHeightReading; prevFakeMuteFix = Settings.FakeMuteFix; prevSoundSync = Settings.SoundSync; } static void restore_previous_settings() { #ifdef PERIPHERAL_SUPPORT Settings.MouseMaster = prevMouseMaster; Settings.SuperScopeMaster = prevSuperScopeMaster; Settings.JustifierMaster = prevJustifierMaster; Settings.MultiPlayer5Master = prevMultiPlayer5Master; S9xSetController(0, (enum controllers)prevPortType[0], prevPortIDs[0][0],prevPortIDs[0][1],prevPortIDs[0][2],prevPortIDs[0][3]); S9xSetController(1, (enum controllers)prevPortType[1], prevPortIDs[1][0],prevPortIDs[1][1],prevPortIDs[1][2],prevPortIDs[1][3]); #endif Settings.ForcePAL = prevForcePal; Settings.ForceNTSC = prevForceNTSC; Settings.SoundEnvelopeHeightReading = prevSoundEnvelopeHeightReading; Settings.FakeMuteFix = prevFakeMuteFix; // Settings.PAL = prevPal; // changing this after the movie while it's still emulating would be bad // Settings.ShutdownMaster = prevCPUShutdown; // changing this after the movie while it's still emulating would be bad delayedPrevRestore = true; // wait to change the above 2 settings until later // Settings.UpAndDown = prevUpAndDown; // doesn't actually affect synchronization, so leave the setting alone; the port can change it if it wants // Settings.SoundSync = prevSoundSync; // doesn't seem to affect synchronization, so leave the setting alone; the port can change it if it wants } static void store_movie_settings() { #ifdef PERIPHERAL_SUPPORT for(int i=0; i<2; i++){ enum controllers pt; S9xGetController(i, &pt, &Movie.PortIDs[i][0],&Movie.PortIDs[i][1],&Movie.PortIDs[i][2],&Movie.PortIDs[i][3]); Movie.PortType[i] = (uint8) pt; } if(!Movie.PortType[0] && !Movie.PortType[1]) fprintf(stderr, "WARNING: S9xMovieCreate: no controller ports set\n"); #endif if(Settings.PAL) Movie.Opts |= MOVIE_OPT_PAL; else Movie.Opts &= ~MOVIE_OPT_PAL; Movie.SyncFlags = MOVIE_SYNC_DATA_EXISTS | MOVIE_SYNC_HASROMINFO; if(Settings.UpAndDown) Movie.SyncFlags |= MOVIE_SYNC_LEFTRIGHT; if(Settings.SoundEnvelopeHeightReading) Movie.SyncFlags |= MOVIE_SYNC_VOLUMEENVX; if(Settings.FakeMuteFix || !Settings.APUEnabled) Movie.SyncFlags |= MOVIE_SYNC_FAKEMUTE; if(Settings.SoundSync) Movie.SyncFlags |= MOVIE_SYNC_SYNCSOUND; if(!Settings.ShutdownMaster) Movie.SyncFlags |= MOVIE_SYNC_NOCPUSHUTDOWN; } static void restore_movie_settings() { #ifdef PERIPHERAL_SUPPORT Settings.MouseMaster = (Movie.PortType[0] == CTL_MOUSE || Movie.PortType[1] == CTL_MOUSE); Settings.SuperScopeMaster = (Movie.PortType[0] == CTL_SUPERSCOPE || Movie.PortType[1] == CTL_SUPERSCOPE); Settings.JustifierMaster = (Movie.PortType[0] == CTL_JUSTIFIER || Movie.PortType[1] == CTL_JUSTIFIER); Settings.MultiPlayer5Master = (Movie.PortType[0] == CTL_MP5 || Movie.PortType[1] == CTL_MP5); S9xSetController(0, (enum controllers)Movie.PortType[0], Movie.PortIDs[0][0],Movie.PortIDs[0][1],Movie.PortIDs[0][2],Movie.PortIDs[0][3]); S9xSetController(1, (enum controllers)Movie.PortType[1], Movie.PortIDs[1][0],Movie.PortIDs[1][1],Movie.PortIDs[1][2],Movie.PortIDs[1][3]); #endif if(Movie.Opts & MOVIE_OPT_PAL) { Settings.ForcePAL = Settings.PAL = TRUE; // OK to change while starting playing a movie because either we are re-loading the ROM or we are entering a state that already had this setting set Settings.ForceNTSC = FALSE; } else { Settings.ForcePAL = Settings.PAL = FALSE; // OK to change while starting playing a movie because either we are re-loading the ROM or we are entering a state that already had this setting set Settings.ForceNTSC = TRUE; } Settings.SoundEnvelopeHeightReading = (Movie.SyncFlags & MOVIE_SYNC_VOLUMEENVX) ? TRUE : FALSE; Settings.FakeMuteFix = (Movie.SyncFlags & MOVIE_SYNC_FAKEMUTE) ? TRUE : FALSE; Settings.ShutdownMaster = (Movie.SyncFlags & MOVIE_SYNC_NOCPUSHUTDOWN) ? FALSE : TRUE; // OK to change while starting playing a movie because either we are re-loading the ROM or we are entering a state that already had this setting set // Settings.UpAndDown = (Movie.SyncFlags & MOVIE_SYNC_LEFTRIGHT) ? TRUE : FALSE; // doesn't actually affect synchronization, so leave the setting alone; the port can change it if it wants // Settings.SoundSync = (Movie.SyncFlags & MOVIE_SYNC_SYNCSOUND) ? TRUE : FALSE; // doesn't seem to affect synchronization, so leave the setting alone; the port can change it if it wants } // file must still be open for this to work static void truncate_movie() { if(!Settings.MovieTruncate || !Movie.File) return; assert(Movie.SaveStateOffset <= Movie.ControllerDataOffset); if(Movie.SaveStateOffset > Movie.ControllerDataOffset) return; const unsigned long length = Movie.ControllerDataOffset + Movie.BytesPerSample * (Movie.MaxSample + 1); ftruncate(fileno(Movie.File), length); } static void change_state(MovieState new_state) { if(new_state==Movie.State) return; if(Movie.State==MOVIE_STATE_RECORD) { flush_movie(); } if(new_state==MOVIE_STATE_NONE) { // truncate movie to MaxSample length if Settings.MovieTruncate is true truncate_movie(); fclose(Movie.File); Movie.File=NULL; if(S9xMoviePlaying() || S9xMovieRecording()) // even if recording, it could have been switched to from playback { restore_previous_settings(); } } Movie.State=new_state; } static void reserve_buffer_space(uint32 space_needed) { if(space_needed > Movie.InputBufferSize) { uint32 ptr_offset = Movie.InputBufferPtr - Movie.InputBuffer; uint32 alloc_chunks = space_needed / BUFFER_GROWTH_SIZE; Movie.InputBufferSize = BUFFER_GROWTH_SIZE * (alloc_chunks+1); Movie.InputBuffer = (uint8*)realloc(Movie.InputBuffer, Movie.InputBufferSize); Movie.InputBufferPtr = Movie.InputBuffer + ptr_offset; } } /* accessors into controls.cpp static variables */ uint16 MovieGetJoypad(int i); void MovieSetJoypad(int i, uint16 buttons); #ifdef PERIPHERAL_SUPPORT bool MovieGetMouse(int i, uint8 out [MOUSE_DATA_SIZE]); void MovieSetMouse(int i, const uint8 in [MOUSE_DATA_SIZE], bool inPolling); bool MovieGetScope(int i, uint8 out [SCOPE_DATA_SIZE]); void MovieSetScope(int i, const uint8 in [SCOPE_DATA_SIZE]); bool MovieGetJustifier(int i, uint8 out [JUSTIFIER_DATA_SIZE]); void MovieSetJustifier(int i, const uint8 in [JUSTIFIER_DATA_SIZE]); #endif static void read_frame_controller_data(bool addFrame) { int i; // one sample of all 1 bits = reset code // (the SNES controller doesn't have enough buttons to possibly generate this sequence) // (a single bit indicator was not used, to avoid having to special-case peripheral recording here) if(Movie.InputBufferPtr[0] == 0xFF) { bool reset = true; for(i=1; i<(int)Movie.BytesPerSample; i++) { if(Movie.InputBufferPtr[i] != 0xFF) { reset = false; break; } } if(reset) { Movie.InputBufferPtr += Movie.BytesPerSample; S9xSoftReset(); return; } } for(i=0; i<8; ++i) { if(Movie.ControllersMask & (1<MOVIE_MAX_METADATA) { metadata_length=MOVIE_MAX_METADATA; } Movie.MovieId=(uint32)time(NULL); Movie.RerecordCount=0; Movie.MaxFrame=0; Movie.MaxSample=0; Movie.SaveStateOffset=SMV_HEADER_SIZE+(sizeof(uint16)*metadata_length) + SMV_EXTRAROMINFO_SIZE; Movie.ControllerDataOffset=0; Movie.ControllersMask=controllers_mask; Movie.Opts=opts; Movie.SyncFlags=MOVIE_SYNC_DATA_EXISTS|MOVIE_SYNC_HASROMINFO; // store previous, in case we switch to playback later store_previous_settings(); // store settings in movie store_movie_settings(); // extra rom info Movie.ROMCRC32 = Memory.ROMCRC32; strncpy(Movie.ROMName, Memory.ROMName, 23); write_movie_header(fd, &Movie); // convert wchar_t metadata string/array to a uint16 array if(metadata_length>0) { uint8 meta_buf[MOVIE_MAX_METADATA * sizeof(uint16)]; for(int i=0; i>8)&0xff); } fwrite(meta_buf, sizeof(uint16), metadata_length, fd); assert(!ferror(fd)); } write_movie_extrarominfo(fd, &Movie); // write snapshot fn=dup(fileno(fd)); fclose(fd); // lseek(fn, Movie.SaveStateOffset, SEEK_SET); if(!(stream=REOPEN_STREAM(fn, "ab"))) return FILE_NOT_FOUND; if(opts & MOVIE_OPT_FROM_RESET) { if(!Memory.LoadLastROM()) S9xReset(); Memory.ClearSRAM(true); // clear non-saving SRAM S9xMovieResetControls(); // save only SRAM for a from-reset snapshot WRITE_STREAM(Memory.SRAM, 0x20000, stream); } else { S9xFreezeToStream(stream); } CLOSE_STREAM(stream); if(!(fd=fopen(movie_filename, "rb+"))) return FILE_NOT_FOUND; fseek(fd, 0, SEEK_END); Movie.ControllerDataOffset=(uint32)ftell(fd); // 16-byte align the controller input, for hex-editing friendliness if nothing else while(Movie.ControllerDataOffset % 16) { fputc(0xCC, fd); // arbitrary Movie.ControllerDataOffset++; } // write "baseline" controller data Movie.File=fd; Movie.BytesPerSample=bytes_per_sample(); Movie.InputBufferPtr=Movie.InputBuffer; write_frame_controller_data(); strncpy(Movie.Filename, movie_filename, _MAX_PATH); Movie.Filename[_MAX_PATH-1]='\0'; Movie.CurrentFrame=0; Movie.CurrentSample=0; Movie.ReadOnly=false; change_state(MOVIE_STATE_RECORD); Settings.Paused = wasPaused; Movie.RecordedThisSession = true; S9xUpdateFrameCounter(-1); S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_RECORD); return SUCCESS; } void S9xMovieUpdateOnReset () { switch(Movie.State) { case MOVIE_STATE_RECORD: { reserve_buffer_space((uint32)((Movie.InputBufferPtr+Movie.BytesPerSample)-Movie.InputBuffer)); memset(Movie.InputBufferPtr, 0xFF, Movie.BytesPerSample); Movie.InputBufferPtr += Movie.BytesPerSample; Movie.MaxSample = ++Movie.CurrentSample; Movie.MaxFrame = ++Movie.CurrentFrame; fwrite((Movie.InputBufferPtr - Movie.BytesPerSample), 1, Movie.BytesPerSample, Movie.File); assert(!ferror(Movie.File)); } break; default: break; } } void S9xMovieUpdate (bool addFrame) { movieUpdateStart: switch(Movie.State) { case MOVIE_STATE_PLAY: { if(Movie.CurrentFrame>=Movie.MaxFrame || Movie.CurrentSample>=Movie.MaxSample) { if(!Movie.RecordedThisSession) { // stop movie; it reached the end change_state(MOVIE_STATE_NONE); S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_END); return; } else { // if user has been recording this movie since the last time it started playing, // they probably don't want the movie to end now during playback, // so switch back to recording when it reaches the end change_state(MOVIE_STATE_RECORD); S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_RECORD); fseek(Movie.File, Movie.ControllerDataOffset+(Movie.BytesPerSample * (Movie.CurrentSample+1)), SEEK_SET); Settings.Paused = true; // also pause so it doesn't keep going unless they want it to goto movieUpdateStart; } } else { if(addFrame) S9xUpdateFrameCounter(); else if(SKIPPED_POLLING_PORT_TYPE(Movie.PortType[0]) && SKIPPED_POLLING_PORT_TYPE(Movie.PortType[1])) return; read_frame_controller_data(addFrame); ++Movie.CurrentSample; if(addFrame) ++Movie.CurrentFrame; } } break; case MOVIE_STATE_RECORD: { if(addFrame) S9xUpdateFrameCounter(); else if(SKIPPED_POLLING_PORT_TYPE(Movie.PortType[0]) && SKIPPED_POLLING_PORT_TYPE(Movie.PortType[1])) return; write_frame_controller_data(); Movie.MaxSample = ++Movie.CurrentSample; if(addFrame) Movie.MaxFrame = ++Movie.CurrentFrame; fwrite((Movie.InputBufferPtr - Movie.BytesPerSample), 1, Movie.BytesPerSample, Movie.File); assert(!ferror(Movie.File)); Movie.RecordedThisSession = true; } break; default: if(addFrame) S9xUpdateFrameCounter(); break; } } void S9xMovieStop (bool8 suppress_message) { if(Movie.State!=MOVIE_STATE_NONE) { change_state(MOVIE_STATE_NONE); if(!suppress_message) S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_STOP); } } int S9xMovieGetInfo (const char* filename, struct MovieInfo* info) { flush_movie(); FILE* fd; int result; SMovie local_movie; int metadata_length; memset(info, 0, sizeof(*info)); if(!(fd=fopen(filename, "rb"))) return FILE_NOT_FOUND; result = read_movie_header(fd, &local_movie); // we can still get this basic info from older, unsupported movies (v1 movies) info->TimeCreated=(time_t)local_movie.MovieId; info->RerecordCount=local_movie.RerecordCount; info->LengthFrames=local_movie.MaxFrame; info->Version=local_movie.Version; if(result!=SUCCESS) return result; info->LengthSamples=local_movie.MaxSample; info->PortType[0]=local_movie.PortType[0]; info->PortType[1]=local_movie.PortType[1]; info->Opts=local_movie.Opts; info->SyncFlags=local_movie.SyncFlags; info->ControllersMask=local_movie.ControllersMask; if(local_movie.SaveStateOffset > SMV_HEADER_SIZE) { uint8 meta_buf[MOVIE_MAX_METADATA * sizeof(uint16)]; int i; int curRomInfoSize = (local_movie.SyncFlags & MOVIE_SYNC_HASROMINFO) != 0 ? SMV_EXTRAROMINFO_SIZE : 0; metadata_length=((int)local_movie.SaveStateOffset-SMV_HEADER_SIZE-curRomInfoSize)/sizeof(uint16); metadata_length=(metadata_length>=MOVIE_MAX_METADATA) ? MOVIE_MAX_METADATA-1 : metadata_length; metadata_length=(int)fread(meta_buf, sizeof(uint16), metadata_length, fd); for(i=0; iMetadata[i]=(wchar_t)c; } info->Metadata[i]='\0'; } else { info->Metadata[0]='\0'; } read_movie_extrarominfo(fd, &local_movie); info->ROMCRC32=local_movie.ROMCRC32; strncpy(info->ROMName,local_movie.ROMName,23); fclose(fd); if(access(filename, W_OK)) info->ReadOnly=true; return SUCCESS; } bool8 S9xMovieActive () { return (Movie.State!=MOVIE_STATE_NONE); } bool8 S9xMoviePlaying () { return (Movie.State==MOVIE_STATE_PLAY); } bool8 S9xMovieRecording () { return (Movie.State==MOVIE_STATE_RECORD); } uint8 S9xMovieControllers () { return Movie.ControllersMask; } bool8 S9xMovieReadOnly () { if(!S9xMovieActive()) return false; return Movie.ReadOnly; } uint32 S9xMovieGetId () { if(!S9xMovieActive()) return 0; return Movie.MovieId; } uint32 S9xMovieGetLength () { if(!S9xMovieActive()) return 0; return Movie.MaxFrame; } uint32 S9xMovieGetFrameCounter () { if(!S9xMovieActive()) return 0; return Movie.CurrentFrame; } void S9xMovieToggleRecState() { Movie.ReadOnly=!Movie.ReadOnly; if (Movie.ReadOnly) S9xMessage(S9X_INFO, S9X_MOVIE_INFO, "Movie is now read-only."); else S9xMessage(S9X_INFO, S9X_MOVIE_INFO, "Movie is now read+write."); } void S9xMovieToggleFrameDisplay () { GFX.FrameDisplay = !GFX.FrameDisplay; // updating the frame counter string here won't work, because it may or may not be 1 too high now extern void S9xReRefresh(); S9xReRefresh(); } void S9xMovieFreeze (uint8** buf, uint32* size) { // sanity check if(!S9xMovieActive()) { return; } *buf = NULL; *size = 0; // compute size needed for the buffer uint32 size_needed = sizeof(Movie.MovieId) + sizeof(Movie.CurrentFrame) + sizeof(Movie.MaxFrame) + sizeof(Movie.CurrentSample) + sizeof(Movie.MaxSample); size_needed += (uint32)(Movie.BytesPerSample * (Movie.MaxSample+1)); *buf=new uint8[size_needed]; *size=size_needed; uint8* ptr = *buf; if(!ptr) { return; } Write32(Movie.MovieId, ptr); Write32(Movie.CurrentFrame, ptr); Write32(Movie.MaxFrame, ptr); Write32(Movie.CurrentSample, ptr); Write32(Movie.MaxSample, ptr); memcpy(ptr, Movie.InputBuffer, Movie.BytesPerSample * (Movie.MaxSample+1)); } int S9xMovieUnfreeze (const uint8* buf, uint32 size) { // sanity check if(!S9xMovieActive()) { return FILE_NOT_FOUND; } const uint8* ptr = buf; if(size < sizeof(Movie.MovieId) + sizeof(Movie.CurrentFrame) + sizeof(Movie.MaxFrame) + sizeof(Movie.CurrentSample) + sizeof(Movie.MaxSample) ) { return WRONG_FORMAT; } uint32 movie_id = Read32(ptr); uint32 current_frame = Read32(ptr); uint32 max_frame = Read32(ptr); uint32 current_sample = Read32(ptr); uint32 max_sample = Read32(ptr); uint32 space_needed = (Movie.BytesPerSample * (max_sample+1)); if(current_frame > max_frame || current_sample > max_sample || space_needed > size) { return WRONG_MOVIE_SNAPSHOT; } if(movie_id != Movie.MovieId) if(Settings.WrongMovieStateProtection) if(max_frame < Movie.MaxFrame || max_sample < Movie.MaxSample || memcmp(Movie.InputBuffer, ptr, space_needed)) return WRONG_MOVIE_SNAPSHOT; if(!Movie.ReadOnly) { // here, we are going to take the input data from the savestate // and make it the input data for the current movie, then continue // writing new input data at the currentframe pointer change_state(MOVIE_STATE_RECORD); // S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_RERECORD); Movie.CurrentFrame = current_frame; Movie.MaxFrame = max_frame; Movie.CurrentSample = current_sample; Movie.MaxSample = max_sample; ++Movie.RerecordCount; // when re-recording, update the sync info in the movie to the new settings as of the last re-record. store_movie_settings(); reserve_buffer_space(space_needed); memcpy(Movie.InputBuffer, ptr, space_needed); flush_movie(); fseek(Movie.File, Movie.ControllerDataOffset+(Movie.BytesPerSample * (Movie.CurrentSample+1)), SEEK_SET); } else { // here, we are going to keep the input data from the movie file // and simply rewind to the currentframe pointer // this will cause a desync if the savestate is not in sync // <-- NOT ANYMORE // with the on-disk recording data, but it's easily solved // by loading another savestate or playing the movie from the beginning // don't allow loading a state inconsistent with the current movie if(current_frame > Movie.MaxFrame || current_sample > Movie.MaxSample || memcmp(Movie.InputBuffer, ptr, space_needed)) { return SNAPSHOT_INCONSISTENT; } change_state(MOVIE_STATE_PLAY); // S9xMessage(S9X_INFO, S9X_MOVIE_INFO, MOVIE_INFO_REWIND); Movie.CurrentFrame = current_frame; Movie.CurrentSample = current_sample; } Movie.InputBufferPtr = Movie.InputBuffer + (Movie.BytesPerSample * (Movie.CurrentSample)); read_frame_controller_data(true); return SUCCESS; }