2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "frontends/verilog/verilog_frontend.h"
22 #include "backends/ilang/ilang_backend.h"
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_
;
36 flags
= RTLIL::CONST_FLAG_NONE
;
39 RTLIL::Const::Const(std::string str
)
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
);
51 RTLIL::Const::Const(int val
, int width
)
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
);
60 RTLIL::Const::Const(RTLIL::State bit
, int width
)
62 flags
= RTLIL::CONST_FLAG_NONE
;
63 for (int i
= 0; i
< width
; i
++)
67 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
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
];
77 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
79 return bits
== other
.bits
;
82 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
84 return bits
!= other
.bits
;
87 bool RTLIL::Const::as_bool() const
89 for (size_t i
= 0; i
< bits
.size(); i
++)
90 if (bits
[i
] == RTLIL::S1
)
95 int RTLIL::Const::as_int(bool is_signed
) const
98 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
99 if (bits
[i
] == RTLIL::S1
)
101 if (is_signed
&& bits
.back() == RTLIL::S1
)
102 for (size_t i
= bits
.size(); i
< 32; i
++)
107 std::string
RTLIL::Const::as_string() const
110 for (size_t i
= bits
.size(); i
> 0; i
--)
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;
122 std::string
RTLIL::Const::decode_string() const
125 std::vector
<char> string_chars
;
126 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
128 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
129 if (bits
[i
+ j
] == RTLIL::State::S1
)
132 string_chars
.push_back(ch
);
134 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
135 string
+= string_chars
[i
];
139 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
143 if (selected_modules
.count(mod_name
) > 0)
145 if (selected_members
.count(mod_name
) > 0)
150 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
154 if (selected_modules
.count(mod_name
) > 0)
159 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
163 if (selected_modules
.count(mod_name
) > 0)
165 if (selected_members
.count(mod_name
) > 0)
166 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
171 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
173 if (full_selection
) {
174 selected_modules
.clear();
175 selected_members
.clear();
179 std::vector
<RTLIL::IdString
> del_list
, add_list
;
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
);
187 for (auto mod_name
: del_list
)
188 selected_modules
.erase(mod_name
);
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
);
197 for (auto &it
: selected_members
) {
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
);
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
);
221 if (selected_modules
.size() == design
->modules_
.size()) {
222 full_selection
= true;
223 selected_modules
.clear();
224 selected_members
.clear();
228 RTLIL::Design::Design()
230 refcount_modules_
= 0;
233 RTLIL::Design::~Design()
235 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
239 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
241 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
244 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
246 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
249 void RTLIL::Design::add(RTLIL::Module
*module
)
251 log_assert(modules_
.count(module
->name
) == 0);
252 log_assert(refcount_modules_
== 0);
253 modules_
[module
->name
] = module
;
254 module
->design
= this;
256 for (auto mon
: monitors
)
257 mon
->notify_module_add(module
);
260 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
262 log_assert(modules_
.count(name
) == 0);
263 log_assert(refcount_modules_
== 0);
265 RTLIL::Module
*module
= new RTLIL::Module
;
266 modules_
[name
] = module
;
267 module
->design
= this;
270 for (auto mon
: monitors
)
271 mon
->notify_module_add(module
);
276 void RTLIL::Design::remove(RTLIL::Module
*module
)
278 for (auto mon
: monitors
)
279 mon
->notify_module_del(module
);
281 log_assert(modules_
.at(module
->name
) == module
);
282 modules_
.erase(module
->name
);
286 void RTLIL::Design::check()
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());
298 void RTLIL::Design::optimize()
300 for (auto &it
: modules_
)
301 it
.second
->optimize();
302 for (auto &it
: selection_stack
)
304 for (auto &it
: selection_vars
)
305 it
.second
.optimize(this);
308 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
310 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
312 if (selection_stack
.size() == 0)
314 return selection_stack
.back().selected_module(mod_name
);
317 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
319 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
321 if (selection_stack
.size() == 0)
323 return selection_stack
.back().selected_whole_module(mod_name
);
326 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
328 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
330 if (selection_stack
.size() == 0)
332 return selection_stack
.back().selected_member(mod_name
, memb_name
);
335 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
337 return selected_module(mod
->name
);
340 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
342 return selected_whole_module(mod
->name
);
345 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
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
);
355 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
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
);
365 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
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
));
377 RTLIL::Module::Module()
384 RTLIL::Module::~Module()
386 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
388 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
390 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
392 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
396 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
398 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
401 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
403 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
408 struct InternalCellChecker
410 RTLIL::Module
*module
;
412 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
414 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
416 void error(int linenr
)
418 std::stringstream buf
;
419 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
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());
426 int param(const char *name
)
428 if (cell
->parameters
.count(name
) == 0)
430 expected_params
.insert(name
);
431 return cell
->parameters
.at(name
).as_int();
434 int param_bool(const char *name
)
437 if (cell
->parameters
.at(name
).bits
.size() > 32)
439 if (v
!= 0 && v
!= 1)
444 void param_bits(const char *name
, int width
)
447 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
451 void port(const char *name
, int width
)
453 if (!cell
->hasPort(name
))
455 if (cell
->getPort(name
).size() != width
)
457 expected_ports
.insert(name
);
460 void check_expected(bool check_matched_sign
= true)
462 for (auto ¶
: cell
->parameters
)
463 if (expected_params
.count(para
.first
) == 0)
465 for (auto &conn
: cell
->connections())
466 if (expected_ports
.count(conn
.first
) == 0)
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
)
477 void check_gate(const char *ports
)
479 if (cell
->parameters
.size() != 0)
482 for (const char *p
= ports
; *p
; p
++) {
483 char portname
[3] = { '\\', *p
, 0 };
484 if (!cell
->hasPort(portname
))
486 if (cell
->getPort(portname
).size() != 1)
490 for (auto &conn
: cell
->connections()) {
491 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
493 if (strchr(ports
, conn
.first
[1]) == NULL
)
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:")
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"));
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"));
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"));
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);
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"));
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");
560 if (cell
->type
== "$alu") {
561 param_bool("\\A_SIGNED");
562 param_bool("\\B_SIGNED");
563 port("\\A", param("\\A_WIDTH"));
564 port("\\B", param("\\B_WIDTH"));
567 port("\\X", param("\\Y_WIDTH"));
568 port("\\Y", param("\\Y_WIDTH"));
569 port("\\CO", param("\\Y_WIDTH"));
574 if (cell
->type
== "$logic_not") {
575 param_bool("\\A_SIGNED");
576 port("\\A", param("\\A_WIDTH"));
577 port("\\Y", param("\\Y_WIDTH"));
582 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
583 param_bool("\\A_SIGNED");
584 param_bool("\\B_SIGNED");
585 port("\\A", param("\\A_WIDTH"));
586 port("\\B", param("\\B_WIDTH"));
587 port("\\Y", param("\\Y_WIDTH"));
588 check_expected(false);
592 if (cell
->type
== "$slice") {
594 port("\\A", param("\\A_WIDTH"));
595 port("\\Y", param("\\Y_WIDTH"));
596 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
602 if (cell
->type
== "$concat") {
603 port("\\A", param("\\A_WIDTH"));
604 port("\\B", param("\\B_WIDTH"));
605 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
610 if (cell
->type
== "$mux") {
611 port("\\A", param("\\WIDTH"));
612 port("\\B", param("\\WIDTH"));
614 port("\\Y", param("\\WIDTH"));
619 if (cell
->type
== "$pmux") {
620 port("\\A", param("\\WIDTH"));
621 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
622 port("\\S", param("\\S_WIDTH"));
623 port("\\Y", param("\\WIDTH"));
628 if (cell
->type
== "$lut") {
630 port("\\A", param("\\WIDTH"));
636 if (cell
->type
== "$sr") {
637 param_bool("\\SET_POLARITY");
638 param_bool("\\CLR_POLARITY");
639 port("\\SET", param("\\WIDTH"));
640 port("\\CLR", param("\\WIDTH"));
641 port("\\Q", param("\\WIDTH"));
646 if (cell
->type
== "$dff") {
647 param_bool("\\CLK_POLARITY");
649 port("\\D", param("\\WIDTH"));
650 port("\\Q", param("\\WIDTH"));
655 if (cell
->type
== "$dffsr") {
656 param_bool("\\CLK_POLARITY");
657 param_bool("\\SET_POLARITY");
658 param_bool("\\CLR_POLARITY");
660 port("\\SET", param("\\WIDTH"));
661 port("\\CLR", param("\\WIDTH"));
662 port("\\D", param("\\WIDTH"));
663 port("\\Q", param("\\WIDTH"));
668 if (cell
->type
== "$adff") {
669 param_bool("\\CLK_POLARITY");
670 param_bool("\\ARST_POLARITY");
671 param_bits("\\ARST_VALUE", param("\\WIDTH"));
674 port("\\D", param("\\WIDTH"));
675 port("\\Q", param("\\WIDTH"));
680 if (cell
->type
== "$dlatch") {
681 param_bool("\\EN_POLARITY");
683 port("\\D", param("\\WIDTH"));
684 port("\\Q", param("\\WIDTH"));
689 if (cell
->type
== "$dlatchsr") {
690 param_bool("\\EN_POLARITY");
691 param_bool("\\SET_POLARITY");
692 param_bool("\\CLR_POLARITY");
694 port("\\SET", param("\\WIDTH"));
695 port("\\CLR", param("\\WIDTH"));
696 port("\\D", param("\\WIDTH"));
697 port("\\Q", param("\\WIDTH"));
702 if (cell
->type
== "$fsm") {
704 param_bool("\\CLK_POLARITY");
705 param_bool("\\ARST_POLARITY");
706 param("\\STATE_BITS");
707 param("\\STATE_NUM");
708 param("\\STATE_NUM_LOG2");
709 param("\\STATE_RST");
710 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
711 param("\\TRANS_NUM");
712 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
715 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
716 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
721 if (cell
->type
== "$memrd") {
723 param_bool("\\CLK_ENABLE");
724 param_bool("\\CLK_POLARITY");
725 param_bool("\\TRANSPARENT");
727 port("\\ADDR", param("\\ABITS"));
728 port("\\DATA", param("\\WIDTH"));
733 if (cell
->type
== "$memwr") {
735 param_bool("\\CLK_ENABLE");
736 param_bool("\\CLK_POLARITY");
739 port("\\EN", param("\\WIDTH"));
740 port("\\ADDR", param("\\ABITS"));
741 port("\\DATA", param("\\WIDTH"));
746 if (cell
->type
== "$mem") {
750 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
751 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
752 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
753 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
754 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
755 port("\\RD_CLK", param("\\RD_PORTS"));
756 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
757 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
758 port("\\WR_CLK", param("\\WR_PORTS"));
759 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
760 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
761 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
766 if (cell
->type
== "$assert") {
773 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
774 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
775 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
776 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
777 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
778 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
779 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
780 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
781 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
782 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
783 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
784 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
786 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
787 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
788 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
789 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
791 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
792 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
794 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
795 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
796 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
797 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
798 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
799 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
800 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
801 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
803 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
804 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
805 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
806 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
807 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
808 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
809 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
810 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
812 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
813 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
815 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
816 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
817 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
818 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
819 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
820 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
821 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
822 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
830 void RTLIL::Module::check()
833 std::vector
<bool> ports_declared
;
834 for (auto &it
: wires_
) {
835 log_assert(this == it
.second
->module
);
836 log_assert(it
.first
== it
.second
->name
);
837 log_assert(!it
.first
.empty());
838 log_assert(it
.second
->width
>= 0);
839 log_assert(it
.second
->port_id
>= 0);
840 for (auto &it2
: it
.second
->attributes
)
841 log_assert(!it2
.first
.empty());
842 if (it
.second
->port_id
) {
843 log_assert(SIZE(ports
) >= it
.second
->port_id
);
844 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
845 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
846 if (SIZE(ports_declared
) < it
.second
->port_id
)
847 ports_declared
.resize(it
.second
->port_id
);
848 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
849 ports_declared
[it
.second
->port_id
-1] = true;
851 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
853 for (auto port_declared
: ports_declared
)
854 log_assert(port_declared
== true);
855 log_assert(SIZE(ports
) == SIZE(ports_declared
));
857 for (auto &it
: memories
) {
858 log_assert(it
.first
== it
.second
->name
);
859 log_assert(!it
.first
.empty());
860 log_assert(it
.second
->width
>= 0);
861 log_assert(it
.second
->size
>= 0);
862 for (auto &it2
: it
.second
->attributes
)
863 log_assert(!it2
.first
.empty());
866 for (auto &it
: cells_
) {
867 log_assert(this == it
.second
->module
);
868 log_assert(it
.first
== it
.second
->name
);
869 log_assert(!it
.first
.empty());
870 log_assert(!it
.second
->type
.empty());
871 for (auto &it2
: it
.second
->connections()) {
872 log_assert(!it2
.first
.empty());
875 for (auto &it2
: it
.second
->attributes
)
876 log_assert(!it2
.first
.empty());
877 for (auto &it2
: it
.second
->parameters
)
878 log_assert(!it2
.first
.empty());
879 InternalCellChecker
checker(this, it
.second
);
883 for (auto &it
: processes
) {
884 log_assert(it
.first
== it
.second
->name
);
885 log_assert(!it
.first
.empty());
886 // FIXME: More checks here..
889 for (auto &it
: connections_
) {
890 log_assert(it
.first
.size() == it
.second
.size());
895 for (auto &it
: attributes
)
896 log_assert(!it
.first
.empty());
900 void RTLIL::Module::optimize()
904 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
906 log_assert(new_mod
->refcount_wires_
== 0);
907 log_assert(new_mod
->refcount_cells_
== 0);
909 new_mod
->connections_
= connections_
;
910 new_mod
->attributes
= attributes
;
912 for (auto &it
: wires_
)
913 new_mod
->addWire(it
.first
, it
.second
);
915 for (auto &it
: memories
)
916 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
918 for (auto &it
: cells_
)
919 new_mod
->addCell(it
.first
, it
.second
);
921 for (auto &it
: processes
)
922 new_mod
->processes
[it
.first
] = it
.second
->clone();
924 struct RewriteSigSpecWorker
927 void operator()(RTLIL::SigSpec
&sig
)
929 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
930 for (auto &c
: chunks
)
932 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
937 RewriteSigSpecWorker rewriteSigSpecWorker
;
938 rewriteSigSpecWorker
.mod
= new_mod
;
939 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
940 new_mod
->fixup_ports();
943 RTLIL::Module
*RTLIL::Module::clone() const
945 RTLIL::Module
*new_mod
= new RTLIL::Module
;
946 new_mod
->name
= name
;
951 bool RTLIL::Module::has_memories() const
953 return !memories
.empty();
956 bool RTLIL::Module::has_processes() const
958 return !processes
.empty();
961 bool RTLIL::Module::has_memories_warn() const
963 if (!memories
.empty())
964 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
965 return !memories
.empty();
968 bool RTLIL::Module::has_processes_warn() const
970 if (!processes
.empty())
971 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
972 return !processes
.empty();
975 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
977 std::vector
<RTLIL::Wire
*> result
;
978 result
.reserve(wires_
.size());
979 for (auto &it
: wires_
)
980 if (design
->selected(this, it
.second
))
981 result
.push_back(it
.second
);
985 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
987 std::vector
<RTLIL::Cell
*> result
;
988 result
.reserve(wires_
.size());
989 for (auto &it
: cells_
)
990 if (design
->selected(this, it
.second
))
991 result
.push_back(it
.second
);
995 void RTLIL::Module::add(RTLIL::Wire
*wire
)
997 log_assert(!wire
->name
.empty());
998 log_assert(count_id(wire
->name
) == 0);
999 log_assert(refcount_wires_
== 0);
1000 wires_
[wire
->name
] = wire
;
1001 wire
->module
= this;
1004 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1006 log_assert(!cell
->name
.empty());
1007 log_assert(count_id(cell
->name
) == 0);
1008 log_assert(refcount_cells_
== 0);
1009 cells_
[cell
->name
] = cell
;
1010 cell
->module
= this;
1014 struct DeleteWireWorker
1016 RTLIL::Module
*module
;
1017 const std::set
<RTLIL::Wire
*> *wires_p
;
1019 void operator()(RTLIL::SigSpec
&sig
) {
1020 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1021 for (auto &c
: chunks
)
1022 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1023 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1032 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1034 std::setPort
<RTLIL::Wire
*> wires_
;
1035 wires_
.insert(wire
);
1040 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1042 log_assert(refcount_wires_
== 0);
1044 DeleteWireWorker delete_wire_worker
;
1045 delete_wire_worker
.module
= this;
1046 delete_wire_worker
.wires_p
= &wires
;
1047 rewrite_sigspecs(delete_wire_worker
);
1049 for (auto &it
: wires
) {
1050 log_assert(wires_
.count(it
->name
) != 0);
1051 wires_
.erase(it
->name
);
1056 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1058 log_assert(cells_
.count(cell
->name
) != 0);
1059 log_assert(refcount_cells_
== 0);
1060 cells_
.erase(cell
->name
);
1064 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1066 log_assert(wires_
[wire
->name
] == wire
);
1067 log_assert(refcount_wires_
== 0);
1068 wires_
.erase(wire
->name
);
1069 wire
->name
= new_name
;
1073 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1075 log_assert(cells_
[cell
->name
] == cell
);
1076 log_assert(refcount_wires_
== 0);
1077 cells_
.erase(cell
->name
);
1078 cell
->name
= new_name
;
1082 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1084 log_assert(count_id(old_name
) != 0);
1085 if (wires_
.count(old_name
))
1086 rename(wires_
.at(old_name
), new_name
);
1087 else if (cells_
.count(old_name
))
1088 rename(cells_
.at(old_name
), new_name
);
1093 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1095 log_assert(wires_
[w1
->name
] == w1
);
1096 log_assert(wires_
[w2
->name
] == w2
);
1097 log_assert(refcount_wires_
== 0);
1099 wires_
.erase(w1
->name
);
1100 wires_
.erase(w2
->name
);
1102 std::swap(w1
->name
, w2
->name
);
1104 wires_
[w1
->name
] = w1
;
1105 wires_
[w2
->name
] = w2
;
1108 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1110 log_assert(cells_
[c1
->name
] == c1
);
1111 log_assert(cells_
[c2
->name
] == c2
);
1112 log_assert(refcount_cells_
== 0);
1114 cells_
.erase(c1
->name
);
1115 cells_
.erase(c2
->name
);
1117 std::swap(c1
->name
, c2
->name
);
1119 cells_
[c1
->name
] = c1
;
1120 cells_
[c2
->name
] = c2
;
1123 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1126 return uniquify(name
, index
);
1129 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1132 if (count_id(name
) == 0)
1138 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1139 if (count_id(new_name
) == 0)
1145 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1147 if (a
->port_id
&& !b
->port_id
)
1149 if (!a
->port_id
&& b
->port_id
)
1152 if (a
->port_id
== b
->port_id
)
1153 return a
->name
< b
->name
;
1154 return a
->port_id
< b
->port_id
;
1157 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1159 for (auto mon
: monitors
)
1160 mon
->notify_connect(this, conn
);
1163 for (auto mon
: design
->monitors
)
1164 mon
->notify_connect(this, conn
);
1166 connections_
.push_back(conn
);
1169 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1171 connect(RTLIL::SigSig(lhs
, rhs
));
1174 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1176 for (auto mon
: monitors
)
1177 mon
->notify_connect(this, new_conn
);
1180 for (auto mon
: design
->monitors
)
1181 mon
->notify_connect(this, new_conn
);
1183 connections_
= new_conn
;
1186 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1188 return connections_
;
1191 void RTLIL::Module::fixup_ports()
1193 std::vector
<RTLIL::Wire
*> all_ports
;
1195 for (auto &w
: wires_
)
1196 if (w
.second
->port_input
|| w
.second
->port_output
)
1197 all_ports
.push_back(w
.second
);
1199 w
.second
->port_id
= 0;
1201 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1204 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1205 ports
.push_back(all_ports
[i
]->name
);
1206 all_ports
[i
]->port_id
= i
+1;
1210 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1212 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1214 wire
->width
= width
;
1219 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1221 RTLIL::Wire
*wire
= addWire(name
);
1222 wire
->width
= other
->width
;
1223 wire
->start_offset
= other
->start_offset
;
1224 wire
->port_id
= other
->port_id
;
1225 wire
->port_input
= other
->port_input
;
1226 wire
->port_output
= other
->port_output
;
1227 wire
->upto
= other
->upto
;
1228 wire
->attributes
= other
->attributes
;
1232 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1234 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1241 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1243 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1244 cell
->connections_
= other
->connections_
;
1245 cell
->parameters
= other
->parameters
;
1246 cell
->attributes
= other
->attributes
;
1250 #define DEF_METHOD(_func, _y_size, _type) \
1251 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1252 RTLIL::Cell *cell = addCell(name, _type); \
1253 cell->parameters["\\A_SIGNED"] = is_signed; \
1254 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1255 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1256 cell->setPort("\\A", sig_a); \
1257 cell->setPort("\\Y", sig_y); \
1260 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1261 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1262 add ## _func(name, sig_a, sig_y, is_signed); \
1265 DEF_METHOD(Not
, sig_a
.size(), "$not")
1266 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1267 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1268 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1269 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1270 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1271 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1272 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1273 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1274 DEF_METHOD(LogicNot
, 1, "$logic_not")
1277 #define DEF_METHOD(_func, _y_size, _type) \
1278 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1279 RTLIL::Cell *cell = addCell(name, _type); \
1280 cell->parameters["\\A_SIGNED"] = is_signed; \
1281 cell->parameters["\\B_SIGNED"] = is_signed; \
1282 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1283 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1284 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1285 cell->setPort("\\A", sig_a); \
1286 cell->setPort("\\B", sig_b); \
1287 cell->setPort("\\Y", sig_y); \
1290 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1291 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1292 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1295 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1296 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1297 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1298 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1299 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1300 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1301 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1302 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1303 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1304 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1305 DEF_METHOD(Lt
, 1, "$lt")
1306 DEF_METHOD(Le
, 1, "$le")
1307 DEF_METHOD(Eq
, 1, "$eq")
1308 DEF_METHOD(Ne
, 1, "$ne")
1309 DEF_METHOD(Eqx
, 1, "$eqx")
1310 DEF_METHOD(Nex
, 1, "$nex")
1311 DEF_METHOD(Ge
, 1, "$ge")
1312 DEF_METHOD(Gt
, 1, "$gt")
1313 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1314 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1315 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1316 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1317 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1318 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1319 DEF_METHOD(LogicOr
, 1, "$logic_or")
1322 #define DEF_METHOD(_func, _type, _pmux) \
1323 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) { \
1324 RTLIL::Cell *cell = addCell(name, _type); \
1325 cell->parameters["\\WIDTH"] = sig_a.size(); \
1326 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1327 cell->setPort("\\A", sig_a); \
1328 cell->setPort("\\B", sig_b); \
1329 cell->setPort("\\S", sig_s); \
1330 cell->setPort("\\Y", sig_y); \
1333 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1334 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1335 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1338 DEF_METHOD(Mux
, "$mux", 0)
1339 DEF_METHOD(Pmux
, "$pmux", 1)
1342 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1343 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1344 RTLIL::Cell *cell = addCell(name, _type); \
1345 cell->setPort("\\" #_P1, sig1); \
1346 cell->setPort("\\" #_P2, sig2); \
1349 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1350 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1351 add ## _func(name, sig1, sig2); \
1354 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1355 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1356 RTLIL::Cell *cell = addCell(name, _type); \
1357 cell->setPort("\\" #_P1, sig1); \
1358 cell->setPort("\\" #_P2, sig2); \
1359 cell->setPort("\\" #_P3, sig3); \
1362 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1363 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1364 add ## _func(name, sig1, sig2, sig3); \
1367 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1368 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
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); \
1376 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1377 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1378 add ## _func(name, sig1, sig2, sig3, sig4); \
1381 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1382 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1383 RTLIL::Cell *cell = addCell(name, _type); \
1384 cell->setPort("\\" #_P1, sig1); \
1385 cell->setPort("\\" #_P2, sig2); \
1386 cell->setPort("\\" #_P3, sig3); \
1387 cell->setPort("\\" #_P4, sig4); \
1388 cell->setPort("\\" #_P5, sig5); \
1391 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1392 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1393 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1396 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1397 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1398 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1399 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1400 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1401 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1402 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1403 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1404 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1405 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1406 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1407 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1413 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
)
1415 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1416 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1417 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1418 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1419 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1420 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1421 cell
->setPort("\\A", sig_a
);
1422 cell
->setPort("\\B", sig_b
);
1423 cell
->setPort("\\Y", sig_y
);
1427 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1429 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1430 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1431 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1432 cell
->parameters
["\\OFFSET"] = offset
;
1433 cell
->setPort("\\A", sig_a
);
1434 cell
->setPort("\\Y", sig_y
);
1438 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1440 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1441 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1442 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1443 cell
->setPort("\\A", sig_a
);
1444 cell
->setPort("\\B", sig_b
);
1445 cell
->setPort("\\Y", sig_y
);
1449 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1451 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1452 cell
->parameters
["\\LUT"] = lut
;
1453 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1454 cell
->setPort("\\A", sig_i
);
1455 cell
->setPort("\\Y", sig_o
);
1459 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1461 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1462 cell
->setPort("\\A", sig_a
);
1463 cell
->setPort("\\EN", sig_en
);
1467 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
)
1469 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1470 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1471 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1472 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1473 cell
->setPort("\\SET", sig_set
);
1474 cell
->setPort("\\CLR", sig_clr
);
1475 cell
->setPort("\\Q", sig_q
);
1479 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1481 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1482 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1483 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1484 cell
->setPort("\\CLK", sig_clk
);
1485 cell
->setPort("\\D", sig_d
);
1486 cell
->setPort("\\Q", sig_q
);
1490 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1491 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1493 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1494 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1495 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1496 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1497 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1498 cell
->setPort("\\CLK", sig_clk
);
1499 cell
->setPort("\\SET", sig_set
);
1500 cell
->setPort("\\CLR", sig_clr
);
1501 cell
->setPort("\\D", sig_d
);
1502 cell
->setPort("\\Q", sig_q
);
1506 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1507 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1509 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1510 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1511 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1512 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1513 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1514 cell
->setPort("\\CLK", sig_clk
);
1515 cell
->setPort("\\ARST", sig_arst
);
1516 cell
->setPort("\\D", sig_d
);
1517 cell
->setPort("\\Q", sig_q
);
1521 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1523 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1524 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1525 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1526 cell
->setPort("\\EN", sig_en
);
1527 cell
->setPort("\\D", sig_d
);
1528 cell
->setPort("\\Q", sig_q
);
1532 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1533 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1535 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1536 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1537 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1538 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1539 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1540 cell
->setPort("\\EN", sig_en
);
1541 cell
->setPort("\\SET", sig_set
);
1542 cell
->setPort("\\CLR", sig_clr
);
1543 cell
->setPort("\\D", sig_d
);
1544 cell
->setPort("\\Q", sig_q
);
1548 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1550 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1551 cell
->setPort("\\C", sig_clk
);
1552 cell
->setPort("\\D", sig_d
);
1553 cell
->setPort("\\Q", sig_q
);
1557 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1558 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1560 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1561 cell
->setPort("\\C", sig_clk
);
1562 cell
->setPort("\\S", sig_set
);
1563 cell
->setPort("\\R", sig_clr
);
1564 cell
->setPort("\\D", sig_d
);
1565 cell
->setPort("\\Q", sig_q
);
1569 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1570 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1572 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1573 cell
->setPort("\\C", sig_clk
);
1574 cell
->setPort("\\R", sig_arst
);
1575 cell
->setPort("\\D", sig_d
);
1576 cell
->setPort("\\Q", sig_q
);
1580 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1582 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1583 cell
->setPort("\\E", sig_en
);
1584 cell
->setPort("\\D", sig_d
);
1585 cell
->setPort("\\Q", sig_q
);
1589 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1590 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1592 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1593 cell
->setPort("\\E", sig_en
);
1594 cell
->setPort("\\S", sig_set
);
1595 cell
->setPort("\\R", sig_clr
);
1596 cell
->setPort("\\D", sig_d
);
1597 cell
->setPort("\\Q", sig_q
);
1609 port_output
= false;
1613 RTLIL::Memory::Memory()
1619 RTLIL::Cell::Cell() : module(nullptr)
1623 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1625 return connections_
.count(portname
) != 0;
1628 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1630 RTLIL::SigSpec signal
;
1631 auto conn_it
= connections_
.find(portname
);
1633 if (conn_it
!= connections_
.end())
1635 for (auto mon
: module
->monitors
)
1636 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1639 for (auto mon
: module
->design
->monitors
)
1640 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1642 connections_
.erase(conn_it
);
1646 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1648 auto conn_it
= connections_
.find(portname
);
1650 if (conn_it
== connections_
.end()) {
1651 connections_
[portname
] = RTLIL::SigSpec();
1652 conn_it
= connections_
.find(portname
);
1653 log_assert(conn_it
!= connections_
.end());
1656 for (auto mon
: module
->monitors
)
1657 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1660 for (auto mon
: module
->design
->monitors
)
1661 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1663 conn_it
->second
= signal
;
1666 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1668 return connections_
.at(portname
);
1671 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1673 return connections_
;
1676 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1678 return parameters
.count(paramname
);
1681 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1683 parameters
.erase(paramname
);
1686 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1688 parameters
[paramname
] = value
;
1691 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1693 return parameters
.at(paramname
);
1696 void RTLIL::Cell::check()
1699 InternalCellChecker
checker(NULL
, this);
1704 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1706 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1707 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1710 if (type
== "$mux" || type
== "$pmux")
1712 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1713 if (type
== "$pmux")
1714 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1719 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1721 if (connections_
.count("\\A")) {
1722 if (signedness_ab
) {
1724 parameters
["\\A_SIGNED"] = true;
1725 else if (parameters
.count("\\A_SIGNED") == 0)
1726 parameters
["\\A_SIGNED"] = false;
1728 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1731 if (connections_
.count("\\B")) {
1732 if (signedness_ab
) {
1734 parameters
["\\B_SIGNED"] = true;
1735 else if (parameters
.count("\\B_SIGNED") == 0)
1736 parameters
["\\B_SIGNED"] = false;
1738 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1741 if (connections_
.count("\\Y"))
1742 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1747 RTLIL::SigChunk::SigChunk()
1754 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1758 width
= data
.bits
.size();
1762 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1764 log_assert(wire
!= nullptr);
1766 this->width
= wire
->width
;
1770 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1772 log_assert(wire
!= nullptr);
1774 this->width
= width
;
1775 this->offset
= offset
;
1778 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1781 data
= RTLIL::Const(str
);
1782 width
= data
.bits
.size();
1786 RTLIL::SigChunk::SigChunk(int val
, int width
)
1789 data
= RTLIL::Const(val
, width
);
1790 this->width
= data
.bits
.size();
1794 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1797 data
= RTLIL::Const(bit
, width
);
1798 this->width
= data
.bits
.size();
1802 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1807 data
= RTLIL::Const(bit
.data
);
1809 offset
= bit
.offset
;
1813 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1815 RTLIL::SigChunk ret
;
1818 ret
.offset
= this->offset
+ offset
;
1821 for (int i
= 0; i
< length
; i
++)
1822 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1828 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1830 if (wire
&& other
.wire
)
1831 if (wire
->name
!= other
.wire
->name
)
1832 return wire
->name
< other
.wire
->name
;
1834 if (wire
!= other
.wire
)
1835 return wire
< other
.wire
;
1837 if (offset
!= other
.offset
)
1838 return offset
< other
.offset
;
1840 if (width
!= other
.width
)
1841 return width
< other
.width
;
1843 return data
.bits
< other
.data
.bits
;
1846 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1848 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1850 if (data
.bits
!= other
.data
.bits
)
1855 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1862 RTLIL::SigSpec::SigSpec()
1868 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1873 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1875 cover("kernel.rtlil.sigspec.init.list");
1880 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1881 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1885 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1887 cover("kernel.rtlil.sigspec.assign");
1889 width_
= other
.width_
;
1890 hash_
= other
.hash_
;
1891 chunks_
= other
.chunks_
;
1894 if (!other
.bits_
.empty())
1896 RTLIL::SigChunk
*last
= NULL
;
1897 int last_end_offset
= 0;
1899 for (auto &bit
: other
.bits_
) {
1900 if (last
&& bit
.wire
== last
->wire
) {
1901 if (bit
.wire
== NULL
) {
1902 last
->data
.bits
.push_back(bit
.data
);
1905 } else if (last_end_offset
== bit
.offset
) {
1911 chunks_
.push_back(bit
);
1912 last
= &chunks_
.back();
1913 last_end_offset
= bit
.offset
+ 1;
1922 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1924 cover("kernel.rtlil.sigspec.init.const");
1926 chunks_
.push_back(RTLIL::SigChunk(value
));
1927 width_
= chunks_
.back().width
;
1932 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1934 cover("kernel.rtlil.sigspec.init.chunk");
1936 chunks_
.push_back(chunk
);
1937 width_
= chunks_
.back().width
;
1942 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1944 cover("kernel.rtlil.sigspec.init.wire");
1946 chunks_
.push_back(RTLIL::SigChunk(wire
));
1947 width_
= chunks_
.back().width
;
1952 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1954 cover("kernel.rtlil.sigspec.init.wire_part");
1956 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1957 width_
= chunks_
.back().width
;
1962 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1964 cover("kernel.rtlil.sigspec.init.str");
1966 chunks_
.push_back(RTLIL::SigChunk(str
));
1967 width_
= chunks_
.back().width
;
1972 RTLIL::SigSpec::SigSpec(int val
, int width
)
1974 cover("kernel.rtlil.sigspec.init.int");
1976 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1982 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1984 cover("kernel.rtlil.sigspec.init.state");
1986 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1992 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1994 cover("kernel.rtlil.sigspec.init.bit");
1996 if (bit
.wire
== NULL
)
1997 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1999 for (int i
= 0; i
< width
; i
++)
2000 chunks_
.push_back(bit
);
2006 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2008 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2012 for (auto &c
: chunks
)
2017 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2019 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2023 for (auto &bit
: bits
)
2028 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2030 cover("kernel.rtlil.sigspec.init.stdset_bits");
2034 for (auto &bit
: bits
)
2039 void RTLIL::SigSpec::pack() const
2041 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2043 if (that
->bits_
.empty())
2046 cover("kernel.rtlil.sigspec.convert.pack");
2047 log_assert(that
->chunks_
.empty());
2049 std::vector
<RTLIL::SigBit
> old_bits
;
2050 old_bits
.swap(that
->bits_
);
2052 RTLIL::SigChunk
*last
= NULL
;
2053 int last_end_offset
= 0;
2055 for (auto &bit
: old_bits
) {
2056 if (last
&& bit
.wire
== last
->wire
) {
2057 if (bit
.wire
== NULL
) {
2058 last
->data
.bits
.push_back(bit
.data
);
2061 } else if (last_end_offset
== bit
.offset
) {
2067 that
->chunks_
.push_back(bit
);
2068 last
= &that
->chunks_
.back();
2069 last_end_offset
= bit
.offset
+ 1;
2075 void RTLIL::SigSpec::unpack() const
2077 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2079 if (that
->chunks_
.empty())
2082 cover("kernel.rtlil.sigspec.convert.unpack");
2083 log_assert(that
->bits_
.empty());
2085 that
->bits_
.reserve(that
->width_
);
2086 for (auto &c
: that
->chunks_
)
2087 for (int i
= 0; i
< c
.width
; i
++)
2088 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2090 that
->chunks_
.clear();
2094 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2096 void RTLIL::SigSpec::hash() const
2098 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2100 if (that
->hash_
!= 0)
2103 cover("kernel.rtlil.sigspec.hash");
2107 for (auto &c
: that
->chunks_
)
2108 if (c
.wire
== NULL
) {
2109 for (auto &v
: c
.data
.bits
)
2110 DJB2(that
->hash_
, v
);
2112 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2113 DJB2(that
->hash_
, c
.offset
);
2114 DJB2(that
->hash_
, c
.width
);
2117 if (that
->hash_
== 0)
2121 void RTLIL::SigSpec::sort()
2124 cover("kernel.rtlil.sigspec.sort");
2125 std::sort(bits_
.begin(), bits_
.end());
2128 void RTLIL::SigSpec::sort_and_unify()
2130 cover("kernel.rtlil.sigspec.sort_and_unify");
2131 *this = this->to_sigbit_set();
2134 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2136 replace(pattern
, with
, this);
2139 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2141 log_assert(pattern
.width_
== with
.width_
);
2146 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2148 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2149 if (pattern
.bits_
[i
].wire
!= NULL
)
2150 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2152 replace(rules
, other
);
2155 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2157 replace(rules
, this);
2160 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2162 cover("kernel.rtlil.sigspec.replace");
2164 log_assert(other
!= NULL
);
2165 log_assert(width_
== other
->width_
);
2170 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2171 auto it
= rules
.find(bits_
[i
]);
2172 if (it
!= rules
.end())
2173 other
->bits_
[i
] = it
->second
;
2179 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2181 remove2(pattern
, NULL
);
2184 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2186 RTLIL::SigSpec tmp
= *this;
2187 tmp
.remove2(pattern
, other
);
2190 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2192 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2193 remove2(pattern_bits
, other
);
2196 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2198 remove2(pattern
, NULL
);
2201 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2203 RTLIL::SigSpec tmp
= *this;
2204 tmp
.remove2(pattern
, other
);
2207 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2210 cover("kernel.rtlil.sigspec.remove_other");
2212 cover("kernel.rtlil.sigspec.remove");
2216 if (other
!= NULL
) {
2217 log_assert(width_
== other
->width_
);
2221 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2223 new_bits
.resize(SIZE(bits_
));
2225 new_other_bits
.resize(SIZE(bits_
));
2228 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2229 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2232 new_other_bits
[k
] = other
->bits_
[i
];
2233 new_bits
[k
++] = bits_
[i
];
2238 new_other_bits
.resize(k
);
2240 bits_
.swap(new_bits
);
2241 width_
= SIZE(bits_
);
2243 if (other
!= NULL
) {
2244 other
->bits_
.swap(new_other_bits
);
2245 other
->width_
= SIZE(other
->bits_
);
2251 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2253 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2254 return extract(pattern_bits
, other
);
2257 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2260 cover("kernel.rtlil.sigspec.extract_other");
2262 cover("kernel.rtlil.sigspec.extract");
2264 log_assert(other
== NULL
|| width_
== other
->width_
);
2266 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2270 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2271 for (int i
= 0; i
< width_
; i
++)
2272 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2273 ret
.append_bit(bits_other
[i
]);
2275 for (int i
= 0; i
< width_
; i
++)
2276 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2277 ret
.append_bit(bits_match
[i
]);
2284 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2286 cover("kernel.rtlil.sigspec.replace_pos");
2291 log_assert(offset
>= 0);
2292 log_assert(with
.width_
>= 0);
2293 log_assert(offset
+with
.width_
<= width_
);
2295 for (int i
= 0; i
< with
.width_
; i
++)
2296 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2301 void RTLIL::SigSpec::remove_const()
2305 cover("kernel.rtlil.sigspec.remove_const.packed");
2307 std::vector
<RTLIL::SigChunk
> new_chunks
;
2308 new_chunks
.reserve(SIZE(chunks_
));
2311 for (auto &chunk
: chunks_
)
2312 if (chunk
.wire
!= NULL
) {
2313 new_chunks
.push_back(chunk
);
2314 width_
+= chunk
.width
;
2317 chunks_
.swap(new_chunks
);
2321 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2323 std::vector
<RTLIL::SigBit
> new_bits
;
2324 new_bits
.reserve(width_
);
2326 for (auto &bit
: bits_
)
2327 if (bit
.wire
!= NULL
)
2328 new_bits
.push_back(bit
);
2330 bits_
.swap(new_bits
);
2331 width_
= bits_
.size();
2337 void RTLIL::SigSpec::remove(int offset
, int length
)
2339 cover("kernel.rtlil.sigspec.remove_pos");
2343 log_assert(offset
>= 0);
2344 log_assert(length
>= 0);
2345 log_assert(offset
+ length
<= width_
);
2347 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2348 width_
= bits_
.size();
2353 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2356 cover("kernel.rtlil.sigspec.extract_pos");
2357 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2360 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2362 if (signal
.width_
== 0)
2370 cover("kernel.rtlil.sigspec.append");
2372 if (packed() != signal
.packed()) {
2378 for (auto &other_c
: signal
.chunks_
)
2380 auto &my_last_c
= chunks_
.back();
2381 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2382 auto &this_data
= my_last_c
.data
.bits
;
2383 auto &other_data
= other_c
.data
.bits
;
2384 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2385 my_last_c
.width
+= other_c
.width
;
2387 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2388 my_last_c
.width
+= other_c
.width
;
2390 chunks_
.push_back(other_c
);
2393 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2395 width_
+= signal
.width_
;
2399 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2403 cover("kernel.rtlil.sigspec.append_bit.packed");
2405 if (chunks_
.size() == 0)
2406 chunks_
.push_back(bit
);
2408 if (bit
.wire
== NULL
)
2409 if (chunks_
.back().wire
== NULL
) {
2410 chunks_
.back().data
.bits
.push_back(bit
.data
);
2411 chunks_
.back().width
++;
2413 chunks_
.push_back(bit
);
2415 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2416 chunks_
.back().width
++;
2418 chunks_
.push_back(bit
);
2422 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2423 bits_
.push_back(bit
);
2430 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2432 cover("kernel.rtlil.sigspec.extend");
2437 remove(width
, width_
- width
);
2439 if (width_
< width
) {
2440 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2441 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2442 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2443 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2444 while (width_
< width
)
2449 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2451 cover("kernel.rtlil.sigspec.extend_u0");
2456 remove(width
, width_
- width
);
2458 if (width_
< width
) {
2459 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2461 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2462 while (width_
< width
)
2468 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2470 cover("kernel.rtlil.sigspec.repeat");
2473 for (int i
= 0; i
< num
; i
++)
2479 void RTLIL::SigSpec::check() const
2483 cover("kernel.rtlil.sigspec.check.skip");
2487 cover("kernel.rtlil.sigspec.check.packed");
2490 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2491 const RTLIL::SigChunk chunk
= chunks_
[i
];
2492 if (chunk
.wire
== NULL
) {
2494 log_assert(chunks_
[i
-1].wire
!= NULL
);
2495 log_assert(chunk
.offset
== 0);
2496 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2498 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2499 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2500 log_assert(chunk
.offset
>= 0);
2501 log_assert(chunk
.width
>= 0);
2502 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2503 log_assert(chunk
.data
.bits
.size() == 0);
2507 log_assert(w
== width_
);
2508 log_assert(bits_
.empty());
2512 cover("kernel.rtlil.sigspec.check.unpacked");
2514 log_assert(width_
== SIZE(bits_
));
2515 log_assert(chunks_
.empty());
2520 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2522 cover("kernel.rtlil.sigspec.comp_lt");
2527 if (width_
!= other
.width_
)
2528 return width_
< other
.width_
;
2533 if (chunks_
.size() != other
.chunks_
.size())
2534 return chunks_
.size() < other
.chunks_
.size();
2539 if (hash_
!= other
.hash_
)
2540 return hash_
< other
.hash_
;
2542 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2543 if (chunks_
[i
] != other
.chunks_
[i
]) {
2544 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2545 return chunks_
[i
] < other
.chunks_
[i
];
2548 cover("kernel.rtlil.sigspec.comp_lt.equal");
2552 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2554 cover("kernel.rtlil.sigspec.comp_eq");
2559 if (width_
!= other
.width_
)
2565 if (chunks_
.size() != chunks_
.size())
2571 if (hash_
!= other
.hash_
)
2574 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2575 if (chunks_
[i
] != other
.chunks_
[i
]) {
2576 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2580 cover("kernel.rtlil.sigspec.comp_eq.equal");
2584 bool RTLIL::SigSpec::is_wire() const
2586 cover("kernel.rtlil.sigspec.is_wire");
2589 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2592 bool RTLIL::SigSpec::is_chunk() const
2594 cover("kernel.rtlil.sigspec.is_chunk");
2597 return SIZE(chunks_
) == 1;
2600 bool RTLIL::SigSpec::is_fully_const() const
2602 cover("kernel.rtlil.sigspec.is_fully_const");
2605 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2606 if (it
->width
> 0 && it
->wire
!= NULL
)
2611 bool RTLIL::SigSpec::is_fully_def() const
2613 cover("kernel.rtlil.sigspec.is_fully_def");
2616 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2617 if (it
->width
> 0 && it
->wire
!= NULL
)
2619 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2620 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2626 bool RTLIL::SigSpec::is_fully_undef() const
2628 cover("kernel.rtlil.sigspec.is_fully_undef");
2631 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2632 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::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2641 bool RTLIL::SigSpec::has_marked_bits() const
2643 cover("kernel.rtlil.sigspec.has_marked_bits");
2646 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2647 if (it
->width
> 0 && it
->wire
== NULL
) {
2648 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2649 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2655 bool RTLIL::SigSpec::as_bool() const
2657 cover("kernel.rtlil.sigspec.as_bool");
2660 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2662 return chunks_
[0].data
.as_bool();
2666 int RTLIL::SigSpec::as_int(bool is_signed
) const
2668 cover("kernel.rtlil.sigspec.as_int");
2671 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2673 return chunks_
[0].data
.as_int(is_signed
);
2677 std::string
RTLIL::SigSpec::as_string() const
2679 cover("kernel.rtlil.sigspec.as_string");
2683 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2684 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2685 if (chunk
.wire
!= NULL
)
2686 for (int j
= 0; j
< chunk
.width
; j
++)
2689 str
+= chunk
.data
.as_string();
2694 RTLIL::Const
RTLIL::SigSpec::as_const() const
2696 cover("kernel.rtlil.sigspec.as_const");
2699 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2701 return chunks_
[0].data
;
2702 return RTLIL::Const();
2705 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2707 cover("kernel.rtlil.sigspec.as_wire");
2710 log_assert(is_wire());
2711 return chunks_
[0].wire
;
2714 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2716 cover("kernel.rtlil.sigspec.as_chunk");
2719 log_assert(is_chunk());
2723 bool RTLIL::SigSpec::match(std::string pattern
) const
2725 cover("kernel.rtlil.sigspec.match");
2728 std::string str
= as_string();
2729 log_assert(pattern
.size() == str
.size());
2731 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2732 if (pattern
[i
] == ' ')
2734 if (pattern
[i
] == '*') {
2735 if (str
[i
] != 'z' && str
[i
] != 'x')
2739 if (pattern
[i
] != str
[i
])
2746 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2748 cover("kernel.rtlil.sigspec.to_sigbit_set");
2751 std::set
<RTLIL::SigBit
> sigbits
;
2752 for (auto &c
: chunks_
)
2753 for (int i
= 0; i
< c
.width
; i
++)
2754 sigbits
.insert(RTLIL::SigBit(c
, i
));
2758 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2760 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2766 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2768 cover("kernel.rtlil.sigspec.to_sigbit_map");
2773 log_assert(width_
== other
.width_
);
2775 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2776 for (int i
= 0; i
< width_
; i
++)
2777 new_map
[bits_
[i
]] = other
.bits_
[i
];
2782 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2784 cover("kernel.rtlil.sigspec.to_single_sigbit");
2787 log_assert(width_
== 1);
2788 for (auto &c
: chunks_
)
2790 return RTLIL::SigBit(c
);
2794 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2796 size_t start
= 0, end
= 0;
2797 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2798 tokens
.push_back(text
.substr(start
, end
- start
));
2801 tokens
.push_back(text
.substr(start
));
2804 static int sigspec_parse_get_dummy_line_num()
2809 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2811 cover("kernel.rtlil.sigspec.parse");
2813 std::vector
<std::string
> tokens
;
2814 sigspec_parse_split(tokens
, str
, ',');
2816 sig
= RTLIL::SigSpec();
2817 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2819 std::string netname
= tokens
[tokidx
];
2820 std::string indices
;
2822 if (netname
.size() == 0)
2825 if ('0' <= netname
[0] && netname
[0] <= '9') {
2826 cover("kernel.rtlil.sigspec.parse.const");
2827 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2828 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2831 sig
.append(RTLIL::Const(ast
->bits
));
2839 cover("kernel.rtlil.sigspec.parse.net");
2841 if (netname
[0] != '$' && netname
[0] != '\\')
2842 netname
= "\\" + netname
;
2844 if (module
->wires_
.count(netname
) == 0) {
2845 size_t indices_pos
= netname
.size()-1;
2846 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2849 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2850 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2852 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2854 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2855 indices
= netname
.substr(indices_pos
);
2856 netname
= netname
.substr(0, indices_pos
);
2861 if (module
->wires_
.count(netname
) == 0)
2864 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2865 if (!indices
.empty()) {
2866 std::vector
<std::string
> index_tokens
;
2867 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2868 if (index_tokens
.size() == 1) {
2869 cover("kernel.rtlil.sigspec.parse.bit_sel");
2870 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2872 cover("kernel.rtlil.sigspec.parse.part_sel");
2873 int a
= atoi(index_tokens
.at(0).c_str());
2874 int b
= atoi(index_tokens
.at(1).c_str());
2879 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2888 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2890 if (str
.empty() || str
[0] != '@')
2891 return parse(sig
, module
, str
);
2893 cover("kernel.rtlil.sigspec.parse.sel");
2895 str
= RTLIL::escape_id(str
.substr(1));
2896 if (design
->selection_vars
.count(str
) == 0)
2899 sig
= RTLIL::SigSpec();
2900 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2901 for (auto &it
: module
->wires_
)
2902 if (sel
.selected_member(module
->name
, it
.first
))
2903 sig
.append(it
.second
);
2908 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2911 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2912 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2917 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2918 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2922 if (lhs
.chunks_
.size() == 1) {
2923 char *p
= (char*)str
.c_str(), *endptr
;
2924 long long int val
= strtoll(p
, &endptr
, 10);
2925 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2926 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2927 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2932 return parse(sig
, module
, str
);
2935 RTLIL::CaseRule::~CaseRule()
2937 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2941 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2943 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2944 new_caserule
->compare
= compare
;
2945 new_caserule
->actions
= actions
;
2946 for (auto &it
: switches
)
2947 new_caserule
->switches
.push_back(it
->clone());
2948 return new_caserule
;
2951 RTLIL::SwitchRule::~SwitchRule()
2953 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2957 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2959 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2960 new_switchrule
->signal
= signal
;
2961 new_switchrule
->attributes
= attributes
;
2962 for (auto &it
: cases
)
2963 new_switchrule
->cases
.push_back(it
->clone());
2964 return new_switchrule
;
2968 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2970 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2971 new_syncrule
->type
= type
;
2972 new_syncrule
->signal
= signal
;
2973 new_syncrule
->actions
= actions
;
2974 return new_syncrule
;
2977 RTLIL::Process::~Process()
2979 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2983 RTLIL::Process
*RTLIL::Process::clone() const
2985 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2987 new_proc
->name
= name
;
2988 new_proc
->attributes
= attributes
;
2990 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2991 new_proc
->root_case
= *rc_ptr
;
2992 rc_ptr
->switches
.clear();
2995 for (auto &it
: syncs
)
2996 new_proc
->syncs
.push_back(it
->clone());