core.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "common/common_types.h"
  9. #include "core/hle/kernel/object.h"
  10. namespace Core::Frontend {
  11. class EmuWindow;
  12. } // namespace Core::Frontend
  13. namespace FileSys {
  14. class VfsFilesystem;
  15. } // namespace FileSys
  16. namespace Kernel {
  17. class KernelCore;
  18. class Process;
  19. class Scheduler;
  20. } // namespace Kernel
  21. namespace Loader {
  22. class AppLoader;
  23. enum class ResultStatus : u16;
  24. } // namespace Loader
  25. namespace Service::SM {
  26. class ServiceManager;
  27. } // namespace Service::SM
  28. namespace Tegra {
  29. class DebugContext;
  30. class GPU;
  31. } // namespace Tegra
  32. namespace VideoCore {
  33. class RendererBase;
  34. } // namespace VideoCore
  35. namespace Core {
  36. class ARM_Interface;
  37. class Cpu;
  38. class ExclusiveMonitor;
  39. class FrameLimiter;
  40. class PerfStats;
  41. class TelemetrySession;
  42. struct PerfStatsResults;
  43. class System {
  44. public:
  45. System(const System&) = delete;
  46. System& operator=(const System&) = delete;
  47. System(System&&) = delete;
  48. System& operator=(System&&) = delete;
  49. ~System();
  50. /**
  51. * Gets the instance of the System singleton class.
  52. * @returns Reference to the instance of the System singleton class.
  53. */
  54. static System& GetInstance() {
  55. return s_instance;
  56. }
  57. /// Enumeration representing the return values of the System Initialize and Load process.
  58. enum class ResultStatus : u32 {
  59. Success, ///< Succeeded
  60. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  61. ErrorGetLoader, ///< Error finding the correct application loader
  62. ErrorSystemMode, ///< Error determining the system mode
  63. ErrorSystemFiles, ///< Error in finding system files
  64. ErrorSharedFont, ///< Error in finding shared font
  65. ErrorVideoCore, ///< Error in the video core
  66. ErrorUnknown, ///< Any other error
  67. ErrorLoader, ///< The base for loader errors (too many to repeat)
  68. };
  69. /**
  70. * Run the core CPU loop
  71. * This function runs the core for the specified number of CPU instructions before trying to
  72. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  73. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  74. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  75. * update is requested (e.g. on a thread switch).
  76. * @param tight_loop If false, the CPU single-steps.
  77. * @return Result status, indicating whether or not the operation succeeded.
  78. */
  79. ResultStatus RunLoop(bool tight_loop = true);
  80. /**
  81. * Step the CPU one instruction
  82. * @return Result status, indicating whether or not the operation succeeded.
  83. */
  84. ResultStatus SingleStep();
  85. /**
  86. * Invalidate the CPU instruction caches
  87. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  88. * step/continue commands.
  89. */
  90. void InvalidateCpuInstructionCaches();
  91. /// Shutdown the emulated system.
  92. void Shutdown();
  93. /**
  94. * Load an executable application.
  95. * @param emu_window Reference to the host-system window used for video output and keyboard
  96. * input.
  97. * @param filepath String path to the executable application to load on the host file system.
  98. * @returns ResultStatus code, indicating if the operation succeeded.
  99. */
  100. ResultStatus Load(Frontend::EmuWindow& emu_window, const std::string& filepath);
  101. /**
  102. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  103. * application).
  104. * @returns True if the emulated system is powered on, otherwise false.
  105. */
  106. bool IsPoweredOn() const;
  107. /**
  108. * Returns a reference to the telemetry session for this emulation session.
  109. * @returns Reference to the telemetry session.
  110. */
  111. Core::TelemetrySession& TelemetrySession() const;
  112. /// Prepare the core emulation for a reschedule
  113. void PrepareReschedule();
  114. /// Gets and resets core performance statistics
  115. PerfStatsResults GetAndResetPerfStats();
  116. /// Gets an ARM interface to the CPU core that is currently running
  117. ARM_Interface& CurrentArmInterface();
  118. /// Gets the index of the currently running CPU core
  119. std::size_t CurrentCoreIndex();
  120. /// Gets the scheduler for the CPU core that is currently running
  121. Kernel::Scheduler& CurrentScheduler();
  122. /// Gets an ARM interface to the CPU core with the specified index
  123. ARM_Interface& ArmInterface(std::size_t core_index);
  124. /// Gets a CPU interface to the CPU core with the specified index
  125. Cpu& CpuCore(std::size_t core_index);
  126. /// Gets the exclusive monitor
  127. ExclusiveMonitor& Monitor();
  128. /// Gets a mutable reference to the GPU interface
  129. Tegra::GPU& GPU();
  130. /// Gets an immutable reference to the GPU interface.
  131. const Tegra::GPU& GPU() const;
  132. /// Gets a mutable reference to the renderer.
  133. VideoCore::RendererBase& Renderer();
  134. /// Gets an immutable reference to the renderer.
  135. const VideoCore::RendererBase& Renderer() const;
  136. /// Gets the scheduler for the CPU core with the specified index
  137. const std::shared_ptr<Kernel::Scheduler>& Scheduler(std::size_t core_index);
  138. /// Provides a reference to the current process
  139. Kernel::SharedPtr<Kernel::Process>& CurrentProcess();
  140. /// Provides a constant reference to the current process.
  141. const Kernel::SharedPtr<Kernel::Process>& CurrentProcess() const;
  142. /// Provides a reference to the kernel instance.
  143. Kernel::KernelCore& Kernel();
  144. /// Provides a constant reference to the kernel instance.
  145. const Kernel::KernelCore& Kernel() const;
  146. /// Provides a reference to the internal PerfStats instance.
  147. Core::PerfStats& GetPerfStats();
  148. /// Provides a constant reference to the internal PerfStats instance.
  149. const Core::PerfStats& GetPerfStats() const;
  150. /// Provides a reference to the frame limiter;
  151. Core::FrameLimiter& FrameLimiter();
  152. /// Provides a constant referent to the frame limiter
  153. const Core::FrameLimiter& FrameLimiter() const;
  154. /// Gets the name of the current game
  155. Loader::ResultStatus GetGameName(std::string& out) const;
  156. void SetStatus(ResultStatus new_status, const char* details);
  157. const std::string& GetStatusDetails() const;
  158. Loader::AppLoader& GetAppLoader() const;
  159. Service::SM::ServiceManager& ServiceManager();
  160. const Service::SM::ServiceManager& ServiceManager() const;
  161. void SetGPUDebugContext(std::shared_ptr<Tegra::DebugContext> context);
  162. Tegra::DebugContext* GetGPUDebugContext() const;
  163. void SetFilesystem(std::shared_ptr<FileSys::VfsFilesystem> vfs);
  164. std::shared_ptr<FileSys::VfsFilesystem> GetFilesystem() const;
  165. private:
  166. System();
  167. /// Returns the currently running CPU core
  168. Cpu& CurrentCpuCore();
  169. /**
  170. * Initialize the emulated system.
  171. * @param emu_window Reference to the host-system window used for video output and keyboard
  172. * input.
  173. * @return ResultStatus code, indicating if the operation succeeded.
  174. */
  175. ResultStatus Init(Frontend::EmuWindow& emu_window);
  176. struct Impl;
  177. std::unique_ptr<Impl> impl;
  178. static System s_instance;
  179. };
  180. inline ARM_Interface& CurrentArmInterface() {
  181. return System::GetInstance().CurrentArmInterface();
  182. }
  183. inline TelemetrySession& Telemetry() {
  184. return System::GetInstance().TelemetrySession();
  185. }
  186. inline Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
  187. return System::GetInstance().CurrentProcess();
  188. }
  189. } // namespace Core