2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 02:43:11 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2016-11-05 06:34:06 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
|
2015-07-06 10:25:48 +00:00
|
|
|
#include <atomic>
|
2015-09-28 15:57:16 +00:00
|
|
|
#include <cstring>
|
2017-01-30 22:49:21 +00:00
|
|
|
#include <locale>
|
2016-05-12 01:18:30 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
2017-05-27 13:43:40 +00:00
|
|
|
#include <variant>
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2009-01-16 02:58:34 +00:00
|
|
|
#ifdef _WIN32
|
2010-06-26 12:52:57 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "AudioCommon/AudioCommon.h"
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/CPUDetect.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2016-08-05 14:04:39 +00:00
|
|
|
#include "Common/Flag.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/Logging/LogManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/MemoryUtil.h"
|
2017-03-02 17:15:02 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2017-05-27 16:48:40 +00:00
|
|
|
#include "Common/ScopeGuard.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/Timer.h"
|
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
#include "Core/Analytics.h"
|
2016-10-07 19:57:07 +00:00
|
|
|
#include "Core/BootManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/DSPEmulator.h"
|
|
|
|
#include "Core/Host.h"
|
|
|
|
#include "Core/MemTools.h"
|
2015-12-30 01:59:16 +00:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
|
|
|
#include "Core/MemoryWatcher.h"
|
|
|
|
#endif
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/Boot/Boot.h"
|
|
|
|
#include "Core/FifoPlayer/FifoPlayer.h"
|
2016-10-01 06:17:24 +00:00
|
|
|
#include "Core/HLE/HLE.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/CPU.h"
|
|
|
|
#include "Core/HW/DSP.h"
|
2017-01-20 20:33:43 +00:00
|
|
|
#include "Core/HW/EXI/EXI.h"
|
2015-01-16 22:36:05 +00:00
|
|
|
#include "Core/HW/GCKeyboard.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/GCPad.h"
|
|
|
|
#include "Core/HW/HW.h"
|
|
|
|
#include "Core/HW/SystemTimers.h"
|
|
|
|
#include "Core/HW/VideoInterface.h"
|
|
|
|
#include "Core/HW/Wiimote.h"
|
2017-04-29 14:11:22 +00:00
|
|
|
#include "Core/IOS/IOS.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Core/Movie.h"
|
|
|
|
#include "Core/NetPlayClient.h"
|
|
|
|
#include "Core/NetPlayProto.h"
|
|
|
|
#include "Core/PatchEngine.h"
|
2014-09-06 10:41:17 +00:00
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Core/State.h"
|
2017-01-06 20:59:02 +00:00
|
|
|
#include "Core/WiiRoot.h"
|
2014-02-19 18:09:14 +00:00
|
|
|
|
2013-01-06 10:28:27 +00:00
|
|
|
#ifdef USE_GDBSTUB
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/PowerPC/GDBStub.h"
|
2013-01-06 10:28:27 +00:00
|
|
|
#endif
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "InputCommon/GCAdapter.h"
|
2017-02-12 12:02:57 +00:00
|
|
|
|
2016-01-12 08:55:29 +00:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2016-01-12 08:55:29 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2016-02-05 04:05:58 +00:00
|
|
|
// Android and OSX haven't implemented the keyword yet.
|
|
|
|
#if defined __ANDROID__ || defined __APPLE__
|
2015-02-19 06:33:50 +00:00
|
|
|
#include <pthread.h>
|
2016-02-05 04:05:58 +00:00
|
|
|
#else // Everything besides OSX and Android
|
|
|
|
#define ThreadLocalStorage thread_local
|
2015-02-19 06:33:50 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
namespace Core
|
|
|
|
{
|
2017-04-03 17:30:58 +00:00
|
|
|
static bool s_wants_determinism;
|
2014-09-06 21:26:40 +00:00
|
|
|
|
2009-01-04 21:53:41 +00:00
|
|
|
// Declarations and definitions
|
2014-09-04 12:26:11 +00:00
|
|
|
static Common::Timer s_timer;
|
2015-07-06 10:25:48 +00:00
|
|
|
static std::atomic<u32> s_drawn_frame;
|
|
|
|
static std::atomic<u32> s_drawn_video;
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2014-09-04 12:26:11 +00:00
|
|
|
static bool s_is_stopping = false;
|
|
|
|
static bool s_hardware_initialized = false;
|
|
|
|
static bool s_is_started = false;
|
2016-08-05 14:04:39 +00:00
|
|
|
static Common::Flag s_is_booting;
|
2014-09-04 12:26:11 +00:00
|
|
|
static void* s_window_handle = nullptr;
|
|
|
|
static std::string s_state_filename;
|
|
|
|
static std::thread s_emu_thread;
|
2017-09-04 17:57:42 +00:00
|
|
|
static StateChangedCallbackFunc s_on_state_changed_callback;
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2014-09-04 12:26:11 +00:00
|
|
|
static std::thread s_cpu_thread;
|
|
|
|
static bool s_request_refresh_info = false;
|
2015-12-15 23:10:47 +00:00
|
|
|
static bool s_is_throttler_temp_disabled = false;
|
2017-07-01 20:17:18 +00:00
|
|
|
static bool s_frame_step = false;
|
2010-04-17 21:02:03 +00:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
struct HostJob
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::function<void()> job;
|
|
|
|
bool run_after_stop;
|
2016-05-12 01:18:30 +00:00
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
static std::mutex s_host_jobs_lock;
|
2016-05-12 01:18:30 +00:00
|
|
|
static std::queue<HostJob> s_host_jobs_queue;
|
|
|
|
|
2015-02-19 06:33:50 +00:00
|
|
|
#ifdef ThreadLocalStorage
|
2015-02-22 19:08:28 +00:00
|
|
|
static ThreadLocalStorage bool tls_is_cpu_thread = false;
|
2015-02-19 06:33:50 +00:00
|
|
|
#else
|
|
|
|
static pthread_key_t s_tls_is_cpu_key;
|
|
|
|
static pthread_once_t s_cpu_key_is_init = PTHREAD_ONCE_INIT;
|
|
|
|
static void InitIsCPUKey()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
pthread_key_create(&s_tls_is_cpu_key, nullptr);
|
2015-02-19 06:33:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-27 13:43:40 +00:00
|
|
|
static void EmuThread(std::unique_ptr<BootParameters> boot);
|
2017-04-03 17:27:35 +00:00
|
|
|
|
2015-12-15 23:10:47 +00:00
|
|
|
bool GetIsThrottlerTempDisabled()
|
2014-04-30 10:50:29 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return s_is_throttler_temp_disabled;
|
2014-04-30 10:50:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 23:10:47 +00:00
|
|
|
void SetIsThrottlerTempDisabled(bool disable)
|
2014-04-30 10:50:29 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_is_throttler_temp_disabled = disable;
|
2014-04-30 10:50:29 +00:00
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string GetStateFileName()
|
|
|
|
{
|
|
|
|
return s_state_filename;
|
|
|
|
}
|
|
|
|
void SetStateFileName(const std::string& val)
|
|
|
|
{
|
|
|
|
s_state_filename = val;
|
|
|
|
}
|
2010-04-17 21:02:03 +00:00
|
|
|
|
2015-06-06 05:20:51 +00:00
|
|
|
void FrameUpdateOnCPUThread()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (NetPlay::IsNetPlayRunning())
|
|
|
|
NetPlayClient::SendTimeBase();
|
2015-06-06 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
2009-01-07 02:59:19 +00:00
|
|
|
// Display messages and return values
|
2009-09-07 12:40:43 +00:00
|
|
|
|
|
|
|
// Formatted stop message
|
2015-05-29 00:28:48 +00:00
|
|
|
std::string StopMessage(bool main_thread, const std::string& message)
|
2009-09-07 12:40:43 +00:00
|
|
|
{
|
2017-06-09 05:25:46 +00:00
|
|
|
return StringFromFormat("Stop [%s %i]\t%s", main_thread ? "Main Thread" : "Video Thread",
|
|
|
|
Common::CurrentThreadId(), message.c_str());
|
2009-09-07 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
void DisplayMessage(const std::string& message, int time_in_ms)
|
2009-01-07 02:59:19 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!IsRunning())
|
|
|
|
return;
|
|
|
|
|
2016-10-15 10:36:11 +00:00
|
|
|
// Actually displaying non-ASCII could cause things to go pear-shaped
|
|
|
|
for (const char& c : message)
|
|
|
|
{
|
2017-01-30 22:49:21 +00:00
|
|
|
if (!std::isprint(c, std::locale::classic()))
|
2016-10-15 10:36:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
OSD::AddMessage(message, time_in_ms);
|
|
|
|
Host_UpdateTitle(message);
|
2009-01-07 02:59:19 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
bool IsRunning()
|
2009-06-26 22:36:44 +00:00
|
|
|
{
|
2017-02-05 12:39:58 +00:00
|
|
|
return (GetState() != State::Uninitialized || s_hardware_initialized) && !s_is_stopping;
|
2009-06-26 22:36:44 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2011-12-31 04:16:12 +00:00
|
|
|
bool IsRunningAndStarted()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return s_is_started && !s_is_stopping;
|
2011-12-31 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2010-04-17 21:02:03 +00:00
|
|
|
bool IsRunningInCurrentThread()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return IsRunning() && IsCPUThread();
|
2010-04-17 21:02:03 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2011-02-15 09:07:55 +00:00
|
|
|
bool IsCPUThread()
|
|
|
|
{
|
2015-02-19 06:33:50 +00:00
|
|
|
#ifdef ThreadLocalStorage
|
2016-06-24 08:43:46 +00:00
|
|
|
return tls_is_cpu_thread;
|
2015-02-19 06:33:50 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
// Use pthread implementation for Android and Mac
|
|
|
|
// Make sure that s_tls_is_cpu_key is initialized
|
|
|
|
pthread_once(&s_cpu_key_is_init, InitIsCPUKey);
|
|
|
|
return pthread_getspecific(s_tls_is_cpu_key);
|
2015-02-19 06:33:50 +00:00
|
|
|
#endif
|
2011-02-15 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-31 04:16:12 +00:00
|
|
|
bool IsGPUThread()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
|
|
|
return (s_emu_thread.joinable() && (s_emu_thread.get_id() == std::this_thread::get_id()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return IsCPUThread();
|
|
|
|
}
|
2011-12-31 04:16:12 +00:00
|
|
|
}
|
2013-02-26 19:49:00 +00:00
|
|
|
|
2017-04-03 17:30:58 +00:00
|
|
|
bool WantsDeterminism()
|
|
|
|
{
|
|
|
|
return s_wants_determinism;
|
|
|
|
}
|
|
|
|
|
2010-02-02 08:10:23 +00:00
|
|
|
// This is called from the GUI thread. See the booting call schedule in
|
|
|
|
// BootManager.cpp
|
2017-05-27 13:43:40 +00:00
|
|
|
bool Init(std::unique_ptr<BootParameters> boot)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_emu_thread.joinable())
|
|
|
|
{
|
|
|
|
if (IsRunning())
|
|
|
|
{
|
|
|
|
PanicAlertT("Emu Thread already running");
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 00:43:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// The Emu Thread was stopped, synchronize with it.
|
|
|
|
s_emu_thread.join();
|
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Drain any left over jobs
|
|
|
|
HostDispatchJobs();
|
2016-05-12 01:18:30 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Core::UpdateWantDeterminism(/*initial*/ true);
|
2014-09-06 21:26:40 +00:00
|
|
|
|
2017-07-09 17:47:48 +00:00
|
|
|
INFO_LOG(BOOT, "Starting core = %s mode", SConfig::GetInstance().bWii ? "Wii" : "GameCube");
|
|
|
|
INFO_LOG(BOOT, "CPU Thread separate = %s", SConfig::GetInstance().bCPUThread ? "Yes" : "No");
|
2010-07-29 12:17:47 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Host_UpdateMainFrame(); // Disable any menus or buttons at boot
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_window_handle = Host_GetRenderHandle();
|
2011-02-17 17:51:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Start the emu thread
|
2017-05-27 13:43:40 +00:00
|
|
|
s_emu_thread = std::thread(EmuThread, std::move(boot));
|
2010-02-02 08:10:23 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-17 22:48:16 +00:00
|
|
|
|
2010-07-19 02:09:34 +00:00
|
|
|
// Called from GUI thread
|
2009-07-03 12:22:32 +00:00
|
|
|
void Stop() // - Hammertime!
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-02-05 12:39:58 +00:00
|
|
|
if (GetState() == State::Stopping)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2011-03-15 23:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2011-03-15 23:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_is_stopping = true;
|
2009-09-07 12:40:43 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Dump left over jobs
|
|
|
|
HostDispatchJobs();
|
2016-05-12 01:18:30 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Fifo::EmulatorState(false);
|
2009-09-07 12:40:43 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
INFO_LOG(CONSOLE, "Stop [Main Thread]\t\t---- Shutting down ----");
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Stop the CPU
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stop CPU").c_str());
|
|
|
|
CPU::Stop();
|
2009-07-03 12:22:32 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
|
|
|
// Video_EnterLoop() should now exit so that EmuThread()
|
|
|
|
// will continue concurrently with the rest of the commands
|
|
|
|
// in this function. We no longer rely on Postmessage.
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Wait for Video Loop to exit ...").c_str());
|
2013-08-25 00:49:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
g_video_backend->Video_ExitLoop();
|
|
|
|
}
|
2016-09-07 21:39:46 +00:00
|
|
|
#if defined(__LIBUSB__)
|
2016-06-24 08:43:46 +00:00
|
|
|
GCAdapter::ResetRumble();
|
2015-11-08 17:13:58 +00:00
|
|
|
#endif
|
2015-12-30 01:59:16 +00:00
|
|
|
|
|
|
|
#ifdef USE_MEMORYWATCHER
|
2016-06-24 08:43:46 +00:00
|
|
|
MemoryWatcher::Shutdown();
|
2015-12-30 01:59:16 +00:00
|
|
|
#endif
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-22 21:16:12 +00:00
|
|
|
|
2015-06-06 05:41:26 +00:00
|
|
|
void DeclareAsCPUThread()
|
2015-02-19 06:33:50 +00:00
|
|
|
{
|
|
|
|
#ifdef ThreadLocalStorage
|
2016-06-24 08:43:46 +00:00
|
|
|
tls_is_cpu_thread = true;
|
2015-02-19 06:33:50 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
// Use pthread implementation for Android and Mac
|
|
|
|
// Make sure that s_tls_is_cpu_key is initialized
|
|
|
|
pthread_once(&s_cpu_key_is_init, InitIsCPUKey);
|
|
|
|
pthread_setspecific(s_tls_is_cpu_key, (void*)true);
|
2015-02-19 06:33:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-06-06 05:41:26 +00:00
|
|
|
void UndeclareAsCPUThread()
|
2015-02-19 06:33:50 +00:00
|
|
|
{
|
|
|
|
#ifdef ThreadLocalStorage
|
2016-06-24 08:43:46 +00:00
|
|
|
tls_is_cpu_thread = false;
|
2015-02-19 06:33:50 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
// Use pthread implementation for Android and Mac
|
|
|
|
// Make sure that s_tls_is_cpu_key is initialized
|
|
|
|
pthread_once(&s_cpu_key_is_init, InitIsCPUKey);
|
|
|
|
pthread_setspecific(s_tls_is_cpu_key, (void*)false);
|
2015-02-19 06:33:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// For the CPU Thread only.
|
|
|
|
static void CPUSetInitialExecutionState()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
QueueHostJob([] {
|
2017-02-05 12:39:58 +00:00
|
|
|
SetState(SConfig::GetInstance().bBootToPause ? State::Paused : State::Running);
|
2016-06-24 08:43:46 +00:00
|
|
|
Host_UpdateMainFrame();
|
|
|
|
});
|
2016-05-12 01:18:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 14:27:30 +00:00
|
|
|
// Create the CPU thread, which is a CPU + Video thread in Single Core mode.
|
2014-07-08 12:29:26 +00:00
|
|
|
static void CpuThread()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DeclareAsCPUThread();
|
2015-02-19 06:33:50 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2009-07-15 15:09:20 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("CPU thread");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("CPU-GPU thread");
|
|
|
|
g_video_backend->Video_Prepare();
|
2017-07-30 04:52:58 +00:00
|
|
|
Host_Message(WM_USER_CREATE);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
// This needs to be delayed until after the video backend is ready.
|
2016-06-24 08:43:46 +00:00
|
|
|
DolphinAnalytics::Instance()->ReportGameStart();
|
|
|
|
|
|
|
|
if (_CoreParameter.bFastmem)
|
|
|
|
EMM::InstallExceptionHandler(); // Let's run under memory watch
|
|
|
|
|
|
|
|
if (!s_state_filename.empty())
|
|
|
|
{
|
|
|
|
// Needs to PauseAndLock the Core
|
2017-03-28 14:55:00 +00:00
|
|
|
// NOTE: EmuThread should have left us in State::Stepping so nothing will happen
|
2016-06-24 08:43:46 +00:00
|
|
|
// until after the job is serviced.
|
|
|
|
QueueHostJob([] {
|
|
|
|
// Recheck in case Movie cleared it since.
|
|
|
|
if (!s_state_filename.empty())
|
2017-02-05 12:39:58 +00:00
|
|
|
::State::LoadAs(s_state_filename);
|
2016-06-24 08:43:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
s_is_started = true;
|
|
|
|
CPUSetInitialExecutionState();
|
|
|
|
|
|
|
|
#ifdef USE_GDBSTUB
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!_CoreParameter.gdb_socket.empty())
|
|
|
|
{
|
|
|
|
gdb_init_local(_CoreParameter.gdb_socket.data());
|
|
|
|
gdb_break();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (_CoreParameter.iGDBPort > 0)
|
|
|
|
{
|
|
|
|
gdb_init(_CoreParameter.iGDBPort);
|
|
|
|
// break at next instruction (the first instruction)
|
|
|
|
gdb_break();
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-25 00:49:58 +00:00
|
|
|
|
2015-12-30 01:59:16 +00:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
2016-06-24 08:43:46 +00:00
|
|
|
MemoryWatcher::Init();
|
2015-12-30 01:59:16 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Enter CPU run loop. When we leave it - we are done.
|
|
|
|
CPU::Run();
|
2009-07-15 15:09:20 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_is_started = false;
|
2013-08-25 00:49:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!_CoreParameter.bCPUThread)
|
|
|
|
g_video_backend->Video_Cleanup();
|
2011-12-31 04:16:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (_CoreParameter.bFastmem)
|
|
|
|
EMM::UninstallExceptionHandler();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void FifoPlayerThread()
|
2011-03-27 02:55:08 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DeclareAsCPUThread();
|
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
|
|
|
|
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("FIFO player thread");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_video_backend->Video_Prepare();
|
2017-07-30 04:52:58 +00:00
|
|
|
Host_Message(WM_USER_CREATE);
|
2016-06-24 08:43:46 +00:00
|
|
|
Common::SetCurrentThreadName("FIFO-GPU thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter CPU run loop. When we leave it - we are done.
|
2017-05-27 13:43:40 +00:00
|
|
|
if (auto cpu_core = FifoPlayer::GetInstance().GetCPUCore())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-05-27 13:43:40 +00:00
|
|
|
PowerPC::InjectExternalCPUCore(cpu_core.get());
|
|
|
|
s_is_started = true;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-05-27 13:43:40 +00:00
|
|
|
CPUSetInitialExecutionState();
|
|
|
|
CPU::Run();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-05-27 13:43:40 +00:00
|
|
|
s_is_started = false;
|
|
|
|
PowerPC::InjectExternalCPUCore(nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2017-05-27 13:43:40 +00:00
|
|
|
FifoPlayer::GetInstance().Close();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// If we did not enter the CPU Run Loop above then run a fake one instead.
|
|
|
|
// We need to be IsRunningAndStarted() for DolphinWX to stop us.
|
2017-03-28 14:55:00 +00:00
|
|
|
if (CPU::GetState() != CPU::State::PowerDown)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
s_is_started = true;
|
|
|
|
Host_Message(WM_USER_STOP);
|
2017-03-28 14:55:00 +00:00
|
|
|
while (CPU::GetState() != CPU::State::PowerDown)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (!_CoreParameter.bCPUThread)
|
|
|
|
g_video_backend->PeekMessages();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
|
|
}
|
|
|
|
s_is_started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_CoreParameter.bCPUThread)
|
|
|
|
g_video_backend->Video_Cleanup();
|
2011-03-27 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 12:26:11 +00:00
|
|
|
// Initialize and create emulation thread
|
|
|
|
// Call browser: Init():s_emu_thread().
|
2011-02-18 14:27:30 +00:00
|
|
|
// See the BootManager.cpp file description for a complete call schedule.
|
2017-05-27 13:43:40 +00:00
|
|
|
static void EmuThread(std::unique_ptr<BootParameters> boot)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& core_parameter = SConfig::GetInstance();
|
2016-08-05 14:04:39 +00:00
|
|
|
s_is_booting.Set();
|
2017-09-04 17:57:42 +00:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(State::Starting);
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard flag_guard{[] {
|
|
|
|
s_is_booting.Clear();
|
|
|
|
s_is_started = false;
|
|
|
|
s_is_stopping = false;
|
2017-07-31 07:38:39 +00:00
|
|
|
s_wants_determinism = false;
|
2017-05-27 16:48:40 +00:00
|
|
|
|
2017-09-04 17:57:42 +00:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(State::Uninitialized);
|
2017-05-27 16:48:40 +00:00
|
|
|
|
|
|
|
INFO_LOG(CONSOLE, "Stop\t\t---- Shutdown complete ----");
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Prevent the UI from getting stuck whenever an error occurs.
|
|
|
|
Common::ScopeGuard stop_message_guard{[] { Host_Message(WM_USER_STOP); }};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
Common::SetCurrentThreadName("Emuthread - Starting");
|
|
|
|
|
|
|
|
// For a time this acts as the CPU thread...
|
|
|
|
DeclareAsCPUThread();
|
2017-07-01 20:17:18 +00:00
|
|
|
s_frame_step = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-05-27 13:43:40 +00:00
|
|
|
Movie::Init(*boot);
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard movie_guard{Movie::Shutdown};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
HW::Init();
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard hw_guard{[] {
|
|
|
|
// We must set up this flag before executing HW::Shutdown()
|
|
|
|
s_hardware_initialized = false;
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(false, "Shutting down HW").c_str());
|
|
|
|
HW::Shutdown();
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(false, "HW shutdown").c_str());
|
2017-06-10 17:46:31 +00:00
|
|
|
|
|
|
|
// Clear on screen messages that haven't expired
|
|
|
|
OSD::ClearMessages();
|
|
|
|
|
|
|
|
// The config must be restored only after the whole HW has shut down,
|
|
|
|
// not when it is still running.
|
|
|
|
BootManager::RestoreConfig();
|
|
|
|
|
|
|
|
PatchEngine::Shutdown();
|
|
|
|
HLE::Clear();
|
2017-05-27 16:48:40 +00:00
|
|
|
}};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (!g_video_backend->Initialize(s_window_handle))
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to initialize video backend!");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard video_guard{[] { g_video_backend->Shutdown(); }};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
OSD::AddMessage("Dolphin " + g_video_backend->GetName() + " Video Backend.", 5000);
|
|
|
|
|
|
|
|
if (cpu_info.HTT)
|
|
|
|
SConfig::GetInstance().bDSPThread = cpu_info.num_cores > 4;
|
|
|
|
else
|
|
|
|
SConfig::GetInstance().bDSPThread = cpu_info.num_cores > 2;
|
|
|
|
|
|
|
|
if (!DSP::GetDSPEmulator()->Initialize(core_parameter.bWii, core_parameter.bDSPThread))
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to initialize DSP emulation!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init_controllers = false;
|
|
|
|
if (!g_controller_interface.IsInit())
|
|
|
|
{
|
2016-10-11 19:54:35 +00:00
|
|
|
g_controller_interface.Initialize(s_window_handle);
|
|
|
|
Pad::Initialize();
|
|
|
|
Keyboard::Initialize();
|
2016-06-24 08:43:46 +00:00
|
|
|
init_controllers = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Update references in case controllers were refreshed
|
|
|
|
Pad::LoadConfig();
|
|
|
|
Keyboard::LoadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load and Init Wiimotes - only if we are booting in Wii mode
|
2016-08-23 14:19:30 +00:00
|
|
|
if (core_parameter.bWii && !SConfig::GetInstance().m_bt_passthrough_enabled)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (init_controllers)
|
2016-10-11 19:54:35 +00:00
|
|
|
Wiimote::Initialize(!s_state_filename.empty() ?
|
|
|
|
Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES :
|
|
|
|
Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
|
|
|
Wiimote::LoadConfig();
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard controller_guard{[init_controllers] {
|
|
|
|
if (!init_controllers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Wiimote::Shutdown();
|
|
|
|
Keyboard::Shutdown();
|
|
|
|
Pad::Shutdown();
|
|
|
|
g_controller_interface.Shutdown();
|
|
|
|
}};
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
AudioCommon::InitSoundStream();
|
2017-05-27 16:48:40 +00:00
|
|
|
Common::ScopeGuard audio_guard{AudioCommon::ShutdownSoundStream};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// The hardware is initialized.
|
|
|
|
s_hardware_initialized = true;
|
2016-08-05 14:04:39 +00:00
|
|
|
s_is_booting.Clear();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Set execution state to known values (CPU/FIFO/Audio Paused)
|
|
|
|
CPU::Break();
|
|
|
|
|
|
|
|
// Load GCM/DOL/ELF whatever ... we boot with the interpreter core
|
2017-02-05 00:18:13 +00:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-05-27 13:43:40 +00:00
|
|
|
// Determine the CPU thread function
|
|
|
|
void (*cpuThreadFunc)();
|
|
|
|
if (std::holds_alternative<BootParameters::DFF>(boot->parameters))
|
|
|
|
cpuThreadFunc = FifoPlayerThread;
|
|
|
|
else
|
|
|
|
cpuThreadFunc = CpuThread;
|
|
|
|
|
|
|
|
if (!CBoot::BootUp(std::move(boot)))
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// This adds the SyncGPU handler to CoreTiming, so now CoreTiming::Advance might block.
|
|
|
|
Fifo::Prepare();
|
|
|
|
|
|
|
|
// Thread is no longer acting as CPU Thread
|
|
|
|
UndeclareAsCPUThread();
|
|
|
|
|
|
|
|
// Setup our core, but can't use dynarec if we are compare server
|
|
|
|
if (core_parameter.iCPUCore != PowerPC::CORE_INTERPRETER &&
|
|
|
|
(!core_parameter.bRunCompareServer || core_parameter.bRunCompareClient))
|
|
|
|
{
|
2017-02-05 00:18:13 +00:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::JIT);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-05 00:18:13 +00:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the window again because all stuff is initialized
|
|
|
|
Host_UpdateDisasmDialog();
|
|
|
|
Host_UpdateMainFrame();
|
|
|
|
|
|
|
|
// ENTER THE VIDEO THREAD LOOP
|
|
|
|
if (core_parameter.bCPUThread)
|
|
|
|
{
|
|
|
|
// This thread, after creating the EmuWindow, spawns a CPU
|
|
|
|
// thread, and then takes over and becomes the video thread
|
|
|
|
Common::SetCurrentThreadName("Video thread");
|
|
|
|
|
|
|
|
g_video_backend->Video_Prepare();
|
2017-07-30 04:52:58 +00:00
|
|
|
Host_Message(WM_USER_CREATE);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Spawn the CPU thread
|
|
|
|
s_cpu_thread = std::thread(cpuThreadFunc);
|
|
|
|
|
|
|
|
// become the GPU thread
|
|
|
|
Fifo::RunGpuLoop();
|
|
|
|
|
|
|
|
// We have now exited the Video Loop
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(false, "Video Loop Ended").c_str());
|
|
|
|
}
|
|
|
|
else // SingleCore mode
|
|
|
|
{
|
|
|
|
// The spawned CPU Thread also does the graphics.
|
|
|
|
// The EmuThread is thus an idle thread, which sleeps while
|
|
|
|
// waiting for the program to terminate. Without this extra
|
|
|
|
// thread, the video backend window hangs in single core mode
|
|
|
|
// because no one is pumping messages.
|
|
|
|
Common::SetCurrentThreadName("Emuthread - Idle");
|
|
|
|
|
|
|
|
// Spawn the CPU+GPU thread
|
|
|
|
s_cpu_thread = std::thread(cpuThreadFunc);
|
|
|
|
|
2017-03-28 14:55:00 +00:00
|
|
|
while (CPU::GetState() != CPU::State::PowerDown)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
g_video_backend->PeekMessages();
|
|
|
|
Common::SleepCurrentThread(20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stopping Emu thread ...").c_str());
|
|
|
|
|
|
|
|
// Wait for s_cpu_thread to exit
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stopping CPU-GPU thread ...").c_str());
|
|
|
|
|
|
|
|
#ifdef USE_GDBSTUB
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stopping GDB ...").c_str());
|
|
|
|
gdb_deinit();
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "GDB stopped.").c_str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s_cpu_thread.join();
|
|
|
|
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "CPU thread stopped.").c_str());
|
|
|
|
|
|
|
|
if (core_parameter.bCPUThread)
|
|
|
|
g_video_backend->Video_Cleanup();
|
|
|
|
|
2017-05-27 16:48:40 +00:00
|
|
|
// If we shut down normally, the stop message does not need to be triggered.
|
|
|
|
stop_message_guard.Dismiss();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2009-01-04 21:53:41 +00:00
|
|
|
// Set or get the running state
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
void SetState(State state)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// State cannot be controlled until the CPU Thread is operational
|
|
|
|
if (!IsRunningAndStarted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
2017-02-05 12:39:58 +00:00
|
|
|
case State::Paused:
|
|
|
|
// NOTE: GetState() will return State::Paused immediately, even before anything has
|
2016-06-24 08:43:46 +00:00
|
|
|
// stopped (including the CPU).
|
|
|
|
CPU::EnableStepping(true); // Break
|
|
|
|
Wiimote::Pause();
|
2016-09-07 21:39:46 +00:00
|
|
|
#if defined(__LIBUSB__)
|
2016-06-24 08:43:46 +00:00
|
|
|
GCAdapter::ResetRumble();
|
2015-11-08 17:13:58 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
break;
|
2017-02-05 12:39:58 +00:00
|
|
|
case State::Running:
|
2016-06-24 08:43:46 +00:00
|
|
|
CPU::EnableStepping(false);
|
|
|
|
Wiimote::Resume();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PanicAlert("Invalid state");
|
|
|
|
break;
|
|
|
|
}
|
2017-09-04 17:57:42 +00:00
|
|
|
|
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(GetState());
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
State GetState()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_is_stopping)
|
2017-02-05 12:39:58 +00:00
|
|
|
return State::Stopping;
|
2014-06-20 01:26:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_hardware_initialized)
|
|
|
|
{
|
|
|
|
if (CPU::IsStepping())
|
2017-02-05 12:39:58 +00:00
|
|
|
return State::Paused;
|
2015-10-04 19:06:31 +00:00
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
return State::Running;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-06-20 01:26:06 +00:00
|
|
|
|
2017-07-01 21:08:58 +00:00
|
|
|
if (s_is_booting.IsSet())
|
|
|
|
return State::Starting;
|
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
return State::Uninitialized;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2015-06-24 03:08:10 +00:00
|
|
|
static std::string GenerateScreenshotFolderPath()
|
2009-06-28 19:47:02 +00:00
|
|
|
{
|
2016-10-29 12:42:43 +00:00
|
|
|
const std::string& gameId = SConfig::GetInstance().GetGameID();
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string path = File::GetUserPath(D_SCREENSHOTS_IDX) + gameId + DIR_SEP_CHR;
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!File::CreateFullPath(path))
|
|
|
|
{
|
|
|
|
// fallback to old-style screenshots, without folder.
|
|
|
|
path = File::GetUserPath(D_SCREENSHOTS_IDX);
|
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return path;
|
2015-06-24 03:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GenerateScreenshotName()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string path = GenerateScreenshotFolderPath();
|
2015-06-24 03:08:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// append gameId, path only contains the folder here.
|
2016-10-29 12:42:43 +00:00
|
|
|
path += SConfig::GetInstance().GetGameID();
|
2011-03-15 23:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string name;
|
|
|
|
for (int i = 1; File::Exists(name = StringFromFormat("%s-%d.png", path.c_str(), i)); ++i)
|
|
|
|
{
|
|
|
|
// TODO?
|
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return name;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2017-03-04 06:42:35 +00:00
|
|
|
void SaveScreenShot(bool wait_for_completion)
|
2009-06-28 19:47:02 +00:00
|
|
|
{
|
2017-02-05 12:39:58 +00:00
|
|
|
const bool bPaused = GetState() == State::Paused;
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
SetState(State::Paused);
|
2009-02-27 03:56:34 +00:00
|
|
|
|
2017-03-04 06:42:35 +00:00
|
|
|
g_renderer->SaveScreenshot(GenerateScreenshotName(), wait_for_completion);
|
2013-08-25 00:49:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!bPaused)
|
2017-02-05 12:39:58 +00:00
|
|
|
SetState(State::Running);
|
2009-06-28 19:47:02 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2017-03-04 06:42:35 +00:00
|
|
|
void SaveScreenShot(const std::string& name, bool wait_for_completion)
|
2015-06-19 01:28:40 +00:00
|
|
|
{
|
2017-02-05 12:39:58 +00:00
|
|
|
const bool bPaused = GetState() == State::Paused;
|
2015-06-19 01:28:40 +00:00
|
|
|
|
2017-02-05 12:39:58 +00:00
|
|
|
SetState(State::Paused);
|
2015-06-19 01:28:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string filePath = GenerateScreenshotFolderPath() + name + ".png";
|
2015-06-19 01:46:47 +00:00
|
|
|
|
2017-03-04 06:42:35 +00:00
|
|
|
g_renderer->SaveScreenshot(filePath, wait_for_completion);
|
2015-06-19 01:28:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!bPaused)
|
2017-02-05 12:39:58 +00:00
|
|
|
SetState(State::Running);
|
2015-06-19 01:28:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 12:03:05 +00:00
|
|
|
void RequestRefreshInfo()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_request_refresh_info = true;
|
2011-12-14 12:03:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 06:06:02 +00:00
|
|
|
static bool PauseAndLock(bool do_lock, bool unpause_on_unlock)
|
2011-12-31 04:16:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// WARNING: PauseAndLock is not fully threadsafe so is only valid on the Host Thread
|
|
|
|
if (!IsRunning())
|
|
|
|
return true;
|
2014-09-06 21:26:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
bool was_unpaused = true;
|
|
|
|
if (do_lock)
|
|
|
|
{
|
|
|
|
// first pause the CPU
|
|
|
|
// This acquires a wrapper mutex and converts the current thread into
|
|
|
|
// a temporary replacement CPU Thread.
|
|
|
|
was_unpaused = CPU::PauseAndLock(true);
|
|
|
|
}
|
2016-05-12 09:17:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ExpansionInterface::PauseAndLock(do_lock, false);
|
2011-12-31 04:16:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// audio has to come after CPU, because CPU thread can wait for audio thread (m_throttle).
|
|
|
|
DSP::GetDSPEmulator()->PauseAndLock(do_lock, false);
|
2011-12-31 04:16:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// video has to come after CPU, because CPU thread can wait for video thread
|
|
|
|
// (s_efbAccessRequested).
|
|
|
|
Fifo::PauseAndLock(do_lock, false);
|
2015-06-06 05:41:26 +00:00
|
|
|
|
2016-09-07 21:39:46 +00:00
|
|
|
#if defined(__LIBUSB__)
|
2016-06-24 08:43:46 +00:00
|
|
|
GCAdapter::ResetRumble();
|
2015-11-08 17:13:58 +00:00
|
|
|
#endif
|
2016-05-12 09:17:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// CPU is unlocked last because CPU::PauseAndLock contains the synchronization
|
|
|
|
// mechanism that prevents CPU::Break from racing.
|
|
|
|
if (!do_lock)
|
|
|
|
{
|
|
|
|
// The CPU is responsible for managing the Audio and FIFO state so we use its
|
|
|
|
// mechanism to unpause them. If we unpaused the systems above when releasing
|
|
|
|
// the locks then they could call CPU::Break which would require detecting it
|
|
|
|
// and re-pausing with CPU::EnableStepping.
|
|
|
|
was_unpaused = CPU::PauseAndLock(false, unpause_on_unlock, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return was_unpaused;
|
2011-12-31 04:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 06:06:02 +00:00
|
|
|
void RunAsCPUThread(std::function<void()> function)
|
|
|
|
{
|
|
|
|
const bool is_cpu_thread = IsCPUThread();
|
|
|
|
bool was_unpaused = false;
|
|
|
|
if (!is_cpu_thread)
|
|
|
|
was_unpaused = PauseAndLock(true, true);
|
|
|
|
|
|
|
|
function();
|
|
|
|
|
|
|
|
if (!is_cpu_thread)
|
|
|
|
PauseAndLock(false, was_unpaused);
|
|
|
|
}
|
|
|
|
|
2015-12-15 23:10:47 +00:00
|
|
|
// Display FPS info
|
2010-01-07 20:01:41 +00:00
|
|
|
// This should only be called from VI
|
2010-01-08 11:25:51 +00:00
|
|
|
void VideoThrottle()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Update info per second
|
|
|
|
u32 ElapseTime = (u32)s_timer.GetTimeDifference();
|
|
|
|
if ((ElapseTime >= 1000 && s_drawn_video.load() > 0) || s_request_refresh_info)
|
|
|
|
{
|
|
|
|
UpdateTitle();
|
|
|
|
|
|
|
|
// Reset counter
|
|
|
|
s_timer.Update();
|
|
|
|
s_drawn_frame.store(0);
|
|
|
|
s_drawn_video.store(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
s_drawn_video++;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-09 21:27:45 +00:00
|
|
|
|
2010-01-07 20:01:41 +00:00
|
|
|
// Executed from GPU thread
|
|
|
|
// reports if a frame should be skipped or not
|
2015-12-15 23:10:47 +00:00
|
|
|
// depending on the emulation speed set
|
2011-03-15 23:09:12 +00:00
|
|
|
bool ShouldSkipFrame(int skipped)
|
2010-01-07 20:01:41 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 TargetFPS = VideoInterface::GetTargetRefreshRate();
|
|
|
|
if (SConfig::GetInstance().m_EmulationSpeed > 0.0f)
|
|
|
|
TargetFPS = u32(TargetFPS * SConfig::GetInstance().m_EmulationSpeed);
|
|
|
|
const u32 frames = s_drawn_frame.load();
|
|
|
|
const bool fps_slow = !(s_timer.GetTimeDifference() < (frames + skipped) * 1000 / TargetFPS);
|
2010-01-07 20:01:41 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return fps_slow;
|
2010-01-07 20:01:41 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 02:18:03 +00:00
|
|
|
// --- Callbacks for backends / engine ---
|
2010-01-07 20:01:41 +00:00
|
|
|
|
|
|
|
// Should be called from GPU thread when a frame is drawn
|
|
|
|
void Callback_VideoCopiedToXFB(bool video_update)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (video_update)
|
|
|
|
s_drawn_frame++;
|
2015-07-06 10:25:48 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Movie::FrameUpdate();
|
2017-07-01 20:17:18 +00:00
|
|
|
|
|
|
|
if (s_frame_step)
|
|
|
|
{
|
|
|
|
s_frame_step = false;
|
|
|
|
CPU::Break();
|
2017-09-04 17:57:42 +00:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(Core::GetState());
|
2017-07-01 20:17:18 +00:00
|
|
|
}
|
2010-01-07 20:01:41 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2013-01-17 03:52:01 +00:00
|
|
|
void UpdateTitle()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 ElapseTime = (u32)s_timer.GetTimeDifference();
|
|
|
|
s_request_refresh_info = false;
|
|
|
|
SConfig& _CoreParameter = SConfig::GetInstance();
|
|
|
|
|
|
|
|
if (ElapseTime == 0)
|
|
|
|
ElapseTime = 1;
|
|
|
|
|
|
|
|
float FPS = (float)(s_drawn_frame.load() * 1000.0 / ElapseTime);
|
|
|
|
float VPS = (float)(s_drawn_video.load() * 1000.0 / ElapseTime);
|
|
|
|
float Speed = (float)(s_drawn_video.load() * (100 * 1000.0) /
|
|
|
|
(VideoInterface::GetTargetRefreshRate() * ElapseTime));
|
|
|
|
|
|
|
|
// Settings are shown the same for both extended and summary info
|
|
|
|
std::string SSettings = StringFromFormat(
|
|
|
|
"%s %s | %s | %s", PowerPC::GetCPUName(), _CoreParameter.bCPUThread ? "DC" : "SC",
|
|
|
|
g_video_backend->GetDisplayName().c_str(), _CoreParameter.bDSPHLE ? "HLE" : "LLE");
|
|
|
|
|
|
|
|
std::string SFPS;
|
|
|
|
|
|
|
|
if (Movie::IsPlayingInput())
|
2016-07-10 00:29:33 +00:00
|
|
|
SFPS = StringFromFormat("Input: %u/%u - VI: %u - FPS: %.0f - VPS: %.0f - %.0f%%",
|
2016-08-04 16:54:45 +00:00
|
|
|
(u32)Movie::GetCurrentInputCount(), (u32)Movie::GetTotalInputCount(),
|
|
|
|
(u32)Movie::GetCurrentFrame(), FPS, VPS, Speed);
|
2016-06-24 08:43:46 +00:00
|
|
|
else if (Movie::IsRecordingInput())
|
2016-07-10 00:29:33 +00:00
|
|
|
SFPS = StringFromFormat("Input: %u - VI: %u - FPS: %.0f - VPS: %.0f - %.0f%%",
|
2016-08-11 19:14:39 +00:00
|
|
|
(u32)Movie::GetCurrentInputCount(), (u32)Movie::GetCurrentFrame(), FPS,
|
|
|
|
VPS, Speed);
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SFPS = StringFromFormat("FPS: %.0f - VPS: %.0f - %.0f%%", FPS, VPS, Speed);
|
|
|
|
if (SConfig::GetInstance().m_InterfaceExtendedFPSInfo)
|
|
|
|
{
|
|
|
|
// Use extended or summary information. The summary information does not print the ticks data,
|
|
|
|
// that's more of a debugging interest, it can always be optional of course if someone is
|
|
|
|
// interested.
|
|
|
|
static u64 ticks = 0;
|
|
|
|
static u64 idleTicks = 0;
|
|
|
|
u64 newTicks = CoreTiming::GetTicks();
|
|
|
|
u64 newIdleTicks = CoreTiming::GetIdleTicks();
|
|
|
|
|
|
|
|
u64 diff = (newTicks - ticks) / 1000000;
|
|
|
|
u64 idleDiff = (newIdleTicks - idleTicks) / 1000000;
|
|
|
|
|
|
|
|
ticks = newTicks;
|
|
|
|
idleTicks = newIdleTicks;
|
|
|
|
|
|
|
|
float TicksPercentage =
|
|
|
|
(float)diff / (float)(SystemTimers::GetTicksPerSecond() / 1000000) * 100;
|
|
|
|
|
2016-04-30 12:13:04 +00:00
|
|
|
SFPS += StringFromFormat(" | CPU: ~%i MHz [Real: %i + IdleSkip: %i] / %i MHz (~%3.0f%%)",
|
|
|
|
(int)(diff), (int)(diff - idleDiff), (int)(idleDiff),
|
|
|
|
SystemTimers::GetTicksPerSecond() / 1000000, TicksPercentage);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 09:17:51 +00:00
|
|
|
|
|
|
|
std::string message = StringFromFormat("%s | %s", SSettings.c_str(), SFPS.c_str());
|
|
|
|
if (SConfig::GetInstance().m_show_active_title)
|
|
|
|
{
|
|
|
|
const std::string& title = SConfig::GetInstance().GetTitleDescription();
|
|
|
|
if (!title.empty())
|
|
|
|
message += " | " + title;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Update the audio timestretcher with the current speed
|
|
|
|
if (g_sound_stream)
|
|
|
|
{
|
2017-06-26 21:41:12 +00:00
|
|
|
Mixer* pMixer = g_sound_stream->GetMixer();
|
2016-06-24 08:43:46 +00:00
|
|
|
pMixer->UpdateSpeed((float)Speed / 100);
|
|
|
|
}
|
|
|
|
|
2017-05-15 09:17:51 +00:00
|
|
|
Host_UpdateTitle(message);
|
2014-06-20 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 12:52:09 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// During shutdown DXGI expects us to handle some messages on the UI thread.
|
|
|
|
// Therefore we can't immediately block and wait for the emu thread to shut
|
|
|
|
// down, so we join the emu thread as late as possible when the UI has already
|
|
|
|
// shut down.
|
|
|
|
// For more info read "DirectX Graphics Infrastructure (DXGI): Best Practices"
|
|
|
|
// on MSDN.
|
|
|
|
if (s_emu_thread.joinable())
|
|
|
|
s_emu_thread.join();
|
|
|
|
|
|
|
|
// Make sure there's nothing left over in case we're about to exit.
|
|
|
|
HostDispatchJobs();
|
2015-06-06 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 17:57:42 +00:00
|
|
|
void SetOnStateChangedCallback(StateChangedCallbackFunc callback)
|
2014-06-20 00:43:57 +00:00
|
|
|
{
|
2017-09-04 17:57:42 +00:00
|
|
|
s_on_state_changed_callback = std::move(callback);
|
2014-06-20 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 21:26:40 +00:00
|
|
|
void UpdateWantDeterminism(bool initial)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// For now, this value is not itself configurable. Instead, individual
|
|
|
|
// settings that depend on it, such as GPU determinism mode. should have
|
|
|
|
// override options for testing,
|
2016-07-05 16:50:06 +00:00
|
|
|
bool new_want_determinism = Movie::IsMovieActive() || NetPlay::IsNetPlayRunning();
|
2017-04-03 17:30:58 +00:00
|
|
|
if (new_want_determinism != s_wants_determinism || initial)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-09-24 23:06:47 +00:00
|
|
|
NOTICE_LOG(COMMON, "Want determinism <- %s", new_want_determinism ? "true" : "false");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-21 06:06:02 +00:00
|
|
|
RunAsCPUThread([&] {
|
|
|
|
s_wants_determinism = new_want_determinism;
|
|
|
|
const auto ios = IOS::HLE::GetIOS();
|
|
|
|
if (ios)
|
|
|
|
ios->UpdateWantDeterminism(new_want_determinism);
|
|
|
|
Fifo::UpdateWantDeterminism(new_want_determinism);
|
|
|
|
// We need to clear the cache because some parts of the JIT depend on want_determinism,
|
|
|
|
// e.g. use of FMA.
|
|
|
|
JitInterface::ClearCache();
|
|
|
|
|
|
|
|
// Don't call InitializeWiiRoot during boot, because IOS already does it.
|
|
|
|
if (!initial)
|
|
|
|
Core::InitializeWiiRoot(s_wants_determinism);
|
|
|
|
});
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-09-06 21:26:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
void QueueHostJob(std::function<void()> job, bool run_during_stop)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!job)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool send_message = false;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(s_host_jobs_lock);
|
|
|
|
send_message = s_host_jobs_queue.empty();
|
|
|
|
s_host_jobs_queue.emplace(HostJob{std::move(job), run_during_stop});
|
|
|
|
}
|
|
|
|
// If the the queue was empty then kick the Host to come and get this job.
|
|
|
|
if (send_message)
|
|
|
|
Host_Message(WM_USER_JOB_DISPATCH);
|
2016-05-12 01:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostDispatchJobs()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// WARNING: This should only run on the Host Thread.
|
|
|
|
// NOTE: This function is potentially re-entrant. If a job calls
|
|
|
|
// Core::Stop for instance then we'll enter this a second time.
|
|
|
|
std::unique_lock<std::mutex> guard(s_host_jobs_lock);
|
|
|
|
while (!s_host_jobs_queue.empty())
|
|
|
|
{
|
|
|
|
HostJob job = std::move(s_host_jobs_queue.front());
|
|
|
|
s_host_jobs_queue.pop();
|
|
|
|
|
|
|
|
// NOTE: Memory ordering is important. The booting flag needs to be
|
|
|
|
// checked first because the state transition is:
|
2017-02-05 12:39:58 +00:00
|
|
|
// Core::State::Uninitialized: s_is_booting -> s_hardware_initialized
|
2016-06-24 08:43:46 +00:00
|
|
|
// We need to check variables in the same order as the state
|
|
|
|
// transition, otherwise we race and get transient failures.
|
2016-08-05 14:04:39 +00:00
|
|
|
if (!job.run_after_stop && !s_is_booting.IsSet() && !IsRunning())
|
2016-06-24 08:43:46 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
guard.unlock();
|
|
|
|
job.job();
|
|
|
|
guard.lock();
|
|
|
|
}
|
2016-05-12 01:18:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 20:17:18 +00:00
|
|
|
// NOTE: Host Thread
|
|
|
|
void DoFrameStep()
|
|
|
|
{
|
|
|
|
if (GetState() == State::Paused)
|
|
|
|
{
|
|
|
|
// if already paused, frame advance for 1 frame
|
|
|
|
s_frame_step = true;
|
|
|
|
RequestRefreshInfo();
|
|
|
|
SetState(State::Running);
|
|
|
|
}
|
|
|
|
else if (!s_frame_step)
|
|
|
|
{
|
|
|
|
// if not paused yet, pause immediately instead
|
|
|
|
SetState(State::Paused);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // Core
|