core.cpp 26 KB

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