2017-07-26 00:20:55 +00:00
|
|
|
//
|
|
|
|
// ClockReceiver.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 22/07/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-07-26 00:20:55 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ClockReceiver_hpp
|
|
|
|
#define ClockReceiver_hpp
|
|
|
|
|
2017-07-28 01:38:50 +00:00
|
|
|
/*
|
|
|
|
Informal pattern for all classes that run from a clock cycle:
|
|
|
|
|
|
|
|
Each will implement either or both of run_for(Cycles) and run_for(HalfCycles), as
|
|
|
|
is appropriate.
|
|
|
|
|
|
|
|
Callers that are accumulating HalfCycles but want to talk to receivers that implement
|
|
|
|
only run_for(Cycles) can use HalfCycle.flush_cycles if they have appropriate storage, or
|
|
|
|
can wrap the receiver in HalfClockReceiver in order automatically to bind half-cycle
|
|
|
|
storage to it.
|
|
|
|
|
|
|
|
Alignment rule:
|
|
|
|
|
|
|
|
run_for(Cycles) may be called only after an even number of half cycles. E.g. the following
|
|
|
|
sequence will have undefined results:
|
|
|
|
|
|
|
|
run_for(HalfCycles(1))
|
|
|
|
run_for(Cycles(1))
|
|
|
|
|
|
|
|
An easy way to ensure this as a caller is to pick only one of run_for(Cycles) and
|
|
|
|
run_for(HalfCycles) to use.
|
|
|
|
|
|
|
|
Reasoning:
|
|
|
|
|
|
|
|
Users of this template may with to implement run_for(Cycles) and run_for(HalfCycles)
|
|
|
|
where there is a need to implement at half-cycle precision but a faster execution
|
|
|
|
path can be offered for full-cycle precision. Those users are permitted to assume
|
|
|
|
phase in run_for(Cycles) and should do so to be compatible with callers that use
|
|
|
|
only run_for(Cycles).
|
|
|
|
|
|
|
|
Corollary:
|
|
|
|
|
|
|
|
Starting from nothing, the first run_for(HalfCycles(1)) will do the **first** half
|
|
|
|
of a full cycle. The second will do the second half. Etc.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-07-26 00:20:55 +00:00
|
|
|
/*!
|
|
|
|
Provides a class that wraps a plain int, providing most of the basic arithmetic and
|
|
|
|
Boolean operators, but forcing callers and receivers to be explicit as to usage.
|
|
|
|
*/
|
|
|
|
template <class T> class WrappedInt {
|
|
|
|
public:
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr WrappedInt(int l) : length_(l) {}
|
|
|
|
constexpr WrappedInt() : length_(0) {}
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2018-05-28 21:48:55 +00:00
|
|
|
T &operator =(const T &rhs) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ = rhs.length_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:48:55 +00:00
|
|
|
T &operator +=(const T &rhs) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ += rhs.length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:48:55 +00:00
|
|
|
T &operator -=(const T &rhs) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ -= rhs.length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator ++() {
|
2017-07-26 00:20:55 +00:00
|
|
|
++ length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator ++(int) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ ++;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator --() {
|
2017-07-26 00:20:55 +00:00
|
|
|
-- length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator --(int) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ --;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator %=(const T &rhs) {
|
2017-07-26 00:20:55 +00:00
|
|
|
length_ %= rhs.length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:28:57 +00:00
|
|
|
T &operator &=(const T &rhs) {
|
2017-07-31 11:29:50 +00:00
|
|
|
length_ &= rhs.length_;
|
|
|
|
return *static_cast<T *>(this);
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr T operator +(const T &rhs) const { return T(length_ + rhs.length_); }
|
|
|
|
constexpr T operator -(const T &rhs) const { return T(length_ - rhs.length_); }
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr T operator %(const T &rhs) const { return T(length_ % rhs.length_); }
|
|
|
|
constexpr T operator &(const T &rhs) const { return T(length_ & rhs.length_); }
|
2017-07-31 11:29:50 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr T operator -() const { return T(- length_); }
|
2017-07-28 01:53:45 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr bool operator <(const T &rhs) const { return length_ < rhs.length_; }
|
|
|
|
constexpr bool operator >(const T &rhs) const { return length_ > rhs.length_; }
|
|
|
|
constexpr bool operator <=(const T &rhs) const { return length_ <= rhs.length_; }
|
|
|
|
constexpr bool operator >=(const T &rhs) const { return length_ >= rhs.length_; }
|
|
|
|
constexpr bool operator ==(const T &rhs) const { return length_ == rhs.length_; }
|
|
|
|
constexpr bool operator !=(const T &rhs) const { return length_ != rhs.length_; }
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr bool operator !() const { return !length_; }
|
2017-07-28 01:38:50 +00:00
|
|
|
// bool operator () is not supported because it offers an implicit cast to int, which is prone silently to permit misuse
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr int as_int() const { return length_; }
|
2017-07-26 00:20:55 +00:00
|
|
|
|
|
|
|
/*!
|
2018-05-13 19:34:31 +00:00
|
|
|
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
2017-07-26 00:20:55 +00:00
|
|
|
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
|
|
|
*/
|
2018-05-28 21:28:57 +00:00
|
|
|
T divide(const T &divisor) {
|
2017-07-26 00:20:55 +00:00
|
|
|
T result(length_ / divisor.length_);
|
|
|
|
length_ %= divisor.length_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Flushes the value in @c this. The current value is returned, and the internal value
|
|
|
|
is reset to zero.
|
|
|
|
*/
|
2018-05-28 21:28:57 +00:00
|
|
|
T flush() {
|
2017-07-26 00:20:55 +00:00
|
|
|
T result(length_);
|
|
|
|
length_ = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// operator int() is deliberately not provided, to avoid accidental subtitution of
|
|
|
|
// classes that use this template.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int length_;
|
|
|
|
};
|
|
|
|
|
2018-05-13 19:34:31 +00:00
|
|
|
/// Describes an integer number of whole cycles: pairs of clock signal transitions.
|
2017-07-26 00:20:55 +00:00
|
|
|
class Cycles: public WrappedInt<Cycles> {
|
|
|
|
public:
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr Cycles(int l) : WrappedInt<Cycles>(l) {}
|
|
|
|
constexpr Cycles() : WrappedInt<Cycles>() {}
|
|
|
|
constexpr Cycles(const Cycles &cycles) : WrappedInt<Cycles>(cycles.length_) {}
|
2017-07-26 00:20:55 +00:00
|
|
|
};
|
|
|
|
|
2018-05-13 19:34:31 +00:00
|
|
|
/// Describes an integer number of half cycles: single clock signal transitions.
|
2017-07-26 00:20:55 +00:00
|
|
|
class HalfCycles: public WrappedInt<HalfCycles> {
|
|
|
|
public:
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr HalfCycles(int l) : WrappedInt<HalfCycles>(l) {}
|
|
|
|
constexpr HalfCycles() : WrappedInt<HalfCycles>() {}
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr HalfCycles(const Cycles cycles) : WrappedInt<HalfCycles>(cycles.as_int() * 2) {}
|
|
|
|
constexpr HalfCycles(const HalfCycles &half_cycles) : WrappedInt<HalfCycles>(half_cycles.length_) {}
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-07-28 01:38:50 +00:00
|
|
|
/// @returns The number of whole cycles completely covered by this span of half cycles.
|
2018-05-28 21:20:11 +00:00
|
|
|
constexpr Cycles cycles() {
|
2017-07-28 01:38:50 +00:00
|
|
|
return Cycles(length_ >> 1);
|
|
|
|
}
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-12-02 21:30:43 +00:00
|
|
|
/// Flushes the whole cycles in @c this, subtracting that many from the total stored here.
|
2018-05-28 21:28:57 +00:00
|
|
|
Cycles flush_cycles() {
|
2017-07-28 01:38:50 +00:00
|
|
|
Cycles result(length_ >> 1);
|
|
|
|
length_ &= 1;
|
|
|
|
return result;
|
|
|
|
}
|
2017-08-02 11:21:21 +00:00
|
|
|
|
2017-12-02 21:30:43 +00:00
|
|
|
/// Flushes the half cycles in @c this, returning the number stored and setting this total to zero.
|
2018-05-28 21:28:57 +00:00
|
|
|
HalfCycles flush() {
|
2017-12-02 21:30:43 +00:00
|
|
|
HalfCycles result(length_);
|
|
|
|
length_ = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:21:21 +00:00
|
|
|
/*!
|
2018-05-13 19:34:31 +00:00
|
|
|
Severs from @c this the effect of dividing by @c divisor; @c this will end up with
|
2017-08-02 11:21:21 +00:00
|
|
|
the value of @c this modulo @c divisor and @c divided by @c divisor is returned.
|
|
|
|
*/
|
2018-05-28 21:28:57 +00:00
|
|
|
Cycles divide_cycles(const Cycles &divisor) {
|
2017-08-02 11:21:21 +00:00
|
|
|
HalfCycles half_divisor = HalfCycles(divisor);
|
|
|
|
Cycles result(length_ / half_divisor.length_);
|
|
|
|
length_ %= half_divisor.length_;
|
|
|
|
return result;
|
|
|
|
}
|
2017-07-28 01:38:50 +00:00
|
|
|
};
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-07-27 11:40:02 +00:00
|
|
|
/*!
|
|
|
|
If a component implements only run_for(Cycles), an owner can wrap it in HalfClockReceiver
|
|
|
|
automatically to gain run_for(HalfCycles).
|
|
|
|
*/
|
|
|
|
template <class T> class HalfClockReceiver: public T {
|
|
|
|
public:
|
|
|
|
using T::T;
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
inline void run_for(const HalfCycles half_cycles) {
|
2017-07-28 01:38:50 +00:00
|
|
|
half_cycles_ += half_cycles;
|
|
|
|
T::run_for(half_cycles_.flush_cycles());
|
2017-07-26 00:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-07-28 01:38:50 +00:00
|
|
|
HalfCycles half_cycles_;
|
2017-07-26 00:20:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ClockReceiver_hpp */
|