2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/macc.h"
22 #include "kernel/celltypes.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
31 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
32 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
33 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
34 #ifndef YOSYS_NO_IDS_REFCNT
35 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
36 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
38 #ifdef YOSYS_USE_STICKY_IDS
39 int RTLIL::IdString::last_created_idx_
[8];
40 int RTLIL::IdString::last_created_idx_ptr_
;
43 IdString
RTLIL::ID::A
;
44 IdString
RTLIL::ID::B
;
45 IdString
RTLIL::ID::Y
;
46 IdString
RTLIL::ID::keep
;
47 IdString
RTLIL::ID::whitebox
;
48 IdString
RTLIL::ID::blackbox
;
52 flags
= RTLIL::CONST_FLAG_NONE
;
55 RTLIL::Const::Const(std::string str
)
57 flags
= RTLIL::CONST_FLAG_STRING
;
58 for (int i
= str
.size()-1; i
>= 0; i
--) {
59 unsigned char ch
= str
[i
];
60 for (int j
= 0; j
< 8; j
++) {
61 bits
.push_back((ch
& 1) != 0 ? State::S1
: State::S0
);
67 RTLIL::Const::Const(int val
, int width
)
69 flags
= RTLIL::CONST_FLAG_NONE
;
70 for (int i
= 0; i
< width
; i
++) {
71 bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
76 RTLIL::Const::Const(RTLIL::State bit
, int width
)
78 flags
= RTLIL::CONST_FLAG_NONE
;
79 for (int i
= 0; i
< width
; i
++)
83 RTLIL::Const::Const(const std::vector
<bool> &bits
)
85 flags
= RTLIL::CONST_FLAG_NONE
;
87 this->bits
.push_back(b
? State::S1
: State::S0
);
90 RTLIL::Const::Const(const RTLIL::Const
&c
)
94 this->bits
.push_back(b
);
97 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
99 if (bits
.size() != other
.bits
.size())
100 return bits
.size() < other
.bits
.size();
101 for (size_t i
= 0; i
< bits
.size(); i
++)
102 if (bits
[i
] != other
.bits
[i
])
103 return bits
[i
] < other
.bits
[i
];
107 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
109 return bits
== other
.bits
;
112 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
114 return bits
!= other
.bits
;
117 bool RTLIL::Const::as_bool() const
119 for (size_t i
= 0; i
< bits
.size(); i
++)
120 if (bits
[i
] == State::S1
)
125 int RTLIL::Const::as_int(bool is_signed
) const
128 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
129 if (bits
[i
] == State::S1
)
131 if (is_signed
&& bits
.back() == State::S1
)
132 for (size_t i
= bits
.size(); i
< 32; i
++)
137 std::string
RTLIL::Const::as_string() const
140 for (size_t i
= bits
.size(); i
> 0; i
--)
142 case S0
: ret
+= "0"; break;
143 case S1
: ret
+= "1"; break;
144 case Sx
: ret
+= "x"; break;
145 case Sz
: ret
+= "z"; break;
146 case Sa
: ret
+= "-"; break;
147 case Sm
: ret
+= "m"; break;
152 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
155 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
157 case '0': c
.bits
.push_back(State::S0
); break;
158 case '1': c
.bits
.push_back(State::S1
); break;
159 case 'x': c
.bits
.push_back(State::Sx
); break;
160 case 'z': c
.bits
.push_back(State::Sz
); break;
161 case 'm': c
.bits
.push_back(State::Sm
); break;
162 default: c
.bits
.push_back(State::Sa
);
167 std::string
RTLIL::Const::decode_string() const
170 std::vector
<char> string_chars
;
171 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
173 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
174 if (bits
[i
+ j
] == RTLIL::State::S1
)
177 string_chars
.push_back(ch
);
179 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
180 string
+= string_chars
[i
];
184 bool RTLIL::Const::is_fully_zero() const
186 cover("kernel.rtlil.const.is_fully_zero");
188 for (auto bit
: bits
)
189 if (bit
!= RTLIL::State::S0
)
195 bool RTLIL::Const::is_fully_ones() const
197 cover("kernel.rtlil.const.is_fully_ones");
199 for (auto bit
: bits
)
200 if (bit
!= RTLIL::State::S1
)
206 bool RTLIL::Const::is_fully_def() const
208 cover("kernel.rtlil.const.is_fully_def");
210 for (auto bit
: bits
)
211 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
217 bool RTLIL::Const::is_fully_undef() const
219 cover("kernel.rtlil.const.is_fully_undef");
221 for (auto bit
: bits
)
222 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
228 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
231 attributes
[id
] = RTLIL::Const(1);
233 const auto it
= attributes
.find(id
);
234 if (it
!= attributes
.end())
235 attributes
.erase(it
);
239 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
241 const auto it
= attributes
.find(id
);
242 if (it
== attributes
.end())
244 return it
->second
.as_bool();
247 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
250 for (auto &s
: data
) {
251 if (!attrval
.empty())
255 attributes
[id
] = RTLIL::Const(attrval
);
258 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
260 pool
<string
> union_data
= get_strpool_attribute(id
);
261 union_data
.insert(data
.begin(), data
.end());
262 if (!union_data
.empty())
263 set_strpool_attribute(id
, union_data
);
266 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
269 if (attributes
.count(id
) != 0)
270 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
275 void RTLIL::AttrObject::set_src_attribute(const std::string
&src
)
278 attributes
.erase(ID(src
));
280 attributes
[ID(src
)] = src
;
283 std::string
RTLIL::AttrObject::get_src_attribute() const
286 if (attributes
.count(ID(src
)))
287 src
= attributes
.at(ID(src
)).decode_string();
291 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
295 if (selected_modules
.count(mod_name
) > 0)
297 if (selected_members
.count(mod_name
) > 0)
302 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
306 if (selected_modules
.count(mod_name
) > 0)
311 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
315 if (selected_modules
.count(mod_name
) > 0)
317 if (selected_members
.count(mod_name
) > 0)
318 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
323 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
325 if (full_selection
) {
326 selected_modules
.clear();
327 selected_members
.clear();
331 std::vector
<RTLIL::IdString
> del_list
, add_list
;
334 for (auto mod_name
: selected_modules
) {
335 if (design
->modules_
.count(mod_name
) == 0)
336 del_list
.push_back(mod_name
);
337 selected_members
.erase(mod_name
);
339 for (auto mod_name
: del_list
)
340 selected_modules
.erase(mod_name
);
343 for (auto &it
: selected_members
)
344 if (design
->modules_
.count(it
.first
) == 0)
345 del_list
.push_back(it
.first
);
346 for (auto mod_name
: del_list
)
347 selected_members
.erase(mod_name
);
349 for (auto &it
: selected_members
) {
351 for (auto memb_name
: it
.second
)
352 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
353 del_list
.push_back(memb_name
);
354 for (auto memb_name
: del_list
)
355 it
.second
.erase(memb_name
);
360 for (auto &it
: selected_members
)
361 if (it
.second
.size() == 0)
362 del_list
.push_back(it
.first
);
363 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
364 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
365 add_list
.push_back(it
.first
);
366 for (auto mod_name
: del_list
)
367 selected_members
.erase(mod_name
);
368 for (auto mod_name
: add_list
) {
369 selected_members
.erase(mod_name
);
370 selected_modules
.insert(mod_name
);
373 if (selected_modules
.size() == design
->modules_
.size()) {
374 full_selection
= true;
375 selected_modules
.clear();
376 selected_members
.clear();
380 RTLIL::Design::Design()
382 static unsigned int hashidx_count
= 123456789;
383 hashidx_count
= mkhash_xorshift(hashidx_count
);
384 hashidx_
= hashidx_count
;
386 refcount_modules_
= 0;
387 selection_stack
.push_back(RTLIL::Selection());
390 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
394 RTLIL::Design::~Design()
396 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
398 for (auto n
: verilog_packages
)
400 for (auto n
: verilog_globals
)
403 RTLIL::Design::get_all_designs()->erase(hashidx_
);
408 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
409 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
415 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
417 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
420 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
422 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
425 RTLIL::Module
*RTLIL::Design::top_module()
427 RTLIL::Module
*module
= nullptr;
428 int module_count
= 0;
430 for (auto mod
: selected_modules()) {
431 if (mod
->get_bool_attribute(ID(top
)))
437 return module_count
== 1 ? module
: nullptr;
440 void RTLIL::Design::add(RTLIL::Module
*module
)
442 log_assert(modules_
.count(module
->name
) == 0);
443 log_assert(refcount_modules_
== 0);
444 modules_
[module
->name
] = module
;
445 module
->design
= this;
447 for (auto mon
: monitors
)
448 mon
->notify_module_add(module
);
451 log("#X# New Module: %s\n", log_id(module
));
452 log_backtrace("-X- ", yosys_xtrace
-1);
456 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
458 log_assert(modules_
.count(name
) == 0);
459 log_assert(refcount_modules_
== 0);
461 RTLIL::Module
*module
= new RTLIL::Module
;
462 modules_
[name
] = module
;
463 module
->design
= this;
466 for (auto mon
: monitors
)
467 mon
->notify_module_add(module
);
470 log("#X# New Module: %s\n", log_id(module
));
471 log_backtrace("-X- ", yosys_xtrace
-1);
477 void RTLIL::Design::scratchpad_unset(std::string varname
)
479 scratchpad
.erase(varname
);
482 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
484 scratchpad
[varname
] = stringf("%d", value
);
487 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
489 scratchpad
[varname
] = value
? "true" : "false";
492 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
494 scratchpad
[varname
] = value
;
497 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
499 if (scratchpad
.count(varname
) == 0)
500 return default_value
;
502 std::string str
= scratchpad
.at(varname
);
504 if (str
== "0" || str
== "false")
507 if (str
== "1" || str
== "true")
510 char *endptr
= nullptr;
511 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
512 return *endptr
? default_value
: parsed_value
;
515 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
517 if (scratchpad
.count(varname
) == 0)
518 return default_value
;
520 std::string str
= scratchpad
.at(varname
);
522 if (str
== "0" || str
== "false")
525 if (str
== "1" || str
== "true")
528 return default_value
;
531 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
533 if (scratchpad
.count(varname
) == 0)
534 return default_value
;
535 return scratchpad
.at(varname
);
538 void RTLIL::Design::remove(RTLIL::Module
*module
)
540 for (auto mon
: monitors
)
541 mon
->notify_module_del(module
);
544 log("#X# Remove Module: %s\n", log_id(module
));
545 log_backtrace("-X- ", yosys_xtrace
-1);
548 log_assert(modules_
.at(module
->name
) == module
);
549 modules_
.erase(module
->name
);
553 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
555 modules_
.erase(module
->name
);
556 module
->name
= new_name
;
560 void RTLIL::Design::sort()
563 modules_
.sort(sort_by_id_str());
564 for (auto &it
: modules_
)
568 void RTLIL::Design::check()
571 for (auto &it
: modules_
) {
572 log_assert(this == it
.second
->design
);
573 log_assert(it
.first
== it
.second
->name
);
574 log_assert(!it
.first
.empty());
580 void RTLIL::Design::optimize()
582 for (auto &it
: modules_
)
583 it
.second
->optimize();
584 for (auto &it
: selection_stack
)
586 for (auto &it
: selection_vars
)
587 it
.second
.optimize(this);
590 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
592 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
594 if (selection_stack
.size() == 0)
596 return selection_stack
.back().selected_module(mod_name
);
599 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
601 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
603 if (selection_stack
.size() == 0)
605 return selection_stack
.back().selected_whole_module(mod_name
);
608 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
610 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
612 if (selection_stack
.size() == 0)
614 return selection_stack
.back().selected_member(mod_name
, memb_name
);
617 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
619 return selected_module(mod
->name
);
622 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
624 return selected_whole_module(mod
->name
);
627 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
629 std::vector
<RTLIL::Module
*> result
;
630 result
.reserve(modules_
.size());
631 for (auto &it
: modules_
)
632 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
633 result
.push_back(it
.second
);
637 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
639 std::vector
<RTLIL::Module
*> result
;
640 result
.reserve(modules_
.size());
641 for (auto &it
: modules_
)
642 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
643 result
.push_back(it
.second
);
647 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
649 std::vector
<RTLIL::Module
*> result
;
650 result
.reserve(modules_
.size());
651 for (auto &it
: modules_
)
652 if (it
.second
->get_blackbox_attribute())
654 else if (selected_whole_module(it
.first
))
655 result
.push_back(it
.second
);
656 else if (selected_module(it
.first
))
657 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
661 RTLIL::Module::Module()
663 static unsigned int hashidx_count
= 123456789;
664 hashidx_count
= mkhash_xorshift(hashidx_count
);
665 hashidx_
= hashidx_count
;
672 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
676 RTLIL::Module::~Module()
678 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
680 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
682 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
684 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
687 RTLIL::Module::get_all_modules()->erase(hashidx_
);
692 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
693 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
699 void RTLIL::Module::makeblackbox()
701 pool
<RTLIL::Wire
*> delwires
;
703 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
704 if (!it
->second
->port_input
&& !it
->second
->port_output
)
705 delwires
.insert(it
->second
);
707 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
711 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
715 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
720 set_bool_attribute(ID::blackbox
);
723 void RTLIL::Module::reprocess_module(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Module
*>)
725 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
728 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, bool mayfail
)
731 return RTLIL::IdString();
732 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
736 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, dict
<RTLIL::IdString
, RTLIL::Module
*>, dict
<RTLIL::IdString
, RTLIL::IdString
>, bool mayfail
)
739 return RTLIL::IdString();
740 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
743 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
745 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
750 struct InternalCellChecker
752 RTLIL::Module
*module
;
754 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
756 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
758 void error(int linenr
)
760 std::stringstream buf
;
761 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
763 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
764 module
? module
->name
.c_str() : "", module
? "." : "",
765 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
768 int param(RTLIL::IdString name
)
770 if (cell
->parameters
.count(name
) == 0)
772 expected_params
.insert(name
);
773 return cell
->parameters
.at(name
).as_int();
776 int param_bool(RTLIL::IdString name
)
779 if (cell
->parameters
.at(name
).bits
.size() > 32)
781 if (v
!= 0 && v
!= 1)
786 int param_bool(RTLIL::IdString name
, bool expected
)
788 int v
= param_bool(name
);
794 void param_bits(RTLIL::IdString name
, int width
)
797 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
801 void port(RTLIL::IdString name
, int width
)
803 if (!cell
->hasPort(name
))
805 if (cell
->getPort(name
).size() != width
)
807 expected_ports
.insert(name
);
810 void check_expected(bool check_matched_sign
= true)
812 for (auto ¶
: cell
->parameters
)
813 if (expected_params
.count(para
.first
) == 0)
815 for (auto &conn
: cell
->connections())
816 if (expected_ports
.count(conn
.first
) == 0)
819 if (expected_params
.count(ID(A_SIGNED
)) != 0 && expected_params
.count(ID(B_SIGNED
)) && check_matched_sign
) {
820 bool a_is_signed
= param(ID(A_SIGNED
)) != 0;
821 bool b_is_signed
= param(ID(B_SIGNED
)) != 0;
822 if (a_is_signed
!= b_is_signed
)
827 void check_gate(const char *ports
)
829 if (cell
->parameters
.size() != 0)
832 for (const char *p
= ports
; *p
; p
++) {
833 char portname
[3] = { '\\', *p
, 0 };
834 if (!cell
->hasPort(portname
))
836 if (cell
->getPort(portname
).size() != 1)
840 for (auto &conn
: cell
->connections()) {
841 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
843 if (strchr(ports
, conn
.first
[1]) == NULL
)
850 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
851 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
854 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
855 param_bool(ID(A_SIGNED
));
856 port(ID::A
, param(ID(A_WIDTH
)));
857 port(ID::Y
, param(ID(Y_WIDTH
)));
862 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
863 param_bool(ID(A_SIGNED
));
864 param_bool(ID(B_SIGNED
));
865 port(ID::A
, param(ID(A_WIDTH
)));
866 port(ID::B
, param(ID(B_WIDTH
)));
867 port(ID::Y
, param(ID(Y_WIDTH
)));
872 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
873 param_bool(ID(A_SIGNED
));
874 port(ID::A
, param(ID(A_WIDTH
)));
875 port(ID::Y
, param(ID(Y_WIDTH
)));
880 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
881 param_bool(ID(A_SIGNED
));
882 param_bool(ID(B_SIGNED
), /*expected=*/false);
883 port(ID::A
, param(ID(A_WIDTH
)));
884 port(ID::B
, param(ID(B_WIDTH
)));
885 port(ID::Y
, param(ID(Y_WIDTH
)));
886 check_expected(/*check_matched_sign=*/false);
890 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
891 param_bool(ID(A_SIGNED
));
892 param_bool(ID(B_SIGNED
));
893 port(ID::A
, param(ID(A_WIDTH
)));
894 port(ID::B
, param(ID(B_WIDTH
)));
895 port(ID::Y
, param(ID(Y_WIDTH
)));
896 check_expected(/*check_matched_sign=*/false);
900 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
901 param_bool(ID(A_SIGNED
));
902 param_bool(ID(B_SIGNED
));
903 port(ID::A
, param(ID(A_WIDTH
)));
904 port(ID::B
, param(ID(B_WIDTH
)));
905 port(ID::Y
, param(ID(Y_WIDTH
)));
910 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($pow
))) {
911 param_bool(ID(A_SIGNED
));
912 param_bool(ID(B_SIGNED
));
913 port(ID::A
, param(ID(A_WIDTH
)));
914 port(ID::B
, param(ID(B_WIDTH
)));
915 port(ID::Y
, param(ID(Y_WIDTH
)));
916 check_expected(cell
->type
!= ID($pow
));
920 if (cell
->type
== ID($fa
)) {
921 port(ID::A
, param(ID(WIDTH
)));
922 port(ID::B
, param(ID(WIDTH
)));
923 port(ID(C
), param(ID(WIDTH
)));
924 port(ID(X
), param(ID(WIDTH
)));
925 port(ID::Y
, param(ID(WIDTH
)));
930 if (cell
->type
== ID($lcu
)) {
931 port(ID(P
), param(ID(WIDTH
)));
932 port(ID(G
), param(ID(WIDTH
)));
934 port(ID(CO
), param(ID(WIDTH
)));
939 if (cell
->type
== ID($alu
)) {
940 param_bool(ID(A_SIGNED
));
941 param_bool(ID(B_SIGNED
));
942 port(ID::A
, param(ID(A_WIDTH
)));
943 port(ID::B
, param(ID(B_WIDTH
)));
946 port(ID(X
), param(ID(Y_WIDTH
)));
947 port(ID::Y
, param(ID(Y_WIDTH
)));
948 port(ID(CO
), param(ID(Y_WIDTH
)));
953 if (cell
->type
== ID($macc
)) {
955 param(ID(CONFIG_WIDTH
));
956 port(ID::A
, param(ID(A_WIDTH
)));
957 port(ID::B
, param(ID(B_WIDTH
)));
958 port(ID::Y
, param(ID(Y_WIDTH
)));
960 Macc().from_cell(cell
);
964 if (cell
->type
== ID($logic_not
)) {
965 param_bool(ID(A_SIGNED
));
966 port(ID::A
, param(ID(A_WIDTH
)));
967 port(ID::Y
, param(ID(Y_WIDTH
)));
972 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
973 param_bool(ID(A_SIGNED
));
974 param_bool(ID(B_SIGNED
));
975 port(ID::A
, param(ID(A_WIDTH
)));
976 port(ID::B
, param(ID(B_WIDTH
)));
977 port(ID::Y
, param(ID(Y_WIDTH
)));
978 check_expected(/*check_matched_sign=*/false);
982 if (cell
->type
== ID($slice
)) {
984 port(ID::A
, param(ID(A_WIDTH
)));
985 port(ID::Y
, param(ID(Y_WIDTH
)));
986 if (param(ID(OFFSET
)) + param(ID(Y_WIDTH
)) > param(ID(A_WIDTH
)))
992 if (cell
->type
== ID($concat
)) {
993 port(ID::A
, param(ID(A_WIDTH
)));
994 port(ID::B
, param(ID(B_WIDTH
)));
995 port(ID::Y
, param(ID(A_WIDTH
)) + param(ID(B_WIDTH
)));
1000 if (cell
->type
== ID($mux
)) {
1001 port(ID::A
, param(ID(WIDTH
)));
1002 port(ID::B
, param(ID(WIDTH
)));
1004 port(ID::Y
, param(ID(WIDTH
)));
1009 if (cell
->type
== ID($pmux
)) {
1010 port(ID::A
, param(ID(WIDTH
)));
1011 port(ID::B
, param(ID(WIDTH
)) * param(ID(S_WIDTH
)));
1012 port(ID(S
), param(ID(S_WIDTH
)));
1013 port(ID::Y
, param(ID(WIDTH
)));
1018 if (cell
->type
== ID($lut
)) {
1020 port(ID::A
, param(ID(WIDTH
)));
1026 if (cell
->type
== ID($sop
)) {
1029 port(ID::A
, param(ID(WIDTH
)));
1035 if (cell
->type
== ID($sr
)) {
1036 param_bool(ID(SET_POLARITY
));
1037 param_bool(ID(CLR_POLARITY
));
1038 port(ID(SET
), param(ID(WIDTH
)));
1039 port(ID(CLR
), param(ID(WIDTH
)));
1040 port(ID(Q
), param(ID(WIDTH
)));
1045 if (cell
->type
== ID($ff
)) {
1046 port(ID(D
), param(ID(WIDTH
)));
1047 port(ID(Q
), param(ID(WIDTH
)));
1052 if (cell
->type
== ID($dff
)) {
1053 param_bool(ID(CLK_POLARITY
));
1055 port(ID(D
), param(ID(WIDTH
)));
1056 port(ID(Q
), param(ID(WIDTH
)));
1061 if (cell
->type
== ID($dffe
)) {
1062 param_bool(ID(CLK_POLARITY
));
1063 param_bool(ID(EN_POLARITY
));
1066 port(ID(D
), param(ID(WIDTH
)));
1067 port(ID(Q
), param(ID(WIDTH
)));
1072 if (cell
->type
== ID($dffsr
)) {
1073 param_bool(ID(CLK_POLARITY
));
1074 param_bool(ID(SET_POLARITY
));
1075 param_bool(ID(CLR_POLARITY
));
1077 port(ID(SET
), param(ID(WIDTH
)));
1078 port(ID(CLR
), param(ID(WIDTH
)));
1079 port(ID(D
), param(ID(WIDTH
)));
1080 port(ID(Q
), param(ID(WIDTH
)));
1085 if (cell
->type
== ID($adff
)) {
1086 param_bool(ID(CLK_POLARITY
));
1087 param_bool(ID(ARST_POLARITY
));
1088 param_bits(ID(ARST_VALUE
), param(ID(WIDTH
)));
1091 port(ID(D
), param(ID(WIDTH
)));
1092 port(ID(Q
), param(ID(WIDTH
)));
1097 if (cell
->type
== ID($dlatch
)) {
1098 param_bool(ID(EN_POLARITY
));
1100 port(ID(D
), param(ID(WIDTH
)));
1101 port(ID(Q
), param(ID(WIDTH
)));
1106 if (cell
->type
== ID($dlatchsr
)) {
1107 param_bool(ID(EN_POLARITY
));
1108 param_bool(ID(SET_POLARITY
));
1109 param_bool(ID(CLR_POLARITY
));
1111 port(ID(SET
), param(ID(WIDTH
)));
1112 port(ID(CLR
), param(ID(WIDTH
)));
1113 port(ID(D
), param(ID(WIDTH
)));
1114 port(ID(Q
), param(ID(WIDTH
)));
1119 if (cell
->type
== ID($fsm
)) {
1121 param_bool(ID(CLK_POLARITY
));
1122 param_bool(ID(ARST_POLARITY
));
1123 param(ID(STATE_BITS
));
1124 param(ID(STATE_NUM
));
1125 param(ID(STATE_NUM_LOG2
));
1126 param(ID(STATE_RST
));
1127 param_bits(ID(STATE_TABLE
), param(ID(STATE_BITS
)) * param(ID(STATE_NUM
)));
1128 param(ID(TRANS_NUM
));
1129 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
))));
1132 port(ID(CTRL_IN
), param(ID(CTRL_IN_WIDTH
)));
1133 port(ID(CTRL_OUT
), param(ID(CTRL_OUT_WIDTH
)));
1138 if (cell
->type
== ID($memrd
)) {
1140 param_bool(ID(CLK_ENABLE
));
1141 param_bool(ID(CLK_POLARITY
));
1142 param_bool(ID(TRANSPARENT
));
1145 port(ID(ADDR
), param(ID(ABITS
)));
1146 port(ID(DATA
), param(ID(WIDTH
)));
1151 if (cell
->type
== ID($memwr
)) {
1153 param_bool(ID(CLK_ENABLE
));
1154 param_bool(ID(CLK_POLARITY
));
1155 param(ID(PRIORITY
));
1157 port(ID(EN
), param(ID(WIDTH
)));
1158 port(ID(ADDR
), param(ID(ABITS
)));
1159 port(ID(DATA
), param(ID(WIDTH
)));
1164 if (cell
->type
== ID($meminit
)) {
1166 param(ID(PRIORITY
));
1167 port(ID(ADDR
), param(ID(ABITS
)));
1168 port(ID(DATA
), param(ID(WIDTH
)) * param(ID(WORDS
)));
1173 if (cell
->type
== ID($mem
)) {
1178 param_bits(ID(RD_CLK_ENABLE
), max(1, param(ID(RD_PORTS
))));
1179 param_bits(ID(RD_CLK_POLARITY
), max(1, param(ID(RD_PORTS
))));
1180 param_bits(ID(RD_TRANSPARENT
), max(1, param(ID(RD_PORTS
))));
1181 param_bits(ID(WR_CLK_ENABLE
), max(1, param(ID(WR_PORTS
))));
1182 param_bits(ID(WR_CLK_POLARITY
), max(1, param(ID(WR_PORTS
))));
1183 port(ID(RD_CLK
), param(ID(RD_PORTS
)));
1184 port(ID(RD_EN
), param(ID(RD_PORTS
)));
1185 port(ID(RD_ADDR
), param(ID(RD_PORTS
)) * param(ID(ABITS
)));
1186 port(ID(RD_DATA
), param(ID(RD_PORTS
)) * param(ID(WIDTH
)));
1187 port(ID(WR_CLK
), param(ID(WR_PORTS
)));
1188 port(ID(WR_EN
), param(ID(WR_PORTS
)) * param(ID(WIDTH
)));
1189 port(ID(WR_ADDR
), param(ID(WR_PORTS
)) * param(ID(ABITS
)));
1190 port(ID(WR_DATA
), param(ID(WR_PORTS
)) * param(ID(WIDTH
)));
1195 if (cell
->type
== ID($tribuf
)) {
1196 port(ID::A
, param(ID(WIDTH
)));
1197 port(ID::Y
, param(ID(WIDTH
)));
1203 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1210 if (cell
->type
== ID($initstate
)) {
1216 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1217 port(ID::Y
, param(ID(WIDTH
)));
1222 if (cell
->type
== ID($equiv
)) {
1230 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1231 param_bool(ID(FULL
));
1232 param_bool(ID(SRC_DST_PEN
));
1233 param_bool(ID(SRC_DST_POL
));
1234 param(ID(T_RISE_MIN
));
1235 param(ID(T_RISE_TYP
));
1236 param(ID(T_RISE_MAX
));
1237 param(ID(T_FALL_MIN
));
1238 param(ID(T_FALL_TYP
));
1239 param(ID(T_FALL_MAX
));
1241 port(ID(SRC
), param(ID(SRC_WIDTH
)));
1242 port(ID(DST
), param(ID(DST_WIDTH
)));
1243 if (cell
->type
== ID($specify3
)) {
1244 param_bool(ID(EDGE_EN
));
1245 param_bool(ID(EDGE_POL
));
1246 param_bool(ID(DAT_DST_PEN
));
1247 param_bool(ID(DAT_DST_POL
));
1248 port(ID(DAT
), param(ID(DST_WIDTH
)));
1254 if (cell
->type
== ID($specrule
)) {
1256 param_bool(ID(SRC_PEN
));
1257 param_bool(ID(SRC_POL
));
1258 param_bool(ID(DST_PEN
));
1259 param_bool(ID(DST_POL
));
1261 param(ID(T_LIMIT2
));
1262 port(ID(SRC_EN
), 1);
1263 port(ID(DST_EN
), 1);
1264 port(ID(SRC
), param(ID(SRC_WIDTH
)));
1265 port(ID(DST
), param(ID(DST_WIDTH
)));
1270 if (cell
->type
== ID($_BUF_
)) { check_gate("AY"); return; }
1271 if (cell
->type
== ID($_NOT_
)) { check_gate("AY"); return; }
1272 if (cell
->type
== ID($_AND_
)) { check_gate("ABY"); return; }
1273 if (cell
->type
== ID($_NAND_
)) { check_gate("ABY"); return; }
1274 if (cell
->type
== ID($_OR_
)) { check_gate("ABY"); return; }
1275 if (cell
->type
== ID($_NOR_
)) { check_gate("ABY"); return; }
1276 if (cell
->type
== ID($_XOR_
)) { check_gate("ABY"); return; }
1277 if (cell
->type
== ID($_XNOR_
)) { check_gate("ABY"); return; }
1278 if (cell
->type
== ID($_ANDNOT_
)) { check_gate("ABY"); return; }
1279 if (cell
->type
== ID($_ORNOT_
)) { check_gate("ABY"); return; }
1280 if (cell
->type
== ID($_MUX_
)) { check_gate("ABSY"); return; }
1281 if (cell
->type
== ID($_NMUX_
)) { check_gate("ABSY"); return; }
1282 if (cell
->type
== ID($_AOI3_
)) { check_gate("ABCY"); return; }
1283 if (cell
->type
== ID($_OAI3_
)) { check_gate("ABCY"); return; }
1284 if (cell
->type
== ID($_AOI4_
)) { check_gate("ABCDY"); return; }
1285 if (cell
->type
== ID($_OAI4_
)) { check_gate("ABCDY"); return; }
1287 if (cell
->type
== ID($_TBUF_
)) { check_gate("AYE"); return; }
1289 if (cell
->type
== ID($_MUX4_
)) { check_gate("ABCDSTY"); return; }
1290 if (cell
->type
== ID($_MUX8_
)) { check_gate("ABCDEFGHSTUY"); return; }
1291 if (cell
->type
== ID($_MUX16_
)) { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1293 if (cell
->type
== ID($_SR_NN_
)) { check_gate("SRQ"); return; }
1294 if (cell
->type
== ID($_SR_NP_
)) { check_gate("SRQ"); return; }
1295 if (cell
->type
== ID($_SR_PN_
)) { check_gate("SRQ"); return; }
1296 if (cell
->type
== ID($_SR_PP_
)) { check_gate("SRQ"); return; }
1298 if (cell
->type
== ID($_FF_
)) { check_gate("DQ"); return; }
1299 if (cell
->type
== ID($_DFF_N_
)) { check_gate("DQC"); return; }
1300 if (cell
->type
== ID($_DFF_P_
)) { check_gate("DQC"); return; }
1302 if (cell
->type
== ID($_DFFE_NN_
)) { check_gate("DQCE"); return; }
1303 if (cell
->type
== ID($_DFFE_NP_
)) { check_gate("DQCE"); return; }
1304 if (cell
->type
== ID($_DFFE_PN_
)) { check_gate("DQCE"); return; }
1305 if (cell
->type
== ID($_DFFE_PP_
)) { check_gate("DQCE"); return; }
1307 if (cell
->type
== ID($_DFF_NN0_
)) { check_gate("DQCR"); return; }
1308 if (cell
->type
== ID($_DFF_NN1_
)) { check_gate("DQCR"); return; }
1309 if (cell
->type
== ID($_DFF_NP0_
)) { check_gate("DQCR"); return; }
1310 if (cell
->type
== ID($_DFF_NP1_
)) { check_gate("DQCR"); return; }
1311 if (cell
->type
== ID($_DFF_PN0_
)) { check_gate("DQCR"); return; }
1312 if (cell
->type
== ID($_DFF_PN1_
)) { check_gate("DQCR"); return; }
1313 if (cell
->type
== ID($_DFF_PP0_
)) { check_gate("DQCR"); return; }
1314 if (cell
->type
== ID($_DFF_PP1_
)) { check_gate("DQCR"); return; }
1316 if (cell
->type
== ID($_DFFSR_NNN_
)) { check_gate("CSRDQ"); return; }
1317 if (cell
->type
== ID($_DFFSR_NNP_
)) { check_gate("CSRDQ"); return; }
1318 if (cell
->type
== ID($_DFFSR_NPN_
)) { check_gate("CSRDQ"); return; }
1319 if (cell
->type
== ID($_DFFSR_NPP_
)) { check_gate("CSRDQ"); return; }
1320 if (cell
->type
== ID($_DFFSR_PNN_
)) { check_gate("CSRDQ"); return; }
1321 if (cell
->type
== ID($_DFFSR_PNP_
)) { check_gate("CSRDQ"); return; }
1322 if (cell
->type
== ID($_DFFSR_PPN_
)) { check_gate("CSRDQ"); return; }
1323 if (cell
->type
== ID($_DFFSR_PPP_
)) { check_gate("CSRDQ"); return; }
1325 if (cell
->type
== ID($_DLATCH_N_
)) { check_gate("EDQ"); return; }
1326 if (cell
->type
== ID($_DLATCH_P_
)) { check_gate("EDQ"); return; }
1328 if (cell
->type
== ID($_DLATCHSR_NNN_
)) { check_gate("ESRDQ"); return; }
1329 if (cell
->type
== ID($_DLATCHSR_NNP_
)) { check_gate("ESRDQ"); return; }
1330 if (cell
->type
== ID($_DLATCHSR_NPN_
)) { check_gate("ESRDQ"); return; }
1331 if (cell
->type
== ID($_DLATCHSR_NPP_
)) { check_gate("ESRDQ"); return; }
1332 if (cell
->type
== ID($_DLATCHSR_PNN_
)) { check_gate("ESRDQ"); return; }
1333 if (cell
->type
== ID($_DLATCHSR_PNP_
)) { check_gate("ESRDQ"); return; }
1334 if (cell
->type
== ID($_DLATCHSR_PPN_
)) { check_gate("ESRDQ"); return; }
1335 if (cell
->type
== ID($_DLATCHSR_PPP_
)) { check_gate("ESRDQ"); return; }
1343 void RTLIL::Module::sort()
1345 wires_
.sort(sort_by_id_str());
1346 cells_
.sort(sort_by_id_str());
1347 avail_parameters
.sort(sort_by_id_str());
1348 memories
.sort(sort_by_id_str());
1349 processes
.sort(sort_by_id_str());
1350 for (auto &it
: cells_
)
1352 for (auto &it
: wires_
)
1353 it
.second
->attributes
.sort(sort_by_id_str());
1354 for (auto &it
: memories
)
1355 it
.second
->attributes
.sort(sort_by_id_str());
1358 void RTLIL::Module::check()
1361 std::vector
<bool> ports_declared
;
1362 for (auto &it
: wires_
) {
1363 log_assert(this == it
.second
->module
);
1364 log_assert(it
.first
== it
.second
->name
);
1365 log_assert(!it
.first
.empty());
1366 log_assert(it
.second
->width
>= 0);
1367 log_assert(it
.second
->port_id
>= 0);
1368 for (auto &it2
: it
.second
->attributes
)
1369 log_assert(!it2
.first
.empty());
1370 if (it
.second
->port_id
) {
1371 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1372 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1373 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1374 if (GetSize(ports_declared
) < it
.second
->port_id
)
1375 ports_declared
.resize(it
.second
->port_id
);
1376 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1377 ports_declared
[it
.second
->port_id
-1] = true;
1379 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1381 for (auto port_declared
: ports_declared
)
1382 log_assert(port_declared
== true);
1383 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1385 for (auto &it
: memories
) {
1386 log_assert(it
.first
== it
.second
->name
);
1387 log_assert(!it
.first
.empty());
1388 log_assert(it
.second
->width
>= 0);
1389 log_assert(it
.second
->size
>= 0);
1390 for (auto &it2
: it
.second
->attributes
)
1391 log_assert(!it2
.first
.empty());
1394 for (auto &it
: cells_
) {
1395 log_assert(this == it
.second
->module
);
1396 log_assert(it
.first
== it
.second
->name
);
1397 log_assert(!it
.first
.empty());
1398 log_assert(!it
.second
->type
.empty());
1399 for (auto &it2
: it
.second
->connections()) {
1400 log_assert(!it2
.first
.empty());
1403 for (auto &it2
: it
.second
->attributes
)
1404 log_assert(!it2
.first
.empty());
1405 for (auto &it2
: it
.second
->parameters
)
1406 log_assert(!it2
.first
.empty());
1407 InternalCellChecker
checker(this, it
.second
);
1411 for (auto &it
: processes
) {
1412 log_assert(it
.first
== it
.second
->name
);
1413 log_assert(!it
.first
.empty());
1414 log_assert(it
.second
->root_case
.compare
.empty());
1415 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1416 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1417 for (auto &switch_it
: all_cases
[i
]->switches
) {
1418 for (auto &case_it
: switch_it
->cases
) {
1419 for (auto &compare_it
: case_it
->compare
) {
1420 log_assert(switch_it
->signal
.size() == compare_it
.size());
1422 all_cases
.push_back(case_it
);
1426 for (auto &sync_it
: it
.second
->syncs
) {
1427 switch (sync_it
->type
) {
1433 log_assert(!sync_it
->signal
.empty());
1438 log_assert(sync_it
->signal
.empty());
1444 for (auto &it
: connections_
) {
1445 log_assert(it
.first
.size() == it
.second
.size());
1446 log_assert(!it
.first
.has_const());
1451 for (auto &it
: attributes
)
1452 log_assert(!it
.first
.empty());
1456 void RTLIL::Module::optimize()
1460 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1462 log_assert(new_mod
->refcount_wires_
== 0);
1463 log_assert(new_mod
->refcount_cells_
== 0);
1465 new_mod
->avail_parameters
= avail_parameters
;
1467 for (auto &conn
: connections_
)
1468 new_mod
->connect(conn
);
1470 for (auto &attr
: attributes
)
1471 new_mod
->attributes
[attr
.first
] = attr
.second
;
1473 for (auto &it
: wires_
)
1474 new_mod
->addWire(it
.first
, it
.second
);
1476 for (auto &it
: memories
)
1477 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1479 for (auto &it
: cells_
)
1480 new_mod
->addCell(it
.first
, it
.second
);
1482 for (auto &it
: processes
)
1483 new_mod
->processes
[it
.first
] = it
.second
->clone();
1485 struct RewriteSigSpecWorker
1488 void operator()(RTLIL::SigSpec
&sig
)
1490 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1491 for (auto &c
: chunks
)
1493 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1498 RewriteSigSpecWorker rewriteSigSpecWorker
;
1499 rewriteSigSpecWorker
.mod
= new_mod
;
1500 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1501 new_mod
->fixup_ports();
1504 RTLIL::Module
*RTLIL::Module::clone() const
1506 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1507 new_mod
->name
= name
;
1512 bool RTLIL::Module::has_memories() const
1514 return !memories
.empty();
1517 bool RTLIL::Module::has_processes() const
1519 return !processes
.empty();
1522 bool RTLIL::Module::has_memories_warn() const
1524 if (!memories
.empty())
1525 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1526 return !memories
.empty();
1529 bool RTLIL::Module::has_processes_warn() const
1531 if (!processes
.empty())
1532 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1533 return !processes
.empty();
1536 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1538 std::vector
<RTLIL::Wire
*> result
;
1539 result
.reserve(wires_
.size());
1540 for (auto &it
: wires_
)
1541 if (design
->selected(this, it
.second
))
1542 result
.push_back(it
.second
);
1546 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1548 std::vector
<RTLIL::Cell
*> result
;
1549 result
.reserve(cells_
.size());
1550 for (auto &it
: cells_
)
1551 if (design
->selected(this, it
.second
))
1552 result
.push_back(it
.second
);
1556 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1558 log_assert(!wire
->name
.empty());
1559 log_assert(count_id(wire
->name
) == 0);
1560 log_assert(refcount_wires_
== 0);
1561 wires_
[wire
->name
] = wire
;
1562 wire
->module
= this;
1565 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1567 log_assert(!cell
->name
.empty());
1568 log_assert(count_id(cell
->name
) == 0);
1569 log_assert(refcount_cells_
== 0);
1570 cells_
[cell
->name
] = cell
;
1571 cell
->module
= this;
1574 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1576 log_assert(refcount_wires_
== 0);
1578 struct DeleteWireWorker
1580 RTLIL::Module
*module
;
1581 const pool
<RTLIL::Wire
*> *wires_p
;
1583 void operator()(RTLIL::SigSpec
&sig
) {
1584 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1585 for (auto &c
: chunks
)
1586 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1587 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1593 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1594 log_assert(GetSize(lhs
) == GetSize(rhs
));
1595 RTLIL::SigSpec new_lhs
, new_rhs
;
1596 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1597 RTLIL::SigBit lhs_bit
= lhs
[i
];
1598 if (lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
))
1600 RTLIL::SigBit rhs_bit
= rhs
[i
];
1601 if (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))
1603 new_lhs
.append(lhs_bit
);
1604 new_rhs
.append(rhs_bit
);
1611 DeleteWireWorker delete_wire_worker
;
1612 delete_wire_worker
.module
= this;
1613 delete_wire_worker
.wires_p
= &wires
;
1614 rewrite_sigspecs2(delete_wire_worker
);
1616 for (auto &it
: wires
) {
1617 log_assert(wires_
.count(it
->name
) != 0);
1618 wires_
.erase(it
->name
);
1623 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1625 while (!cell
->connections_
.empty())
1626 cell
->unsetPort(cell
->connections_
.begin()->first
);
1628 log_assert(cells_
.count(cell
->name
) != 0);
1629 log_assert(refcount_cells_
== 0);
1630 cells_
.erase(cell
->name
);
1634 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1636 log_assert(wires_
[wire
->name
] == wire
);
1637 log_assert(refcount_wires_
== 0);
1638 wires_
.erase(wire
->name
);
1639 wire
->name
= new_name
;
1643 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1645 log_assert(cells_
[cell
->name
] == cell
);
1646 log_assert(refcount_wires_
== 0);
1647 cells_
.erase(cell
->name
);
1648 cell
->name
= new_name
;
1652 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1654 log_assert(count_id(old_name
) != 0);
1655 if (wires_
.count(old_name
))
1656 rename(wires_
.at(old_name
), new_name
);
1657 else if (cells_
.count(old_name
))
1658 rename(cells_
.at(old_name
), new_name
);
1663 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1665 log_assert(wires_
[w1
->name
] == w1
);
1666 log_assert(wires_
[w2
->name
] == w2
);
1667 log_assert(refcount_wires_
== 0);
1669 wires_
.erase(w1
->name
);
1670 wires_
.erase(w2
->name
);
1672 std::swap(w1
->name
, w2
->name
);
1674 wires_
[w1
->name
] = w1
;
1675 wires_
[w2
->name
] = w2
;
1678 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1680 log_assert(cells_
[c1
->name
] == c1
);
1681 log_assert(cells_
[c2
->name
] == c2
);
1682 log_assert(refcount_cells_
== 0);
1684 cells_
.erase(c1
->name
);
1685 cells_
.erase(c2
->name
);
1687 std::swap(c1
->name
, c2
->name
);
1689 cells_
[c1
->name
] = c1
;
1690 cells_
[c2
->name
] = c2
;
1693 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1696 return uniquify(name
, index
);
1699 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1702 if (count_id(name
) == 0)
1708 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1709 if (count_id(new_name
) == 0)
1715 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1717 if (a
->port_id
&& !b
->port_id
)
1719 if (!a
->port_id
&& b
->port_id
)
1722 if (a
->port_id
== b
->port_id
)
1723 return a
->name
< b
->name
;
1724 return a
->port_id
< b
->port_id
;
1727 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1729 for (auto mon
: monitors
)
1730 mon
->notify_connect(this, conn
);
1733 for (auto mon
: design
->monitors
)
1734 mon
->notify_connect(this, conn
);
1736 // ignore all attempts to assign constants to other constants
1737 if (conn
.first
.has_const()) {
1738 RTLIL::SigSig new_conn
;
1739 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1740 if (conn
.first
[i
].wire
) {
1741 new_conn
.first
.append(conn
.first
[i
]);
1742 new_conn
.second
.append(conn
.second
[i
]);
1744 if (GetSize(new_conn
.first
))
1750 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
));
1751 log_backtrace("-X- ", yosys_xtrace
-1);
1754 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1755 connections_
.push_back(conn
);
1758 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1760 connect(RTLIL::SigSig(lhs
, rhs
));
1763 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1765 for (auto mon
: monitors
)
1766 mon
->notify_connect(this, new_conn
);
1769 for (auto mon
: design
->monitors
)
1770 mon
->notify_connect(this, new_conn
);
1773 log("#X# New connections vector in %s:\n", log_id(this));
1774 for (auto &conn
: new_conn
)
1775 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1776 log_backtrace("-X- ", yosys_xtrace
-1);
1779 connections_
= new_conn
;
1782 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1784 return connections_
;
1787 void RTLIL::Module::fixup_ports()
1789 std::vector
<RTLIL::Wire
*> all_ports
;
1791 for (auto &w
: wires_
)
1792 if (w
.second
->port_input
|| w
.second
->port_output
)
1793 all_ports
.push_back(w
.second
);
1795 w
.second
->port_id
= 0;
1797 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1800 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1801 ports
.push_back(all_ports
[i
]->name
);
1802 all_ports
[i
]->port_id
= i
+1;
1806 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1808 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1810 wire
->width
= width
;
1815 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1817 RTLIL::Wire
*wire
= addWire(name
);
1818 wire
->width
= other
->width
;
1819 wire
->start_offset
= other
->start_offset
;
1820 wire
->port_id
= other
->port_id
;
1821 wire
->port_input
= other
->port_input
;
1822 wire
->port_output
= other
->port_output
;
1823 wire
->upto
= other
->upto
;
1824 wire
->attributes
= other
->attributes
;
1828 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1830 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1837 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1839 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1840 cell
->connections_
= other
->connections_
;
1841 cell
->parameters
= other
->parameters
;
1842 cell
->attributes
= other
->attributes
;
1846 #define DEF_METHOD(_func, _y_size, _type) \
1847 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1848 RTLIL::Cell *cell = addCell(name, _type); \
1849 cell->parameters[ID(A_SIGNED)] = is_signed; \
1850 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1851 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1852 cell->setPort(ID::A, sig_a); \
1853 cell->setPort(ID::Y, sig_y); \
1854 cell->set_src_attribute(src); \
1857 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1858 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1859 add ## _func(name, sig_a, sig_y, is_signed, src); \
1862 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
1863 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
1864 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
1865 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
1866 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
1867 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
1868 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
1869 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
1870 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
1873 #define DEF_METHOD(_func, _y_size, _type) \
1874 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1875 RTLIL::Cell *cell = addCell(name, _type); \
1876 cell->parameters[ID(A_SIGNED)] = is_signed; \
1877 cell->parameters[ID(B_SIGNED)] = is_signed; \
1878 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1879 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1880 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1881 cell->setPort(ID::A, sig_a); \
1882 cell->setPort(ID::B, sig_b); \
1883 cell->setPort(ID::Y, sig_y); \
1884 cell->set_src_attribute(src); \
1887 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1888 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1889 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1892 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
1893 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
1894 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
1895 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
1896 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
1897 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
1898 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
1899 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
1900 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
1901 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
1902 DEF_METHOD(Lt
, 1, ID($lt
))
1903 DEF_METHOD(Le
, 1, ID($le
))
1904 DEF_METHOD(Eq
, 1, ID($eq
))
1905 DEF_METHOD(Ne
, 1, ID($ne
))
1906 DEF_METHOD(Eqx
, 1, ID($eqx
))
1907 DEF_METHOD(Nex
, 1, ID($nex
))
1908 DEF_METHOD(Ge
, 1, ID($ge
))
1909 DEF_METHOD(Gt
, 1, ID($gt
))
1910 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
1911 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
1912 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
1913 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
1914 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
1915 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
1916 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
1919 #define DEF_METHOD(_func, _type, _pmux) \
1920 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y, const std::string &src) { \
1921 RTLIL::Cell *cell = addCell(name, _type); \
1922 cell->parameters[ID(WIDTH)] = sig_a.size(); \
1923 if (_pmux) cell->parameters[ID(S_WIDTH)] = sig_s.size(); \
1924 cell->setPort(ID::A, sig_a); \
1925 cell->setPort(ID::B, sig_b); \
1926 cell->setPort(ID(S), sig_s); \
1927 cell->setPort(ID::Y, sig_y); \
1928 cell->set_src_attribute(src); \
1931 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1932 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1933 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1936 DEF_METHOD(Mux
, ID($mux
), 0)
1937 DEF_METHOD(Pmux
, ID($pmux
), 1)
1940 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1941 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1942 RTLIL::Cell *cell = addCell(name, _type); \
1943 cell->setPort("\\" #_P1, sig1); \
1944 cell->setPort("\\" #_P2, sig2); \
1945 cell->set_src_attribute(src); \
1948 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1949 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1950 add ## _func(name, sig1, sig2, src); \
1953 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1954 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1955 RTLIL::Cell *cell = addCell(name, _type); \
1956 cell->setPort("\\" #_P1, sig1); \
1957 cell->setPort("\\" #_P2, sig2); \
1958 cell->setPort("\\" #_P3, sig3); \
1959 cell->set_src_attribute(src); \
1962 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1963 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1964 add ## _func(name, sig1, sig2, sig3, src); \
1967 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1968 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1969 RTLIL::Cell *cell = addCell(name, _type); \
1970 cell->setPort("\\" #_P1, sig1); \
1971 cell->setPort("\\" #_P2, sig2); \
1972 cell->setPort("\\" #_P3, sig3); \
1973 cell->setPort("\\" #_P4, sig4); \
1974 cell->set_src_attribute(src); \
1977 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1978 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1979 add ## _func(name, sig1, sig2, sig3, sig4, src); \
1982 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1983 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5, const std::string &src) { \
1984 RTLIL::Cell *cell = addCell(name, _type); \
1985 cell->setPort("\\" #_P1, sig1); \
1986 cell->setPort("\\" #_P2, sig2); \
1987 cell->setPort("\\" #_P3, sig3); \
1988 cell->setPort("\\" #_P4, sig4); \
1989 cell->setPort("\\" #_P5, sig5); \
1990 cell->set_src_attribute(src); \
1993 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1994 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1995 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
1998 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
1999 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2000 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2001 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2002 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2003 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2004 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2005 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2006 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2007 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2008 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2009 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2010 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2011 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2012 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2013 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2019 RTLIL::Cell
* RTLIL::Module::addPow(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool a_signed
, bool b_signed
, const std::string
&src
)
2021 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2022 cell
->parameters
[ID(A_SIGNED
)] = a_signed
;
2023 cell
->parameters
[ID(B_SIGNED
)] = b_signed
;
2024 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2025 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2026 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2027 cell
->setPort(ID::A
, sig_a
);
2028 cell
->setPort(ID::B
, sig_b
);
2029 cell
->setPort(ID::Y
, sig_y
);
2030 cell
->set_src_attribute(src
);
2034 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
2036 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2037 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2038 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2039 cell
->parameters
[ID(OFFSET
)] = offset
;
2040 cell
->setPort(ID::A
, sig_a
);
2041 cell
->setPort(ID::Y
, sig_y
);
2042 cell
->set_src_attribute(src
);
2046 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2048 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2049 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2050 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2051 cell
->setPort(ID::A
, sig_a
);
2052 cell
->setPort(ID::B
, sig_b
);
2053 cell
->setPort(ID::Y
, sig_y
);
2054 cell
->set_src_attribute(src
);
2058 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
2060 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2061 cell
->parameters
[ID(LUT
)] = lut
;
2062 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2063 cell
->setPort(ID::A
, sig_a
);
2064 cell
->setPort(ID::Y
, sig_y
);
2065 cell
->set_src_attribute(src
);
2069 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2071 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2072 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2073 cell
->setPort(ID::A
, sig_a
);
2074 cell
->setPort(ID(EN
), sig_en
);
2075 cell
->setPort(ID::Y
, sig_y
);
2076 cell
->set_src_attribute(src
);
2080 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2082 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2083 cell
->setPort(ID::A
, sig_a
);
2084 cell
->setPort(ID(EN
), sig_en
);
2085 cell
->set_src_attribute(src
);
2089 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2091 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2092 cell
->setPort(ID::A
, sig_a
);
2093 cell
->setPort(ID(EN
), sig_en
);
2094 cell
->set_src_attribute(src
);
2098 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2100 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2101 cell
->setPort(ID::A
, sig_a
);
2102 cell
->setPort(ID(EN
), sig_en
);
2103 cell
->set_src_attribute(src
);
2107 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2109 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2110 cell
->setPort(ID::A
, sig_a
);
2111 cell
->setPort(ID(EN
), sig_en
);
2112 cell
->set_src_attribute(src
);
2116 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2118 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2119 cell
->setPort(ID::A
, sig_a
);
2120 cell
->setPort(ID(EN
), sig_en
);
2121 cell
->set_src_attribute(src
);
2125 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2127 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2128 cell
->setPort(ID::A
, sig_a
);
2129 cell
->setPort(ID::B
, sig_b
);
2130 cell
->setPort(ID::Y
, sig_y
);
2131 cell
->set_src_attribute(src
);
2135 RTLIL::Cell
* RTLIL::Module::addSr(RTLIL::IdString name
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
, RTLIL::SigSpec sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2137 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2138 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2139 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2140 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2141 cell
->setPort(ID(SET
), sig_set
);
2142 cell
->setPort(ID(CLR
), sig_clr
);
2143 cell
->setPort(ID(Q
), sig_q
);
2144 cell
->set_src_attribute(src
);
2148 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2150 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2151 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2152 cell
->setPort(ID(D
), sig_d
);
2153 cell
->setPort(ID(Q
), sig_q
);
2154 cell
->set_src_attribute(src
);
2158 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, const std::string
&src
)
2160 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2161 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2162 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2163 cell
->setPort(ID(CLK
), sig_clk
);
2164 cell
->setPort(ID(D
), sig_d
);
2165 cell
->setPort(ID(Q
), sig_q
);
2166 cell
->set_src_attribute(src
);
2170 RTLIL::Cell
* RTLIL::Module::addDffe(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2172 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2173 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2174 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2175 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2176 cell
->setPort(ID(CLK
), sig_clk
);
2177 cell
->setPort(ID(EN
), sig_en
);
2178 cell
->setPort(ID(D
), sig_d
);
2179 cell
->setPort(ID(Q
), sig_q
);
2180 cell
->set_src_attribute(src
);
2184 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2185 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2187 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2188 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2189 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2190 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2191 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2192 cell
->setPort(ID(CLK
), sig_clk
);
2193 cell
->setPort(ID(SET
), sig_set
);
2194 cell
->setPort(ID(CLR
), sig_clr
);
2195 cell
->setPort(ID(D
), sig_d
);
2196 cell
->setPort(ID(Q
), sig_q
);
2197 cell
->set_src_attribute(src
);
2201 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2202 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2204 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2205 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2206 cell
->parameters
[ID(ARST_POLARITY
)] = arst_polarity
;
2207 cell
->parameters
[ID(ARST_VALUE
)] = arst_value
;
2208 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2209 cell
->setPort(ID(CLK
), sig_clk
);
2210 cell
->setPort(ID(ARST
), sig_arst
);
2211 cell
->setPort(ID(D
), sig_d
);
2212 cell
->setPort(ID(Q
), sig_q
);
2213 cell
->set_src_attribute(src
);
2217 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, const std::string
&src
)
2219 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2220 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2221 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2222 cell
->setPort(ID(EN
), sig_en
);
2223 cell
->setPort(ID(D
), sig_d
);
2224 cell
->setPort(ID(Q
), sig_q
);
2225 cell
->set_src_attribute(src
);
2229 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2230 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2232 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2233 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2234 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2235 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2236 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2237 cell
->setPort(ID(EN
), sig_en
);
2238 cell
->setPort(ID(SET
), sig_set
);
2239 cell
->setPort(ID(CLR
), sig_clr
);
2240 cell
->setPort(ID(D
), sig_d
);
2241 cell
->setPort(ID(Q
), sig_q
);
2242 cell
->set_src_attribute(src
);
2246 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2248 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2249 cell
->setPort(ID(D
), sig_d
);
2250 cell
->setPort(ID(Q
), sig_q
);
2251 cell
->set_src_attribute(src
);
2255 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, const std::string
&src
)
2257 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2258 cell
->setPort(ID(C
), sig_clk
);
2259 cell
->setPort(ID(D
), sig_d
);
2260 cell
->setPort(ID(Q
), sig_q
);
2261 cell
->set_src_attribute(src
);
2265 RTLIL::Cell
* RTLIL::Module::addDffeGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2267 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2268 cell
->setPort(ID(C
), sig_clk
);
2269 cell
->setPort(ID(E
), sig_en
);
2270 cell
->setPort(ID(D
), sig_d
);
2271 cell
->setPort(ID(Q
), sig_q
);
2272 cell
->set_src_attribute(src
);
2276 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2277 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2279 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2280 cell
->setPort(ID(C
), sig_clk
);
2281 cell
->setPort(ID(S
), sig_set
);
2282 cell
->setPort(ID(R
), sig_clr
);
2283 cell
->setPort(ID(D
), sig_d
);
2284 cell
->setPort(ID(Q
), sig_q
);
2285 cell
->set_src_attribute(src
);
2289 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2290 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2292 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2293 cell
->setPort(ID(C
), sig_clk
);
2294 cell
->setPort(ID(R
), sig_arst
);
2295 cell
->setPort(ID(D
), sig_d
);
2296 cell
->setPort(ID(Q
), sig_q
);
2297 cell
->set_src_attribute(src
);
2301 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, const std::string
&src
)
2303 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2304 cell
->setPort(ID(E
), sig_en
);
2305 cell
->setPort(ID(D
), sig_d
);
2306 cell
->setPort(ID(Q
), sig_q
);
2307 cell
->set_src_attribute(src
);
2311 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2312 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2314 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2315 cell
->setPort(ID(E
), sig_en
);
2316 cell
->setPort(ID(S
), sig_set
);
2317 cell
->setPort(ID(R
), sig_clr
);
2318 cell
->setPort(ID(D
), sig_d
);
2319 cell
->setPort(ID(Q
), sig_q
);
2320 cell
->set_src_attribute(src
);
2324 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2326 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2327 Cell
*cell
= addCell(name
, ID($anyconst
));
2328 cell
->setParam(ID(WIDTH
), width
);
2329 cell
->setPort(ID::Y
, sig
);
2330 cell
->set_src_attribute(src
);
2334 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2336 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2337 Cell
*cell
= addCell(name
, ID($anyseq
));
2338 cell
->setParam(ID(WIDTH
), width
);
2339 cell
->setPort(ID::Y
, sig
);
2340 cell
->set_src_attribute(src
);
2344 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2346 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2347 Cell
*cell
= addCell(name
, ID($allconst
));
2348 cell
->setParam(ID(WIDTH
), width
);
2349 cell
->setPort(ID::Y
, sig
);
2350 cell
->set_src_attribute(src
);
2354 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2356 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2357 Cell
*cell
= addCell(name
, ID($allseq
));
2358 cell
->setParam(ID(WIDTH
), width
);
2359 cell
->setPort(ID::Y
, sig
);
2360 cell
->set_src_attribute(src
);
2364 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2366 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2367 Cell
*cell
= addCell(name
, ID($initstate
));
2368 cell
->setPort(ID::Y
, sig
);
2369 cell
->set_src_attribute(src
);
2375 static unsigned int hashidx_count
= 123456789;
2376 hashidx_count
= mkhash_xorshift(hashidx_count
);
2377 hashidx_
= hashidx_count
;
2384 port_output
= false;
2388 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2392 RTLIL::Wire::~Wire()
2395 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2400 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2401 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2407 RTLIL::Memory::Memory()
2409 static unsigned int hashidx_count
= 123456789;
2410 hashidx_count
= mkhash_xorshift(hashidx_count
);
2411 hashidx_
= hashidx_count
;
2417 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2421 RTLIL::Cell::Cell() : module(nullptr)
2423 static unsigned int hashidx_count
= 123456789;
2424 hashidx_count
= mkhash_xorshift(hashidx_count
);
2425 hashidx_
= hashidx_count
;
2427 // log("#memtrace# %p\n", this);
2431 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2435 RTLIL::Cell::~Cell()
2438 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2443 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2444 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2450 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2452 return connections_
.count(portname
) != 0;
2455 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2457 RTLIL::SigSpec signal
;
2458 auto conn_it
= connections_
.find(portname
);
2460 if (conn_it
!= connections_
.end())
2462 for (auto mon
: module
->monitors
)
2463 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2466 for (auto mon
: module
->design
->monitors
)
2467 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2470 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2471 log_backtrace("-X- ", yosys_xtrace
-1);
2474 connections_
.erase(conn_it
);
2478 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2480 auto conn_it
= connections_
.find(portname
);
2482 if (conn_it
== connections_
.end()) {
2483 connections_
[portname
] = RTLIL::SigSpec();
2484 conn_it
= connections_
.find(portname
);
2485 log_assert(conn_it
!= connections_
.end());
2487 if (conn_it
->second
== signal
)
2490 for (auto mon
: module
->monitors
)
2491 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2494 for (auto mon
: module
->design
->monitors
)
2495 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2498 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2499 log_backtrace("-X- ", yosys_xtrace
-1);
2502 conn_it
->second
= signal
;
2505 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2507 return connections_
.at(portname
);
2510 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2512 return connections_
;
2515 bool RTLIL::Cell::known() const
2517 if (yosys_celltypes
.cell_known(type
))
2519 if (module
&& module
->design
&& module
->design
->module(type
))
2524 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2526 if (yosys_celltypes
.cell_known(type
))
2527 return yosys_celltypes
.cell_input(type
, portname
);
2528 if (module
&& module
->design
) {
2529 RTLIL::Module
*m
= module
->design
->module(type
);
2530 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2531 return w
&& w
->port_input
;
2536 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2538 if (yosys_celltypes
.cell_known(type
))
2539 return yosys_celltypes
.cell_output(type
, portname
);
2540 if (module
&& module
->design
) {
2541 RTLIL::Module
*m
= module
->design
->module(type
);
2542 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2543 return w
&& w
->port_output
;
2548 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2550 return parameters
.count(paramname
) != 0;
2553 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2555 parameters
.erase(paramname
);
2558 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2560 parameters
[paramname
] = value
;
2563 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2565 return parameters
.at(paramname
);
2568 void RTLIL::Cell::sort()
2570 connections_
.sort(sort_by_id_str());
2571 parameters
.sort(sort_by_id_str());
2572 attributes
.sort(sort_by_id_str());
2575 void RTLIL::Cell::check()
2578 InternalCellChecker
checker(NULL
, this);
2583 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2585 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
2586 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
2589 if (type
== ID($mux
) || type
== ID($pmux
)) {
2590 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2591 if (type
== ID($pmux
))
2592 parameters
[ID(S_WIDTH
)] = GetSize(connections_
[ID(S
)]);
2597 if (type
== ID($lut
) || type
== ID($sop
)) {
2598 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::A
]);
2602 if (type
== ID($fa
)) {
2603 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2607 if (type
== ID($lcu
)) {
2608 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(CO
)]);
2612 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
2614 if (connections_
.count(ID::A
)) {
2615 if (signedness_ab
) {
2617 parameters
[ID(A_SIGNED
)] = true;
2618 else if (parameters
.count(ID(A_SIGNED
)) == 0)
2619 parameters
[ID(A_SIGNED
)] = false;
2621 parameters
[ID(A_WIDTH
)] = GetSize(connections_
[ID::A
]);
2624 if (connections_
.count(ID::B
)) {
2625 if (signedness_ab
) {
2627 parameters
[ID(B_SIGNED
)] = true;
2628 else if (parameters
.count(ID(B_SIGNED
)) == 0)
2629 parameters
[ID(B_SIGNED
)] = false;
2631 parameters
[ID(B_WIDTH
)] = GetSize(connections_
[ID::B
]);
2634 if (connections_
.count(ID::Y
))
2635 parameters
[ID(Y_WIDTH
)] = GetSize(connections_
[ID::Y
]);
2637 if (connections_
.count(ID(Q
)))
2638 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(Q
)]);
2643 RTLIL::SigChunk::SigChunk()
2650 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2654 width
= GetSize(data
);
2658 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2660 log_assert(wire
!= nullptr);
2662 this->width
= wire
->width
;
2666 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2668 log_assert(wire
!= nullptr);
2670 this->width
= width
;
2671 this->offset
= offset
;
2674 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2677 data
= RTLIL::Const(str
).bits
;
2678 width
= GetSize(data
);
2682 RTLIL::SigChunk::SigChunk(int val
, int width
)
2685 data
= RTLIL::Const(val
, width
).bits
;
2686 this->width
= GetSize(data
);
2690 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2693 data
= RTLIL::Const(bit
, width
).bits
;
2694 this->width
= GetSize(data
);
2698 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2703 data
= RTLIL::Const(bit
.data
).bits
;
2705 offset
= bit
.offset
;
2709 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2711 wire
= sigchunk
.wire
;
2712 data
= sigchunk
.data
;
2713 width
= sigchunk
.width
;
2714 offset
= sigchunk
.offset
;
2717 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2719 RTLIL::SigChunk ret
;
2722 ret
.offset
= this->offset
+ offset
;
2725 for (int i
= 0; i
< length
; i
++)
2726 ret
.data
.push_back(data
[offset
+i
]);
2732 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2734 if (wire
&& other
.wire
)
2735 if (wire
->name
!= other
.wire
->name
)
2736 return wire
->name
< other
.wire
->name
;
2738 if (wire
!= other
.wire
)
2739 return wire
< other
.wire
;
2741 if (offset
!= other
.offset
)
2742 return offset
< other
.offset
;
2744 if (width
!= other
.width
)
2745 return width
< other
.width
;
2747 return data
< other
.data
;
2750 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2752 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2755 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2762 RTLIL::SigSpec::SigSpec()
2768 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2773 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2775 cover("kernel.rtlil.sigspec.init.list");
2780 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2781 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2785 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2787 cover("kernel.rtlil.sigspec.assign");
2789 width_
= other
.width_
;
2790 hash_
= other
.hash_
;
2791 chunks_
= other
.chunks_
;
2794 if (!other
.bits_
.empty())
2796 RTLIL::SigChunk
*last
= NULL
;
2797 int last_end_offset
= 0;
2799 for (auto &bit
: other
.bits_
) {
2800 if (last
&& bit
.wire
== last
->wire
) {
2801 if (bit
.wire
== NULL
) {
2802 last
->data
.push_back(bit
.data
);
2805 } else if (last_end_offset
== bit
.offset
) {
2811 chunks_
.push_back(bit
);
2812 last
= &chunks_
.back();
2813 last_end_offset
= bit
.offset
+ 1;
2822 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2824 cover("kernel.rtlil.sigspec.init.const");
2826 chunks_
.push_back(RTLIL::SigChunk(value
));
2827 width_
= chunks_
.back().width
;
2832 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2834 cover("kernel.rtlil.sigspec.init.chunk");
2836 chunks_
.push_back(chunk
);
2837 width_
= chunks_
.back().width
;
2842 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2844 cover("kernel.rtlil.sigspec.init.wire");
2846 chunks_
.push_back(RTLIL::SigChunk(wire
));
2847 width_
= chunks_
.back().width
;
2852 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2854 cover("kernel.rtlil.sigspec.init.wire_part");
2856 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2857 width_
= chunks_
.back().width
;
2862 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2864 cover("kernel.rtlil.sigspec.init.str");
2866 chunks_
.push_back(RTLIL::SigChunk(str
));
2867 width_
= chunks_
.back().width
;
2872 RTLIL::SigSpec::SigSpec(int val
, int width
)
2874 cover("kernel.rtlil.sigspec.init.int");
2876 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2882 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2884 cover("kernel.rtlil.sigspec.init.state");
2886 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2892 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2894 cover("kernel.rtlil.sigspec.init.bit");
2896 if (bit
.wire
== NULL
)
2897 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2899 for (int i
= 0; i
< width
; i
++)
2900 chunks_
.push_back(bit
);
2906 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2908 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2912 for (auto &c
: chunks
)
2917 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2919 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2923 for (auto &bit
: bits
)
2928 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2930 cover("kernel.rtlil.sigspec.init.pool_bits");
2934 for (auto &bit
: bits
)
2939 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2941 cover("kernel.rtlil.sigspec.init.stdset_bits");
2945 for (auto &bit
: bits
)
2950 RTLIL::SigSpec::SigSpec(bool bit
)
2952 cover("kernel.rtlil.sigspec.init.bool");
2960 void RTLIL::SigSpec::pack() const
2962 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2964 if (that
->bits_
.empty())
2967 cover("kernel.rtlil.sigspec.convert.pack");
2968 log_assert(that
->chunks_
.empty());
2970 std::vector
<RTLIL::SigBit
> old_bits
;
2971 old_bits
.swap(that
->bits_
);
2973 RTLIL::SigChunk
*last
= NULL
;
2974 int last_end_offset
= 0;
2976 for (auto &bit
: old_bits
) {
2977 if (last
&& bit
.wire
== last
->wire
) {
2978 if (bit
.wire
== NULL
) {
2979 last
->data
.push_back(bit
.data
);
2982 } else if (last_end_offset
== bit
.offset
) {
2988 that
->chunks_
.push_back(bit
);
2989 last
= &that
->chunks_
.back();
2990 last_end_offset
= bit
.offset
+ 1;
2996 void RTLIL::SigSpec::unpack() const
2998 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3000 if (that
->chunks_
.empty())
3003 cover("kernel.rtlil.sigspec.convert.unpack");
3004 log_assert(that
->bits_
.empty());
3006 that
->bits_
.reserve(that
->width_
);
3007 for (auto &c
: that
->chunks_
)
3008 for (int i
= 0; i
< c
.width
; i
++)
3009 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
3011 that
->chunks_
.clear();
3015 void RTLIL::SigSpec::updhash() const
3017 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3019 if (that
->hash_
!= 0)
3022 cover("kernel.rtlil.sigspec.hash");
3025 that
->hash_
= mkhash_init
;
3026 for (auto &c
: that
->chunks_
)
3027 if (c
.wire
== NULL
) {
3028 for (auto &v
: c
.data
)
3029 that
->hash_
= mkhash(that
->hash_
, v
);
3031 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3032 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3033 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3036 if (that
->hash_
== 0)
3040 void RTLIL::SigSpec::sort()
3043 cover("kernel.rtlil.sigspec.sort");
3044 std::sort(bits_
.begin(), bits_
.end());
3047 void RTLIL::SigSpec::sort_and_unify()
3050 cover("kernel.rtlil.sigspec.sort_and_unify");
3052 // A copy of the bits vector is used to prevent duplicating the logic from
3053 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3054 // that isn't showing up as significant in profiles.
3055 std::vector
<SigBit
> unique_bits
= bits_
;
3056 std::sort(unique_bits
.begin(), unique_bits
.end());
3057 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3058 unique_bits
.erase(last
, unique_bits
.end());
3060 *this = unique_bits
;
3063 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3065 replace(pattern
, with
, this);
3068 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3070 log_assert(other
!= NULL
);
3071 log_assert(width_
== other
->width_
);
3072 log_assert(pattern
.width_
== with
.width_
);
3079 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3080 if (pattern
.bits_
[i
].wire
!= NULL
) {
3081 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3082 if (bits_
[j
] == pattern
.bits_
[i
]) {
3083 other
->bits_
[j
] = with
.bits_
[i
];
3092 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3094 replace(rules
, this);
3097 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3099 cover("kernel.rtlil.sigspec.replace_dict");
3101 log_assert(other
!= NULL
);
3102 log_assert(width_
== other
->width_
);
3104 if (rules
.empty()) return;
3108 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3109 auto it
= rules
.find(bits_
[i
]);
3110 if (it
!= rules
.end())
3111 other
->bits_
[i
] = it
->second
;
3117 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3119 replace(rules
, this);
3122 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3124 cover("kernel.rtlil.sigspec.replace_map");
3126 log_assert(other
!= NULL
);
3127 log_assert(width_
== other
->width_
);
3129 if (rules
.empty()) return;
3133 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3134 auto it
= rules
.find(bits_
[i
]);
3135 if (it
!= rules
.end())
3136 other
->bits_
[i
] = it
->second
;
3142 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3144 remove2(pattern
, NULL
);
3147 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3149 RTLIL::SigSpec tmp
= *this;
3150 tmp
.remove2(pattern
, other
);
3153 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3156 cover("kernel.rtlil.sigspec.remove_other");
3158 cover("kernel.rtlil.sigspec.remove");
3161 if (other
!= NULL
) {
3162 log_assert(width_
== other
->width_
);
3166 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3168 if (bits_
[i
].wire
== NULL
) continue;
3170 for (auto &pattern_chunk
: pattern
.chunks())
3171 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3172 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3173 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3174 bits_
.erase(bits_
.begin() + i
);
3176 if (other
!= NULL
) {
3177 other
->bits_
.erase(other
->bits_
.begin() + i
);
3187 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3189 remove2(pattern
, NULL
);
3192 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3194 RTLIL::SigSpec tmp
= *this;
3195 tmp
.remove2(pattern
, other
);
3198 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3201 cover("kernel.rtlil.sigspec.remove_other");
3203 cover("kernel.rtlil.sigspec.remove");
3207 if (other
!= NULL
) {
3208 log_assert(width_
== other
->width_
);
3212 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3213 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3214 bits_
.erase(bits_
.begin() + i
);
3216 if (other
!= NULL
) {
3217 other
->bits_
.erase(other
->bits_
.begin() + i
);
3226 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3229 cover("kernel.rtlil.sigspec.remove_other");
3231 cover("kernel.rtlil.sigspec.remove");
3235 if (other
!= NULL
) {
3236 log_assert(width_
== other
->width_
);
3240 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3241 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3242 bits_
.erase(bits_
.begin() + i
);
3244 if (other
!= NULL
) {
3245 other
->bits_
.erase(other
->bits_
.begin() + i
);
3254 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3257 cover("kernel.rtlil.sigspec.extract_other");
3259 cover("kernel.rtlil.sigspec.extract");
3261 log_assert(other
== NULL
|| width_
== other
->width_
);
3264 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3266 for (auto& pattern_chunk
: pattern
.chunks()) {
3268 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3269 for (int i
= 0; i
< width_
; i
++)
3270 if (bits_match
[i
].wire
&&
3271 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3272 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3273 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3274 ret
.append_bit(bits_other
[i
]);
3276 for (int i
= 0; i
< width_
; i
++)
3277 if (bits_match
[i
].wire
&&
3278 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3279 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3280 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3281 ret
.append_bit(bits_match
[i
]);
3289 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3292 cover("kernel.rtlil.sigspec.extract_other");
3294 cover("kernel.rtlil.sigspec.extract");
3296 log_assert(other
== NULL
|| width_
== other
->width_
);
3298 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3302 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3303 for (int i
= 0; i
< width_
; i
++)
3304 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3305 ret
.append_bit(bits_other
[i
]);
3307 for (int i
= 0; i
< width_
; i
++)
3308 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3309 ret
.append_bit(bits_match
[i
]);
3316 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3318 cover("kernel.rtlil.sigspec.replace_pos");
3323 log_assert(offset
>= 0);
3324 log_assert(with
.width_
>= 0);
3325 log_assert(offset
+with
.width_
<= width_
);
3327 for (int i
= 0; i
< with
.width_
; i
++)
3328 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3333 void RTLIL::SigSpec::remove_const()
3337 cover("kernel.rtlil.sigspec.remove_const.packed");
3339 std::vector
<RTLIL::SigChunk
> new_chunks
;
3340 new_chunks
.reserve(GetSize(chunks_
));
3343 for (auto &chunk
: chunks_
)
3344 if (chunk
.wire
!= NULL
) {
3345 new_chunks
.push_back(chunk
);
3346 width_
+= chunk
.width
;
3349 chunks_
.swap(new_chunks
);
3353 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3355 std::vector
<RTLIL::SigBit
> new_bits
;
3356 new_bits
.reserve(width_
);
3358 for (auto &bit
: bits_
)
3359 if (bit
.wire
!= NULL
)
3360 new_bits
.push_back(bit
);
3362 bits_
.swap(new_bits
);
3363 width_
= bits_
.size();
3369 void RTLIL::SigSpec::remove(int offset
, int length
)
3371 cover("kernel.rtlil.sigspec.remove_pos");
3375 log_assert(offset
>= 0);
3376 log_assert(length
>= 0);
3377 log_assert(offset
+ length
<= width_
);
3379 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3380 width_
= bits_
.size();
3385 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3388 cover("kernel.rtlil.sigspec.extract_pos");
3389 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3392 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3394 if (signal
.width_
== 0)
3402 cover("kernel.rtlil.sigspec.append");
3404 if (packed() != signal
.packed()) {
3410 for (auto &other_c
: signal
.chunks_
)
3412 auto &my_last_c
= chunks_
.back();
3413 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3414 auto &this_data
= my_last_c
.data
;
3415 auto &other_data
= other_c
.data
;
3416 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3417 my_last_c
.width
+= other_c
.width
;
3419 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3420 my_last_c
.width
+= other_c
.width
;
3422 chunks_
.push_back(other_c
);
3425 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3427 width_
+= signal
.width_
;
3431 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3435 cover("kernel.rtlil.sigspec.append_bit.packed");
3437 if (chunks_
.size() == 0)
3438 chunks_
.push_back(bit
);
3440 if (bit
.wire
== NULL
)
3441 if (chunks_
.back().wire
== NULL
) {
3442 chunks_
.back().data
.push_back(bit
.data
);
3443 chunks_
.back().width
++;
3445 chunks_
.push_back(bit
);
3447 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3448 chunks_
.back().width
++;
3450 chunks_
.push_back(bit
);
3454 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3455 bits_
.push_back(bit
);
3462 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3464 cover("kernel.rtlil.sigspec.extend_u0");
3469 remove(width
, width_
- width
);
3471 if (width_
< width
) {
3472 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3474 padding
= RTLIL::State::S0
;
3475 while (width_
< width
)
3481 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3483 cover("kernel.rtlil.sigspec.repeat");
3486 for (int i
= 0; i
< num
; i
++)
3492 void RTLIL::SigSpec::check() const
3496 cover("kernel.rtlil.sigspec.check.skip");
3500 cover("kernel.rtlil.sigspec.check.packed");
3503 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3504 const RTLIL::SigChunk chunk
= chunks_
[i
];
3505 if (chunk
.wire
== NULL
) {
3507 log_assert(chunks_
[i
-1].wire
!= NULL
);
3508 log_assert(chunk
.offset
== 0);
3509 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3511 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3512 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3513 log_assert(chunk
.offset
>= 0);
3514 log_assert(chunk
.width
>= 0);
3515 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3516 log_assert(chunk
.data
.size() == 0);
3520 log_assert(w
== width_
);
3521 log_assert(bits_
.empty());
3525 cover("kernel.rtlil.sigspec.check.unpacked");
3527 log_assert(width_
== GetSize(bits_
));
3528 log_assert(chunks_
.empty());
3533 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3535 cover("kernel.rtlil.sigspec.comp_lt");
3540 if (width_
!= other
.width_
)
3541 return width_
< other
.width_
;
3546 if (chunks_
.size() != other
.chunks_
.size())
3547 return chunks_
.size() < other
.chunks_
.size();
3552 if (hash_
!= other
.hash_
)
3553 return hash_
< other
.hash_
;
3555 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3556 if (chunks_
[i
] != other
.chunks_
[i
]) {
3557 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3558 return chunks_
[i
] < other
.chunks_
[i
];
3561 cover("kernel.rtlil.sigspec.comp_lt.equal");
3565 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3567 cover("kernel.rtlil.sigspec.comp_eq");
3572 if (width_
!= other
.width_
)
3575 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
3576 // since the RHS will contain one SigChunk of width 0 causing
3577 // the size check below to fail
3584 if (chunks_
.size() != other
.chunks_
.size())
3590 if (hash_
!= other
.hash_
)
3593 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3594 if (chunks_
[i
] != other
.chunks_
[i
]) {
3595 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3599 cover("kernel.rtlil.sigspec.comp_eq.equal");
3603 bool RTLIL::SigSpec::is_wire() const
3605 cover("kernel.rtlil.sigspec.is_wire");
3608 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3611 bool RTLIL::SigSpec::is_chunk() const
3613 cover("kernel.rtlil.sigspec.is_chunk");
3616 return GetSize(chunks_
) == 1;
3619 bool RTLIL::SigSpec::is_fully_const() const
3621 cover("kernel.rtlil.sigspec.is_fully_const");
3624 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3625 if (it
->width
> 0 && it
->wire
!= NULL
)
3630 bool RTLIL::SigSpec::is_fully_zero() const
3632 cover("kernel.rtlil.sigspec.is_fully_zero");
3635 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3636 if (it
->width
> 0 && it
->wire
!= NULL
)
3638 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3639 if (it
->data
[i
] != RTLIL::State::S0
)
3645 bool RTLIL::SigSpec::is_fully_ones() const
3647 cover("kernel.rtlil.sigspec.is_fully_ones");
3650 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3651 if (it
->width
> 0 && it
->wire
!= NULL
)
3653 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3654 if (it
->data
[i
] != RTLIL::State::S1
)
3660 bool RTLIL::SigSpec::is_fully_def() const
3662 cover("kernel.rtlil.sigspec.is_fully_def");
3665 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3666 if (it
->width
> 0 && it
->wire
!= NULL
)
3668 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3669 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3675 bool RTLIL::SigSpec::is_fully_undef() const
3677 cover("kernel.rtlil.sigspec.is_fully_undef");
3680 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3681 if (it
->width
> 0 && it
->wire
!= NULL
)
3683 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3684 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3690 bool RTLIL::SigSpec::has_const() const
3692 cover("kernel.rtlil.sigspec.has_const");
3695 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3696 if (it
->width
> 0 && it
->wire
== NULL
)
3701 bool RTLIL::SigSpec::has_marked_bits() const
3703 cover("kernel.rtlil.sigspec.has_marked_bits");
3706 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3707 if (it
->width
> 0 && it
->wire
== NULL
) {
3708 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3709 if (it
->data
[i
] == RTLIL::State::Sm
)
3715 bool RTLIL::SigSpec::as_bool() const
3717 cover("kernel.rtlil.sigspec.as_bool");
3720 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3722 return RTLIL::Const(chunks_
[0].data
).as_bool();
3726 int RTLIL::SigSpec::as_int(bool is_signed
) const
3728 cover("kernel.rtlil.sigspec.as_int");
3731 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3733 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3737 std::string
RTLIL::SigSpec::as_string() const
3739 cover("kernel.rtlil.sigspec.as_string");
3743 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3744 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3745 if (chunk
.wire
!= NULL
)
3746 for (int j
= 0; j
< chunk
.width
; j
++)
3749 str
+= RTLIL::Const(chunk
.data
).as_string();
3754 RTLIL::Const
RTLIL::SigSpec::as_const() const
3756 cover("kernel.rtlil.sigspec.as_const");
3759 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3761 return chunks_
[0].data
;
3762 return RTLIL::Const();
3765 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3767 cover("kernel.rtlil.sigspec.as_wire");
3770 log_assert(is_wire());
3771 return chunks_
[0].wire
;
3774 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3776 cover("kernel.rtlil.sigspec.as_chunk");
3779 log_assert(is_chunk());
3783 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3785 cover("kernel.rtlil.sigspec.as_bit");
3787 log_assert(width_
== 1);
3789 return RTLIL::SigBit(*chunks_
.begin());
3794 bool RTLIL::SigSpec::match(std::string pattern
) const
3796 cover("kernel.rtlil.sigspec.match");
3799 std::string str
= as_string();
3800 log_assert(pattern
.size() == str
.size());
3802 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3803 if (pattern
[i
] == ' ')
3805 if (pattern
[i
] == '*') {
3806 if (str
[i
] != 'z' && str
[i
] != 'x')
3810 if (pattern
[i
] != str
[i
])
3817 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3819 cover("kernel.rtlil.sigspec.to_sigbit_set");
3822 std::set
<RTLIL::SigBit
> sigbits
;
3823 for (auto &c
: chunks_
)
3824 for (int i
= 0; i
< c
.width
; i
++)
3825 sigbits
.insert(RTLIL::SigBit(c
, i
));
3829 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3831 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3834 pool
<RTLIL::SigBit
> sigbits
;
3835 for (auto &c
: chunks_
)
3836 for (int i
= 0; i
< c
.width
; i
++)
3837 sigbits
.insert(RTLIL::SigBit(c
, i
));
3841 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3843 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3849 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3851 cover("kernel.rtlil.sigspec.to_sigbit_map");
3856 log_assert(width_
== other
.width_
);
3858 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3859 for (int i
= 0; i
< width_
; i
++)
3860 new_map
[bits_
[i
]] = other
.bits_
[i
];
3865 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3867 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3872 log_assert(width_
== other
.width_
);
3874 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3875 for (int i
= 0; i
< width_
; i
++)
3876 new_map
[bits_
[i
]] = other
.bits_
[i
];
3881 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3883 size_t start
= 0, end
= 0;
3884 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3885 tokens
.push_back(text
.substr(start
, end
- start
));
3888 tokens
.push_back(text
.substr(start
));
3891 static int sigspec_parse_get_dummy_line_num()
3896 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3898 cover("kernel.rtlil.sigspec.parse");
3900 AST::current_filename
= "input";
3901 AST::use_internal_line_num();
3902 AST::set_line_num(0);
3904 std::vector
<std::string
> tokens
;
3905 sigspec_parse_split(tokens
, str
, ',');
3907 sig
= RTLIL::SigSpec();
3908 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3910 std::string netname
= tokens
[tokidx
];
3911 std::string indices
;
3913 if (netname
.size() == 0)
3916 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3917 cover("kernel.rtlil.sigspec.parse.const");
3918 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3919 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3922 sig
.append(RTLIL::Const(ast
->bits
));
3930 cover("kernel.rtlil.sigspec.parse.net");
3932 if (netname
[0] != '$' && netname
[0] != '\\')
3933 netname
= "\\" + netname
;
3935 if (module
->wires_
.count(netname
) == 0) {
3936 size_t indices_pos
= netname
.size()-1;
3937 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3940 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3941 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3943 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3945 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3946 indices
= netname
.substr(indices_pos
);
3947 netname
= netname
.substr(0, indices_pos
);
3952 if (module
->wires_
.count(netname
) == 0)
3955 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3956 if (!indices
.empty()) {
3957 std::vector
<std::string
> index_tokens
;
3958 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3959 if (index_tokens
.size() == 1) {
3960 cover("kernel.rtlil.sigspec.parse.bit_sel");
3961 int a
= atoi(index_tokens
.at(0).c_str());
3962 if (a
< 0 || a
>= wire
->width
)
3964 sig
.append(RTLIL::SigSpec(wire
, a
));
3966 cover("kernel.rtlil.sigspec.parse.part_sel");
3967 int a
= atoi(index_tokens
.at(0).c_str());
3968 int b
= atoi(index_tokens
.at(1).c_str());
3973 if (a
< 0 || a
>= wire
->width
)
3975 if (b
< 0 || b
>= wire
->width
)
3977 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3986 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3988 if (str
.empty() || str
[0] != '@')
3989 return parse(sig
, module
, str
);
3991 cover("kernel.rtlil.sigspec.parse.sel");
3993 str
= RTLIL::escape_id(str
.substr(1));
3994 if (design
->selection_vars
.count(str
) == 0)
3997 sig
= RTLIL::SigSpec();
3998 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3999 for (auto &it
: module
->wires_
)
4000 if (sel
.selected_member(module
->name
, it
.first
))
4001 sig
.append(it
.second
);
4006 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4009 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4010 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4015 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4016 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4020 if (lhs
.chunks_
.size() == 1) {
4021 char *p
= (char*)str
.c_str(), *endptr
;
4022 long int val
= strtol(p
, &endptr
, 10);
4023 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4024 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4025 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4030 return parse(sig
, module
, str
);
4033 RTLIL::CaseRule::~CaseRule()
4035 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4039 bool RTLIL::CaseRule::empty() const
4041 return actions
.empty() && switches
.empty();
4044 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4046 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4047 new_caserule
->compare
= compare
;
4048 new_caserule
->actions
= actions
;
4049 for (auto &it
: switches
)
4050 new_caserule
->switches
.push_back(it
->clone());
4051 return new_caserule
;
4054 RTLIL::SwitchRule::~SwitchRule()
4056 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4060 bool RTLIL::SwitchRule::empty() const
4062 return cases
.empty();
4065 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4067 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4068 new_switchrule
->signal
= signal
;
4069 new_switchrule
->attributes
= attributes
;
4070 for (auto &it
: cases
)
4071 new_switchrule
->cases
.push_back(it
->clone());
4072 return new_switchrule
;
4076 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4078 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4079 new_syncrule
->type
= type
;
4080 new_syncrule
->signal
= signal
;
4081 new_syncrule
->actions
= actions
;
4082 return new_syncrule
;
4085 RTLIL::Process::~Process()
4087 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4091 RTLIL::Process
*RTLIL::Process::clone() const
4093 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4095 new_proc
->name
= name
;
4096 new_proc
->attributes
= attributes
;
4098 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4099 new_proc
->root_case
= *rc_ptr
;
4100 rc_ptr
->switches
.clear();
4103 for (auto &it
: syncs
)
4104 new_proc
->syncs
.push_back(it
->clone());
4110 RTLIL::Memory::~Memory()
4112 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4114 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4115 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4117 return &all_memorys
;