core.cpp 27 KB

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