-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathnet.cpp
489 lines (424 loc) · 15 KB
/
net.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "hal_core/netlist/net.h"
#include "hal_core/netlist/endpoint.h"
#include "hal_core/netlist/event_system/event_handler.h"
#include "hal_core/netlist/gate.h"
#include "hal_core/netlist/grouping.h"
#include "hal_core/netlist/netlist.h"
#include "hal_core/netlist/netlist_internal_manager.h"
#include "hal_core/utilities/log.h"
#include <assert.h>
#include <memory>
namespace hal
{
Net::Net(NetlistInternalManager* internal_manager, EventHandler* event_handler, const u32 id, const std::string& name)
{
assert(internal_manager != nullptr);
m_internal_manager = internal_manager;
m_id = id;
m_name = name;
m_event_handler = event_handler;
}
bool Net::operator==(const Net& other) const
{
if (m_id != other.get_id() || m_name != other.get_name())
{
log_debug("net", "the nets with IDs {} and {} are not equal due to an unequal ID or name.", m_id, other.get_id());
return false;
}
if (is_global_input_net() != other.is_global_input_net() || is_global_output_net() != other.is_global_output_net())
{
log_debug("net", "the nets with IDs {} and {} are not equal as one is a global input or output net and the other is not.", m_id, other.get_id());
return false;
}
if (m_sources.size() != other.get_num_of_sources() || m_destinations.size() != other.get_num_of_destinations())
{
log_debug("net", "the nets with IDs {} and {} are not equal due to an unequal number of sources or destinations.", m_id, other.get_id());
return false;
}
const std::vector<Endpoint*>& sources_n2 = other.get_sources();
for (const Endpoint* ep_n1 : m_sources_raw)
{
if (std::find_if(sources_n2.begin(), sources_n2.end(), [ep_n1](const Endpoint* ep_n2) { return *ep_n1->get_pin() == *ep_n2->get_pin() && *ep_n1->get_gate() == *ep_n2->get_gate(); })
== sources_n2.end())
{
log_debug("net", "the nets with IDs {} and {} are not equal due to an unequal source endpoint.", m_id, other.get_id());
return false;
}
}
const std::vector<Endpoint*>& destinations_n2 = other.get_destinations();
for (const Endpoint* ep_n1 : m_destinations_raw)
{
if (std::find_if(
destinations_n2.begin(), destinations_n2.end(), [ep_n1](const Endpoint* ep_n2) { return *ep_n1->get_pin() == *ep_n2->get_pin() && *ep_n1->get_gate() == *ep_n2->get_gate(); })
== destinations_n2.end())
{
log_debug("net", "the nets with IDs {} and {} are not equal due to an unequal destination endpoint.", m_id, other.get_id());
return false;
}
}
if (!DataContainer::operator==(other))
{
log_debug("net", "the nets with IDs {} and {} are not equal due to unequal data.", m_id, other.get_id());
return false;
}
return true;
}
bool Net::operator!=(const Net& other) const
{
return !operator==(other);
}
ssize_t Net::get_hash() const
{
return (uintptr_t)this;
}
u32 Net::get_id() const
{
return m_id;
}
Netlist* Net::get_netlist() const
{
return m_internal_manager->m_netlist;
}
const std::string& Net::get_name() const
{
return m_name;
}
void Net::set_name(const std::string& name)
{
if (utils::trim(name).empty())
{
log_error("net", "net name cannot be empty.");
return;
}
if (name != m_name)
{
m_name = name;
m_event_handler->notify(NetEvent::event::name_changed, this);
}
}
Grouping* Net::get_grouping() const
{
return m_grouping;
}
Endpoint* Net::add_source(Gate* gate, GatePin* pin)
{
return m_internal_manager->net_add_source(this, gate, pin);
}
Endpoint* Net::add_source(Gate* gate, const std::string& pin_name)
{
if (gate == nullptr)
{
log_warning("net", "could not add source to gate: nullptr given for gate");
return nullptr;
}
if (pin_name.empty())
{
log_warning("net", "could not add source to gate '{}' with ID {}: empty string provided as pin name", gate->get_name(), gate->get_id());
return nullptr;
}
GatePin* pin = gate->get_type()->get_pin_by_name(pin_name);
if (pin == nullptr)
{
log_warning("net", "could not add source to gate '{}' with ID {}: no pin with name '{}' exists", gate->get_name(), gate->get_id(), pin_name);
return nullptr;
}
return add_source(gate, pin);
}
bool Net::remove_source(Gate* gate, const GatePin* pin)
{
if (auto it = std::find_if(m_sources_raw.begin(), m_sources_raw.end(), [gate, pin](auto ep) { return ep->get_gate() == gate && *ep->get_pin() == *pin; }); it != m_sources_raw.end())
{
return m_internal_manager->net_remove_source(this, *it);
}
return false;
}
bool Net::remove_source(Gate* gate, const std::string& pin_name)
{
if (gate == nullptr)
{
log_warning("net", "could not remove source from gate: nullptr given for gate");
return false;
}
if (pin_name.empty())
{
log_warning("net", "could not remove source from gate '{}' with ID {}: empty string provided as pin name", gate->get_name(), gate->get_id());
return false;
}
GatePin* pin = gate->get_type()->get_pin_by_name(pin_name);
if (pin == nullptr)
{
log_warning("net", "could not remove source from gate '{}' with ID {}: no pin with name '{}' exists", gate->get_name(), gate->get_id(), pin_name);
return false;
}
return remove_source(gate, pin);
}
bool Net::remove_source(Endpoint* ep)
{
if (ep == nullptr)
{
return false;
}
return m_internal_manager->net_remove_source(this, ep);
}
bool Net::is_a_source(const Gate* gate) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a source: nullptr given for gate");
return false;
}
return std::find_if(m_sources_raw.begin(), m_sources_raw.end(), [gate](const auto* ep) { return ep->get_gate() == gate; }) != m_sources_raw.end();
}
bool Net::is_a_source(const Gate* gate, const GatePin* pin) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a source: nullptr given for gate");
return false;
}
if (pin == nullptr)
{
log_warning("net", "could not check if gate is a source: nullptr given for pin");
return false;
}
return std::find_if(m_sources_raw.begin(), m_sources_raw.end(), [gate, pin](const auto* ep) { return ep->get_gate() == gate && *ep->get_pin() == *pin; }) != m_sources_raw.end();
}
bool Net::is_a_source(const Gate* gate, const std::string& pin_name) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a source: nullptr given for gate");
return false;
}
if (pin_name.empty())
{
log_warning("net", "could not check if gate '{}' with ID {} is a source: empty string provided as pin name", gate->get_name(), gate->get_id());
return false;
}
return is_a_source(gate, gate->get_type()->get_pin_by_name(pin_name));
}
bool Net::is_a_source(const Endpoint* ep) const
{
if (ep == nullptr)
{
return false;
}
if (!ep->is_source_pin())
{
return false;
}
return std::find(m_sources_raw.begin(), m_sources_raw.end(), ep) != m_sources_raw.end();
}
u32 Net::get_num_of_sources(const std::function<bool(Endpoint* ep)>& filter) const
{
if (!filter)
{
return (u32)m_sources_raw.size();
}
u32 num = 0;
for (auto dst : m_sources_raw)
{
if (filter(dst))
{
num++;
}
}
return num;
}
std::vector<Endpoint*> Net::get_sources(const std::function<bool(Endpoint* ep)>& filter) const
{
if (!filter)
{
return m_sources_raw;
}
std::vector<Endpoint*> srcs;
for (auto src : m_sources_raw)
{
if (!filter(src))
{
continue;
}
srcs.push_back(src);
}
return srcs;
}
Endpoint* Net::add_destination(Gate* gate, GatePin* pin)
{
return m_internal_manager->net_add_destination(this, gate, pin);
}
Endpoint* Net::add_destination(Gate* gate, const std::string& pin_name)
{
if (gate == nullptr)
{
log_warning("net", "could not add destination to gate: nullptr given for gate");
return nullptr;
}
if (pin_name.empty())
{
log_warning("net", "could not add destination to gate '{}' with ID {}: empty string provided as pin name", gate->get_name(), gate->get_id());
return nullptr;
}
GatePin* pin = gate->get_type()->get_pin_by_name(pin_name);
if (pin == nullptr)
{
log_warning("net", "could not add destination to gate '{}' with ID {}: no pin with name '{}' exists", gate->get_name(), gate->get_id(), pin_name);
return nullptr;
}
return add_destination(gate, pin);
}
bool Net::remove_destination(Gate* gate, const GatePin* pin)
{
if (auto it = std::find_if(m_destinations_raw.begin(), m_destinations_raw.end(), [gate, pin](const auto* ep) { return ep->get_gate() == gate && *ep->get_pin() == *pin; });
it != m_destinations_raw.end())
{
return m_internal_manager->net_remove_destination(this, *it);
}
return false;
}
bool Net::remove_destination(Gate* gate, const std::string& pin_name)
{
if (gate == nullptr)
{
log_warning("net", "could not remove destination from gate: nullptr given for gate");
return false;
}
if (pin_name.empty())
{
log_warning("net", "could not remove destination from gate '{}' with ID {}: empty string provided as pin name", gate->get_name(), gate->get_id());
return false;
}
GatePin* pin = gate->get_type()->get_pin_by_name(pin_name);
if (pin == nullptr)
{
log_warning("net", "could not remove destination from gate '{}' with ID {}: no pin with name '{}' exists", gate->get_name(), gate->get_id(), pin_name);
return false;
}
return remove_destination(gate, pin);
}
bool Net::remove_destination(Endpoint* ep)
{
if (ep == nullptr)
{
return false;
}
return m_internal_manager->net_remove_destination(this, ep);
}
bool Net::is_a_destination(const Gate* gate) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a destination: nullptr given for gate");
return false;
}
return std::find_if(m_destinations_raw.begin(), m_destinations_raw.end(), [gate](const auto* ep) { return ep->get_gate() == gate; }) != m_destinations_raw.end();
}
bool Net::is_a_destination(const Gate* gate, const GatePin* pin) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a destination: nullptr given for gate");
return false;
}
if (pin == nullptr)
{
log_warning("net", "could not check if gate is a destination: nullptr given for pin");
return false;
}
return std::find_if(m_destinations_raw.begin(), m_destinations_raw.end(), [gate, pin](const auto* ep) { return ep->get_gate() == gate && *ep->get_pin() == *pin; }) != m_destinations_raw.end();
}
bool Net::is_a_destination(const Gate* gate, const std::string& pin_name) const
{
if (gate == nullptr)
{
log_warning("net", "could not check if gate is a destination: nullptr given for gate");
return false;
}
if (pin_name.empty())
{
log_warning("net", "could not check if gate '{}' with ID {} is a destination: empty string provided as pin name", gate->get_name(), gate->get_id());
return false;
}
return is_a_destination(gate, gate->get_type()->get_pin_by_name(pin_name));
}
bool Net::is_a_destination(const Endpoint* ep) const
{
if (ep == nullptr)
{
return false;
}
if (!ep->is_destination_pin())
{
return false;
}
return std::find(m_destinations_raw.begin(), m_destinations_raw.end(), ep) != m_destinations_raw.end();
}
u32 Net::get_num_of_destinations(const std::function<bool(Endpoint* ep)>& filter) const
{
if (!filter)
{
return (u32)m_destinations_raw.size();
}
u32 num = 0;
for (auto dst : m_destinations_raw)
{
if (filter(dst))
{
num++;
}
}
return num;
}
std::vector<Endpoint*> Net::get_destinations(const std::function<bool(Endpoint* ep)>& filter) const
{
if (!filter)
{
return m_destinations_raw;
}
std::vector<Endpoint*> dsts;
for (auto dst : m_destinations_raw)
{
if (!filter(dst))
{
continue;
}
dsts.push_back(dst);
}
return dsts;
}
bool Net::is_unrouted() const
{
return ((m_sources.size() == 0) || (m_destinations.size() == 0));
}
bool Net::is_gnd_net() const
{
return m_sources.size() == 1 && m_sources.front()->get_gate()->is_gnd_gate();
}
bool Net::is_vcc_net() const
{
return m_sources.size() == 1 && m_sources.front()->get_gate()->is_vcc_gate();
}
bool Net::mark_global_input_net()
{
return m_internal_manager->m_netlist->mark_global_input_net(this);
}
bool Net::mark_global_output_net()
{
return m_internal_manager->m_netlist->mark_global_output_net(this);
}
bool Net::unmark_global_input_net()
{
return m_internal_manager->m_netlist->unmark_global_input_net(this);
}
bool Net::unmark_global_output_net()
{
return m_internal_manager->m_netlist->unmark_global_output_net(this);
}
bool Net::is_global_input_net() const
{
return m_internal_manager->m_netlist->is_global_input_net(this);
}
bool Net::is_global_output_net() const
{
return m_internal_manager->m_netlist->is_global_output_net(this);
}
} // namespace hal