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 "kernel/celltypes.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
31 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
32 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
33 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
34 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
35 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
39 flags
= RTLIL::CONST_FLAG_NONE
;
42 RTLIL::Const::Const(std::string str
)
44 flags
= RTLIL::CONST_FLAG_STRING
;
45 for (int i
= str
.size()-1; i
>= 0; i
--) {
46 unsigned char ch
= str
[i
];
47 for (int j
= 0; j
< 8; j
++) {
48 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
54 RTLIL::Const::Const(int val
, int width
)
56 flags
= RTLIL::CONST_FLAG_NONE
;
57 for (int i
= 0; i
< width
; i
++) {
58 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
63 RTLIL::Const::Const(RTLIL::State bit
, int width
)
65 flags
= RTLIL::CONST_FLAG_NONE
;
66 for (int i
= 0; i
< width
; i
++)
70 RTLIL::Const::Const(const std::vector
<bool> &bits
)
72 flags
= RTLIL::CONST_FLAG_NONE
;
74 this->bits
.push_back(b
? RTLIL::S1
: RTLIL::S0
);
77 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
79 if (bits
.size() != other
.bits
.size())
80 return bits
.size() < other
.bits
.size();
81 for (size_t i
= 0; i
< bits
.size(); i
++)
82 if (bits
[i
] != other
.bits
[i
])
83 return bits
[i
] < other
.bits
[i
];
87 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
89 return bits
== other
.bits
;
92 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
94 return bits
!= other
.bits
;
97 bool RTLIL::Const::as_bool() const
99 for (size_t i
= 0; i
< bits
.size(); i
++)
100 if (bits
[i
] == RTLIL::S1
)
105 int RTLIL::Const::as_int(bool is_signed
) const
108 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
109 if (bits
[i
] == RTLIL::S1
)
111 if (is_signed
&& bits
.back() == RTLIL::S1
)
112 for (size_t i
= bits
.size(); i
< 32; i
++)
117 std::string
RTLIL::Const::as_string() const
120 for (size_t i
= bits
.size(); i
> 0; i
--)
122 case S0
: ret
+= "0"; break;
123 case S1
: ret
+= "1"; break;
124 case Sx
: ret
+= "x"; break;
125 case Sz
: ret
+= "z"; break;
126 case Sa
: ret
+= "-"; break;
127 case Sm
: ret
+= "m"; break;
132 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
135 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
137 case '0': c
.bits
.push_back(State::S0
); break;
138 case '1': c
.bits
.push_back(State::S1
); break;
139 case 'x': c
.bits
.push_back(State::Sx
); break;
140 case 'z': c
.bits
.push_back(State::Sz
); break;
141 case 'm': c
.bits
.push_back(State::Sm
); break;
142 default: c
.bits
.push_back(State::Sa
);
147 std::string
RTLIL::Const::decode_string() const
150 std::vector
<char> string_chars
;
151 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
153 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
154 if (bits
[i
+ j
] == RTLIL::State::S1
)
157 string_chars
.push_back(ch
);
159 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
160 string
+= string_chars
[i
];
164 bool RTLIL::Const::is_fully_zero() const
166 cover("kernel.rtlil.const.is_fully_zero");
168 for (auto bit
: bits
)
169 if (bit
!= RTLIL::State::S0
)
175 bool RTLIL::Const::is_fully_ones() const
177 cover("kernel.rtlil.const.is_fully_ones");
179 for (auto bit
: bits
)
180 if (bit
!= RTLIL::State::S1
)
186 bool RTLIL::Const::is_fully_def() const
188 cover("kernel.rtlil.const.is_fully_def");
190 for (auto bit
: bits
)
191 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
197 bool RTLIL::Const::is_fully_undef() const
199 cover("kernel.rtlil.const.is_fully_undef");
201 for (auto bit
: bits
)
202 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
208 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
)
210 attributes
[id
] = RTLIL::Const(1);
213 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
215 if (attributes
.count(id
) == 0)
217 return attributes
.at(id
).as_bool();
220 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
223 for (auto &s
: data
) {
224 if (!attrval
.empty())
228 attributes
[id
] = RTLIL::Const(attrval
);
231 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
233 pool
<string
> union_data
= get_strpool_attribute(id
);
234 union_data
.insert(data
.begin(), data
.end());
235 if (!union_data
.empty())
236 set_strpool_attribute(id
, union_data
);
239 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
242 if (attributes
.count(id
) != 0)
243 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
248 void RTLIL::AttrObject::set_src_attribute(const std::string
&src
)
251 attributes
.erase("\\src");
253 attributes
["\\src"] = src
;
256 std::string
RTLIL::AttrObject::get_src_attribute() const
259 if (attributes
.count("\\src"))
260 src
= attributes
.at("\\src").decode_string();
264 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
268 if (selected_modules
.count(mod_name
) > 0)
270 if (selected_members
.count(mod_name
) > 0)
275 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
279 if (selected_modules
.count(mod_name
) > 0)
284 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
288 if (selected_modules
.count(mod_name
) > 0)
290 if (selected_members
.count(mod_name
) > 0)
291 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
296 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
298 if (full_selection
) {
299 selected_modules
.clear();
300 selected_members
.clear();
304 std::vector
<RTLIL::IdString
> del_list
, add_list
;
307 for (auto mod_name
: selected_modules
) {
308 if (design
->modules_
.count(mod_name
) == 0)
309 del_list
.push_back(mod_name
);
310 selected_members
.erase(mod_name
);
312 for (auto mod_name
: del_list
)
313 selected_modules
.erase(mod_name
);
316 for (auto &it
: selected_members
)
317 if (design
->modules_
.count(it
.first
) == 0)
318 del_list
.push_back(it
.first
);
319 for (auto mod_name
: del_list
)
320 selected_members
.erase(mod_name
);
322 for (auto &it
: selected_members
) {
324 for (auto memb_name
: it
.second
)
325 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
326 del_list
.push_back(memb_name
);
327 for (auto memb_name
: del_list
)
328 it
.second
.erase(memb_name
);
333 for (auto &it
: selected_members
)
334 if (it
.second
.size() == 0)
335 del_list
.push_back(it
.first
);
336 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
337 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
338 add_list
.push_back(it
.first
);
339 for (auto mod_name
: del_list
)
340 selected_members
.erase(mod_name
);
341 for (auto mod_name
: add_list
) {
342 selected_members
.erase(mod_name
);
343 selected_modules
.insert(mod_name
);
346 if (selected_modules
.size() == design
->modules_
.size()) {
347 full_selection
= true;
348 selected_modules
.clear();
349 selected_members
.clear();
353 RTLIL::Design::Design()
355 static unsigned int hashidx_count
= 123456789;
356 hashidx_count
= mkhash_xorshift(hashidx_count
);
357 hashidx_
= hashidx_count
;
359 refcount_modules_
= 0;
360 selection_stack
.push_back(RTLIL::Selection());
363 RTLIL::Design::~Design()
365 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
367 for (auto n
: verilog_packages
)
369 for (auto n
: verilog_globals
)
373 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
375 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
378 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
380 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
383 RTLIL::Module
*RTLIL::Design::top_module()
385 RTLIL::Module
*module
= nullptr;
386 int module_count
= 0;
388 for (auto mod
: selected_modules()) {
389 if (mod
->get_bool_attribute("\\top"))
395 return module_count
== 1 ? module
: nullptr;
398 void RTLIL::Design::add(RTLIL::Module
*module
)
400 log_assert(modules_
.count(module
->name
) == 0);
401 log_assert(refcount_modules_
== 0);
402 modules_
[module
->name
] = module
;
403 module
->design
= this;
405 for (auto mon
: monitors
)
406 mon
->notify_module_add(module
);
409 log("#X# New Module: %s\n", log_id(module
));
410 log_backtrace("-X- ", yosys_xtrace
-1);
414 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
416 log_assert(modules_
.count(name
) == 0);
417 log_assert(refcount_modules_
== 0);
419 RTLIL::Module
*module
= new RTLIL::Module
;
420 modules_
[name
] = module
;
421 module
->design
= this;
424 for (auto mon
: monitors
)
425 mon
->notify_module_add(module
);
428 log("#X# New Module: %s\n", log_id(module
));
429 log_backtrace("-X- ", yosys_xtrace
-1);
435 void RTLIL::Design::scratchpad_unset(std::string varname
)
437 scratchpad
.erase(varname
);
440 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
442 scratchpad
[varname
] = stringf("%d", value
);
445 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
447 scratchpad
[varname
] = value
? "true" : "false";
450 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
452 scratchpad
[varname
] = value
;
455 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
457 if (scratchpad
.count(varname
) == 0)
458 return default_value
;
460 std::string str
= scratchpad
.at(varname
);
462 if (str
== "0" || str
== "false")
465 if (str
== "1" || str
== "true")
468 char *endptr
= nullptr;
469 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
470 return *endptr
? default_value
: parsed_value
;
473 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
475 if (scratchpad
.count(varname
) == 0)
476 return default_value
;
478 std::string str
= scratchpad
.at(varname
);
480 if (str
== "0" || str
== "false")
483 if (str
== "1" || str
== "true")
486 return default_value
;
489 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
491 if (scratchpad
.count(varname
) == 0)
492 return default_value
;
493 return scratchpad
.at(varname
);
496 void RTLIL::Design::remove(RTLIL::Module
*module
)
498 for (auto mon
: monitors
)
499 mon
->notify_module_del(module
);
502 log("#X# Remove Module: %s\n", log_id(module
));
503 log_backtrace("-X- ", yosys_xtrace
-1);
506 log_assert(modules_
.at(module
->name
) == module
);
507 modules_
.erase(module
->name
);
511 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
513 modules_
.erase(module
->name
);
514 module
->name
= new_name
;
518 void RTLIL::Design::sort()
521 modules_
.sort(sort_by_id_str());
522 for (auto &it
: modules_
)
526 void RTLIL::Design::check()
529 for (auto &it
: modules_
) {
530 log_assert(this == it
.second
->design
);
531 log_assert(it
.first
== it
.second
->name
);
532 log_assert(!it
.first
.empty());
538 void RTLIL::Design::optimize()
540 for (auto &it
: modules_
)
541 it
.second
->optimize();
542 for (auto &it
: selection_stack
)
544 for (auto &it
: selection_vars
)
545 it
.second
.optimize(this);
548 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
550 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
552 if (selection_stack
.size() == 0)
554 return selection_stack
.back().selected_module(mod_name
);
557 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
559 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
561 if (selection_stack
.size() == 0)
563 return selection_stack
.back().selected_whole_module(mod_name
);
566 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
568 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
570 if (selection_stack
.size() == 0)
572 return selection_stack
.back().selected_member(mod_name
, memb_name
);
575 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
577 return selected_module(mod
->name
);
580 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
582 return selected_whole_module(mod
->name
);
585 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
587 std::vector
<RTLIL::Module
*> result
;
588 result
.reserve(modules_
.size());
589 for (auto &it
: modules_
)
590 if (selected_module(it
.first
) && !it
.second
->get_bool_attribute("\\blackbox"))
591 result
.push_back(it
.second
);
595 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
597 std::vector
<RTLIL::Module
*> result
;
598 result
.reserve(modules_
.size());
599 for (auto &it
: modules_
)
600 if (selected_whole_module(it
.first
) && !it
.second
->get_bool_attribute("\\blackbox"))
601 result
.push_back(it
.second
);
605 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
607 std::vector
<RTLIL::Module
*> result
;
608 result
.reserve(modules_
.size());
609 for (auto &it
: modules_
)
610 if (it
.second
->get_bool_attribute("\\blackbox"))
612 else if (selected_whole_module(it
.first
))
613 result
.push_back(it
.second
);
614 else if (selected_module(it
.first
))
615 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
619 RTLIL::Module::Module()
621 static unsigned int hashidx_count
= 123456789;
622 hashidx_count
= mkhash_xorshift(hashidx_count
);
623 hashidx_
= hashidx_count
;
630 RTLIL::Module::~Module()
632 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
634 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
636 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
638 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
642 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, bool mayfail
)
645 return RTLIL::IdString();
646 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
649 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
651 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
656 struct InternalCellChecker
658 RTLIL::Module
*module
;
660 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
662 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
664 void error(int linenr
)
666 std::stringstream buf
;
667 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
669 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
670 module
? module
->name
.c_str() : "", module
? "." : "",
671 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
674 int param(const char *name
)
676 if (cell
->parameters
.count(name
) == 0)
678 expected_params
.insert(name
);
679 return cell
->parameters
.at(name
).as_int();
682 int param_bool(const char *name
)
685 if (cell
->parameters
.at(name
).bits
.size() > 32)
687 if (v
!= 0 && v
!= 1)
692 void param_bits(const char *name
, int width
)
695 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
699 void port(const char *name
, int width
)
701 if (!cell
->hasPort(name
))
703 if (cell
->getPort(name
).size() != width
)
705 expected_ports
.insert(name
);
708 void check_expected(bool check_matched_sign
= true)
710 for (auto ¶
: cell
->parameters
)
711 if (expected_params
.count(para
.first
) == 0)
713 for (auto &conn
: cell
->connections())
714 if (expected_ports
.count(conn
.first
) == 0)
717 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
718 bool a_is_signed
= param("\\A_SIGNED") != 0;
719 bool b_is_signed
= param("\\B_SIGNED") != 0;
720 if (a_is_signed
!= b_is_signed
)
725 void check_gate(const char *ports
)
727 if (cell
->parameters
.size() != 0)
730 for (const char *p
= ports
; *p
; p
++) {
731 char portname
[3] = { '\\', *p
, 0 };
732 if (!cell
->hasPort(portname
))
734 if (cell
->getPort(portname
).size() != 1)
738 for (auto &conn
: cell
->connections()) {
739 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
741 if (strchr(ports
, conn
.first
[1]) == NULL
)
748 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
749 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
752 if (cell
->type
.in("$not", "$pos", "$neg")) {
753 param_bool("\\A_SIGNED");
754 port("\\A", param("\\A_WIDTH"));
755 port("\\Y", param("\\Y_WIDTH"));
760 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
761 param_bool("\\A_SIGNED");
762 param_bool("\\B_SIGNED");
763 port("\\A", param("\\A_WIDTH"));
764 port("\\B", param("\\B_WIDTH"));
765 port("\\Y", param("\\Y_WIDTH"));
770 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
771 param_bool("\\A_SIGNED");
772 port("\\A", param("\\A_WIDTH"));
773 port("\\Y", param("\\Y_WIDTH"));
778 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
779 param_bool("\\A_SIGNED");
780 param_bool("\\B_SIGNED");
781 port("\\A", param("\\A_WIDTH"));
782 port("\\B", param("\\B_WIDTH"));
783 port("\\Y", param("\\Y_WIDTH"));
784 check_expected(false);
788 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
789 param_bool("\\A_SIGNED");
790 param_bool("\\B_SIGNED");
791 port("\\A", param("\\A_WIDTH"));
792 port("\\B", param("\\B_WIDTH"));
793 port("\\Y", param("\\Y_WIDTH"));
798 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
799 param_bool("\\A_SIGNED");
800 param_bool("\\B_SIGNED");
801 port("\\A", param("\\A_WIDTH"));
802 port("\\B", param("\\B_WIDTH"));
803 port("\\Y", param("\\Y_WIDTH"));
804 check_expected(cell
->type
!= "$pow");
808 if (cell
->type
== "$fa") {
809 port("\\A", param("\\WIDTH"));
810 port("\\B", param("\\WIDTH"));
811 port("\\C", param("\\WIDTH"));
812 port("\\X", param("\\WIDTH"));
813 port("\\Y", param("\\WIDTH"));
818 if (cell
->type
== "$lcu") {
819 port("\\P", param("\\WIDTH"));
820 port("\\G", param("\\WIDTH"));
822 port("\\CO", param("\\WIDTH"));
827 if (cell
->type
== "$alu") {
828 param_bool("\\A_SIGNED");
829 param_bool("\\B_SIGNED");
830 port("\\A", param("\\A_WIDTH"));
831 port("\\B", param("\\B_WIDTH"));
834 port("\\X", param("\\Y_WIDTH"));
835 port("\\Y", param("\\Y_WIDTH"));
836 port("\\CO", param("\\Y_WIDTH"));
841 if (cell
->type
== "$macc") {
843 param("\\CONFIG_WIDTH");
844 port("\\A", param("\\A_WIDTH"));
845 port("\\B", param("\\B_WIDTH"));
846 port("\\Y", param("\\Y_WIDTH"));
848 Macc().from_cell(cell
);
852 if (cell
->type
== "$logic_not") {
853 param_bool("\\A_SIGNED");
854 port("\\A", param("\\A_WIDTH"));
855 port("\\Y", param("\\Y_WIDTH"));
860 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
861 param_bool("\\A_SIGNED");
862 param_bool("\\B_SIGNED");
863 port("\\A", param("\\A_WIDTH"));
864 port("\\B", param("\\B_WIDTH"));
865 port("\\Y", param("\\Y_WIDTH"));
866 check_expected(false);
870 if (cell
->type
== "$slice") {
872 port("\\A", param("\\A_WIDTH"));
873 port("\\Y", param("\\Y_WIDTH"));
874 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
880 if (cell
->type
== "$concat") {
881 port("\\A", param("\\A_WIDTH"));
882 port("\\B", param("\\B_WIDTH"));
883 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
888 if (cell
->type
== "$mux") {
889 port("\\A", param("\\WIDTH"));
890 port("\\B", param("\\WIDTH"));
892 port("\\Y", param("\\WIDTH"));
897 if (cell
->type
== "$pmux") {
898 port("\\A", param("\\WIDTH"));
899 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
900 port("\\S", param("\\S_WIDTH"));
901 port("\\Y", param("\\WIDTH"));
906 if (cell
->type
== "$lut") {
908 port("\\A", param("\\WIDTH"));
914 if (cell
->type
== "$sop") {
917 port("\\A", param("\\WIDTH"));
923 if (cell
->type
== "$sr") {
924 param_bool("\\SET_POLARITY");
925 param_bool("\\CLR_POLARITY");
926 port("\\SET", param("\\WIDTH"));
927 port("\\CLR", param("\\WIDTH"));
928 port("\\Q", param("\\WIDTH"));
933 if (cell
->type
== "$ff") {
934 port("\\D", param("\\WIDTH"));
935 port("\\Q", param("\\WIDTH"));
940 if (cell
->type
== "$dff") {
941 param_bool("\\CLK_POLARITY");
943 port("\\D", param("\\WIDTH"));
944 port("\\Q", param("\\WIDTH"));
949 if (cell
->type
== "$dffe") {
950 param_bool("\\CLK_POLARITY");
951 param_bool("\\EN_POLARITY");
954 port("\\D", param("\\WIDTH"));
955 port("\\Q", param("\\WIDTH"));
960 if (cell
->type
== "$dffsr") {
961 param_bool("\\CLK_POLARITY");
962 param_bool("\\SET_POLARITY");
963 param_bool("\\CLR_POLARITY");
965 port("\\SET", param("\\WIDTH"));
966 port("\\CLR", param("\\WIDTH"));
967 port("\\D", param("\\WIDTH"));
968 port("\\Q", param("\\WIDTH"));
973 if (cell
->type
== "$adff") {
974 param_bool("\\CLK_POLARITY");
975 param_bool("\\ARST_POLARITY");
976 param_bits("\\ARST_VALUE", param("\\WIDTH"));
979 port("\\D", param("\\WIDTH"));
980 port("\\Q", param("\\WIDTH"));
985 if (cell
->type
== "$dlatch") {
986 param_bool("\\EN_POLARITY");
988 port("\\D", param("\\WIDTH"));
989 port("\\Q", param("\\WIDTH"));
994 if (cell
->type
== "$dlatchsr") {
995 param_bool("\\EN_POLARITY");
996 param_bool("\\SET_POLARITY");
997 param_bool("\\CLR_POLARITY");
999 port("\\SET", param("\\WIDTH"));
1000 port("\\CLR", param("\\WIDTH"));
1001 port("\\D", param("\\WIDTH"));
1002 port("\\Q", param("\\WIDTH"));
1007 if (cell
->type
== "$fsm") {
1009 param_bool("\\CLK_POLARITY");
1010 param_bool("\\ARST_POLARITY");
1011 param("\\STATE_BITS");
1012 param("\\STATE_NUM");
1013 param("\\STATE_NUM_LOG2");
1014 param("\\STATE_RST");
1015 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
1016 param("\\TRANS_NUM");
1017 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
1020 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
1021 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
1026 if (cell
->type
== "$memrd") {
1028 param_bool("\\CLK_ENABLE");
1029 param_bool("\\CLK_POLARITY");
1030 param_bool("\\TRANSPARENT");
1033 port("\\ADDR", param("\\ABITS"));
1034 port("\\DATA", param("\\WIDTH"));
1039 if (cell
->type
== "$memwr") {
1041 param_bool("\\CLK_ENABLE");
1042 param_bool("\\CLK_POLARITY");
1043 param("\\PRIORITY");
1045 port("\\EN", param("\\WIDTH"));
1046 port("\\ADDR", param("\\ABITS"));
1047 port("\\DATA", param("\\WIDTH"));
1052 if (cell
->type
== "$meminit") {
1054 param("\\PRIORITY");
1055 port("\\ADDR", param("\\ABITS"));
1056 port("\\DATA", param("\\WIDTH") * param("\\WORDS"));
1061 if (cell
->type
== "$mem") {
1066 param_bits("\\RD_CLK_ENABLE", max(1, param("\\RD_PORTS")));
1067 param_bits("\\RD_CLK_POLARITY", max(1, param("\\RD_PORTS")));
1068 param_bits("\\RD_TRANSPARENT", max(1, param("\\RD_PORTS")));
1069 param_bits("\\WR_CLK_ENABLE", max(1, param("\\WR_PORTS")));
1070 param_bits("\\WR_CLK_POLARITY", max(1, param("\\WR_PORTS")));
1071 port("\\RD_CLK", param("\\RD_PORTS"));
1072 port("\\RD_EN", param("\\RD_PORTS"));
1073 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
1074 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
1075 port("\\WR_CLK", param("\\WR_PORTS"));
1076 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
1077 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
1078 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
1083 if (cell
->type
== "$tribuf") {
1084 port("\\A", param("\\WIDTH"));
1085 port("\\Y", param("\\WIDTH"));
1091 if (cell
->type
.in("$assert", "$assume", "$live", "$fair", "$cover")) {
1098 if (cell
->type
== "$initstate") {
1104 if (cell
->type
.in("$anyconst", "$anyseq", "$allconst", "$allseq")) {
1105 port("\\Y", param("\\WIDTH"));
1110 if (cell
->type
== "$equiv") {
1118 if (cell
->type
== "$_BUF_") { check_gate("AY"); return; }
1119 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
1120 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
1121 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
1122 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
1123 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
1124 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
1125 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
1126 if (cell
->type
== "$_ANDNOT_") { check_gate("ABY"); return; }
1127 if (cell
->type
== "$_ORNOT_") { check_gate("ABY"); return; }
1128 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
1129 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
1130 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
1131 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
1132 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
1134 if (cell
->type
== "$_TBUF_") { check_gate("AYE"); return; }
1136 if (cell
->type
== "$_MUX4_") { check_gate("ABCDSTY"); return; }
1137 if (cell
->type
== "$_MUX8_") { check_gate("ABCDEFGHSTUY"); return; }
1138 if (cell
->type
== "$_MUX16_") { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1140 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
1141 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
1142 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
1143 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
1145 if (cell
->type
== "$_FF_") { check_gate("DQ"); return; }
1146 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
1147 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
1149 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
1150 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
1151 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
1152 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
1154 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
1155 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
1156 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
1157 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
1158 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
1159 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
1160 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
1161 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
1163 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
1164 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
1165 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
1166 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
1167 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
1168 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
1169 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
1170 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
1172 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
1173 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
1175 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
1176 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
1177 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
1178 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
1179 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
1180 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
1181 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
1182 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
1190 void RTLIL::Module::sort()
1192 wires_
.sort(sort_by_id_str());
1193 cells_
.sort(sort_by_id_str());
1194 avail_parameters
.sort(sort_by_id_str());
1195 memories
.sort(sort_by_id_str());
1196 processes
.sort(sort_by_id_str());
1197 for (auto &it
: cells_
)
1199 for (auto &it
: wires_
)
1200 it
.second
->attributes
.sort(sort_by_id_str());
1201 for (auto &it
: memories
)
1202 it
.second
->attributes
.sort(sort_by_id_str());
1205 void RTLIL::Module::check()
1208 std::vector
<bool> ports_declared
;
1209 for (auto &it
: wires_
) {
1210 log_assert(this == it
.second
->module
);
1211 log_assert(it
.first
== it
.second
->name
);
1212 log_assert(!it
.first
.empty());
1213 log_assert(it
.second
->width
>= 0);
1214 log_assert(it
.second
->port_id
>= 0);
1215 for (auto &it2
: it
.second
->attributes
)
1216 log_assert(!it2
.first
.empty());
1217 if (it
.second
->port_id
) {
1218 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1219 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1220 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1221 if (GetSize(ports_declared
) < it
.second
->port_id
)
1222 ports_declared
.resize(it
.second
->port_id
);
1223 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1224 ports_declared
[it
.second
->port_id
-1] = true;
1226 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1228 for (auto port_declared
: ports_declared
)
1229 log_assert(port_declared
== true);
1230 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1232 for (auto &it
: memories
) {
1233 log_assert(it
.first
== it
.second
->name
);
1234 log_assert(!it
.first
.empty());
1235 log_assert(it
.second
->width
>= 0);
1236 log_assert(it
.second
->size
>= 0);
1237 for (auto &it2
: it
.second
->attributes
)
1238 log_assert(!it2
.first
.empty());
1241 for (auto &it
: cells_
) {
1242 log_assert(this == it
.second
->module
);
1243 log_assert(it
.first
== it
.second
->name
);
1244 log_assert(!it
.first
.empty());
1245 log_assert(!it
.second
->type
.empty());
1246 for (auto &it2
: it
.second
->connections()) {
1247 log_assert(!it2
.first
.empty());
1250 for (auto &it2
: it
.second
->attributes
)
1251 log_assert(!it2
.first
.empty());
1252 for (auto &it2
: it
.second
->parameters
)
1253 log_assert(!it2
.first
.empty());
1254 InternalCellChecker
checker(this, it
.second
);
1258 for (auto &it
: processes
) {
1259 log_assert(it
.first
== it
.second
->name
);
1260 log_assert(!it
.first
.empty());
1261 // FIXME: More checks here..
1264 for (auto &it
: connections_
) {
1265 log_assert(it
.first
.size() == it
.second
.size());
1266 log_assert(!it
.first
.has_const());
1271 for (auto &it
: attributes
)
1272 log_assert(!it
.first
.empty());
1276 void RTLIL::Module::optimize()
1280 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1282 log_assert(new_mod
->refcount_wires_
== 0);
1283 log_assert(new_mod
->refcount_cells_
== 0);
1285 new_mod
->avail_parameters
= avail_parameters
;
1287 for (auto &conn
: connections_
)
1288 new_mod
->connect(conn
);
1290 for (auto &attr
: attributes
)
1291 new_mod
->attributes
[attr
.first
] = attr
.second
;
1293 for (auto &it
: wires_
)
1294 new_mod
->addWire(it
.first
, it
.second
);
1296 for (auto &it
: memories
)
1297 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1299 for (auto &it
: cells_
)
1300 new_mod
->addCell(it
.first
, it
.second
);
1302 for (auto &it
: processes
)
1303 new_mod
->processes
[it
.first
] = it
.second
->clone();
1305 struct RewriteSigSpecWorker
1308 void operator()(RTLIL::SigSpec
&sig
)
1310 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1311 for (auto &c
: chunks
)
1313 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1318 RewriteSigSpecWorker rewriteSigSpecWorker
;
1319 rewriteSigSpecWorker
.mod
= new_mod
;
1320 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1321 new_mod
->fixup_ports();
1324 RTLIL::Module
*RTLIL::Module::clone() const
1326 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1327 new_mod
->name
= name
;
1332 bool RTLIL::Module::has_memories() const
1334 return !memories
.empty();
1337 bool RTLIL::Module::has_processes() const
1339 return !processes
.empty();
1342 bool RTLIL::Module::has_memories_warn() const
1344 if (!memories
.empty())
1345 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1346 return !memories
.empty();
1349 bool RTLIL::Module::has_processes_warn() const
1351 if (!processes
.empty())
1352 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1353 return !processes
.empty();
1356 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1358 std::vector
<RTLIL::Wire
*> result
;
1359 result
.reserve(wires_
.size());
1360 for (auto &it
: wires_
)
1361 if (design
->selected(this, it
.second
))
1362 result
.push_back(it
.second
);
1366 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1368 std::vector
<RTLIL::Cell
*> result
;
1369 result
.reserve(wires_
.size());
1370 for (auto &it
: cells_
)
1371 if (design
->selected(this, it
.second
))
1372 result
.push_back(it
.second
);
1376 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1378 log_assert(!wire
->name
.empty());
1379 log_assert(count_id(wire
->name
) == 0);
1380 log_assert(refcount_wires_
== 0);
1381 wires_
[wire
->name
] = wire
;
1382 wire
->module
= this;
1385 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1387 log_assert(!cell
->name
.empty());
1388 log_assert(count_id(cell
->name
) == 0);
1389 log_assert(refcount_cells_
== 0);
1390 cells_
[cell
->name
] = cell
;
1391 cell
->module
= this;
1395 struct DeleteWireWorker
1397 RTLIL::Module
*module
;
1398 const pool
<RTLIL::Wire
*> *wires_p
;
1400 void operator()(RTLIL::SigSpec
&sig
) {
1401 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1402 for (auto &c
: chunks
)
1403 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1404 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1412 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1414 log_assert(refcount_wires_
== 0);
1416 DeleteWireWorker delete_wire_worker
;
1417 delete_wire_worker
.module
= this;
1418 delete_wire_worker
.wires_p
= &wires
;
1419 rewrite_sigspecs(delete_wire_worker
);
1421 for (auto &it
: wires
) {
1422 log_assert(wires_
.count(it
->name
) != 0);
1423 wires_
.erase(it
->name
);
1428 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1430 while (!cell
->connections_
.empty())
1431 cell
->unsetPort(cell
->connections_
.begin()->first
);
1433 log_assert(cells_
.count(cell
->name
) != 0);
1434 log_assert(refcount_cells_
== 0);
1435 cells_
.erase(cell
->name
);
1439 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1441 log_assert(wires_
[wire
->name
] == wire
);
1442 log_assert(refcount_wires_
== 0);
1443 wires_
.erase(wire
->name
);
1444 wire
->name
= new_name
;
1448 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1450 log_assert(cells_
[cell
->name
] == cell
);
1451 log_assert(refcount_wires_
== 0);
1452 cells_
.erase(cell
->name
);
1453 cell
->name
= new_name
;
1457 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1459 log_assert(count_id(old_name
) != 0);
1460 if (wires_
.count(old_name
))
1461 rename(wires_
.at(old_name
), new_name
);
1462 else if (cells_
.count(old_name
))
1463 rename(cells_
.at(old_name
), new_name
);
1468 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1470 log_assert(wires_
[w1
->name
] == w1
);
1471 log_assert(wires_
[w2
->name
] == w2
);
1472 log_assert(refcount_wires_
== 0);
1474 wires_
.erase(w1
->name
);
1475 wires_
.erase(w2
->name
);
1477 std::swap(w1
->name
, w2
->name
);
1479 wires_
[w1
->name
] = w1
;
1480 wires_
[w2
->name
] = w2
;
1483 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1485 log_assert(cells_
[c1
->name
] == c1
);
1486 log_assert(cells_
[c2
->name
] == c2
);
1487 log_assert(refcount_cells_
== 0);
1489 cells_
.erase(c1
->name
);
1490 cells_
.erase(c2
->name
);
1492 std::swap(c1
->name
, c2
->name
);
1494 cells_
[c1
->name
] = c1
;
1495 cells_
[c2
->name
] = c2
;
1498 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1501 return uniquify(name
, index
);
1504 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1507 if (count_id(name
) == 0)
1513 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1514 if (count_id(new_name
) == 0)
1520 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1522 if (a
->port_id
&& !b
->port_id
)
1524 if (!a
->port_id
&& b
->port_id
)
1527 if (a
->port_id
== b
->port_id
)
1528 return a
->name
< b
->name
;
1529 return a
->port_id
< b
->port_id
;
1532 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1534 for (auto mon
: monitors
)
1535 mon
->notify_connect(this, conn
);
1538 for (auto mon
: design
->monitors
)
1539 mon
->notify_connect(this, conn
);
1541 // ignore all attempts to assign constants to other constants
1542 if (conn
.first
.has_const()) {
1543 RTLIL::SigSig new_conn
;
1544 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1545 if (conn
.first
[i
].wire
) {
1546 new_conn
.first
.append(conn
.first
[i
]);
1547 new_conn
.second
.append(conn
.second
[i
]);
1549 if (GetSize(new_conn
.first
))
1555 log("#X# Connect (SigSig) in %s: %s = %s (%d bits)\n", log_id(this), log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1556 log_backtrace("-X- ", yosys_xtrace
-1);
1559 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1560 connections_
.push_back(conn
);
1563 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1565 connect(RTLIL::SigSig(lhs
, rhs
));
1568 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1570 for (auto mon
: monitors
)
1571 mon
->notify_connect(this, new_conn
);
1574 for (auto mon
: design
->monitors
)
1575 mon
->notify_connect(this, new_conn
);
1578 log("#X# New connections vector in %s:\n", log_id(this));
1579 for (auto &conn
: new_conn
)
1580 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1581 log_backtrace("-X- ", yosys_xtrace
-1);
1584 connections_
= new_conn
;
1587 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1589 return connections_
;
1592 void RTLIL::Module::fixup_ports()
1594 std::vector
<RTLIL::Wire
*> all_ports
;
1596 for (auto &w
: wires_
)
1597 if (w
.second
->port_input
|| w
.second
->port_output
)
1598 all_ports
.push_back(w
.second
);
1600 w
.second
->port_id
= 0;
1602 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1605 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1606 ports
.push_back(all_ports
[i
]->name
);
1607 all_ports
[i
]->port_id
= i
+1;
1611 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1613 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1615 wire
->width
= width
;
1620 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1622 RTLIL::Wire
*wire
= addWire(name
);
1623 wire
->width
= other
->width
;
1624 wire
->start_offset
= other
->start_offset
;
1625 wire
->port_id
= other
->port_id
;
1626 wire
->port_input
= other
->port_input
;
1627 wire
->port_output
= other
->port_output
;
1628 wire
->upto
= other
->upto
;
1629 wire
->attributes
= other
->attributes
;
1633 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1635 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1642 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1644 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1645 cell
->connections_
= other
->connections_
;
1646 cell
->parameters
= other
->parameters
;
1647 cell
->attributes
= other
->attributes
;
1651 #define DEF_METHOD(_func, _y_size, _type) \
1652 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1653 RTLIL::Cell *cell = addCell(name, _type); \
1654 cell->parameters["\\A_SIGNED"] = is_signed; \
1655 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1656 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1657 cell->setPort("\\A", sig_a); \
1658 cell->setPort("\\Y", sig_y); \
1659 cell->set_src_attribute(src); \
1662 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1663 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1664 add ## _func(name, sig_a, sig_y, is_signed, src); \
1667 DEF_METHOD(Not
, sig_a
.size(), "$not")
1668 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1669 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1670 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1671 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1672 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1673 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1674 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1675 DEF_METHOD(LogicNot
, 1, "$logic_not")
1678 #define DEF_METHOD(_func, _y_size, _type) \
1679 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1680 RTLIL::Cell *cell = addCell(name, _type); \
1681 cell->parameters["\\A_SIGNED"] = is_signed; \
1682 cell->parameters["\\B_SIGNED"] = is_signed; \
1683 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1684 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1685 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1686 cell->setPort("\\A", sig_a); \
1687 cell->setPort("\\B", sig_b); \
1688 cell->setPort("\\Y", sig_y); \
1689 cell->set_src_attribute(src); \
1692 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1693 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1694 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1697 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), "$and")
1698 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), "$or")
1699 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), "$xor")
1700 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), "$xnor")
1701 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1702 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1703 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1704 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1705 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1706 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1707 DEF_METHOD(Lt
, 1, "$lt")
1708 DEF_METHOD(Le
, 1, "$le")
1709 DEF_METHOD(Eq
, 1, "$eq")
1710 DEF_METHOD(Ne
, 1, "$ne")
1711 DEF_METHOD(Eqx
, 1, "$eqx")
1712 DEF_METHOD(Nex
, 1, "$nex")
1713 DEF_METHOD(Ge
, 1, "$ge")
1714 DEF_METHOD(Gt
, 1, "$gt")
1715 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), "$add")
1716 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), "$sub")
1717 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), "$mul")
1718 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), "$div")
1719 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), "$mod")
1720 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1721 DEF_METHOD(LogicOr
, 1, "$logic_or")
1724 #define DEF_METHOD(_func, _type, _pmux) \
1725 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, const std::string &src) { \
1726 RTLIL::Cell *cell = addCell(name, _type); \
1727 cell->parameters["\\WIDTH"] = sig_a.size(); \
1728 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1729 cell->setPort("\\A", sig_a); \
1730 cell->setPort("\\B", sig_b); \
1731 cell->setPort("\\S", sig_s); \
1732 cell->setPort("\\Y", sig_y); \
1733 cell->set_src_attribute(src); \
1736 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1737 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1738 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1741 DEF_METHOD(Mux
, "$mux", 0)
1742 DEF_METHOD(Pmux
, "$pmux", 1)
1745 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1746 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1747 RTLIL::Cell *cell = addCell(name, _type); \
1748 cell->setPort("\\" #_P1, sig1); \
1749 cell->setPort("\\" #_P2, sig2); \
1750 cell->set_src_attribute(src); \
1753 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1754 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1755 add ## _func(name, sig1, sig2, src); \
1758 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1759 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1760 RTLIL::Cell *cell = addCell(name, _type); \
1761 cell->setPort("\\" #_P1, sig1); \
1762 cell->setPort("\\" #_P2, sig2); \
1763 cell->setPort("\\" #_P3, sig3); \
1764 cell->set_src_attribute(src); \
1767 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1768 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1769 add ## _func(name, sig1, sig2, sig3, src); \
1772 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1773 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1774 RTLIL::Cell *cell = addCell(name, _type); \
1775 cell->setPort("\\" #_P1, sig1); \
1776 cell->setPort("\\" #_P2, sig2); \
1777 cell->setPort("\\" #_P3, sig3); \
1778 cell->setPort("\\" #_P4, sig4); \
1779 cell->set_src_attribute(src); \
1782 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1783 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1784 add ## _func(name, sig1, sig2, sig3, sig4, src); \
1787 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1788 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5, const std::string &src) { \
1789 RTLIL::Cell *cell = addCell(name, _type); \
1790 cell->setPort("\\" #_P1, sig1); \
1791 cell->setPort("\\" #_P2, sig2); \
1792 cell->setPort("\\" #_P3, sig3); \
1793 cell->setPort("\\" #_P4, sig4); \
1794 cell->setPort("\\" #_P5, sig5); \
1795 cell->set_src_attribute(src); \
1798 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1799 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1800 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
1803 DEF_METHOD_2(BufGate
, "$_BUF_", A
, Y
)
1804 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1805 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1806 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1807 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1808 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1809 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1810 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1811 DEF_METHOD_3(AndnotGate
, "$_ANDNOT_", A
, B
, Y
)
1812 DEF_METHOD_3(OrnotGate
, "$_ORNOT_", A
, B
, Y
)
1813 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1814 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1815 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1816 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1817 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1823 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
, const std::string
&src
)
1825 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1826 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1827 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1828 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1829 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1830 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1831 cell
->setPort("\\A", sig_a
);
1832 cell
->setPort("\\B", sig_b
);
1833 cell
->setPort("\\Y", sig_y
);
1834 cell
->set_src_attribute(src
);
1838 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
1840 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1841 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1842 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1843 cell
->parameters
["\\OFFSET"] = offset
;
1844 cell
->setPort("\\A", sig_a
);
1845 cell
->setPort("\\Y", sig_y
);
1846 cell
->set_src_attribute(src
);
1850 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1852 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1853 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1854 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1855 cell
->setPort("\\A", sig_a
);
1856 cell
->setPort("\\B", sig_b
);
1857 cell
->setPort("\\Y", sig_y
);
1858 cell
->set_src_attribute(src
);
1862 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
1864 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1865 cell
->parameters
["\\LUT"] = lut
;
1866 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1867 cell
->setPort("\\A", sig_a
);
1868 cell
->setPort("\\Y", sig_y
);
1869 cell
->set_src_attribute(src
);
1873 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1875 RTLIL::Cell
*cell
= addCell(name
, "$tribuf");
1876 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1877 cell
->setPort("\\A", sig_a
);
1878 cell
->setPort("\\EN", sig_en
);
1879 cell
->setPort("\\Y", sig_y
);
1880 cell
->set_src_attribute(src
);
1884 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1886 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1887 cell
->setPort("\\A", sig_a
);
1888 cell
->setPort("\\EN", sig_en
);
1889 cell
->set_src_attribute(src
);
1893 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1895 RTLIL::Cell
*cell
= addCell(name
, "$assume");
1896 cell
->setPort("\\A", sig_a
);
1897 cell
->setPort("\\EN", sig_en
);
1898 cell
->set_src_attribute(src
);
1902 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1904 RTLIL::Cell
*cell
= addCell(name
, "$live");
1905 cell
->setPort("\\A", sig_a
);
1906 cell
->setPort("\\EN", sig_en
);
1907 cell
->set_src_attribute(src
);
1911 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1913 RTLIL::Cell
*cell
= addCell(name
, "$fair");
1914 cell
->setPort("\\A", sig_a
);
1915 cell
->setPort("\\EN", sig_en
);
1916 cell
->set_src_attribute(src
);
1920 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1922 RTLIL::Cell
*cell
= addCell(name
, "$cover");
1923 cell
->setPort("\\A", sig_a
);
1924 cell
->setPort("\\EN", sig_en
);
1925 cell
->set_src_attribute(src
);
1929 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1931 RTLIL::Cell
*cell
= addCell(name
, "$equiv");
1932 cell
->setPort("\\A", sig_a
);
1933 cell
->setPort("\\B", sig_b
);
1934 cell
->setPort("\\Y", sig_y
);
1935 cell
->set_src_attribute(src
);
1939 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
, const std::string
&src
)
1941 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1942 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1943 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1944 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1945 cell
->setPort("\\SET", sig_set
);
1946 cell
->setPort("\\CLR", sig_clr
);
1947 cell
->setPort("\\Q", sig_q
);
1948 cell
->set_src_attribute(src
);
1952 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
1954 RTLIL::Cell
*cell
= addCell(name
, "$ff");
1955 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1956 cell
->setPort("\\D", sig_d
);
1957 cell
->setPort("\\Q", sig_q
);
1958 cell
->set_src_attribute(src
);
1962 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, const std::string
&src
)
1964 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1965 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1966 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1967 cell
->setPort("\\CLK", sig_clk
);
1968 cell
->setPort("\\D", sig_d
);
1969 cell
->setPort("\\Q", sig_q
);
1970 cell
->set_src_attribute(src
);
1974 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
, const std::string
&src
)
1976 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
1977 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1978 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1979 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1980 cell
->setPort("\\CLK", sig_clk
);
1981 cell
->setPort("\\EN", sig_en
);
1982 cell
->setPort("\\D", sig_d
);
1983 cell
->setPort("\\Q", sig_q
);
1984 cell
->set_src_attribute(src
);
1988 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1989 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
1991 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1992 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1993 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1994 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1995 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1996 cell
->setPort("\\CLK", sig_clk
);
1997 cell
->setPort("\\SET", sig_set
);
1998 cell
->setPort("\\CLR", sig_clr
);
1999 cell
->setPort("\\D", sig_d
);
2000 cell
->setPort("\\Q", sig_q
);
2001 cell
->set_src_attribute(src
);
2005 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2006 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2008 RTLIL::Cell
*cell
= addCell(name
, "$adff");
2009 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2010 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
2011 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
2012 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2013 cell
->setPort("\\CLK", sig_clk
);
2014 cell
->setPort("\\ARST", sig_arst
);
2015 cell
->setPort("\\D", sig_d
);
2016 cell
->setPort("\\Q", sig_q
);
2017 cell
->set_src_attribute(src
);
2021 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, const std::string
&src
)
2023 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
2024 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2025 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2026 cell
->setPort("\\EN", sig_en
);
2027 cell
->setPort("\\D", sig_d
);
2028 cell
->setPort("\\Q", sig_q
);
2029 cell
->set_src_attribute(src
);
2033 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2034 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2036 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
2037 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2038 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2039 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2040 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2041 cell
->setPort("\\EN", sig_en
);
2042 cell
->setPort("\\SET", sig_set
);
2043 cell
->setPort("\\CLR", sig_clr
);
2044 cell
->setPort("\\D", sig_d
);
2045 cell
->setPort("\\Q", sig_q
);
2046 cell
->set_src_attribute(src
);
2050 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2052 RTLIL::Cell
*cell
= addCell(name
, "$_FF_");
2053 cell
->setPort("\\D", sig_d
);
2054 cell
->setPort("\\Q", sig_q
);
2055 cell
->set_src_attribute(src
);
2059 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, const std::string
&src
)
2061 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2062 cell
->setPort("\\C", sig_clk
);
2063 cell
->setPort("\\D", sig_d
);
2064 cell
->setPort("\\Q", sig_q
);
2065 cell
->set_src_attribute(src
);
2069 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
, const std::string
&src
)
2071 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2072 cell
->setPort("\\C", sig_clk
);
2073 cell
->setPort("\\E", sig_en
);
2074 cell
->setPort("\\D", sig_d
);
2075 cell
->setPort("\\Q", sig_q
);
2076 cell
->set_src_attribute(src
);
2080 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2081 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2083 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2084 cell
->setPort("\\C", sig_clk
);
2085 cell
->setPort("\\S", sig_set
);
2086 cell
->setPort("\\R", sig_clr
);
2087 cell
->setPort("\\D", sig_d
);
2088 cell
->setPort("\\Q", sig_q
);
2089 cell
->set_src_attribute(src
);
2093 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2094 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2096 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2097 cell
->setPort("\\C", sig_clk
);
2098 cell
->setPort("\\R", sig_arst
);
2099 cell
->setPort("\\D", sig_d
);
2100 cell
->setPort("\\Q", sig_q
);
2101 cell
->set_src_attribute(src
);
2105 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, const std::string
&src
)
2107 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2108 cell
->setPort("\\E", sig_en
);
2109 cell
->setPort("\\D", sig_d
);
2110 cell
->setPort("\\Q", sig_q
);
2111 cell
->set_src_attribute(src
);
2115 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2116 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2118 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2119 cell
->setPort("\\E", sig_en
);
2120 cell
->setPort("\\S", sig_set
);
2121 cell
->setPort("\\R", sig_clr
);
2122 cell
->setPort("\\D", sig_d
);
2123 cell
->setPort("\\Q", sig_q
);
2124 cell
->set_src_attribute(src
);
2128 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2130 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2131 Cell
*cell
= addCell(name
, "$anyconst");
2132 cell
->setParam("\\WIDTH", width
);
2133 cell
->setPort("\\Y", sig
);
2134 cell
->set_src_attribute(src
);
2138 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2140 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2141 Cell
*cell
= addCell(name
, "$anyseq");
2142 cell
->setParam("\\WIDTH", width
);
2143 cell
->setPort("\\Y", sig
);
2144 cell
->set_src_attribute(src
);
2148 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2150 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2151 Cell
*cell
= addCell(name
, "$allconst");
2152 cell
->setParam("\\WIDTH", width
);
2153 cell
->setPort("\\Y", sig
);
2154 cell
->set_src_attribute(src
);
2158 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2160 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2161 Cell
*cell
= addCell(name
, "$allseq");
2162 cell
->setParam("\\WIDTH", width
);
2163 cell
->setPort("\\Y", sig
);
2164 cell
->set_src_attribute(src
);
2168 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2170 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2171 Cell
*cell
= addCell(name
, "$initstate");
2172 cell
->setPort("\\Y", sig
);
2173 cell
->set_src_attribute(src
);
2179 static unsigned int hashidx_count
= 123456789;
2180 hashidx_count
= mkhash_xorshift(hashidx_count
);
2181 hashidx_
= hashidx_count
;
2188 port_output
= false;
2192 RTLIL::Memory::Memory()
2194 static unsigned int hashidx_count
= 123456789;
2195 hashidx_count
= mkhash_xorshift(hashidx_count
);
2196 hashidx_
= hashidx_count
;
2203 RTLIL::Cell::Cell() : module(nullptr)
2205 static unsigned int hashidx_count
= 123456789;
2206 hashidx_count
= mkhash_xorshift(hashidx_count
);
2207 hashidx_
= hashidx_count
;
2209 // log("#memtrace# %p\n", this);
2213 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2215 return connections_
.count(portname
) != 0;
2218 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2220 RTLIL::SigSpec signal
;
2221 auto conn_it
= connections_
.find(portname
);
2223 if (conn_it
!= connections_
.end())
2225 for (auto mon
: module
->monitors
)
2226 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2229 for (auto mon
: module
->design
->monitors
)
2230 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2233 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2234 log_backtrace("-X- ", yosys_xtrace
-1);
2237 connections_
.erase(conn_it
);
2241 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2243 auto conn_it
= connections_
.find(portname
);
2245 if (conn_it
== connections_
.end()) {
2246 connections_
[portname
] = RTLIL::SigSpec();
2247 conn_it
= connections_
.find(portname
);
2248 log_assert(conn_it
!= connections_
.end());
2250 if (conn_it
->second
== signal
)
2253 for (auto mon
: module
->monitors
)
2254 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2257 for (auto mon
: module
->design
->monitors
)
2258 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2261 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2262 log_backtrace("-X- ", yosys_xtrace
-1);
2265 conn_it
->second
= signal
;
2268 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2270 return connections_
.at(portname
);
2273 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2275 return connections_
;
2278 bool RTLIL::Cell::known() const
2280 if (yosys_celltypes
.cell_known(type
))
2282 if (module
&& module
->design
&& module
->design
->module(type
))
2287 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2289 if (yosys_celltypes
.cell_known(type
))
2290 return yosys_celltypes
.cell_input(type
, portname
);
2291 if (module
&& module
->design
) {
2292 RTLIL::Module
*m
= module
->design
->module(type
);
2293 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2294 return w
&& w
->port_input
;
2299 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2301 if (yosys_celltypes
.cell_known(type
))
2302 return yosys_celltypes
.cell_output(type
, portname
);
2303 if (module
&& module
->design
) {
2304 RTLIL::Module
*m
= module
->design
->module(type
);
2305 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2306 return w
&& w
->port_output
;
2311 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2313 return parameters
.count(paramname
) != 0;
2316 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2318 parameters
.erase(paramname
);
2321 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2323 parameters
[paramname
] = value
;
2326 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2328 return parameters
.at(paramname
);
2331 void RTLIL::Cell::sort()
2333 connections_
.sort(sort_by_id_str());
2334 parameters
.sort(sort_by_id_str());
2335 attributes
.sort(sort_by_id_str());
2338 void RTLIL::Cell::check()
2341 InternalCellChecker
checker(NULL
, this);
2346 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2348 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
2349 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
2352 if (type
== "$mux" || type
== "$pmux") {
2353 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2354 if (type
== "$pmux")
2355 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
2360 if (type
== "$lut" || type
== "$sop") {
2361 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
2365 if (type
== "$fa") {
2366 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2370 if (type
== "$lcu") {
2371 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
2375 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
2377 if (connections_
.count("\\A")) {
2378 if (signedness_ab
) {
2380 parameters
["\\A_SIGNED"] = true;
2381 else if (parameters
.count("\\A_SIGNED") == 0)
2382 parameters
["\\A_SIGNED"] = false;
2384 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
2387 if (connections_
.count("\\B")) {
2388 if (signedness_ab
) {
2390 parameters
["\\B_SIGNED"] = true;
2391 else if (parameters
.count("\\B_SIGNED") == 0)
2392 parameters
["\\B_SIGNED"] = false;
2394 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
2397 if (connections_
.count("\\Y"))
2398 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
2403 RTLIL::SigChunk::SigChunk()
2410 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2414 width
= GetSize(data
);
2418 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2420 log_assert(wire
!= nullptr);
2422 this->width
= wire
->width
;
2426 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2428 log_assert(wire
!= nullptr);
2430 this->width
= width
;
2431 this->offset
= offset
;
2434 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2437 data
= RTLIL::Const(str
).bits
;
2438 width
= GetSize(data
);
2442 RTLIL::SigChunk::SigChunk(int val
, int width
)
2445 data
= RTLIL::Const(val
, width
).bits
;
2446 this->width
= GetSize(data
);
2450 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2453 data
= RTLIL::Const(bit
, width
).bits
;
2454 this->width
= GetSize(data
);
2458 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2463 data
= RTLIL::Const(bit
.data
).bits
;
2465 offset
= bit
.offset
;
2469 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2471 RTLIL::SigChunk ret
;
2474 ret
.offset
= this->offset
+ offset
;
2477 for (int i
= 0; i
< length
; i
++)
2478 ret
.data
.push_back(data
[offset
+i
]);
2484 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2486 if (wire
&& other
.wire
)
2487 if (wire
->name
!= other
.wire
->name
)
2488 return wire
->name
< other
.wire
->name
;
2490 if (wire
!= other
.wire
)
2491 return wire
< other
.wire
;
2493 if (offset
!= other
.offset
)
2494 return offset
< other
.offset
;
2496 if (width
!= other
.width
)
2497 return width
< other
.width
;
2499 return data
< other
.data
;
2502 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2504 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2507 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2514 RTLIL::SigSpec::SigSpec()
2520 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2525 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2527 cover("kernel.rtlil.sigspec.init.list");
2532 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2533 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2537 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2539 cover("kernel.rtlil.sigspec.assign");
2541 width_
= other
.width_
;
2542 hash_
= other
.hash_
;
2543 chunks_
= other
.chunks_
;
2546 if (!other
.bits_
.empty())
2548 RTLIL::SigChunk
*last
= NULL
;
2549 int last_end_offset
= 0;
2551 for (auto &bit
: other
.bits_
) {
2552 if (last
&& bit
.wire
== last
->wire
) {
2553 if (bit
.wire
== NULL
) {
2554 last
->data
.push_back(bit
.data
);
2557 } else if (last_end_offset
== bit
.offset
) {
2563 chunks_
.push_back(bit
);
2564 last
= &chunks_
.back();
2565 last_end_offset
= bit
.offset
+ 1;
2574 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2576 cover("kernel.rtlil.sigspec.init.const");
2578 chunks_
.push_back(RTLIL::SigChunk(value
));
2579 width_
= chunks_
.back().width
;
2584 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2586 cover("kernel.rtlil.sigspec.init.chunk");
2588 chunks_
.push_back(chunk
);
2589 width_
= chunks_
.back().width
;
2594 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2596 cover("kernel.rtlil.sigspec.init.wire");
2598 chunks_
.push_back(RTLIL::SigChunk(wire
));
2599 width_
= chunks_
.back().width
;
2604 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2606 cover("kernel.rtlil.sigspec.init.wire_part");
2608 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2609 width_
= chunks_
.back().width
;
2614 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2616 cover("kernel.rtlil.sigspec.init.str");
2618 chunks_
.push_back(RTLIL::SigChunk(str
));
2619 width_
= chunks_
.back().width
;
2624 RTLIL::SigSpec::SigSpec(int val
, int width
)
2626 cover("kernel.rtlil.sigspec.init.int");
2628 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2634 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2636 cover("kernel.rtlil.sigspec.init.state");
2638 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2644 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2646 cover("kernel.rtlil.sigspec.init.bit");
2648 if (bit
.wire
== NULL
)
2649 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2651 for (int i
= 0; i
< width
; i
++)
2652 chunks_
.push_back(bit
);
2658 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2660 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2664 for (auto &c
: chunks
)
2669 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2671 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2675 for (auto &bit
: bits
)
2680 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2682 cover("kernel.rtlil.sigspec.init.pool_bits");
2686 for (auto &bit
: bits
)
2691 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2693 cover("kernel.rtlil.sigspec.init.stdset_bits");
2697 for (auto &bit
: bits
)
2702 RTLIL::SigSpec::SigSpec(bool bit
)
2704 cover("kernel.rtlil.sigspec.init.bool");
2712 void RTLIL::SigSpec::pack() const
2714 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2716 if (that
->bits_
.empty())
2719 cover("kernel.rtlil.sigspec.convert.pack");
2720 log_assert(that
->chunks_
.empty());
2722 std::vector
<RTLIL::SigBit
> old_bits
;
2723 old_bits
.swap(that
->bits_
);
2725 RTLIL::SigChunk
*last
= NULL
;
2726 int last_end_offset
= 0;
2728 for (auto &bit
: old_bits
) {
2729 if (last
&& bit
.wire
== last
->wire
) {
2730 if (bit
.wire
== NULL
) {
2731 last
->data
.push_back(bit
.data
);
2734 } else if (last_end_offset
== bit
.offset
) {
2740 that
->chunks_
.push_back(bit
);
2741 last
= &that
->chunks_
.back();
2742 last_end_offset
= bit
.offset
+ 1;
2748 void RTLIL::SigSpec::unpack() const
2750 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2752 if (that
->chunks_
.empty())
2755 cover("kernel.rtlil.sigspec.convert.unpack");
2756 log_assert(that
->bits_
.empty());
2758 that
->bits_
.reserve(that
->width_
);
2759 for (auto &c
: that
->chunks_
)
2760 for (int i
= 0; i
< c
.width
; i
++)
2761 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2763 that
->chunks_
.clear();
2767 void RTLIL::SigSpec::updhash() const
2769 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2771 if (that
->hash_
!= 0)
2774 cover("kernel.rtlil.sigspec.hash");
2777 that
->hash_
= mkhash_init
;
2778 for (auto &c
: that
->chunks_
)
2779 if (c
.wire
== NULL
) {
2780 for (auto &v
: c
.data
)
2781 that
->hash_
= mkhash(that
->hash_
, v
);
2783 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
2784 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
2785 that
->hash_
= mkhash(that
->hash_
, c
.width
);
2788 if (that
->hash_
== 0)
2792 void RTLIL::SigSpec::sort()
2795 cover("kernel.rtlil.sigspec.sort");
2796 std::sort(bits_
.begin(), bits_
.end());
2799 void RTLIL::SigSpec::sort_and_unify()
2802 cover("kernel.rtlil.sigspec.sort_and_unify");
2804 // A copy of the bits vector is used to prevent duplicating the logic from
2805 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
2806 // that isn't showing up as significant in profiles.
2807 std::vector
<SigBit
> unique_bits
= bits_
;
2808 std::sort(unique_bits
.begin(), unique_bits
.end());
2809 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
2810 unique_bits
.erase(last
, unique_bits
.end());
2812 *this = unique_bits
;
2815 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2817 replace(pattern
, with
, this);
2820 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2822 log_assert(other
!= NULL
);
2823 log_assert(width_
== other
->width_
);
2824 log_assert(pattern
.width_
== with
.width_
);
2831 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
2832 if (pattern
.bits_
[i
].wire
!= NULL
) {
2833 for (int j
= 0; j
< GetSize(bits_
); j
++) {
2834 if (bits_
[j
] == pattern
.bits_
[i
]) {
2835 other
->bits_
[j
] = with
.bits_
[i
];
2844 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2846 replace(rules
, this);
2849 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2851 cover("kernel.rtlil.sigspec.replace_dict");
2853 log_assert(other
!= NULL
);
2854 log_assert(width_
== other
->width_
);
2859 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2860 auto it
= rules
.find(bits_
[i
]);
2861 if (it
!= rules
.end())
2862 other
->bits_
[i
] = it
->second
;
2868 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2870 replace(rules
, this);
2873 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2875 cover("kernel.rtlil.sigspec.replace_map");
2877 log_assert(other
!= NULL
);
2878 log_assert(width_
== other
->width_
);
2883 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2884 auto it
= rules
.find(bits_
[i
]);
2885 if (it
!= rules
.end())
2886 other
->bits_
[i
] = it
->second
;
2892 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2894 remove2(pattern
, NULL
);
2897 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2899 RTLIL::SigSpec tmp
= *this;
2900 tmp
.remove2(pattern
, other
);
2903 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2906 cover("kernel.rtlil.sigspec.remove_other");
2908 cover("kernel.rtlil.sigspec.remove");
2911 if (other
!= NULL
) {
2912 log_assert(width_
== other
->width_
);
2916 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
2918 if (bits_
[i
].wire
== NULL
) continue;
2920 for (auto &pattern_chunk
: pattern
.chunks())
2921 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
2922 bits_
[i
].offset
>= pattern_chunk
.offset
&&
2923 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
2924 bits_
.erase(bits_
.begin() + i
);
2926 if (other
!= NULL
) {
2927 other
->bits_
.erase(other
->bits_
.begin() + i
);
2937 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
2939 remove2(pattern
, NULL
);
2942 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2944 RTLIL::SigSpec tmp
= *this;
2945 tmp
.remove2(pattern
, other
);
2948 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2951 cover("kernel.rtlil.sigspec.remove_other");
2953 cover("kernel.rtlil.sigspec.remove");
2957 if (other
!= NULL
) {
2958 log_assert(width_
== other
->width_
);
2962 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
2963 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
2964 bits_
.erase(bits_
.begin() + i
);
2966 if (other
!= NULL
) {
2967 other
->bits_
.erase(other
->bits_
.begin() + i
);
2976 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2979 cover("kernel.rtlil.sigspec.remove_other");
2981 cover("kernel.rtlil.sigspec.remove");
2985 if (other
!= NULL
) {
2986 log_assert(width_
== other
->width_
);
2990 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
2991 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
2992 bits_
.erase(bits_
.begin() + i
);
2994 if (other
!= NULL
) {
2995 other
->bits_
.erase(other
->bits_
.begin() + i
);
3004 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3007 cover("kernel.rtlil.sigspec.extract_other");
3009 cover("kernel.rtlil.sigspec.extract");
3011 log_assert(other
== NULL
|| width_
== other
->width_
);
3014 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3016 for (auto& pattern_chunk
: pattern
.chunks()) {
3018 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3019 for (int i
= 0; i
< width_
; i
++)
3020 if (bits_match
[i
].wire
&&
3021 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3022 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3023 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3024 ret
.append_bit(bits_other
[i
]);
3026 for (int i
= 0; i
< width_
; i
++)
3027 if (bits_match
[i
].wire
&&
3028 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3029 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3030 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3031 ret
.append_bit(bits_match
[i
]);
3039 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3042 cover("kernel.rtlil.sigspec.extract_other");
3044 cover("kernel.rtlil.sigspec.extract");
3046 log_assert(other
== NULL
|| width_
== other
->width_
);
3048 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3052 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3053 for (int i
= 0; i
< width_
; i
++)
3054 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3055 ret
.append_bit(bits_other
[i
]);
3057 for (int i
= 0; i
< width_
; i
++)
3058 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3059 ret
.append_bit(bits_match
[i
]);
3066 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3068 cover("kernel.rtlil.sigspec.replace_pos");
3073 log_assert(offset
>= 0);
3074 log_assert(with
.width_
>= 0);
3075 log_assert(offset
+with
.width_
<= width_
);
3077 for (int i
= 0; i
< with
.width_
; i
++)
3078 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3083 void RTLIL::SigSpec::remove_const()
3087 cover("kernel.rtlil.sigspec.remove_const.packed");
3089 std::vector
<RTLIL::SigChunk
> new_chunks
;
3090 new_chunks
.reserve(GetSize(chunks_
));
3093 for (auto &chunk
: chunks_
)
3094 if (chunk
.wire
!= NULL
) {
3095 new_chunks
.push_back(chunk
);
3096 width_
+= chunk
.width
;
3099 chunks_
.swap(new_chunks
);
3103 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3105 std::vector
<RTLIL::SigBit
> new_bits
;
3106 new_bits
.reserve(width_
);
3108 for (auto &bit
: bits_
)
3109 if (bit
.wire
!= NULL
)
3110 new_bits
.push_back(bit
);
3112 bits_
.swap(new_bits
);
3113 width_
= bits_
.size();
3119 void RTLIL::SigSpec::remove(int offset
, int length
)
3121 cover("kernel.rtlil.sigspec.remove_pos");
3125 log_assert(offset
>= 0);
3126 log_assert(length
>= 0);
3127 log_assert(offset
+ length
<= width_
);
3129 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3130 width_
= bits_
.size();
3135 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3138 cover("kernel.rtlil.sigspec.extract_pos");
3139 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3142 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3144 if (signal
.width_
== 0)
3152 cover("kernel.rtlil.sigspec.append");
3154 if (packed() != signal
.packed()) {
3160 for (auto &other_c
: signal
.chunks_
)
3162 auto &my_last_c
= chunks_
.back();
3163 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3164 auto &this_data
= my_last_c
.data
;
3165 auto &other_data
= other_c
.data
;
3166 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3167 my_last_c
.width
+= other_c
.width
;
3169 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3170 my_last_c
.width
+= other_c
.width
;
3172 chunks_
.push_back(other_c
);
3175 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3177 width_
+= signal
.width_
;
3181 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3185 cover("kernel.rtlil.sigspec.append_bit.packed");
3187 if (chunks_
.size() == 0)
3188 chunks_
.push_back(bit
);
3190 if (bit
.wire
== NULL
)
3191 if (chunks_
.back().wire
== NULL
) {
3192 chunks_
.back().data
.push_back(bit
.data
);
3193 chunks_
.back().width
++;
3195 chunks_
.push_back(bit
);
3197 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3198 chunks_
.back().width
++;
3200 chunks_
.push_back(bit
);
3204 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3205 bits_
.push_back(bit
);
3212 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3214 cover("kernel.rtlil.sigspec.extend_u0");
3219 remove(width
, width_
- width
);
3221 if (width_
< width
) {
3222 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::S0
;
3224 padding
= RTLIL::State::S0
;
3225 while (width_
< width
)
3231 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3233 cover("kernel.rtlil.sigspec.repeat");
3236 for (int i
= 0; i
< num
; i
++)
3242 void RTLIL::SigSpec::check() const
3246 cover("kernel.rtlil.sigspec.check.skip");
3250 cover("kernel.rtlil.sigspec.check.packed");
3253 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3254 const RTLIL::SigChunk chunk
= chunks_
[i
];
3255 if (chunk
.wire
== NULL
) {
3257 log_assert(chunks_
[i
-1].wire
!= NULL
);
3258 log_assert(chunk
.offset
== 0);
3259 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3261 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3262 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3263 log_assert(chunk
.offset
>= 0);
3264 log_assert(chunk
.width
>= 0);
3265 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3266 log_assert(chunk
.data
.size() == 0);
3270 log_assert(w
== width_
);
3271 log_assert(bits_
.empty());
3275 cover("kernel.rtlil.sigspec.check.unpacked");
3277 log_assert(width_
== GetSize(bits_
));
3278 log_assert(chunks_
.empty());
3283 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3285 cover("kernel.rtlil.sigspec.comp_lt");
3290 if (width_
!= other
.width_
)
3291 return width_
< other
.width_
;
3296 if (chunks_
.size() != other
.chunks_
.size())
3297 return chunks_
.size() < other
.chunks_
.size();
3302 if (hash_
!= other
.hash_
)
3303 return hash_
< other
.hash_
;
3305 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3306 if (chunks_
[i
] != other
.chunks_
[i
]) {
3307 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3308 return chunks_
[i
] < other
.chunks_
[i
];
3311 cover("kernel.rtlil.sigspec.comp_lt.equal");
3315 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3317 cover("kernel.rtlil.sigspec.comp_eq");
3322 if (width_
!= other
.width_
)
3328 if (chunks_
.size() != chunks_
.size())
3334 if (hash_
!= other
.hash_
)
3337 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3338 if (chunks_
[i
] != other
.chunks_
[i
]) {
3339 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3343 cover("kernel.rtlil.sigspec.comp_eq.equal");
3347 bool RTLIL::SigSpec::is_wire() const
3349 cover("kernel.rtlil.sigspec.is_wire");
3352 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3355 bool RTLIL::SigSpec::is_chunk() const
3357 cover("kernel.rtlil.sigspec.is_chunk");
3360 return GetSize(chunks_
) == 1;
3363 bool RTLIL::SigSpec::is_fully_const() const
3365 cover("kernel.rtlil.sigspec.is_fully_const");
3368 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3369 if (it
->width
> 0 && it
->wire
!= NULL
)
3374 bool RTLIL::SigSpec::is_fully_zero() const
3376 cover("kernel.rtlil.sigspec.is_fully_zero");
3379 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3380 if (it
->width
> 0 && it
->wire
!= NULL
)
3382 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3383 if (it
->data
[i
] != RTLIL::State::S0
)
3389 bool RTLIL::SigSpec::is_fully_ones() const
3391 cover("kernel.rtlil.sigspec.is_fully_ones");
3394 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3395 if (it
->width
> 0 && it
->wire
!= NULL
)
3397 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3398 if (it
->data
[i
] != RTLIL::State::S1
)
3404 bool RTLIL::SigSpec::is_fully_def() const
3406 cover("kernel.rtlil.sigspec.is_fully_def");
3409 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3410 if (it
->width
> 0 && it
->wire
!= NULL
)
3412 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3413 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3419 bool RTLIL::SigSpec::is_fully_undef() const
3421 cover("kernel.rtlil.sigspec.is_fully_undef");
3424 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3425 if (it
->width
> 0 && it
->wire
!= NULL
)
3427 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3428 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3434 bool RTLIL::SigSpec::has_const() const
3436 cover("kernel.rtlil.sigspec.has_const");
3439 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3440 if (it
->width
> 0 && it
->wire
== NULL
)
3445 bool RTLIL::SigSpec::has_marked_bits() const
3447 cover("kernel.rtlil.sigspec.has_marked_bits");
3450 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3451 if (it
->width
> 0 && it
->wire
== NULL
) {
3452 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3453 if (it
->data
[i
] == RTLIL::State::Sm
)
3459 bool RTLIL::SigSpec::as_bool() const
3461 cover("kernel.rtlil.sigspec.as_bool");
3464 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3466 return RTLIL::Const(chunks_
[0].data
).as_bool();
3470 int RTLIL::SigSpec::as_int(bool is_signed
) const
3472 cover("kernel.rtlil.sigspec.as_int");
3475 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3477 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3481 std::string
RTLIL::SigSpec::as_string() const
3483 cover("kernel.rtlil.sigspec.as_string");
3487 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3488 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3489 if (chunk
.wire
!= NULL
)
3490 for (int j
= 0; j
< chunk
.width
; j
++)
3493 str
+= RTLIL::Const(chunk
.data
).as_string();
3498 RTLIL::Const
RTLIL::SigSpec::as_const() const
3500 cover("kernel.rtlil.sigspec.as_const");
3503 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3505 return chunks_
[0].data
;
3506 return RTLIL::Const();
3509 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3511 cover("kernel.rtlil.sigspec.as_wire");
3514 log_assert(is_wire());
3515 return chunks_
[0].wire
;
3518 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3520 cover("kernel.rtlil.sigspec.as_chunk");
3523 log_assert(is_chunk());
3527 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3529 cover("kernel.rtlil.sigspec.as_bit");
3531 log_assert(width_
== 1);
3533 return RTLIL::SigBit(*chunks_
.begin());
3538 bool RTLIL::SigSpec::match(std::string pattern
) const
3540 cover("kernel.rtlil.sigspec.match");
3543 std::string str
= as_string();
3544 log_assert(pattern
.size() == str
.size());
3546 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3547 if (pattern
[i
] == ' ')
3549 if (pattern
[i
] == '*') {
3550 if (str
[i
] != 'z' && str
[i
] != 'x')
3554 if (pattern
[i
] != str
[i
])
3561 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3563 cover("kernel.rtlil.sigspec.to_sigbit_set");
3566 std::set
<RTLIL::SigBit
> sigbits
;
3567 for (auto &c
: chunks_
)
3568 for (int i
= 0; i
< c
.width
; i
++)
3569 sigbits
.insert(RTLIL::SigBit(c
, i
));
3573 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3575 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3578 pool
<RTLIL::SigBit
> sigbits
;
3579 for (auto &c
: chunks_
)
3580 for (int i
= 0; i
< c
.width
; i
++)
3581 sigbits
.insert(RTLIL::SigBit(c
, i
));
3585 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3587 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3593 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3595 cover("kernel.rtlil.sigspec.to_sigbit_map");
3600 log_assert(width_
== other
.width_
);
3602 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3603 for (int i
= 0; i
< width_
; i
++)
3604 new_map
[bits_
[i
]] = other
.bits_
[i
];
3609 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3611 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3616 log_assert(width_
== other
.width_
);
3618 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3619 for (int i
= 0; i
< width_
; i
++)
3620 new_map
[bits_
[i
]] = other
.bits_
[i
];
3625 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3627 size_t start
= 0, end
= 0;
3628 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3629 tokens
.push_back(text
.substr(start
, end
- start
));
3632 tokens
.push_back(text
.substr(start
));
3635 static int sigspec_parse_get_dummy_line_num()
3640 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3642 cover("kernel.rtlil.sigspec.parse");
3644 AST::current_filename
= "input";
3645 AST::use_internal_line_num();
3646 AST::set_line_num(0);
3648 std::vector
<std::string
> tokens
;
3649 sigspec_parse_split(tokens
, str
, ',');
3651 sig
= RTLIL::SigSpec();
3652 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3654 std::string netname
= tokens
[tokidx
];
3655 std::string indices
;
3657 if (netname
.size() == 0)
3660 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3661 cover("kernel.rtlil.sigspec.parse.const");
3662 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3663 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3666 sig
.append(RTLIL::Const(ast
->bits
));
3674 cover("kernel.rtlil.sigspec.parse.net");
3676 if (netname
[0] != '$' && netname
[0] != '\\')
3677 netname
= "\\" + netname
;
3679 if (module
->wires_
.count(netname
) == 0) {
3680 size_t indices_pos
= netname
.size()-1;
3681 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3684 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3685 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3687 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3689 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3690 indices
= netname
.substr(indices_pos
);
3691 netname
= netname
.substr(0, indices_pos
);
3696 if (module
->wires_
.count(netname
) == 0)
3699 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3700 if (!indices
.empty()) {
3701 std::vector
<std::string
> index_tokens
;
3702 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3703 if (index_tokens
.size() == 1) {
3704 cover("kernel.rtlil.sigspec.parse.bit_sel");
3705 int a
= atoi(index_tokens
.at(0).c_str());
3706 if (a
< 0 || a
>= wire
->width
)
3708 sig
.append(RTLIL::SigSpec(wire
, a
));
3710 cover("kernel.rtlil.sigspec.parse.part_sel");
3711 int a
= atoi(index_tokens
.at(0).c_str());
3712 int b
= atoi(index_tokens
.at(1).c_str());
3717 if (a
< 0 || a
>= wire
->width
)
3719 if (b
< 0 || b
>= wire
->width
)
3721 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3730 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3732 if (str
.empty() || str
[0] != '@')
3733 return parse(sig
, module
, str
);
3735 cover("kernel.rtlil.sigspec.parse.sel");
3737 str
= RTLIL::escape_id(str
.substr(1));
3738 if (design
->selection_vars
.count(str
) == 0)
3741 sig
= RTLIL::SigSpec();
3742 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3743 for (auto &it
: module
->wires_
)
3744 if (sel
.selected_member(module
->name
, it
.first
))
3745 sig
.append(it
.second
);
3750 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3753 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3754 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3759 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3760 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3764 if (lhs
.chunks_
.size() == 1) {
3765 char *p
= (char*)str
.c_str(), *endptr
;
3766 long int val
= strtol(p
, &endptr
, 10);
3767 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3768 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3769 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3774 return parse(sig
, module
, str
);
3777 RTLIL::CaseRule::~CaseRule()
3779 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3783 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3785 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3786 new_caserule
->compare
= compare
;
3787 new_caserule
->actions
= actions
;
3788 for (auto &it
: switches
)
3789 new_caserule
->switches
.push_back(it
->clone());
3790 return new_caserule
;
3793 RTLIL::SwitchRule::~SwitchRule()
3795 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3799 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3801 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3802 new_switchrule
->signal
= signal
;
3803 new_switchrule
->attributes
= attributes
;
3804 for (auto &it
: cases
)
3805 new_switchrule
->cases
.push_back(it
->clone());
3806 return new_switchrule
;
3810 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3812 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3813 new_syncrule
->type
= type
;
3814 new_syncrule
->signal
= signal
;
3815 new_syncrule
->actions
= actions
;
3816 return new_syncrule
;
3819 RTLIL::Process::~Process()
3821 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3825 RTLIL::Process
*RTLIL::Process::clone() const
3827 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3829 new_proc
->name
= name
;
3830 new_proc
->attributes
= attributes
;
3832 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3833 new_proc
->root_case
= *rc_ptr
;
3834 rc_ptr
->switches
.clear();
3837 for (auto &it
: syncs
)
3838 new_proc
->syncs
.push_back(it
->clone());