2024-06-10 14:18:42 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2024-07-31 12:25:55 +02:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "common/config.h"
|
2024-06-11 12:14:33 +02:00
|
|
|
#include "common/debug.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "common/logging/backend.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
#include "common/logging/log.h"
|
2024-07-11 14:35:58 +02:00
|
|
|
#include "common/ntapi.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "common/path_util.h"
|
2024-07-09 11:18:34 +02:00
|
|
|
#include "common/polyfill_thread.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "common/singleton.h"
|
2024-07-04 13:30:34 +02:00
|
|
|
#include "common/version.h"
|
2024-07-31 12:25:55 +02:00
|
|
|
#include "core/file_format/playgo_chunk.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
#include "core/file_format/psf.h"
|
|
|
|
#include "core/file_format/splash.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "core/file_sys/fs.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
#include "core/libraries/disc_map/disc_map.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "core/libraries/kernel/thread_management.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
#include "core/libraries/libc/libc.h"
|
|
|
|
#include "core/libraries/libc_internal/libc_internal.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "core/libraries/libs.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
#include "core/libraries/rtc/rtc.h"
|
2024-07-31 12:25:55 +02:00
|
|
|
#include "core/libraries/videoout/video_out.h"
|
2024-06-10 14:18:42 +02:00
|
|
|
#include "core/linker.h"
|
|
|
|
#include "core/memory.h"
|
|
|
|
#include "emulator.h"
|
2024-07-31 23:56:10 +02:00
|
|
|
#include "src/common/scm_rev.h"
|
2024-08-01 00:11:58 +02:00
|
|
|
#include "video_core/renderdoc.h"
|
2024-07-28 15:54:09 +02:00
|
|
|
|
2024-06-10 14:18:42 +02:00
|
|
|
Frontend::WindowSDL* g_window = nullptr;
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
static constexpr s32 WindowWidth = 1280;
|
|
|
|
static constexpr s32 WindowHeight = 720;
|
|
|
|
|
2024-07-15 23:22:47 +02:00
|
|
|
Emulator::Emulator() {
|
2024-06-10 14:18:42 +02:00
|
|
|
// Read configuration file.
|
|
|
|
const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
|
|
|
Config::load(config_dir / "config.toml");
|
|
|
|
|
2024-07-11 14:35:58 +02:00
|
|
|
// Initialize NT API functions
|
2024-07-11 15:29:36 +02:00
|
|
|
#ifdef _WIN32
|
2024-07-11 14:35:58 +02:00
|
|
|
Common::NtApi::Initialize();
|
2024-07-11 15:29:36 +02:00
|
|
|
#endif
|
2024-07-11 14:35:58 +02:00
|
|
|
|
2024-06-10 14:18:42 +02:00
|
|
|
// Start logger.
|
|
|
|
Common::Log::Initialize();
|
|
|
|
Common::Log::Start();
|
2024-07-04 13:30:34 +02:00
|
|
|
LOG_INFO(Loader, "Starting shadps4 emulator v{} ", Common::VERSION);
|
2024-08-01 00:11:58 +02:00
|
|
|
LOG_INFO(Loader, "Revision {}", Common::g_scm_rev);
|
|
|
|
LOG_INFO(Loader, "Branch {}", Common::g_scm_branch);
|
|
|
|
LOG_INFO(Loader, "Description {}", Common::g_scm_desc);
|
2024-07-15 23:22:47 +02:00
|
|
|
|
|
|
|
// Defer until after logging is initialized.
|
|
|
|
memory = Core::Memory::Instance();
|
|
|
|
controller = Common::Singleton<Input::GameController>::Instance();
|
|
|
|
linker = Common::Singleton<Core::Linker>::Instance();
|
2024-07-28 15:54:09 +02:00
|
|
|
|
|
|
|
// Load renderdoc module.
|
|
|
|
VideoCore::LoadRenderDoc();
|
2024-06-10 14:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Emulator::~Emulator() {
|
|
|
|
const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
|
|
|
Config::save(config_dir / "config.toml");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Emulator::Run(const std::filesystem::path& file) {
|
|
|
|
// Applications expect to be run from /app0 so mount the file's parent path as app0.
|
|
|
|
auto* mnt = Common::Singleton<Core::FileSys::MntPoints>::Instance();
|
|
|
|
mnt->Mount(file.parent_path(), "/app0");
|
|
|
|
|
|
|
|
// Loading param.sfo file if exists
|
2024-06-15 16:51:51 +02:00
|
|
|
std::string id;
|
2024-07-24 08:12:53 +02:00
|
|
|
std::string title;
|
|
|
|
std::string app_version;
|
2024-06-10 14:18:42 +02:00
|
|
|
std::filesystem::path sce_sys_folder = file.parent_path() / "sce_sys";
|
|
|
|
if (std::filesystem::is_directory(sce_sys_folder)) {
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(sce_sys_folder)) {
|
|
|
|
if (entry.path().filename() == "param.sfo") {
|
|
|
|
auto* param_sfo = Common::Singleton<PSF>::Instance();
|
2024-06-11 04:42:21 +02:00
|
|
|
param_sfo->open(sce_sys_folder.string() + "/param.sfo", {});
|
2024-06-15 16:51:51 +02:00
|
|
|
id = std::string(param_sfo->GetString("CONTENT_ID"), 7, 9);
|
2024-07-24 08:12:53 +02:00
|
|
|
title = param_sfo->GetString("TITLE");
|
2024-06-10 14:18:42 +02:00
|
|
|
LOG_INFO(Loader, "Game id: {} Title: {}", id, title);
|
|
|
|
u32 fw_version = param_sfo->GetInteger("SYSTEM_VER");
|
2024-07-24 08:12:53 +02:00
|
|
|
app_version = param_sfo->GetString("APP_VER");
|
2024-06-10 14:18:42 +02:00
|
|
|
LOG_INFO(Loader, "Fw: {:#x} App Version: {}", fw_version, app_version);
|
2024-07-24 18:02:38 +02:00
|
|
|
} else if (entry.path().filename() == "playgo-chunk.dat") {
|
|
|
|
auto* playgo = Common::Singleton<PlaygoChunk>::Instance();
|
|
|
|
playgo->Open(sce_sys_folder.string() + "/playgo-chunk.dat");
|
2024-06-10 14:18:42 +02:00
|
|
|
} else if (entry.path().filename() == "pic0.png" ||
|
|
|
|
entry.path().filename() == "pic1.png") {
|
|
|
|
auto* splash = Common::Singleton<Splash>::Instance();
|
|
|
|
if (splash->IsLoaded()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!splash->Open(entry.path().string())) {
|
|
|
|
LOG_ERROR(Loader, "Game splash: unable to open file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-01 00:11:58 +02:00
|
|
|
|
2024-07-25 09:13:14 +02:00
|
|
|
std::string game_title = fmt::format("{} - {} <{}>", id, title, app_version);
|
2024-08-01 00:11:58 +02:00
|
|
|
std::string window_title = "";
|
|
|
|
if (Common::isRelease) {
|
|
|
|
window_title = fmt::format("shadPS4 v{} | {}", Common::VERSION, game_title);
|
|
|
|
} else {
|
|
|
|
window_title =
|
|
|
|
fmt::format("shadPS4 v{} {} | {}", Common::VERSION, Common::g_scm_desc, game_title);
|
|
|
|
}
|
2024-07-24 08:12:53 +02:00
|
|
|
window =
|
2024-07-31 23:56:10 +02:00
|
|
|
std::make_unique<Frontend::WindowSDL>(WindowWidth, WindowHeight, controller, window_title);
|
2024-07-24 08:12:53 +02:00
|
|
|
|
|
|
|
g_window = window.get();
|
2024-06-10 14:18:42 +02:00
|
|
|
|
2024-06-15 16:51:51 +02:00
|
|
|
const auto& mount_data_dir = Common::FS::GetUserPath(Common::FS::PathType::GameDataDir) / id;
|
|
|
|
if (!std::filesystem::exists(mount_data_dir)) {
|
|
|
|
std::filesystem::create_directory(mount_data_dir);
|
|
|
|
}
|
|
|
|
mnt->Mount(mount_data_dir, "/data"); // should just exist, manually create with game serial
|
|
|
|
const auto& mount_temp_dir = Common::FS::GetUserPath(Common::FS::PathType::TempDataDir) / id;
|
2024-07-04 09:51:46 +02:00
|
|
|
if (!std::filesystem::exists(mount_temp_dir)) {
|
|
|
|
std::filesystem::create_directory(mount_temp_dir);
|
|
|
|
}
|
2024-06-15 16:51:51 +02:00
|
|
|
mnt->Mount(mount_temp_dir, "/temp0"); // called in app_content ==> stat/mkdir
|
2024-08-09 11:56:03 +02:00
|
|
|
mnt->Mount(mount_temp_dir, "/temp");
|
2024-06-15 16:51:51 +02:00
|
|
|
|
2024-07-26 16:07:22 +02:00
|
|
|
const auto& mount_download_dir =
|
|
|
|
Common::FS::GetUserPath(Common::FS::PathType::DownloadDir) / id;
|
|
|
|
if (!std::filesystem::exists(mount_download_dir)) {
|
|
|
|
std::filesystem::create_directory(mount_download_dir);
|
|
|
|
}
|
|
|
|
mnt->Mount(mount_download_dir, "/download0");
|
|
|
|
|
2024-07-28 15:54:09 +02:00
|
|
|
const auto& mount_captures_dir = Common::FS::GetUserPath(Common::FS::PathType::CapturesDir);
|
|
|
|
if (!std::filesystem::exists(mount_captures_dir)) {
|
|
|
|
std::filesystem::create_directory(mount_captures_dir);
|
|
|
|
}
|
|
|
|
VideoCore::SetOutputDir(mount_captures_dir.generic_string(), id);
|
|
|
|
|
2024-06-30 18:22:39 +02:00
|
|
|
// Initialize kernel and library facilities.
|
|
|
|
Libraries::Kernel::init_pthreads();
|
|
|
|
Libraries::InitHLELibs(&linker->GetHLESymbols());
|
|
|
|
|
2024-06-10 14:18:42 +02:00
|
|
|
// Load the module with the linker
|
|
|
|
linker->LoadModule(file);
|
|
|
|
|
|
|
|
// check if we have system modules to load
|
2024-06-10 17:26:37 +02:00
|
|
|
LoadSystemModules(file);
|
|
|
|
|
2024-06-10 14:18:42 +02:00
|
|
|
// Check if there is a libc.prx in sce_module folder
|
|
|
|
bool found = false;
|
|
|
|
if (Config::isLleLibc()) {
|
|
|
|
std::filesystem::path sce_module_folder = file.parent_path() / "sce_module";
|
|
|
|
if (std::filesystem::is_directory(sce_module_folder)) {
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(sce_module_folder)) {
|
2024-07-18 17:34:08 +02:00
|
|
|
if (entry.path().filename() == "libc.prx") {
|
2024-06-10 14:18:42 +02:00
|
|
|
found = true;
|
|
|
|
}
|
2024-07-18 17:34:08 +02:00
|
|
|
LOG_INFO(Loader, "Loading {}", entry.path().string().c_str());
|
|
|
|
linker->LoadModule(entry.path());
|
2024-06-10 14:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
Libraries::LibC::libcSymbolsRegister(&linker->GetHLESymbols());
|
|
|
|
}
|
|
|
|
|
|
|
|
// start execution
|
2024-06-10 17:26:37 +02:00
|
|
|
std::jthread mainthread =
|
|
|
|
std::jthread([this](std::stop_token stop_token) { linker->Execute(); });
|
2024-06-10 14:18:42 +02:00
|
|
|
|
2024-07-15 23:22:47 +02:00
|
|
|
while (window->isOpen()) {
|
|
|
|
window->waitEvent();
|
2024-06-10 14:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
2024-06-10 17:26:37 +02:00
|
|
|
void Emulator::LoadSystemModules(const std::filesystem::path& file) {
|
2024-08-06 18:28:17 +02:00
|
|
|
constexpr std::array<SysModules, 9> ModulesToLoad{
|
2024-06-26 13:43:01 +02:00
|
|
|
{{"libSceNgs2.sprx", nullptr},
|
2024-07-17 14:37:32 +02:00
|
|
|
{"libSceFiber.sprx", nullptr},
|
2024-07-17 14:44:20 +02:00
|
|
|
{"libSceUlt.sprx", nullptr},
|
2024-07-25 22:01:12 +02:00
|
|
|
{"libSceJson.sprx", nullptr},
|
|
|
|
{"libSceJson2.sprx", nullptr},
|
2024-06-26 13:43:01 +02:00
|
|
|
{"libSceLibcInternal.sprx", &Libraries::LibcInternal::RegisterlibSceLibcInternal},
|
|
|
|
{"libSceDiscMap.sprx", &Libraries::DiscMap::RegisterlibSceDiscMap},
|
2024-06-30 09:12:36 +02:00
|
|
|
{"libSceRtc.sprx", &Libraries::Rtc::RegisterlibSceRtc},
|
2024-08-06 18:28:17 +02:00
|
|
|
{"libSceJpegEnc.sprx", nullptr}},
|
2024-07-17 14:37:32 +02:00
|
|
|
};
|
2024-06-26 13:43:01 +02:00
|
|
|
|
|
|
|
std::vector<std::filesystem::path> found_modules;
|
2024-06-10 17:26:37 +02:00
|
|
|
const auto& sys_module_path = Common::FS::GetUserPath(Common::FS::PathType::SysModuleDir);
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(sys_module_path)) {
|
2024-06-26 13:43:01 +02:00
|
|
|
found_modules.push_back(entry.path());
|
|
|
|
}
|
2024-07-17 14:37:32 +02:00
|
|
|
for (const auto& [module_name, init_func] : ModulesToLoad) {
|
2024-07-17 16:57:54 +02:00
|
|
|
const auto it = std::ranges::find_if(
|
|
|
|
found_modules, [&](const auto& path) { return path.filename() == module_name; });
|
2024-07-17 14:37:32 +02:00
|
|
|
if (it != found_modules.end()) {
|
|
|
|
LOG_INFO(Loader, "Loading {}", it->string());
|
|
|
|
linker->LoadModule(*it);
|
|
|
|
continue;
|
2024-06-26 13:43:01 +02:00
|
|
|
}
|
2024-07-17 14:37:32 +02:00
|
|
|
if (init_func) {
|
|
|
|
LOG_INFO(Loader, "Can't Load {} switching to HLE", module_name);
|
|
|
|
init_func(&linker->GetHLESymbols());
|
2024-06-26 13:43:01 +02:00
|
|
|
} else {
|
2024-07-17 14:37:32 +02:00
|
|
|
LOG_INFO(Loader, "No HLE available for {} module", module_name);
|
2024-06-10 17:26:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 23:58:57 +02:00
|
|
|
} // namespace Core
|