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;
231 selection_stack
.push_back(RTLIL::Selection());
234 RTLIL::Design::~Design()
236 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
240 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
242 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
245 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
247 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
250 void RTLIL::Design::add(RTLIL::Module
*module
)
252 log_assert(modules_
.count(module
->name
) == 0);
253 log_assert(refcount_modules_
== 0);
254 modules_
[module
->name
] = module
;
255 module
->design
= this;
257 for (auto mon
: monitors
)
258 mon
->notify_module_add(module
);
261 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
263 log_assert(modules_
.count(name
) == 0);
264 log_assert(refcount_modules_
== 0);
266 RTLIL::Module
*module
= new RTLIL::Module
;
267 modules_
[name
] = module
;
268 module
->design
= this;
271 for (auto mon
: monitors
)
272 mon
->notify_module_add(module
);
277 void RTLIL::Design::scratchpad_unset(std::string varname
)
279 scratchpad
.erase(varname
);
282 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
284 scratchpad
[varname
] = stringf("%d", value
);
287 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
289 scratchpad
[varname
] = value
? "true" : "false";
292 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
294 scratchpad
[varname
] = value
;
297 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
299 if (scratchpad
.count(varname
) == 0)
300 return default_value
;
302 std::string str
= scratchpad
.at(varname
);
304 if (str
== "0" || str
== "false")
307 if (str
== "1" || str
== "true")
310 char *endptr
= nullptr;
311 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
312 return *endptr
? default_value
: parsed_value
;
315 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
317 if (scratchpad
.count(varname
) == 0)
318 return default_value
;
320 std::string str
= scratchpad
.at(varname
);
322 if (str
== "0" || str
== "false")
325 if (str
== "1" || str
== "true")
328 return default_value
;
331 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
333 if (scratchpad
.count(varname
) == 0)
334 return default_value
;
335 return scratchpad
.at(varname
);
338 void RTLIL::Design::remove(RTLIL::Module
*module
)
340 for (auto mon
: monitors
)
341 mon
->notify_module_del(module
);
343 log_assert(modules_
.at(module
->name
) == module
);
344 modules_
.erase(module
->name
);
348 void RTLIL::Design::check()
351 for (auto &it
: modules_
) {
352 log_assert(this == it
.second
->design
);
353 log_assert(it
.first
== it
.second
->name
);
354 log_assert(!it
.first
.empty());
360 void RTLIL::Design::optimize()
362 for (auto &it
: modules_
)
363 it
.second
->optimize();
364 for (auto &it
: selection_stack
)
366 for (auto &it
: selection_vars
)
367 it
.second
.optimize(this);
370 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
372 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
374 if (selection_stack
.size() == 0)
376 return selection_stack
.back().selected_module(mod_name
);
379 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
381 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
383 if (selection_stack
.size() == 0)
385 return selection_stack
.back().selected_whole_module(mod_name
);
388 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
390 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
392 if (selection_stack
.size() == 0)
394 return selection_stack
.back().selected_member(mod_name
, memb_name
);
397 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
399 return selected_module(mod
->name
);
402 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
404 return selected_whole_module(mod
->name
);
407 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
409 std::vector
<RTLIL::Module
*> result
;
410 result
.reserve(modules_
.size());
411 for (auto &it
: modules_
)
412 if (selected_module(it
.first
))
413 result
.push_back(it
.second
);
417 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
419 std::vector
<RTLIL::Module
*> result
;
420 result
.reserve(modules_
.size());
421 for (auto &it
: modules_
)
422 if (selected_whole_module(it
.first
))
423 result
.push_back(it
.second
);
427 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
429 std::vector
<RTLIL::Module
*> result
;
430 result
.reserve(modules_
.size());
431 for (auto &it
: modules_
)
432 if (selected_whole_module(it
.first
))
433 result
.push_back(it
.second
);
434 else if (selected_module(it
.first
))
435 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
439 RTLIL::Module::Module()
446 RTLIL::Module::~Module()
448 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
450 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
452 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
454 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
458 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
460 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
463 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
465 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
470 struct InternalCellChecker
472 RTLIL::Module
*module
;
474 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
476 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
478 void error(int linenr
)
480 std::stringstream buf
;
481 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
483 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
484 module
? module
->name
.c_str() : "", module
? "." : "",
485 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
488 int param(const char *name
)
490 if (cell
->parameters
.count(name
) == 0)
492 expected_params
.insert(name
);
493 return cell
->parameters
.at(name
).as_int();
496 int param_bool(const char *name
)
499 if (cell
->parameters
.at(name
).bits
.size() > 32)
501 if (v
!= 0 && v
!= 1)
506 void param_bits(const char *name
, int width
)
509 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
513 void port(const char *name
, int width
)
515 if (!cell
->hasPort(name
))
517 if (cell
->getPort(name
).size() != width
)
519 expected_ports
.insert(name
);
522 void check_expected(bool check_matched_sign
= true)
524 for (auto ¶
: cell
->parameters
)
525 if (expected_params
.count(para
.first
) == 0)
527 for (auto &conn
: cell
->connections())
528 if (expected_ports
.count(conn
.first
) == 0)
531 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
532 bool a_is_signed
= param("\\A_SIGNED") != 0;
533 bool b_is_signed
= param("\\B_SIGNED") != 0;
534 if (a_is_signed
!= b_is_signed
)
539 void check_gate(const char *ports
)
541 if (cell
->parameters
.size() != 0)
544 for (const char *p
= ports
; *p
; p
++) {
545 char portname
[3] = { '\\', *p
, 0 };
546 if (!cell
->hasPort(portname
))
548 if (cell
->getPort(portname
).size() != 1)
552 for (auto &conn
: cell
->connections()) {
553 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
555 if (strchr(ports
, conn
.first
[1]) == NULL
)
562 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
563 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
566 if (cell
->type
.in("$not", "$pos", "$neg")) {
567 param_bool("\\A_SIGNED");
568 port("\\A", param("\\A_WIDTH"));
569 port("\\Y", param("\\Y_WIDTH"));
574 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
575 param_bool("\\A_SIGNED");
576 param_bool("\\B_SIGNED");
577 port("\\A", param("\\A_WIDTH"));
578 port("\\B", param("\\B_WIDTH"));
579 port("\\Y", param("\\Y_WIDTH"));
584 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
585 param_bool("\\A_SIGNED");
586 port("\\A", param("\\A_WIDTH"));
587 port("\\Y", param("\\Y_WIDTH"));
592 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
593 param_bool("\\A_SIGNED");
594 param_bool("\\B_SIGNED");
595 port("\\A", param("\\A_WIDTH"));
596 port("\\B", param("\\B_WIDTH"));
597 port("\\Y", param("\\Y_WIDTH"));
598 check_expected(false);
602 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
603 param_bool("\\A_SIGNED");
604 param_bool("\\B_SIGNED");
605 port("\\A", param("\\A_WIDTH"));
606 port("\\B", param("\\B_WIDTH"));
607 port("\\Y", param("\\Y_WIDTH"));
612 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
613 param_bool("\\A_SIGNED");
614 param_bool("\\B_SIGNED");
615 port("\\A", param("\\A_WIDTH"));
616 port("\\B", param("\\B_WIDTH"));
617 port("\\Y", param("\\Y_WIDTH"));
618 check_expected(cell
->type
!= "$pow");
622 if (cell
->type
== "$alu") {
623 param_bool("\\A_SIGNED");
624 param_bool("\\B_SIGNED");
625 port("\\A", param("\\A_WIDTH"));
626 port("\\B", param("\\B_WIDTH"));
629 port("\\X", param("\\Y_WIDTH"));
630 port("\\Y", param("\\Y_WIDTH"));
631 port("\\CO", param("\\Y_WIDTH"));
636 if (cell
->type
== "$logic_not") {
637 param_bool("\\A_SIGNED");
638 port("\\A", param("\\A_WIDTH"));
639 port("\\Y", param("\\Y_WIDTH"));
644 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
645 param_bool("\\A_SIGNED");
646 param_bool("\\B_SIGNED");
647 port("\\A", param("\\A_WIDTH"));
648 port("\\B", param("\\B_WIDTH"));
649 port("\\Y", param("\\Y_WIDTH"));
650 check_expected(false);
654 if (cell
->type
== "$slice") {
656 port("\\A", param("\\A_WIDTH"));
657 port("\\Y", param("\\Y_WIDTH"));
658 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
664 if (cell
->type
== "$concat") {
665 port("\\A", param("\\A_WIDTH"));
666 port("\\B", param("\\B_WIDTH"));
667 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
672 if (cell
->type
== "$mux") {
673 port("\\A", param("\\WIDTH"));
674 port("\\B", param("\\WIDTH"));
676 port("\\Y", param("\\WIDTH"));
681 if (cell
->type
== "$pmux") {
682 port("\\A", param("\\WIDTH"));
683 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
684 port("\\S", param("\\S_WIDTH"));
685 port("\\Y", param("\\WIDTH"));
690 if (cell
->type
== "$lut") {
692 port("\\A", param("\\WIDTH"));
698 if (cell
->type
== "$sr") {
699 param_bool("\\SET_POLARITY");
700 param_bool("\\CLR_POLARITY");
701 port("\\SET", param("\\WIDTH"));
702 port("\\CLR", param("\\WIDTH"));
703 port("\\Q", param("\\WIDTH"));
708 if (cell
->type
== "$dff") {
709 param_bool("\\CLK_POLARITY");
711 port("\\D", param("\\WIDTH"));
712 port("\\Q", param("\\WIDTH"));
717 if (cell
->type
== "$dffsr") {
718 param_bool("\\CLK_POLARITY");
719 param_bool("\\SET_POLARITY");
720 param_bool("\\CLR_POLARITY");
722 port("\\SET", param("\\WIDTH"));
723 port("\\CLR", param("\\WIDTH"));
724 port("\\D", param("\\WIDTH"));
725 port("\\Q", param("\\WIDTH"));
730 if (cell
->type
== "$adff") {
731 param_bool("\\CLK_POLARITY");
732 param_bool("\\ARST_POLARITY");
733 param_bits("\\ARST_VALUE", param("\\WIDTH"));
736 port("\\D", param("\\WIDTH"));
737 port("\\Q", param("\\WIDTH"));
742 if (cell
->type
== "$dlatch") {
743 param_bool("\\EN_POLARITY");
745 port("\\D", param("\\WIDTH"));
746 port("\\Q", param("\\WIDTH"));
751 if (cell
->type
== "$dlatchsr") {
752 param_bool("\\EN_POLARITY");
753 param_bool("\\SET_POLARITY");
754 param_bool("\\CLR_POLARITY");
756 port("\\SET", param("\\WIDTH"));
757 port("\\CLR", param("\\WIDTH"));
758 port("\\D", param("\\WIDTH"));
759 port("\\Q", param("\\WIDTH"));
764 if (cell
->type
== "$fsm") {
766 param_bool("\\CLK_POLARITY");
767 param_bool("\\ARST_POLARITY");
768 param("\\STATE_BITS");
769 param("\\STATE_NUM");
770 param("\\STATE_NUM_LOG2");
771 param("\\STATE_RST");
772 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
773 param("\\TRANS_NUM");
774 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
777 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
778 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
783 if (cell
->type
== "$memrd") {
785 param_bool("\\CLK_ENABLE");
786 param_bool("\\CLK_POLARITY");
787 param_bool("\\TRANSPARENT");
789 port("\\ADDR", param("\\ABITS"));
790 port("\\DATA", param("\\WIDTH"));
795 if (cell
->type
== "$memwr") {
797 param_bool("\\CLK_ENABLE");
798 param_bool("\\CLK_POLARITY");
801 port("\\EN", param("\\WIDTH"));
802 port("\\ADDR", param("\\ABITS"));
803 port("\\DATA", param("\\WIDTH"));
808 if (cell
->type
== "$mem") {
812 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
813 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
814 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
815 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
816 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
817 port("\\RD_CLK", param("\\RD_PORTS"));
818 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
819 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
820 port("\\WR_CLK", param("\\WR_PORTS"));
821 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
822 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
823 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
828 if (cell
->type
== "$assert") {
835 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
836 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
837 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
838 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
839 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
840 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
841 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
842 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
843 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
844 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
845 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
846 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
848 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
849 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
850 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
851 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
853 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
854 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
856 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
857 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
858 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
859 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
860 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
861 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
862 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
863 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
865 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
866 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
867 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
868 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
869 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
870 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
871 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
872 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
874 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
875 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
877 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
878 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
879 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
880 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
881 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
882 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
883 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
884 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
892 void RTLIL::Module::check()
895 std::vector
<bool> ports_declared
;
896 for (auto &it
: wires_
) {
897 log_assert(this == it
.second
->module
);
898 log_assert(it
.first
== it
.second
->name
);
899 log_assert(!it
.first
.empty());
900 log_assert(it
.second
->width
>= 0);
901 log_assert(it
.second
->port_id
>= 0);
902 for (auto &it2
: it
.second
->attributes
)
903 log_assert(!it2
.first
.empty());
904 if (it
.second
->port_id
) {
905 log_assert(SIZE(ports
) >= it
.second
->port_id
);
906 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
907 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
908 if (SIZE(ports_declared
) < it
.second
->port_id
)
909 ports_declared
.resize(it
.second
->port_id
);
910 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
911 ports_declared
[it
.second
->port_id
-1] = true;
913 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
915 for (auto port_declared
: ports_declared
)
916 log_assert(port_declared
== true);
917 log_assert(SIZE(ports
) == SIZE(ports_declared
));
919 for (auto &it
: memories
) {
920 log_assert(it
.first
== it
.second
->name
);
921 log_assert(!it
.first
.empty());
922 log_assert(it
.second
->width
>= 0);
923 log_assert(it
.second
->size
>= 0);
924 for (auto &it2
: it
.second
->attributes
)
925 log_assert(!it2
.first
.empty());
928 for (auto &it
: cells_
) {
929 log_assert(this == it
.second
->module
);
930 log_assert(it
.first
== it
.second
->name
);
931 log_assert(!it
.first
.empty());
932 log_assert(!it
.second
->type
.empty());
933 for (auto &it2
: it
.second
->connections()) {
934 log_assert(!it2
.first
.empty());
937 for (auto &it2
: it
.second
->attributes
)
938 log_assert(!it2
.first
.empty());
939 for (auto &it2
: it
.second
->parameters
)
940 log_assert(!it2
.first
.empty());
941 InternalCellChecker
checker(this, it
.second
);
945 for (auto &it
: processes
) {
946 log_assert(it
.first
== it
.second
->name
);
947 log_assert(!it
.first
.empty());
948 // FIXME: More checks here..
951 for (auto &it
: connections_
) {
952 log_assert(it
.first
.size() == it
.second
.size());
957 for (auto &it
: attributes
)
958 log_assert(!it
.first
.empty());
962 void RTLIL::Module::optimize()
966 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
968 log_assert(new_mod
->refcount_wires_
== 0);
969 log_assert(new_mod
->refcount_cells_
== 0);
971 new_mod
->connections_
= connections_
;
972 new_mod
->attributes
= attributes
;
974 for (auto &it
: wires_
)
975 new_mod
->addWire(it
.first
, it
.second
);
977 for (auto &it
: memories
)
978 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
980 for (auto &it
: cells_
)
981 new_mod
->addCell(it
.first
, it
.second
);
983 for (auto &it
: processes
)
984 new_mod
->processes
[it
.first
] = it
.second
->clone();
986 struct RewriteSigSpecWorker
989 void operator()(RTLIL::SigSpec
&sig
)
991 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
992 for (auto &c
: chunks
)
994 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
999 RewriteSigSpecWorker rewriteSigSpecWorker
;
1000 rewriteSigSpecWorker
.mod
= new_mod
;
1001 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1002 new_mod
->fixup_ports();
1005 RTLIL::Module
*RTLIL::Module::clone() const
1007 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1008 new_mod
->name
= name
;
1013 bool RTLIL::Module::has_memories() const
1015 return !memories
.empty();
1018 bool RTLIL::Module::has_processes() const
1020 return !processes
.empty();
1023 bool RTLIL::Module::has_memories_warn() const
1025 if (!memories
.empty())
1026 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1027 return !memories
.empty();
1030 bool RTLIL::Module::has_processes_warn() const
1032 if (!processes
.empty())
1033 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1034 return !processes
.empty();
1037 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1039 std::vector
<RTLIL::Wire
*> result
;
1040 result
.reserve(wires_
.size());
1041 for (auto &it
: wires_
)
1042 if (design
->selected(this, it
.second
))
1043 result
.push_back(it
.second
);
1047 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1049 std::vector
<RTLIL::Cell
*> result
;
1050 result
.reserve(wires_
.size());
1051 for (auto &it
: cells_
)
1052 if (design
->selected(this, it
.second
))
1053 result
.push_back(it
.second
);
1057 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1059 log_assert(!wire
->name
.empty());
1060 log_assert(count_id(wire
->name
) == 0);
1061 log_assert(refcount_wires_
== 0);
1062 wires_
[wire
->name
] = wire
;
1063 wire
->module
= this;
1066 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1068 log_assert(!cell
->name
.empty());
1069 log_assert(count_id(cell
->name
) == 0);
1070 log_assert(refcount_cells_
== 0);
1071 cells_
[cell
->name
] = cell
;
1072 cell
->module
= this;
1076 struct DeleteWireWorker
1078 RTLIL::Module
*module
;
1079 const std::set
<RTLIL::Wire
*> *wires_p
;
1081 void operator()(RTLIL::SigSpec
&sig
) {
1082 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1083 for (auto &c
: chunks
)
1084 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1085 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1094 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1096 std::setPort
<RTLIL::Wire
*> wires_
;
1097 wires_
.insert(wire
);
1102 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1104 log_assert(refcount_wires_
== 0);
1106 DeleteWireWorker delete_wire_worker
;
1107 delete_wire_worker
.module
= this;
1108 delete_wire_worker
.wires_p
= &wires
;
1109 rewrite_sigspecs(delete_wire_worker
);
1111 for (auto &it
: wires
) {
1112 log_assert(wires_
.count(it
->name
) != 0);
1113 wires_
.erase(it
->name
);
1118 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1120 log_assert(cells_
.count(cell
->name
) != 0);
1121 log_assert(refcount_cells_
== 0);
1122 cells_
.erase(cell
->name
);
1126 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1128 log_assert(wires_
[wire
->name
] == wire
);
1129 log_assert(refcount_wires_
== 0);
1130 wires_
.erase(wire
->name
);
1131 wire
->name
= new_name
;
1135 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1137 log_assert(cells_
[cell
->name
] == cell
);
1138 log_assert(refcount_wires_
== 0);
1139 cells_
.erase(cell
->name
);
1140 cell
->name
= new_name
;
1144 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1146 log_assert(count_id(old_name
) != 0);
1147 if (wires_
.count(old_name
))
1148 rename(wires_
.at(old_name
), new_name
);
1149 else if (cells_
.count(old_name
))
1150 rename(cells_
.at(old_name
), new_name
);
1155 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1157 log_assert(wires_
[w1
->name
] == w1
);
1158 log_assert(wires_
[w2
->name
] == w2
);
1159 log_assert(refcount_wires_
== 0);
1161 wires_
.erase(w1
->name
);
1162 wires_
.erase(w2
->name
);
1164 std::swap(w1
->name
, w2
->name
);
1166 wires_
[w1
->name
] = w1
;
1167 wires_
[w2
->name
] = w2
;
1170 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1172 log_assert(cells_
[c1
->name
] == c1
);
1173 log_assert(cells_
[c2
->name
] == c2
);
1174 log_assert(refcount_cells_
== 0);
1176 cells_
.erase(c1
->name
);
1177 cells_
.erase(c2
->name
);
1179 std::swap(c1
->name
, c2
->name
);
1181 cells_
[c1
->name
] = c1
;
1182 cells_
[c2
->name
] = c2
;
1185 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1188 return uniquify(name
, index
);
1191 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1194 if (count_id(name
) == 0)
1200 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1201 if (count_id(new_name
) == 0)
1207 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1209 if (a
->port_id
&& !b
->port_id
)
1211 if (!a
->port_id
&& b
->port_id
)
1214 if (a
->port_id
== b
->port_id
)
1215 return a
->name
< b
->name
;
1216 return a
->port_id
< b
->port_id
;
1219 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1221 for (auto mon
: monitors
)
1222 mon
->notify_connect(this, conn
);
1225 for (auto mon
: design
->monitors
)
1226 mon
->notify_connect(this, conn
);
1228 connections_
.push_back(conn
);
1231 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1233 connect(RTLIL::SigSig(lhs
, rhs
));
1236 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1238 for (auto mon
: monitors
)
1239 mon
->notify_connect(this, new_conn
);
1242 for (auto mon
: design
->monitors
)
1243 mon
->notify_connect(this, new_conn
);
1245 connections_
= new_conn
;
1248 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1250 return connections_
;
1253 void RTLIL::Module::fixup_ports()
1255 std::vector
<RTLIL::Wire
*> all_ports
;
1257 for (auto &w
: wires_
)
1258 if (w
.second
->port_input
|| w
.second
->port_output
)
1259 all_ports
.push_back(w
.second
);
1261 w
.second
->port_id
= 0;
1263 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1266 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1267 ports
.push_back(all_ports
[i
]->name
);
1268 all_ports
[i
]->port_id
= i
+1;
1272 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1274 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1276 wire
->width
= width
;
1281 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1283 RTLIL::Wire
*wire
= addWire(name
);
1284 wire
->width
= other
->width
;
1285 wire
->start_offset
= other
->start_offset
;
1286 wire
->port_id
= other
->port_id
;
1287 wire
->port_input
= other
->port_input
;
1288 wire
->port_output
= other
->port_output
;
1289 wire
->upto
= other
->upto
;
1290 wire
->attributes
= other
->attributes
;
1294 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1296 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1303 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1305 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1306 cell
->connections_
= other
->connections_
;
1307 cell
->parameters
= other
->parameters
;
1308 cell
->attributes
= other
->attributes
;
1312 #define DEF_METHOD(_func, _y_size, _type) \
1313 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1314 RTLIL::Cell *cell = addCell(name, _type); \
1315 cell->parameters["\\A_SIGNED"] = is_signed; \
1316 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1317 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1318 cell->setPort("\\A", sig_a); \
1319 cell->setPort("\\Y", sig_y); \
1322 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1323 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1324 add ## _func(name, sig_a, sig_y, is_signed); \
1327 DEF_METHOD(Not
, sig_a
.size(), "$not")
1328 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1329 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1330 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1331 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1332 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1333 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1334 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1335 DEF_METHOD(LogicNot
, 1, "$logic_not")
1338 #define DEF_METHOD(_func, _y_size, _type) \
1339 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1340 RTLIL::Cell *cell = addCell(name, _type); \
1341 cell->parameters["\\A_SIGNED"] = is_signed; \
1342 cell->parameters["\\B_SIGNED"] = is_signed; \
1343 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1344 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1345 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1346 cell->setPort("\\A", sig_a); \
1347 cell->setPort("\\B", sig_b); \
1348 cell->setPort("\\Y", sig_y); \
1351 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1352 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1353 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1356 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1357 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1358 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1359 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1360 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1361 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1362 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1363 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1364 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1365 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1366 DEF_METHOD(Lt
, 1, "$lt")
1367 DEF_METHOD(Le
, 1, "$le")
1368 DEF_METHOD(Eq
, 1, "$eq")
1369 DEF_METHOD(Ne
, 1, "$ne")
1370 DEF_METHOD(Eqx
, 1, "$eqx")
1371 DEF_METHOD(Nex
, 1, "$nex")
1372 DEF_METHOD(Ge
, 1, "$ge")
1373 DEF_METHOD(Gt
, 1, "$gt")
1374 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1375 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1376 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1377 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1378 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1379 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1380 DEF_METHOD(LogicOr
, 1, "$logic_or")
1383 #define DEF_METHOD(_func, _type, _pmux) \
1384 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) { \
1385 RTLIL::Cell *cell = addCell(name, _type); \
1386 cell->parameters["\\WIDTH"] = sig_a.size(); \
1387 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1388 cell->setPort("\\A", sig_a); \
1389 cell->setPort("\\B", sig_b); \
1390 cell->setPort("\\S", sig_s); \
1391 cell->setPort("\\Y", sig_y); \
1394 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1395 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1396 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1399 DEF_METHOD(Mux
, "$mux", 0)
1400 DEF_METHOD(Pmux
, "$pmux", 1)
1403 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1404 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1405 RTLIL::Cell *cell = addCell(name, _type); \
1406 cell->setPort("\\" #_P1, sig1); \
1407 cell->setPort("\\" #_P2, sig2); \
1410 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1411 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1412 add ## _func(name, sig1, sig2); \
1415 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1416 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1417 RTLIL::Cell *cell = addCell(name, _type); \
1418 cell->setPort("\\" #_P1, sig1); \
1419 cell->setPort("\\" #_P2, sig2); \
1420 cell->setPort("\\" #_P3, sig3); \
1423 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1424 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1425 add ## _func(name, sig1, sig2, sig3); \
1428 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1429 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1430 RTLIL::Cell *cell = addCell(name, _type); \
1431 cell->setPort("\\" #_P1, sig1); \
1432 cell->setPort("\\" #_P2, sig2); \
1433 cell->setPort("\\" #_P3, sig3); \
1434 cell->setPort("\\" #_P4, sig4); \
1437 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1438 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1439 add ## _func(name, sig1, sig2, sig3, sig4); \
1442 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1443 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1444 RTLIL::Cell *cell = addCell(name, _type); \
1445 cell->setPort("\\" #_P1, sig1); \
1446 cell->setPort("\\" #_P2, sig2); \
1447 cell->setPort("\\" #_P3, sig3); \
1448 cell->setPort("\\" #_P4, sig4); \
1449 cell->setPort("\\" #_P5, sig5); \
1452 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1453 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1454 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1457 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1458 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1459 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1460 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1461 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1462 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1463 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1464 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1465 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1466 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1467 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1468 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1474 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
)
1476 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1477 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1478 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1479 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1480 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1481 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1482 cell
->setPort("\\A", sig_a
);
1483 cell
->setPort("\\B", sig_b
);
1484 cell
->setPort("\\Y", sig_y
);
1488 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1490 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1491 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1492 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1493 cell
->parameters
["\\OFFSET"] = offset
;
1494 cell
->setPort("\\A", sig_a
);
1495 cell
->setPort("\\Y", sig_y
);
1499 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1501 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1502 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1503 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1504 cell
->setPort("\\A", sig_a
);
1505 cell
->setPort("\\B", sig_b
);
1506 cell
->setPort("\\Y", sig_y
);
1510 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1512 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1513 cell
->parameters
["\\LUT"] = lut
;
1514 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1515 cell
->setPort("\\A", sig_i
);
1516 cell
->setPort("\\Y", sig_o
);
1520 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1522 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1523 cell
->setPort("\\A", sig_a
);
1524 cell
->setPort("\\EN", sig_en
);
1528 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
)
1530 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1531 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1532 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1533 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1534 cell
->setPort("\\SET", sig_set
);
1535 cell
->setPort("\\CLR", sig_clr
);
1536 cell
->setPort("\\Q", sig_q
);
1540 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1542 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1543 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1544 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1545 cell
->setPort("\\CLK", sig_clk
);
1546 cell
->setPort("\\D", sig_d
);
1547 cell
->setPort("\\Q", sig_q
);
1551 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1552 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1554 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1555 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1556 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1557 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1558 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1559 cell
->setPort("\\CLK", sig_clk
);
1560 cell
->setPort("\\SET", sig_set
);
1561 cell
->setPort("\\CLR", sig_clr
);
1562 cell
->setPort("\\D", sig_d
);
1563 cell
->setPort("\\Q", sig_q
);
1567 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1568 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1570 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1571 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1572 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1573 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1574 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1575 cell
->setPort("\\CLK", sig_clk
);
1576 cell
->setPort("\\ARST", sig_arst
);
1577 cell
->setPort("\\D", sig_d
);
1578 cell
->setPort("\\Q", sig_q
);
1582 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1584 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1585 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1586 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1587 cell
->setPort("\\EN", sig_en
);
1588 cell
->setPort("\\D", sig_d
);
1589 cell
->setPort("\\Q", sig_q
);
1593 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1594 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1596 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1597 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1598 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1599 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1600 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1601 cell
->setPort("\\EN", sig_en
);
1602 cell
->setPort("\\SET", sig_set
);
1603 cell
->setPort("\\CLR", sig_clr
);
1604 cell
->setPort("\\D", sig_d
);
1605 cell
->setPort("\\Q", sig_q
);
1609 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1611 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1612 cell
->setPort("\\C", sig_clk
);
1613 cell
->setPort("\\D", sig_d
);
1614 cell
->setPort("\\Q", sig_q
);
1618 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1619 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1621 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1622 cell
->setPort("\\C", sig_clk
);
1623 cell
->setPort("\\S", sig_set
);
1624 cell
->setPort("\\R", sig_clr
);
1625 cell
->setPort("\\D", sig_d
);
1626 cell
->setPort("\\Q", sig_q
);
1630 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1631 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1633 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1634 cell
->setPort("\\C", sig_clk
);
1635 cell
->setPort("\\R", sig_arst
);
1636 cell
->setPort("\\D", sig_d
);
1637 cell
->setPort("\\Q", sig_q
);
1641 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1643 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1644 cell
->setPort("\\E", sig_en
);
1645 cell
->setPort("\\D", sig_d
);
1646 cell
->setPort("\\Q", sig_q
);
1650 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1651 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1653 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1654 cell
->setPort("\\E", sig_en
);
1655 cell
->setPort("\\S", sig_set
);
1656 cell
->setPort("\\R", sig_clr
);
1657 cell
->setPort("\\D", sig_d
);
1658 cell
->setPort("\\Q", sig_q
);
1670 port_output
= false;
1674 RTLIL::Memory::Memory()
1680 RTLIL::Cell::Cell() : module(nullptr)
1684 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1686 return connections_
.count(portname
) != 0;
1689 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1691 RTLIL::SigSpec signal
;
1692 auto conn_it
= connections_
.find(portname
);
1694 if (conn_it
!= connections_
.end())
1696 for (auto mon
: module
->monitors
)
1697 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1700 for (auto mon
: module
->design
->monitors
)
1701 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1703 connections_
.erase(conn_it
);
1707 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1709 auto conn_it
= connections_
.find(portname
);
1711 if (conn_it
== connections_
.end()) {
1712 connections_
[portname
] = RTLIL::SigSpec();
1713 conn_it
= connections_
.find(portname
);
1714 log_assert(conn_it
!= connections_
.end());
1717 for (auto mon
: module
->monitors
)
1718 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1721 for (auto mon
: module
->design
->monitors
)
1722 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1724 conn_it
->second
= signal
;
1727 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1729 return connections_
.at(portname
);
1732 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1734 return connections_
;
1737 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1739 return parameters
.count(paramname
);
1742 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1744 parameters
.erase(paramname
);
1747 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1749 parameters
[paramname
] = value
;
1752 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1754 return parameters
.at(paramname
);
1757 void RTLIL::Cell::check()
1760 InternalCellChecker
checker(NULL
, this);
1765 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1767 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1768 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1771 if (type
== "$mux" || type
== "$pmux") {
1772 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1773 if (type
== "$pmux")
1774 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1779 if (type
== "$lut") {
1780 parameters
["\\WIDTH"] = SIZE(connections_
["\\A"]);
1784 bool signedness_ab
= !type
.in("$slice", "$concat");
1786 if (connections_
.count("\\A")) {
1787 if (signedness_ab
) {
1789 parameters
["\\A_SIGNED"] = true;
1790 else if (parameters
.count("\\A_SIGNED") == 0)
1791 parameters
["\\A_SIGNED"] = false;
1793 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1796 if (connections_
.count("\\B")) {
1797 if (signedness_ab
) {
1799 parameters
["\\B_SIGNED"] = true;
1800 else if (parameters
.count("\\B_SIGNED") == 0)
1801 parameters
["\\B_SIGNED"] = false;
1803 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1806 if (connections_
.count("\\Y"))
1807 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1812 RTLIL::SigChunk::SigChunk()
1819 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1827 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1829 log_assert(wire
!= nullptr);
1831 this->width
= wire
->width
;
1835 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1837 log_assert(wire
!= nullptr);
1839 this->width
= width
;
1840 this->offset
= offset
;
1843 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1846 data
= RTLIL::Const(str
).bits
;
1851 RTLIL::SigChunk::SigChunk(int val
, int width
)
1854 data
= RTLIL::Const(val
, width
).bits
;
1855 this->width
= SIZE(data
);
1859 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1862 data
= RTLIL::Const(bit
, width
).bits
;
1863 this->width
= SIZE(data
);
1867 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1872 data
= RTLIL::Const(bit
.data
).bits
;
1874 offset
= bit
.offset
;
1878 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1880 RTLIL::SigChunk ret
;
1883 ret
.offset
= this->offset
+ offset
;
1886 for (int i
= 0; i
< length
; i
++)
1887 ret
.data
.push_back(data
[offset
+i
]);
1893 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1895 if (wire
&& other
.wire
)
1896 if (wire
->name
!= other
.wire
->name
)
1897 return wire
->name
< other
.wire
->name
;
1899 if (wire
!= other
.wire
)
1900 return wire
< other
.wire
;
1902 if (offset
!= other
.offset
)
1903 return offset
< other
.offset
;
1905 if (width
!= other
.width
)
1906 return width
< other
.width
;
1908 return data
< other
.data
;
1911 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1913 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
1916 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1923 RTLIL::SigSpec::SigSpec()
1929 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1934 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1936 cover("kernel.rtlil.sigspec.init.list");
1941 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1942 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1946 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1948 cover("kernel.rtlil.sigspec.assign");
1950 width_
= other
.width_
;
1951 hash_
= other
.hash_
;
1952 chunks_
= other
.chunks_
;
1955 if (!other
.bits_
.empty())
1957 RTLIL::SigChunk
*last
= NULL
;
1958 int last_end_offset
= 0;
1960 for (auto &bit
: other
.bits_
) {
1961 if (last
&& bit
.wire
== last
->wire
) {
1962 if (bit
.wire
== NULL
) {
1963 last
->data
.push_back(bit
.data
);
1966 } else if (last_end_offset
== bit
.offset
) {
1972 chunks_
.push_back(bit
);
1973 last
= &chunks_
.back();
1974 last_end_offset
= bit
.offset
+ 1;
1983 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1985 cover("kernel.rtlil.sigspec.init.const");
1987 chunks_
.push_back(RTLIL::SigChunk(value
));
1988 width_
= chunks_
.back().width
;
1993 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1995 cover("kernel.rtlil.sigspec.init.chunk");
1997 chunks_
.push_back(chunk
);
1998 width_
= chunks_
.back().width
;
2003 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2005 cover("kernel.rtlil.sigspec.init.wire");
2007 chunks_
.push_back(RTLIL::SigChunk(wire
));
2008 width_
= chunks_
.back().width
;
2013 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2015 cover("kernel.rtlil.sigspec.init.wire_part");
2017 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2018 width_
= chunks_
.back().width
;
2023 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2025 cover("kernel.rtlil.sigspec.init.str");
2027 chunks_
.push_back(RTLIL::SigChunk(str
));
2028 width_
= chunks_
.back().width
;
2033 RTLIL::SigSpec::SigSpec(int val
, int width
)
2035 cover("kernel.rtlil.sigspec.init.int");
2037 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2043 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2045 cover("kernel.rtlil.sigspec.init.state");
2047 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2053 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2055 cover("kernel.rtlil.sigspec.init.bit");
2057 if (bit
.wire
== NULL
)
2058 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2060 for (int i
= 0; i
< width
; i
++)
2061 chunks_
.push_back(bit
);
2067 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2069 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2073 for (auto &c
: chunks
)
2078 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2080 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2084 for (auto &bit
: bits
)
2089 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2091 cover("kernel.rtlil.sigspec.init.stdset_bits");
2095 for (auto &bit
: bits
)
2100 void RTLIL::SigSpec::pack() const
2102 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2104 if (that
->bits_
.empty())
2107 cover("kernel.rtlil.sigspec.convert.pack");
2108 log_assert(that
->chunks_
.empty());
2110 std::vector
<RTLIL::SigBit
> old_bits
;
2111 old_bits
.swap(that
->bits_
);
2113 RTLIL::SigChunk
*last
= NULL
;
2114 int last_end_offset
= 0;
2116 for (auto &bit
: old_bits
) {
2117 if (last
&& bit
.wire
== last
->wire
) {
2118 if (bit
.wire
== NULL
) {
2119 last
->data
.push_back(bit
.data
);
2122 } else if (last_end_offset
== bit
.offset
) {
2128 that
->chunks_
.push_back(bit
);
2129 last
= &that
->chunks_
.back();
2130 last_end_offset
= bit
.offset
+ 1;
2136 void RTLIL::SigSpec::unpack() const
2138 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2140 if (that
->chunks_
.empty())
2143 cover("kernel.rtlil.sigspec.convert.unpack");
2144 log_assert(that
->bits_
.empty());
2146 that
->bits_
.reserve(that
->width_
);
2147 for (auto &c
: that
->chunks_
)
2148 for (int i
= 0; i
< c
.width
; i
++)
2149 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2151 that
->chunks_
.clear();
2155 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2157 void RTLIL::SigSpec::hash() const
2159 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2161 if (that
->hash_
!= 0)
2164 cover("kernel.rtlil.sigspec.hash");
2168 for (auto &c
: that
->chunks_
)
2169 if (c
.wire
== NULL
) {
2170 for (auto &v
: c
.data
)
2171 DJB2(that
->hash_
, v
);
2173 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2174 DJB2(that
->hash_
, c
.offset
);
2175 DJB2(that
->hash_
, c
.width
);
2178 if (that
->hash_
== 0)
2182 void RTLIL::SigSpec::sort()
2185 cover("kernel.rtlil.sigspec.sort");
2186 std::sort(bits_
.begin(), bits_
.end());
2189 void RTLIL::SigSpec::sort_and_unify()
2191 cover("kernel.rtlil.sigspec.sort_and_unify");
2192 *this = this->to_sigbit_set();
2195 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2197 replace(pattern
, with
, this);
2200 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2202 log_assert(pattern
.width_
== with
.width_
);
2207 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2209 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2210 if (pattern
.bits_
[i
].wire
!= NULL
)
2211 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2213 replace(rules
, other
);
2216 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2218 replace(rules
, this);
2221 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2223 cover("kernel.rtlil.sigspec.replace");
2225 log_assert(other
!= NULL
);
2226 log_assert(width_
== other
->width_
);
2231 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2232 auto it
= rules
.find(bits_
[i
]);
2233 if (it
!= rules
.end())
2234 other
->bits_
[i
] = it
->second
;
2240 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2242 remove2(pattern
, NULL
);
2245 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2247 RTLIL::SigSpec tmp
= *this;
2248 tmp
.remove2(pattern
, other
);
2251 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2253 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2254 remove2(pattern_bits
, other
);
2257 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2259 remove2(pattern
, NULL
);
2262 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2264 RTLIL::SigSpec tmp
= *this;
2265 tmp
.remove2(pattern
, other
);
2268 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2271 cover("kernel.rtlil.sigspec.remove_other");
2273 cover("kernel.rtlil.sigspec.remove");
2277 if (other
!= NULL
) {
2278 log_assert(width_
== other
->width_
);
2282 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2284 new_bits
.resize(SIZE(bits_
));
2286 new_other_bits
.resize(SIZE(bits_
));
2289 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2290 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2293 new_other_bits
[k
] = other
->bits_
[i
];
2294 new_bits
[k
++] = bits_
[i
];
2299 new_other_bits
.resize(k
);
2301 bits_
.swap(new_bits
);
2302 width_
= SIZE(bits_
);
2304 if (other
!= NULL
) {
2305 other
->bits_
.swap(new_other_bits
);
2306 other
->width_
= SIZE(other
->bits_
);
2312 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2314 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2315 return extract(pattern_bits
, other
);
2318 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2321 cover("kernel.rtlil.sigspec.extract_other");
2323 cover("kernel.rtlil.sigspec.extract");
2325 log_assert(other
== NULL
|| width_
== other
->width_
);
2327 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2331 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2332 for (int i
= 0; i
< width_
; i
++)
2333 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2334 ret
.append_bit(bits_other
[i
]);
2336 for (int i
= 0; i
< width_
; i
++)
2337 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2338 ret
.append_bit(bits_match
[i
]);
2345 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2347 cover("kernel.rtlil.sigspec.replace_pos");
2352 log_assert(offset
>= 0);
2353 log_assert(with
.width_
>= 0);
2354 log_assert(offset
+with
.width_
<= width_
);
2356 for (int i
= 0; i
< with
.width_
; i
++)
2357 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2362 void RTLIL::SigSpec::remove_const()
2366 cover("kernel.rtlil.sigspec.remove_const.packed");
2368 std::vector
<RTLIL::SigChunk
> new_chunks
;
2369 new_chunks
.reserve(SIZE(chunks_
));
2372 for (auto &chunk
: chunks_
)
2373 if (chunk
.wire
!= NULL
) {
2374 new_chunks
.push_back(chunk
);
2375 width_
+= chunk
.width
;
2378 chunks_
.swap(new_chunks
);
2382 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2384 std::vector
<RTLIL::SigBit
> new_bits
;
2385 new_bits
.reserve(width_
);
2387 for (auto &bit
: bits_
)
2388 if (bit
.wire
!= NULL
)
2389 new_bits
.push_back(bit
);
2391 bits_
.swap(new_bits
);
2392 width_
= bits_
.size();
2398 void RTLIL::SigSpec::remove(int offset
, int length
)
2400 cover("kernel.rtlil.sigspec.remove_pos");
2404 log_assert(offset
>= 0);
2405 log_assert(length
>= 0);
2406 log_assert(offset
+ length
<= width_
);
2408 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2409 width_
= bits_
.size();
2414 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2417 cover("kernel.rtlil.sigspec.extract_pos");
2418 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2421 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2423 if (signal
.width_
== 0)
2431 cover("kernel.rtlil.sigspec.append");
2433 if (packed() != signal
.packed()) {
2439 for (auto &other_c
: signal
.chunks_
)
2441 auto &my_last_c
= chunks_
.back();
2442 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2443 auto &this_data
= my_last_c
.data
;
2444 auto &other_data
= other_c
.data
;
2445 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2446 my_last_c
.width
+= other_c
.width
;
2448 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2449 my_last_c
.width
+= other_c
.width
;
2451 chunks_
.push_back(other_c
);
2454 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2456 width_
+= signal
.width_
;
2460 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2464 cover("kernel.rtlil.sigspec.append_bit.packed");
2466 if (chunks_
.size() == 0)
2467 chunks_
.push_back(bit
);
2469 if (bit
.wire
== NULL
)
2470 if (chunks_
.back().wire
== NULL
) {
2471 chunks_
.back().data
.push_back(bit
.data
);
2472 chunks_
.back().width
++;
2474 chunks_
.push_back(bit
);
2476 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2477 chunks_
.back().width
++;
2479 chunks_
.push_back(bit
);
2483 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2484 bits_
.push_back(bit
);
2491 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2493 cover("kernel.rtlil.sigspec.extend");
2498 remove(width
, width_
- width
);
2500 if (width_
< width
) {
2501 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2502 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2503 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2504 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2505 while (width_
< width
)
2510 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2512 cover("kernel.rtlil.sigspec.extend_u0");
2517 remove(width
, width_
- width
);
2519 if (width_
< width
) {
2520 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2522 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2523 while (width_
< width
)
2529 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2531 cover("kernel.rtlil.sigspec.repeat");
2534 for (int i
= 0; i
< num
; i
++)
2540 void RTLIL::SigSpec::check() const
2544 cover("kernel.rtlil.sigspec.check.skip");
2548 cover("kernel.rtlil.sigspec.check.packed");
2551 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2552 const RTLIL::SigChunk chunk
= chunks_
[i
];
2553 if (chunk
.wire
== NULL
) {
2555 log_assert(chunks_
[i
-1].wire
!= NULL
);
2556 log_assert(chunk
.offset
== 0);
2557 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
2559 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2560 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2561 log_assert(chunk
.offset
>= 0);
2562 log_assert(chunk
.width
>= 0);
2563 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2564 log_assert(chunk
.data
.size() == 0);
2568 log_assert(w
== width_
);
2569 log_assert(bits_
.empty());
2573 cover("kernel.rtlil.sigspec.check.unpacked");
2575 log_assert(width_
== SIZE(bits_
));
2576 log_assert(chunks_
.empty());
2581 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2583 cover("kernel.rtlil.sigspec.comp_lt");
2588 if (width_
!= other
.width_
)
2589 return width_
< other
.width_
;
2594 if (chunks_
.size() != other
.chunks_
.size())
2595 return chunks_
.size() < other
.chunks_
.size();
2600 if (hash_
!= other
.hash_
)
2601 return hash_
< other
.hash_
;
2603 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2604 if (chunks_
[i
] != other
.chunks_
[i
]) {
2605 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2606 return chunks_
[i
] < other
.chunks_
[i
];
2609 cover("kernel.rtlil.sigspec.comp_lt.equal");
2613 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2615 cover("kernel.rtlil.sigspec.comp_eq");
2620 if (width_
!= other
.width_
)
2626 if (chunks_
.size() != chunks_
.size())
2632 if (hash_
!= other
.hash_
)
2635 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2636 if (chunks_
[i
] != other
.chunks_
[i
]) {
2637 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2641 cover("kernel.rtlil.sigspec.comp_eq.equal");
2645 bool RTLIL::SigSpec::is_wire() const
2647 cover("kernel.rtlil.sigspec.is_wire");
2650 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2653 bool RTLIL::SigSpec::is_chunk() const
2655 cover("kernel.rtlil.sigspec.is_chunk");
2658 return SIZE(chunks_
) == 1;
2661 bool RTLIL::SigSpec::is_fully_const() const
2663 cover("kernel.rtlil.sigspec.is_fully_const");
2666 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2667 if (it
->width
> 0 && it
->wire
!= NULL
)
2672 bool RTLIL::SigSpec::is_fully_def() const
2674 cover("kernel.rtlil.sigspec.is_fully_def");
2677 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2678 if (it
->width
> 0 && it
->wire
!= NULL
)
2680 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2681 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
2687 bool RTLIL::SigSpec::is_fully_undef() const
2689 cover("kernel.rtlil.sigspec.is_fully_undef");
2692 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2693 if (it
->width
> 0 && it
->wire
!= NULL
)
2695 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2696 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
2702 bool RTLIL::SigSpec::has_marked_bits() const
2704 cover("kernel.rtlil.sigspec.has_marked_bits");
2707 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2708 if (it
->width
> 0 && it
->wire
== NULL
) {
2709 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2710 if (it
->data
[i
] == RTLIL::State::Sm
)
2716 bool RTLIL::SigSpec::as_bool() const
2718 cover("kernel.rtlil.sigspec.as_bool");
2721 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2723 return RTLIL::Const(chunks_
[0].data
).as_bool();
2727 int RTLIL::SigSpec::as_int(bool is_signed
) const
2729 cover("kernel.rtlil.sigspec.as_int");
2732 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2734 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
2738 std::string
RTLIL::SigSpec::as_string() const
2740 cover("kernel.rtlil.sigspec.as_string");
2744 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2745 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2746 if (chunk
.wire
!= NULL
)
2747 for (int j
= 0; j
< chunk
.width
; j
++)
2750 str
+= RTLIL::Const(chunk
.data
).as_string();
2755 RTLIL::Const
RTLIL::SigSpec::as_const() const
2757 cover("kernel.rtlil.sigspec.as_const");
2760 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2762 return chunks_
[0].data
;
2763 return RTLIL::Const();
2766 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2768 cover("kernel.rtlil.sigspec.as_wire");
2771 log_assert(is_wire());
2772 return chunks_
[0].wire
;
2775 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2777 cover("kernel.rtlil.sigspec.as_chunk");
2780 log_assert(is_chunk());
2784 bool RTLIL::SigSpec::match(std::string pattern
) const
2786 cover("kernel.rtlil.sigspec.match");
2789 std::string str
= as_string();
2790 log_assert(pattern
.size() == str
.size());
2792 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2793 if (pattern
[i
] == ' ')
2795 if (pattern
[i
] == '*') {
2796 if (str
[i
] != 'z' && str
[i
] != 'x')
2800 if (pattern
[i
] != str
[i
])
2807 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2809 cover("kernel.rtlil.sigspec.to_sigbit_set");
2812 std::set
<RTLIL::SigBit
> sigbits
;
2813 for (auto &c
: chunks_
)
2814 for (int i
= 0; i
< c
.width
; i
++)
2815 sigbits
.insert(RTLIL::SigBit(c
, i
));
2819 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2821 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2827 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2829 cover("kernel.rtlil.sigspec.to_sigbit_map");
2834 log_assert(width_
== other
.width_
);
2836 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2837 for (int i
= 0; i
< width_
; i
++)
2838 new_map
[bits_
[i
]] = other
.bits_
[i
];
2843 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2845 cover("kernel.rtlil.sigspec.to_single_sigbit");
2848 log_assert(width_
== 1);
2849 for (auto &c
: chunks_
)
2851 return RTLIL::SigBit(c
);
2855 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2857 size_t start
= 0, end
= 0;
2858 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2859 tokens
.push_back(text
.substr(start
, end
- start
));
2862 tokens
.push_back(text
.substr(start
));
2865 static int sigspec_parse_get_dummy_line_num()
2870 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2872 cover("kernel.rtlil.sigspec.parse");
2874 std::vector
<std::string
> tokens
;
2875 sigspec_parse_split(tokens
, str
, ',');
2877 sig
= RTLIL::SigSpec();
2878 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2880 std::string netname
= tokens
[tokidx
];
2881 std::string indices
;
2883 if (netname
.size() == 0)
2886 if ('0' <= netname
[0] && netname
[0] <= '9') {
2887 cover("kernel.rtlil.sigspec.parse.const");
2888 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2889 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2892 sig
.append(RTLIL::Const(ast
->bits
));
2900 cover("kernel.rtlil.sigspec.parse.net");
2902 if (netname
[0] != '$' && netname
[0] != '\\')
2903 netname
= "\\" + netname
;
2905 if (module
->wires_
.count(netname
) == 0) {
2906 size_t indices_pos
= netname
.size()-1;
2907 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2910 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2911 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2913 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2915 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2916 indices
= netname
.substr(indices_pos
);
2917 netname
= netname
.substr(0, indices_pos
);
2922 if (module
->wires_
.count(netname
) == 0)
2925 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2926 if (!indices
.empty()) {
2927 std::vector
<std::string
> index_tokens
;
2928 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2929 if (index_tokens
.size() == 1) {
2930 cover("kernel.rtlil.sigspec.parse.bit_sel");
2931 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2933 cover("kernel.rtlil.sigspec.parse.part_sel");
2934 int a
= atoi(index_tokens
.at(0).c_str());
2935 int b
= atoi(index_tokens
.at(1).c_str());
2940 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2949 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2951 if (str
.empty() || str
[0] != '@')
2952 return parse(sig
, module
, str
);
2954 cover("kernel.rtlil.sigspec.parse.sel");
2956 str
= RTLIL::escape_id(str
.substr(1));
2957 if (design
->selection_vars
.count(str
) == 0)
2960 sig
= RTLIL::SigSpec();
2961 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2962 for (auto &it
: module
->wires_
)
2963 if (sel
.selected_member(module
->name
, it
.first
))
2964 sig
.append(it
.second
);
2969 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2972 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2973 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2978 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2979 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2983 if (lhs
.chunks_
.size() == 1) {
2984 char *p
= (char*)str
.c_str(), *endptr
;
2985 long long int val
= strtoll(p
, &endptr
, 10);
2986 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2987 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2988 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2993 return parse(sig
, module
, str
);
2996 RTLIL::CaseRule::~CaseRule()
2998 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3002 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3004 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3005 new_caserule
->compare
= compare
;
3006 new_caserule
->actions
= actions
;
3007 for (auto &it
: switches
)
3008 new_caserule
->switches
.push_back(it
->clone());
3009 return new_caserule
;
3012 RTLIL::SwitchRule::~SwitchRule()
3014 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3018 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3020 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3021 new_switchrule
->signal
= signal
;
3022 new_switchrule
->attributes
= attributes
;
3023 for (auto &it
: cases
)
3024 new_switchrule
->cases
.push_back(it
->clone());
3025 return new_switchrule
;
3029 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3031 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3032 new_syncrule
->type
= type
;
3033 new_syncrule
->signal
= signal
;
3034 new_syncrule
->actions
= actions
;
3035 return new_syncrule
;
3038 RTLIL::Process::~Process()
3040 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3044 RTLIL::Process
*RTLIL::Process::clone() const
3046 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3048 new_proc
->name
= name
;
3049 new_proc
->attributes
= attributes
;
3051 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3052 new_proc
->root_case
= *rc_ptr
;
3053 rc_ptr
->switches
.clear();
3056 for (auto &it
: syncs
)
3057 new_proc
->syncs
.push_back(it
->clone());