diff --git a/include/cpu.h b/include/cpu.h new file mode 100644 index 0000000..215003f --- /dev/null +++ b/include/cpu.h @@ -0,0 +1,51 @@ +#include "module.h" +#include +#include +#include + +namespace dark { + +class CPU { +private: + std::vector> modules; + +public: + unsigned long long cycles = 0; + +private: + void sync_all() { + for (auto &module: modules) + module->sync(); + } + +public: + void add_module(std::unique_ptr module) { + modules.push_back(std::move(module)); + } + void run_once() { + ++cycles; + for (auto &module: modules) + module->work(); + sync_all(); + } + void run_once_shuffle() { + static std::default_random_engine engine; + std::vector shuffled; + shuffled.reserve(modules.size()); + for (auto &module: modules) + shuffled.push_back(module.get()); + std::shuffle(shuffled.begin(), shuffled.end(), engine); + + ++cycles; + for (auto &module: shuffled) + module->work(); + sync_all(); + } + void run(unsigned long long max_cycles = 0, bool shuffle = false) { + auto func = shuffle ? &CPU::run_once_shuffle : &CPU::run_once; + while (max_cycles == 0 || cycles < max_cycles) + (this->*func)(); + } +}; + +}// namespace dark diff --git a/include/module.h b/include/module.h new file mode 100644 index 0000000..674fafb --- /dev/null +++ b/include/module.h @@ -0,0 +1,19 @@ +#include "synchronize.h" +namespace dark { + +struct ModuleBase { + virtual void work() = 0; + virtual void sync() = 0; + virtual ~ModuleBase() = default; +}; + +template +struct Module : public ModuleBase, public _Tinput, public _Toutput, protected _Tprivate { + void sync() override final { + sync_member(static_cast<_Tinput &>(*this)); + sync_member(static_cast<_Toutput &>(*this)); + sync_member(static_cast<_Tprivate &>(*this)); + } +}; + +}// namespace dark