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-03-28 19:17:45 +00:00
|
|
|
#ifdef _WIN32
|
2013-07-29 03:43:25 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2013-03-28 19:17:45 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-09 06:30:45 +00:00
|
|
|
#include "native/thread/thread.h"
|
2013-08-11 19:22:01 +00:00
|
|
|
#include "native/thread/threadutil.h"
|
2013-08-09 06:30:45 +00:00
|
|
|
#include "native/base/mutex.h"
|
2013-08-26 17:00:16 +00:00
|
|
|
#include "util/text/utf8.h"
|
2013-08-09 06:30:45 +00:00
|
|
|
|
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"
|
2013-08-07 06:00:20 +00:00
|
|
|
#include "Core/SaveState.h"
|
2013-07-29 15:12:46 +00:00
|
|
|
#include "Common/LogManager.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-08-07 15:07:13 +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;
|
2013-01-02 20:00:10 +00:00
|
|
|
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;
|
2013-08-07 15:07:13 +00:00
|
|
|
static std::thread *cpuThread = NULL;
|
2013-08-09 06:30:45 +00:00
|
|
|
static recursive_mutex cpuThreadLock;
|
|
|
|
static condition_variable cpuThreadCond;
|
2013-08-07 15:07:13 +00:00
|
|
|
static u64 cpuThreadUntil;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-03-29 19:51:14 +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;
|
2013-08-07 15:07:13 +00:00
|
|
|
static volatile CPUThreadState cpuThreadState = CPU_THREAD_NOT_RUNNING;
|
|
|
|
|
2013-08-14 12:44:36 +00:00
|
|
|
bool IsAudioInitialised() {
|
|
|
|
return mixer != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_Init() {
|
|
|
|
if(mixer == NULL) {
|
|
|
|
mixer = new PSPMixer();
|
|
|
|
host->InitSound(mixer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 07:32:40 +00:00
|
|
|
bool IsOnSeparateCPUThread() {
|
2013-08-10 10:36:22 +00:00
|
|
|
if (cpuThread != NULL) {
|
2013-08-09 07:32:40 +00:00
|
|
|
return cpuThread->get_id() == std::this_thread::get_id();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
bool CPU_NextState(CPUThreadState from, CPUThreadState to) {
|
|
|
|
if (cpuThreadState == from) {
|
|
|
|
cpuThreadState = to;
|
2013-08-09 06:30:45 +00:00
|
|
|
cpuThreadCond.notify_one();
|
2013-08-07 15:07:13 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-03-29 19:51:14 +00:00
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
void CPU_SetState(CPUThreadState to) {
|
|
|
|
cpuThreadState = to;
|
2013-08-09 06:30:45 +00:00
|
|
|
cpuThreadCond.notify_one();
|
2013-03-29 19:51:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
bool CPU_IsReady() {
|
|
|
|
return cpuThreadState == CPU_THREAD_RUNNING || cpuThreadState == CPU_THREAD_NOT_RUNNING;
|
|
|
|
}
|
2013-03-02 20:34:41 +00:00
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
bool CPU_IsShutdown() {
|
|
|
|
return cpuThreadState == CPU_THREAD_NOT_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPU_HasPendingAction() {
|
|
|
|
return cpuThreadState != CPU_THREAD_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPU_WaitStatus(bool (*pred)()) {
|
2013-08-09 06:30:45 +00:00
|
|
|
cpuThreadLock.lock();
|
|
|
|
while (!pred())
|
2013-08-11 04:27:51 +00:00
|
|
|
cpuThreadCond.wait_for(cpuThreadLock, 16);
|
2013-08-09 06:30:45 +00:00
|
|
|
cpuThreadLock.unlock();
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 20:50:21 +00:00
|
|
|
void CPU_Shutdown();
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
void CPU_Init() {
|
2012-11-01 15:19:01 +00:00
|
|
|
currentCPU = &mipsr4k;
|
|
|
|
numCPUs = 1;
|
2013-06-22 09:14:01 +00:00
|
|
|
|
2013-06-23 04:17:33 +00:00
|
|
|
// Default memory settings
|
|
|
|
// Seems to be the safest place currently..
|
2013-09-09 07:19:26 +00:00
|
|
|
Memory::g_MemorySize = Memory::RAM_NORMAL_SIZE; // 32 MB of ram by default
|
2013-06-23 04:17:33 +00:00
|
|
|
g_RemasterMode = false;
|
|
|
|
g_DoubleTextureCoordinates = false;
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
std::string filename = coreParameter.fileToStart;
|
2013-08-10 17:56:47 +00:00
|
|
|
IdentifiedFileType type = Identify_File(filename);
|
2013-06-22 09:14:01 +00:00
|
|
|
|
2013-08-10 17:56:47 +00:00
|
|
|
switch (type) {
|
|
|
|
case FILETYPE_PSP_ISO:
|
|
|
|
case FILETYPE_PSP_ISO_NP:
|
|
|
|
case FILETYPE_PSP_DISC_DIRECTORY:
|
2013-06-22 09:14:01 +00:00
|
|
|
InitMemoryForGameISO(filename);
|
2013-08-10 17:56:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
2013-06-22 09:14:01 +00:00
|
|
|
|
|
|
|
Memory::Init();
|
2012-11-01 15:19:01 +00:00
|
|
|
mipsr4k.Reset();
|
|
|
|
mipsr4k.pc = 0;
|
|
|
|
|
2013-03-31 04:42:43 +00:00
|
|
|
host->AttemptLoadSymbolMap();
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
if (coreParameter.enableSound) {
|
2013-08-14 12:44:36 +00:00
|
|
|
Audio_Init();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
if (coreParameter.disableG3Dlog) {
|
2013-01-15 12:57:35 +00:00
|
|
|
LogManager::GetInstance()->SetEnable(LogTypes::G3D, false);
|
|
|
|
}
|
|
|
|
|
2013-01-17 09:12:40 +00:00
|
|
|
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
|
|
|
|
|
2013-07-29 20:19:53 +00:00
|
|
|
// Why did we check for CORE_POWERDOWN here?
|
2013-08-07 15:07:13 +00:00
|
|
|
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 = "";
|
2013-08-07 15:07:13 +00:00
|
|
|
CPU_SetState(CPU_THREAD_NOT_RUNNING);
|
|
|
|
return;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 15:21:25 +00:00
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
if (coreParameter.updateRecent) {
|
2013-06-08 15:48:41 +00:00
|
|
|
g_Config.AddRecent(filename);
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
2013-05-22 19:01:24 +00:00
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
coreState = coreParameter.startPaused ? CORE_STEPPING : CORE_RUNNING;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
void CPU_Shutdown() {
|
|
|
|
if (g_Config.bAutoSaveSymbolMap) {
|
2013-03-31 04:42:43 +00:00
|
|
|
host->SaveSymbolMap();
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
2013-03-31 04:42:43 +00:00
|
|
|
|
2013-08-10 20:50:21 +00:00
|
|
|
CoreTiming::Shutdown();
|
|
|
|
__KernelShutdown();
|
|
|
|
HLEShutdown();
|
2013-08-07 15:07:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
void CPU_RunLoop() {
|
2013-08-11 19:22:01 +00:00
|
|
|
setCurrentThreadName("CPUThread");
|
2013-08-07 15:07:13 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
CPU_WaitStatus(&CPU_HasPendingAction);
|
|
|
|
switch (cpuThreadState) {
|
|
|
|
case CPU_THREAD_EXECUTE:
|
|
|
|
mipsr4k.RunLoopUntil(cpuThreadUntil);
|
2013-08-09 07:45:27 +00:00
|
|
|
gpu->FinishEventLoop();
|
2013-08-07 15:07:13 +00:00
|
|
|
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.
|
2013-08-08 08:09:43 +00:00
|
|
|
CPU_SetState(CPU_THREAD_SHUTDOWN);
|
2013-08-07 15:07:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coreState != CORE_ERROR) {
|
|
|
|
coreState = CORE_POWERDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-09-15 01:43:23 +00:00
|
|
|
Core_UpdateSingleStep();
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-08-07 15:07:13 +00:00
|
|
|
|
|
|
|
coreParameter = coreParam;
|
|
|
|
coreParameter.errorString = "";
|
|
|
|
|
2013-08-08 08:09:43 +00:00
|
|
|
if (g_Config.bSeparateCPUThread) {
|
2013-08-07 15:07:13 +00:00
|
|
|
CPU_SetState(CPU_THREAD_PENDING);
|
|
|
|
cpuThread = new std::thread(&CPU_RunLoop);
|
|
|
|
CPU_WaitStatus(&CPU_IsReady);
|
|
|
|
} else {
|
|
|
|
CPU_Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = coreParameter.fileToStart != "";
|
2013-08-10 17:56:47 +00:00
|
|
|
*error_string = coreParameter.errorString;
|
2013-08-07 15:07:13 +00:00
|
|
|
if (success) {
|
2013-09-08 05:30:30 +00:00
|
|
|
success = GPU_Init();
|
|
|
|
if (!success) {
|
|
|
|
PSP_Shutdown();
|
|
|
|
*error_string = "Unable to initialize rendering engine.";
|
|
|
|
}
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PSP_IsInited() {
|
|
|
|
return currentCPU != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSP_Shutdown() {
|
|
|
|
if (coreState == CORE_RUNNING)
|
2013-09-15 01:43:23 +00:00
|
|
|
Core_UpdateState(CORE_ERROR);
|
2013-08-10 10:36:22 +00:00
|
|
|
if (cpuThread != NULL) {
|
2013-08-07 15:07:13 +00:00
|
|
|
CPU_SetState(CPU_THREAD_SHUTDOWN);
|
|
|
|
CPU_WaitStatus(&CPU_IsShutdown);
|
2013-08-10 09:39:53 +00:00
|
|
|
delete cpuThread;
|
|
|
|
cpuThread = 0;
|
2013-08-07 15:07:13 +00:00
|
|
|
} else {
|
|
|
|
CPU_Shutdown();
|
|
|
|
}
|
|
|
|
GPU_Shutdown();
|
2013-08-20 15:21:25 +00:00
|
|
|
host->SetWindowTitle(0);
|
2013-08-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 22:22:30 +00:00
|
|
|
void PSP_RunLoopUntil(u64 globalticks) {
|
2013-08-07 06:00:20 +00:00
|
|
|
SaveState::Process();
|
2013-09-15 04:19:10 +00:00
|
|
|
if (coreState == CORE_POWERDOWN || coreState == CORE_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-07 15:07:13 +00:00
|
|
|
|
2013-08-10 10:36:22 +00:00
|
|
|
if (cpuThread != NULL) {
|
2013-08-07 15:07:13 +00:00
|
|
|
cpuThreadUntil = globalticks;
|
|
|
|
if (CPU_NextState(CPU_THREAD_RUNNING, CPU_THREAD_EXECUTE)) {
|
2013-08-08 06:59:32 +00:00
|
|
|
// 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));
|
|
|
|
}
|
2013-08-07 15:07:13 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2013-08-07 15:07:13 +00:00
|
|
|
CoreParameter &PSP_CoreParameter() {
|
2012-11-01 15:19:01 +00:00
|
|
|
return coreParameter;
|
|
|
|
}
|
2013-03-28 19:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GetSysDirectories(std::string &memstickpath, std::string &flash0path) {
|
|
|
|
#ifdef _WIN32
|
2013-08-26 17:00:16 +00:00
|
|
|
wchar_t path_buffer[_MAX_PATH];
|
|
|
|
char drive[_MAX_DRIVE] ,dir[_MAX_DIR], file[_MAX_FNAME], ext[_MAX_EXT];
|
2013-03-28 19:17:45 +00:00
|
|
|
char memstickpath_buf[_MAX_PATH];
|
|
|
|
char flash0path_buf[_MAX_PATH];
|
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
GetModuleFileName(NULL, path_buffer, sizeof(path_buffer));
|
2013-03-28 19:17:45 +00:00
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
std::string path = ConvertWStringToUTF8(path_buffer);
|
|
|
|
|
|
|
|
_splitpath_s(path.c_str(), drive, dir, file, ext );
|
2013-03-28 19:17:45 +00:00
|
|
|
|
|
|
|
// Mount a couple of filesystems
|
|
|
|
sprintf(memstickpath_buf, "%s%smemstick\\", drive, dir);
|
|
|
|
sprintf(flash0path_buf, "%s%sflash0\\", drive, dir);
|
|
|
|
|
|
|
|
memstickpath = memstickpath_buf;
|
|
|
|
flash0path = flash0path_buf;
|
|
|
|
#else
|
|
|
|
// TODO
|
|
|
|
memstickpath = g_Config.memCardDirectory;
|
|
|
|
flash0path = g_Config.flashDirectory;
|
|
|
|
#endif
|
|
|
|
}
|