summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.cpp2
-rw-r--r--src/core/arm/unicorn/arm_unicorn.cpp2
-rw-r--r--src/core/core_cpu.cpp14
-rw-r--r--src/core/core_timing.cpp66
-rw-r--r--src/core/core_timing.h24
-rw-r--r--src/core/cpu_core_manager.cpp19
6 files changed, 89 insertions, 38 deletions
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index f1506b372..4d2e99ed0 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -116,7 +116,7 @@ public:
116 num_interpreted_instructions = 0; 116 num_interpreted_instructions = 0;
117 } 117 }
118 u64 GetTicksRemaining() override { 118 u64 GetTicksRemaining() override {
119 return std::max(parent.system.CoreTiming().GetDowncount(), 0); 119 return std::max<s64>(parent.system.CoreTiming().GetDowncount(), 0LL);
120 } 120 }
121 u64 GetCNTPCT() override { 121 u64 GetCNTPCT() override {
122 return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks()); 122 return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks());
diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp
index 97d5c2a8a..3f91b06d4 100644
--- a/src/core/arm/unicorn/arm_unicorn.cpp
+++ b/src/core/arm/unicorn/arm_unicorn.cpp
@@ -156,7 +156,7 @@ void ARM_Unicorn::Run() {
156 if (GDBStub::IsServerEnabled()) { 156 if (GDBStub::IsServerEnabled()) {
157 ExecuteInstructions(std::max(4000000, 0)); 157 ExecuteInstructions(std::max(4000000, 0));
158 } else { 158 } else {
159 ExecuteInstructions(std::max(system.CoreTiming().GetDowncount(), 0)); 159 ExecuteInstructions(std::max<s64>(system.CoreTiming().GetDowncount(), 0LL));
160 } 160 }
161} 161}
162 162
diff --git a/src/core/core_cpu.cpp b/src/core/core_cpu.cpp
index 21c410e34..6bd9639c6 100644
--- a/src/core/core_cpu.cpp
+++ b/src/core/core_cpu.cpp
@@ -85,24 +85,16 @@ void Cpu::RunLoop(bool tight_loop) {
85 // instead advance to the next event and try to yield to the next thread 85 // instead advance to the next event and try to yield to the next thread
86 if (Kernel::GetCurrentThread() == nullptr) { 86 if (Kernel::GetCurrentThread() == nullptr) {
87 LOG_TRACE(Core, "Core-{} idling", core_index); 87 LOG_TRACE(Core, "Core-{} idling", core_index);
88 88 core_timing.Idle();
89 if (IsMainCore()) { 89 core_timing.Advance();
90 // TODO(Subv): Only let CoreTiming idle if all 4 cores are idling.
91 core_timing.Idle();
92 core_timing.Advance();
93 }
94
95 PrepareReschedule(); 90 PrepareReschedule();
96 } else { 91 } else {
97 if (IsMainCore()) {
98 core_timing.Advance();
99 }
100
101 if (tight_loop) { 92 if (tight_loop) {
102 arm_interface->Run(); 93 arm_interface->Run();
103 } else { 94 } else {
104 arm_interface->Step(); 95 arm_interface->Step();
105 } 96 }
97 core_timing.Advance();
106 } 98 }
107 99
108 Reschedule(); 100 Reschedule();
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index a58f7b131..6da2dcfb4 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -15,7 +15,7 @@
15 15
16namespace Core::Timing { 16namespace Core::Timing {
17 17
18constexpr int MAX_SLICE_LENGTH = 20000; 18constexpr int MAX_SLICE_LENGTH = 10000;
19 19
20struct CoreTiming::Event { 20struct CoreTiming::Event {
21 s64 time; 21 s64 time;
@@ -38,10 +38,14 @@ CoreTiming::CoreTiming() = default;
38CoreTiming::~CoreTiming() = default; 38CoreTiming::~CoreTiming() = default;
39 39
40void CoreTiming::Initialize() { 40void CoreTiming::Initialize() {
41 downcount = MAX_SLICE_LENGTH; 41 for (std::size_t core = 0; core < num_cpu_cores; core++) {
42 downcounts[core] = MAX_SLICE_LENGTH;
43 time_slice[core] = MAX_SLICE_LENGTH;
44 }
42 slice_length = MAX_SLICE_LENGTH; 45 slice_length = MAX_SLICE_LENGTH;
43 global_timer = 0; 46 global_timer = 0;
44 idled_cycles = 0; 47 idled_cycles = 0;
48 current_context = 0;
45 49
46 // The time between CoreTiming being initialized and the first call to Advance() is considered 50 // The time between CoreTiming being initialized and the first call to Advance() is considered
47 // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before 51 // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
@@ -110,7 +114,7 @@ void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
110u64 CoreTiming::GetTicks() const { 114u64 CoreTiming::GetTicks() const {
111 u64 ticks = static_cast<u64>(global_timer); 115 u64 ticks = static_cast<u64>(global_timer);
112 if (!is_global_timer_sane) { 116 if (!is_global_timer_sane) {
113 ticks += slice_length - downcount; 117 ticks += time_slice[current_context] - downcounts[current_context];
114 } 118 }
115 return ticks; 119 return ticks;
116} 120}
@@ -120,7 +124,7 @@ u64 CoreTiming::GetIdleTicks() const {
120} 124}
121 125
122void CoreTiming::AddTicks(u64 ticks) { 126void CoreTiming::AddTicks(u64 ticks) {
123 downcount -= static_cast<int>(ticks); 127 downcounts[current_context] -= static_cast<s64>(ticks);
124} 128}
125 129
126void CoreTiming::ClearPendingEvents() { 130void CoreTiming::ClearPendingEvents() {
@@ -141,22 +145,36 @@ void CoreTiming::RemoveEvent(const EventType* event_type) {
141 145
142void CoreTiming::ForceExceptionCheck(s64 cycles) { 146void CoreTiming::ForceExceptionCheck(s64 cycles) {
143 cycles = std::max<s64>(0, cycles); 147 cycles = std::max<s64>(0, cycles);
144 if (downcount <= cycles) { 148 if (downcounts[current_context] <= cycles) {
145 return; 149 return;
146 } 150 }
147 151
148 // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int 152 // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
149 // here. Account for cycles already executed by adjusting the g.slice_length 153 // here. Account for cycles already executed by adjusting the g.slice_length
150 slice_length -= downcount - static_cast<int>(cycles); 154 slice_length -= downcounts[current_context] - static_cast<int>(cycles);
151 downcount = static_cast<int>(cycles); 155 downcounts[current_context] = static_cast<int>(cycles);
156}
157
158std::optional<u64> CoreTiming::NextAvailableCore(const s64 needed_ticks) const {
159 const u64 original_context = current_context;
160 u64 next_context = (original_context + 1) % num_cpu_cores;
161 while (next_context != original_context) {
162 if (time_slice[next_context] >= needed_ticks) {
163 return {next_context};
164 } else if (time_slice[next_context] >= 0) {
165 return {};
166 }
167 next_context = (next_context + 1) % num_cpu_cores;
168 }
169 return {};
152} 170}
153 171
154void CoreTiming::Advance() { 172void CoreTiming::Advance() {
155 std::unique_lock<std::mutex> guard(inner_mutex); 173 std::unique_lock<std::mutex> guard(inner_mutex);
156 174
157 const int cycles_executed = slice_length - downcount; 175 const int cycles_executed = time_slice[current_context] - downcounts[current_context];
176 time_slice[current_context] = std::max<s64>(0, downcounts[current_context]);
158 global_timer += cycles_executed; 177 global_timer += cycles_executed;
159 slice_length = MAX_SLICE_LENGTH;
160 178
161 is_global_timer_sane = true; 179 is_global_timer_sane = true;
162 180
@@ -173,24 +191,40 @@ void CoreTiming::Advance() {
173 191
174 // Still events left (scheduled in the future) 192 // Still events left (scheduled in the future)
175 if (!event_queue.empty()) { 193 if (!event_queue.empty()) {
176 slice_length = static_cast<int>( 194 s64 needed_ticks = std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
177 std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH)); 195 const auto next_core = NextAvailableCore(needed_ticks);
196 if (next_core) {
197 downcounts[*next_core] = needed_ticks;
198 }
178 } 199 }
179 200
180 downcount = slice_length; 201 downcounts[current_context] = time_slice[current_context];
202}
203
204void CoreTiming::ResetRun() {
205 for (std::size_t core = 0; core < num_cpu_cores; core++) {
206 downcounts[core] = MAX_SLICE_LENGTH;
207 time_slice[core] = MAX_SLICE_LENGTH;
208 }
209 current_context = 0;
210 // Still events left (scheduled in the future)
211 if (!event_queue.empty()) {
212 s64 needed_ticks = std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
213 downcounts[current_context] = needed_ticks;
214 }
181} 215}
182 216
183void CoreTiming::Idle() { 217void CoreTiming::Idle() {
184 idled_cycles += downcount; 218 idled_cycles += downcounts[current_context];
185 downcount = 0; 219 downcounts[current_context] = 0;
186} 220}
187 221
188std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { 222std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
189 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE}; 223 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
190} 224}
191 225
192int CoreTiming::GetDowncount() const { 226s64 CoreTiming::GetDowncount() const {
193 return downcount; 227 return downcounts[current_context];
194} 228}
195 229
196} // namespace Core::Timing 230} // namespace Core::Timing
diff --git a/src/core/core_timing.h b/src/core/core_timing.h
index 161c7007d..ec0a6d2c0 100644
--- a/src/core/core_timing.h
+++ b/src/core/core_timing.h
@@ -7,6 +7,7 @@
7#include <chrono> 7#include <chrono>
8#include <functional> 8#include <functional>
9#include <mutex> 9#include <mutex>
10#include <optional>
10#include <string> 11#include <string>
11#include <unordered_map> 12#include <unordered_map>
12#include <vector> 13#include <vector>
@@ -104,7 +105,19 @@ public:
104 105
105 std::chrono::microseconds GetGlobalTimeUs() const; 106 std::chrono::microseconds GetGlobalTimeUs() const;
106 107
107 int GetDowncount() const; 108 void ResetRun();
109
110 s64 GetDowncount() const;
111
112 void SwitchContext(u64 new_context) {
113 current_context = new_context;
114 }
115
116 bool CurrentContextCanRun() const {
117 return time_slice[current_context] > 0;
118 }
119
120 std::optional<u64> NextAvailableCore(const s64 needed_ticks) const;
108 121
109private: 122private:
110 struct Event; 123 struct Event;
@@ -112,10 +125,15 @@ private:
112 /// Clear all pending events. This should ONLY be done on exit. 125 /// Clear all pending events. This should ONLY be done on exit.
113 void ClearPendingEvents(); 126 void ClearPendingEvents();
114 127
128 static constexpr u64 num_cpu_cores = 4;
129
115 s64 global_timer = 0; 130 s64 global_timer = 0;
116 s64 idled_cycles = 0; 131 s64 idled_cycles = 0;
117 int slice_length = 0; 132 s64 slice_length = 0;
118 int downcount = 0; 133 std::array<s64, num_cpu_cores> downcounts{};
134 // Slice of time assigned to each core per run.
135 std::array<s64, num_cpu_cores> time_slice{};
136 u64 current_context = 0;
119 137
120 // Are we in a function that has been called from Advance() 138 // Are we in a function that has been called from Advance()
121 // If events are scheduled from a function that gets called from Advance(), 139 // If events are scheduled from a function that gets called from Advance(),
diff --git a/src/core/cpu_core_manager.cpp b/src/core/cpu_core_manager.cpp
index 8fcb4eeb1..e022e6a60 100644
--- a/src/core/cpu_core_manager.cpp
+++ b/src/core/cpu_core_manager.cpp
@@ -6,6 +6,7 @@
6#include "core/arm/exclusive_monitor.h" 6#include "core/arm/exclusive_monitor.h"
7#include "core/core.h" 7#include "core/core.h"
8#include "core/core_cpu.h" 8#include "core/core_cpu.h"
9#include "core/core_timing.h"
9#include "core/cpu_core_manager.h" 10#include "core/cpu_core_manager.h"
10#include "core/gdbstub/gdbstub.h" 11#include "core/gdbstub/gdbstub.h"
11#include "core/settings.h" 12#include "core/settings.h"
@@ -122,13 +123,19 @@ void CpuCoreManager::RunLoop(bool tight_loop) {
122 } 123 }
123 } 124 }
124 125
125 for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) { 126 auto& core_timing = system.CoreTiming();
126 cores[active_core]->RunLoop(tight_loop); 127 core_timing.ResetRun();
127 if (Settings::values.use_multi_core) { 128 bool keep_running{};
128 // Cores 1-3 are run on other threads in this mode 129 do {
129 break; 130 keep_running = false;
131 for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) {
132 core_timing.SwitchContext(active_core);
133 if (core_timing.CurrentContextCanRun()) {
134 cores[active_core]->RunLoop(tight_loop);
135 }
136 keep_running |= core_timing.CurrentContextCanRun();
130 } 137 }
131 } 138 } while (keep_running);
132 139
133 if (GDBStub::IsServerEnabled()) { 140 if (GDBStub::IsServerEnabled()) {
134 GDBStub::SetCpuStepFlag(false); 141 GDBStub::SetCpuStepFlag(false);