2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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 "kernel/binding.h"
24 #include "frontends/verilog/verilog_frontend.h"
25 #include "frontends/verilog/preproc.h"
26 #include "backends/rtlil/rtlil_backend.h"
33 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
34 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
35 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
36 #ifndef YOSYS_NO_IDS_REFCNT
37 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
38 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
40 #ifdef YOSYS_USE_STICKY_IDS
41 int RTLIL::IdString::last_created_idx_
[8];
42 int RTLIL::IdString::last_created_idx_ptr_
;
45 #define X(_id) IdString RTLIL::ID::_id;
46 #include "kernel/constids.inc"
49 dict
<std::string
, std::string
> RTLIL::constpad
;
51 const pool
<IdString
> &RTLIL::builtin_ff_cell_types() {
52 static const pool
<IdString
> res
= {
202 RTLIL::Const::Const()
204 flags
= RTLIL::CONST_FLAG_NONE
;
207 RTLIL::Const::Const(std::string str
)
209 flags
= RTLIL::CONST_FLAG_STRING
;
210 bits
.reserve(str
.size() * 8);
211 for (int i
= str
.size()-1; i
>= 0; i
--) {
212 unsigned char ch
= str
[i
];
213 for (int j
= 0; j
< 8; j
++) {
214 bits
.push_back((ch
& 1) != 0 ? State::S1
: State::S0
);
220 RTLIL::Const::Const(int val
, int width
)
222 flags
= RTLIL::CONST_FLAG_NONE
;
224 for (int i
= 0; i
< width
; i
++) {
225 bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
230 RTLIL::Const::Const(RTLIL::State bit
, int width
)
232 flags
= RTLIL::CONST_FLAG_NONE
;
234 for (int i
= 0; i
< width
; i
++)
238 RTLIL::Const::Const(const std::vector
<bool> &bits
)
240 flags
= RTLIL::CONST_FLAG_NONE
;
241 this->bits
.reserve(bits
.size());
242 for (const auto &b
: bits
)
243 this->bits
.emplace_back(b
? State::S1
: State::S0
);
246 RTLIL::Const::Const(const RTLIL::Const
&c
)
249 this->bits
.reserve(c
.size());
250 for (const auto &b
: c
.bits
)
251 this->bits
.push_back(b
);
254 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
256 if (bits
.size() != other
.bits
.size())
257 return bits
.size() < other
.bits
.size();
258 for (size_t i
= 0; i
< bits
.size(); i
++)
259 if (bits
[i
] != other
.bits
[i
])
260 return bits
[i
] < other
.bits
[i
];
264 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
266 return bits
== other
.bits
;
269 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
271 return bits
!= other
.bits
;
274 bool RTLIL::Const::as_bool() const
276 for (size_t i
= 0; i
< bits
.size(); i
++)
277 if (bits
[i
] == State::S1
)
282 int RTLIL::Const::as_int(bool is_signed
) const
285 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
286 if (bits
[i
] == State::S1
)
288 if (is_signed
&& bits
.back() == State::S1
)
289 for (size_t i
= bits
.size(); i
< 32; i
++)
294 std::string
RTLIL::Const::as_string() const
297 ret
.reserve(bits
.size());
298 for (size_t i
= bits
.size(); i
> 0; i
--)
300 case S0
: ret
+= "0"; break;
301 case S1
: ret
+= "1"; break;
302 case Sx
: ret
+= "x"; break;
303 case Sz
: ret
+= "z"; break;
304 case Sa
: ret
+= "-"; break;
305 case Sm
: ret
+= "m"; break;
310 RTLIL::Const
RTLIL::Const::from_string(const std::string
&str
)
313 c
.bits
.reserve(str
.size());
314 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
316 case '0': c
.bits
.push_back(State::S0
); break;
317 case '1': c
.bits
.push_back(State::S1
); break;
318 case 'x': c
.bits
.push_back(State::Sx
); break;
319 case 'z': c
.bits
.push_back(State::Sz
); break;
320 case 'm': c
.bits
.push_back(State::Sm
); break;
321 default: c
.bits
.push_back(State::Sa
);
326 std::string
RTLIL::Const::decode_string() const
329 string
.reserve(GetSize(bits
)/8);
330 for (int i
= 0; i
< GetSize(bits
); i
+= 8) {
332 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
333 if (bits
[i
+ j
] == RTLIL::State::S1
)
338 std::reverse(string
.begin(), string
.end());
342 bool RTLIL::Const::is_fully_zero() const
344 cover("kernel.rtlil.const.is_fully_zero");
346 for (const auto &bit
: bits
)
347 if (bit
!= RTLIL::State::S0
)
353 bool RTLIL::Const::is_fully_ones() const
355 cover("kernel.rtlil.const.is_fully_ones");
357 for (const auto &bit
: bits
)
358 if (bit
!= RTLIL::State::S1
)
364 bool RTLIL::Const::is_fully_def() const
366 cover("kernel.rtlil.const.is_fully_def");
368 for (const auto &bit
: bits
)
369 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
375 bool RTLIL::Const::is_fully_undef() const
377 cover("kernel.rtlil.const.is_fully_undef");
379 for (const auto &bit
: bits
)
380 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
386 bool RTLIL::Const::is_onehot(int *pos
) const
388 cover("kernel.rtlil.const.is_onehot");
391 for (int i
= 0; i
< GetSize(*this); i
++) {
393 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
395 if (bit
== RTLIL::State::S1
) {
406 bool RTLIL::AttrObject::has_attribute(RTLIL::IdString id
) const
408 return attributes
.count(id
);
411 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
414 attributes
[id
] = RTLIL::Const(1);
416 attributes
.erase(id
);
419 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
421 const auto it
= attributes
.find(id
);
422 if (it
== attributes
.end())
424 return it
->second
.as_bool();
427 void RTLIL::AttrObject::set_string_attribute(RTLIL::IdString id
, string value
)
430 attributes
.erase(id
);
432 attributes
[id
] = value
;
435 string
RTLIL::AttrObject::get_string_attribute(RTLIL::IdString id
) const
438 const auto it
= attributes
.find(id
);
439 if (it
!= attributes
.end())
440 value
= it
->second
.decode_string();
444 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
447 for (const auto &s
: data
) {
448 if (!attrval
.empty())
452 set_string_attribute(id
, attrval
);
455 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
457 pool
<string
> union_data
= get_strpool_attribute(id
);
458 union_data
.insert(data
.begin(), data
.end());
459 if (!union_data
.empty())
460 set_strpool_attribute(id
, union_data
);
463 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
466 if (attributes
.count(id
) != 0)
467 for (auto s
: split_tokens(get_string_attribute(id
), "|"))
472 void RTLIL::AttrObject::set_hdlname_attribute(const vector
<string
> &hierarchy
)
475 for (const auto &ident
: hierarchy
) {
476 if (!attrval
.empty())
480 set_string_attribute(ID::hdlname
, attrval
);
483 vector
<string
> RTLIL::AttrObject::get_hdlname_attribute() const
485 return split_tokens(get_string_attribute(ID::hdlname
), " ");
488 void RTLIL::AttrObject::set_intvec_attribute(RTLIL::IdString id
, const vector
<int> &data
)
490 std::stringstream attrval
;
491 for (auto &i
: data
) {
492 if (attrval
.tellp() > 0)
496 attributes
[id
] = RTLIL::Const(attrval
.str());
499 vector
<int> RTLIL::AttrObject::get_intvec_attribute(RTLIL::IdString id
) const
502 auto it
= attributes
.find(id
);
503 if (it
!= attributes
.end())
504 for (const auto &s
: split_tokens(attributes
.at(id
).decode_string())) {
507 long value
= strtol(s
.c_str(), &end
, 10);
508 if (end
!= s
.c_str() + s
.size())
509 log_cmd_error("Literal for intvec attribute has invalid format");
510 if (errno
== ERANGE
|| value
< INT_MIN
|| value
> INT_MAX
)
511 log_cmd_error("Literal for intvec attribute is out of range");
512 data
.push_back(value
);
517 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
521 if (selected_modules
.count(mod_name
) > 0)
523 if (selected_members
.count(mod_name
) > 0)
528 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
532 if (selected_modules
.count(mod_name
) > 0)
537 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
541 if (selected_modules
.count(mod_name
) > 0)
543 if (selected_members
.count(mod_name
) > 0)
544 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
549 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
551 if (full_selection
) {
552 selected_modules
.clear();
553 selected_members
.clear();
557 std::vector
<RTLIL::IdString
> del_list
, add_list
;
560 for (auto mod_name
: selected_modules
) {
561 if (design
->modules_
.count(mod_name
) == 0)
562 del_list
.push_back(mod_name
);
563 selected_members
.erase(mod_name
);
565 for (auto mod_name
: del_list
)
566 selected_modules
.erase(mod_name
);
569 for (auto &it
: selected_members
)
570 if (design
->modules_
.count(it
.first
) == 0)
571 del_list
.push_back(it
.first
);
572 for (auto mod_name
: del_list
)
573 selected_members
.erase(mod_name
);
575 for (auto &it
: selected_members
) {
577 for (auto memb_name
: it
.second
)
578 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
579 del_list
.push_back(memb_name
);
580 for (auto memb_name
: del_list
)
581 it
.second
.erase(memb_name
);
586 for (auto &it
: selected_members
)
587 if (it
.second
.size() == 0)
588 del_list
.push_back(it
.first
);
589 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
590 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
591 add_list
.push_back(it
.first
);
592 for (auto mod_name
: del_list
)
593 selected_members
.erase(mod_name
);
594 for (auto mod_name
: add_list
) {
595 selected_members
.erase(mod_name
);
596 selected_modules
.insert(mod_name
);
599 if (selected_modules
.size() == design
->modules_
.size()) {
600 full_selection
= true;
601 selected_modules
.clear();
602 selected_members
.clear();
606 RTLIL::Design::Design()
607 : verilog_defines (new define_map_t
)
609 static unsigned int hashidx_count
= 123456789;
610 hashidx_count
= mkhash_xorshift(hashidx_count
);
611 hashidx_
= hashidx_count
;
613 refcount_modules_
= 0;
614 selection_stack
.push_back(RTLIL::Selection());
617 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
621 RTLIL::Design::~Design()
623 for (auto &pr
: modules_
)
625 for (auto n
: bindings_
)
627 for (auto n
: verilog_packages
)
629 for (auto n
: verilog_globals
)
632 RTLIL::Design::get_all_designs()->erase(hashidx_
);
637 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
638 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
644 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
646 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
649 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
651 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
654 const RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
) const
656 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
659 RTLIL::Module
*RTLIL::Design::top_module()
661 RTLIL::Module
*module
= nullptr;
662 int module_count
= 0;
664 for (auto mod
: selected_modules()) {
665 if (mod
->get_bool_attribute(ID::top
))
671 return module_count
== 1 ? module
: nullptr;
674 void RTLIL::Design::add(RTLIL::Module
*module
)
676 log_assert(modules_
.count(module
->name
) == 0);
677 log_assert(refcount_modules_
== 0);
678 modules_
[module
->name
] = module
;
679 module
->design
= this;
681 for (auto mon
: monitors
)
682 mon
->notify_module_add(module
);
685 log("#X# New Module: %s\n", log_id(module
));
686 log_backtrace("-X- ", yosys_xtrace
-1);
690 void RTLIL::Design::add(RTLIL::Binding
*binding
)
692 log_assert(binding
!= nullptr);
693 bindings_
.push_back(binding
);
696 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
698 if (modules_
.count(name
) != 0)
699 log_error("Attempted to add new module named '%s', but a module by that name already exists\n", name
.c_str());
700 log_assert(refcount_modules_
== 0);
702 RTLIL::Module
*module
= new RTLIL::Module
;
703 modules_
[name
] = module
;
704 module
->design
= this;
707 for (auto mon
: monitors
)
708 mon
->notify_module_add(module
);
711 log("#X# New Module: %s\n", log_id(module
));
712 log_backtrace("-X- ", yosys_xtrace
-1);
718 void RTLIL::Design::scratchpad_unset(const std::string
&varname
)
720 scratchpad
.erase(varname
);
723 void RTLIL::Design::scratchpad_set_int(const std::string
&varname
, int value
)
725 scratchpad
[varname
] = stringf("%d", value
);
728 void RTLIL::Design::scratchpad_set_bool(const std::string
&varname
, bool value
)
730 scratchpad
[varname
] = value
? "true" : "false";
733 void RTLIL::Design::scratchpad_set_string(const std::string
&varname
, std::string value
)
735 scratchpad
[varname
] = std::move(value
);
738 int RTLIL::Design::scratchpad_get_int(const std::string
&varname
, int default_value
) const
740 auto it
= scratchpad
.find(varname
);
741 if (it
== scratchpad
.end())
742 return default_value
;
744 const std::string
&str
= it
->second
;
746 if (str
== "0" || str
== "false")
749 if (str
== "1" || str
== "true")
752 char *endptr
= nullptr;
753 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
754 return *endptr
? default_value
: parsed_value
;
757 bool RTLIL::Design::scratchpad_get_bool(const std::string
&varname
, bool default_value
) const
759 auto it
= scratchpad
.find(varname
);
760 if (it
== scratchpad
.end())
761 return default_value
;
763 const std::string
&str
= it
->second
;
765 if (str
== "0" || str
== "false")
768 if (str
== "1" || str
== "true")
771 return default_value
;
774 std::string
RTLIL::Design::scratchpad_get_string(const std::string
&varname
, const std::string
&default_value
) const
776 auto it
= scratchpad
.find(varname
);
777 if (it
== scratchpad
.end())
778 return default_value
;
783 void RTLIL::Design::remove(RTLIL::Module
*module
)
785 for (auto mon
: monitors
)
786 mon
->notify_module_del(module
);
789 log("#X# Remove Module: %s\n", log_id(module
));
790 log_backtrace("-X- ", yosys_xtrace
-1);
793 log_assert(modules_
.at(module
->name
) == module
);
794 log_assert(refcount_modules_
== 0);
795 modules_
.erase(module
->name
);
799 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
801 modules_
.erase(module
->name
);
802 module
->name
= new_name
;
806 void RTLIL::Design::sort()
809 modules_
.sort(sort_by_id_str());
810 for (auto &it
: modules_
)
814 void RTLIL::Design::check()
817 for (auto &it
: modules_
) {
818 log_assert(this == it
.second
->design
);
819 log_assert(it
.first
== it
.second
->name
);
820 log_assert(!it
.first
.empty());
826 void RTLIL::Design::optimize()
828 for (auto &it
: modules_
)
829 it
.second
->optimize();
830 for (auto &it
: selection_stack
)
832 for (auto &it
: selection_vars
)
833 it
.second
.optimize(this);
836 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
838 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
840 if (selection_stack
.size() == 0)
842 return selection_stack
.back().selected_module(mod_name
);
845 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
847 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
849 if (selection_stack
.size() == 0)
851 return selection_stack
.back().selected_whole_module(mod_name
);
854 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
856 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
858 if (selection_stack
.size() == 0)
860 return selection_stack
.back().selected_member(mod_name
, memb_name
);
863 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
865 return selected_module(mod
->name
);
868 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
870 return selected_whole_module(mod
->name
);
873 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
875 std::vector
<RTLIL::Module
*> result
;
876 result
.reserve(modules_
.size());
877 for (auto &it
: modules_
)
878 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
879 result
.push_back(it
.second
);
883 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
885 std::vector
<RTLIL::Module
*> result
;
886 result
.reserve(modules_
.size());
887 for (auto &it
: modules_
)
888 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
889 result
.push_back(it
.second
);
893 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn(bool include_wb
) const
895 std::vector
<RTLIL::Module
*> result
;
896 result
.reserve(modules_
.size());
897 for (auto &it
: modules_
)
898 if (it
.second
->get_blackbox_attribute(include_wb
))
900 else if (selected_whole_module(it
.first
))
901 result
.push_back(it
.second
);
902 else if (selected_module(it
.first
))
903 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
907 RTLIL::Module::Module()
909 static unsigned int hashidx_count
= 123456789;
910 hashidx_count
= mkhash_xorshift(hashidx_count
);
911 hashidx_
= hashidx_count
;
918 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
922 RTLIL::Module::~Module()
924 for (auto &pr
: wires_
)
926 for (auto &pr
: memories
)
928 for (auto &pr
: cells_
)
930 for (auto &pr
: processes
)
932 for (auto binding
: bindings_
)
935 RTLIL::Module::get_all_modules()->erase(hashidx_
);
940 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
941 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
947 void RTLIL::Module::makeblackbox()
949 pool
<RTLIL::Wire
*> delwires
;
951 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
952 if (!it
->second
->port_input
&& !it
->second
->port_output
)
953 delwires
.insert(it
->second
);
955 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
959 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
963 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
967 connections_
.clear();
970 set_bool_attribute(ID::blackbox
);
973 void RTLIL::Module::expand_interfaces(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Module
*> &)
975 log_error("Class doesn't support expand_interfaces (module: `%s')!\n", id2cstr(name
));
978 bool RTLIL::Module::reprocess_if_necessary(RTLIL::Design
*)
983 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, bool mayfail
)
986 return RTLIL::IdString();
987 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
991 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, const dict
<RTLIL::IdString
, RTLIL::Module
*> &, const dict
<RTLIL::IdString
, RTLIL::IdString
> &, bool mayfail
)
994 return RTLIL::IdString();
995 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
998 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
1000 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
1005 struct InternalCellChecker
1007 RTLIL::Module
*module
;
1009 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
1011 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
1013 void error(int linenr
)
1015 std::stringstream buf
;
1016 RTLIL_BACKEND::dump_cell(buf
, " ", cell
);
1018 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
1019 module
? module
->name
.c_str() : "", module
? "." : "",
1020 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
1023 int param(RTLIL::IdString name
)
1025 auto it
= cell
->parameters
.find(name
);
1026 if (it
== cell
->parameters
.end())
1028 expected_params
.insert(name
);
1029 return it
->second
.as_int();
1032 int param_bool(RTLIL::IdString name
)
1034 int v
= param(name
);
1035 if (GetSize(cell
->parameters
.at(name
)) > 32)
1037 if (v
!= 0 && v
!= 1)
1042 int param_bool(RTLIL::IdString name
, bool expected
)
1044 int v
= param_bool(name
);
1050 void param_bits(RTLIL::IdString name
, int width
)
1053 if (GetSize(cell
->parameters
.at(name
).bits
) != width
)
1057 void port(RTLIL::IdString name
, int width
)
1059 auto it
= cell
->connections_
.find(name
);
1060 if (it
== cell
->connections_
.end())
1062 if (GetSize(it
->second
) != width
)
1064 expected_ports
.insert(name
);
1067 void check_expected(bool check_matched_sign
= false)
1069 for (auto ¶
: cell
->parameters
)
1070 if (expected_params
.count(para
.first
) == 0)
1072 for (auto &conn
: cell
->connections())
1073 if (expected_ports
.count(conn
.first
) == 0)
1076 if (check_matched_sign
) {
1077 log_assert(expected_params
.count(ID::A_SIGNED
) != 0 && expected_params
.count(ID::B_SIGNED
) != 0);
1078 bool a_is_signed
= cell
->parameters
.at(ID::A_SIGNED
).as_bool();
1079 bool b_is_signed
= cell
->parameters
.at(ID::B_SIGNED
).as_bool();
1080 if (a_is_signed
!= b_is_signed
)
1087 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
1088 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
1091 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
1092 param_bool(ID::A_SIGNED
);
1093 port(ID::A
, param(ID::A_WIDTH
));
1094 port(ID::Y
, param(ID::Y_WIDTH
));
1099 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
1100 param_bool(ID::A_SIGNED
);
1101 param_bool(ID::B_SIGNED
);
1102 port(ID::A
, param(ID::A_WIDTH
));
1103 port(ID::B
, param(ID::B_WIDTH
));
1104 port(ID::Y
, param(ID::Y_WIDTH
));
1105 check_expected(true);
1109 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
1110 param_bool(ID::A_SIGNED
);
1111 port(ID::A
, param(ID::A_WIDTH
));
1112 port(ID::Y
, param(ID::Y_WIDTH
));
1117 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
1118 param_bool(ID::A_SIGNED
);
1119 param_bool(ID::B_SIGNED
, /*expected=*/false);
1120 port(ID::A
, param(ID::A_WIDTH
));
1121 port(ID::B
, param(ID::B_WIDTH
));
1122 port(ID::Y
, param(ID::Y_WIDTH
));
1123 check_expected(/*check_matched_sign=*/false);
1127 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
1128 if (cell
->type
== ID($shiftx
)) {
1129 param_bool(ID::A_SIGNED
, /*expected=*/false);
1131 param_bool(ID::A_SIGNED
);
1133 param_bool(ID::B_SIGNED
);
1134 port(ID::A
, param(ID::A_WIDTH
));
1135 port(ID::B
, param(ID::B_WIDTH
));
1136 port(ID::Y
, param(ID::Y_WIDTH
));
1137 check_expected(/*check_matched_sign=*/false);
1141 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
1142 param_bool(ID::A_SIGNED
);
1143 param_bool(ID::B_SIGNED
);
1144 port(ID::A
, param(ID::A_WIDTH
));
1145 port(ID::B
, param(ID::B_WIDTH
));
1146 port(ID::Y
, param(ID::Y_WIDTH
));
1147 check_expected(true);
1151 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($divfloor
), ID($modfloor
), ID($pow
))) {
1152 param_bool(ID::A_SIGNED
);
1153 param_bool(ID::B_SIGNED
);
1154 port(ID::A
, param(ID::A_WIDTH
));
1155 port(ID::B
, param(ID::B_WIDTH
));
1156 port(ID::Y
, param(ID::Y_WIDTH
));
1157 check_expected(cell
->type
!= ID($pow
));
1161 if (cell
->type
== ID($fa
)) {
1162 port(ID::A
, param(ID::WIDTH
));
1163 port(ID::B
, param(ID::WIDTH
));
1164 port(ID::C
, param(ID::WIDTH
));
1165 port(ID::X
, param(ID::WIDTH
));
1166 port(ID::Y
, param(ID::WIDTH
));
1171 if (cell
->type
== ID($lcu
)) {
1172 port(ID::P
, param(ID::WIDTH
));
1173 port(ID::G
, param(ID::WIDTH
));
1175 port(ID::CO
, param(ID::WIDTH
));
1180 if (cell
->type
== ID($alu
)) {
1181 param_bool(ID::A_SIGNED
);
1182 param_bool(ID::B_SIGNED
);
1183 port(ID::A
, param(ID::A_WIDTH
));
1184 port(ID::B
, param(ID::B_WIDTH
));
1187 port(ID::X
, param(ID::Y_WIDTH
));
1188 port(ID::Y
, param(ID::Y_WIDTH
));
1189 port(ID::CO
, param(ID::Y_WIDTH
));
1190 check_expected(true);
1194 if (cell
->type
== ID($macc
)) {
1196 param(ID::CONFIG_WIDTH
);
1197 port(ID::A
, param(ID::A_WIDTH
));
1198 port(ID::B
, param(ID::B_WIDTH
));
1199 port(ID::Y
, param(ID::Y_WIDTH
));
1201 Macc().from_cell(cell
);
1205 if (cell
->type
== ID($logic_not
)) {
1206 param_bool(ID::A_SIGNED
);
1207 port(ID::A
, param(ID::A_WIDTH
));
1208 port(ID::Y
, param(ID::Y_WIDTH
));
1213 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
1214 param_bool(ID::A_SIGNED
);
1215 param_bool(ID::B_SIGNED
);
1216 port(ID::A
, param(ID::A_WIDTH
));
1217 port(ID::B
, param(ID::B_WIDTH
));
1218 port(ID::Y
, param(ID::Y_WIDTH
));
1219 check_expected(/*check_matched_sign=*/false);
1223 if (cell
->type
== ID($slice
)) {
1225 port(ID::A
, param(ID::A_WIDTH
));
1226 port(ID::Y
, param(ID::Y_WIDTH
));
1227 if (param(ID::OFFSET
) + param(ID::Y_WIDTH
) > param(ID::A_WIDTH
))
1233 if (cell
->type
== ID($concat
)) {
1234 port(ID::A
, param(ID::A_WIDTH
));
1235 port(ID::B
, param(ID::B_WIDTH
));
1236 port(ID::Y
, param(ID::A_WIDTH
) + param(ID::B_WIDTH
));
1241 if (cell
->type
== ID($mux
)) {
1242 port(ID::A
, param(ID::WIDTH
));
1243 port(ID::B
, param(ID::WIDTH
));
1245 port(ID::Y
, param(ID::WIDTH
));
1250 if (cell
->type
== ID($pmux
)) {
1251 port(ID::A
, param(ID::WIDTH
));
1252 port(ID::B
, param(ID::WIDTH
) * param(ID::S_WIDTH
));
1253 port(ID::S
, param(ID::S_WIDTH
));
1254 port(ID::Y
, param(ID::WIDTH
));
1259 if (cell
->type
== ID($bmux
)) {
1260 port(ID::A
, param(ID::WIDTH
) << param(ID::S_WIDTH
));
1261 port(ID::S
, param(ID::S_WIDTH
));
1262 port(ID::Y
, param(ID::WIDTH
));
1267 if (cell
->type
== ID($demux
)) {
1268 port(ID::A
, param(ID::WIDTH
));
1269 port(ID::S
, param(ID::S_WIDTH
));
1270 port(ID::Y
, param(ID::WIDTH
) << param(ID::S_WIDTH
));
1275 if (cell
->type
== ID($lut
)) {
1277 port(ID::A
, param(ID::WIDTH
));
1283 if (cell
->type
== ID($sop
)) {
1286 port(ID::A
, param(ID::WIDTH
));
1292 if (cell
->type
== ID($sr
)) {
1293 param_bool(ID::SET_POLARITY
);
1294 param_bool(ID::CLR_POLARITY
);
1295 port(ID::SET
, param(ID::WIDTH
));
1296 port(ID::CLR
, param(ID::WIDTH
));
1297 port(ID::Q
, param(ID::WIDTH
));
1302 if (cell
->type
== ID($ff
)) {
1303 port(ID::D
, param(ID::WIDTH
));
1304 port(ID::Q
, param(ID::WIDTH
));
1309 if (cell
->type
== ID($dff
)) {
1310 param_bool(ID::CLK_POLARITY
);
1312 port(ID::D
, param(ID::WIDTH
));
1313 port(ID::Q
, param(ID::WIDTH
));
1318 if (cell
->type
== ID($dffe
)) {
1319 param_bool(ID::CLK_POLARITY
);
1320 param_bool(ID::EN_POLARITY
);
1323 port(ID::D
, param(ID::WIDTH
));
1324 port(ID::Q
, param(ID::WIDTH
));
1329 if (cell
->type
== ID($dffsr
)) {
1330 param_bool(ID::CLK_POLARITY
);
1331 param_bool(ID::SET_POLARITY
);
1332 param_bool(ID::CLR_POLARITY
);
1334 port(ID::SET
, param(ID::WIDTH
));
1335 port(ID::CLR
, param(ID::WIDTH
));
1336 port(ID::D
, param(ID::WIDTH
));
1337 port(ID::Q
, param(ID::WIDTH
));
1342 if (cell
->type
== ID($dffsre
)) {
1343 param_bool(ID::CLK_POLARITY
);
1344 param_bool(ID::SET_POLARITY
);
1345 param_bool(ID::CLR_POLARITY
);
1346 param_bool(ID::EN_POLARITY
);
1349 port(ID::SET
, param(ID::WIDTH
));
1350 port(ID::CLR
, param(ID::WIDTH
));
1351 port(ID::D
, param(ID::WIDTH
));
1352 port(ID::Q
, param(ID::WIDTH
));
1357 if (cell
->type
== ID($adff
)) {
1358 param_bool(ID::CLK_POLARITY
);
1359 param_bool(ID::ARST_POLARITY
);
1360 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1363 port(ID::D
, param(ID::WIDTH
));
1364 port(ID::Q
, param(ID::WIDTH
));
1369 if (cell
->type
== ID($sdff
)) {
1370 param_bool(ID::CLK_POLARITY
);
1371 param_bool(ID::SRST_POLARITY
);
1372 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1375 port(ID::D
, param(ID::WIDTH
));
1376 port(ID::Q
, param(ID::WIDTH
));
1381 if (cell
->type
.in(ID($sdffe
), ID($sdffce
))) {
1382 param_bool(ID::CLK_POLARITY
);
1383 param_bool(ID::EN_POLARITY
);
1384 param_bool(ID::SRST_POLARITY
);
1385 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1389 port(ID::D
, param(ID::WIDTH
));
1390 port(ID::Q
, param(ID::WIDTH
));
1395 if (cell
->type
== ID($adffe
)) {
1396 param_bool(ID::CLK_POLARITY
);
1397 param_bool(ID::EN_POLARITY
);
1398 param_bool(ID::ARST_POLARITY
);
1399 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1403 port(ID::D
, param(ID::WIDTH
));
1404 port(ID::Q
, param(ID::WIDTH
));
1409 if (cell
->type
== ID($aldff
)) {
1410 param_bool(ID::CLK_POLARITY
);
1411 param_bool(ID::ALOAD_POLARITY
);
1414 port(ID::D
, param(ID::WIDTH
));
1415 port(ID::AD
, param(ID::WIDTH
));
1416 port(ID::Q
, param(ID::WIDTH
));
1421 if (cell
->type
== ID($aldffe
)) {
1422 param_bool(ID::CLK_POLARITY
);
1423 param_bool(ID::EN_POLARITY
);
1424 param_bool(ID::ALOAD_POLARITY
);
1428 port(ID::D
, param(ID::WIDTH
));
1429 port(ID::AD
, param(ID::WIDTH
));
1430 port(ID::Q
, param(ID::WIDTH
));
1435 if (cell
->type
== ID($dlatch
)) {
1436 param_bool(ID::EN_POLARITY
);
1438 port(ID::D
, param(ID::WIDTH
));
1439 port(ID::Q
, param(ID::WIDTH
));
1444 if (cell
->type
== ID($adlatch
)) {
1445 param_bool(ID::EN_POLARITY
);
1446 param_bool(ID::ARST_POLARITY
);
1447 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1450 port(ID::D
, param(ID::WIDTH
));
1451 port(ID::Q
, param(ID::WIDTH
));
1456 if (cell
->type
== ID($dlatchsr
)) {
1457 param_bool(ID::EN_POLARITY
);
1458 param_bool(ID::SET_POLARITY
);
1459 param_bool(ID::CLR_POLARITY
);
1461 port(ID::SET
, param(ID::WIDTH
));
1462 port(ID::CLR
, param(ID::WIDTH
));
1463 port(ID::D
, param(ID::WIDTH
));
1464 port(ID::Q
, param(ID::WIDTH
));
1469 if (cell
->type
== ID($fsm
)) {
1471 param_bool(ID::CLK_POLARITY
);
1472 param_bool(ID::ARST_POLARITY
);
1473 param(ID::STATE_BITS
);
1474 param(ID::STATE_NUM
);
1475 param(ID::STATE_NUM_LOG2
);
1476 param(ID::STATE_RST
);
1477 param_bits(ID::STATE_TABLE
, param(ID::STATE_BITS
) * param(ID::STATE_NUM
));
1478 param(ID::TRANS_NUM
);
1479 param_bits(ID::TRANS_TABLE
, param(ID::TRANS_NUM
) * (2*param(ID::STATE_NUM_LOG2
) + param(ID::CTRL_IN_WIDTH
) + param(ID::CTRL_OUT_WIDTH
)));
1482 port(ID::CTRL_IN
, param(ID::CTRL_IN_WIDTH
));
1483 port(ID::CTRL_OUT
, param(ID::CTRL_OUT_WIDTH
));
1488 if (cell
->type
== ID($memrd
)) {
1490 param_bool(ID::CLK_ENABLE
);
1491 param_bool(ID::CLK_POLARITY
);
1492 param_bool(ID::TRANSPARENT
);
1495 port(ID::ADDR
, param(ID::ABITS
));
1496 port(ID::DATA
, param(ID::WIDTH
));
1501 if (cell
->type
== ID($memrd_v2
)) {
1503 param_bool(ID::CLK_ENABLE
);
1504 param_bool(ID::CLK_POLARITY
);
1505 param(ID::TRANSPARENCY_MASK
);
1506 param(ID::COLLISION_X_MASK
);
1507 param_bool(ID::CE_OVER_SRST
);
1508 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1509 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1510 param_bits(ID::INIT_VALUE
, param(ID::WIDTH
));
1515 port(ID::ADDR
, param(ID::ABITS
));
1516 port(ID::DATA
, param(ID::WIDTH
));
1521 if (cell
->type
== ID($memwr
)) {
1523 param_bool(ID::CLK_ENABLE
);
1524 param_bool(ID::CLK_POLARITY
);
1525 param(ID::PRIORITY
);
1527 port(ID::EN
, param(ID::WIDTH
));
1528 port(ID::ADDR
, param(ID::ABITS
));
1529 port(ID::DATA
, param(ID::WIDTH
));
1534 if (cell
->type
== ID($memwr_v2
)) {
1536 param_bool(ID::CLK_ENABLE
);
1537 param_bool(ID::CLK_POLARITY
);
1539 param(ID::PRIORITY_MASK
);
1541 port(ID::EN
, param(ID::WIDTH
));
1542 port(ID::ADDR
, param(ID::ABITS
));
1543 port(ID::DATA
, param(ID::WIDTH
));
1548 if (cell
->type
== ID($meminit
)) {
1550 param(ID::PRIORITY
);
1551 port(ID::ADDR
, param(ID::ABITS
));
1552 port(ID::DATA
, param(ID::WIDTH
) * param(ID::WORDS
));
1557 if (cell
->type
== ID($meminit_v2
)) {
1559 param(ID::PRIORITY
);
1560 port(ID::ADDR
, param(ID::ABITS
));
1561 port(ID::DATA
, param(ID::WIDTH
) * param(ID::WORDS
));
1562 port(ID::EN
, param(ID::WIDTH
));
1567 if (cell
->type
== ID($mem
)) {
1572 param_bits(ID::RD_CLK_ENABLE
, max(1, param(ID::RD_PORTS
)));
1573 param_bits(ID::RD_CLK_POLARITY
, max(1, param(ID::RD_PORTS
)));
1574 param_bits(ID::RD_TRANSPARENT
, max(1, param(ID::RD_PORTS
)));
1575 param_bits(ID::WR_CLK_ENABLE
, max(1, param(ID::WR_PORTS
)));
1576 param_bits(ID::WR_CLK_POLARITY
, max(1, param(ID::WR_PORTS
)));
1577 port(ID::RD_CLK
, param(ID::RD_PORTS
));
1578 port(ID::RD_EN
, param(ID::RD_PORTS
));
1579 port(ID::RD_ADDR
, param(ID::RD_PORTS
) * param(ID::ABITS
));
1580 port(ID::RD_DATA
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1581 port(ID::WR_CLK
, param(ID::WR_PORTS
));
1582 port(ID::WR_EN
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1583 port(ID::WR_ADDR
, param(ID::WR_PORTS
) * param(ID::ABITS
));
1584 port(ID::WR_DATA
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1589 if (cell
->type
== ID($mem_v2
)) {
1594 param_bits(ID::RD_CLK_ENABLE
, max(1, param(ID::RD_PORTS
)));
1595 param_bits(ID::RD_CLK_POLARITY
, max(1, param(ID::RD_PORTS
)));
1596 param_bits(ID::RD_TRANSPARENCY_MASK
, max(1, param(ID::RD_PORTS
) * param(ID::WR_PORTS
)));
1597 param_bits(ID::RD_COLLISION_X_MASK
, max(1, param(ID::RD_PORTS
) * param(ID::WR_PORTS
)));
1598 param_bits(ID::RD_WIDE_CONTINUATION
, max(1, param(ID::RD_PORTS
)));
1599 param_bits(ID::RD_CE_OVER_SRST
, max(1, param(ID::RD_PORTS
)));
1600 param_bits(ID::RD_ARST_VALUE
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1601 param_bits(ID::RD_SRST_VALUE
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1602 param_bits(ID::RD_INIT_VALUE
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1603 param_bits(ID::WR_CLK_ENABLE
, max(1, param(ID::WR_PORTS
)));
1604 param_bits(ID::WR_CLK_POLARITY
, max(1, param(ID::WR_PORTS
)));
1605 param_bits(ID::WR_WIDE_CONTINUATION
, max(1, param(ID::WR_PORTS
)));
1606 param_bits(ID::WR_PRIORITY_MASK
, max(1, param(ID::WR_PORTS
) * param(ID::WR_PORTS
)));
1607 port(ID::RD_CLK
, param(ID::RD_PORTS
));
1608 port(ID::RD_EN
, param(ID::RD_PORTS
));
1609 port(ID::RD_ARST
, param(ID::RD_PORTS
));
1610 port(ID::RD_SRST
, param(ID::RD_PORTS
));
1611 port(ID::RD_ADDR
, param(ID::RD_PORTS
) * param(ID::ABITS
));
1612 port(ID::RD_DATA
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1613 port(ID::WR_CLK
, param(ID::WR_PORTS
));
1614 port(ID::WR_EN
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1615 port(ID::WR_ADDR
, param(ID::WR_PORTS
) * param(ID::ABITS
));
1616 port(ID::WR_DATA
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1621 if (cell
->type
== ID($tribuf
)) {
1622 port(ID::A
, param(ID::WIDTH
));
1623 port(ID::Y
, param(ID::WIDTH
));
1629 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1636 if (cell
->type
== ID($initstate
)) {
1642 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1643 port(ID::Y
, param(ID::WIDTH
));
1648 if (cell
->type
== ID($equiv
)) {
1656 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1657 param_bool(ID::FULL
);
1658 param_bool(ID::SRC_DST_PEN
);
1659 param_bool(ID::SRC_DST_POL
);
1660 param(ID::T_RISE_MIN
);
1661 param(ID::T_RISE_TYP
);
1662 param(ID::T_RISE_MAX
);
1663 param(ID::T_FALL_MIN
);
1664 param(ID::T_FALL_TYP
);
1665 param(ID::T_FALL_MAX
);
1667 port(ID::SRC
, param(ID::SRC_WIDTH
));
1668 port(ID::DST
, param(ID::DST_WIDTH
));
1669 if (cell
->type
== ID($specify3
)) {
1670 param_bool(ID::EDGE_EN
);
1671 param_bool(ID::EDGE_POL
);
1672 param_bool(ID::DAT_DST_PEN
);
1673 param_bool(ID::DAT_DST_POL
);
1674 port(ID::DAT
, param(ID::DST_WIDTH
));
1680 if (cell
->type
== ID($specrule
)) {
1682 param_bool(ID::SRC_PEN
);
1683 param_bool(ID::SRC_POL
);
1684 param_bool(ID::DST_PEN
);
1685 param_bool(ID::DST_POL
);
1686 param(ID::T_LIMIT_MIN
);
1687 param(ID::T_LIMIT_TYP
);
1688 param(ID::T_LIMIT_MAX
);
1689 param(ID::T_LIMIT2_MIN
);
1690 param(ID::T_LIMIT2_TYP
);
1691 param(ID::T_LIMIT2_MAX
);
1692 port(ID::SRC_EN
, 1);
1693 port(ID::DST_EN
, 1);
1694 port(ID::SRC
, param(ID::SRC_WIDTH
));
1695 port(ID::DST
, param(ID::DST_WIDTH
));
1700 if (cell
->type
== ID($_BUF_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1701 if (cell
->type
== ID($_NOT_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1702 if (cell
->type
== ID($_AND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1703 if (cell
->type
== ID($_NAND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1704 if (cell
->type
== ID($_OR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1705 if (cell
->type
== ID($_NOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1706 if (cell
->type
== ID($_XOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1707 if (cell
->type
== ID($_XNOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1708 if (cell
->type
== ID($_ANDNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1709 if (cell
->type
== ID($_ORNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1710 if (cell
->type
== ID($_MUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1711 if (cell
->type
== ID($_NMUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1712 if (cell
->type
== ID($_AOI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1713 if (cell
->type
== ID($_OAI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1714 if (cell
->type
== ID($_AOI4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::Y
,1); check_expected(); return; }
1715 if (cell
->type
== ID($_OAI4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::Y
,1); check_expected(); return; }
1717 if (cell
->type
== ID($_TBUF_
)) { port(ID::A
,1); port(ID::Y
,1); port(ID::E
,1); check_expected(); return; }
1719 if (cell
->type
== ID($_MUX4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::S
,1); port(ID::T
,1); port(ID::Y
,1); check_expected(); return; }
1720 if (cell
->type
== ID($_MUX8_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::E
,1); port(ID::F
,1); port(ID::G
,1); port(ID::H
,1); port(ID::S
,1); port(ID::T
,1); port(ID::U
,1); port(ID::Y
,1); check_expected(); return; }
1721 if (cell
->type
== ID($_MUX16_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::E
,1); port(ID::F
,1); port(ID::G
,1); port(ID::H
,1); port(ID::I
,1); port(ID::J
,1); port(ID::K
,1); port(ID::L
,1); port(ID::M
,1); port(ID::N
,1); port(ID::O
,1); port(ID::P
,1); port(ID::S
,1); port(ID::T
,1); port(ID::U
,1); port(ID::V
,1); port(ID::Y
,1); check_expected(); return; }
1723 if (cell
->type
.in(ID($_SR_NN_
), ID($_SR_NP_
), ID($_SR_PN_
), ID($_SR_PP_
)))
1724 { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1726 if (cell
->type
== ID($_FF_
)) { port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1728 if (cell
->type
.in(ID($_DFF_N_
), ID($_DFF_P_
)))
1729 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); check_expected(); return; }
1731 if (cell
->type
.in(ID($_DFFE_NN_
), ID($_DFFE_NP_
), ID($_DFFE_PN_
), ID($_DFFE_PP_
)))
1732 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1735 ID($_DFF_NN0_
), ID($_DFF_NN1_
), ID($_DFF_NP0_
), ID($_DFF_NP1_
),
1736 ID($_DFF_PN0_
), ID($_DFF_PN1_
), ID($_DFF_PP0_
), ID($_DFF_PP1_
)))
1737 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1740 ID($_DFFE_NN0N_
), ID($_DFFE_NN0P_
), ID($_DFFE_NN1N_
), ID($_DFFE_NN1P_
),
1741 ID($_DFFE_NP0N_
), ID($_DFFE_NP0P_
), ID($_DFFE_NP1N_
), ID($_DFFE_NP1P_
),
1742 ID($_DFFE_PN0N_
), ID($_DFFE_PN0P_
), ID($_DFFE_PN1N_
), ID($_DFFE_PN1P_
),
1743 ID($_DFFE_PP0N_
), ID($_DFFE_PP0P_
), ID($_DFFE_PP1N_
), ID($_DFFE_PP1P_
)))
1744 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1747 ID($_ALDFF_NN_
), ID($_ALDFF_NP_
), ID($_ALDFF_PN_
), ID($_ALDFF_PP_
)))
1748 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::L
,1); port(ID::AD
,1); check_expected(); return; }
1751 ID($_ALDFFE_NNN_
), ID($_ALDFFE_NNP_
), ID($_ALDFFE_NPN_
), ID($_ALDFFE_NPP_
),
1752 ID($_ALDFFE_PNN_
), ID($_ALDFFE_PNP_
), ID($_ALDFFE_PPN_
), ID($_ALDFFE_PPP_
)))
1753 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::L
,1); port(ID::AD
,1); port(ID::E
,1); check_expected(); return; }
1756 ID($_DFFSR_NNN_
), ID($_DFFSR_NNP_
), ID($_DFFSR_NPN_
), ID($_DFFSR_NPP_
),
1757 ID($_DFFSR_PNN_
), ID($_DFFSR_PNP_
), ID($_DFFSR_PPN_
), ID($_DFFSR_PPP_
)))
1758 { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1761 ID($_DFFSRE_NNNN_
), ID($_DFFSRE_NNNP_
), ID($_DFFSRE_NNPN_
), ID($_DFFSRE_NNPP_
),
1762 ID($_DFFSRE_NPNN_
), ID($_DFFSRE_NPNP_
), ID($_DFFSRE_NPPN_
), ID($_DFFSRE_NPPP_
),
1763 ID($_DFFSRE_PNNN_
), ID($_DFFSRE_PNNP_
), ID($_DFFSRE_PNPN_
), ID($_DFFSRE_PNPP_
),
1764 ID($_DFFSRE_PPNN_
), ID($_DFFSRE_PPNP_
), ID($_DFFSRE_PPPN_
), ID($_DFFSRE_PPPP_
)))
1765 { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::E
,1); port(ID::Q
,1); check_expected(); return; }
1768 ID($_SDFF_NN0_
), ID($_SDFF_NN1_
), ID($_SDFF_NP0_
), ID($_SDFF_NP1_
),
1769 ID($_SDFF_PN0_
), ID($_SDFF_PN1_
), ID($_SDFF_PP0_
), ID($_SDFF_PP1_
)))
1770 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1773 ID($_SDFFE_NN0N_
), ID($_SDFFE_NN0P_
), ID($_SDFFE_NN1N_
), ID($_SDFFE_NN1P_
),
1774 ID($_SDFFE_NP0N_
), ID($_SDFFE_NP0P_
), ID($_SDFFE_NP1N_
), ID($_SDFFE_NP1P_
),
1775 ID($_SDFFE_PN0N_
), ID($_SDFFE_PN0P_
), ID($_SDFFE_PN1N_
), ID($_SDFFE_PN1P_
),
1776 ID($_SDFFE_PP0N_
), ID($_SDFFE_PP0P_
), ID($_SDFFE_PP1N_
), ID($_SDFFE_PP1P_
),
1777 ID($_SDFFCE_NN0N_
), ID($_SDFFCE_NN0P_
), ID($_SDFFCE_NN1N_
), ID($_SDFFCE_NN1P_
),
1778 ID($_SDFFCE_NP0N_
), ID($_SDFFCE_NP0P_
), ID($_SDFFCE_NP1N_
), ID($_SDFFCE_NP1P_
),
1779 ID($_SDFFCE_PN0N_
), ID($_SDFFCE_PN0P_
), ID($_SDFFCE_PN1N_
), ID($_SDFFCE_PN1P_
),
1780 ID($_SDFFCE_PP0N_
), ID($_SDFFCE_PP0P_
), ID($_SDFFCE_PP1N_
), ID($_SDFFCE_PP1P_
)))
1781 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1783 if (cell
->type
.in(ID($_DLATCH_N_
), ID($_DLATCH_P_
)))
1784 { port(ID::E
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1787 ID($_DLATCH_NN0_
), ID($_DLATCH_NN1_
), ID($_DLATCH_NP0_
), ID($_DLATCH_NP1_
),
1788 ID($_DLATCH_PN0_
), ID($_DLATCH_PN1_
), ID($_DLATCH_PP0_
), ID($_DLATCH_PP1_
)))
1789 { port(ID::E
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1792 ID($_DLATCHSR_NNN_
), ID($_DLATCHSR_NNP_
), ID($_DLATCHSR_NPN_
), ID($_DLATCHSR_NPP_
),
1793 ID($_DLATCHSR_PNN_
), ID($_DLATCHSR_PNP_
), ID($_DLATCHSR_PPN_
), ID($_DLATCHSR_PPP_
)))
1794 { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1802 void RTLIL::Module::sort()
1804 wires_
.sort(sort_by_id_str());
1805 cells_
.sort(sort_by_id_str());
1806 parameter_default_values
.sort(sort_by_id_str());
1807 memories
.sort(sort_by_id_str());
1808 processes
.sort(sort_by_id_str());
1809 for (auto &it
: cells_
)
1811 for (auto &it
: wires_
)
1812 it
.second
->attributes
.sort(sort_by_id_str());
1813 for (auto &it
: memories
)
1814 it
.second
->attributes
.sort(sort_by_id_str());
1817 void RTLIL::Module::check()
1820 std::vector
<bool> ports_declared
;
1821 for (auto &it
: wires_
) {
1822 log_assert(this == it
.second
->module
);
1823 log_assert(it
.first
== it
.second
->name
);
1824 log_assert(!it
.first
.empty());
1825 log_assert(it
.second
->width
>= 0);
1826 log_assert(it
.second
->port_id
>= 0);
1827 for (auto &it2
: it
.second
->attributes
)
1828 log_assert(!it2
.first
.empty());
1829 if (it
.second
->port_id
) {
1830 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1831 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1832 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1833 if (GetSize(ports_declared
) < it
.second
->port_id
)
1834 ports_declared
.resize(it
.second
->port_id
);
1835 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1836 ports_declared
[it
.second
->port_id
-1] = true;
1838 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1840 for (auto port_declared
: ports_declared
)
1841 log_assert(port_declared
== true);
1842 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1844 for (auto &it
: memories
) {
1845 log_assert(it
.first
== it
.second
->name
);
1846 log_assert(!it
.first
.empty());
1847 log_assert(it
.second
->width
>= 0);
1848 log_assert(it
.second
->size
>= 0);
1849 for (auto &it2
: it
.second
->attributes
)
1850 log_assert(!it2
.first
.empty());
1853 for (auto &it
: cells_
) {
1854 log_assert(this == it
.second
->module
);
1855 log_assert(it
.first
== it
.second
->name
);
1856 log_assert(!it
.first
.empty());
1857 log_assert(!it
.second
->type
.empty());
1858 for (auto &it2
: it
.second
->connections()) {
1859 log_assert(!it2
.first
.empty());
1860 it2
.second
.check(this);
1862 for (auto &it2
: it
.second
->attributes
)
1863 log_assert(!it2
.first
.empty());
1864 for (auto &it2
: it
.second
->parameters
)
1865 log_assert(!it2
.first
.empty());
1866 InternalCellChecker
checker(this, it
.second
);
1870 for (auto &it
: processes
) {
1871 log_assert(it
.first
== it
.second
->name
);
1872 log_assert(!it
.first
.empty());
1873 log_assert(it
.second
->root_case
.compare
.empty());
1874 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1875 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1876 for (auto &switch_it
: all_cases
[i
]->switches
) {
1877 for (auto &case_it
: switch_it
->cases
) {
1878 for (auto &compare_it
: case_it
->compare
) {
1879 log_assert(switch_it
->signal
.size() == compare_it
.size());
1881 all_cases
.push_back(case_it
);
1885 for (auto &sync_it
: it
.second
->syncs
) {
1886 switch (sync_it
->type
) {
1892 log_assert(!sync_it
->signal
.empty());
1897 log_assert(sync_it
->signal
.empty());
1903 for (auto &it
: connections_
) {
1904 log_assert(it
.first
.size() == it
.second
.size());
1905 log_assert(!it
.first
.has_const());
1906 it
.first
.check(this);
1907 it
.second
.check(this);
1910 for (auto &it
: attributes
)
1911 log_assert(!it
.first
.empty());
1915 void RTLIL::Module::optimize()
1919 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1921 log_assert(new_mod
->refcount_wires_
== 0);
1922 log_assert(new_mod
->refcount_cells_
== 0);
1924 new_mod
->avail_parameters
= avail_parameters
;
1925 new_mod
->parameter_default_values
= parameter_default_values
;
1927 for (auto &conn
: connections_
)
1928 new_mod
->connect(conn
);
1930 for (auto &attr
: attributes
)
1931 new_mod
->attributes
[attr
.first
] = attr
.second
;
1933 for (auto &it
: wires_
)
1934 new_mod
->addWire(it
.first
, it
.second
);
1936 for (auto &it
: memories
)
1937 new_mod
->addMemory(it
.first
, it
.second
);
1939 for (auto &it
: cells_
)
1940 new_mod
->addCell(it
.first
, it
.second
);
1942 for (auto &it
: processes
)
1943 new_mod
->addProcess(it
.first
, it
.second
);
1945 struct RewriteSigSpecWorker
1948 void operator()(RTLIL::SigSpec
&sig
)
1951 for (auto &c
: sig
.chunks_
)
1953 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1957 RewriteSigSpecWorker rewriteSigSpecWorker
;
1958 rewriteSigSpecWorker
.mod
= new_mod
;
1959 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1960 new_mod
->fixup_ports();
1963 RTLIL::Module
*RTLIL::Module::clone() const
1965 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1966 new_mod
->name
= name
;
1971 bool RTLIL::Module::has_memories() const
1973 return !memories
.empty();
1976 bool RTLIL::Module::has_processes() const
1978 return !processes
.empty();
1981 bool RTLIL::Module::has_memories_warn() const
1983 if (!memories
.empty())
1984 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1985 return !memories
.empty();
1988 bool RTLIL::Module::has_processes_warn() const
1990 if (!processes
.empty())
1991 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1992 return !processes
.empty();
1995 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1997 std::vector
<RTLIL::Wire
*> result
;
1998 result
.reserve(wires_
.size());
1999 for (auto &it
: wires_
)
2000 if (design
->selected(this, it
.second
))
2001 result
.push_back(it
.second
);
2005 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
2007 std::vector
<RTLIL::Cell
*> result
;
2008 result
.reserve(cells_
.size());
2009 for (auto &it
: cells_
)
2010 if (design
->selected(this, it
.second
))
2011 result
.push_back(it
.second
);
2015 void RTLIL::Module::add(RTLIL::Wire
*wire
)
2017 log_assert(!wire
->name
.empty());
2018 log_assert(count_id(wire
->name
) == 0);
2019 log_assert(refcount_wires_
== 0);
2020 wires_
[wire
->name
] = wire
;
2021 wire
->module
= this;
2024 void RTLIL::Module::add(RTLIL::Cell
*cell
)
2026 log_assert(!cell
->name
.empty());
2027 log_assert(count_id(cell
->name
) == 0);
2028 log_assert(refcount_cells_
== 0);
2029 cells_
[cell
->name
] = cell
;
2030 cell
->module
= this;
2033 void RTLIL::Module::add(RTLIL::Process
*process
)
2035 log_assert(!process
->name
.empty());
2036 log_assert(count_id(process
->name
) == 0);
2037 processes
[process
->name
] = process
;
2038 process
->module
= this;
2041 void RTLIL::Module::add(RTLIL::Binding
*binding
)
2043 log_assert(binding
!= nullptr);
2044 bindings_
.push_back(binding
);
2047 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
2049 log_assert(refcount_wires_
== 0);
2051 struct DeleteWireWorker
2053 RTLIL::Module
*module
;
2054 const pool
<RTLIL::Wire
*> *wires_p
;
2056 void operator()(RTLIL::SigSpec
&sig
) {
2058 for (auto &c
: sig
.chunks_
)
2059 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
2060 c
.wire
= module
->addWire(stringf("$delete_wire$%d", autoidx
++), c
.width
);
2065 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
2066 log_assert(GetSize(lhs
) == GetSize(rhs
));
2069 for (int i
= 0; i
< GetSize(lhs
); i
++) {
2070 RTLIL::SigBit
&lhs_bit
= lhs
.bits_
[i
];
2071 RTLIL::SigBit
&rhs_bit
= rhs
.bits_
[i
];
2072 if ((lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
)) || (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))) {
2073 lhs_bit
= State::Sx
;
2074 rhs_bit
= State::Sx
;
2080 DeleteWireWorker delete_wire_worker
;
2081 delete_wire_worker
.module
= this;
2082 delete_wire_worker
.wires_p
= &wires
;
2083 rewrite_sigspecs2(delete_wire_worker
);
2085 for (auto &it
: wires
) {
2086 log_assert(wires_
.count(it
->name
) != 0);
2087 wires_
.erase(it
->name
);
2092 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
2094 while (!cell
->connections_
.empty())
2095 cell
->unsetPort(cell
->connections_
.begin()->first
);
2097 log_assert(cells_
.count(cell
->name
) != 0);
2098 log_assert(refcount_cells_
== 0);
2099 cells_
.erase(cell
->name
);
2103 void RTLIL::Module::remove(RTLIL::Process
*process
)
2105 log_assert(processes
.count(process
->name
) != 0);
2106 processes
.erase(process
->name
);
2110 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
2112 log_assert(wires_
[wire
->name
] == wire
);
2113 log_assert(refcount_wires_
== 0);
2114 wires_
.erase(wire
->name
);
2115 wire
->name
= new_name
;
2119 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
2121 log_assert(cells_
[cell
->name
] == cell
);
2122 log_assert(refcount_wires_
== 0);
2123 cells_
.erase(cell
->name
);
2124 cell
->name
= new_name
;
2128 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
2130 log_assert(count_id(old_name
) != 0);
2131 if (wires_
.count(old_name
))
2132 rename(wires_
.at(old_name
), new_name
);
2133 else if (cells_
.count(old_name
))
2134 rename(cells_
.at(old_name
), new_name
);
2139 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
2141 log_assert(wires_
[w1
->name
] == w1
);
2142 log_assert(wires_
[w2
->name
] == w2
);
2143 log_assert(refcount_wires_
== 0);
2145 wires_
.erase(w1
->name
);
2146 wires_
.erase(w2
->name
);
2148 std::swap(w1
->name
, w2
->name
);
2150 wires_
[w1
->name
] = w1
;
2151 wires_
[w2
->name
] = w2
;
2154 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
2156 log_assert(cells_
[c1
->name
] == c1
);
2157 log_assert(cells_
[c2
->name
] == c2
);
2158 log_assert(refcount_cells_
== 0);
2160 cells_
.erase(c1
->name
);
2161 cells_
.erase(c2
->name
);
2163 std::swap(c1
->name
, c2
->name
);
2165 cells_
[c1
->name
] = c1
;
2166 cells_
[c2
->name
] = c2
;
2169 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
2172 return uniquify(name
, index
);
2175 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
2178 if (count_id(name
) == 0)
2184 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
2185 if (count_id(new_name
) == 0)
2191 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
2193 if (a
->port_id
&& !b
->port_id
)
2195 if (!a
->port_id
&& b
->port_id
)
2198 if (a
->port_id
== b
->port_id
)
2199 return a
->name
< b
->name
;
2200 return a
->port_id
< b
->port_id
;
2203 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
2205 for (auto mon
: monitors
)
2206 mon
->notify_connect(this, conn
);
2209 for (auto mon
: design
->monitors
)
2210 mon
->notify_connect(this, conn
);
2212 // ignore all attempts to assign constants to other constants
2213 if (conn
.first
.has_const()) {
2214 RTLIL::SigSig new_conn
;
2215 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
2216 if (conn
.first
[i
].wire
) {
2217 new_conn
.first
.append(conn
.first
[i
]);
2218 new_conn
.second
.append(conn
.second
[i
]);
2220 if (GetSize(new_conn
.first
))
2226 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
));
2227 log_backtrace("-X- ", yosys_xtrace
-1);
2230 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
2231 connections_
.push_back(conn
);
2234 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
2236 connect(RTLIL::SigSig(lhs
, rhs
));
2239 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
2241 for (auto mon
: monitors
)
2242 mon
->notify_connect(this, new_conn
);
2245 for (auto mon
: design
->monitors
)
2246 mon
->notify_connect(this, new_conn
);
2249 log("#X# New connections vector in %s:\n", log_id(this));
2250 for (auto &conn
: new_conn
)
2251 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
2252 log_backtrace("-X- ", yosys_xtrace
-1);
2255 connections_
= new_conn
;
2258 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
2260 return connections_
;
2263 void RTLIL::Module::fixup_ports()
2265 std::vector
<RTLIL::Wire
*> all_ports
;
2267 for (auto &w
: wires_
)
2268 if (w
.second
->port_input
|| w
.second
->port_output
)
2269 all_ports
.push_back(w
.second
);
2271 w
.second
->port_id
= 0;
2273 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
2276 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
2277 ports
.push_back(all_ports
[i
]->name
);
2278 all_ports
[i
]->port_id
= i
+1;
2282 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
2284 RTLIL::Wire
*wire
= new RTLIL::Wire
;
2286 wire
->width
= width
;
2291 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
2293 RTLIL::Wire
*wire
= addWire(name
);
2294 wire
->width
= other
->width
;
2295 wire
->start_offset
= other
->start_offset
;
2296 wire
->port_id
= other
->port_id
;
2297 wire
->port_input
= other
->port_input
;
2298 wire
->port_output
= other
->port_output
;
2299 wire
->upto
= other
->upto
;
2300 wire
->is_signed
= other
->is_signed
;
2301 wire
->attributes
= other
->attributes
;
2305 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
2307 RTLIL::Cell
*cell
= new RTLIL::Cell
;
2314 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
2316 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
2317 cell
->connections_
= other
->connections_
;
2318 cell
->parameters
= other
->parameters
;
2319 cell
->attributes
= other
->attributes
;
2323 RTLIL::Memory
*RTLIL::Module::addMemory(RTLIL::IdString name
, const RTLIL::Memory
*other
)
2325 RTLIL::Memory
*mem
= new RTLIL::Memory
;
2327 mem
->width
= other
->width
;
2328 mem
->start_offset
= other
->start_offset
;
2329 mem
->size
= other
->size
;
2330 mem
->attributes
= other
->attributes
;
2331 memories
[mem
->name
] = mem
;
2335 RTLIL::Process
*RTLIL::Module::addProcess(RTLIL::IdString name
)
2337 RTLIL::Process
*proc
= new RTLIL::Process
;
2343 RTLIL::Process
*RTLIL::Module::addProcess(RTLIL::IdString name
, const RTLIL::Process
*other
)
2345 RTLIL::Process
*proc
= other
->clone();
2351 #define DEF_METHOD(_func, _y_size, _type) \
2352 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
2353 RTLIL::Cell *cell = addCell(name, _type); \
2354 cell->parameters[ID::A_SIGNED] = is_signed; \
2355 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2356 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2357 cell->setPort(ID::A, sig_a); \
2358 cell->setPort(ID::Y, sig_y); \
2359 cell->set_src_attribute(src); \
2362 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed, const std::string &src) { \
2363 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2364 add ## _func(name, sig_a, sig_y, is_signed, src); \
2367 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
2368 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
2369 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
2370 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
2371 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
2372 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
2373 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
2374 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
2375 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
2378 #define DEF_METHOD(_func, _y_size, _type) \
2379 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
2380 RTLIL::Cell *cell = addCell(name, _type); \
2381 cell->parameters[ID::A_SIGNED] = is_signed; \
2382 cell->parameters[ID::B_SIGNED] = is_signed; \
2383 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2384 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2385 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2386 cell->setPort(ID::A, sig_a); \
2387 cell->setPort(ID::B, sig_b); \
2388 cell->setPort(ID::Y, sig_y); \
2389 cell->set_src_attribute(src); \
2392 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed, const std::string &src) { \
2393 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2394 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2397 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
2398 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
2399 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
2400 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
2401 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
2402 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
2403 DEF_METHOD(Lt
, 1, ID($lt
))
2404 DEF_METHOD(Le
, 1, ID($le
))
2405 DEF_METHOD(Eq
, 1, ID($eq
))
2406 DEF_METHOD(Ne
, 1, ID($ne
))
2407 DEF_METHOD(Eqx
, 1, ID($eqx
))
2408 DEF_METHOD(Nex
, 1, ID($nex
))
2409 DEF_METHOD(Ge
, 1, ID($ge
))
2410 DEF_METHOD(Gt
, 1, ID($gt
))
2411 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
2412 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
2413 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
2414 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
2415 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
2416 DEF_METHOD(DivFloor
, max(sig_a
.size(), sig_b
.size()), ID($divfloor
))
2417 DEF_METHOD(ModFloor
, max(sig_a
.size(), sig_b
.size()), ID($modfloor
))
2418 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
2419 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
2422 #define DEF_METHOD(_func, _y_size, _type) \
2423 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
2424 RTLIL::Cell *cell = addCell(name, _type); \
2425 cell->parameters[ID::A_SIGNED] = is_signed; \
2426 cell->parameters[ID::B_SIGNED] = false; \
2427 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2428 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2429 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2430 cell->setPort(ID::A, sig_a); \
2431 cell->setPort(ID::B, sig_b); \
2432 cell->setPort(ID::Y, sig_y); \
2433 cell->set_src_attribute(src); \
2436 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed, const std::string &src) { \
2437 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2438 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2441 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
2442 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
2443 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
2444 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
2447 #define DEF_METHOD(_func, _type, _pmux) \
2448 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const RTLIL::SigSpec &sig_y, const std::string &src) { \
2449 RTLIL::Cell *cell = addCell(name, _type); \
2450 cell->parameters[ID::WIDTH] = sig_a.size(); \
2451 if (_pmux) cell->parameters[ID::S_WIDTH] = sig_s.size(); \
2452 cell->setPort(ID::A, sig_a); \
2453 cell->setPort(ID::B, sig_b); \
2454 cell->setPort(ID::S, sig_s); \
2455 cell->setPort(ID::Y, sig_y); \
2456 cell->set_src_attribute(src); \
2459 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const std::string &src) { \
2460 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
2461 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
2464 DEF_METHOD(Mux
, ID($mux
), 0)
2465 DEF_METHOD(Pmux
, ID($pmux
), 1)
2468 #define DEF_METHOD(_func, _type, _demux) \
2469 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_s, const RTLIL::SigSpec &sig_y, const std::string &src) { \
2470 RTLIL::Cell *cell = addCell(name, _type); \
2471 cell->parameters[ID::WIDTH] = _demux ? sig_a.size() : sig_y.size(); \
2472 cell->parameters[ID::S_WIDTH] = sig_s.size(); \
2473 cell->setPort(ID::A, sig_a); \
2474 cell->setPort(ID::S, sig_s); \
2475 cell->setPort(ID::Y, sig_y); \
2476 cell->set_src_attribute(src); \
2479 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_s, const std::string &src) { \
2480 RTLIL::SigSpec sig_y = addWire(NEW_ID, _demux ? sig_a.size() << sig_s.size() : sig_a.size() >> sig_s.size()); \
2481 add ## _func(name, sig_a, sig_s, sig_y, src); \
2484 DEF_METHOD(Bmux
, ID($bmux
), 0)
2485 DEF_METHOD(Demux
, ID($demux
), 1)
2488 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
2489 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2490 RTLIL::Cell *cell = addCell(name, _type); \
2491 cell->setPort("\\" #_P1, sig1); \
2492 cell->setPort("\\" #_P2, sig2); \
2493 cell->set_src_attribute(src); \
2496 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const std::string &src) { \
2497 RTLIL::SigBit sig2 = addWire(NEW_ID); \
2498 add ## _func(name, sig1, sig2, src); \
2501 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
2502 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2503 RTLIL::Cell *cell = addCell(name, _type); \
2504 cell->setPort("\\" #_P1, sig1); \
2505 cell->setPort("\\" #_P2, sig2); \
2506 cell->setPort("\\" #_P3, sig3); \
2507 cell->set_src_attribute(src); \
2510 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2511 RTLIL::SigBit sig3 = addWire(NEW_ID); \
2512 add ## _func(name, sig1, sig2, sig3, src); \
2515 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
2516 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const std::string &src) { \
2517 RTLIL::Cell *cell = addCell(name, _type); \
2518 cell->setPort("\\" #_P1, sig1); \
2519 cell->setPort("\\" #_P2, sig2); \
2520 cell->setPort("\\" #_P3, sig3); \
2521 cell->setPort("\\" #_P4, sig4); \
2522 cell->set_src_attribute(src); \
2525 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2526 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2527 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2530 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2531 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const RTLIL::SigBit &sig5, const std::string &src) { \
2532 RTLIL::Cell *cell = addCell(name, _type); \
2533 cell->setPort("\\" #_P1, sig1); \
2534 cell->setPort("\\" #_P2, sig2); \
2535 cell->setPort("\\" #_P3, sig3); \
2536 cell->setPort("\\" #_P4, sig4); \
2537 cell->setPort("\\" #_P5, sig5); \
2538 cell->set_src_attribute(src); \
2541 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const std::string &src) { \
2542 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2543 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2546 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2547 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2548 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2549 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2550 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2551 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2552 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2553 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2554 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2555 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2556 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2557 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2558 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2559 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2560 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2561 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2567 RTLIL::Cell
* RTLIL::Module::addPow(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, bool a_signed
, bool b_signed
, const std::string
&src
)
2569 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2570 cell
->parameters
[ID::A_SIGNED
] = a_signed
;
2571 cell
->parameters
[ID::B_SIGNED
] = b_signed
;
2572 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2573 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2574 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2575 cell
->setPort(ID::A
, sig_a
);
2576 cell
->setPort(ID::B
, sig_b
);
2577 cell
->setPort(ID::Y
, sig_y
);
2578 cell
->set_src_attribute(src
);
2582 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_y
, RTLIL::Const offset
, const std::string
&src
)
2584 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2585 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2586 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2587 cell
->parameters
[ID::OFFSET
] = offset
;
2588 cell
->setPort(ID::A
, sig_a
);
2589 cell
->setPort(ID::Y
, sig_y
);
2590 cell
->set_src_attribute(src
);
2594 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2596 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2597 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2598 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2599 cell
->setPort(ID::A
, sig_a
);
2600 cell
->setPort(ID::B
, sig_b
);
2601 cell
->setPort(ID::Y
, sig_y
);
2602 cell
->set_src_attribute(src
);
2606 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_y
, RTLIL::Const lut
, const std::string
&src
)
2608 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2609 cell
->parameters
[ID::LUT
] = lut
;
2610 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2611 cell
->setPort(ID::A
, sig_a
);
2612 cell
->setPort(ID::Y
, sig_y
);
2613 cell
->set_src_attribute(src
);
2617 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2619 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2620 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2621 cell
->setPort(ID::A
, sig_a
);
2622 cell
->setPort(ID::EN
, sig_en
);
2623 cell
->setPort(ID::Y
, sig_y
);
2624 cell
->set_src_attribute(src
);
2628 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2630 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2631 cell
->setPort(ID::A
, sig_a
);
2632 cell
->setPort(ID::EN
, sig_en
);
2633 cell
->set_src_attribute(src
);
2637 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2639 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2640 cell
->setPort(ID::A
, sig_a
);
2641 cell
->setPort(ID::EN
, sig_en
);
2642 cell
->set_src_attribute(src
);
2646 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2648 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2649 cell
->setPort(ID::A
, sig_a
);
2650 cell
->setPort(ID::EN
, sig_en
);
2651 cell
->set_src_attribute(src
);
2655 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2657 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2658 cell
->setPort(ID::A
, sig_a
);
2659 cell
->setPort(ID::EN
, sig_en
);
2660 cell
->set_src_attribute(src
);
2664 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2666 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2667 cell
->setPort(ID::A
, sig_a
);
2668 cell
->setPort(ID::EN
, sig_en
);
2669 cell
->set_src_attribute(src
);
2673 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2675 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2676 cell
->setPort(ID::A
, sig_a
);
2677 cell
->setPort(ID::B
, sig_b
);
2678 cell
->setPort(ID::Y
, sig_y
);
2679 cell
->set_src_attribute(src
);
2683 RTLIL::Cell
* RTLIL::Module::addSr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
, const RTLIL::SigSpec
&sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2685 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2686 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2687 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2688 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2689 cell
->setPort(ID::SET
, sig_set
);
2690 cell
->setPort(ID::CLR
, sig_clr
);
2691 cell
->setPort(ID::Q
, sig_q
);
2692 cell
->set_src_attribute(src
);
2696 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2698 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2699 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2700 cell
->setPort(ID::D
, sig_d
);
2701 cell
->setPort(ID::Q
, sig_q
);
2702 cell
->set_src_attribute(src
);
2706 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, const std::string
&src
)
2708 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2709 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2710 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2711 cell
->setPort(ID::CLK
, sig_clk
);
2712 cell
->setPort(ID::D
, sig_d
);
2713 cell
->setPort(ID::Q
, sig_q
);
2714 cell
->set_src_attribute(src
);
2718 RTLIL::Cell
* RTLIL::Module::addDffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2720 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2721 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2722 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2723 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2724 cell
->setPort(ID::CLK
, sig_clk
);
2725 cell
->setPort(ID::EN
, sig_en
);
2726 cell
->setPort(ID::D
, sig_d
);
2727 cell
->setPort(ID::Q
, sig_q
);
2728 cell
->set_src_attribute(src
);
2732 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2733 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2735 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2736 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2737 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2738 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2739 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2740 cell
->setPort(ID::CLK
, sig_clk
);
2741 cell
->setPort(ID::SET
, sig_set
);
2742 cell
->setPort(ID::CLR
, sig_clr
);
2743 cell
->setPort(ID::D
, sig_d
);
2744 cell
->setPort(ID::Q
, sig_q
);
2745 cell
->set_src_attribute(src
);
2749 RTLIL::Cell
* RTLIL::Module::addDffsre(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2750 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2752 RTLIL::Cell
*cell
= addCell(name
, ID($dffsre
));
2753 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2754 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2755 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2756 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2757 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2758 cell
->setPort(ID::CLK
, sig_clk
);
2759 cell
->setPort(ID::EN
, sig_en
);
2760 cell
->setPort(ID::SET
, sig_set
);
2761 cell
->setPort(ID::CLR
, sig_clr
);
2762 cell
->setPort(ID::D
, sig_d
);
2763 cell
->setPort(ID::Q
, sig_q
);
2764 cell
->set_src_attribute(src
);
2768 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2769 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2771 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2772 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2773 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2774 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2775 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2776 cell
->setPort(ID::CLK
, sig_clk
);
2777 cell
->setPort(ID::ARST
, sig_arst
);
2778 cell
->setPort(ID::D
, sig_d
);
2779 cell
->setPort(ID::Q
, sig_q
);
2780 cell
->set_src_attribute(src
);
2784 RTLIL::Cell
* RTLIL::Module::addAdffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2785 RTLIL::Const arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2787 RTLIL::Cell
*cell
= addCell(name
, ID($adffe
));
2788 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2789 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2790 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2791 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2792 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2793 cell
->setPort(ID::CLK
, sig_clk
);
2794 cell
->setPort(ID::EN
, sig_en
);
2795 cell
->setPort(ID::ARST
, sig_arst
);
2796 cell
->setPort(ID::D
, sig_d
);
2797 cell
->setPort(ID::Q
, sig_q
);
2798 cell
->set_src_attribute(src
);
2802 RTLIL::Cell
* RTLIL::Module::addAldff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_aload
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2803 const RTLIL::SigSpec
&sig_ad
, bool clk_polarity
, bool aload_polarity
, const std::string
&src
)
2805 RTLIL::Cell
*cell
= addCell(name
, ID($aldff
));
2806 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2807 cell
->parameters
[ID::ALOAD_POLARITY
] = aload_polarity
;
2808 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2809 cell
->setPort(ID::CLK
, sig_clk
);
2810 cell
->setPort(ID::ALOAD
, sig_aload
);
2811 cell
->setPort(ID::D
, sig_d
);
2812 cell
->setPort(ID::AD
, sig_ad
);
2813 cell
->setPort(ID::Q
, sig_q
);
2814 cell
->set_src_attribute(src
);
2818 RTLIL::Cell
* RTLIL::Module::addAldffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_aload
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2819 const RTLIL::SigSpec
&sig_ad
, bool clk_polarity
, bool en_polarity
, bool aload_polarity
, const std::string
&src
)
2821 RTLIL::Cell
*cell
= addCell(name
, ID($aldffe
));
2822 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2823 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2824 cell
->parameters
[ID::ALOAD_POLARITY
] = aload_polarity
;
2825 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2826 cell
->setPort(ID::CLK
, sig_clk
);
2827 cell
->setPort(ID::EN
, sig_en
);
2828 cell
->setPort(ID::ALOAD
, sig_aload
);
2829 cell
->setPort(ID::D
, sig_d
);
2830 cell
->setPort(ID::AD
, sig_ad
);
2831 cell
->setPort(ID::Q
, sig_q
);
2832 cell
->set_src_attribute(src
);
2836 RTLIL::Cell
* RTLIL::Module::addSdff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2837 RTLIL::Const srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
2839 RTLIL::Cell
*cell
= addCell(name
, ID($sdff
));
2840 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2841 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2842 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2843 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2844 cell
->setPort(ID::CLK
, sig_clk
);
2845 cell
->setPort(ID::SRST
, sig_srst
);
2846 cell
->setPort(ID::D
, sig_d
);
2847 cell
->setPort(ID::Q
, sig_q
);
2848 cell
->set_src_attribute(src
);
2852 RTLIL::Cell
* RTLIL::Module::addSdffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2853 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2855 RTLIL::Cell
*cell
= addCell(name
, ID($sdffe
));
2856 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2857 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2858 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2859 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2860 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2861 cell
->setPort(ID::CLK
, sig_clk
);
2862 cell
->setPort(ID::EN
, sig_en
);
2863 cell
->setPort(ID::SRST
, sig_srst
);
2864 cell
->setPort(ID::D
, sig_d
);
2865 cell
->setPort(ID::Q
, sig_q
);
2866 cell
->set_src_attribute(src
);
2870 RTLIL::Cell
* RTLIL::Module::addSdffce(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2871 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2873 RTLIL::Cell
*cell
= addCell(name
, ID($sdffce
));
2874 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2875 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2876 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2877 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2878 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2879 cell
->setPort(ID::CLK
, sig_clk
);
2880 cell
->setPort(ID::EN
, sig_en
);
2881 cell
->setPort(ID::SRST
, sig_srst
);
2882 cell
->setPort(ID::D
, sig_d
);
2883 cell
->setPort(ID::Q
, sig_q
);
2884 cell
->set_src_attribute(src
);
2888 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, const std::string
&src
)
2890 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2891 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2892 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2893 cell
->setPort(ID::EN
, sig_en
);
2894 cell
->setPort(ID::D
, sig_d
);
2895 cell
->setPort(ID::Q
, sig_q
);
2896 cell
->set_src_attribute(src
);
2900 RTLIL::Cell
* RTLIL::Module::addAdlatch(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2901 RTLIL::Const arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2903 RTLIL::Cell
*cell
= addCell(name
, ID($adlatch
));
2904 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2905 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2906 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2907 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2908 cell
->setPort(ID::EN
, sig_en
);
2909 cell
->setPort(ID::ARST
, sig_arst
);
2910 cell
->setPort(ID::D
, sig_d
);
2911 cell
->setPort(ID::Q
, sig_q
);
2912 cell
->set_src_attribute(src
);
2916 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2917 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2919 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2920 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2921 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2922 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2923 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2924 cell
->setPort(ID::EN
, sig_en
);
2925 cell
->setPort(ID::SET
, sig_set
);
2926 cell
->setPort(ID::CLR
, sig_clr
);
2927 cell
->setPort(ID::D
, sig_d
);
2928 cell
->setPort(ID::Q
, sig_q
);
2929 cell
->set_src_attribute(src
);
2933 RTLIL::Cell
* RTLIL::Module::addSrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2934 const RTLIL::SigSpec
&sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2936 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SR_%c%c_", set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2937 cell
->setPort(ID::S
, sig_set
);
2938 cell
->setPort(ID::R
, sig_clr
);
2939 cell
->setPort(ID::Q
, sig_q
);
2940 cell
->set_src_attribute(src
);
2944 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2946 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2947 cell
->setPort(ID::D
, sig_d
);
2948 cell
->setPort(ID::Q
, sig_q
);
2949 cell
->set_src_attribute(src
);
2953 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, const std::string
&src
)
2955 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2956 cell
->setPort(ID::C
, sig_clk
);
2957 cell
->setPort(ID::D
, sig_d
);
2958 cell
->setPort(ID::Q
, sig_q
);
2959 cell
->set_src_attribute(src
);
2963 RTLIL::Cell
* RTLIL::Module::addDffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2965 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2966 cell
->setPort(ID::C
, sig_clk
);
2967 cell
->setPort(ID::E
, sig_en
);
2968 cell
->setPort(ID::D
, sig_d
);
2969 cell
->setPort(ID::Q
, sig_q
);
2970 cell
->set_src_attribute(src
);
2974 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2975 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2977 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2978 cell
->setPort(ID::C
, sig_clk
);
2979 cell
->setPort(ID::S
, sig_set
);
2980 cell
->setPort(ID::R
, sig_clr
);
2981 cell
->setPort(ID::D
, sig_d
);
2982 cell
->setPort(ID::Q
, sig_q
);
2983 cell
->set_src_attribute(src
);
2987 RTLIL::Cell
* RTLIL::Module::addDffsreGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2988 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2990 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSRE_%c%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2991 cell
->setPort(ID::C
, sig_clk
);
2992 cell
->setPort(ID::S
, sig_set
);
2993 cell
->setPort(ID::R
, sig_clr
);
2994 cell
->setPort(ID::E
, sig_en
);
2995 cell
->setPort(ID::D
, sig_d
);
2996 cell
->setPort(ID::Q
, sig_q
);
2997 cell
->set_src_attribute(src
);
3001 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3002 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
3004 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
3005 cell
->setPort(ID::C
, sig_clk
);
3006 cell
->setPort(ID::R
, sig_arst
);
3007 cell
->setPort(ID::D
, sig_d
);
3008 cell
->setPort(ID::Q
, sig_q
);
3009 cell
->set_src_attribute(src
);
3013 RTLIL::Cell
* RTLIL::Module::addAdffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3014 bool arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
3016 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
3017 cell
->setPort(ID::C
, sig_clk
);
3018 cell
->setPort(ID::R
, sig_arst
);
3019 cell
->setPort(ID::E
, sig_en
);
3020 cell
->setPort(ID::D
, sig_d
);
3021 cell
->setPort(ID::Q
, sig_q
);
3022 cell
->set_src_attribute(src
);
3026 RTLIL::Cell
* RTLIL::Module::addAldffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_aload
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3027 const RTLIL::SigSpec
&sig_ad
, bool clk_polarity
, bool aload_polarity
, const std::string
&src
)
3029 RTLIL::Cell
*cell
= addCell(name
, stringf("$_ALDFF_%c%c_", clk_polarity
? 'P' : 'N', aload_polarity
? 'P' : 'N'));
3030 cell
->setPort(ID::C
, sig_clk
);
3031 cell
->setPort(ID::L
, sig_aload
);
3032 cell
->setPort(ID::D
, sig_d
);
3033 cell
->setPort(ID::AD
, sig_ad
);
3034 cell
->setPort(ID::Q
, sig_q
);
3035 cell
->set_src_attribute(src
);
3039 RTLIL::Cell
* RTLIL::Module::addAldffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_aload
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3040 const RTLIL::SigSpec
&sig_ad
, bool clk_polarity
, bool en_polarity
, bool aload_polarity
, const std::string
&src
)
3042 RTLIL::Cell
*cell
= addCell(name
, stringf("$_ALDFFE_%c%c%c_", clk_polarity
? 'P' : 'N', aload_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
3043 cell
->setPort(ID::C
, sig_clk
);
3044 cell
->setPort(ID::L
, sig_aload
);
3045 cell
->setPort(ID::E
, sig_en
);
3046 cell
->setPort(ID::D
, sig_d
);
3047 cell
->setPort(ID::AD
, sig_ad
);
3048 cell
->setPort(ID::Q
, sig_q
);
3049 cell
->set_src_attribute(src
);
3053 RTLIL::Cell
* RTLIL::Module::addSdffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3054 bool srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
3056 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFF_%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0'));
3057 cell
->setPort(ID::C
, sig_clk
);
3058 cell
->setPort(ID::R
, sig_srst
);
3059 cell
->setPort(ID::D
, sig_d
);
3060 cell
->setPort(ID::Q
, sig_q
);
3061 cell
->set_src_attribute(src
);
3065 RTLIL::Cell
* RTLIL::Module::addSdffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3066 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
3068 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFFE_%c%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
3069 cell
->setPort(ID::C
, sig_clk
);
3070 cell
->setPort(ID::R
, sig_srst
);
3071 cell
->setPort(ID::E
, sig_en
);
3072 cell
->setPort(ID::D
, sig_d
);
3073 cell
->setPort(ID::Q
, sig_q
);
3074 cell
->set_src_attribute(src
);
3078 RTLIL::Cell
* RTLIL::Module::addSdffceGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3079 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
3081 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFFCE_%c%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
3082 cell
->setPort(ID::C
, sig_clk
);
3083 cell
->setPort(ID::R
, sig_srst
);
3084 cell
->setPort(ID::E
, sig_en
);
3085 cell
->setPort(ID::D
, sig_d
);
3086 cell
->setPort(ID::Q
, sig_q
);
3087 cell
->set_src_attribute(src
);
3091 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, const std::string
&src
)
3093 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
3094 cell
->setPort(ID::E
, sig_en
);
3095 cell
->setPort(ID::D
, sig_d
);
3096 cell
->setPort(ID::Q
, sig_q
);
3097 cell
->set_src_attribute(src
);
3101 RTLIL::Cell
* RTLIL::Module::addAdlatchGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
3102 bool arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
3104 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c%c%c_", en_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
3105 cell
->setPort(ID::E
, sig_en
);
3106 cell
->setPort(ID::R
, sig_arst
);
3107 cell
->setPort(ID::D
, sig_d
);
3108 cell
->setPort(ID::Q
, sig_q
);
3109 cell
->set_src_attribute(src
);
3113 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
3114 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
3116 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
3117 cell
->setPort(ID::E
, sig_en
);
3118 cell
->setPort(ID::S
, sig_set
);
3119 cell
->setPort(ID::R
, sig_clr
);
3120 cell
->setPort(ID::D
, sig_d
);
3121 cell
->setPort(ID::Q
, sig_q
);
3122 cell
->set_src_attribute(src
);
3126 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
3128 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
3129 Cell
*cell
= addCell(name
, ID($anyconst
));
3130 cell
->setParam(ID::WIDTH
, width
);
3131 cell
->setPort(ID::Y
, sig
);
3132 cell
->set_src_attribute(src
);
3136 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
3138 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
3139 Cell
*cell
= addCell(name
, ID($anyseq
));
3140 cell
->setParam(ID::WIDTH
, width
);
3141 cell
->setPort(ID::Y
, sig
);
3142 cell
->set_src_attribute(src
);
3146 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
3148 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
3149 Cell
*cell
= addCell(name
, ID($allconst
));
3150 cell
->setParam(ID::WIDTH
, width
);
3151 cell
->setPort(ID::Y
, sig
);
3152 cell
->set_src_attribute(src
);
3156 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
3158 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
3159 Cell
*cell
= addCell(name
, ID($allseq
));
3160 cell
->setParam(ID::WIDTH
, width
);
3161 cell
->setPort(ID::Y
, sig
);
3162 cell
->set_src_attribute(src
);
3166 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
3168 RTLIL::SigSpec sig
= addWire(NEW_ID
);
3169 Cell
*cell
= addCell(name
, ID($initstate
));
3170 cell
->setPort(ID::Y
, sig
);
3171 cell
->set_src_attribute(src
);
3177 static unsigned int hashidx_count
= 123456789;
3178 hashidx_count
= mkhash_xorshift(hashidx_count
);
3179 hashidx_
= hashidx_count
;
3186 port_output
= false;
3191 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
3195 RTLIL::Wire::~Wire()
3198 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
3203 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
3204 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
3210 RTLIL::Memory::Memory()
3212 static unsigned int hashidx_count
= 123456789;
3213 hashidx_count
= mkhash_xorshift(hashidx_count
);
3214 hashidx_
= hashidx_count
;
3220 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
3224 RTLIL::Process::Process() : module(nullptr)
3226 static unsigned int hashidx_count
= 123456789;
3227 hashidx_count
= mkhash_xorshift(hashidx_count
);
3228 hashidx_
= hashidx_count
;
3231 RTLIL::Cell::Cell() : module(nullptr)
3233 static unsigned int hashidx_count
= 123456789;
3234 hashidx_count
= mkhash_xorshift(hashidx_count
);
3235 hashidx_
= hashidx_count
;
3237 // log("#memtrace# %p\n", this);
3241 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
3245 RTLIL::Cell::~Cell()
3248 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
3253 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
3254 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
3260 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
3262 return connections_
.count(portname
) != 0;
3265 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
3267 RTLIL::SigSpec signal
;
3268 auto conn_it
= connections_
.find(portname
);
3270 if (conn_it
!= connections_
.end())
3272 for (auto mon
: module
->monitors
)
3273 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
3276 for (auto mon
: module
->design
->monitors
)
3277 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
3280 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
3281 log_backtrace("-X- ", yosys_xtrace
-1);
3284 connections_
.erase(conn_it
);
3288 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
3290 auto r
= connections_
.insert(portname
);
3291 auto conn_it
= r
.first
;
3292 if (!r
.second
&& conn_it
->second
== signal
)
3295 for (auto mon
: module
->monitors
)
3296 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
3299 for (auto mon
: module
->design
->monitors
)
3300 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
3303 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
3304 log_backtrace("-X- ", yosys_xtrace
-1);
3307 conn_it
->second
= std::move(signal
);
3310 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
3312 return connections_
.at(portname
);
3315 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
3317 return connections_
;
3320 bool RTLIL::Cell::known() const
3322 if (yosys_celltypes
.cell_known(type
))
3324 if (module
&& module
->design
&& module
->design
->module(type
))
3329 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
3331 if (yosys_celltypes
.cell_known(type
))
3332 return yosys_celltypes
.cell_input(type
, portname
);
3333 if (module
&& module
->design
) {
3334 RTLIL::Module
*m
= module
->design
->module(type
);
3335 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3336 return w
&& w
->port_input
;
3341 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
3343 if (yosys_celltypes
.cell_known(type
))
3344 return yosys_celltypes
.cell_output(type
, portname
);
3345 if (module
&& module
->design
) {
3346 RTLIL::Module
*m
= module
->design
->module(type
);
3347 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3348 return w
&& w
->port_output
;
3353 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
3355 return parameters
.count(paramname
) != 0;
3358 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
3360 parameters
.erase(paramname
);
3363 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
3365 parameters
[paramname
] = std::move(value
);
3368 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
3370 const auto &it
= parameters
.find(paramname
);
3371 if (it
!= parameters
.end())
3373 if (module
&& module
->design
) {
3374 RTLIL::Module
*m
= module
->design
->module(type
);
3376 return m
->parameter_default_values
.at(paramname
);
3378 throw std::out_of_range("Cell::getParam()");
3381 void RTLIL::Cell::sort()
3383 connections_
.sort(sort_by_id_str());
3384 parameters
.sort(sort_by_id_str());
3385 attributes
.sort(sort_by_id_str());
3388 void RTLIL::Cell::check()
3391 InternalCellChecker
checker(NULL
, this);
3396 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
3398 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
3399 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
3402 if (type
== ID($mux
) || type
== ID($pmux
) || type
== ID($bmux
)) {
3403 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3404 if (type
!= ID($mux
))
3405 parameters
[ID::S_WIDTH
] = GetSize(connections_
[ID::S
]);
3410 if (type
== ID($demux
)) {
3411 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::A
]);
3412 parameters
[ID::S_WIDTH
] = GetSize(connections_
[ID::S
]);
3417 if (type
== ID($lut
) || type
== ID($sop
)) {
3418 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::A
]);
3422 if (type
== ID($fa
)) {
3423 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3427 if (type
== ID($lcu
)) {
3428 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::CO
]);
3432 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
3434 if (connections_
.count(ID::A
)) {
3435 if (signedness_ab
) {
3437 parameters
[ID::A_SIGNED
] = true;
3438 else if (parameters
.count(ID::A_SIGNED
) == 0)
3439 parameters
[ID::A_SIGNED
] = false;
3441 parameters
[ID::A_WIDTH
] = GetSize(connections_
[ID::A
]);
3444 if (connections_
.count(ID::B
)) {
3445 if (signedness_ab
) {
3447 parameters
[ID::B_SIGNED
] = true;
3448 else if (parameters
.count(ID::B_SIGNED
) == 0)
3449 parameters
[ID::B_SIGNED
] = false;
3451 parameters
[ID::B_WIDTH
] = GetSize(connections_
[ID::B
]);
3454 if (connections_
.count(ID::Y
))
3455 parameters
[ID::Y_WIDTH
] = GetSize(connections_
[ID::Y
]);
3457 if (connections_
.count(ID::Q
))
3458 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Q
]);
3463 bool RTLIL::Cell::has_memid() const
3465 return type
.in(ID($memwr
), ID($memwr_v2
), ID($memrd
), ID($memrd_v2
), ID($meminit
), ID($meminit_v2
));
3468 bool RTLIL::Cell::is_mem_cell() const
3470 return type
.in(ID($mem
), ID($mem_v2
)) || has_memid();
3473 RTLIL::SigChunk::SigChunk()
3480 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
3484 width
= GetSize(data
);
3488 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
3490 log_assert(wire
!= nullptr);
3492 this->width
= wire
->width
;
3496 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
3498 log_assert(wire
!= nullptr);
3500 this->width
= width
;
3501 this->offset
= offset
;
3504 RTLIL::SigChunk::SigChunk(const std::string
&str
)
3507 data
= RTLIL::Const(str
).bits
;
3508 width
= GetSize(data
);
3512 RTLIL::SigChunk::SigChunk(int val
, int width
)
3515 data
= RTLIL::Const(val
, width
).bits
;
3516 this->width
= GetSize(data
);
3520 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
3523 data
= RTLIL::Const(bit
, width
).bits
;
3524 this->width
= GetSize(data
);
3528 RTLIL::SigChunk::SigChunk(const RTLIL::SigBit
&bit
)
3533 data
= RTLIL::Const(bit
.data
).bits
;
3535 offset
= bit
.offset
;
3539 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
)
3544 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
3546 RTLIL::SigChunk ret
;
3549 ret
.offset
= this->offset
+ offset
;
3552 for (int i
= 0; i
< length
; i
++)
3553 ret
.data
.push_back(data
[offset
+i
]);
3559 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
3561 if (wire
&& other
.wire
)
3562 if (wire
->name
!= other
.wire
->name
)
3563 return wire
->name
< other
.wire
->name
;
3565 if (wire
!= other
.wire
)
3566 return wire
< other
.wire
;
3568 if (offset
!= other
.offset
)
3569 return offset
< other
.offset
;
3571 if (width
!= other
.width
)
3572 return width
< other
.width
;
3574 return data
< other
.data
;
3577 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
3579 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
3582 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
3589 RTLIL::SigSpec::SigSpec()
3595 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
3600 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
3602 cover("kernel.rtlil.sigspec.init.list");
3607 log_assert(parts
.size() > 0);
3608 auto ie
= parts
.begin();
3609 auto it
= ie
+ parts
.size() - 1;
3614 RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
3616 cover("kernel.rtlil.sigspec.assign");
3618 width_
= other
.width_
;
3619 hash_
= other
.hash_
;
3620 chunks_
= other
.chunks_
;
3621 bits_
= other
.bits_
;
3625 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
3627 cover("kernel.rtlil.sigspec.init.const");
3629 if (GetSize(value
) != 0) {
3630 chunks_
.emplace_back(value
);
3631 width_
= chunks_
.back().width
;
3639 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
3641 cover("kernel.rtlil.sigspec.init.chunk");
3643 if (chunk
.width
!= 0) {
3644 chunks_
.emplace_back(chunk
);
3645 width_
= chunks_
.back().width
;
3653 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
3655 cover("kernel.rtlil.sigspec.init.wire");
3657 if (wire
->width
!= 0) {
3658 chunks_
.emplace_back(wire
);
3659 width_
= chunks_
.back().width
;
3667 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
3669 cover("kernel.rtlil.sigspec.init.wire_part");
3672 chunks_
.emplace_back(wire
, offset
, width
);
3673 width_
= chunks_
.back().width
;
3681 RTLIL::SigSpec::SigSpec(const std::string
&str
)
3683 cover("kernel.rtlil.sigspec.init.str");
3685 if (str
.size() != 0) {
3686 chunks_
.emplace_back(str
);
3687 width_
= chunks_
.back().width
;
3695 RTLIL::SigSpec::SigSpec(int val
, int width
)
3697 cover("kernel.rtlil.sigspec.init.int");
3700 chunks_
.emplace_back(val
, width
);
3706 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
3708 cover("kernel.rtlil.sigspec.init.state");
3711 chunks_
.emplace_back(bit
, width
);
3717 RTLIL::SigSpec::SigSpec(const RTLIL::SigBit
&bit
, int width
)
3719 cover("kernel.rtlil.sigspec.init.bit");
3722 if (bit
.wire
== NULL
)
3723 chunks_
.emplace_back(bit
.data
, width
);
3725 for (int i
= 0; i
< width
; i
++)
3726 chunks_
.push_back(bit
);
3733 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigChunk
> &chunks
)
3735 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
3739 for (const auto &c
: chunks
)
3744 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigBit
> &bits
)
3746 cover("kernel.rtlil.sigspec.init.stdvec_bits");
3750 for (const auto &bit
: bits
)
3755 RTLIL::SigSpec::SigSpec(const pool
<RTLIL::SigBit
> &bits
)
3757 cover("kernel.rtlil.sigspec.init.pool_bits");
3761 for (const auto &bit
: bits
)
3766 RTLIL::SigSpec::SigSpec(const std::set
<RTLIL::SigBit
> &bits
)
3768 cover("kernel.rtlil.sigspec.init.stdset_bits");
3772 for (const auto &bit
: bits
)
3777 RTLIL::SigSpec::SigSpec(bool bit
)
3779 cover("kernel.rtlil.sigspec.init.bool");
3783 append(SigBit(bit
));
3787 void RTLIL::SigSpec::pack() const
3789 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3791 if (that
->bits_
.empty())
3794 cover("kernel.rtlil.sigspec.convert.pack");
3795 log_assert(that
->chunks_
.empty());
3797 std::vector
<RTLIL::SigBit
> old_bits
;
3798 old_bits
.swap(that
->bits_
);
3800 RTLIL::SigChunk
*last
= NULL
;
3801 int last_end_offset
= 0;
3803 for (auto &bit
: old_bits
) {
3804 if (last
&& bit
.wire
== last
->wire
) {
3805 if (bit
.wire
== NULL
) {
3806 last
->data
.push_back(bit
.data
);
3809 } else if (last_end_offset
== bit
.offset
) {
3815 that
->chunks_
.push_back(bit
);
3816 last
= &that
->chunks_
.back();
3817 last_end_offset
= bit
.offset
+ 1;
3823 void RTLIL::SigSpec::unpack() const
3825 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3827 if (that
->chunks_
.empty())
3830 cover("kernel.rtlil.sigspec.convert.unpack");
3831 log_assert(that
->bits_
.empty());
3833 that
->bits_
.reserve(that
->width_
);
3834 for (auto &c
: that
->chunks_
)
3835 for (int i
= 0; i
< c
.width
; i
++)
3836 that
->bits_
.emplace_back(c
, i
);
3838 that
->chunks_
.clear();
3842 void RTLIL::SigSpec::updhash() const
3844 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3846 if (that
->hash_
!= 0)
3849 cover("kernel.rtlil.sigspec.hash");
3852 that
->hash_
= mkhash_init
;
3853 for (auto &c
: that
->chunks_
)
3854 if (c
.wire
== NULL
) {
3855 for (auto &v
: c
.data
)
3856 that
->hash_
= mkhash(that
->hash_
, v
);
3858 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3859 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3860 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3863 if (that
->hash_
== 0)
3867 void RTLIL::SigSpec::sort()
3870 cover("kernel.rtlil.sigspec.sort");
3871 std::sort(bits_
.begin(), bits_
.end());
3874 void RTLIL::SigSpec::sort_and_unify()
3877 cover("kernel.rtlil.sigspec.sort_and_unify");
3879 // A copy of the bits vector is used to prevent duplicating the logic from
3880 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3881 // that isn't showing up as significant in profiles.
3882 std::vector
<SigBit
> unique_bits
= bits_
;
3883 std::sort(unique_bits
.begin(), unique_bits
.end());
3884 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3885 unique_bits
.erase(last
, unique_bits
.end());
3887 *this = unique_bits
;
3890 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3892 replace(pattern
, with
, this);
3895 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3897 log_assert(other
!= NULL
);
3898 log_assert(width_
== other
->width_
);
3899 log_assert(pattern
.width_
== with
.width_
);
3906 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3907 if (pattern
.bits_
[i
].wire
!= NULL
) {
3908 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3909 if (bits_
[j
] == pattern
.bits_
[i
]) {
3910 other
->bits_
[j
] = with
.bits_
[i
];
3919 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3921 replace(rules
, this);
3924 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3926 cover("kernel.rtlil.sigspec.replace_dict");
3928 log_assert(other
!= NULL
);
3929 log_assert(width_
== other
->width_
);
3931 if (rules
.empty()) return;
3935 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3936 auto it
= rules
.find(bits_
[i
]);
3937 if (it
!= rules
.end())
3938 other
->bits_
[i
] = it
->second
;
3944 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3946 replace(rules
, this);
3949 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3951 cover("kernel.rtlil.sigspec.replace_map");
3953 log_assert(other
!= NULL
);
3954 log_assert(width_
== other
->width_
);
3956 if (rules
.empty()) return;
3960 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3961 auto it
= rules
.find(bits_
[i
]);
3962 if (it
!= rules
.end())
3963 other
->bits_
[i
] = it
->second
;
3969 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3971 remove2(pattern
, NULL
);
3974 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3976 RTLIL::SigSpec tmp
= *this;
3977 tmp
.remove2(pattern
, other
);
3980 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3983 cover("kernel.rtlil.sigspec.remove_other");
3985 cover("kernel.rtlil.sigspec.remove");
3988 if (other
!= NULL
) {
3989 log_assert(width_
== other
->width_
);
3993 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3995 if (bits_
[i
].wire
== NULL
) continue;
3997 for (auto &pattern_chunk
: pattern
.chunks())
3998 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3999 bits_
[i
].offset
>= pattern_chunk
.offset
&&
4000 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
4001 bits_
.erase(bits_
.begin() + i
);
4003 if (other
!= NULL
) {
4004 other
->bits_
.erase(other
->bits_
.begin() + i
);
4014 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
4016 remove2(pattern
, NULL
);
4019 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
4021 RTLIL::SigSpec tmp
= *this;
4022 tmp
.remove2(pattern
, other
);
4025 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
4028 cover("kernel.rtlil.sigspec.remove_other");
4030 cover("kernel.rtlil.sigspec.remove");
4034 if (other
!= NULL
) {
4035 log_assert(width_
== other
->width_
);
4039 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
4040 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
4041 bits_
.erase(bits_
.begin() + i
);
4043 if (other
!= NULL
) {
4044 other
->bits_
.erase(other
->bits_
.begin() + i
);
4053 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
4056 cover("kernel.rtlil.sigspec.remove_other");
4058 cover("kernel.rtlil.sigspec.remove");
4062 if (other
!= NULL
) {
4063 log_assert(width_
== other
->width_
);
4067 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
4068 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
4069 bits_
.erase(bits_
.begin() + i
);
4071 if (other
!= NULL
) {
4072 other
->bits_
.erase(other
->bits_
.begin() + i
);
4081 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
4084 cover("kernel.rtlil.sigspec.extract_other");
4086 cover("kernel.rtlil.sigspec.extract");
4088 log_assert(other
== NULL
|| width_
== other
->width_
);
4091 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
4093 for (auto& pattern_chunk
: pattern
.chunks()) {
4095 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
4096 for (int i
= 0; i
< width_
; i
++)
4097 if (bits_match
[i
].wire
&&
4098 bits_match
[i
].wire
== pattern_chunk
.wire
&&
4099 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
4100 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
4101 ret
.append(bits_other
[i
]);
4103 for (int i
= 0; i
< width_
; i
++)
4104 if (bits_match
[i
].wire
&&
4105 bits_match
[i
].wire
== pattern_chunk
.wire
&&
4106 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
4107 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
4108 ret
.append(bits_match
[i
]);
4116 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
4119 cover("kernel.rtlil.sigspec.extract_other");
4121 cover("kernel.rtlil.sigspec.extract");
4123 log_assert(other
== NULL
|| width_
== other
->width_
);
4125 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
4129 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
4130 for (int i
= 0; i
< width_
; i
++)
4131 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
4132 ret
.append(bits_other
[i
]);
4134 for (int i
= 0; i
< width_
; i
++)
4135 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
4136 ret
.append(bits_match
[i
]);
4143 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
4145 cover("kernel.rtlil.sigspec.replace_pos");
4150 log_assert(offset
>= 0);
4151 log_assert(with
.width_
>= 0);
4152 log_assert(offset
+with
.width_
<= width_
);
4154 for (int i
= 0; i
< with
.width_
; i
++)
4155 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
4160 void RTLIL::SigSpec::remove_const()
4164 cover("kernel.rtlil.sigspec.remove_const.packed");
4166 std::vector
<RTLIL::SigChunk
> new_chunks
;
4167 new_chunks
.reserve(GetSize(chunks_
));
4170 for (auto &chunk
: chunks_
)
4171 if (chunk
.wire
!= NULL
) {
4172 if (!new_chunks
.empty() &&
4173 new_chunks
.back().wire
== chunk
.wire
&&
4174 new_chunks
.back().offset
+ new_chunks
.back().width
== chunk
.offset
) {
4175 new_chunks
.back().width
+= chunk
.width
;
4177 new_chunks
.push_back(chunk
);
4179 width_
+= chunk
.width
;
4182 chunks_
.swap(new_chunks
);
4186 cover("kernel.rtlil.sigspec.remove_const.unpacked");
4188 std::vector
<RTLIL::SigBit
> new_bits
;
4189 new_bits
.reserve(width_
);
4191 for (auto &bit
: bits_
)
4192 if (bit
.wire
!= NULL
)
4193 new_bits
.push_back(bit
);
4195 bits_
.swap(new_bits
);
4196 width_
= bits_
.size();
4202 void RTLIL::SigSpec::remove(int offset
, int length
)
4204 cover("kernel.rtlil.sigspec.remove_pos");
4208 log_assert(offset
>= 0);
4209 log_assert(length
>= 0);
4210 log_assert(offset
+ length
<= width_
);
4212 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
4213 width_
= bits_
.size();
4218 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
4221 cover("kernel.rtlil.sigspec.extract_pos");
4222 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
4225 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
4227 if (signal
.width_
== 0)
4235 cover("kernel.rtlil.sigspec.append");
4237 if (packed() != signal
.packed()) {
4243 for (auto &other_c
: signal
.chunks_
)
4245 auto &my_last_c
= chunks_
.back();
4246 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
4247 auto &this_data
= my_last_c
.data
;
4248 auto &other_data
= other_c
.data
;
4249 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
4250 my_last_c
.width
+= other_c
.width
;
4252 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
4253 my_last_c
.width
+= other_c
.width
;
4255 chunks_
.push_back(other_c
);
4258 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
4260 width_
+= signal
.width_
;
4264 void RTLIL::SigSpec::append(const RTLIL::SigBit
&bit
)
4268 cover("kernel.rtlil.sigspec.append_bit.packed");
4270 if (chunks_
.size() == 0)
4271 chunks_
.push_back(bit
);
4273 if (bit
.wire
== NULL
)
4274 if (chunks_
.back().wire
== NULL
) {
4275 chunks_
.back().data
.push_back(bit
.data
);
4276 chunks_
.back().width
++;
4278 chunks_
.push_back(bit
);
4280 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
4281 chunks_
.back().width
++;
4283 chunks_
.push_back(bit
);
4287 cover("kernel.rtlil.sigspec.append_bit.unpacked");
4288 bits_
.push_back(bit
);
4295 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
4297 cover("kernel.rtlil.sigspec.extend_u0");
4302 remove(width
, width_
- width
);
4304 if (width_
< width
) {
4305 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
4307 padding
= RTLIL::State::S0
;
4308 while (width_
< width
)
4314 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
4316 cover("kernel.rtlil.sigspec.repeat");
4319 for (int i
= 0; i
< num
; i
++)
4325 void RTLIL::SigSpec::check(Module
*mod
) const
4329 cover("kernel.rtlil.sigspec.check.skip");
4333 cover("kernel.rtlil.sigspec.check.packed");
4336 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
4337 const RTLIL::SigChunk
&chunk
= chunks_
[i
];
4338 log_assert(chunk
.width
!= 0);
4339 if (chunk
.wire
== NULL
) {
4341 log_assert(chunks_
[i
-1].wire
!= NULL
);
4342 log_assert(chunk
.offset
== 0);
4343 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
4345 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
4346 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
4347 log_assert(chunk
.offset
>= 0);
4348 log_assert(chunk
.width
>= 0);
4349 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
4350 log_assert(chunk
.data
.size() == 0);
4352 log_assert(chunk
.wire
->module
== mod
);
4356 log_assert(w
== width_
);
4357 log_assert(bits_
.empty());
4361 cover("kernel.rtlil.sigspec.check.unpacked");
4363 if (mod
!= nullptr) {
4364 for (size_t i
= 0; i
< bits_
.size(); i
++)
4365 if (bits_
[i
].wire
!= nullptr)
4366 log_assert(bits_
[i
].wire
->module
== mod
);
4369 log_assert(width_
== GetSize(bits_
));
4370 log_assert(chunks_
.empty());
4375 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
4377 cover("kernel.rtlil.sigspec.comp_lt");
4382 if (width_
!= other
.width_
)
4383 return width_
< other
.width_
;
4388 if (chunks_
.size() != other
.chunks_
.size())
4389 return chunks_
.size() < other
.chunks_
.size();
4394 if (hash_
!= other
.hash_
)
4395 return hash_
< other
.hash_
;
4397 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4398 if (chunks_
[i
] != other
.chunks_
[i
]) {
4399 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
4400 return chunks_
[i
] < other
.chunks_
[i
];
4403 cover("kernel.rtlil.sigspec.comp_lt.equal");
4407 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
4409 cover("kernel.rtlil.sigspec.comp_eq");
4414 if (width_
!= other
.width_
)
4417 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
4418 // since the RHS will contain one SigChunk of width 0 causing
4419 // the size check below to fail
4426 if (chunks_
.size() != other
.chunks_
.size())
4432 if (hash_
!= other
.hash_
)
4435 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4436 if (chunks_
[i
] != other
.chunks_
[i
]) {
4437 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
4441 cover("kernel.rtlil.sigspec.comp_eq.equal");
4445 bool RTLIL::SigSpec::is_wire() const
4447 cover("kernel.rtlil.sigspec.is_wire");
4450 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
4453 bool RTLIL::SigSpec::is_chunk() const
4455 cover("kernel.rtlil.sigspec.is_chunk");
4458 return GetSize(chunks_
) == 1;
4461 bool RTLIL::SigSpec::is_fully_const() const
4463 cover("kernel.rtlil.sigspec.is_fully_const");
4466 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4467 if (it
->width
> 0 && it
->wire
!= NULL
)
4472 bool RTLIL::SigSpec::is_fully_zero() const
4474 cover("kernel.rtlil.sigspec.is_fully_zero");
4477 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4478 if (it
->width
> 0 && it
->wire
!= NULL
)
4480 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4481 if (it
->data
[i
] != RTLIL::State::S0
)
4487 bool RTLIL::SigSpec::is_fully_ones() const
4489 cover("kernel.rtlil.sigspec.is_fully_ones");
4492 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4493 if (it
->width
> 0 && it
->wire
!= NULL
)
4495 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4496 if (it
->data
[i
] != RTLIL::State::S1
)
4502 bool RTLIL::SigSpec::is_fully_def() const
4504 cover("kernel.rtlil.sigspec.is_fully_def");
4507 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4508 if (it
->width
> 0 && it
->wire
!= NULL
)
4510 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4511 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
4517 bool RTLIL::SigSpec::is_fully_undef() const
4519 cover("kernel.rtlil.sigspec.is_fully_undef");
4522 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4523 if (it
->width
> 0 && it
->wire
!= NULL
)
4525 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4526 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
4532 bool RTLIL::SigSpec::has_const() const
4534 cover("kernel.rtlil.sigspec.has_const");
4537 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4538 if (it
->width
> 0 && it
->wire
== NULL
)
4543 bool RTLIL::SigSpec::has_marked_bits() const
4545 cover("kernel.rtlil.sigspec.has_marked_bits");
4548 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4549 if (it
->width
> 0 && it
->wire
== NULL
) {
4550 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4551 if (it
->data
[i
] == RTLIL::State::Sm
)
4557 bool RTLIL::SigSpec::is_onehot(int *pos
) const
4559 cover("kernel.rtlil.sigspec.is_onehot");
4562 if (!is_fully_const())
4564 log_assert(GetSize(chunks_
) <= 1);
4566 return RTLIL::Const(chunks_
[0].data
).is_onehot(pos
);
4570 bool RTLIL::SigSpec::as_bool() const
4572 cover("kernel.rtlil.sigspec.as_bool");
4575 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4577 return RTLIL::Const(chunks_
[0].data
).as_bool();
4581 int RTLIL::SigSpec::as_int(bool is_signed
) const
4583 cover("kernel.rtlil.sigspec.as_int");
4586 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4588 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
4592 std::string
RTLIL::SigSpec::as_string() const
4594 cover("kernel.rtlil.sigspec.as_string");
4598 str
.reserve(size());
4599 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
4600 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
4601 if (chunk
.wire
!= NULL
)
4602 str
.append(chunk
.width
, '?');
4604 str
+= RTLIL::Const(chunk
.data
).as_string();
4609 RTLIL::Const
RTLIL::SigSpec::as_const() const
4611 cover("kernel.rtlil.sigspec.as_const");
4614 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4616 return chunks_
[0].data
;
4617 return RTLIL::Const();
4620 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
4622 cover("kernel.rtlil.sigspec.as_wire");
4625 log_assert(is_wire());
4626 return chunks_
[0].wire
;
4629 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
4631 cover("kernel.rtlil.sigspec.as_chunk");
4634 log_assert(is_chunk());
4638 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
4640 cover("kernel.rtlil.sigspec.as_bit");
4642 log_assert(width_
== 1);
4644 return RTLIL::SigBit(*chunks_
.begin());
4649 bool RTLIL::SigSpec::match(const char* pattern
) const
4651 cover("kernel.rtlil.sigspec.match");
4654 log_assert(int(strlen(pattern
)) == GetSize(bits_
));
4656 for (auto it
= bits_
.rbegin(); it
!= bits_
.rend(); it
++, pattern
++) {
4657 if (*pattern
== ' ')
4659 if (*pattern
== '*') {
4660 if (*it
!= State::Sz
&& *it
!= State::Sx
)
4664 if (*pattern
== '0') {
4665 if (*it
!= State::S0
)
4668 if (*pattern
== '1') {
4669 if (*it
!= State::S1
)
4678 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
4680 cover("kernel.rtlil.sigspec.to_sigbit_set");
4683 std::set
<RTLIL::SigBit
> sigbits
;
4684 for (auto &c
: chunks_
)
4685 for (int i
= 0; i
< c
.width
; i
++)
4686 sigbits
.insert(RTLIL::SigBit(c
, i
));
4690 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
4692 cover("kernel.rtlil.sigspec.to_sigbit_pool");
4695 pool
<RTLIL::SigBit
> sigbits
;
4696 sigbits
.reserve(size());
4697 for (auto &c
: chunks_
)
4698 for (int i
= 0; i
< c
.width
; i
++)
4699 sigbits
.insert(RTLIL::SigBit(c
, i
));
4703 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
4705 cover("kernel.rtlil.sigspec.to_sigbit_vector");
4711 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
4713 cover("kernel.rtlil.sigspec.to_sigbit_map");
4718 log_assert(width_
== other
.width_
);
4720 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4721 for (int i
= 0; i
< width_
; i
++)
4722 new_map
[bits_
[i
]] = other
.bits_
[i
];
4727 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
4729 cover("kernel.rtlil.sigspec.to_sigbit_dict");
4734 log_assert(width_
== other
.width_
);
4736 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4737 new_map
.reserve(size());
4738 for (int i
= 0; i
< width_
; i
++)
4739 new_map
[bits_
[i
]] = other
.bits_
[i
];
4744 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
4746 size_t start
= 0, end
= 0;
4747 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
4748 tokens
.push_back(text
.substr(start
, end
- start
));
4751 tokens
.push_back(text
.substr(start
));
4754 static int sigspec_parse_get_dummy_line_num()
4759 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4761 cover("kernel.rtlil.sigspec.parse");
4763 AST::current_filename
= "input";
4765 std::vector
<std::string
> tokens
;
4766 sigspec_parse_split(tokens
, str
, ',');
4768 sig
= RTLIL::SigSpec();
4769 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
4771 std::string netname
= tokens
[tokidx
];
4772 std::string indices
;
4774 if (netname
.size() == 0)
4777 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
4778 cover("kernel.rtlil.sigspec.parse.const");
4779 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
4780 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
4783 sig
.append(RTLIL::Const(ast
->bits
));
4791 cover("kernel.rtlil.sigspec.parse.net");
4793 if (netname
[0] != '$' && netname
[0] != '\\')
4794 netname
= "\\" + netname
;
4796 if (module
->wires_
.count(netname
) == 0) {
4797 size_t indices_pos
= netname
.size()-1;
4798 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
4801 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4802 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
4804 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4806 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
4807 indices
= netname
.substr(indices_pos
);
4808 netname
= netname
.substr(0, indices_pos
);
4813 if (module
->wires_
.count(netname
) == 0)
4816 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
4817 if (!indices
.empty()) {
4818 std::vector
<std::string
> index_tokens
;
4819 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
4820 if (index_tokens
.size() == 1) {
4821 cover("kernel.rtlil.sigspec.parse.bit_sel");
4822 int a
= atoi(index_tokens
.at(0).c_str());
4823 if (a
< 0 || a
>= wire
->width
)
4825 sig
.append(RTLIL::SigSpec(wire
, a
));
4827 cover("kernel.rtlil.sigspec.parse.part_sel");
4828 int a
= atoi(index_tokens
.at(0).c_str());
4829 int b
= atoi(index_tokens
.at(1).c_str());
4834 if (a
< 0 || a
>= wire
->width
)
4836 if (b
< 0 || b
>= wire
->width
)
4838 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4847 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4849 if (str
.empty() || str
[0] != '@')
4850 return parse(sig
, module
, str
);
4852 cover("kernel.rtlil.sigspec.parse.sel");
4854 str
= RTLIL::escape_id(str
.substr(1));
4855 if (design
->selection_vars
.count(str
) == 0)
4858 sig
= RTLIL::SigSpec();
4859 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4860 for (auto &it
: module
->wires_
)
4861 if (sel
.selected_member(module
->name
, it
.first
))
4862 sig
.append(it
.second
);
4867 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4870 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4871 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4876 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4877 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4881 if (lhs
.chunks_
.size() == 1) {
4882 char *p
= (char*)str
.c_str(), *endptr
;
4883 long int val
= strtol(p
, &endptr
, 10);
4884 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4885 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4886 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4891 return parse(sig
, module
, str
);
4894 RTLIL::CaseRule::~CaseRule()
4896 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4900 bool RTLIL::CaseRule::empty() const
4902 return actions
.empty() && switches
.empty();
4905 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4907 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4908 new_caserule
->compare
= compare
;
4909 new_caserule
->actions
= actions
;
4910 for (auto &it
: switches
)
4911 new_caserule
->switches
.push_back(it
->clone());
4912 return new_caserule
;
4915 RTLIL::SwitchRule::~SwitchRule()
4917 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4921 bool RTLIL::SwitchRule::empty() const
4923 return cases
.empty();
4926 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4928 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4929 new_switchrule
->signal
= signal
;
4930 new_switchrule
->attributes
= attributes
;
4931 for (auto &it
: cases
)
4932 new_switchrule
->cases
.push_back(it
->clone());
4933 return new_switchrule
;
4937 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4939 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4940 new_syncrule
->type
= type
;
4941 new_syncrule
->signal
= signal
;
4942 new_syncrule
->actions
= actions
;
4943 new_syncrule
->mem_write_actions
= mem_write_actions
;
4944 return new_syncrule
;
4947 RTLIL::Process::~Process()
4949 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4953 RTLIL::Process
*RTLIL::Process::clone() const
4955 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4957 new_proc
->name
= name
;
4958 new_proc
->attributes
= attributes
;
4960 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4961 new_proc
->root_case
= *rc_ptr
;
4962 rc_ptr
->switches
.clear();
4965 for (auto &it
: syncs
)
4966 new_proc
->syncs
.push_back(it
->clone());
4972 RTLIL::Memory::~Memory()
4974 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4976 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4977 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4979 return &all_memorys
;