core_cpu.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/unicorn/arm_unicorn.h"
  11. #include "core/core_cpu.h"
  12. #include "core/core_timing.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/kernel/scheduler.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/settings.h"
  17. namespace Core {
  18. Cpu::Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index)
  19. : cpu_barrier{std::move(cpu_barrier)}, core_index{core_index} {
  20. if (Settings::values.use_cpu_jit) {
  21. #ifdef ARCHITECTURE_x86_64
  22. arm_interface = std::make_shared<ARM_Dynarmic>();
  23. #else
  24. cpu_core = std::make_shared<ARM_Unicorn>();
  25. NGLOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
  26. #endif
  27. } else {
  28. arm_interface = std::make_shared<ARM_Unicorn>();
  29. }
  30. scheduler = std::make_shared<Kernel::Scheduler>(arm_interface.get());
  31. }
  32. void Cpu::RunLoop(bool tight_loop) {
  33. // Wait for all other CPU cores to complete the previous slice, such that they run in lock-step
  34. cpu_barrier->Rendezvous();
  35. // If we don't have a currently active thread then don't execute instructions,
  36. // instead advance to the next event and try to yield to the next thread
  37. if (Kernel::GetCurrentThread() == nullptr) {
  38. NGLOG_TRACE(Core, "Core-{} idling", core_index);
  39. if (IsMainCore()) {
  40. CoreTiming::Idle();
  41. CoreTiming::Advance();
  42. }
  43. PrepareReschedule();
  44. } else {
  45. if (IsMainCore()) {
  46. CoreTiming::Advance();
  47. }
  48. if (tight_loop) {
  49. arm_interface->Run();
  50. } else {
  51. arm_interface->Step();
  52. }
  53. }
  54. Reschedule();
  55. }
  56. void Cpu::SingleStep() {
  57. return RunLoop(false);
  58. }
  59. void Cpu::PrepareReschedule() {
  60. arm_interface->PrepareReschedule();
  61. reschedule_pending = true;
  62. }
  63. void Cpu::Reschedule() {
  64. if (!reschedule_pending) {
  65. return;
  66. }
  67. reschedule_pending = false;
  68. scheduler->Reschedule();
  69. }
  70. } // namespace Core