2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "frontends/verilog/verilog_frontend.h"
22 #include "backends/ilang/ilang_backend.h"
29 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
30 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
31 std::map
<char*, int, RTLIL::IdString::char_ptr_cmp
> RTLIL::IdString::global_id_index_
;
32 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
36 flags
= RTLIL::CONST_FLAG_NONE
;
39 RTLIL::Const::Const(std::string str
)
41 flags
= RTLIL::CONST_FLAG_STRING
;
42 for (int i
= str
.size()-1; i
>= 0; i
--) {
43 unsigned char ch
= str
[i
];
44 for (int j
= 0; j
< 8; j
++) {
45 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
51 RTLIL::Const::Const(int val
, int width
)
53 flags
= RTLIL::CONST_FLAG_NONE
;
54 for (int i
= 0; i
< width
; i
++) {
55 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
60 RTLIL::Const::Const(RTLIL::State bit
, int width
)
62 flags
= RTLIL::CONST_FLAG_NONE
;
63 for (int i
= 0; i
< width
; i
++)
67 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
69 if (bits
.size() != other
.bits
.size())
70 return bits
.size() < other
.bits
.size();
71 for (size_t i
= 0; i
< bits
.size(); i
++)
72 if (bits
[i
] != other
.bits
[i
])
73 return bits
[i
] < other
.bits
[i
];
77 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
79 return bits
== other
.bits
;
82 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
84 return bits
!= other
.bits
;
87 bool RTLIL::Const::as_bool() const
89 for (size_t i
= 0; i
< bits
.size(); i
++)
90 if (bits
[i
] == RTLIL::S1
)
95 int RTLIL::Const::as_int(bool is_signed
) const
98 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
99 if (bits
[i
] == RTLIL::S1
)
101 if (is_signed
&& bits
.back() == RTLIL::S1
)
102 for (size_t i
= bits
.size(); i
< 32; i
++)
107 std::string
RTLIL::Const::as_string() const
110 for (size_t i
= bits
.size(); i
> 0; i
--)
112 case S0
: ret
+= "0"; break;
113 case S1
: ret
+= "1"; break;
114 case Sx
: ret
+= "x"; break;
115 case Sz
: ret
+= "z"; break;
116 case Sa
: ret
+= "-"; break;
117 case Sm
: ret
+= "m"; break;
122 std::string
RTLIL::Const::decode_string() const
125 std::vector
<char> string_chars
;
126 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
128 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
129 if (bits
[i
+ j
] == RTLIL::State::S1
)
132 string_chars
.push_back(ch
);
134 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
135 string
+= string_chars
[i
];
139 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
143 if (selected_modules
.count(mod_name
) > 0)
145 if (selected_members
.count(mod_name
) > 0)
150 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
154 if (selected_modules
.count(mod_name
) > 0)
159 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
163 if (selected_modules
.count(mod_name
) > 0)
165 if (selected_members
.count(mod_name
) > 0)
166 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
171 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
173 if (full_selection
) {
174 selected_modules
.clear();
175 selected_members
.clear();
179 std::vector
<RTLIL::IdString
> del_list
, add_list
;
182 for (auto mod_name
: selected_modules
) {
183 if (design
->modules_
.count(mod_name
) == 0)
184 del_list
.push_back(mod_name
);
185 selected_members
.erase(mod_name
);
187 for (auto mod_name
: del_list
)
188 selected_modules
.erase(mod_name
);
191 for (auto &it
: selected_members
)
192 if (design
->modules_
.count(it
.first
) == 0)
193 del_list
.push_back(it
.first
);
194 for (auto mod_name
: del_list
)
195 selected_members
.erase(mod_name
);
197 for (auto &it
: selected_members
) {
199 for (auto memb_name
: it
.second
)
200 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
201 del_list
.push_back(memb_name
);
202 for (auto memb_name
: del_list
)
203 it
.second
.erase(memb_name
);
208 for (auto &it
: selected_members
)
209 if (it
.second
.size() == 0)
210 del_list
.push_back(it
.first
);
211 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
212 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
213 add_list
.push_back(it
.first
);
214 for (auto mod_name
: del_list
)
215 selected_members
.erase(mod_name
);
216 for (auto mod_name
: add_list
) {
217 selected_members
.erase(mod_name
);
218 selected_modules
.insert(mod_name
);
221 if (selected_modules
.size() == design
->modules_
.size()) {
222 full_selection
= true;
223 selected_modules
.clear();
224 selected_members
.clear();
228 RTLIL::Design::Design()
230 refcount_modules_
= 0;
233 RTLIL::Design::~Design()
235 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
239 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
241 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
244 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
246 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
249 void RTLIL::Design::add(RTLIL::Module
*module
)
251 log_assert(modules_
.count(module
->name
) == 0);
252 log_assert(refcount_modules_
== 0);
253 modules_
[module
->name
] = module
;
254 module
->design
= this;
256 for (auto mon
: monitors
)
257 mon
->notify_module_add(module
);
260 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
262 log_assert(modules_
.count(name
) == 0);
263 log_assert(refcount_modules_
== 0);
265 RTLIL::Module
*module
= new RTLIL::Module
;
266 modules_
[name
] = module
;
267 module
->design
= this;
270 for (auto mon
: monitors
)
271 mon
->notify_module_add(module
);
276 void RTLIL::Design::scratchpad_unset(std::string varname
)
278 scratchpad
.erase(varname
);
281 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
283 scratchpad
[varname
] = stringf("%d", value
);
286 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
288 scratchpad
[varname
] = value
? "true" : "false";
291 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
293 scratchpad
[varname
] = value
;
296 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
298 if (scratchpad
.count(varname
) == 0)
299 return default_value
;
301 std::string str
= scratchpad
.at(varname
);
303 if (str
== "0" || str
== "false")
306 if (str
== "1" || str
== "true")
309 char *endptr
= nullptr;
310 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
311 return *endptr
? default_value
: parsed_value
;
314 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
316 if (scratchpad
.count(varname
) == 0)
317 return default_value
;
319 std::string str
= scratchpad
.at(varname
);
321 if (str
== "0" || str
== "false")
324 if (str
== "1" || str
== "true")
327 return default_value
;
330 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
332 if (scratchpad
.count(varname
) == 0)
333 return default_value
;
334 return scratchpad
.at(varname
);
337 void RTLIL::Design::remove(RTLIL::Module
*module
)
339 for (auto mon
: monitors
)
340 mon
->notify_module_del(module
);
342 log_assert(modules_
.at(module
->name
) == module
);
343 modules_
.erase(module
->name
);
347 void RTLIL::Design::check()
350 for (auto &it
: modules_
) {
351 log_assert(this == it
.second
->design
);
352 log_assert(it
.first
== it
.second
->name
);
353 log_assert(!it
.first
.empty());
359 void RTLIL::Design::optimize()
361 for (auto &it
: modules_
)
362 it
.second
->optimize();
363 for (auto &it
: selection_stack
)
365 for (auto &it
: selection_vars
)
366 it
.second
.optimize(this);
369 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
371 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
373 if (selection_stack
.size() == 0)
375 return selection_stack
.back().selected_module(mod_name
);
378 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
380 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
382 if (selection_stack
.size() == 0)
384 return selection_stack
.back().selected_whole_module(mod_name
);
387 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
389 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
391 if (selection_stack
.size() == 0)
393 return selection_stack
.back().selected_member(mod_name
, memb_name
);
396 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
398 return selected_module(mod
->name
);
401 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
403 return selected_whole_module(mod
->name
);
406 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
408 std::vector
<RTLIL::Module
*> result
;
409 result
.reserve(modules_
.size());
410 for (auto &it
: modules_
)
411 if (selected_module(it
.first
))
412 result
.push_back(it
.second
);
416 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
418 std::vector
<RTLIL::Module
*> result
;
419 result
.reserve(modules_
.size());
420 for (auto &it
: modules_
)
421 if (selected_whole_module(it
.first
))
422 result
.push_back(it
.second
);
426 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
428 std::vector
<RTLIL::Module
*> result
;
429 result
.reserve(modules_
.size());
430 for (auto &it
: modules_
)
431 if (selected_whole_module(it
.first
))
432 result
.push_back(it
.second
);
433 else if (selected_module(it
.first
))
434 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
438 RTLIL::Module::Module()
445 RTLIL::Module::~Module()
447 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
449 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
451 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
453 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
457 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
459 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
462 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
464 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
469 struct InternalCellChecker
471 RTLIL::Module
*module
;
473 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
475 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
477 void error(int linenr
)
479 std::stringstream buf
;
480 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
482 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
483 module
? module
->name
.c_str() : "", module
? "." : "",
484 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
487 int param(const char *name
)
489 if (cell
->parameters
.count(name
) == 0)
491 expected_params
.insert(name
);
492 return cell
->parameters
.at(name
).as_int();
495 int param_bool(const char *name
)
498 if (cell
->parameters
.at(name
).bits
.size() > 32)
500 if (v
!= 0 && v
!= 1)
505 void param_bits(const char *name
, int width
)
508 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
512 void port(const char *name
, int width
)
514 if (!cell
->hasPort(name
))
516 if (cell
->getPort(name
).size() != width
)
518 expected_ports
.insert(name
);
521 void check_expected(bool check_matched_sign
= true)
523 for (auto ¶
: cell
->parameters
)
524 if (expected_params
.count(para
.first
) == 0)
526 for (auto &conn
: cell
->connections())
527 if (expected_ports
.count(conn
.first
) == 0)
530 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
531 bool a_is_signed
= param("\\A_SIGNED") != 0;
532 bool b_is_signed
= param("\\B_SIGNED") != 0;
533 if (a_is_signed
!= b_is_signed
)
538 void check_gate(const char *ports
)
540 if (cell
->parameters
.size() != 0)
543 for (const char *p
= ports
; *p
; p
++) {
544 char portname
[3] = { '\\', *p
, 0 };
545 if (!cell
->hasPort(portname
))
547 if (cell
->getPort(portname
).size() != 1)
551 for (auto &conn
: cell
->connections()) {
552 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
554 if (strchr(ports
, conn
.first
[1]) == NULL
)
561 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
562 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
565 if (cell
->type
.in("$not", "$pos", "$bu0", "$neg")) {
566 param_bool("\\A_SIGNED");
567 port("\\A", param("\\A_WIDTH"));
568 port("\\Y", param("\\Y_WIDTH"));
573 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
574 param_bool("\\A_SIGNED");
575 param_bool("\\B_SIGNED");
576 port("\\A", param("\\A_WIDTH"));
577 port("\\B", param("\\B_WIDTH"));
578 port("\\Y", param("\\Y_WIDTH"));
583 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
584 param_bool("\\A_SIGNED");
585 port("\\A", param("\\A_WIDTH"));
586 port("\\Y", param("\\Y_WIDTH"));
591 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
592 param_bool("\\A_SIGNED");
593 param_bool("\\B_SIGNED");
594 port("\\A", param("\\A_WIDTH"));
595 port("\\B", param("\\B_WIDTH"));
596 port("\\Y", param("\\Y_WIDTH"));
597 check_expected(false);
601 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
602 param_bool("\\A_SIGNED");
603 param_bool("\\B_SIGNED");
604 port("\\A", param("\\A_WIDTH"));
605 port("\\B", param("\\B_WIDTH"));
606 port("\\Y", param("\\Y_WIDTH"));
611 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
612 param_bool("\\A_SIGNED");
613 param_bool("\\B_SIGNED");
614 port("\\A", param("\\A_WIDTH"));
615 port("\\B", param("\\B_WIDTH"));
616 port("\\Y", param("\\Y_WIDTH"));
617 check_expected(cell
->type
!= "$pow");
621 if (cell
->type
== "$alu") {
622 param_bool("\\A_SIGNED");
623 param_bool("\\B_SIGNED");
624 port("\\A", param("\\A_WIDTH"));
625 port("\\B", param("\\B_WIDTH"));
628 port("\\X", param("\\Y_WIDTH"));
629 port("\\Y", param("\\Y_WIDTH"));
630 port("\\CO", param("\\Y_WIDTH"));
635 if (cell
->type
== "$logic_not") {
636 param_bool("\\A_SIGNED");
637 port("\\A", param("\\A_WIDTH"));
638 port("\\Y", param("\\Y_WIDTH"));
643 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
644 param_bool("\\A_SIGNED");
645 param_bool("\\B_SIGNED");
646 port("\\A", param("\\A_WIDTH"));
647 port("\\B", param("\\B_WIDTH"));
648 port("\\Y", param("\\Y_WIDTH"));
649 check_expected(false);
653 if (cell
->type
== "$slice") {
655 port("\\A", param("\\A_WIDTH"));
656 port("\\Y", param("\\Y_WIDTH"));
657 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
663 if (cell
->type
== "$concat") {
664 port("\\A", param("\\A_WIDTH"));
665 port("\\B", param("\\B_WIDTH"));
666 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
671 if (cell
->type
== "$mux") {
672 port("\\A", param("\\WIDTH"));
673 port("\\B", param("\\WIDTH"));
675 port("\\Y", param("\\WIDTH"));
680 if (cell
->type
== "$pmux") {
681 port("\\A", param("\\WIDTH"));
682 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
683 port("\\S", param("\\S_WIDTH"));
684 port("\\Y", param("\\WIDTH"));
689 if (cell
->type
== "$lut") {
691 port("\\A", param("\\WIDTH"));
697 if (cell
->type
== "$sr") {
698 param_bool("\\SET_POLARITY");
699 param_bool("\\CLR_POLARITY");
700 port("\\SET", param("\\WIDTH"));
701 port("\\CLR", param("\\WIDTH"));
702 port("\\Q", param("\\WIDTH"));
707 if (cell
->type
== "$dff") {
708 param_bool("\\CLK_POLARITY");
710 port("\\D", param("\\WIDTH"));
711 port("\\Q", param("\\WIDTH"));
716 if (cell
->type
== "$dffsr") {
717 param_bool("\\CLK_POLARITY");
718 param_bool("\\SET_POLARITY");
719 param_bool("\\CLR_POLARITY");
721 port("\\SET", param("\\WIDTH"));
722 port("\\CLR", param("\\WIDTH"));
723 port("\\D", param("\\WIDTH"));
724 port("\\Q", param("\\WIDTH"));
729 if (cell
->type
== "$adff") {
730 param_bool("\\CLK_POLARITY");
731 param_bool("\\ARST_POLARITY");
732 param_bits("\\ARST_VALUE", param("\\WIDTH"));
735 port("\\D", param("\\WIDTH"));
736 port("\\Q", param("\\WIDTH"));
741 if (cell
->type
== "$dlatch") {
742 param_bool("\\EN_POLARITY");
744 port("\\D", param("\\WIDTH"));
745 port("\\Q", param("\\WIDTH"));
750 if (cell
->type
== "$dlatchsr") {
751 param_bool("\\EN_POLARITY");
752 param_bool("\\SET_POLARITY");
753 param_bool("\\CLR_POLARITY");
755 port("\\SET", param("\\WIDTH"));
756 port("\\CLR", param("\\WIDTH"));
757 port("\\D", param("\\WIDTH"));
758 port("\\Q", param("\\WIDTH"));
763 if (cell
->type
== "$fsm") {
765 param_bool("\\CLK_POLARITY");
766 param_bool("\\ARST_POLARITY");
767 param("\\STATE_BITS");
768 param("\\STATE_NUM");
769 param("\\STATE_NUM_LOG2");
770 param("\\STATE_RST");
771 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
772 param("\\TRANS_NUM");
773 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
776 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
777 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
782 if (cell
->type
== "$memrd") {
784 param_bool("\\CLK_ENABLE");
785 param_bool("\\CLK_POLARITY");
786 param_bool("\\TRANSPARENT");
788 port("\\ADDR", param("\\ABITS"));
789 port("\\DATA", param("\\WIDTH"));
794 if (cell
->type
== "$memwr") {
796 param_bool("\\CLK_ENABLE");
797 param_bool("\\CLK_POLARITY");
800 port("\\EN", param("\\WIDTH"));
801 port("\\ADDR", param("\\ABITS"));
802 port("\\DATA", param("\\WIDTH"));
807 if (cell
->type
== "$mem") {
811 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
812 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
813 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
814 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
815 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
816 port("\\RD_CLK", param("\\RD_PORTS"));
817 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
818 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
819 port("\\WR_CLK", param("\\WR_PORTS"));
820 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
821 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
822 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
827 if (cell
->type
== "$assert") {
834 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
835 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
836 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
837 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
838 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
839 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
840 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
841 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
842 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
843 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
844 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
845 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
847 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
848 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
849 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
850 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
852 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
853 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
855 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
856 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
857 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
858 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
859 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
860 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
861 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
862 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
864 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
865 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
866 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
867 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
868 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
869 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
870 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
871 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
873 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
874 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
876 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
877 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
878 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
879 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
880 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
881 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
882 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
883 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
891 void RTLIL::Module::check()
894 std::vector
<bool> ports_declared
;
895 for (auto &it
: wires_
) {
896 log_assert(this == it
.second
->module
);
897 log_assert(it
.first
== it
.second
->name
);
898 log_assert(!it
.first
.empty());
899 log_assert(it
.second
->width
>= 0);
900 log_assert(it
.second
->port_id
>= 0);
901 for (auto &it2
: it
.second
->attributes
)
902 log_assert(!it2
.first
.empty());
903 if (it
.second
->port_id
) {
904 log_assert(SIZE(ports
) >= it
.second
->port_id
);
905 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
906 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
907 if (SIZE(ports_declared
) < it
.second
->port_id
)
908 ports_declared
.resize(it
.second
->port_id
);
909 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
910 ports_declared
[it
.second
->port_id
-1] = true;
912 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
914 for (auto port_declared
: ports_declared
)
915 log_assert(port_declared
== true);
916 log_assert(SIZE(ports
) == SIZE(ports_declared
));
918 for (auto &it
: memories
) {
919 log_assert(it
.first
== it
.second
->name
);
920 log_assert(!it
.first
.empty());
921 log_assert(it
.second
->width
>= 0);
922 log_assert(it
.second
->size
>= 0);
923 for (auto &it2
: it
.second
->attributes
)
924 log_assert(!it2
.first
.empty());
927 for (auto &it
: cells_
) {
928 log_assert(this == it
.second
->module
);
929 log_assert(it
.first
== it
.second
->name
);
930 log_assert(!it
.first
.empty());
931 log_assert(!it
.second
->type
.empty());
932 for (auto &it2
: it
.second
->connections()) {
933 log_assert(!it2
.first
.empty());
936 for (auto &it2
: it
.second
->attributes
)
937 log_assert(!it2
.first
.empty());
938 for (auto &it2
: it
.second
->parameters
)
939 log_assert(!it2
.first
.empty());
940 InternalCellChecker
checker(this, it
.second
);
944 for (auto &it
: processes
) {
945 log_assert(it
.first
== it
.second
->name
);
946 log_assert(!it
.first
.empty());
947 // FIXME: More checks here..
950 for (auto &it
: connections_
) {
951 log_assert(it
.first
.size() == it
.second
.size());
956 for (auto &it
: attributes
)
957 log_assert(!it
.first
.empty());
961 void RTLIL::Module::optimize()
965 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
967 log_assert(new_mod
->refcount_wires_
== 0);
968 log_assert(new_mod
->refcount_cells_
== 0);
970 new_mod
->connections_
= connections_
;
971 new_mod
->attributes
= attributes
;
973 for (auto &it
: wires_
)
974 new_mod
->addWire(it
.first
, it
.second
);
976 for (auto &it
: memories
)
977 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
979 for (auto &it
: cells_
)
980 new_mod
->addCell(it
.first
, it
.second
);
982 for (auto &it
: processes
)
983 new_mod
->processes
[it
.first
] = it
.second
->clone();
985 struct RewriteSigSpecWorker
988 void operator()(RTLIL::SigSpec
&sig
)
990 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
991 for (auto &c
: chunks
)
993 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
998 RewriteSigSpecWorker rewriteSigSpecWorker
;
999 rewriteSigSpecWorker
.mod
= new_mod
;
1000 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1001 new_mod
->fixup_ports();
1004 RTLIL::Module
*RTLIL::Module::clone() const
1006 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1007 new_mod
->name
= name
;
1012 bool RTLIL::Module::has_memories() const
1014 return !memories
.empty();
1017 bool RTLIL::Module::has_processes() const
1019 return !processes
.empty();
1022 bool RTLIL::Module::has_memories_warn() const
1024 if (!memories
.empty())
1025 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1026 return !memories
.empty();
1029 bool RTLIL::Module::has_processes_warn() const
1031 if (!processes
.empty())
1032 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1033 return !processes
.empty();
1036 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1038 std::vector
<RTLIL::Wire
*> result
;
1039 result
.reserve(wires_
.size());
1040 for (auto &it
: wires_
)
1041 if (design
->selected(this, it
.second
))
1042 result
.push_back(it
.second
);
1046 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1048 std::vector
<RTLIL::Cell
*> result
;
1049 result
.reserve(wires_
.size());
1050 for (auto &it
: cells_
)
1051 if (design
->selected(this, it
.second
))
1052 result
.push_back(it
.second
);
1056 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1058 log_assert(!wire
->name
.empty());
1059 log_assert(count_id(wire
->name
) == 0);
1060 log_assert(refcount_wires_
== 0);
1061 wires_
[wire
->name
] = wire
;
1062 wire
->module
= this;
1065 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1067 log_assert(!cell
->name
.empty());
1068 log_assert(count_id(cell
->name
) == 0);
1069 log_assert(refcount_cells_
== 0);
1070 cells_
[cell
->name
] = cell
;
1071 cell
->module
= this;
1075 struct DeleteWireWorker
1077 RTLIL::Module
*module
;
1078 const std::set
<RTLIL::Wire
*> *wires_p
;
1080 void operator()(RTLIL::SigSpec
&sig
) {
1081 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1082 for (auto &c
: chunks
)
1083 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1084 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1093 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1095 std::setPort
<RTLIL::Wire
*> wires_
;
1096 wires_
.insert(wire
);
1101 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1103 log_assert(refcount_wires_
== 0);
1105 DeleteWireWorker delete_wire_worker
;
1106 delete_wire_worker
.module
= this;
1107 delete_wire_worker
.wires_p
= &wires
;
1108 rewrite_sigspecs(delete_wire_worker
);
1110 for (auto &it
: wires
) {
1111 log_assert(wires_
.count(it
->name
) != 0);
1112 wires_
.erase(it
->name
);
1117 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1119 log_assert(cells_
.count(cell
->name
) != 0);
1120 log_assert(refcount_cells_
== 0);
1121 cells_
.erase(cell
->name
);
1125 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1127 log_assert(wires_
[wire
->name
] == wire
);
1128 log_assert(refcount_wires_
== 0);
1129 wires_
.erase(wire
->name
);
1130 wire
->name
= new_name
;
1134 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1136 log_assert(cells_
[cell
->name
] == cell
);
1137 log_assert(refcount_wires_
== 0);
1138 cells_
.erase(cell
->name
);
1139 cell
->name
= new_name
;
1143 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1145 log_assert(count_id(old_name
) != 0);
1146 if (wires_
.count(old_name
))
1147 rename(wires_
.at(old_name
), new_name
);
1148 else if (cells_
.count(old_name
))
1149 rename(cells_
.at(old_name
), new_name
);
1154 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1156 log_assert(wires_
[w1
->name
] == w1
);
1157 log_assert(wires_
[w2
->name
] == w2
);
1158 log_assert(refcount_wires_
== 0);
1160 wires_
.erase(w1
->name
);
1161 wires_
.erase(w2
->name
);
1163 std::swap(w1
->name
, w2
->name
);
1165 wires_
[w1
->name
] = w1
;
1166 wires_
[w2
->name
] = w2
;
1169 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1171 log_assert(cells_
[c1
->name
] == c1
);
1172 log_assert(cells_
[c2
->name
] == c2
);
1173 log_assert(refcount_cells_
== 0);
1175 cells_
.erase(c1
->name
);
1176 cells_
.erase(c2
->name
);
1178 std::swap(c1
->name
, c2
->name
);
1180 cells_
[c1
->name
] = c1
;
1181 cells_
[c2
->name
] = c2
;
1184 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1187 return uniquify(name
, index
);
1190 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1193 if (count_id(name
) == 0)
1199 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1200 if (count_id(new_name
) == 0)
1206 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1208 if (a
->port_id
&& !b
->port_id
)
1210 if (!a
->port_id
&& b
->port_id
)
1213 if (a
->port_id
== b
->port_id
)
1214 return a
->name
< b
->name
;
1215 return a
->port_id
< b
->port_id
;
1218 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1220 for (auto mon
: monitors
)
1221 mon
->notify_connect(this, conn
);
1224 for (auto mon
: design
->monitors
)
1225 mon
->notify_connect(this, conn
);
1227 connections_
.push_back(conn
);
1230 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1232 connect(RTLIL::SigSig(lhs
, rhs
));
1235 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1237 for (auto mon
: monitors
)
1238 mon
->notify_connect(this, new_conn
);
1241 for (auto mon
: design
->monitors
)
1242 mon
->notify_connect(this, new_conn
);
1244 connections_
= new_conn
;
1247 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1249 return connections_
;
1252 void RTLIL::Module::fixup_ports()
1254 std::vector
<RTLIL::Wire
*> all_ports
;
1256 for (auto &w
: wires_
)
1257 if (w
.second
->port_input
|| w
.second
->port_output
)
1258 all_ports
.push_back(w
.second
);
1260 w
.second
->port_id
= 0;
1262 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1265 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1266 ports
.push_back(all_ports
[i
]->name
);
1267 all_ports
[i
]->port_id
= i
+1;
1271 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1273 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1275 wire
->width
= width
;
1280 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1282 RTLIL::Wire
*wire
= addWire(name
);
1283 wire
->width
= other
->width
;
1284 wire
->start_offset
= other
->start_offset
;
1285 wire
->port_id
= other
->port_id
;
1286 wire
->port_input
= other
->port_input
;
1287 wire
->port_output
= other
->port_output
;
1288 wire
->upto
= other
->upto
;
1289 wire
->attributes
= other
->attributes
;
1293 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1295 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1302 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1304 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1305 cell
->connections_
= other
->connections_
;
1306 cell
->parameters
= other
->parameters
;
1307 cell
->attributes
= other
->attributes
;
1311 #define DEF_METHOD(_func, _y_size, _type) \
1312 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1313 RTLIL::Cell *cell = addCell(name, _type); \
1314 cell->parameters["\\A_SIGNED"] = is_signed; \
1315 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1316 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1317 cell->setPort("\\A", sig_a); \
1318 cell->setPort("\\Y", sig_y); \
1321 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1322 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1323 add ## _func(name, sig_a, sig_y, is_signed); \
1326 DEF_METHOD(Not
, sig_a
.size(), "$not")
1327 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1328 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
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
)
1823 width
= data
.bits
.size();
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
);
1847 width
= data
.bits
.size();
1851 RTLIL::SigChunk::SigChunk(int val
, int width
)
1854 data
= RTLIL::Const(val
, width
);
1855 this->width
= data
.bits
.size();
1859 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1862 data
= RTLIL::Const(bit
, width
);
1863 this->width
= data
.bits
.size();
1867 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1872 data
= RTLIL::Const(bit
.data
);
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
.bits
.push_back(data
.bits
[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
.bits
< other
.data
.bits
;
1911 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1913 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1915 if (data
.bits
!= other
.data
.bits
)
1920 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1927 RTLIL::SigSpec::SigSpec()
1933 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1938 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1940 cover("kernel.rtlil.sigspec.init.list");
1945 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1946 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1950 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1952 cover("kernel.rtlil.sigspec.assign");
1954 width_
= other
.width_
;
1955 hash_
= other
.hash_
;
1956 chunks_
= other
.chunks_
;
1959 if (!other
.bits_
.empty())
1961 RTLIL::SigChunk
*last
= NULL
;
1962 int last_end_offset
= 0;
1964 for (auto &bit
: other
.bits_
) {
1965 if (last
&& bit
.wire
== last
->wire
) {
1966 if (bit
.wire
== NULL
) {
1967 last
->data
.bits
.push_back(bit
.data
);
1970 } else if (last_end_offset
== bit
.offset
) {
1976 chunks_
.push_back(bit
);
1977 last
= &chunks_
.back();
1978 last_end_offset
= bit
.offset
+ 1;
1987 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1989 cover("kernel.rtlil.sigspec.init.const");
1991 chunks_
.push_back(RTLIL::SigChunk(value
));
1992 width_
= chunks_
.back().width
;
1997 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1999 cover("kernel.rtlil.sigspec.init.chunk");
2001 chunks_
.push_back(chunk
);
2002 width_
= chunks_
.back().width
;
2007 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2009 cover("kernel.rtlil.sigspec.init.wire");
2011 chunks_
.push_back(RTLIL::SigChunk(wire
));
2012 width_
= chunks_
.back().width
;
2017 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2019 cover("kernel.rtlil.sigspec.init.wire_part");
2021 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2022 width_
= chunks_
.back().width
;
2027 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2029 cover("kernel.rtlil.sigspec.init.str");
2031 chunks_
.push_back(RTLIL::SigChunk(str
));
2032 width_
= chunks_
.back().width
;
2037 RTLIL::SigSpec::SigSpec(int val
, int width
)
2039 cover("kernel.rtlil.sigspec.init.int");
2041 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2047 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2049 cover("kernel.rtlil.sigspec.init.state");
2051 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2057 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2059 cover("kernel.rtlil.sigspec.init.bit");
2061 if (bit
.wire
== NULL
)
2062 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2064 for (int i
= 0; i
< width
; i
++)
2065 chunks_
.push_back(bit
);
2071 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2073 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2077 for (auto &c
: chunks
)
2082 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2084 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2088 for (auto &bit
: bits
)
2093 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2095 cover("kernel.rtlil.sigspec.init.stdset_bits");
2099 for (auto &bit
: bits
)
2104 void RTLIL::SigSpec::pack() const
2106 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2108 if (that
->bits_
.empty())
2111 cover("kernel.rtlil.sigspec.convert.pack");
2112 log_assert(that
->chunks_
.empty());
2114 std::vector
<RTLIL::SigBit
> old_bits
;
2115 old_bits
.swap(that
->bits_
);
2117 RTLIL::SigChunk
*last
= NULL
;
2118 int last_end_offset
= 0;
2120 for (auto &bit
: old_bits
) {
2121 if (last
&& bit
.wire
== last
->wire
) {
2122 if (bit
.wire
== NULL
) {
2123 last
->data
.bits
.push_back(bit
.data
);
2126 } else if (last_end_offset
== bit
.offset
) {
2132 that
->chunks_
.push_back(bit
);
2133 last
= &that
->chunks_
.back();
2134 last_end_offset
= bit
.offset
+ 1;
2140 void RTLIL::SigSpec::unpack() const
2142 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2144 if (that
->chunks_
.empty())
2147 cover("kernel.rtlil.sigspec.convert.unpack");
2148 log_assert(that
->bits_
.empty());
2150 that
->bits_
.reserve(that
->width_
);
2151 for (auto &c
: that
->chunks_
)
2152 for (int i
= 0; i
< c
.width
; i
++)
2153 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2155 that
->chunks_
.clear();
2159 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2161 void RTLIL::SigSpec::hash() const
2163 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2165 if (that
->hash_
!= 0)
2168 cover("kernel.rtlil.sigspec.hash");
2172 for (auto &c
: that
->chunks_
)
2173 if (c
.wire
== NULL
) {
2174 for (auto &v
: c
.data
.bits
)
2175 DJB2(that
->hash_
, v
);
2177 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2178 DJB2(that
->hash_
, c
.offset
);
2179 DJB2(that
->hash_
, c
.width
);
2182 if (that
->hash_
== 0)
2186 void RTLIL::SigSpec::sort()
2189 cover("kernel.rtlil.sigspec.sort");
2190 std::sort(bits_
.begin(), bits_
.end());
2193 void RTLIL::SigSpec::sort_and_unify()
2195 cover("kernel.rtlil.sigspec.sort_and_unify");
2196 *this = this->to_sigbit_set();
2199 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2201 replace(pattern
, with
, this);
2204 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2206 log_assert(pattern
.width_
== with
.width_
);
2211 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2213 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2214 if (pattern
.bits_
[i
].wire
!= NULL
)
2215 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2217 replace(rules
, other
);
2220 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2222 replace(rules
, this);
2225 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2227 cover("kernel.rtlil.sigspec.replace");
2229 log_assert(other
!= NULL
);
2230 log_assert(width_
== other
->width_
);
2235 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2236 auto it
= rules
.find(bits_
[i
]);
2237 if (it
!= rules
.end())
2238 other
->bits_
[i
] = it
->second
;
2244 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2246 remove2(pattern
, NULL
);
2249 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2251 RTLIL::SigSpec tmp
= *this;
2252 tmp
.remove2(pattern
, other
);
2255 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2257 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2258 remove2(pattern_bits
, other
);
2261 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2263 remove2(pattern
, NULL
);
2266 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2268 RTLIL::SigSpec tmp
= *this;
2269 tmp
.remove2(pattern
, other
);
2272 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2275 cover("kernel.rtlil.sigspec.remove_other");
2277 cover("kernel.rtlil.sigspec.remove");
2281 if (other
!= NULL
) {
2282 log_assert(width_
== other
->width_
);
2286 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2288 new_bits
.resize(SIZE(bits_
));
2290 new_other_bits
.resize(SIZE(bits_
));
2293 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2294 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2297 new_other_bits
[k
] = other
->bits_
[i
];
2298 new_bits
[k
++] = bits_
[i
];
2303 new_other_bits
.resize(k
);
2305 bits_
.swap(new_bits
);
2306 width_
= SIZE(bits_
);
2308 if (other
!= NULL
) {
2309 other
->bits_
.swap(new_other_bits
);
2310 other
->width_
= SIZE(other
->bits_
);
2316 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2318 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2319 return extract(pattern_bits
, other
);
2322 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2325 cover("kernel.rtlil.sigspec.extract_other");
2327 cover("kernel.rtlil.sigspec.extract");
2329 log_assert(other
== NULL
|| width_
== other
->width_
);
2331 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2335 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2336 for (int i
= 0; i
< width_
; i
++)
2337 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2338 ret
.append_bit(bits_other
[i
]);
2340 for (int i
= 0; i
< width_
; i
++)
2341 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2342 ret
.append_bit(bits_match
[i
]);
2349 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2351 cover("kernel.rtlil.sigspec.replace_pos");
2356 log_assert(offset
>= 0);
2357 log_assert(with
.width_
>= 0);
2358 log_assert(offset
+with
.width_
<= width_
);
2360 for (int i
= 0; i
< with
.width_
; i
++)
2361 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2366 void RTLIL::SigSpec::remove_const()
2370 cover("kernel.rtlil.sigspec.remove_const.packed");
2372 std::vector
<RTLIL::SigChunk
> new_chunks
;
2373 new_chunks
.reserve(SIZE(chunks_
));
2376 for (auto &chunk
: chunks_
)
2377 if (chunk
.wire
!= NULL
) {
2378 new_chunks
.push_back(chunk
);
2379 width_
+= chunk
.width
;
2382 chunks_
.swap(new_chunks
);
2386 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2388 std::vector
<RTLIL::SigBit
> new_bits
;
2389 new_bits
.reserve(width_
);
2391 for (auto &bit
: bits_
)
2392 if (bit
.wire
!= NULL
)
2393 new_bits
.push_back(bit
);
2395 bits_
.swap(new_bits
);
2396 width_
= bits_
.size();
2402 void RTLIL::SigSpec::remove(int offset
, int length
)
2404 cover("kernel.rtlil.sigspec.remove_pos");
2408 log_assert(offset
>= 0);
2409 log_assert(length
>= 0);
2410 log_assert(offset
+ length
<= width_
);
2412 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2413 width_
= bits_
.size();
2418 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2421 cover("kernel.rtlil.sigspec.extract_pos");
2422 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2425 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2427 if (signal
.width_
== 0)
2435 cover("kernel.rtlil.sigspec.append");
2437 if (packed() != signal
.packed()) {
2443 for (auto &other_c
: signal
.chunks_
)
2445 auto &my_last_c
= chunks_
.back();
2446 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2447 auto &this_data
= my_last_c
.data
.bits
;
2448 auto &other_data
= other_c
.data
.bits
;
2449 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2450 my_last_c
.width
+= other_c
.width
;
2452 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2453 my_last_c
.width
+= other_c
.width
;
2455 chunks_
.push_back(other_c
);
2458 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2460 width_
+= signal
.width_
;
2464 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2468 cover("kernel.rtlil.sigspec.append_bit.packed");
2470 if (chunks_
.size() == 0)
2471 chunks_
.push_back(bit
);
2473 if (bit
.wire
== NULL
)
2474 if (chunks_
.back().wire
== NULL
) {
2475 chunks_
.back().data
.bits
.push_back(bit
.data
);
2476 chunks_
.back().width
++;
2478 chunks_
.push_back(bit
);
2480 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2481 chunks_
.back().width
++;
2483 chunks_
.push_back(bit
);
2487 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2488 bits_
.push_back(bit
);
2495 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2497 cover("kernel.rtlil.sigspec.extend");
2502 remove(width
, width_
- width
);
2504 if (width_
< width
) {
2505 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2506 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2507 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2508 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2509 while (width_
< width
)
2514 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2516 cover("kernel.rtlil.sigspec.extend_u0");
2521 remove(width
, width_
- width
);
2523 if (width_
< width
) {
2524 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2526 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2527 while (width_
< width
)
2533 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2535 cover("kernel.rtlil.sigspec.repeat");
2538 for (int i
= 0; i
< num
; i
++)
2544 void RTLIL::SigSpec::check() const
2548 cover("kernel.rtlil.sigspec.check.skip");
2552 cover("kernel.rtlil.sigspec.check.packed");
2555 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2556 const RTLIL::SigChunk chunk
= chunks_
[i
];
2557 if (chunk
.wire
== NULL
) {
2559 log_assert(chunks_
[i
-1].wire
!= NULL
);
2560 log_assert(chunk
.offset
== 0);
2561 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2563 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2564 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2565 log_assert(chunk
.offset
>= 0);
2566 log_assert(chunk
.width
>= 0);
2567 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2568 log_assert(chunk
.data
.bits
.size() == 0);
2572 log_assert(w
== width_
);
2573 log_assert(bits_
.empty());
2577 cover("kernel.rtlil.sigspec.check.unpacked");
2579 log_assert(width_
== SIZE(bits_
));
2580 log_assert(chunks_
.empty());
2585 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2587 cover("kernel.rtlil.sigspec.comp_lt");
2592 if (width_
!= other
.width_
)
2593 return width_
< other
.width_
;
2598 if (chunks_
.size() != other
.chunks_
.size())
2599 return chunks_
.size() < other
.chunks_
.size();
2604 if (hash_
!= other
.hash_
)
2605 return hash_
< other
.hash_
;
2607 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2608 if (chunks_
[i
] != other
.chunks_
[i
]) {
2609 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2610 return chunks_
[i
] < other
.chunks_
[i
];
2613 cover("kernel.rtlil.sigspec.comp_lt.equal");
2617 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2619 cover("kernel.rtlil.sigspec.comp_eq");
2624 if (width_
!= other
.width_
)
2630 if (chunks_
.size() != chunks_
.size())
2636 if (hash_
!= other
.hash_
)
2639 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2640 if (chunks_
[i
] != other
.chunks_
[i
]) {
2641 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2645 cover("kernel.rtlil.sigspec.comp_eq.equal");
2649 bool RTLIL::SigSpec::is_wire() const
2651 cover("kernel.rtlil.sigspec.is_wire");
2654 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2657 bool RTLIL::SigSpec::is_chunk() const
2659 cover("kernel.rtlil.sigspec.is_chunk");
2662 return SIZE(chunks_
) == 1;
2665 bool RTLIL::SigSpec::is_fully_const() const
2667 cover("kernel.rtlil.sigspec.is_fully_const");
2670 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2671 if (it
->width
> 0 && it
->wire
!= NULL
)
2676 bool RTLIL::SigSpec::is_fully_def() const
2678 cover("kernel.rtlil.sigspec.is_fully_def");
2681 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2682 if (it
->width
> 0 && it
->wire
!= NULL
)
2684 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2685 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2691 bool RTLIL::SigSpec::is_fully_undef() const
2693 cover("kernel.rtlil.sigspec.is_fully_undef");
2696 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2697 if (it
->width
> 0 && it
->wire
!= NULL
)
2699 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2700 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2706 bool RTLIL::SigSpec::has_marked_bits() const
2708 cover("kernel.rtlil.sigspec.has_marked_bits");
2711 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2712 if (it
->width
> 0 && it
->wire
== NULL
) {
2713 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2714 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2720 bool RTLIL::SigSpec::as_bool() const
2722 cover("kernel.rtlil.sigspec.as_bool");
2725 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2727 return chunks_
[0].data
.as_bool();
2731 int RTLIL::SigSpec::as_int(bool is_signed
) const
2733 cover("kernel.rtlil.sigspec.as_int");
2736 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2738 return chunks_
[0].data
.as_int(is_signed
);
2742 std::string
RTLIL::SigSpec::as_string() const
2744 cover("kernel.rtlil.sigspec.as_string");
2748 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2749 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2750 if (chunk
.wire
!= NULL
)
2751 for (int j
= 0; j
< chunk
.width
; j
++)
2754 str
+= chunk
.data
.as_string();
2759 RTLIL::Const
RTLIL::SigSpec::as_const() const
2761 cover("kernel.rtlil.sigspec.as_const");
2764 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2766 return chunks_
[0].data
;
2767 return RTLIL::Const();
2770 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2772 cover("kernel.rtlil.sigspec.as_wire");
2775 log_assert(is_wire());
2776 return chunks_
[0].wire
;
2779 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2781 cover("kernel.rtlil.sigspec.as_chunk");
2784 log_assert(is_chunk());
2788 bool RTLIL::SigSpec::match(std::string pattern
) const
2790 cover("kernel.rtlil.sigspec.match");
2793 std::string str
= as_string();
2794 log_assert(pattern
.size() == str
.size());
2796 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2797 if (pattern
[i
] == ' ')
2799 if (pattern
[i
] == '*') {
2800 if (str
[i
] != 'z' && str
[i
] != 'x')
2804 if (pattern
[i
] != str
[i
])
2811 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2813 cover("kernel.rtlil.sigspec.to_sigbit_set");
2816 std::set
<RTLIL::SigBit
> sigbits
;
2817 for (auto &c
: chunks_
)
2818 for (int i
= 0; i
< c
.width
; i
++)
2819 sigbits
.insert(RTLIL::SigBit(c
, i
));
2823 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2825 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2831 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2833 cover("kernel.rtlil.sigspec.to_sigbit_map");
2838 log_assert(width_
== other
.width_
);
2840 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2841 for (int i
= 0; i
< width_
; i
++)
2842 new_map
[bits_
[i
]] = other
.bits_
[i
];
2847 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2849 cover("kernel.rtlil.sigspec.to_single_sigbit");
2852 log_assert(width_
== 1);
2853 for (auto &c
: chunks_
)
2855 return RTLIL::SigBit(c
);
2859 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2861 size_t start
= 0, end
= 0;
2862 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2863 tokens
.push_back(text
.substr(start
, end
- start
));
2866 tokens
.push_back(text
.substr(start
));
2869 static int sigspec_parse_get_dummy_line_num()
2874 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2876 cover("kernel.rtlil.sigspec.parse");
2878 std::vector
<std::string
> tokens
;
2879 sigspec_parse_split(tokens
, str
, ',');
2881 sig
= RTLIL::SigSpec();
2882 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2884 std::string netname
= tokens
[tokidx
];
2885 std::string indices
;
2887 if (netname
.size() == 0)
2890 if ('0' <= netname
[0] && netname
[0] <= '9') {
2891 cover("kernel.rtlil.sigspec.parse.const");
2892 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2893 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2896 sig
.append(RTLIL::Const(ast
->bits
));
2904 cover("kernel.rtlil.sigspec.parse.net");
2906 if (netname
[0] != '$' && netname
[0] != '\\')
2907 netname
= "\\" + netname
;
2909 if (module
->wires_
.count(netname
) == 0) {
2910 size_t indices_pos
= netname
.size()-1;
2911 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2914 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2915 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2917 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2919 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2920 indices
= netname
.substr(indices_pos
);
2921 netname
= netname
.substr(0, indices_pos
);
2926 if (module
->wires_
.count(netname
) == 0)
2929 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2930 if (!indices
.empty()) {
2931 std::vector
<std::string
> index_tokens
;
2932 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2933 if (index_tokens
.size() == 1) {
2934 cover("kernel.rtlil.sigspec.parse.bit_sel");
2935 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2937 cover("kernel.rtlil.sigspec.parse.part_sel");
2938 int a
= atoi(index_tokens
.at(0).c_str());
2939 int b
= atoi(index_tokens
.at(1).c_str());
2944 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2953 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2955 if (str
.empty() || str
[0] != '@')
2956 return parse(sig
, module
, str
);
2958 cover("kernel.rtlil.sigspec.parse.sel");
2960 str
= RTLIL::escape_id(str
.substr(1));
2961 if (design
->selection_vars
.count(str
) == 0)
2964 sig
= RTLIL::SigSpec();
2965 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2966 for (auto &it
: module
->wires_
)
2967 if (sel
.selected_member(module
->name
, it
.first
))
2968 sig
.append(it
.second
);
2973 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2976 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2977 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2982 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2983 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2987 if (lhs
.chunks_
.size() == 1) {
2988 char *p
= (char*)str
.c_str(), *endptr
;
2989 long long int val
= strtoll(p
, &endptr
, 10);
2990 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2991 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2992 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2997 return parse(sig
, module
, str
);
3000 RTLIL::CaseRule::~CaseRule()
3002 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3006 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3008 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3009 new_caserule
->compare
= compare
;
3010 new_caserule
->actions
= actions
;
3011 for (auto &it
: switches
)
3012 new_caserule
->switches
.push_back(it
->clone());
3013 return new_caserule
;
3016 RTLIL::SwitchRule::~SwitchRule()
3018 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3022 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3024 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3025 new_switchrule
->signal
= signal
;
3026 new_switchrule
->attributes
= attributes
;
3027 for (auto &it
: cases
)
3028 new_switchrule
->cases
.push_back(it
->clone());
3029 return new_switchrule
;
3033 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3035 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3036 new_syncrule
->type
= type
;
3037 new_syncrule
->signal
= signal
;
3038 new_syncrule
->actions
= actions
;
3039 return new_syncrule
;
3042 RTLIL::Process::~Process()
3044 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3048 RTLIL::Process
*RTLIL::Process::clone() const
3050 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3052 new_proc
->name
= name
;
3053 new_proc
->attributes
= attributes
;
3055 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3056 new_proc
->root_case
= *rc_ptr
;
3057 rc_ptr
->switches
.clear();
3060 for (auto &it
: syncs
)
3061 new_proc
->syncs
.push_back(it
->clone());