core_cpu.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <condition_variable>
  5. #include <mutex>
  6. #include "common/logging/log.h"
  7. #ifdef ARCHITECTURE_x86_64
  8. #include "core/arm/dynarmic/arm_dynarmic.h"
  9. #endif
  10. #include "core/arm/exclusive_monitor.h"
  11. #include "core/arm/unicorn/arm_unicorn.h"
  12. #include "core/core.h"
  13. #include "core/core_cpu.h"
  14. #include "core/core_timing.h"
  15. #include "core/hle/kernel/kernel.h"
  16. #include "core/hle/kernel/physical_core.h"
  17. #include "core/hle/kernel/scheduler.h"
  18. #include "core/hle/kernel/thread.h"
  19. #include "core/hle/lock.h"
  20. #include "core/settings.h"
  21. namespace Core {
  22. Cpu::Cpu(System& system, std::size_t core_index)
  23. : global_scheduler{system.GlobalScheduler()},
  24. physical_core{system.Kernel().PhysicalCore(core_index)}, core_timing{system.CoreTiming()},
  25. core_index{core_index} {
  26. }
  27. Cpu::~Cpu() = default;
  28. void Cpu::RunLoop(bool tight_loop) {
  29. Reschedule();
  30. // If we don't have a currently active thread then don't execute instructions,
  31. // instead advance to the next event and try to yield to the next thread
  32. if (Kernel::GetCurrentThread() == nullptr) {
  33. LOG_TRACE(Core, "Core-{} idling", core_index);
  34. core_timing.Idle();
  35. } else {
  36. if (tight_loop) {
  37. physical_core.Run();
  38. } else {
  39. physical_core.Step();
  40. }
  41. }
  42. core_timing.Advance();
  43. Reschedule();
  44. }
  45. void Cpu::SingleStep() {
  46. return RunLoop(false);
  47. }
  48. void Cpu::PrepareReschedule() {
  49. physical_core.Stop();
  50. }
  51. void Cpu::Reschedule() {
  52. // Lock the global kernel mutex when we manipulate the HLE state
  53. std::lock_guard lock(HLE::g_hle_lock);
  54. global_scheduler.SelectThread(core_index);
  55. physical_core.Scheduler().TryDoContextSwitch();
  56. }
  57. } // namespace Core