core.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/memory.h"
  9. #include "core/perf_stats.h"
  10. #include "core/telemetry_session.h"
  11. class EmuWindow;
  12. class ARM_Interface;
  13. namespace Loader {
  14. class AppLoader;
  15. }
  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 whethor or not the operation succeeded.
  50. */
  51. ResultStatus RunLoop(int tight_loop = 1000);
  52. /**
  53. * Step the CPU one instruction
  54. * @return Result status, indicating whethor 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. PerfStats perf_stats;
  92. FrameLimiter frame_limiter;
  93. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  94. status = new_status;
  95. if (details) {
  96. status_details = details;
  97. }
  98. }
  99. const std::string& GetStatusDetails() const {
  100. return status_details;
  101. }
  102. private:
  103. /**
  104. * Initialize the emulated system.
  105. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  106. * @param system_mode The system mode.
  107. * @return ResultStatus code, indicating if the operation succeeded.
  108. */
  109. ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
  110. /// Reschedule the core emulation
  111. void Reschedule();
  112. /// AppLoader used to load the current executing application
  113. std::unique_ptr<Loader::AppLoader> app_loader;
  114. ///< ARM11 CPU core
  115. std::unique_ptr<ARM_Interface> cpu_core;
  116. /// When true, signals that a reschedule should happen
  117. bool reschedule_pending{};
  118. /// Telemetry session for this emulation session
  119. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  120. static System s_instance;
  121. ResultStatus status = ResultStatus::Success;
  122. std::string status_details = "";
  123. };
  124. inline ARM_Interface& CPU() {
  125. return System::GetInstance().CPU();
  126. }
  127. inline TelemetrySession& Telemetry() {
  128. return System::GetInstance().TelemetrySession();
  129. }
  130. } // namespace Core