Use CPU for counting ticks instead of scheduler

This commit is contained in:
wheremyfoodat 2024-01-22 00:38:11 +02:00
parent 7343497f36
commit 97f97dc6e2
3 changed files with 36 additions and 36 deletions

View file

@ -11,14 +11,16 @@
#include "memory.hpp" #include "memory.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
class Emulator;
class CPU; class CPU;
class MyEnvironment final : public Dynarmic::A32::UserCallbacks { class MyEnvironment final : public Dynarmic::A32::UserCallbacks {
public: public:
u64 ticksLeft = 0; u64 ticksLeft = 0;
u64 totalTicks = 0; u64 totalTicks = 0;
Memory& mem; Memory& mem;
Kernel& kernel; Kernel& kernel;
Scheduler& scheduler;
u64 getCyclesForInstruction(bool isThumb, u32 instruction); u64 getCyclesForInstruction(bool isThumb, u32 instruction);
@ -77,39 +79,39 @@ public:
std::terminate(); std::terminate();
} }
void CallSVC(u32 swi) override { void CallSVC(u32 swi) override {
kernel.serviceSVC(swi); kernel.serviceSVC(swi);
} }
void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override { void ExceptionRaised(u32 pc, Dynarmic::A32::Exception exception) override {
switch (exception) { switch (exception) {
case Dynarmic::A32::Exception::UnpredictableInstruction: case Dynarmic::A32::Exception::UnpredictableInstruction:
Helpers::panic("Unpredictable instruction at pc = %08X", pc); Helpers::panic("Unpredictable instruction at pc = %08X", pc);
break; break;
default: Helpers::panic("Fired exception oops"); default: Helpers::panic("Fired exception oops");
} }
} }
void AddTicks(u64 ticks) override { void AddTicks(u64 ticks) override {
totalTicks += ticks; totalTicks += ticks;
if (ticks > ticksLeft) { if (ticks > ticksLeft) {
ticksLeft = 0; ticksLeft = 0;
return; return;
} }
ticksLeft -= ticks; ticksLeft -= ticks;
} }
u64 GetTicksRemaining() override { u64 GetTicksRemaining() override {
return ticksLeft; return ticksLeft;
} }
u64 GetTicksForCode(bool isThumb, u32 vaddr, u32 instruction) override { u64 GetTicksForCode(bool isThumb, u32 vaddr, u32 instruction) override {
return getCyclesForInstruction(isThumb, instruction); return getCyclesForInstruction(isThumb, instruction);
} }
MyEnvironment(Memory& mem, Kernel& kernel) : mem(mem), kernel(kernel) {} MyEnvironment(Memory& mem, Kernel& kernel, Scheduler& scheduler) : mem(mem), kernel(kernel), scheduler(scheduler) {}
}; };
class CPU { class CPU {

View file

@ -18,12 +18,10 @@ struct Scheduler {
using EventMap = boost::container::flat_multimap<Key, Val, std::less<Key>, boost::container::static_vector<std::pair<Key, Val>, size>>; using EventMap = boost::container::flat_multimap<Key, Val, std::less<Key>, boost::container::static_vector<std::pair<Key, Val>, size>>;
EventMap<u64, EventType, totalNumberOfEvents> events; EventMap<u64, EventType, totalNumberOfEvents> events;
u64 currentTimestamp = 0;
u64 nextTimestamp = 0; u64 nextTimestamp = 0;
// Set nextTimestamp to the timestamp of the next event // Set nextTimestamp to the timestamp of the next event
void updateNextTimestamp() { nextTimestamp = events.cbegin()->first; } void updateNextTimestamp() { nextTimestamp = events.cbegin()->first; }
void addCycles(u64 cycles) { currentTimestamp += cycles; }
void addEvent(EventType type, u64 timestamp) { void addEvent(EventType type, u64 timestamp) {
events.emplace(timestamp, type); events.emplace(timestamp, type);
@ -40,8 +38,6 @@ struct Scheduler {
}; };
void reset() { void reset() {
currentTimestamp = 0;
// Clear any pending events // Clear any pending events
events.clear(); events.clear();
// Add a dummy event to always keep the scheduler non-empty // Add a dummy event to always keep the scheduler non-empty

View file

@ -3,7 +3,7 @@
#include "arm_defs.hpp" #include "arm_defs.hpp"
CPU::CPU(Memory& mem, Kernel& kernel) : mem(mem), env(mem, kernel) { CPU::CPU(Memory& mem, Kernel& kernel) : mem(mem), env(mem, kernel, scheduler) {
cp15 = std::make_shared<CP15>(); cp15 = std::make_shared<CP15>();
Dynarmic::A32::UserConfig config; Dynarmic::A32::UserConfig config;
@ -32,6 +32,8 @@ void CPU::reset() {
// Reset scheduler and add a VBlank event // Reset scheduler and add a VBlank event
scheduler.reset(); scheduler.reset();
scheduler.addEvent(Scheduler::EventType::VBlank, ticksPerSec / 60); scheduler.addEvent(Scheduler::EventType::VBlank, ticksPerSec / 60);
printf("%lld\n", scheduler.nextTimestamp);
} }
#endif // CPU_DYNARMIC #endif // CPU_DYNARMIC