core.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <memory>
  6. #include <string>
  7. #include "common/common_types.h"
  8. #include "core/hle/kernel/scheduler.h"
  9. #include "core/loader/loader.h"
  10. #include "core/memory.h"
  11. #include "core/perf_stats.h"
  12. #include "core/telemetry_session.h"
  13. #include "video_core/gpu.h"
  14. class EmuWindow;
  15. class ARM_Interface;
  16. namespace Core {
  17. class System {
  18. public:
  19. /**
  20. * Gets the instance of the System singleton class.
  21. * @returns Reference to the instance of the System singleton class.
  22. */
  23. static System& GetInstance() {
  24. return s_instance;
  25. }
  26. /// Enumeration representing the return values of the System Initialize and Load process.
  27. enum class ResultStatus : u32 {
  28. Success, ///< Succeeded
  29. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  30. ErrorGetLoader, ///< Error finding the correct application loader
  31. ErrorSystemMode, ///< Error determining the system mode
  32. ErrorLoader, ///< Error loading the specified application
  33. ErrorLoader_ErrorEncrypted, ///< Error loading the specified application due to encryption
  34. ErrorLoader_ErrorInvalidFormat, ///< Error loading the specified application due to an
  35. /// invalid format
  36. ErrorSystemFiles, ///< Error in finding system files
  37. ErrorSharedFont, ///< Error in finding shared font
  38. ErrorVideoCore, ///< Error in the video core
  39. ErrorUnknown ///< Any other error
  40. };
  41. /**
  42. * Run the core CPU loop
  43. * This function runs the core for the specified number of CPU instructions before trying to
  44. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  45. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  46. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  47. * update is requested (e.g. on a thread switch).
  48. * @param tight_loop Number of instructions to execute.
  49. * @return Result status, indicating whether or not the operation succeeded.
  50. */
  51. ResultStatus RunLoop(int tight_loop = 100000);
  52. /**
  53. * Step the CPU one instruction
  54. * @return Result status, indicating whether or not the operation succeeded.
  55. */
  56. ResultStatus SingleStep();
  57. /// Shutdown the emulated system.
  58. void Shutdown();
  59. /**
  60. * Load an executable application.
  61. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  62. * @param filepath String path to the executable application to load on the host file system.
  63. * @returns ResultStatus code, indicating if the operation succeeded.
  64. */
  65. ResultStatus Load(EmuWindow* emu_window, const std::string& filepath);
  66. /**
  67. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  68. * application).
  69. * @returns True if the emulated system is powered on, otherwise false.
  70. */
  71. bool IsPoweredOn() const {
  72. return cpu_core != nullptr;
  73. }
  74. /**
  75. * Returns a reference to the telemetry session for this emulation session.
  76. * @returns Reference to the telemetry session.
  77. */
  78. Core::TelemetrySession& TelemetrySession() const {
  79. return *telemetry_session;
  80. }
  81. /// Prepare the core emulation for a reschedule
  82. void PrepareReschedule();
  83. PerfStats::Results GetAndResetPerfStats();
  84. /**
  85. * Gets a reference to the emulated CPU.
  86. * @returns A reference to the emulated CPU.
  87. */
  88. ARM_Interface& CPU() {
  89. return *cpu_core;
  90. }
  91. Tegra::GPU& GPU() {
  92. return *gpu_core;
  93. }
  94. Kernel::Scheduler& Scheduler() {
  95. return *scheduler;
  96. }
  97. PerfStats perf_stats;
  98. FrameLimiter frame_limiter;
  99. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  100. status = new_status;
  101. if (details) {
  102. status_details = details;
  103. }
  104. }
  105. const std::string& GetStatusDetails() const {
  106. return status_details;
  107. }
  108. Loader::AppLoader& GetAppLoader() const {
  109. return *app_loader;
  110. }
  111. private:
  112. /**
  113. * Initialize the emulated system.
  114. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  115. * @param system_mode The system mode.
  116. * @return ResultStatus code, indicating if the operation succeeded.
  117. */
  118. ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
  119. /// Reschedule the core emulation
  120. void Reschedule();
  121. /// AppLoader used to load the current executing application
  122. std::unique_ptr<Loader::AppLoader> app_loader;
  123. std::shared_ptr<ARM_Interface> cpu_core;
  124. std::unique_ptr<Kernel::Scheduler> scheduler;
  125. std::unique_ptr<Tegra::GPU> gpu_core;
  126. /// When true, signals that a reschedule should happen
  127. bool reschedule_pending{};
  128. /// Telemetry session for this emulation session
  129. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  130. static System s_instance;
  131. ResultStatus status = ResultStatus::Success;
  132. std::string status_details = "";
  133. };
  134. inline ARM_Interface& CPU() {
  135. return System::GetInstance().CPU();
  136. }
  137. inline TelemetrySession& Telemetry() {
  138. return System::GetInstance().TelemetrySession();
  139. }
  140. } // namespace Core