forked from cdkersey/chdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttable.h
99 lines (76 loc) · 2.06 KB
/
ttable.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
89
90
91
92
93
94
95
96
97
98
99
#ifndef CHDL_TTABLE_H
#define CHDL_TTABLE_H
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <random>
#include <string>
namespace chdl {
node TruthTable(const std::vector<node> &in, const char *tt,
std::map<std::set<int>, node> &cache);
template <unsigned N>
node TruthTable(const bvec<N> &in, const char *tt,
std::map<std::set<int>, node> &cache)
{
using namespace std;
vector<node> v(N);
for (unsigned i = 0; i < N; ++i) v[i] = in[i];
return TruthTable(v, tt, cache);
}
template <unsigned A>
node LLRom(const bvec<A> &a, const std::vector<bool> &contents,
std::map<std::set<int>, node> &cache)
{
char tt[1ul<<A];
unsigned sz(contents.size() > (1ul<<A) ? (1ul<<A) : contents.size());
for (unsigned i = 0; i < sz; ++i) {
if (contents[i]) tt[i] = '1';
else tt[i] = '0';
}
for (unsigned i = sz; i < (1ul<<A); ++i) {
tt[i] = 'x';
}
return TruthTable(a, tt, cache);
}
template <unsigned A, unsigned N, typename T>
bvec<N> LLRom(const bvec<A> &a, const std::vector<T> &init)
{
using namespace std;
bvec<N> q;
map<set<int>, node> cache;
for (unsigned i = 0; i < N; ++i) {
vector<bool> tt;
for (unsigned j = 0; j < init.size(); ++j)
tt.push_back((init[j] >> i) & 1);
q[i] = LLRom(a, tt, cache);
}
return q;
}
template <unsigned A, unsigned N>
bvec<N> LLRom(const bvec<A> &a, std::string filename)
{
using namespace std;
ifstream in(filename);
vector<vector<bool> > contents(N);
while (!!in) {
vector<bool> row;
char c;
while (!!in && (c = in.get()) != '\n') {
unsigned cval = (c >= 'a' && c <= 'f') ? (c - 'a' + 10) : (c - '0');
row.push_back(cval & 8);
row.push_back(cval & 4);
row.push_back(cval & 2);
row.push_back(cval & 1);
}
if (!in) break;
for (unsigned i = 0; i < N; ++i)
contents[i].push_back(row[row.size() - 1 - i]);
}
map<set<int>, node> cache;
bvec<N> out;
for (unsigned i = 0; i < N; ++i)
out[i] = LLRom(a, contents[i], cache);
return out;
}
}
#endif