From c630f86f33909d6e4db3914f04840a67104edc26 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 29 May 2023 22:56:36 +0100 Subject: [PATCH] Attempt to generalise out from the 9918's current sense of dispatching. --- ClockReceiver/Dispatcher.hpp | 89 +++++++++++++++++++ .../Clock Signal.xcodeproj/project.pbxproj | 4 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 ClockReceiver/Dispatcher.hpp diff --git a/ClockReceiver/Dispatcher.hpp b/ClockReceiver/Dispatcher.hpp new file mode 100644 index 000000000..2773c6c55 --- /dev/null +++ b/ClockReceiver/Dispatcher.hpp @@ -0,0 +1,89 @@ +// +// Serialiser.hpp +// Clock Signal +// +// Created by Thomas Harte on 29/05/2023. +// Copyright © 2023 Thomas Harte. All rights reserved. +// + +#ifndef Dispatcher_hpp +#define Dispatcherr_hpp + +namespace Dispatcher { + +/// The unity function; converts n directly to n. +struct UnitConverter { + constexpr int operator ()(int n) { + return n; + } +}; + +template +struct Dispatcher { + + /// Perform @c target.perform() for the input range [start, end]; @c ConverterT()(n) will be applied to + /// each individual step before it becomes the relevant template argument. + void dispatch(SequencerT &target, int start, int end) { + + // Minor optimisation: do a comparison with end once outside the loop and if it implies so + // then do no further comparisons within the loop. This is somewhat targetted at expected + // use cases. + if(end < max) { + dispatch(target, start, end); + } else { + dispatch(target, start, end); + } + } + +private: + template void dispatch(SequencerT &target, int start, int end) { + static_assert(max < 2048); + + // Yes, macros, yuck. But I want an actual switch statement for the dispatch to start + // and to allow a simple [[fallthrough]] through all subsequent steps up until end. + // So I don't think I have much in the way of options here. + // + // Sensible choices by the optimiser are assumed. + +#define index(n) \ + if constexpr (n == max) return; \ + if(use_end && end == n) return; \ + [[fallthrough]]; \ + case n: target.template perform(); + +#define index2(n) index(n); index(n+1); +#define index4(n) index2(n); index2(n+2); +#define index8(n) index4(n); index4(n+4); +#define index16(n) index8(n); index8(n+8); +#define index32(n) index16(n); index16(n+16); +#define index64(n) index32(n); index32(n+32); +#define index128(n) index64(n); index64(n+64); +#define index256(n) index128(n); index128(n+128); +#define index512(n) index256(n); index256(n+256); +#define index1024(n) index512(n); index512(n+512); +#define index2048(n) index1024(n); index1024(n+1024); + + switch(start) { + default: assert(false); + index2048(0); + } + +#undef index +#undef index2 +#undef index4 +#undef index8 +#undef index16 +#undef index32 +#undef index64 +#undef index128 +#undef index256 +#undef index512 +#undef index1024 +#undef index2048 + } + +}; + +} + +#endif /* Dispatcher_hpp */ diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 75e1fd830..953a4376f 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -1101,6 +1101,7 @@ /* Begin PBXFileReference section */ 428168372A16C25C008ECD27 /* LineLayout.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = LineLayout.hpp; sourceTree = ""; }; + 428168382A254FBE008ECD27 /* Dispatcher.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Dispatcher.hpp; sourceTree = ""; }; 42AD552E2A0C4D5000ACE410 /* 68000.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 68000.hpp; sourceTree = ""; }; 42AD55302A0C4D5000ACE410 /* 68000Storage.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 68000Storage.hpp; sourceTree = ""; }; 42AD55312A0C4D5000ACE410 /* 68000Implementation.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 68000Implementation.hpp; sourceTree = ""; }; @@ -4967,12 +4968,13 @@ 4BB146C61F49D7D700253439 /* ClockingHintSource.hpp */, 4BF6606A1F281573002CB053 /* ClockReceiver.hpp */, 4B8A7E85212F988200F2BBC6 /* DeferredQueue.hpp */, + 4B99EBD026BF2D9F00CA924D /* DeferredValue.hpp */, 4BB06B211F316A3F00600C7A /* ForceInline.hpp */, 4B80214322EE7C3E00068002 /* JustInTime.hpp */, 4B644ED023F0FB55006C0CC5 /* ScanSynchroniser.hpp */, + 428168382A254FBE008ECD27 /* Dispatcher.hpp */, 4B449C942063389900A095C8 /* TimeTypes.hpp */, 4B996B2D2496DAC2001660EF /* VSyncPredictor.hpp */, - 4B99EBD026BF2D9F00CA924D /* DeferredValue.hpp */, ); name = ClockReceiver; path = ../../ClockReceiver;