| 1 |
#ifndef CLOCK_HH |
| 2 |
#define CLOCK_HH |
| 3 |
|
| 4 |
#include "EmuDuration.hh" |
| 5 |
#include "EmuTime.hh" |
| 6 |
#include "serialize.hh" |
| 7 |
|
| 8 |
#include <cassert> |
| 9 |
|
| 10 |
namespace openmsx { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
template<unsigned FREQ_NUM, unsigned FREQ_DENOM = 1> |
| 18 |
class Clock |
| 19 |
{ |
| 20 |
private: |
| 21 |
|
| 22 |
|
| 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; |
| 28 |
|
| 29 |
public: |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
[[nodiscard]] static constexpr EmuDuration duration(unsigned ticks) { |
| 36 |
return EmuDuration(ticks * MASTER_TICKS); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
constexpr explicit Clock(EmuTime e) |
| 42 |
: lastTick(e) { } |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
[[nodiscard]] constexpr EmuTime getTime() const { return lastTick; } |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
[[nodiscard]] constexpr bool before(EmuTime e) const { |
| 52 |
return lastTick.time < e.time; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
[[nodiscard]] constexpr uint64_t getTicksTill(EmuTime e) const { |
| 59 |
assert(e.time >= lastTick.time); |
| 60 |
return (e.time - lastTick.time) / MASTER_TICKS; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
[[nodiscard]] constexpr uint64_t getTicksTillUp(EmuTime e) const { |
| 67 |
assert(e.time >= lastTick.time); |
| 68 |
return (e.time - lastTick.time + MASTER_TICKS - 1) / MASTER_TICKS; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
[[nodiscard]] constexpr friend EmuTime operator+(const Clock& c, uint64_t n) { |
| 75 |
return EmuTime::fromUint64(c.lastTick.toUint64() + n * MASTER_TICKS); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
[[nodiscard]] constexpr EmuTime getFastAdd(unsigned n) const { |
| 81 |
#ifdef DEBUG |
| 82 |
assert((uint64_t(n) * MASTER_TICKS) < (1ULL << 32)); |
| 83 |
#endif |
| 84 |
return EmuTime(lastTick.time + n * MASTER_TICKS); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
constexpr void reset(EmuTime e) { |
| 90 |
lastTick.time = e.time; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
constexpr void advance(EmuTime e) { |
| 98 |
assert(lastTick.time <= e.time); |
| 99 |
lastTick.time = e.time - ((e.time - lastTick.time) % MASTER_TICKS); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
constexpr void operator+=(unsigned n) { |
| 105 |
lastTick.time += n * MASTER_TICKS; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
constexpr void fastAdd(unsigned n) { |
| 115 |
#ifdef DEBUG |
| 116 |
|
| 117 |
assert((n * MASTER_TICKS) < (1ULL << 32)); |
| 118 |
#endif |
| 119 |
lastTick.time += n * MASTER_TICKS32; |
| 120 |
} |
| 121 |
|
| 122 |
template<typename Archive> |
| 123 |
void serialize(Archive& ar, unsigned ) |
| 124 |
{ |
| 125 |
ar.serialize("lastTick", lastTick); |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
|
| 130 |
|
| 131 |
EmuTime lastTick; |
| 132 |
}; |
| 133 |
|
| 134 |
template<unsigned FREQ_NUM, unsigned FREQ_DENOM> |
| 135 |
struct SerializeAsMemcpy<Clock<FREQ_NUM, FREQ_DENOM>> : std::true_type {}; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
#endif |