forked from cdkersey/chdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.h
88 lines (71 loc) · 2.35 KB
/
ingress.h
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
#ifndef __CHDL_INGRESS_H
#define __CHDL_INGRESS_H
#include "chdl.h"
#include "nodeimpl.h"
#include "sim.h"
#include "cdomain.h"
#include "reset.h"
#include <functional>
namespace chdl {
template <typename T> node IngressFunc(const T &f);
static node Ingress(bool &x);
static node IngressAutoclear(bool &x);
template <unsigned N, typename T> void IngressInt(bvec<N> &b, const T &x);
template <unsigned N, typename T> bvec<N> IngressInt(const T &x);
template <unsigned N, typename T> void IngressIntFunc(bvec<N> &b, const T &x);
template <unsigned N, typename T> bvec<N> IngressIntFunc(const T &x);
template <typename T> class ingressimpl : public nodeimpl {
public:
ingressimpl(T f): nodeimpl(), f(f), eval_time(0), val(0) {}
virtual ~ingressimpl() {}
bool eval(cdomain_handle_t cd) {
if (sim_time(cd) >= eval_time) { val = f(); eval_time = sim_time(cd) + 1;}
return val;
}
void print(std::ostream &out) { abort(); }
void print_vl(std::ostream &out) { abort(); }
private:
cycle_t eval_time;
bool val;
T f;
};
template <typename T> ingressimpl<T> *IngressImpl(T f) {
return new ingressimpl<T>(f);
}
template<typename T> node IngressFunc(const T &f) {
IngressImpl(f);
return node(nodes.size()-1);
}
static node Ingress(bool &x) {
return IngressFunc([&x](){ return x; });
}
static node IngressAutoclear(bool &x) {
return IngressFunc([&x](){ bool prevx(x); x = false; return prevx; });
}
template <typename T> std::function<bool()> GetBit(unsigned bit, T &x) {
return std::function<bool()>([&x, bit](){ return bool((x >> bit)&1); });
}
template <typename T> std::function<bool()> GetBitFunc(unsigned bit, T &x) {
return std::function<bool()>([&x, bit](){ return bool((x() >> bit)&1); });
}
template <unsigned N, typename T> void IngressInt(bvec<N> &b, T &x) {
for (unsigned i = 0; i < N; ++i)
b[i] = IngressFunc(GetBit(i, x));
}
template <unsigned N, typename T> void IngressIntFunc(bvec<N> &b, const T &x)
{
for (unsigned i = 0; i < N; ++i)
b[i] = IngressFunc(GetBitFunc(i, x));
}
template <unsigned N, typename T> bvec<N> IngressInt(T &x) {
bvec<N> r;
IngressInt(r, x);
return r;
}
template <unsigned N, typename T> bvec<N> IngressIntFunc(const T& x) {
bvec<N> r;
IngressIntFunc(r, x);
return r;
}
}
#endif