Repository navigation
Expand file tree
/
Copy pathCore.cpp
More file actions
309 lines (264 loc) · 8.66 KB
/
Copy pathCore.cpp
File metadata and controls
309 lines (264 loc) · 8.66 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <algorithm>
#include <csignal>
#include <iostream>
#include <memory>
#include <ostream>
#include <regex>
#include <vector>
#include <readline/history.h>
#include <readline/readline.h>
#include "components/And.hpp"
#include "components/C4001.hpp"
#include "components/Clock.hpp"
#include "components/False.hpp"
#include "components/Input.hpp"
#include "components/Not.hpp"
#include "components/Or.hpp"
#include "components/Output.hpp"
#include "components/True.hpp"
#include "components/Xor.hpp"
#include "Core.hpp"
#include "IComponent.hpp"
#include "main.hpp"
#include "Tristate.hpp"
template<>
struct std::hash<job> {
static auto int_hash(std::size_t x) -> size_t
{
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
x = x ^ (x >> 31);
return x;
}
auto operator()(const job &j) const noexcept -> size_t
{
return (int_hash(j.pin) << 1) ^ int_hash(j.comp);
}
};
namespace {
void sigint_handler([[maybe_unused]] int sig)
{
rl_replace_line("", 0);
rl_on_new_line();
rl_crlf();
rl_redisplay();
}
const char *COMMAND_NAMES[] = {
"display", "simulate", "exit", "dump", "stabilize", "loop", nullptr,
};
auto command_name_generator(const char *text, int state) -> char *
{
static std::size_t list_index;
static std::size_t len;
const char *name;
if (state == 0) {
list_index = 0;
len = strlen(text);
}
while ((name = COMMAND_NAMES[list_index++]) != nullptr) {
if (strncmp(name, text, len) == 0) {
return strdup(name);
}
}
return nullptr;
}
auto command_name_completion(const char *text, [[maybe_unused]] int start, [[maybe_unused]] int end) -> char **
{
rl_attempted_completion_over = 1;
return rl_completion_matches(text, command_name_generator);
}
auto trim(const std::string &s) -> std::string
{
// Whitespace is one of: space, tab, carriage return,
// line feed, form feed, or vertical tab.
const char *whitespace = " \t\n\r\f\v";
size_t const begin = s.find_first_not_of(whitespace);
if (begin == std::string::npos) {
return std::string{};
}
size_t const end = s.find_last_not_of(whitespace);
return std::string{ s.substr(begin, end - begin + 1) };
}
auto make_component(const std::string &type) -> std::unique_ptr<nts::IComponent>
{
if (type == "input")
return std::unique_ptr<nts::IComponent>(new Input);
if (type == "output")
return std::unique_ptr<nts::IComponent>(new Output);
if (type == "true")
return std::unique_ptr<nts::IComponent>(new True);
if (type == "false")
return std::unique_ptr<nts::IComponent>(new False);
if (type == "or")
return std::unique_ptr<nts::IComponent>(new Or);
if (type == "and")
return std::unique_ptr<nts::IComponent>(new And);
if (type == "xor")
return std::unique_ptr<nts::IComponent>(new Xor);
if (type == "not")
return std::unique_ptr<nts::IComponent>(new Not);
if (type == "4001")
return std::unique_ptr<nts::IComponent>(new C4001);
if (type == "clock")
return std::unique_ptr<nts::IComponent>(new Clock);
abort();
}
} // namespace
Core::Core() = default;
auto Core::get_instance() -> Core &
{
static Core core;
return core;
}
auto Core::cli_loop() -> void
{
signal(SIGINT, sigint_handler);
rl_attempted_completion_function = command_name_completion;
while (true) {
char *tmp = readline("> ");
if (tmp == nullptr)
return;
add_history(tmp);
std::string const line(trim(tmp));
if (line == "simulate")
this->simulate();
else if (line == "stabilize")
this->stabilize();
else if (line == "display")
this->display();
else if (line == "dump")
this->dump();
else if (line == "exit") {
free(tmp);
return;
} else {
static const std::regex assign_regex(R"(([\S]+)=(0|1|U|u))");
static const std::regex loop_regex(R"(loop[\s]+([\d]+))");
std::smatch match_assign;
std::smatch match_loop;
const auto matched_assign = std::regex_match(line, match_assign, assign_regex);
const auto matched_loop = std::regex_match(line, match_loop, loop_regex);
if (matched_assign) {
auto comp = this->get_comp(match_assign[1]);
auto val = nts::string_to_tristate(match_assign[2]);
this->_to_set.emplace_back(comp, val);
} else if (matched_loop) {
std::size_t const idx = static_cast<std::size_t>(std::stoi(match_loop[1]));
this->loop(idx);
} else {
std::cout << "Unknown command" << '\n';
}
}
free(tmp);
std::flush(std::cout);
}
}
auto Core::loop(const std::size_t idx) -> void
{
for (std::size_t i = 0; i < idx; i++) {
this->simulate();
this->display();
}
}
auto Core::get_comp(const std::string &name) const -> std::size_t
{
for (std::size_t i = 0; i < this->_comp_names.size(); i++)
if (this->_comp_names.at(i) == name)
return i;
abort();
}
auto Core::load_circuit(const std::vector<nts_entry> &entries) -> void
{
for (const auto &entry : entries) {
const std::size_t i = entry.index();
if (i == 0) {
const auto &data = std::get<0>(entry);
const std::size_t id = this->_comp_names.size();
this->_components.emplace_back(make_component(data.type));
this->_comp_names.emplace_back(data.name);
if (data.type == "output")
this->_outputs.emplace_back(id);
else if (data.type == "input" || data.type == "clock")
this->_inputs.emplace_back(id);
} else if (i == 1) {
const auto &data = std::get<1>(entry);
std::size_t const comp_a = this->get_comp(data.name_a);
std::size_t const comp_b = this->get_comp(data.name_b);
this->_components.at(comp_a)->set_link(data.pin_a, comp_b, data.pin_b);
this->_components.at(comp_b)->set_link(data.pin_b, comp_a, data.pin_a);
}
}
}
auto Core::submit_job(std::size_t comp, std::size_t pin) -> void
{
this->_jobs.emplace_back(comp, pin);
}
auto Core::get_pin(const std::size_t comp, const std::size_t pin) const -> nts::tristate
{
return this->_components.at(comp)->get_pin_state(pin);
}
auto Core::dump() const -> void
{
for (std::size_t i = 0; i < this->_components.size(); i++)
this->_components.at(i)->dump(this->_comp_names.at(i));
std::flush(std::cout);
}
auto Core::display() -> void
{
std::cout << "tick: " << this->_tick << '\n';
std::cout << "input(s):" << '\n';
for (auto i : this->_inputs)
std::cout << " " << this->_comp_names.at(i) << ": " << nts::tristate_to_string(this->get_pin(i, 0)) << '\n';
std::cout << "output(s):" << '\n';
for (auto i : this->_outputs)
std::cout << " " << this->_comp_names.at(i) << ": " << nts::tristate_to_string(this->get_pin(i, 0)) << '\n';
}
auto Core::simulate() -> void
{
this->_tick++;
std::set<std::size_t> seen_jobs;
// Set the inputs to the desired values
for (auto i : this->_to_set) {
auto *comp = dynamic_cast<Input *>(this->_components.at(i.comp).get());
if (comp == nullptr)
continue;
comp->set_state(i.state);
}
// Queue the outputs
for (auto i : this->_outputs)
this->submit_job(i, 0);
// Loop until there are no more jobs
while (!this->_jobs.empty()) {
auto j = this->_jobs.at(0);
this->_jobs.erase(this->_jobs.begin());
std::size_t const job_hash = std::hash<job>{}(j);
if (seen_jobs.contains(job_hash))
continue;
seen_jobs.insert(job_hash);
this->_components.at(j.comp)->simulate(this->_tick, j.pin);
}
this->_jobs.clear(); // This one should be empty, but just to be sure
this->_to_set.clear();
}
auto Core::stabilize() -> void
{
this->_stabilization_hashes.clear();
while (true) {
auto current_hash = this->hash_circuit_state();
if (this->_stabilization_hashes.contains(current_hash))
break;
this->_stabilization_hashes.insert(current_hash);
this->simulate();
}
}
auto Core::get_comp_name(const std::size_t id) -> const std::string &
{
return this->_comp_names.at(id);
}
auto Core::hash_circuit_state() const -> std::size_t
{
std::size_t result = 0;
for (const auto &i : this->_components)
result = (result << 1) ^ i->hash_state();
return result;
}