1 #ifndef DYNAMICCLOCK_HH
2 #define DYNAMICCLOCK_HH
37 [[nodiscard]] EmuTime::param
getTime()
const {
44 [[nodiscard]]
bool before(EmuTime::param e)
const {
45 return lastTick.time < e.time;
52 assert(e.time >= lastTick.time);
53 return divmod.
div(e.time - lastTick.time);
68 assert(e.time >= lastTick.time);
69 auto dur = e.time - lastTick.time;
70 auto [q, r] = divmod.
divMod(dur);
71 auto f = float(r) / float(getStep());
72 assert(0.0f <= f); assert(f < 1.0f);
76 template<
typename FIXED>
78 assert(e.time >= lastTick.time);
79 uint64_t tmp = (e.time - lastTick.time) << FIXED::FRACTION_BITS;
88 assert(e.time >= lastTick.time);
89 return divmod.
div(e.time - lastTick.time + (getStep() - 1));
93 assert(e.time >= lastTick.time);
94 return double(e.time - lastTick.time) / getStep();
100 return lastTick.time / getStep();
109 unsigned newStep = (
MAIN_FREQ32 + (freq / 2)) / freq;
117 void setFreq(
unsigned freq_num,
unsigned freq_denom) {
118 static_assert(
MAIN_FREQ < (1ull << 32),
"must fit in 32 bit");
119 uint64_t p =
MAIN_FREQ * freq_denom + (freq_num / 2);
120 uint64_t newStep = p / freq_num;
128 auto step = getStep();
140 assert(period.
length() < (1ull << 32));
147 lastTick.time = e.time;
155 assert(lastTick.time <= e.time);
156 lastTick.time = e.time - divmod.
mod(e.time - lastTick.time);
162 lastTick.time += n * getStep();
169 return EmuTime(lastTick.time + n * getStep());
181 assert((uint64_t(n) * getStep()) < (1ull << 32));
183 lastTick.time += n * getStep();
186 return add(lastTick, n);
188 [[nodiscard]] EmuTime
add(EmuTime::param time,
unsigned n)
const {
190 assert((uint64_t(n) * getStep()) < (1ull << 32));
192 return EmuTime(time.time + n * getStep());
195 template<
typename Archive>
196 void serialize(Archive& ar,
unsigned version);
205 [[nodiscard]]
unsigned getStep()
const {
return divmod.
getDivisor(); }
uint32_t div(uint64_t dividend) const
uint32_t mod(uint64_t dividend) const
std::pair< uint32_t, uint32_t > divMod(uint64_t dividend) const
uint32_t getDivisor() const
void setDivisor(uint32_t divisor)
Represents a clock with a variable frequency.
bool before(EmuTime::param e) const
Checks whether this clock's last tick is or is not before the given time stamp.
EmuTime getFastAdd(unsigned n) const
void serialize(Archive &ar, unsigned version)
uint64_t getTotalTicks() const
unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
EmuDuration getPeriod() const
Returns the length of one clock-cycle.
DynamicClock(EmuTime::param time, unsigned freq)
Create a new clock, which starts ticking at given time with given frequency.
void getTicksTill(EmuTime::param e, FIXED &result) const
void operator+=(uint64_t n)
Advance this clock by the given number of ticks.
void setFreq(unsigned freq_num, unsigned freq_denom)
Equivalent to setFreq(freq_num / freq_denom), but possibly with less rounding errors.
void setPeriod(EmuDuration period)
Set the duration of a clock tick.
double getTicksTillDouble(EmuTime::param e) const
IntegralFractional getTicksTillAsIntFloat(EmuTime::param e) const
unsigned getFreq() const
Returns the frequency (in Hz) at which this clock ticks.
DynamicClock(EmuTime::param time)
Create a new clock, which starts ticking at given time.
unsigned getTicksTillUp(EmuTime::param e) const
Calculate the number of ticks this clock has to tick to reach or go past the given time.
EmuTime add(EmuTime::param time, unsigned n) const
void reset(EmuTime::param e)
Reset the clock to start ticking at the given time.
EmuTime operator+(uint64_t n) const
Calculate the time at which this clock will have ticked the given number of times (counted from its l...
void advance(EmuTime::param e)
Advance this clock in time until the last tick which is not past the given time.
void fastAdd(unsigned n)
Advance this clock by the given number of ticks.
void setFreq(unsigned freq)
Change the frequency at which this clock ticks.
EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
constexpr uint64_t length() const
std::unique_ptr< IDEDevice > create(const DeviceConfig &config)
This file implemented 3 utility functions:
constexpr unsigned MAIN_FREQ32
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
constexpr uint64_t MAIN_FREQ
Like getTicksTill(), but also returns the fractional part (in range [0, 1)).