2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 22:43:11 -04:00
|
|
|
// Refer to the license.txt file included.
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2016-11-05 02:34:06 -04:00
|
|
|
#include "Core/Core.h"
|
|
|
|
|
2019-12-30 10:48:11 +01:00
|
|
|
#include <algorithm>
|
2015-07-06 06:25:48 -04:00
|
|
|
#include <atomic>
|
2015-09-28 10:57:16 -05:00
|
|
|
#include <cstring>
|
2016-05-12 01:18:30 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
2017-05-27 15:43:40 +02:00
|
|
|
#include <variant>
|
2014-03-12 15:33:41 -04:00
|
|
|
|
2020-02-04 14:58:28 -05:00
|
|
|
#include <fmt/chrono.h>
|
2019-07-03 18:41:43 -04:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
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 05:18:15 -05:00
|
|
|
#include "AudioCommon/AudioCommon.h"
|
|
|
|
|
|
|
|
#include "Common/CPUDetect.h"
|
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-07 20:06:58 -05:00
|
|
|
#include "Common/CommonTypes.h"
|
2019-06-29 18:18:24 +10:00
|
|
|
#include "Common/Event.h"
|
2017-01-15 21:46:32 +01:00
|
|
|
#include "Common/FileUtil.h"
|
2016-08-05 16:04:39 +02:00
|
|
|
#include "Common/Flag.h"
|
2019-07-03 18:41:43 -04:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/MemoryUtil.h"
|
2017-03-02 12:15:02 -05:00
|
|
|
#include "Common/MsgHandler.h"
|
2017-05-27 18:48:40 +02:00
|
|
|
#include "Common/ScopeGuard.h"
|
2019-12-30 10:48:11 +01:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Common/Timer.h"
|
2019-07-08 14:51:09 +02:00
|
|
|
#include "Common/Version.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
|
2016-06-18 02:43:59 +02:00
|
|
|
#include "Core/Analytics.h"
|
2019-04-28 04:23:13 -04:00
|
|
|
#include "Core/Boot/Boot.h"
|
2016-10-07 21:57:07 +02:00
|
|
|
#include "Core/BootManager.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/DSPEmulator.h"
|
|
|
|
#include "Core/FifoPlayer/FifoPlayer.h"
|
2016-10-01 06:17:24 +00:00
|
|
|
#include "Core/HLE/HLE.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/HW/CPU.h"
|
|
|
|
#include "Core/HW/DSP.h"
|
2017-01-20 15:33:43 -05:00
|
|
|
#include "Core/HW/EXI/EXI.h"
|
2015-01-17 09:36:05 +11:00
|
|
|
#include "Core/HW/GCKeyboard.h"
|
2014-02-17 05:18:15 -05: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"
|
2019-04-28 04:23:13 -04:00
|
|
|
#include "Core/Host.h"
|
2017-04-29 16:11:22 +02:00
|
|
|
#include "Core/IOS/IOS.h"
|
2019-04-28 04:23:13 -04:00
|
|
|
#include "Core/MemTools.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/Movie.h"
|
2015-06-06 01:20:51 -04:00
|
|
|
#include "Core/NetPlayClient.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/NetPlayProto.h"
|
|
|
|
#include "Core/PatchEngine.h"
|
2014-09-06 03:41:17 -07:00
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
|
|
|
#include "Core/State.h"
|
2017-01-06 21:59:02 +01:00
|
|
|
#include "Core/WiiRoot.h"
|
2014-02-19 13:09:14 -05:00
|
|
|
|
2013-01-06 23:28:27 +13:00
|
|
|
#ifdef USE_GDBSTUB
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/PowerPC/GDBStub.h"
|
2013-01-06 23:28:27 +13:00
|
|
|
#endif
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2019-05-03 20:46:37 +01:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
|
|
|
#include "Core/MemoryWatcher.h"
|
|
|
|
#endif
|
|
|
|
|
2019-11-06 15:59:36 -06:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
2015-05-08 17:28:03 -04:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2015-12-31 10:27:51 -06:00
|
|
|
#include "InputCommon/GCAdapter.h"
|
2017-02-12 13:02:57 +01:00
|
|
|
|
2016-01-12 09:55:29 +01:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2016-01-12 09:55:29 +01:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2018-09-28 17:53:55 +10:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
namespace Core
|
|
|
|
{
|
2017-04-03 13:30:58 -04:00
|
|
|
static bool s_wants_determinism;
|
2014-09-06 17:26:40 -04:00
|
|
|
|
2009-01-04 21:53:41 +00:00
|
|
|
// Declarations and definitions
|
2014-09-04 08:26:11 -04:00
|
|
|
static Common::Timer s_timer;
|
2015-07-06 06:25:48 -04: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 08:26:11 -04:00
|
|
|
static bool s_is_stopping = false;
|
|
|
|
static bool s_hardware_initialized = false;
|
|
|
|
static bool s_is_started = false;
|
2016-08-05 16:04:39 +02:00
|
|
|
static Common::Flag s_is_booting;
|
2019-11-04 17:15:13 +01:00
|
|
|
static Common::Event s_done_booting;
|
2014-09-04 08:26:11 -04:00
|
|
|
static std::thread s_emu_thread;
|
2017-09-04 10:57:42 -07:00
|
|
|
static StateChangedCallbackFunc s_on_state_changed_callback;
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
static std::thread s_cpu_thread;
|
|
|
|
static bool s_request_refresh_info = false;
|
2015-12-16 00:10:47 +01:00
|
|
|
static bool s_is_throttler_temp_disabled = false;
|
2017-07-01 13:17:18 -07:00
|
|
|
static bool s_frame_step = false;
|
2010-04-17 21:02:03 +00:00
|
|
|
|
2017-02-25 20:58:33 -08:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
|
|
|
static std::unique_ptr<MemoryWatcher> s_memory_watcher;
|
|
|
|
#endif
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
struct HostJob
|
|
|
|
{
|
|
|
|
std::function<void()> job;
|
|
|
|
bool run_after_stop;
|
|
|
|
};
|
|
|
|
static std::mutex s_host_jobs_lock;
|
|
|
|
static std::queue<HostJob> s_host_jobs_queue;
|
2019-06-29 18:18:24 +10:00
|
|
|
static Common::Event s_cpu_thread_job_finished;
|
2016-05-12 01:18:30 +00:00
|
|
|
|
2018-04-17 19:00:34 -04:00
|
|
|
static thread_local bool tls_is_cpu_thread = false;
|
2015-02-19 00:33:50 -06:00
|
|
|
|
2018-10-03 23:03:22 +10:00
|
|
|
static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi);
|
2017-04-03 13:27:35 -04:00
|
|
|
|
2015-12-16 00:10:47 +01:00
|
|
|
bool GetIsThrottlerTempDisabled()
|
2014-04-30 12:50:29 +02:00
|
|
|
{
|
2015-12-16 00:10:47 +01:00
|
|
|
return s_is_throttler_temp_disabled;
|
2014-04-30 12:50:29 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 00:10:47 +01:00
|
|
|
void SetIsThrottlerTempDisabled(bool disable)
|
2014-04-30 12:50:29 +02:00
|
|
|
{
|
2015-12-16 00:10:47 +01:00
|
|
|
s_is_throttler_temp_disabled = disable;
|
2014-04-30 12:50:29 +02:00
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2015-06-06 01:20:51 -04:00
|
|
|
void FrameUpdateOnCPUThread()
|
|
|
|
{
|
|
|
|
if (NetPlay::IsNetPlayRunning())
|
2018-07-06 19:39:42 -04:00
|
|
|
NetPlay::NetPlayClient::SendTimeBase();
|
2015-06-06 01:20:51 -04:00
|
|
|
}
|
|
|
|
|
2017-02-25 20:58:33 -08:00
|
|
|
void OnFrameEnd()
|
|
|
|
{
|
|
|
|
#ifdef USE_MEMORYWATCHER
|
2019-07-10 20:43:15 +02:00
|
|
|
if (s_memory_watcher)
|
|
|
|
s_memory_watcher->Step();
|
2017-02-25 20:58:33 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-07 02:59:19 +00:00
|
|
|
// Display messages and return values
|
2009-09-07 12:40:43 +00:00
|
|
|
|
|
|
|
// Formatted stop message
|
2019-07-03 18:41:43 -04:00
|
|
|
std::string StopMessage(bool main_thread, std::string_view message)
|
2009-09-07 12:40:43 +00:00
|
|
|
{
|
2019-07-03 18:41:43 -04:00
|
|
|
return fmt::format("Stop [{} {}]\t{}", main_thread ? "Main Thread" : "Video Thread",
|
|
|
|
Common::CurrentThreadId(), message);
|
2009-09-07 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 22:46:08 -04:00
|
|
|
void DisplayMessage(std::string message, int time_in_ms)
|
2009-01-07 02:59:19 +00:00
|
|
|
{
|
2015-04-20 18:12:25 -04:00
|
|
|
if (!IsRunning())
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-10-15 12:36:11 +02:00
|
|
|
// Actually displaying non-ASCII could cause things to go pear-shaped
|
2019-12-30 10:48:11 +01:00
|
|
|
if (!std::all_of(message.begin(), message.end(), IsPrintableCharacter))
|
|
|
|
return;
|
2016-10-15 12:36:11 +02:00
|
|
|
|
2014-08-29 17:45:39 -04:00
|
|
|
Host_UpdateTitle(message);
|
2019-07-28 22:46:08 -04:00
|
|
|
OSD::AddMessage(std::move(message), time_in_ms);
|
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 07:39:58 -05: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-30 20:16:12 -08:00
|
|
|
bool IsRunningAndStarted()
|
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
return s_is_started && !s_is_stopping;
|
2011-12-30 20:16:12 -08:00
|
|
|
}
|
|
|
|
|
2010-04-17 21:02:03 +00:00
|
|
|
bool IsRunningInCurrentThread()
|
|
|
|
{
|
2011-12-30 20:16:12 -08: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 00:33:50 -06:00
|
|
|
return tls_is_cpu_thread;
|
2011-02-15 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 20:16:12 -08:00
|
|
|
bool IsGPUThread()
|
|
|
|
{
|
2015-06-12 13:56:53 +02:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2011-12-30 20:16:12 -08:00
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
return (s_emu_thread.joinable() && (s_emu_thread.get_id() == std::this_thread::get_id()));
|
2011-12-30 20:16:12 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return IsCPUThread();
|
|
|
|
}
|
|
|
|
}
|
2013-02-26 13:49:00 -06:00
|
|
|
|
2017-04-03 13:30:58 -04: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
|
2018-10-03 23:03:22 +10:00
|
|
|
bool Init(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
if (s_emu_thread.joinable())
|
2009-01-22 11:18:46 +00:00
|
|
|
{
|
2014-06-20 02:43:57 +02:00
|
|
|
if (IsRunning())
|
|
|
|
{
|
|
|
|
PanicAlertT("Emu Thread already running");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Emu Thread was stopped, synchronize with it.
|
2014-09-04 08:26:11 -04:00
|
|
|
s_emu_thread.join();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// Drain any left over jobs
|
|
|
|
HostDispatchJobs();
|
|
|
|
|
2014-09-06 17:26:40 -04:00
|
|
|
Core::UpdateWantDeterminism(/*initial*/ true);
|
|
|
|
|
2017-07-09 18:47:48 +01: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
|
|
|
|
2011-02-17 17:51:18 +00:00
|
|
|
Host_UpdateMainFrame(); // Disable any menus or buttons at boot
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2018-11-07 05:10:51 -08:00
|
|
|
// Issue any API calls which must occur on the main thread for the graphics backend.
|
|
|
|
g_video_backend->PrepareWindow(wsi);
|
|
|
|
|
2013-08-25 02:49:58 +02:00
|
|
|
// Start the emu thread
|
2019-11-04 17:15:13 +01:00
|
|
|
s_done_booting.Reset();
|
|
|
|
s_is_booting.Set();
|
2018-10-03 23:03:22 +10:00
|
|
|
s_emu_thread = std::thread(EmuThread, std::move(boot), wsi);
|
2008-12-08 05:30:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-02-17 22:48:16 +00:00
|
|
|
|
2018-03-25 22:23:46 -04:00
|
|
|
static void ResetRumble()
|
|
|
|
{
|
|
|
|
#if defined(__LIBUSB__)
|
|
|
|
GCAdapter::ResetRumble();
|
|
|
|
#endif
|
2018-03-27 16:24:36 +02:00
|
|
|
if (!Pad::IsInitialized())
|
|
|
|
return;
|
2018-03-25 22:23:46 -04:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
Pad::ResetRumble(i);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-07-04 00:52:53 +02:00
|
|
|
if (GetState() == State::Stopping || GetState() == State::Uninitialized)
|
2011-03-15 23:09:12 +00:00
|
|
|
return;
|
|
|
|
|
2015-06-12 13:56:53 +02:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2011-03-15 23:09:12 +00:00
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
s_is_stopping = true;
|
2009-09-07 12:40:43 +00:00
|
|
|
|
2018-11-30 01:20:22 -05:00
|
|
|
// Notify state changed callback
|
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(State::Stopping);
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// Dump left over jobs
|
|
|
|
HostDispatchJobs();
|
|
|
|
|
2016-01-12 22:44:58 +01:00
|
|
|
Fifo::EmulatorState(false);
|
2009-09-07 12:40:43 +00:00
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
INFO_LOG(CONSOLE, "Stop [Main Thread]\t\t---- Shutting down ----");
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2009-02-22 21:16:12 +00:00
|
|
|
// Stop the CPU
|
2011-02-06 01:56:45 +00:00
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stop CPU").c_str());
|
2016-05-12 09:17:17 +00:00
|
|
|
CPU::Stop();
|
2009-07-03 12:22:32 +00:00
|
|
|
|
2009-12-30 16:26:16 +00:00
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
{
|
2011-02-17 19:33:50 +00:00
|
|
|
// 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.
|
2011-03-15 23:09:12 +00:00
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Wait for Video Loop to exit ...").c_str());
|
2013-08-25 02:49:58 +02:00
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
g_video_backend->Video_ExitLoop();
|
2010-01-01 03:55:39 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-22 21:16:12 +00:00
|
|
|
|
2015-06-06 01:41:26 -04:00
|
|
|
void DeclareAsCPUThread()
|
2015-02-19 00:33:50 -06:00
|
|
|
{
|
|
|
|
tls_is_cpu_thread = true;
|
|
|
|
}
|
|
|
|
|
2015-06-06 01:41:26 -04:00
|
|
|
void UndeclareAsCPUThread()
|
2015-02-19 00:33:50 -06:00
|
|
|
{
|
|
|
|
tls_is_cpu_thread = false;
|
|
|
|
}
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// For the CPU Thread only.
|
|
|
|
static void CPUSetInitialExecutionState()
|
|
|
|
{
|
2018-01-26 14:41:57 +10:00
|
|
|
// The CPU starts in stepping state, and will wait until a new state is set before executing.
|
|
|
|
// SetState must be called on the host thread, so we defer it for later.
|
|
|
|
QueueHostJob([]() {
|
2017-02-05 07:39:58 -05:00
|
|
|
SetState(SConfig::GetInstance().bBootToPause ? State::Paused : State::Running);
|
2018-01-26 14:41:57 +10:00
|
|
|
Host_UpdateDisasmDialog();
|
2016-05-12 01:18:30 +00:00
|
|
|
Host_UpdateMainFrame();
|
2018-05-28 13:03:29 -04:00
|
|
|
Host_Message(HostMessageID::WMUserCreate);
|
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.
|
2017-12-28 10:13:53 +01:00
|
|
|
static void CpuThread(const std::optional<std::string>& savestate_path, bool delete_savestate)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-02-19 00:33:50 -06:00
|
|
|
DeclareAsCPUThread();
|
|
|
|
|
2015-06-12 13:56:53 +02:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2009-10-23 15:26:35 +00:00
|
|
|
if (_CoreParameter.bCPUThread)
|
2009-07-15 15:09:20 +00:00
|
|
|
Common::SetCurrentThreadName("CPU thread");
|
|
|
|
else
|
|
|
|
Common::SetCurrentThreadName("CPU-GPU thread");
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2016-06-18 02:43:59 +02:00
|
|
|
// This needs to be delayed until after the video backend is ready.
|
2019-06-23 19:26:07 +02:00
|
|
|
DolphinAnalytics::Instance().ReportGameStart();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-07-07 05:30:06 +02:00
|
|
|
if (_CoreParameter.bFastmem)
|
2011-02-18 14:27:30 +00:00
|
|
|
EMM::InstallExceptionHandler(); // Let's run under memory watch
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-05-03 20:46:37 +01:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
2017-02-25 20:58:33 -08:00
|
|
|
s_memory_watcher = std::make_unique<MemoryWatcher>();
|
2019-05-03 20:46:37 +01:00
|
|
|
#endif
|
|
|
|
|
2018-01-26 14:41:57 +10:00
|
|
|
if (savestate_path)
|
|
|
|
{
|
|
|
|
::State::LoadAs(*savestate_path);
|
|
|
|
if (delete_savestate)
|
|
|
|
File::Delete(*savestate_path);
|
|
|
|
}
|
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
s_is_started = true;
|
2016-05-12 01:18:30 +00:00
|
|
|
CPUSetInitialExecutionState();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-08 20:26:07 +13:00
|
|
|
#ifdef USE_GDBSTUB
|
2015-05-05 01:15:16 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (!_CoreParameter.gdb_socket.empty())
|
|
|
|
{
|
|
|
|
gdb_init_local(_CoreParameter.gdb_socket.data());
|
|
|
|
gdb_break();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2014-03-11 00:30:55 +13:00
|
|
|
if (_CoreParameter.iGDBPort > 0)
|
2013-01-08 20:26:07 +13:00
|
|
|
{
|
|
|
|
gdb_init(_CoreParameter.iGDBPort);
|
2013-01-08 20:56:26 +13:00
|
|
|
// break at next instruction (the first instruction)
|
|
|
|
gdb_break();
|
2013-01-08 20:26:07 +13:00
|
|
|
}
|
|
|
|
#endif
|
2013-08-25 02:49:58 +02:00
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
// Enter CPU run loop. When we leave it - we are done.
|
2015-10-04 15:06:31 -04:00
|
|
|
CPU::Run();
|
2009-07-15 15:09:20 +00:00
|
|
|
|
2019-12-09 20:06:07 +10:00
|
|
|
#ifdef USE_MEMORYWATCHER
|
|
|
|
s_memory_watcher.reset();
|
|
|
|
#endif
|
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
s_is_started = false;
|
2013-08-25 02:49:58 +02:00
|
|
|
|
2015-02-24 04:52:37 -06:00
|
|
|
if (_CoreParameter.bFastmem)
|
|
|
|
EMM::UninstallExceptionHandler();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2017-12-28 10:13:53 +01:00
|
|
|
static void FifoPlayerThread(const std::optional<std::string>& savestate_path,
|
|
|
|
bool delete_savestate)
|
2011-03-27 02:55:08 +00:00
|
|
|
{
|
2016-05-12 09:17:17 +00:00
|
|
|
DeclareAsCPUThread();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-01-26 14:41:57 +10:00
|
|
|
const SConfig& _CoreParameter = SConfig::GetInstance();
|
2011-03-27 02:55:08 +00:00
|
|
|
if (_CoreParameter.bCPUThread)
|
|
|
|
Common::SetCurrentThreadName("FIFO player thread");
|
|
|
|
else
|
|
|
|
Common::SetCurrentThreadName("FIFO-GPU thread");
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2011-03-27 02:55:08 +00:00
|
|
|
// Enter CPU run loop. When we leave it - we are done.
|
2017-05-27 15:43:40 +02:00
|
|
|
if (auto cpu_core = FifoPlayer::GetInstance().GetCPUCore())
|
2011-03-27 02:55:08 +00:00
|
|
|
{
|
2017-05-27 15:43:40 +02:00
|
|
|
PowerPC::InjectExternalCPUCore(cpu_core.get());
|
|
|
|
s_is_started = true;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-05-27 15:43:40 +02:00
|
|
|
CPUSetInitialExecutionState();
|
|
|
|
CPU::Run();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-05-27 15:43:40 +02:00
|
|
|
s_is_started = false;
|
|
|
|
PowerPC::InjectExternalCPUCore(nullptr);
|
2018-01-26 14:41:57 +10:00
|
|
|
FifoPlayer::GetInstance().Close();
|
2011-03-27 02:55:08 +00:00
|
|
|
}
|
2018-01-26 14:41:57 +10:00
|
|
|
else
|
2016-05-12 09:17:17 +00:00
|
|
|
{
|
2018-01-26 14:41:57 +10:00
|
|
|
// FIFO log does not contain any frames, cannot continue.
|
|
|
|
PanicAlert("FIFO file is invalid, cannot playback.");
|
|
|
|
FifoPlayer::GetInstance().Close();
|
|
|
|
return;
|
2016-05-12 09:17:17 +00:00
|
|
|
}
|
2011-03-27 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 08:26:11 -04: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.
|
2018-10-03 23:03:22 +10:00
|
|
|
static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-06-12 13:56:53 +02:00
|
|
|
const SConfig& core_parameter = SConfig::GetInstance();
|
2017-09-04 10:57:42 -07:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(State::Starting);
|
2017-05-27 18:48:40 +02:00
|
|
|
Common::ScopeGuard flag_guard{[] {
|
|
|
|
s_is_booting.Clear();
|
2019-11-04 17:15:13 +01:00
|
|
|
s_done_booting.Set();
|
2017-05-27 18:48:40 +02:00
|
|
|
s_is_started = false;
|
|
|
|
s_is_stopping = false;
|
2017-07-31 15:38:39 +08:00
|
|
|
s_wants_determinism = false;
|
2017-05-27 18:48:40 +02:00
|
|
|
|
2017-09-04 10:57:42 -07:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(State::Uninitialized);
|
2017-05-27 18:48:40 +02:00
|
|
|
|
|
|
|
INFO_LOG(CONSOLE, "Stop\t\t---- Shutdown complete ----");
|
|
|
|
}};
|
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
Common::SetCurrentThreadName("Emuthread - Starting");
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-02-19 00:33:50 -06:00
|
|
|
// For a time this acts as the CPU thread...
|
|
|
|
DeclareAsCPUThread();
|
2017-07-01 13:17:18 -07:00
|
|
|
s_frame_step = false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-05-27 15:43:40 +02:00
|
|
|
Movie::Init(*boot);
|
2020-02-15 21:13:29 +01:00
|
|
|
Common::ScopeGuard movie_guard{&Movie::Shutdown};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-08-25 02:49:58 +02:00
|
|
|
HW::Init();
|
2019-06-29 18:18:24 +10:00
|
|
|
|
2017-05-27 18:48:40 +02: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 19:46:31 +02: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 18:48:40 +02:00
|
|
|
}};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-09-28 17:53:55 +10:00
|
|
|
// Backend info has to be initialized before we can initialize the backend.
|
|
|
|
// This is because when we load the config, we validate it against the current backend info.
|
|
|
|
// We also should have the correct adapter selected for creating the device in Initialize().
|
|
|
|
g_video_backend->InitBackendInfo();
|
|
|
|
g_Config.Refresh();
|
|
|
|
|
2018-10-03 23:03:22 +10:00
|
|
|
if (!g_video_backend->Initialize(wsi))
|
2011-03-31 05:36:54 +00:00
|
|
|
{
|
|
|
|
PanicAlert("Failed to initialize video backend!");
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 15:09:07 +10:00
|
|
|
Common::ScopeGuard video_guard{[] { g_video_backend->Shutdown(); }};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-10-29 23:22:17 +11:00
|
|
|
if (cpu_info.HTT)
|
2015-06-12 13:56:53 +02:00
|
|
|
SConfig::GetInstance().bDSPThread = cpu_info.num_cores > 4;
|
2014-10-29 23:22:17 +11:00
|
|
|
else
|
2015-06-12 13:56:53 +02:00
|
|
|
SConfig::GetInstance().bDSPThread = cpu_info.num_cores > 2;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-09-27 15:54:07 -04:00
|
|
|
if (!DSP::GetDSPEmulator()->Initialize(core_parameter.bWii, core_parameter.bDSPThread))
|
2011-03-31 05:36:54 +00:00
|
|
|
{
|
2015-03-15 16:42:22 +01:00
|
|
|
PanicAlert("Failed to initialize DSP emulation!");
|
2011-03-31 05:36:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-10-27 20:06:35 +10:00
|
|
|
// The frontend will likely have initialized the controller interface, as it needs
|
|
|
|
// it to provide the configuration dialogs. In this case, instead of re-initializing
|
|
|
|
// entirely, we switch the window used for inputs to the render window. This way, the
|
|
|
|
// cursor position is relative to the render window, instead of the main window.
|
2015-02-04 18:05:22 +11:00
|
|
|
bool init_controllers = false;
|
|
|
|
if (!g_controller_interface.IsInit())
|
|
|
|
{
|
2018-10-03 17:34:27 +10:00
|
|
|
g_controller_interface.Initialize(wsi);
|
2016-10-11 12:54:35 -07:00
|
|
|
Pad::Initialize();
|
|
|
|
Keyboard::Initialize();
|
2015-02-04 18:05:22 +11:00
|
|
|
init_controllers = true;
|
|
|
|
}
|
2015-03-05 19:49:10 +11:00
|
|
|
else
|
|
|
|
{
|
2018-10-27 20:06:35 +10:00
|
|
|
g_controller_interface.ChangeWindow(wsi.render_surface);
|
2015-03-05 19:49:10 +11:00
|
|
|
Pad::LoadConfig();
|
|
|
|
Keyboard::LoadConfig();
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-12-25 18:07:29 +01:00
|
|
|
const std::optional<std::string> savestate_path = boot->savestate_path;
|
2017-12-28 10:13:53 +01:00
|
|
|
const bool delete_savestate = boot->delete_savestate;
|
2017-12-25 18:07:29 +01:00
|
|
|
|
2014-11-13 21:28:27 -05:00
|
|
|
// Load and Init Wiimotes - only if we are booting in Wii mode
|
2019-01-10 09:02:38 -06:00
|
|
|
bool init_wiimotes = false;
|
2016-08-23 16:19:30 +02:00
|
|
|
if (core_parameter.bWii && !SConfig::GetInstance().m_bt_passthrough_enabled)
|
2011-03-31 05:36:54 +00:00
|
|
|
{
|
2015-03-05 19:49:10 +11:00
|
|
|
if (init_controllers)
|
2017-12-25 18:07:29 +01:00
|
|
|
{
|
|
|
|
Wiimote::Initialize(savestate_path ? Wiimote::InitializeMode::DO_WAIT_FOR_WIIMOTES :
|
|
|
|
Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES);
|
2019-01-10 09:02:38 -06:00
|
|
|
init_wiimotes = true;
|
2017-12-25 18:07:29 +01:00
|
|
|
}
|
2015-03-05 19:49:10 +11:00
|
|
|
else
|
2017-12-25 18:07:29 +01:00
|
|
|
{
|
2015-03-05 19:49:10 +11:00
|
|
|
Wiimote::LoadConfig();
|
2017-12-25 18:07:29 +01:00
|
|
|
}
|
2018-11-22 06:52:48 -05:00
|
|
|
|
|
|
|
if (NetPlay::IsNetPlayRunning())
|
|
|
|
NetPlay::SetupWiimotes();
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 09:02:38 -06:00
|
|
|
Common::ScopeGuard controller_guard{[init_controllers, init_wiimotes] {
|
2017-05-27 18:48:40 +02:00
|
|
|
if (!init_controllers)
|
|
|
|
return;
|
|
|
|
|
2019-01-10 09:02:38 -06:00
|
|
|
if (init_wiimotes)
|
2019-12-09 20:06:36 +10:00
|
|
|
{
|
|
|
|
Wiimote::ResetAllWiimotes();
|
2019-01-10 09:02:38 -06:00
|
|
|
Wiimote::Shutdown();
|
2019-12-09 20:06:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
ResetRumble();
|
2019-01-10 09:02:38 -06:00
|
|
|
|
2017-05-27 18:48:40 +02:00
|
|
|
Keyboard::Shutdown();
|
|
|
|
Pad::Shutdown();
|
|
|
|
g_controller_interface.Shutdown();
|
|
|
|
}};
|
|
|
|
|
2015-01-12 22:28:12 -05:00
|
|
|
AudioCommon::InitSoundStream();
|
2020-02-15 21:13:29 +01:00
|
|
|
Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-01-12 22:28:12 -05:00
|
|
|
// The hardware is initialized.
|
2014-09-04 08:26:11 -04:00
|
|
|
s_hardware_initialized = true;
|
2016-08-05 16:04:39 +02:00
|
|
|
s_is_booting.Clear();
|
2019-11-04 17:15:13 +01:00
|
|
|
s_done_booting.Set();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-01-12 22:28:12 -05:00
|
|
|
// Set execution state to known values (CPU/FIFO/Audio Paused)
|
2014-08-30 23:36:00 -04:00
|
|
|
CPU::Break();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2011-03-31 05:36:54 +00:00
|
|
|
// Load GCM/DOL/ELF whatever ... we boot with the interpreter core
|
2017-02-04 19:18:13 -05:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-05-27 15:43:40 +02:00
|
|
|
// Determine the CPU thread function
|
2017-12-28 10:13:53 +01:00
|
|
|
void (*cpuThreadFunc)(const std::optional<std::string>& savestate_path, bool delete_savestate);
|
2017-05-27 15:43:40 +02:00
|
|
|
if (std::holds_alternative<BootParameters::DFF>(boot->parameters))
|
|
|
|
cpuThreadFunc = FifoPlayerThread;
|
|
|
|
else
|
|
|
|
cpuThreadFunc = CpuThread;
|
|
|
|
|
|
|
|
if (!CBoot::BootUp(std::move(boot)))
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-05-22 19:05:45 +02:00
|
|
|
// Initialise Wii filesystem contents.
|
|
|
|
// This is done here after Boot and not in HW to ensure that we operate
|
|
|
|
// with the correct title context since save copying requires title directories to exist.
|
2020-02-15 21:13:29 +01:00
|
|
|
Common::ScopeGuard wiifs_guard{&Core::CleanUpWiiFileSystemContents};
|
2018-05-23 13:39:53 +02:00
|
|
|
if (SConfig::GetInstance().bWii)
|
|
|
|
Core::InitializeWiiFileSystemContents();
|
|
|
|
else
|
|
|
|
wiifs_guard.Dismiss();
|
2018-05-22 19:05:45 +02:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// This adds the SyncGPU handler to CoreTiming, so now CoreTiming::Advance might block.
|
|
|
|
Fifo::Prepare();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
// Setup our core, but can't use dynarec if we are compare server
|
2018-06-15 08:11:18 -04:00
|
|
|
if (core_parameter.cpu_core != PowerPC::CPUCore::Interpreter &&
|
2014-09-27 15:54:07 -04:00
|
|
|
(!core_parameter.bRunCompareServer || core_parameter.bRunCompareClient))
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
2017-02-04 19:18:13 -05:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::JIT);
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
else
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
2017-02-04 19:18:13 -05:00
|
|
|
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 00:33:50 -06:00
|
|
|
// ENTER THE VIDEO THREAD LOOP
|
|
|
|
if (core_parameter.bCPUThread)
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
2015-02-19 00:33:50 -06:00
|
|
|
// This thread, after creating the EmuWindow, spawns a CPU
|
2009-02-20 22:04:52 +00:00
|
|
|
// thread, and then takes over and becomes the video thread
|
2014-09-27 15:54:07 -04:00
|
|
|
Common::SetCurrentThreadName("Video thread");
|
2018-01-26 14:41:57 +10:00
|
|
|
UndeclareAsCPUThread();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-01-26 14:41:57 +10:00
|
|
|
// Spawn the CPU thread. The CPU thread will signal the event that boot is complete.
|
2017-12-28 10:13:53 +01:00
|
|
|
s_cpu_thread = std::thread(cpuThreadFunc, savestate_path, delete_savestate);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
// become the GPU thread
|
2016-01-12 22:44:58 +01:00
|
|
|
Fifo::RunGpuLoop();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
// We have now exited the Video Loop
|
2014-09-27 15:54:07 -04:00
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(false, "Video Loop Ended").c_str());
|
2018-01-26 12:44:23 +10:00
|
|
|
|
|
|
|
// Join with the CPU thread.
|
|
|
|
s_cpu_thread.join();
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "CPU thread stopped.").c_str());
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
2014-09-27 15:54:07 -04:00
|
|
|
else // SingleCore mode
|
2009-07-15 15:09:20 +00:00
|
|
|
{
|
2018-01-26 12:44:23 +10:00
|
|
|
// Become the CPU thread
|
|
|
|
cpuThreadFunc(savestate_path, delete_savestate);
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-12 22:44:58 +01:00
|
|
|
#ifdef USE_GDBSTUB
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Stopping GDB ...").c_str());
|
2011-03-15 23:09:12 +00:00
|
|
|
gdb_deinit();
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "GDB stopped.").c_str());
|
|
|
|
#endif
|
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 07:39:58 -05:00
|
|
|
void SetState(State state)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-05-12 01:18:30 +00:00
|
|
|
// State cannot be controlled until the CPU Thread is operational
|
|
|
|
if (!IsRunningAndStarted())
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 09:17:17 +00:00
|
|
|
switch (state)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2017-02-05 07:39:58 -05:00
|
|
|
case State::Paused:
|
|
|
|
// NOTE: GetState() will return State::Paused immediately, even before anything has
|
2016-05-12 09:17:17 +00:00
|
|
|
// stopped (including the CPU).
|
2015-10-04 15:06:31 -04:00
|
|
|
CPU::EnableStepping(true); // Break
|
2013-05-31 07:12:59 +02:00
|
|
|
Wiimote::Pause();
|
2018-03-25 22:23:46 -04:00
|
|
|
ResetRumble();
|
2008-12-08 05:30:24 +00:00
|
|
|
break;
|
2017-02-05 07:39:58 -05:00
|
|
|
case State::Running:
|
2015-10-04 15:06:31 -04:00
|
|
|
CPU::EnableStepping(false);
|
2013-05-31 07:12:59 +02:00
|
|
|
Wiimote::Resume();
|
2008-12-08 05:30:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-04 13:23:58 +02:00
|
|
|
PanicAlert("Invalid state");
|
2009-02-20 22:04:52 +00:00
|
|
|
break;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2017-09-04 10:57:42 -07: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 07:39:58 -05:00
|
|
|
State GetState()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
if (s_is_stopping)
|
2017-02-05 07:39:58 -05:00
|
|
|
return State::Stopping;
|
2014-06-20 03:26:06 +02:00
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
if (s_hardware_initialized)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2018-02-09 22:54:35 +11:00
|
|
|
if (CPU::IsStepping() || s_frame_step)
|
2017-02-05 07:39:58 -05:00
|
|
|
return State::Paused;
|
2015-10-04 15:06:31 -04:00
|
|
|
|
2017-02-05 07:39:58 -05:00
|
|
|
return State::Running;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2014-06-20 03:26:06 +02:00
|
|
|
|
2017-07-01 14:08:58 -07:00
|
|
|
if (s_is_booting.IsSet())
|
|
|
|
return State::Starting;
|
|
|
|
|
2017-02-05 07:39:58 -05:00
|
|
|
return State::Uninitialized;
|
2019-11-04 17:15:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaitUntilDoneBooting()
|
|
|
|
{
|
|
|
|
if (s_is_booting.IsSet() || !s_hardware_initialized)
|
|
|
|
s_done_booting.Wait();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2015-06-23 23:08:10 -04:00
|
|
|
static std::string GenerateScreenshotFolderPath()
|
2009-06-28 19:47:02 +00:00
|
|
|
{
|
2016-10-29 14:42:43 +02:00
|
|
|
const std::string& gameId = SConfig::GetInstance().GetGameID();
|
2011-03-15 23:09:12 +00:00
|
|
|
std::string path = File::GetUserPath(D_SCREENSHOTS_IDX) + gameId + DIR_SEP_CHR;
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
if (!File::CreateFullPath(path))
|
2011-03-01 03:06:14 +00:00
|
|
|
{
|
2011-03-15 23:09:12 +00:00
|
|
|
// fallback to old-style screenshots, without folder.
|
|
|
|
path = File::GetUserPath(D_SCREENSHOTS_IDX);
|
2010-01-15 16:11:06 +00:00
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2015-06-23 23:08:10 -04:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GenerateScreenshotName()
|
|
|
|
{
|
2011-03-15 23:09:12 +00:00
|
|
|
// append gameId, path only contains the folder here.
|
2019-08-26 19:31:12 +02:00
|
|
|
const std::string path_prefix =
|
|
|
|
GenerateScreenshotFolderPath() + SConfig::GetInstance().GetGameID();
|
|
|
|
|
|
|
|
const std::time_t cur_time = std::time(nullptr);
|
|
|
|
const std::string base_name =
|
|
|
|
fmt::format("{}_{:%Y-%m-%d_%H-%M-%S}", path_prefix, *std::localtime(&cur_time));
|
2011-03-15 23:09:12 +00:00
|
|
|
|
2019-08-26 19:31:12 +02:00
|
|
|
// First try a filename without any suffixes, if already exists then append increasing numbers
|
|
|
|
std::string name = fmt::format("{}.png", base_name);
|
|
|
|
if (File::Exists(name))
|
2013-04-16 23:14:36 -04:00
|
|
|
{
|
2019-08-26 19:31:12 +02:00
|
|
|
for (u32 i = 1; File::Exists(name = fmt::format("{}_{}.png", base_name, i)); ++i)
|
|
|
|
;
|
2013-04-16 23:14:36 -04:00
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
|
|
|
return name;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2017-03-04 16:42:35 +10:00
|
|
|
void SaveScreenShot(bool wait_for_completion)
|
2009-06-28 19:47:02 +00:00
|
|
|
{
|
2017-02-05 07:39:58 -05:00
|
|
|
const bool bPaused = GetState() == State::Paused;
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2017-02-05 07:39:58 -05:00
|
|
|
SetState(State::Paused);
|
2009-02-27 03:56:34 +00:00
|
|
|
|
2017-03-04 16:42:35 +10:00
|
|
|
g_renderer->SaveScreenshot(GenerateScreenshotName(), wait_for_completion);
|
2013-08-25 02:49:58 +02:00
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
if (!bPaused)
|
2017-02-05 07:39:58 -05:00
|
|
|
SetState(State::Running);
|
2009-06-28 19:47:02 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2019-07-03 18:41:43 -04:00
|
|
|
void SaveScreenShot(std::string_view name, bool wait_for_completion)
|
2015-06-18 21:28:40 -04:00
|
|
|
{
|
2017-02-05 07:39:58 -05:00
|
|
|
const bool bPaused = GetState() == State::Paused;
|
2015-06-18 21:28:40 -04:00
|
|
|
|
2017-02-05 07:39:58 -05:00
|
|
|
SetState(State::Paused);
|
2015-06-18 21:28:40 -04:00
|
|
|
|
2019-08-26 19:31:12 +02:00
|
|
|
g_renderer->SaveScreenshot(fmt::format("{}{}.png", GenerateScreenshotFolderPath(), name),
|
|
|
|
wait_for_completion);
|
2015-06-18 21:28:40 -04:00
|
|
|
|
|
|
|
if (!bPaused)
|
2017-02-05 07:39:58 -05:00
|
|
|
SetState(State::Running);
|
2015-06-18 21:28:40 -04:00
|
|
|
}
|
|
|
|
|
2011-12-14 04:03:05 -08:00
|
|
|
void RequestRefreshInfo()
|
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
s_request_refresh_info = true;
|
2011-12-14 04:03:05 -08:00
|
|
|
}
|
|
|
|
|
2017-07-21 14:06:02 +08:00
|
|
|
static bool PauseAndLock(bool do_lock, bool unpause_on_unlock)
|
2011-12-30 20:16:12 -08:00
|
|
|
{
|
2016-05-12 09:17:17 +00:00
|
|
|
// WARNING: PauseAndLock is not fully threadsafe so is only valid on the Host Thread
|
2014-09-06 17:26:40 -04:00
|
|
|
if (!IsRunning())
|
|
|
|
return true;
|
|
|
|
|
2016-05-12 09:17:17 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpansionInterface::PauseAndLock(do_lock, false);
|
2011-12-30 20:16:12 -08:00
|
|
|
|
2014-11-13 21:28:27 -05:00
|
|
|
// audio has to come after CPU, because CPU thread can wait for audio thread (m_throttle).
|
2016-05-12 09:17:17 +00:00
|
|
|
DSP::GetDSPEmulator()->PauseAndLock(do_lock, false);
|
2011-12-30 20:16:12 -08:00
|
|
|
|
2014-11-13 21:28:27 -05:00
|
|
|
// video has to come after CPU, because CPU thread can wait for video thread
|
|
|
|
// (s_efbAccessRequested).
|
2016-05-12 09:17:17 +00:00
|
|
|
Fifo::PauseAndLock(do_lock, false);
|
2015-06-06 01:41:26 -04:00
|
|
|
|
2018-03-25 22:23:46 -04:00
|
|
|
ResetRumble();
|
2016-05-12 09:17:17 +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);
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 09:17:17 +00:00
|
|
|
return was_unpaused;
|
2011-12-30 20:16:12 -08:00
|
|
|
}
|
|
|
|
|
2017-07-21 14:06:02 +08: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);
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:18:24 +10:00
|
|
|
void RunOnCPUThread(std::function<void()> function, bool wait_for_completion)
|
|
|
|
{
|
|
|
|
// If the CPU thread is not running, assume there is no active CPU thread we can race against.
|
|
|
|
if (!IsRunning() || IsCPUThread())
|
|
|
|
{
|
|
|
|
function();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause the CPU (set it to stepping mode).
|
|
|
|
const bool was_running = PauseAndLock(true, true);
|
|
|
|
|
|
|
|
// Queue the job function.
|
|
|
|
if (wait_for_completion)
|
|
|
|
{
|
|
|
|
// Trigger the event after executing the function.
|
|
|
|
s_cpu_thread_job_finished.Reset();
|
|
|
|
CPU::AddCPUThreadJob([&function]() {
|
|
|
|
function();
|
|
|
|
s_cpu_thread_job_finished.Set();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CPU::AddCPUThreadJob(std::move(function));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the CPU thread, and let it execute the callback.
|
|
|
|
PauseAndLock(false, was_running);
|
|
|
|
|
|
|
|
// If we're waiting for completion, block until the event fires.
|
|
|
|
if (wait_for_completion)
|
|
|
|
{
|
|
|
|
// Periodically yield to the UI thread, so we don't deadlock.
|
|
|
|
while (!s_cpu_thread_job_finished.WaitFor(std::chrono::milliseconds(10)))
|
|
|
|
Host_YieldToUI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 00:10:47 +01: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
|
|
|
{
|
2010-01-07 20:01:41 +00:00
|
|
|
// Update info per second
|
2014-09-04 08:26:11 -04:00
|
|
|
u32 ElapseTime = (u32)s_timer.GetTimeDifference();
|
2015-07-06 06:25:48 -04:00
|
|
|
if ((ElapseTime >= 1000 && s_drawn_video.load() > 0) || s_request_refresh_info)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-01-16 22:52:01 -05:00
|
|
|
UpdateTitle();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2010-01-08 11:25:51 +00:00
|
|
|
// Reset counter
|
2014-09-04 08:26:11 -04:00
|
|
|
s_timer.Update();
|
2015-07-06 06:25:48 -04:00
|
|
|
s_drawn_frame.store(0);
|
|
|
|
s_drawn_video.store(0);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-09-04 08:26:11 -04:00
|
|
|
s_drawn_video++;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-09 21:27:45 +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)
|
|
|
|
{
|
2014-03-11 00:30:55 +13:00
|
|
|
if (video_update)
|
2015-07-06 06:25:48 -04:00
|
|
|
s_drawn_frame++;
|
2019-11-18 15:05:06 -06:00
|
|
|
}
|
2015-07-06 06:25:48 -04:00
|
|
|
|
2019-11-18 15:05:06 -06:00
|
|
|
// Called at field boundaries in `VideoInterface::Update()`
|
|
|
|
void FrameUpdate()
|
|
|
|
{
|
2011-06-24 06:50:50 +00:00
|
|
|
Movie::FrameUpdate();
|
2017-07-01 13:17:18 -07:00
|
|
|
if (s_frame_step)
|
|
|
|
{
|
|
|
|
s_frame_step = false;
|
|
|
|
CPU::Break();
|
2017-09-04 10:57:42 -07:00
|
|
|
if (s_on_state_changed_callback)
|
|
|
|
s_on_state_changed_callback(Core::GetState());
|
2017-07-01 13:17:18 -07:00
|
|
|
}
|
2010-01-07 20:01:41 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2013-01-16 22:52:01 -05:00
|
|
|
void UpdateTitle()
|
|
|
|
{
|
2014-09-04 08:26:11 -04:00
|
|
|
u32 ElapseTime = (u32)s_timer.GetTimeDifference();
|
|
|
|
s_request_refresh_info = false;
|
2015-06-12 13:56:53 +02:00
|
|
|
SConfig& _CoreParameter = SConfig::GetInstance();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-16 22:52:01 -05:00
|
|
|
if (ElapseTime == 0)
|
|
|
|
ElapseTime = 1;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-07-06 06:25:48 -04:00
|
|
|
float FPS = (float)(s_drawn_frame.load() * 1000.0 / ElapseTime);
|
|
|
|
float VPS = (float)(s_drawn_video.load() * 1000.0 / ElapseTime);
|
2016-01-21 00:27:54 -05:00
|
|
|
float Speed = (float)(s_drawn_video.load() * (100 * 1000.0) /
|
|
|
|
(VideoInterface::GetTargetRefreshRate() * ElapseTime));
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-16 22:52:01 -05:00
|
|
|
// Settings are shown the same for both extended and summary info
|
2019-07-03 18:41:43 -04:00
|
|
|
const std::string SSettings =
|
|
|
|
fmt::format("{} {} | {} | {}", PowerPC::GetCPUName(), _CoreParameter.bCPUThread ? "DC" : "SC",
|
|
|
|
g_video_backend->GetDisplayName(), _CoreParameter.bDSPHLE ? "HLE" : "LLE");
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-16 22:52:01 -05:00
|
|
|
std::string SFPS;
|
|
|
|
if (Movie::IsPlayingInput())
|
2019-07-03 18:41:43 -04:00
|
|
|
{
|
|
|
|
SFPS = fmt::format("Input: {}/{} - VI: {} - FPS: {:.0f} - VPS: {:.0f} - {:.0f}%",
|
|
|
|
Movie::GetCurrentInputCount(), Movie::GetTotalInputCount(),
|
|
|
|
Movie::GetCurrentFrame(), FPS, VPS, Speed);
|
|
|
|
}
|
2013-01-16 22:52:01 -05:00
|
|
|
else if (Movie::IsRecordingInput())
|
2019-07-03 18:41:43 -04:00
|
|
|
{
|
|
|
|
SFPS = fmt::format("Input: {} - VI: {} - FPS: {:.0f} - VPS: {:.0f} - {:.0f}%",
|
|
|
|
Movie::GetCurrentInputCount(), Movie::GetCurrentFrame(), FPS, VPS, Speed);
|
|
|
|
}
|
2013-01-16 22:52:01 -05:00
|
|
|
else
|
2014-03-22 04:15:30 -05:00
|
|
|
{
|
2019-07-03 18:41:43 -04:00
|
|
|
SFPS = fmt::format("FPS: {:.0f} - VPS: {:.0f} - {:.0f}%", FPS, VPS, Speed);
|
2014-03-22 04:15:30 -05:00
|
|
|
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();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-03-22 04:15:30 -05:00
|
|
|
u64 diff = (newTicks - ticks) / 1000000;
|
|
|
|
u64 idleDiff = (newIdleTicks - idleTicks) / 1000000;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-03-22 04:15:30 -05:00
|
|
|
ticks = newTicks;
|
|
|
|
idleTicks = newIdleTicks;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-03-22 04:15:30 -05:00
|
|
|
float TicksPercentage =
|
|
|
|
(float)diff / (float)(SystemTimers::GetTicksPerSecond() / 1000000) * 100;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-07-03 18:41:43 -04:00
|
|
|
SFPS += fmt::format(" | CPU: ~{} MHz [Real: {} + IdleSkip: {}] / {} MHz (~{:3.0f}%)", diff,
|
|
|
|
diff - idleDiff, idleDiff, SystemTimers::GetTicksPerSecond() / 1000000,
|
|
|
|
TicksPercentage);
|
2014-03-22 04:15:30 -05:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
2017-05-15 11:17:51 +02:00
|
|
|
|
2019-07-03 18:41:43 -04:00
|
|
|
std::string message = fmt::format("{} | {} | {}", Common::scm_rev_str, SSettings, SFPS);
|
2017-05-15 11:17:51 +02:00
|
|
|
if (SConfig::GetInstance().m_show_active_title)
|
|
|
|
{
|
|
|
|
const std::string& title = SConfig::GetInstance().GetTitleDescription();
|
|
|
|
if (!title.empty())
|
|
|
|
message += " | " + title;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-01-16 22:52:01 -05:00
|
|
|
// Update the audio timestretcher with the current speed
|
2014-09-29 21:35:57 -04:00
|
|
|
if (g_sound_stream)
|
2013-01-16 22:52:01 -05:00
|
|
|
{
|
2017-06-26 14:41:12 -07:00
|
|
|
Mixer* pMixer = g_sound_stream->GetMixer();
|
2013-01-16 22:52:01 -05:00
|
|
|
pMixer->UpdateSpeed((float)Speed / 100);
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-05-15 11:17:51 +02:00
|
|
|
Host_UpdateTitle(message);
|
2014-06-20 02:43:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 14:52:09 +02:00
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
// 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();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// Make sure there's nothing left over in case we're about to exit.
|
|
|
|
HostDispatchJobs();
|
2015-06-06 14:52:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 10:57:42 -07:00
|
|
|
void SetOnStateChangedCallback(StateChangedCallbackFunc callback)
|
2014-06-20 02:43:57 +02:00
|
|
|
{
|
2017-09-04 10:57:42 -07:00
|
|
|
s_on_state_changed_callback = std::move(callback);
|
2014-06-20 02:43:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-06 17:26:40 -04:00
|
|
|
void UpdateWantDeterminism(bool initial)
|
|
|
|
{
|
|
|
|
// 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 18:50:06 +02:00
|
|
|
bool new_want_determinism = Movie::IsMovieActive() || NetPlay::IsNetPlayRunning();
|
2017-04-03 13:30:58 -04:00
|
|
|
if (new_want_determinism != s_wants_determinism || initial)
|
2014-09-06 17:26:40 -04:00
|
|
|
{
|
2016-09-24 19:06:47 -04:00
|
|
|
NOTICE_LOG(COMMON, "Want determinism <- %s", new_want_determinism ? "true" : "false");
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-07-21 14:06:02 +08: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();
|
|
|
|
});
|
2014-09-06 17:26:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
void QueueHostJob(std::function<void()> job, bool run_during_stop)
|
|
|
|
{
|
|
|
|
if (!job)
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
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)
|
2018-05-28 13:03:29 -04:00
|
|
|
Host_Message(HostMessageID::WMUserJobDispatch);
|
2016-05-12 01:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostDispatchJobs()
|
|
|
|
{
|
|
|
|
// 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();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
// NOTE: Memory ordering is important. The booting flag needs to be
|
|
|
|
// checked first because the state transition is:
|
2017-02-05 07:39:58 -05:00
|
|
|
// Core::State::Uninitialized: s_is_booting -> s_hardware_initialized
|
2016-05-12 01:18:30 +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 16:04:39 +02:00
|
|
|
if (!job.run_after_stop && !s_is_booting.IsSet() && !IsRunning())
|
2016-05-12 01:18:30 +00:00
|
|
|
continue;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-05-12 01:18:30 +00:00
|
|
|
guard.unlock();
|
|
|
|
job.job();
|
|
|
|
guard.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 13:17:18 -07: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 15:59:36 -06:00
|
|
|
void UpdateInputGate()
|
|
|
|
{
|
|
|
|
ControlReference::SetInputGate(
|
|
|
|
(SConfig::GetInstance().m_BackgroundInput || Host_RendererHasFocus()) &&
|
|
|
|
!Host_UIBlocksControllerState());
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:03:13 +10:00
|
|
|
} // namespace Core
|