core.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <atomic>
  6. #include <exception>
  7. #include <memory>
  8. #include <utility>
  9. #include "common/fs/fs.h"
  10. #include "common/logging/log.h"
  11. #include "common/microprofile.h"
  12. #include "common/settings.h"
  13. #include "common/string_util.h"
  14. #include "core/arm/exclusive_monitor.h"
  15. #include "core/core.h"
  16. #include "core/core_timing.h"
  17. #include "core/cpu_manager.h"
  18. #include "core/device_memory.h"
  19. #include "core/file_sys/bis_factory.h"
  20. #include "core/file_sys/card_image.h"
  21. #include "core/file_sys/mode.h"
  22. #include "core/file_sys/patch_manager.h"
  23. #include "core/file_sys/registered_cache.h"
  24. #include "core/file_sys/romfs_factory.h"
  25. #include "core/file_sys/savedata_factory.h"
  26. #include "core/file_sys/sdmc_factory.h"
  27. #include "core/file_sys/vfs_concat.h"
  28. #include "core/file_sys/vfs_real.h"
  29. #include "core/hardware_interrupt_manager.h"
  30. #include "core/hle/kernel/k_client_port.h"
  31. #include "core/hle/kernel/k_process.h"
  32. #include "core/hle/kernel/k_scheduler.h"
  33. #include "core/hle/kernel/k_thread.h"
  34. #include "core/hle/kernel/kernel.h"
  35. #include "core/hle/kernel/physical_core.h"
  36. #include "core/hle/service/am/applets/applets.h"
  37. #include "core/hle/service/apm/apm_controller.h"
  38. #include "core/hle/service/filesystem/filesystem.h"
  39. #include "core/hle/service/glue/glue_manager.h"
  40. #include "core/hle/service/hid/hid.h"
  41. #include "core/hle/service/service.h"
  42. #include "core/hle/service/sm/sm.h"
  43. #include "core/hle/service/time/time_manager.h"
  44. #include "core/loader/loader.h"
  45. #include "core/memory.h"
  46. #include "core/memory/cheat_engine.h"
  47. #include "core/network/network.h"
  48. #include "core/perf_stats.h"
  49. #include "core/reporter.h"
  50. #include "core/telemetry_session.h"
  51. #include "core/tools/freezer.h"
  52. #include "video_core/renderer_base.h"
  53. #include "video_core/video_core.h"
  54. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU0, "ARM JIT", "Dynarmic CPU 0", MP_RGB(255, 64, 64));
  55. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU1, "ARM JIT", "Dynarmic CPU 1", MP_RGB(255, 64, 64));
  56. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU2, "ARM JIT", "Dynarmic CPU 2", MP_RGB(255, 64, 64));
  57. MICROPROFILE_DEFINE(ARM_Jit_Dynarmic_CPU3, "ARM JIT", "Dynarmic CPU 3", MP_RGB(255, 64, 64));
  58. namespace Core {
  59. namespace {
  60. FileSys::StorageId GetStorageIdForFrontendSlot(
  61. std::optional<FileSys::ContentProviderUnionSlot> slot) {
  62. if (!slot.has_value()) {
  63. return FileSys::StorageId::None;
  64. }
  65. switch (*slot) {
  66. case FileSys::ContentProviderUnionSlot::UserNAND:
  67. return FileSys::StorageId::NandUser;
  68. case FileSys::ContentProviderUnionSlot::SysNAND:
  69. return FileSys::StorageId::NandSystem;
  70. case FileSys::ContentProviderUnionSlot::SDMC:
  71. return FileSys::StorageId::SdCard;
  72. case FileSys::ContentProviderUnionSlot::FrontendManual:
  73. return FileSys::StorageId::Host;
  74. default:
  75. return FileSys::StorageId::None;
  76. }
  77. }
  78. void KProcessDeleter(Kernel::KProcess* process) {
  79. process->Destroy();
  80. }
  81. using KProcessPtr = std::unique_ptr<Kernel::KProcess, decltype(&KProcessDeleter)>;
  82. } // Anonymous namespace
  83. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  84. const std::string& path) {
  85. // To account for split 00+01+etc files.
  86. std::string dir_name;
  87. std::string filename;
  88. Common::SplitPath(path, &dir_name, &filename, nullptr);
  89. if (filename == "00") {
  90. const auto dir = vfs->OpenDirectory(dir_name, FileSys::Mode::Read);
  91. std::vector<FileSys::VirtualFile> concat;
  92. for (u32 i = 0; i < 0x10; ++i) {
  93. const auto file_name = fmt::format("{:02X}", i);
  94. auto next = dir->GetFile(file_name);
  95. if (next != nullptr) {
  96. concat.push_back(std::move(next));
  97. } else {
  98. next = dir->GetFile(file_name);
  99. if (next == nullptr) {
  100. break;
  101. }
  102. concat.push_back(std::move(next));
  103. }
  104. }
  105. if (concat.empty()) {
  106. return nullptr;
  107. }
  108. return FileSys::ConcatenatedVfsFile::MakeConcatenatedFile(std::move(concat),
  109. dir->GetName());
  110. }
  111. if (Common::FS::IsDir(path)) {
  112. return vfs->OpenFile(path + "/main", FileSys::Mode::Read);
  113. }
  114. return vfs->OpenFile(path, FileSys::Mode::Read);
  115. }
  116. struct System::Impl {
  117. explicit Impl(System& system)
  118. : kernel{system}, fs_controller{system}, memory{system},
  119. cpu_manager{system}, reporter{system}, applet_manager{system}, time_manager{system} {}
  120. ResultStatus Run() {
  121. status = ResultStatus::Success;
  122. kernel.Suspend(false);
  123. core_timing.SyncPause(false);
  124. cpu_manager.Pause(false);
  125. return status;
  126. }
  127. ResultStatus Pause() {
  128. status = ResultStatus::Success;
  129. core_timing.SyncPause(true);
  130. kernel.Suspend(true);
  131. cpu_manager.Pause(true);
  132. return status;
  133. }
  134. ResultStatus Init(System& system, Frontend::EmuWindow& emu_window) {
  135. LOG_DEBUG(Core, "initialized OK");
  136. device_memory = std::make_unique<Core::DeviceMemory>();
  137. is_multicore = Settings::values.use_multi_core.GetValue();
  138. is_async_gpu = Settings::values.use_asynchronous_gpu_emulation.GetValue();
  139. kernel.SetMulticore(is_multicore);
  140. cpu_manager.SetMulticore(is_multicore);
  141. cpu_manager.SetAsyncGpu(is_async_gpu);
  142. core_timing.SetMulticore(is_multicore);
  143. kernel.Initialize();
  144. cpu_manager.Initialize();
  145. core_timing.Initialize([&system]() { system.RegisterHostThread(); });
  146. const auto current_time = std::chrono::duration_cast<std::chrono::seconds>(
  147. std::chrono::system_clock::now().time_since_epoch());
  148. Settings::values.custom_rtc_differential =
  149. Settings::values.custom_rtc.value_or(current_time) - current_time;
  150. // Create a default fs if one doesn't already exist.
  151. if (virtual_filesystem == nullptr)
  152. virtual_filesystem = std::make_shared<FileSys::RealVfsFilesystem>();
  153. if (content_provider == nullptr)
  154. content_provider = std::make_unique<FileSys::ContentProviderUnion>();
  155. /// Create default implementations of applets if one is not provided.
  156. applet_manager.SetDefaultAppletsIfMissing();
  157. /// Reset all glue registrations
  158. arp_manager.ResetAll();
  159. telemetry_session = std::make_unique<Core::TelemetrySession>();
  160. gpu_core = VideoCore::CreateGPU(emu_window, system);
  161. if (!gpu_core) {
  162. return ResultStatus::ErrorVideoCore;
  163. }
  164. service_manager = std::make_shared<Service::SM::ServiceManager>(kernel);
  165. services = std::make_unique<Service::Services>(service_manager, system);
  166. interrupt_manager = std::make_unique<Hardware::InterruptManager>(system);
  167. // Initialize time manager, which must happen after kernel is created
  168. time_manager.Initialize();
  169. is_powered_on = true;
  170. exit_lock = false;
  171. microprofile_dynarmic[0] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU0);
  172. microprofile_dynarmic[1] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU1);
  173. microprofile_dynarmic[2] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU2);
  174. microprofile_dynarmic[3] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU3);
  175. LOG_DEBUG(Core, "Initialized OK");
  176. return ResultStatus::Success;
  177. }
  178. ResultStatus Load(System& system, Frontend::EmuWindow& emu_window, const std::string& filepath,
  179. u64 program_id, std::size_t program_index) {
  180. app_loader = Loader::GetLoader(system, GetGameFileFromPath(virtual_filesystem, filepath),
  181. program_id, program_index);
  182. if (!app_loader) {
  183. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  184. return ResultStatus::ErrorGetLoader;
  185. }
  186. ResultStatus init_result{Init(system, emu_window)};
  187. if (init_result != ResultStatus::Success) {
  188. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  189. static_cast<int>(init_result));
  190. Shutdown();
  191. return init_result;
  192. }
  193. telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider);
  194. main_process = KProcessPtr{Kernel::KProcess::Create(system.Kernel()), KProcessDeleter};
  195. ASSERT(Kernel::KProcess::Initialize(main_process.get(), system, "main",
  196. Kernel::KProcess::ProcessType::Userland)
  197. .IsSuccess());
  198. main_process->Open();
  199. const auto [load_result, load_parameters] = app_loader->Load(*main_process, system);
  200. if (load_result != Loader::ResultStatus::Success) {
  201. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", load_result);
  202. Shutdown();
  203. return static_cast<ResultStatus>(static_cast<u32>(ResultStatus::ErrorLoader) +
  204. static_cast<u32>(load_result));
  205. }
  206. AddGlueRegistrationForProcess(*app_loader, *main_process);
  207. kernel.MakeCurrentProcess(main_process.get());
  208. kernel.InitializeCores();
  209. // Initialize cheat engine
  210. if (cheat_engine) {
  211. cheat_engine->Initialize();
  212. }
  213. // All threads are started, begin main process execution, now that we're in the clear.
  214. main_process->Run(load_parameters->main_thread_priority,
  215. load_parameters->main_thread_stack_size);
  216. if (Settings::values.gamecard_inserted) {
  217. if (Settings::values.gamecard_current_game) {
  218. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, filepath));
  219. } else if (!Settings::values.gamecard_path.GetValue().empty()) {
  220. const auto gamecard_path = Settings::values.gamecard_path.GetValue();
  221. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, gamecard_path));
  222. }
  223. }
  224. if (app_loader->ReadProgramId(program_id) != Loader::ResultStatus::Success) {
  225. LOG_ERROR(Core, "Failed to find title id for ROM (Error {})", load_result);
  226. }
  227. perf_stats = std::make_unique<PerfStats>(program_id);
  228. // Reset counters and set time origin to current frame
  229. GetAndResetPerfStats();
  230. perf_stats->BeginSystemFrame();
  231. status = ResultStatus::Success;
  232. return status;
  233. }
  234. void Shutdown() {
  235. // Log last frame performance stats if game was loded
  236. if (perf_stats) {
  237. const auto perf_results = GetAndResetPerfStats();
  238. constexpr auto performance = Common::Telemetry::FieldType::Performance;
  239. telemetry_session->AddField(performance, "Shutdown_EmulationSpeed",
  240. perf_results.emulation_speed * 100.0);
  241. telemetry_session->AddField(performance, "Shutdown_Framerate",
  242. perf_results.average_game_fps);
  243. telemetry_session->AddField(performance, "Shutdown_Frametime",
  244. perf_results.frametime * 1000.0);
  245. telemetry_session->AddField(performance, "Mean_Frametime_MS",
  246. perf_stats->GetMeanFrametime());
  247. }
  248. is_powered_on = false;
  249. exit_lock = false;
  250. if (gpu_core) {
  251. gpu_core->ShutDown();
  252. }
  253. services.reset();
  254. service_manager.reset();
  255. cheat_engine.reset();
  256. telemetry_session.reset();
  257. cpu_manager.Shutdown();
  258. time_manager.Shutdown();
  259. core_timing.Shutdown();
  260. app_loader.reset();
  261. gpu_core.reset();
  262. perf_stats.reset();
  263. kernel.Shutdown();
  264. memory.Reset();
  265. applet_manager.ClearAll();
  266. // TODO: The main process should be freed based on KAutoObject ref counting.
  267. main_process.reset();
  268. LOG_DEBUG(Core, "Shutdown OK");
  269. }
  270. Loader::ResultStatus GetGameName(std::string& out) const {
  271. if (app_loader == nullptr)
  272. return Loader::ResultStatus::ErrorNotInitialized;
  273. return app_loader->ReadTitle(out);
  274. }
  275. void AddGlueRegistrationForProcess(Loader::AppLoader& loader, Kernel::KProcess& process) {
  276. std::vector<u8> nacp_data;
  277. FileSys::NACP nacp;
  278. if (loader.ReadControlData(nacp) == Loader::ResultStatus::Success) {
  279. nacp_data = nacp.GetRawBytes();
  280. } else {
  281. nacp_data.resize(sizeof(FileSys::RawNACP));
  282. }
  283. Service::Glue::ApplicationLaunchProperty launch{};
  284. launch.title_id = process.GetTitleID();
  285. FileSys::PatchManager pm{launch.title_id, fs_controller, *content_provider};
  286. launch.version = pm.GetGameVersion().value_or(0);
  287. // TODO(DarkLordZach): When FSController/Game Card Support is added, if
  288. // current_process_game_card use correct StorageId
  289. launch.base_game_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  290. launch.title_id, FileSys::ContentRecordType::Program));
  291. launch.update_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  292. FileSys::GetUpdateTitleID(launch.title_id), FileSys::ContentRecordType::Program));
  293. arp_manager.Register(launch.title_id, launch, std::move(nacp_data));
  294. }
  295. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  296. status = new_status;
  297. if (details) {
  298. status_details = details;
  299. }
  300. }
  301. PerfStatsResults GetAndResetPerfStats() {
  302. return perf_stats->GetAndResetStats(core_timing.GetGlobalTimeUs());
  303. }
  304. Timing::CoreTiming core_timing;
  305. Kernel::KernelCore kernel;
  306. /// RealVfsFilesystem instance
  307. FileSys::VirtualFilesystem virtual_filesystem;
  308. /// ContentProviderUnion instance
  309. std::unique_ptr<FileSys::ContentProviderUnion> content_provider;
  310. Service::FileSystem::FileSystemController fs_controller;
  311. /// AppLoader used to load the current executing application
  312. std::unique_ptr<Loader::AppLoader> app_loader;
  313. std::unique_ptr<Tegra::GPU> gpu_core;
  314. std::unique_ptr<Hardware::InterruptManager> interrupt_manager;
  315. std::unique_ptr<Core::DeviceMemory> device_memory;
  316. KProcessPtr main_process{nullptr, KProcessDeleter};
  317. Core::Memory::Memory memory;
  318. CpuManager cpu_manager;
  319. std::atomic_bool is_powered_on{};
  320. bool exit_lock = false;
  321. Reporter reporter;
  322. std::unique_ptr<Memory::CheatEngine> cheat_engine;
  323. std::unique_ptr<Tools::Freezer> memory_freezer;
  324. std::array<u8, 0x20> build_id{};
  325. /// Frontend applets
  326. Service::AM::Applets::AppletManager applet_manager;
  327. /// APM (Performance) services
  328. Service::APM::Controller apm_controller{core_timing};
  329. /// Service State
  330. Service::Glue::ARPManager arp_manager;
  331. Service::Time::TimeManager time_manager;
  332. /// Service manager
  333. std::shared_ptr<Service::SM::ServiceManager> service_manager;
  334. /// Services
  335. std::unique_ptr<Service::Services> services;
  336. /// Telemetry session for this emulation session
  337. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  338. /// Network instance
  339. Network::NetworkInstance network_instance;
  340. ResultStatus status = ResultStatus::Success;
  341. std::string status_details = "";
  342. std::unique_ptr<Core::PerfStats> perf_stats;
  343. Core::SpeedLimiter speed_limiter;
  344. bool is_multicore{};
  345. bool is_async_gpu{};
  346. ExecuteProgramCallback execute_program_callback;
  347. std::array<u64, Core::Hardware::NUM_CPU_CORES> dynarmic_ticks{};
  348. std::array<MicroProfileToken, Core::Hardware::NUM_CPU_CORES> microprofile_dynarmic{};
  349. };
  350. System::System() : impl{std::make_unique<Impl>(*this)} {}
  351. System::~System() = default;
  352. System& System::GetInstance() {
  353. if (!s_instance) {
  354. throw std::runtime_error("Using System instance before its initialization");
  355. }
  356. return *s_instance;
  357. }
  358. void System::InitializeGlobalInstance() {
  359. if (s_instance) {
  360. throw std::runtime_error("Reinitializing Global System instance.");
  361. }
  362. s_instance = std::unique_ptr<System>(new System);
  363. }
  364. CpuManager& System::GetCpuManager() {
  365. return impl->cpu_manager;
  366. }
  367. const CpuManager& System::GetCpuManager() const {
  368. return impl->cpu_manager;
  369. }
  370. System::ResultStatus System::Run() {
  371. return impl->Run();
  372. }
  373. System::ResultStatus System::Pause() {
  374. return impl->Pause();
  375. }
  376. System::ResultStatus System::SingleStep() {
  377. return ResultStatus::Success;
  378. }
  379. void System::InvalidateCpuInstructionCaches() {
  380. impl->kernel.InvalidateAllInstructionCaches();
  381. }
  382. void System::InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size) {
  383. impl->kernel.InvalidateCpuInstructionCacheRange(addr, size);
  384. }
  385. void System::Shutdown() {
  386. impl->Shutdown();
  387. }
  388. System::ResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::string& filepath,
  389. u64 program_id, std::size_t program_index) {
  390. return impl->Load(*this, emu_window, filepath, program_id, program_index);
  391. }
  392. bool System::IsPoweredOn() const {
  393. return impl->is_powered_on.load(std::memory_order::relaxed);
  394. }
  395. void System::PrepareReschedule() {
  396. // Deprecated, does nothing, kept for backward compatibility.
  397. }
  398. void System::PrepareReschedule(const u32 core_index) {
  399. impl->kernel.PrepareReschedule(core_index);
  400. }
  401. PerfStatsResults System::GetAndResetPerfStats() {
  402. return impl->GetAndResetPerfStats();
  403. }
  404. TelemetrySession& System::TelemetrySession() {
  405. return *impl->telemetry_session;
  406. }
  407. const TelemetrySession& System::TelemetrySession() const {
  408. return *impl->telemetry_session;
  409. }
  410. ARM_Interface& System::CurrentArmInterface() {
  411. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  412. }
  413. const ARM_Interface& System::CurrentArmInterface() const {
  414. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  415. }
  416. std::size_t System::CurrentCoreIndex() const {
  417. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  418. ASSERT(core < Core::Hardware::NUM_CPU_CORES);
  419. return core;
  420. }
  421. Kernel::PhysicalCore& System::CurrentPhysicalCore() {
  422. return impl->kernel.CurrentPhysicalCore();
  423. }
  424. const Kernel::PhysicalCore& System::CurrentPhysicalCore() const {
  425. return impl->kernel.CurrentPhysicalCore();
  426. }
  427. /// Gets the global scheduler
  428. Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() {
  429. return impl->kernel.GlobalSchedulerContext();
  430. }
  431. /// Gets the global scheduler
  432. const Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() const {
  433. return impl->kernel.GlobalSchedulerContext();
  434. }
  435. Kernel::KProcess* System::CurrentProcess() {
  436. return impl->kernel.CurrentProcess();
  437. }
  438. Core::DeviceMemory& System::DeviceMemory() {
  439. return *impl->device_memory;
  440. }
  441. const Core::DeviceMemory& System::DeviceMemory() const {
  442. return *impl->device_memory;
  443. }
  444. const Kernel::KProcess* System::CurrentProcess() const {
  445. return impl->kernel.CurrentProcess();
  446. }
  447. ARM_Interface& System::ArmInterface(std::size_t core_index) {
  448. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  449. }
  450. const ARM_Interface& System::ArmInterface(std::size_t core_index) const {
  451. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  452. }
  453. ExclusiveMonitor& System::Monitor() {
  454. return impl->kernel.GetExclusiveMonitor();
  455. }
  456. const ExclusiveMonitor& System::Monitor() const {
  457. return impl->kernel.GetExclusiveMonitor();
  458. }
  459. Memory::Memory& System::Memory() {
  460. return impl->memory;
  461. }
  462. const Core::Memory::Memory& System::Memory() const {
  463. return impl->memory;
  464. }
  465. Tegra::GPU& System::GPU() {
  466. return *impl->gpu_core;
  467. }
  468. const Tegra::GPU& System::GPU() const {
  469. return *impl->gpu_core;
  470. }
  471. Core::Hardware::InterruptManager& System::InterruptManager() {
  472. return *impl->interrupt_manager;
  473. }
  474. const Core::Hardware::InterruptManager& System::InterruptManager() const {
  475. return *impl->interrupt_manager;
  476. }
  477. VideoCore::RendererBase& System::Renderer() {
  478. return impl->gpu_core->Renderer();
  479. }
  480. const VideoCore::RendererBase& System::Renderer() const {
  481. return impl->gpu_core->Renderer();
  482. }
  483. Kernel::KernelCore& System::Kernel() {
  484. return impl->kernel;
  485. }
  486. const Kernel::KernelCore& System::Kernel() const {
  487. return impl->kernel;
  488. }
  489. Timing::CoreTiming& System::CoreTiming() {
  490. return impl->core_timing;
  491. }
  492. const Timing::CoreTiming& System::CoreTiming() const {
  493. return impl->core_timing;
  494. }
  495. Core::PerfStats& System::GetPerfStats() {
  496. return *impl->perf_stats;
  497. }
  498. const Core::PerfStats& System::GetPerfStats() const {
  499. return *impl->perf_stats;
  500. }
  501. Core::SpeedLimiter& System::SpeedLimiter() {
  502. return impl->speed_limiter;
  503. }
  504. const Core::SpeedLimiter& System::SpeedLimiter() const {
  505. return impl->speed_limiter;
  506. }
  507. Loader::ResultStatus System::GetGameName(std::string& out) const {
  508. return impl->GetGameName(out);
  509. }
  510. void System::SetStatus(ResultStatus new_status, const char* details) {
  511. impl->SetStatus(new_status, details);
  512. }
  513. const std::string& System::GetStatusDetails() const {
  514. return impl->status_details;
  515. }
  516. Loader::AppLoader& System::GetAppLoader() {
  517. return *impl->app_loader;
  518. }
  519. const Loader::AppLoader& System::GetAppLoader() const {
  520. return *impl->app_loader;
  521. }
  522. void System::SetFilesystem(FileSys::VirtualFilesystem vfs) {
  523. impl->virtual_filesystem = std::move(vfs);
  524. }
  525. FileSys::VirtualFilesystem System::GetFilesystem() const {
  526. return impl->virtual_filesystem;
  527. }
  528. void System::RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  529. const std::array<u8, 32>& build_id, VAddr main_region_begin,
  530. u64 main_region_size) {
  531. impl->cheat_engine = std::make_unique<Memory::CheatEngine>(*this, list, build_id);
  532. impl->cheat_engine->SetMainMemoryParameters(main_region_begin, main_region_size);
  533. }
  534. void System::SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set) {
  535. impl->applet_manager.SetAppletFrontendSet(std::move(set));
  536. }
  537. void System::SetDefaultAppletFrontendSet() {
  538. impl->applet_manager.SetDefaultAppletFrontendSet();
  539. }
  540. Service::AM::Applets::AppletManager& System::GetAppletManager() {
  541. return impl->applet_manager;
  542. }
  543. const Service::AM::Applets::AppletManager& System::GetAppletManager() const {
  544. return impl->applet_manager;
  545. }
  546. void System::SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider) {
  547. impl->content_provider = std::move(provider);
  548. }
  549. FileSys::ContentProvider& System::GetContentProvider() {
  550. return *impl->content_provider;
  551. }
  552. const FileSys::ContentProvider& System::GetContentProvider() const {
  553. return *impl->content_provider;
  554. }
  555. Service::FileSystem::FileSystemController& System::GetFileSystemController() {
  556. return impl->fs_controller;
  557. }
  558. const Service::FileSystem::FileSystemController& System::GetFileSystemController() const {
  559. return impl->fs_controller;
  560. }
  561. void System::RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  562. FileSys::ContentProvider* provider) {
  563. impl->content_provider->SetSlot(slot, provider);
  564. }
  565. void System::ClearContentProvider(FileSys::ContentProviderUnionSlot slot) {
  566. impl->content_provider->ClearSlot(slot);
  567. }
  568. const Reporter& System::GetReporter() const {
  569. return impl->reporter;
  570. }
  571. Service::Glue::ARPManager& System::GetARPManager() {
  572. return impl->arp_manager;
  573. }
  574. const Service::Glue::ARPManager& System::GetARPManager() const {
  575. return impl->arp_manager;
  576. }
  577. Service::APM::Controller& System::GetAPMController() {
  578. return impl->apm_controller;
  579. }
  580. const Service::APM::Controller& System::GetAPMController() const {
  581. return impl->apm_controller;
  582. }
  583. Service::Time::TimeManager& System::GetTimeManager() {
  584. return impl->time_manager;
  585. }
  586. const Service::Time::TimeManager& System::GetTimeManager() const {
  587. return impl->time_manager;
  588. }
  589. void System::SetExitLock(bool locked) {
  590. impl->exit_lock = locked;
  591. }
  592. bool System::GetExitLock() const {
  593. return impl->exit_lock;
  594. }
  595. void System::SetCurrentProcessBuildID(const CurrentBuildProcessID& id) {
  596. impl->build_id = id;
  597. }
  598. const System::CurrentBuildProcessID& System::GetCurrentProcessBuildID() const {
  599. return impl->build_id;
  600. }
  601. Service::SM::ServiceManager& System::ServiceManager() {
  602. return *impl->service_manager;
  603. }
  604. const Service::SM::ServiceManager& System::ServiceManager() const {
  605. return *impl->service_manager;
  606. }
  607. void System::RegisterCoreThread(std::size_t id) {
  608. impl->kernel.RegisterCoreThread(id);
  609. }
  610. void System::RegisterHostThread() {
  611. impl->kernel.RegisterHostThread();
  612. }
  613. void System::EnterDynarmicProfile() {
  614. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  615. impl->dynarmic_ticks[core] = MicroProfileEnter(impl->microprofile_dynarmic[core]);
  616. }
  617. void System::ExitDynarmicProfile() {
  618. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  619. MicroProfileLeave(impl->microprofile_dynarmic[core], impl->dynarmic_ticks[core]);
  620. }
  621. bool System::IsMulticore() const {
  622. return impl->is_multicore;
  623. }
  624. void System::RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback) {
  625. impl->execute_program_callback = std::move(callback);
  626. }
  627. void System::ExecuteProgram(std::size_t program_index) {
  628. if (impl->execute_program_callback) {
  629. impl->execute_program_callback(program_index);
  630. } else {
  631. LOG_CRITICAL(Core, "execute_program_callback must be initialized by the frontend");
  632. }
  633. }
  634. void System::ApplySettings() {
  635. if (IsPoweredOn()) {
  636. Renderer().RefreshBaseSettings();
  637. }
  638. Service::HID::ReloadInputDevices();
  639. }
  640. } // namespace Core