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(Shift
, sig_a
.size(), ID($shift
))
1897 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
1898 DEF_METHOD(Lt
, 1, ID($lt
))
1899 DEF_METHOD(Le
, 1, ID($le
))
1900 DEF_METHOD(Eq
, 1, ID($eq
))
1901 DEF_METHOD(Ne
, 1, ID($ne
))
1902 DEF_METHOD(Eqx
, 1, ID($eqx
))
1903 DEF_METHOD(Nex
, 1, ID($nex
))
1904 DEF_METHOD(Ge
, 1, ID($ge
))
1905 DEF_METHOD(Gt
, 1, ID($gt
))
1906 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
1907 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
1908 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
1909 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
1910 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
1911 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
1912 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
1915 #define DEF_METHOD(_func, _y_size, _type) \
1916 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) { \
1917 RTLIL::Cell *cell = addCell(name, _type); \
1918 cell->parameters[ID(A_SIGNED)] = is_signed; \
1919 cell->parameters[ID(B_SIGNED)] = false; \
1920 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1921 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1922 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1923 cell->setPort(ID::A, sig_a); \
1924 cell->setPort(ID::B, sig_b); \
1925 cell->setPort(ID::Y, sig_y); \
1926 cell->set_src_attribute(src); \
1929 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1930 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1931 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1934 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
1935 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
1936 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
1937 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
1940 #define DEF_METHOD(_func, _type, _pmux) \
1941 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) { \
1942 RTLIL::Cell *cell = addCell(name, _type); \
1943 cell->parameters[ID(WIDTH)] = sig_a.size(); \
1944 if (_pmux) cell->parameters[ID(S_WIDTH)] = sig_s.size(); \
1945 cell->setPort(ID::A, sig_a); \
1946 cell->setPort(ID::B, sig_b); \
1947 cell->setPort(ID(S), sig_s); \
1948 cell->setPort(ID::Y, sig_y); \
1949 cell->set_src_attribute(src); \
1952 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1953 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1954 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1957 DEF_METHOD(Mux
, ID($mux
), 0)
1958 DEF_METHOD(Pmux
, ID($pmux
), 1)
1961 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1962 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1963 RTLIL::Cell *cell = addCell(name, _type); \
1964 cell->setPort("\\" #_P1, sig1); \
1965 cell->setPort("\\" #_P2, sig2); \
1966 cell->set_src_attribute(src); \
1969 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1970 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1971 add ## _func(name, sig1, sig2, src); \
1974 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1975 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1976 RTLIL::Cell *cell = addCell(name, _type); \
1977 cell->setPort("\\" #_P1, sig1); \
1978 cell->setPort("\\" #_P2, sig2); \
1979 cell->setPort("\\" #_P3, sig3); \
1980 cell->set_src_attribute(src); \
1983 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1984 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1985 add ## _func(name, sig1, sig2, sig3, src); \
1988 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1989 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) { \
1990 RTLIL::Cell *cell = addCell(name, _type); \
1991 cell->setPort("\\" #_P1, sig1); \
1992 cell->setPort("\\" #_P2, sig2); \
1993 cell->setPort("\\" #_P3, sig3); \
1994 cell->setPort("\\" #_P4, sig4); \
1995 cell->set_src_attribute(src); \
1998 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1999 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2000 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2003 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2004 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) { \
2005 RTLIL::Cell *cell = addCell(name, _type); \
2006 cell->setPort("\\" #_P1, sig1); \
2007 cell->setPort("\\" #_P2, sig2); \
2008 cell->setPort("\\" #_P3, sig3); \
2009 cell->setPort("\\" #_P4, sig4); \
2010 cell->setPort("\\" #_P5, sig5); \
2011 cell->set_src_attribute(src); \
2014 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
2015 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2016 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2019 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2020 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2021 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2022 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2023 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2024 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2025 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2026 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2027 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2028 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2029 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2030 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2031 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2032 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2033 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2034 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2040 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
)
2042 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2043 cell
->parameters
[ID(A_SIGNED
)] = a_signed
;
2044 cell
->parameters
[ID(B_SIGNED
)] = b_signed
;
2045 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2046 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2047 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2048 cell
->setPort(ID::A
, sig_a
);
2049 cell
->setPort(ID::B
, sig_b
);
2050 cell
->setPort(ID::Y
, sig_y
);
2051 cell
->set_src_attribute(src
);
2055 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
2057 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2058 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2059 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2060 cell
->parameters
[ID(OFFSET
)] = offset
;
2061 cell
->setPort(ID::A
, sig_a
);
2062 cell
->setPort(ID::Y
, sig_y
);
2063 cell
->set_src_attribute(src
);
2067 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2069 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2070 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2071 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2072 cell
->setPort(ID::A
, sig_a
);
2073 cell
->setPort(ID::B
, sig_b
);
2074 cell
->setPort(ID::Y
, sig_y
);
2075 cell
->set_src_attribute(src
);
2079 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
2081 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2082 cell
->parameters
[ID(LUT
)] = lut
;
2083 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2084 cell
->setPort(ID::A
, sig_a
);
2085 cell
->setPort(ID::Y
, sig_y
);
2086 cell
->set_src_attribute(src
);
2090 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2092 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2093 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2094 cell
->setPort(ID::A
, sig_a
);
2095 cell
->setPort(ID(EN
), sig_en
);
2096 cell
->setPort(ID::Y
, sig_y
);
2097 cell
->set_src_attribute(src
);
2101 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2103 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2104 cell
->setPort(ID::A
, sig_a
);
2105 cell
->setPort(ID(EN
), sig_en
);
2106 cell
->set_src_attribute(src
);
2110 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2112 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2113 cell
->setPort(ID::A
, sig_a
);
2114 cell
->setPort(ID(EN
), sig_en
);
2115 cell
->set_src_attribute(src
);
2119 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2121 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2122 cell
->setPort(ID::A
, sig_a
);
2123 cell
->setPort(ID(EN
), sig_en
);
2124 cell
->set_src_attribute(src
);
2128 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2130 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2131 cell
->setPort(ID::A
, sig_a
);
2132 cell
->setPort(ID(EN
), sig_en
);
2133 cell
->set_src_attribute(src
);
2137 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2139 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2140 cell
->setPort(ID::A
, sig_a
);
2141 cell
->setPort(ID(EN
), sig_en
);
2142 cell
->set_src_attribute(src
);
2146 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2148 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2149 cell
->setPort(ID::A
, sig_a
);
2150 cell
->setPort(ID::B
, sig_b
);
2151 cell
->setPort(ID::Y
, sig_y
);
2152 cell
->set_src_attribute(src
);
2156 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
)
2158 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2159 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2160 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2161 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2162 cell
->setPort(ID(SET
), sig_set
);
2163 cell
->setPort(ID(CLR
), sig_clr
);
2164 cell
->setPort(ID(Q
), sig_q
);
2165 cell
->set_src_attribute(src
);
2169 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2171 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2172 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2173 cell
->setPort(ID(D
), sig_d
);
2174 cell
->setPort(ID(Q
), sig_q
);
2175 cell
->set_src_attribute(src
);
2179 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
)
2181 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2182 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2183 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2184 cell
->setPort(ID(CLK
), sig_clk
);
2185 cell
->setPort(ID(D
), sig_d
);
2186 cell
->setPort(ID(Q
), sig_q
);
2187 cell
->set_src_attribute(src
);
2191 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
)
2193 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2194 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2195 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2196 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2197 cell
->setPort(ID(CLK
), sig_clk
);
2198 cell
->setPort(ID(EN
), sig_en
);
2199 cell
->setPort(ID(D
), sig_d
);
2200 cell
->setPort(ID(Q
), sig_q
);
2201 cell
->set_src_attribute(src
);
2205 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2206 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2208 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2209 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2210 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2211 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2212 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2213 cell
->setPort(ID(CLK
), sig_clk
);
2214 cell
->setPort(ID(SET
), sig_set
);
2215 cell
->setPort(ID(CLR
), sig_clr
);
2216 cell
->setPort(ID(D
), sig_d
);
2217 cell
->setPort(ID(Q
), sig_q
);
2218 cell
->set_src_attribute(src
);
2222 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2223 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2225 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2226 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2227 cell
->parameters
[ID(ARST_POLARITY
)] = arst_polarity
;
2228 cell
->parameters
[ID(ARST_VALUE
)] = arst_value
;
2229 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2230 cell
->setPort(ID(CLK
), sig_clk
);
2231 cell
->setPort(ID(ARST
), sig_arst
);
2232 cell
->setPort(ID(D
), sig_d
);
2233 cell
->setPort(ID(Q
), sig_q
);
2234 cell
->set_src_attribute(src
);
2238 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
)
2240 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2241 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2242 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2243 cell
->setPort(ID(EN
), sig_en
);
2244 cell
->setPort(ID(D
), sig_d
);
2245 cell
->setPort(ID(Q
), sig_q
);
2246 cell
->set_src_attribute(src
);
2250 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2251 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2253 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2254 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2255 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2256 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2257 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2258 cell
->setPort(ID(EN
), sig_en
);
2259 cell
->setPort(ID(SET
), sig_set
);
2260 cell
->setPort(ID(CLR
), sig_clr
);
2261 cell
->setPort(ID(D
), sig_d
);
2262 cell
->setPort(ID(Q
), sig_q
);
2263 cell
->set_src_attribute(src
);
2267 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2269 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
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::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, const std::string
&src
)
2278 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2279 cell
->setPort(ID(C
), sig_clk
);
2280 cell
->setPort(ID(D
), sig_d
);
2281 cell
->setPort(ID(Q
), sig_q
);
2282 cell
->set_src_attribute(src
);
2286 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
)
2288 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2289 cell
->setPort(ID(C
), sig_clk
);
2290 cell
->setPort(ID(E
), sig_en
);
2291 cell
->setPort(ID(D
), sig_d
);
2292 cell
->setPort(ID(Q
), sig_q
);
2293 cell
->set_src_attribute(src
);
2297 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2298 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2300 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2301 cell
->setPort(ID(C
), sig_clk
);
2302 cell
->setPort(ID(S
), sig_set
);
2303 cell
->setPort(ID(R
), sig_clr
);
2304 cell
->setPort(ID(D
), sig_d
);
2305 cell
->setPort(ID(Q
), sig_q
);
2306 cell
->set_src_attribute(src
);
2310 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2311 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2313 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2314 cell
->setPort(ID(C
), sig_clk
);
2315 cell
->setPort(ID(R
), sig_arst
);
2316 cell
->setPort(ID(D
), sig_d
);
2317 cell
->setPort(ID(Q
), sig_q
);
2318 cell
->set_src_attribute(src
);
2322 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
)
2324 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2325 cell
->setPort(ID(E
), sig_en
);
2326 cell
->setPort(ID(D
), sig_d
);
2327 cell
->setPort(ID(Q
), sig_q
);
2328 cell
->set_src_attribute(src
);
2332 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2333 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2335 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2336 cell
->setPort(ID(E
), sig_en
);
2337 cell
->setPort(ID(S
), sig_set
);
2338 cell
->setPort(ID(R
), sig_clr
);
2339 cell
->setPort(ID(D
), sig_d
);
2340 cell
->setPort(ID(Q
), sig_q
);
2341 cell
->set_src_attribute(src
);
2345 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2347 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2348 Cell
*cell
= addCell(name
, ID($anyconst
));
2349 cell
->setParam(ID(WIDTH
), width
);
2350 cell
->setPort(ID::Y
, sig
);
2351 cell
->set_src_attribute(src
);
2355 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2357 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2358 Cell
*cell
= addCell(name
, ID($anyseq
));
2359 cell
->setParam(ID(WIDTH
), width
);
2360 cell
->setPort(ID::Y
, sig
);
2361 cell
->set_src_attribute(src
);
2365 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2367 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2368 Cell
*cell
= addCell(name
, ID($allconst
));
2369 cell
->setParam(ID(WIDTH
), width
);
2370 cell
->setPort(ID::Y
, sig
);
2371 cell
->set_src_attribute(src
);
2375 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2377 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2378 Cell
*cell
= addCell(name
, ID($allseq
));
2379 cell
->setParam(ID(WIDTH
), width
);
2380 cell
->setPort(ID::Y
, sig
);
2381 cell
->set_src_attribute(src
);
2385 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2387 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2388 Cell
*cell
= addCell(name
, ID($initstate
));
2389 cell
->setPort(ID::Y
, sig
);
2390 cell
->set_src_attribute(src
);
2396 static unsigned int hashidx_count
= 123456789;
2397 hashidx_count
= mkhash_xorshift(hashidx_count
);
2398 hashidx_
= hashidx_count
;
2405 port_output
= false;
2409 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2413 RTLIL::Wire::~Wire()
2416 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2421 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2422 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2428 RTLIL::Memory::Memory()
2430 static unsigned int hashidx_count
= 123456789;
2431 hashidx_count
= mkhash_xorshift(hashidx_count
);
2432 hashidx_
= hashidx_count
;
2438 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2442 RTLIL::Cell::Cell() : module(nullptr)
2444 static unsigned int hashidx_count
= 123456789;
2445 hashidx_count
= mkhash_xorshift(hashidx_count
);
2446 hashidx_
= hashidx_count
;
2448 // log("#memtrace# %p\n", this);
2452 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2456 RTLIL::Cell::~Cell()
2459 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2464 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2465 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2471 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2473 return connections_
.count(portname
) != 0;
2476 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2478 RTLIL::SigSpec signal
;
2479 auto conn_it
= connections_
.find(portname
);
2481 if (conn_it
!= connections_
.end())
2483 for (auto mon
: module
->monitors
)
2484 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2487 for (auto mon
: module
->design
->monitors
)
2488 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2491 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2492 log_backtrace("-X- ", yosys_xtrace
-1);
2495 connections_
.erase(conn_it
);
2499 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2501 auto conn_it
= connections_
.find(portname
);
2503 if (conn_it
== connections_
.end()) {
2504 connections_
[portname
] = RTLIL::SigSpec();
2505 conn_it
= connections_
.find(portname
);
2506 log_assert(conn_it
!= connections_
.end());
2508 if (conn_it
->second
== signal
)
2511 for (auto mon
: module
->monitors
)
2512 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2515 for (auto mon
: module
->design
->monitors
)
2516 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2519 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2520 log_backtrace("-X- ", yosys_xtrace
-1);
2523 conn_it
->second
= signal
;
2526 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2528 return connections_
.at(portname
);
2531 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2533 return connections_
;
2536 bool RTLIL::Cell::known() const
2538 if (yosys_celltypes
.cell_known(type
))
2540 if (module
&& module
->design
&& module
->design
->module(type
))
2545 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2547 if (yosys_celltypes
.cell_known(type
))
2548 return yosys_celltypes
.cell_input(type
, portname
);
2549 if (module
&& module
->design
) {
2550 RTLIL::Module
*m
= module
->design
->module(type
);
2551 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2552 return w
&& w
->port_input
;
2557 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2559 if (yosys_celltypes
.cell_known(type
))
2560 return yosys_celltypes
.cell_output(type
, portname
);
2561 if (module
&& module
->design
) {
2562 RTLIL::Module
*m
= module
->design
->module(type
);
2563 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2564 return w
&& w
->port_output
;
2569 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2571 return parameters
.count(paramname
) != 0;
2574 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2576 parameters
.erase(paramname
);
2579 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2581 parameters
[paramname
] = value
;
2584 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2586 return parameters
.at(paramname
);
2589 void RTLIL::Cell::sort()
2591 connections_
.sort(sort_by_id_str());
2592 parameters
.sort(sort_by_id_str());
2593 attributes
.sort(sort_by_id_str());
2596 void RTLIL::Cell::check()
2599 InternalCellChecker
checker(NULL
, this);
2604 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2606 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
2607 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
2610 if (type
== ID($mux
) || type
== ID($pmux
)) {
2611 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2612 if (type
== ID($pmux
))
2613 parameters
[ID(S_WIDTH
)] = GetSize(connections_
[ID(S
)]);
2618 if (type
== ID($lut
) || type
== ID($sop
)) {
2619 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::A
]);
2623 if (type
== ID($fa
)) {
2624 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2628 if (type
== ID($lcu
)) {
2629 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(CO
)]);
2633 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
2635 if (connections_
.count(ID::A
)) {
2636 if (signedness_ab
) {
2638 parameters
[ID(A_SIGNED
)] = true;
2639 else if (parameters
.count(ID(A_SIGNED
)) == 0)
2640 parameters
[ID(A_SIGNED
)] = false;
2642 parameters
[ID(A_WIDTH
)] = GetSize(connections_
[ID::A
]);
2645 if (connections_
.count(ID::B
)) {
2646 if (signedness_ab
) {
2648 parameters
[ID(B_SIGNED
)] = true;
2649 else if (parameters
.count(ID(B_SIGNED
)) == 0)
2650 parameters
[ID(B_SIGNED
)] = false;
2652 parameters
[ID(B_WIDTH
)] = GetSize(connections_
[ID::B
]);
2655 if (connections_
.count(ID::Y
))
2656 parameters
[ID(Y_WIDTH
)] = GetSize(connections_
[ID::Y
]);
2658 if (connections_
.count(ID(Q
)))
2659 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(Q
)]);
2664 RTLIL::SigChunk::SigChunk()
2671 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2675 width
= GetSize(data
);
2679 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2681 log_assert(wire
!= nullptr);
2683 this->width
= wire
->width
;
2687 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2689 log_assert(wire
!= nullptr);
2691 this->width
= width
;
2692 this->offset
= offset
;
2695 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2698 data
= RTLIL::Const(str
).bits
;
2699 width
= GetSize(data
);
2703 RTLIL::SigChunk::SigChunk(int val
, int width
)
2706 data
= RTLIL::Const(val
, width
).bits
;
2707 this->width
= GetSize(data
);
2711 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2714 data
= RTLIL::Const(bit
, width
).bits
;
2715 this->width
= GetSize(data
);
2719 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2724 data
= RTLIL::Const(bit
.data
).bits
;
2726 offset
= bit
.offset
;
2730 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2732 wire
= sigchunk
.wire
;
2733 data
= sigchunk
.data
;
2734 width
= sigchunk
.width
;
2735 offset
= sigchunk
.offset
;
2738 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2740 RTLIL::SigChunk ret
;
2743 ret
.offset
= this->offset
+ offset
;
2746 for (int i
= 0; i
< length
; i
++)
2747 ret
.data
.push_back(data
[offset
+i
]);
2753 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2755 if (wire
&& other
.wire
)
2756 if (wire
->name
!= other
.wire
->name
)
2757 return wire
->name
< other
.wire
->name
;
2759 if (wire
!= other
.wire
)
2760 return wire
< other
.wire
;
2762 if (offset
!= other
.offset
)
2763 return offset
< other
.offset
;
2765 if (width
!= other
.width
)
2766 return width
< other
.width
;
2768 return data
< other
.data
;
2771 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2773 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2776 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2783 RTLIL::SigSpec::SigSpec()
2789 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2794 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2796 cover("kernel.rtlil.sigspec.init.list");
2801 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2802 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2806 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2808 cover("kernel.rtlil.sigspec.assign");
2810 width_
= other
.width_
;
2811 hash_
= other
.hash_
;
2812 chunks_
= other
.chunks_
;
2815 if (!other
.bits_
.empty())
2817 RTLIL::SigChunk
*last
= NULL
;
2818 int last_end_offset
= 0;
2820 for (auto &bit
: other
.bits_
) {
2821 if (last
&& bit
.wire
== last
->wire
) {
2822 if (bit
.wire
== NULL
) {
2823 last
->data
.push_back(bit
.data
);
2826 } else if (last_end_offset
== bit
.offset
) {
2832 chunks_
.push_back(bit
);
2833 last
= &chunks_
.back();
2834 last_end_offset
= bit
.offset
+ 1;
2843 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2845 cover("kernel.rtlil.sigspec.init.const");
2847 chunks_
.push_back(RTLIL::SigChunk(value
));
2848 width_
= chunks_
.back().width
;
2853 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2855 cover("kernel.rtlil.sigspec.init.chunk");
2857 chunks_
.push_back(chunk
);
2858 width_
= chunks_
.back().width
;
2863 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2865 cover("kernel.rtlil.sigspec.init.wire");
2867 chunks_
.push_back(RTLIL::SigChunk(wire
));
2868 width_
= chunks_
.back().width
;
2873 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2875 cover("kernel.rtlil.sigspec.init.wire_part");
2877 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2878 width_
= chunks_
.back().width
;
2883 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2885 cover("kernel.rtlil.sigspec.init.str");
2887 chunks_
.push_back(RTLIL::SigChunk(str
));
2888 width_
= chunks_
.back().width
;
2893 RTLIL::SigSpec::SigSpec(int val
, int width
)
2895 cover("kernel.rtlil.sigspec.init.int");
2897 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2903 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2905 cover("kernel.rtlil.sigspec.init.state");
2907 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2913 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2915 cover("kernel.rtlil.sigspec.init.bit");
2917 if (bit
.wire
== NULL
)
2918 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2920 for (int i
= 0; i
< width
; i
++)
2921 chunks_
.push_back(bit
);
2927 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2929 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2933 for (auto &c
: chunks
)
2938 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2940 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2944 for (auto &bit
: bits
)
2949 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2951 cover("kernel.rtlil.sigspec.init.pool_bits");
2955 for (auto &bit
: bits
)
2960 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2962 cover("kernel.rtlil.sigspec.init.stdset_bits");
2966 for (auto &bit
: bits
)
2971 RTLIL::SigSpec::SigSpec(bool bit
)
2973 cover("kernel.rtlil.sigspec.init.bool");
2981 void RTLIL::SigSpec::pack() const
2983 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2985 if (that
->bits_
.empty())
2988 cover("kernel.rtlil.sigspec.convert.pack");
2989 log_assert(that
->chunks_
.empty());
2991 std::vector
<RTLIL::SigBit
> old_bits
;
2992 old_bits
.swap(that
->bits_
);
2994 RTLIL::SigChunk
*last
= NULL
;
2995 int last_end_offset
= 0;
2997 for (auto &bit
: old_bits
) {
2998 if (last
&& bit
.wire
== last
->wire
) {
2999 if (bit
.wire
== NULL
) {
3000 last
->data
.push_back(bit
.data
);
3003 } else if (last_end_offset
== bit
.offset
) {
3009 that
->chunks_
.push_back(bit
);
3010 last
= &that
->chunks_
.back();
3011 last_end_offset
= bit
.offset
+ 1;
3017 void RTLIL::SigSpec::unpack() const
3019 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3021 if (that
->chunks_
.empty())
3024 cover("kernel.rtlil.sigspec.convert.unpack");
3025 log_assert(that
->bits_
.empty());
3027 that
->bits_
.reserve(that
->width_
);
3028 for (auto &c
: that
->chunks_
)
3029 for (int i
= 0; i
< c
.width
; i
++)
3030 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
3032 that
->chunks_
.clear();
3036 void RTLIL::SigSpec::updhash() const
3038 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3040 if (that
->hash_
!= 0)
3043 cover("kernel.rtlil.sigspec.hash");
3046 that
->hash_
= mkhash_init
;
3047 for (auto &c
: that
->chunks_
)
3048 if (c
.wire
== NULL
) {
3049 for (auto &v
: c
.data
)
3050 that
->hash_
= mkhash(that
->hash_
, v
);
3052 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3053 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3054 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3057 if (that
->hash_
== 0)
3061 void RTLIL::SigSpec::sort()
3064 cover("kernel.rtlil.sigspec.sort");
3065 std::sort(bits_
.begin(), bits_
.end());
3068 void RTLIL::SigSpec::sort_and_unify()
3071 cover("kernel.rtlil.sigspec.sort_and_unify");
3073 // A copy of the bits vector is used to prevent duplicating the logic from
3074 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3075 // that isn't showing up as significant in profiles.
3076 std::vector
<SigBit
> unique_bits
= bits_
;
3077 std::sort(unique_bits
.begin(), unique_bits
.end());
3078 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3079 unique_bits
.erase(last
, unique_bits
.end());
3081 *this = unique_bits
;
3084 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3086 replace(pattern
, with
, this);
3089 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3091 log_assert(other
!= NULL
);
3092 log_assert(width_
== other
->width_
);
3093 log_assert(pattern
.width_
== with
.width_
);
3100 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3101 if (pattern
.bits_
[i
].wire
!= NULL
) {
3102 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3103 if (bits_
[j
] == pattern
.bits_
[i
]) {
3104 other
->bits_
[j
] = with
.bits_
[i
];
3113 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3115 replace(rules
, this);
3118 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3120 cover("kernel.rtlil.sigspec.replace_dict");
3122 log_assert(other
!= NULL
);
3123 log_assert(width_
== other
->width_
);
3125 if (rules
.empty()) return;
3129 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3130 auto it
= rules
.find(bits_
[i
]);
3131 if (it
!= rules
.end())
3132 other
->bits_
[i
] = it
->second
;
3138 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3140 replace(rules
, this);
3143 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3145 cover("kernel.rtlil.sigspec.replace_map");
3147 log_assert(other
!= NULL
);
3148 log_assert(width_
== other
->width_
);
3150 if (rules
.empty()) return;
3154 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3155 auto it
= rules
.find(bits_
[i
]);
3156 if (it
!= rules
.end())
3157 other
->bits_
[i
] = it
->second
;
3163 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3165 remove2(pattern
, NULL
);
3168 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3170 RTLIL::SigSpec tmp
= *this;
3171 tmp
.remove2(pattern
, other
);
3174 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3177 cover("kernel.rtlil.sigspec.remove_other");
3179 cover("kernel.rtlil.sigspec.remove");
3182 if (other
!= NULL
) {
3183 log_assert(width_
== other
->width_
);
3187 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3189 if (bits_
[i
].wire
== NULL
) continue;
3191 for (auto &pattern_chunk
: pattern
.chunks())
3192 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3193 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3194 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3195 bits_
.erase(bits_
.begin() + i
);
3197 if (other
!= NULL
) {
3198 other
->bits_
.erase(other
->bits_
.begin() + i
);
3208 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3210 remove2(pattern
, NULL
);
3213 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3215 RTLIL::SigSpec tmp
= *this;
3216 tmp
.remove2(pattern
, other
);
3219 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3222 cover("kernel.rtlil.sigspec.remove_other");
3224 cover("kernel.rtlil.sigspec.remove");
3228 if (other
!= NULL
) {
3229 log_assert(width_
== other
->width_
);
3233 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3234 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3235 bits_
.erase(bits_
.begin() + i
);
3237 if (other
!= NULL
) {
3238 other
->bits_
.erase(other
->bits_
.begin() + i
);
3247 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3250 cover("kernel.rtlil.sigspec.remove_other");
3252 cover("kernel.rtlil.sigspec.remove");
3256 if (other
!= NULL
) {
3257 log_assert(width_
== other
->width_
);
3261 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3262 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3263 bits_
.erase(bits_
.begin() + i
);
3265 if (other
!= NULL
) {
3266 other
->bits_
.erase(other
->bits_
.begin() + i
);
3275 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3278 cover("kernel.rtlil.sigspec.extract_other");
3280 cover("kernel.rtlil.sigspec.extract");
3282 log_assert(other
== NULL
|| width_
== other
->width_
);
3285 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3287 for (auto& pattern_chunk
: pattern
.chunks()) {
3289 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3290 for (int i
= 0; i
< width_
; i
++)
3291 if (bits_match
[i
].wire
&&
3292 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3293 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3294 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3295 ret
.append_bit(bits_other
[i
]);
3297 for (int i
= 0; i
< width_
; i
++)
3298 if (bits_match
[i
].wire
&&
3299 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3300 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3301 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3302 ret
.append_bit(bits_match
[i
]);
3310 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3313 cover("kernel.rtlil.sigspec.extract_other");
3315 cover("kernel.rtlil.sigspec.extract");
3317 log_assert(other
== NULL
|| width_
== other
->width_
);
3319 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3323 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3324 for (int i
= 0; i
< width_
; i
++)
3325 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3326 ret
.append_bit(bits_other
[i
]);
3328 for (int i
= 0; i
< width_
; i
++)
3329 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3330 ret
.append_bit(bits_match
[i
]);
3337 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3339 cover("kernel.rtlil.sigspec.replace_pos");
3344 log_assert(offset
>= 0);
3345 log_assert(with
.width_
>= 0);
3346 log_assert(offset
+with
.width_
<= width_
);
3348 for (int i
= 0; i
< with
.width_
; i
++)
3349 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3354 void RTLIL::SigSpec::remove_const()
3358 cover("kernel.rtlil.sigspec.remove_const.packed");
3360 std::vector
<RTLIL::SigChunk
> new_chunks
;
3361 new_chunks
.reserve(GetSize(chunks_
));
3364 for (auto &chunk
: chunks_
)
3365 if (chunk
.wire
!= NULL
) {
3366 new_chunks
.push_back(chunk
);
3367 width_
+= chunk
.width
;
3370 chunks_
.swap(new_chunks
);
3374 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3376 std::vector
<RTLIL::SigBit
> new_bits
;
3377 new_bits
.reserve(width_
);
3379 for (auto &bit
: bits_
)
3380 if (bit
.wire
!= NULL
)
3381 new_bits
.push_back(bit
);
3383 bits_
.swap(new_bits
);
3384 width_
= bits_
.size();
3390 void RTLIL::SigSpec::remove(int offset
, int length
)
3392 cover("kernel.rtlil.sigspec.remove_pos");
3396 log_assert(offset
>= 0);
3397 log_assert(length
>= 0);
3398 log_assert(offset
+ length
<= width_
);
3400 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3401 width_
= bits_
.size();
3406 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3409 cover("kernel.rtlil.sigspec.extract_pos");
3410 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3413 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3415 if (signal
.width_
== 0)
3423 cover("kernel.rtlil.sigspec.append");
3425 if (packed() != signal
.packed()) {
3431 for (auto &other_c
: signal
.chunks_
)
3433 auto &my_last_c
= chunks_
.back();
3434 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3435 auto &this_data
= my_last_c
.data
;
3436 auto &other_data
= other_c
.data
;
3437 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3438 my_last_c
.width
+= other_c
.width
;
3440 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3441 my_last_c
.width
+= other_c
.width
;
3443 chunks_
.push_back(other_c
);
3446 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3448 width_
+= signal
.width_
;
3452 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3456 cover("kernel.rtlil.sigspec.append_bit.packed");
3458 if (chunks_
.size() == 0)
3459 chunks_
.push_back(bit
);
3461 if (bit
.wire
== NULL
)
3462 if (chunks_
.back().wire
== NULL
) {
3463 chunks_
.back().data
.push_back(bit
.data
);
3464 chunks_
.back().width
++;
3466 chunks_
.push_back(bit
);
3468 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3469 chunks_
.back().width
++;
3471 chunks_
.push_back(bit
);
3475 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3476 bits_
.push_back(bit
);
3483 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3485 cover("kernel.rtlil.sigspec.extend_u0");
3490 remove(width
, width_
- width
);
3492 if (width_
< width
) {
3493 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3495 padding
= RTLIL::State::S0
;
3496 while (width_
< width
)
3502 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3504 cover("kernel.rtlil.sigspec.repeat");
3507 for (int i
= 0; i
< num
; i
++)
3513 void RTLIL::SigSpec::check() const
3517 cover("kernel.rtlil.sigspec.check.skip");
3521 cover("kernel.rtlil.sigspec.check.packed");
3524 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3525 const RTLIL::SigChunk chunk
= chunks_
[i
];
3526 if (chunk
.wire
== NULL
) {
3528 log_assert(chunks_
[i
-1].wire
!= NULL
);
3529 log_assert(chunk
.offset
== 0);
3530 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3532 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3533 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3534 log_assert(chunk
.offset
>= 0);
3535 log_assert(chunk
.width
>= 0);
3536 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3537 log_assert(chunk
.data
.size() == 0);
3541 log_assert(w
== width_
);
3542 log_assert(bits_
.empty());
3546 cover("kernel.rtlil.sigspec.check.unpacked");
3548 log_assert(width_
== GetSize(bits_
));
3549 log_assert(chunks_
.empty());
3554 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3556 cover("kernel.rtlil.sigspec.comp_lt");
3561 if (width_
!= other
.width_
)
3562 return width_
< other
.width_
;
3567 if (chunks_
.size() != other
.chunks_
.size())
3568 return chunks_
.size() < other
.chunks_
.size();
3573 if (hash_
!= other
.hash_
)
3574 return hash_
< other
.hash_
;
3576 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3577 if (chunks_
[i
] != other
.chunks_
[i
]) {
3578 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3579 return chunks_
[i
] < other
.chunks_
[i
];
3582 cover("kernel.rtlil.sigspec.comp_lt.equal");
3586 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3588 cover("kernel.rtlil.sigspec.comp_eq");
3593 if (width_
!= other
.width_
)
3596 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
3597 // since the RHS will contain one SigChunk of width 0 causing
3598 // the size check below to fail
3605 if (chunks_
.size() != other
.chunks_
.size())
3611 if (hash_
!= other
.hash_
)
3614 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3615 if (chunks_
[i
] != other
.chunks_
[i
]) {
3616 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3620 cover("kernel.rtlil.sigspec.comp_eq.equal");
3624 bool RTLIL::SigSpec::is_wire() const
3626 cover("kernel.rtlil.sigspec.is_wire");
3629 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3632 bool RTLIL::SigSpec::is_chunk() const
3634 cover("kernel.rtlil.sigspec.is_chunk");
3637 return GetSize(chunks_
) == 1;
3640 bool RTLIL::SigSpec::is_fully_const() const
3642 cover("kernel.rtlil.sigspec.is_fully_const");
3645 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3646 if (it
->width
> 0 && it
->wire
!= NULL
)
3651 bool RTLIL::SigSpec::is_fully_zero() const
3653 cover("kernel.rtlil.sigspec.is_fully_zero");
3656 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3657 if (it
->width
> 0 && it
->wire
!= NULL
)
3659 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3660 if (it
->data
[i
] != RTLIL::State::S0
)
3666 bool RTLIL::SigSpec::is_fully_ones() const
3668 cover("kernel.rtlil.sigspec.is_fully_ones");
3671 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3672 if (it
->width
> 0 && it
->wire
!= NULL
)
3674 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3675 if (it
->data
[i
] != RTLIL::State::S1
)
3681 bool RTLIL::SigSpec::is_fully_def() const
3683 cover("kernel.rtlil.sigspec.is_fully_def");
3686 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3687 if (it
->width
> 0 && it
->wire
!= NULL
)
3689 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3690 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3696 bool RTLIL::SigSpec::is_fully_undef() const
3698 cover("kernel.rtlil.sigspec.is_fully_undef");
3701 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3702 if (it
->width
> 0 && it
->wire
!= NULL
)
3704 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3705 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3711 bool RTLIL::SigSpec::has_const() const
3713 cover("kernel.rtlil.sigspec.has_const");
3716 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3717 if (it
->width
> 0 && it
->wire
== NULL
)
3722 bool RTLIL::SigSpec::has_marked_bits() const
3724 cover("kernel.rtlil.sigspec.has_marked_bits");
3727 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3728 if (it
->width
> 0 && it
->wire
== NULL
) {
3729 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3730 if (it
->data
[i
] == RTLIL::State::Sm
)
3736 bool RTLIL::SigSpec::as_bool() const
3738 cover("kernel.rtlil.sigspec.as_bool");
3741 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3743 return RTLIL::Const(chunks_
[0].data
).as_bool();
3747 int RTLIL::SigSpec::as_int(bool is_signed
) const
3749 cover("kernel.rtlil.sigspec.as_int");
3752 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3754 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3758 std::string
RTLIL::SigSpec::as_string() const
3760 cover("kernel.rtlil.sigspec.as_string");
3764 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3765 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3766 if (chunk
.wire
!= NULL
)
3767 for (int j
= 0; j
< chunk
.width
; j
++)
3770 str
+= RTLIL::Const(chunk
.data
).as_string();
3775 RTLIL::Const
RTLIL::SigSpec::as_const() const
3777 cover("kernel.rtlil.sigspec.as_const");
3780 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3782 return chunks_
[0].data
;
3783 return RTLIL::Const();
3786 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3788 cover("kernel.rtlil.sigspec.as_wire");
3791 log_assert(is_wire());
3792 return chunks_
[0].wire
;
3795 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3797 cover("kernel.rtlil.sigspec.as_chunk");
3800 log_assert(is_chunk());
3804 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3806 cover("kernel.rtlil.sigspec.as_bit");
3808 log_assert(width_
== 1);
3810 return RTLIL::SigBit(*chunks_
.begin());
3815 bool RTLIL::SigSpec::match(std::string pattern
) const
3817 cover("kernel.rtlil.sigspec.match");
3820 std::string str
= as_string();
3821 log_assert(pattern
.size() == str
.size());
3823 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3824 if (pattern
[i
] == ' ')
3826 if (pattern
[i
] == '*') {
3827 if (str
[i
] != 'z' && str
[i
] != 'x')
3831 if (pattern
[i
] != str
[i
])
3838 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3840 cover("kernel.rtlil.sigspec.to_sigbit_set");
3843 std::set
<RTLIL::SigBit
> sigbits
;
3844 for (auto &c
: chunks_
)
3845 for (int i
= 0; i
< c
.width
; i
++)
3846 sigbits
.insert(RTLIL::SigBit(c
, i
));
3850 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3852 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3855 pool
<RTLIL::SigBit
> sigbits
;
3856 for (auto &c
: chunks_
)
3857 for (int i
= 0; i
< c
.width
; i
++)
3858 sigbits
.insert(RTLIL::SigBit(c
, i
));
3862 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3864 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3870 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3872 cover("kernel.rtlil.sigspec.to_sigbit_map");
3877 log_assert(width_
== other
.width_
);
3879 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3880 for (int i
= 0; i
< width_
; i
++)
3881 new_map
[bits_
[i
]] = other
.bits_
[i
];
3886 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3888 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3893 log_assert(width_
== other
.width_
);
3895 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3896 for (int i
= 0; i
< width_
; i
++)
3897 new_map
[bits_
[i
]] = other
.bits_
[i
];
3902 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3904 size_t start
= 0, end
= 0;
3905 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3906 tokens
.push_back(text
.substr(start
, end
- start
));
3909 tokens
.push_back(text
.substr(start
));
3912 static int sigspec_parse_get_dummy_line_num()
3917 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3919 cover("kernel.rtlil.sigspec.parse");
3921 AST::current_filename
= "input";
3922 AST::use_internal_line_num();
3923 AST::set_line_num(0);
3925 std::vector
<std::string
> tokens
;
3926 sigspec_parse_split(tokens
, str
, ',');
3928 sig
= RTLIL::SigSpec();
3929 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3931 std::string netname
= tokens
[tokidx
];
3932 std::string indices
;
3934 if (netname
.size() == 0)
3937 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3938 cover("kernel.rtlil.sigspec.parse.const");
3939 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3940 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3943 sig
.append(RTLIL::Const(ast
->bits
));
3951 cover("kernel.rtlil.sigspec.parse.net");
3953 if (netname
[0] != '$' && netname
[0] != '\\')
3954 netname
= "\\" + netname
;
3956 if (module
->wires_
.count(netname
) == 0) {
3957 size_t indices_pos
= netname
.size()-1;
3958 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3961 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3962 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3964 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3966 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3967 indices
= netname
.substr(indices_pos
);
3968 netname
= netname
.substr(0, indices_pos
);
3973 if (module
->wires_
.count(netname
) == 0)
3976 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3977 if (!indices
.empty()) {
3978 std::vector
<std::string
> index_tokens
;
3979 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3980 if (index_tokens
.size() == 1) {
3981 cover("kernel.rtlil.sigspec.parse.bit_sel");
3982 int a
= atoi(index_tokens
.at(0).c_str());
3983 if (a
< 0 || a
>= wire
->width
)
3985 sig
.append(RTLIL::SigSpec(wire
, a
));
3987 cover("kernel.rtlil.sigspec.parse.part_sel");
3988 int a
= atoi(index_tokens
.at(0).c_str());
3989 int b
= atoi(index_tokens
.at(1).c_str());
3994 if (a
< 0 || a
>= wire
->width
)
3996 if (b
< 0 || b
>= wire
->width
)
3998 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4007 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4009 if (str
.empty() || str
[0] != '@')
4010 return parse(sig
, module
, str
);
4012 cover("kernel.rtlil.sigspec.parse.sel");
4014 str
= RTLIL::escape_id(str
.substr(1));
4015 if (design
->selection_vars
.count(str
) == 0)
4018 sig
= RTLIL::SigSpec();
4019 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4020 for (auto &it
: module
->wires_
)
4021 if (sel
.selected_member(module
->name
, it
.first
))
4022 sig
.append(it
.second
);
4027 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4030 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4031 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4036 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4037 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4041 if (lhs
.chunks_
.size() == 1) {
4042 char *p
= (char*)str
.c_str(), *endptr
;
4043 long int val
= strtol(p
, &endptr
, 10);
4044 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4045 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4046 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4051 return parse(sig
, module
, str
);
4054 RTLIL::CaseRule::~CaseRule()
4056 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4060 bool RTLIL::CaseRule::empty() const
4062 return actions
.empty() && switches
.empty();
4065 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4067 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4068 new_caserule
->compare
= compare
;
4069 new_caserule
->actions
= actions
;
4070 for (auto &it
: switches
)
4071 new_caserule
->switches
.push_back(it
->clone());
4072 return new_caserule
;
4075 RTLIL::SwitchRule::~SwitchRule()
4077 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4081 bool RTLIL::SwitchRule::empty() const
4083 return cases
.empty();
4086 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4088 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4089 new_switchrule
->signal
= signal
;
4090 new_switchrule
->attributes
= attributes
;
4091 for (auto &it
: cases
)
4092 new_switchrule
->cases
.push_back(it
->clone());
4093 return new_switchrule
;
4097 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4099 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4100 new_syncrule
->type
= type
;
4101 new_syncrule
->signal
= signal
;
4102 new_syncrule
->actions
= actions
;
4103 return new_syncrule
;
4106 RTLIL::Process::~Process()
4108 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4112 RTLIL::Process
*RTLIL::Process::clone() const
4114 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4116 new_proc
->name
= name
;
4117 new_proc
->attributes
= attributes
;
4119 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4120 new_proc
->root_case
= *rc_ptr
;
4121 rc_ptr
->switches
.clear();
4124 for (auto &it
: syncs
)
4125 new_proc
->syncs
.push_back(it
->clone());
4131 RTLIL::Memory::~Memory()
4133 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4135 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4136 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4138 return &all_memorys
;