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() const
98 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
99 if (bits
[i
] == RTLIL::S1
)
104 std::string
RTLIL::Const::as_string() const
107 for (size_t i
= bits
.size(); i
> 0; i
--)
109 case S0
: ret
+= "0"; break;
110 case S1
: ret
+= "1"; break;
111 case Sx
: ret
+= "x"; break;
112 case Sz
: ret
+= "z"; break;
113 case Sa
: ret
+= "-"; break;
114 case Sm
: ret
+= "m"; break;
119 std::string
RTLIL::Const::decode_string() const
122 std::vector
<char> string_chars
;
123 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
125 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
126 if (bits
[i
+ j
] == RTLIL::State::S1
)
129 string_chars
.push_back(ch
);
131 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
132 string
+= string_chars
[i
];
136 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
140 if (selected_modules
.count(mod_name
) > 0)
142 if (selected_members
.count(mod_name
) > 0)
147 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
151 if (selected_modules
.count(mod_name
) > 0)
156 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
160 if (selected_modules
.count(mod_name
) > 0)
162 if (selected_members
.count(mod_name
) > 0)
163 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
168 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
170 if (full_selection
) {
171 selected_modules
.clear();
172 selected_members
.clear();
176 std::vector
<RTLIL::IdString
> del_list
, add_list
;
179 for (auto mod_name
: selected_modules
) {
180 if (design
->modules_
.count(mod_name
) == 0)
181 del_list
.push_back(mod_name
);
182 selected_members
.erase(mod_name
);
184 for (auto mod_name
: del_list
)
185 selected_modules
.erase(mod_name
);
188 for (auto &it
: selected_members
)
189 if (design
->modules_
.count(it
.first
) == 0)
190 del_list
.push_back(it
.first
);
191 for (auto mod_name
: del_list
)
192 selected_members
.erase(mod_name
);
194 for (auto &it
: selected_members
) {
196 for (auto memb_name
: it
.second
)
197 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
198 del_list
.push_back(memb_name
);
199 for (auto memb_name
: del_list
)
200 it
.second
.erase(memb_name
);
205 for (auto &it
: selected_members
)
206 if (it
.second
.size() == 0)
207 del_list
.push_back(it
.first
);
208 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
209 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
210 add_list
.push_back(it
.first
);
211 for (auto mod_name
: del_list
)
212 selected_members
.erase(mod_name
);
213 for (auto mod_name
: add_list
) {
214 selected_members
.erase(mod_name
);
215 selected_modules
.insert(mod_name
);
218 if (selected_modules
.size() == design
->modules_
.size()) {
219 full_selection
= true;
220 selected_modules
.clear();
221 selected_members
.clear();
225 RTLIL::Design::Design()
227 refcount_modules_
= 0;
230 RTLIL::Design::~Design()
232 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
236 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
238 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
241 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
243 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
246 void RTLIL::Design::add(RTLIL::Module
*module
)
248 log_assert(modules_
.count(module
->name
) == 0);
249 log_assert(refcount_modules_
== 0);
250 modules_
[module
->name
] = module
;
251 module
->design
= this;
253 for (auto mon
: monitors
)
254 mon
->notify_module_add(module
);
257 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
259 log_assert(modules_
.count(name
) == 0);
260 log_assert(refcount_modules_
== 0);
262 RTLIL::Module
*module
= new RTLIL::Module
;
263 modules_
[name
] = module
;
264 module
->design
= this;
267 for (auto mon
: monitors
)
268 mon
->notify_module_add(module
);
273 void RTLIL::Design::remove(RTLIL::Module
*module
)
275 for (auto mon
: monitors
)
276 mon
->notify_module_del(module
);
278 log_assert(modules_
.at(module
->name
) == module
);
279 modules_
.erase(module
->name
);
283 void RTLIL::Design::check()
286 for (auto &it
: modules_
) {
287 log_assert(this == it
.second
->design
);
288 log_assert(it
.first
== it
.second
->name
);
289 log_assert(!it
.first
.empty());
295 void RTLIL::Design::optimize()
297 for (auto &it
: modules_
)
298 it
.second
->optimize();
299 for (auto &it
: selection_stack
)
301 for (auto &it
: selection_vars
)
302 it
.second
.optimize(this);
305 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
307 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
309 if (selection_stack
.size() == 0)
311 return selection_stack
.back().selected_module(mod_name
);
314 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
316 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
318 if (selection_stack
.size() == 0)
320 return selection_stack
.back().selected_whole_module(mod_name
);
323 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
325 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
327 if (selection_stack
.size() == 0)
329 return selection_stack
.back().selected_member(mod_name
, memb_name
);
332 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
334 return selected_module(mod
->name
);
337 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
339 return selected_whole_module(mod
->name
);
342 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
344 std::vector
<RTLIL::Module
*> result
;
345 result
.reserve(modules_
.size());
346 for (auto &it
: modules_
)
347 if (selected_module(it
.first
))
348 result
.push_back(it
.second
);
352 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
354 std::vector
<RTLIL::Module
*> result
;
355 result
.reserve(modules_
.size());
356 for (auto &it
: modules_
)
357 if (selected_whole_module(it
.first
))
358 result
.push_back(it
.second
);
362 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
364 std::vector
<RTLIL::Module
*> result
;
365 result
.reserve(modules_
.size());
366 for (auto &it
: modules_
)
367 if (selected_whole_module(it
.first
))
368 result
.push_back(it
.second
);
369 else if (selected_module(it
.first
))
370 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
374 RTLIL::Module::Module()
381 RTLIL::Module::~Module()
383 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
385 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
387 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
389 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
393 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
395 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
398 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
400 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
405 struct InternalCellChecker
407 RTLIL::Module
*module
;
409 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
411 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
413 void error(int linenr
)
418 FILE *f
= open_memstream(&ptr
, &size
);
419 ILANG_BACKEND::dump_cell(f
, " ", cell
);
423 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
424 module
? module
->name
.c_str() : "", module
? "." : "",
425 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
428 int param(const char *name
)
430 if (cell
->parameters
.count(name
) == 0)
432 expected_params
.insert(name
);
433 return cell
->parameters
.at(name
).as_int();
436 int param_bool(const char *name
)
439 if (cell
->parameters
.at(name
).bits
.size() > 32)
441 if (v
!= 0 && v
!= 1)
446 void param_bits(const char *name
, int width
)
449 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
453 void port(const char *name
, int width
)
455 if (!cell
->hasPort(name
))
457 if (cell
->getPort(name
).size() != width
)
459 expected_ports
.insert(name
);
462 void check_expected(bool check_matched_sign
= true)
464 for (auto ¶
: cell
->parameters
)
465 if (expected_params
.count(para
.first
) == 0)
467 for (auto &conn
: cell
->connections())
468 if (expected_ports
.count(conn
.first
) == 0)
471 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
472 bool a_is_signed
= param("\\A_SIGNED") != 0;
473 bool b_is_signed
= param("\\B_SIGNED") != 0;
474 if (a_is_signed
!= b_is_signed
)
479 void check_gate(const char *ports
)
481 if (cell
->parameters
.size() != 0)
484 for (const char *p
= ports
; *p
; p
++) {
485 char portname
[3] = { '\\', *p
, 0 };
486 if (!cell
->hasPort(portname
))
488 if (cell
->getPort(portname
).size() != 1)
492 for (auto &conn
: cell
->connections()) {
493 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
495 if (strchr(ports
, conn
.first
[1]) == NULL
)
502 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
503 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
506 if (cell
->type
.in("$not", "$pos", "$bu0", "$neg")) {
507 param_bool("\\A_SIGNED");
508 port("\\A", param("\\A_WIDTH"));
509 port("\\Y", param("\\Y_WIDTH"));
514 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
515 param_bool("\\A_SIGNED");
516 param_bool("\\B_SIGNED");
517 port("\\A", param("\\A_WIDTH"));
518 port("\\B", param("\\B_WIDTH"));
519 port("\\Y", param("\\Y_WIDTH"));
524 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
525 param_bool("\\A_SIGNED");
526 port("\\A", param("\\A_WIDTH"));
527 port("\\Y", param("\\Y_WIDTH"));
532 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
533 param_bool("\\A_SIGNED");
534 param_bool("\\B_SIGNED");
535 port("\\A", param("\\A_WIDTH"));
536 port("\\B", param("\\B_WIDTH"));
537 port("\\Y", param("\\Y_WIDTH"));
538 check_expected(false);
542 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
543 param_bool("\\A_SIGNED");
544 param_bool("\\B_SIGNED");
545 port("\\A", param("\\A_WIDTH"));
546 port("\\B", param("\\B_WIDTH"));
547 port("\\Y", param("\\Y_WIDTH"));
552 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
553 param_bool("\\A_SIGNED");
554 param_bool("\\B_SIGNED");
555 port("\\A", param("\\A_WIDTH"));
556 port("\\B", param("\\B_WIDTH"));
557 port("\\Y", param("\\Y_WIDTH"));
558 check_expected(cell
->type
!= "$pow");
562 if (cell
->type
== "$logic_not") {
563 param_bool("\\A_SIGNED");
564 port("\\A", param("\\A_WIDTH"));
565 port("\\Y", param("\\Y_WIDTH"));
570 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
571 param_bool("\\A_SIGNED");
572 param_bool("\\B_SIGNED");
573 port("\\A", param("\\A_WIDTH"));
574 port("\\B", param("\\B_WIDTH"));
575 port("\\Y", param("\\Y_WIDTH"));
576 check_expected(false);
580 if (cell
->type
== "$slice") {
582 port("\\A", param("\\A_WIDTH"));
583 port("\\Y", param("\\Y_WIDTH"));
584 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
590 if (cell
->type
== "$concat") {
591 port("\\A", param("\\A_WIDTH"));
592 port("\\B", param("\\B_WIDTH"));
593 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
598 if (cell
->type
== "$mux") {
599 port("\\A", param("\\WIDTH"));
600 port("\\B", param("\\WIDTH"));
602 port("\\Y", param("\\WIDTH"));
607 if (cell
->type
== "$pmux") {
608 port("\\A", param("\\WIDTH"));
609 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
610 port("\\S", param("\\S_WIDTH"));
611 port("\\Y", param("\\WIDTH"));
616 if (cell
->type
== "$lut") {
618 port("\\A", param("\\WIDTH"));
624 if (cell
->type
== "$sr") {
625 param_bool("\\SET_POLARITY");
626 param_bool("\\CLR_POLARITY");
627 port("\\SET", param("\\WIDTH"));
628 port("\\CLR", param("\\WIDTH"));
629 port("\\Q", param("\\WIDTH"));
634 if (cell
->type
== "$dff") {
635 param_bool("\\CLK_POLARITY");
637 port("\\D", param("\\WIDTH"));
638 port("\\Q", param("\\WIDTH"));
643 if (cell
->type
== "$dffsr") {
644 param_bool("\\CLK_POLARITY");
645 param_bool("\\SET_POLARITY");
646 param_bool("\\CLR_POLARITY");
648 port("\\SET", param("\\WIDTH"));
649 port("\\CLR", param("\\WIDTH"));
650 port("\\D", param("\\WIDTH"));
651 port("\\Q", param("\\WIDTH"));
656 if (cell
->type
== "$adff") {
657 param_bool("\\CLK_POLARITY");
658 param_bool("\\ARST_POLARITY");
659 param_bits("\\ARST_VALUE", param("\\WIDTH"));
662 port("\\D", param("\\WIDTH"));
663 port("\\Q", param("\\WIDTH"));
668 if (cell
->type
== "$dlatch") {
669 param_bool("\\EN_POLARITY");
671 port("\\D", param("\\WIDTH"));
672 port("\\Q", param("\\WIDTH"));
677 if (cell
->type
== "$dlatchsr") {
678 param_bool("\\EN_POLARITY");
679 param_bool("\\SET_POLARITY");
680 param_bool("\\CLR_POLARITY");
682 port("\\SET", param("\\WIDTH"));
683 port("\\CLR", param("\\WIDTH"));
684 port("\\D", param("\\WIDTH"));
685 port("\\Q", param("\\WIDTH"));
690 if (cell
->type
== "$fsm") {
692 param_bool("\\CLK_POLARITY");
693 param_bool("\\ARST_POLARITY");
694 param("\\STATE_BITS");
695 param("\\STATE_NUM");
696 param("\\STATE_NUM_LOG2");
697 param("\\STATE_RST");
698 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
699 param("\\TRANS_NUM");
700 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
703 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
704 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
709 if (cell
->type
== "$memrd") {
711 param_bool("\\CLK_ENABLE");
712 param_bool("\\CLK_POLARITY");
713 param_bool("\\TRANSPARENT");
715 port("\\ADDR", param("\\ABITS"));
716 port("\\DATA", param("\\WIDTH"));
721 if (cell
->type
== "$memwr") {
723 param_bool("\\CLK_ENABLE");
724 param_bool("\\CLK_POLARITY");
727 port("\\EN", param("\\WIDTH"));
728 port("\\ADDR", param("\\ABITS"));
729 port("\\DATA", param("\\WIDTH"));
734 if (cell
->type
== "$mem") {
738 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
739 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
740 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
741 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
742 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
743 port("\\RD_CLK", param("\\RD_PORTS"));
744 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
745 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
746 port("\\WR_CLK", param("\\WR_PORTS"));
747 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
748 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
749 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
754 if (cell
->type
== "$assert") {
761 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
762 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
763 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
764 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
765 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
766 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
767 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
768 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
769 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
770 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
771 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
772 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
774 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
775 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
776 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
777 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
779 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
780 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
782 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
783 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
784 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
785 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
786 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
787 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
788 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
789 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
791 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
792 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
793 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
794 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
795 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
796 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
797 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
798 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
800 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
801 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
803 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
804 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
805 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
806 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
807 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
808 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
809 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
810 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
818 void RTLIL::Module::check()
821 std::vector
<bool> ports_declared
;
822 for (auto &it
: wires_
) {
823 log_assert(this == it
.second
->module
);
824 log_assert(it
.first
== it
.second
->name
);
825 log_assert(!it
.first
.empty());
826 log_assert(it
.second
->width
>= 0);
827 log_assert(it
.second
->port_id
>= 0);
828 for (auto &it2
: it
.second
->attributes
)
829 log_assert(!it2
.first
.empty());
830 if (it
.second
->port_id
) {
831 log_assert(SIZE(ports
) >= it
.second
->port_id
);
832 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
833 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
834 if (SIZE(ports_declared
) < it
.second
->port_id
)
835 ports_declared
.resize(it
.second
->port_id
);
836 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
837 ports_declared
[it
.second
->port_id
-1] = true;
839 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
841 for (auto port_declared
: ports_declared
)
842 log_assert(port_declared
== true);
843 log_assert(SIZE(ports
) == SIZE(ports_declared
));
845 for (auto &it
: memories
) {
846 log_assert(it
.first
== it
.second
->name
);
847 log_assert(!it
.first
.empty());
848 log_assert(it
.second
->width
>= 0);
849 log_assert(it
.second
->size
>= 0);
850 for (auto &it2
: it
.second
->attributes
)
851 log_assert(!it2
.first
.empty());
854 for (auto &it
: cells_
) {
855 log_assert(this == it
.second
->module
);
856 log_assert(it
.first
== it
.second
->name
);
857 log_assert(!it
.first
.empty());
858 log_assert(!it
.second
->type
.empty());
859 for (auto &it2
: it
.second
->connections()) {
860 log_assert(!it2
.first
.empty());
863 for (auto &it2
: it
.second
->attributes
)
864 log_assert(!it2
.first
.empty());
865 for (auto &it2
: it
.second
->parameters
)
866 log_assert(!it2
.first
.empty());
867 InternalCellChecker
checker(this, it
.second
);
871 for (auto &it
: processes
) {
872 log_assert(it
.first
== it
.second
->name
);
873 log_assert(!it
.first
.empty());
874 // FIXME: More checks here..
877 for (auto &it
: connections_
) {
878 log_assert(it
.first
.size() == it
.second
.size());
883 for (auto &it
: attributes
)
884 log_assert(!it
.first
.empty());
888 void RTLIL::Module::optimize()
892 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
894 log_assert(new_mod
->refcount_wires_
== 0);
895 log_assert(new_mod
->refcount_cells_
== 0);
897 new_mod
->connections_
= connections_
;
898 new_mod
->attributes
= attributes
;
900 for (auto &it
: wires_
)
901 new_mod
->addWire(it
.first
, it
.second
);
903 for (auto &it
: memories
)
904 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
906 for (auto &it
: cells_
)
907 new_mod
->addCell(it
.first
, it
.second
);
909 for (auto &it
: processes
)
910 new_mod
->processes
[it
.first
] = it
.second
->clone();
912 struct RewriteSigSpecWorker
915 void operator()(RTLIL::SigSpec
&sig
)
917 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
918 for (auto &c
: chunks
)
920 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
925 RewriteSigSpecWorker rewriteSigSpecWorker
;
926 rewriteSigSpecWorker
.mod
= new_mod
;
927 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
928 new_mod
->fixup_ports();
931 RTLIL::Module
*RTLIL::Module::clone() const
933 RTLIL::Module
*new_mod
= new RTLIL::Module
;
934 new_mod
->name
= name
;
939 bool RTLIL::Module::has_memories() const
941 return !memories
.empty();
944 bool RTLIL::Module::has_processes() const
946 return !processes
.empty();
949 bool RTLIL::Module::has_memories_warn() const
951 if (!memories
.empty())
952 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
953 return !memories
.empty();
956 bool RTLIL::Module::has_processes_warn() const
958 if (!processes
.empty())
959 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
960 return !processes
.empty();
963 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
965 std::vector
<RTLIL::Wire
*> result
;
966 result
.reserve(wires_
.size());
967 for (auto &it
: wires_
)
968 if (design
->selected(this, it
.second
))
969 result
.push_back(it
.second
);
973 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
975 std::vector
<RTLIL::Cell
*> result
;
976 result
.reserve(wires_
.size());
977 for (auto &it
: cells_
)
978 if (design
->selected(this, it
.second
))
979 result
.push_back(it
.second
);
983 void RTLIL::Module::add(RTLIL::Wire
*wire
)
985 log_assert(!wire
->name
.empty());
986 log_assert(count_id(wire
->name
) == 0);
987 log_assert(refcount_wires_
== 0);
988 wires_
[wire
->name
] = wire
;
992 void RTLIL::Module::add(RTLIL::Cell
*cell
)
994 log_assert(!cell
->name
.empty());
995 log_assert(count_id(cell
->name
) == 0);
996 log_assert(refcount_cells_
== 0);
997 cells_
[cell
->name
] = cell
;
1002 struct DeleteWireWorker
1004 RTLIL::Module
*module
;
1005 const std::set
<RTLIL::Wire
*> *wires_p
;
1007 void operator()(RTLIL::SigSpec
&sig
) {
1008 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1009 for (auto &c
: chunks
)
1010 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1011 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1020 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1022 std::setPort
<RTLIL::Wire
*> wires_
;
1023 wires_
.insert(wire
);
1028 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1030 log_assert(refcount_wires_
== 0);
1032 DeleteWireWorker delete_wire_worker
;
1033 delete_wire_worker
.module
= this;
1034 delete_wire_worker
.wires_p
= &wires
;
1035 rewrite_sigspecs(delete_wire_worker
);
1037 for (auto &it
: wires
) {
1038 log_assert(wires_
.count(it
->name
) != 0);
1039 wires_
.erase(it
->name
);
1044 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1046 log_assert(cells_
.count(cell
->name
) != 0);
1047 log_assert(refcount_cells_
== 0);
1048 cells_
.erase(cell
->name
);
1052 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1054 log_assert(wires_
[wire
->name
] == wire
);
1055 log_assert(refcount_wires_
== 0);
1056 wires_
.erase(wire
->name
);
1057 wire
->name
= new_name
;
1061 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1063 log_assert(cells_
[cell
->name
] == cell
);
1064 log_assert(refcount_wires_
== 0);
1065 cells_
.erase(cell
->name
);
1066 cell
->name
= new_name
;
1070 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1072 log_assert(count_id(old_name
) != 0);
1073 if (wires_
.count(old_name
))
1074 rename(wires_
.at(old_name
), new_name
);
1075 else if (cells_
.count(old_name
))
1076 rename(cells_
.at(old_name
), new_name
);
1081 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1083 log_assert(wires_
[w1
->name
] == w1
);
1084 log_assert(wires_
[w2
->name
] == w2
);
1085 log_assert(refcount_wires_
== 0);
1087 wires_
.erase(w1
->name
);
1088 wires_
.erase(w2
->name
);
1090 std::swap(w1
->name
, w2
->name
);
1092 wires_
[w1
->name
] = w1
;
1093 wires_
[w2
->name
] = w2
;
1096 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1098 log_assert(cells_
[c1
->name
] == c1
);
1099 log_assert(cells_
[c2
->name
] == c2
);
1100 log_assert(refcount_cells_
== 0);
1102 cells_
.erase(c1
->name
);
1103 cells_
.erase(c2
->name
);
1105 std::swap(c1
->name
, c2
->name
);
1107 cells_
[c1
->name
] = c1
;
1108 cells_
[c2
->name
] = c2
;
1111 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1114 return uniquify(name
, index
);
1117 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1120 if (count_id(name
) == 0)
1126 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1127 if (count_id(new_name
) == 0)
1133 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1135 if (a
->port_id
&& !b
->port_id
)
1137 if (!a
->port_id
&& b
->port_id
)
1140 if (a
->port_id
== b
->port_id
)
1141 return a
->name
< b
->name
;
1142 return a
->port_id
< b
->port_id
;
1145 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1147 for (auto mon
: monitors
)
1148 mon
->notify_connect(this, conn
);
1151 for (auto mon
: design
->monitors
)
1152 mon
->notify_connect(this, conn
);
1154 connections_
.push_back(conn
);
1157 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1159 connect(RTLIL::SigSig(lhs
, rhs
));
1162 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1164 for (auto mon
: monitors
)
1165 mon
->notify_connect(this, new_conn
);
1168 for (auto mon
: design
->monitors
)
1169 mon
->notify_connect(this, new_conn
);
1171 connections_
= new_conn
;
1174 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1176 return connections_
;
1179 void RTLIL::Module::fixup_ports()
1181 std::vector
<RTLIL::Wire
*> all_ports
;
1183 for (auto &w
: wires_
)
1184 if (w
.second
->port_input
|| w
.second
->port_output
)
1185 all_ports
.push_back(w
.second
);
1187 w
.second
->port_id
= 0;
1189 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1192 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1193 ports
.push_back(all_ports
[i
]->name
);
1194 all_ports
[i
]->port_id
= i
+1;
1198 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1200 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1202 wire
->width
= width
;
1207 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1209 RTLIL::Wire
*wire
= addWire(name
);
1210 wire
->width
= other
->width
;
1211 wire
->start_offset
= other
->start_offset
;
1212 wire
->port_id
= other
->port_id
;
1213 wire
->port_input
= other
->port_input
;
1214 wire
->port_output
= other
->port_output
;
1215 wire
->upto
= other
->upto
;
1216 wire
->attributes
= other
->attributes
;
1220 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1222 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1229 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1231 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1232 cell
->connections_
= other
->connections_
;
1233 cell
->parameters
= other
->parameters
;
1234 cell
->attributes
= other
->attributes
;
1238 #define DEF_METHOD(_func, _y_size, _type) \
1239 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1240 RTLIL::Cell *cell = addCell(name, _type); \
1241 cell->parameters["\\A_SIGNED"] = is_signed; \
1242 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1243 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1244 cell->setPort("\\A", sig_a); \
1245 cell->setPort("\\Y", sig_y); \
1248 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1249 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1250 add ## _func(name, sig_a, sig_y, is_signed); \
1253 DEF_METHOD(Not
, sig_a
.size(), "$not")
1254 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1255 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1256 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1257 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1258 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1259 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1260 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1261 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1262 DEF_METHOD(LogicNot
, 1, "$logic_not")
1265 #define DEF_METHOD(_func, _y_size, _type) \
1266 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1267 RTLIL::Cell *cell = addCell(name, _type); \
1268 cell->parameters["\\A_SIGNED"] = is_signed; \
1269 cell->parameters["\\B_SIGNED"] = is_signed; \
1270 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1271 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1272 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1273 cell->setPort("\\A", sig_a); \
1274 cell->setPort("\\B", sig_b); \
1275 cell->setPort("\\Y", sig_y); \
1278 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1279 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1280 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1283 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1284 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1285 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1286 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1287 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1288 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1289 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1290 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1291 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1292 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1293 DEF_METHOD(Lt
, 1, "$lt")
1294 DEF_METHOD(Le
, 1, "$le")
1295 DEF_METHOD(Eq
, 1, "$eq")
1296 DEF_METHOD(Ne
, 1, "$ne")
1297 DEF_METHOD(Eqx
, 1, "$eqx")
1298 DEF_METHOD(Nex
, 1, "$nex")
1299 DEF_METHOD(Ge
, 1, "$ge")
1300 DEF_METHOD(Gt
, 1, "$gt")
1301 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1302 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1303 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1304 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1305 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1306 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1307 DEF_METHOD(LogicOr
, 1, "$logic_or")
1310 #define DEF_METHOD(_func, _type, _pmux) \
1311 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) { \
1312 RTLIL::Cell *cell = addCell(name, _type); \
1313 cell->parameters["\\WIDTH"] = sig_a.size(); \
1314 cell->parameters["\\WIDTH"] = sig_b.size(); \
1315 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1316 cell->setPort("\\A", sig_a); \
1317 cell->setPort("\\B", sig_b); \
1318 cell->setPort("\\S", sig_s); \
1319 cell->setPort("\\Y", sig_y); \
1322 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1323 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1324 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1327 DEF_METHOD(Mux
, "$mux", 0)
1328 DEF_METHOD(Pmux
, "$pmux", 1)
1331 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1332 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1333 RTLIL::Cell *cell = addCell(name, _type); \
1334 cell->setPort("\\" #_P1, sig1); \
1335 cell->setPort("\\" #_P2, sig2); \
1338 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1339 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1340 add ## _func(name, sig1, sig2); \
1343 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1344 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1345 RTLIL::Cell *cell = addCell(name, _type); \
1346 cell->setPort("\\" #_P1, sig1); \
1347 cell->setPort("\\" #_P2, sig2); \
1348 cell->setPort("\\" #_P3, sig3); \
1351 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1352 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1353 add ## _func(name, sig1, sig2, sig3); \
1356 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1357 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1358 RTLIL::Cell *cell = addCell(name, _type); \
1359 cell->setPort("\\" #_P1, sig1); \
1360 cell->setPort("\\" #_P2, sig2); \
1361 cell->setPort("\\" #_P3, sig3); \
1362 cell->setPort("\\" #_P4, sig4); \
1365 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1366 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1367 add ## _func(name, sig1, sig2, sig3, sig4); \
1370 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1371 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1372 RTLIL::Cell *cell = addCell(name, _type); \
1373 cell->setPort("\\" #_P1, sig1); \
1374 cell->setPort("\\" #_P2, sig2); \
1375 cell->setPort("\\" #_P3, sig3); \
1376 cell->setPort("\\" #_P4, sig4); \
1377 cell->setPort("\\" #_P5, sig5); \
1380 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1381 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1382 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1385 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1386 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1387 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1388 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1389 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1390 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1391 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1392 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1393 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1394 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1395 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1396 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1402 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
)
1404 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1405 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1406 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1407 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1408 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1409 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1410 cell
->setPort("\\A", sig_a
);
1411 cell
->setPort("\\B", sig_b
);
1412 cell
->setPort("\\Y", sig_y
);
1416 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1418 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1419 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1420 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1421 cell
->parameters
["\\OFFSET"] = offset
;
1422 cell
->setPort("\\A", sig_a
);
1423 cell
->setPort("\\Y", sig_y
);
1427 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1429 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1430 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1431 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1432 cell
->setPort("\\A", sig_a
);
1433 cell
->setPort("\\B", sig_b
);
1434 cell
->setPort("\\Y", sig_y
);
1438 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1440 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1441 cell
->parameters
["\\LUT"] = lut
;
1442 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1443 cell
->setPort("\\A", sig_i
);
1444 cell
->setPort("\\Y", sig_o
);
1448 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1450 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1451 cell
->setPort("\\A", sig_a
);
1452 cell
->setPort("\\EN", sig_en
);
1456 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
)
1458 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1459 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1460 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1461 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1462 cell
->setPort("\\SET", sig_set
);
1463 cell
->setPort("\\CLR", sig_clr
);
1464 cell
->setPort("\\Q", sig_q
);
1468 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1470 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1471 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1472 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1473 cell
->setPort("\\CLK", sig_clk
);
1474 cell
->setPort("\\D", sig_d
);
1475 cell
->setPort("\\Q", sig_q
);
1479 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1480 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1482 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1483 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1484 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1485 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1486 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1487 cell
->setPort("\\CLK", sig_clk
);
1488 cell
->setPort("\\SET", sig_set
);
1489 cell
->setPort("\\CLR", sig_clr
);
1490 cell
->setPort("\\D", sig_d
);
1491 cell
->setPort("\\Q", sig_q
);
1495 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1496 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1498 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1499 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1500 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1501 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1502 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1503 cell
->setPort("\\CLK", sig_clk
);
1504 cell
->setPort("\\ARST", sig_arst
);
1505 cell
->setPort("\\D", sig_d
);
1506 cell
->setPort("\\Q", sig_q
);
1510 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1512 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1513 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1514 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1515 cell
->setPort("\\EN", sig_en
);
1516 cell
->setPort("\\D", sig_d
);
1517 cell
->setPort("\\Q", sig_q
);
1521 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1522 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1524 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1525 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1526 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1527 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1528 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1529 cell
->setPort("\\EN", sig_en
);
1530 cell
->setPort("\\SET", sig_set
);
1531 cell
->setPort("\\CLR", sig_clr
);
1532 cell
->setPort("\\D", sig_d
);
1533 cell
->setPort("\\Q", sig_q
);
1537 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1539 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1540 cell
->setPort("\\C", sig_clk
);
1541 cell
->setPort("\\D", sig_d
);
1542 cell
->setPort("\\Q", sig_q
);
1546 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1547 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1549 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1550 cell
->setPort("\\C", sig_clk
);
1551 cell
->setPort("\\S", sig_set
);
1552 cell
->setPort("\\R", sig_clr
);
1553 cell
->setPort("\\D", sig_d
);
1554 cell
->setPort("\\Q", sig_q
);
1558 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1559 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1561 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1562 cell
->setPort("\\C", sig_clk
);
1563 cell
->setPort("\\R", sig_arst
);
1564 cell
->setPort("\\D", sig_d
);
1565 cell
->setPort("\\Q", sig_q
);
1569 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1571 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1572 cell
->setPort("\\E", sig_en
);
1573 cell
->setPort("\\D", sig_d
);
1574 cell
->setPort("\\Q", sig_q
);
1578 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1579 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1581 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1582 cell
->setPort("\\E", sig_en
);
1583 cell
->setPort("\\S", sig_set
);
1584 cell
->setPort("\\R", sig_clr
);
1585 cell
->setPort("\\D", sig_d
);
1586 cell
->setPort("\\Q", sig_q
);
1598 port_output
= false;
1602 RTLIL::Memory::Memory()
1608 RTLIL::Cell::Cell() : module(nullptr)
1612 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1614 return connections_
.count(portname
) != 0;
1617 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1619 RTLIL::SigSpec signal
;
1620 auto conn_it
= connections_
.find(portname
);
1622 if (conn_it
!= connections_
.end())
1624 for (auto mon
: module
->monitors
)
1625 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1628 for (auto mon
: module
->design
->monitors
)
1629 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1631 connections_
.erase(conn_it
);
1635 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1637 auto conn_it
= connections_
.find(portname
);
1639 if (conn_it
== connections_
.end()) {
1640 connections_
[portname
] = RTLIL::SigSpec();
1641 conn_it
= connections_
.find(portname
);
1642 log_assert(conn_it
!= connections_
.end());
1645 for (auto mon
: module
->monitors
)
1646 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1649 for (auto mon
: module
->design
->monitors
)
1650 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1652 conn_it
->second
= signal
;
1655 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1657 return connections_
.at(portname
);
1660 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1662 return connections_
;
1665 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1667 return parameters
.count(paramname
);
1670 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1672 parameters
.erase(paramname
);
1675 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1677 parameters
[paramname
] = value
;
1680 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1682 return parameters
.at(paramname
);
1685 void RTLIL::Cell::check()
1688 InternalCellChecker
checker(NULL
, this);
1693 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1695 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1696 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1699 if (type
== "$mux" || type
== "$pmux")
1701 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1702 if (type
== "$pmux")
1703 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1708 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1710 if (connections_
.count("\\A")) {
1711 if (signedness_ab
) {
1713 parameters
["\\A_SIGNED"] = true;
1714 else if (parameters
.count("\\A_SIGNED") == 0)
1715 parameters
["\\A_SIGNED"] = false;
1717 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1720 if (connections_
.count("\\B")) {
1721 if (signedness_ab
) {
1723 parameters
["\\B_SIGNED"] = true;
1724 else if (parameters
.count("\\B_SIGNED") == 0)
1725 parameters
["\\B_SIGNED"] = false;
1727 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1730 if (connections_
.count("\\Y"))
1731 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1736 RTLIL::SigChunk::SigChunk()
1743 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1747 width
= data
.bits
.size();
1751 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1753 log_assert(wire
!= nullptr);
1755 this->width
= wire
->width
;
1759 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1761 log_assert(wire
!= nullptr);
1763 this->width
= width
;
1764 this->offset
= offset
;
1767 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1770 data
= RTLIL::Const(str
);
1771 width
= data
.bits
.size();
1775 RTLIL::SigChunk::SigChunk(int val
, int width
)
1778 data
= RTLIL::Const(val
, width
);
1779 this->width
= data
.bits
.size();
1783 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1786 data
= RTLIL::Const(bit
, width
);
1787 this->width
= data
.bits
.size();
1791 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1796 data
= RTLIL::Const(bit
.data
);
1798 offset
= bit
.offset
;
1802 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1804 RTLIL::SigChunk ret
;
1807 ret
.offset
= this->offset
+ offset
;
1810 for (int i
= 0; i
< length
; i
++)
1811 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1817 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1819 if (wire
&& other
.wire
)
1820 if (wire
->name
!= other
.wire
->name
)
1821 return wire
->name
< other
.wire
->name
;
1823 if (wire
!= other
.wire
)
1824 return wire
< other
.wire
;
1826 if (offset
!= other
.offset
)
1827 return offset
< other
.offset
;
1829 if (width
!= other
.width
)
1830 return width
< other
.width
;
1832 return data
.bits
< other
.data
.bits
;
1835 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1837 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1839 if (data
.bits
!= other
.data
.bits
)
1844 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1851 RTLIL::SigSpec::SigSpec()
1857 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1862 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1864 cover("kernel.rtlil.sigspec.init.list");
1869 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1870 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1874 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1876 cover("kernel.rtlil.sigspec.assign");
1878 width_
= other
.width_
;
1879 hash_
= other
.hash_
;
1880 chunks_
= other
.chunks_
;
1883 if (!other
.bits_
.empty())
1885 RTLIL::SigChunk
*last
= NULL
;
1886 int last_end_offset
= 0;
1888 for (auto &bit
: other
.bits_
) {
1889 if (last
&& bit
.wire
== last
->wire
) {
1890 if (bit
.wire
== NULL
) {
1891 last
->data
.bits
.push_back(bit
.data
);
1894 } else if (last_end_offset
== bit
.offset
) {
1900 chunks_
.push_back(bit
);
1901 last
= &chunks_
.back();
1902 last_end_offset
= bit
.offset
+ 1;
1911 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1913 cover("kernel.rtlil.sigspec.init.const");
1915 chunks_
.push_back(RTLIL::SigChunk(value
));
1916 width_
= chunks_
.back().width
;
1921 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1923 cover("kernel.rtlil.sigspec.init.chunk");
1925 chunks_
.push_back(chunk
);
1926 width_
= chunks_
.back().width
;
1931 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1933 cover("kernel.rtlil.sigspec.init.wire");
1935 chunks_
.push_back(RTLIL::SigChunk(wire
));
1936 width_
= chunks_
.back().width
;
1941 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1943 cover("kernel.rtlil.sigspec.init.wire_part");
1945 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1946 width_
= chunks_
.back().width
;
1951 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1953 cover("kernel.rtlil.sigspec.init.str");
1955 chunks_
.push_back(RTLIL::SigChunk(str
));
1956 width_
= chunks_
.back().width
;
1961 RTLIL::SigSpec::SigSpec(int val
, int width
)
1963 cover("kernel.rtlil.sigspec.init.int");
1965 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1971 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1973 cover("kernel.rtlil.sigspec.init.state");
1975 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1981 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1983 cover("kernel.rtlil.sigspec.init.bit");
1985 if (bit
.wire
== NULL
)
1986 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1988 for (int i
= 0; i
< width
; i
++)
1989 chunks_
.push_back(bit
);
1995 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1997 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2001 for (auto &c
: chunks
)
2006 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2008 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2012 for (auto &bit
: bits
)
2017 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2019 cover("kernel.rtlil.sigspec.init.stdset_bits");
2023 for (auto &bit
: bits
)
2028 void RTLIL::SigSpec::pack() const
2030 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2032 if (that
->bits_
.empty())
2035 cover("kernel.rtlil.sigspec.convert.pack");
2036 log_assert(that
->chunks_
.empty());
2038 std::vector
<RTLIL::SigBit
> old_bits
;
2039 old_bits
.swap(that
->bits_
);
2041 RTLIL::SigChunk
*last
= NULL
;
2042 int last_end_offset
= 0;
2044 for (auto &bit
: old_bits
) {
2045 if (last
&& bit
.wire
== last
->wire
) {
2046 if (bit
.wire
== NULL
) {
2047 last
->data
.bits
.push_back(bit
.data
);
2050 } else if (last_end_offset
== bit
.offset
) {
2056 that
->chunks_
.push_back(bit
);
2057 last
= &that
->chunks_
.back();
2058 last_end_offset
= bit
.offset
+ 1;
2064 void RTLIL::SigSpec::unpack() const
2066 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2068 if (that
->chunks_
.empty())
2071 cover("kernel.rtlil.sigspec.convert.unpack");
2072 log_assert(that
->bits_
.empty());
2074 that
->bits_
.reserve(that
->width_
);
2075 for (auto &c
: that
->chunks_
)
2076 for (int i
= 0; i
< c
.width
; i
++)
2077 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2079 that
->chunks_
.clear();
2083 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2085 void RTLIL::SigSpec::hash() const
2087 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2089 if (that
->hash_
!= 0)
2092 cover("kernel.rtlil.sigspec.hash");
2096 for (auto &c
: that
->chunks_
)
2097 if (c
.wire
== NULL
) {
2098 for (auto &v
: c
.data
.bits
)
2099 DJB2(that
->hash_
, v
);
2101 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2102 DJB2(that
->hash_
, c
.offset
);
2103 DJB2(that
->hash_
, c
.width
);
2106 if (that
->hash_
== 0)
2110 void RTLIL::SigSpec::sort()
2113 cover("kernel.rtlil.sigspec.sort");
2114 std::sort(bits_
.begin(), bits_
.end());
2117 void RTLIL::SigSpec::sort_and_unify()
2119 cover("kernel.rtlil.sigspec.sort_and_unify");
2120 *this = this->to_sigbit_set();
2123 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2125 replace(pattern
, with
, this);
2128 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2130 log_assert(pattern
.width_
== with
.width_
);
2135 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2137 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2138 if (pattern
.bits_
[i
].wire
!= NULL
)
2139 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2141 replace(rules
, other
);
2144 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2146 replace(rules
, this);
2149 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2151 cover("kernel.rtlil.sigspec.replace");
2153 log_assert(other
!= NULL
);
2154 log_assert(width_
== other
->width_
);
2159 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2160 auto it
= rules
.find(bits_
[i
]);
2161 if (it
!= rules
.end())
2162 other
->bits_
[i
] = it
->second
;
2168 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2170 remove2(pattern
, NULL
);
2173 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2175 RTLIL::SigSpec tmp
= *this;
2176 tmp
.remove2(pattern
, other
);
2179 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2181 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2182 remove2(pattern_bits
, other
);
2185 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2187 remove2(pattern
, NULL
);
2190 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2192 RTLIL::SigSpec tmp
= *this;
2193 tmp
.remove2(pattern
, other
);
2196 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2199 cover("kernel.rtlil.sigspec.remove_other");
2201 cover("kernel.rtlil.sigspec.remove");
2205 if (other
!= NULL
) {
2206 log_assert(width_
== other
->width_
);
2210 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2212 new_bits
.resize(SIZE(bits_
));
2214 new_other_bits
.resize(SIZE(bits_
));
2217 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2218 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2221 new_other_bits
[k
] = other
->bits_
[i
];
2222 new_bits
[k
++] = bits_
[i
];
2227 new_other_bits
.resize(k
);
2229 bits_
.swap(new_bits
);
2230 width_
= SIZE(bits_
);
2232 if (other
!= NULL
) {
2233 other
->bits_
.swap(new_other_bits
);
2234 other
->width_
= SIZE(other
->bits_
);
2240 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2242 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2243 return extract(pattern_bits
, other
);
2246 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2249 cover("kernel.rtlil.sigspec.extract_other");
2251 cover("kernel.rtlil.sigspec.extract");
2253 log_assert(other
== NULL
|| width_
== other
->width_
);
2255 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2259 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2260 for (int i
= 0; i
< width_
; i
++)
2261 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2262 ret
.append_bit(bits_other
[i
]);
2264 for (int i
= 0; i
< width_
; i
++)
2265 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2266 ret
.append_bit(bits_match
[i
]);
2273 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2275 cover("kernel.rtlil.sigspec.replace_pos");
2280 log_assert(offset
>= 0);
2281 log_assert(with
.width_
>= 0);
2282 log_assert(offset
+with
.width_
<= width_
);
2284 for (int i
= 0; i
< with
.width_
; i
++)
2285 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2290 void RTLIL::SigSpec::remove_const()
2294 cover("kernel.rtlil.sigspec.remove_const.packed");
2296 std::vector
<RTLIL::SigChunk
> new_chunks
;
2297 new_chunks
.reserve(SIZE(chunks_
));
2300 for (auto &chunk
: chunks_
)
2301 if (chunk
.wire
!= NULL
) {
2302 new_chunks
.push_back(chunk
);
2303 width_
+= chunk
.width
;
2306 chunks_
.swap(new_chunks
);
2310 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2312 std::vector
<RTLIL::SigBit
> new_bits
;
2313 new_bits
.reserve(width_
);
2315 for (auto &bit
: bits_
)
2316 if (bit
.wire
!= NULL
)
2317 new_bits
.push_back(bit
);
2319 bits_
.swap(new_bits
);
2320 width_
= bits_
.size();
2326 void RTLIL::SigSpec::remove(int offset
, int length
)
2328 cover("kernel.rtlil.sigspec.remove_pos");
2332 log_assert(offset
>= 0);
2333 log_assert(length
>= 0);
2334 log_assert(offset
+ length
<= width_
);
2336 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2337 width_
= bits_
.size();
2342 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2345 cover("kernel.rtlil.sigspec.extract_pos");
2346 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2349 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2351 if (signal
.width_
== 0)
2359 cover("kernel.rtlil.sigspec.append");
2361 if (packed() != signal
.packed()) {
2367 for (auto &other_c
: signal
.chunks_
)
2369 auto &my_last_c
= chunks_
.back();
2370 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2371 auto &this_data
= my_last_c
.data
.bits
;
2372 auto &other_data
= other_c
.data
.bits
;
2373 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2374 my_last_c
.width
+= other_c
.width
;
2376 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2377 my_last_c
.width
+= other_c
.width
;
2379 chunks_
.push_back(other_c
);
2382 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2384 width_
+= signal
.width_
;
2388 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2392 cover("kernel.rtlil.sigspec.append_bit.packed");
2394 if (chunks_
.size() == 0)
2395 chunks_
.push_back(bit
);
2397 if (bit
.wire
== NULL
)
2398 if (chunks_
.back().wire
== NULL
) {
2399 chunks_
.back().data
.bits
.push_back(bit
.data
);
2400 chunks_
.back().width
++;
2402 chunks_
.push_back(bit
);
2404 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2405 chunks_
.back().width
++;
2407 chunks_
.push_back(bit
);
2411 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2412 bits_
.push_back(bit
);
2419 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2421 cover("kernel.rtlil.sigspec.extend");
2426 remove(width
, width_
- width
);
2428 if (width_
< width
) {
2429 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2430 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2431 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2432 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2433 while (width_
< width
)
2438 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2440 cover("kernel.rtlil.sigspec.extend_u0");
2445 remove(width
, width_
- width
);
2447 if (width_
< width
) {
2448 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2450 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2451 while (width_
< width
)
2457 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2459 cover("kernel.rtlil.sigspec.repeat");
2462 for (int i
= 0; i
< num
; i
++)
2468 void RTLIL::SigSpec::check() const
2472 cover("kernel.rtlil.sigspec.check.skip");
2476 cover("kernel.rtlil.sigspec.check.packed");
2479 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2480 const RTLIL::SigChunk chunk
= chunks_
[i
];
2481 if (chunk
.wire
== NULL
) {
2483 log_assert(chunks_
[i
-1].wire
!= NULL
);
2484 log_assert(chunk
.offset
== 0);
2485 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2487 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2488 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2489 log_assert(chunk
.offset
>= 0);
2490 log_assert(chunk
.width
>= 0);
2491 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2492 log_assert(chunk
.data
.bits
.size() == 0);
2496 log_assert(w
== width_
);
2497 log_assert(bits_
.empty());
2501 cover("kernel.rtlil.sigspec.check.unpacked");
2503 log_assert(width_
== SIZE(bits_
));
2504 log_assert(chunks_
.empty());
2509 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2511 cover("kernel.rtlil.sigspec.comp_lt");
2516 if (width_
!= other
.width_
)
2517 return width_
< other
.width_
;
2522 if (chunks_
.size() != other
.chunks_
.size())
2523 return chunks_
.size() < other
.chunks_
.size();
2528 if (hash_
!= other
.hash_
)
2529 return hash_
< other
.hash_
;
2531 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2532 if (chunks_
[i
] != other
.chunks_
[i
]) {
2533 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2534 return chunks_
[i
] < other
.chunks_
[i
];
2537 cover("kernel.rtlil.sigspec.comp_lt.equal");
2541 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2543 cover("kernel.rtlil.sigspec.comp_eq");
2548 if (width_
!= other
.width_
)
2554 if (chunks_
.size() != chunks_
.size())
2560 if (hash_
!= other
.hash_
)
2563 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2564 if (chunks_
[i
] != other
.chunks_
[i
]) {
2565 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2569 cover("kernel.rtlil.sigspec.comp_eq.equal");
2573 bool RTLIL::SigSpec::is_wire() const
2575 cover("kernel.rtlil.sigspec.is_wire");
2578 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2581 bool RTLIL::SigSpec::is_chunk() const
2583 cover("kernel.rtlil.sigspec.is_chunk");
2586 return SIZE(chunks_
) == 1;
2589 bool RTLIL::SigSpec::is_fully_const() const
2591 cover("kernel.rtlil.sigspec.is_fully_const");
2594 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2595 if (it
->width
> 0 && it
->wire
!= NULL
)
2600 bool RTLIL::SigSpec::is_fully_def() const
2602 cover("kernel.rtlil.sigspec.is_fully_def");
2605 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2606 if (it
->width
> 0 && it
->wire
!= NULL
)
2608 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2609 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2615 bool RTLIL::SigSpec::is_fully_undef() const
2617 cover("kernel.rtlil.sigspec.is_fully_undef");
2620 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2621 if (it
->width
> 0 && it
->wire
!= NULL
)
2623 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2624 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2630 bool RTLIL::SigSpec::has_marked_bits() const
2632 cover("kernel.rtlil.sigspec.has_marked_bits");
2635 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2636 if (it
->width
> 0 && it
->wire
== NULL
) {
2637 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2638 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2644 bool RTLIL::SigSpec::as_bool() const
2646 cover("kernel.rtlil.sigspec.as_bool");
2649 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2651 return chunks_
[0].data
.as_bool();
2655 int RTLIL::SigSpec::as_int() const
2657 cover("kernel.rtlil.sigspec.as_int");
2660 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2662 return chunks_
[0].data
.as_int();
2666 std::string
RTLIL::SigSpec::as_string() const
2668 cover("kernel.rtlil.sigspec.as_string");
2672 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2673 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2674 if (chunk
.wire
!= NULL
)
2675 for (int j
= 0; j
< chunk
.width
; j
++)
2678 str
+= chunk
.data
.as_string();
2683 RTLIL::Const
RTLIL::SigSpec::as_const() const
2685 cover("kernel.rtlil.sigspec.as_const");
2688 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2690 return chunks_
[0].data
;
2691 return RTLIL::Const();
2694 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2696 cover("kernel.rtlil.sigspec.as_wire");
2699 log_assert(is_wire());
2700 return chunks_
[0].wire
;
2703 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2705 cover("kernel.rtlil.sigspec.as_chunk");
2708 log_assert(is_chunk());
2712 bool RTLIL::SigSpec::match(std::string pattern
) const
2714 cover("kernel.rtlil.sigspec.match");
2717 std::string str
= as_string();
2718 log_assert(pattern
.size() == str
.size());
2720 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2721 if (pattern
[i
] == ' ')
2723 if (pattern
[i
] == '*') {
2724 if (str
[i
] != 'z' && str
[i
] != 'x')
2728 if (pattern
[i
] != str
[i
])
2735 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2737 cover("kernel.rtlil.sigspec.to_sigbit_set");
2740 std::set
<RTLIL::SigBit
> sigbits
;
2741 for (auto &c
: chunks_
)
2742 for (int i
= 0; i
< c
.width
; i
++)
2743 sigbits
.insert(RTLIL::SigBit(c
, i
));
2747 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2749 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2755 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2757 cover("kernel.rtlil.sigspec.to_sigbit_map");
2762 log_assert(width_
== other
.width_
);
2764 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2765 for (int i
= 0; i
< width_
; i
++)
2766 new_map
[bits_
[i
]] = other
.bits_
[i
];
2771 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2773 cover("kernel.rtlil.sigspec.to_single_sigbit");
2776 log_assert(width_
== 1);
2777 for (auto &c
: chunks_
)
2779 return RTLIL::SigBit(c
);
2783 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2785 size_t start
= 0, end
= 0;
2786 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2787 tokens
.push_back(text
.substr(start
, end
- start
));
2790 tokens
.push_back(text
.substr(start
));
2793 static int sigspec_parse_get_dummy_line_num()
2798 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2800 cover("kernel.rtlil.sigspec.parse");
2802 std::vector
<std::string
> tokens
;
2803 sigspec_parse_split(tokens
, str
, ',');
2805 sig
= RTLIL::SigSpec();
2806 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2808 std::string netname
= tokens
[tokidx
];
2809 std::string indices
;
2811 if (netname
.size() == 0)
2814 if ('0' <= netname
[0] && netname
[0] <= '9') {
2815 cover("kernel.rtlil.sigspec.parse.const");
2816 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2817 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2820 sig
.append(RTLIL::Const(ast
->bits
));
2828 cover("kernel.rtlil.sigspec.parse.net");
2830 if (netname
[0] != '$' && netname
[0] != '\\')
2831 netname
= "\\" + netname
;
2833 if (module
->wires_
.count(netname
) == 0) {
2834 size_t indices_pos
= netname
.size()-1;
2835 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2838 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2839 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2841 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2843 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2844 indices
= netname
.substr(indices_pos
);
2845 netname
= netname
.substr(0, indices_pos
);
2850 if (module
->wires_
.count(netname
) == 0)
2853 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2854 if (!indices
.empty()) {
2855 std::vector
<std::string
> index_tokens
;
2856 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2857 if (index_tokens
.size() == 1) {
2858 cover("kernel.rtlil.sigspec.parse.bit_sel");
2859 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2861 cover("kernel.rtlil.sigspec.parse.part_sel");
2862 int a
= atoi(index_tokens
.at(0).c_str());
2863 int b
= atoi(index_tokens
.at(1).c_str());
2868 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2877 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2879 if (str
.empty() || str
[0] != '@')
2880 return parse(sig
, module
, str
);
2882 cover("kernel.rtlil.sigspec.parse.sel");
2884 str
= RTLIL::escape_id(str
.substr(1));
2885 if (design
->selection_vars
.count(str
) == 0)
2888 sig
= RTLIL::SigSpec();
2889 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2890 for (auto &it
: module
->wires_
)
2891 if (sel
.selected_member(module
->name
, it
.first
))
2892 sig
.append(it
.second
);
2897 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2900 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2901 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2906 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2907 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2911 if (lhs
.chunks_
.size() == 1) {
2912 char *p
= (char*)str
.c_str(), *endptr
;
2913 long long int val
= strtoll(p
, &endptr
, 10);
2914 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2915 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2916 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2921 return parse(sig
, module
, str
);
2924 RTLIL::CaseRule::~CaseRule()
2926 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2930 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2932 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2933 new_caserule
->compare
= compare
;
2934 new_caserule
->actions
= actions
;
2935 for (auto &it
: switches
)
2936 new_caserule
->switches
.push_back(it
->clone());
2937 return new_caserule
;
2940 RTLIL::SwitchRule::~SwitchRule()
2942 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2946 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2948 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2949 new_switchrule
->signal
= signal
;
2950 new_switchrule
->attributes
= attributes
;
2951 for (auto &it
: cases
)
2952 new_switchrule
->cases
.push_back(it
->clone());
2953 return new_switchrule
;
2957 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2959 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2960 new_syncrule
->type
= type
;
2961 new_syncrule
->signal
= signal
;
2962 new_syncrule
->actions
= actions
;
2963 return new_syncrule
;
2966 RTLIL::Process::~Process()
2968 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2972 RTLIL::Process
*RTLIL::Process::clone() const
2974 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2976 new_proc
->name
= name
;
2977 new_proc
->attributes
= attributes
;
2979 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2980 new_proc
->root_case
= *rc_ptr
;
2981 rc_ptr
->switches
.clear();
2984 for (auto &it
: syncs
)
2985 new_proc
->syncs
.push_back(it
->clone());