core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <deque>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <string>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. #include "core/file_sys/vfs_types.h"
  13. namespace Core::Frontend {
  14. class EmuWindow;
  15. } // namespace Core::Frontend
  16. namespace FileSys {
  17. class ContentProvider;
  18. class ContentProviderUnion;
  19. enum class ContentProviderUnionSlot;
  20. class VfsFilesystem;
  21. } // namespace FileSys
  22. namespace Kernel {
  23. class GlobalSchedulerContext;
  24. class KernelCore;
  25. class PhysicalCore;
  26. class KProcess;
  27. class KScheduler;
  28. } // namespace Kernel
  29. namespace Loader {
  30. class AppLoader;
  31. enum class ResultStatus : u16;
  32. } // namespace Loader
  33. namespace Core::Memory {
  34. struct CheatEntry;
  35. class Memory;
  36. } // namespace Core::Memory
  37. namespace Service {
  38. namespace Account {
  39. class ProfileManager;
  40. } // namespace Account
  41. namespace AM::Applets {
  42. struct AppletFrontendSet;
  43. class AppletManager;
  44. } // namespace AM::Applets
  45. namespace APM {
  46. class Controller;
  47. }
  48. namespace FileSystem {
  49. class FileSystemController;
  50. } // namespace FileSystem
  51. namespace Glue {
  52. class ARPManager;
  53. }
  54. class ServerManager;
  55. namespace SM {
  56. class ServiceManager;
  57. } // namespace SM
  58. namespace Time {
  59. class TimeManager;
  60. } // namespace Time
  61. } // namespace Service
  62. namespace Tegra {
  63. class DebugContext;
  64. class GPU;
  65. namespace Host1x {
  66. class Host1x;
  67. } // namespace Host1x
  68. } // namespace Tegra
  69. namespace VideoCore {
  70. class RendererBase;
  71. } // namespace VideoCore
  72. namespace AudioCore {
  73. class AudioCore;
  74. } // namespace AudioCore
  75. namespace Core::Timing {
  76. class CoreTiming;
  77. }
  78. namespace Core::HID {
  79. class HIDCore;
  80. }
  81. namespace Network {
  82. class RoomNetwork;
  83. }
  84. namespace Tools {
  85. class RenderdocAPI;
  86. }
  87. namespace Core {
  88. class CpuManager;
  89. class Debugger;
  90. class DeviceMemory;
  91. class ExclusiveMonitor;
  92. class PerfStats;
  93. class Reporter;
  94. class SpeedLimiter;
  95. class TelemetrySession;
  96. struct PerfStatsResults;
  97. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  98. const std::string& path);
  99. /// Enumeration representing the return values of the System Initialize and Load process.
  100. enum class SystemResultStatus : u32 {
  101. Success, ///< Succeeded
  102. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  103. ErrorGetLoader, ///< Error finding the correct application loader
  104. ErrorSystemFiles, ///< Error in finding system files
  105. ErrorSharedFont, ///< Error in finding shared font
  106. ErrorVideoCore, ///< Error in the video core
  107. ErrorUnknown, ///< Any other error
  108. ErrorLoader, ///< The base for loader errors (too many to repeat)
  109. };
  110. class System {
  111. public:
  112. using CurrentBuildProcessID = std::array<u8, 0x20>;
  113. explicit System();
  114. ~System();
  115. System(const System&) = delete;
  116. System& operator=(const System&) = delete;
  117. System(System&&) = delete;
  118. System& operator=(System&&) = delete;
  119. /**
  120. * Initializes the system
  121. * This function will initialize core functionality used for system emulation
  122. */
  123. void Initialize();
  124. /**
  125. * Run the OS and Application
  126. * This function will start emulation and run the relevant devices
  127. */
  128. void Run();
  129. /**
  130. * Pause the OS and Application
  131. * This function will pause emulation and stop the relevant devices
  132. */
  133. void Pause();
  134. /// Check if the core is currently paused.
  135. [[nodiscard]] bool IsPaused() const;
  136. /// Shutdown the main emulated process.
  137. void ShutdownMainProcess();
  138. /// Check if the core is shutting down.
  139. [[nodiscard]] bool IsShuttingDown() const;
  140. /// Set the shutting down state.
  141. void SetShuttingDown(bool shutting_down);
  142. /// Forcibly detach the debugger if it is running.
  143. void DetachDebugger();
  144. std::unique_lock<std::mutex> StallApplication();
  145. void UnstallApplication();
  146. void SetNVDECActive(bool is_nvdec_active);
  147. [[nodiscard]] bool GetNVDECActive();
  148. /**
  149. * Initialize the debugger.
  150. */
  151. void InitializeDebugger();
  152. /**
  153. * Load an executable application.
  154. * @param emu_window Reference to the host-system window used for video output and keyboard
  155. * input.
  156. * @param filepath String path to the executable application to load on the host file system.
  157. * @param program_index Specifies the index within the container of the program to launch.
  158. * @returns SystemResultStatus code, indicating if the operation succeeded.
  159. */
  160. [[nodiscard]] SystemResultStatus Load(Frontend::EmuWindow& emu_window,
  161. const std::string& filepath, u64 program_id = 0,
  162. std::size_t program_index = 0);
  163. /**
  164. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  165. * application).
  166. * @returns True if the emulated system is powered on, otherwise false.
  167. */
  168. [[nodiscard]] bool IsPoweredOn() const;
  169. /// Gets a reference to the telemetry session for this emulation session.
  170. [[nodiscard]] Core::TelemetrySession& TelemetrySession();
  171. /// Gets a reference to the telemetry session for this emulation session.
  172. [[nodiscard]] const Core::TelemetrySession& TelemetrySession() const;
  173. /// Prepare the core emulation for a reschedule
  174. void PrepareReschedule(u32 core_index);
  175. void GatherGPUDirtyMemory(std::function<void(VAddr, size_t)>& callback);
  176. [[nodiscard]] size_t GetCurrentHostThreadID() const;
  177. /// Gets and resets core performance statistics
  178. [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
  179. /// Gets the physical core for the CPU core that is currently running
  180. [[nodiscard]] Kernel::PhysicalCore& CurrentPhysicalCore();
  181. /// Gets the physical core for the CPU core that is currently running
  182. [[nodiscard]] const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  183. /// Gets a reference to the underlying CPU manager.
  184. [[nodiscard]] CpuManager& GetCpuManager();
  185. /// Gets a const reference to the underlying CPU manager
  186. [[nodiscard]] const CpuManager& GetCpuManager() const;
  187. /// Gets a mutable reference to the system memory instance.
  188. [[nodiscard]] Core::Memory::Memory& ApplicationMemory();
  189. /// Gets a constant reference to the system memory instance.
  190. [[nodiscard]] const Core::Memory::Memory& ApplicationMemory() const;
  191. /// Gets a mutable reference to the GPU interface
  192. [[nodiscard]] Tegra::GPU& GPU();
  193. /// Gets an immutable reference to the GPU interface.
  194. [[nodiscard]] const Tegra::GPU& GPU() const;
  195. /// Gets a mutable reference to the Host1x interface
  196. [[nodiscard]] Tegra::Host1x::Host1x& Host1x();
  197. /// Gets an immutable reference to the Host1x interface.
  198. [[nodiscard]] const Tegra::Host1x::Host1x& Host1x() const;
  199. /// Gets a mutable reference to the renderer.
  200. [[nodiscard]] VideoCore::RendererBase& Renderer();
  201. /// Gets an immutable reference to the renderer.
  202. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  203. /// Gets a mutable reference to the audio interface
  204. [[nodiscard]] AudioCore::AudioCore& AudioCore();
  205. /// Gets an immutable reference to the audio interface.
  206. [[nodiscard]] const AudioCore::AudioCore& AudioCore() const;
  207. /// Gets the global scheduler
  208. [[nodiscard]] Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  209. /// Gets the global scheduler
  210. [[nodiscard]] const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  211. /// Gets the manager for the guest device memory
  212. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  213. /// Gets the manager for the guest device memory
  214. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  215. /// Provides a pointer to the application process
  216. [[nodiscard]] Kernel::KProcess* ApplicationProcess();
  217. /// Provides a constant pointer to the application process.
  218. [[nodiscard]] const Kernel::KProcess* ApplicationProcess() const;
  219. /// Provides a reference to the core timing instance.
  220. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  221. /// Provides a constant reference to the core timing instance.
  222. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  223. /// Provides a reference to the kernel instance.
  224. [[nodiscard]] Kernel::KernelCore& Kernel();
  225. /// Provides a constant reference to the kernel instance.
  226. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  227. /// Gets a mutable reference to the HID interface.
  228. [[nodiscard]] HID::HIDCore& HIDCore();
  229. /// Gets an immutable reference to the HID interface.
  230. [[nodiscard]] const HID::HIDCore& HIDCore() const;
  231. /// Provides a reference to the internal PerfStats instance.
  232. [[nodiscard]] Core::PerfStats& GetPerfStats();
  233. /// Provides a constant reference to the internal PerfStats instance.
  234. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  235. /// Provides a reference to the speed limiter;
  236. [[nodiscard]] Core::SpeedLimiter& SpeedLimiter();
  237. /// Provides a constant reference to the speed limiter
  238. [[nodiscard]] const Core::SpeedLimiter& SpeedLimiter() const;
  239. [[nodiscard]] u64 GetApplicationProcessProgramID() const;
  240. /// Gets the name of the current game
  241. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  242. void SetStatus(SystemResultStatus new_status, const char* details);
  243. [[nodiscard]] const std::string& GetStatusDetails() const;
  244. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  245. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  246. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  247. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  248. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  249. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  250. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  251. const std::array<u8, 0x20>& build_id, u64 main_region_begin,
  252. u64 main_region_size);
  253. void SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set);
  254. void SetDefaultAppletFrontendSet();
  255. [[nodiscard]] Service::AM::Applets::AppletManager& GetAppletManager();
  256. [[nodiscard]] const Service::AM::Applets::AppletManager& GetAppletManager() const;
  257. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  258. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  259. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  260. [[nodiscard]] FileSys::ContentProviderUnion& GetContentProviderUnion();
  261. [[nodiscard]] const FileSys::ContentProviderUnion& GetContentProviderUnion() const;
  262. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  263. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  264. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  265. FileSys::ContentProvider* provider);
  266. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  267. [[nodiscard]] const Reporter& GetReporter() const;
  268. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  269. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  270. [[nodiscard]] Service::APM::Controller& GetAPMController();
  271. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  272. [[nodiscard]] Service::Account::ProfileManager& GetProfileManager();
  273. [[nodiscard]] const Service::Account::ProfileManager& GetProfileManager() const;
  274. [[nodiscard]] Service::Time::TimeManager& GetTimeManager();
  275. [[nodiscard]] const Service::Time::TimeManager& GetTimeManager() const;
  276. [[nodiscard]] Core::Debugger& GetDebugger();
  277. [[nodiscard]] const Core::Debugger& GetDebugger() const;
  278. /// Gets a mutable reference to the Room Network.
  279. [[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
  280. /// Gets an immutable reference to the Room Network.
  281. [[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
  282. [[nodiscard]] Tools::RenderdocAPI& GetRenderdocAPI();
  283. void SetExitLocked(bool locked);
  284. bool GetExitLocked() const;
  285. void SetExitRequested(bool requested);
  286. bool GetExitRequested() const;
  287. void SetApplicationProcessBuildID(const CurrentBuildProcessID& id);
  288. [[nodiscard]] const CurrentBuildProcessID& GetApplicationProcessBuildID() const;
  289. /// Register a host thread as an emulated CPU Core.
  290. void RegisterCoreThread(std::size_t id);
  291. /// Register a host thread as an auxiliary thread.
  292. void RegisterHostThread();
  293. /// Enter CPU Microprofile
  294. void EnterCPUProfile();
  295. /// Exit CPU Microprofile
  296. void ExitCPUProfile();
  297. /// Tells if system is running on multicore.
  298. [[nodiscard]] bool IsMulticore() const;
  299. /// Tells if the system debugger is enabled.
  300. [[nodiscard]] bool DebuggerEnabled() const;
  301. /// Runs a server instance until shutdown.
  302. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  303. /// Type used for the frontend to designate a callback for System to re-launch the application
  304. /// using a specified program index.
  305. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  306. /**
  307. * Registers a callback from the frontend for System to re-launch the application using a
  308. * specified program index.
  309. * @param callback Callback from the frontend to relaunch the application.
  310. */
  311. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  312. /**
  313. * Instructs the frontend to re-launch the application using the specified program_index.
  314. * @param program_index Specifies the index within the application of the program to launch.
  315. */
  316. void ExecuteProgram(std::size_t program_index);
  317. /**
  318. * Gets a reference to the user channel stack.
  319. * It is used to transfer data between programs.
  320. */
  321. [[nodiscard]] std::deque<std::vector<u8>>& GetUserChannel();
  322. /// Type used for the frontend to designate a callback for System to exit the application.
  323. using ExitCallback = std::function<void()>;
  324. /**
  325. * Registers a callback from the frontend for System to exit the application.
  326. * @param callback Callback from the frontend to exit the application.
  327. */
  328. void RegisterExitCallback(ExitCallback&& callback);
  329. /// Instructs the frontend to exit the application.
  330. void Exit();
  331. /// Applies any changes to settings to this core instance.
  332. void ApplySettings();
  333. private:
  334. struct Impl;
  335. std::unique_ptr<Impl> impl;
  336. };
  337. } // namespace Core