feat: fix compile error and update demo
This commit is contained in:
@ -43,7 +43,9 @@ concept int_type = !has_length<_Tp> && implicit_convertible_to<_Tp, max_size_t>;
|
||||
|
||||
template<typename _Lhs, typename _Rhs>
|
||||
concept bit_match =
|
||||
(bit_type<_Lhs> && bit_type<_Rhs> && _Lhs::_Bit_Len == _Rhs::_Bit_Len) || (int_type<_Lhs> || int_type<_Rhs>);
|
||||
(bit_type<_Lhs> && bit_type<_Rhs> && _Lhs::_Bit_Len == _Rhs::_Bit_Len) // prevent format
|
||||
|| (int_type<_Lhs> && bit_type<_Rhs>) //
|
||||
|| (bit_type<_Lhs> && int_type<_Rhs>);
|
||||
|
||||
template<typename _Tp, std::size_t _Len>
|
||||
concept bit_convertible =
|
||||
|
@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include "module.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
@ -7,7 +9,8 @@ namespace dark {
|
||||
|
||||
class CPU {
|
||||
private:
|
||||
std::vector<std::unique_ptr<ModuleBase>> modules;
|
||||
std::vector<std::unique_ptr<ModuleBase>> mod_owned;
|
||||
std::vector<ModuleBase *> modules;
|
||||
|
||||
public:
|
||||
unsigned long long cycles = 0;
|
||||
@ -19,9 +22,21 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
void add_module(std::unique_ptr<ModuleBase> module) {
|
||||
modules.push_back(std::move(module));
|
||||
/// @attention the pointer will be moved. you SHOULD NOT use it after calling this function.
|
||||
template<typename _Tp>
|
||||
requires std::derived_from<_Tp, ModuleBase>
|
||||
void add_module(std::unique_ptr<_Tp> &module) {
|
||||
modules.push_back(module.get());
|
||||
mod_owned.emplace_back(std::move(module));
|
||||
}
|
||||
void add_module(std::unique_ptr<ModuleBase> module) {
|
||||
modules.push_back(module.get());
|
||||
mod_owned.emplace_back(std::move(module));
|
||||
}
|
||||
void add_module(ModuleBase *module) {
|
||||
modules.push_back(module);
|
||||
}
|
||||
|
||||
void run_once() {
|
||||
++cycles;
|
||||
for (auto &module: modules)
|
||||
@ -30,10 +45,7 @@ public:
|
||||
}
|
||||
void run_once_shuffle() {
|
||||
static std::default_random_engine engine;
|
||||
std::vector<ModuleBase *> shuffled;
|
||||
shuffled.reserve(modules.size());
|
||||
for (auto &module: modules)
|
||||
shuffled.push_back(module.get());
|
||||
std::vector<ModuleBase *> shuffled = modules;
|
||||
std::shuffle(shuffled.begin(), shuffled.end(), engine);
|
||||
|
||||
++cycles;
|
||||
|
@ -1,13 +1,21 @@
|
||||
#pragma once
|
||||
#include "synchronize.h"
|
||||
namespace dark {
|
||||
|
||||
namespace details {
|
||||
struct empty_class {
|
||||
void sync() { /* do nothing */ }
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
struct ModuleBase {
|
||||
virtual void work() = 0;
|
||||
virtual void sync() = 0;
|
||||
virtual ~ModuleBase() = default;
|
||||
};
|
||||
|
||||
template<typename _Tinput, typename _Toutput, typename _Tprivate>
|
||||
template<typename _Tinput, typename _Toutput, typename _Tprivate = details::empty_class>
|
||||
requires std::is_aggregate_v<_Tinput> && std::is_aggregate_v<_Toutput> && std::is_aggregate_v<_Tprivate>
|
||||
struct Module : public ModuleBase, public _Tinput, public _Toutput, protected _Tprivate {
|
||||
void sync() override final {
|
||||
sync_member(static_cast<_Tinput &>(*this));
|
||||
|
@ -41,6 +41,7 @@ public:
|
||||
}
|
||||
|
||||
explicit operator max_size_t() const { return this->_M_old; }
|
||||
explicit operator bool() const { return this->_M_old; }
|
||||
};
|
||||
|
||||
} // namespace dark
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include "register.h"
|
||||
#include "synchronize.h"
|
||||
#include "wire.h"
|
||||
#include "module.h"
|
||||
#include "cpu.h"
|
||||
|
||||
using dark::Bit;
|
||||
using dark::sign_extend;
|
||||
|
@ -107,6 +107,10 @@ public:
|
||||
this->_M_func.reset(_M_new_func(std::forward<_Fn>(fn)));
|
||||
this->sync();
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
return static_cast<max_size_t>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user