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 "kernel/macc.h"
22 #include "frontends/verilog/verilog_frontend.h"
23 #include "backends/ilang/ilang_backend.h"
30 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
31 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
32 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
33 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
34 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
38 flags
= RTLIL::CONST_FLAG_NONE
;
41 RTLIL::Const::Const(std::string str
)
43 flags
= RTLIL::CONST_FLAG_STRING
;
44 for (int i
= str
.size()-1; i
>= 0; i
--) {
45 unsigned char ch
= str
[i
];
46 for (int j
= 0; j
< 8; j
++) {
47 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
53 RTLIL::Const::Const(int val
, int width
)
55 flags
= RTLIL::CONST_FLAG_NONE
;
56 for (int i
= 0; i
< width
; i
++) {
57 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
62 RTLIL::Const::Const(RTLIL::State bit
, int width
)
64 flags
= RTLIL::CONST_FLAG_NONE
;
65 for (int i
= 0; i
< width
; i
++)
69 RTLIL::Const::Const(const std::vector
<bool> &bits
)
71 flags
= RTLIL::CONST_FLAG_NONE
;
73 this->bits
.push_back(b
? RTLIL::S1
: RTLIL::S0
);
76 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
78 if (bits
.size() != other
.bits
.size())
79 return bits
.size() < other
.bits
.size();
80 for (size_t i
= 0; i
< bits
.size(); i
++)
81 if (bits
[i
] != other
.bits
[i
])
82 return bits
[i
] < other
.bits
[i
];
86 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
88 return bits
== other
.bits
;
91 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
93 return bits
!= other
.bits
;
96 bool RTLIL::Const::as_bool() const
98 for (size_t i
= 0; i
< bits
.size(); i
++)
99 if (bits
[i
] == RTLIL::S1
)
104 int RTLIL::Const::as_int(bool is_signed
) const
107 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
108 if (bits
[i
] == RTLIL::S1
)
110 if (is_signed
&& bits
.back() == RTLIL::S1
)
111 for (size_t i
= bits
.size(); i
< 32; i
++)
116 std::string
RTLIL::Const::as_string() const
119 for (size_t i
= bits
.size(); i
> 0; i
--)
121 case S0
: ret
+= "0"; break;
122 case S1
: ret
+= "1"; break;
123 case Sx
: ret
+= "x"; break;
124 case Sz
: ret
+= "z"; break;
125 case Sa
: ret
+= "-"; break;
126 case Sm
: ret
+= "m"; break;
131 std::string
RTLIL::Const::decode_string() const
134 std::vector
<char> string_chars
;
135 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
137 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
138 if (bits
[i
+ j
] == RTLIL::State::S1
)
141 string_chars
.push_back(ch
);
143 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
144 string
+= string_chars
[i
];
148 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
152 if (selected_modules
.count(mod_name
) > 0)
154 if (selected_members
.count(mod_name
) > 0)
159 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
163 if (selected_modules
.count(mod_name
) > 0)
168 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
172 if (selected_modules
.count(mod_name
) > 0)
174 if (selected_members
.count(mod_name
) > 0)
175 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
180 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
182 if (full_selection
) {
183 selected_modules
.clear();
184 selected_members
.clear();
188 std::vector
<RTLIL::IdString
> del_list
, add_list
;
191 for (auto mod_name
: selected_modules
) {
192 if (design
->modules_
.count(mod_name
) == 0)
193 del_list
.push_back(mod_name
);
194 selected_members
.erase(mod_name
);
196 for (auto mod_name
: del_list
)
197 selected_modules
.erase(mod_name
);
200 for (auto &it
: selected_members
)
201 if (design
->modules_
.count(it
.first
) == 0)
202 del_list
.push_back(it
.first
);
203 for (auto mod_name
: del_list
)
204 selected_members
.erase(mod_name
);
206 for (auto &it
: selected_members
) {
208 for (auto memb_name
: it
.second
)
209 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
210 del_list
.push_back(memb_name
);
211 for (auto memb_name
: del_list
)
212 it
.second
.erase(memb_name
);
217 for (auto &it
: selected_members
)
218 if (it
.second
.size() == 0)
219 del_list
.push_back(it
.first
);
220 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
221 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
222 add_list
.push_back(it
.first
);
223 for (auto mod_name
: del_list
)
224 selected_members
.erase(mod_name
);
225 for (auto mod_name
: add_list
) {
226 selected_members
.erase(mod_name
);
227 selected_modules
.insert(mod_name
);
230 if (selected_modules
.size() == design
->modules_
.size()) {
231 full_selection
= true;
232 selected_modules
.clear();
233 selected_members
.clear();
237 RTLIL::Design::Design()
239 static unsigned int hashidx_count
= 123456789;
240 hashidx_count
= mkhash_xorshift(hashidx_count
);
241 hashidx_
= hashidx_count
;
243 refcount_modules_
= 0;
244 selection_stack
.push_back(RTLIL::Selection());
247 RTLIL::Design::~Design()
249 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
253 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
255 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
258 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
260 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
263 void RTLIL::Design::add(RTLIL::Module
*module
)
265 log_assert(modules_
.count(module
->name
) == 0);
266 log_assert(refcount_modules_
== 0);
267 modules_
[module
->name
] = module
;
268 module
->design
= this;
270 for (auto mon
: monitors
)
271 mon
->notify_module_add(module
);
274 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
276 log_assert(modules_
.count(name
) == 0);
277 log_assert(refcount_modules_
== 0);
279 RTLIL::Module
*module
= new RTLIL::Module
;
280 modules_
[name
] = module
;
281 module
->design
= this;
284 for (auto mon
: monitors
)
285 mon
->notify_module_add(module
);
290 void RTLIL::Design::scratchpad_unset(std::string varname
)
292 scratchpad
.erase(varname
);
295 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
297 scratchpad
[varname
] = stringf("%d", value
);
300 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
302 scratchpad
[varname
] = value
? "true" : "false";
305 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
307 scratchpad
[varname
] = value
;
310 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
312 if (scratchpad
.count(varname
) == 0)
313 return default_value
;
315 std::string str
= scratchpad
.at(varname
);
317 if (str
== "0" || str
== "false")
320 if (str
== "1" || str
== "true")
323 char *endptr
= nullptr;
324 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
325 return *endptr
? default_value
: parsed_value
;
328 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
330 if (scratchpad
.count(varname
) == 0)
331 return default_value
;
333 std::string str
= scratchpad
.at(varname
);
335 if (str
== "0" || str
== "false")
338 if (str
== "1" || str
== "true")
341 return default_value
;
344 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
346 if (scratchpad
.count(varname
) == 0)
347 return default_value
;
348 return scratchpad
.at(varname
);
351 void RTLIL::Design::remove(RTLIL::Module
*module
)
353 for (auto mon
: monitors
)
354 mon
->notify_module_del(module
);
356 log_assert(modules_
.at(module
->name
) == module
);
357 modules_
.erase(module
->name
);
361 void RTLIL::Design::check()
364 for (auto &it
: modules_
) {
365 log_assert(this == it
.second
->design
);
366 log_assert(it
.first
== it
.second
->name
);
367 log_assert(!it
.first
.empty());
373 void RTLIL::Design::optimize()
375 for (auto &it
: modules_
)
376 it
.second
->optimize();
377 for (auto &it
: selection_stack
)
379 for (auto &it
: selection_vars
)
380 it
.second
.optimize(this);
383 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
385 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
387 if (selection_stack
.size() == 0)
389 return selection_stack
.back().selected_module(mod_name
);
392 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
394 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
396 if (selection_stack
.size() == 0)
398 return selection_stack
.back().selected_whole_module(mod_name
);
401 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
403 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
405 if (selection_stack
.size() == 0)
407 return selection_stack
.back().selected_member(mod_name
, memb_name
);
410 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
412 return selected_module(mod
->name
);
415 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
417 return selected_whole_module(mod
->name
);
420 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
422 std::vector
<RTLIL::Module
*> result
;
423 result
.reserve(modules_
.size());
424 for (auto &it
: modules_
)
425 if (selected_module(it
.first
))
426 result
.push_back(it
.second
);
430 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
432 std::vector
<RTLIL::Module
*> result
;
433 result
.reserve(modules_
.size());
434 for (auto &it
: modules_
)
435 if (selected_whole_module(it
.first
))
436 result
.push_back(it
.second
);
440 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
442 std::vector
<RTLIL::Module
*> result
;
443 result
.reserve(modules_
.size());
444 for (auto &it
: modules_
)
445 if (selected_whole_module(it
.first
))
446 result
.push_back(it
.second
);
447 else if (selected_module(it
.first
))
448 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
452 RTLIL::Module::Module()
454 static unsigned int hashidx_count
= 123456789;
455 hashidx_count
= mkhash_xorshift(hashidx_count
);
456 hashidx_
= hashidx_count
;
463 RTLIL::Module::~Module()
465 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
467 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
469 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
471 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
475 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>)
477 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
480 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
482 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
487 struct InternalCellChecker
489 RTLIL::Module
*module
;
491 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
493 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
495 void error(int linenr
)
497 std::stringstream buf
;
498 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
500 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
501 module
? module
->name
.c_str() : "", module
? "." : "",
502 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
505 int param(const char *name
)
507 if (cell
->parameters
.count(name
) == 0)
509 expected_params
.insert(name
);
510 return cell
->parameters
.at(name
).as_int();
513 int param_bool(const char *name
)
516 if (cell
->parameters
.at(name
).bits
.size() > 32)
518 if (v
!= 0 && v
!= 1)
523 void param_bits(const char *name
, int width
)
526 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
530 void port(const char *name
, int width
)
532 if (!cell
->hasPort(name
))
534 if (cell
->getPort(name
).size() != width
)
536 expected_ports
.insert(name
);
539 void check_expected(bool check_matched_sign
= true)
541 for (auto ¶
: cell
->parameters
)
542 if (expected_params
.count(para
.first
) == 0)
544 for (auto &conn
: cell
->connections())
545 if (expected_ports
.count(conn
.first
) == 0)
548 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
549 bool a_is_signed
= param("\\A_SIGNED") != 0;
550 bool b_is_signed
= param("\\B_SIGNED") != 0;
551 if (a_is_signed
!= b_is_signed
)
556 void check_gate(const char *ports
)
558 if (cell
->parameters
.size() != 0)
561 for (const char *p
= ports
; *p
; p
++) {
562 char portname
[3] = { '\\', *p
, 0 };
563 if (!cell
->hasPort(portname
))
565 if (cell
->getPort(portname
).size() != 1)
569 for (auto &conn
: cell
->connections()) {
570 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
572 if (strchr(ports
, conn
.first
[1]) == NULL
)
579 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
580 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
583 if (cell
->type
.in("$not", "$pos", "$neg")) {
584 param_bool("\\A_SIGNED");
585 port("\\A", param("\\A_WIDTH"));
586 port("\\Y", param("\\Y_WIDTH"));
591 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
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"));
601 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
602 param_bool("\\A_SIGNED");
603 port("\\A", param("\\A_WIDTH"));
604 port("\\Y", param("\\Y_WIDTH"));
609 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
610 param_bool("\\A_SIGNED");
611 param_bool("\\B_SIGNED");
612 port("\\A", param("\\A_WIDTH"));
613 port("\\B", param("\\B_WIDTH"));
614 port("\\Y", param("\\Y_WIDTH"));
615 check_expected(false);
619 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
620 param_bool("\\A_SIGNED");
621 param_bool("\\B_SIGNED");
622 port("\\A", param("\\A_WIDTH"));
623 port("\\B", param("\\B_WIDTH"));
624 port("\\Y", param("\\Y_WIDTH"));
629 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
630 param_bool("\\A_SIGNED");
631 param_bool("\\B_SIGNED");
632 port("\\A", param("\\A_WIDTH"));
633 port("\\B", param("\\B_WIDTH"));
634 port("\\Y", param("\\Y_WIDTH"));
635 check_expected(cell
->type
!= "$pow");
639 if (cell
->type
== "$fa") {
640 port("\\A", param("\\WIDTH"));
641 port("\\B", param("\\WIDTH"));
642 port("\\C", param("\\WIDTH"));
643 port("\\X", param("\\WIDTH"));
644 port("\\Y", param("\\WIDTH"));
649 if (cell
->type
== "$lcu") {
650 port("\\P", param("\\WIDTH"));
651 port("\\G", param("\\WIDTH"));
653 port("\\CO", param("\\WIDTH"));
658 if (cell
->type
== "$alu") {
659 param_bool("\\A_SIGNED");
660 param_bool("\\B_SIGNED");
661 port("\\A", param("\\A_WIDTH"));
662 port("\\B", param("\\B_WIDTH"));
665 port("\\X", param("\\Y_WIDTH"));
666 port("\\Y", param("\\Y_WIDTH"));
667 port("\\CO", param("\\Y_WIDTH"));
672 if (cell
->type
== "$macc") {
674 param("\\CONFIG_WIDTH");
675 port("\\A", param("\\A_WIDTH"));
676 port("\\B", param("\\B_WIDTH"));
677 port("\\Y", param("\\Y_WIDTH"));
679 Macc().from_cell(cell
);
683 if (cell
->type
== "$logic_not") {
684 param_bool("\\A_SIGNED");
685 port("\\A", param("\\A_WIDTH"));
686 port("\\Y", param("\\Y_WIDTH"));
691 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
692 param_bool("\\A_SIGNED");
693 param_bool("\\B_SIGNED");
694 port("\\A", param("\\A_WIDTH"));
695 port("\\B", param("\\B_WIDTH"));
696 port("\\Y", param("\\Y_WIDTH"));
697 check_expected(false);
701 if (cell
->type
== "$slice") {
703 port("\\A", param("\\A_WIDTH"));
704 port("\\Y", param("\\Y_WIDTH"));
705 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
711 if (cell
->type
== "$concat") {
712 port("\\A", param("\\A_WIDTH"));
713 port("\\B", param("\\B_WIDTH"));
714 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
719 if (cell
->type
== "$mux") {
720 port("\\A", param("\\WIDTH"));
721 port("\\B", param("\\WIDTH"));
723 port("\\Y", param("\\WIDTH"));
728 if (cell
->type
== "$pmux") {
729 port("\\A", param("\\WIDTH"));
730 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
731 port("\\S", param("\\S_WIDTH"));
732 port("\\Y", param("\\WIDTH"));
737 if (cell
->type
== "$lut") {
739 port("\\A", param("\\WIDTH"));
745 if (cell
->type
== "$sr") {
746 param_bool("\\SET_POLARITY");
747 param_bool("\\CLR_POLARITY");
748 port("\\SET", param("\\WIDTH"));
749 port("\\CLR", param("\\WIDTH"));
750 port("\\Q", param("\\WIDTH"));
755 if (cell
->type
== "$dff") {
756 param_bool("\\CLK_POLARITY");
758 port("\\D", param("\\WIDTH"));
759 port("\\Q", param("\\WIDTH"));
764 if (cell
->type
== "$dffe") {
765 param_bool("\\CLK_POLARITY");
766 param_bool("\\EN_POLARITY");
769 port("\\D", param("\\WIDTH"));
770 port("\\Q", param("\\WIDTH"));
775 if (cell
->type
== "$dffsr") {
776 param_bool("\\CLK_POLARITY");
777 param_bool("\\SET_POLARITY");
778 param_bool("\\CLR_POLARITY");
780 port("\\SET", param("\\WIDTH"));
781 port("\\CLR", param("\\WIDTH"));
782 port("\\D", param("\\WIDTH"));
783 port("\\Q", param("\\WIDTH"));
788 if (cell
->type
== "$adff") {
789 param_bool("\\CLK_POLARITY");
790 param_bool("\\ARST_POLARITY");
791 param_bits("\\ARST_VALUE", param("\\WIDTH"));
794 port("\\D", param("\\WIDTH"));
795 port("\\Q", param("\\WIDTH"));
800 if (cell
->type
== "$dlatch") {
801 param_bool("\\EN_POLARITY");
803 port("\\D", param("\\WIDTH"));
804 port("\\Q", param("\\WIDTH"));
809 if (cell
->type
== "$dlatchsr") {
810 param_bool("\\EN_POLARITY");
811 param_bool("\\SET_POLARITY");
812 param_bool("\\CLR_POLARITY");
814 port("\\SET", param("\\WIDTH"));
815 port("\\CLR", param("\\WIDTH"));
816 port("\\D", param("\\WIDTH"));
817 port("\\Q", param("\\WIDTH"));
822 if (cell
->type
== "$fsm") {
824 param_bool("\\CLK_POLARITY");
825 param_bool("\\ARST_POLARITY");
826 param("\\STATE_BITS");
827 param("\\STATE_NUM");
828 param("\\STATE_NUM_LOG2");
829 param("\\STATE_RST");
830 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
831 param("\\TRANS_NUM");
832 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
835 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
836 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
841 if (cell
->type
== "$memrd") {
843 param_bool("\\CLK_ENABLE");
844 param_bool("\\CLK_POLARITY");
845 param_bool("\\TRANSPARENT");
847 port("\\ADDR", param("\\ABITS"));
848 port("\\DATA", param("\\WIDTH"));
853 if (cell
->type
== "$memwr") {
855 param_bool("\\CLK_ENABLE");
856 param_bool("\\CLK_POLARITY");
859 port("\\EN", param("\\WIDTH"));
860 port("\\ADDR", param("\\ABITS"));
861 port("\\DATA", param("\\WIDTH"));
866 if (cell
->type
== "$mem") {
870 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
871 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
872 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
873 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
874 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
875 port("\\RD_CLK", param("\\RD_PORTS"));
876 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
877 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
878 port("\\WR_CLK", param("\\WR_PORTS"));
879 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
880 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
881 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
886 if (cell
->type
== "$assert") {
893 if (cell
->type
== "$_BUF_") { check_gate("AY"); return; }
894 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
895 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
896 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
897 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
898 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
899 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
900 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
901 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
902 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
903 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
904 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
905 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
907 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
908 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
909 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
910 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
912 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
913 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
915 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
916 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
917 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
918 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
920 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
921 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
922 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
923 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
924 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
925 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
926 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
927 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
929 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
930 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
931 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
932 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
933 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
934 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
935 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
936 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
938 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
939 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
941 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
942 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
943 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
944 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
945 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
946 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
947 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
948 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
956 void RTLIL::Module::check()
959 std::vector
<bool> ports_declared
;
960 for (auto &it
: wires_
) {
961 log_assert(this == it
.second
->module
);
962 log_assert(it
.first
== it
.second
->name
);
963 log_assert(!it
.first
.empty());
964 log_assert(it
.second
->width
>= 0);
965 log_assert(it
.second
->port_id
>= 0);
966 for (auto &it2
: it
.second
->attributes
)
967 log_assert(!it2
.first
.empty());
968 if (it
.second
->port_id
) {
969 log_assert(GetSize(ports
) >= it
.second
->port_id
);
970 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
971 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
972 if (GetSize(ports_declared
) < it
.second
->port_id
)
973 ports_declared
.resize(it
.second
->port_id
);
974 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
975 ports_declared
[it
.second
->port_id
-1] = true;
977 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
979 for (auto port_declared
: ports_declared
)
980 log_assert(port_declared
== true);
981 log_assert(GetSize(ports
) == GetSize(ports_declared
));
983 for (auto &it
: memories
) {
984 log_assert(it
.first
== it
.second
->name
);
985 log_assert(!it
.first
.empty());
986 log_assert(it
.second
->width
>= 0);
987 log_assert(it
.second
->size
>= 0);
988 for (auto &it2
: it
.second
->attributes
)
989 log_assert(!it2
.first
.empty());
992 for (auto &it
: cells_
) {
993 log_assert(this == it
.second
->module
);
994 log_assert(it
.first
== it
.second
->name
);
995 log_assert(!it
.first
.empty());
996 log_assert(!it
.second
->type
.empty());
997 for (auto &it2
: it
.second
->connections()) {
998 log_assert(!it2
.first
.empty());
1001 for (auto &it2
: it
.second
->attributes
)
1002 log_assert(!it2
.first
.empty());
1003 for (auto &it2
: it
.second
->parameters
)
1004 log_assert(!it2
.first
.empty());
1005 InternalCellChecker
checker(this, it
.second
);
1009 for (auto &it
: processes
) {
1010 log_assert(it
.first
== it
.second
->name
);
1011 log_assert(!it
.first
.empty());
1012 // FIXME: More checks here..
1015 for (auto &it
: connections_
) {
1016 log_assert(it
.first
.size() == it
.second
.size());
1021 for (auto &it
: attributes
)
1022 log_assert(!it
.first
.empty());
1026 void RTLIL::Module::optimize()
1030 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1032 log_assert(new_mod
->refcount_wires_
== 0);
1033 log_assert(new_mod
->refcount_cells_
== 0);
1035 new_mod
->connections_
= connections_
;
1036 new_mod
->attributes
= attributes
;
1038 for (auto &it
: wires_
)
1039 new_mod
->addWire(it
.first
, it
.second
);
1041 for (auto &it
: memories
)
1042 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1044 for (auto &it
: cells_
)
1045 new_mod
->addCell(it
.first
, it
.second
);
1047 for (auto &it
: processes
)
1048 new_mod
->processes
[it
.first
] = it
.second
->clone();
1050 struct RewriteSigSpecWorker
1053 void operator()(RTLIL::SigSpec
&sig
)
1055 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1056 for (auto &c
: chunks
)
1058 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1063 RewriteSigSpecWorker rewriteSigSpecWorker
;
1064 rewriteSigSpecWorker
.mod
= new_mod
;
1065 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1066 new_mod
->fixup_ports();
1069 RTLIL::Module
*RTLIL::Module::clone() const
1071 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1072 new_mod
->name
= name
;
1077 bool RTLIL::Module::has_memories() const
1079 return !memories
.empty();
1082 bool RTLIL::Module::has_processes() const
1084 return !processes
.empty();
1087 bool RTLIL::Module::has_memories_warn() const
1089 if (!memories
.empty())
1090 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1091 return !memories
.empty();
1094 bool RTLIL::Module::has_processes_warn() const
1096 if (!processes
.empty())
1097 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1098 return !processes
.empty();
1101 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1103 std::vector
<RTLIL::Wire
*> result
;
1104 result
.reserve(wires_
.size());
1105 for (auto &it
: wires_
)
1106 if (design
->selected(this, it
.second
))
1107 result
.push_back(it
.second
);
1111 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1113 std::vector
<RTLIL::Cell
*> result
;
1114 result
.reserve(wires_
.size());
1115 for (auto &it
: cells_
)
1116 if (design
->selected(this, it
.second
))
1117 result
.push_back(it
.second
);
1121 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1123 log_assert(!wire
->name
.empty());
1124 log_assert(count_id(wire
->name
) == 0);
1125 log_assert(refcount_wires_
== 0);
1126 wires_
[wire
->name
] = wire
;
1127 wire
->module
= this;
1130 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1132 log_assert(!cell
->name
.empty());
1133 log_assert(count_id(cell
->name
) == 0);
1134 log_assert(refcount_cells_
== 0);
1135 cells_
[cell
->name
] = cell
;
1136 cell
->module
= this;
1140 struct DeleteWireWorker
1142 RTLIL::Module
*module
;
1143 const pool
<RTLIL::Wire
*> *wires_p
;
1145 void operator()(RTLIL::SigSpec
&sig
) {
1146 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1147 for (auto &c
: chunks
)
1148 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1149 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1157 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1159 log_assert(refcount_wires_
== 0);
1161 DeleteWireWorker delete_wire_worker
;
1162 delete_wire_worker
.module
= this;
1163 delete_wire_worker
.wires_p
= &wires
;
1164 rewrite_sigspecs(delete_wire_worker
);
1166 for (auto &it
: wires
) {
1167 log_assert(wires_
.count(it
->name
) != 0);
1168 wires_
.erase(it
->name
);
1173 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1175 while (!cell
->connections_
.empty())
1176 cell
->unsetPort(cell
->connections_
.begin()->first
);
1178 log_assert(cells_
.count(cell
->name
) != 0);
1179 log_assert(refcount_cells_
== 0);
1180 cells_
.erase(cell
->name
);
1184 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1186 log_assert(wires_
[wire
->name
] == wire
);
1187 log_assert(refcount_wires_
== 0);
1188 wires_
.erase(wire
->name
);
1189 wire
->name
= new_name
;
1193 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1195 log_assert(cells_
[cell
->name
] == cell
);
1196 log_assert(refcount_wires_
== 0);
1197 cells_
.erase(cell
->name
);
1198 cell
->name
= new_name
;
1202 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1204 log_assert(count_id(old_name
) != 0);
1205 if (wires_
.count(old_name
))
1206 rename(wires_
.at(old_name
), new_name
);
1207 else if (cells_
.count(old_name
))
1208 rename(cells_
.at(old_name
), new_name
);
1213 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1215 log_assert(wires_
[w1
->name
] == w1
);
1216 log_assert(wires_
[w2
->name
] == w2
);
1217 log_assert(refcount_wires_
== 0);
1219 wires_
.erase(w1
->name
);
1220 wires_
.erase(w2
->name
);
1222 std::swap(w1
->name
, w2
->name
);
1224 wires_
[w1
->name
] = w1
;
1225 wires_
[w2
->name
] = w2
;
1228 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1230 log_assert(cells_
[c1
->name
] == c1
);
1231 log_assert(cells_
[c2
->name
] == c2
);
1232 log_assert(refcount_cells_
== 0);
1234 cells_
.erase(c1
->name
);
1235 cells_
.erase(c2
->name
);
1237 std::swap(c1
->name
, c2
->name
);
1239 cells_
[c1
->name
] = c1
;
1240 cells_
[c2
->name
] = c2
;
1243 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1246 return uniquify(name
, index
);
1249 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1252 if (count_id(name
) == 0)
1258 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1259 if (count_id(new_name
) == 0)
1265 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1267 if (a
->port_id
&& !b
->port_id
)
1269 if (!a
->port_id
&& b
->port_id
)
1272 if (a
->port_id
== b
->port_id
)
1273 return a
->name
< b
->name
;
1274 return a
->port_id
< b
->port_id
;
1277 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1279 for (auto mon
: monitors
)
1280 mon
->notify_connect(this, conn
);
1283 for (auto mon
: design
->monitors
)
1284 mon
->notify_connect(this, conn
);
1286 connections_
.push_back(conn
);
1289 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1291 connect(RTLIL::SigSig(lhs
, rhs
));
1294 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1296 for (auto mon
: monitors
)
1297 mon
->notify_connect(this, new_conn
);
1300 for (auto mon
: design
->monitors
)
1301 mon
->notify_connect(this, new_conn
);
1303 connections_
= new_conn
;
1306 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1308 return connections_
;
1311 void RTLIL::Module::fixup_ports()
1313 std::vector
<RTLIL::Wire
*> all_ports
;
1315 for (auto &w
: wires_
)
1316 if (w
.second
->port_input
|| w
.second
->port_output
)
1317 all_ports
.push_back(w
.second
);
1319 w
.second
->port_id
= 0;
1321 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1324 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1325 ports
.push_back(all_ports
[i
]->name
);
1326 all_ports
[i
]->port_id
= i
+1;
1330 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1332 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1334 wire
->width
= width
;
1339 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1341 RTLIL::Wire
*wire
= addWire(name
);
1342 wire
->width
= other
->width
;
1343 wire
->start_offset
= other
->start_offset
;
1344 wire
->port_id
= other
->port_id
;
1345 wire
->port_input
= other
->port_input
;
1346 wire
->port_output
= other
->port_output
;
1347 wire
->upto
= other
->upto
;
1348 wire
->attributes
= other
->attributes
;
1352 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1354 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1361 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1363 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1364 cell
->connections_
= other
->connections_
;
1365 cell
->parameters
= other
->parameters
;
1366 cell
->attributes
= other
->attributes
;
1370 #define DEF_METHOD(_func, _y_size, _type) \
1371 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1372 RTLIL::Cell *cell = addCell(name, _type); \
1373 cell->parameters["\\A_SIGNED"] = is_signed; \
1374 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1375 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1376 cell->setPort("\\A", sig_a); \
1377 cell->setPort("\\Y", sig_y); \
1380 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1381 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1382 add ## _func(name, sig_a, sig_y, is_signed); \
1385 DEF_METHOD(Not
, sig_a
.size(), "$not")
1386 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1387 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1388 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1389 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1390 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1391 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1392 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1393 DEF_METHOD(LogicNot
, 1, "$logic_not")
1396 #define DEF_METHOD(_func, _y_size, _type) \
1397 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1398 RTLIL::Cell *cell = addCell(name, _type); \
1399 cell->parameters["\\A_SIGNED"] = is_signed; \
1400 cell->parameters["\\B_SIGNED"] = is_signed; \
1401 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1402 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1403 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1404 cell->setPort("\\A", sig_a); \
1405 cell->setPort("\\B", sig_b); \
1406 cell->setPort("\\Y", sig_y); \
1409 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1410 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1411 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1414 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1415 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1416 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1417 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1418 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1419 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1420 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1421 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1422 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1423 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1424 DEF_METHOD(Lt
, 1, "$lt")
1425 DEF_METHOD(Le
, 1, "$le")
1426 DEF_METHOD(Eq
, 1, "$eq")
1427 DEF_METHOD(Ne
, 1, "$ne")
1428 DEF_METHOD(Eqx
, 1, "$eqx")
1429 DEF_METHOD(Nex
, 1, "$nex")
1430 DEF_METHOD(Ge
, 1, "$ge")
1431 DEF_METHOD(Gt
, 1, "$gt")
1432 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1433 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1434 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1435 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1436 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1437 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1438 DEF_METHOD(LogicOr
, 1, "$logic_or")
1441 #define DEF_METHOD(_func, _type, _pmux) \
1442 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) { \
1443 RTLIL::Cell *cell = addCell(name, _type); \
1444 cell->parameters["\\WIDTH"] = sig_a.size(); \
1445 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1446 cell->setPort("\\A", sig_a); \
1447 cell->setPort("\\B", sig_b); \
1448 cell->setPort("\\S", sig_s); \
1449 cell->setPort("\\Y", sig_y); \
1452 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1453 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1454 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1457 DEF_METHOD(Mux
, "$mux", 0)
1458 DEF_METHOD(Pmux
, "$pmux", 1)
1461 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1462 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1463 RTLIL::Cell *cell = addCell(name, _type); \
1464 cell->setPort("\\" #_P1, sig1); \
1465 cell->setPort("\\" #_P2, sig2); \
1468 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1469 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1470 add ## _func(name, sig1, sig2); \
1473 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1474 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1475 RTLIL::Cell *cell = addCell(name, _type); \
1476 cell->setPort("\\" #_P1, sig1); \
1477 cell->setPort("\\" #_P2, sig2); \
1478 cell->setPort("\\" #_P3, sig3); \
1481 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1482 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1483 add ## _func(name, sig1, sig2, sig3); \
1486 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1487 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1488 RTLIL::Cell *cell = addCell(name, _type); \
1489 cell->setPort("\\" #_P1, sig1); \
1490 cell->setPort("\\" #_P2, sig2); \
1491 cell->setPort("\\" #_P3, sig3); \
1492 cell->setPort("\\" #_P4, sig4); \
1495 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1496 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1497 add ## _func(name, sig1, sig2, sig3, sig4); \
1500 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1501 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1502 RTLIL::Cell *cell = addCell(name, _type); \
1503 cell->setPort("\\" #_P1, sig1); \
1504 cell->setPort("\\" #_P2, sig2); \
1505 cell->setPort("\\" #_P3, sig3); \
1506 cell->setPort("\\" #_P4, sig4); \
1507 cell->setPort("\\" #_P5, sig5); \
1510 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1511 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1512 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1515 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1516 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1517 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1518 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1519 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1520 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1521 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1522 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1523 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1524 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1525 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1526 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1532 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
)
1534 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1535 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1536 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1537 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1538 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1539 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1540 cell
->setPort("\\A", sig_a
);
1541 cell
->setPort("\\B", sig_b
);
1542 cell
->setPort("\\Y", sig_y
);
1546 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1548 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1549 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1550 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1551 cell
->parameters
["\\OFFSET"] = offset
;
1552 cell
->setPort("\\A", sig_a
);
1553 cell
->setPort("\\Y", sig_y
);
1557 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1559 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1560 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1561 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1562 cell
->setPort("\\A", sig_a
);
1563 cell
->setPort("\\B", sig_b
);
1564 cell
->setPort("\\Y", sig_y
);
1568 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1570 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1571 cell
->parameters
["\\LUT"] = lut
;
1572 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1573 cell
->setPort("\\A", sig_i
);
1574 cell
->setPort("\\Y", sig_o
);
1578 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1580 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1581 cell
->setPort("\\A", sig_a
);
1582 cell
->setPort("\\EN", sig_en
);
1586 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
)
1588 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1589 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1590 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1591 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1592 cell
->setPort("\\SET", sig_set
);
1593 cell
->setPort("\\CLR", sig_clr
);
1594 cell
->setPort("\\Q", sig_q
);
1598 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1600 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1601 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1602 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1603 cell
->setPort("\\CLK", sig_clk
);
1604 cell
->setPort("\\D", sig_d
);
1605 cell
->setPort("\\Q", sig_q
);
1609 RTLIL::Cell
* RTLIL::Module::addDffe(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool en_polarity
)
1611 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
1612 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1613 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1614 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1615 cell
->setPort("\\CLK", sig_clk
);
1616 cell
->setPort("\\EN", sig_en
);
1617 cell
->setPort("\\D", sig_d
);
1618 cell
->setPort("\\Q", sig_q
);
1622 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1623 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1625 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1626 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1627 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1628 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1629 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1630 cell
->setPort("\\CLK", sig_clk
);
1631 cell
->setPort("\\SET", sig_set
);
1632 cell
->setPort("\\CLR", sig_clr
);
1633 cell
->setPort("\\D", sig_d
);
1634 cell
->setPort("\\Q", sig_q
);
1638 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1639 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1641 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1642 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1643 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1644 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1645 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1646 cell
->setPort("\\CLK", sig_clk
);
1647 cell
->setPort("\\ARST", sig_arst
);
1648 cell
->setPort("\\D", sig_d
);
1649 cell
->setPort("\\Q", sig_q
);
1653 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1655 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1656 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1657 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1658 cell
->setPort("\\EN", sig_en
);
1659 cell
->setPort("\\D", sig_d
);
1660 cell
->setPort("\\Q", sig_q
);
1664 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1665 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1667 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1668 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1669 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1670 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1671 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1672 cell
->setPort("\\EN", sig_en
);
1673 cell
->setPort("\\SET", sig_set
);
1674 cell
->setPort("\\CLR", sig_clr
);
1675 cell
->setPort("\\D", sig_d
);
1676 cell
->setPort("\\Q", sig_q
);
1680 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1682 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1683 cell
->setPort("\\C", sig_clk
);
1684 cell
->setPort("\\D", sig_d
);
1685 cell
->setPort("\\Q", sig_q
);
1689 RTLIL::Cell
* RTLIL::Module::addDffeGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool en_polarity
)
1691 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
1692 cell
->setPort("\\C", sig_clk
);
1693 cell
->setPort("\\E", sig_en
);
1694 cell
->setPort("\\D", sig_d
);
1695 cell
->setPort("\\Q", sig_q
);
1699 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1700 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1702 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1703 cell
->setPort("\\C", sig_clk
);
1704 cell
->setPort("\\S", sig_set
);
1705 cell
->setPort("\\R", sig_clr
);
1706 cell
->setPort("\\D", sig_d
);
1707 cell
->setPort("\\Q", sig_q
);
1711 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1712 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1714 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1715 cell
->setPort("\\C", sig_clk
);
1716 cell
->setPort("\\R", sig_arst
);
1717 cell
->setPort("\\D", sig_d
);
1718 cell
->setPort("\\Q", sig_q
);
1722 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1724 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1725 cell
->setPort("\\E", sig_en
);
1726 cell
->setPort("\\D", sig_d
);
1727 cell
->setPort("\\Q", sig_q
);
1731 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1732 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1734 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1735 cell
->setPort("\\E", sig_en
);
1736 cell
->setPort("\\S", sig_set
);
1737 cell
->setPort("\\R", sig_clr
);
1738 cell
->setPort("\\D", sig_d
);
1739 cell
->setPort("\\Q", sig_q
);
1746 static unsigned int hashidx_count
= 123456789;
1747 hashidx_count
= mkhash_xorshift(hashidx_count
);
1748 hashidx_
= hashidx_count
;
1755 port_output
= false;
1759 RTLIL::Memory::Memory()
1761 static unsigned int hashidx_count
= 123456789;
1762 hashidx_count
= mkhash_xorshift(hashidx_count
);
1763 hashidx_
= hashidx_count
;
1769 RTLIL::Cell::Cell() : module(nullptr)
1771 static unsigned int hashidx_count
= 123456789;
1772 hashidx_count
= mkhash_xorshift(hashidx_count
);
1773 hashidx_
= hashidx_count
;
1775 // log("#memtrace# %p\n", this);
1779 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1781 return connections_
.count(portname
) != 0;
1784 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1786 RTLIL::SigSpec signal
;
1787 auto conn_it
= connections_
.find(portname
);
1789 if (conn_it
!= connections_
.end())
1791 for (auto mon
: module
->monitors
)
1792 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1795 for (auto mon
: module
->design
->monitors
)
1796 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1798 connections_
.erase(conn_it
);
1802 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1804 auto conn_it
= connections_
.find(portname
);
1806 if (conn_it
== connections_
.end()) {
1807 connections_
[portname
] = RTLIL::SigSpec();
1808 conn_it
= connections_
.find(portname
);
1809 log_assert(conn_it
!= connections_
.end());
1812 for (auto mon
: module
->monitors
)
1813 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1816 for (auto mon
: module
->design
->monitors
)
1817 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1819 conn_it
->second
= signal
;
1822 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1824 return connections_
.at(portname
);
1827 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1829 return connections_
;
1832 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1834 return parameters
.count(paramname
) != 0;
1837 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1839 parameters
.erase(paramname
);
1842 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1844 parameters
[paramname
] = value
;
1847 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1849 return parameters
.at(paramname
);
1852 void RTLIL::Cell::check()
1855 InternalCellChecker
checker(NULL
, this);
1860 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1862 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1863 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1866 if (type
== "$mux" || type
== "$pmux") {
1867 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
1868 if (type
== "$pmux")
1869 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
1874 if (type
== "$lut") {
1875 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
1879 if (type
== "$fa") {
1880 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
1884 if (type
== "$lcu") {
1885 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
1889 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
1891 if (connections_
.count("\\A")) {
1892 if (signedness_ab
) {
1894 parameters
["\\A_SIGNED"] = true;
1895 else if (parameters
.count("\\A_SIGNED") == 0)
1896 parameters
["\\A_SIGNED"] = false;
1898 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
1901 if (connections_
.count("\\B")) {
1902 if (signedness_ab
) {
1904 parameters
["\\B_SIGNED"] = true;
1905 else if (parameters
.count("\\B_SIGNED") == 0)
1906 parameters
["\\B_SIGNED"] = false;
1908 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
1911 if (connections_
.count("\\Y"))
1912 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
1917 RTLIL::SigChunk::SigChunk()
1924 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1928 width
= GetSize(data
);
1932 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1934 log_assert(wire
!= nullptr);
1936 this->width
= wire
->width
;
1940 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1942 log_assert(wire
!= nullptr);
1944 this->width
= width
;
1945 this->offset
= offset
;
1948 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1951 data
= RTLIL::Const(str
).bits
;
1952 width
= GetSize(data
);
1956 RTLIL::SigChunk::SigChunk(int val
, int width
)
1959 data
= RTLIL::Const(val
, width
).bits
;
1960 this->width
= GetSize(data
);
1964 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1967 data
= RTLIL::Const(bit
, width
).bits
;
1968 this->width
= GetSize(data
);
1972 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1977 data
= RTLIL::Const(bit
.data
).bits
;
1979 offset
= bit
.offset
;
1983 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1985 RTLIL::SigChunk ret
;
1988 ret
.offset
= this->offset
+ offset
;
1991 for (int i
= 0; i
< length
; i
++)
1992 ret
.data
.push_back(data
[offset
+i
]);
1998 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2000 if (wire
&& other
.wire
)
2001 if (wire
->name
!= other
.wire
->name
)
2002 return wire
->name
< other
.wire
->name
;
2004 if (wire
!= other
.wire
)
2005 return wire
< other
.wire
;
2007 if (offset
!= other
.offset
)
2008 return offset
< other
.offset
;
2010 if (width
!= other
.width
)
2011 return width
< other
.width
;
2013 return data
< other
.data
;
2016 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2018 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2021 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2028 RTLIL::SigSpec::SigSpec()
2034 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2039 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2041 cover("kernel.rtlil.sigspec.init.list");
2046 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2047 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2051 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2053 cover("kernel.rtlil.sigspec.assign");
2055 width_
= other
.width_
;
2056 hash_
= other
.hash_
;
2057 chunks_
= other
.chunks_
;
2060 if (!other
.bits_
.empty())
2062 RTLIL::SigChunk
*last
= NULL
;
2063 int last_end_offset
= 0;
2065 for (auto &bit
: other
.bits_
) {
2066 if (last
&& bit
.wire
== last
->wire
) {
2067 if (bit
.wire
== NULL
) {
2068 last
->data
.push_back(bit
.data
);
2071 } else if (last_end_offset
== bit
.offset
) {
2077 chunks_
.push_back(bit
);
2078 last
= &chunks_
.back();
2079 last_end_offset
= bit
.offset
+ 1;
2088 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2090 cover("kernel.rtlil.sigspec.init.const");
2092 chunks_
.push_back(RTLIL::SigChunk(value
));
2093 width_
= chunks_
.back().width
;
2098 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2100 cover("kernel.rtlil.sigspec.init.chunk");
2102 chunks_
.push_back(chunk
);
2103 width_
= chunks_
.back().width
;
2108 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2110 cover("kernel.rtlil.sigspec.init.wire");
2112 chunks_
.push_back(RTLIL::SigChunk(wire
));
2113 width_
= chunks_
.back().width
;
2118 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2120 cover("kernel.rtlil.sigspec.init.wire_part");
2122 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2123 width_
= chunks_
.back().width
;
2128 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2130 cover("kernel.rtlil.sigspec.init.str");
2132 chunks_
.push_back(RTLIL::SigChunk(str
));
2133 width_
= chunks_
.back().width
;
2138 RTLIL::SigSpec::SigSpec(int val
, int width
)
2140 cover("kernel.rtlil.sigspec.init.int");
2142 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2148 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2150 cover("kernel.rtlil.sigspec.init.state");
2152 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2158 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2160 cover("kernel.rtlil.sigspec.init.bit");
2162 if (bit
.wire
== NULL
)
2163 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2165 for (int i
= 0; i
< width
; i
++)
2166 chunks_
.push_back(bit
);
2172 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2174 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2178 for (auto &c
: chunks
)
2183 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2185 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2189 for (auto &bit
: bits
)
2194 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2196 cover("kernel.rtlil.sigspec.init.pool_bits");
2200 for (auto &bit
: bits
)
2205 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2207 cover("kernel.rtlil.sigspec.init.stdset_bits");
2211 for (auto &bit
: bits
)
2216 RTLIL::SigSpec::SigSpec(bool bit
)
2218 cover("kernel.rtlil.sigspec.init.bool");
2226 void RTLIL::SigSpec::pack() const
2228 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2230 if (that
->bits_
.empty())
2233 cover("kernel.rtlil.sigspec.convert.pack");
2234 log_assert(that
->chunks_
.empty());
2236 std::vector
<RTLIL::SigBit
> old_bits
;
2237 old_bits
.swap(that
->bits_
);
2239 RTLIL::SigChunk
*last
= NULL
;
2240 int last_end_offset
= 0;
2242 for (auto &bit
: old_bits
) {
2243 if (last
&& bit
.wire
== last
->wire
) {
2244 if (bit
.wire
== NULL
) {
2245 last
->data
.push_back(bit
.data
);
2248 } else if (last_end_offset
== bit
.offset
) {
2254 that
->chunks_
.push_back(bit
);
2255 last
= &that
->chunks_
.back();
2256 last_end_offset
= bit
.offset
+ 1;
2262 void RTLIL::SigSpec::unpack() const
2264 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2266 if (that
->chunks_
.empty())
2269 cover("kernel.rtlil.sigspec.convert.unpack");
2270 log_assert(that
->bits_
.empty());
2272 that
->bits_
.reserve(that
->width_
);
2273 for (auto &c
: that
->chunks_
)
2274 for (int i
= 0; i
< c
.width
; i
++)
2275 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2277 that
->chunks_
.clear();
2281 void RTLIL::SigSpec::updhash() const
2283 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2285 if (that
->hash_
!= 0)
2288 cover("kernel.rtlil.sigspec.hash");
2292 for (auto &c
: that
->chunks_
)
2293 if (c
.wire
== NULL
) {
2294 for (auto &v
: c
.data
)
2295 that
->hash_
= mkhash(that
->hash_
, v
);
2297 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
2298 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
2299 that
->hash_
= mkhash(that
->hash_
, c
.width
);
2302 if (that
->hash_
== 0)
2306 void RTLIL::SigSpec::sort()
2309 cover("kernel.rtlil.sigspec.sort");
2310 std::sort(bits_
.begin(), bits_
.end());
2313 void RTLIL::SigSpec::sort_and_unify()
2315 cover("kernel.rtlil.sigspec.sort_and_unify");
2316 *this = this->to_sigbit_set();
2319 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2321 replace(pattern
, with
, this);
2324 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2326 log_assert(pattern
.width_
== with
.width_
);
2331 dict
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2333 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++)
2334 if (pattern
.bits_
[i
].wire
!= NULL
)
2335 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2337 replace(rules
, other
);
2340 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2342 replace(rules
, this);
2345 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2347 cover("kernel.rtlil.sigspec.replace_dict");
2349 log_assert(other
!= NULL
);
2350 log_assert(width_
== other
->width_
);
2355 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2356 auto it
= rules
.find(bits_
[i
]);
2357 if (it
!= rules
.end())
2358 other
->bits_
[i
] = it
->second
;
2364 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2366 replace(rules
, this);
2369 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2371 cover("kernel.rtlil.sigspec.replace_map");
2373 log_assert(other
!= NULL
);
2374 log_assert(width_
== other
->width_
);
2379 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2380 auto it
= rules
.find(bits_
[i
]);
2381 if (it
!= rules
.end())
2382 other
->bits_
[i
] = it
->second
;
2388 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2390 remove2(pattern
, NULL
);
2393 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2395 RTLIL::SigSpec tmp
= *this;
2396 tmp
.remove2(pattern
, other
);
2399 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2401 pool
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_pool();
2402 remove2(pattern_bits
, other
);
2405 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
2407 remove2(pattern
, NULL
);
2410 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2412 RTLIL::SigSpec tmp
= *this;
2413 tmp
.remove2(pattern
, other
);
2416 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2419 cover("kernel.rtlil.sigspec.remove_other");
2421 cover("kernel.rtlil.sigspec.remove");
2425 if (other
!= NULL
) {
2426 log_assert(width_
== other
->width_
);
2430 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2432 new_bits
.resize(GetSize(bits_
));
2434 new_other_bits
.resize(GetSize(bits_
));
2437 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2438 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2441 new_other_bits
[k
] = other
->bits_
[i
];
2442 new_bits
[k
++] = bits_
[i
];
2447 new_other_bits
.resize(k
);
2449 bits_
.swap(new_bits
);
2450 width_
= GetSize(bits_
);
2452 if (other
!= NULL
) {
2453 other
->bits_
.swap(new_other_bits
);
2454 other
->width_
= GetSize(other
->bits_
);
2460 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2462 pool
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_pool();
2463 return extract(pattern_bits
, other
);
2466 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2469 cover("kernel.rtlil.sigspec.extract_other");
2471 cover("kernel.rtlil.sigspec.extract");
2473 log_assert(other
== NULL
|| width_
== other
->width_
);
2475 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2479 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2480 for (int i
= 0; i
< width_
; i
++)
2481 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2482 ret
.append_bit(bits_other
[i
]);
2484 for (int i
= 0; i
< width_
; i
++)
2485 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2486 ret
.append_bit(bits_match
[i
]);
2493 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2495 cover("kernel.rtlil.sigspec.replace_pos");
2500 log_assert(offset
>= 0);
2501 log_assert(with
.width_
>= 0);
2502 log_assert(offset
+with
.width_
<= width_
);
2504 for (int i
= 0; i
< with
.width_
; i
++)
2505 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2510 void RTLIL::SigSpec::remove_const()
2514 cover("kernel.rtlil.sigspec.remove_const.packed");
2516 std::vector
<RTLIL::SigChunk
> new_chunks
;
2517 new_chunks
.reserve(GetSize(chunks_
));
2520 for (auto &chunk
: chunks_
)
2521 if (chunk
.wire
!= NULL
) {
2522 new_chunks
.push_back(chunk
);
2523 width_
+= chunk
.width
;
2526 chunks_
.swap(new_chunks
);
2530 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2532 std::vector
<RTLIL::SigBit
> new_bits
;
2533 new_bits
.reserve(width_
);
2535 for (auto &bit
: bits_
)
2536 if (bit
.wire
!= NULL
)
2537 new_bits
.push_back(bit
);
2539 bits_
.swap(new_bits
);
2540 width_
= bits_
.size();
2546 void RTLIL::SigSpec::remove(int offset
, int length
)
2548 cover("kernel.rtlil.sigspec.remove_pos");
2552 log_assert(offset
>= 0);
2553 log_assert(length
>= 0);
2554 log_assert(offset
+ length
<= width_
);
2556 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2557 width_
= bits_
.size();
2562 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2565 cover("kernel.rtlil.sigspec.extract_pos");
2566 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2569 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2571 if (signal
.width_
== 0)
2579 cover("kernel.rtlil.sigspec.append");
2581 if (packed() != signal
.packed()) {
2587 for (auto &other_c
: signal
.chunks_
)
2589 auto &my_last_c
= chunks_
.back();
2590 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2591 auto &this_data
= my_last_c
.data
;
2592 auto &other_data
= other_c
.data
;
2593 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2594 my_last_c
.width
+= other_c
.width
;
2596 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2597 my_last_c
.width
+= other_c
.width
;
2599 chunks_
.push_back(other_c
);
2602 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2604 width_
+= signal
.width_
;
2608 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2612 cover("kernel.rtlil.sigspec.append_bit.packed");
2614 if (chunks_
.size() == 0)
2615 chunks_
.push_back(bit
);
2617 if (bit
.wire
== NULL
)
2618 if (chunks_
.back().wire
== NULL
) {
2619 chunks_
.back().data
.push_back(bit
.data
);
2620 chunks_
.back().width
++;
2622 chunks_
.push_back(bit
);
2624 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2625 chunks_
.back().width
++;
2627 chunks_
.push_back(bit
);
2631 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2632 bits_
.push_back(bit
);
2639 void RTLIL::SigSpec::extend_xx(int width
, bool is_signed
)
2641 cover("kernel.rtlil.sigspec.extend_xx");
2646 remove(width
, width_
- width
);
2648 if (width_
< width
) {
2649 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::S0
;
2650 if (!is_signed
&& (padding
== RTLIL::State::S1
|| padding
.wire
))
2651 padding
= RTLIL::State::S0
;
2652 while (width_
< width
)
2657 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2659 cover("kernel.rtlil.sigspec.extend_u0");
2664 remove(width
, width_
- width
);
2666 if (width_
< width
) {
2667 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::S0
;
2669 padding
= RTLIL::State::S0
;
2670 while (width_
< width
)
2676 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2678 cover("kernel.rtlil.sigspec.repeat");
2681 for (int i
= 0; i
< num
; i
++)
2687 void RTLIL::SigSpec::check() const
2691 cover("kernel.rtlil.sigspec.check.skip");
2695 cover("kernel.rtlil.sigspec.check.packed");
2698 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2699 const RTLIL::SigChunk chunk
= chunks_
[i
];
2700 if (chunk
.wire
== NULL
) {
2702 log_assert(chunks_
[i
-1].wire
!= NULL
);
2703 log_assert(chunk
.offset
== 0);
2704 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
2706 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2707 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2708 log_assert(chunk
.offset
>= 0);
2709 log_assert(chunk
.width
>= 0);
2710 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2711 log_assert(chunk
.data
.size() == 0);
2715 log_assert(w
== width_
);
2716 log_assert(bits_
.empty());
2720 cover("kernel.rtlil.sigspec.check.unpacked");
2722 log_assert(width_
== GetSize(bits_
));
2723 log_assert(chunks_
.empty());
2728 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2730 cover("kernel.rtlil.sigspec.comp_lt");
2735 if (width_
!= other
.width_
)
2736 return width_
< other
.width_
;
2741 if (chunks_
.size() != other
.chunks_
.size())
2742 return chunks_
.size() < other
.chunks_
.size();
2747 if (hash_
!= other
.hash_
)
2748 return hash_
< other
.hash_
;
2750 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2751 if (chunks_
[i
] != other
.chunks_
[i
]) {
2752 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2753 return chunks_
[i
] < other
.chunks_
[i
];
2756 cover("kernel.rtlil.sigspec.comp_lt.equal");
2760 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2762 cover("kernel.rtlil.sigspec.comp_eq");
2767 if (width_
!= other
.width_
)
2773 if (chunks_
.size() != chunks_
.size())
2779 if (hash_
!= other
.hash_
)
2782 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2783 if (chunks_
[i
] != other
.chunks_
[i
]) {
2784 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2788 cover("kernel.rtlil.sigspec.comp_eq.equal");
2792 bool RTLIL::SigSpec::is_wire() const
2794 cover("kernel.rtlil.sigspec.is_wire");
2797 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2800 bool RTLIL::SigSpec::is_chunk() const
2802 cover("kernel.rtlil.sigspec.is_chunk");
2805 return GetSize(chunks_
) == 1;
2808 bool RTLIL::SigSpec::is_fully_const() const
2810 cover("kernel.rtlil.sigspec.is_fully_const");
2813 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2814 if (it
->width
> 0 && it
->wire
!= NULL
)
2819 bool RTLIL::SigSpec::is_fully_def() const
2821 cover("kernel.rtlil.sigspec.is_fully_def");
2824 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2825 if (it
->width
> 0 && it
->wire
!= NULL
)
2827 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2828 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
2834 bool RTLIL::SigSpec::is_fully_undef() const
2836 cover("kernel.rtlil.sigspec.is_fully_undef");
2839 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2840 if (it
->width
> 0 && it
->wire
!= NULL
)
2842 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2843 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
2849 bool RTLIL::SigSpec::has_marked_bits() const
2851 cover("kernel.rtlil.sigspec.has_marked_bits");
2854 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2855 if (it
->width
> 0 && it
->wire
== NULL
) {
2856 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2857 if (it
->data
[i
] == RTLIL::State::Sm
)
2863 bool RTLIL::SigSpec::as_bool() const
2865 cover("kernel.rtlil.sigspec.as_bool");
2868 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
2870 return RTLIL::Const(chunks_
[0].data
).as_bool();
2874 int RTLIL::SigSpec::as_int(bool is_signed
) const
2876 cover("kernel.rtlil.sigspec.as_int");
2879 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
2881 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
2885 std::string
RTLIL::SigSpec::as_string() const
2887 cover("kernel.rtlil.sigspec.as_string");
2891 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2892 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2893 if (chunk
.wire
!= NULL
)
2894 for (int j
= 0; j
< chunk
.width
; j
++)
2897 str
+= RTLIL::Const(chunk
.data
).as_string();
2902 RTLIL::Const
RTLIL::SigSpec::as_const() const
2904 cover("kernel.rtlil.sigspec.as_const");
2907 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
2909 return chunks_
[0].data
;
2910 return RTLIL::Const();
2913 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2915 cover("kernel.rtlil.sigspec.as_wire");
2918 log_assert(is_wire());
2919 return chunks_
[0].wire
;
2922 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2924 cover("kernel.rtlil.sigspec.as_chunk");
2927 log_assert(is_chunk());
2931 bool RTLIL::SigSpec::match(std::string pattern
) const
2933 cover("kernel.rtlil.sigspec.match");
2936 std::string str
= as_string();
2937 log_assert(pattern
.size() == str
.size());
2939 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2940 if (pattern
[i
] == ' ')
2942 if (pattern
[i
] == '*') {
2943 if (str
[i
] != 'z' && str
[i
] != 'x')
2947 if (pattern
[i
] != str
[i
])
2954 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2956 cover("kernel.rtlil.sigspec.to_sigbit_set");
2959 std::set
<RTLIL::SigBit
> sigbits
;
2960 for (auto &c
: chunks_
)
2961 for (int i
= 0; i
< c
.width
; i
++)
2962 sigbits
.insert(RTLIL::SigBit(c
, i
));
2966 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
2968 cover("kernel.rtlil.sigspec.to_sigbit_pool");
2971 pool
<RTLIL::SigBit
> sigbits
;
2972 for (auto &c
: chunks_
)
2973 for (int i
= 0; i
< c
.width
; i
++)
2974 sigbits
.insert(RTLIL::SigBit(c
, i
));
2978 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2980 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2986 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2988 cover("kernel.rtlil.sigspec.to_sigbit_map");
2993 log_assert(width_
== other
.width_
);
2995 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2996 for (int i
= 0; i
< width_
; i
++)
2997 new_map
[bits_
[i
]] = other
.bits_
[i
];
3002 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3004 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3009 log_assert(width_
== other
.width_
);
3011 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3012 for (int i
= 0; i
< width_
; i
++)
3013 new_map
[bits_
[i
]] = other
.bits_
[i
];
3018 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
3020 cover("kernel.rtlil.sigspec.to_single_sigbit");
3023 log_assert(width_
== 1);
3024 for (auto &c
: chunks_
)
3026 return RTLIL::SigBit(c
);
3030 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3032 size_t start
= 0, end
= 0;
3033 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3034 tokens
.push_back(text
.substr(start
, end
- start
));
3037 tokens
.push_back(text
.substr(start
));
3040 static int sigspec_parse_get_dummy_line_num()
3045 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3047 cover("kernel.rtlil.sigspec.parse");
3049 std::vector
<std::string
> tokens
;
3050 sigspec_parse_split(tokens
, str
, ',');
3052 sig
= RTLIL::SigSpec();
3053 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3055 std::string netname
= tokens
[tokidx
];
3056 std::string indices
;
3058 if (netname
.size() == 0)
3061 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3062 cover("kernel.rtlil.sigspec.parse.const");
3063 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3064 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3067 sig
.append(RTLIL::Const(ast
->bits
));
3075 cover("kernel.rtlil.sigspec.parse.net");
3077 if (netname
[0] != '$' && netname
[0] != '\\')
3078 netname
= "\\" + netname
;
3080 if (module
->wires_
.count(netname
) == 0) {
3081 size_t indices_pos
= netname
.size()-1;
3082 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3085 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3086 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3088 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3090 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3091 indices
= netname
.substr(indices_pos
);
3092 netname
= netname
.substr(0, indices_pos
);
3097 if (module
->wires_
.count(netname
) == 0)
3100 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3101 if (!indices
.empty()) {
3102 std::vector
<std::string
> index_tokens
;
3103 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3104 if (index_tokens
.size() == 1) {
3105 cover("kernel.rtlil.sigspec.parse.bit_sel");
3106 int a
= atoi(index_tokens
.at(0).c_str());
3107 if (a
< 0 || a
>= wire
->width
)
3109 sig
.append(RTLIL::SigSpec(wire
, a
));
3111 cover("kernel.rtlil.sigspec.parse.part_sel");
3112 int a
= atoi(index_tokens
.at(0).c_str());
3113 int b
= atoi(index_tokens
.at(1).c_str());
3118 if (a
< 0 || a
>= wire
->width
)
3120 if (b
< 0 || b
>= wire
->width
)
3122 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3131 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3133 if (str
.empty() || str
[0] != '@')
3134 return parse(sig
, module
, str
);
3136 cover("kernel.rtlil.sigspec.parse.sel");
3138 str
= RTLIL::escape_id(str
.substr(1));
3139 if (design
->selection_vars
.count(str
) == 0)
3142 sig
= RTLIL::SigSpec();
3143 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3144 for (auto &it
: module
->wires_
)
3145 if (sel
.selected_member(module
->name
, it
.first
))
3146 sig
.append(it
.second
);
3151 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3154 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3155 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3160 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3161 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3165 if (lhs
.chunks_
.size() == 1) {
3166 char *p
= (char*)str
.c_str(), *endptr
;
3167 long int val
= strtol(p
, &endptr
, 10);
3168 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3169 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3170 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3175 return parse(sig
, module
, str
);
3178 RTLIL::CaseRule::~CaseRule()
3180 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3184 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3186 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3187 new_caserule
->compare
= compare
;
3188 new_caserule
->actions
= actions
;
3189 for (auto &it
: switches
)
3190 new_caserule
->switches
.push_back(it
->clone());
3191 return new_caserule
;
3194 RTLIL::SwitchRule::~SwitchRule()
3196 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3200 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3202 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3203 new_switchrule
->signal
= signal
;
3204 new_switchrule
->attributes
= attributes
;
3205 for (auto &it
: cases
)
3206 new_switchrule
->cases
.push_back(it
->clone());
3207 return new_switchrule
;
3211 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3213 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3214 new_syncrule
->type
= type
;
3215 new_syncrule
->signal
= signal
;
3216 new_syncrule
->actions
= actions
;
3217 return new_syncrule
;
3220 RTLIL::Process::~Process()
3222 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3226 RTLIL::Process
*RTLIL::Process::clone() const
3228 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3230 new_proc
->name
= name
;
3231 new_proc
->attributes
= attributes
;
3233 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3234 new_proc
->root_case
= *rc_ptr
;
3235 rc_ptr
->switches
.clear();
3238 for (auto &it
: syncs
)
3239 new_proc
->syncs
.push_back(it
->clone());