core.h 13 KB

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