From 1ba3f262a2fad6ccafbb206fa95369fe8a43bba7 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 22 Jul 2017 21:46:50 -0400 Subject: [PATCH] Sketched out a template for clock-receiving components to allow them to be implemented in terms of either half or whole cycles. --- Components/ClockReceiver.hpp | 55 +++++++++++++++++++ .../Clock Signal.xcodeproj/project.pbxproj | 2 + 2 files changed, 57 insertions(+) create mode 100644 Components/ClockReceiver.hpp diff --git a/Components/ClockReceiver.hpp b/Components/ClockReceiver.hpp new file mode 100644 index 000000000..2e2a0501d --- /dev/null +++ b/Components/ClockReceiver.hpp @@ -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 ClockReceiver { + public: + void run_for(const Cycles &cycles) { + static_cast(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 */ diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 2925ec984..3970d170e 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -563,6 +563,7 @@ 4B4DC8271D2C2470003C5BF8 /* C1540.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = C1540.hpp; sourceTree = ""; }; 4B4DC8291D2C27A4003C5BF8 /* SerialBus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SerialBus.cpp; sourceTree = ""; }; 4B4DC82A1D2C27A4003C5BF8 /* SerialBus.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SerialBus.hpp; sourceTree = ""; }; + 4B4EA7E01F24349400C216B4 /* ClockReceiver.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ClockReceiver.hpp; sourceTree = ""; }; 4B5073051DDD3B9400C48FBD /* ArrayBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ArrayBuilder.cpp; sourceTree = ""; }; 4B5073061DDD3B9400C48FBD /* ArrayBuilder.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ArrayBuilder.hpp; sourceTree = ""; }; 4B5073091DDFCFDF00C48FBD /* ArrayBuilderTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ArrayBuilderTests.mm; sourceTree = ""; }; @@ -1988,6 +1989,7 @@ 4BC9DF4A1D04691600F44158 /* Components */ = { isa = PBXGroup; children = ( + 4B4EA7E01F24349400C216B4 /* ClockReceiver.hpp */, 4BD468F81D8DF4290084958B /* 1770 */, 4BC9DF4B1D04691600F44158 /* 6522 */, 4B1E85791D174DEC001EF87D /* 6532 */,