core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "core/file_sys/vfs_types.h"
  11. #include "core/hle/kernel/object.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 GlobalScheduler;
  23. class KernelCore;
  24. class PhysicalCore;
  25. class Process;
  26. class Scheduler;
  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 LM {
  51. class Manager;
  52. } // namespace LM
  53. namespace SM {
  54. class ServiceManager;
  55. } // namespace SM
  56. namespace Time {
  57. class TimeManager;
  58. } // namespace Time
  59. } // namespace Service
  60. namespace Tegra {
  61. class DebugContext;
  62. class GPU;
  63. } // namespace Tegra
  64. namespace VideoCore {
  65. class RendererBase;
  66. } // namespace VideoCore
  67. namespace Core::Timing {
  68. class CoreTiming;
  69. }
  70. namespace Core::Hardware {
  71. class InterruptManager;
  72. }
  73. namespace Core {
  74. class ARM_Interface;
  75. class CpuManager;
  76. class DeviceMemory;
  77. class ExclusiveMonitor;
  78. class FrameLimiter;
  79. class PerfStats;
  80. class Reporter;
  81. class TelemetrySession;
  82. struct PerfStatsResults;
  83. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  84. const std::string& path);
  85. class System {
  86. public:
  87. using CurrentBuildProcessID = std::array<u8, 0x20>;
  88. System(const System&) = delete;
  89. System& operator=(const System&) = delete;
  90. System(System&&) = delete;
  91. System& operator=(System&&) = delete;
  92. ~System();
  93. /**
  94. * Gets the instance of the System singleton class.
  95. * @returns Reference to the instance of the System singleton class.
  96. */
  97. [[deprecated("Use of the global system instance is deprecated")]] static System& GetInstance() {
  98. return s_instance;
  99. }
  100. /// Enumeration representing the return values of the System Initialize and Load process.
  101. enum class ResultStatus : u32 {
  102. Success, ///< Succeeded
  103. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  104. ErrorGetLoader, ///< Error finding the correct application loader
  105. ErrorSystemFiles, ///< Error in finding system files
  106. ErrorSharedFont, ///< Error in finding shared font
  107. ErrorVideoCore, ///< Error in the video core
  108. ErrorUnknown, ///< Any other error
  109. ErrorLoader, ///< The base for loader errors (too many to repeat)
  110. };
  111. /**
  112. * Run the OS and Application
  113. * This function will start emulation and run the relevant devices
  114. */
  115. [[nodiscard]] ResultStatus Run();
  116. /**
  117. * Pause the OS and Application
  118. * This function will pause emulation and stop the relevant devices
  119. */
  120. [[nodiscard]] ResultStatus Pause();
  121. /**
  122. * Step the CPU one instruction
  123. * @return Result status, indicating whether or not the operation succeeded.
  124. */
  125. [[nodiscard]] ResultStatus SingleStep();
  126. /**
  127. * Invalidate the CPU instruction caches
  128. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  129. * step/continue commands.
  130. */
  131. void InvalidateCpuInstructionCaches();
  132. /// Shutdown the emulated system.
  133. void Shutdown();
  134. /**
  135. * Load an executable application.
  136. * @param emu_window Reference to the host-system window used for video output and keyboard
  137. * input.
  138. * @param filepath String path to the executable application to load on the host file system.
  139. * @returns ResultStatus code, indicating if the operation succeeded.
  140. */
  141. [[nodiscard]] ResultStatus Load(Frontend::EmuWindow& emu_window, const std::string& filepath);
  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 scheduler for the CPU core that is currently running
  165. [[nodiscard]] Kernel::Scheduler& CurrentScheduler();
  166. /// Gets the scheduler for the CPU core that is currently running
  167. [[nodiscard]] const Kernel::Scheduler& CurrentScheduler() 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 scheduler for the CPU core with the specified index
  197. [[nodiscard]] Kernel::Scheduler& Scheduler(std::size_t core_index);
  198. /// Gets the scheduler for the CPU core with the specified index
  199. [[nodiscard]] const Kernel::Scheduler& Scheduler(std::size_t core_index) const;
  200. /// Gets the global scheduler
  201. [[nodiscard]] Kernel::GlobalScheduler& GlobalScheduler();
  202. /// Gets the global scheduler
  203. [[nodiscard]] const Kernel::GlobalScheduler& GlobalScheduler() const;
  204. /// Gets the manager for the guest device memory
  205. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  206. /// Gets the manager for the guest device memory
  207. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  208. /// Provides a pointer to the current process
  209. [[nodiscard]] Kernel::Process* CurrentProcess();
  210. /// Provides a constant pointer to the current process.
  211. [[nodiscard]] const Kernel::Process* CurrentProcess() const;
  212. /// Provides a reference to the core timing instance.
  213. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  214. /// Provides a constant reference to the core timing instance.
  215. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  216. /// Provides a reference to the interrupt manager instance.
  217. [[nodiscard]] Core::Hardware::InterruptManager& InterruptManager();
  218. /// Provides a constant reference to the interrupt manager instance.
  219. [[nodiscard]] const Core::Hardware::InterruptManager& InterruptManager() const;
  220. /// Provides a reference to the kernel instance.
  221. [[nodiscard]] Kernel::KernelCore& Kernel();
  222. /// Provides a constant reference to the kernel instance.
  223. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  224. /// Provides a reference to the internal PerfStats instance.
  225. [[nodiscard]] Core::PerfStats& GetPerfStats();
  226. /// Provides a constant reference to the internal PerfStats instance.
  227. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  228. /// Provides a reference to the frame limiter;
  229. [[nodiscard]] Core::FrameLimiter& FrameLimiter();
  230. /// Provides a constant referent to the frame limiter
  231. [[nodiscard]] const Core::FrameLimiter& FrameLimiter() const;
  232. /// Gets the name of the current game
  233. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  234. void SetStatus(ResultStatus new_status, const char* details);
  235. [[nodiscard]] const std::string& GetStatusDetails() const;
  236. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  237. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  238. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  239. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  240. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  241. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  242. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  243. const std::array<u8, 0x20>& build_id, VAddr main_region_begin,
  244. u64 main_region_size);
  245. void SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set);
  246. void SetDefaultAppletFrontendSet();
  247. [[nodiscard]] Service::AM::Applets::AppletManager& GetAppletManager();
  248. [[nodiscard]] const Service::AM::Applets::AppletManager& GetAppletManager() const;
  249. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  250. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  251. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  252. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  253. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  254. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  255. FileSys::ContentProvider* provider);
  256. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  257. [[nodiscard]] const Reporter& GetReporter() const;
  258. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  259. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  260. [[nodiscard]] Service::APM::Controller& GetAPMController();
  261. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  262. [[nodiscard]] Service::LM::Manager& GetLogManager();
  263. [[nodiscard]] const Service::LM::Manager& GetLogManager() const;
  264. [[nodiscard]] Service::Time::TimeManager& GetTimeManager();
  265. [[nodiscard]] const Service::Time::TimeManager& GetTimeManager() const;
  266. void SetExitLock(bool locked);
  267. [[nodiscard]] bool GetExitLock() const;
  268. void SetCurrentProcessBuildID(const CurrentBuildProcessID& id);
  269. [[nodiscard]] const CurrentBuildProcessID& GetCurrentProcessBuildID() const;
  270. /// Register a host thread as an emulated CPU Core.
  271. void RegisterCoreThread(std::size_t id);
  272. /// Register a host thread as an auxiliary thread.
  273. void RegisterHostThread();
  274. /// Enter Dynarmic Microprofile
  275. void EnterDynarmicProfile();
  276. /// Exit Dynarmic Microprofile
  277. void ExitDynarmicProfile();
  278. /// Tells if system is running on multicore.
  279. [[nodiscard]] bool IsMulticore() const;
  280. private:
  281. System();
  282. /**
  283. * Initialize the emulated system.
  284. * @param emu_window Reference to the host-system window used for video output and keyboard
  285. * input.
  286. * @return ResultStatus code, indicating if the operation succeeded.
  287. */
  288. [[nodiscard]] ResultStatus Init(Frontend::EmuWindow& emu_window);
  289. struct Impl;
  290. std::unique_ptr<Impl> impl;
  291. static System s_instance;
  292. };
  293. } // namespace Core