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_
;
36 int RTLIL::IdString::last_created_idx_
[8];
37 int RTLIL::IdString::last_created_idx_ptr_
;
41 flags
= RTLIL::CONST_FLAG_NONE
;
44 RTLIL::Const::Const(std::string str
)
46 flags
= RTLIL::CONST_FLAG_STRING
;
47 for (int i
= str
.size()-1; i
>= 0; i
--) {
48 unsigned char ch
= str
[i
];
49 for (int j
= 0; j
< 8; j
++) {
50 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
56 RTLIL::Const::Const(int val
, int width
)
58 flags
= RTLIL::CONST_FLAG_NONE
;
59 for (int i
= 0; i
< width
; i
++) {
60 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
65 RTLIL::Const::Const(RTLIL::State bit
, int width
)
67 flags
= RTLIL::CONST_FLAG_NONE
;
68 for (int i
= 0; i
< width
; i
++)
72 RTLIL::Const::Const(const std::vector
<bool> &bits
)
74 flags
= RTLIL::CONST_FLAG_NONE
;
76 this->bits
.push_back(b
? RTLIL::S1
: RTLIL::S0
);
79 RTLIL::Const::Const(const RTLIL::Const
&c
)
83 this->bits
.push_back(b
);
86 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
88 if (bits
.size() != other
.bits
.size())
89 return bits
.size() < other
.bits
.size();
90 for (size_t i
= 0; i
< bits
.size(); i
++)
91 if (bits
[i
] != other
.bits
[i
])
92 return bits
[i
] < other
.bits
[i
];
96 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
98 return bits
== other
.bits
;
101 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
103 return bits
!= other
.bits
;
106 bool RTLIL::Const::as_bool() const
108 for (size_t i
= 0; i
< bits
.size(); i
++)
109 if (bits
[i
] == RTLIL::S1
)
114 int RTLIL::Const::as_int(bool is_signed
) const
117 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
118 if (bits
[i
] == RTLIL::S1
)
120 if (is_signed
&& bits
.back() == RTLIL::S1
)
121 for (size_t i
= bits
.size(); i
< 32; i
++)
126 std::string
RTLIL::Const::as_string() const
129 for (size_t i
= bits
.size(); i
> 0; i
--)
131 case S0
: ret
+= "0"; break;
132 case S1
: ret
+= "1"; break;
133 case Sx
: ret
+= "x"; break;
134 case Sz
: ret
+= "z"; break;
135 case Sa
: ret
+= "-"; break;
136 case Sm
: ret
+= "m"; break;
141 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
144 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
146 case '0': c
.bits
.push_back(State::S0
); break;
147 case '1': c
.bits
.push_back(State::S1
); break;
148 case 'x': c
.bits
.push_back(State::Sx
); break;
149 case 'z': c
.bits
.push_back(State::Sz
); break;
150 case 'm': c
.bits
.push_back(State::Sm
); break;
151 default: c
.bits
.push_back(State::Sa
);
156 std::string
RTLIL::Const::decode_string() const
159 std::vector
<char> string_chars
;
160 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
162 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
163 if (bits
[i
+ j
] == RTLIL::State::S1
)
166 string_chars
.push_back(ch
);
168 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
169 string
+= string_chars
[i
];
173 bool RTLIL::Const::is_fully_zero() const
175 cover("kernel.rtlil.const.is_fully_zero");
177 for (auto bit
: bits
)
178 if (bit
!= RTLIL::State::S0
)
184 bool RTLIL::Const::is_fully_ones() const
186 cover("kernel.rtlil.const.is_fully_ones");
188 for (auto bit
: bits
)
189 if (bit
!= RTLIL::State::S1
)
195 bool RTLIL::Const::is_fully_def() const
197 cover("kernel.rtlil.const.is_fully_def");
199 for (auto bit
: bits
)
200 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
206 bool RTLIL::Const::is_fully_undef() const
208 cover("kernel.rtlil.const.is_fully_undef");
210 for (auto bit
: bits
)
211 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
217 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
220 attributes
[id
] = RTLIL::Const(1);
222 const auto it
= attributes
.find(id
);
223 if (it
!= attributes
.end())
224 attributes
.erase(it
);
228 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
230 const auto it
= attributes
.find(id
);
231 if (it
== attributes
.end())
233 return it
->second
.as_bool();
236 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
239 for (auto &s
: data
) {
240 if (!attrval
.empty())
244 attributes
[id
] = RTLIL::Const(attrval
);
247 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
249 pool
<string
> union_data
= get_strpool_attribute(id
);
250 union_data
.insert(data
.begin(), data
.end());
251 if (!union_data
.empty())
252 set_strpool_attribute(id
, union_data
);
255 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
258 if (attributes
.count(id
) != 0)
259 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
264 void RTLIL::AttrObject::set_src_attribute(const std::string
&src
)
267 attributes
.erase("\\src");
269 attributes
["\\src"] = src
;
272 std::string
RTLIL::AttrObject::get_src_attribute() const
275 if (attributes
.count("\\src"))
276 src
= attributes
.at("\\src").decode_string();
280 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
284 if (selected_modules
.count(mod_name
) > 0)
286 if (selected_members
.count(mod_name
) > 0)
291 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
295 if (selected_modules
.count(mod_name
) > 0)
300 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
304 if (selected_modules
.count(mod_name
) > 0)
306 if (selected_members
.count(mod_name
) > 0)
307 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
312 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
314 if (full_selection
) {
315 selected_modules
.clear();
316 selected_members
.clear();
320 std::vector
<RTLIL::IdString
> del_list
, add_list
;
323 for (auto mod_name
: selected_modules
) {
324 if (design
->modules_
.count(mod_name
) == 0)
325 del_list
.push_back(mod_name
);
326 selected_members
.erase(mod_name
);
328 for (auto mod_name
: del_list
)
329 selected_modules
.erase(mod_name
);
332 for (auto &it
: selected_members
)
333 if (design
->modules_
.count(it
.first
) == 0)
334 del_list
.push_back(it
.first
);
335 for (auto mod_name
: del_list
)
336 selected_members
.erase(mod_name
);
338 for (auto &it
: selected_members
) {
340 for (auto memb_name
: it
.second
)
341 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
342 del_list
.push_back(memb_name
);
343 for (auto memb_name
: del_list
)
344 it
.second
.erase(memb_name
);
349 for (auto &it
: selected_members
)
350 if (it
.second
.size() == 0)
351 del_list
.push_back(it
.first
);
352 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
353 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
354 add_list
.push_back(it
.first
);
355 for (auto mod_name
: del_list
)
356 selected_members
.erase(mod_name
);
357 for (auto mod_name
: add_list
) {
358 selected_members
.erase(mod_name
);
359 selected_modules
.insert(mod_name
);
362 if (selected_modules
.size() == design
->modules_
.size()) {
363 full_selection
= true;
364 selected_modules
.clear();
365 selected_members
.clear();
369 RTLIL::Design::Design()
371 static unsigned int hashidx_count
= 123456789;
372 hashidx_count
= mkhash_xorshift(hashidx_count
);
373 hashidx_
= hashidx_count
;
375 refcount_modules_
= 0;
376 selection_stack
.push_back(RTLIL::Selection());
379 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
383 RTLIL::Design::~Design()
385 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
387 for (auto n
: verilog_packages
)
389 for (auto n
: verilog_globals
)
392 RTLIL::Design::get_all_designs()->erase(hashidx_
);
397 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
398 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
404 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
406 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
409 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
411 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
414 RTLIL::Module
*RTLIL::Design::top_module()
416 RTLIL::Module
*module
= nullptr;
417 int module_count
= 0;
419 for (auto mod
: selected_modules()) {
420 if (mod
->get_bool_attribute("\\top"))
426 return module_count
== 1 ? module
: nullptr;
429 void RTLIL::Design::add(RTLIL::Module
*module
)
431 log_assert(modules_
.count(module
->name
) == 0);
432 log_assert(refcount_modules_
== 0);
433 modules_
[module
->name
] = module
;
434 module
->design
= this;
436 for (auto mon
: monitors
)
437 mon
->notify_module_add(module
);
440 log("#X# New Module: %s\n", log_id(module
));
441 log_backtrace("-X- ", yosys_xtrace
-1);
445 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
447 log_assert(modules_
.count(name
) == 0);
448 log_assert(refcount_modules_
== 0);
450 RTLIL::Module
*module
= new RTLIL::Module
;
451 modules_
[name
] = module
;
452 module
->design
= this;
455 for (auto mon
: monitors
)
456 mon
->notify_module_add(module
);
459 log("#X# New Module: %s\n", log_id(module
));
460 log_backtrace("-X- ", yosys_xtrace
-1);
466 void RTLIL::Design::scratchpad_unset(std::string varname
)
468 scratchpad
.erase(varname
);
471 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
473 scratchpad
[varname
] = stringf("%d", value
);
476 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
478 scratchpad
[varname
] = value
? "true" : "false";
481 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
483 scratchpad
[varname
] = value
;
486 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
488 if (scratchpad
.count(varname
) == 0)
489 return default_value
;
491 std::string str
= scratchpad
.at(varname
);
493 if (str
== "0" || str
== "false")
496 if (str
== "1" || str
== "true")
499 char *endptr
= nullptr;
500 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
501 return *endptr
? default_value
: parsed_value
;
504 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
506 if (scratchpad
.count(varname
) == 0)
507 return default_value
;
509 std::string str
= scratchpad
.at(varname
);
511 if (str
== "0" || str
== "false")
514 if (str
== "1" || str
== "true")
517 return default_value
;
520 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
522 if (scratchpad
.count(varname
) == 0)
523 return default_value
;
524 return scratchpad
.at(varname
);
527 void RTLIL::Design::remove(RTLIL::Module
*module
)
529 for (auto mon
: monitors
)
530 mon
->notify_module_del(module
);
533 log("#X# Remove Module: %s\n", log_id(module
));
534 log_backtrace("-X- ", yosys_xtrace
-1);
537 log_assert(modules_
.at(module
->name
) == module
);
538 modules_
.erase(module
->name
);
542 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
544 modules_
.erase(module
->name
);
545 module
->name
= new_name
;
549 void RTLIL::Design::sort()
552 modules_
.sort(sort_by_id_str());
553 for (auto &it
: modules_
)
557 void RTLIL::Design::check()
560 for (auto &it
: modules_
) {
561 log_assert(this == it
.second
->design
);
562 log_assert(it
.first
== it
.second
->name
);
563 log_assert(!it
.first
.empty());
569 void RTLIL::Design::optimize()
571 for (auto &it
: modules_
)
572 it
.second
->optimize();
573 for (auto &it
: selection_stack
)
575 for (auto &it
: selection_vars
)
576 it
.second
.optimize(this);
579 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
581 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
583 if (selection_stack
.size() == 0)
585 return selection_stack
.back().selected_module(mod_name
);
588 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
590 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
592 if (selection_stack
.size() == 0)
594 return selection_stack
.back().selected_whole_module(mod_name
);
597 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
599 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
601 if (selection_stack
.size() == 0)
603 return selection_stack
.back().selected_member(mod_name
, memb_name
);
606 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
608 return selected_module(mod
->name
);
611 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
613 return selected_whole_module(mod
->name
);
616 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
618 std::vector
<RTLIL::Module
*> result
;
619 result
.reserve(modules_
.size());
620 for (auto &it
: modules_
)
621 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
622 result
.push_back(it
.second
);
626 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
628 std::vector
<RTLIL::Module
*> result
;
629 result
.reserve(modules_
.size());
630 for (auto &it
: modules_
)
631 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
632 result
.push_back(it
.second
);
636 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
638 std::vector
<RTLIL::Module
*> result
;
639 result
.reserve(modules_
.size());
640 for (auto &it
: modules_
)
641 if (it
.second
->get_blackbox_attribute())
643 else if (selected_whole_module(it
.first
))
644 result
.push_back(it
.second
);
645 else if (selected_module(it
.first
))
646 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
650 RTLIL::Module::Module()
652 static unsigned int hashidx_count
= 123456789;
653 hashidx_count
= mkhash_xorshift(hashidx_count
);
654 hashidx_
= hashidx_count
;
661 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
665 RTLIL::Module::~Module()
667 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
669 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
671 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
673 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
676 RTLIL::Module::get_all_modules()->erase(hashidx_
);
681 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
682 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
688 void RTLIL::Module::makeblackbox()
690 pool
<RTLIL::Wire
*> delwires
;
692 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
693 if (!it
->second
->port_input
&& !it
->second
->port_output
)
694 delwires
.insert(it
->second
);
696 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
700 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
704 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
709 set_bool_attribute("\\blackbox");
712 void RTLIL::Module::reprocess_module(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Module
*>)
714 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
717 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, bool mayfail
)
720 return RTLIL::IdString();
721 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
725 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, dict
<RTLIL::IdString
, RTLIL::Module
*>, dict
<RTLIL::IdString
, RTLIL::IdString
>, bool mayfail
)
728 return RTLIL::IdString();
729 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
732 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
734 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
739 struct InternalCellChecker
741 RTLIL::Module
*module
;
743 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
745 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
747 void error(int linenr
)
749 std::stringstream buf
;
750 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
752 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
753 module
? module
->name
.c_str() : "", module
? "." : "",
754 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
757 int param(const char *name
)
759 if (cell
->parameters
.count(name
) == 0)
761 expected_params
.insert(name
);
762 return cell
->parameters
.at(name
).as_int();
765 int param_bool(const char *name
)
768 if (cell
->parameters
.at(name
).bits
.size() > 32)
770 if (v
!= 0 && v
!= 1)
775 void param_bits(const char *name
, int width
)
778 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
782 void port(const char *name
, int width
)
784 if (!cell
->hasPort(name
))
786 if (cell
->getPort(name
).size() != width
)
788 expected_ports
.insert(name
);
791 void check_expected(bool check_matched_sign
= true)
793 for (auto ¶
: cell
->parameters
)
794 if (expected_params
.count(para
.first
) == 0)
796 for (auto &conn
: cell
->connections())
797 if (expected_ports
.count(conn
.first
) == 0)
800 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
801 bool a_is_signed
= param("\\A_SIGNED") != 0;
802 bool b_is_signed
= param("\\B_SIGNED") != 0;
803 if (a_is_signed
!= b_is_signed
)
808 void check_gate(const char *ports
)
810 if (cell
->parameters
.size() != 0)
813 for (const char *p
= ports
; *p
; p
++) {
814 char portname
[3] = { '\\', *p
, 0 };
815 if (!cell
->hasPort(portname
))
817 if (cell
->getPort(portname
).size() != 1)
821 for (auto &conn
: cell
->connections()) {
822 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
824 if (strchr(ports
, conn
.first
[1]) == NULL
)
831 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" || cell
->type
.substr(0,10) == "$fmcombine" ||
832 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
835 if (cell
->type
.in("$not", "$pos", "$neg")) {
836 param_bool("\\A_SIGNED");
837 port("\\A", param("\\A_WIDTH"));
838 port("\\Y", param("\\Y_WIDTH"));
843 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
844 param_bool("\\A_SIGNED");
845 param_bool("\\B_SIGNED");
846 port("\\A", param("\\A_WIDTH"));
847 port("\\B", param("\\B_WIDTH"));
848 port("\\Y", param("\\Y_WIDTH"));
853 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
854 param_bool("\\A_SIGNED");
855 port("\\A", param("\\A_WIDTH"));
856 port("\\Y", param("\\Y_WIDTH"));
861 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
862 param_bool("\\A_SIGNED");
863 param_bool("\\B_SIGNED");
864 port("\\A", param("\\A_WIDTH"));
865 port("\\B", param("\\B_WIDTH"));
866 port("\\Y", param("\\Y_WIDTH"));
867 check_expected(false);
871 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
872 param_bool("\\A_SIGNED");
873 param_bool("\\B_SIGNED");
874 port("\\A", param("\\A_WIDTH"));
875 port("\\B", param("\\B_WIDTH"));
876 port("\\Y", param("\\Y_WIDTH"));
881 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
882 param_bool("\\A_SIGNED");
883 param_bool("\\B_SIGNED");
884 port("\\A", param("\\A_WIDTH"));
885 port("\\B", param("\\B_WIDTH"));
886 port("\\Y", param("\\Y_WIDTH"));
887 check_expected(cell
->type
!= "$pow");
891 if (cell
->type
== "$fa") {
892 port("\\A", param("\\WIDTH"));
893 port("\\B", param("\\WIDTH"));
894 port("\\C", param("\\WIDTH"));
895 port("\\X", param("\\WIDTH"));
896 port("\\Y", param("\\WIDTH"));
901 if (cell
->type
== "$lcu") {
902 port("\\P", param("\\WIDTH"));
903 port("\\G", param("\\WIDTH"));
905 port("\\CO", param("\\WIDTH"));
910 if (cell
->type
== "$alu") {
911 param_bool("\\A_SIGNED");
912 param_bool("\\B_SIGNED");
913 port("\\A", param("\\A_WIDTH"));
914 port("\\B", param("\\B_WIDTH"));
917 port("\\X", param("\\Y_WIDTH"));
918 port("\\Y", param("\\Y_WIDTH"));
919 port("\\CO", param("\\Y_WIDTH"));
924 if (cell
->type
== "$macc") {
926 param("\\CONFIG_WIDTH");
927 port("\\A", param("\\A_WIDTH"));
928 port("\\B", param("\\B_WIDTH"));
929 port("\\Y", param("\\Y_WIDTH"));
931 Macc().from_cell(cell
);
935 if (cell
->type
== "$logic_not") {
936 param_bool("\\A_SIGNED");
937 port("\\A", param("\\A_WIDTH"));
938 port("\\Y", param("\\Y_WIDTH"));
943 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
944 param_bool("\\A_SIGNED");
945 param_bool("\\B_SIGNED");
946 port("\\A", param("\\A_WIDTH"));
947 port("\\B", param("\\B_WIDTH"));
948 port("\\Y", param("\\Y_WIDTH"));
949 check_expected(false);
953 if (cell
->type
== "$slice") {
955 port("\\A", param("\\A_WIDTH"));
956 port("\\Y", param("\\Y_WIDTH"));
957 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
963 if (cell
->type
== "$concat") {
964 port("\\A", param("\\A_WIDTH"));
965 port("\\B", param("\\B_WIDTH"));
966 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
971 if (cell
->type
== "$mux") {
972 port("\\A", param("\\WIDTH"));
973 port("\\B", param("\\WIDTH"));
975 port("\\Y", param("\\WIDTH"));
980 if (cell
->type
== "$pmux") {
981 port("\\A", param("\\WIDTH"));
982 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
983 port("\\S", param("\\S_WIDTH"));
984 port("\\Y", param("\\WIDTH"));
989 if (cell
->type
== "$lut") {
991 port("\\A", param("\\WIDTH"));
997 if (cell
->type
== "$sop") {
1000 port("\\A", param("\\WIDTH"));
1006 if (cell
->type
== "$sr") {
1007 param_bool("\\SET_POLARITY");
1008 param_bool("\\CLR_POLARITY");
1009 port("\\SET", param("\\WIDTH"));
1010 port("\\CLR", param("\\WIDTH"));
1011 port("\\Q", param("\\WIDTH"));
1016 if (cell
->type
== "$ff") {
1017 port("\\D", param("\\WIDTH"));
1018 port("\\Q", param("\\WIDTH"));
1023 if (cell
->type
== "$dff") {
1024 param_bool("\\CLK_POLARITY");
1026 port("\\D", param("\\WIDTH"));
1027 port("\\Q", param("\\WIDTH"));
1032 if (cell
->type
== "$dffe") {
1033 param_bool("\\CLK_POLARITY");
1034 param_bool("\\EN_POLARITY");
1037 port("\\D", param("\\WIDTH"));
1038 port("\\Q", param("\\WIDTH"));
1043 if (cell
->type
== "$dffsr") {
1044 param_bool("\\CLK_POLARITY");
1045 param_bool("\\SET_POLARITY");
1046 param_bool("\\CLR_POLARITY");
1048 port("\\SET", param("\\WIDTH"));
1049 port("\\CLR", param("\\WIDTH"));
1050 port("\\D", param("\\WIDTH"));
1051 port("\\Q", param("\\WIDTH"));
1056 if (cell
->type
== "$adff") {
1057 param_bool("\\CLK_POLARITY");
1058 param_bool("\\ARST_POLARITY");
1059 param_bits("\\ARST_VALUE", param("\\WIDTH"));
1062 port("\\D", param("\\WIDTH"));
1063 port("\\Q", param("\\WIDTH"));
1068 if (cell
->type
== "$dlatch") {
1069 param_bool("\\EN_POLARITY");
1071 port("\\D", param("\\WIDTH"));
1072 port("\\Q", param("\\WIDTH"));
1077 if (cell
->type
== "$dlatchsr") {
1078 param_bool("\\EN_POLARITY");
1079 param_bool("\\SET_POLARITY");
1080 param_bool("\\CLR_POLARITY");
1082 port("\\SET", param("\\WIDTH"));
1083 port("\\CLR", param("\\WIDTH"));
1084 port("\\D", param("\\WIDTH"));
1085 port("\\Q", param("\\WIDTH"));
1090 if (cell
->type
== "$fsm") {
1092 param_bool("\\CLK_POLARITY");
1093 param_bool("\\ARST_POLARITY");
1094 param("\\STATE_BITS");
1095 param("\\STATE_NUM");
1096 param("\\STATE_NUM_LOG2");
1097 param("\\STATE_RST");
1098 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
1099 param("\\TRANS_NUM");
1100 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
1103 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
1104 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
1109 if (cell
->type
== "$memrd") {
1111 param_bool("\\CLK_ENABLE");
1112 param_bool("\\CLK_POLARITY");
1113 param_bool("\\TRANSPARENT");
1116 port("\\ADDR", param("\\ABITS"));
1117 port("\\DATA", param("\\WIDTH"));
1122 if (cell
->type
== "$memwr") {
1124 param_bool("\\CLK_ENABLE");
1125 param_bool("\\CLK_POLARITY");
1126 param("\\PRIORITY");
1128 port("\\EN", param("\\WIDTH"));
1129 port("\\ADDR", param("\\ABITS"));
1130 port("\\DATA", param("\\WIDTH"));
1135 if (cell
->type
== "$meminit") {
1137 param("\\PRIORITY");
1138 port("\\ADDR", param("\\ABITS"));
1139 port("\\DATA", param("\\WIDTH") * param("\\WORDS"));
1144 if (cell
->type
== "$mem") {
1149 param_bits("\\RD_CLK_ENABLE", max(1, param("\\RD_PORTS")));
1150 param_bits("\\RD_CLK_POLARITY", max(1, param("\\RD_PORTS")));
1151 param_bits("\\RD_TRANSPARENT", max(1, param("\\RD_PORTS")));
1152 param_bits("\\WR_CLK_ENABLE", max(1, param("\\WR_PORTS")));
1153 param_bits("\\WR_CLK_POLARITY", max(1, param("\\WR_PORTS")));
1154 port("\\RD_CLK", param("\\RD_PORTS"));
1155 port("\\RD_EN", param("\\RD_PORTS"));
1156 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
1157 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
1158 port("\\WR_CLK", param("\\WR_PORTS"));
1159 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
1160 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
1161 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
1166 if (cell
->type
== "$tribuf") {
1167 port("\\A", param("\\WIDTH"));
1168 port("\\Y", param("\\WIDTH"));
1174 if (cell
->type
.in("$assert", "$assume", "$live", "$fair", "$cover")) {
1181 if (cell
->type
== "$initstate") {
1187 if (cell
->type
.in("$anyconst", "$anyseq", "$allconst", "$allseq")) {
1188 port("\\Y", param("\\WIDTH"));
1193 if (cell
->type
== "$equiv") {
1201 if (cell
->type
.in("$specify2", "$specify3")) {
1202 param_bool("\\FULL");
1203 param_bool("\\SRC_DST_PEN");
1204 param_bool("\\SRC_DST_POL");
1205 param("\\T_RISE_MIN");
1206 param("\\T_RISE_TYP");
1207 param("\\T_RISE_MAX");
1208 param("\\T_FALL_MIN");
1209 param("\\T_FALL_TYP");
1210 param("\\T_FALL_MAX");
1212 port("\\SRC", param("\\SRC_WIDTH"));
1213 port("\\DST", param("\\DST_WIDTH"));
1214 if (cell
->type
== "$specify3") {
1215 param_bool("\\EDGE_EN");
1216 param_bool("\\EDGE_POL");
1217 param_bool("\\DAT_DST_PEN");
1218 param_bool("\\DAT_DST_POL");
1219 port("\\DAT", param("\\DST_WIDTH"));
1225 if (cell
->type
== "$specrule") {
1227 param_bool("\\SRC_PEN");
1228 param_bool("\\SRC_POL");
1229 param_bool("\\DST_PEN");
1230 param_bool("\\DST_POL");
1232 param("\\T_LIMIT2");
1233 port("\\SRC_EN", 1);
1234 port("\\DST_EN", 1);
1235 port("\\SRC", param("\\SRC_WIDTH"));
1236 port("\\DST", param("\\DST_WIDTH"));
1241 if (cell
->type
== "$_BUF_") { check_gate("AY"); return; }
1242 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
1243 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
1244 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
1245 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
1246 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
1247 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
1248 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
1249 if (cell
->type
== "$_ANDNOT_") { check_gate("ABY"); return; }
1250 if (cell
->type
== "$_ORNOT_") { check_gate("ABY"); return; }
1251 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
1252 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
1253 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
1254 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
1255 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
1257 if (cell
->type
== "$_TBUF_") { check_gate("AYE"); return; }
1259 if (cell
->type
== "$_MUX4_") { check_gate("ABCDSTY"); return; }
1260 if (cell
->type
== "$_MUX8_") { check_gate("ABCDEFGHSTUY"); return; }
1261 if (cell
->type
== "$_MUX16_") { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1263 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
1264 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
1265 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
1266 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
1268 if (cell
->type
== "$_FF_") { check_gate("DQ"); return; }
1269 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
1270 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
1272 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
1273 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
1274 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
1275 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
1277 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
1278 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
1279 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
1280 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
1281 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
1282 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
1283 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
1284 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
1286 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
1287 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
1288 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
1289 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
1290 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
1291 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
1292 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
1293 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
1295 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
1296 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
1298 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
1299 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
1300 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
1301 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
1302 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
1303 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
1304 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
1305 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
1313 void RTLIL::Module::sort()
1315 wires_
.sort(sort_by_id_str());
1316 cells_
.sort(sort_by_id_str());
1317 avail_parameters
.sort(sort_by_id_str());
1318 memories
.sort(sort_by_id_str());
1319 processes
.sort(sort_by_id_str());
1320 for (auto &it
: cells_
)
1322 for (auto &it
: wires_
)
1323 it
.second
->attributes
.sort(sort_by_id_str());
1324 for (auto &it
: memories
)
1325 it
.second
->attributes
.sort(sort_by_id_str());
1328 void RTLIL::Module::check()
1331 std::vector
<bool> ports_declared
;
1332 for (auto &it
: wires_
) {
1333 log_assert(this == it
.second
->module
);
1334 log_assert(it
.first
== it
.second
->name
);
1335 log_assert(!it
.first
.empty());
1336 log_assert(it
.second
->width
>= 0);
1337 log_assert(it
.second
->port_id
>= 0);
1338 for (auto &it2
: it
.second
->attributes
)
1339 log_assert(!it2
.first
.empty());
1340 if (it
.second
->port_id
) {
1341 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1342 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1343 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1344 if (GetSize(ports_declared
) < it
.second
->port_id
)
1345 ports_declared
.resize(it
.second
->port_id
);
1346 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1347 ports_declared
[it
.second
->port_id
-1] = true;
1349 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1351 for (auto port_declared
: ports_declared
)
1352 log_assert(port_declared
== true);
1353 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1355 for (auto &it
: memories
) {
1356 log_assert(it
.first
== it
.second
->name
);
1357 log_assert(!it
.first
.empty());
1358 log_assert(it
.second
->width
>= 0);
1359 log_assert(it
.second
->size
>= 0);
1360 for (auto &it2
: it
.second
->attributes
)
1361 log_assert(!it2
.first
.empty());
1364 for (auto &it
: cells_
) {
1365 log_assert(this == it
.second
->module
);
1366 log_assert(it
.first
== it
.second
->name
);
1367 log_assert(!it
.first
.empty());
1368 log_assert(!it
.second
->type
.empty());
1369 for (auto &it2
: it
.second
->connections()) {
1370 log_assert(!it2
.first
.empty());
1373 for (auto &it2
: it
.second
->attributes
)
1374 log_assert(!it2
.first
.empty());
1375 for (auto &it2
: it
.second
->parameters
)
1376 log_assert(!it2
.first
.empty());
1377 InternalCellChecker
checker(this, it
.second
);
1381 for (auto &it
: processes
) {
1382 log_assert(it
.first
== it
.second
->name
);
1383 log_assert(!it
.first
.empty());
1384 log_assert(it
.second
->root_case
.compare
.empty());
1385 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1386 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1387 for (auto &switch_it
: all_cases
[i
]->switches
) {
1388 for (auto &case_it
: switch_it
->cases
) {
1389 for (auto &compare_it
: case_it
->compare
) {
1390 log_assert(switch_it
->signal
.size() == compare_it
.size());
1392 all_cases
.push_back(case_it
);
1396 for (auto &sync_it
: it
.second
->syncs
) {
1397 switch (sync_it
->type
) {
1403 log_assert(!sync_it
->signal
.empty());
1408 log_assert(sync_it
->signal
.empty());
1414 for (auto &it
: connections_
) {
1415 log_assert(it
.first
.size() == it
.second
.size());
1416 log_assert(!it
.first
.has_const());
1421 for (auto &it
: attributes
)
1422 log_assert(!it
.first
.empty());
1426 void RTLIL::Module::optimize()
1430 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1432 log_assert(new_mod
->refcount_wires_
== 0);
1433 log_assert(new_mod
->refcount_cells_
== 0);
1435 new_mod
->avail_parameters
= avail_parameters
;
1437 for (auto &conn
: connections_
)
1438 new_mod
->connect(conn
);
1440 for (auto &attr
: attributes
)
1441 new_mod
->attributes
[attr
.first
] = attr
.second
;
1443 for (auto &it
: wires_
)
1444 new_mod
->addWire(it
.first
, it
.second
);
1446 for (auto &it
: memories
)
1447 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1449 for (auto &it
: cells_
)
1450 new_mod
->addCell(it
.first
, it
.second
);
1452 for (auto &it
: processes
)
1453 new_mod
->processes
[it
.first
] = it
.second
->clone();
1455 struct RewriteSigSpecWorker
1458 void operator()(RTLIL::SigSpec
&sig
)
1460 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1461 for (auto &c
: chunks
)
1463 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1468 RewriteSigSpecWorker rewriteSigSpecWorker
;
1469 rewriteSigSpecWorker
.mod
= new_mod
;
1470 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1471 new_mod
->fixup_ports();
1474 RTLIL::Module
*RTLIL::Module::clone() const
1476 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1477 new_mod
->name
= name
;
1482 bool RTLIL::Module::has_memories() const
1484 return !memories
.empty();
1487 bool RTLIL::Module::has_processes() const
1489 return !processes
.empty();
1492 bool RTLIL::Module::has_memories_warn() const
1494 if (!memories
.empty())
1495 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1496 return !memories
.empty();
1499 bool RTLIL::Module::has_processes_warn() const
1501 if (!processes
.empty())
1502 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1503 return !processes
.empty();
1506 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1508 std::vector
<RTLIL::Wire
*> result
;
1509 result
.reserve(wires_
.size());
1510 for (auto &it
: wires_
)
1511 if (design
->selected(this, it
.second
))
1512 result
.push_back(it
.second
);
1516 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1518 std::vector
<RTLIL::Cell
*> result
;
1519 result
.reserve(wires_
.size());
1520 for (auto &it
: cells_
)
1521 if (design
->selected(this, it
.second
))
1522 result
.push_back(it
.second
);
1526 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1528 log_assert(!wire
->name
.empty());
1529 log_assert(count_id(wire
->name
) == 0);
1530 log_assert(refcount_wires_
== 0);
1531 wires_
[wire
->name
] = wire
;
1532 wire
->module
= this;
1535 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1537 log_assert(!cell
->name
.empty());
1538 log_assert(count_id(cell
->name
) == 0);
1539 log_assert(refcount_cells_
== 0);
1540 cells_
[cell
->name
] = cell
;
1541 cell
->module
= this;
1544 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1546 log_assert(refcount_wires_
== 0);
1548 struct DeleteWireWorker
1550 RTLIL::Module
*module
;
1551 const pool
<RTLIL::Wire
*> *wires_p
;
1553 void operator()(RTLIL::SigSpec
&sig
) {
1554 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1555 for (auto &c
: chunks
)
1556 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1557 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1563 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1564 log_assert(GetSize(lhs
) == GetSize(rhs
));
1565 RTLIL::SigSpec new_lhs
, new_rhs
;
1566 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1567 RTLIL::SigBit lhs_bit
= lhs
[i
];
1568 if (lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
))
1570 RTLIL::SigBit rhs_bit
= rhs
[i
];
1571 if (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))
1573 new_lhs
.append(lhs_bit
);
1574 new_rhs
.append(rhs_bit
);
1581 DeleteWireWorker delete_wire_worker
;
1582 delete_wire_worker
.module
= this;
1583 delete_wire_worker
.wires_p
= &wires
;
1584 rewrite_sigspecs2(delete_wire_worker
);
1586 for (auto &it
: wires
) {
1587 log_assert(wires_
.count(it
->name
) != 0);
1588 wires_
.erase(it
->name
);
1593 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1595 auto it
= cells_
.find(cell
->name
);
1596 log_assert(it
!= cells_
.end());
1600 dict
<RTLIL::IdString
, RTLIL::Cell
*>::iterator
RTLIL::Module::remove(dict
<RTLIL::IdString
, RTLIL::Cell
*>::iterator it
)
1602 RTLIL::Cell
*cell
= it
->second
;
1603 while (!cell
->connections_
.empty())
1604 cell
->unsetPort(cell
->connections_
.begin()->first
);
1606 log_assert(refcount_cells_
== 0);
1607 it
= cells_
.erase(it
);
1612 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1614 log_assert(wires_
[wire
->name
] == wire
);
1615 log_assert(refcount_wires_
== 0);
1616 wires_
.erase(wire
->name
);
1617 wire
->name
= new_name
;
1621 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1623 log_assert(cells_
[cell
->name
] == cell
);
1624 log_assert(refcount_wires_
== 0);
1625 cells_
.erase(cell
->name
);
1626 cell
->name
= new_name
;
1630 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1632 log_assert(count_id(old_name
) != 0);
1633 if (wires_
.count(old_name
))
1634 rename(wires_
.at(old_name
), new_name
);
1635 else if (cells_
.count(old_name
))
1636 rename(cells_
.at(old_name
), new_name
);
1641 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1643 log_assert(wires_
[w1
->name
] == w1
);
1644 log_assert(wires_
[w2
->name
] == w2
);
1645 log_assert(refcount_wires_
== 0);
1647 wires_
.erase(w1
->name
);
1648 wires_
.erase(w2
->name
);
1650 std::swap(w1
->name
, w2
->name
);
1652 wires_
[w1
->name
] = w1
;
1653 wires_
[w2
->name
] = w2
;
1656 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1658 log_assert(cells_
[c1
->name
] == c1
);
1659 log_assert(cells_
[c2
->name
] == c2
);
1660 log_assert(refcount_cells_
== 0);
1662 cells_
.erase(c1
->name
);
1663 cells_
.erase(c2
->name
);
1665 std::swap(c1
->name
, c2
->name
);
1667 cells_
[c1
->name
] = c1
;
1668 cells_
[c2
->name
] = c2
;
1671 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1674 return uniquify(name
, index
);
1677 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1680 if (count_id(name
) == 0)
1686 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1687 if (count_id(new_name
) == 0)
1693 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1695 if (a
->port_id
&& !b
->port_id
)
1697 if (!a
->port_id
&& b
->port_id
)
1700 if (a
->port_id
== b
->port_id
)
1701 return a
->name
< b
->name
;
1702 return a
->port_id
< b
->port_id
;
1705 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1707 for (auto mon
: monitors
)
1708 mon
->notify_connect(this, conn
);
1711 for (auto mon
: design
->monitors
)
1712 mon
->notify_connect(this, conn
);
1714 // ignore all attempts to assign constants to other constants
1715 if (conn
.first
.has_const()) {
1716 RTLIL::SigSig new_conn
;
1717 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1718 if (conn
.first
[i
].wire
) {
1719 new_conn
.first
.append(conn
.first
[i
]);
1720 new_conn
.second
.append(conn
.second
[i
]);
1722 if (GetSize(new_conn
.first
))
1728 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
));
1729 log_backtrace("-X- ", yosys_xtrace
-1);
1732 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1733 connections_
.push_back(conn
);
1736 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1738 connect(RTLIL::SigSig(lhs
, rhs
));
1741 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1743 for (auto mon
: monitors
)
1744 mon
->notify_connect(this, new_conn
);
1747 for (auto mon
: design
->monitors
)
1748 mon
->notify_connect(this, new_conn
);
1751 log("#X# New connections vector in %s:\n", log_id(this));
1752 for (auto &conn
: new_conn
)
1753 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1754 log_backtrace("-X- ", yosys_xtrace
-1);
1757 connections_
= new_conn
;
1760 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1762 return connections_
;
1765 void RTLIL::Module::fixup_ports()
1767 std::vector
<RTLIL::Wire
*> all_ports
;
1769 for (auto &w
: wires_
)
1770 if (w
.second
->port_input
|| w
.second
->port_output
)
1771 all_ports
.push_back(w
.second
);
1773 w
.second
->port_id
= 0;
1775 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1778 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1779 ports
.push_back(all_ports
[i
]->name
);
1780 all_ports
[i
]->port_id
= i
+1;
1784 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1786 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1788 wire
->width
= width
;
1793 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1795 RTLIL::Wire
*wire
= addWire(name
);
1796 wire
->width
= other
->width
;
1797 wire
->start_offset
= other
->start_offset
;
1798 wire
->port_id
= other
->port_id
;
1799 wire
->port_input
= other
->port_input
;
1800 wire
->port_output
= other
->port_output
;
1801 wire
->upto
= other
->upto
;
1802 wire
->attributes
= other
->attributes
;
1806 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1808 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1815 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1817 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1818 cell
->connections_
= other
->connections_
;
1819 cell
->parameters
= other
->parameters
;
1820 cell
->attributes
= other
->attributes
;
1824 #define DEF_METHOD(_func, _y_size, _type) \
1825 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1826 RTLIL::Cell *cell = addCell(name, _type); \
1827 cell->parameters["\\A_SIGNED"] = is_signed; \
1828 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1829 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1830 cell->setPort("\\A", sig_a); \
1831 cell->setPort("\\Y", sig_y); \
1832 cell->set_src_attribute(src); \
1835 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1836 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1837 add ## _func(name, sig_a, sig_y, is_signed, src); \
1840 DEF_METHOD(Not
, sig_a
.size(), "$not")
1841 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1842 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1843 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1844 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1845 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1846 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1847 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1848 DEF_METHOD(LogicNot
, 1, "$logic_not")
1851 #define DEF_METHOD(_func, _y_size, _type) \
1852 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) { \
1853 RTLIL::Cell *cell = addCell(name, _type); \
1854 cell->parameters["\\A_SIGNED"] = is_signed; \
1855 cell->parameters["\\B_SIGNED"] = is_signed; \
1856 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1857 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1858 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1859 cell->setPort("\\A", sig_a); \
1860 cell->setPort("\\B", sig_b); \
1861 cell->setPort("\\Y", sig_y); \
1862 cell->set_src_attribute(src); \
1865 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1866 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1867 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1870 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), "$and")
1871 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), "$or")
1872 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), "$xor")
1873 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), "$xnor")
1874 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1875 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1876 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1877 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1878 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1879 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1880 DEF_METHOD(Lt
, 1, "$lt")
1881 DEF_METHOD(Le
, 1, "$le")
1882 DEF_METHOD(Eq
, 1, "$eq")
1883 DEF_METHOD(Ne
, 1, "$ne")
1884 DEF_METHOD(Eqx
, 1, "$eqx")
1885 DEF_METHOD(Nex
, 1, "$nex")
1886 DEF_METHOD(Ge
, 1, "$ge")
1887 DEF_METHOD(Gt
, 1, "$gt")
1888 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), "$add")
1889 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), "$sub")
1890 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), "$mul")
1891 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), "$div")
1892 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), "$mod")
1893 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1894 DEF_METHOD(LogicOr
, 1, "$logic_or")
1897 #define DEF_METHOD(_func, _type, _pmux) \
1898 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) { \
1899 RTLIL::Cell *cell = addCell(name, _type); \
1900 cell->parameters["\\WIDTH"] = sig_a.size(); \
1901 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1902 cell->setPort("\\A", sig_a); \
1903 cell->setPort("\\B", sig_b); \
1904 cell->setPort("\\S", sig_s); \
1905 cell->setPort("\\Y", sig_y); \
1906 cell->set_src_attribute(src); \
1909 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1910 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1911 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1914 DEF_METHOD(Mux
, "$mux", 0)
1915 DEF_METHOD(Pmux
, "$pmux", 1)
1918 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1919 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1920 RTLIL::Cell *cell = addCell(name, _type); \
1921 cell->setPort("\\" #_P1, sig1); \
1922 cell->setPort("\\" #_P2, sig2); \
1923 cell->set_src_attribute(src); \
1926 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1927 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1928 add ## _func(name, sig1, sig2, src); \
1931 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1932 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1933 RTLIL::Cell *cell = addCell(name, _type); \
1934 cell->setPort("\\" #_P1, sig1); \
1935 cell->setPort("\\" #_P2, sig2); \
1936 cell->setPort("\\" #_P3, sig3); \
1937 cell->set_src_attribute(src); \
1940 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1941 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1942 add ## _func(name, sig1, sig2, sig3, src); \
1945 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1946 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) { \
1947 RTLIL::Cell *cell = addCell(name, _type); \
1948 cell->setPort("\\" #_P1, sig1); \
1949 cell->setPort("\\" #_P2, sig2); \
1950 cell->setPort("\\" #_P3, sig3); \
1951 cell->setPort("\\" #_P4, sig4); \
1952 cell->set_src_attribute(src); \
1955 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1956 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1957 add ## _func(name, sig1, sig2, sig3, sig4, src); \
1960 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1961 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) { \
1962 RTLIL::Cell *cell = addCell(name, _type); \
1963 cell->setPort("\\" #_P1, sig1); \
1964 cell->setPort("\\" #_P2, sig2); \
1965 cell->setPort("\\" #_P3, sig3); \
1966 cell->setPort("\\" #_P4, sig4); \
1967 cell->setPort("\\" #_P5, sig5); \
1968 cell->set_src_attribute(src); \
1971 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1972 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1973 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
1976 DEF_METHOD_2(BufGate
, "$_BUF_", A
, Y
)
1977 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1978 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1979 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1980 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1981 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1982 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1983 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1984 DEF_METHOD_3(AndnotGate
, "$_ANDNOT_", A
, B
, Y
)
1985 DEF_METHOD_3(OrnotGate
, "$_ORNOT_", A
, B
, Y
)
1986 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1987 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1988 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1989 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1990 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1996 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
)
1998 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1999 cell
->parameters
["\\A_SIGNED"] = a_signed
;
2000 cell
->parameters
["\\B_SIGNED"] = b_signed
;
2001 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
2002 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
2003 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
2004 cell
->setPort("\\A", sig_a
);
2005 cell
->setPort("\\B", sig_b
);
2006 cell
->setPort("\\Y", sig_y
);
2007 cell
->set_src_attribute(src
);
2011 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
2013 RTLIL::Cell
*cell
= addCell(name
, "$slice");
2014 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
2015 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
2016 cell
->parameters
["\\OFFSET"] = offset
;
2017 cell
->setPort("\\A", sig_a
);
2018 cell
->setPort("\\Y", sig_y
);
2019 cell
->set_src_attribute(src
);
2023 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2025 RTLIL::Cell
*cell
= addCell(name
, "$concat");
2026 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
2027 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
2028 cell
->setPort("\\A", sig_a
);
2029 cell
->setPort("\\B", sig_b
);
2030 cell
->setPort("\\Y", sig_y
);
2031 cell
->set_src_attribute(src
);
2035 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
2037 RTLIL::Cell
*cell
= addCell(name
, "$lut");
2038 cell
->parameters
["\\LUT"] = lut
;
2039 cell
->parameters
["\\WIDTH"] = sig_a
.size();
2040 cell
->setPort("\\A", sig_a
);
2041 cell
->setPort("\\Y", sig_y
);
2042 cell
->set_src_attribute(src
);
2046 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2048 RTLIL::Cell
*cell
= addCell(name
, "$tribuf");
2049 cell
->parameters
["\\WIDTH"] = sig_a
.size();
2050 cell
->setPort("\\A", sig_a
);
2051 cell
->setPort("\\EN", sig_en
);
2052 cell
->setPort("\\Y", sig_y
);
2053 cell
->set_src_attribute(src
);
2057 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2059 RTLIL::Cell
*cell
= addCell(name
, "$assert");
2060 cell
->setPort("\\A", sig_a
);
2061 cell
->setPort("\\EN", sig_en
);
2062 cell
->set_src_attribute(src
);
2066 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2068 RTLIL::Cell
*cell
= addCell(name
, "$assume");
2069 cell
->setPort("\\A", sig_a
);
2070 cell
->setPort("\\EN", sig_en
);
2071 cell
->set_src_attribute(src
);
2075 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2077 RTLIL::Cell
*cell
= addCell(name
, "$live");
2078 cell
->setPort("\\A", sig_a
);
2079 cell
->setPort("\\EN", sig_en
);
2080 cell
->set_src_attribute(src
);
2084 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2086 RTLIL::Cell
*cell
= addCell(name
, "$fair");
2087 cell
->setPort("\\A", sig_a
);
2088 cell
->setPort("\\EN", sig_en
);
2089 cell
->set_src_attribute(src
);
2093 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2095 RTLIL::Cell
*cell
= addCell(name
, "$cover");
2096 cell
->setPort("\\A", sig_a
);
2097 cell
->setPort("\\EN", sig_en
);
2098 cell
->set_src_attribute(src
);
2102 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2104 RTLIL::Cell
*cell
= addCell(name
, "$equiv");
2105 cell
->setPort("\\A", sig_a
);
2106 cell
->setPort("\\B", sig_b
);
2107 cell
->setPort("\\Y", sig_y
);
2108 cell
->set_src_attribute(src
);
2112 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
)
2114 RTLIL::Cell
*cell
= addCell(name
, "$sr");
2115 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2116 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2117 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2118 cell
->setPort("\\SET", sig_set
);
2119 cell
->setPort("\\CLR", sig_clr
);
2120 cell
->setPort("\\Q", sig_q
);
2121 cell
->set_src_attribute(src
);
2125 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2127 RTLIL::Cell
*cell
= addCell(name
, "$ff");
2128 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2129 cell
->setPort("\\D", sig_d
);
2130 cell
->setPort("\\Q", sig_q
);
2131 cell
->set_src_attribute(src
);
2135 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
)
2137 RTLIL::Cell
*cell
= addCell(name
, "$dff");
2138 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2139 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2140 cell
->setPort("\\CLK", sig_clk
);
2141 cell
->setPort("\\D", sig_d
);
2142 cell
->setPort("\\Q", sig_q
);
2143 cell
->set_src_attribute(src
);
2147 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
)
2149 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
2150 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2151 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2152 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2153 cell
->setPort("\\CLK", sig_clk
);
2154 cell
->setPort("\\EN", sig_en
);
2155 cell
->setPort("\\D", sig_d
);
2156 cell
->setPort("\\Q", sig_q
);
2157 cell
->set_src_attribute(src
);
2161 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2162 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2164 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
2165 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2166 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2167 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2168 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2169 cell
->setPort("\\CLK", sig_clk
);
2170 cell
->setPort("\\SET", sig_set
);
2171 cell
->setPort("\\CLR", sig_clr
);
2172 cell
->setPort("\\D", sig_d
);
2173 cell
->setPort("\\Q", sig_q
);
2174 cell
->set_src_attribute(src
);
2178 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2179 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2181 RTLIL::Cell
*cell
= addCell(name
, "$adff");
2182 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2183 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
2184 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
2185 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2186 cell
->setPort("\\CLK", sig_clk
);
2187 cell
->setPort("\\ARST", sig_arst
);
2188 cell
->setPort("\\D", sig_d
);
2189 cell
->setPort("\\Q", sig_q
);
2190 cell
->set_src_attribute(src
);
2194 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
)
2196 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
2197 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2198 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2199 cell
->setPort("\\EN", sig_en
);
2200 cell
->setPort("\\D", sig_d
);
2201 cell
->setPort("\\Q", sig_q
);
2202 cell
->set_src_attribute(src
);
2206 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2207 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2209 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
2210 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2211 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2212 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2213 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2214 cell
->setPort("\\EN", sig_en
);
2215 cell
->setPort("\\SET", sig_set
);
2216 cell
->setPort("\\CLR", sig_clr
);
2217 cell
->setPort("\\D", sig_d
);
2218 cell
->setPort("\\Q", sig_q
);
2219 cell
->set_src_attribute(src
);
2223 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2225 RTLIL::Cell
*cell
= addCell(name
, "$_FF_");
2226 cell
->setPort("\\D", sig_d
);
2227 cell
->setPort("\\Q", sig_q
);
2228 cell
->set_src_attribute(src
);
2232 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
)
2234 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2235 cell
->setPort("\\C", sig_clk
);
2236 cell
->setPort("\\D", sig_d
);
2237 cell
->setPort("\\Q", sig_q
);
2238 cell
->set_src_attribute(src
);
2242 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
)
2244 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2245 cell
->setPort("\\C", sig_clk
);
2246 cell
->setPort("\\E", sig_en
);
2247 cell
->setPort("\\D", sig_d
);
2248 cell
->setPort("\\Q", sig_q
);
2249 cell
->set_src_attribute(src
);
2253 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2254 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2256 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2257 cell
->setPort("\\C", sig_clk
);
2258 cell
->setPort("\\S", sig_set
);
2259 cell
->setPort("\\R", sig_clr
);
2260 cell
->setPort("\\D", sig_d
);
2261 cell
->setPort("\\Q", sig_q
);
2262 cell
->set_src_attribute(src
);
2266 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2267 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2269 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2270 cell
->setPort("\\C", sig_clk
);
2271 cell
->setPort("\\R", sig_arst
);
2272 cell
->setPort("\\D", sig_d
);
2273 cell
->setPort("\\Q", sig_q
);
2274 cell
->set_src_attribute(src
);
2278 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
)
2280 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2281 cell
->setPort("\\E", sig_en
);
2282 cell
->setPort("\\D", sig_d
);
2283 cell
->setPort("\\Q", sig_q
);
2284 cell
->set_src_attribute(src
);
2288 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2289 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2291 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2292 cell
->setPort("\\E", sig_en
);
2293 cell
->setPort("\\S", sig_set
);
2294 cell
->setPort("\\R", sig_clr
);
2295 cell
->setPort("\\D", sig_d
);
2296 cell
->setPort("\\Q", sig_q
);
2297 cell
->set_src_attribute(src
);
2301 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2303 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2304 Cell
*cell
= addCell(name
, "$anyconst");
2305 cell
->setParam("\\WIDTH", width
);
2306 cell
->setPort("\\Y", sig
);
2307 cell
->set_src_attribute(src
);
2311 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2313 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2314 Cell
*cell
= addCell(name
, "$anyseq");
2315 cell
->setParam("\\WIDTH", width
);
2316 cell
->setPort("\\Y", sig
);
2317 cell
->set_src_attribute(src
);
2321 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2323 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2324 Cell
*cell
= addCell(name
, "$allconst");
2325 cell
->setParam("\\WIDTH", width
);
2326 cell
->setPort("\\Y", sig
);
2327 cell
->set_src_attribute(src
);
2331 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2333 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2334 Cell
*cell
= addCell(name
, "$allseq");
2335 cell
->setParam("\\WIDTH", width
);
2336 cell
->setPort("\\Y", sig
);
2337 cell
->set_src_attribute(src
);
2341 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2343 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2344 Cell
*cell
= addCell(name
, "$initstate");
2345 cell
->setPort("\\Y", sig
);
2346 cell
->set_src_attribute(src
);
2352 static unsigned int hashidx_count
= 123456789;
2353 hashidx_count
= mkhash_xorshift(hashidx_count
);
2354 hashidx_
= hashidx_count
;
2361 port_output
= false;
2365 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2369 RTLIL::Wire::~Wire()
2372 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2377 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2378 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2384 RTLIL::Memory::Memory()
2386 static unsigned int hashidx_count
= 123456789;
2387 hashidx_count
= mkhash_xorshift(hashidx_count
);
2388 hashidx_
= hashidx_count
;
2394 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2398 RTLIL::Cell::Cell() : module(nullptr)
2400 static unsigned int hashidx_count
= 123456789;
2401 hashidx_count
= mkhash_xorshift(hashidx_count
);
2402 hashidx_
= hashidx_count
;
2404 // log("#memtrace# %p\n", this);
2408 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2412 RTLIL::Cell::~Cell()
2415 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2420 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2421 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2427 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2429 return connections_
.count(portname
) != 0;
2432 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2434 RTLIL::SigSpec signal
;
2435 auto conn_it
= connections_
.find(portname
);
2437 if (conn_it
!= connections_
.end())
2439 for (auto mon
: module
->monitors
)
2440 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2443 for (auto mon
: module
->design
->monitors
)
2444 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2447 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2448 log_backtrace("-X- ", yosys_xtrace
-1);
2451 connections_
.erase(conn_it
);
2455 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2457 auto conn_it
= connections_
.find(portname
);
2459 if (conn_it
== connections_
.end()) {
2460 connections_
[portname
] = RTLIL::SigSpec();
2461 conn_it
= connections_
.find(portname
);
2462 log_assert(conn_it
!= connections_
.end());
2464 if (conn_it
->second
== signal
)
2467 for (auto mon
: module
->monitors
)
2468 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2471 for (auto mon
: module
->design
->monitors
)
2472 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2475 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2476 log_backtrace("-X- ", yosys_xtrace
-1);
2479 conn_it
->second
= signal
;
2482 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2484 return connections_
.at(portname
);
2487 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2489 return connections_
;
2492 bool RTLIL::Cell::known() const
2494 if (yosys_celltypes
.cell_known(type
))
2496 if (module
&& module
->design
&& module
->design
->module(type
))
2501 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2503 if (yosys_celltypes
.cell_known(type
))
2504 return yosys_celltypes
.cell_input(type
, portname
);
2505 if (module
&& module
->design
) {
2506 RTLIL::Module
*m
= module
->design
->module(type
);
2507 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2508 return w
&& w
->port_input
;
2513 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2515 if (yosys_celltypes
.cell_known(type
))
2516 return yosys_celltypes
.cell_output(type
, portname
);
2517 if (module
&& module
->design
) {
2518 RTLIL::Module
*m
= module
->design
->module(type
);
2519 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2520 return w
&& w
->port_output
;
2525 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2527 return parameters
.count(paramname
) != 0;
2530 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2532 parameters
.erase(paramname
);
2535 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2537 parameters
[paramname
] = value
;
2540 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2542 return parameters
.at(paramname
);
2545 void RTLIL::Cell::sort()
2547 connections_
.sort(sort_by_id_str());
2548 parameters
.sort(sort_by_id_str());
2549 attributes
.sort(sort_by_id_str());
2552 void RTLIL::Cell::check()
2555 InternalCellChecker
checker(NULL
, this);
2560 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2562 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" || type
.substr(0,10) == "$fmcombine" ||
2563 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
2566 if (type
== "$mux" || type
== "$pmux") {
2567 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2568 if (type
== "$pmux")
2569 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
2574 if (type
== "$lut" || type
== "$sop") {
2575 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
2579 if (type
== "$fa") {
2580 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2584 if (type
== "$lcu") {
2585 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
2589 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
2591 if (connections_
.count("\\A")) {
2592 if (signedness_ab
) {
2594 parameters
["\\A_SIGNED"] = true;
2595 else if (parameters
.count("\\A_SIGNED") == 0)
2596 parameters
["\\A_SIGNED"] = false;
2598 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
2601 if (connections_
.count("\\B")) {
2602 if (signedness_ab
) {
2604 parameters
["\\B_SIGNED"] = true;
2605 else if (parameters
.count("\\B_SIGNED") == 0)
2606 parameters
["\\B_SIGNED"] = false;
2608 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
2611 if (connections_
.count("\\Y"))
2612 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
2614 if (connections_
.count("\\Q"))
2615 parameters
["\\WIDTH"] = GetSize(connections_
["\\Q"]);
2620 RTLIL::SigChunk::SigChunk()
2627 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2631 width
= GetSize(data
);
2635 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2637 log_assert(wire
!= nullptr);
2639 this->width
= wire
->width
;
2643 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2645 log_assert(wire
!= nullptr);
2647 this->width
= width
;
2648 this->offset
= offset
;
2651 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2654 data
= RTLIL::Const(str
).bits
;
2655 width
= GetSize(data
);
2659 RTLIL::SigChunk::SigChunk(int val
, int width
)
2662 data
= RTLIL::Const(val
, width
).bits
;
2663 this->width
= GetSize(data
);
2667 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2670 data
= RTLIL::Const(bit
, width
).bits
;
2671 this->width
= GetSize(data
);
2675 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2680 data
= RTLIL::Const(bit
.data
).bits
;
2682 offset
= bit
.offset
;
2686 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2688 wire
= sigchunk
.wire
;
2689 data
= sigchunk
.data
;
2690 width
= sigchunk
.width
;
2691 offset
= sigchunk
.offset
;
2694 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2696 RTLIL::SigChunk ret
;
2699 ret
.offset
= this->offset
+ offset
;
2702 for (int i
= 0; i
< length
; i
++)
2703 ret
.data
.push_back(data
[offset
+i
]);
2709 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2711 if (wire
&& other
.wire
)
2712 if (wire
->name
!= other
.wire
->name
)
2713 return wire
->name
< other
.wire
->name
;
2715 if (wire
!= other
.wire
)
2716 return wire
< other
.wire
;
2718 if (offset
!= other
.offset
)
2719 return offset
< other
.offset
;
2721 if (width
!= other
.width
)
2722 return width
< other
.width
;
2724 return data
< other
.data
;
2727 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2729 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2732 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2739 RTLIL::SigSpec::SigSpec()
2745 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2750 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2752 cover("kernel.rtlil.sigspec.init.list");
2757 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2758 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2762 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2764 cover("kernel.rtlil.sigspec.assign");
2766 width_
= other
.width_
;
2767 hash_
= other
.hash_
;
2768 chunks_
= other
.chunks_
;
2771 if (!other
.bits_
.empty())
2773 RTLIL::SigChunk
*last
= NULL
;
2774 int last_end_offset
= 0;
2776 for (auto &bit
: other
.bits_
) {
2777 if (last
&& bit
.wire
== last
->wire
) {
2778 if (bit
.wire
== NULL
) {
2779 last
->data
.push_back(bit
.data
);
2782 } else if (last_end_offset
== bit
.offset
) {
2788 chunks_
.push_back(bit
);
2789 last
= &chunks_
.back();
2790 last_end_offset
= bit
.offset
+ 1;
2799 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2801 cover("kernel.rtlil.sigspec.init.const");
2803 chunks_
.push_back(RTLIL::SigChunk(value
));
2804 width_
= chunks_
.back().width
;
2809 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2811 cover("kernel.rtlil.sigspec.init.chunk");
2813 chunks_
.push_back(chunk
);
2814 width_
= chunks_
.back().width
;
2819 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2821 cover("kernel.rtlil.sigspec.init.wire");
2823 chunks_
.push_back(RTLIL::SigChunk(wire
));
2824 width_
= chunks_
.back().width
;
2829 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2831 cover("kernel.rtlil.sigspec.init.wire_part");
2833 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2834 width_
= chunks_
.back().width
;
2839 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2841 cover("kernel.rtlil.sigspec.init.str");
2843 chunks_
.push_back(RTLIL::SigChunk(str
));
2844 width_
= chunks_
.back().width
;
2849 RTLIL::SigSpec::SigSpec(int val
, int width
)
2851 cover("kernel.rtlil.sigspec.init.int");
2853 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2859 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2861 cover("kernel.rtlil.sigspec.init.state");
2863 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2869 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2871 cover("kernel.rtlil.sigspec.init.bit");
2873 if (bit
.wire
== NULL
)
2874 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2876 for (int i
= 0; i
< width
; i
++)
2877 chunks_
.push_back(bit
);
2883 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2885 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2889 for (auto &c
: chunks
)
2894 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2896 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2900 for (auto &bit
: bits
)
2905 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2907 cover("kernel.rtlil.sigspec.init.pool_bits");
2911 for (auto &bit
: bits
)
2916 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2918 cover("kernel.rtlil.sigspec.init.stdset_bits");
2922 for (auto &bit
: bits
)
2927 RTLIL::SigSpec::SigSpec(bool bit
)
2929 cover("kernel.rtlil.sigspec.init.bool");
2937 void RTLIL::SigSpec::pack() const
2939 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2941 if (that
->bits_
.empty())
2944 cover("kernel.rtlil.sigspec.convert.pack");
2945 log_assert(that
->chunks_
.empty());
2947 std::vector
<RTLIL::SigBit
> old_bits
;
2948 old_bits
.swap(that
->bits_
);
2950 RTLIL::SigChunk
*last
= NULL
;
2951 int last_end_offset
= 0;
2953 for (auto &bit
: old_bits
) {
2954 if (last
&& bit
.wire
== last
->wire
) {
2955 if (bit
.wire
== NULL
) {
2956 last
->data
.push_back(bit
.data
);
2959 } else if (last_end_offset
== bit
.offset
) {
2965 that
->chunks_
.push_back(bit
);
2966 last
= &that
->chunks_
.back();
2967 last_end_offset
= bit
.offset
+ 1;
2973 void RTLIL::SigSpec::unpack() const
2975 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2977 if (that
->chunks_
.empty())
2980 cover("kernel.rtlil.sigspec.convert.unpack");
2981 log_assert(that
->bits_
.empty());
2983 that
->bits_
.reserve(that
->width_
);
2984 for (auto &c
: that
->chunks_
)
2985 for (int i
= 0; i
< c
.width
; i
++)
2986 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2988 that
->chunks_
.clear();
2992 void RTLIL::SigSpec::updhash() const
2994 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2996 if (that
->hash_
!= 0)
2999 cover("kernel.rtlil.sigspec.hash");
3002 that
->hash_
= mkhash_init
;
3003 for (auto &c
: that
->chunks_
)
3004 if (c
.wire
== NULL
) {
3005 for (auto &v
: c
.data
)
3006 that
->hash_
= mkhash(that
->hash_
, v
);
3008 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3009 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3010 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3013 if (that
->hash_
== 0)
3017 void RTLIL::SigSpec::sort()
3020 cover("kernel.rtlil.sigspec.sort");
3021 std::sort(bits_
.begin(), bits_
.end());
3024 void RTLIL::SigSpec::sort_and_unify()
3027 cover("kernel.rtlil.sigspec.sort_and_unify");
3029 // A copy of the bits vector is used to prevent duplicating the logic from
3030 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3031 // that isn't showing up as significant in profiles.
3032 std::vector
<SigBit
> unique_bits
= bits_
;
3033 std::sort(unique_bits
.begin(), unique_bits
.end());
3034 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3035 unique_bits
.erase(last
, unique_bits
.end());
3037 *this = unique_bits
;
3040 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3042 replace(pattern
, with
, this);
3045 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3047 log_assert(other
!= NULL
);
3048 log_assert(width_
== other
->width_
);
3049 log_assert(pattern
.width_
== with
.width_
);
3056 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3057 if (pattern
.bits_
[i
].wire
!= NULL
) {
3058 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3059 if (bits_
[j
] == pattern
.bits_
[i
]) {
3060 other
->bits_
[j
] = with
.bits_
[i
];
3069 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3071 replace(rules
, this);
3074 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3076 cover("kernel.rtlil.sigspec.replace_dict");
3078 log_assert(other
!= NULL
);
3079 log_assert(width_
== other
->width_
);
3084 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3085 auto it
= rules
.find(bits_
[i
]);
3086 if (it
!= rules
.end())
3087 other
->bits_
[i
] = it
->second
;
3093 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3095 replace(rules
, this);
3098 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3100 cover("kernel.rtlil.sigspec.replace_map");
3102 log_assert(other
!= NULL
);
3103 log_assert(width_
== other
->width_
);
3108 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3109 auto it
= rules
.find(bits_
[i
]);
3110 if (it
!= rules
.end())
3111 other
->bits_
[i
] = it
->second
;
3117 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3119 remove2(pattern
, NULL
);
3122 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3124 RTLIL::SigSpec tmp
= *this;
3125 tmp
.remove2(pattern
, other
);
3128 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3131 cover("kernel.rtlil.sigspec.remove_other");
3133 cover("kernel.rtlil.sigspec.remove");
3136 if (other
!= NULL
) {
3137 log_assert(width_
== other
->width_
);
3141 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3143 if (bits_
[i
].wire
== NULL
) continue;
3145 for (auto &pattern_chunk
: pattern
.chunks())
3146 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3147 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3148 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3149 bits_
.erase(bits_
.begin() + i
);
3151 if (other
!= NULL
) {
3152 other
->bits_
.erase(other
->bits_
.begin() + i
);
3162 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3164 remove2(pattern
, NULL
);
3167 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3169 RTLIL::SigSpec tmp
= *this;
3170 tmp
.remove2(pattern
, other
);
3173 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3176 cover("kernel.rtlil.sigspec.remove_other");
3178 cover("kernel.rtlil.sigspec.remove");
3182 if (other
!= NULL
) {
3183 log_assert(width_
== other
->width_
);
3187 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3188 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3189 bits_
.erase(bits_
.begin() + i
);
3191 if (other
!= NULL
) {
3192 other
->bits_
.erase(other
->bits_
.begin() + i
);
3201 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3204 cover("kernel.rtlil.sigspec.remove_other");
3206 cover("kernel.rtlil.sigspec.remove");
3210 if (other
!= NULL
) {
3211 log_assert(width_
== other
->width_
);
3215 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3216 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3217 bits_
.erase(bits_
.begin() + i
);
3219 if (other
!= NULL
) {
3220 other
->bits_
.erase(other
->bits_
.begin() + i
);
3229 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3232 cover("kernel.rtlil.sigspec.extract_other");
3234 cover("kernel.rtlil.sigspec.extract");
3236 log_assert(other
== NULL
|| width_
== other
->width_
);
3239 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3241 for (auto& pattern_chunk
: pattern
.chunks()) {
3243 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3244 for (int i
= 0; i
< width_
; i
++)
3245 if (bits_match
[i
].wire
&&
3246 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3247 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3248 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3249 ret
.append_bit(bits_other
[i
]);
3251 for (int i
= 0; i
< width_
; i
++)
3252 if (bits_match
[i
].wire
&&
3253 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3254 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3255 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3256 ret
.append_bit(bits_match
[i
]);
3264 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3267 cover("kernel.rtlil.sigspec.extract_other");
3269 cover("kernel.rtlil.sigspec.extract");
3271 log_assert(other
== NULL
|| width_
== other
->width_
);
3273 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3277 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3278 for (int i
= 0; i
< width_
; i
++)
3279 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3280 ret
.append_bit(bits_other
[i
]);
3282 for (int i
= 0; i
< width_
; i
++)
3283 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3284 ret
.append_bit(bits_match
[i
]);
3291 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3293 cover("kernel.rtlil.sigspec.replace_pos");
3298 log_assert(offset
>= 0);
3299 log_assert(with
.width_
>= 0);
3300 log_assert(offset
+with
.width_
<= width_
);
3302 for (int i
= 0; i
< with
.width_
; i
++)
3303 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3308 void RTLIL::SigSpec::remove_const()
3312 cover("kernel.rtlil.sigspec.remove_const.packed");
3314 std::vector
<RTLIL::SigChunk
> new_chunks
;
3315 new_chunks
.reserve(GetSize(chunks_
));
3318 for (auto &chunk
: chunks_
)
3319 if (chunk
.wire
!= NULL
) {
3320 new_chunks
.push_back(chunk
);
3321 width_
+= chunk
.width
;
3324 chunks_
.swap(new_chunks
);
3328 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3330 std::vector
<RTLIL::SigBit
> new_bits
;
3331 new_bits
.reserve(width_
);
3333 for (auto &bit
: bits_
)
3334 if (bit
.wire
!= NULL
)
3335 new_bits
.push_back(bit
);
3337 bits_
.swap(new_bits
);
3338 width_
= bits_
.size();
3344 void RTLIL::SigSpec::remove(int offset
, int length
)
3346 cover("kernel.rtlil.sigspec.remove_pos");
3350 log_assert(offset
>= 0);
3351 log_assert(length
>= 0);
3352 log_assert(offset
+ length
<= width_
);
3354 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3355 width_
= bits_
.size();
3360 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3363 cover("kernel.rtlil.sigspec.extract_pos");
3364 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3367 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3369 if (signal
.width_
== 0)
3377 cover("kernel.rtlil.sigspec.append");
3379 if (packed() != signal
.packed()) {
3385 for (auto &other_c
: signal
.chunks_
)
3387 auto &my_last_c
= chunks_
.back();
3388 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3389 auto &this_data
= my_last_c
.data
;
3390 auto &other_data
= other_c
.data
;
3391 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3392 my_last_c
.width
+= other_c
.width
;
3394 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3395 my_last_c
.width
+= other_c
.width
;
3397 chunks_
.push_back(other_c
);
3400 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3402 width_
+= signal
.width_
;
3406 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3410 cover("kernel.rtlil.sigspec.append_bit.packed");
3412 if (chunks_
.size() == 0)
3413 chunks_
.push_back(bit
);
3415 if (bit
.wire
== NULL
)
3416 if (chunks_
.back().wire
== NULL
) {
3417 chunks_
.back().data
.push_back(bit
.data
);
3418 chunks_
.back().width
++;
3420 chunks_
.push_back(bit
);
3422 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3423 chunks_
.back().width
++;
3425 chunks_
.push_back(bit
);
3429 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3430 bits_
.push_back(bit
);
3437 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3439 cover("kernel.rtlil.sigspec.extend_u0");
3444 remove(width
, width_
- width
);
3446 if (width_
< width
) {
3447 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3449 padding
= RTLIL::State::S0
;
3450 while (width_
< width
)
3456 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3458 cover("kernel.rtlil.sigspec.repeat");
3461 for (int i
= 0; i
< num
; i
++)
3467 void RTLIL::SigSpec::check() const
3471 cover("kernel.rtlil.sigspec.check.skip");
3475 cover("kernel.rtlil.sigspec.check.packed");
3478 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3479 const RTLIL::SigChunk chunk
= chunks_
[i
];
3480 if (chunk
.wire
== NULL
) {
3482 log_assert(chunks_
[i
-1].wire
!= NULL
);
3483 log_assert(chunk
.offset
== 0);
3484 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3486 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3487 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3488 log_assert(chunk
.offset
>= 0);
3489 log_assert(chunk
.width
>= 0);
3490 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3491 log_assert(chunk
.data
.size() == 0);
3495 log_assert(w
== width_
);
3496 log_assert(bits_
.empty());
3500 cover("kernel.rtlil.sigspec.check.unpacked");
3502 log_assert(width_
== GetSize(bits_
));
3503 log_assert(chunks_
.empty());
3508 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3510 cover("kernel.rtlil.sigspec.comp_lt");
3515 if (width_
!= other
.width_
)
3516 return width_
< other
.width_
;
3521 if (chunks_
.size() != other
.chunks_
.size())
3522 return chunks_
.size() < other
.chunks_
.size();
3527 if (hash_
!= other
.hash_
)
3528 return hash_
< other
.hash_
;
3530 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3531 if (chunks_
[i
] != other
.chunks_
[i
]) {
3532 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3533 return chunks_
[i
] < other
.chunks_
[i
];
3536 cover("kernel.rtlil.sigspec.comp_lt.equal");
3540 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3542 cover("kernel.rtlil.sigspec.comp_eq");
3547 if (width_
!= other
.width_
)
3553 if (chunks_
.size() != other
.chunks_
.size())
3559 if (hash_
!= other
.hash_
)
3562 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3563 if (chunks_
[i
] != other
.chunks_
[i
]) {
3564 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3568 cover("kernel.rtlil.sigspec.comp_eq.equal");
3572 bool RTLIL::SigSpec::is_wire() const
3574 cover("kernel.rtlil.sigspec.is_wire");
3577 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3580 bool RTLIL::SigSpec::is_chunk() const
3582 cover("kernel.rtlil.sigspec.is_chunk");
3585 return GetSize(chunks_
) == 1;
3588 bool RTLIL::SigSpec::is_fully_const() const
3590 cover("kernel.rtlil.sigspec.is_fully_const");
3593 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3594 if (it
->width
> 0 && it
->wire
!= NULL
)
3599 bool RTLIL::SigSpec::is_fully_zero() const
3601 cover("kernel.rtlil.sigspec.is_fully_zero");
3604 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3605 if (it
->width
> 0 && it
->wire
!= NULL
)
3607 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3608 if (it
->data
[i
] != RTLIL::State::S0
)
3614 bool RTLIL::SigSpec::is_fully_ones() const
3616 cover("kernel.rtlil.sigspec.is_fully_ones");
3619 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3620 if (it
->width
> 0 && it
->wire
!= NULL
)
3622 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3623 if (it
->data
[i
] != RTLIL::State::S1
)
3629 bool RTLIL::SigSpec::is_fully_def() const
3631 cover("kernel.rtlil.sigspec.is_fully_def");
3634 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3635 if (it
->width
> 0 && it
->wire
!= NULL
)
3637 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3638 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3644 bool RTLIL::SigSpec::is_fully_undef() const
3646 cover("kernel.rtlil.sigspec.is_fully_undef");
3649 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3650 if (it
->width
> 0 && it
->wire
!= NULL
)
3652 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3653 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3659 bool RTLIL::SigSpec::has_const() const
3661 cover("kernel.rtlil.sigspec.has_const");
3664 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3665 if (it
->width
> 0 && it
->wire
== NULL
)
3670 bool RTLIL::SigSpec::has_marked_bits() const
3672 cover("kernel.rtlil.sigspec.has_marked_bits");
3675 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3676 if (it
->width
> 0 && it
->wire
== NULL
) {
3677 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3678 if (it
->data
[i
] == RTLIL::State::Sm
)
3684 bool RTLIL::SigSpec::as_bool() const
3686 cover("kernel.rtlil.sigspec.as_bool");
3689 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3691 return RTLIL::Const(chunks_
[0].data
).as_bool();
3695 int RTLIL::SigSpec::as_int(bool is_signed
) const
3697 cover("kernel.rtlil.sigspec.as_int");
3700 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3702 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3706 std::string
RTLIL::SigSpec::as_string() const
3708 cover("kernel.rtlil.sigspec.as_string");
3712 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3713 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3714 if (chunk
.wire
!= NULL
)
3715 for (int j
= 0; j
< chunk
.width
; j
++)
3718 str
+= RTLIL::Const(chunk
.data
).as_string();
3723 RTLIL::Const
RTLIL::SigSpec::as_const() const
3725 cover("kernel.rtlil.sigspec.as_const");
3728 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3730 return chunks_
[0].data
;
3731 return RTLIL::Const();
3734 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3736 cover("kernel.rtlil.sigspec.as_wire");
3739 log_assert(is_wire());
3740 return chunks_
[0].wire
;
3743 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3745 cover("kernel.rtlil.sigspec.as_chunk");
3748 log_assert(is_chunk());
3752 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3754 cover("kernel.rtlil.sigspec.as_bit");
3756 log_assert(width_
== 1);
3758 return RTLIL::SigBit(*chunks_
.begin());
3763 bool RTLIL::SigSpec::match(std::string pattern
) const
3765 cover("kernel.rtlil.sigspec.match");
3768 std::string str
= as_string();
3769 log_assert(pattern
.size() == str
.size());
3771 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3772 if (pattern
[i
] == ' ')
3774 if (pattern
[i
] == '*') {
3775 if (str
[i
] != 'z' && str
[i
] != 'x')
3779 if (pattern
[i
] != str
[i
])
3786 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3788 cover("kernel.rtlil.sigspec.to_sigbit_set");
3791 std::set
<RTLIL::SigBit
> sigbits
;
3792 for (auto &c
: chunks_
)
3793 for (int i
= 0; i
< c
.width
; i
++)
3794 sigbits
.insert(RTLIL::SigBit(c
, i
));
3798 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3800 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3803 pool
<RTLIL::SigBit
> sigbits
;
3804 for (auto &c
: chunks_
)
3805 for (int i
= 0; i
< c
.width
; i
++)
3806 sigbits
.insert(RTLIL::SigBit(c
, i
));
3810 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3812 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3818 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3820 cover("kernel.rtlil.sigspec.to_sigbit_map");
3825 log_assert(width_
== other
.width_
);
3827 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3828 for (int i
= 0; i
< width_
; i
++)
3829 new_map
[bits_
[i
]] = other
.bits_
[i
];
3834 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3836 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3841 log_assert(width_
== other
.width_
);
3843 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3844 for (int i
= 0; i
< width_
; i
++)
3845 new_map
[bits_
[i
]] = other
.bits_
[i
];
3850 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3852 size_t start
= 0, end
= 0;
3853 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3854 tokens
.push_back(text
.substr(start
, end
- start
));
3857 tokens
.push_back(text
.substr(start
));
3860 static int sigspec_parse_get_dummy_line_num()
3865 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3867 cover("kernel.rtlil.sigspec.parse");
3869 AST::current_filename
= "input";
3870 AST::use_internal_line_num();
3871 AST::set_line_num(0);
3873 std::vector
<std::string
> tokens
;
3874 sigspec_parse_split(tokens
, str
, ',');
3876 sig
= RTLIL::SigSpec();
3877 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3879 std::string netname
= tokens
[tokidx
];
3880 std::string indices
;
3882 if (netname
.size() == 0)
3885 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3886 cover("kernel.rtlil.sigspec.parse.const");
3887 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3888 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3891 sig
.append(RTLIL::Const(ast
->bits
));
3899 cover("kernel.rtlil.sigspec.parse.net");
3901 if (netname
[0] != '$' && netname
[0] != '\\')
3902 netname
= "\\" + netname
;
3904 if (module
->wires_
.count(netname
) == 0) {
3905 size_t indices_pos
= netname
.size()-1;
3906 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3909 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3910 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3912 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3914 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3915 indices
= netname
.substr(indices_pos
);
3916 netname
= netname
.substr(0, indices_pos
);
3921 if (module
->wires_
.count(netname
) == 0)
3924 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3925 if (!indices
.empty()) {
3926 std::vector
<std::string
> index_tokens
;
3927 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3928 if (index_tokens
.size() == 1) {
3929 cover("kernel.rtlil.sigspec.parse.bit_sel");
3930 int a
= atoi(index_tokens
.at(0).c_str());
3931 if (a
< 0 || a
>= wire
->width
)
3933 sig
.append(RTLIL::SigSpec(wire
, a
));
3935 cover("kernel.rtlil.sigspec.parse.part_sel");
3936 int a
= atoi(index_tokens
.at(0).c_str());
3937 int b
= atoi(index_tokens
.at(1).c_str());
3942 if (a
< 0 || a
>= wire
->width
)
3944 if (b
< 0 || b
>= wire
->width
)
3946 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3955 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3957 if (str
.empty() || str
[0] != '@')
3958 return parse(sig
, module
, str
);
3960 cover("kernel.rtlil.sigspec.parse.sel");
3962 str
= RTLIL::escape_id(str
.substr(1));
3963 if (design
->selection_vars
.count(str
) == 0)
3966 sig
= RTLIL::SigSpec();
3967 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3968 for (auto &it
: module
->wires_
)
3969 if (sel
.selected_member(module
->name
, it
.first
))
3970 sig
.append(it
.second
);
3975 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3978 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3979 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3984 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3985 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3989 if (lhs
.chunks_
.size() == 1) {
3990 char *p
= (char*)str
.c_str(), *endptr
;
3991 long int val
= strtol(p
, &endptr
, 10);
3992 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3993 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3994 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3999 return parse(sig
, module
, str
);
4002 RTLIL::CaseRule::~CaseRule()
4004 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4008 bool RTLIL::CaseRule::empty() const
4010 return actions
.empty() && switches
.empty();
4013 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4015 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4016 new_caserule
->compare
= compare
;
4017 new_caserule
->actions
= actions
;
4018 for (auto &it
: switches
)
4019 new_caserule
->switches
.push_back(it
->clone());
4020 return new_caserule
;
4023 RTLIL::SwitchRule::~SwitchRule()
4025 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4029 bool RTLIL::SwitchRule::empty() const
4031 return cases
.empty();
4034 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4036 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4037 new_switchrule
->signal
= signal
;
4038 new_switchrule
->attributes
= attributes
;
4039 for (auto &it
: cases
)
4040 new_switchrule
->cases
.push_back(it
->clone());
4041 return new_switchrule
;
4045 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4047 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4048 new_syncrule
->type
= type
;
4049 new_syncrule
->signal
= signal
;
4050 new_syncrule
->actions
= actions
;
4051 return new_syncrule
;
4054 RTLIL::Process::~Process()
4056 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4060 RTLIL::Process
*RTLIL::Process::clone() const
4062 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4064 new_proc
->name
= name
;
4065 new_proc
->attributes
= attributes
;
4067 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4068 new_proc
->root_case
= *rc_ptr
;
4069 rc_ptr
->switches
.clear();
4072 for (auto &it
: syncs
)
4073 new_proc
->syncs
.push_back(it
->clone());
4079 RTLIL::Memory::~Memory()
4081 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4083 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4084 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4086 return &all_memorys
;