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
== "$logic_not") {
561 param_bool("\\A_SIGNED");
562 port("\\A", param("\\A_WIDTH"));
563 port("\\Y", param("\\Y_WIDTH"));
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);
578 if (cell
->type
== "$slice") {
580 port("\\A", param("\\A_WIDTH"));
581 port("\\Y", param("\\Y_WIDTH"));
582 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
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"));
596 if (cell
->type
== "$mux") {
597 port("\\A", param("\\WIDTH"));
598 port("\\B", param("\\WIDTH"));
600 port("\\Y", param("\\WIDTH"));
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"));
614 if (cell
->type
== "$lut") {
616 port("\\A", param("\\WIDTH"));
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"));
632 if (cell
->type
== "$dff") {
633 param_bool("\\CLK_POLARITY");
635 port("\\D", param("\\WIDTH"));
636 port("\\Q", param("\\WIDTH"));
641 if (cell
->type
== "$dffsr") {
642 param_bool("\\CLK_POLARITY");
643 param_bool("\\SET_POLARITY");
644 param_bool("\\CLR_POLARITY");
646 port("\\SET", param("\\WIDTH"));
647 port("\\CLR", param("\\WIDTH"));
648 port("\\D", param("\\WIDTH"));
649 port("\\Q", param("\\WIDTH"));
654 if (cell
->type
== "$adff") {
655 param_bool("\\CLK_POLARITY");
656 param_bool("\\ARST_POLARITY");
657 param_bits("\\ARST_VALUE", param("\\WIDTH"));
660 port("\\D", param("\\WIDTH"));
661 port("\\Q", param("\\WIDTH"));
666 if (cell
->type
== "$dlatch") {
667 param_bool("\\EN_POLARITY");
669 port("\\D", param("\\WIDTH"));
670 port("\\Q", param("\\WIDTH"));
675 if (cell
->type
== "$dlatchsr") {
676 param_bool("\\EN_POLARITY");
677 param_bool("\\SET_POLARITY");
678 param_bool("\\CLR_POLARITY");
680 port("\\SET", param("\\WIDTH"));
681 port("\\CLR", param("\\WIDTH"));
682 port("\\D", param("\\WIDTH"));
683 port("\\Q", param("\\WIDTH"));
688 if (cell
->type
== "$fsm") {
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")));
701 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
702 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
707 if (cell
->type
== "$memrd") {
709 param_bool("\\CLK_ENABLE");
710 param_bool("\\CLK_POLARITY");
711 param_bool("\\TRANSPARENT");
713 port("\\ADDR", param("\\ABITS"));
714 port("\\DATA", param("\\WIDTH"));
719 if (cell
->type
== "$memwr") {
721 param_bool("\\CLK_ENABLE");
722 param_bool("\\CLK_POLARITY");
725 port("\\EN", param("\\WIDTH"));
726 port("\\ADDR", param("\\ABITS"));
727 port("\\DATA", param("\\WIDTH"));
732 if (cell
->type
== "$mem") {
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"));
752 if (cell
->type
== "$assert") {
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; }
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; }
777 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
778 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
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; }
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; }
798 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
799 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
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; }
816 void RTLIL::Module::check()
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;
837 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
839 for (auto port_declared
: ports_declared
)
840 log_assert(port_declared
== true);
841 log_assert(SIZE(ports
) == SIZE(ports_declared
));
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());
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());
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
);
869 for (auto &it
: processes
) {
870 log_assert(it
.first
== it
.second
->name
);
871 log_assert(!it
.first
.empty());
872 // FIXME: More checks here..
875 for (auto &it
: connections_
) {
876 log_assert(it
.first
.size() == it
.second
.size());
881 for (auto &it
: attributes
)
882 log_assert(!it
.first
.empty());
886 void RTLIL::Module::optimize()
890 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
892 log_assert(new_mod
->refcount_wires_
== 0);
893 log_assert(new_mod
->refcount_cells_
== 0);
895 new_mod
->connections_
= connections_
;
896 new_mod
->attributes
= attributes
;
898 for (auto &it
: wires_
)
899 new_mod
->addWire(it
.first
, it
.second
);
901 for (auto &it
: memories
)
902 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
904 for (auto &it
: cells_
)
905 new_mod
->addCell(it
.first
, it
.second
);
907 for (auto &it
: processes
)
908 new_mod
->processes
[it
.first
] = it
.second
->clone();
910 struct RewriteSigSpecWorker
913 void operator()(RTLIL::SigSpec
&sig
)
915 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
916 for (auto &c
: chunks
)
918 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
923 RewriteSigSpecWorker rewriteSigSpecWorker
;
924 rewriteSigSpecWorker
.mod
= new_mod
;
925 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
926 new_mod
->fixup_ports();
929 RTLIL::Module
*RTLIL::Module::clone() const
931 RTLIL::Module
*new_mod
= new RTLIL::Module
;
932 new_mod
->name
= name
;
937 bool RTLIL::Module::has_memories() const
939 return !memories
.empty();
942 bool RTLIL::Module::has_processes() const
944 return !processes
.empty();
947 bool RTLIL::Module::has_memories_warn() const
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();
954 bool RTLIL::Module::has_processes_warn() const
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();
961 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
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
);
971 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
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
);
981 void RTLIL::Module::add(RTLIL::Wire
*wire
)
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
;
990 void RTLIL::Module::add(RTLIL::Cell
*cell
)
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
;
1000 struct DeleteWireWorker
1002 RTLIL::Module
*module
;
1003 const std::set
<RTLIL::Wire
*> *wires_p
;
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
);
1018 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1020 std::setPort
<RTLIL::Wire
*> wires_
;
1021 wires_
.insert(wire
);
1026 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1028 log_assert(refcount_wires_
== 0);
1030 DeleteWireWorker delete_wire_worker
;
1031 delete_wire_worker
.module
= this;
1032 delete_wire_worker
.wires_p
= &wires
;
1033 rewrite_sigspecs(delete_wire_worker
);
1035 for (auto &it
: wires
) {
1036 log_assert(wires_
.count(it
->name
) != 0);
1037 wires_
.erase(it
->name
);
1042 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1044 log_assert(cells_
.count(cell
->name
) != 0);
1045 log_assert(refcount_cells_
== 0);
1046 cells_
.erase(cell
->name
);
1050 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1052 log_assert(wires_
[wire
->name
] == wire
);
1053 log_assert(refcount_wires_
== 0);
1054 wires_
.erase(wire
->name
);
1055 wire
->name
= new_name
;
1059 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1061 log_assert(cells_
[cell
->name
] == cell
);
1062 log_assert(refcount_wires_
== 0);
1063 cells_
.erase(cell
->name
);
1064 cell
->name
= new_name
;
1068 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
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
);
1079 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1081 log_assert(wires_
[w1
->name
] == w1
);
1082 log_assert(wires_
[w2
->name
] == w2
);
1083 log_assert(refcount_wires_
== 0);
1085 wires_
.erase(w1
->name
);
1086 wires_
.erase(w2
->name
);
1088 std::swap(w1
->name
, w2
->name
);
1090 wires_
[w1
->name
] = w1
;
1091 wires_
[w2
->name
] = w2
;
1094 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1096 log_assert(cells_
[c1
->name
] == c1
);
1097 log_assert(cells_
[c2
->name
] == c2
);
1098 log_assert(refcount_cells_
== 0);
1100 cells_
.erase(c1
->name
);
1101 cells_
.erase(c2
->name
);
1103 std::swap(c1
->name
, c2
->name
);
1105 cells_
[c1
->name
] = c1
;
1106 cells_
[c2
->name
] = c2
;
1109 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1112 return uniquify(name
, index
);
1115 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1118 if (count_id(name
) == 0)
1124 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1125 if (count_id(new_name
) == 0)
1131 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1133 if (a
->port_id
&& !b
->port_id
)
1135 if (!a
->port_id
&& b
->port_id
)
1138 if (a
->port_id
== b
->port_id
)
1139 return a
->name
< b
->name
;
1140 return a
->port_id
< b
->port_id
;
1143 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1145 for (auto mon
: monitors
)
1146 mon
->notify_connect(this, conn
);
1149 for (auto mon
: design
->monitors
)
1150 mon
->notify_connect(this, conn
);
1152 connections_
.push_back(conn
);
1155 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1157 connect(RTLIL::SigSig(lhs
, rhs
));
1160 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1162 for (auto mon
: monitors
)
1163 mon
->notify_connect(this, new_conn
);
1166 for (auto mon
: design
->monitors
)
1167 mon
->notify_connect(this, new_conn
);
1169 connections_
= new_conn
;
1172 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1174 return connections_
;
1177 void RTLIL::Module::fixup_ports()
1179 std::vector
<RTLIL::Wire
*> all_ports
;
1181 for (auto &w
: wires_
)
1182 if (w
.second
->port_input
|| w
.second
->port_output
)
1183 all_ports
.push_back(w
.second
);
1185 w
.second
->port_id
= 0;
1187 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
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;
1196 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1198 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1200 wire
->width
= width
;
1205 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
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
;
1218 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1220 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1227 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1229 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1230 cell
->connections_
= other
->connections_
;
1231 cell
->parameters
= other
->parameters
;
1232 cell
->attributes
= other
->attributes
;
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); \
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); \
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")
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); \
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); \
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")
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); \
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); \
1324 DEF_METHOD(Mux
, "$mux", 0)
1325 DEF_METHOD(Pmux
, "$pmux", 1)
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); \
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); \
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); \
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); \
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); \
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); \
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); \
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); \
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
)
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
)
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
);
1413 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
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
);
1424 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
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
);
1435 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
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
);
1445 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1447 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1448 cell
->setPort("\\A", sig_a
);
1449 cell
->setPort("\\EN", sig_en
);
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
)
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
);
1465 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
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
);
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
)
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
);
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
)
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
);
1507 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
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
);
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
)
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
);
1534 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
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
);
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
)
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
);
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
)
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
);
1566 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
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
);
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
)
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
);
1595 port_output
= false;
1599 RTLIL::Memory::Memory()
1605 RTLIL::Cell::Cell() : module(nullptr)
1609 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1611 return connections_
.count(portname
) != 0;
1614 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1616 RTLIL::SigSpec signal
;
1617 auto conn_it
= connections_
.find(portname
);
1619 if (conn_it
!= connections_
.end())
1621 for (auto mon
: module
->monitors
)
1622 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1625 for (auto mon
: module
->design
->monitors
)
1626 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1628 connections_
.erase(conn_it
);
1632 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1634 auto conn_it
= connections_
.find(portname
);
1636 if (conn_it
== connections_
.end()) {
1637 connections_
[portname
] = RTLIL::SigSpec();
1638 conn_it
= connections_
.find(portname
);
1639 log_assert(conn_it
!= connections_
.end());
1642 for (auto mon
: module
->monitors
)
1643 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1646 for (auto mon
: module
->design
->monitors
)
1647 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1649 conn_it
->second
= signal
;
1652 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1654 return connections_
.at(portname
);
1657 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1659 return connections_
;
1662 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1664 return parameters
.count(paramname
);
1667 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1669 parameters
.erase(paramname
);
1672 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1674 parameters
[paramname
] = value
;
1677 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1679 return parameters
.at(paramname
);
1682 void RTLIL::Cell::check()
1685 InternalCellChecker
checker(NULL
, this);
1690 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
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:")
1696 if (type
== "$mux" || type
== "$pmux")
1698 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1699 if (type
== "$pmux")
1700 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1705 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1707 if (connections_
.count("\\A")) {
1708 if (signedness_ab
) {
1710 parameters
["\\A_SIGNED"] = true;
1711 else if (parameters
.count("\\A_SIGNED") == 0)
1712 parameters
["\\A_SIGNED"] = false;
1714 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1717 if (connections_
.count("\\B")) {
1718 if (signedness_ab
) {
1720 parameters
["\\B_SIGNED"] = true;
1721 else if (parameters
.count("\\B_SIGNED") == 0)
1722 parameters
["\\B_SIGNED"] = false;
1724 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1727 if (connections_
.count("\\Y"))
1728 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1733 RTLIL::SigChunk::SigChunk()
1740 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1744 width
= data
.bits
.size();
1748 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1750 log_assert(wire
!= nullptr);
1752 this->width
= wire
->width
;
1756 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1758 log_assert(wire
!= nullptr);
1760 this->width
= width
;
1761 this->offset
= offset
;
1764 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1767 data
= RTLIL::Const(str
);
1768 width
= data
.bits
.size();
1772 RTLIL::SigChunk::SigChunk(int val
, int width
)
1775 data
= RTLIL::Const(val
, width
);
1776 this->width
= data
.bits
.size();
1780 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1783 data
= RTLIL::Const(bit
, width
);
1784 this->width
= data
.bits
.size();
1788 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1793 data
= RTLIL::Const(bit
.data
);
1795 offset
= bit
.offset
;
1799 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1801 RTLIL::SigChunk ret
;
1804 ret
.offset
= this->offset
+ offset
;
1807 for (int i
= 0; i
< length
; i
++)
1808 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1814 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1816 if (wire
&& other
.wire
)
1817 if (wire
->name
!= other
.wire
->name
)
1818 return wire
->name
< other
.wire
->name
;
1820 if (wire
!= other
.wire
)
1821 return wire
< other
.wire
;
1823 if (offset
!= other
.offset
)
1824 return offset
< other
.offset
;
1826 if (width
!= other
.width
)
1827 return width
< other
.width
;
1829 return data
.bits
< other
.data
.bits
;
1832 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1834 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1836 if (data
.bits
!= other
.data
.bits
)
1841 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1848 RTLIL::SigSpec::SigSpec()
1854 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1859 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1861 cover("kernel.rtlil.sigspec.init.list");
1866 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1867 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1871 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1873 cover("kernel.rtlil.sigspec.assign");
1875 width_
= other
.width_
;
1876 hash_
= other
.hash_
;
1877 chunks_
= other
.chunks_
;
1880 if (!other
.bits_
.empty())
1882 RTLIL::SigChunk
*last
= NULL
;
1883 int last_end_offset
= 0;
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
);
1891 } else if (last_end_offset
== bit
.offset
) {
1897 chunks_
.push_back(bit
);
1898 last
= &chunks_
.back();
1899 last_end_offset
= bit
.offset
+ 1;
1908 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1910 cover("kernel.rtlil.sigspec.init.const");
1912 chunks_
.push_back(RTLIL::SigChunk(value
));
1913 width_
= chunks_
.back().width
;
1918 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1920 cover("kernel.rtlil.sigspec.init.chunk");
1922 chunks_
.push_back(chunk
);
1923 width_
= chunks_
.back().width
;
1928 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1930 cover("kernel.rtlil.sigspec.init.wire");
1932 chunks_
.push_back(RTLIL::SigChunk(wire
));
1933 width_
= chunks_
.back().width
;
1938 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1940 cover("kernel.rtlil.sigspec.init.wire_part");
1942 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1943 width_
= chunks_
.back().width
;
1948 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1950 cover("kernel.rtlil.sigspec.init.str");
1952 chunks_
.push_back(RTLIL::SigChunk(str
));
1953 width_
= chunks_
.back().width
;
1958 RTLIL::SigSpec::SigSpec(int val
, int width
)
1960 cover("kernel.rtlil.sigspec.init.int");
1962 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1968 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1970 cover("kernel.rtlil.sigspec.init.state");
1972 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1978 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1980 cover("kernel.rtlil.sigspec.init.bit");
1982 if (bit
.wire
== NULL
)
1983 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1985 for (int i
= 0; i
< width
; i
++)
1986 chunks_
.push_back(bit
);
1992 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1994 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1998 for (auto &c
: chunks
)
2003 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2005 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2009 for (auto &bit
: bits
)
2014 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2016 cover("kernel.rtlil.sigspec.init.stdset_bits");
2020 for (auto &bit
: bits
)
2025 void RTLIL::SigSpec::pack() const
2027 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2029 if (that
->bits_
.empty())
2032 cover("kernel.rtlil.sigspec.convert.pack");
2033 log_assert(that
->chunks_
.empty());
2035 std::vector
<RTLIL::SigBit
> old_bits
;
2036 old_bits
.swap(that
->bits_
);
2038 RTLIL::SigChunk
*last
= NULL
;
2039 int last_end_offset
= 0;
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
);
2047 } else if (last_end_offset
== bit
.offset
) {
2053 that
->chunks_
.push_back(bit
);
2054 last
= &that
->chunks_
.back();
2055 last_end_offset
= bit
.offset
+ 1;
2061 void RTLIL::SigSpec::unpack() const
2063 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2065 if (that
->chunks_
.empty())
2068 cover("kernel.rtlil.sigspec.convert.unpack");
2069 log_assert(that
->bits_
.empty());
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
));
2076 that
->chunks_
.clear();
2080 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2082 void RTLIL::SigSpec::hash() const
2084 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2086 if (that
->hash_
!= 0)
2089 cover("kernel.rtlil.sigspec.hash");
2093 for (auto &c
: that
->chunks_
)
2094 if (c
.wire
== NULL
) {
2095 for (auto &v
: c
.data
.bits
)
2096 DJB2(that
->hash_
, v
);
2098 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2099 DJB2(that
->hash_
, c
.offset
);
2100 DJB2(that
->hash_
, c
.width
);
2103 if (that
->hash_
== 0)
2107 void RTLIL::SigSpec::sort()
2110 cover("kernel.rtlil.sigspec.sort");
2111 std::sort(bits_
.begin(), bits_
.end());
2114 void RTLIL::SigSpec::sort_and_unify()
2116 cover("kernel.rtlil.sigspec.sort_and_unify");
2117 *this = this->to_sigbit_set();
2120 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2122 replace(pattern
, with
, this);
2125 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2127 log_assert(pattern
.width_
== with
.width_
);
2132 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
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
];
2138 replace(rules
, other
);
2141 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2143 replace(rules
, this);
2146 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2148 cover("kernel.rtlil.sigspec.replace");
2150 log_assert(other
!= NULL
);
2151 log_assert(width_
== other
->width_
);
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
;
2165 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2167 remove2(pattern
, NULL
);
2170 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2172 RTLIL::SigSpec tmp
= *this;
2173 tmp
.remove2(pattern
, other
);
2176 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2178 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2179 remove2(pattern_bits
, other
);
2182 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2184 remove2(pattern
, NULL
);
2187 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2189 RTLIL::SigSpec tmp
= *this;
2190 tmp
.remove2(pattern
, other
);
2193 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2196 cover("kernel.rtlil.sigspec.remove_other");
2198 cover("kernel.rtlil.sigspec.remove");
2202 if (other
!= NULL
) {
2203 log_assert(width_
== other
->width_
);
2207 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2209 new_bits
.resize(SIZE(bits_
));
2211 new_other_bits
.resize(SIZE(bits_
));
2214 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2215 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2218 new_other_bits
[k
] = other
->bits_
[i
];
2219 new_bits
[k
++] = bits_
[i
];
2224 new_other_bits
.resize(k
);
2226 bits_
.swap(new_bits
);
2227 width_
= SIZE(bits_
);
2229 if (other
!= NULL
) {
2230 other
->bits_
.swap(new_other_bits
);
2231 other
->width_
= SIZE(other
->bits_
);
2237 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2239 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2240 return extract(pattern_bits
, other
);
2243 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2246 cover("kernel.rtlil.sigspec.extract_other");
2248 cover("kernel.rtlil.sigspec.extract");
2250 log_assert(other
== NULL
|| width_
== other
->width_
);
2252 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
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
]);
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
]);
2270 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2272 cover("kernel.rtlil.sigspec.replace_pos");
2277 log_assert(offset
>= 0);
2278 log_assert(with
.width_
>= 0);
2279 log_assert(offset
+with
.width_
<= width_
);
2281 for (int i
= 0; i
< with
.width_
; i
++)
2282 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2287 void RTLIL::SigSpec::remove_const()
2291 cover("kernel.rtlil.sigspec.remove_const.packed");
2293 std::vector
<RTLIL::SigChunk
> new_chunks
;
2294 new_chunks
.reserve(SIZE(chunks_
));
2297 for (auto &chunk
: chunks_
)
2298 if (chunk
.wire
!= NULL
) {
2299 new_chunks
.push_back(chunk
);
2300 width_
+= chunk
.width
;
2303 chunks_
.swap(new_chunks
);
2307 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2309 std::vector
<RTLIL::SigBit
> new_bits
;
2310 new_bits
.reserve(width_
);
2312 for (auto &bit
: bits_
)
2313 if (bit
.wire
!= NULL
)
2314 new_bits
.push_back(bit
);
2316 bits_
.swap(new_bits
);
2317 width_
= bits_
.size();
2323 void RTLIL::SigSpec::remove(int offset
, int length
)
2325 cover("kernel.rtlil.sigspec.remove_pos");
2329 log_assert(offset
>= 0);
2330 log_assert(length
>= 0);
2331 log_assert(offset
+ length
<= width_
);
2333 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2334 width_
= bits_
.size();
2339 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2342 cover("kernel.rtlil.sigspec.extract_pos");
2343 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2346 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2348 if (signal
.width_
== 0)
2356 cover("kernel.rtlil.sigspec.append");
2358 if (packed() != signal
.packed()) {
2364 for (auto &other_c
: signal
.chunks_
)
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
;
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
;
2376 chunks_
.push_back(other_c
);
2379 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2381 width_
+= signal
.width_
;
2385 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2389 cover("kernel.rtlil.sigspec.append_bit.packed");
2391 if (chunks_
.size() == 0)
2392 chunks_
.push_back(bit
);
2394 if (bit
.wire
== NULL
)
2395 if (chunks_
.back().wire
== NULL
) {
2396 chunks_
.back().data
.bits
.push_back(bit
.data
);
2397 chunks_
.back().width
++;
2399 chunks_
.push_back(bit
);
2401 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2402 chunks_
.back().width
++;
2404 chunks_
.push_back(bit
);
2408 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2409 bits_
.push_back(bit
);
2416 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2418 cover("kernel.rtlil.sigspec.extend");
2423 remove(width
, width_
- width
);
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
)
2435 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2437 cover("kernel.rtlil.sigspec.extend_u0");
2442 remove(width
, width_
- width
);
2444 if (width_
< width
) {
2445 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2447 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2448 while (width_
< width
)
2454 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2456 cover("kernel.rtlil.sigspec.repeat");
2459 for (int i
= 0; i
< num
; i
++)
2465 void RTLIL::SigSpec::check() const
2469 cover("kernel.rtlil.sigspec.check.skip");
2473 cover("kernel.rtlil.sigspec.check.packed");
2476 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2477 const RTLIL::SigChunk chunk
= chunks_
[i
];
2478 if (chunk
.wire
== NULL
) {
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
);
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);
2493 log_assert(w
== width_
);
2494 log_assert(bits_
.empty());
2498 cover("kernel.rtlil.sigspec.check.unpacked");
2500 log_assert(width_
== SIZE(bits_
));
2501 log_assert(chunks_
.empty());
2506 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2508 cover("kernel.rtlil.sigspec.comp_lt");
2513 if (width_
!= other
.width_
)
2514 return width_
< other
.width_
;
2519 if (chunks_
.size() != other
.chunks_
.size())
2520 return chunks_
.size() < other
.chunks_
.size();
2525 if (hash_
!= other
.hash_
)
2526 return hash_
< other
.hash_
;
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
];
2534 cover("kernel.rtlil.sigspec.comp_lt.equal");
2538 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2540 cover("kernel.rtlil.sigspec.comp_eq");
2545 if (width_
!= other
.width_
)
2551 if (chunks_
.size() != chunks_
.size())
2557 if (hash_
!= other
.hash_
)
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");
2566 cover("kernel.rtlil.sigspec.comp_eq.equal");
2570 bool RTLIL::SigSpec::is_wire() const
2572 cover("kernel.rtlil.sigspec.is_wire");
2575 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2578 bool RTLIL::SigSpec::is_chunk() const
2580 cover("kernel.rtlil.sigspec.is_chunk");
2583 return SIZE(chunks_
) == 1;
2586 bool RTLIL::SigSpec::is_fully_const() const
2588 cover("kernel.rtlil.sigspec.is_fully_const");
2591 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2592 if (it
->width
> 0 && it
->wire
!= NULL
)
2597 bool RTLIL::SigSpec::is_fully_def() const
2599 cover("kernel.rtlil.sigspec.is_fully_def");
2602 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2603 if (it
->width
> 0 && it
->wire
!= NULL
)
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
)
2612 bool RTLIL::SigSpec::is_fully_undef() const
2614 cover("kernel.rtlil.sigspec.is_fully_undef");
2617 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2618 if (it
->width
> 0 && it
->wire
!= NULL
)
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
)
2627 bool RTLIL::SigSpec::has_marked_bits() const
2629 cover("kernel.rtlil.sigspec.has_marked_bits");
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
)
2641 bool RTLIL::SigSpec::as_bool() const
2643 cover("kernel.rtlil.sigspec.as_bool");
2646 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2648 return chunks_
[0].data
.as_bool();
2652 int RTLIL::SigSpec::as_int(bool is_signed
) const
2654 cover("kernel.rtlil.sigspec.as_int");
2657 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2659 return chunks_
[0].data
.as_int(is_signed
);
2663 std::string
RTLIL::SigSpec::as_string() const
2665 cover("kernel.rtlil.sigspec.as_string");
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
++)
2675 str
+= chunk
.data
.as_string();
2680 RTLIL::Const
RTLIL::SigSpec::as_const() const
2682 cover("kernel.rtlil.sigspec.as_const");
2685 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2687 return chunks_
[0].data
;
2688 return RTLIL::Const();
2691 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2693 cover("kernel.rtlil.sigspec.as_wire");
2696 log_assert(is_wire());
2697 return chunks_
[0].wire
;
2700 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2702 cover("kernel.rtlil.sigspec.as_chunk");
2705 log_assert(is_chunk());
2709 bool RTLIL::SigSpec::match(std::string pattern
) const
2711 cover("kernel.rtlil.sigspec.match");
2714 std::string str
= as_string();
2715 log_assert(pattern
.size() == str
.size());
2717 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2718 if (pattern
[i
] == ' ')
2720 if (pattern
[i
] == '*') {
2721 if (str
[i
] != 'z' && str
[i
] != 'x')
2725 if (pattern
[i
] != str
[i
])
2732 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2734 cover("kernel.rtlil.sigspec.to_sigbit_set");
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
));
2744 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2746 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2752 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2754 cover("kernel.rtlil.sigspec.to_sigbit_map");
2759 log_assert(width_
== other
.width_
);
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
];
2768 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2770 cover("kernel.rtlil.sigspec.to_single_sigbit");
2773 log_assert(width_
== 1);
2774 for (auto &c
: chunks_
)
2776 return RTLIL::SigBit(c
);
2780 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
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
));
2787 tokens
.push_back(text
.substr(start
));
2790 static int sigspec_parse_get_dummy_line_num()
2795 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2797 cover("kernel.rtlil.sigspec.parse");
2799 std::vector
<std::string
> tokens
;
2800 sigspec_parse_split(tokens
, str
, ',');
2802 sig
= RTLIL::SigSpec();
2803 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2805 std::string netname
= tokens
[tokidx
];
2806 std::string indices
;
2808 if (netname
.size() == 0)
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
);
2817 sig
.append(RTLIL::Const(ast
->bits
));
2825 cover("kernel.rtlil.sigspec.parse.net");
2827 if (netname
[0] != '$' && netname
[0] != '\\')
2828 netname
= "\\" + netname
;
2830 if (module
->wires_
.count(netname
) == 0) {
2831 size_t indices_pos
= netname
.size()-1;
2832 if (indices_pos
> 2 && netname
[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
] == ':') {
2838 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2840 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2841 indices
= netname
.substr(indices_pos
);
2842 netname
= netname
.substr(0, indices_pos
);
2847 if (module
->wires_
.count(netname
) == 0)
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())));
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());
2865 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2874 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2876 if (str
.empty() || str
[0] != '@')
2877 return parse(sig
, module
, str
);
2879 cover("kernel.rtlil.sigspec.parse.sel");
2881 str
= RTLIL::escape_id(str
.substr(1));
2882 if (design
->selection_vars
.count(str
) == 0)
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
);
2894 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2897 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2898 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2903 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2904 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
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");
2918 return parse(sig
, module
, str
);
2921 RTLIL::CaseRule::~CaseRule()
2923 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2927 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
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
;
2937 RTLIL::SwitchRule::~SwitchRule()
2939 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2943 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
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
;
2954 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
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
;
2963 RTLIL::Process::~Process()
2965 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2969 RTLIL::Process
*RTLIL::Process::clone() const
2971 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2973 new_proc
->name
= name
;
2974 new_proc
->attributes
= attributes
;
2976 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2977 new_proc
->root_case
= *rc_ptr
;
2978 rc_ptr
->switches
.clear();
2981 for (auto &it
: syncs
)
2982 new_proc
->syncs
.push_back(it
->clone());