-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircuit.cpp
228 lines (211 loc) · 5.06 KB
/
circuit.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <memory>
#include <cstdlib>
#include <functional>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
#include <map>
class Wire
{
public:
auto getSignal() const
{
return mValue;
}
void setSignal(bool newValue)
{
if (mValue == newValue)
{
return;
}
mValue = newValue;
for (auto const& action : mActions)
{
action();
}
}
void addAction(std::function<void()> func)
{
func();
mActions.push_back(func);
}
private:
bool mValue{};
std::vector<std::function<void()>> mActions;
};
using WirePtr = std::shared_ptr<Wire>;
auto makeWire()
{
return std::make_shared<Wire>();
}
auto afterDelay(int32_t delay, std::function<void()> action) -> std::function<void()>;
void orGate(WirePtr in1, WirePtr in2, WirePtr out)
{
auto const action = [out = std::move(out), in1, in2]{
auto const newValue = in1->getSignal() || in2->getSignal();
out->setSignal(newValue);
};
auto const orGateDelay = 5;
auto const delayedAction = afterDelay(orGateDelay, action);
in1->addAction(delayedAction);
in2->addAction(delayedAction);
}
void andGate(WirePtr in1, WirePtr in2, WirePtr out)
{
auto const action = [out = std::move(out), in1, in2]{
auto const newValue = in1->getSignal() && in2->getSignal();
out->setSignal(newValue);
};
auto const andGateDelay = 3;
auto const delayedAction = afterDelay(andGateDelay, action);
in1->addAction(delayedAction);
in2->addAction(delayedAction);
}
void inverter(WirePtr in, WirePtr out)
{
auto action = [in, out = std::move(out)]
{
auto newValue = !(in->getSignal());
out->setSignal(newValue);
};
auto const inverterDelay = 2;
in->addAction(afterDelay(inverterDelay, action));
}
auto halfAdder(WirePtr a, WirePtr b, WirePtr s, WirePtr c)
{
auto d = makeWire();
auto e = makeWire();
orGate(a, b, d);
andGate(a, b, c);
inverter(c, e);
andGate(d, e, s);
}
auto fullAdder(WirePtr a, WirePtr b, WirePtr cIn, WirePtr sum, WirePtr cOut)
{
auto s = makeWire();
auto c1 = makeWire();
auto c2 = makeWire();
halfAdder(b, cIn, s, c1);
halfAdder(a, s, sum, c2);
orGate(c1, c2, cOut);
}
auto adder(std::vector<WirePtr> A, std::vector<WirePtr> B, std::vector<WirePtr> S, WirePtr C)
{
assert(A.size() == B.size());
assert(A.size() == S.size());
auto Cin = makeWire();
for (size_t i = 0; i < A.size() - 1; ++i)
{
auto Cout = makeWire();
fullAdder(A[i], B[i], Cin, S[i], Cout);
Cin = Cout;
}
fullAdder(A.back(), B.back(), Cin, S.back(), C);
}
class Agenda
{
private:
std::map<int32_t, std::queue<std::function<void()>>> mItems{};
int32_t mTime{};
Agenda() = default;
public:
static Agenda& instance()
{
static Agenda theAgenda;
return theAgenda;
}
bool empty() const
{
return mItems.empty();
}
auto pop()
{
auto& first = mItems.begin()->second;
auto result = first.front();
mTime = mItems.begin()->first;
first.pop();
if (first.empty())
{
mItems.erase(mItems.begin());
}
return result;
}
auto push(int32_t time, std::function<void()> action)
{
mItems[time].push(action);
return action;
}
auto currentTime() const
{
return mTime;
}
};
auto afterDelay(int32_t delay, std::function<void()> action) -> std::function<void()>
{
return [=]
{
auto &theAgenda = Agenda::instance();
theAgenda.push(theAgenda.currentTime() + delay, action);
};
}
auto propagate()
{
auto& theAgenda = Agenda::instance();
if (theAgenda.empty())
{
return;
}
auto firstItem = theAgenda.pop();
firstItem();
propagate();
}
void probe(std::string const& name, WirePtr wire)
{
auto const action = [=]
{
std::cout << name << " "
<< Agenda::instance().currentTime()
<< " New-value = "
<< wire->getSignal() << std::endl;
};
wire->addAction(action);
}
int32_t main()
{
auto input1 = makeWire();
auto input2 = makeWire();
auto sum = makeWire();
auto carry = makeWire();
// probe("input1", input1);
// probe("input2", input2);
probe("sum", sum);
probe("carry", carry);
//sum 0 New-value = 0
// carry 0 New-value = 0
halfAdder(input1, input2, sum, carry);
input1->setSignal(1);
propagate();
// sum 5 New-value = 1
input2->setSignal(1);
propagate();
// carry 11 New-value = 1
// sum 16 New-value = 0
//=== 2 bit adder
auto a0 = makeWire();
auto a1 = makeWire();
auto b0 = makeWire();
auto b1 = makeWire();
auto s0 = makeWire();
auto s1 = makeWire();
auto c = makeWire();
adder({a0, a1}, {b0, b1}, {s0, s1}, c);
a0->setSignal(1);
a1->setSignal(1);
b1->setSignal(1);
propagate();
assert(s0->getSignal() == 1);
assert(s1->getSignal() == 0);
assert(c->getSignal() == 1);
return 0;
}