core.h 14 KB

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