| 1 |
#ifndef CPUCLOCK_HH |
| 2 |
#define CPUCLOCK_HH |
| 3 |
|
| 4 |
#include "DynamicClock.hh" |
| 5 |
#include "Scheduler.hh" |
| 6 |
#include "narrow.hh" |
| 7 |
#include <cassert> |
| 8 |
|
| 9 |
namespace openmsx { |
| 10 |
|
| 11 |
class CPUClock |
| 12 |
{ |
| 13 |
public: |
| 14 |
unsigned getFreq() const { return clock.getFreq(); } |
| 15 |
|
| 16 |
protected: |
| 17 |
CPUClock(EmuTime time, Scheduler& scheduler); |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#if 0 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#else |
| 26 |
|
| 27 |
|
| 28 |
void add(unsigned ticks) { remaining -= narrow_cast<int>(ticks); } |
| 29 |
void sync() const { |
| 30 |
clock.fastAdd(limit - remaining); |
| 31 |
limit = remaining; |
| 32 |
} |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
[[nodiscard]] EmuTime getTime() const { sync(); return clock.getTime(); } |
| 37 |
[[nodiscard]] EmuTime getTimeFast() const { return clock.getFastAdd(limit - remaining); } |
| 38 |
[[nodiscard]] EmuTime getTimeFast(int cc) const { |
| 39 |
return clock.getFastAdd(limit - remaining + cc); |
| 40 |
} |
| 41 |
void setTime(EmuTime time) { sync(); clock.reset(time); } |
| 42 |
void setFreq(unsigned freq) { sync(); disableLimit(); clock.setFreq(freq); } |
| 43 |
void advanceTime(EmuTime time); |
| 44 |
[[nodiscard]] EmuTime calcTime(EmuTime time, unsigned ticks) const { |
| 45 |
return clock.add(time, ticks); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
unsigned advanceHalt(unsigned hltStates, EmuTime time) { |
| 54 |
sync(); |
| 55 |
unsigned ticks = clock.getTicksTillUp(time); |
| 56 |
unsigned halts = (ticks + hltStates - 1) / hltStates; |
| 57 |
clock += halts * hltStates; |
| 58 |
return halts; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
void waitForEvenCycle(int cc) |
| 66 |
{ |
| 67 |
sync(); |
| 68 |
auto totalTicks = clock.getTotalTicks() + cc; |
| 69 |
if (totalTicks & 1) { |
| 70 |
add(1); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void setLimit(EmuTime time) { |
| 94 |
if (limitEnabled) { |
| 95 |
sync(); |
| 96 |
assert(remaining == limit); |
| 97 |
limit = narrow_cast<int>(clock.getTicksTillUp(time) - 1); |
| 98 |
remaining = limit; |
| 99 |
} else { |
| 100 |
assert(limit < 0); |
| 101 |
} |
| 102 |
} |
| 103 |
void enableLimit() { |
| 104 |
limitEnabled = true; |
| 105 |
setLimit(scheduler.getNext()); |
| 106 |
} |
| 107 |
void disableLimit() { |
| 108 |
limitEnabled = false; |
| 109 |
int extra = limit - remaining; |
| 110 |
limit = -1; |
| 111 |
remaining = limit - extra; |
| 112 |
} |
| 113 |
[[nodiscard]] bool limitReached() const { |
| 114 |
return remaining < 0; |
| 115 |
} |
| 116 |
|
| 117 |
template<typename Archive> |
| 118 |
void serialize(Archive& ar, unsigned version); |
| 119 |
|
| 120 |
private: |
| 121 |
mutable DynamicClock clock; |
| 122 |
Scheduler& scheduler; |
| 123 |
int remaining = -1; |
| 124 |
mutable int limit = -1; |
| 125 |
bool limitEnabled = false; |
| 126 |
}; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
#endif |