core.h 15 KB

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