-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reg.hpp
154 lines (137 loc) · 4.05 KB
/
Reg.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#pragma once
#include "Logic.hpp"
#include <functional>
#include <cassert>
template<typename T>
class Reg: public SeqLogic{
public:
Reg()=default;
Reg(std::function<T()> next){
in(next);
}
Reg(T init){
val_out = val_eval = init;
}
Reg(T init, const std::function<T()>& next){
val_out = val_eval = init;
in(next);
}
Reg(const std::function<T()>& next, const std::function<bool()>& enable){
in(next);
en(enable);
}
Reg(T init, const std::function<T()>& next, const std::function<bool()>& enable){
val_out = val_eval = init;
in(next);
en(enable);
}
Reg& in(const std::function<T()>& next){
infunc = next;
if(enfunc()){
val_eval = infunc();
}
return *this;
}
const std::function<T()>& operator=(const std::function<T()>& next){
in(next);
return next;
}
Reg& in(const T& next){
if(enfunc()){
infunc = [next](){return next;};
val_eval = infunc();
}
return *this;
}
const T& operator=(const T& next){
in(next);
return next;
}
Reg& in(const Reg& next){
assert((&next)!=this);
infunc = [&next](){return next.out();};
if(enfunc()){
val_eval = infunc();
}
return *this;
}
const Reg& operator=(const Reg& next){
in(next);
return next;
}
Reg& en(const std::function<bool()>& enable){
enfunc = enable;
return *this;
}
T out() const{
return val_out;
}
T operator*() const{
return val_out;
}
T* outp(){
return &val_out;
}
T* operator&(){
return &val_out;
}
// operator T() const{
// return out();
// }
protected:
std::function<T()> infunc=[&](){return val_eval;};
std::function<bool()> enfunc=[](){return true;};
T val_eval, val_out;
void eval() override{
if(enfunc()){
val_eval = infunc();
}
}
void update() override{
val_out = val_eval;
}
private:
// no move assignment
Reg<T>& operator=(Reg<T>&& other) noexcept{
}
};
#define reg_comparison_operator_overloading(SYMBOL) \
template<typename T, typename T2> \
inline const std::function<bool()> operator SYMBOL(const Reg<T>& reg, const T2& val){ \
return [®, val](){return reg.out() SYMBOL val;}; \
} \
template<typename T, typename T2> \
inline const std::function<bool()> operator SYMBOL(const T2& val, const Reg<T>& reg){ \
return reg SYMBOL val; \
} \
template<typename T, typename T2> \
inline const std::function<bool()> operator SYMBOL(const Reg<T>& regl, const Reg<T2>& regr){ \
return [®l, ®r](){return regl.out() SYMBOL regr.out();}; \
}
reg_comparison_operator_overloading(==)
reg_comparison_operator_overloading(!=)
reg_comparison_operator_overloading(<)
reg_comparison_operator_overloading(<=)
reg_comparison_operator_overloading(>)
reg_comparison_operator_overloading(>=)
#define reg_binary_arithmetic_operator_overloading(SYMBOL) \
template<typename T, typename T2> \
inline const std::function<T()> operator SYMBOL(const Reg<T>& reg, const T2& val){ \
return [®, val](){return reg.out() SYMBOL val;}; \
} \
template<typename T, typename T2> \
inline const std::function<T()> operator SYMBOL(const T2& val, const Reg<T>& reg){ \
return reg SYMBOL val; \
} \
template<typename T> \
inline const std::function<T()> operator SYMBOL(const Reg<T>& regl, const Reg<T>& regr){ \
return [®l, ®r](){return regl.out() SYMBOL regr.out();}; \
}
reg_binary_arithmetic_operator_overloading(+)
reg_binary_arithmetic_operator_overloading(-)
reg_binary_arithmetic_operator_overloading(*)
reg_binary_arithmetic_operator_overloading(/)
reg_binary_arithmetic_operator_overloading(%)
reg_binary_arithmetic_operator_overloading(^)
reg_binary_arithmetic_operator_overloading(&)
reg_binary_arithmetic_operator_overloading(|)