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>
|
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"
|
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/MathUtil.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
|
|
|
#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/AudioInterface.h"
|
|
|
|
#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/GPFifo.h"
|
|
|
|
#include "Core/HW/HW.h"
|
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
#include "Core/HW/ProcessorInterface.h"
|
|
|
|
#include "Core/HW/SystemTimers.h"
|
|
|
|
#include "Core/HW/VideoInterface.h"
|
|
|
|
#include "Core/HW/Wiimote.h"
|
2017-01-18 12:50:28 +00:00
|
|
|
#include "Core/IOS/Network/Socket.h"
|
|
|
|
#include "Core/IOS/USB/Bluetooth/BTEmu.h"
|
|
|
|
#include "Core/IOS/USB/Bluetooth/WiimoteDevice.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
|
|
|
|
2014-02-19 18:09:14 +00:00
|
|
|
#include "DiscIO/FileMonitor.h"
|
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"
|
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
|
|
|
|
{
|
2015-10-17 02:52:38 +00:00
|
|
|
// TODO: ugly, remove
|
|
|
|
bool g_aspect_wide;
|
|
|
|
|
2014-09-06 21:26:40 +00:00
|
|
|
bool g_want_determinism;
|
|
|
|
|
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
|
|
|
|
2009-01-04 21:53:41 +00:00
|
|
|
// Function forwarding
|
2010-06-05 19:03:37 +00:00
|
|
|
void Callback_WiimoteInterruptChannel(int _number, u16 _channelID, const void* _pData, u32 _Size);
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2009-01-22 11:18:46 +00:00
|
|
|
// Function declarations
|
2011-01-27 20:47:58 +00:00
|
|
|
void EmuThread();
|
2009-01-29 08:28:15 +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;
|
|
|
|
static StoppedCallbackFunc s_on_stopped_callback = nullptr;
|
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;
|
|
|
|
static int s_pause_and_lock_depth = 0;
|
2015-12-15 23:10:47 +00:00
|
|
|
static bool s_is_throttler_temp_disabled = 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
|
|
|
|
|
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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return StringFromFormat("Stop [%s %i]\t%s\t%s", main_thread ? "Main Thread" : "Video Thread",
|
2016-08-07 17:03:07 +00:00
|
|
|
Common::CurrentThreadId(), Common::MemUsage().c_str(), 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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return (GetState() != CORE_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
|
|
|
|
2010-02-02 08:10:23 +00:00
|
|
|
// This is called from the GUI thread. See the booting call schedule in
|
|
|
|
// BootManager.cpp
|
2009-01-18 01:45:53 +00:00
|
|
|
bool Init()
|
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-01-22 15:53:56 +00:00
|
|
|
INFO_LOG(OSREPORT, "Starting core = %s mode", SConfig::GetInstance().bWii ? "Wii" : "GameCube");
|
|
|
|
INFO_LOG(OSREPORT, "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
|
|
|
|
s_emu_thread = std::thread(EmuThread);
|
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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (GetState() == CORE_STOPPING)
|
|
|
|
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([] {
|
|
|
|
SetState(SConfig::GetInstance().bBootToPause ? CORE_PAUSE : CORE_RUN);
|
|
|
|
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();
|
|
|
|
}
|
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
|
|
|
|
// NOTE: EmuThread should have left us in CPU_STEPPING so nothing will happen
|
|
|
|
// until after the job is serviced.
|
|
|
|
QueueHostJob([] {
|
|
|
|
// Recheck in case Movie cleared it since.
|
|
|
|
if (!s_state_filename.empty())
|
|
|
|
State::LoadAs(s_state_filename);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2014-09-07 23:10:02 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
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();
|
|
|
|
Common::SetCurrentThreadName("FIFO-GPU thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter CPU run loop. When we leave it - we are done.
|
|
|
|
if (FifoPlayer::GetInstance().Open(_CoreParameter.m_strFilename))
|
|
|
|
{
|
|
|
|
if (auto cpu_core = FifoPlayer::GetInstance().GetCPUCore())
|
|
|
|
{
|
|
|
|
PowerPC::InjectExternalCPUCore(cpu_core.get());
|
|
|
|
s_is_started = true;
|
|
|
|
|
|
|
|
CPUSetInitialExecutionState();
|
|
|
|
CPU::Run();
|
|
|
|
|
|
|
|
s_is_started = false;
|
|
|
|
PowerPC::InjectExternalCPUCore(nullptr);
|
|
|
|
}
|
|
|
|
FifoPlayer::GetInstance().Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (CPU::GetState() != CPU::CPU_POWERDOWN)
|
|
|
|
{
|
|
|
|
s_is_started = true;
|
|
|
|
Host_Message(WM_USER_STOP);
|
|
|
|
while (CPU::GetState() != CPU::CPU_POWERDOWN)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
return;
|
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.
|
2011-01-27 20:47:58 +00:00
|
|
|
void EmuThread()
|
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();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
Common::SetCurrentThreadName("Emuthread - Starting");
|
|
|
|
|
|
|
|
if (SConfig::GetInstance().m_OCEnable)
|
|
|
|
DisplayMessage("WARNING: running at non-native CPU clock! Game may not be stable.", 8000);
|
|
|
|
DisplayMessage(cpu_info.brand_string, 8000);
|
|
|
|
DisplayMessage(cpu_info.Summarize(), 8000);
|
|
|
|
DisplayMessage(core_parameter.m_strFilename, 3000);
|
|
|
|
|
|
|
|
// For a time this acts as the CPU thread...
|
|
|
|
DeclareAsCPUThread();
|
|
|
|
|
|
|
|
Movie::Init();
|
|
|
|
|
|
|
|
HW::Init();
|
|
|
|
|
|
|
|
if (!g_video_backend->Initialize(s_window_handle))
|
|
|
|
{
|
2016-08-05 14:04:39 +00:00
|
|
|
s_is_booting.Clear();
|
2016-06-24 08:43:46 +00:00
|
|
|
PanicAlert("Failed to initialize video backend!");
|
|
|
|
Host_Message(WM_USER_STOP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2016-08-05 14:04:39 +00:00
|
|
|
s_is_booting.Clear();
|
2016-06-24 08:43:46 +00:00
|
|
|
HW::Shutdown();
|
|
|
|
g_video_backend->Shutdown();
|
|
|
|
PanicAlert("Failed to initialize DSP emulation!");
|
|
|
|
Host_Message(WM_USER_STOP);
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Activate Wiimotes which don't have source set to "None"
|
|
|
|
for (unsigned int i = 0; i != MAX_BBMOTES; ++i)
|
|
|
|
if (g_wiimote_sources[i])
|
2017-01-17 20:01:30 +00:00
|
|
|
IOS::HLE::GetUsbPointer()->AccessWiiMote(i | 0x100)->Activate(true);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioCommon::InitSoundStream();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
CBoot::BootUp();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Determine the CPU thread function
|
|
|
|
void (*cpuThreadFunc)(void);
|
|
|
|
if (core_parameter.m_BootType == SConfig::BOOT_DFF)
|
|
|
|
cpuThreadFunc = FifoPlayerThread;
|
|
|
|
else
|
|
|
|
cpuThreadFunc = CpuThread;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
while (CPU::GetState() != CPU::CPU_POWERDOWN)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
FileMon::Close();
|
|
|
|
|
|
|
|
// Stop audio thread - Actually this does nothing when using HLE
|
|
|
|
// emulation, but stops the DSP Interpreter when using LLE emulation.
|
|
|
|
DSP::GetDSPEmulator()->DSP_StopSoundStream();
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
|
|
if (init_controllers)
|
|
|
|
{
|
|
|
|
Wiimote::Shutdown();
|
|
|
|
Keyboard::Shutdown();
|
|
|
|
Pad::Shutdown();
|
2016-10-11 19:54:35 +00:00
|
|
|
g_controller_interface.Shutdown();
|
2016-06-24 08:43:46 +00:00
|
|
|
init_controllers = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_video_backend->Shutdown();
|
|
|
|
AudioCommon::ShutdownSoundStream();
|
|
|
|
|
|
|
|
INFO_LOG(CONSOLE, "%s", StopMessage(true, "Main Emu thread stopped").c_str());
|
|
|
|
|
|
|
|
// Clear on screen messages that haven't expired
|
|
|
|
OSD::ClearMessages();
|
2010-01-01 03:55:39 +00:00
|
|
|
|
2016-10-07 19:57:07 +00:00
|
|
|
BootManager::RestoreConfig();
|
2010-01-01 03:55:39 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
INFO_LOG(CONSOLE, "Stop [Video Thread]\t\t---- Shutdown complete ----");
|
|
|
|
Movie::Shutdown();
|
|
|
|
PatchEngine::Shutdown();
|
2016-10-01 06:17:24 +00:00
|
|
|
HLE::Clear();
|
2011-03-31 05:36:54 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_is_stopping = false;
|
2014-06-20 00:43:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_on_stopped_callback)
|
|
|
|
s_on_stopped_callback();
|
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
|
|
|
|
2016-05-12 09:17:17 +00:00
|
|
|
void SetState(EState 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)
|
|
|
|
{
|
|
|
|
case CORE_PAUSE:
|
|
|
|
// NOTE: GetState() will return CORE_PAUSE immediately, even before anything has
|
|
|
|
// 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;
|
|
|
|
case CORE_RUN:
|
|
|
|
CPU::EnableStepping(false);
|
|
|
|
Wiimote::Resume();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PanicAlert("Invalid state");
|
|
|
|
break;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2010-02-20 04:18:19 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
EState GetState()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_is_stopping)
|
|
|
|
return CORE_STOPPING;
|
2014-06-20 01:26:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_hardware_initialized)
|
|
|
|
{
|
|
|
|
if (CPU::IsStepping())
|
|
|
|
return CORE_PAUSE;
|
2015-10-04 19:06:31 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return CORE_RUN;
|
|
|
|
}
|
2014-06-20 01:26:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return CORE_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
|
|
|
|
2011-03-15 23:09:12 +00:00
|
|
|
void SaveScreenShot()
|
2009-06-28 19:47:02 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const bool bPaused = (GetState() == CORE_PAUSE);
|
2009-06-28 19:47:02 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SetState(CORE_PAUSE);
|
2009-02-27 03:56:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Renderer::SetScreenshot(GenerateScreenshotName());
|
2013-08-25 00:49:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!bPaused)
|
|
|
|
SetState(CORE_RUN);
|
2009-06-28 19:47:02 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2015-07-09 11:24:30 +00:00
|
|
|
void SaveScreenShot(const std::string& name)
|
2015-06-19 01:28:40 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const bool bPaused = (GetState() == CORE_PAUSE);
|
2015-06-19 01:28:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SetState(CORE_PAUSE);
|
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
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Renderer::SetScreenshot(filePath);
|
2015-06-19 01:28:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!bPaused)
|
|
|
|
SetState(CORE_RUN);
|
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
|
|
|
}
|
|
|
|
|
2016-05-12 09:17:17 +00:00
|
|
|
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
|
|
|
// let's support recursive locking to simplify things on the caller's side,
|
|
|
|
// and let's do it at this outer level in case the individual systems don't support it.
|
|
|
|
if (do_lock ? s_pause_and_lock_depth++ : --s_pause_and_lock_depth)
|
|
|
|
return true;
|
2011-12-31 04:16:12 +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
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
// This is our final "frame counter" string
|
|
|
|
std::string SMessage = StringFromFormat("%s | %s", SSettings.c_str(), SFPS.c_str());
|
|
|
|
|
|
|
|
// Update the audio timestretcher with the current speed
|
|
|
|
if (g_sound_stream)
|
|
|
|
{
|
|
|
|
CMixer* pMixer = g_sound_stream->GetMixer();
|
|
|
|
pMixer->UpdateSpeed((float)Speed / 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
Host_UpdateTitle(SMessage);
|
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
|
|
|
}
|
|
|
|
|
2014-06-20 00:43:57 +00:00
|
|
|
void SetOnStoppedCallback(StoppedCallbackFunc callback)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_on_stopped_callback = 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();
|
2016-06-24 08:43:46 +00:00
|
|
|
if (new_want_determinism != g_want_determinism || initial)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
bool was_unpaused = Core::PauseAndLock(true);
|
|
|
|
|
|
|
|
g_want_determinism = new_want_determinism;
|
2017-01-17 20:01:30 +00:00
|
|
|
IOS::HLE::WiiSockMan::GetInstance().UpdateWantDeterminism(new_want_determinism);
|
2016-06-24 08:43:46 +00:00
|
|
|
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();
|
2017-01-06 20:59:02 +00:00
|
|
|
Core::InitializeWiiRoot(g_want_determinism);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
Core::PauseAndLock(false, was_unpaused);
|
|
|
|
}
|
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:
|
|
|
|
// CORE_UNINITIALIZED: s_is_booting -> s_hardware_initialized
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // Core
|