2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2013-09-28 07:32:45 +00:00
|
|
|
#include <set>
|
2013-12-30 09:17:11 +00:00
|
|
|
|
2013-03-29 17:50:08 +00:00
|
|
|
#include "base/NativeApp.h"
|
|
|
|
#include "base/display.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "base/mutex.h"
|
|
|
|
#include "base/timeutil.h"
|
2013-03-29 17:50:08 +00:00
|
|
|
#include "input/input_state.h"
|
2015-05-12 17:45:14 +00:00
|
|
|
#include "profiler/profiler.h"
|
2013-03-29 17:50:08 +00:00
|
|
|
|
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "Core/MemMap.h"
|
2013-09-15 01:43:23 +00:00
|
|
|
#include "Core/SaveState.h"
|
2013-03-29 17:50:08 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
2013-12-29 23:11:29 +00:00
|
|
|
|
2013-02-18 22:25:06 +00:00
|
|
|
#ifdef _WIN32
|
2015-11-03 22:22:09 +00:00
|
|
|
#include "Windows/GPU/WindowsGLContext.h"
|
|
|
|
#include "Windows/GPU/D3D9Context.h"
|
2015-10-10 14:41:19 +00:00
|
|
|
#include "Windows/GPU/WindowsVulkanContext.h"
|
2013-03-29 19:51:14 +00:00
|
|
|
#include "Windows/InputDevice.h"
|
2013-02-18 22:25:06 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#include "Host.h"
|
|
|
|
|
2013-03-29 17:50:08 +00:00
|
|
|
#include "Core/Debugger/Breakpoints.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-03-01 06:20:14 +00:00
|
|
|
// Time until we stop considering the core active without user input.
|
|
|
|
// Should this be configurable? 2 hours currently.
|
|
|
|
static const double ACTIVITY_IDLE_TIMEOUT = 2.0 * 3600.0;
|
|
|
|
|
2013-09-15 01:43:23 +00:00
|
|
|
static event m_hStepEvent;
|
|
|
|
static recursive_mutex m_hStepMutex;
|
|
|
|
static event m_hInactiveEvent;
|
|
|
|
static recursive_mutex m_hInactiveMutex;
|
|
|
|
static bool singleStepPending = false;
|
2013-09-28 07:32:45 +00:00
|
|
|
static std::set<Core_ShutdownFunc> shutdownFuncs;
|
2014-06-29 11:11:06 +00:00
|
|
|
static bool windowHidden = false;
|
2015-03-01 06:20:14 +00:00
|
|
|
static double lastActivity = 0.0;
|
|
|
|
static double lastKeepAwake = 0.0;
|
2015-12-31 15:59:40 +00:00
|
|
|
static GraphicsContext *graphicsContext;
|
2013-02-23 20:59:40 +00:00
|
|
|
|
2013-03-29 21:56:57 +00:00
|
|
|
extern InputState input_state;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2016-01-01 13:08:23 +00:00
|
|
|
void Core_SetGraphicsContext(GraphicsContext *ctx) {
|
|
|
|
graphicsContext = ctx;
|
|
|
|
}
|
|
|
|
|
2014-06-29 11:11:06 +00:00
|
|
|
void Core_NotifyWindowHidden(bool hidden) {
|
|
|
|
windowHidden = hidden;
|
|
|
|
// TODO: Wait until we can react?
|
|
|
|
}
|
|
|
|
|
2015-03-01 06:20:14 +00:00
|
|
|
void Core_NotifyActivity() {
|
|
|
|
lastActivity = time_now_d();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_ListenShutdown(Core_ShutdownFunc func) {
|
2013-09-28 07:32:45 +00:00
|
|
|
shutdownFuncs.insert(func);
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_NotifyShutdown() {
|
|
|
|
for (auto it = shutdownFuncs.begin(); it != shutdownFuncs.end(); ++it) {
|
2013-09-28 07:32:45 +00:00
|
|
|
(*it)();
|
2013-10-12 08:44:12 +00:00
|
|
|
}
|
2013-09-28 07:32:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_ErrorPause() {
|
2013-02-23 20:59:40 +00:00
|
|
|
Core_UpdateState(CORE_ERROR);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_Halt(const char *msg) {
|
2012-11-01 15:19:01 +00:00
|
|
|
Core_EnableStepping(true);
|
|
|
|
ERROR_LOG(CPU, "CPU HALTED : %s",msg);
|
|
|
|
_dbg_update_();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_Stop() {
|
2013-02-23 20:59:40 +00:00
|
|
|
Core_UpdateState(CORE_POWERDOWN);
|
2013-10-01 06:34:33 +00:00
|
|
|
Core_NotifyShutdown();
|
2012-11-01 15:19:01 +00:00
|
|
|
m_hStepEvent.notify_one();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
bool Core_IsStepping() {
|
2012-11-01 15:19:01 +00:00
|
|
|
return coreState == CORE_STEPPING || coreState == CORE_POWERDOWN;
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
bool Core_IsActive() {
|
2013-06-08 00:32:07 +00:00
|
|
|
return coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME || coreStatePending;
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
bool Core_IsInactive() {
|
2013-02-23 20:59:40 +00:00
|
|
|
return coreState != CORE_RUNNING && coreState != CORE_NEXTFRAME && !coreStatePending;
|
|
|
|
}
|
2013-02-23 21:21:28 +00:00
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_WaitInactive() {
|
|
|
|
while (Core_IsActive()) {
|
2013-02-23 20:59:40 +00:00
|
|
|
m_hInactiveEvent.wait(m_hInactiveMutex);
|
2013-10-12 08:44:12 +00:00
|
|
|
}
|
2013-02-23 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_WaitInactive(int milliseconds) {
|
|
|
|
if (Core_IsActive()) {
|
2013-07-01 05:47:39 +00:00
|
|
|
m_hInactiveEvent.wait_for(m_hInactiveMutex, milliseconds);
|
2013-10-12 08:44:12 +00:00
|
|
|
}
|
2013-02-23 21:21:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 16:04:05 +00:00
|
|
|
bool UpdateScreenScale(int width, int height, bool smallWindow) {
|
2013-08-17 04:41:40 +00:00
|
|
|
g_dpi = 72;
|
|
|
|
g_dpi_scale = 1.0f;
|
2014-12-28 21:19:19 +00:00
|
|
|
#if defined(__SYMBIAN32__)
|
2014-02-19 17:37:06 +00:00
|
|
|
g_dpi_scale = 1.4f;
|
2016-05-21 14:56:41 +00:00
|
|
|
#endif
|
2015-05-15 16:04:05 +00:00
|
|
|
if (smallWindow) {
|
2013-08-17 04:41:40 +00:00
|
|
|
g_dpi_scale = 2.0f;
|
2013-04-01 02:15:59 +00:00
|
|
|
}
|
2016-05-21 14:56:41 +00:00
|
|
|
|
2014-12-28 21:19:19 +00:00
|
|
|
pixel_in_dps = 1.0f / g_dpi_scale;
|
|
|
|
|
|
|
|
int new_dp_xres = width * g_dpi_scale;
|
|
|
|
int new_dp_yres = height * g_dpi_scale;
|
|
|
|
|
|
|
|
bool dp_changed = new_dp_xres != dp_xres || new_dp_yres != dp_yres;
|
|
|
|
bool px_changed = pixel_xres != width || pixel_yres != height;
|
|
|
|
|
|
|
|
if (dp_changed || px_changed) {
|
|
|
|
dp_xres = new_dp_xres;
|
|
|
|
dp_yres = new_dp_yres;
|
|
|
|
pixel_xres = width;
|
|
|
|
pixel_yres = height;
|
|
|
|
|
|
|
|
NativeResized();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-03-29 17:50:08 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 14:17:34 +00:00
|
|
|
void UpdateRunLoop() {
|
2014-06-30 17:04:44 +00:00
|
|
|
if (windowHidden && g_Config.bPauseWhenMinimized) {
|
2014-07-01 03:29:39 +00:00
|
|
|
sleep_ms(16);
|
2014-06-29 14:17:34 +00:00
|
|
|
return;
|
2014-07-01 03:29:39 +00:00
|
|
|
}
|
2014-03-24 05:18:54 +00:00
|
|
|
NativeUpdate(input_state);
|
|
|
|
|
2013-10-12 08:40:33 +00:00
|
|
|
{
|
2014-03-24 05:18:54 +00:00
|
|
|
lock_guard guard(input_state.lock);
|
2013-10-12 08:40:33 +00:00
|
|
|
EndInputState(&input_state);
|
|
|
|
}
|
2014-03-24 05:18:54 +00:00
|
|
|
|
2014-06-22 07:38:46 +00:00
|
|
|
if (GetUIState() != UISTATE_EXIT) {
|
2015-12-31 15:59:40 +00:00
|
|
|
NativeRender(graphicsContext);
|
2014-02-10 01:35:43 +00:00
|
|
|
}
|
2013-10-12 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 15:59:40 +00:00
|
|
|
void Core_RunLoop(GraphicsContext *ctx) {
|
|
|
|
graphicsContext = ctx;
|
2014-06-22 07:38:46 +00:00
|
|
|
while ((GetUIState() != UISTATE_INGAME || !PSP_IsInited()) && GetUIState() != UISTATE_EXIT) {
|
2014-06-29 11:11:06 +00:00
|
|
|
time_update();
|
2014-06-29 14:17:34 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2013-10-12 08:40:33 +00:00
|
|
|
double startTime = time_now_d();
|
2014-06-29 14:17:34 +00:00
|
|
|
UpdateRunLoop();
|
2013-10-12 08:40:33 +00:00
|
|
|
|
2013-03-29 17:50:08 +00:00
|
|
|
// Simple throttling to not burn the GPU in the menu.
|
2013-10-12 08:40:33 +00:00
|
|
|
time_update();
|
|
|
|
double diffTime = time_now_d() - startTime;
|
2014-07-15 15:03:25 +00:00
|
|
|
int sleepTime = (int)(1000.0 / 60.0) - (int)(diffTime * 1000.0);
|
2013-10-12 08:40:33 +00:00
|
|
|
if (sleepTime > 0)
|
2014-07-15 15:03:25 +00:00
|
|
|
Sleep(sleepTime);
|
2014-06-29 11:11:06 +00:00
|
|
|
if (!windowHidden) {
|
2015-12-31 15:59:40 +00:00
|
|
|
ctx->SwapBuffers();
|
2014-06-29 11:11:06 +00:00
|
|
|
}
|
2013-10-12 08:40:33 +00:00
|
|
|
#else
|
|
|
|
UpdateRunLoop();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-22 07:38:46 +00:00
|
|
|
while (!coreState && GetUIState() == UISTATE_INGAME) {
|
2013-10-12 08:40:33 +00:00
|
|
|
time_update();
|
|
|
|
UpdateRunLoop();
|
2014-02-08 19:11:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2014-07-01 03:29:39 +00:00
|
|
|
if (!windowHidden && !Core_IsStepping()) {
|
2015-12-31 15:59:40 +00:00
|
|
|
ctx->SwapBuffers();
|
2015-03-01 06:20:14 +00:00
|
|
|
|
|
|
|
// Keep the system awake for longer than normal for cutscenes and the like.
|
|
|
|
const double now = time_now_d();
|
|
|
|
if (now < lastActivity + ACTIVITY_IDLE_TIMEOUT) {
|
|
|
|
// Only resetting it ever prime number seconds in case the call is expensive.
|
|
|
|
// Using a prime number to ensure there's no interaction with other periodic events.
|
|
|
|
if (now - lastKeepAwake > 89.0 || now < lastKeepAwake) {
|
|
|
|
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
|
|
|
|
lastKeepAwake = now;
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 07:34:27 +00:00
|
|
|
}
|
2013-03-29 21:56:57 +00:00
|
|
|
#endif
|
2013-02-18 22:25:06 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_DoSingleStep() {
|
2013-09-15 01:43:23 +00:00
|
|
|
singleStepPending = true;
|
|
|
|
m_hStepEvent.notify_one();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_UpdateSingleStep() {
|
2012-11-01 15:19:01 +00:00
|
|
|
m_hStepEvent.notify_one();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_SingleStep() {
|
2012-11-01 15:19:01 +00:00
|
|
|
currentMIPS->SingleStep();
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
static inline void CoreStateProcessed() {
|
|
|
|
if (coreStatePending) {
|
|
|
|
coreStatePending = false;
|
|
|
|
m_hInactiveEvent.notify_one();
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Some platforms, like Android, do not call this function but handle things on their own.
|
2015-12-31 15:59:40 +00:00
|
|
|
void Core_Run(GraphicsContext *ctx)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-12-26 07:52:40 +00:00
|
|
|
#if defined(_DEBUG)
|
2012-11-01 15:19:01 +00:00
|
|
|
host->UpdateDisassembly();
|
|
|
|
#endif
|
2014-02-08 18:29:22 +00:00
|
|
|
#if !defined(USING_QT_UI) || defined(MOBILE_DEVICE)
|
2012-11-01 15:19:01 +00:00
|
|
|
while (true)
|
2013-01-13 23:29:42 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
reswitch:
|
2014-06-22 07:38:46 +00:00
|
|
|
if (GetUIState() != UISTATE_INGAME) {
|
2013-10-12 08:44:12 +00:00
|
|
|
CoreStateProcessed();
|
2014-06-22 07:38:46 +00:00
|
|
|
if (GetUIState() == UISTATE_EXIT) {
|
2013-10-12 08:40:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-12-31 15:59:40 +00:00
|
|
|
Core_RunLoop(ctx);
|
2014-02-08 18:29:22 +00:00
|
|
|
#if defined(USING_QT_UI) && !defined(MOBILE_DEVICE)
|
2013-10-14 14:40:25 +00:00
|
|
|
return;
|
|
|
|
#else
|
2013-10-12 08:40:33 +00:00
|
|
|
continue;
|
2013-10-14 14:40:25 +00:00
|
|
|
#endif
|
2013-10-12 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 09:26:14 +00:00
|
|
|
switch (coreState)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
case CORE_RUNNING:
|
2013-09-15 01:43:23 +00:00
|
|
|
// enter a fast runloop
|
2015-12-31 15:59:40 +00:00
|
|
|
Core_RunLoop(ctx);
|
2012-11-01 15:19:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// We should never get here on Android.
|
|
|
|
case CORE_STEPPING:
|
2013-09-15 01:43:23 +00:00
|
|
|
singleStepPending = false;
|
2013-10-12 08:44:12 +00:00
|
|
|
CoreStateProcessed();
|
2013-02-23 20:59:40 +00:00
|
|
|
|
2013-09-15 01:43:23 +00:00
|
|
|
// Check if there's any pending savestate actions.
|
|
|
|
SaveState::Process();
|
2013-09-15 04:19:10 +00:00
|
|
|
if (coreState == CORE_POWERDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-15 01:43:23 +00:00
|
|
|
|
|
|
|
// wait for step command..
|
2013-02-10 15:36:06 +00:00
|
|
|
#if defined(USING_QT_UI) || defined(_DEBUG)
|
2013-02-17 00:06:06 +00:00
|
|
|
host->UpdateDisassembly();
|
|
|
|
host->UpdateMemView();
|
2013-02-10 15:36:06 +00:00
|
|
|
host->SendCoreWait(true);
|
|
|
|
#endif
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
m_hStepEvent.wait(m_hStepMutex);
|
2013-02-10 15:36:06 +00:00
|
|
|
|
|
|
|
#if defined(USING_QT_UI) || defined(_DEBUG)
|
|
|
|
host->SendCoreWait(false);
|
|
|
|
#endif
|
2014-02-08 18:29:22 +00:00
|
|
|
#if defined(USING_QT_UI) && !defined(MOBILE_DEVICE)
|
2013-09-15 01:43:23 +00:00
|
|
|
if (coreState != CORE_STEPPING)
|
2013-01-13 23:29:42 +00:00
|
|
|
return;
|
|
|
|
#endif
|
2013-09-15 01:43:23 +00:00
|
|
|
// No step pending? Let's go back to the wait.
|
|
|
|
if (!singleStepPending || coreState != CORE_STEPPING) {
|
|
|
|
if (coreState == CORE_POWERDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
goto reswitch;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
Core_SingleStep();
|
2013-09-15 01:43:23 +00:00
|
|
|
// update disasm dialog
|
2013-02-10 15:36:06 +00:00
|
|
|
#if defined(USING_QT_UI) || defined(_DEBUG)
|
2012-11-01 15:19:01 +00:00
|
|
|
host->UpdateDisassembly();
|
|
|
|
host->UpdateMemView();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2013-11-10 04:56:11 +00:00
|
|
|
case CORE_POWERUP:
|
2012-11-01 15:19:01 +00:00
|
|
|
case CORE_POWERDOWN:
|
|
|
|
case CORE_ERROR:
|
2013-09-15 01:43:23 +00:00
|
|
|
// Exit loop!!
|
2013-10-12 08:44:12 +00:00
|
|
|
CoreStateProcessed();
|
2013-03-23 17:30:52 +00:00
|
|
|
|
2013-02-23 20:59:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case CORE_NEXTFRAME:
|
2012-11-01 15:19:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 08:44:12 +00:00
|
|
|
void Core_EnableStepping(bool step) {
|
|
|
|
if (step) {
|
2012-11-01 15:19:01 +00:00
|
|
|
sleep_ms(1);
|
2012-12-26 07:52:40 +00:00
|
|
|
#if defined(_DEBUG)
|
2012-11-01 15:19:01 +00:00
|
|
|
host->SetDebugMode(true);
|
|
|
|
#endif
|
2013-04-20 16:41:17 +00:00
|
|
|
m_hStepEvent.reset();
|
2013-02-23 20:59:40 +00:00
|
|
|
Core_UpdateState(CORE_STEPPING);
|
2013-10-12 08:44:12 +00:00
|
|
|
} else {
|
2012-12-26 07:52:40 +00:00
|
|
|
#if defined(_DEBUG)
|
2012-11-01 15:19:01 +00:00
|
|
|
host->SetDebugMode(false);
|
|
|
|
#endif
|
|
|
|
coreState = CORE_RUNNING;
|
2013-02-23 20:59:40 +00:00
|
|
|
coreStatePending = false;
|
2012-11-01 15:19:01 +00:00
|
|
|
m_hStepEvent.notify_one();
|
|
|
|
}
|
|
|
|
}
|