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
;
49 dict
<std::string
, std::string
> RTLIL::constpad
;
53 flags
= RTLIL::CONST_FLAG_NONE
;
56 RTLIL::Const::Const(std::string str
)
58 flags
= RTLIL::CONST_FLAG_STRING
;
59 for (int i
= str
.size()-1; i
>= 0; i
--) {
60 unsigned char ch
= str
[i
];
61 for (int j
= 0; j
< 8; j
++) {
62 bits
.push_back((ch
& 1) != 0 ? State::S1
: State::S0
);
68 RTLIL::Const::Const(int val
, int width
)
70 flags
= RTLIL::CONST_FLAG_NONE
;
71 for (int i
= 0; i
< width
; i
++) {
72 bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
77 RTLIL::Const::Const(RTLIL::State bit
, int width
)
79 flags
= RTLIL::CONST_FLAG_NONE
;
80 for (int i
= 0; i
< width
; i
++)
84 RTLIL::Const::Const(const std::vector
<bool> &bits
)
86 flags
= RTLIL::CONST_FLAG_NONE
;
88 this->bits
.push_back(b
? State::S1
: State::S0
);
91 RTLIL::Const::Const(const RTLIL::Const
&c
)
95 this->bits
.push_back(b
);
98 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
100 if (bits
.size() != other
.bits
.size())
101 return bits
.size() < other
.bits
.size();
102 for (size_t i
= 0; i
< bits
.size(); i
++)
103 if (bits
[i
] != other
.bits
[i
])
104 return bits
[i
] < other
.bits
[i
];
108 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
110 return bits
== other
.bits
;
113 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
115 return bits
!= other
.bits
;
118 bool RTLIL::Const::as_bool() const
120 for (size_t i
= 0; i
< bits
.size(); i
++)
121 if (bits
[i
] == State::S1
)
126 int RTLIL::Const::as_int(bool is_signed
) const
129 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
130 if (bits
[i
] == State::S1
)
132 if (is_signed
&& bits
.back() == State::S1
)
133 for (size_t i
= bits
.size(); i
< 32; i
++)
138 std::string
RTLIL::Const::as_string() const
141 for (size_t i
= bits
.size(); i
> 0; i
--)
143 case S0
: ret
+= "0"; break;
144 case S1
: ret
+= "1"; break;
145 case Sx
: ret
+= "x"; break;
146 case Sz
: ret
+= "z"; break;
147 case Sa
: ret
+= "-"; break;
148 case Sm
: ret
+= "m"; break;
153 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
156 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
158 case '0': c
.bits
.push_back(State::S0
); break;
159 case '1': c
.bits
.push_back(State::S1
); break;
160 case 'x': c
.bits
.push_back(State::Sx
); break;
161 case 'z': c
.bits
.push_back(State::Sz
); break;
162 case 'm': c
.bits
.push_back(State::Sm
); break;
163 default: c
.bits
.push_back(State::Sa
);
168 std::string
RTLIL::Const::decode_string() const
171 std::vector
<char> string_chars
;
172 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
174 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
175 if (bits
[i
+ j
] == RTLIL::State::S1
)
178 string_chars
.push_back(ch
);
180 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
181 string
+= string_chars
[i
];
185 bool RTLIL::Const::is_fully_zero() const
187 cover("kernel.rtlil.const.is_fully_zero");
189 for (auto bit
: bits
)
190 if (bit
!= RTLIL::State::S0
)
196 bool RTLIL::Const::is_fully_ones() const
198 cover("kernel.rtlil.const.is_fully_ones");
200 for (auto bit
: bits
)
201 if (bit
!= RTLIL::State::S1
)
207 bool RTLIL::Const::is_fully_def() const
209 cover("kernel.rtlil.const.is_fully_def");
211 for (auto bit
: bits
)
212 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
218 bool RTLIL::Const::is_fully_undef() const
220 cover("kernel.rtlil.const.is_fully_undef");
222 for (auto bit
: bits
)
223 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
229 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
232 attributes
[id
] = RTLIL::Const(1);
234 const auto it
= attributes
.find(id
);
235 if (it
!= attributes
.end())
236 attributes
.erase(it
);
240 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
242 const auto it
= attributes
.find(id
);
243 if (it
== attributes
.end())
245 return it
->second
.as_bool();
248 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
251 for (auto &s
: data
) {
252 if (!attrval
.empty())
256 attributes
[id
] = RTLIL::Const(attrval
);
259 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
261 pool
<string
> union_data
= get_strpool_attribute(id
);
262 union_data
.insert(data
.begin(), data
.end());
263 if (!union_data
.empty())
264 set_strpool_attribute(id
, union_data
);
267 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
270 if (attributes
.count(id
) != 0)
271 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
276 void RTLIL::AttrObject::set_src_attribute(const std::string
&src
)
279 attributes
.erase(ID(src
));
281 attributes
[ID(src
)] = src
;
284 std::string
RTLIL::AttrObject::get_src_attribute() const
287 if (attributes
.count(ID(src
)))
288 src
= attributes
.at(ID(src
)).decode_string();
292 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
296 if (selected_modules
.count(mod_name
) > 0)
298 if (selected_members
.count(mod_name
) > 0)
303 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
307 if (selected_modules
.count(mod_name
) > 0)
312 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
316 if (selected_modules
.count(mod_name
) > 0)
318 if (selected_members
.count(mod_name
) > 0)
319 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
324 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
326 if (full_selection
) {
327 selected_modules
.clear();
328 selected_members
.clear();
332 std::vector
<RTLIL::IdString
> del_list
, add_list
;
335 for (auto mod_name
: selected_modules
) {
336 if (design
->modules_
.count(mod_name
) == 0)
337 del_list
.push_back(mod_name
);
338 selected_members
.erase(mod_name
);
340 for (auto mod_name
: del_list
)
341 selected_modules
.erase(mod_name
);
344 for (auto &it
: selected_members
)
345 if (design
->modules_
.count(it
.first
) == 0)
346 del_list
.push_back(it
.first
);
347 for (auto mod_name
: del_list
)
348 selected_members
.erase(mod_name
);
350 for (auto &it
: selected_members
) {
352 for (auto memb_name
: it
.second
)
353 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
354 del_list
.push_back(memb_name
);
355 for (auto memb_name
: del_list
)
356 it
.second
.erase(memb_name
);
361 for (auto &it
: selected_members
)
362 if (it
.second
.size() == 0)
363 del_list
.push_back(it
.first
);
364 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
365 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
366 add_list
.push_back(it
.first
);
367 for (auto mod_name
: del_list
)
368 selected_members
.erase(mod_name
);
369 for (auto mod_name
: add_list
) {
370 selected_members
.erase(mod_name
);
371 selected_modules
.insert(mod_name
);
374 if (selected_modules
.size() == design
->modules_
.size()) {
375 full_selection
= true;
376 selected_modules
.clear();
377 selected_members
.clear();
381 RTLIL::Design::Design()
383 static unsigned int hashidx_count
= 123456789;
384 hashidx_count
= mkhash_xorshift(hashidx_count
);
385 hashidx_
= hashidx_count
;
387 refcount_modules_
= 0;
388 selection_stack
.push_back(RTLIL::Selection());
391 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
395 RTLIL::Design::~Design()
397 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
399 for (auto n
: verilog_packages
)
401 for (auto n
: verilog_globals
)
404 RTLIL::Design::get_all_designs()->erase(hashidx_
);
409 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
410 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
416 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
418 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
421 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
423 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
426 RTLIL::Module
*RTLIL::Design::top_module()
428 RTLIL::Module
*module
= nullptr;
429 int module_count
= 0;
431 for (auto mod
: selected_modules()) {
432 if (mod
->get_bool_attribute(ID(top
)))
438 return module_count
== 1 ? module
: nullptr;
441 void RTLIL::Design::add(RTLIL::Module
*module
)
443 log_assert(modules_
.count(module
->name
) == 0);
444 log_assert(refcount_modules_
== 0);
445 modules_
[module
->name
] = module
;
446 module
->design
= this;
448 for (auto mon
: monitors
)
449 mon
->notify_module_add(module
);
452 log("#X# New Module: %s\n", log_id(module
));
453 log_backtrace("-X- ", yosys_xtrace
-1);
457 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
459 log_assert(modules_
.count(name
) == 0);
460 log_assert(refcount_modules_
== 0);
462 RTLIL::Module
*module
= new RTLIL::Module
;
463 modules_
[name
] = module
;
464 module
->design
= this;
467 for (auto mon
: monitors
)
468 mon
->notify_module_add(module
);
471 log("#X# New Module: %s\n", log_id(module
));
472 log_backtrace("-X- ", yosys_xtrace
-1);
478 void RTLIL::Design::scratchpad_unset(std::string varname
)
480 scratchpad
.erase(varname
);
483 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
485 scratchpad
[varname
] = stringf("%d", value
);
488 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
490 scratchpad
[varname
] = value
? "true" : "false";
493 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
495 scratchpad
[varname
] = value
;
498 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
500 if (scratchpad
.count(varname
) == 0)
501 return default_value
;
503 std::string str
= scratchpad
.at(varname
);
505 if (str
== "0" || str
== "false")
508 if (str
== "1" || str
== "true")
511 char *endptr
= nullptr;
512 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
513 return *endptr
? default_value
: parsed_value
;
516 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
518 if (scratchpad
.count(varname
) == 0)
519 return default_value
;
521 std::string str
= scratchpad
.at(varname
);
523 if (str
== "0" || str
== "false")
526 if (str
== "1" || str
== "true")
529 return default_value
;
532 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
534 if (scratchpad
.count(varname
) == 0)
535 return default_value
;
536 return scratchpad
.at(varname
);
539 void RTLIL::Design::remove(RTLIL::Module
*module
)
541 for (auto mon
: monitors
)
542 mon
->notify_module_del(module
);
545 log("#X# Remove Module: %s\n", log_id(module
));
546 log_backtrace("-X- ", yosys_xtrace
-1);
549 log_assert(modules_
.at(module
->name
) == module
);
550 modules_
.erase(module
->name
);
554 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
556 modules_
.erase(module
->name
);
557 module
->name
= new_name
;
561 void RTLIL::Design::sort()
564 modules_
.sort(sort_by_id_str());
565 for (auto &it
: modules_
)
569 void RTLIL::Design::check()
572 for (auto &it
: modules_
) {
573 log_assert(this == it
.second
->design
);
574 log_assert(it
.first
== it
.second
->name
);
575 log_assert(!it
.first
.empty());
581 void RTLIL::Design::optimize()
583 for (auto &it
: modules_
)
584 it
.second
->optimize();
585 for (auto &it
: selection_stack
)
587 for (auto &it
: selection_vars
)
588 it
.second
.optimize(this);
591 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
593 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
595 if (selection_stack
.size() == 0)
597 return selection_stack
.back().selected_module(mod_name
);
600 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
602 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
604 if (selection_stack
.size() == 0)
606 return selection_stack
.back().selected_whole_module(mod_name
);
609 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
611 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
613 if (selection_stack
.size() == 0)
615 return selection_stack
.back().selected_member(mod_name
, memb_name
);
618 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
620 return selected_module(mod
->name
);
623 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
625 return selected_whole_module(mod
->name
);
628 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
630 std::vector
<RTLIL::Module
*> result
;
631 result
.reserve(modules_
.size());
632 for (auto &it
: modules_
)
633 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
634 result
.push_back(it
.second
);
638 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
640 std::vector
<RTLIL::Module
*> result
;
641 result
.reserve(modules_
.size());
642 for (auto &it
: modules_
)
643 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
644 result
.push_back(it
.second
);
648 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
650 std::vector
<RTLIL::Module
*> result
;
651 result
.reserve(modules_
.size());
652 for (auto &it
: modules_
)
653 if (it
.second
->get_blackbox_attribute())
655 else if (selected_whole_module(it
.first
))
656 result
.push_back(it
.second
);
657 else if (selected_module(it
.first
))
658 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
662 RTLIL::Module::Module()
664 static unsigned int hashidx_count
= 123456789;
665 hashidx_count
= mkhash_xorshift(hashidx_count
);
666 hashidx_
= hashidx_count
;
673 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
677 RTLIL::Module::~Module()
679 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
681 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
683 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
685 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
688 RTLIL::Module::get_all_modules()->erase(hashidx_
);
693 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
694 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
700 void RTLIL::Module::makeblackbox()
702 pool
<RTLIL::Wire
*> delwires
;
704 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
705 if (!it
->second
->port_input
&& !it
->second
->port_output
)
706 delwires
.insert(it
->second
);
708 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
712 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
716 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
721 set_bool_attribute(ID::blackbox
);
724 void RTLIL::Module::reprocess_module(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Module
*>)
726 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
729 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, bool mayfail
)
732 return RTLIL::IdString();
733 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
737 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, dict
<RTLIL::IdString
, RTLIL::Module
*>, dict
<RTLIL::IdString
, RTLIL::IdString
>, bool mayfail
)
740 return RTLIL::IdString();
741 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
744 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
746 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
751 struct InternalCellChecker
753 RTLIL::Module
*module
;
755 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
757 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
759 void error(int linenr
)
761 std::stringstream buf
;
762 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
764 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
765 module
? module
->name
.c_str() : "", module
? "." : "",
766 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
769 int param(RTLIL::IdString name
)
771 if (cell
->parameters
.count(name
) == 0)
773 expected_params
.insert(name
);
774 return cell
->parameters
.at(name
).as_int();
777 int param_bool(RTLIL::IdString name
)
780 if (cell
->parameters
.at(name
).bits
.size() > 32)
782 if (v
!= 0 && v
!= 1)
787 int param_bool(RTLIL::IdString name
, bool expected
)
789 int v
= param_bool(name
);
795 void param_bits(RTLIL::IdString name
, int width
)
798 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
802 void port(RTLIL::IdString name
, int width
)
804 if (!cell
->hasPort(name
))
806 if (cell
->getPort(name
).size() != width
)
808 expected_ports
.insert(name
);
811 void check_expected(bool check_matched_sign
= true)
813 for (auto ¶
: cell
->parameters
)
814 if (expected_params
.count(para
.first
) == 0)
816 for (auto &conn
: cell
->connections())
817 if (expected_ports
.count(conn
.first
) == 0)
820 if (expected_params
.count(ID(A_SIGNED
)) != 0 && expected_params
.count(ID(B_SIGNED
)) && check_matched_sign
) {
821 bool a_is_signed
= param(ID(A_SIGNED
)) != 0;
822 bool b_is_signed
= param(ID(B_SIGNED
)) != 0;
823 if (a_is_signed
!= b_is_signed
)
828 void check_gate(const char *ports
)
830 if (cell
->parameters
.size() != 0)
833 for (const char *p
= ports
; *p
; p
++) {
834 char portname
[3] = { '\\', *p
, 0 };
835 if (!cell
->hasPort(portname
))
837 if (cell
->getPort(portname
).size() != 1)
841 for (auto &conn
: cell
->connections()) {
842 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
844 if (strchr(ports
, conn
.first
[1]) == NULL
)
851 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
852 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
855 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
856 param_bool(ID(A_SIGNED
));
857 port(ID::A
, param(ID(A_WIDTH
)));
858 port(ID::Y
, param(ID(Y_WIDTH
)));
863 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
864 param_bool(ID(A_SIGNED
));
865 param_bool(ID(B_SIGNED
));
866 port(ID::A
, param(ID(A_WIDTH
)));
867 port(ID::B
, param(ID(B_WIDTH
)));
868 port(ID::Y
, param(ID(Y_WIDTH
)));
873 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
874 param_bool(ID(A_SIGNED
));
875 port(ID::A
, param(ID(A_WIDTH
)));
876 port(ID::Y
, param(ID(Y_WIDTH
)));
881 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
882 param_bool(ID(A_SIGNED
));
883 param_bool(ID(B_SIGNED
), /*expected=*/false);
884 port(ID::A
, param(ID(A_WIDTH
)));
885 port(ID::B
, param(ID(B_WIDTH
)));
886 port(ID::Y
, param(ID(Y_WIDTH
)));
887 check_expected(/*check_matched_sign=*/false);
891 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
892 param_bool(ID(A_SIGNED
));
893 param_bool(ID(B_SIGNED
));
894 port(ID::A
, param(ID(A_WIDTH
)));
895 port(ID::B
, param(ID(B_WIDTH
)));
896 port(ID::Y
, param(ID(Y_WIDTH
)));
897 check_expected(/*check_matched_sign=*/false);
901 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
902 param_bool(ID(A_SIGNED
));
903 param_bool(ID(B_SIGNED
));
904 port(ID::A
, param(ID(A_WIDTH
)));
905 port(ID::B
, param(ID(B_WIDTH
)));
906 port(ID::Y
, param(ID(Y_WIDTH
)));
911 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($pow
))) {
912 param_bool(ID(A_SIGNED
));
913 param_bool(ID(B_SIGNED
));
914 port(ID::A
, param(ID(A_WIDTH
)));
915 port(ID::B
, param(ID(B_WIDTH
)));
916 port(ID::Y
, param(ID(Y_WIDTH
)));
917 check_expected(cell
->type
!= ID($pow
));
921 if (cell
->type
== ID($fa
)) {
922 port(ID::A
, param(ID(WIDTH
)));
923 port(ID::B
, param(ID(WIDTH
)));
924 port(ID(C
), param(ID(WIDTH
)));
925 port(ID(X
), param(ID(WIDTH
)));
926 port(ID::Y
, param(ID(WIDTH
)));
931 if (cell
->type
== ID($lcu
)) {
932 port(ID(P
), param(ID(WIDTH
)));
933 port(ID(G
), param(ID(WIDTH
)));
935 port(ID(CO
), param(ID(WIDTH
)));
940 if (cell
->type
== ID($alu
)) {
941 param_bool(ID(A_SIGNED
));
942 param_bool(ID(B_SIGNED
));
943 port(ID::A
, param(ID(A_WIDTH
)));
944 port(ID::B
, param(ID(B_WIDTH
)));
947 port(ID(X
), param(ID(Y_WIDTH
)));
948 port(ID::Y
, param(ID(Y_WIDTH
)));
949 port(ID(CO
), param(ID(Y_WIDTH
)));
954 if (cell
->type
== ID($macc
)) {
956 param(ID(CONFIG_WIDTH
));
957 port(ID::A
, param(ID(A_WIDTH
)));
958 port(ID::B
, param(ID(B_WIDTH
)));
959 port(ID::Y
, param(ID(Y_WIDTH
)));
961 Macc().from_cell(cell
);
965 if (cell
->type
== ID($logic_not
)) {
966 param_bool(ID(A_SIGNED
));
967 port(ID::A
, param(ID(A_WIDTH
)));
968 port(ID::Y
, param(ID(Y_WIDTH
)));
973 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
974 param_bool(ID(A_SIGNED
));
975 param_bool(ID(B_SIGNED
));
976 port(ID::A
, param(ID(A_WIDTH
)));
977 port(ID::B
, param(ID(B_WIDTH
)));
978 port(ID::Y
, param(ID(Y_WIDTH
)));
979 check_expected(/*check_matched_sign=*/false);
983 if (cell
->type
== ID($slice
)) {
985 port(ID::A
, param(ID(A_WIDTH
)));
986 port(ID::Y
, param(ID(Y_WIDTH
)));
987 if (param(ID(OFFSET
)) + param(ID(Y_WIDTH
)) > param(ID(A_WIDTH
)))
993 if (cell
->type
== ID($concat
)) {
994 port(ID::A
, param(ID(A_WIDTH
)));
995 port(ID::B
, param(ID(B_WIDTH
)));
996 port(ID::Y
, param(ID(A_WIDTH
)) + param(ID(B_WIDTH
)));
1001 if (cell
->type
== ID($mux
)) {
1002 port(ID::A
, param(ID(WIDTH
)));
1003 port(ID::B
, param(ID(WIDTH
)));
1005 port(ID::Y
, param(ID(WIDTH
)));
1010 if (cell
->type
== ID($pmux
)) {
1011 port(ID::A
, param(ID(WIDTH
)));
1012 port(ID::B
, param(ID(WIDTH
)) * param(ID(S_WIDTH
)));
1013 port(ID(S
), param(ID(S_WIDTH
)));
1014 port(ID::Y
, param(ID(WIDTH
)));
1019 if (cell
->type
== ID($lut
)) {
1021 port(ID::A
, param(ID(WIDTH
)));
1027 if (cell
->type
== ID($sop
)) {
1030 port(ID::A
, param(ID(WIDTH
)));
1036 if (cell
->type
== ID($sr
)) {
1037 param_bool(ID(SET_POLARITY
));
1038 param_bool(ID(CLR_POLARITY
));
1039 port(ID(SET
), param(ID(WIDTH
)));
1040 port(ID(CLR
), param(ID(WIDTH
)));
1041 port(ID(Q
), param(ID(WIDTH
)));
1046 if (cell
->type
== ID($ff
)) {
1047 port(ID(D
), param(ID(WIDTH
)));
1048 port(ID(Q
), param(ID(WIDTH
)));
1053 if (cell
->type
== ID($dff
)) {
1054 param_bool(ID(CLK_POLARITY
));
1056 port(ID(D
), param(ID(WIDTH
)));
1057 port(ID(Q
), param(ID(WIDTH
)));
1062 if (cell
->type
== ID($dffe
)) {
1063 param_bool(ID(CLK_POLARITY
));
1064 param_bool(ID(EN_POLARITY
));
1067 port(ID(D
), param(ID(WIDTH
)));
1068 port(ID(Q
), param(ID(WIDTH
)));
1073 if (cell
->type
== ID($dffsr
)) {
1074 param_bool(ID(CLK_POLARITY
));
1075 param_bool(ID(SET_POLARITY
));
1076 param_bool(ID(CLR_POLARITY
));
1078 port(ID(SET
), param(ID(WIDTH
)));
1079 port(ID(CLR
), param(ID(WIDTH
)));
1080 port(ID(D
), param(ID(WIDTH
)));
1081 port(ID(Q
), param(ID(WIDTH
)));
1086 if (cell
->type
== ID($adff
)) {
1087 param_bool(ID(CLK_POLARITY
));
1088 param_bool(ID(ARST_POLARITY
));
1089 param_bits(ID(ARST_VALUE
), param(ID(WIDTH
)));
1092 port(ID(D
), param(ID(WIDTH
)));
1093 port(ID(Q
), param(ID(WIDTH
)));
1098 if (cell
->type
== ID($dlatch
)) {
1099 param_bool(ID(EN_POLARITY
));
1101 port(ID(D
), param(ID(WIDTH
)));
1102 port(ID(Q
), param(ID(WIDTH
)));
1107 if (cell
->type
== ID($dlatchsr
)) {
1108 param_bool(ID(EN_POLARITY
));
1109 param_bool(ID(SET_POLARITY
));
1110 param_bool(ID(CLR_POLARITY
));
1112 port(ID(SET
), param(ID(WIDTH
)));
1113 port(ID(CLR
), param(ID(WIDTH
)));
1114 port(ID(D
), param(ID(WIDTH
)));
1115 port(ID(Q
), param(ID(WIDTH
)));
1120 if (cell
->type
== ID($fsm
)) {
1122 param_bool(ID(CLK_POLARITY
));
1123 param_bool(ID(ARST_POLARITY
));
1124 param(ID(STATE_BITS
));
1125 param(ID(STATE_NUM
));
1126 param(ID(STATE_NUM_LOG2
));
1127 param(ID(STATE_RST
));
1128 param_bits(ID(STATE_TABLE
), param(ID(STATE_BITS
)) * param(ID(STATE_NUM
)));
1129 param(ID(TRANS_NUM
));
1130 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
))));
1133 port(ID(CTRL_IN
), param(ID(CTRL_IN_WIDTH
)));
1134 port(ID(CTRL_OUT
), param(ID(CTRL_OUT_WIDTH
)));
1139 if (cell
->type
== ID($memrd
)) {
1141 param_bool(ID(CLK_ENABLE
));
1142 param_bool(ID(CLK_POLARITY
));
1143 param_bool(ID(TRANSPARENT
));
1146 port(ID(ADDR
), param(ID(ABITS
)));
1147 port(ID(DATA
), param(ID(WIDTH
)));
1152 if (cell
->type
== ID($memwr
)) {
1154 param_bool(ID(CLK_ENABLE
));
1155 param_bool(ID(CLK_POLARITY
));
1156 param(ID(PRIORITY
));
1158 port(ID(EN
), param(ID(WIDTH
)));
1159 port(ID(ADDR
), param(ID(ABITS
)));
1160 port(ID(DATA
), param(ID(WIDTH
)));
1165 if (cell
->type
== ID($meminit
)) {
1167 param(ID(PRIORITY
));
1168 port(ID(ADDR
), param(ID(ABITS
)));
1169 port(ID(DATA
), param(ID(WIDTH
)) * param(ID(WORDS
)));
1174 if (cell
->type
== ID($mem
)) {
1179 param_bits(ID(RD_CLK_ENABLE
), max(1, param(ID(RD_PORTS
))));
1180 param_bits(ID(RD_CLK_POLARITY
), max(1, param(ID(RD_PORTS
))));
1181 param_bits(ID(RD_TRANSPARENT
), max(1, param(ID(RD_PORTS
))));
1182 param_bits(ID(WR_CLK_ENABLE
), max(1, param(ID(WR_PORTS
))));
1183 param_bits(ID(WR_CLK_POLARITY
), max(1, param(ID(WR_PORTS
))));
1184 port(ID(RD_CLK
), param(ID(RD_PORTS
)));
1185 port(ID(RD_EN
), param(ID(RD_PORTS
)));
1186 port(ID(RD_ADDR
), param(ID(RD_PORTS
)) * param(ID(ABITS
)));
1187 port(ID(RD_DATA
), param(ID(RD_PORTS
)) * param(ID(WIDTH
)));
1188 port(ID(WR_CLK
), param(ID(WR_PORTS
)));
1189 port(ID(WR_EN
), param(ID(WR_PORTS
)) * param(ID(WIDTH
)));
1190 port(ID(WR_ADDR
), param(ID(WR_PORTS
)) * param(ID(ABITS
)));
1191 port(ID(WR_DATA
), param(ID(WR_PORTS
)) * param(ID(WIDTH
)));
1196 if (cell
->type
== ID($tribuf
)) {
1197 port(ID::A
, param(ID(WIDTH
)));
1198 port(ID::Y
, param(ID(WIDTH
)));
1204 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1211 if (cell
->type
== ID($initstate
)) {
1217 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1218 port(ID::Y
, param(ID(WIDTH
)));
1223 if (cell
->type
== ID($equiv
)) {
1231 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1232 param_bool(ID(FULL
));
1233 param_bool(ID(SRC_DST_PEN
));
1234 param_bool(ID(SRC_DST_POL
));
1235 param(ID(T_RISE_MIN
));
1236 param(ID(T_RISE_TYP
));
1237 param(ID(T_RISE_MAX
));
1238 param(ID(T_FALL_MIN
));
1239 param(ID(T_FALL_TYP
));
1240 param(ID(T_FALL_MAX
));
1242 port(ID(SRC
), param(ID(SRC_WIDTH
)));
1243 port(ID(DST
), param(ID(DST_WIDTH
)));
1244 if (cell
->type
== ID($specify3
)) {
1245 param_bool(ID(EDGE_EN
));
1246 param_bool(ID(EDGE_POL
));
1247 param_bool(ID(DAT_DST_PEN
));
1248 param_bool(ID(DAT_DST_POL
));
1249 port(ID(DAT
), param(ID(DST_WIDTH
)));
1255 if (cell
->type
== ID($specrule
)) {
1257 param_bool(ID(SRC_PEN
));
1258 param_bool(ID(SRC_POL
));
1259 param_bool(ID(DST_PEN
));
1260 param_bool(ID(DST_POL
));
1261 param(ID(T_LIMIT_MIN
));
1262 param(ID(T_LIMIT_TYP
));
1263 param(ID(T_LIMIT_MAX
));
1264 param(ID(T_LIMIT2_MIN
));
1265 param(ID(T_LIMIT2_TYP
));
1266 param(ID(T_LIMIT2_MAX
));
1267 port(ID(SRC_EN
), 1);
1268 port(ID(DST_EN
), 1);
1269 port(ID(SRC
), param(ID(SRC_WIDTH
)));
1270 port(ID(DST
), param(ID(DST_WIDTH
)));
1275 if (cell
->type
== ID($_BUF_
)) { check_gate("AY"); return; }
1276 if (cell
->type
== ID($_NOT_
)) { check_gate("AY"); return; }
1277 if (cell
->type
== ID($_AND_
)) { check_gate("ABY"); return; }
1278 if (cell
->type
== ID($_NAND_
)) { check_gate("ABY"); return; }
1279 if (cell
->type
== ID($_OR_
)) { check_gate("ABY"); return; }
1280 if (cell
->type
== ID($_NOR_
)) { check_gate("ABY"); return; }
1281 if (cell
->type
== ID($_XOR_
)) { check_gate("ABY"); return; }
1282 if (cell
->type
== ID($_XNOR_
)) { check_gate("ABY"); return; }
1283 if (cell
->type
== ID($_ANDNOT_
)) { check_gate("ABY"); return; }
1284 if (cell
->type
== ID($_ORNOT_
)) { check_gate("ABY"); return; }
1285 if (cell
->type
== ID($_MUX_
)) { check_gate("ABSY"); return; }
1286 if (cell
->type
== ID($_NMUX_
)) { check_gate("ABSY"); return; }
1287 if (cell
->type
== ID($_AOI3_
)) { check_gate("ABCY"); return; }
1288 if (cell
->type
== ID($_OAI3_
)) { check_gate("ABCY"); return; }
1289 if (cell
->type
== ID($_AOI4_
)) { check_gate("ABCDY"); return; }
1290 if (cell
->type
== ID($_OAI4_
)) { check_gate("ABCDY"); return; }
1292 if (cell
->type
== ID($_TBUF_
)) { check_gate("AYE"); return; }
1294 if (cell
->type
== ID($_MUX4_
)) { check_gate("ABCDSTY"); return; }
1295 if (cell
->type
== ID($_MUX8_
)) { check_gate("ABCDEFGHSTUY"); return; }
1296 if (cell
->type
== ID($_MUX16_
)) { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1298 if (cell
->type
== ID($_SR_NN_
)) { check_gate("SRQ"); return; }
1299 if (cell
->type
== ID($_SR_NP_
)) { check_gate("SRQ"); return; }
1300 if (cell
->type
== ID($_SR_PN_
)) { check_gate("SRQ"); return; }
1301 if (cell
->type
== ID($_SR_PP_
)) { check_gate("SRQ"); return; }
1303 if (cell
->type
== ID($_FF_
)) { check_gate("DQ"); return; }
1304 if (cell
->type
== ID($_DFF_N_
)) { check_gate("DQC"); return; }
1305 if (cell
->type
== ID($_DFF_P_
)) { check_gate("DQC"); return; }
1307 if (cell
->type
== ID($_DFFE_NN_
)) { check_gate("DQCE"); return; }
1308 if (cell
->type
== ID($_DFFE_NP_
)) { check_gate("DQCE"); return; }
1309 if (cell
->type
== ID($_DFFE_PN_
)) { check_gate("DQCE"); return; }
1310 if (cell
->type
== ID($_DFFE_PP_
)) { check_gate("DQCE"); return; }
1312 if (cell
->type
== ID($_DFF_NN0_
)) { check_gate("DQCR"); return; }
1313 if (cell
->type
== ID($_DFF_NN1_
)) { check_gate("DQCR"); return; }
1314 if (cell
->type
== ID($_DFF_NP0_
)) { check_gate("DQCR"); return; }
1315 if (cell
->type
== ID($_DFF_NP1_
)) { check_gate("DQCR"); return; }
1316 if (cell
->type
== ID($_DFF_PN0_
)) { check_gate("DQCR"); return; }
1317 if (cell
->type
== ID($_DFF_PN1_
)) { check_gate("DQCR"); return; }
1318 if (cell
->type
== ID($_DFF_PP0_
)) { check_gate("DQCR"); return; }
1319 if (cell
->type
== ID($_DFF_PP1_
)) { check_gate("DQCR"); return; }
1321 if (cell
->type
== ID($_DFFSR_NNN_
)) { check_gate("CSRDQ"); return; }
1322 if (cell
->type
== ID($_DFFSR_NNP_
)) { check_gate("CSRDQ"); return; }
1323 if (cell
->type
== ID($_DFFSR_NPN_
)) { check_gate("CSRDQ"); return; }
1324 if (cell
->type
== ID($_DFFSR_NPP_
)) { check_gate("CSRDQ"); return; }
1325 if (cell
->type
== ID($_DFFSR_PNN_
)) { check_gate("CSRDQ"); return; }
1326 if (cell
->type
== ID($_DFFSR_PNP_
)) { check_gate("CSRDQ"); return; }
1327 if (cell
->type
== ID($_DFFSR_PPN_
)) { check_gate("CSRDQ"); return; }
1328 if (cell
->type
== ID($_DFFSR_PPP_
)) { check_gate("CSRDQ"); return; }
1330 if (cell
->type
== ID($_DLATCH_N_
)) { check_gate("EDQ"); return; }
1331 if (cell
->type
== ID($_DLATCH_P_
)) { check_gate("EDQ"); return; }
1333 if (cell
->type
== ID($_DLATCHSR_NNN_
)) { check_gate("ESRDQ"); return; }
1334 if (cell
->type
== ID($_DLATCHSR_NNP_
)) { check_gate("ESRDQ"); return; }
1335 if (cell
->type
== ID($_DLATCHSR_NPN_
)) { check_gate("ESRDQ"); return; }
1336 if (cell
->type
== ID($_DLATCHSR_NPP_
)) { check_gate("ESRDQ"); return; }
1337 if (cell
->type
== ID($_DLATCHSR_PNN_
)) { check_gate("ESRDQ"); return; }
1338 if (cell
->type
== ID($_DLATCHSR_PNP_
)) { check_gate("ESRDQ"); return; }
1339 if (cell
->type
== ID($_DLATCHSR_PPN_
)) { check_gate("ESRDQ"); return; }
1340 if (cell
->type
== ID($_DLATCHSR_PPP_
)) { check_gate("ESRDQ"); return; }
1348 void RTLIL::Module::sort()
1350 wires_
.sort(sort_by_id_str());
1351 cells_
.sort(sort_by_id_str());
1352 avail_parameters
.sort(sort_by_id_str());
1353 memories
.sort(sort_by_id_str());
1354 processes
.sort(sort_by_id_str());
1355 for (auto &it
: cells_
)
1357 for (auto &it
: wires_
)
1358 it
.second
->attributes
.sort(sort_by_id_str());
1359 for (auto &it
: memories
)
1360 it
.second
->attributes
.sort(sort_by_id_str());
1363 void RTLIL::Module::check()
1366 std::vector
<bool> ports_declared
;
1367 for (auto &it
: wires_
) {
1368 log_assert(this == it
.second
->module
);
1369 log_assert(it
.first
== it
.second
->name
);
1370 log_assert(!it
.first
.empty());
1371 log_assert(it
.second
->width
>= 0);
1372 log_assert(it
.second
->port_id
>= 0);
1373 for (auto &it2
: it
.second
->attributes
)
1374 log_assert(!it2
.first
.empty());
1375 if (it
.second
->port_id
) {
1376 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1377 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1378 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1379 if (GetSize(ports_declared
) < it
.second
->port_id
)
1380 ports_declared
.resize(it
.second
->port_id
);
1381 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1382 ports_declared
[it
.second
->port_id
-1] = true;
1384 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1386 for (auto port_declared
: ports_declared
)
1387 log_assert(port_declared
== true);
1388 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1390 for (auto &it
: memories
) {
1391 log_assert(it
.first
== it
.second
->name
);
1392 log_assert(!it
.first
.empty());
1393 log_assert(it
.second
->width
>= 0);
1394 log_assert(it
.second
->size
>= 0);
1395 for (auto &it2
: it
.second
->attributes
)
1396 log_assert(!it2
.first
.empty());
1399 for (auto &it
: cells_
) {
1400 log_assert(this == it
.second
->module
);
1401 log_assert(it
.first
== it
.second
->name
);
1402 log_assert(!it
.first
.empty());
1403 log_assert(!it
.second
->type
.empty());
1404 for (auto &it2
: it
.second
->connections()) {
1405 log_assert(!it2
.first
.empty());
1408 for (auto &it2
: it
.second
->attributes
)
1409 log_assert(!it2
.first
.empty());
1410 for (auto &it2
: it
.second
->parameters
)
1411 log_assert(!it2
.first
.empty());
1412 InternalCellChecker
checker(this, it
.second
);
1416 for (auto &it
: processes
) {
1417 log_assert(it
.first
== it
.second
->name
);
1418 log_assert(!it
.first
.empty());
1419 log_assert(it
.second
->root_case
.compare
.empty());
1420 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1421 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1422 for (auto &switch_it
: all_cases
[i
]->switches
) {
1423 for (auto &case_it
: switch_it
->cases
) {
1424 for (auto &compare_it
: case_it
->compare
) {
1425 log_assert(switch_it
->signal
.size() == compare_it
.size());
1427 all_cases
.push_back(case_it
);
1431 for (auto &sync_it
: it
.second
->syncs
) {
1432 switch (sync_it
->type
) {
1438 log_assert(!sync_it
->signal
.empty());
1443 log_assert(sync_it
->signal
.empty());
1449 for (auto &it
: connections_
) {
1450 log_assert(it
.first
.size() == it
.second
.size());
1451 log_assert(!it
.first
.has_const());
1456 for (auto &it
: attributes
)
1457 log_assert(!it
.first
.empty());
1461 void RTLIL::Module::optimize()
1465 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1467 log_assert(new_mod
->refcount_wires_
== 0);
1468 log_assert(new_mod
->refcount_cells_
== 0);
1470 new_mod
->avail_parameters
= avail_parameters
;
1472 for (auto &conn
: connections_
)
1473 new_mod
->connect(conn
);
1475 for (auto &attr
: attributes
)
1476 new_mod
->attributes
[attr
.first
] = attr
.second
;
1478 for (auto &it
: wires_
)
1479 new_mod
->addWire(it
.first
, it
.second
);
1481 for (auto &it
: memories
)
1482 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1484 for (auto &it
: cells_
)
1485 new_mod
->addCell(it
.first
, it
.second
);
1487 for (auto &it
: processes
)
1488 new_mod
->processes
[it
.first
] = it
.second
->clone();
1490 struct RewriteSigSpecWorker
1493 void operator()(RTLIL::SigSpec
&sig
)
1495 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1496 for (auto &c
: chunks
)
1498 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1503 RewriteSigSpecWorker rewriteSigSpecWorker
;
1504 rewriteSigSpecWorker
.mod
= new_mod
;
1505 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1506 new_mod
->fixup_ports();
1509 RTLIL::Module
*RTLIL::Module::clone() const
1511 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1512 new_mod
->name
= name
;
1517 bool RTLIL::Module::has_memories() const
1519 return !memories
.empty();
1522 bool RTLIL::Module::has_processes() const
1524 return !processes
.empty();
1527 bool RTLIL::Module::has_memories_warn() const
1529 if (!memories
.empty())
1530 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1531 return !memories
.empty();
1534 bool RTLIL::Module::has_processes_warn() const
1536 if (!processes
.empty())
1537 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1538 return !processes
.empty();
1541 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1543 std::vector
<RTLIL::Wire
*> result
;
1544 result
.reserve(wires_
.size());
1545 for (auto &it
: wires_
)
1546 if (design
->selected(this, it
.second
))
1547 result
.push_back(it
.second
);
1551 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1553 std::vector
<RTLIL::Cell
*> result
;
1554 result
.reserve(cells_
.size());
1555 for (auto &it
: cells_
)
1556 if (design
->selected(this, it
.second
))
1557 result
.push_back(it
.second
);
1561 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1563 log_assert(!wire
->name
.empty());
1564 log_assert(count_id(wire
->name
) == 0);
1565 log_assert(refcount_wires_
== 0);
1566 wires_
[wire
->name
] = wire
;
1567 wire
->module
= this;
1570 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1572 log_assert(!cell
->name
.empty());
1573 log_assert(count_id(cell
->name
) == 0);
1574 log_assert(refcount_cells_
== 0);
1575 cells_
[cell
->name
] = cell
;
1576 cell
->module
= this;
1579 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1581 log_assert(refcount_wires_
== 0);
1583 struct DeleteWireWorker
1585 RTLIL::Module
*module
;
1586 const pool
<RTLIL::Wire
*> *wires_p
;
1588 void operator()(RTLIL::SigSpec
&sig
) {
1590 for (auto &c
: sig
.chunks_
)
1591 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1592 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1597 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1598 log_assert(GetSize(lhs
) == GetSize(rhs
));
1601 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1602 RTLIL::SigBit
&lhs_bit
= lhs
.bits_
[i
];
1603 RTLIL::SigBit
&rhs_bit
= rhs
.bits_
[i
];
1604 if ((lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
)) || (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))) {
1605 lhs_bit
= State::Sx
;
1606 rhs_bit
= State::Sx
;
1612 DeleteWireWorker delete_wire_worker
;
1613 delete_wire_worker
.module
= this;
1614 delete_wire_worker
.wires_p
= &wires
;
1615 rewrite_sigspecs2(delete_wire_worker
);
1617 for (auto &it
: wires
) {
1618 log_assert(wires_
.count(it
->name
) != 0);
1619 wires_
.erase(it
->name
);
1624 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1626 while (!cell
->connections_
.empty())
1627 cell
->unsetPort(cell
->connections_
.begin()->first
);
1629 log_assert(cells_
.count(cell
->name
) != 0);
1630 log_assert(refcount_cells_
== 0);
1631 cells_
.erase(cell
->name
);
1635 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1637 log_assert(wires_
[wire
->name
] == wire
);
1638 log_assert(refcount_wires_
== 0);
1639 wires_
.erase(wire
->name
);
1640 wire
->name
= new_name
;
1644 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1646 log_assert(cells_
[cell
->name
] == cell
);
1647 log_assert(refcount_wires_
== 0);
1648 cells_
.erase(cell
->name
);
1649 cell
->name
= new_name
;
1653 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1655 log_assert(count_id(old_name
) != 0);
1656 if (wires_
.count(old_name
))
1657 rename(wires_
.at(old_name
), new_name
);
1658 else if (cells_
.count(old_name
))
1659 rename(cells_
.at(old_name
), new_name
);
1664 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1666 log_assert(wires_
[w1
->name
] == w1
);
1667 log_assert(wires_
[w2
->name
] == w2
);
1668 log_assert(refcount_wires_
== 0);
1670 wires_
.erase(w1
->name
);
1671 wires_
.erase(w2
->name
);
1673 std::swap(w1
->name
, w2
->name
);
1675 wires_
[w1
->name
] = w1
;
1676 wires_
[w2
->name
] = w2
;
1679 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1681 log_assert(cells_
[c1
->name
] == c1
);
1682 log_assert(cells_
[c2
->name
] == c2
);
1683 log_assert(refcount_cells_
== 0);
1685 cells_
.erase(c1
->name
);
1686 cells_
.erase(c2
->name
);
1688 std::swap(c1
->name
, c2
->name
);
1690 cells_
[c1
->name
] = c1
;
1691 cells_
[c2
->name
] = c2
;
1694 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1697 return uniquify(name
, index
);
1700 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1703 if (count_id(name
) == 0)
1709 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1710 if (count_id(new_name
) == 0)
1716 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1718 if (a
->port_id
&& !b
->port_id
)
1720 if (!a
->port_id
&& b
->port_id
)
1723 if (a
->port_id
== b
->port_id
)
1724 return a
->name
< b
->name
;
1725 return a
->port_id
< b
->port_id
;
1728 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1730 for (auto mon
: monitors
)
1731 mon
->notify_connect(this, conn
);
1734 for (auto mon
: design
->monitors
)
1735 mon
->notify_connect(this, conn
);
1737 // ignore all attempts to assign constants to other constants
1738 if (conn
.first
.has_const()) {
1739 RTLIL::SigSig new_conn
;
1740 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1741 if (conn
.first
[i
].wire
) {
1742 new_conn
.first
.append(conn
.first
[i
]);
1743 new_conn
.second
.append(conn
.second
[i
]);
1745 if (GetSize(new_conn
.first
))
1751 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
));
1752 log_backtrace("-X- ", yosys_xtrace
-1);
1755 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1756 connections_
.push_back(conn
);
1759 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1761 connect(RTLIL::SigSig(lhs
, rhs
));
1764 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1766 for (auto mon
: monitors
)
1767 mon
->notify_connect(this, new_conn
);
1770 for (auto mon
: design
->monitors
)
1771 mon
->notify_connect(this, new_conn
);
1774 log("#X# New connections vector in %s:\n", log_id(this));
1775 for (auto &conn
: new_conn
)
1776 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1777 log_backtrace("-X- ", yosys_xtrace
-1);
1780 connections_
= new_conn
;
1783 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1785 return connections_
;
1788 void RTLIL::Module::fixup_ports()
1790 std::vector
<RTLIL::Wire
*> all_ports
;
1792 for (auto &w
: wires_
)
1793 if (w
.second
->port_input
|| w
.second
->port_output
)
1794 all_ports
.push_back(w
.second
);
1796 w
.second
->port_id
= 0;
1798 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1801 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1802 ports
.push_back(all_ports
[i
]->name
);
1803 all_ports
[i
]->port_id
= i
+1;
1807 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1809 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1811 wire
->width
= width
;
1816 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1818 RTLIL::Wire
*wire
= addWire(name
);
1819 wire
->width
= other
->width
;
1820 wire
->start_offset
= other
->start_offset
;
1821 wire
->port_id
= other
->port_id
;
1822 wire
->port_input
= other
->port_input
;
1823 wire
->port_output
= other
->port_output
;
1824 wire
->upto
= other
->upto
;
1825 wire
->attributes
= other
->attributes
;
1829 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1831 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1838 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1840 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1841 cell
->connections_
= other
->connections_
;
1842 cell
->parameters
= other
->parameters
;
1843 cell
->attributes
= other
->attributes
;
1847 #define DEF_METHOD(_func, _y_size, _type) \
1848 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1849 RTLIL::Cell *cell = addCell(name, _type); \
1850 cell->parameters[ID(A_SIGNED)] = is_signed; \
1851 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1852 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1853 cell->setPort(ID::A, sig_a); \
1854 cell->setPort(ID::Y, sig_y); \
1855 cell->set_src_attribute(src); \
1858 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1859 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1860 add ## _func(name, sig_a, sig_y, is_signed, src); \
1863 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
1864 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
1865 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
1866 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
1867 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
1868 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
1869 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
1870 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
1871 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
1874 #define DEF_METHOD(_func, _y_size, _type) \
1875 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) { \
1876 RTLIL::Cell *cell = addCell(name, _type); \
1877 cell->parameters[ID(A_SIGNED)] = is_signed; \
1878 cell->parameters[ID(B_SIGNED)] = is_signed; \
1879 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1880 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1881 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1882 cell->setPort(ID::A, sig_a); \
1883 cell->setPort(ID::B, sig_b); \
1884 cell->setPort(ID::Y, sig_y); \
1885 cell->set_src_attribute(src); \
1888 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1889 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1890 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1893 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
1894 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
1895 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
1896 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
1897 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
1898 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
1899 DEF_METHOD(Lt
, 1, ID($lt
))
1900 DEF_METHOD(Le
, 1, ID($le
))
1901 DEF_METHOD(Eq
, 1, ID($eq
))
1902 DEF_METHOD(Ne
, 1, ID($ne
))
1903 DEF_METHOD(Eqx
, 1, ID($eqx
))
1904 DEF_METHOD(Nex
, 1, ID($nex
))
1905 DEF_METHOD(Ge
, 1, ID($ge
))
1906 DEF_METHOD(Gt
, 1, ID($gt
))
1907 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
1908 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
1909 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
1910 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
1911 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
1912 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
1913 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
1916 #define DEF_METHOD(_func, _y_size, _type) \
1917 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) { \
1918 RTLIL::Cell *cell = addCell(name, _type); \
1919 cell->parameters[ID(A_SIGNED)] = is_signed; \
1920 cell->parameters[ID(B_SIGNED)] = false; \
1921 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1922 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1923 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1924 cell->setPort(ID::A, sig_a); \
1925 cell->setPort(ID::B, sig_b); \
1926 cell->setPort(ID::Y, sig_y); \
1927 cell->set_src_attribute(src); \
1930 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1931 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1932 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1935 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
1936 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
1937 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
1938 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
1941 #define DEF_METHOD(_func, _type, _pmux) \
1942 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) { \
1943 RTLIL::Cell *cell = addCell(name, _type); \
1944 cell->parameters[ID(WIDTH)] = sig_a.size(); \
1945 if (_pmux) cell->parameters[ID(S_WIDTH)] = sig_s.size(); \
1946 cell->setPort(ID::A, sig_a); \
1947 cell->setPort(ID::B, sig_b); \
1948 cell->setPort(ID(S), sig_s); \
1949 cell->setPort(ID::Y, sig_y); \
1950 cell->set_src_attribute(src); \
1953 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1954 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1955 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1958 DEF_METHOD(Mux
, ID($mux
), 0)
1959 DEF_METHOD(Pmux
, ID($pmux
), 1)
1962 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1963 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1964 RTLIL::Cell *cell = addCell(name, _type); \
1965 cell->setPort("\\" #_P1, sig1); \
1966 cell->setPort("\\" #_P2, sig2); \
1967 cell->set_src_attribute(src); \
1970 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1971 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1972 add ## _func(name, sig1, sig2, src); \
1975 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1976 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1977 RTLIL::Cell *cell = addCell(name, _type); \
1978 cell->setPort("\\" #_P1, sig1); \
1979 cell->setPort("\\" #_P2, sig2); \
1980 cell->setPort("\\" #_P3, sig3); \
1981 cell->set_src_attribute(src); \
1984 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1985 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1986 add ## _func(name, sig1, sig2, sig3, src); \
1989 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1990 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) { \
1991 RTLIL::Cell *cell = addCell(name, _type); \
1992 cell->setPort("\\" #_P1, sig1); \
1993 cell->setPort("\\" #_P2, sig2); \
1994 cell->setPort("\\" #_P3, sig3); \
1995 cell->setPort("\\" #_P4, sig4); \
1996 cell->set_src_attribute(src); \
1999 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
2000 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2001 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2004 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2005 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) { \
2006 RTLIL::Cell *cell = addCell(name, _type); \
2007 cell->setPort("\\" #_P1, sig1); \
2008 cell->setPort("\\" #_P2, sig2); \
2009 cell->setPort("\\" #_P3, sig3); \
2010 cell->setPort("\\" #_P4, sig4); \
2011 cell->setPort("\\" #_P5, sig5); \
2012 cell->set_src_attribute(src); \
2015 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
2016 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2017 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2020 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2021 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2022 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2023 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2024 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2025 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2026 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2027 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2028 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2029 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2030 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2031 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2032 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2033 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2034 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2035 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2041 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
)
2043 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2044 cell
->parameters
[ID(A_SIGNED
)] = a_signed
;
2045 cell
->parameters
[ID(B_SIGNED
)] = b_signed
;
2046 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2047 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2048 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2049 cell
->setPort(ID::A
, sig_a
);
2050 cell
->setPort(ID::B
, sig_b
);
2051 cell
->setPort(ID::Y
, sig_y
);
2052 cell
->set_src_attribute(src
);
2056 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
2058 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2059 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2060 cell
->parameters
[ID(Y_WIDTH
)] = sig_y
.size();
2061 cell
->parameters
[ID(OFFSET
)] = offset
;
2062 cell
->setPort(ID::A
, sig_a
);
2063 cell
->setPort(ID::Y
, sig_y
);
2064 cell
->set_src_attribute(src
);
2068 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2070 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2071 cell
->parameters
[ID(A_WIDTH
)] = sig_a
.size();
2072 cell
->parameters
[ID(B_WIDTH
)] = sig_b
.size();
2073 cell
->setPort(ID::A
, sig_a
);
2074 cell
->setPort(ID::B
, sig_b
);
2075 cell
->setPort(ID::Y
, sig_y
);
2076 cell
->set_src_attribute(src
);
2080 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
2082 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2083 cell
->parameters
[ID(LUT
)] = lut
;
2084 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2085 cell
->setPort(ID::A
, sig_a
);
2086 cell
->setPort(ID::Y
, sig_y
);
2087 cell
->set_src_attribute(src
);
2091 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2093 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2094 cell
->parameters
[ID(WIDTH
)] = sig_a
.size();
2095 cell
->setPort(ID::A
, sig_a
);
2096 cell
->setPort(ID(EN
), sig_en
);
2097 cell
->setPort(ID::Y
, sig_y
);
2098 cell
->set_src_attribute(src
);
2102 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2104 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2105 cell
->setPort(ID::A
, sig_a
);
2106 cell
->setPort(ID(EN
), sig_en
);
2107 cell
->set_src_attribute(src
);
2111 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2113 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2114 cell
->setPort(ID::A
, sig_a
);
2115 cell
->setPort(ID(EN
), sig_en
);
2116 cell
->set_src_attribute(src
);
2120 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2122 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2123 cell
->setPort(ID::A
, sig_a
);
2124 cell
->setPort(ID(EN
), sig_en
);
2125 cell
->set_src_attribute(src
);
2129 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2131 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2132 cell
->setPort(ID::A
, sig_a
);
2133 cell
->setPort(ID(EN
), sig_en
);
2134 cell
->set_src_attribute(src
);
2138 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2140 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2141 cell
->setPort(ID::A
, sig_a
);
2142 cell
->setPort(ID(EN
), sig_en
);
2143 cell
->set_src_attribute(src
);
2147 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2149 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2150 cell
->setPort(ID::A
, sig_a
);
2151 cell
->setPort(ID::B
, sig_b
);
2152 cell
->setPort(ID::Y
, sig_y
);
2153 cell
->set_src_attribute(src
);
2157 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
)
2159 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2160 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2161 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2162 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2163 cell
->setPort(ID(SET
), sig_set
);
2164 cell
->setPort(ID(CLR
), sig_clr
);
2165 cell
->setPort(ID(Q
), sig_q
);
2166 cell
->set_src_attribute(src
);
2170 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2172 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2173 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2174 cell
->setPort(ID(D
), sig_d
);
2175 cell
->setPort(ID(Q
), sig_q
);
2176 cell
->set_src_attribute(src
);
2180 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
)
2182 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2183 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2184 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2185 cell
->setPort(ID(CLK
), sig_clk
);
2186 cell
->setPort(ID(D
), sig_d
);
2187 cell
->setPort(ID(Q
), sig_q
);
2188 cell
->set_src_attribute(src
);
2192 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
)
2194 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2195 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2196 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2197 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2198 cell
->setPort(ID(CLK
), sig_clk
);
2199 cell
->setPort(ID(EN
), sig_en
);
2200 cell
->setPort(ID(D
), sig_d
);
2201 cell
->setPort(ID(Q
), sig_q
);
2202 cell
->set_src_attribute(src
);
2206 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2207 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2209 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2210 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2211 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2212 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2213 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2214 cell
->setPort(ID(CLK
), sig_clk
);
2215 cell
->setPort(ID(SET
), sig_set
);
2216 cell
->setPort(ID(CLR
), sig_clr
);
2217 cell
->setPort(ID(D
), sig_d
);
2218 cell
->setPort(ID(Q
), sig_q
);
2219 cell
->set_src_attribute(src
);
2223 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2224 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2226 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2227 cell
->parameters
[ID(CLK_POLARITY
)] = clk_polarity
;
2228 cell
->parameters
[ID(ARST_POLARITY
)] = arst_polarity
;
2229 cell
->parameters
[ID(ARST_VALUE
)] = arst_value
;
2230 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2231 cell
->setPort(ID(CLK
), sig_clk
);
2232 cell
->setPort(ID(ARST
), sig_arst
);
2233 cell
->setPort(ID(D
), sig_d
);
2234 cell
->setPort(ID(Q
), sig_q
);
2235 cell
->set_src_attribute(src
);
2239 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
)
2241 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2242 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2243 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2244 cell
->setPort(ID(EN
), sig_en
);
2245 cell
->setPort(ID(D
), sig_d
);
2246 cell
->setPort(ID(Q
), sig_q
);
2247 cell
->set_src_attribute(src
);
2251 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2252 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2254 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2255 cell
->parameters
[ID(EN_POLARITY
)] = en_polarity
;
2256 cell
->parameters
[ID(SET_POLARITY
)] = set_polarity
;
2257 cell
->parameters
[ID(CLR_POLARITY
)] = clr_polarity
;
2258 cell
->parameters
[ID(WIDTH
)] = sig_q
.size();
2259 cell
->setPort(ID(EN
), sig_en
);
2260 cell
->setPort(ID(SET
), sig_set
);
2261 cell
->setPort(ID(CLR
), sig_clr
);
2262 cell
->setPort(ID(D
), sig_d
);
2263 cell
->setPort(ID(Q
), sig_q
);
2264 cell
->set_src_attribute(src
);
2268 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2270 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2271 cell
->setPort(ID(D
), sig_d
);
2272 cell
->setPort(ID(Q
), sig_q
);
2273 cell
->set_src_attribute(src
);
2277 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
)
2279 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2280 cell
->setPort(ID(C
), sig_clk
);
2281 cell
->setPort(ID(D
), sig_d
);
2282 cell
->setPort(ID(Q
), sig_q
);
2283 cell
->set_src_attribute(src
);
2287 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
)
2289 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2290 cell
->setPort(ID(C
), sig_clk
);
2291 cell
->setPort(ID(E
), sig_en
);
2292 cell
->setPort(ID(D
), sig_d
);
2293 cell
->setPort(ID(Q
), sig_q
);
2294 cell
->set_src_attribute(src
);
2298 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2299 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2301 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2302 cell
->setPort(ID(C
), sig_clk
);
2303 cell
->setPort(ID(S
), sig_set
);
2304 cell
->setPort(ID(R
), sig_clr
);
2305 cell
->setPort(ID(D
), sig_d
);
2306 cell
->setPort(ID(Q
), sig_q
);
2307 cell
->set_src_attribute(src
);
2311 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2312 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2314 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2315 cell
->setPort(ID(C
), sig_clk
);
2316 cell
->setPort(ID(R
), sig_arst
);
2317 cell
->setPort(ID(D
), sig_d
);
2318 cell
->setPort(ID(Q
), sig_q
);
2319 cell
->set_src_attribute(src
);
2323 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
)
2325 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2326 cell
->setPort(ID(E
), sig_en
);
2327 cell
->setPort(ID(D
), sig_d
);
2328 cell
->setPort(ID(Q
), sig_q
);
2329 cell
->set_src_attribute(src
);
2333 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2334 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2336 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2337 cell
->setPort(ID(E
), sig_en
);
2338 cell
->setPort(ID(S
), sig_set
);
2339 cell
->setPort(ID(R
), sig_clr
);
2340 cell
->setPort(ID(D
), sig_d
);
2341 cell
->setPort(ID(Q
), sig_q
);
2342 cell
->set_src_attribute(src
);
2346 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2348 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2349 Cell
*cell
= addCell(name
, ID($anyconst
));
2350 cell
->setParam(ID(WIDTH
), width
);
2351 cell
->setPort(ID::Y
, sig
);
2352 cell
->set_src_attribute(src
);
2356 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2358 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2359 Cell
*cell
= addCell(name
, ID($anyseq
));
2360 cell
->setParam(ID(WIDTH
), width
);
2361 cell
->setPort(ID::Y
, sig
);
2362 cell
->set_src_attribute(src
);
2366 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2368 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2369 Cell
*cell
= addCell(name
, ID($allconst
));
2370 cell
->setParam(ID(WIDTH
), width
);
2371 cell
->setPort(ID::Y
, sig
);
2372 cell
->set_src_attribute(src
);
2376 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2378 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2379 Cell
*cell
= addCell(name
, ID($allseq
));
2380 cell
->setParam(ID(WIDTH
), width
);
2381 cell
->setPort(ID::Y
, sig
);
2382 cell
->set_src_attribute(src
);
2386 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2388 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2389 Cell
*cell
= addCell(name
, ID($initstate
));
2390 cell
->setPort(ID::Y
, sig
);
2391 cell
->set_src_attribute(src
);
2397 static unsigned int hashidx_count
= 123456789;
2398 hashidx_count
= mkhash_xorshift(hashidx_count
);
2399 hashidx_
= hashidx_count
;
2406 port_output
= false;
2410 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2414 RTLIL::Wire::~Wire()
2417 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2422 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2423 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2429 RTLIL::Memory::Memory()
2431 static unsigned int hashidx_count
= 123456789;
2432 hashidx_count
= mkhash_xorshift(hashidx_count
);
2433 hashidx_
= hashidx_count
;
2439 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2443 RTLIL::Cell::Cell() : module(nullptr)
2445 static unsigned int hashidx_count
= 123456789;
2446 hashidx_count
= mkhash_xorshift(hashidx_count
);
2447 hashidx_
= hashidx_count
;
2449 // log("#memtrace# %p\n", this);
2453 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2457 RTLIL::Cell::~Cell()
2460 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2465 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2466 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2472 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2474 return connections_
.count(portname
) != 0;
2477 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2479 RTLIL::SigSpec signal
;
2480 auto conn_it
= connections_
.find(portname
);
2482 if (conn_it
!= connections_
.end())
2484 for (auto mon
: module
->monitors
)
2485 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2488 for (auto mon
: module
->design
->monitors
)
2489 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2492 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2493 log_backtrace("-X- ", yosys_xtrace
-1);
2496 connections_
.erase(conn_it
);
2500 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2502 auto conn_it
= connections_
.find(portname
);
2504 if (conn_it
== connections_
.end()) {
2505 connections_
[portname
] = RTLIL::SigSpec();
2506 conn_it
= connections_
.find(portname
);
2507 log_assert(conn_it
!= connections_
.end());
2509 if (conn_it
->second
== signal
)
2512 for (auto mon
: module
->monitors
)
2513 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2516 for (auto mon
: module
->design
->monitors
)
2517 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2520 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2521 log_backtrace("-X- ", yosys_xtrace
-1);
2524 conn_it
->second
= signal
;
2527 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2529 return connections_
.at(portname
);
2532 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2534 return connections_
;
2537 bool RTLIL::Cell::known() const
2539 if (yosys_celltypes
.cell_known(type
))
2541 if (module
&& module
->design
&& module
->design
->module(type
))
2546 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2548 if (yosys_celltypes
.cell_known(type
))
2549 return yosys_celltypes
.cell_input(type
, portname
);
2550 if (module
&& module
->design
) {
2551 RTLIL::Module
*m
= module
->design
->module(type
);
2552 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2553 return w
&& w
->port_input
;
2558 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2560 if (yosys_celltypes
.cell_known(type
))
2561 return yosys_celltypes
.cell_output(type
, portname
);
2562 if (module
&& module
->design
) {
2563 RTLIL::Module
*m
= module
->design
->module(type
);
2564 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2565 return w
&& w
->port_output
;
2570 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2572 return parameters
.count(paramname
) != 0;
2575 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2577 parameters
.erase(paramname
);
2580 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2582 parameters
[paramname
] = value
;
2585 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2587 return parameters
.at(paramname
);
2590 void RTLIL::Cell::sort()
2592 connections_
.sort(sort_by_id_str());
2593 parameters
.sort(sort_by_id_str());
2594 attributes
.sort(sort_by_id_str());
2597 void RTLIL::Cell::check()
2600 InternalCellChecker
checker(NULL
, this);
2605 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2607 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
2608 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
2611 if (type
== ID($mux
) || type
== ID($pmux
)) {
2612 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2613 if (type
== ID($pmux
))
2614 parameters
[ID(S_WIDTH
)] = GetSize(connections_
[ID(S
)]);
2619 if (type
== ID($lut
) || type
== ID($sop
)) {
2620 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::A
]);
2624 if (type
== ID($fa
)) {
2625 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID::Y
]);
2629 if (type
== ID($lcu
)) {
2630 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(CO
)]);
2634 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
2636 if (connections_
.count(ID::A
)) {
2637 if (signedness_ab
) {
2639 parameters
[ID(A_SIGNED
)] = true;
2640 else if (parameters
.count(ID(A_SIGNED
)) == 0)
2641 parameters
[ID(A_SIGNED
)] = false;
2643 parameters
[ID(A_WIDTH
)] = GetSize(connections_
[ID::A
]);
2646 if (connections_
.count(ID::B
)) {
2647 if (signedness_ab
) {
2649 parameters
[ID(B_SIGNED
)] = true;
2650 else if (parameters
.count(ID(B_SIGNED
)) == 0)
2651 parameters
[ID(B_SIGNED
)] = false;
2653 parameters
[ID(B_WIDTH
)] = GetSize(connections_
[ID::B
]);
2656 if (connections_
.count(ID::Y
))
2657 parameters
[ID(Y_WIDTH
)] = GetSize(connections_
[ID::Y
]);
2659 if (connections_
.count(ID(Q
)))
2660 parameters
[ID(WIDTH
)] = GetSize(connections_
[ID(Q
)]);
2665 RTLIL::SigChunk::SigChunk()
2672 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2676 width
= GetSize(data
);
2680 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2682 log_assert(wire
!= nullptr);
2684 this->width
= wire
->width
;
2688 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2690 log_assert(wire
!= nullptr);
2692 this->width
= width
;
2693 this->offset
= offset
;
2696 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2699 data
= RTLIL::Const(str
).bits
;
2700 width
= GetSize(data
);
2704 RTLIL::SigChunk::SigChunk(int val
, int width
)
2707 data
= RTLIL::Const(val
, width
).bits
;
2708 this->width
= GetSize(data
);
2712 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2715 data
= RTLIL::Const(bit
, width
).bits
;
2716 this->width
= GetSize(data
);
2720 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2725 data
= RTLIL::Const(bit
.data
).bits
;
2727 offset
= bit
.offset
;
2731 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2733 wire
= sigchunk
.wire
;
2734 data
= sigchunk
.data
;
2735 width
= sigchunk
.width
;
2736 offset
= sigchunk
.offset
;
2739 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2741 RTLIL::SigChunk ret
;
2744 ret
.offset
= this->offset
+ offset
;
2747 for (int i
= 0; i
< length
; i
++)
2748 ret
.data
.push_back(data
[offset
+i
]);
2754 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2756 if (wire
&& other
.wire
)
2757 if (wire
->name
!= other
.wire
->name
)
2758 return wire
->name
< other
.wire
->name
;
2760 if (wire
!= other
.wire
)
2761 return wire
< other
.wire
;
2763 if (offset
!= other
.offset
)
2764 return offset
< other
.offset
;
2766 if (width
!= other
.width
)
2767 return width
< other
.width
;
2769 return data
< other
.data
;
2772 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2774 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2777 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2784 RTLIL::SigSpec::SigSpec()
2790 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2795 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2797 cover("kernel.rtlil.sigspec.init.list");
2802 log_assert(parts
.size() > 0);
2803 auto ie
= parts
.begin();
2804 auto it
= ie
+ parts
.size() - 1;
2809 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2811 cover("kernel.rtlil.sigspec.assign");
2813 width_
= other
.width_
;
2814 hash_
= other
.hash_
;
2815 chunks_
= other
.chunks_
;
2818 if (!other
.bits_
.empty())
2820 RTLIL::SigChunk
*last
= NULL
;
2821 int last_end_offset
= 0;
2823 for (auto &bit
: other
.bits_
) {
2824 if (last
&& bit
.wire
== last
->wire
) {
2825 if (bit
.wire
== NULL
) {
2826 last
->data
.push_back(bit
.data
);
2829 } else if (last_end_offset
== bit
.offset
) {
2835 chunks_
.push_back(bit
);
2836 last
= &chunks_
.back();
2837 last_end_offset
= bit
.offset
+ 1;
2846 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2848 cover("kernel.rtlil.sigspec.init.const");
2850 chunks_
.emplace_back(value
);
2851 width_
= chunks_
.back().width
;
2856 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2858 cover("kernel.rtlil.sigspec.init.chunk");
2860 chunks_
.emplace_back(chunk
);
2861 width_
= chunks_
.back().width
;
2866 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2868 cover("kernel.rtlil.sigspec.init.wire");
2870 chunks_
.emplace_back(wire
);
2871 width_
= chunks_
.back().width
;
2876 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2878 cover("kernel.rtlil.sigspec.init.wire_part");
2880 chunks_
.emplace_back(wire
, offset
, width
);
2881 width_
= chunks_
.back().width
;
2886 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2888 cover("kernel.rtlil.sigspec.init.str");
2890 chunks_
.emplace_back(str
);
2891 width_
= chunks_
.back().width
;
2896 RTLIL::SigSpec::SigSpec(int val
, int width
)
2898 cover("kernel.rtlil.sigspec.init.int");
2900 chunks_
.emplace_back(val
, width
);
2906 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2908 cover("kernel.rtlil.sigspec.init.state");
2910 chunks_
.emplace_back(bit
, width
);
2916 RTLIL::SigSpec::SigSpec(const RTLIL::SigBit
&bit
, int width
)
2918 cover("kernel.rtlil.sigspec.init.bit");
2920 if (bit
.wire
== NULL
)
2921 chunks_
.emplace_back(bit
.data
, width
);
2923 for (int i
= 0; i
< width
; i
++)
2924 chunks_
.push_back(bit
);
2930 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigChunk
> &chunks
)
2932 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2936 for (const auto &c
: chunks
)
2941 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigBit
> &bits
)
2943 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2947 for (const auto &bit
: bits
)
2952 RTLIL::SigSpec::SigSpec(const pool
<RTLIL::SigBit
> &bits
)
2954 cover("kernel.rtlil.sigspec.init.pool_bits");
2958 for (const auto &bit
: bits
)
2963 RTLIL::SigSpec::SigSpec(const std::set
<RTLIL::SigBit
> &bits
)
2965 cover("kernel.rtlil.sigspec.init.stdset_bits");
2969 for (const auto &bit
: bits
)
2974 RTLIL::SigSpec::SigSpec(bool bit
)
2976 cover("kernel.rtlil.sigspec.init.bool");
2980 append(SigBit(bit
));
2984 void RTLIL::SigSpec::pack() const
2986 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2988 if (that
->bits_
.empty())
2991 cover("kernel.rtlil.sigspec.convert.pack");
2992 log_assert(that
->chunks_
.empty());
2994 std::vector
<RTLIL::SigBit
> old_bits
;
2995 old_bits
.swap(that
->bits_
);
2997 RTLIL::SigChunk
*last
= NULL
;
2998 int last_end_offset
= 0;
3000 for (auto &bit
: old_bits
) {
3001 if (last
&& bit
.wire
== last
->wire
) {
3002 if (bit
.wire
== NULL
) {
3003 last
->data
.push_back(bit
.data
);
3006 } else if (last_end_offset
== bit
.offset
) {
3012 that
->chunks_
.push_back(bit
);
3013 last
= &that
->chunks_
.back();
3014 last_end_offset
= bit
.offset
+ 1;
3020 void RTLIL::SigSpec::unpack() const
3022 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3024 if (that
->chunks_
.empty())
3027 cover("kernel.rtlil.sigspec.convert.unpack");
3028 log_assert(that
->bits_
.empty());
3030 that
->bits_
.reserve(that
->width_
);
3031 for (auto &c
: that
->chunks_
)
3032 for (int i
= 0; i
< c
.width
; i
++)
3033 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
3035 that
->chunks_
.clear();
3039 void RTLIL::SigSpec::updhash() const
3041 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3043 if (that
->hash_
!= 0)
3046 cover("kernel.rtlil.sigspec.hash");
3049 that
->hash_
= mkhash_init
;
3050 for (auto &c
: that
->chunks_
)
3051 if (c
.wire
== NULL
) {
3052 for (auto &v
: c
.data
)
3053 that
->hash_
= mkhash(that
->hash_
, v
);
3055 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3056 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3057 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3060 if (that
->hash_
== 0)
3064 void RTLIL::SigSpec::sort()
3067 cover("kernel.rtlil.sigspec.sort");
3068 std::sort(bits_
.begin(), bits_
.end());
3071 void RTLIL::SigSpec::sort_and_unify()
3074 cover("kernel.rtlil.sigspec.sort_and_unify");
3076 // A copy of the bits vector is used to prevent duplicating the logic from
3077 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3078 // that isn't showing up as significant in profiles.
3079 std::vector
<SigBit
> unique_bits
= bits_
;
3080 std::sort(unique_bits
.begin(), unique_bits
.end());
3081 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3082 unique_bits
.erase(last
, unique_bits
.end());
3084 *this = unique_bits
;
3087 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3089 replace(pattern
, with
, this);
3092 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3094 log_assert(other
!= NULL
);
3095 log_assert(width_
== other
->width_
);
3096 log_assert(pattern
.width_
== with
.width_
);
3103 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3104 if (pattern
.bits_
[i
].wire
!= NULL
) {
3105 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3106 if (bits_
[j
] == pattern
.bits_
[i
]) {
3107 other
->bits_
[j
] = with
.bits_
[i
];
3116 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3118 replace(rules
, this);
3121 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3123 cover("kernel.rtlil.sigspec.replace_dict");
3125 log_assert(other
!= NULL
);
3126 log_assert(width_
== other
->width_
);
3128 if (rules
.empty()) return;
3132 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3133 auto it
= rules
.find(bits_
[i
]);
3134 if (it
!= rules
.end())
3135 other
->bits_
[i
] = it
->second
;
3141 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3143 replace(rules
, this);
3146 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3148 cover("kernel.rtlil.sigspec.replace_map");
3150 log_assert(other
!= NULL
);
3151 log_assert(width_
== other
->width_
);
3153 if (rules
.empty()) return;
3157 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3158 auto it
= rules
.find(bits_
[i
]);
3159 if (it
!= rules
.end())
3160 other
->bits_
[i
] = it
->second
;
3166 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3168 remove2(pattern
, NULL
);
3171 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3173 RTLIL::SigSpec tmp
= *this;
3174 tmp
.remove2(pattern
, other
);
3177 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3180 cover("kernel.rtlil.sigspec.remove_other");
3182 cover("kernel.rtlil.sigspec.remove");
3185 if (other
!= NULL
) {
3186 log_assert(width_
== other
->width_
);
3190 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3192 if (bits_
[i
].wire
== NULL
) continue;
3194 for (auto &pattern_chunk
: pattern
.chunks())
3195 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3196 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3197 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3198 bits_
.erase(bits_
.begin() + i
);
3200 if (other
!= NULL
) {
3201 other
->bits_
.erase(other
->bits_
.begin() + i
);
3211 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3213 remove2(pattern
, NULL
);
3216 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3218 RTLIL::SigSpec tmp
= *this;
3219 tmp
.remove2(pattern
, other
);
3222 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3225 cover("kernel.rtlil.sigspec.remove_other");
3227 cover("kernel.rtlil.sigspec.remove");
3231 if (other
!= NULL
) {
3232 log_assert(width_
== other
->width_
);
3236 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3237 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3238 bits_
.erase(bits_
.begin() + i
);
3240 if (other
!= NULL
) {
3241 other
->bits_
.erase(other
->bits_
.begin() + i
);
3250 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3253 cover("kernel.rtlil.sigspec.remove_other");
3255 cover("kernel.rtlil.sigspec.remove");
3259 if (other
!= NULL
) {
3260 log_assert(width_
== other
->width_
);
3264 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3265 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3266 bits_
.erase(bits_
.begin() + i
);
3268 if (other
!= NULL
) {
3269 other
->bits_
.erase(other
->bits_
.begin() + i
);
3278 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3281 cover("kernel.rtlil.sigspec.extract_other");
3283 cover("kernel.rtlil.sigspec.extract");
3285 log_assert(other
== NULL
|| width_
== other
->width_
);
3288 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3290 for (auto& pattern_chunk
: pattern
.chunks()) {
3292 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3293 for (int i
= 0; i
< width_
; i
++)
3294 if (bits_match
[i
].wire
&&
3295 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3296 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3297 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3298 ret
.append(bits_other
[i
]);
3300 for (int i
= 0; i
< width_
; i
++)
3301 if (bits_match
[i
].wire
&&
3302 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3303 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3304 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3305 ret
.append(bits_match
[i
]);
3313 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3316 cover("kernel.rtlil.sigspec.extract_other");
3318 cover("kernel.rtlil.sigspec.extract");
3320 log_assert(other
== NULL
|| width_
== other
->width_
);
3322 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3326 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3327 for (int i
= 0; i
< width_
; i
++)
3328 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3329 ret
.append(bits_other
[i
]);
3331 for (int i
= 0; i
< width_
; i
++)
3332 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3333 ret
.append(bits_match
[i
]);
3340 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3342 cover("kernel.rtlil.sigspec.replace_pos");
3347 log_assert(offset
>= 0);
3348 log_assert(with
.width_
>= 0);
3349 log_assert(offset
+with
.width_
<= width_
);
3351 for (int i
= 0; i
< with
.width_
; i
++)
3352 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3357 void RTLIL::SigSpec::remove_const()
3361 cover("kernel.rtlil.sigspec.remove_const.packed");
3363 std::vector
<RTLIL::SigChunk
> new_chunks
;
3364 new_chunks
.reserve(GetSize(chunks_
));
3367 for (auto &chunk
: chunks_
)
3368 if (chunk
.wire
!= NULL
) {
3369 new_chunks
.push_back(chunk
);
3370 width_
+= chunk
.width
;
3373 chunks_
.swap(new_chunks
);
3377 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3379 std::vector
<RTLIL::SigBit
> new_bits
;
3380 new_bits
.reserve(width_
);
3382 for (auto &bit
: bits_
)
3383 if (bit
.wire
!= NULL
)
3384 new_bits
.push_back(bit
);
3386 bits_
.swap(new_bits
);
3387 width_
= bits_
.size();
3393 void RTLIL::SigSpec::remove(int offset
, int length
)
3395 cover("kernel.rtlil.sigspec.remove_pos");
3399 log_assert(offset
>= 0);
3400 log_assert(length
>= 0);
3401 log_assert(offset
+ length
<= width_
);
3403 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3404 width_
= bits_
.size();
3409 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3412 cover("kernel.rtlil.sigspec.extract_pos");
3413 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3416 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3418 if (signal
.width_
== 0)
3426 cover("kernel.rtlil.sigspec.append");
3428 if (packed() != signal
.packed()) {
3434 for (auto &other_c
: signal
.chunks_
)
3436 auto &my_last_c
= chunks_
.back();
3437 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3438 auto &this_data
= my_last_c
.data
;
3439 auto &other_data
= other_c
.data
;
3440 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3441 my_last_c
.width
+= other_c
.width
;
3443 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3444 my_last_c
.width
+= other_c
.width
;
3446 chunks_
.push_back(other_c
);
3449 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3451 width_
+= signal
.width_
;
3455 void RTLIL::SigSpec::append(const RTLIL::SigBit
&bit
)
3459 cover("kernel.rtlil.sigspec.append_bit.packed");
3461 if (chunks_
.size() == 0)
3462 chunks_
.push_back(bit
);
3464 if (bit
.wire
== NULL
)
3465 if (chunks_
.back().wire
== NULL
) {
3466 chunks_
.back().data
.push_back(bit
.data
);
3467 chunks_
.back().width
++;
3469 chunks_
.push_back(bit
);
3471 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3472 chunks_
.back().width
++;
3474 chunks_
.push_back(bit
);
3478 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3479 bits_
.push_back(bit
);
3486 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3488 cover("kernel.rtlil.sigspec.extend_u0");
3493 remove(width
, width_
- width
);
3495 if (width_
< width
) {
3496 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3498 padding
= RTLIL::State::S0
;
3499 while (width_
< width
)
3505 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3507 cover("kernel.rtlil.sigspec.repeat");
3510 for (int i
= 0; i
< num
; i
++)
3516 void RTLIL::SigSpec::check() const
3520 cover("kernel.rtlil.sigspec.check.skip");
3524 cover("kernel.rtlil.sigspec.check.packed");
3527 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3528 const RTLIL::SigChunk chunk
= chunks_
[i
];
3529 if (chunk
.wire
== NULL
) {
3531 log_assert(chunks_
[i
-1].wire
!= NULL
);
3532 log_assert(chunk
.offset
== 0);
3533 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3535 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3536 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3537 log_assert(chunk
.offset
>= 0);
3538 log_assert(chunk
.width
>= 0);
3539 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3540 log_assert(chunk
.data
.size() == 0);
3544 log_assert(w
== width_
);
3545 log_assert(bits_
.empty());
3549 cover("kernel.rtlil.sigspec.check.unpacked");
3551 log_assert(width_
== GetSize(bits_
));
3552 log_assert(chunks_
.empty());
3557 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3559 cover("kernel.rtlil.sigspec.comp_lt");
3564 if (width_
!= other
.width_
)
3565 return width_
< other
.width_
;
3570 if (chunks_
.size() != other
.chunks_
.size())
3571 return chunks_
.size() < other
.chunks_
.size();
3576 if (hash_
!= other
.hash_
)
3577 return hash_
< other
.hash_
;
3579 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3580 if (chunks_
[i
] != other
.chunks_
[i
]) {
3581 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3582 return chunks_
[i
] < other
.chunks_
[i
];
3585 cover("kernel.rtlil.sigspec.comp_lt.equal");
3589 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3591 cover("kernel.rtlil.sigspec.comp_eq");
3596 if (width_
!= other
.width_
)
3599 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
3600 // since the RHS will contain one SigChunk of width 0 causing
3601 // the size check below to fail
3608 if (chunks_
.size() != other
.chunks_
.size())
3614 if (hash_
!= other
.hash_
)
3617 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3618 if (chunks_
[i
] != other
.chunks_
[i
]) {
3619 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3623 cover("kernel.rtlil.sigspec.comp_eq.equal");
3627 bool RTLIL::SigSpec::is_wire() const
3629 cover("kernel.rtlil.sigspec.is_wire");
3632 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3635 bool RTLIL::SigSpec::is_chunk() const
3637 cover("kernel.rtlil.sigspec.is_chunk");
3640 return GetSize(chunks_
) == 1;
3643 bool RTLIL::SigSpec::is_fully_const() const
3645 cover("kernel.rtlil.sigspec.is_fully_const");
3648 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3649 if (it
->width
> 0 && it
->wire
!= NULL
)
3654 bool RTLIL::SigSpec::is_fully_zero() const
3656 cover("kernel.rtlil.sigspec.is_fully_zero");
3659 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3660 if (it
->width
> 0 && it
->wire
!= NULL
)
3662 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3663 if (it
->data
[i
] != RTLIL::State::S0
)
3669 bool RTLIL::SigSpec::is_fully_ones() const
3671 cover("kernel.rtlil.sigspec.is_fully_ones");
3674 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3675 if (it
->width
> 0 && it
->wire
!= NULL
)
3677 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3678 if (it
->data
[i
] != RTLIL::State::S1
)
3684 bool RTLIL::SigSpec::is_fully_def() const
3686 cover("kernel.rtlil.sigspec.is_fully_def");
3689 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3690 if (it
->width
> 0 && it
->wire
!= NULL
)
3692 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3693 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3699 bool RTLIL::SigSpec::is_fully_undef() const
3701 cover("kernel.rtlil.sigspec.is_fully_undef");
3704 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3705 if (it
->width
> 0 && it
->wire
!= NULL
)
3707 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3708 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3714 bool RTLIL::SigSpec::has_const() const
3716 cover("kernel.rtlil.sigspec.has_const");
3719 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3720 if (it
->width
> 0 && it
->wire
== NULL
)
3725 bool RTLIL::SigSpec::has_marked_bits() const
3727 cover("kernel.rtlil.sigspec.has_marked_bits");
3730 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3731 if (it
->width
> 0 && it
->wire
== NULL
) {
3732 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3733 if (it
->data
[i
] == RTLIL::State::Sm
)
3739 bool RTLIL::SigSpec::as_bool() const
3741 cover("kernel.rtlil.sigspec.as_bool");
3744 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3746 return RTLIL::Const(chunks_
[0].data
).as_bool();
3750 int RTLIL::SigSpec::as_int(bool is_signed
) const
3752 cover("kernel.rtlil.sigspec.as_int");
3755 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3757 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3761 std::string
RTLIL::SigSpec::as_string() const
3763 cover("kernel.rtlil.sigspec.as_string");
3767 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3768 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3769 if (chunk
.wire
!= NULL
)
3770 for (int j
= 0; j
< chunk
.width
; j
++)
3773 str
+= RTLIL::Const(chunk
.data
).as_string();
3778 RTLIL::Const
RTLIL::SigSpec::as_const() const
3780 cover("kernel.rtlil.sigspec.as_const");
3783 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3785 return chunks_
[0].data
;
3786 return RTLIL::Const();
3789 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3791 cover("kernel.rtlil.sigspec.as_wire");
3794 log_assert(is_wire());
3795 return chunks_
[0].wire
;
3798 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3800 cover("kernel.rtlil.sigspec.as_chunk");
3803 log_assert(is_chunk());
3807 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3809 cover("kernel.rtlil.sigspec.as_bit");
3811 log_assert(width_
== 1);
3813 return RTLIL::SigBit(*chunks_
.begin());
3818 bool RTLIL::SigSpec::match(std::string pattern
) const
3820 cover("kernel.rtlil.sigspec.match");
3823 std::string str
= as_string();
3824 log_assert(pattern
.size() == str
.size());
3826 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3827 if (pattern
[i
] == ' ')
3829 if (pattern
[i
] == '*') {
3830 if (str
[i
] != 'z' && str
[i
] != 'x')
3834 if (pattern
[i
] != str
[i
])
3841 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3843 cover("kernel.rtlil.sigspec.to_sigbit_set");
3846 std::set
<RTLIL::SigBit
> sigbits
;
3847 for (auto &c
: chunks_
)
3848 for (int i
= 0; i
< c
.width
; i
++)
3849 sigbits
.insert(RTLIL::SigBit(c
, i
));
3853 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3855 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3858 pool
<RTLIL::SigBit
> sigbits
;
3859 for (auto &c
: chunks_
)
3860 for (int i
= 0; i
< c
.width
; i
++)
3861 sigbits
.insert(RTLIL::SigBit(c
, i
));
3865 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3867 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3873 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3875 cover("kernel.rtlil.sigspec.to_sigbit_map");
3880 log_assert(width_
== other
.width_
);
3882 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3883 for (int i
= 0; i
< width_
; i
++)
3884 new_map
[bits_
[i
]] = other
.bits_
[i
];
3889 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3891 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3896 log_assert(width_
== other
.width_
);
3898 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3899 for (int i
= 0; i
< width_
; i
++)
3900 new_map
[bits_
[i
]] = other
.bits_
[i
];
3905 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3907 size_t start
= 0, end
= 0;
3908 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3909 tokens
.push_back(text
.substr(start
, end
- start
));
3912 tokens
.push_back(text
.substr(start
));
3915 static int sigspec_parse_get_dummy_line_num()
3920 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3922 cover("kernel.rtlil.sigspec.parse");
3924 AST::current_filename
= "input";
3926 std::vector
<std::string
> tokens
;
3927 sigspec_parse_split(tokens
, str
, ',');
3929 sig
= RTLIL::SigSpec();
3930 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3932 std::string netname
= tokens
[tokidx
];
3933 std::string indices
;
3935 if (netname
.size() == 0)
3938 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3939 cover("kernel.rtlil.sigspec.parse.const");
3940 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3941 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3944 sig
.append(RTLIL::Const(ast
->bits
));
3952 cover("kernel.rtlil.sigspec.parse.net");
3954 if (netname
[0] != '$' && netname
[0] != '\\')
3955 netname
= "\\" + netname
;
3957 if (module
->wires_
.count(netname
) == 0) {
3958 size_t indices_pos
= netname
.size()-1;
3959 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3962 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3963 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3965 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3967 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3968 indices
= netname
.substr(indices_pos
);
3969 netname
= netname
.substr(0, indices_pos
);
3974 if (module
->wires_
.count(netname
) == 0)
3977 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3978 if (!indices
.empty()) {
3979 std::vector
<std::string
> index_tokens
;
3980 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3981 if (index_tokens
.size() == 1) {
3982 cover("kernel.rtlil.sigspec.parse.bit_sel");
3983 int a
= atoi(index_tokens
.at(0).c_str());
3984 if (a
< 0 || a
>= wire
->width
)
3986 sig
.append(RTLIL::SigSpec(wire
, a
));
3988 cover("kernel.rtlil.sigspec.parse.part_sel");
3989 int a
= atoi(index_tokens
.at(0).c_str());
3990 int b
= atoi(index_tokens
.at(1).c_str());
3995 if (a
< 0 || a
>= wire
->width
)
3997 if (b
< 0 || b
>= wire
->width
)
3999 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4008 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4010 if (str
.empty() || str
[0] != '@')
4011 return parse(sig
, module
, str
);
4013 cover("kernel.rtlil.sigspec.parse.sel");
4015 str
= RTLIL::escape_id(str
.substr(1));
4016 if (design
->selection_vars
.count(str
) == 0)
4019 sig
= RTLIL::SigSpec();
4020 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4021 for (auto &it
: module
->wires_
)
4022 if (sel
.selected_member(module
->name
, it
.first
))
4023 sig
.append(it
.second
);
4028 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4031 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4032 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4037 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4038 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4042 if (lhs
.chunks_
.size() == 1) {
4043 char *p
= (char*)str
.c_str(), *endptr
;
4044 long int val
= strtol(p
, &endptr
, 10);
4045 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4046 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4047 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4052 return parse(sig
, module
, str
);
4055 RTLIL::CaseRule::~CaseRule()
4057 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4061 bool RTLIL::CaseRule::empty() const
4063 return actions
.empty() && switches
.empty();
4066 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4068 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4069 new_caserule
->compare
= compare
;
4070 new_caserule
->actions
= actions
;
4071 for (auto &it
: switches
)
4072 new_caserule
->switches
.push_back(it
->clone());
4073 return new_caserule
;
4076 RTLIL::SwitchRule::~SwitchRule()
4078 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4082 bool RTLIL::SwitchRule::empty() const
4084 return cases
.empty();
4087 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4089 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4090 new_switchrule
->signal
= signal
;
4091 new_switchrule
->attributes
= attributes
;
4092 for (auto &it
: cases
)
4093 new_switchrule
->cases
.push_back(it
->clone());
4094 return new_switchrule
;
4098 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4100 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4101 new_syncrule
->type
= type
;
4102 new_syncrule
->signal
= signal
;
4103 new_syncrule
->actions
= actions
;
4104 return new_syncrule
;
4107 RTLIL::Process::~Process()
4109 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4113 RTLIL::Process
*RTLIL::Process::clone() const
4115 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4117 new_proc
->name
= name
;
4118 new_proc
->attributes
= attributes
;
4120 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4121 new_proc
->root_case
= *rc_ptr
;
4122 rc_ptr
->switches
.clear();
4125 for (auto &it
: syncs
)
4126 new_proc
->syncs
.push_back(it
->clone());
4132 RTLIL::Memory::~Memory()
4134 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4136 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4137 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4139 return &all_memorys
;