Fixed module->addPmux()
[yosys.git] / kernel / rtlil.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/yosys.h"
21 #include "frontends/verilog/verilog_frontend.h"
22 #include "backends/ilang/ilang_backend.h"
23
24 #include <string.h>
25 #include <algorithm>
26
27 YOSYS_NAMESPACE_BEGIN
28
29 std::vector<int> RTLIL::IdString::global_refcount_storage_;
30 std::vector<char*> RTLIL::IdString::global_id_storage_;
31 std::map<char*, int, RTLIL::IdString::char_ptr_cmp> RTLIL::IdString::global_id_index_;
32 std::vector<int> RTLIL::IdString::global_free_idx_list_;
33
34 RTLIL::Const::Const()
35 {
36 flags = RTLIL::CONST_FLAG_NONE;
37 }
38
39 RTLIL::Const::Const(std::string str)
40 {
41 flags = RTLIL::CONST_FLAG_STRING;
42 for (int i = str.size()-1; i >= 0; i--) {
43 unsigned char ch = str[i];
44 for (int j = 0; j < 8; j++) {
45 bits.push_back((ch & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
46 ch = ch >> 1;
47 }
48 }
49 }
50
51 RTLIL::Const::Const(int val, int width)
52 {
53 flags = RTLIL::CONST_FLAG_NONE;
54 for (int i = 0; i < width; i++) {
55 bits.push_back((val & 1) != 0 ? RTLIL::S1 : RTLIL::S0);
56 val = val >> 1;
57 }
58 }
59
60 RTLIL::Const::Const(RTLIL::State bit, int width)
61 {
62 flags = RTLIL::CONST_FLAG_NONE;
63 for (int i = 0; i < width; i++)
64 bits.push_back(bit);
65 }
66
67 bool RTLIL::Const::operator <(const RTLIL::Const &other) const
68 {
69 if (bits.size() != other.bits.size())
70 return bits.size() < other.bits.size();
71 for (size_t i = 0; i < bits.size(); i++)
72 if (bits[i] != other.bits[i])
73 return bits[i] < other.bits[i];
74 return false;
75 }
76
77 bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
78 {
79 return bits == other.bits;
80 }
81
82 bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
83 {
84 return bits != other.bits;
85 }
86
87 bool RTLIL::Const::as_bool() const
88 {
89 for (size_t i = 0; i < bits.size(); i++)
90 if (bits[i] == RTLIL::S1)
91 return true;
92 return false;
93 }
94
95 int RTLIL::Const::as_int(bool is_signed) const
96 {
97 int32_t ret = 0;
98 for (size_t i = 0; i < bits.size() && i < 32; i++)
99 if (bits[i] == RTLIL::S1)
100 ret |= 1 << i;
101 if (is_signed && bits.back() == RTLIL::S1)
102 for (size_t i = bits.size(); i < 32; i++)
103 ret |= 1 << i;
104 return ret;
105 }
106
107 std::string RTLIL::Const::as_string() const
108 {
109 std::string ret;
110 for (size_t i = bits.size(); i > 0; i--)
111 switch (bits[i-1]) {
112 case S0: ret += "0"; break;
113 case S1: ret += "1"; break;
114 case Sx: ret += "x"; break;
115 case Sz: ret += "z"; break;
116 case Sa: ret += "-"; break;
117 case Sm: ret += "m"; break;
118 }
119 return ret;
120 }
121
122 std::string RTLIL::Const::decode_string() const
123 {
124 std::string string;
125 std::vector <char> string_chars;
126 for (int i = 0; i < int (bits.size()); i += 8) {
127 char ch = 0;
128 for (int j = 0; j < 8 && i + j < int (bits.size()); j++)
129 if (bits[i + j] == RTLIL::State::S1)
130 ch |= 1 << j;
131 if (ch != 0)
132 string_chars.push_back(ch);
133 }
134 for (int i = int (string_chars.size()) - 1; i >= 0; i--)
135 string += string_chars[i];
136 return string;
137 }
138
139 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name) const
140 {
141 if (full_selection)
142 return true;
143 if (selected_modules.count(mod_name) > 0)
144 return true;
145 if (selected_members.count(mod_name) > 0)
146 return true;
147 return false;
148 }
149
150 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name) const
151 {
152 if (full_selection)
153 return true;
154 if (selected_modules.count(mod_name) > 0)
155 return true;
156 return false;
157 }
158
159 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
160 {
161 if (full_selection)
162 return true;
163 if (selected_modules.count(mod_name) > 0)
164 return true;
165 if (selected_members.count(mod_name) > 0)
166 if (selected_members.at(mod_name).count(memb_name) > 0)
167 return true;
168 return false;
169 }
170
171 void RTLIL::Selection::optimize(RTLIL::Design *design)
172 {
173 if (full_selection) {
174 selected_modules.clear();
175 selected_members.clear();
176 return;
177 }
178
179 std::vector<RTLIL::IdString> del_list, add_list;
180
181 del_list.clear();
182 for (auto mod_name : selected_modules) {
183 if (design->modules_.count(mod_name) == 0)
184 del_list.push_back(mod_name);
185 selected_members.erase(mod_name);
186 }
187 for (auto mod_name : del_list)
188 selected_modules.erase(mod_name);
189
190 del_list.clear();
191 for (auto &it : selected_members)
192 if (design->modules_.count(it.first) == 0)
193 del_list.push_back(it.first);
194 for (auto mod_name : del_list)
195 selected_members.erase(mod_name);
196
197 for (auto &it : selected_members) {
198 del_list.clear();
199 for (auto memb_name : it.second)
200 if (design->modules_[it.first]->count_id(memb_name) == 0)
201 del_list.push_back(memb_name);
202 for (auto memb_name : del_list)
203 it.second.erase(memb_name);
204 }
205
206 del_list.clear();
207 add_list.clear();
208 for (auto &it : selected_members)
209 if (it.second.size() == 0)
210 del_list.push_back(it.first);
211 else if (it.second.size() == design->modules_[it.first]->wires_.size() + design->modules_[it.first]->memories.size() +
212 design->modules_[it.first]->cells_.size() + design->modules_[it.first]->processes.size())
213 add_list.push_back(it.first);
214 for (auto mod_name : del_list)
215 selected_members.erase(mod_name);
216 for (auto mod_name : add_list) {
217 selected_members.erase(mod_name);
218 selected_modules.insert(mod_name);
219 }
220
221 if (selected_modules.size() == design->modules_.size()) {
222 full_selection = true;
223 selected_modules.clear();
224 selected_members.clear();
225 }
226 }
227
228 RTLIL::Design::Design()
229 {
230 refcount_modules_ = 0;
231 }
232
233 RTLIL::Design::~Design()
234 {
235 for (auto it = modules_.begin(); it != modules_.end(); it++)
236 delete it->second;
237 }
238
239 RTLIL::ObjRange<RTLIL::Module*> RTLIL::Design::modules()
240 {
241 return RTLIL::ObjRange<RTLIL::Module*>(&modules_, &refcount_modules_);
242 }
243
244 RTLIL::Module *RTLIL::Design::module(RTLIL::IdString name)
245 {
246 return modules_.count(name) ? modules_.at(name) : NULL;
247 }
248
249 void RTLIL::Design::add(RTLIL::Module *module)
250 {
251 log_assert(modules_.count(module->name) == 0);
252 log_assert(refcount_modules_ == 0);
253 modules_[module->name] = module;
254 module->design = this;
255
256 for (auto mon : monitors)
257 mon->notify_module_add(module);
258 }
259
260 RTLIL::Module *RTLIL::Design::addModule(RTLIL::IdString name)
261 {
262 log_assert(modules_.count(name) == 0);
263 log_assert(refcount_modules_ == 0);
264
265 RTLIL::Module *module = new RTLIL::Module;
266 modules_[name] = module;
267 module->design = this;
268 module->name = name;
269
270 for (auto mon : monitors)
271 mon->notify_module_add(module);
272
273 return module;
274 }
275
276 void RTLIL::Design::remove(RTLIL::Module *module)
277 {
278 for (auto mon : monitors)
279 mon->notify_module_del(module);
280
281 log_assert(modules_.at(module->name) == module);
282 modules_.erase(module->name);
283 delete module;
284 }
285
286 void RTLIL::Design::check()
287 {
288 #ifndef NDEBUG
289 for (auto &it : modules_) {
290 log_assert(this == it.second->design);
291 log_assert(it.first == it.second->name);
292 log_assert(!it.first.empty());
293 it.second->check();
294 }
295 #endif
296 }
297
298 void RTLIL::Design::optimize()
299 {
300 for (auto &it : modules_)
301 it.second->optimize();
302 for (auto &it : selection_stack)
303 it.optimize(this);
304 for (auto &it : selection_vars)
305 it.second.optimize(this);
306 }
307
308 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name) const
309 {
310 if (!selected_active_module.empty() && mod_name != selected_active_module)
311 return false;
312 if (selection_stack.size() == 0)
313 return true;
314 return selection_stack.back().selected_module(mod_name);
315 }
316
317 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name) const
318 {
319 if (!selected_active_module.empty() && mod_name != selected_active_module)
320 return false;
321 if (selection_stack.size() == 0)
322 return true;
323 return selection_stack.back().selected_whole_module(mod_name);
324 }
325
326 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
327 {
328 if (!selected_active_module.empty() && mod_name != selected_active_module)
329 return false;
330 if (selection_stack.size() == 0)
331 return true;
332 return selection_stack.back().selected_member(mod_name, memb_name);
333 }
334
335 bool RTLIL::Design::selected_module(RTLIL::Module *mod) const
336 {
337 return selected_module(mod->name);
338 }
339
340 bool RTLIL::Design::selected_whole_module(RTLIL::Module *mod) const
341 {
342 return selected_whole_module(mod->name);
343 }
344
345 std::vector<RTLIL::Module*> RTLIL::Design::selected_modules() const
346 {
347 std::vector<RTLIL::Module*> result;
348 result.reserve(modules_.size());
349 for (auto &it : modules_)
350 if (selected_module(it.first))
351 result.push_back(it.second);
352 return result;
353 }
354
355 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules() const
356 {
357 std::vector<RTLIL::Module*> result;
358 result.reserve(modules_.size());
359 for (auto &it : modules_)
360 if (selected_whole_module(it.first))
361 result.push_back(it.second);
362 return result;
363 }
364
365 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules_warn() const
366 {
367 std::vector<RTLIL::Module*> result;
368 result.reserve(modules_.size());
369 for (auto &it : modules_)
370 if (selected_whole_module(it.first))
371 result.push_back(it.second);
372 else if (selected_module(it.first))
373 log("Warning: Ignoring partially selected module %s.\n", log_id(it.first));
374 return result;
375 }
376
377 RTLIL::Module::Module()
378 {
379 design = nullptr;
380 refcount_wires_ = 0;
381 refcount_cells_ = 0;
382 }
383
384 RTLIL::Module::~Module()
385 {
386 for (auto it = wires_.begin(); it != wires_.end(); it++)
387 delete it->second;
388 for (auto it = memories.begin(); it != memories.end(); it++)
389 delete it->second;
390 for (auto it = cells_.begin(); it != cells_.end(); it++)
391 delete it->second;
392 for (auto it = processes.begin(); it != processes.end(); it++)
393 delete it->second;
394 }
395
396 RTLIL::IdString RTLIL::Module::derive(RTLIL::Design*, std::map<RTLIL::IdString, RTLIL::Const>)
397 {
398 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name));
399 }
400
401 size_t RTLIL::Module::count_id(RTLIL::IdString id)
402 {
403 return wires_.count(id) + memories.count(id) + cells_.count(id) + processes.count(id);
404 }
405
406 #ifndef NDEBUG
407 namespace {
408 struct InternalCellChecker
409 {
410 RTLIL::Module *module;
411 RTLIL::Cell *cell;
412 std::set<RTLIL::IdString> expected_params, expected_ports;
413
414 InternalCellChecker(RTLIL::Module *module, RTLIL::Cell *cell) : module(module), cell(cell) { }
415
416 void error(int linenr)
417 {
418 std::stringstream buf;
419 ILANG_BACKEND::dump_cell(buf, " ", cell);
420
421 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
422 module ? module->name.c_str() : "", module ? "." : "",
423 cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, buf.str().c_str());
424 }
425
426 int param(const char *name)
427 {
428 if (cell->parameters.count(name) == 0)
429 error(__LINE__);
430 expected_params.insert(name);
431 return cell->parameters.at(name).as_int();
432 }
433
434 int param_bool(const char *name)
435 {
436 int v = param(name);
437 if (cell->parameters.at(name).bits.size() > 32)
438 error(__LINE__);
439 if (v != 0 && v != 1)
440 error(__LINE__);
441 return v;
442 }
443
444 void param_bits(const char *name, int width)
445 {
446 param(name);
447 if (int(cell->parameters.at(name).bits.size()) != width)
448 error(__LINE__);
449 }
450
451 void port(const char *name, int width)
452 {
453 if (!cell->hasPort(name))
454 error(__LINE__);
455 if (cell->getPort(name).size() != width)
456 error(__LINE__);
457 expected_ports.insert(name);
458 }
459
460 void check_expected(bool check_matched_sign = true)
461 {
462 for (auto &para : cell->parameters)
463 if (expected_params.count(para.first) == 0)
464 error(__LINE__);
465 for (auto &conn : cell->connections())
466 if (expected_ports.count(conn.first) == 0)
467 error(__LINE__);
468
469 if (expected_params.count("\\A_SIGNED") != 0 && expected_params.count("\\B_SIGNED") && check_matched_sign) {
470 bool a_is_signed = param("\\A_SIGNED") != 0;
471 bool b_is_signed = param("\\B_SIGNED") != 0;
472 if (a_is_signed != b_is_signed)
473 error(__LINE__);
474 }
475 }
476
477 void check_gate(const char *ports)
478 {
479 if (cell->parameters.size() != 0)
480 error(__LINE__);
481
482 for (const char *p = ports; *p; p++) {
483 char portname[3] = { '\\', *p, 0 };
484 if (!cell->hasPort(portname))
485 error(__LINE__);
486 if (cell->getPort(portname).size() != 1)
487 error(__LINE__);
488 }
489
490 for (auto &conn : cell->connections()) {
491 if (conn.first.size() != 2 || conn.first[0] != '\\')
492 error(__LINE__);
493 if (strchr(ports, conn.first[1]) == NULL)
494 error(__LINE__);
495 }
496 }
497
498 void check()
499 {
500 if (cell->type.substr(0, 1) != "$" || cell->type.substr(0, 3) == "$__" || cell->type.substr(0, 8) == "$paramod" ||
501 cell->type.substr(0, 9) == "$verific$" || cell->type.substr(0, 7) == "$array:" || cell->type.substr(0, 8) == "$extern:")
502 return;
503
504 if (cell->type.in("$not", "$pos", "$bu0", "$neg")) {
505 param_bool("\\A_SIGNED");
506 port("\\A", param("\\A_WIDTH"));
507 port("\\Y", param("\\Y_WIDTH"));
508 check_expected();
509 return;
510 }
511
512 if (cell->type.in("$and", "$or", "$xor", "$xnor")) {
513 param_bool("\\A_SIGNED");
514 param_bool("\\B_SIGNED");
515 port("\\A", param("\\A_WIDTH"));
516 port("\\B", param("\\B_WIDTH"));
517 port("\\Y", param("\\Y_WIDTH"));
518 check_expected();
519 return;
520 }
521
522 if (cell->type.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
523 param_bool("\\A_SIGNED");
524 port("\\A", param("\\A_WIDTH"));
525 port("\\Y", param("\\Y_WIDTH"));
526 check_expected();
527 return;
528 }
529
530 if (cell->type.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
531 param_bool("\\A_SIGNED");
532 param_bool("\\B_SIGNED");
533 port("\\A", param("\\A_WIDTH"));
534 port("\\B", param("\\B_WIDTH"));
535 port("\\Y", param("\\Y_WIDTH"));
536 check_expected(false);
537 return;
538 }
539
540 if (cell->type.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
541 param_bool("\\A_SIGNED");
542 param_bool("\\B_SIGNED");
543 port("\\A", param("\\A_WIDTH"));
544 port("\\B", param("\\B_WIDTH"));
545 port("\\Y", param("\\Y_WIDTH"));
546 check_expected();
547 return;
548 }
549
550 if (cell->type.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
551 param_bool("\\A_SIGNED");
552 param_bool("\\B_SIGNED");
553 port("\\A", param("\\A_WIDTH"));
554 port("\\B", param("\\B_WIDTH"));
555 port("\\Y", param("\\Y_WIDTH"));
556 check_expected(cell->type != "$pow");
557 return;
558 }
559
560 if (cell->type == "$logic_not") {
561 param_bool("\\A_SIGNED");
562 port("\\A", param("\\A_WIDTH"));
563 port("\\Y", param("\\Y_WIDTH"));
564 check_expected();
565 return;
566 }
567
568 if (cell->type == "$logic_and" || cell->type == "$logic_or") {
569 param_bool("\\A_SIGNED");
570 param_bool("\\B_SIGNED");
571 port("\\A", param("\\A_WIDTH"));
572 port("\\B", param("\\B_WIDTH"));
573 port("\\Y", param("\\Y_WIDTH"));
574 check_expected(false);
575 return;
576 }
577
578 if (cell->type == "$slice") {
579 param("\\OFFSET");
580 port("\\A", param("\\A_WIDTH"));
581 port("\\Y", param("\\Y_WIDTH"));
582 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
583 error(__LINE__);
584 check_expected();
585 return;
586 }
587
588 if (cell->type == "$concat") {
589 port("\\A", param("\\A_WIDTH"));
590 port("\\B", param("\\B_WIDTH"));
591 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
592 check_expected();
593 return;
594 }
595
596 if (cell->type == "$mux") {
597 port("\\A", param("\\WIDTH"));
598 port("\\B", param("\\WIDTH"));
599 port("\\S", 1);
600 port("\\Y", param("\\WIDTH"));
601 check_expected();
602 return;
603 }
604
605 if (cell->type == "$pmux") {
606 port("\\A", param("\\WIDTH"));
607 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
608 port("\\S", param("\\S_WIDTH"));
609 port("\\Y", param("\\WIDTH"));
610 check_expected();
611 return;
612 }
613
614 if (cell->type == "$lut") {
615 param("\\LUT");
616 port("\\A", param("\\WIDTH"));
617 port("\\Y", 1);
618 check_expected();
619 return;
620 }
621
622 if (cell->type == "$sr") {
623 param_bool("\\SET_POLARITY");
624 param_bool("\\CLR_POLARITY");
625 port("\\SET", param("\\WIDTH"));
626 port("\\CLR", param("\\WIDTH"));
627 port("\\Q", param("\\WIDTH"));
628 check_expected();
629 return;
630 }
631
632 if (cell->type == "$dff") {
633 param_bool("\\CLK_POLARITY");
634 port("\\CLK", 1);
635 port("\\D", param("\\WIDTH"));
636 port("\\Q", param("\\WIDTH"));
637 check_expected();
638 return;
639 }
640
641 if (cell->type == "$dffsr") {
642 param_bool("\\CLK_POLARITY");
643 param_bool("\\SET_POLARITY");
644 param_bool("\\CLR_POLARITY");
645 port("\\CLK", 1);
646 port("\\SET", param("\\WIDTH"));
647 port("\\CLR", param("\\WIDTH"));
648 port("\\D", param("\\WIDTH"));
649 port("\\Q", param("\\WIDTH"));
650 check_expected();
651 return;
652 }
653
654 if (cell->type == "$adff") {
655 param_bool("\\CLK_POLARITY");
656 param_bool("\\ARST_POLARITY");
657 param_bits("\\ARST_VALUE", param("\\WIDTH"));
658 port("\\CLK", 1);
659 port("\\ARST", 1);
660 port("\\D", param("\\WIDTH"));
661 port("\\Q", param("\\WIDTH"));
662 check_expected();
663 return;
664 }
665
666 if (cell->type == "$dlatch") {
667 param_bool("\\EN_POLARITY");
668 port("\\EN", 1);
669 port("\\D", param("\\WIDTH"));
670 port("\\Q", param("\\WIDTH"));
671 check_expected();
672 return;
673 }
674
675 if (cell->type == "$dlatchsr") {
676 param_bool("\\EN_POLARITY");
677 param_bool("\\SET_POLARITY");
678 param_bool("\\CLR_POLARITY");
679 port("\\EN", 1);
680 port("\\SET", param("\\WIDTH"));
681 port("\\CLR", param("\\WIDTH"));
682 port("\\D", param("\\WIDTH"));
683 port("\\Q", param("\\WIDTH"));
684 check_expected();
685 return;
686 }
687
688 if (cell->type == "$fsm") {
689 param("\\NAME");
690 param_bool("\\CLK_POLARITY");
691 param_bool("\\ARST_POLARITY");
692 param("\\STATE_BITS");
693 param("\\STATE_NUM");
694 param("\\STATE_NUM_LOG2");
695 param("\\STATE_RST");
696 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
697 param("\\TRANS_NUM");
698 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
699 port("\\CLK", 1);
700 port("\\ARST", 1);
701 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
702 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
703 check_expected();
704 return;
705 }
706
707 if (cell->type == "$memrd") {
708 param("\\MEMID");
709 param_bool("\\CLK_ENABLE");
710 param_bool("\\CLK_POLARITY");
711 param_bool("\\TRANSPARENT");
712 port("\\CLK", 1);
713 port("\\ADDR", param("\\ABITS"));
714 port("\\DATA", param("\\WIDTH"));
715 check_expected();
716 return;
717 }
718
719 if (cell->type == "$memwr") {
720 param("\\MEMID");
721 param_bool("\\CLK_ENABLE");
722 param_bool("\\CLK_POLARITY");
723 param("\\PRIORITY");
724 port("\\CLK", 1);
725 port("\\EN", param("\\WIDTH"));
726 port("\\ADDR", param("\\ABITS"));
727 port("\\DATA", param("\\WIDTH"));
728 check_expected();
729 return;
730 }
731
732 if (cell->type == "$mem") {
733 param("\\MEMID");
734 param("\\SIZE");
735 param("\\OFFSET");
736 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
737 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
738 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
739 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
740 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
741 port("\\RD_CLK", param("\\RD_PORTS"));
742 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
743 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
744 port("\\WR_CLK", param("\\WR_PORTS"));
745 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
746 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
747 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
748 check_expected();
749 return;
750 }
751
752 if (cell->type == "$assert") {
753 port("\\A", 1);
754 port("\\EN", 1);
755 check_expected();
756 return;
757 }
758
759 if (cell->type == "$_NOT_") { check_gate("AY"); return; }
760 if (cell->type == "$_AND_") { check_gate("ABY"); return; }
761 if (cell->type == "$_NAND_") { check_gate("ABY"); return; }
762 if (cell->type == "$_OR_") { check_gate("ABY"); return; }
763 if (cell->type == "$_NOR_") { check_gate("ABY"); return; }
764 if (cell->type == "$_XOR_") { check_gate("ABY"); return; }
765 if (cell->type == "$_XNOR_") { check_gate("ABY"); return; }
766 if (cell->type == "$_MUX_") { check_gate("ABSY"); return; }
767 if (cell->type == "$_AOI3_") { check_gate("ABCY"); return; }
768 if (cell->type == "$_OAI3_") { check_gate("ABCY"); return; }
769 if (cell->type == "$_AOI4_") { check_gate("ABCDY"); return; }
770 if (cell->type == "$_OAI4_") { check_gate("ABCDY"); return; }
771
772 if (cell->type == "$_SR_NN_") { check_gate("SRQ"); return; }
773 if (cell->type == "$_SR_NP_") { check_gate("SRQ"); return; }
774 if (cell->type == "$_SR_PN_") { check_gate("SRQ"); return; }
775 if (cell->type == "$_SR_PP_") { check_gate("SRQ"); return; }
776
777 if (cell->type == "$_DFF_N_") { check_gate("DQC"); return; }
778 if (cell->type == "$_DFF_P_") { check_gate("DQC"); return; }
779
780 if (cell->type == "$_DFF_NN0_") { check_gate("DQCR"); return; }
781 if (cell->type == "$_DFF_NN1_") { check_gate("DQCR"); return; }
782 if (cell->type == "$_DFF_NP0_") { check_gate("DQCR"); return; }
783 if (cell->type == "$_DFF_NP1_") { check_gate("DQCR"); return; }
784 if (cell->type == "$_DFF_PN0_") { check_gate("DQCR"); return; }
785 if (cell->type == "$_DFF_PN1_") { check_gate("DQCR"); return; }
786 if (cell->type == "$_DFF_PP0_") { check_gate("DQCR"); return; }
787 if (cell->type == "$_DFF_PP1_") { check_gate("DQCR"); return; }
788
789 if (cell->type == "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
790 if (cell->type == "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
791 if (cell->type == "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
792 if (cell->type == "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
793 if (cell->type == "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
794 if (cell->type == "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
795 if (cell->type == "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
796 if (cell->type == "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
797
798 if (cell->type == "$_DLATCH_N_") { check_gate("EDQ"); return; }
799 if (cell->type == "$_DLATCH_P_") { check_gate("EDQ"); return; }
800
801 if (cell->type == "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
802 if (cell->type == "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
803 if (cell->type == "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
804 if (cell->type == "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
805 if (cell->type == "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
806 if (cell->type == "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
807 if (cell->type == "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
808 if (cell->type == "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
809
810 error(__LINE__);
811 }
812 };
813 }
814 #endif
815
816 void RTLIL::Module::check()
817 {
818 #ifndef NDEBUG
819 std::vector<bool> ports_declared;
820 for (auto &it : wires_) {
821 log_assert(this == it.second->module);
822 log_assert(it.first == it.second->name);
823 log_assert(!it.first.empty());
824 log_assert(it.second->width >= 0);
825 log_assert(it.second->port_id >= 0);
826 for (auto &it2 : it.second->attributes)
827 log_assert(!it2.first.empty());
828 if (it.second->port_id) {
829 log_assert(SIZE(ports) >= it.second->port_id);
830 log_assert(ports.at(it.second->port_id-1) == it.first);
831 log_assert(it.second->port_input || it.second->port_output);
832 if (SIZE(ports_declared) < it.second->port_id)
833 ports_declared.resize(it.second->port_id);
834 log_assert(ports_declared[it.second->port_id-1] == false);
835 ports_declared[it.second->port_id-1] = true;
836 } else
837 log_assert(!it.second->port_input && !it.second->port_output);
838 }
839 for (auto port_declared : ports_declared)
840 log_assert(port_declared == true);
841 log_assert(SIZE(ports) == SIZE(ports_declared));
842
843 for (auto &it : memories) {
844 log_assert(it.first == it.second->name);
845 log_assert(!it.first.empty());
846 log_assert(it.second->width >= 0);
847 log_assert(it.second->size >= 0);
848 for (auto &it2 : it.second->attributes)
849 log_assert(!it2.first.empty());
850 }
851
852 for (auto &it : cells_) {
853 log_assert(this == it.second->module);
854 log_assert(it.first == it.second->name);
855 log_assert(!it.first.empty());
856 log_assert(!it.second->type.empty());
857 for (auto &it2 : it.second->connections()) {
858 log_assert(!it2.first.empty());
859 it2.second.check();
860 }
861 for (auto &it2 : it.second->attributes)
862 log_assert(!it2.first.empty());
863 for (auto &it2 : it.second->parameters)
864 log_assert(!it2.first.empty());
865 InternalCellChecker checker(this, it.second);
866 checker.check();
867 }
868
869 for (auto &it : processes) {
870 log_assert(it.first == it.second->name);
871 log_assert(!it.first.empty());
872 // FIXME: More checks here..
873 }
874
875 for (auto &it : connections_) {
876 log_assert(it.first.size() == it.second.size());
877 it.first.check();
878 it.second.check();
879 }
880
881 for (auto &it : attributes)
882 log_assert(!it.first.empty());
883 #endif
884 }
885
886 void RTLIL::Module::optimize()
887 {
888 }
889
890 void RTLIL::Module::cloneInto(RTLIL::Module *new_mod) const
891 {
892 log_assert(new_mod->refcount_wires_ == 0);
893 log_assert(new_mod->refcount_cells_ == 0);
894
895 new_mod->connections_ = connections_;
896 new_mod->attributes = attributes;
897
898 for (auto &it : wires_)
899 new_mod->addWire(it.first, it.second);
900
901 for (auto &it : memories)
902 new_mod->memories[it.first] = new RTLIL::Memory(*it.second);
903
904 for (auto &it : cells_)
905 new_mod->addCell(it.first, it.second);
906
907 for (auto &it : processes)
908 new_mod->processes[it.first] = it.second->clone();
909
910 struct RewriteSigSpecWorker
911 {
912 RTLIL::Module *mod;
913 void operator()(RTLIL::SigSpec &sig)
914 {
915 std::vector<RTLIL::SigChunk> chunks = sig.chunks();
916 for (auto &c : chunks)
917 if (c.wire != NULL)
918 c.wire = mod->wires_.at(c.wire->name);
919 sig = chunks;
920 }
921 };
922
923 RewriteSigSpecWorker rewriteSigSpecWorker;
924 rewriteSigSpecWorker.mod = new_mod;
925 new_mod->rewrite_sigspecs(rewriteSigSpecWorker);
926 new_mod->fixup_ports();
927 }
928
929 RTLIL::Module *RTLIL::Module::clone() const
930 {
931 RTLIL::Module *new_mod = new RTLIL::Module;
932 new_mod->name = name;
933 cloneInto(new_mod);
934 return new_mod;
935 }
936
937 bool RTLIL::Module::has_memories() const
938 {
939 return !memories.empty();
940 }
941
942 bool RTLIL::Module::has_processes() const
943 {
944 return !processes.empty();
945 }
946
947 bool RTLIL::Module::has_memories_warn() const
948 {
949 if (!memories.empty())
950 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
951 return !memories.empty();
952 }
953
954 bool RTLIL::Module::has_processes_warn() const
955 {
956 if (!processes.empty())
957 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
958 return !processes.empty();
959 }
960
961 std::vector<RTLIL::Wire*> RTLIL::Module::selected_wires() const
962 {
963 std::vector<RTLIL::Wire*> result;
964 result.reserve(wires_.size());
965 for (auto &it : wires_)
966 if (design->selected(this, it.second))
967 result.push_back(it.second);
968 return result;
969 }
970
971 std::vector<RTLIL::Cell*> RTLIL::Module::selected_cells() const
972 {
973 std::vector<RTLIL::Cell*> result;
974 result.reserve(wires_.size());
975 for (auto &it : cells_)
976 if (design->selected(this, it.second))
977 result.push_back(it.second);
978 return result;
979 }
980
981 void RTLIL::Module::add(RTLIL::Wire *wire)
982 {
983 log_assert(!wire->name.empty());
984 log_assert(count_id(wire->name) == 0);
985 log_assert(refcount_wires_ == 0);
986 wires_[wire->name] = wire;
987 wire->module = this;
988 }
989
990 void RTLIL::Module::add(RTLIL::Cell *cell)
991 {
992 log_assert(!cell->name.empty());
993 log_assert(count_id(cell->name) == 0);
994 log_assert(refcount_cells_ == 0);
995 cells_[cell->name] = cell;
996 cell->module = this;
997 }
998
999 namespace {
1000 struct DeleteWireWorker
1001 {
1002 RTLIL::Module *module;
1003 const std::set<RTLIL::Wire*> *wires_p;
1004
1005 void operator()(RTLIL::SigSpec &sig) {
1006 std::vector<RTLIL::SigChunk> chunks = sig;
1007 for (auto &c : chunks)
1008 if (c.wire != NULL && wires_p->count(c.wire)) {
1009 c.wire = module->addWire(NEW_ID, c.width);
1010 c.offset = 0;
1011 }
1012 sig = chunks;
1013 }
1014 };
1015 }
1016
1017 #if 0
1018 void RTLIL::Module::remove(RTLIL::Wire *wire)
1019 {
1020 std::setPort<RTLIL::Wire*> wires_;
1021 wires_.insert(wire);
1022 remove(wires_);
1023 }
1024 #endif
1025
1026 void RTLIL::Module::remove(const std::set<RTLIL::Wire*> &wires)
1027 {
1028 log_assert(refcount_wires_ == 0);
1029
1030 DeleteWireWorker delete_wire_worker;
1031 delete_wire_worker.module = this;
1032 delete_wire_worker.wires_p = &wires;
1033 rewrite_sigspecs(delete_wire_worker);
1034
1035 for (auto &it : wires) {
1036 log_assert(wires_.count(it->name) != 0);
1037 wires_.erase(it->name);
1038 delete it;
1039 }
1040 }
1041
1042 void RTLIL::Module::remove(RTLIL::Cell *cell)
1043 {
1044 log_assert(cells_.count(cell->name) != 0);
1045 log_assert(refcount_cells_ == 0);
1046 cells_.erase(cell->name);
1047 delete cell;
1048 }
1049
1050 void RTLIL::Module::rename(RTLIL::Wire *wire, RTLIL::IdString new_name)
1051 {
1052 log_assert(wires_[wire->name] == wire);
1053 log_assert(refcount_wires_ == 0);
1054 wires_.erase(wire->name);
1055 wire->name = new_name;
1056 add(wire);
1057 }
1058
1059 void RTLIL::Module::rename(RTLIL::Cell *cell, RTLIL::IdString new_name)
1060 {
1061 log_assert(cells_[cell->name] == cell);
1062 log_assert(refcount_wires_ == 0);
1063 cells_.erase(cell->name);
1064 cell->name = new_name;
1065 add(cell);
1066 }
1067
1068 void RTLIL::Module::rename(RTLIL::IdString old_name, RTLIL::IdString new_name)
1069 {
1070 log_assert(count_id(old_name) != 0);
1071 if (wires_.count(old_name))
1072 rename(wires_.at(old_name), new_name);
1073 else if (cells_.count(old_name))
1074 rename(cells_.at(old_name), new_name);
1075 else
1076 log_abort();
1077 }
1078
1079 void RTLIL::Module::swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2)
1080 {
1081 log_assert(wires_[w1->name] == w1);
1082 log_assert(wires_[w2->name] == w2);
1083 log_assert(refcount_wires_ == 0);
1084
1085 wires_.erase(w1->name);
1086 wires_.erase(w2->name);
1087
1088 std::swap(w1->name, w2->name);
1089
1090 wires_[w1->name] = w1;
1091 wires_[w2->name] = w2;
1092 }
1093
1094 void RTLIL::Module::swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2)
1095 {
1096 log_assert(cells_[c1->name] == c1);
1097 log_assert(cells_[c2->name] == c2);
1098 log_assert(refcount_cells_ == 0);
1099
1100 cells_.erase(c1->name);
1101 cells_.erase(c2->name);
1102
1103 std::swap(c1->name, c2->name);
1104
1105 cells_[c1->name] = c1;
1106 cells_[c2->name] = c2;
1107 }
1108
1109 RTLIL::IdString RTLIL::Module::uniquify(RTLIL::IdString name)
1110 {
1111 int index = 0;
1112 return uniquify(name, index);
1113 }
1114
1115 RTLIL::IdString RTLIL::Module::uniquify(RTLIL::IdString name, int &index)
1116 {
1117 if (index == 0) {
1118 if (count_id(name) == 0)
1119 return name;
1120 index++;
1121 }
1122
1123 while (1) {
1124 RTLIL::IdString new_name = stringf("%s_%d", name.c_str(), index);
1125 if (count_id(new_name) == 0)
1126 return new_name;
1127 index++;
1128 }
1129 }
1130
1131 static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
1132 {
1133 if (a->port_id && !b->port_id)
1134 return true;
1135 if (!a->port_id && b->port_id)
1136 return false;
1137
1138 if (a->port_id == b->port_id)
1139 return a->name < b->name;
1140 return a->port_id < b->port_id;
1141 }
1142
1143 void RTLIL::Module::connect(const RTLIL::SigSig &conn)
1144 {
1145 for (auto mon : monitors)
1146 mon->notify_connect(this, conn);
1147
1148 if (design)
1149 for (auto mon : design->monitors)
1150 mon->notify_connect(this, conn);
1151
1152 connections_.push_back(conn);
1153 }
1154
1155 void RTLIL::Module::connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs)
1156 {
1157 connect(RTLIL::SigSig(lhs, rhs));
1158 }
1159
1160 void RTLIL::Module::new_connections(const std::vector<RTLIL::SigSig> &new_conn)
1161 {
1162 for (auto mon : monitors)
1163 mon->notify_connect(this, new_conn);
1164
1165 if (design)
1166 for (auto mon : design->monitors)
1167 mon->notify_connect(this, new_conn);
1168
1169 connections_ = new_conn;
1170 }
1171
1172 const std::vector<RTLIL::SigSig> &RTLIL::Module::connections() const
1173 {
1174 return connections_;
1175 }
1176
1177 void RTLIL::Module::fixup_ports()
1178 {
1179 std::vector<RTLIL::Wire*> all_ports;
1180
1181 for (auto &w : wires_)
1182 if (w.second->port_input || w.second->port_output)
1183 all_ports.push_back(w.second);
1184 else
1185 w.second->port_id = 0;
1186
1187 std::sort(all_ports.begin(), all_ports.end(), fixup_ports_compare);
1188
1189 ports.clear();
1190 for (size_t i = 0; i < all_ports.size(); i++) {
1191 ports.push_back(all_ports[i]->name);
1192 all_ports[i]->port_id = i+1;
1193 }
1194 }
1195
1196 RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, int width)
1197 {
1198 RTLIL::Wire *wire = new RTLIL::Wire;
1199 wire->name = name;
1200 wire->width = width;
1201 add(wire);
1202 return wire;
1203 }
1204
1205 RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, const RTLIL::Wire *other)
1206 {
1207 RTLIL::Wire *wire = addWire(name);
1208 wire->width = other->width;
1209 wire->start_offset = other->start_offset;
1210 wire->port_id = other->port_id;
1211 wire->port_input = other->port_input;
1212 wire->port_output = other->port_output;
1213 wire->upto = other->upto;
1214 wire->attributes = other->attributes;
1215 return wire;
1216 }
1217
1218 RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, RTLIL::IdString type)
1219 {
1220 RTLIL::Cell *cell = new RTLIL::Cell;
1221 cell->name = name;
1222 cell->type = type;
1223 add(cell);
1224 return cell;
1225 }
1226
1227 RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, const RTLIL::Cell *other)
1228 {
1229 RTLIL::Cell *cell = addCell(name, other->type);
1230 cell->connections_ = other->connections_;
1231 cell->parameters = other->parameters;
1232 cell->attributes = other->attributes;
1233 return cell;
1234 }
1235
1236 #define DEF_METHOD(_func, _y_size, _type) \
1237 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1238 RTLIL::Cell *cell = addCell(name, _type); \
1239 cell->parameters["\\A_SIGNED"] = is_signed; \
1240 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1241 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1242 cell->setPort("\\A", sig_a); \
1243 cell->setPort("\\Y", sig_y); \
1244 return cell; \
1245 } \
1246 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1247 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1248 add ## _func(name, sig_a, sig_y, is_signed); \
1249 return sig_y; \
1250 }
1251 DEF_METHOD(Not, sig_a.size(), "$not")
1252 DEF_METHOD(Pos, sig_a.size(), "$pos")
1253 DEF_METHOD(Bu0, sig_a.size(), "$bu0")
1254 DEF_METHOD(Neg, sig_a.size(), "$neg")
1255 DEF_METHOD(ReduceAnd, 1, "$reduce_and")
1256 DEF_METHOD(ReduceOr, 1, "$reduce_or")
1257 DEF_METHOD(ReduceXor, 1, "$reduce_xor")
1258 DEF_METHOD(ReduceXnor, 1, "$reduce_xnor")
1259 DEF_METHOD(ReduceBool, 1, "$reduce_bool")
1260 DEF_METHOD(LogicNot, 1, "$logic_not")
1261 #undef DEF_METHOD
1262
1263 #define DEF_METHOD(_func, _y_size, _type) \
1264 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1265 RTLIL::Cell *cell = addCell(name, _type); \
1266 cell->parameters["\\A_SIGNED"] = is_signed; \
1267 cell->parameters["\\B_SIGNED"] = is_signed; \
1268 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1269 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1270 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1271 cell->setPort("\\A", sig_a); \
1272 cell->setPort("\\B", sig_b); \
1273 cell->setPort("\\Y", sig_y); \
1274 return cell; \
1275 } \
1276 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1277 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1278 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1279 return sig_y; \
1280 }
1281 DEF_METHOD(And, std::max(sig_a.size(), sig_b.size()), "$and")
1282 DEF_METHOD(Or, std::max(sig_a.size(), sig_b.size()), "$or")
1283 DEF_METHOD(Xor, std::max(sig_a.size(), sig_b.size()), "$xor")
1284 DEF_METHOD(Xnor, std::max(sig_a.size(), sig_b.size()), "$xnor")
1285 DEF_METHOD(Shl, sig_a.size(), "$shl")
1286 DEF_METHOD(Shr, sig_a.size(), "$shr")
1287 DEF_METHOD(Sshl, sig_a.size(), "$sshl")
1288 DEF_METHOD(Sshr, sig_a.size(), "$sshr")
1289 DEF_METHOD(Shift, sig_a.size(), "$shift")
1290 DEF_METHOD(Shiftx, sig_a.size(), "$shiftx")
1291 DEF_METHOD(Lt, 1, "$lt")
1292 DEF_METHOD(Le, 1, "$le")
1293 DEF_METHOD(Eq, 1, "$eq")
1294 DEF_METHOD(Ne, 1, "$ne")
1295 DEF_METHOD(Eqx, 1, "$eqx")
1296 DEF_METHOD(Nex, 1, "$nex")
1297 DEF_METHOD(Ge, 1, "$ge")
1298 DEF_METHOD(Gt, 1, "$gt")
1299 DEF_METHOD(Add, std::max(sig_a.size(), sig_b.size()), "$add")
1300 DEF_METHOD(Sub, std::max(sig_a.size(), sig_b.size()), "$sub")
1301 DEF_METHOD(Mul, std::max(sig_a.size(), sig_b.size()), "$mul")
1302 DEF_METHOD(Div, std::max(sig_a.size(), sig_b.size()), "$div")
1303 DEF_METHOD(Mod, std::max(sig_a.size(), sig_b.size()), "$mod")
1304 DEF_METHOD(LogicAnd, 1, "$logic_and")
1305 DEF_METHOD(LogicOr, 1, "$logic_or")
1306 #undef DEF_METHOD
1307
1308 #define DEF_METHOD(_func, _type, _pmux) \
1309 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y) { \
1310 RTLIL::Cell *cell = addCell(name, _type); \
1311 cell->parameters["\\WIDTH"] = sig_a.size(); \
1312 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1313 cell->setPort("\\A", sig_a); \
1314 cell->setPort("\\B", sig_b); \
1315 cell->setPort("\\S", sig_s); \
1316 cell->setPort("\\Y", sig_y); \
1317 return cell; \
1318 } \
1319 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1320 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1321 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1322 return sig_y; \
1323 }
1324 DEF_METHOD(Mux, "$mux", 0)
1325 DEF_METHOD(Pmux, "$pmux", 1)
1326 #undef DEF_METHOD
1327
1328 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1329 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1330 RTLIL::Cell *cell = addCell(name, _type); \
1331 cell->setPort("\\" #_P1, sig1); \
1332 cell->setPort("\\" #_P2, sig2); \
1333 return cell; \
1334 } \
1335 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1336 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1337 add ## _func(name, sig1, sig2); \
1338 return sig2; \
1339 }
1340 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1341 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1342 RTLIL::Cell *cell = addCell(name, _type); \
1343 cell->setPort("\\" #_P1, sig1); \
1344 cell->setPort("\\" #_P2, sig2); \
1345 cell->setPort("\\" #_P3, sig3); \
1346 return cell; \
1347 } \
1348 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1349 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1350 add ## _func(name, sig1, sig2, sig3); \
1351 return sig3; \
1352 }
1353 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1354 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1355 RTLIL::Cell *cell = addCell(name, _type); \
1356 cell->setPort("\\" #_P1, sig1); \
1357 cell->setPort("\\" #_P2, sig2); \
1358 cell->setPort("\\" #_P3, sig3); \
1359 cell->setPort("\\" #_P4, sig4); \
1360 return cell; \
1361 } \
1362 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1363 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1364 add ## _func(name, sig1, sig2, sig3, sig4); \
1365 return sig4; \
1366 }
1367 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1368 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1369 RTLIL::Cell *cell = addCell(name, _type); \
1370 cell->setPort("\\" #_P1, sig1); \
1371 cell->setPort("\\" #_P2, sig2); \
1372 cell->setPort("\\" #_P3, sig3); \
1373 cell->setPort("\\" #_P4, sig4); \
1374 cell->setPort("\\" #_P5, sig5); \
1375 return cell; \
1376 } \
1377 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1378 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1379 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1380 return sig5; \
1381 }
1382 DEF_METHOD_2(NotGate, "$_NOT_", A, Y)
1383 DEF_METHOD_3(AndGate, "$_AND_", A, B, Y)
1384 DEF_METHOD_3(NandGate, "$_NAND_", A, B, Y)
1385 DEF_METHOD_3(OrGate, "$_OR_", A, B, Y)
1386 DEF_METHOD_3(NorGate, "$_NOR_", A, B, Y)
1387 DEF_METHOD_3(XorGate, "$_XOR_", A, B, Y)
1388 DEF_METHOD_3(XnorGate, "$_XNOR_", A, B, Y)
1389 DEF_METHOD_4(MuxGate, "$_MUX_", A, B, S, Y)
1390 DEF_METHOD_4(Aoi3Gate, "$_AOI3_", A, B, C, Y)
1391 DEF_METHOD_4(Oai3Gate, "$_OAI3_", A, B, C, Y)
1392 DEF_METHOD_5(Aoi4Gate, "$_AOI4_", A, B, C, D, Y)
1393 DEF_METHOD_5(Oai4Gate, "$_OAI4_", A, B, C, D, Y)
1394 #undef DEF_METHOD_2
1395 #undef DEF_METHOD_3
1396 #undef DEF_METHOD_4
1397 #undef DEF_METHOD_5
1398
1399 RTLIL::Cell* RTLIL::Module::addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed, bool b_signed)
1400 {
1401 RTLIL::Cell *cell = addCell(name, "$pow");
1402 cell->parameters["\\A_SIGNED"] = a_signed;
1403 cell->parameters["\\B_SIGNED"] = b_signed;
1404 cell->parameters["\\A_WIDTH"] = sig_a.size();
1405 cell->parameters["\\B_WIDTH"] = sig_b.size();
1406 cell->parameters["\\Y_WIDTH"] = sig_y.size();
1407 cell->setPort("\\A", sig_a);
1408 cell->setPort("\\B", sig_b);
1409 cell->setPort("\\Y", sig_y);
1410 return cell;
1411 }
1412
1413 RTLIL::Cell* RTLIL::Module::addSlice(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const offset)
1414 {
1415 RTLIL::Cell *cell = addCell(name, "$slice");
1416 cell->parameters["\\A_WIDTH"] = sig_a.size();
1417 cell->parameters["\\Y_WIDTH"] = sig_y.size();
1418 cell->parameters["\\OFFSET"] = offset;
1419 cell->setPort("\\A", sig_a);
1420 cell->setPort("\\Y", sig_y);
1421 return cell;
1422 }
1423
1424 RTLIL::Cell* RTLIL::Module::addConcat(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y)
1425 {
1426 RTLIL::Cell *cell = addCell(name, "$concat");
1427 cell->parameters["\\A_WIDTH"] = sig_a.size();
1428 cell->parameters["\\B_WIDTH"] = sig_b.size();
1429 cell->setPort("\\A", sig_a);
1430 cell->setPort("\\B", sig_b);
1431 cell->setPort("\\Y", sig_y);
1432 return cell;
1433 }
1434
1435 RTLIL::Cell* RTLIL::Module::addLut(RTLIL::IdString name, RTLIL::SigSpec sig_i, RTLIL::SigSpec sig_o, RTLIL::Const lut)
1436 {
1437 RTLIL::Cell *cell = addCell(name, "$lut");
1438 cell->parameters["\\LUT"] = lut;
1439 cell->parameters["\\WIDTH"] = sig_i.size();
1440 cell->setPort("\\A", sig_i);
1441 cell->setPort("\\Y", sig_o);
1442 return cell;
1443 }
1444
1445 RTLIL::Cell* RTLIL::Module::addAssert(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en)
1446 {
1447 RTLIL::Cell *cell = addCell(name, "$assert");
1448 cell->setPort("\\A", sig_a);
1449 cell->setPort("\\EN", sig_en);
1450 return cell;
1451 }
1452
1453 RTLIL::Cell* RTLIL::Module::addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity, bool clr_polarity)
1454 {
1455 RTLIL::Cell *cell = addCell(name, "$sr");
1456 cell->parameters["\\SET_POLARITY"] = set_polarity;
1457 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1458 cell->parameters["\\WIDTH"] = sig_q.size();
1459 cell->setPort("\\SET", sig_set);
1460 cell->setPort("\\CLR", sig_clr);
1461 cell->setPort("\\Q", sig_q);
1462 return cell;
1463 }
1464
1465 RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity)
1466 {
1467 RTLIL::Cell *cell = addCell(name, "$dff");
1468 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1469 cell->parameters["\\WIDTH"] = sig_q.size();
1470 cell->setPort("\\CLK", sig_clk);
1471 cell->setPort("\\D", sig_d);
1472 cell->setPort("\\Q", sig_q);
1473 return cell;
1474 }
1475
1476 RTLIL::Cell* RTLIL::Module::addDffsr(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1477 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1478 {
1479 RTLIL::Cell *cell = addCell(name, "$dffsr");
1480 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1481 cell->parameters["\\SET_POLARITY"] = set_polarity;
1482 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1483 cell->parameters["\\WIDTH"] = sig_q.size();
1484 cell->setPort("\\CLK", sig_clk);
1485 cell->setPort("\\SET", sig_set);
1486 cell->setPort("\\CLR", sig_clr);
1487 cell->setPort("\\D", sig_d);
1488 cell->setPort("\\Q", sig_q);
1489 return cell;
1490 }
1491
1492 RTLIL::Cell* RTLIL::Module::addAdff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
1493 RTLIL::Const arst_value, bool clk_polarity, bool arst_polarity)
1494 {
1495 RTLIL::Cell *cell = addCell(name, "$adff");
1496 cell->parameters["\\CLK_POLARITY"] = clk_polarity;
1497 cell->parameters["\\ARST_POLARITY"] = arst_polarity;
1498 cell->parameters["\\ARST_VALUE"] = arst_value;
1499 cell->parameters["\\WIDTH"] = sig_q.size();
1500 cell->setPort("\\CLK", sig_clk);
1501 cell->setPort("\\ARST", sig_arst);
1502 cell->setPort("\\D", sig_d);
1503 cell->setPort("\\Q", sig_q);
1504 return cell;
1505 }
1506
1507 RTLIL::Cell* RTLIL::Module::addDlatch(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity)
1508 {
1509 RTLIL::Cell *cell = addCell(name, "$dlatch");
1510 cell->parameters["\\EN_POLARITY"] = en_polarity;
1511 cell->parameters["\\WIDTH"] = sig_q.size();
1512 cell->setPort("\\EN", sig_en);
1513 cell->setPort("\\D", sig_d);
1514 cell->setPort("\\Q", sig_q);
1515 return cell;
1516 }
1517
1518 RTLIL::Cell* RTLIL::Module::addDlatchsr(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1519 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1520 {
1521 RTLIL::Cell *cell = addCell(name, "$dlatchsr");
1522 cell->parameters["\\EN_POLARITY"] = en_polarity;
1523 cell->parameters["\\SET_POLARITY"] = set_polarity;
1524 cell->parameters["\\CLR_POLARITY"] = clr_polarity;
1525 cell->parameters["\\WIDTH"] = sig_q.size();
1526 cell->setPort("\\EN", sig_en);
1527 cell->setPort("\\SET", sig_set);
1528 cell->setPort("\\CLR", sig_clr);
1529 cell->setPort("\\D", sig_d);
1530 cell->setPort("\\Q", sig_q);
1531 return cell;
1532 }
1533
1534 RTLIL::Cell* RTLIL::Module::addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity)
1535 {
1536 RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c_", clk_polarity ? 'P' : 'N'));
1537 cell->setPort("\\C", sig_clk);
1538 cell->setPort("\\D", sig_d);
1539 cell->setPort("\\Q", sig_q);
1540 return cell;
1541 }
1542
1543 RTLIL::Cell* RTLIL::Module::addDffsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1544 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity)
1545 {
1546 RTLIL::Cell *cell = addCell(name, stringf("$_DFFSR_%c%c%c_", clk_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
1547 cell->setPort("\\C", sig_clk);
1548 cell->setPort("\\S", sig_set);
1549 cell->setPort("\\R", sig_clr);
1550 cell->setPort("\\D", sig_d);
1551 cell->setPort("\\Q", sig_q);
1552 return cell;
1553 }
1554
1555 RTLIL::Cell* RTLIL::Module::addAdffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
1556 bool arst_value, bool clk_polarity, bool arst_polarity)
1557 {
1558 RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c%c%c_", clk_polarity ? 'P' : 'N', arst_polarity ? 'P' : 'N', arst_value ? '1' : '0'));
1559 cell->setPort("\\C", sig_clk);
1560 cell->setPort("\\R", sig_arst);
1561 cell->setPort("\\D", sig_d);
1562 cell->setPort("\\Q", sig_q);
1563 return cell;
1564 }
1565
1566 RTLIL::Cell* RTLIL::Module::addDlatchGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity)
1567 {
1568 RTLIL::Cell *cell = addCell(name, stringf("$_DLATCH_%c_", en_polarity ? 'P' : 'N'));
1569 cell->setPort("\\E", sig_en);
1570 cell->setPort("\\D", sig_d);
1571 cell->setPort("\\Q", sig_q);
1572 return cell;
1573 }
1574
1575 RTLIL::Cell* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
1576 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity)
1577 {
1578 RTLIL::Cell *cell = addCell(name, stringf("$_DLATCHSR_%c%c%c_", en_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
1579 cell->setPort("\\E", sig_en);
1580 cell->setPort("\\S", sig_set);
1581 cell->setPort("\\R", sig_clr);
1582 cell->setPort("\\D", sig_d);
1583 cell->setPort("\\Q", sig_q);
1584 return cell;
1585 }
1586
1587
1588 RTLIL::Wire::Wire()
1589 {
1590 module = nullptr;
1591 width = 1;
1592 start_offset = 0;
1593 port_id = 0;
1594 port_input = false;
1595 port_output = false;
1596 upto = false;
1597 }
1598
1599 RTLIL::Memory::Memory()
1600 {
1601 width = 1;
1602 size = 0;
1603 }
1604
1605 RTLIL::Cell::Cell() : module(nullptr)
1606 {
1607 }
1608
1609 bool RTLIL::Cell::hasPort(RTLIL::IdString portname) const
1610 {
1611 return connections_.count(portname) != 0;
1612 }
1613
1614 void RTLIL::Cell::unsetPort(RTLIL::IdString portname)
1615 {
1616 RTLIL::SigSpec signal;
1617 auto conn_it = connections_.find(portname);
1618
1619 if (conn_it != connections_.end())
1620 {
1621 for (auto mon : module->monitors)
1622 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1623
1624 if (module->design)
1625 for (auto mon : module->design->monitors)
1626 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1627
1628 connections_.erase(conn_it);
1629 }
1630 }
1631
1632 void RTLIL::Cell::setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
1633 {
1634 auto conn_it = connections_.find(portname);
1635
1636 if (conn_it == connections_.end()) {
1637 connections_[portname] = RTLIL::SigSpec();
1638 conn_it = connections_.find(portname);
1639 log_assert(conn_it != connections_.end());
1640 }
1641
1642 for (auto mon : module->monitors)
1643 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1644
1645 if (module->design)
1646 for (auto mon : module->design->monitors)
1647 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
1648
1649 conn_it->second = signal;
1650 }
1651
1652 const RTLIL::SigSpec &RTLIL::Cell::getPort(RTLIL::IdString portname) const
1653 {
1654 return connections_.at(portname);
1655 }
1656
1657 const std::map<RTLIL::IdString, RTLIL::SigSpec> &RTLIL::Cell::connections() const
1658 {
1659 return connections_;
1660 }
1661
1662 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname) const
1663 {
1664 return parameters.count(paramname);
1665 }
1666
1667 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname)
1668 {
1669 parameters.erase(paramname);
1670 }
1671
1672 void RTLIL::Cell::setParam(RTLIL::IdString paramname, RTLIL::Const value)
1673 {
1674 parameters[paramname] = value;
1675 }
1676
1677 const RTLIL::Const &RTLIL::Cell::getParam(RTLIL::IdString paramname) const
1678 {
1679 return parameters.at(paramname);
1680 }
1681
1682 void RTLIL::Cell::check()
1683 {
1684 #ifndef NDEBUG
1685 InternalCellChecker checker(NULL, this);
1686 checker.check();
1687 #endif
1688 }
1689
1690 void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
1691 {
1692 if (type.substr(0, 1) != "$" || type.substr(0, 2) == "$_" || type.substr(0, 8) == "$paramod" ||
1693 type.substr(0, 9) == "$verific$" || type.substr(0, 7) == "$array:" || type.substr(0, 8) == "$extern:")
1694 return;
1695
1696 if (type == "$mux" || type == "$pmux")
1697 {
1698 parameters["\\WIDTH"] = SIZE(connections_["\\Y"]);
1699 if (type == "$pmux")
1700 parameters["\\S_WIDTH"] = SIZE(connections_["\\S"]);
1701 check();
1702 return;
1703 }
1704
1705 bool signedness_ab = type != "$slice" && type != "$concat";
1706
1707 if (connections_.count("\\A")) {
1708 if (signedness_ab) {
1709 if (set_a_signed)
1710 parameters["\\A_SIGNED"] = true;
1711 else if (parameters.count("\\A_SIGNED") == 0)
1712 parameters["\\A_SIGNED"] = false;
1713 }
1714 parameters["\\A_WIDTH"] = SIZE(connections_["\\A"]);
1715 }
1716
1717 if (connections_.count("\\B")) {
1718 if (signedness_ab) {
1719 if (set_b_signed)
1720 parameters["\\B_SIGNED"] = true;
1721 else if (parameters.count("\\B_SIGNED") == 0)
1722 parameters["\\B_SIGNED"] = false;
1723 }
1724 parameters["\\B_WIDTH"] = SIZE(connections_["\\B"]);
1725 }
1726
1727 if (connections_.count("\\Y"))
1728 parameters["\\Y_WIDTH"] = SIZE(connections_["\\Y"]);
1729
1730 check();
1731 }
1732
1733 RTLIL::SigChunk::SigChunk()
1734 {
1735 wire = NULL;
1736 width = 0;
1737 offset = 0;
1738 }
1739
1740 RTLIL::SigChunk::SigChunk(const RTLIL::Const &value)
1741 {
1742 wire = NULL;
1743 data = value;
1744 width = data.bits.size();
1745 offset = 0;
1746 }
1747
1748 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire)
1749 {
1750 log_assert(wire != nullptr);
1751 this->wire = wire;
1752 this->width = wire->width;
1753 this->offset = 0;
1754 }
1755
1756 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire, int offset, int width)
1757 {
1758 log_assert(wire != nullptr);
1759 this->wire = wire;
1760 this->width = width;
1761 this->offset = offset;
1762 }
1763
1764 RTLIL::SigChunk::SigChunk(const std::string &str)
1765 {
1766 wire = NULL;
1767 data = RTLIL::Const(str);
1768 width = data.bits.size();
1769 offset = 0;
1770 }
1771
1772 RTLIL::SigChunk::SigChunk(int val, int width)
1773 {
1774 wire = NULL;
1775 data = RTLIL::Const(val, width);
1776 this->width = data.bits.size();
1777 offset = 0;
1778 }
1779
1780 RTLIL::SigChunk::SigChunk(RTLIL::State bit, int width)
1781 {
1782 wire = NULL;
1783 data = RTLIL::Const(bit, width);
1784 this->width = data.bits.size();
1785 offset = 0;
1786 }
1787
1788 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit)
1789 {
1790 wire = bit.wire;
1791 offset = 0;
1792 if (wire == NULL)
1793 data = RTLIL::Const(bit.data);
1794 else
1795 offset = bit.offset;
1796 width = 1;
1797 }
1798
1799 RTLIL::SigChunk RTLIL::SigChunk::extract(int offset, int length) const
1800 {
1801 RTLIL::SigChunk ret;
1802 if (wire) {
1803 ret.wire = wire;
1804 ret.offset = this->offset + offset;
1805 ret.width = length;
1806 } else {
1807 for (int i = 0; i < length; i++)
1808 ret.data.bits.push_back(data.bits[offset+i]);
1809 ret.width = length;
1810 }
1811 return ret;
1812 }
1813
1814 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk &other) const
1815 {
1816 if (wire && other.wire)
1817 if (wire->name != other.wire->name)
1818 return wire->name < other.wire->name;
1819
1820 if (wire != other.wire)
1821 return wire < other.wire;
1822
1823 if (offset != other.offset)
1824 return offset < other.offset;
1825
1826 if (width != other.width)
1827 return width < other.width;
1828
1829 return data.bits < other.data.bits;
1830 }
1831
1832 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk &other) const
1833 {
1834 if (wire != other.wire || width != other.width || offset != other.offset)
1835 return false;
1836 if (data.bits != other.data.bits)
1837 return false;
1838 return true;
1839 }
1840
1841 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk &other) const
1842 {
1843 if (*this == other)
1844 return false;
1845 return true;
1846 }
1847
1848 RTLIL::SigSpec::SigSpec()
1849 {
1850 width_ = 0;
1851 hash_ = 0;
1852 }
1853
1854 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec &other)
1855 {
1856 *this = other;
1857 }
1858
1859 RTLIL::SigSpec::SigSpec(std::initializer_list<RTLIL::SigSpec> parts)
1860 {
1861 cover("kernel.rtlil.sigspec.init.list");
1862
1863 width_ = 0;
1864 hash_ = 0;
1865
1866 std::vector<RTLIL::SigSpec> parts_vec(parts.begin(), parts.end());
1867 for (auto it = parts_vec.rbegin(); it != parts_vec.rend(); it++)
1868 append(*it);
1869 }
1870
1871 const RTLIL::SigSpec &RTLIL::SigSpec::operator=(const RTLIL::SigSpec &other)
1872 {
1873 cover("kernel.rtlil.sigspec.assign");
1874
1875 width_ = other.width_;
1876 hash_ = other.hash_;
1877 chunks_ = other.chunks_;
1878 bits_.clear();
1879
1880 if (!other.bits_.empty())
1881 {
1882 RTLIL::SigChunk *last = NULL;
1883 int last_end_offset = 0;
1884
1885 for (auto &bit : other.bits_) {
1886 if (last && bit.wire == last->wire) {
1887 if (bit.wire == NULL) {
1888 last->data.bits.push_back(bit.data);
1889 last->width++;
1890 continue;
1891 } else if (last_end_offset == bit.offset) {
1892 last_end_offset++;
1893 last->width++;
1894 continue;
1895 }
1896 }
1897 chunks_.push_back(bit);
1898 last = &chunks_.back();
1899 last_end_offset = bit.offset + 1;
1900 }
1901
1902 check();
1903 }
1904
1905 return *this;
1906 }
1907
1908 RTLIL::SigSpec::SigSpec(const RTLIL::Const &value)
1909 {
1910 cover("kernel.rtlil.sigspec.init.const");
1911
1912 chunks_.push_back(RTLIL::SigChunk(value));
1913 width_ = chunks_.back().width;
1914 hash_ = 0;
1915 check();
1916 }
1917
1918 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk &chunk)
1919 {
1920 cover("kernel.rtlil.sigspec.init.chunk");
1921
1922 chunks_.push_back(chunk);
1923 width_ = chunks_.back().width;
1924 hash_ = 0;
1925 check();
1926 }
1927
1928 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire)
1929 {
1930 cover("kernel.rtlil.sigspec.init.wire");
1931
1932 chunks_.push_back(RTLIL::SigChunk(wire));
1933 width_ = chunks_.back().width;
1934 hash_ = 0;
1935 check();
1936 }
1937
1938 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire, int offset, int width)
1939 {
1940 cover("kernel.rtlil.sigspec.init.wire_part");
1941
1942 chunks_.push_back(RTLIL::SigChunk(wire, offset, width));
1943 width_ = chunks_.back().width;
1944 hash_ = 0;
1945 check();
1946 }
1947
1948 RTLIL::SigSpec::SigSpec(const std::string &str)
1949 {
1950 cover("kernel.rtlil.sigspec.init.str");
1951
1952 chunks_.push_back(RTLIL::SigChunk(str));
1953 width_ = chunks_.back().width;
1954 hash_ = 0;
1955 check();
1956 }
1957
1958 RTLIL::SigSpec::SigSpec(int val, int width)
1959 {
1960 cover("kernel.rtlil.sigspec.init.int");
1961
1962 chunks_.push_back(RTLIL::SigChunk(val, width));
1963 width_ = width;
1964 hash_ = 0;
1965 check();
1966 }
1967
1968 RTLIL::SigSpec::SigSpec(RTLIL::State bit, int width)
1969 {
1970 cover("kernel.rtlil.sigspec.init.state");
1971
1972 chunks_.push_back(RTLIL::SigChunk(bit, width));
1973 width_ = width;
1974 hash_ = 0;
1975 check();
1976 }
1977
1978 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit, int width)
1979 {
1980 cover("kernel.rtlil.sigspec.init.bit");
1981
1982 if (bit.wire == NULL)
1983 chunks_.push_back(RTLIL::SigChunk(bit.data, width));
1984 else
1985 for (int i = 0; i < width; i++)
1986 chunks_.push_back(bit);
1987 width_ = width;
1988 hash_ = 0;
1989 check();
1990 }
1991
1992 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigChunk> chunks)
1993 {
1994 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1995
1996 width_ = 0;
1997 hash_ = 0;
1998 for (auto &c : chunks)
1999 append(c);
2000 check();
2001 }
2002
2003 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigBit> bits)
2004 {
2005 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2006
2007 width_ = 0;
2008 hash_ = 0;
2009 for (auto &bit : bits)
2010 append_bit(bit);
2011 check();
2012 }
2013
2014 RTLIL::SigSpec::SigSpec(std::set<RTLIL::SigBit> bits)
2015 {
2016 cover("kernel.rtlil.sigspec.init.stdset_bits");
2017
2018 width_ = 0;
2019 hash_ = 0;
2020 for (auto &bit : bits)
2021 append_bit(bit);
2022 check();
2023 }
2024
2025 void RTLIL::SigSpec::pack() const
2026 {
2027 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2028
2029 if (that->bits_.empty())
2030 return;
2031
2032 cover("kernel.rtlil.sigspec.convert.pack");
2033 log_assert(that->chunks_.empty());
2034
2035 std::vector<RTLIL::SigBit> old_bits;
2036 old_bits.swap(that->bits_);
2037
2038 RTLIL::SigChunk *last = NULL;
2039 int last_end_offset = 0;
2040
2041 for (auto &bit : old_bits) {
2042 if (last && bit.wire == last->wire) {
2043 if (bit.wire == NULL) {
2044 last->data.bits.push_back(bit.data);
2045 last->width++;
2046 continue;
2047 } else if (last_end_offset == bit.offset) {
2048 last_end_offset++;
2049 last->width++;
2050 continue;
2051 }
2052 }
2053 that->chunks_.push_back(bit);
2054 last = &that->chunks_.back();
2055 last_end_offset = bit.offset + 1;
2056 }
2057
2058 check();
2059 }
2060
2061 void RTLIL::SigSpec::unpack() const
2062 {
2063 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2064
2065 if (that->chunks_.empty())
2066 return;
2067
2068 cover("kernel.rtlil.sigspec.convert.unpack");
2069 log_assert(that->bits_.empty());
2070
2071 that->bits_.reserve(that->width_);
2072 for (auto &c : that->chunks_)
2073 for (int i = 0; i < c.width; i++)
2074 that->bits_.push_back(RTLIL::SigBit(c, i));
2075
2076 that->chunks_.clear();
2077 that->hash_ = 0;
2078 }
2079
2080 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2081
2082 void RTLIL::SigSpec::hash() const
2083 {
2084 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2085
2086 if (that->hash_ != 0)
2087 return;
2088
2089 cover("kernel.rtlil.sigspec.hash");
2090 that->pack();
2091
2092 that->hash_ = 5381;
2093 for (auto &c : that->chunks_)
2094 if (c.wire == NULL) {
2095 for (auto &v : c.data.bits)
2096 DJB2(that->hash_, v);
2097 } else {
2098 DJB2(that->hash_, c.wire->name.index_);
2099 DJB2(that->hash_, c.offset);
2100 DJB2(that->hash_, c.width);
2101 }
2102
2103 if (that->hash_ == 0)
2104 that->hash_ = 1;
2105 }
2106
2107 void RTLIL::SigSpec::sort()
2108 {
2109 unpack();
2110 cover("kernel.rtlil.sigspec.sort");
2111 std::sort(bits_.begin(), bits_.end());
2112 }
2113
2114 void RTLIL::SigSpec::sort_and_unify()
2115 {
2116 cover("kernel.rtlil.sigspec.sort_and_unify");
2117 *this = this->to_sigbit_set();
2118 }
2119
2120 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
2121 {
2122 replace(pattern, with, this);
2123 }
2124
2125 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with, RTLIL::SigSpec *other) const
2126 {
2127 log_assert(pattern.width_ == with.width_);
2128
2129 pattern.unpack();
2130 with.unpack();
2131
2132 std::map<RTLIL::SigBit, RTLIL::SigBit> rules;
2133
2134 for (int i = 0; i < SIZE(pattern.bits_); i++)
2135 if (pattern.bits_[i].wire != NULL)
2136 rules[pattern.bits_[i]] = with.bits_[i];
2137
2138 replace(rules, other);
2139 }
2140
2141 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules)
2142 {
2143 replace(rules, this);
2144 }
2145
2146 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const
2147 {
2148 cover("kernel.rtlil.sigspec.replace");
2149
2150 log_assert(other != NULL);
2151 log_assert(width_ == other->width_);
2152
2153 unpack();
2154 other->unpack();
2155
2156 for (int i = 0; i < SIZE(bits_); i++) {
2157 auto it = rules.find(bits_[i]);
2158 if (it != rules.end())
2159 other->bits_[i] = it->second;
2160 }
2161
2162 other->check();
2163 }
2164
2165 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern)
2166 {
2167 remove2(pattern, NULL);
2168 }
2169
2170 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other) const
2171 {
2172 RTLIL::SigSpec tmp = *this;
2173 tmp.remove2(pattern, other);
2174 }
2175
2176 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other)
2177 {
2178 std::set<RTLIL::SigBit> pattern_bits = pattern.to_sigbit_set();
2179 remove2(pattern_bits, other);
2180 }
2181
2182 void RTLIL::SigSpec::remove(const std::set<RTLIL::SigBit> &pattern)
2183 {
2184 remove2(pattern, NULL);
2185 }
2186
2187 void RTLIL::SigSpec::remove(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other) const
2188 {
2189 RTLIL::SigSpec tmp = *this;
2190 tmp.remove2(pattern, other);
2191 }
2192
2193 void RTLIL::SigSpec::remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other)
2194 {
2195 if (other)
2196 cover("kernel.rtlil.sigspec.remove_other");
2197 else
2198 cover("kernel.rtlil.sigspec.remove");
2199
2200 unpack();
2201
2202 if (other != NULL) {
2203 log_assert(width_ == other->width_);
2204 other->unpack();
2205 }
2206
2207 std::vector<RTLIL::SigBit> new_bits, new_other_bits;
2208
2209 new_bits.resize(SIZE(bits_));
2210 if (other != NULL)
2211 new_other_bits.resize(SIZE(bits_));
2212
2213 int k = 0;
2214 for (int i = 0; i < SIZE(bits_); i++) {
2215 if (bits_[i].wire != NULL && pattern.count(bits_[i]))
2216 continue;
2217 if (other != NULL)
2218 new_other_bits[k] = other->bits_[i];
2219 new_bits[k++] = bits_[i];
2220 }
2221
2222 new_bits.resize(k);
2223 if (other != NULL)
2224 new_other_bits.resize(k);
2225
2226 bits_.swap(new_bits);
2227 width_ = SIZE(bits_);
2228
2229 if (other != NULL) {
2230 other->bits_.swap(new_other_bits);
2231 other->width_ = SIZE(other->bits_);
2232 }
2233
2234 check();
2235 }
2236
2237 RTLIL::SigSpec RTLIL::SigSpec::extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other) const
2238 {
2239 std::set<RTLIL::SigBit> pattern_bits = pattern.to_sigbit_set();
2240 return extract(pattern_bits, other);
2241 }
2242
2243 RTLIL::SigSpec RTLIL::SigSpec::extract(const std::set<RTLIL::SigBit> &pattern, const RTLIL::SigSpec *other) const
2244 {
2245 if (other)
2246 cover("kernel.rtlil.sigspec.extract_other");
2247 else
2248 cover("kernel.rtlil.sigspec.extract");
2249
2250 log_assert(other == NULL || width_ == other->width_);
2251
2252 std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
2253 RTLIL::SigSpec ret;
2254
2255 if (other) {
2256 std::vector<RTLIL::SigBit> bits_other = other->to_sigbit_vector();
2257 for (int i = 0; i < width_; i++)
2258 if (bits_match[i].wire && pattern.count(bits_match[i]))
2259 ret.append_bit(bits_other[i]);
2260 } else {
2261 for (int i = 0; i < width_; i++)
2262 if (bits_match[i].wire && pattern.count(bits_match[i]))
2263 ret.append_bit(bits_match[i]);
2264 }
2265
2266 ret.check();
2267 return ret;
2268 }
2269
2270 void RTLIL::SigSpec::replace(int offset, const RTLIL::SigSpec &with)
2271 {
2272 cover("kernel.rtlil.sigspec.replace_pos");
2273
2274 unpack();
2275 with.unpack();
2276
2277 log_assert(offset >= 0);
2278 log_assert(with.width_ >= 0);
2279 log_assert(offset+with.width_ <= width_);
2280
2281 for (int i = 0; i < with.width_; i++)
2282 bits_.at(offset + i) = with.bits_.at(i);
2283
2284 check();
2285 }
2286
2287 void RTLIL::SigSpec::remove_const()
2288 {
2289 if (packed())
2290 {
2291 cover("kernel.rtlil.sigspec.remove_const.packed");
2292
2293 std::vector<RTLIL::SigChunk> new_chunks;
2294 new_chunks.reserve(SIZE(chunks_));
2295
2296 width_ = 0;
2297 for (auto &chunk : chunks_)
2298 if (chunk.wire != NULL) {
2299 new_chunks.push_back(chunk);
2300 width_ += chunk.width;
2301 }
2302
2303 chunks_.swap(new_chunks);
2304 }
2305 else
2306 {
2307 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2308
2309 std::vector<RTLIL::SigBit> new_bits;
2310 new_bits.reserve(width_);
2311
2312 for (auto &bit : bits_)
2313 if (bit.wire != NULL)
2314 new_bits.push_back(bit);
2315
2316 bits_.swap(new_bits);
2317 width_ = bits_.size();
2318 }
2319
2320 check();
2321 }
2322
2323 void RTLIL::SigSpec::remove(int offset, int length)
2324 {
2325 cover("kernel.rtlil.sigspec.remove_pos");
2326
2327 unpack();
2328
2329 log_assert(offset >= 0);
2330 log_assert(length >= 0);
2331 log_assert(offset + length <= width_);
2332
2333 bits_.erase(bits_.begin() + offset, bits_.begin() + offset + length);
2334 width_ = bits_.size();
2335
2336 check();
2337 }
2338
2339 RTLIL::SigSpec RTLIL::SigSpec::extract(int offset, int length) const
2340 {
2341 unpack();
2342 cover("kernel.rtlil.sigspec.extract_pos");
2343 return std::vector<RTLIL::SigBit>(bits_.begin() + offset, bits_.begin() + offset + length);
2344 }
2345
2346 void RTLIL::SigSpec::append(const RTLIL::SigSpec &signal)
2347 {
2348 if (signal.width_ == 0)
2349 return;
2350
2351 if (width_ == 0) {
2352 *this = signal;
2353 return;
2354 }
2355
2356 cover("kernel.rtlil.sigspec.append");
2357
2358 if (packed() != signal.packed()) {
2359 pack();
2360 signal.pack();
2361 }
2362
2363 if (packed())
2364 for (auto &other_c : signal.chunks_)
2365 {
2366 auto &my_last_c = chunks_.back();
2367 if (my_last_c.wire == NULL && other_c.wire == NULL) {
2368 auto &this_data = my_last_c.data.bits;
2369 auto &other_data = other_c.data.bits;
2370 this_data.insert(this_data.end(), other_data.begin(), other_data.end());
2371 my_last_c.width += other_c.width;
2372 } else
2373 if (my_last_c.wire == other_c.wire && my_last_c.offset + my_last_c.width == other_c.offset) {
2374 my_last_c.width += other_c.width;
2375 } else
2376 chunks_.push_back(other_c);
2377 }
2378 else
2379 bits_.insert(bits_.end(), signal.bits_.begin(), signal.bits_.end());
2380
2381 width_ += signal.width_;
2382 check();
2383 }
2384
2385 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit &bit)
2386 {
2387 if (packed())
2388 {
2389 cover("kernel.rtlil.sigspec.append_bit.packed");
2390
2391 if (chunks_.size() == 0)
2392 chunks_.push_back(bit);
2393 else
2394 if (bit.wire == NULL)
2395 if (chunks_.back().wire == NULL) {
2396 chunks_.back().data.bits.push_back(bit.data);
2397 chunks_.back().width++;
2398 } else
2399 chunks_.push_back(bit);
2400 else
2401 if (chunks_.back().wire == bit.wire && chunks_.back().offset + chunks_.back().width == bit.offset)
2402 chunks_.back().width++;
2403 else
2404 chunks_.push_back(bit);
2405 }
2406 else
2407 {
2408 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2409 bits_.push_back(bit);
2410 }
2411
2412 width_++;
2413 check();
2414 }
2415
2416 void RTLIL::SigSpec::extend(int width, bool is_signed)
2417 {
2418 cover("kernel.rtlil.sigspec.extend");
2419
2420 pack();
2421
2422 if (width_ > width)
2423 remove(width, width_ - width);
2424
2425 if (width_ < width) {
2426 RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
2427 if (!is_signed && padding != RTLIL::SigSpec(RTLIL::State::Sx) && padding != RTLIL::SigSpec(RTLIL::State::Sz) &&
2428 padding != RTLIL::SigSpec(RTLIL::State::Sa) && padding != RTLIL::SigSpec(RTLIL::State::Sm))
2429 padding = RTLIL::SigSpec(RTLIL::State::S0);
2430 while (width_ < width)
2431 append(padding);
2432 }
2433 }
2434
2435 void RTLIL::SigSpec::extend_u0(int width, bool is_signed)
2436 {
2437 cover("kernel.rtlil.sigspec.extend_u0");
2438
2439 pack();
2440
2441 if (width_ > width)
2442 remove(width, width_ - width);
2443
2444 if (width_ < width) {
2445 RTLIL::SigSpec padding = width_ > 0 ? extract(width_ - 1, 1) : RTLIL::SigSpec(RTLIL::State::S0);
2446 if (!is_signed)
2447 padding = RTLIL::SigSpec(RTLIL::State::S0);
2448 while (width_ < width)
2449 append(padding);
2450 }
2451
2452 }
2453
2454 RTLIL::SigSpec RTLIL::SigSpec::repeat(int num) const
2455 {
2456 cover("kernel.rtlil.sigspec.repeat");
2457
2458 RTLIL::SigSpec sig;
2459 for (int i = 0; i < num; i++)
2460 sig.append(*this);
2461 return sig;
2462 }
2463
2464 #ifndef NDEBUG
2465 void RTLIL::SigSpec::check() const
2466 {
2467 if (width_ > 64)
2468 {
2469 cover("kernel.rtlil.sigspec.check.skip");
2470 }
2471 else if (packed())
2472 {
2473 cover("kernel.rtlil.sigspec.check.packed");
2474
2475 int w = 0;
2476 for (size_t i = 0; i < chunks_.size(); i++) {
2477 const RTLIL::SigChunk chunk = chunks_[i];
2478 if (chunk.wire == NULL) {
2479 if (i > 0)
2480 log_assert(chunks_[i-1].wire != NULL);
2481 log_assert(chunk.offset == 0);
2482 log_assert(chunk.data.bits.size() == (size_t)chunk.width);
2483 } else {
2484 if (i > 0 && chunks_[i-1].wire == chunk.wire)
2485 log_assert(chunk.offset != chunks_[i-1].offset + chunks_[i-1].width);
2486 log_assert(chunk.offset >= 0);
2487 log_assert(chunk.width >= 0);
2488 log_assert(chunk.offset + chunk.width <= chunk.wire->width);
2489 log_assert(chunk.data.bits.size() == 0);
2490 }
2491 w += chunk.width;
2492 }
2493 log_assert(w == width_);
2494 log_assert(bits_.empty());
2495 }
2496 else
2497 {
2498 cover("kernel.rtlil.sigspec.check.unpacked");
2499
2500 log_assert(width_ == SIZE(bits_));
2501 log_assert(chunks_.empty());
2502 }
2503 }
2504 #endif
2505
2506 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec &other) const
2507 {
2508 cover("kernel.rtlil.sigspec.comp_lt");
2509
2510 if (this == &other)
2511 return false;
2512
2513 if (width_ != other.width_)
2514 return width_ < other.width_;
2515
2516 pack();
2517 other.pack();
2518
2519 if (chunks_.size() != other.chunks_.size())
2520 return chunks_.size() < other.chunks_.size();
2521
2522 hash();
2523 other.hash();
2524
2525 if (hash_ != other.hash_)
2526 return hash_ < other.hash_;
2527
2528 for (size_t i = 0; i < chunks_.size(); i++)
2529 if (chunks_[i] != other.chunks_[i]) {
2530 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2531 return chunks_[i] < other.chunks_[i];
2532 }
2533
2534 cover("kernel.rtlil.sigspec.comp_lt.equal");
2535 return false;
2536 }
2537
2538 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec &other) const
2539 {
2540 cover("kernel.rtlil.sigspec.comp_eq");
2541
2542 if (this == &other)
2543 return true;
2544
2545 if (width_ != other.width_)
2546 return false;
2547
2548 pack();
2549 other.pack();
2550
2551 if (chunks_.size() != chunks_.size())
2552 return false;
2553
2554 hash();
2555 other.hash();
2556
2557 if (hash_ != other.hash_)
2558 return false;
2559
2560 for (size_t i = 0; i < chunks_.size(); i++)
2561 if (chunks_[i] != other.chunks_[i]) {
2562 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2563 return false;
2564 }
2565
2566 cover("kernel.rtlil.sigspec.comp_eq.equal");
2567 return true;
2568 }
2569
2570 bool RTLIL::SigSpec::is_wire() const
2571 {
2572 cover("kernel.rtlil.sigspec.is_wire");
2573
2574 pack();
2575 return SIZE(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
2576 }
2577
2578 bool RTLIL::SigSpec::is_chunk() const
2579 {
2580 cover("kernel.rtlil.sigspec.is_chunk");
2581
2582 pack();
2583 return SIZE(chunks_) == 1;
2584 }
2585
2586 bool RTLIL::SigSpec::is_fully_const() const
2587 {
2588 cover("kernel.rtlil.sigspec.is_fully_const");
2589
2590 pack();
2591 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
2592 if (it->width > 0 && it->wire != NULL)
2593 return false;
2594 return true;
2595 }
2596
2597 bool RTLIL::SigSpec::is_fully_def() const
2598 {
2599 cover("kernel.rtlil.sigspec.is_fully_def");
2600
2601 pack();
2602 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
2603 if (it->width > 0 && it->wire != NULL)
2604 return false;
2605 for (size_t i = 0; i < it->data.bits.size(); i++)
2606 if (it->data.bits[i] != RTLIL::State::S0 && it->data.bits[i] != RTLIL::State::S1)
2607 return false;
2608 }
2609 return true;
2610 }
2611
2612 bool RTLIL::SigSpec::is_fully_undef() const
2613 {
2614 cover("kernel.rtlil.sigspec.is_fully_undef");
2615
2616 pack();
2617 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
2618 if (it->width > 0 && it->wire != NULL)
2619 return false;
2620 for (size_t i = 0; i < it->data.bits.size(); i++)
2621 if (it->data.bits[i] != RTLIL::State::Sx && it->data.bits[i] != RTLIL::State::Sz)
2622 return false;
2623 }
2624 return true;
2625 }
2626
2627 bool RTLIL::SigSpec::has_marked_bits() const
2628 {
2629 cover("kernel.rtlil.sigspec.has_marked_bits");
2630
2631 pack();
2632 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
2633 if (it->width > 0 && it->wire == NULL) {
2634 for (size_t i = 0; i < it->data.bits.size(); i++)
2635 if (it->data.bits[i] == RTLIL::State::Sm)
2636 return true;
2637 }
2638 return false;
2639 }
2640
2641 bool RTLIL::SigSpec::as_bool() const
2642 {
2643 cover("kernel.rtlil.sigspec.as_bool");
2644
2645 pack();
2646 log_assert(is_fully_const() && SIZE(chunks_) <= 1);
2647 if (width_)
2648 return chunks_[0].data.as_bool();
2649 return false;
2650 }
2651
2652 int RTLIL::SigSpec::as_int(bool is_signed) const
2653 {
2654 cover("kernel.rtlil.sigspec.as_int");
2655
2656 pack();
2657 log_assert(is_fully_const() && SIZE(chunks_) <= 1);
2658 if (width_)
2659 return chunks_[0].data.as_int(is_signed);
2660 return 0;
2661 }
2662
2663 std::string RTLIL::SigSpec::as_string() const
2664 {
2665 cover("kernel.rtlil.sigspec.as_string");
2666
2667 pack();
2668 std::string str;
2669 for (size_t i = chunks_.size(); i > 0; i--) {
2670 const RTLIL::SigChunk &chunk = chunks_[i-1];
2671 if (chunk.wire != NULL)
2672 for (int j = 0; j < chunk.width; j++)
2673 str += "?";
2674 else
2675 str += chunk.data.as_string();
2676 }
2677 return str;
2678 }
2679
2680 RTLIL::Const RTLIL::SigSpec::as_const() const
2681 {
2682 cover("kernel.rtlil.sigspec.as_const");
2683
2684 pack();
2685 log_assert(is_fully_const() && SIZE(chunks_) <= 1);
2686 if (width_)
2687 return chunks_[0].data;
2688 return RTLIL::Const();
2689 }
2690
2691 RTLIL::Wire *RTLIL::SigSpec::as_wire() const
2692 {
2693 cover("kernel.rtlil.sigspec.as_wire");
2694
2695 pack();
2696 log_assert(is_wire());
2697 return chunks_[0].wire;
2698 }
2699
2700 RTLIL::SigChunk RTLIL::SigSpec::as_chunk() const
2701 {
2702 cover("kernel.rtlil.sigspec.as_chunk");
2703
2704 pack();
2705 log_assert(is_chunk());
2706 return chunks_[0];
2707 }
2708
2709 bool RTLIL::SigSpec::match(std::string pattern) const
2710 {
2711 cover("kernel.rtlil.sigspec.match");
2712
2713 pack();
2714 std::string str = as_string();
2715 log_assert(pattern.size() == str.size());
2716
2717 for (size_t i = 0; i < pattern.size(); i++) {
2718 if (pattern[i] == ' ')
2719 continue;
2720 if (pattern[i] == '*') {
2721 if (str[i] != 'z' && str[i] != 'x')
2722 return false;
2723 continue;
2724 }
2725 if (pattern[i] != str[i])
2726 return false;
2727 }
2728
2729 return true;
2730 }
2731
2732 std::set<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_set() const
2733 {
2734 cover("kernel.rtlil.sigspec.to_sigbit_set");
2735
2736 pack();
2737 std::set<RTLIL::SigBit> sigbits;
2738 for (auto &c : chunks_)
2739 for (int i = 0; i < c.width; i++)
2740 sigbits.insert(RTLIL::SigBit(c, i));
2741 return sigbits;
2742 }
2743
2744 std::vector<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_vector() const
2745 {
2746 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2747
2748 unpack();
2749 return bits_;
2750 }
2751
2752 std::map<RTLIL::SigBit, RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec &other) const
2753 {
2754 cover("kernel.rtlil.sigspec.to_sigbit_map");
2755
2756 unpack();
2757 other.unpack();
2758
2759 log_assert(width_ == other.width_);
2760
2761 std::map<RTLIL::SigBit, RTLIL::SigBit> new_map;
2762 for (int i = 0; i < width_; i++)
2763 new_map[bits_[i]] = other.bits_[i];
2764
2765 return new_map;
2766 }
2767
2768 RTLIL::SigBit RTLIL::SigSpec::to_single_sigbit() const
2769 {
2770 cover("kernel.rtlil.sigspec.to_single_sigbit");
2771
2772 pack();
2773 log_assert(width_ == 1);
2774 for (auto &c : chunks_)
2775 if (c.width)
2776 return RTLIL::SigBit(c);
2777 log_abort();
2778 }
2779
2780 static void sigspec_parse_split(std::vector<std::string> &tokens, const std::string &text, char sep)
2781 {
2782 size_t start = 0, end = 0;
2783 while ((end = text.find(sep, start)) != std::string::npos) {
2784 tokens.push_back(text.substr(start, end - start));
2785 start = end + 1;
2786 }
2787 tokens.push_back(text.substr(start));
2788 }
2789
2790 static int sigspec_parse_get_dummy_line_num()
2791 {
2792 return 0;
2793 }
2794
2795 bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
2796 {
2797 cover("kernel.rtlil.sigspec.parse");
2798
2799 std::vector<std::string> tokens;
2800 sigspec_parse_split(tokens, str, ',');
2801
2802 sig = RTLIL::SigSpec();
2803 for (int tokidx = int(tokens.size())-1; tokidx >= 0; tokidx--)
2804 {
2805 std::string netname = tokens[tokidx];
2806 std::string indices;
2807
2808 if (netname.size() == 0)
2809 continue;
2810
2811 if ('0' <= netname[0] && netname[0] <= '9') {
2812 cover("kernel.rtlil.sigspec.parse.const");
2813 AST::get_line_num = sigspec_parse_get_dummy_line_num;
2814 AST::AstNode *ast = VERILOG_FRONTEND::const2ast(netname);
2815 if (ast == NULL)
2816 return false;
2817 sig.append(RTLIL::Const(ast->bits));
2818 delete ast;
2819 continue;
2820 }
2821
2822 if (module == NULL)
2823 return false;
2824
2825 cover("kernel.rtlil.sigspec.parse.net");
2826
2827 if (netname[0] != '$' && netname[0] != '\\')
2828 netname = "\\" + netname;
2829
2830 if (module->wires_.count(netname) == 0) {
2831 size_t indices_pos = netname.size()-1;
2832 if (indices_pos > 2 && netname[indices_pos] == ']')
2833 {
2834 indices_pos--;
2835 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
2836 if (indices_pos > 0 && netname[indices_pos] == ':') {
2837 indices_pos--;
2838 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
2839 }
2840 if (indices_pos > 0 && netname[indices_pos] == '[') {
2841 indices = netname.substr(indices_pos);
2842 netname = netname.substr(0, indices_pos);
2843 }
2844 }
2845 }
2846
2847 if (module->wires_.count(netname) == 0)
2848 return false;
2849
2850 RTLIL::Wire *wire = module->wires_.at(netname);
2851 if (!indices.empty()) {
2852 std::vector<std::string> index_tokens;
2853 sigspec_parse_split(index_tokens, indices.substr(1, indices.size()-2), ':');
2854 if (index_tokens.size() == 1) {
2855 cover("kernel.rtlil.sigspec.parse.bit_sel");
2856 sig.append(RTLIL::SigSpec(wire, atoi(index_tokens.at(0).c_str())));
2857 } else {
2858 cover("kernel.rtlil.sigspec.parse.part_sel");
2859 int a = atoi(index_tokens.at(0).c_str());
2860 int b = atoi(index_tokens.at(1).c_str());
2861 if (a > b) {
2862 int tmp = a;
2863 a = b, b = tmp;
2864 }
2865 sig.append(RTLIL::SigSpec(wire, a, b-a+1));
2866 }
2867 } else
2868 sig.append(wire);
2869 }
2870
2871 return true;
2872 }
2873
2874 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str)
2875 {
2876 if (str.empty() || str[0] != '@')
2877 return parse(sig, module, str);
2878
2879 cover("kernel.rtlil.sigspec.parse.sel");
2880
2881 str = RTLIL::escape_id(str.substr(1));
2882 if (design->selection_vars.count(str) == 0)
2883 return false;
2884
2885 sig = RTLIL::SigSpec();
2886 RTLIL::Selection &sel = design->selection_vars.at(str);
2887 for (auto &it : module->wires_)
2888 if (sel.selected_member(module->name, it.first))
2889 sig.append(it.second);
2890
2891 return true;
2892 }
2893
2894 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
2895 {
2896 if (str == "0") {
2897 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2898 sig = RTLIL::SigSpec(RTLIL::State::S0, lhs.width_);
2899 return true;
2900 }
2901
2902 if (str == "~0") {
2903 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2904 sig = RTLIL::SigSpec(RTLIL::State::S1, lhs.width_);
2905 return true;
2906 }
2907
2908 if (lhs.chunks_.size() == 1) {
2909 char *p = (char*)str.c_str(), *endptr;
2910 long long int val = strtoll(p, &endptr, 10);
2911 if (endptr && endptr != p && *endptr == 0) {
2912 sig = RTLIL::SigSpec(val, lhs.width_);
2913 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2914 return true;
2915 }
2916 }
2917
2918 return parse(sig, module, str);
2919 }
2920
2921 RTLIL::CaseRule::~CaseRule()
2922 {
2923 for (auto it = switches.begin(); it != switches.end(); it++)
2924 delete *it;
2925 }
2926
2927 RTLIL::CaseRule *RTLIL::CaseRule::clone() const
2928 {
2929 RTLIL::CaseRule *new_caserule = new RTLIL::CaseRule;
2930 new_caserule->compare = compare;
2931 new_caserule->actions = actions;
2932 for (auto &it : switches)
2933 new_caserule->switches.push_back(it->clone());
2934 return new_caserule;
2935 }
2936
2937 RTLIL::SwitchRule::~SwitchRule()
2938 {
2939 for (auto it = cases.begin(); it != cases.end(); it++)
2940 delete *it;
2941 }
2942
2943 RTLIL::SwitchRule *RTLIL::SwitchRule::clone() const
2944 {
2945 RTLIL::SwitchRule *new_switchrule = new RTLIL::SwitchRule;
2946 new_switchrule->signal = signal;
2947 new_switchrule->attributes = attributes;
2948 for (auto &it : cases)
2949 new_switchrule->cases.push_back(it->clone());
2950 return new_switchrule;
2951
2952 }
2953
2954 RTLIL::SyncRule *RTLIL::SyncRule::clone() const
2955 {
2956 RTLIL::SyncRule *new_syncrule = new RTLIL::SyncRule;
2957 new_syncrule->type = type;
2958 new_syncrule->signal = signal;
2959 new_syncrule->actions = actions;
2960 return new_syncrule;
2961 }
2962
2963 RTLIL::Process::~Process()
2964 {
2965 for (auto it = syncs.begin(); it != syncs.end(); it++)
2966 delete *it;
2967 }
2968
2969 RTLIL::Process *RTLIL::Process::clone() const
2970 {
2971 RTLIL::Process *new_proc = new RTLIL::Process;
2972
2973 new_proc->name = name;
2974 new_proc->attributes = attributes;
2975
2976 RTLIL::CaseRule *rc_ptr = root_case.clone();
2977 new_proc->root_case = *rc_ptr;
2978 rc_ptr->switches.clear();
2979 delete rc_ptr;
2980
2981 for (auto &it : syncs)
2982 new_proc->syncs.push_back(it->clone());
2983
2984 return new_proc;
2985 }
2986
2987 YOSYS_NAMESPACE_END
2988