yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
clock.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EMU_CLOCK_H_
2#define YAZE_APP_EMU_CLOCK_H_
3
4#include <cstdint>
5
6namespace yaze {
7namespace app {
8namespace emu {
9
10class Clock {
11 public:
12 virtual ~Clock() = default;
13 virtual void UpdateClock(double delta) = 0;
14 virtual unsigned long long GetCycleCount() const = 0;
15 virtual void ResetAccumulatedTime() = 0;
16 virtual void SetFrequency(float new_frequency) = 0;
17 virtual float GetFrequency() const = 0;
18};
19
20class ClockImpl : public Clock {
21 public:
22 ClockImpl() = default;
23 virtual ~ClockImpl() = default;
24
25 void UpdateCycleCount(double deltaTime) {
26 accumulatedTime += deltaTime;
27 double cycleTime = 1.0 / frequency;
28
29 while (accumulatedTime >= cycleTime) {
30 Cycle();
31 accumulatedTime -= cycleTime;
32 }
33 }
34
35 void Cycle() {
36 cycle++;
37 cycleCount++;
38 }
39
40 void UpdateClock(double delta) override {
41 UpdateCycleCount(delta);
43 }
44
45 void ResetAccumulatedTime() override { accumulatedTime = 0.0; }
46 unsigned long long GetCycleCount() const override { return cycleCount; }
47 float GetFrequency() const override { return frequency; }
48 void SetFrequency(float new_frequency) override {
49 this->frequency = new_frequency;
50 }
51
52 private:
53 uint64_t cycle = 0; // Current cycle
54 float frequency = 0.0; // Frequency of the clock in Hz
55 unsigned long long cycleCount = 0; // Total number of cycles executed
56 double accumulatedTime = 0.0; // Accumulated time since the last cycle update
57};
58
59} // namespace emu
60} // namespace app
61} // namespace yaze
62
63#endif // YAZE_APP_EMU_CLOCK_H_
float GetFrequency() const override
Definition clock.h:47
void UpdateCycleCount(double deltaTime)
Definition clock.h:25
void UpdateClock(double delta) override
Definition clock.h:40
void SetFrequency(float new_frequency) override
Definition clock.h:48
void ResetAccumulatedTime() override
Definition clock.h:45
unsigned long long GetCycleCount() const override
Definition clock.h:46
virtual ~ClockImpl()=default
unsigned long long cycleCount
Definition clock.h:55
virtual float GetFrequency() const =0
virtual ~Clock()=default
virtual void SetFrequency(float new_frequency)=0
virtual void UpdateClock(double delta)=0
virtual void ResetAccumulatedTime()=0
virtual unsigned long long GetCycleCount() const =0
Definition common.cc:22