core.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. SystemResultStatus Run() {
  121. std::unique_lock<std::mutex> lk(suspend_guard);
  122. status = SystemResultStatus::Success;
  123. kernel.Suspend(false);
  124. core_timing.SyncPause(false);
  125. cpu_manager.Pause(false);
  126. is_paused = false;
  127. return status;
  128. }
  129. SystemResultStatus Pause() {
  130. std::unique_lock<std::mutex> lk(suspend_guard);
  131. status = SystemResultStatus::Success;
  132. core_timing.SyncPause(true);
  133. kernel.Suspend(true);
  134. cpu_manager.Pause(true);
  135. is_paused = true;
  136. return status;
  137. }
  138. std::unique_lock<std::mutex> StallCPU() {
  139. std::unique_lock<std::mutex> lk(suspend_guard);
  140. kernel.Suspend(true);
  141. core_timing.SyncPause(true);
  142. cpu_manager.Pause(true);
  143. return lk;
  144. }
  145. void UnstallCPU() {
  146. if (!is_paused) {
  147. core_timing.SyncPause(false);
  148. kernel.Suspend(false);
  149. cpu_manager.Pause(false);
  150. }
  151. }
  152. SystemResultStatus Init(System& system, Frontend::EmuWindow& emu_window) {
  153. LOG_DEBUG(Core, "initialized OK");
  154. device_memory = std::make_unique<Core::DeviceMemory>();
  155. is_multicore = Settings::values.use_multi_core.GetValue();
  156. is_async_gpu = Settings::values.use_asynchronous_gpu_emulation.GetValue();
  157. kernel.SetMulticore(is_multicore);
  158. cpu_manager.SetMulticore(is_multicore);
  159. cpu_manager.SetAsyncGpu(is_async_gpu);
  160. core_timing.SetMulticore(is_multicore);
  161. kernel.Initialize();
  162. cpu_manager.Initialize();
  163. core_timing.Initialize([&system]() { system.RegisterHostThread(); });
  164. const auto posix_time = std::chrono::system_clock::now().time_since_epoch();
  165. const auto current_time =
  166. std::chrono::duration_cast<std::chrono::seconds>(posix_time).count();
  167. Settings::values.custom_rtc_differential =
  168. Settings::values.custom_rtc.value_or(current_time) - current_time;
  169. // Create a default fs if one doesn't already exist.
  170. if (virtual_filesystem == nullptr)
  171. virtual_filesystem = std::make_shared<FileSys::RealVfsFilesystem>();
  172. if (content_provider == nullptr)
  173. content_provider = std::make_unique<FileSys::ContentProviderUnion>();
  174. /// Create default implementations of applets if one is not provided.
  175. applet_manager.SetDefaultAppletsIfMissing();
  176. /// Reset all glue registrations
  177. arp_manager.ResetAll();
  178. telemetry_session = std::make_unique<Core::TelemetrySession>();
  179. gpu_core = VideoCore::CreateGPU(emu_window, system);
  180. if (!gpu_core) {
  181. return SystemResultStatus::ErrorVideoCore;
  182. }
  183. service_manager = std::make_shared<Service::SM::ServiceManager>(kernel);
  184. services = std::make_unique<Service::Services>(service_manager, system);
  185. interrupt_manager = std::make_unique<Hardware::InterruptManager>(system);
  186. // Initialize time manager, which must happen after kernel is created
  187. time_manager.Initialize();
  188. is_powered_on = true;
  189. exit_lock = false;
  190. microprofile_dynarmic[0] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU0);
  191. microprofile_dynarmic[1] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU1);
  192. microprofile_dynarmic[2] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU2);
  193. microprofile_dynarmic[3] = MICROPROFILE_TOKEN(ARM_Jit_Dynarmic_CPU3);
  194. LOG_DEBUG(Core, "Initialized OK");
  195. return SystemResultStatus::Success;
  196. }
  197. SystemResultStatus Load(System& system, Frontend::EmuWindow& emu_window,
  198. const std::string& filepath, u64 program_id,
  199. std::size_t program_index) {
  200. app_loader = Loader::GetLoader(system, GetGameFileFromPath(virtual_filesystem, filepath),
  201. program_id, program_index);
  202. if (!app_loader) {
  203. LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  204. return SystemResultStatus::ErrorGetLoader;
  205. }
  206. SystemResultStatus init_result{Init(system, emu_window)};
  207. if (init_result != SystemResultStatus::Success) {
  208. LOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  209. static_cast<int>(init_result));
  210. Shutdown();
  211. return init_result;
  212. }
  213. telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider);
  214. main_process = KProcessPtr{Kernel::KProcess::Create(system.Kernel()), KProcessDeleter};
  215. ASSERT(Kernel::KProcess::Initialize(main_process.get(), system, "main",
  216. Kernel::KProcess::ProcessType::Userland)
  217. .IsSuccess());
  218. main_process->Open();
  219. const auto [load_result, load_parameters] = app_loader->Load(*main_process, system);
  220. if (load_result != Loader::ResultStatus::Success) {
  221. LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", load_result);
  222. Shutdown();
  223. return static_cast<SystemResultStatus>(
  224. static_cast<u32>(SystemResultStatus::ErrorLoader) + static_cast<u32>(load_result));
  225. }
  226. AddGlueRegistrationForProcess(*app_loader, *main_process);
  227. kernel.MakeCurrentProcess(main_process.get());
  228. kernel.InitializeCores();
  229. // Initialize cheat engine
  230. if (cheat_engine) {
  231. cheat_engine->Initialize();
  232. }
  233. // All threads are started, begin main process execution, now that we're in the clear.
  234. main_process->Run(load_parameters->main_thread_priority,
  235. load_parameters->main_thread_stack_size);
  236. if (Settings::values.gamecard_inserted) {
  237. if (Settings::values.gamecard_current_game) {
  238. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, filepath));
  239. } else if (!Settings::values.gamecard_path.GetValue().empty()) {
  240. const auto gamecard_path = Settings::values.gamecard_path.GetValue();
  241. fs_controller.SetGameCard(GetGameFileFromPath(virtual_filesystem, gamecard_path));
  242. }
  243. }
  244. if (app_loader->ReadProgramId(program_id) != Loader::ResultStatus::Success) {
  245. LOG_ERROR(Core, "Failed to find title id for ROM (Error {})", load_result);
  246. }
  247. perf_stats = std::make_unique<PerfStats>(program_id);
  248. // Reset counters and set time origin to current frame
  249. GetAndResetPerfStats();
  250. perf_stats->BeginSystemFrame();
  251. status = SystemResultStatus::Success;
  252. return status;
  253. }
  254. void Shutdown() {
  255. // Log last frame performance stats if game was loded
  256. if (perf_stats) {
  257. const auto perf_results = GetAndResetPerfStats();
  258. constexpr auto performance = Common::Telemetry::FieldType::Performance;
  259. telemetry_session->AddField(performance, "Shutdown_EmulationSpeed",
  260. perf_results.emulation_speed * 100.0);
  261. telemetry_session->AddField(performance, "Shutdown_Framerate",
  262. perf_results.average_game_fps);
  263. telemetry_session->AddField(performance, "Shutdown_Frametime",
  264. perf_results.frametime * 1000.0);
  265. telemetry_session->AddField(performance, "Mean_Frametime_MS",
  266. perf_stats->GetMeanFrametime());
  267. }
  268. is_powered_on = false;
  269. exit_lock = false;
  270. services.reset();
  271. service_manager.reset();
  272. cheat_engine.reset();
  273. telemetry_session.reset();
  274. cpu_manager.Shutdown();
  275. time_manager.Shutdown();
  276. core_timing.Shutdown();
  277. app_loader.reset();
  278. perf_stats.reset();
  279. gpu_core.reset();
  280. kernel.Shutdown();
  281. memory.Reset();
  282. applet_manager.ClearAll();
  283. // TODO: The main process should be freed based on KAutoObject ref counting.
  284. main_process.reset();
  285. LOG_DEBUG(Core, "Shutdown OK");
  286. }
  287. Loader::ResultStatus GetGameName(std::string& out) const {
  288. if (app_loader == nullptr)
  289. return Loader::ResultStatus::ErrorNotInitialized;
  290. return app_loader->ReadTitle(out);
  291. }
  292. void AddGlueRegistrationForProcess(Loader::AppLoader& loader, Kernel::KProcess& process) {
  293. std::vector<u8> nacp_data;
  294. FileSys::NACP nacp;
  295. if (loader.ReadControlData(nacp) == Loader::ResultStatus::Success) {
  296. nacp_data = nacp.GetRawBytes();
  297. } else {
  298. nacp_data.resize(sizeof(FileSys::RawNACP));
  299. }
  300. Service::Glue::ApplicationLaunchProperty launch{};
  301. launch.title_id = process.GetTitleID();
  302. FileSys::PatchManager pm{launch.title_id, fs_controller, *content_provider};
  303. launch.version = pm.GetGameVersion().value_or(0);
  304. // TODO(DarkLordZach): When FSController/Game Card Support is added, if
  305. // current_process_game_card use correct StorageId
  306. launch.base_game_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  307. launch.title_id, FileSys::ContentRecordType::Program));
  308. launch.update_storage_id = GetStorageIdForFrontendSlot(content_provider->GetSlotForEntry(
  309. FileSys::GetUpdateTitleID(launch.title_id), FileSys::ContentRecordType::Program));
  310. arp_manager.Register(launch.title_id, launch, std::move(nacp_data));
  311. }
  312. void SetStatus(SystemResultStatus new_status, const char* details = nullptr) {
  313. status = new_status;
  314. if (details) {
  315. status_details = details;
  316. }
  317. }
  318. PerfStatsResults GetAndResetPerfStats() {
  319. return perf_stats->GetAndResetStats(core_timing.GetGlobalTimeUs());
  320. }
  321. std::mutex suspend_guard;
  322. bool is_paused{};
  323. Timing::CoreTiming core_timing;
  324. Kernel::KernelCore kernel;
  325. /// RealVfsFilesystem instance
  326. FileSys::VirtualFilesystem virtual_filesystem;
  327. /// ContentProviderUnion instance
  328. std::unique_ptr<FileSys::ContentProviderUnion> content_provider;
  329. Service::FileSystem::FileSystemController fs_controller;
  330. /// AppLoader used to load the current executing application
  331. std::unique_ptr<Loader::AppLoader> app_loader;
  332. std::unique_ptr<Tegra::GPU> gpu_core;
  333. std::unique_ptr<Hardware::InterruptManager> interrupt_manager;
  334. std::unique_ptr<Core::DeviceMemory> device_memory;
  335. KProcessPtr main_process{nullptr, KProcessDeleter};
  336. Core::Memory::Memory memory;
  337. CpuManager cpu_manager;
  338. std::atomic_bool is_powered_on{};
  339. bool exit_lock = false;
  340. Reporter reporter;
  341. std::unique_ptr<Memory::CheatEngine> cheat_engine;
  342. std::unique_ptr<Tools::Freezer> memory_freezer;
  343. std::array<u8, 0x20> build_id{};
  344. /// Frontend applets
  345. Service::AM::Applets::AppletManager applet_manager;
  346. /// APM (Performance) services
  347. Service::APM::Controller apm_controller{core_timing};
  348. /// Service State
  349. Service::Glue::ARPManager arp_manager;
  350. Service::Time::TimeManager time_manager;
  351. /// Service manager
  352. std::shared_ptr<Service::SM::ServiceManager> service_manager;
  353. /// Services
  354. std::unique_ptr<Service::Services> services;
  355. /// Telemetry session for this emulation session
  356. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  357. /// Network instance
  358. Network::NetworkInstance network_instance;
  359. SystemResultStatus status = SystemResultStatus::Success;
  360. std::string status_details = "";
  361. std::unique_ptr<Core::PerfStats> perf_stats;
  362. Core::SpeedLimiter speed_limiter;
  363. bool is_multicore{};
  364. bool is_async_gpu{};
  365. ExecuteProgramCallback execute_program_callback;
  366. ExitCallback exit_callback;
  367. std::array<u64, Core::Hardware::NUM_CPU_CORES> dynarmic_ticks{};
  368. std::array<MicroProfileToken, Core::Hardware::NUM_CPU_CORES> microprofile_dynarmic{};
  369. };
  370. System::System() : impl{std::make_unique<Impl>(*this)} {}
  371. System::~System() = default;
  372. CpuManager& System::GetCpuManager() {
  373. return impl->cpu_manager;
  374. }
  375. const CpuManager& System::GetCpuManager() const {
  376. return impl->cpu_manager;
  377. }
  378. SystemResultStatus System::Run() {
  379. return impl->Run();
  380. }
  381. SystemResultStatus System::Pause() {
  382. return impl->Pause();
  383. }
  384. SystemResultStatus System::SingleStep() {
  385. return SystemResultStatus::Success;
  386. }
  387. void System::InvalidateCpuInstructionCaches() {
  388. impl->kernel.InvalidateAllInstructionCaches();
  389. }
  390. void System::InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size) {
  391. impl->kernel.InvalidateCpuInstructionCacheRange(addr, size);
  392. }
  393. void System::Shutdown() {
  394. impl->Shutdown();
  395. }
  396. std::unique_lock<std::mutex> System::StallCPU() {
  397. return impl->StallCPU();
  398. }
  399. void System::UnstallCPU() {
  400. impl->UnstallCPU();
  401. }
  402. SystemResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::string& filepath,
  403. u64 program_id, std::size_t program_index) {
  404. return impl->Load(*this, emu_window, filepath, program_id, program_index);
  405. }
  406. bool System::IsPoweredOn() const {
  407. return impl->is_powered_on.load(std::memory_order::relaxed);
  408. }
  409. void System::PrepareReschedule() {
  410. // Deprecated, does nothing, kept for backward compatibility.
  411. }
  412. void System::PrepareReschedule(const u32 core_index) {
  413. impl->kernel.PrepareReschedule(core_index);
  414. }
  415. PerfStatsResults System::GetAndResetPerfStats() {
  416. return impl->GetAndResetPerfStats();
  417. }
  418. TelemetrySession& System::TelemetrySession() {
  419. return *impl->telemetry_session;
  420. }
  421. const TelemetrySession& System::TelemetrySession() const {
  422. return *impl->telemetry_session;
  423. }
  424. ARM_Interface& System::CurrentArmInterface() {
  425. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  426. }
  427. const ARM_Interface& System::CurrentArmInterface() const {
  428. return impl->kernel.CurrentPhysicalCore().ArmInterface();
  429. }
  430. std::size_t System::CurrentCoreIndex() const {
  431. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  432. ASSERT(core < Core::Hardware::NUM_CPU_CORES);
  433. return core;
  434. }
  435. Kernel::PhysicalCore& System::CurrentPhysicalCore() {
  436. return impl->kernel.CurrentPhysicalCore();
  437. }
  438. const Kernel::PhysicalCore& System::CurrentPhysicalCore() const {
  439. return impl->kernel.CurrentPhysicalCore();
  440. }
  441. /// Gets the global scheduler
  442. Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() {
  443. return impl->kernel.GlobalSchedulerContext();
  444. }
  445. /// Gets the global scheduler
  446. const Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() const {
  447. return impl->kernel.GlobalSchedulerContext();
  448. }
  449. Kernel::KProcess* System::CurrentProcess() {
  450. return impl->kernel.CurrentProcess();
  451. }
  452. Core::DeviceMemory& System::DeviceMemory() {
  453. return *impl->device_memory;
  454. }
  455. const Core::DeviceMemory& System::DeviceMemory() const {
  456. return *impl->device_memory;
  457. }
  458. const Kernel::KProcess* System::CurrentProcess() const {
  459. return impl->kernel.CurrentProcess();
  460. }
  461. ARM_Interface& System::ArmInterface(std::size_t core_index) {
  462. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  463. }
  464. const ARM_Interface& System::ArmInterface(std::size_t core_index) const {
  465. return impl->kernel.PhysicalCore(core_index).ArmInterface();
  466. }
  467. ExclusiveMonitor& System::Monitor() {
  468. return impl->kernel.GetExclusiveMonitor();
  469. }
  470. const ExclusiveMonitor& System::Monitor() const {
  471. return impl->kernel.GetExclusiveMonitor();
  472. }
  473. Memory::Memory& System::Memory() {
  474. return impl->memory;
  475. }
  476. const Core::Memory::Memory& System::Memory() const {
  477. return impl->memory;
  478. }
  479. Tegra::GPU& System::GPU() {
  480. return *impl->gpu_core;
  481. }
  482. const Tegra::GPU& System::GPU() const {
  483. return *impl->gpu_core;
  484. }
  485. Core::Hardware::InterruptManager& System::InterruptManager() {
  486. return *impl->interrupt_manager;
  487. }
  488. const Core::Hardware::InterruptManager& System::InterruptManager() const {
  489. return *impl->interrupt_manager;
  490. }
  491. VideoCore::RendererBase& System::Renderer() {
  492. return impl->gpu_core->Renderer();
  493. }
  494. const VideoCore::RendererBase& System::Renderer() const {
  495. return impl->gpu_core->Renderer();
  496. }
  497. Kernel::KernelCore& System::Kernel() {
  498. return impl->kernel;
  499. }
  500. const Kernel::KernelCore& System::Kernel() const {
  501. return impl->kernel;
  502. }
  503. Timing::CoreTiming& System::CoreTiming() {
  504. return impl->core_timing;
  505. }
  506. const Timing::CoreTiming& System::CoreTiming() const {
  507. return impl->core_timing;
  508. }
  509. Core::PerfStats& System::GetPerfStats() {
  510. return *impl->perf_stats;
  511. }
  512. const Core::PerfStats& System::GetPerfStats() const {
  513. return *impl->perf_stats;
  514. }
  515. Core::SpeedLimiter& System::SpeedLimiter() {
  516. return impl->speed_limiter;
  517. }
  518. const Core::SpeedLimiter& System::SpeedLimiter() const {
  519. return impl->speed_limiter;
  520. }
  521. Loader::ResultStatus System::GetGameName(std::string& out) const {
  522. return impl->GetGameName(out);
  523. }
  524. void System::SetStatus(SystemResultStatus new_status, const char* details) {
  525. impl->SetStatus(new_status, details);
  526. }
  527. const std::string& System::GetStatusDetails() const {
  528. return impl->status_details;
  529. }
  530. Loader::AppLoader& System::GetAppLoader() {
  531. return *impl->app_loader;
  532. }
  533. const Loader::AppLoader& System::GetAppLoader() const {
  534. return *impl->app_loader;
  535. }
  536. void System::SetFilesystem(FileSys::VirtualFilesystem vfs) {
  537. impl->virtual_filesystem = std::move(vfs);
  538. }
  539. FileSys::VirtualFilesystem System::GetFilesystem() const {
  540. return impl->virtual_filesystem;
  541. }
  542. void System::RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  543. const std::array<u8, 32>& build_id, VAddr main_region_begin,
  544. u64 main_region_size) {
  545. impl->cheat_engine = std::make_unique<Memory::CheatEngine>(*this, list, build_id);
  546. impl->cheat_engine->SetMainMemoryParameters(main_region_begin, main_region_size);
  547. }
  548. void System::SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set) {
  549. impl->applet_manager.SetAppletFrontendSet(std::move(set));
  550. }
  551. void System::SetDefaultAppletFrontendSet() {
  552. impl->applet_manager.SetDefaultAppletFrontendSet();
  553. }
  554. Service::AM::Applets::AppletManager& System::GetAppletManager() {
  555. return impl->applet_manager;
  556. }
  557. const Service::AM::Applets::AppletManager& System::GetAppletManager() const {
  558. return impl->applet_manager;
  559. }
  560. void System::SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider) {
  561. impl->content_provider = std::move(provider);
  562. }
  563. FileSys::ContentProvider& System::GetContentProvider() {
  564. return *impl->content_provider;
  565. }
  566. const FileSys::ContentProvider& System::GetContentProvider() const {
  567. return *impl->content_provider;
  568. }
  569. Service::FileSystem::FileSystemController& System::GetFileSystemController() {
  570. return impl->fs_controller;
  571. }
  572. const Service::FileSystem::FileSystemController& System::GetFileSystemController() const {
  573. return impl->fs_controller;
  574. }
  575. void System::RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  576. FileSys::ContentProvider* provider) {
  577. impl->content_provider->SetSlot(slot, provider);
  578. }
  579. void System::ClearContentProvider(FileSys::ContentProviderUnionSlot slot) {
  580. impl->content_provider->ClearSlot(slot);
  581. }
  582. const Reporter& System::GetReporter() const {
  583. return impl->reporter;
  584. }
  585. Service::Glue::ARPManager& System::GetARPManager() {
  586. return impl->arp_manager;
  587. }
  588. const Service::Glue::ARPManager& System::GetARPManager() const {
  589. return impl->arp_manager;
  590. }
  591. Service::APM::Controller& System::GetAPMController() {
  592. return impl->apm_controller;
  593. }
  594. const Service::APM::Controller& System::GetAPMController() const {
  595. return impl->apm_controller;
  596. }
  597. Service::Time::TimeManager& System::GetTimeManager() {
  598. return impl->time_manager;
  599. }
  600. const Service::Time::TimeManager& System::GetTimeManager() const {
  601. return impl->time_manager;
  602. }
  603. void System::SetExitLock(bool locked) {
  604. impl->exit_lock = locked;
  605. }
  606. bool System::GetExitLock() const {
  607. return impl->exit_lock;
  608. }
  609. void System::SetCurrentProcessBuildID(const CurrentBuildProcessID& id) {
  610. impl->build_id = id;
  611. }
  612. const System::CurrentBuildProcessID& System::GetCurrentProcessBuildID() const {
  613. return impl->build_id;
  614. }
  615. Service::SM::ServiceManager& System::ServiceManager() {
  616. return *impl->service_manager;
  617. }
  618. const Service::SM::ServiceManager& System::ServiceManager() const {
  619. return *impl->service_manager;
  620. }
  621. void System::RegisterCoreThread(std::size_t id) {
  622. impl->kernel.RegisterCoreThread(id);
  623. }
  624. void System::RegisterHostThread() {
  625. impl->kernel.RegisterHostThread();
  626. }
  627. void System::EnterDynarmicProfile() {
  628. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  629. impl->dynarmic_ticks[core] = MicroProfileEnter(impl->microprofile_dynarmic[core]);
  630. }
  631. void System::ExitDynarmicProfile() {
  632. std::size_t core = impl->kernel.GetCurrentHostThreadID();
  633. MicroProfileLeave(impl->microprofile_dynarmic[core], impl->dynarmic_ticks[core]);
  634. }
  635. bool System::IsMulticore() const {
  636. return impl->is_multicore;
  637. }
  638. void System::RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback) {
  639. impl->execute_program_callback = std::move(callback);
  640. }
  641. void System::ExecuteProgram(std::size_t program_index) {
  642. if (impl->execute_program_callback) {
  643. impl->execute_program_callback(program_index);
  644. } else {
  645. LOG_CRITICAL(Core, "execute_program_callback must be initialized by the frontend");
  646. }
  647. }
  648. void System::RegisterExitCallback(ExitCallback&& callback) {
  649. impl->exit_callback = std::move(callback);
  650. }
  651. void System::Exit() {
  652. if (impl->exit_callback) {
  653. impl->exit_callback();
  654. } else {
  655. LOG_CRITICAL(Core, "exit_callback must be initialized by the frontend");
  656. }
  657. }
  658. void System::ApplySettings() {
  659. if (IsPoweredOn()) {
  660. Renderer().RefreshBaseSettings();
  661. }
  662. Service::HID::ReloadInputDevices();
  663. }
  664. } // namespace Core