Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
static void write_frame_controller_data()
{
    reserve_buffer_space((uint32)((Movie.InputBufferPtr+Movie.BytesPerSample)-Movie.InputBuffer));

    int i;
    for(i=0; i<8; ++i)
    {
        if(Movie.ControllersMask & (1<<i))
        {
            Write16((uint16)(MovieGetJoypad(i) & 0xffff), Movie.InputBufferPtr);
        }
        else
        {
            MovieSetJoypad(i, 0);		// pretend the controller is disconnected
        }
    }
#ifdef PERIPHERAL_SUPPORT
    for(int port=0; port<2; port++)
    {
        if(Movie.PortType[port] == CTL_MOUSE)
        {
            uint8 buf [MOUSE_DATA_SIZE];
            MovieGetMouse(port, buf);
            memcpy(Movie.InputBufferPtr, buf, MOUSE_DATA_SIZE);
            Movie.InputBufferPtr += MOUSE_DATA_SIZE;
        }
        if(Movie.PortType[port] == CTL_SUPERSCOPE)
        {
            uint8 buf [SCOPE_DATA_SIZE];
            MovieGetScope(port, buf);
            memcpy(Movie.InputBufferPtr, buf, SCOPE_DATA_SIZE);
            Movie.InputBufferPtr += SCOPE_DATA_SIZE;
        }
        if(Movie.PortType[port] == CTL_JUSTIFIER)
        {
            uint8 buf [JUSTIFIER_DATA_SIZE];
            MovieGetJustifier(port, buf);
            memcpy(Movie.InputBufferPtr, buf, JUSTIFIER_DATA_SIZE);
            Movie.InputBufferPtr += JUSTIFIER_DATA_SIZE;
        }
    }
#endif
}
Ejemplo n.º 3
0
void VideoLogger(void *pixels, int width, int height, int depth, int bytes_per_line)
{
	int fc = S9xMovieGetFrameCounter();
	if (fc > 0)
		framecounter = fc;
	else
		framecounter++;
	
	if (video)
	{
		int i;
		char *data = (char*)pixels;
		static int lastwidth = width; // first resolution
		static int lastheight = height;
		if (lastwidth != width || lastheight != height) // this is just for informing the encoder that something has changed
		{
			printf("Frame %d, resolution changed from %dx%d to %dx%d!\n", fc, lastwidth, lastheight, width, height);
			lastwidth = width;
			lastheight = height;
		}
		for (i=0; i < height; i++)
			fwrite(data + i*bytes_per_line, depth, width, video);
		fflush(video);
		fflush(audio);
		drift++;

		if (maxframes > 0 && __builtin_expect((unsigned)framecounter >= maxframes, 0))
		{
			printf("-maxframes hit\ndrift:%d\n",drift);
			S9xExit();
		}

	}

	if (Settings.DisplayPressedKeys==1 || keypressscreen)
	{
		uint16 MovieGetJoypad(int i);

		int buttons = MovieGetJoypad(0);
		static char buffer[128];

		// This string spacing pattern is optimized for the 256 pixel wide screen.
                sprintf(buffer, "%s  %s  %s  %s  %s  %s  %c%c%c%c%c%c",
		buttons & SNES_START_MASK ? "Start" : "_____",
		buttons & SNES_SELECT_MASK ? "Select" : "______",
                buttons & SNES_UP_MASK ? "Up" : "__",
		buttons & SNES_DOWN_MASK ? "Down" : "____",
		buttons & SNES_LEFT_MASK ? "Left" : "____",
		buttons & SNES_RIGHT_MASK ? "Right" : "_____",
		buttons & SNES_A_MASK ? 'A':'_',
		buttons & SNES_B_MASK ? 'B':'_',
                buttons & SNES_X_MASK ? 'X':'_',
                buttons & SNES_Y_MASK ? 'Y':'_',
		buttons & SNES_TL_MASK ? 'L':'_',
		buttons & SNES_TR_MASK ? 'R':'_'
		/*framecounter*/);
		if (Settings.DisplayPressedKeys==1)
			fprintf(stderr, "%s %d           \r", buffer, framecounter);
		//if (keypressscreen)
                S9xSetInfoString(buffer);
	}

	if (__builtin_expect(messageframe >= 0 && framecounter == messageframe, 0))
	{
		S9xMessage(S9X_INFO, S9X_MOVIE_INFO, message);
		GFX.InfoStringTimeout = 300;
		messageframe = -1;
	}

/*	if (__builtin_expect(fastforwardpoint >= 0 && framecounter >= fastforwardpoint, 0))
	{
		Settings.FramesToSkip = fastforwarddistance;
		fastforwardpoint = -1;
	}*/
}
Ejemplo n.º 4
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);
}
Ejemplo n.º 5
0
gboolean
S9xIdleFunc (gpointer data)
{
    if (needs_fullscreening)
    {
        top_level->enter_fullscreen_mode();
        needs_fullscreening = FALSE;
    }

    if (Settings.Paused)
    {
        S9xSetSoundMute (gui_config->mute_sound);
        S9xSoundStop ();

#ifdef USE_JOYSTICK
        gui_config->flush_joysticks ();
#endif

#ifdef NETPLAY_SUPPORT
        if (Settings.NetPlay && NetPlay.Connected)
        {
            S9xNPSendPause (TRUE);
        }
#endif

        /* Move to a timer-based function to use less CPU */
        g_timeout_add (100, S9xPauseFunc, NULL);
        top_level->update_statusbar ();
        return FALSE;
    }

    if (syncing)
        S9xSyncSpeedFinish ();

    S9xCheckPointerTimer ();

    S9xProcessEvents (TRUE);

#ifdef NETPLAY_SUPPORT
    if (!S9xNetplayPush ())
    {
#endif

    if(Settings.Rewinding)
    {
        uint16 joypads[8];
        for (int i = 0; i < 8; i++)
            joypads[i] = MovieGetJoypad(i);

        Settings.Rewinding = stateMan.pop();

        for (int i = 0; i < 8; i++)
            MovieSetJoypad (i, joypads[i]);
    }
    else if(IPPU.TotalEmulatedFrames % gui_config->rewind_granularity == 0)
        stateMan.push();

    static int muted_from_turbo = FALSE;
    static int mute_saved_state = FALSE;

    if (Settings.TurboMode && !muted_from_turbo && gui_config->mute_sound_turbo)
    {
        muted_from_turbo = TRUE;
        mute_saved_state = Settings.Mute;
        S9xSetSoundMute (TRUE);
    }

    if (!Settings.TurboMode && muted_from_turbo)
    {
        muted_from_turbo = FALSE;
        Settings.Mute = mute_saved_state;
    }

    S9xMainLoop ();

#ifdef NETPLAY_SUPPORT
        S9xNetplayPop ();
    }
#endif

    return TRUE;
}