int S9xNetplayPush (void) { static int statusbar_state = FALSE; if (gui_config->netplay_activated && (!Settings.NetPlay || !NetPlay.Connected)) S9xNetplayDisconnect (); if (!Settings.NetPlay) return 0; if (NetPlay.PendingWait4Sync && !S9xNPWaitForHeartBeatDelay (100)) { S9xProcessEvents (FALSE); S9xSoundStop (); NetPlay.Paused = TRUE; if (statusbar_state == FALSE) { top_level->update_statusbar (); statusbar_state = TRUE; } return 1; } NetPlay.Paused = FALSE; if (statusbar_state) { top_level->update_statusbar (); statusbar_state = FALSE; } S9xSoundStart (); /* Save the joypad input */ for (int i = 0; i < 8; i++) { local_joypads[i] = MovieGetJoypad (i); MovieSetJoypad (i, joypads[i]); } if (NetPlay.PendingWait4Sync) { NetPlay.PendingWait4Sync = FALSE; NetPlay.FrameCount++; S9xNPStepJoypadHistory (); } return 0; }
extern "C" int SIStartWithROM(char* rom_filename) { // notify that we're running SI_EmulationIsRunning = 1; // frameskip settings reset SI_NextFrameTime = (timeval){0, 0}; SI_FrameTimeDebt = 0; SI_SleptLastFrame = 0; // ensure dirs exist mode_t dir_mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; mkdir(SI_SRAMPath, dir_mode); // unix init ZeroMemory(&Settings, sizeof(Settings)); Settings.MouseMaster = TRUE; Settings.SuperScopeMaster = TRUE; Settings.JustifierMaster = TRUE; Settings.MultiPlayer5Master = TRUE; Settings.FrameTimePAL = 20000; Settings.FrameTimeNTSC = 16667; Settings.SixteenBitSound = TRUE; Settings.Stereo = TRUE; //Settings.Stereo = FALSE; Settings.SoundPlaybackRate = 32000; //Settings.SoundPlaybackRate = 22050; //Settings.SoundInputRate = 32000; Settings.SoundInputRate = 32000; Settings.SoundSync = FALSE; Settings.SupportHiRes = TRUE; Settings.Transparency = TRUE; Settings.AutoDisplayMessages = TRUE; Settings.InitialInfoStringTimeout = 120; Settings.HDMATimingHack = 100; Settings.BlockInvalidVRAMAccessMaster = TRUE; Settings.StopEmulation = TRUE; Settings.WrongMovieStateProtection = TRUE; Settings.DumpStreamsMaxFrames = -1; Settings.StretchScreenshots = 1; Settings.SnapshotScreenshots = TRUE; if(SI_AutoFrameskip) Settings.SkipFrames = AUTO_FRAMERATE; else Settings.SkipFrames = SI_Frameskip; //Settings.SkipFrames = 1; Settings.TurboSkipFrames = 15; Settings.CartAName[0] = 0; Settings.CartBName[0] = 0; #ifdef NETPLAY_SUPPORT Settings.ServerName[0] = 0; #endif CPU.Flags = 0; /*S9xLoadConfigFiles(argv, argc); rom_filename = S9xParseArgs(argv, argc); make_snes9x_dirs();*/ if (!Memory.Init() || !S9xInitAPU()) { fprintf(stderr, "Snes9x: Memory allocation failure - not enough RAM/virtual memory available.\nExiting...\n"); Memory.Deinit(); S9xDeinitAPU(); exit(1); } int samplecount = Settings.SoundPlaybackRate/(Settings.PAL ? 50 : 60); int soundBufferSize = samplecount<<(1+(Settings.Stereo?1:0)); S9xInitSound(soundBufferSize, 0); S9xSetSoundMute(TRUE); S9xReset(); S9xUnmapAllControls(); S9xSetController(0, CTL_JOYPAD, 0, 0, 0, 0); //S9xSetController(1, CTL_JOYPAD, 1, 0, 0, 0); s9xcommand_t cmd; ASSIGN_BUTTONf(SIOS_X, "Joypad1 X"); ASSIGN_BUTTONf(SIOS_A, "Joypad1 A"); ASSIGN_BUTTONf(SIOS_B, "Joypad1 B"); ASSIGN_BUTTONf(SIOS_Y, "Joypad1 Y"); ASSIGN_BUTTONf(SIOS_L, "Joypad1 L"); ASSIGN_BUTTONf(SIOS_R, "Joypad1 R"); ASSIGN_BUTTONf(SIOS_SELECT, "Joypad1 Select"); ASSIGN_BUTTONf(SIOS_START, "Joypad1 Start"); ASSIGN_BUTTONf(SIOS_UP, "Joypad1 Up"); ASSIGN_BUTTONf(SIOS_DOWN, "Joypad1 Down"); ASSIGN_BUTTONf(SIOS_LEFT, "Joypad1 Left"); ASSIGN_BUTTONf(SIOS_RIGHT, "Joypad1 Right"); S9xReportControllers(); #ifdef GFX_MULTI_FORMAT S9xSetRenderPixelFormat(RGB565); #endif uint32 saved_flags = CPU.Flags; bool8 loaded = FALSE; if (Settings.Multi) { loaded = Memory.LoadMultiCart(Settings.CartAName, Settings.CartBName); if (!loaded) { char s1[PATH_MAX + 1], s2[PATH_MAX + 1]; char drive[_MAX_DRIVE + 1], dir[_MAX_DIR + 1], fname[_MAX_FNAME + 1], ext[_MAX_EXT + 1]; s1[0] = s2[0] = 0; if (Settings.CartAName[0]) { _splitpath(Settings.CartAName, drive, dir, fname, ext); snprintf(s1, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname); if (ext[0] && (strlen(s1) <= PATH_MAX - 1 - strlen(ext))) { strcat(s1, "."); strcat(s1, ext); } } if (Settings.CartBName[0]) { _splitpath(Settings.CartBName, drive, dir, fname, ext); snprintf(s2, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname); if (ext[0] && (strlen(s2) <= PATH_MAX - 1 - strlen(ext))) { strcat(s2, "."); strcat(s2, ext); } } loaded = Memory.LoadMultiCart(s1, s2); } } else if (rom_filename) { char rom_path[1024] = {0}; sprintf(rom_path,"%s%s%s",SI_DocumentsPath,DIR_SEPERATOR,rom_filename); loaded = Memory.LoadROM(rom_path); /*if (!loaded && rom_filename[0]) { char s[PATH_MAX + 1]; char drive[_MAX_DRIVE + 1], dir[_MAX_DIR + 1], fname[_MAX_FNAME + 1], ext[_MAX_EXT + 1]; _splitpath(rom_filename, drive, dir, fname, ext); snprintf(s, PATH_MAX + 1, "%s%s%s", S9xGetDirectory(ROM_DIR), SLASH_STR, fname); if (ext[0] && (strlen(s) <= PATH_MAX - 1 - strlen(ext))) { strcat(s, "."); strcat(s, ext); } loaded = Memory.LoadROM(s); }*/ } if (!loaded) { fprintf(stderr, "Error opening the ROM file.\n"); exit(1); } //NSRTControllerSetup(); //Memory.LoadSRAM(S9xGetFilename(".srm", SRAM_DIR)); SILoadSRAM(); //S9xLoadCheatFile(S9xGetFilename(".cht", CHEAT_DIR)); CPU.Flags = saved_flags; Settings.StopEmulation = FALSE; #ifdef DEBUGGER struct sigaction sa; sa.sa_handler = sigbrkhandler; #ifdef SA_RESTART sa.sa_flags = SA_RESTART; #else sa.sa_flags = 0; #endif sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); #endif GFX.Pitch = SNES_WIDTH*2; /*vrambuffer = (uint8*) malloc (GFX.Pitch * SNES_HEIGHT_EXTENDED*2); memset (vrambuffer, 0, GFX.Pitch * SNES_HEIGHT_EXTENDED*2); GFX.Screen = (uint16*)vrambuffer;*/ S9xGraphicsInit(); #ifdef NETPLAY_SUPPORT if (strlen(Settings.ServerName) == 0) { char *server = getenv("S9XSERVER"); if (server) { strncpy(Settings.ServerName, server, 127); Settings.ServerName[127] = 0; } } char *port = getenv("S9XPORT"); if (Settings.Port >= 0 && port) Settings.Port = atoi(port); else if (Settings.Port < 0) Settings.Port = -Settings.Port; if (Settings.NetPlay) { NetPlay.MaxFrameSkip = 10; if (!S9xNPConnectToServer(Settings.ServerName, Settings.Port, Memory.ROMName)) { fprintf(stderr, "Failed to connect to server %s on port %d.\n", Settings.ServerName, Settings.Port); S9xExit(); } fprintf(stderr, "Connected to server %s on port %d as player #%d playing %s.\n", Settings.ServerName, Settings.Port, NetPlay.Player, Memory.ROMName); } #endif // HACK: disabling SMV /*if (play_smv_filename) { uint32 flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG); if (S9xMovieOpen(play_smv_filename, TRUE) != SUCCESS) exit(1); CPU.Flags |= flags; } else if (record_smv_filename) { uint32 flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG); if (S9xMovieCreate(record_smv_filename, 0xFF, MOVIE_OPT_FROM_RESET, NULL, 0) != SUCCESS) exit(1); CPU.Flags |= flags; } else if (snapshot_filename) { uint32 flags = CPU.Flags & (DEBUG_MODE_FLAG | TRACE_FLAG); if (!S9xUnfreezeGame(snapshot_filename)) exit(1); CPU.Flags |= flags; }*/ //S9xGraphicsMode(); sprintf(String, "\"%s\" %s: %s", Memory.ROMName, TITLE, VERSION); //S9xSetTitle(String); #ifdef JOYSTICK_SUPPORT uint32 JoypadSkip = 0; #endif SILoadRunningStateForGameNamed(rom_filename); SI_EmulationPaused = 0; //if(SI_SoundOn) SIDemuteSound(soundBufferSize); S9xSetSoundMute(FALSE); #ifdef NETPLAY_SUPPORT bool8 NP_Activated = Settings.NetPlay; #endif while (1) { #ifdef NETPLAY_SUPPORT if (NP_Activated) { if (NetPlay.PendingWait4Sync && !S9xNPWaitForHeartBeatDelay(100)) { S9xProcessEvents(FALSE); continue; } for (int J = 0; J < 8; J++) old_joypads[J] = MovieGetJoypad(J); for (int J = 0; J < 8; J++) MovieSetJoypad(J, joypads[J]); if (NetPlay.Connected) { if (NetPlay.PendingWait4Sync) { NetPlay.PendingWait4Sync = FALSE; NetPlay.FrameCount++; S9xNPStepJoypadHistory(); } } else { fprintf(stderr, "Lost connection to server.\n"); S9xExit(); } } #endif #ifdef DEBUGGER if (!Settings.Paused || (CPU.Flags & (DEBUG_MODE_FLAG | SINGLE_STEP_FLAG))) #else if (!Settings.Paused && !SI_EmulationPaused) #endif { S9xMainLoop(); } #ifdef NETPLAY_SUPPORT if (NP_Activated) { for (int J = 0; J < 8; J++) MovieSetJoypad(J, old_joypads[J]); } #endif #ifdef DEBUGGER if(Settings.Paused || (CPU.Flags & DEBUG_MODE_FLAG)) #else if(Settings.Paused || SI_EmulationPaused) #endif S9xSetSoundMute(TRUE); #ifdef DEBUGGER if (CPU.Flags & DEBUG_MODE_FLAG) S9xDoDebug(); else #endif if(Settings.Paused || SI_EmulationPaused || !SI_EmulationRun) { SISaveSRAM(); SISaveRunningStateForGameNamed(rom_filename); SI_EmulationDidPause = 1; do { //S9xProcessEvents(FALSE); if(!SI_EmulationRun) break; usleep(100000); } while (SI_EmulationPaused); if(!SI_EmulationRun) { SISaveSRAM(); SISaveRunningStateForGameNamed(rom_filename); SIMuteSound(); S9xGraphicsDeinit(); Memory.Deinit(); S9xDeinitAPU(); break; } } #ifdef JOYSTICK_SUPPORT if (unixSettings.JoystickEnabled && (JoypadSkip++ & 1) == 0) ReadJoysticks(); #endif //S9xProcessEvents(FALSE); #ifdef DEBUGGER if(!Settings.Paused && !(CPU.Flags & DEBUG_MODE_FLAG)) #else if(!Settings.Paused && !SI_EmulationPaused) #endif S9xSetSoundMute(FALSE); } SI_EmulationIsRunning = 0; return (0); }
int S9xNetplaySyncSpeed (void) { if (!Settings.NetPlay || !NetPlay.Connected) return 0; // Send 1st joypad's position update to server S9xNPSendJoypadUpdate (local_joypads[0]); // set input from network for (int i = 0; i < NP_MAX_CLIENTS; i++) joypads[i] = S9xNPGetJoypad (i); if (!S9xNPCheckForHeartBeat ()) { // No heartbeats already arrived, have to wait for one. NetPlay.PendingWait4Sync = !S9xNPWaitForHeartBeatDelay (100); IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } else { int difference = (int) (NetPlay.MySequenceNum) - (int) (NetPlay.ServerSequenceNum); if (difference < 0) difference += 256; if (NetPlay.Waiting4EmulationThread) { if ((unsigned int) difference <= (NetPlay.MaxBehindFrameCount / 2)) { NetPlay.Waiting4EmulationThread = FALSE; S9xNPSendPause (FALSE); } } else { if ((unsigned int) difference >= (NetPlay.MaxBehindFrameCount)) { NetPlay.Waiting4EmulationThread = TRUE; S9xNPSendPause (TRUE); } } NetPlay.PendingWait4Sync = !S9xNPWaitForHeartBeatDelay (200); if (IPPU.SkippedFrames < NetPlay.MaxFrameSkip) { IPPU.SkippedFrames++; IPPU.RenderThisFrame = FALSE; } else { IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } } if (!NetPlay.PendingWait4Sync) { NetPlay.FrameCount++; S9xNPStepJoypadHistory (); } return 1; }
void S9xSyncSpeed( void) { #ifdef NETPLAY_SUPPORT if (Settings.NetPlay) { #if defined (NP_DEBUG) && NP_DEBUG == 2 printf ("CLIENT: SyncSpeed @%d\n", timeGetTime () - START); #endif S9xWinScanJoypads (); LONG prev; BOOL success; // Wait for heart beat from server if ((success = ReleaseSemaphore (GUI.ClientSemaphore, 1, &prev)) && prev == 0) { // No heartbeats already arrived, have to wait for one. // Mop up the ReleaseSemaphore test above... WaitForSingleObject (GUI.ClientSemaphore, 0); // ... and then wait for the real sync-signal from the // client loop thread. NetPlay.PendingWait4Sync = WaitForSingleObject (GUI.ClientSemaphore, 100) != WAIT_OBJECT_0; #if defined (NP_DEBUG) && NP_DEBUG == 2 if (NetPlay.PendingWait4Sync) printf ("CLIENT: PendingWait4Sync1 @%d\n", timeGetTime () - START); #endif IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } else { if (success) { // Once for the ReleaseSemaphore above... WaitForSingleObject (GUI.ClientSemaphore, 0); if (prev == 4 && NetPlay.Waiting4EmulationThread) { // Reached the lower behind count threshold - tell the // server its safe to start sending sync pulses again. NetPlay.Waiting4EmulationThread = FALSE; S9xNPSendPause (FALSE); } #if defined (NP_DEBUG) && NP_DEBUG == 2 if (prev > 1) { printf ("CLIENT: SyncSpeed prev: %d @%d\n", prev, timeGetTime () - START); } #endif } else { #ifdef NP_DEBUG printf ("*** CLIENT: SyncSpeed: Release failed @ %d\n", timeGetTime () - START); #endif } // ... and again to mop up the already-waiting sync-signal NetPlay.PendingWait4Sync = WaitForSingleObject (GUI.ClientSemaphore, 200) != WAIT_OBJECT_0; #if defined (NP_DEBUG) && NP_DEBUG == 2 if (NetPlay.PendingWait4Sync) printf ("CLIENT: PendingWait4Sync2 @%d\n", timeGetTime () - START); #endif if (IPPU.SkippedFrames < NetPlay.MaxFrameSkip) { IPPU.SkippedFrames++; IPPU.RenderThisFrame = FALSE; } else { IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } } // Give up remainder of time-slice to any other waiting threads, // if they need any time, that is. Sleep (0); if (!NetPlay.PendingWait4Sync) { NetPlay.FrameCount++; S9xNPStepJoypadHistory (); } } else #endif if (!Settings.TurboMode && Settings.SkipFrames == AUTO_FRAMERATE && !GUI.AVIOut) { if (!do_frame_adjust) { IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } else { if (IPPU.SkippedFrames < Settings.AutoMaxSkipFrames) { IPPU.SkippedFrames++; IPPU.RenderThisFrame = FALSE; } else { IPPU.RenderThisFrame = TRUE; IPPU.SkippedFrames = 0; } } } else { uint32 SkipFrames; if(Settings.TurboMode && !GUI.AVIOut) SkipFrames = Settings.TurboSkipFrames; else SkipFrames = (Settings.SkipFrames == AUTO_FRAMERATE) ? 0 : Settings.SkipFrames; if (IPPU.FrameSkip++ >= SkipFrames) { IPPU.FrameSkip = 0; IPPU.SkippedFrames = 0; IPPU.RenderThisFrame = TRUE; } else { IPPU.SkippedFrames++; IPPU.RenderThisFrame = GUI.AVIOut!=0; } } }