1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-06 01:28:57 +00:00

Sketched out a template for clock-receiving components to allow them to be implemented in terms of either half or whole cycles.

This commit is contained in:
Thomas Harte 2017-07-22 21:46:50 -04:00
parent 97334e10af
commit 1ba3f262a2
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,55 @@
//
// ClockReceiver.hpp
// Clock Signal
//
// Created by Thomas Harte on 22/07/2017.
// Copyright © 2017 Thomas Harte. All rights reserved.
//
#ifndef ClockReceiver_hpp
#define ClockReceiver_hpp
/*! Describes an integer number of whole cycles — pairs of clock signal transitions. */
class Cycles {
public:
Cycles(int l) : length_(l) {}
operator int() const { return length_; }
private:
int length_;
};
/*! Describes an integer number of half cycles — single clock signal transitions. */
class HalfCycles {
public:
HalfCycles(int l) : length_(l) {}
HalfCycles(const Cycles &cycles) : length_(int(cycles) << 1) {}
operator int() const { return length_; }
private:
int length_;
};
/*!
ClockReceiver is a template for components that receove a clock, measured either
in cycles or in half cycles. They are expected to implement either of the run_for
methods; buying into the template means that the other run_for will automatically
map appropriately to the implemented one, so callers may use either.
*/
template <class T> class ClockReceiver {
public:
void run_for(const Cycles &cycles) {
static_cast<T *>(this)->run_for(HalfCycles(cycles));
}
void run_for(const HalfCycles &half_cycles) {
int cycles = int(half_cycles) + half_cycle_carry;
half_cycle_carry = cycles & 1;
run_for(Cycles(cycles >> 1));
}
private:
int half_cycle_carry;
};
#endif /* ClockReceiver_hpp */

View File

@ -563,6 +563,7 @@
4B4DC8271D2C2470003C5BF8 /* C1540.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = C1540.hpp; sourceTree = "<group>"; };
4B4DC8291D2C27A4003C5BF8 /* SerialBus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SerialBus.cpp; sourceTree = "<group>"; };
4B4DC82A1D2C27A4003C5BF8 /* SerialBus.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SerialBus.hpp; sourceTree = "<group>"; };
4B4EA7E01F24349400C216B4 /* ClockReceiver.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ClockReceiver.hpp; sourceTree = "<group>"; };
4B5073051DDD3B9400C48FBD /* ArrayBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ArrayBuilder.cpp; sourceTree = "<group>"; };
4B5073061DDD3B9400C48FBD /* ArrayBuilder.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ArrayBuilder.hpp; sourceTree = "<group>"; };
4B5073091DDFCFDF00C48FBD /* ArrayBuilderTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ArrayBuilderTests.mm; sourceTree = "<group>"; };
@ -1988,6 +1989,7 @@
4BC9DF4A1D04691600F44158 /* Components */ = {
isa = PBXGroup;
children = (
4B4EA7E01F24349400C216B4 /* ClockReceiver.hpp */,
4BD468F81D8DF4290084958B /* 1770 */,
4BC9DF4B1D04691600F44158 /* 6522 */,
4B1E85791D174DEC001EF87D /* 6532 */,