ppsspp/Core/Core.cpp

335 lines
6.9 KiB
C++
Raw Normal View History

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
// 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/.
#include <set>
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"
#include "Core/Core.h"
#include "Core/Config.h"
#include "Core/MemMap.h"
#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
#ifdef _WIN32
2013-08-16 12:06:34 +00:00
#ifndef _XBOX
#include "Windows/OpenGLBase.h"
2014-08-17 14:07:14 +00:00
#include "Windows/D3D9Base.h"
2013-08-16 12:06:34 +00:00
#endif
#include "Windows/InputDevice.h"
#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
static event m_hStepEvent;
static recursive_mutex m_hStepMutex;
static event m_hInactiveEvent;
static recursive_mutex m_hInactiveMutex;
static bool singleStepPending = false;
static std::set<Core_ShutdownFunc> shutdownFuncs;
static bool windowHidden = false;
2013-03-31 07:02:15 +00:00
#ifdef _WIN32
InputState input_state;
#else
extern InputState input_state;
#endif
2012-11-01 15:19:01 +00:00
void Core_NotifyWindowHidden(bool hidden) {
windowHidden = hidden;
// TODO: Wait until we can react?
}
2013-10-12 08:44:12 +00:00
void Core_ListenShutdown(Core_ShutdownFunc func) {
shutdownFuncs.insert(func);
}
2013-10-12 08:44:12 +00:00
void Core_NotifyShutdown() {
for (auto it = shutdownFuncs.begin(); it != shutdownFuncs.end(); ++it) {
(*it)();
2013-10-12 08:44:12 +00:00
}
}
2013-10-12 08:44:12 +00:00
void Core_ErrorPause() {
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() {
Core_UpdateState(CORE_POWERDOWN);
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() {
return coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME || coreStatePending;
}
2013-10-12 08:44:12 +00:00
bool Core_IsInactive() {
return coreState != CORE_RUNNING && coreState != CORE_NEXTFRAME && !coreStatePending;
}
2013-10-12 08:44:12 +00:00
void Core_WaitInactive() {
while (Core_IsActive()) {
m_hInactiveEvent.wait(m_hInactiveMutex);
2013-10-12 08:44:12 +00:00
}
}
2013-10-12 08:44:12 +00:00
void Core_WaitInactive(int milliseconds) {
if (Core_IsActive()) {
m_hInactiveEvent.wait_for(m_hInactiveMutex, milliseconds);
2013-10-12 08:44:12 +00:00
}
}
bool UpdateScreenScale(int width, int height) {
2013-08-17 04:41:40 +00:00
g_dpi = 72;
g_dpi_scale = 1.0f;
#if defined(__SYMBIAN32__)
2014-02-19 17:37:06 +00:00
g_dpi_scale = 1.4f;
#elif defined(_WIN32)
2015-01-01 03:41:14 +00:00
if (width < 480 + 80) {
2013-08-17 04:41:40 +00:00
g_dpi_scale = 2.0f;
}
#endif
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
}
void UpdateRunLoop() {
if (windowHidden && g_Config.bPauseWhenMinimized) {
sleep_ms(16);
return;
}
NativeUpdate(input_state);
{
lock_guard guard(input_state.lock);
EndInputState(&input_state);
}
if (GetUIState() != UISTATE_EXIT) {
2014-02-10 01:35:43 +00:00
NativeRender();
}
}
2014-08-23 07:48:08 +00:00
#if defined(USING_WIN_UI)
2014-08-17 14:07:14 +00:00
void GPU_SwapBuffers() {
switch (g_Config.iGPUBackend) {
case GPU_BACKEND_OPENGL:
GL_SwapBuffers();
break;
case GPU_BACKEND_DIRECT3D9:
D3D9_SwapBuffers();
break;
}
}
2014-08-23 07:48:08 +00:00
#endif
void Core_RunLoop() {
while ((GetUIState() != UISTATE_INGAME || !PSP_IsInited()) && GetUIState() != UISTATE_EXIT) {
time_update();
#if defined(USING_WIN_UI)
double startTime = time_now_d();
UpdateRunLoop();
2013-03-29 17:50:08 +00:00
// Simple throttling to not burn the GPU in the menu.
time_update();
double diffTime = time_now_d() - startTime;
int sleepTime = (int)(1000.0 / 60.0) - (int)(diffTime * 1000.0);
if (sleepTime > 0)
Sleep(sleepTime);
if (!windowHidden) {
2014-08-17 14:07:14 +00:00
GPU_SwapBuffers();
}
#else
UpdateRunLoop();
#endif
}
while (!coreState && GetUIState() == UISTATE_INGAME) {
time_update();
UpdateRunLoop();
#if defined(USING_WIN_UI)
if (!windowHidden && !Core_IsStepping()) {
2014-08-17 14:07:14 +00:00
GPU_SwapBuffers();
}
#endif
}
2012-11-01 15:19:01 +00:00
}
2013-10-12 08:44:12 +00:00
void Core_DoSingleStep() {
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.
void Core_Run()
{
2012-12-26 07:52:40 +00:00
#if defined(_DEBUG)
2012-11-01 15:19:01 +00:00
host->UpdateDisassembly();
#endif
#if !defined(USING_QT_UI) || defined(MOBILE_DEVICE)
2012-11-01 15:19:01 +00:00
while (true)
#endif
2012-11-01 15:19:01 +00:00
{
reswitch:
if (GetUIState() != UISTATE_INGAME) {
2013-10-12 08:44:12 +00:00
CoreStateProcessed();
if (GetUIState() == UISTATE_EXIT) {
return;
}
Core_RunLoop();
#if defined(USING_QT_UI) && !defined(MOBILE_DEVICE)
2013-10-14 14:40:25 +00:00
return;
#else
continue;
2013-10-14 14:40:25 +00:00
#endif
}
switch (coreState)
2012-11-01 15:19:01 +00:00
{
case CORE_RUNNING:
// enter a fast runloop
2012-11-01 15:19:01 +00:00
Core_RunLoop();
break;
// We should never get here on Android.
case CORE_STEPPING:
singleStepPending = false;
2013-10-12 08:44:12 +00:00
CoreStateProcessed();
// Check if there's any pending savestate actions.
SaveState::Process();
if (coreState == CORE_POWERDOWN) {
return;
}
// wait for step command..
#if defined(USING_QT_UI) || defined(_DEBUG)
2013-02-17 00:06:06 +00:00
host->UpdateDisassembly();
host->UpdateMemView();
host->SendCoreWait(true);
#endif
2012-11-01 15:19:01 +00:00
m_hStepEvent.wait(m_hStepMutex);
#if defined(USING_QT_UI) || defined(_DEBUG)
host->SendCoreWait(false);
#endif
#if defined(USING_QT_UI) && !defined(MOBILE_DEVICE)
if (coreState != CORE_STEPPING)
return;
#endif
// 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();
// update disasm dialog
#if defined(USING_QT_UI) || defined(_DEBUG)
2012-11-01 15:19:01 +00:00
host->UpdateDisassembly();
host->UpdateMemView();
#endif
break;
case CORE_POWERUP:
2012-11-01 15:19:01 +00:00
case CORE_POWERDOWN:
case CORE_ERROR:
// Exit loop!!
2013-10-12 08:44:12 +00:00
CoreStateProcessed();
2013-03-23 17:30:52 +00:00
return;
case CORE_NEXTFRAME:
2012-11-01 15:19:01 +00:00
return;
}
}
}
2013-03-29 20:21:27 +00:00
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();
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;
coreStatePending = false;
2012-11-01 15:19:01 +00:00
m_hStepEvent.notify_one();
}
}