core.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <span>
  10. #include <string>
  11. #include <vector>
  12. #include "common/common_types.h"
  13. #include "core/file_sys/vfs_types.h"
  14. namespace Core::Frontend {
  15. class EmuWindow;
  16. } // namespace Core::Frontend
  17. namespace FileSys {
  18. class ContentProvider;
  19. class ContentProviderUnion;
  20. enum class ContentProviderUnionSlot;
  21. class VfsFilesystem;
  22. } // namespace FileSys
  23. namespace Kernel {
  24. class GlobalSchedulerContext;
  25. class KernelCore;
  26. class PhysicalCore;
  27. class KProcess;
  28. class KScheduler;
  29. } // namespace Kernel
  30. namespace Loader {
  31. class AppLoader;
  32. enum class ResultStatus : u16;
  33. } // namespace Loader
  34. namespace Core::Memory {
  35. struct CheatEntry;
  36. class Memory;
  37. } // namespace Core::Memory
  38. namespace Service {
  39. namespace Account {
  40. class ProfileManager;
  41. } // namespace Account
  42. namespace AM::Applets {
  43. struct AppletFrontendSet;
  44. class AppletManager;
  45. } // namespace AM::Applets
  46. namespace APM {
  47. class Controller;
  48. }
  49. namespace FileSystem {
  50. class FileSystemController;
  51. } // namespace FileSystem
  52. namespace Glue {
  53. class ARPManager;
  54. }
  55. class ServerManager;
  56. namespace SM {
  57. class ServiceManager;
  58. } // namespace SM
  59. } // namespace Service
  60. namespace Tegra {
  61. class DebugContext;
  62. class GPU;
  63. namespace Host1x {
  64. class Host1x;
  65. } // namespace Host1x
  66. } // namespace Tegra
  67. namespace VideoCore {
  68. class RendererBase;
  69. } // namespace VideoCore
  70. namespace AudioCore {
  71. class AudioCore;
  72. } // namespace AudioCore
  73. namespace Core::Timing {
  74. class CoreTiming;
  75. }
  76. namespace Core::HID {
  77. class HIDCore;
  78. }
  79. namespace Network {
  80. class RoomNetwork;
  81. }
  82. namespace Tools {
  83. class RenderdocAPI;
  84. }
  85. namespace Core {
  86. class CpuManager;
  87. class Debugger;
  88. class DeviceMemory;
  89. class ExclusiveMonitor;
  90. class GPUDirtyMemoryManager;
  91. class PerfStats;
  92. class Reporter;
  93. class SpeedLimiter;
  94. class TelemetrySession;
  95. struct PerfStatsResults;
  96. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  97. const std::string& path);
  98. /// Enumeration representing the return values of the System Initialize and Load process.
  99. enum class SystemResultStatus : u32 {
  100. Success, ///< Succeeded
  101. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  102. ErrorGetLoader, ///< Error finding the correct application loader
  103. ErrorSystemFiles, ///< Error in finding system files
  104. ErrorSharedFont, ///< Error in finding shared font
  105. ErrorVideoCore, ///< Error in the video core
  106. ErrorUnknown, ///< Any other error
  107. ErrorLoader, ///< The base for loader errors (too many to repeat)
  108. };
  109. class System {
  110. public:
  111. using CurrentBuildProcessID = std::array<u8, 0x20>;
  112. explicit System();
  113. ~System();
  114. System(const System&) = delete;
  115. System& operator=(const System&) = delete;
  116. System(System&&) = delete;
  117. System& operator=(System&&) = delete;
  118. /**
  119. * Initializes the system
  120. * This function will initialize core functionality used for system emulation
  121. */
  122. void Initialize();
  123. /**
  124. * Run the OS and Application
  125. * This function will start emulation and run the relevant devices
  126. */
  127. void Run();
  128. /**
  129. * Pause the OS and Application
  130. * This function will pause emulation and stop the relevant devices
  131. */
  132. void Pause();
  133. /// Check if the core is currently paused.
  134. [[nodiscard]] bool IsPaused() const;
  135. /// Shutdown the main emulated process.
  136. void ShutdownMainProcess();
  137. /// Check if the core is shutting down.
  138. [[nodiscard]] bool IsShuttingDown() const;
  139. /// Set the shutting down state.
  140. void SetShuttingDown(bool shutting_down);
  141. /// Forcibly detach the debugger if it is running.
  142. void DetachDebugger();
  143. std::unique_lock<std::mutex> StallApplication();
  144. void UnstallApplication();
  145. void SetNVDECActive(bool is_nvdec_active);
  146. [[nodiscard]] bool GetNVDECActive();
  147. /**
  148. * Initialize the debugger.
  149. */
  150. void InitializeDebugger();
  151. /**
  152. * Load an executable application.
  153. * @param emu_window Reference to the host-system window used for video output and keyboard
  154. * input.
  155. * @param filepath String path to the executable application to load on the host file system.
  156. * @param program_index Specifies the index within the container of the program to launch.
  157. * @returns SystemResultStatus code, indicating if the operation succeeded.
  158. */
  159. [[nodiscard]] SystemResultStatus Load(Frontend::EmuWindow& emu_window,
  160. const std::string& filepath, u64 program_id = 0,
  161. std::size_t program_index = 0);
  162. /**
  163. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  164. * application).
  165. * @returns True if the emulated system is powered on, otherwise false.
  166. */
  167. [[nodiscard]] bool IsPoweredOn() const;
  168. /// Gets a reference to the telemetry session for this emulation session.
  169. [[nodiscard]] Core::TelemetrySession& TelemetrySession();
  170. /// Gets a reference to the telemetry session for this emulation session.
  171. [[nodiscard]] const Core::TelemetrySession& TelemetrySession() const;
  172. /// Prepare the core emulation for a reschedule
  173. void PrepareReschedule(u32 core_index);
  174. std::span<GPUDirtyMemoryManager> GetGPUDirtyMemoryManager();
  175. void GatherGPUDirtyMemory(std::function<void(PAddr, 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]] Core::Debugger& GetDebugger();
  275. [[nodiscard]] const Core::Debugger& GetDebugger() const;
  276. /// Gets a mutable reference to the Room Network.
  277. [[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
  278. /// Gets an immutable reference to the Room Network.
  279. [[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
  280. [[nodiscard]] Tools::RenderdocAPI& GetRenderdocAPI();
  281. void SetExitLocked(bool locked);
  282. bool GetExitLocked() const;
  283. void SetExitRequested(bool requested);
  284. bool GetExitRequested() const;
  285. void SetApplicationProcessBuildID(const CurrentBuildProcessID& id);
  286. [[nodiscard]] const CurrentBuildProcessID& GetApplicationProcessBuildID() const;
  287. /// Register a host thread as an emulated CPU Core.
  288. void RegisterCoreThread(std::size_t id);
  289. /// Register a host thread as an auxiliary thread.
  290. void RegisterHostThread();
  291. /// Enter CPU Microprofile
  292. void EnterCPUProfile();
  293. /// Exit CPU Microprofile
  294. void ExitCPUProfile();
  295. /// Tells if system is running on multicore.
  296. [[nodiscard]] bool IsMulticore() const;
  297. /// Tells if the system debugger is enabled.
  298. [[nodiscard]] bool DebuggerEnabled() const;
  299. /// Runs a server instance until shutdown.
  300. void RunServer(std::unique_ptr<Service::ServerManager>&& server_manager);
  301. /// Type used for the frontend to designate a callback for System to re-launch the application
  302. /// using a specified program index.
  303. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  304. /**
  305. * Registers a callback from the frontend for System to re-launch the application using a
  306. * specified program index.
  307. * @param callback Callback from the frontend to relaunch the application.
  308. */
  309. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  310. /**
  311. * Instructs the frontend to re-launch the application using the specified program_index.
  312. * @param program_index Specifies the index within the application of the program to launch.
  313. */
  314. void ExecuteProgram(std::size_t program_index);
  315. /**
  316. * Gets a reference to the user channel stack.
  317. * It is used to transfer data between programs.
  318. */
  319. [[nodiscard]] std::deque<std::vector<u8>>& GetUserChannel();
  320. /// Type used for the frontend to designate a callback for System to exit the application.
  321. using ExitCallback = std::function<void()>;
  322. /**
  323. * Registers a callback from the frontend for System to exit the application.
  324. * @param callback Callback from the frontend to exit the application.
  325. */
  326. void RegisterExitCallback(ExitCallback&& callback);
  327. /// Instructs the frontend to exit the application.
  328. void Exit();
  329. /// Applies any changes to settings to this core instance.
  330. void ApplySettings();
  331. private:
  332. struct Impl;
  333. std::unique_ptr<Impl> impl;
  334. };
  335. } // namespace Core