ppsspp/Core/System.cpp

422 lines
10 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/.
#ifdef _WIN32
#include "Common/CommonWindows.h"
#include <ShlObj.h>
#include <string>
2013-10-14 06:22:13 +00:00
#include <codecvt>
#endif
#include "native/thread/thread.h"
#include "native/thread/threadutil.h"
#include "native/base/mutex.h"
#include "util/text/utf8.h"
2013-07-29 15:12:46 +00:00
#include "Core/MemMap.h"
#include "Core/MIPS/MIPS.h"
#include "Core/MIPS/JitCommon/JitCommon.h"
#include "Core/System.h"
#include "Core/PSPMixer.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/sceKernel.h"
#include "Core/HLE/sceKernelMemory.h"
#include "Core/HLE/sceAudio.h"
#include "Core/Config.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/CoreParameter.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "Core/Loaders.h"
#include "Core/PSPLoaders.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/SaveState.h"
2013-07-29 15:12:46 +00:00
#include "Common/LogManager.h"
2012-11-01 15:19:01 +00:00
#include "GPU/GPUState.h"
#include "GPU/GPUInterface.h"
enum CPUThreadState {
CPU_THREAD_NOT_RUNNING,
CPU_THREAD_PENDING,
CPU_THREAD_STARTING,
CPU_THREAD_RUNNING,
CPU_THREAD_SHUTDOWN,
CPU_THREAD_EXECUTE,
};
2012-11-01 15:19:01 +00:00
MetaFileSystem pspFileSystem;
ParamSFOData g_paramSFO;
2013-03-29 18:32:20 +00:00
GlobalUIState globalUIState;
2012-11-01 15:19:01 +00:00
static CoreParameter coreParameter;
2013-01-13 15:46:45 +00:00
static PSPMixer *mixer;
static std::thread *cpuThread = NULL;
static recursive_mutex cpuThreadLock;
static condition_variable cpuThreadCond;
2013-10-12 09:08:00 +00:00
static condition_variable cpuThreadReplyCond;
static u64 cpuThreadUntil;
2012-11-01 15:19:01 +00:00
// This can be read and written from ANYWHERE.
volatile CoreState coreState = CORE_STEPPING;
// Note: intentionally not used for CORE_NEXTFRAME.
volatile bool coreStatePending = false;
static volatile CPUThreadState cpuThreadState = CPU_THREAD_NOT_RUNNING;
bool IsAudioInitialised() {
return mixer != NULL;
}
void Audio_Init() {
if(mixer == NULL) {
mixer = new PSPMixer();
host->InitSound(mixer);
}
}
bool IsOnSeparateCPUThread() {
if (cpuThread != NULL) {
return cpuThread->get_id() == std::this_thread::get_id();
} else {
return false;
}
}
void CPU_SetState(CPUThreadState to) {
lock_guard guard(cpuThreadLock);
cpuThreadState = to;
cpuThreadCond.notify_one();
cpuThreadReplyCond.notify_one();
}
bool CPU_NextState(CPUThreadState from, CPUThreadState to) {
2013-10-12 09:08:00 +00:00
lock_guard guard(cpuThreadLock);
if (cpuThreadState == from) {
CPU_SetState(to);
return true;
} else {
return false;
}
}
bool CPU_NextStateNot(CPUThreadState from, CPUThreadState to) {
2013-10-12 09:08:00 +00:00
lock_guard guard(cpuThreadLock);
if (cpuThreadState != from) {
CPU_SetState(to);
return true;
} else {
return false;
}
}
bool CPU_IsReady() {
return cpuThreadState == CPU_THREAD_RUNNING || cpuThreadState == CPU_THREAD_NOT_RUNNING;
}
bool CPU_IsShutdown() {
return cpuThreadState == CPU_THREAD_NOT_RUNNING;
}
bool CPU_HasPendingAction() {
return cpuThreadState != CPU_THREAD_RUNNING;
}
2013-10-12 09:08:00 +00:00
void CPU_WaitStatus(condition_variable &cond, bool (*pred)()) {
lock_guard guard(cpuThreadLock);
while (!pred()) {
cond.wait(cpuThreadLock);
}
}
2013-08-10 20:50:21 +00:00
void CPU_Shutdown();
void CPU_Init() {
2012-11-01 15:19:01 +00:00
currentCPU = &mipsr4k;
numCPUs = 1;
// Default memory settings
// Seems to be the safest place currently..
Memory::g_MemorySize = Memory::RAM_NORMAL_SIZE; // 32 MB of ram by default
g_RemasterMode = false;
g_DoubleTextureCoordinates = false;
std::string filename = coreParameter.fileToStart;
2013-08-10 17:56:47 +00:00
IdentifiedFileType type = Identify_File(filename);
2013-08-10 17:56:47 +00:00
switch (type) {
case FILETYPE_PSP_ISO:
case FILETYPE_PSP_ISO_NP:
case FILETYPE_PSP_DISC_DIRECTORY:
InitMemoryForGameISO(filename);
2013-08-10 17:56:47 +00:00
break;
default:
break;
}
Memory::Init();
2012-11-01 15:19:01 +00:00
mipsr4k.Reset();
mipsr4k.pc = 0;
host->AttemptLoadSymbolMap();
if (coreParameter.enableSound) {
Audio_Init();
2012-11-01 15:19:01 +00:00
}
if (coreParameter.disableG3Dlog) {
2013-01-15 12:57:35 +00:00
LogManager::GetInstance()->SetEnable(LogTypes::G3D, false);
}
CoreTiming::Init();
2012-11-01 15:19:01 +00:00
// Init all the HLE modules
HLEInit();
// TODO: Check Game INI here for settings, patches and cheats, and modify coreParameter accordingly
// Why did we check for CORE_POWERDOWN here?
if (!LoadFile(filename, &coreParameter.errorString)) {
2013-08-10 20:50:21 +00:00
CPU_Shutdown();
2012-11-01 15:19:01 +00:00
coreParameter.fileToStart = "";
CPU_SetState(CPU_THREAD_NOT_RUNNING);
return;
2012-11-01 15:19:01 +00:00
}
2013-08-20 15:21:25 +00:00
if (coreParameter.updateRecent) {
g_Config.AddRecent(filename);
}
coreState = coreParameter.startPaused ? CORE_STEPPING : CORE_RUNNING;
2012-11-01 15:19:01 +00:00
}
void CPU_Shutdown() {
if (g_Config.bAutoSaveSymbolMap) {
host->SaveSymbolMap();
}
2013-08-10 20:50:21 +00:00
CoreTiming::Shutdown();
__KernelShutdown();
HLEShutdown();
if (coreParameter.enableSound) {
2012-11-01 15:19:01 +00:00
host->ShutdownSound();
2013-01-13 16:10:59 +00:00
mixer = 0; // deleted in ShutdownSound
2012-11-01 15:19:01 +00:00
}
2013-08-10 20:50:21 +00:00
pspFileSystem.Shutdown();
2013-01-13 15:46:45 +00:00
Memory::Shutdown();
2012-11-01 15:19:01 +00:00
currentCPU = 0;
}
void CPU_RunLoop() {
setCurrentThreadName("CPUThread");
if (!CPU_NextState(CPU_THREAD_PENDING, CPU_THREAD_STARTING)) {
ERROR_LOG(CPU, "CPU thread in unexpected state: %d", cpuThreadState);
return;
}
CPU_Init();
CPU_NextState(CPU_THREAD_STARTING, CPU_THREAD_RUNNING);
while (cpuThreadState != CPU_THREAD_SHUTDOWN)
{
2013-10-12 09:08:00 +00:00
CPU_WaitStatus(cpuThreadCond, &CPU_HasPendingAction);
switch (cpuThreadState) {
case CPU_THREAD_EXECUTE:
mipsr4k.RunLoopUntil(cpuThreadUntil);
gpu->FinishEventLoop();
CPU_NextState(CPU_THREAD_EXECUTE, CPU_THREAD_RUNNING);
break;
// These are fine, just keep looping.
case CPU_THREAD_RUNNING:
case CPU_THREAD_SHUTDOWN:
break;
default:
ERROR_LOG(CPU, "CPU thread in unexpected state: %d", cpuThreadState);
// Begin shutdown, otherwise we'd just spin on this bad state.
CPU_SetState(CPU_THREAD_SHUTDOWN);
break;
}
}
if (coreState != CORE_ERROR) {
coreState = CORE_POWERDOWN;
}
// Let's make sure the gpu has already cleaned up before we start freeing memory.
gpu->FinishEventLoop();
gpu->SyncThread(true);
CPU_Shutdown();
CPU_SetState(CPU_THREAD_NOT_RUNNING);
}
void Core_UpdateState(CoreState newState) {
if ((coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME) && newState != CORE_RUNNING)
coreStatePending = true;
coreState = newState;
Core_UpdateSingleStep();
}
void System_Wake() {
// Ping the threads so they check coreState.
CPU_NextStateNot(CPU_THREAD_NOT_RUNNING, CPU_THREAD_SHUTDOWN);
if (gpu) {
gpu->FinishEventLoop();
}
}
bool PSP_Init(const CoreParameter &coreParam, std::string *error_string) {
2013-09-07 20:31:14 +00:00
INFO_LOG(BOOT, "PPSSPP %s", PPSSPP_GIT_VERSION);
coreParameter = coreParam;
coreParameter.errorString = "";
if (g_Config.bSeparateCPUThread) {
Core_ListenShutdown(System_Wake);
CPU_SetState(CPU_THREAD_PENDING);
cpuThread = new std::thread(&CPU_RunLoop);
2013-10-12 09:08:00 +00:00
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsReady);
} else {
CPU_Init();
}
bool success = coreParameter.fileToStart != "";
2013-08-10 17:56:47 +00:00
*error_string = coreParameter.errorString;
if (success) {
success = GPU_Init();
if (!success) {
PSP_Shutdown();
*error_string = "Unable to initialize rendering engine.";
}
}
return success;
}
bool PSP_IsInited() {
return currentCPU != 0;
}
void PSP_Shutdown() {
if (coreState == CORE_RUNNING)
Core_UpdateState(CORE_ERROR);
Core_NotifyShutdown();
if (cpuThread != NULL) {
CPU_NextStateNot(CPU_THREAD_NOT_RUNNING, CPU_THREAD_SHUTDOWN);
2013-10-12 09:08:00 +00:00
CPU_WaitStatus(cpuThreadReplyCond, &CPU_IsShutdown);
2013-08-10 09:39:53 +00:00
delete cpuThread;
cpuThread = 0;
} else {
CPU_Shutdown();
}
GPU_Shutdown();
2013-08-20 15:21:25 +00:00
host->SetWindowTitle(0);
}
2013-08-04 22:22:30 +00:00
void PSP_RunLoopUntil(u64 globalticks) {
SaveState::Process();
if (coreState == CORE_POWERDOWN || coreState == CORE_ERROR) {
return;
}
if (cpuThread != NULL) {
cpuThreadUntil = globalticks;
if (CPU_NextState(CPU_THREAD_RUNNING, CPU_THREAD_EXECUTE)) {
// The CPU doesn't actually respect cpuThreadUntil well, especially when skipping frames.
// TODO: Something smarter? Or force CPU to bail periodically?
while (!CPU_IsReady()) {
gpu->RunEventsUntil(CoreTiming::GetTicks() + msToCycles(100));
}
} else {
ERROR_LOG(CPU, "Unable to execute CPU run loop, unexpected state: %d", cpuThreadState);
}
} else {
mipsr4k.RunLoopUntil(globalticks);
}
2013-08-04 22:22:30 +00:00
}
void PSP_RunLoopFor(int cycles) {
PSP_RunLoopUntil(CoreTiming::GetTicks() + cycles);
}
CoreParameter &PSP_CoreParameter() {
2012-11-01 15:19:01 +00:00
return coreParameter;
}
void GetSysDirectories(std::string &memstickpath, std::string &flash0path) {
#ifdef _WIN32
const std::string path = ConvertWStringToUTF8(File::GetExeDirectory());
// Mount a filesystem
flash0path = path + "/flash0/";
// Detect the "My Documents"(XP) or "Documents"(on Vista/7/8) folder.
wchar_t myDocumentsPath[MAX_PATH];
const HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, myDocumentsPath);
const std::string installedFile = path + "/installed.txt";
const bool installed = File::Exists(installedFile);
// If installed.txt exists(and we can determine the Documents directory)
if (installed && (result == S_OK)) {
2013-10-14 06:46:05 +00:00
std::ifstream inputFile(ConvertUTF8ToWString(installedFile));
2013-10-14 06:22:13 +00:00
if (!inputFile.fail() && inputFile.is_open()) {
2013-10-14 06:46:05 +00:00
std::string tempString;
2013-10-14 06:22:13 +00:00
std::getline(inputFile, tempString);
2013-10-14 06:46:05 +00:00
// Skip UTF-8 encoding bytes if there are any. There are 3 of them.
if (tempString.substr(0, 3) == "\xEF\xBB\xBF")
tempString = tempString.substr(3);
memstickpath = tempString;
}
2013-10-14 06:22:13 +00:00
inputFile.close();
// Check if the file is empty first, before appending the slash.
if (memstickpath.empty())
memstickpath = ConvertWStringToUTF8(myDocumentsPath) + "/PPSSPP/";
size_t lastSlash = memstickpath.find_last_of("/");
if (lastSlash != (memstickpath.length() - 1))
memstickpath.append("/");
} else {
memstickpath = path + "/memstick/";
}
// If any directory is read-only, fall back to the Documents directory.
// We're screwed anyway if we can't write to Documents, or can't detect it.
const std::string testFile = "/_writable_test.$$$";
if (!File::CreateEmptyFile(memstickpath + testFile))
memstickpath = ConvertWStringToUTF8(myDocumentsPath) + "/PPSSPP/";
// Clean up our mess.
if (File::Exists(memstickpath + testFile))
File::Delete(memstickpath + testFile);
#else
// TODO
memstickpath = g_Config.memCardDirectory;
flash0path = g_Config.flash0Directory;
#endif
}