core.cpp 29 KB

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