summaryrefslogtreecommitdiff
path: root/lib/nes/util/cycle-count.hh
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-01-23 22:02:11 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-01-23 22:02:11 +0100
commitb1d1573702f21e3104606d6ade5f5d39ec86a33a (patch)
treed703be130a5d6f9ff2626313d3380ed34de44dd7 /lib/nes/util/cycle-count.hh
parent6512dd599d13035f6a90b2009344e02c488f5aae (diff)
restructure project
Diffstat (limited to 'lib/nes/util/cycle-count.hh')
-rw-r--r--lib/nes/util/cycle-count.hh87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/nes/util/cycle-count.hh b/lib/nes/util/cycle-count.hh
new file mode 100644
index 0000000..f1a4a7e
--- /dev/null
+++ b/lib/nes/util/cycle-count.hh
@@ -0,0 +1,87 @@
+#pragma once
+
+#include <chrono>
+
+namespace nes
+{
+ /// A cycle count that can be used for multiple components.
+ ///
+ /// Each component can have a different clock frequency and this structure offers conversions between different
+ /// cycle counts.
+ class cycle_count
+ {
+ friend constexpr auto operator+(cycle_count, cycle_count) -> cycle_count;
+ friend constexpr auto operator-(cycle_count, cycle_count) -> cycle_count;
+ friend constexpr auto operator*(std::uint64_t, cycle_count) -> cycle_count;
+ friend constexpr auto operator*(cycle_count, std::uint64_t) -> cycle_count;
+ friend constexpr auto operator/(cycle_count, cycle_count) -> std::uint64_t;
+
+ std::uint64_t units_{ 0 }; // The most fine-granular cycle count (here: PPU cycles).
+
+ static constexpr auto ppu_cycle() -> cycle_count { return cycle_count{ 1 }; }
+ static constexpr auto cpu_cycle() -> cycle_count { return cycle_count{ 3 }; }
+ static constexpr auto second() -> cycle_count { return 1789773 * cpu_cycle(); }
+
+ public:
+ constexpr explicit cycle_count() = default;
+
+ constexpr auto operator+=(cycle_count const other) -> cycle_count& { return *this = *this + other; }
+ constexpr auto operator*=(std::uint64_t const other) -> cycle_count& { return *this = *this * other; }
+
+ static constexpr auto from_units(std::uint64_t const units) -> cycle_count
+ {
+ return cycle_count{ units };
+ }
+
+ static constexpr auto from_duration(std::chrono::microseconds const duration) -> cycle_count
+ {
+ return from_units(static_cast<std::uint64_t>(duration.count()) * second().get_units() / (1000 * 1000));
+ }
+
+ static constexpr auto from_cpu(std::uint64_t const cpu_cycles) -> cycle_count
+ {
+ return cpu_cycles * cpu_cycle();
+ }
+
+ static constexpr auto from_ppu(std::uint64_t const ppu_cycles) -> cycle_count
+ {
+ return ppu_cycles * ppu_cycle();
+ }
+
+ /// Value in implementation-defined units.
+ constexpr auto get_units() const -> std::uint64_t { return units_; }
+
+ constexpr auto to_duration() const -> std::chrono::microseconds
+ {
+ return std::chrono::microseconds{ get_units() * 1000 * 1000 / second().get_units() };
+ }
+
+ constexpr auto to_cpu() const -> std::uint64_t
+ {
+ return *this / cpu_cycle();
+ }
+
+ constexpr auto to_ppu() const -> std::uint64_t
+ {
+ return *this / ppu_cycle();
+ }
+
+ private:
+ constexpr explicit cycle_count(std::uint64_t const units)
+ : units_{ units }
+ {
+ }
+ };
+
+ constexpr auto operator==(cycle_count const a, cycle_count const b) -> bool { return a.get_units() == b.get_units(); }
+ constexpr auto operator!=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() != b.get_units(); }
+ constexpr auto operator<(cycle_count const a, cycle_count const b) -> bool { return a.get_units() < b.get_units(); }
+ constexpr auto operator>(cycle_count const a, cycle_count const b) -> bool { return a.get_units() > b.get_units(); }
+ constexpr auto operator<=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() <= b.get_units(); }
+ constexpr auto operator>=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() >= b.get_units(); }
+ constexpr auto operator+(cycle_count const a, cycle_count const b) -> cycle_count { return cycle_count{ a.get_units() + b.get_units() }; }
+ constexpr auto operator-(cycle_count const a, cycle_count const b) -> cycle_count { return cycle_count{ a.get_units() - b.get_units() }; }
+ constexpr auto operator*(std::uint64_t const a, cycle_count const b) -> cycle_count { return cycle_count{ a * b.get_units() }; }
+ constexpr auto operator*(cycle_count const a, std::uint64_t const b) -> cycle_count { return cycle_count{ a.get_units() * b }; }
+ constexpr auto operator/(cycle_count const a, cycle_count const b) -> std::uint64_t { return a.get_units() / b.get_units(); }
+} // namespace nes \ No newline at end of file