17template<
unsigned FREQ_NUM,
unsigned FREQ_DENOM = 1>
23 static_assert(
MAIN_FREQ < (1ULL << 32),
"must fit in 32 bit");
24 static constexpr uint64_t P =
MAIN_FREQ * FREQ_DENOM + (FREQ_NUM / 2);
25 static constexpr uint64_t MASTER_TICKS = P / FREQ_NUM;
26 static_assert(MASTER_TICKS < (1ULL << 32),
"must fit in 32 bit");
27 static constexpr unsigned MASTER_TICKS32 = MASTER_TICKS;
41 constexpr explicit Clock(EmuTime::param e)
46 [[nodiscard]]
constexpr EmuTime::param
getTime()
const {
return lastTick; }
51 [[nodiscard]]
constexpr bool before(EmuTime::param e)
const {
52 return lastTick.time < e.time;
58 [[nodiscard]]
constexpr unsigned getTicksTill(EmuTime::param e)
const {
59 assert(e.time >= lastTick.time);
60 uint64_t result = (e.time - lastTick.time) / MASTER_TICKS;
63 assert(result ==
unsigned(result));
65 return unsigned(result);
71 assert(e.time >= lastTick.time);
73 return dm.
div(e.time - lastTick.time);
80 assert(e.time >= lastTick.time);
81 return (e.time - lastTick.time + MASTER_TICKS - 1) / MASTER_TICKS;
88 [[nodiscard]]
constexpr EmuTime
operator+(uint64_t n)
const {
89 return EmuTime(lastTick.time + n * MASTER_TICKS);
94 [[nodiscard]]
constexpr EmuTime
getFastAdd(
unsigned n)
const {
96 assert((uint64_t(n) * MASTER_TICKS) < (1ULL << 32));
98 return EmuTime(lastTick.time + n * MASTER_TICKS);
103 constexpr void reset(EmuTime::param e) {
104 lastTick.time = e.time;
112 assert(lastTick.time <= e.time);
113 lastTick.time = e.time - ((e.time - lastTick.time) % MASTER_TICKS);
119 assert(lastTick.time <= e.time);
121 lastTick.time = e.time - dm.
mod(e.time - lastTick.time);
127 lastTick.time += n * MASTER_TICKS;
139 assert((n * MASTER_TICKS) < (1ULL << 32));
141 lastTick.time += n * MASTER_TICKS32;
144 template<
typename Archive>
147 ar.serialize(
"lastTick", lastTick);
156template<
unsigned FREQ_NUM,
unsigned FREQ_DENOM>
Represents a clock with a fixed frequency.
constexpr uint64_t getTicksTillUp(EmuTime::param e) const
Calculate the number of ticks this clock has to tick to reach or go past the given time.
constexpr 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...
constexpr Clock(EmuTime::param e)
Create a new clock, which starts ticking at the given time.
constexpr void advance_fast(EmuTime::param e)
Same as above, only faster, Though the time interval may not be too large.
constexpr void reset(EmuTime::param e)
Reset the clock to start ticking at the given time.
constexpr void operator+=(unsigned n)
Advance this clock by the given number of ticks.
static constexpr EmuDuration duration(unsigned ticks)
Calculates the duration of the given number of ticks at this clock's frequency.
void serialize(Archive &ar, unsigned)
constexpr EmuTime getFastAdd(unsigned n) const
Like operator+() but faster, though the step can't be too big (max a little over 1 second).
constexpr void fastAdd(unsigned n)
Advance this clock by the given number of ticks.
constexpr unsigned getTicksTill_fast(EmuTime::param e) const
Same as above, only faster, Though the time interval may not be too large.
constexpr bool before(EmuTime::param e) const
Checks whether this clock's last tick is or is not before the given time stamp.
constexpr EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
constexpr void advance(EmuTime::param e)
Advance this clock in time until the last tick which is not past the given time.
constexpr unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
This file implemented 3 utility functions:
constexpr uint64_t MAIN_FREQ
constexpr uint32_t div(uint64_t dividend) const
constexpr uint32_t mod(uint64_t dividend) const