core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <functional>
  7. #include <memory>
  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 Core::Timing {
  65. class CoreTiming;
  66. }
  67. namespace Core::Hardware {
  68. class InterruptManager;
  69. }
  70. namespace Core {
  71. class ARM_Interface;
  72. class CpuManager;
  73. class DeviceMemory;
  74. class ExclusiveMonitor;
  75. class FrameLimiter;
  76. class PerfStats;
  77. class Reporter;
  78. class TelemetrySession;
  79. struct PerfStatsResults;
  80. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  81. const std::string& path);
  82. class System {
  83. public:
  84. using CurrentBuildProcessID = std::array<u8, 0x20>;
  85. System(const System&) = delete;
  86. System& operator=(const System&) = delete;
  87. System(System&&) = delete;
  88. System& operator=(System&&) = delete;
  89. ~System();
  90. /**
  91. * Gets the instance of the System singleton class.
  92. * @returns Reference to the instance of the System singleton class.
  93. */
  94. [[deprecated("Use of the global system instance is deprecated")]] static System& GetInstance() {
  95. return s_instance;
  96. }
  97. /// Enumeration representing the return values of the System Initialize and Load process.
  98. enum class ResultStatus : 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. /**
  109. * Run the OS and Application
  110. * This function will start emulation and run the relevant devices
  111. */
  112. [[nodiscard]] ResultStatus Run();
  113. /**
  114. * Pause the OS and Application
  115. * This function will pause emulation and stop the relevant devices
  116. */
  117. [[nodiscard]] ResultStatus Pause();
  118. /**
  119. * Step the CPU one instruction
  120. * @return Result status, indicating whether or not the operation succeeded.
  121. */
  122. [[nodiscard]] ResultStatus SingleStep();
  123. /**
  124. * Invalidate the CPU instruction caches
  125. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  126. * step/continue commands.
  127. */
  128. void InvalidateCpuInstructionCaches();
  129. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  130. /// Shutdown the emulated system.
  131. void Shutdown();
  132. /**
  133. * Load an executable application.
  134. * @param emu_window Reference to the host-system window used for video output and keyboard
  135. * input.
  136. * @param filepath String path to the executable application to load on the host file system.
  137. * @param program_index Specifies the index within the container of the program to launch.
  138. * @returns ResultStatus code, indicating if the operation succeeded.
  139. */
  140. [[nodiscard]] ResultStatus Load(Frontend::EmuWindow& emu_window, const std::string& filepath,
  141. std::size_t program_index = 0);
  142. /**
  143. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  144. * application).
  145. * @returns True if the emulated system is powered on, otherwise false.
  146. */
  147. [[nodiscard]] bool IsPoweredOn() const;
  148. /// Gets a reference to the telemetry session for this emulation session.
  149. [[nodiscard]] Core::TelemetrySession& TelemetrySession();
  150. /// Gets a reference to the telemetry session for this emulation session.
  151. [[nodiscard]] const Core::TelemetrySession& TelemetrySession() const;
  152. /// Prepare the core emulation for a reschedule
  153. void PrepareReschedule();
  154. /// Prepare the core emulation for a reschedule
  155. void PrepareReschedule(u32 core_index);
  156. /// Gets and resets core performance statistics
  157. [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
  158. /// Gets an ARM interface to the CPU core that is currently running
  159. [[nodiscard]] ARM_Interface& CurrentArmInterface();
  160. /// Gets an ARM interface to the CPU core that is currently running
  161. [[nodiscard]] const ARM_Interface& CurrentArmInterface() const;
  162. /// Gets the index of the currently running CPU core
  163. [[nodiscard]] std::size_t CurrentCoreIndex() const;
  164. /// Gets the physical core for the CPU core that is currently running
  165. [[nodiscard]] Kernel::PhysicalCore& CurrentPhysicalCore();
  166. /// Gets the physical core for the CPU core that is currently running
  167. [[nodiscard]] const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  168. /// Gets a reference to an ARM interface for the CPU core with the specified index
  169. [[nodiscard]] ARM_Interface& ArmInterface(std::size_t core_index);
  170. /// Gets a const reference to an ARM interface from the CPU core with the specified index
  171. [[nodiscard]] const ARM_Interface& ArmInterface(std::size_t core_index) const;
  172. /// Gets a reference to the underlying CPU manager.
  173. [[nodiscard]] CpuManager& GetCpuManager();
  174. /// Gets a const reference to the underlying CPU manager
  175. [[nodiscard]] const CpuManager& GetCpuManager() const;
  176. /// Gets a reference to the exclusive monitor
  177. [[nodiscard]] ExclusiveMonitor& Monitor();
  178. /// Gets a constant reference to the exclusive monitor
  179. [[nodiscard]] const ExclusiveMonitor& Monitor() const;
  180. /// Gets a mutable reference to the system memory instance.
  181. [[nodiscard]] Core::Memory::Memory& Memory();
  182. /// Gets a constant reference to the system memory instance.
  183. [[nodiscard]] const Core::Memory::Memory& Memory() const;
  184. /// Gets a mutable reference to the GPU interface
  185. [[nodiscard]] Tegra::GPU& GPU();
  186. /// Gets an immutable reference to the GPU interface.
  187. [[nodiscard]] const Tegra::GPU& GPU() const;
  188. /// Gets a mutable reference to the renderer.
  189. [[nodiscard]] VideoCore::RendererBase& Renderer();
  190. /// Gets an immutable reference to the renderer.
  191. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  192. /// Gets the global scheduler
  193. [[nodiscard]] Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  194. /// Gets the global scheduler
  195. [[nodiscard]] const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  196. /// Gets the manager for the guest device memory
  197. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  198. /// Gets the manager for the guest device memory
  199. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  200. /// Provides a pointer to the current process
  201. [[nodiscard]] Kernel::KProcess* CurrentProcess();
  202. /// Provides a constant pointer to the current process.
  203. [[nodiscard]] const Kernel::KProcess* CurrentProcess() const;
  204. /// Provides a reference to the core timing instance.
  205. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  206. /// Provides a constant reference to the core timing instance.
  207. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  208. /// Provides a reference to the interrupt manager instance.
  209. [[nodiscard]] Core::Hardware::InterruptManager& InterruptManager();
  210. /// Provides a constant reference to the interrupt manager instance.
  211. [[nodiscard]] const Core::Hardware::InterruptManager& InterruptManager() const;
  212. /// Provides a reference to the kernel instance.
  213. [[nodiscard]] Kernel::KernelCore& Kernel();
  214. /// Provides a constant reference to the kernel instance.
  215. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  216. /// Provides a reference to the internal PerfStats instance.
  217. [[nodiscard]] Core::PerfStats& GetPerfStats();
  218. /// Provides a constant reference to the internal PerfStats instance.
  219. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  220. /// Provides a reference to the frame limiter;
  221. [[nodiscard]] Core::FrameLimiter& FrameLimiter();
  222. /// Provides a constant referent to the frame limiter
  223. [[nodiscard]] const Core::FrameLimiter& FrameLimiter() const;
  224. /// Gets the name of the current game
  225. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  226. void SetStatus(ResultStatus new_status, const char* details);
  227. [[nodiscard]] const std::string& GetStatusDetails() const;
  228. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  229. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  230. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  231. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  232. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  233. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  234. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  235. const std::array<u8, 0x20>& build_id, VAddr main_region_begin,
  236. u64 main_region_size);
  237. void SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set);
  238. void SetDefaultAppletFrontendSet();
  239. [[nodiscard]] Service::AM::Applets::AppletManager& GetAppletManager();
  240. [[nodiscard]] const Service::AM::Applets::AppletManager& GetAppletManager() const;
  241. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  242. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  243. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  244. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  245. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  246. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  247. FileSys::ContentProvider* provider);
  248. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  249. [[nodiscard]] const Reporter& GetReporter() const;
  250. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  251. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  252. [[nodiscard]] Service::APM::Controller& GetAPMController();
  253. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  254. [[nodiscard]] Service::Time::TimeManager& GetTimeManager();
  255. [[nodiscard]] const Service::Time::TimeManager& GetTimeManager() const;
  256. void SetExitLock(bool locked);
  257. [[nodiscard]] bool GetExitLock() const;
  258. void SetCurrentProcessBuildID(const CurrentBuildProcessID& id);
  259. [[nodiscard]] const CurrentBuildProcessID& GetCurrentProcessBuildID() const;
  260. /// Register a host thread as an emulated CPU Core.
  261. void RegisterCoreThread(std::size_t id);
  262. /// Register a host thread as an auxiliary thread.
  263. void RegisterHostThread();
  264. /// Enter Dynarmic Microprofile
  265. void EnterDynarmicProfile();
  266. /// Exit Dynarmic Microprofile
  267. void ExitDynarmicProfile();
  268. /// Tells if system is running on multicore.
  269. [[nodiscard]] bool IsMulticore() const;
  270. /// Type used for the frontend to designate a callback for System to re-launch the application
  271. /// using a specified program index.
  272. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  273. /**
  274. * Registers a callback from the frontend for System to re-launch the application using a
  275. * specified program index.
  276. * @param callback Callback from the frontend to relaunch the application.
  277. */
  278. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  279. /**
  280. * Instructs the frontend to re-launch the application using the specified program_index.
  281. * @param program_index Specifies the index within the application of the program to launch.
  282. */
  283. void ExecuteProgram(std::size_t program_index);
  284. /// Applies any changes to settings to this core instance.
  285. void ApplySettings();
  286. private:
  287. System();
  288. struct Impl;
  289. std::unique_ptr<Impl> impl;
  290. static System s_instance;
  291. };
  292. } // namespace Core