2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/macc.h"
22 #include "kernel/celltypes.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
31 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
32 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
33 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
34 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
35 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
36 int RTLIL::IdString::last_created_idx_
[8];
37 int RTLIL::IdString::last_created_idx_ptr_
;
41 flags
= RTLIL::CONST_FLAG_NONE
;
44 RTLIL::Const::Const(std::string str
)
46 flags
= RTLIL::CONST_FLAG_STRING
;
47 for (int i
= str
.size()-1; i
>= 0; i
--) {
48 unsigned char ch
= str
[i
];
49 for (int j
= 0; j
< 8; j
++) {
50 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
56 RTLIL::Const::Const(int val
, int width
)
58 flags
= RTLIL::CONST_FLAG_NONE
;
59 for (int i
= 0; i
< width
; i
++) {
60 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
65 RTLIL::Const::Const(RTLIL::State bit
, int width
)
67 flags
= RTLIL::CONST_FLAG_NONE
;
68 for (int i
= 0; i
< width
; i
++)
72 RTLIL::Const::Const(const std::vector
<bool> &bits
)
74 flags
= RTLIL::CONST_FLAG_NONE
;
76 this->bits
.push_back(b
? RTLIL::S1
: RTLIL::S0
);
79 RTLIL::Const::Const(const RTLIL::Const
&c
)
83 this->bits
.push_back(b
);
86 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
88 if (bits
.size() != other
.bits
.size())
89 return bits
.size() < other
.bits
.size();
90 for (size_t i
= 0; i
< bits
.size(); i
++)
91 if (bits
[i
] != other
.bits
[i
])
92 return bits
[i
] < other
.bits
[i
];
96 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
98 return bits
== other
.bits
;
101 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
103 return bits
!= other
.bits
;
106 bool RTLIL::Const::as_bool() const
108 for (size_t i
= 0; i
< bits
.size(); i
++)
109 if (bits
[i
] == RTLIL::S1
)
114 int RTLIL::Const::as_int(bool is_signed
) const
117 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
118 if (bits
[i
] == RTLIL::S1
)
120 if (is_signed
&& bits
.back() == RTLIL::S1
)
121 for (size_t i
= bits
.size(); i
< 32; i
++)
126 std::string
RTLIL::Const::as_string() const
129 for (size_t i
= bits
.size(); i
> 0; i
--)
131 case S0
: ret
+= "0"; break;
132 case S1
: ret
+= "1"; break;
133 case Sx
: ret
+= "x"; break;
134 case Sz
: ret
+= "z"; break;
135 case Sa
: ret
+= "-"; break;
136 case Sm
: ret
+= "m"; break;
141 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
144 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
146 case '0': c
.bits
.push_back(State::S0
); break;
147 case '1': c
.bits
.push_back(State::S1
); break;
148 case 'x': c
.bits
.push_back(State::Sx
); break;
149 case 'z': c
.bits
.push_back(State::Sz
); break;
150 case 'm': c
.bits
.push_back(State::Sm
); break;
151 default: c
.bits
.push_back(State::Sa
);
156 std::string
RTLIL::Const::decode_string() const
159 std::vector
<char> string_chars
;
160 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
162 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
163 if (bits
[i
+ j
] == RTLIL::State::S1
)
166 string_chars
.push_back(ch
);
168 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
169 string
+= string_chars
[i
];
173 bool RTLIL::Const::is_fully_zero() const
175 cover("kernel.rtlil.const.is_fully_zero");
177 for (auto bit
: bits
)
178 if (bit
!= RTLIL::State::S0
)
184 bool RTLIL::Const::is_fully_ones() const
186 cover("kernel.rtlil.const.is_fully_ones");
188 for (auto bit
: bits
)
189 if (bit
!= RTLIL::State::S1
)
195 bool RTLIL::Const::is_fully_def() const
197 cover("kernel.rtlil.const.is_fully_def");
199 for (auto bit
: bits
)
200 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
206 bool RTLIL::Const::is_fully_undef() const
208 cover("kernel.rtlil.const.is_fully_undef");
210 for (auto bit
: bits
)
211 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
217 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
220 attributes
[id
] = RTLIL::Const(1);
221 else if (attributes
.count(id
))
222 attributes
.erase(id
);
225 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
227 if (attributes
.count(id
) == 0)
229 return attributes
.at(id
).as_bool();
232 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
235 for (auto &s
: data
) {
236 if (!attrval
.empty())
240 attributes
[id
] = RTLIL::Const(attrval
);
243 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
245 pool
<string
> union_data
= get_strpool_attribute(id
);
246 union_data
.insert(data
.begin(), data
.end());
247 if (!union_data
.empty())
248 set_strpool_attribute(id
, union_data
);
251 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
254 if (attributes
.count(id
) != 0)
255 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
260 void RTLIL::AttrObject::set_src_attribute(const std::string
&src
)
263 attributes
.erase("\\src");
265 attributes
["\\src"] = src
;
268 std::string
RTLIL::AttrObject::get_src_attribute() const
271 if (attributes
.count("\\src"))
272 src
= attributes
.at("\\src").decode_string();
276 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
280 if (selected_modules
.count(mod_name
) > 0)
282 if (selected_members
.count(mod_name
) > 0)
287 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
291 if (selected_modules
.count(mod_name
) > 0)
296 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
300 if (selected_modules
.count(mod_name
) > 0)
302 if (selected_members
.count(mod_name
) > 0)
303 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
308 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
310 if (full_selection
) {
311 selected_modules
.clear();
312 selected_members
.clear();
316 std::vector
<RTLIL::IdString
> del_list
, add_list
;
319 for (auto mod_name
: selected_modules
) {
320 if (design
->modules_
.count(mod_name
) == 0)
321 del_list
.push_back(mod_name
);
322 selected_members
.erase(mod_name
);
324 for (auto mod_name
: del_list
)
325 selected_modules
.erase(mod_name
);
328 for (auto &it
: selected_members
)
329 if (design
->modules_
.count(it
.first
) == 0)
330 del_list
.push_back(it
.first
);
331 for (auto mod_name
: del_list
)
332 selected_members
.erase(mod_name
);
334 for (auto &it
: selected_members
) {
336 for (auto memb_name
: it
.second
)
337 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
338 del_list
.push_back(memb_name
);
339 for (auto memb_name
: del_list
)
340 it
.second
.erase(memb_name
);
345 for (auto &it
: selected_members
)
346 if (it
.second
.size() == 0)
347 del_list
.push_back(it
.first
);
348 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
349 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
350 add_list
.push_back(it
.first
);
351 for (auto mod_name
: del_list
)
352 selected_members
.erase(mod_name
);
353 for (auto mod_name
: add_list
) {
354 selected_members
.erase(mod_name
);
355 selected_modules
.insert(mod_name
);
358 if (selected_modules
.size() == design
->modules_
.size()) {
359 full_selection
= true;
360 selected_modules
.clear();
361 selected_members
.clear();
365 RTLIL::Design::Design()
367 static unsigned int hashidx_count
= 123456789;
368 hashidx_count
= mkhash_xorshift(hashidx_count
);
369 hashidx_
= hashidx_count
;
371 refcount_modules_
= 0;
372 selection_stack
.push_back(RTLIL::Selection());
375 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
379 RTLIL::Design::~Design()
381 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
383 for (auto n
: verilog_packages
)
385 for (auto n
: verilog_globals
)
388 RTLIL::Design::get_all_designs()->erase(hashidx_
);
393 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
394 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
400 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
402 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
405 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
407 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
410 RTLIL::Module
*RTLIL::Design::top_module()
412 RTLIL::Module
*module
= nullptr;
413 int module_count
= 0;
415 for (auto mod
: selected_modules()) {
416 if (mod
->get_bool_attribute("\\top"))
422 return module_count
== 1 ? module
: nullptr;
425 void RTLIL::Design::add(RTLIL::Module
*module
)
427 log_assert(modules_
.count(module
->name
) == 0);
428 log_assert(refcount_modules_
== 0);
429 modules_
[module
->name
] = module
;
430 module
->design
= this;
432 for (auto mon
: monitors
)
433 mon
->notify_module_add(module
);
436 log("#X# New Module: %s\n", log_id(module
));
437 log_backtrace("-X- ", yosys_xtrace
-1);
441 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
443 log_assert(modules_
.count(name
) == 0);
444 log_assert(refcount_modules_
== 0);
446 RTLIL::Module
*module
= new RTLIL::Module
;
447 modules_
[name
] = module
;
448 module
->design
= this;
451 for (auto mon
: monitors
)
452 mon
->notify_module_add(module
);
455 log("#X# New Module: %s\n", log_id(module
));
456 log_backtrace("-X- ", yosys_xtrace
-1);
462 void RTLIL::Design::scratchpad_unset(std::string varname
)
464 scratchpad
.erase(varname
);
467 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
469 scratchpad
[varname
] = stringf("%d", value
);
472 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
474 scratchpad
[varname
] = value
? "true" : "false";
477 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
479 scratchpad
[varname
] = value
;
482 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
484 if (scratchpad
.count(varname
) == 0)
485 return default_value
;
487 std::string str
= scratchpad
.at(varname
);
489 if (str
== "0" || str
== "false")
492 if (str
== "1" || str
== "true")
495 char *endptr
= nullptr;
496 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
497 return *endptr
? default_value
: parsed_value
;
500 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
502 if (scratchpad
.count(varname
) == 0)
503 return default_value
;
505 std::string str
= scratchpad
.at(varname
);
507 if (str
== "0" || str
== "false")
510 if (str
== "1" || str
== "true")
513 return default_value
;
516 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
518 if (scratchpad
.count(varname
) == 0)
519 return default_value
;
520 return scratchpad
.at(varname
);
523 void RTLIL::Design::remove(RTLIL::Module
*module
)
525 for (auto mon
: monitors
)
526 mon
->notify_module_del(module
);
529 log("#X# Remove Module: %s\n", log_id(module
));
530 log_backtrace("-X- ", yosys_xtrace
-1);
533 log_assert(modules_
.at(module
->name
) == module
);
534 modules_
.erase(module
->name
);
538 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
540 modules_
.erase(module
->name
);
541 module
->name
= new_name
;
545 void RTLIL::Design::sort()
548 modules_
.sort(sort_by_id_str());
549 for (auto &it
: modules_
)
553 void RTLIL::Design::check()
556 for (auto &it
: modules_
) {
557 log_assert(this == it
.second
->design
);
558 log_assert(it
.first
== it
.second
->name
);
559 log_assert(!it
.first
.empty());
565 void RTLIL::Design::optimize()
567 for (auto &it
: modules_
)
568 it
.second
->optimize();
569 for (auto &it
: selection_stack
)
571 for (auto &it
: selection_vars
)
572 it
.second
.optimize(this);
575 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
577 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
579 if (selection_stack
.size() == 0)
581 return selection_stack
.back().selected_module(mod_name
);
584 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
586 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
588 if (selection_stack
.size() == 0)
590 return selection_stack
.back().selected_whole_module(mod_name
);
593 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
595 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
597 if (selection_stack
.size() == 0)
599 return selection_stack
.back().selected_member(mod_name
, memb_name
);
602 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
604 return selected_module(mod
->name
);
607 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
609 return selected_whole_module(mod
->name
);
612 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
614 std::vector
<RTLIL::Module
*> result
;
615 result
.reserve(modules_
.size());
616 for (auto &it
: modules_
)
617 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
618 result
.push_back(it
.second
);
622 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
624 std::vector
<RTLIL::Module
*> result
;
625 result
.reserve(modules_
.size());
626 for (auto &it
: modules_
)
627 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
628 result
.push_back(it
.second
);
632 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
634 std::vector
<RTLIL::Module
*> result
;
635 result
.reserve(modules_
.size());
636 for (auto &it
: modules_
)
637 if (it
.second
->get_blackbox_attribute())
639 else if (selected_whole_module(it
.first
))
640 result
.push_back(it
.second
);
641 else if (selected_module(it
.first
))
642 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
646 RTLIL::Module::Module()
648 static unsigned int hashidx_count
= 123456789;
649 hashidx_count
= mkhash_xorshift(hashidx_count
);
650 hashidx_
= hashidx_count
;
657 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
661 RTLIL::Module::~Module()
663 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
665 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
667 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
669 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
672 RTLIL::Module::get_all_modules()->erase(hashidx_
);
677 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
678 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
684 void RTLIL::Module::makeblackbox()
686 pool
<RTLIL::Wire
*> delwires
;
688 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
689 if (!it
->second
->port_input
&& !it
->second
->port_output
)
690 delwires
.insert(it
->second
);
692 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
696 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
700 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
705 set_bool_attribute("\\blackbox");
708 void RTLIL::Module::reprocess_module(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Module
*>)
710 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
713 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, bool mayfail
)
716 return RTLIL::IdString();
717 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
721 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>, dict
<RTLIL::IdString
, RTLIL::Module
*>, dict
<RTLIL::IdString
, RTLIL::IdString
>, bool mayfail
)
724 return RTLIL::IdString();
725 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
728 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
730 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
735 struct InternalCellChecker
737 RTLIL::Module
*module
;
739 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
741 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
743 void error(int linenr
)
745 std::stringstream buf
;
746 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
748 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
749 module
? module
->name
.c_str() : "", module
? "." : "",
750 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
753 int param(const char *name
)
755 if (cell
->parameters
.count(name
) == 0)
757 expected_params
.insert(name
);
758 return cell
->parameters
.at(name
).as_int();
761 int param_bool(const char *name
)
764 if (cell
->parameters
.at(name
).bits
.size() > 32)
766 if (v
!= 0 && v
!= 1)
771 void param_bits(const char *name
, int width
)
774 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
778 void port(const char *name
, int width
)
780 if (!cell
->hasPort(name
))
782 if (cell
->getPort(name
).size() != width
)
784 expected_ports
.insert(name
);
787 void check_expected(bool check_matched_sign
= true)
789 for (auto ¶
: cell
->parameters
)
790 if (expected_params
.count(para
.first
) == 0)
792 for (auto &conn
: cell
->connections())
793 if (expected_ports
.count(conn
.first
) == 0)
796 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
797 bool a_is_signed
= param("\\A_SIGNED") != 0;
798 bool b_is_signed
= param("\\B_SIGNED") != 0;
799 if (a_is_signed
!= b_is_signed
)
804 void check_gate(const char *ports
)
806 if (cell
->parameters
.size() != 0)
809 for (const char *p
= ports
; *p
; p
++) {
810 char portname
[3] = { '\\', *p
, 0 };
811 if (!cell
->hasPort(portname
))
813 if (cell
->getPort(portname
).size() != 1)
817 for (auto &conn
: cell
->connections()) {
818 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
820 if (strchr(ports
, conn
.first
[1]) == NULL
)
827 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" || cell
->type
.substr(0,10) == "$fmcombine" ||
828 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
831 if (cell
->type
.in("$not", "$pos", "$neg")) {
832 param_bool("\\A_SIGNED");
833 port("\\A", param("\\A_WIDTH"));
834 port("\\Y", param("\\Y_WIDTH"));
839 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
840 param_bool("\\A_SIGNED");
841 param_bool("\\B_SIGNED");
842 port("\\A", param("\\A_WIDTH"));
843 port("\\B", param("\\B_WIDTH"));
844 port("\\Y", param("\\Y_WIDTH"));
849 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
850 param_bool("\\A_SIGNED");
851 port("\\A", param("\\A_WIDTH"));
852 port("\\Y", param("\\Y_WIDTH"));
857 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
858 param_bool("\\A_SIGNED");
859 param_bool("\\B_SIGNED");
860 port("\\A", param("\\A_WIDTH"));
861 port("\\B", param("\\B_WIDTH"));
862 port("\\Y", param("\\Y_WIDTH"));
863 check_expected(false);
867 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
868 param_bool("\\A_SIGNED");
869 param_bool("\\B_SIGNED");
870 port("\\A", param("\\A_WIDTH"));
871 port("\\B", param("\\B_WIDTH"));
872 port("\\Y", param("\\Y_WIDTH"));
877 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
878 param_bool("\\A_SIGNED");
879 param_bool("\\B_SIGNED");
880 port("\\A", param("\\A_WIDTH"));
881 port("\\B", param("\\B_WIDTH"));
882 port("\\Y", param("\\Y_WIDTH"));
883 check_expected(cell
->type
!= "$pow");
887 if (cell
->type
== "$fa") {
888 port("\\A", param("\\WIDTH"));
889 port("\\B", param("\\WIDTH"));
890 port("\\C", param("\\WIDTH"));
891 port("\\X", param("\\WIDTH"));
892 port("\\Y", param("\\WIDTH"));
897 if (cell
->type
== "$lcu") {
898 port("\\P", param("\\WIDTH"));
899 port("\\G", param("\\WIDTH"));
901 port("\\CO", param("\\WIDTH"));
906 if (cell
->type
== "$alu") {
907 param_bool("\\A_SIGNED");
908 param_bool("\\B_SIGNED");
909 port("\\A", param("\\A_WIDTH"));
910 port("\\B", param("\\B_WIDTH"));
913 port("\\X", param("\\Y_WIDTH"));
914 port("\\Y", param("\\Y_WIDTH"));
915 port("\\CO", param("\\Y_WIDTH"));
920 if (cell
->type
== "$macc") {
922 param("\\CONFIG_WIDTH");
923 port("\\A", param("\\A_WIDTH"));
924 port("\\B", param("\\B_WIDTH"));
925 port("\\Y", param("\\Y_WIDTH"));
927 Macc().from_cell(cell
);
931 if (cell
->type
== "$logic_not") {
932 param_bool("\\A_SIGNED");
933 port("\\A", param("\\A_WIDTH"));
934 port("\\Y", param("\\Y_WIDTH"));
939 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
940 param_bool("\\A_SIGNED");
941 param_bool("\\B_SIGNED");
942 port("\\A", param("\\A_WIDTH"));
943 port("\\B", param("\\B_WIDTH"));
944 port("\\Y", param("\\Y_WIDTH"));
945 check_expected(false);
949 if (cell
->type
== "$slice") {
951 port("\\A", param("\\A_WIDTH"));
952 port("\\Y", param("\\Y_WIDTH"));
953 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
959 if (cell
->type
== "$concat") {
960 port("\\A", param("\\A_WIDTH"));
961 port("\\B", param("\\B_WIDTH"));
962 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
967 if (cell
->type
== "$mux") {
968 port("\\A", param("\\WIDTH"));
969 port("\\B", param("\\WIDTH"));
971 port("\\Y", param("\\WIDTH"));
976 if (cell
->type
== "$pmux") {
977 port("\\A", param("\\WIDTH"));
978 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
979 port("\\S", param("\\S_WIDTH"));
980 port("\\Y", param("\\WIDTH"));
985 if (cell
->type
== "$lut") {
987 port("\\A", param("\\WIDTH"));
993 if (cell
->type
== "$sop") {
996 port("\\A", param("\\WIDTH"));
1002 if (cell
->type
== "$sr") {
1003 param_bool("\\SET_POLARITY");
1004 param_bool("\\CLR_POLARITY");
1005 port("\\SET", param("\\WIDTH"));
1006 port("\\CLR", param("\\WIDTH"));
1007 port("\\Q", param("\\WIDTH"));
1012 if (cell
->type
== "$ff") {
1013 port("\\D", param("\\WIDTH"));
1014 port("\\Q", param("\\WIDTH"));
1019 if (cell
->type
== "$dff") {
1020 param_bool("\\CLK_POLARITY");
1022 port("\\D", param("\\WIDTH"));
1023 port("\\Q", param("\\WIDTH"));
1028 if (cell
->type
== "$dffe") {
1029 param_bool("\\CLK_POLARITY");
1030 param_bool("\\EN_POLARITY");
1033 port("\\D", param("\\WIDTH"));
1034 port("\\Q", param("\\WIDTH"));
1039 if (cell
->type
== "$dffsr") {
1040 param_bool("\\CLK_POLARITY");
1041 param_bool("\\SET_POLARITY");
1042 param_bool("\\CLR_POLARITY");
1044 port("\\SET", param("\\WIDTH"));
1045 port("\\CLR", param("\\WIDTH"));
1046 port("\\D", param("\\WIDTH"));
1047 port("\\Q", param("\\WIDTH"));
1052 if (cell
->type
== "$adff") {
1053 param_bool("\\CLK_POLARITY");
1054 param_bool("\\ARST_POLARITY");
1055 param_bits("\\ARST_VALUE", param("\\WIDTH"));
1058 port("\\D", param("\\WIDTH"));
1059 port("\\Q", param("\\WIDTH"));
1064 if (cell
->type
== "$dlatch") {
1065 param_bool("\\EN_POLARITY");
1067 port("\\D", param("\\WIDTH"));
1068 port("\\Q", param("\\WIDTH"));
1073 if (cell
->type
== "$dlatchsr") {
1074 param_bool("\\EN_POLARITY");
1075 param_bool("\\SET_POLARITY");
1076 param_bool("\\CLR_POLARITY");
1078 port("\\SET", param("\\WIDTH"));
1079 port("\\CLR", param("\\WIDTH"));
1080 port("\\D", param("\\WIDTH"));
1081 port("\\Q", param("\\WIDTH"));
1086 if (cell
->type
== "$fsm") {
1088 param_bool("\\CLK_POLARITY");
1089 param_bool("\\ARST_POLARITY");
1090 param("\\STATE_BITS");
1091 param("\\STATE_NUM");
1092 param("\\STATE_NUM_LOG2");
1093 param("\\STATE_RST");
1094 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
1095 param("\\TRANS_NUM");
1096 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
1099 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
1100 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
1105 if (cell
->type
== "$memrd") {
1107 param_bool("\\CLK_ENABLE");
1108 param_bool("\\CLK_POLARITY");
1109 param_bool("\\TRANSPARENT");
1112 port("\\ADDR", param("\\ABITS"));
1113 port("\\DATA", param("\\WIDTH"));
1118 if (cell
->type
== "$memwr") {
1120 param_bool("\\CLK_ENABLE");
1121 param_bool("\\CLK_POLARITY");
1122 param("\\PRIORITY");
1124 port("\\EN", param("\\WIDTH"));
1125 port("\\ADDR", param("\\ABITS"));
1126 port("\\DATA", param("\\WIDTH"));
1131 if (cell
->type
== "$meminit") {
1133 param("\\PRIORITY");
1134 port("\\ADDR", param("\\ABITS"));
1135 port("\\DATA", param("\\WIDTH") * param("\\WORDS"));
1140 if (cell
->type
== "$mem") {
1145 param_bits("\\RD_CLK_ENABLE", max(1, param("\\RD_PORTS")));
1146 param_bits("\\RD_CLK_POLARITY", max(1, param("\\RD_PORTS")));
1147 param_bits("\\RD_TRANSPARENT", max(1, param("\\RD_PORTS")));
1148 param_bits("\\WR_CLK_ENABLE", max(1, param("\\WR_PORTS")));
1149 param_bits("\\WR_CLK_POLARITY", max(1, param("\\WR_PORTS")));
1150 port("\\RD_CLK", param("\\RD_PORTS"));
1151 port("\\RD_EN", param("\\RD_PORTS"));
1152 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
1153 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
1154 port("\\WR_CLK", param("\\WR_PORTS"));
1155 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
1156 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
1157 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
1162 if (cell
->type
== "$tribuf") {
1163 port("\\A", param("\\WIDTH"));
1164 port("\\Y", param("\\WIDTH"));
1170 if (cell
->type
.in("$assert", "$assume", "$live", "$fair", "$cover")) {
1177 if (cell
->type
== "$initstate") {
1183 if (cell
->type
.in("$anyconst", "$anyseq", "$allconst", "$allseq")) {
1184 port("\\Y", param("\\WIDTH"));
1189 if (cell
->type
== "$equiv") {
1197 if (cell
->type
.in("$specify2", "$specify3")) {
1198 param_bool("\\FULL");
1199 param_bool("\\SRC_DST_PEN");
1200 param_bool("\\SRC_DST_POL");
1201 param("\\T_RISE_MIN");
1202 param("\\T_RISE_TYP");
1203 param("\\T_RISE_MAX");
1204 param("\\T_FALL_MIN");
1205 param("\\T_FALL_TYP");
1206 param("\\T_FALL_MAX");
1208 port("\\SRC", param("\\SRC_WIDTH"));
1209 port("\\DST", param("\\DST_WIDTH"));
1210 if (cell
->type
== "$specify3") {
1211 param_bool("\\EDGE_EN");
1212 param_bool("\\EDGE_POL");
1213 param_bool("\\DAT_DST_PEN");
1214 param_bool("\\DAT_DST_POL");
1215 port("\\DAT", param("\\DST_WIDTH"));
1221 if (cell
->type
== "$specrule") {
1222 param_bool("\\SRC_PEN");
1223 param_bool("\\SRC_POL");
1224 param_bool("\\DST_PEN");
1225 param_bool("\\DST_POL");
1226 param_bool("\\LIMIT_GT");
1228 port("\\SRC_EN", 1);
1229 port("\\DST_EN", 1);
1230 port("\\SRC", param("\\SRC_WIDTH"));
1231 port("\\DST", param("\\DST_WIDTH"));
1236 if (cell
->type
== "$_BUF_") { check_gate("AY"); return; }
1237 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
1238 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
1239 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
1240 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
1241 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
1242 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
1243 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
1244 if (cell
->type
== "$_ANDNOT_") { check_gate("ABY"); return; }
1245 if (cell
->type
== "$_ORNOT_") { check_gate("ABY"); return; }
1246 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
1247 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
1248 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
1249 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
1250 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
1252 if (cell
->type
== "$_TBUF_") { check_gate("AYE"); return; }
1254 if (cell
->type
== "$_MUX4_") { check_gate("ABCDSTY"); return; }
1255 if (cell
->type
== "$_MUX8_") { check_gate("ABCDEFGHSTUY"); return; }
1256 if (cell
->type
== "$_MUX16_") { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1258 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
1259 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
1260 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
1261 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
1263 if (cell
->type
== "$_FF_") { check_gate("DQ"); return; }
1264 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
1265 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
1267 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
1268 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
1269 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
1270 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
1272 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
1273 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
1274 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
1275 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
1276 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
1277 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
1278 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
1279 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
1281 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
1282 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
1283 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
1284 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
1285 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
1286 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
1287 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
1288 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
1290 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
1291 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
1293 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
1294 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
1295 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
1296 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
1297 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
1298 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
1299 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
1300 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
1308 void RTLIL::Module::sort()
1310 wires_
.sort(sort_by_id_str());
1311 cells_
.sort(sort_by_id_str());
1312 avail_parameters
.sort(sort_by_id_str());
1313 memories
.sort(sort_by_id_str());
1314 processes
.sort(sort_by_id_str());
1315 for (auto &it
: cells_
)
1317 for (auto &it
: wires_
)
1318 it
.second
->attributes
.sort(sort_by_id_str());
1319 for (auto &it
: memories
)
1320 it
.second
->attributes
.sort(sort_by_id_str());
1323 void RTLIL::Module::check()
1326 std::vector
<bool> ports_declared
;
1327 for (auto &it
: wires_
) {
1328 log_assert(this == it
.second
->module
);
1329 log_assert(it
.first
== it
.second
->name
);
1330 log_assert(!it
.first
.empty());
1331 log_assert(it
.second
->width
>= 0);
1332 log_assert(it
.second
->port_id
>= 0);
1333 for (auto &it2
: it
.second
->attributes
)
1334 log_assert(!it2
.first
.empty());
1335 if (it
.second
->port_id
) {
1336 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1337 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1338 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1339 if (GetSize(ports_declared
) < it
.second
->port_id
)
1340 ports_declared
.resize(it
.second
->port_id
);
1341 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1342 ports_declared
[it
.second
->port_id
-1] = true;
1344 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1346 for (auto port_declared
: ports_declared
)
1347 log_assert(port_declared
== true);
1348 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1350 for (auto &it
: memories
) {
1351 log_assert(it
.first
== it
.second
->name
);
1352 log_assert(!it
.first
.empty());
1353 log_assert(it
.second
->width
>= 0);
1354 log_assert(it
.second
->size
>= 0);
1355 for (auto &it2
: it
.second
->attributes
)
1356 log_assert(!it2
.first
.empty());
1359 for (auto &it
: cells_
) {
1360 log_assert(this == it
.second
->module
);
1361 log_assert(it
.first
== it
.second
->name
);
1362 log_assert(!it
.first
.empty());
1363 log_assert(!it
.second
->type
.empty());
1364 for (auto &it2
: it
.second
->connections()) {
1365 log_assert(!it2
.first
.empty());
1368 for (auto &it2
: it
.second
->attributes
)
1369 log_assert(!it2
.first
.empty());
1370 for (auto &it2
: it
.second
->parameters
)
1371 log_assert(!it2
.first
.empty());
1372 InternalCellChecker
checker(this, it
.second
);
1376 for (auto &it
: processes
) {
1377 log_assert(it
.first
== it
.second
->name
);
1378 log_assert(!it
.first
.empty());
1379 // FIXME: More checks here..
1382 for (auto &it
: connections_
) {
1383 log_assert(it
.first
.size() == it
.second
.size());
1384 log_assert(!it
.first
.has_const());
1389 for (auto &it
: attributes
)
1390 log_assert(!it
.first
.empty());
1394 void RTLIL::Module::optimize()
1398 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1400 log_assert(new_mod
->refcount_wires_
== 0);
1401 log_assert(new_mod
->refcount_cells_
== 0);
1403 new_mod
->avail_parameters
= avail_parameters
;
1405 for (auto &conn
: connections_
)
1406 new_mod
->connect(conn
);
1408 for (auto &attr
: attributes
)
1409 new_mod
->attributes
[attr
.first
] = attr
.second
;
1411 for (auto &it
: wires_
)
1412 new_mod
->addWire(it
.first
, it
.second
);
1414 for (auto &it
: memories
)
1415 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1417 for (auto &it
: cells_
)
1418 new_mod
->addCell(it
.first
, it
.second
);
1420 for (auto &it
: processes
)
1421 new_mod
->processes
[it
.first
] = it
.second
->clone();
1423 struct RewriteSigSpecWorker
1426 void operator()(RTLIL::SigSpec
&sig
)
1428 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1429 for (auto &c
: chunks
)
1431 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1436 RewriteSigSpecWorker rewriteSigSpecWorker
;
1437 rewriteSigSpecWorker
.mod
= new_mod
;
1438 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1439 new_mod
->fixup_ports();
1442 RTLIL::Module
*RTLIL::Module::clone() const
1444 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1445 new_mod
->name
= name
;
1450 bool RTLIL::Module::has_memories() const
1452 return !memories
.empty();
1455 bool RTLIL::Module::has_processes() const
1457 return !processes
.empty();
1460 bool RTLIL::Module::has_memories_warn() const
1462 if (!memories
.empty())
1463 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1464 return !memories
.empty();
1467 bool RTLIL::Module::has_processes_warn() const
1469 if (!processes
.empty())
1470 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1471 return !processes
.empty();
1474 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1476 std::vector
<RTLIL::Wire
*> result
;
1477 result
.reserve(wires_
.size());
1478 for (auto &it
: wires_
)
1479 if (design
->selected(this, it
.second
))
1480 result
.push_back(it
.second
);
1484 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1486 std::vector
<RTLIL::Cell
*> result
;
1487 result
.reserve(wires_
.size());
1488 for (auto &it
: cells_
)
1489 if (design
->selected(this, it
.second
))
1490 result
.push_back(it
.second
);
1494 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1496 log_assert(!wire
->name
.empty());
1497 log_assert(count_id(wire
->name
) == 0);
1498 log_assert(refcount_wires_
== 0);
1499 wires_
[wire
->name
] = wire
;
1500 wire
->module
= this;
1503 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1505 log_assert(!cell
->name
.empty());
1506 log_assert(count_id(cell
->name
) == 0);
1507 log_assert(refcount_cells_
== 0);
1508 cells_
[cell
->name
] = cell
;
1509 cell
->module
= this;
1513 struct DeleteWireWorker
1515 RTLIL::Module
*module
;
1516 const pool
<RTLIL::Wire
*> *wires_p
;
1518 void operator()(RTLIL::SigSpec
&sig
) {
1519 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1520 for (auto &c
: chunks
)
1521 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1522 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1530 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1532 log_assert(refcount_wires_
== 0);
1534 DeleteWireWorker delete_wire_worker
;
1535 delete_wire_worker
.module
= this;
1536 delete_wire_worker
.wires_p
= &wires
;
1537 rewrite_sigspecs(delete_wire_worker
);
1539 for (auto &it
: wires
) {
1540 log_assert(wires_
.count(it
->name
) != 0);
1541 wires_
.erase(it
->name
);
1546 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1548 while (!cell
->connections_
.empty())
1549 cell
->unsetPort(cell
->connections_
.begin()->first
);
1551 log_assert(cells_
.count(cell
->name
) != 0);
1552 log_assert(refcount_cells_
== 0);
1553 cells_
.erase(cell
->name
);
1557 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1559 log_assert(wires_
[wire
->name
] == wire
);
1560 log_assert(refcount_wires_
== 0);
1561 wires_
.erase(wire
->name
);
1562 wire
->name
= new_name
;
1566 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1568 log_assert(cells_
[cell
->name
] == cell
);
1569 log_assert(refcount_wires_
== 0);
1570 cells_
.erase(cell
->name
);
1571 cell
->name
= new_name
;
1575 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1577 log_assert(count_id(old_name
) != 0);
1578 if (wires_
.count(old_name
))
1579 rename(wires_
.at(old_name
), new_name
);
1580 else if (cells_
.count(old_name
))
1581 rename(cells_
.at(old_name
), new_name
);
1586 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1588 log_assert(wires_
[w1
->name
] == w1
);
1589 log_assert(wires_
[w2
->name
] == w2
);
1590 log_assert(refcount_wires_
== 0);
1592 wires_
.erase(w1
->name
);
1593 wires_
.erase(w2
->name
);
1595 std::swap(w1
->name
, w2
->name
);
1597 wires_
[w1
->name
] = w1
;
1598 wires_
[w2
->name
] = w2
;
1601 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1603 log_assert(cells_
[c1
->name
] == c1
);
1604 log_assert(cells_
[c2
->name
] == c2
);
1605 log_assert(refcount_cells_
== 0);
1607 cells_
.erase(c1
->name
);
1608 cells_
.erase(c2
->name
);
1610 std::swap(c1
->name
, c2
->name
);
1612 cells_
[c1
->name
] = c1
;
1613 cells_
[c2
->name
] = c2
;
1616 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1619 return uniquify(name
, index
);
1622 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1625 if (count_id(name
) == 0)
1631 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1632 if (count_id(new_name
) == 0)
1638 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1640 if (a
->port_id
&& !b
->port_id
)
1642 if (!a
->port_id
&& b
->port_id
)
1645 if (a
->port_id
== b
->port_id
)
1646 return a
->name
< b
->name
;
1647 return a
->port_id
< b
->port_id
;
1650 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1652 for (auto mon
: monitors
)
1653 mon
->notify_connect(this, conn
);
1656 for (auto mon
: design
->monitors
)
1657 mon
->notify_connect(this, conn
);
1659 // ignore all attempts to assign constants to other constants
1660 if (conn
.first
.has_const()) {
1661 RTLIL::SigSig new_conn
;
1662 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1663 if (conn
.first
[i
].wire
) {
1664 new_conn
.first
.append(conn
.first
[i
]);
1665 new_conn
.second
.append(conn
.second
[i
]);
1667 if (GetSize(new_conn
.first
))
1673 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
));
1674 log_backtrace("-X- ", yosys_xtrace
-1);
1677 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1678 connections_
.push_back(conn
);
1681 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1683 connect(RTLIL::SigSig(lhs
, rhs
));
1686 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1688 for (auto mon
: monitors
)
1689 mon
->notify_connect(this, new_conn
);
1692 for (auto mon
: design
->monitors
)
1693 mon
->notify_connect(this, new_conn
);
1696 log("#X# New connections vector in %s:\n", log_id(this));
1697 for (auto &conn
: new_conn
)
1698 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1699 log_backtrace("-X- ", yosys_xtrace
-1);
1702 connections_
= new_conn
;
1705 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1707 return connections_
;
1710 void RTLIL::Module::fixup_ports()
1712 std::vector
<RTLIL::Wire
*> all_ports
;
1714 for (auto &w
: wires_
)
1715 if (w
.second
->port_input
|| w
.second
->port_output
)
1716 all_ports
.push_back(w
.second
);
1718 w
.second
->port_id
= 0;
1720 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1723 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1724 ports
.push_back(all_ports
[i
]->name
);
1725 all_ports
[i
]->port_id
= i
+1;
1729 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1731 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1733 wire
->width
= width
;
1738 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1740 RTLIL::Wire
*wire
= addWire(name
);
1741 wire
->width
= other
->width
;
1742 wire
->start_offset
= other
->start_offset
;
1743 wire
->port_id
= other
->port_id
;
1744 wire
->port_input
= other
->port_input
;
1745 wire
->port_output
= other
->port_output
;
1746 wire
->upto
= other
->upto
;
1747 wire
->attributes
= other
->attributes
;
1751 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1753 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1760 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1762 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1763 cell
->connections_
= other
->connections_
;
1764 cell
->parameters
= other
->parameters
;
1765 cell
->attributes
= other
->attributes
;
1769 #define DEF_METHOD(_func, _y_size, _type) \
1770 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1771 RTLIL::Cell *cell = addCell(name, _type); \
1772 cell->parameters["\\A_SIGNED"] = is_signed; \
1773 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1774 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1775 cell->setPort("\\A", sig_a); \
1776 cell->setPort("\\Y", sig_y); \
1777 cell->set_src_attribute(src); \
1780 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1781 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1782 add ## _func(name, sig_a, sig_y, is_signed, src); \
1785 DEF_METHOD(Not
, sig_a
.size(), "$not")
1786 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1787 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1788 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1789 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1790 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1791 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1792 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1793 DEF_METHOD(LogicNot
, 1, "$logic_not")
1796 #define DEF_METHOD(_func, _y_size, _type) \
1797 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) { \
1798 RTLIL::Cell *cell = addCell(name, _type); \
1799 cell->parameters["\\A_SIGNED"] = is_signed; \
1800 cell->parameters["\\B_SIGNED"] = is_signed; \
1801 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1802 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1803 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1804 cell->setPort("\\A", sig_a); \
1805 cell->setPort("\\B", sig_b); \
1806 cell->setPort("\\Y", sig_y); \
1807 cell->set_src_attribute(src); \
1810 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1811 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1812 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1815 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), "$and")
1816 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), "$or")
1817 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), "$xor")
1818 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), "$xnor")
1819 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1820 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1821 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1822 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1823 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1824 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1825 DEF_METHOD(Lt
, 1, "$lt")
1826 DEF_METHOD(Le
, 1, "$le")
1827 DEF_METHOD(Eq
, 1, "$eq")
1828 DEF_METHOD(Ne
, 1, "$ne")
1829 DEF_METHOD(Eqx
, 1, "$eqx")
1830 DEF_METHOD(Nex
, 1, "$nex")
1831 DEF_METHOD(Ge
, 1, "$ge")
1832 DEF_METHOD(Gt
, 1, "$gt")
1833 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), "$add")
1834 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), "$sub")
1835 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), "$mul")
1836 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), "$div")
1837 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), "$mod")
1838 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1839 DEF_METHOD(LogicOr
, 1, "$logic_or")
1842 #define DEF_METHOD(_func, _type, _pmux) \
1843 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) { \
1844 RTLIL::Cell *cell = addCell(name, _type); \
1845 cell->parameters["\\WIDTH"] = sig_a.size(); \
1846 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1847 cell->setPort("\\A", sig_a); \
1848 cell->setPort("\\B", sig_b); \
1849 cell->setPort("\\S", sig_s); \
1850 cell->setPort("\\Y", sig_y); \
1851 cell->set_src_attribute(src); \
1854 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1855 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1856 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1859 DEF_METHOD(Mux
, "$mux", 0)
1860 DEF_METHOD(Pmux
, "$pmux", 1)
1863 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1864 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1865 RTLIL::Cell *cell = addCell(name, _type); \
1866 cell->setPort("\\" #_P1, sig1); \
1867 cell->setPort("\\" #_P2, sig2); \
1868 cell->set_src_attribute(src); \
1871 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1872 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1873 add ## _func(name, sig1, sig2, src); \
1876 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1877 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1878 RTLIL::Cell *cell = addCell(name, _type); \
1879 cell->setPort("\\" #_P1, sig1); \
1880 cell->setPort("\\" #_P2, sig2); \
1881 cell->setPort("\\" #_P3, sig3); \
1882 cell->set_src_attribute(src); \
1885 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1886 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1887 add ## _func(name, sig1, sig2, sig3, src); \
1890 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1891 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) { \
1892 RTLIL::Cell *cell = addCell(name, _type); \
1893 cell->setPort("\\" #_P1, sig1); \
1894 cell->setPort("\\" #_P2, sig2); \
1895 cell->setPort("\\" #_P3, sig3); \
1896 cell->setPort("\\" #_P4, sig4); \
1897 cell->set_src_attribute(src); \
1900 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1901 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1902 add ## _func(name, sig1, sig2, sig3, sig4, src); \
1905 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1906 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) { \
1907 RTLIL::Cell *cell = addCell(name, _type); \
1908 cell->setPort("\\" #_P1, sig1); \
1909 cell->setPort("\\" #_P2, sig2); \
1910 cell->setPort("\\" #_P3, sig3); \
1911 cell->setPort("\\" #_P4, sig4); \
1912 cell->setPort("\\" #_P5, sig5); \
1913 cell->set_src_attribute(src); \
1916 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1917 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1918 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
1921 DEF_METHOD_2(BufGate
, "$_BUF_", A
, Y
)
1922 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1923 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1924 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1925 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1926 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1927 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1928 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1929 DEF_METHOD_3(AndnotGate
, "$_ANDNOT_", A
, B
, Y
)
1930 DEF_METHOD_3(OrnotGate
, "$_ORNOT_", A
, B
, Y
)
1931 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1932 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1933 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1934 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1935 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1941 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
)
1943 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1944 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1945 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1946 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1947 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1948 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1949 cell
->setPort("\\A", sig_a
);
1950 cell
->setPort("\\B", sig_b
);
1951 cell
->setPort("\\Y", sig_y
);
1952 cell
->set_src_attribute(src
);
1956 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
1958 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1959 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1960 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1961 cell
->parameters
["\\OFFSET"] = offset
;
1962 cell
->setPort("\\A", sig_a
);
1963 cell
->setPort("\\Y", sig_y
);
1964 cell
->set_src_attribute(src
);
1968 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1970 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1971 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1972 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1973 cell
->setPort("\\A", sig_a
);
1974 cell
->setPort("\\B", sig_b
);
1975 cell
->setPort("\\Y", sig_y
);
1976 cell
->set_src_attribute(src
);
1980 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
1982 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1983 cell
->parameters
["\\LUT"] = lut
;
1984 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1985 cell
->setPort("\\A", sig_a
);
1986 cell
->setPort("\\Y", sig_y
);
1987 cell
->set_src_attribute(src
);
1991 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1993 RTLIL::Cell
*cell
= addCell(name
, "$tribuf");
1994 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1995 cell
->setPort("\\A", sig_a
);
1996 cell
->setPort("\\EN", sig_en
);
1997 cell
->setPort("\\Y", sig_y
);
1998 cell
->set_src_attribute(src
);
2002 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2004 RTLIL::Cell
*cell
= addCell(name
, "$assert");
2005 cell
->setPort("\\A", sig_a
);
2006 cell
->setPort("\\EN", sig_en
);
2007 cell
->set_src_attribute(src
);
2011 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2013 RTLIL::Cell
*cell
= addCell(name
, "$assume");
2014 cell
->setPort("\\A", sig_a
);
2015 cell
->setPort("\\EN", sig_en
);
2016 cell
->set_src_attribute(src
);
2020 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2022 RTLIL::Cell
*cell
= addCell(name
, "$live");
2023 cell
->setPort("\\A", sig_a
);
2024 cell
->setPort("\\EN", sig_en
);
2025 cell
->set_src_attribute(src
);
2029 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2031 RTLIL::Cell
*cell
= addCell(name
, "$fair");
2032 cell
->setPort("\\A", sig_a
);
2033 cell
->setPort("\\EN", sig_en
);
2034 cell
->set_src_attribute(src
);
2038 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2040 RTLIL::Cell
*cell
= addCell(name
, "$cover");
2041 cell
->setPort("\\A", sig_a
);
2042 cell
->setPort("\\EN", sig_en
);
2043 cell
->set_src_attribute(src
);
2047 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2049 RTLIL::Cell
*cell
= addCell(name
, "$equiv");
2050 cell
->setPort("\\A", sig_a
);
2051 cell
->setPort("\\B", sig_b
);
2052 cell
->setPort("\\Y", sig_y
);
2053 cell
->set_src_attribute(src
);
2057 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
)
2059 RTLIL::Cell
*cell
= addCell(name
, "$sr");
2060 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2061 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2062 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2063 cell
->setPort("\\SET", sig_set
);
2064 cell
->setPort("\\CLR", sig_clr
);
2065 cell
->setPort("\\Q", sig_q
);
2066 cell
->set_src_attribute(src
);
2070 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2072 RTLIL::Cell
*cell
= addCell(name
, "$ff");
2073 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2074 cell
->setPort("\\D", sig_d
);
2075 cell
->setPort("\\Q", sig_q
);
2076 cell
->set_src_attribute(src
);
2080 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
)
2082 RTLIL::Cell
*cell
= addCell(name
, "$dff");
2083 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2084 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2085 cell
->setPort("\\CLK", sig_clk
);
2086 cell
->setPort("\\D", sig_d
);
2087 cell
->setPort("\\Q", sig_q
);
2088 cell
->set_src_attribute(src
);
2092 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
)
2094 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
2095 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2096 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2097 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2098 cell
->setPort("\\CLK", sig_clk
);
2099 cell
->setPort("\\EN", sig_en
);
2100 cell
->setPort("\\D", sig_d
);
2101 cell
->setPort("\\Q", sig_q
);
2102 cell
->set_src_attribute(src
);
2106 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2107 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2109 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
2110 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2111 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2112 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2113 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2114 cell
->setPort("\\CLK", sig_clk
);
2115 cell
->setPort("\\SET", sig_set
);
2116 cell
->setPort("\\CLR", sig_clr
);
2117 cell
->setPort("\\D", sig_d
);
2118 cell
->setPort("\\Q", sig_q
);
2119 cell
->set_src_attribute(src
);
2123 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2124 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2126 RTLIL::Cell
*cell
= addCell(name
, "$adff");
2127 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2128 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
2129 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
2130 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2131 cell
->setPort("\\CLK", sig_clk
);
2132 cell
->setPort("\\ARST", sig_arst
);
2133 cell
->setPort("\\D", sig_d
);
2134 cell
->setPort("\\Q", sig_q
);
2135 cell
->set_src_attribute(src
);
2139 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
)
2141 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
2142 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2143 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2144 cell
->setPort("\\EN", sig_en
);
2145 cell
->setPort("\\D", sig_d
);
2146 cell
->setPort("\\Q", sig_q
);
2147 cell
->set_src_attribute(src
);
2151 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2152 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2154 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
2155 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2156 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2157 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2158 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2159 cell
->setPort("\\EN", sig_en
);
2160 cell
->setPort("\\SET", sig_set
);
2161 cell
->setPort("\\CLR", sig_clr
);
2162 cell
->setPort("\\D", sig_d
);
2163 cell
->setPort("\\Q", sig_q
);
2164 cell
->set_src_attribute(src
);
2168 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2170 RTLIL::Cell
*cell
= addCell(name
, "$_FF_");
2171 cell
->setPort("\\D", sig_d
);
2172 cell
->setPort("\\Q", sig_q
);
2173 cell
->set_src_attribute(src
);
2177 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
)
2179 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2180 cell
->setPort("\\C", sig_clk
);
2181 cell
->setPort("\\D", sig_d
);
2182 cell
->setPort("\\Q", sig_q
);
2183 cell
->set_src_attribute(src
);
2187 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
)
2189 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2190 cell
->setPort("\\C", sig_clk
);
2191 cell
->setPort("\\E", sig_en
);
2192 cell
->setPort("\\D", sig_d
);
2193 cell
->setPort("\\Q", sig_q
);
2194 cell
->set_src_attribute(src
);
2198 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2199 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2201 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2202 cell
->setPort("\\C", sig_clk
);
2203 cell
->setPort("\\S", sig_set
);
2204 cell
->setPort("\\R", sig_clr
);
2205 cell
->setPort("\\D", sig_d
);
2206 cell
->setPort("\\Q", sig_q
);
2207 cell
->set_src_attribute(src
);
2211 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2212 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2214 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2215 cell
->setPort("\\C", sig_clk
);
2216 cell
->setPort("\\R", sig_arst
);
2217 cell
->setPort("\\D", sig_d
);
2218 cell
->setPort("\\Q", sig_q
);
2219 cell
->set_src_attribute(src
);
2223 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
)
2225 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2226 cell
->setPort("\\E", sig_en
);
2227 cell
->setPort("\\D", sig_d
);
2228 cell
->setPort("\\Q", sig_q
);
2229 cell
->set_src_attribute(src
);
2233 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2234 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2236 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2237 cell
->setPort("\\E", sig_en
);
2238 cell
->setPort("\\S", sig_set
);
2239 cell
->setPort("\\R", sig_clr
);
2240 cell
->setPort("\\D", sig_d
);
2241 cell
->setPort("\\Q", sig_q
);
2242 cell
->set_src_attribute(src
);
2246 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2248 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2249 Cell
*cell
= addCell(name
, "$anyconst");
2250 cell
->setParam("\\WIDTH", width
);
2251 cell
->setPort("\\Y", sig
);
2252 cell
->set_src_attribute(src
);
2256 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2258 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2259 Cell
*cell
= addCell(name
, "$anyseq");
2260 cell
->setParam("\\WIDTH", width
);
2261 cell
->setPort("\\Y", sig
);
2262 cell
->set_src_attribute(src
);
2266 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2268 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2269 Cell
*cell
= addCell(name
, "$allconst");
2270 cell
->setParam("\\WIDTH", width
);
2271 cell
->setPort("\\Y", sig
);
2272 cell
->set_src_attribute(src
);
2276 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2278 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2279 Cell
*cell
= addCell(name
, "$allseq");
2280 cell
->setParam("\\WIDTH", width
);
2281 cell
->setPort("\\Y", sig
);
2282 cell
->set_src_attribute(src
);
2286 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2288 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2289 Cell
*cell
= addCell(name
, "$initstate");
2290 cell
->setPort("\\Y", sig
);
2291 cell
->set_src_attribute(src
);
2297 static unsigned int hashidx_count
= 123456789;
2298 hashidx_count
= mkhash_xorshift(hashidx_count
);
2299 hashidx_
= hashidx_count
;
2306 port_output
= false;
2310 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2314 RTLIL::Wire::~Wire()
2317 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2322 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2323 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2329 RTLIL::Memory::Memory()
2331 static unsigned int hashidx_count
= 123456789;
2332 hashidx_count
= mkhash_xorshift(hashidx_count
);
2333 hashidx_
= hashidx_count
;
2339 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2343 RTLIL::Cell::Cell() : module(nullptr)
2345 static unsigned int hashidx_count
= 123456789;
2346 hashidx_count
= mkhash_xorshift(hashidx_count
);
2347 hashidx_
= hashidx_count
;
2349 // log("#memtrace# %p\n", this);
2353 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2357 RTLIL::Cell::~Cell()
2360 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2365 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2366 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2372 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2374 return connections_
.count(portname
) != 0;
2377 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2379 RTLIL::SigSpec signal
;
2380 auto conn_it
= connections_
.find(portname
);
2382 if (conn_it
!= connections_
.end())
2384 for (auto mon
: module
->monitors
)
2385 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2388 for (auto mon
: module
->design
->monitors
)
2389 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2392 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2393 log_backtrace("-X- ", yosys_xtrace
-1);
2396 connections_
.erase(conn_it
);
2400 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2402 auto conn_it
= connections_
.find(portname
);
2404 if (conn_it
== connections_
.end()) {
2405 connections_
[portname
] = RTLIL::SigSpec();
2406 conn_it
= connections_
.find(portname
);
2407 log_assert(conn_it
!= connections_
.end());
2409 if (conn_it
->second
== signal
)
2412 for (auto mon
: module
->monitors
)
2413 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2416 for (auto mon
: module
->design
->monitors
)
2417 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2420 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2421 log_backtrace("-X- ", yosys_xtrace
-1);
2424 conn_it
->second
= signal
;
2427 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2429 return connections_
.at(portname
);
2432 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2434 return connections_
;
2437 bool RTLIL::Cell::known() const
2439 if (yosys_celltypes
.cell_known(type
))
2441 if (module
&& module
->design
&& module
->design
->module(type
))
2446 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2448 if (yosys_celltypes
.cell_known(type
))
2449 return yosys_celltypes
.cell_input(type
, portname
);
2450 if (module
&& module
->design
) {
2451 RTLIL::Module
*m
= module
->design
->module(type
);
2452 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2453 return w
&& w
->port_input
;
2458 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2460 if (yosys_celltypes
.cell_known(type
))
2461 return yosys_celltypes
.cell_output(type
, portname
);
2462 if (module
&& module
->design
) {
2463 RTLIL::Module
*m
= module
->design
->module(type
);
2464 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2465 return w
&& w
->port_output
;
2470 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2472 return parameters
.count(paramname
) != 0;
2475 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2477 parameters
.erase(paramname
);
2480 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2482 parameters
[paramname
] = value
;
2485 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2487 return parameters
.at(paramname
);
2490 void RTLIL::Cell::sort()
2492 connections_
.sort(sort_by_id_str());
2493 parameters
.sort(sort_by_id_str());
2494 attributes
.sort(sort_by_id_str());
2497 void RTLIL::Cell::check()
2500 InternalCellChecker
checker(NULL
, this);
2505 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2507 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" || type
.substr(0,10) == "$fmcombine" ||
2508 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
2511 if (type
== "$mux" || type
== "$pmux") {
2512 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2513 if (type
== "$pmux")
2514 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
2519 if (type
== "$lut" || type
== "$sop") {
2520 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
2524 if (type
== "$fa") {
2525 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2529 if (type
== "$lcu") {
2530 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
2534 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
2536 if (connections_
.count("\\A")) {
2537 if (signedness_ab
) {
2539 parameters
["\\A_SIGNED"] = true;
2540 else if (parameters
.count("\\A_SIGNED") == 0)
2541 parameters
["\\A_SIGNED"] = false;
2543 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
2546 if (connections_
.count("\\B")) {
2547 if (signedness_ab
) {
2549 parameters
["\\B_SIGNED"] = true;
2550 else if (parameters
.count("\\B_SIGNED") == 0)
2551 parameters
["\\B_SIGNED"] = false;
2553 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
2556 if (connections_
.count("\\Y"))
2557 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
2559 if (connections_
.count("\\Q"))
2560 parameters
["\\WIDTH"] = GetSize(connections_
["\\Q"]);
2565 RTLIL::SigChunk::SigChunk()
2572 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2576 width
= GetSize(data
);
2580 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2582 log_assert(wire
!= nullptr);
2584 this->width
= wire
->width
;
2588 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2590 log_assert(wire
!= nullptr);
2592 this->width
= width
;
2593 this->offset
= offset
;
2596 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2599 data
= RTLIL::Const(str
).bits
;
2600 width
= GetSize(data
);
2604 RTLIL::SigChunk::SigChunk(int val
, int width
)
2607 data
= RTLIL::Const(val
, width
).bits
;
2608 this->width
= GetSize(data
);
2612 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2615 data
= RTLIL::Const(bit
, width
).bits
;
2616 this->width
= GetSize(data
);
2620 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2625 data
= RTLIL::Const(bit
.data
).bits
;
2627 offset
= bit
.offset
;
2631 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2633 wire
= sigchunk
.wire
;
2634 data
= sigchunk
.data
;
2635 width
= sigchunk
.width
;
2636 offset
= sigchunk
.offset
;
2639 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2641 RTLIL::SigChunk ret
;
2644 ret
.offset
= this->offset
+ offset
;
2647 for (int i
= 0; i
< length
; i
++)
2648 ret
.data
.push_back(data
[offset
+i
]);
2654 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2656 if (wire
&& other
.wire
)
2657 if (wire
->name
!= other
.wire
->name
)
2658 return wire
->name
< other
.wire
->name
;
2660 if (wire
!= other
.wire
)
2661 return wire
< other
.wire
;
2663 if (offset
!= other
.offset
)
2664 return offset
< other
.offset
;
2666 if (width
!= other
.width
)
2667 return width
< other
.width
;
2669 return data
< other
.data
;
2672 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2674 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2677 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2684 RTLIL::SigSpec::SigSpec()
2690 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2695 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2697 cover("kernel.rtlil.sigspec.init.list");
2702 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2703 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2707 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2709 cover("kernel.rtlil.sigspec.assign");
2711 width_
= other
.width_
;
2712 hash_
= other
.hash_
;
2713 chunks_
= other
.chunks_
;
2716 if (!other
.bits_
.empty())
2718 RTLIL::SigChunk
*last
= NULL
;
2719 int last_end_offset
= 0;
2721 for (auto &bit
: other
.bits_
) {
2722 if (last
&& bit
.wire
== last
->wire
) {
2723 if (bit
.wire
== NULL
) {
2724 last
->data
.push_back(bit
.data
);
2727 } else if (last_end_offset
== bit
.offset
) {
2733 chunks_
.push_back(bit
);
2734 last
= &chunks_
.back();
2735 last_end_offset
= bit
.offset
+ 1;
2744 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2746 cover("kernel.rtlil.sigspec.init.const");
2748 chunks_
.push_back(RTLIL::SigChunk(value
));
2749 width_
= chunks_
.back().width
;
2754 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2756 cover("kernel.rtlil.sigspec.init.chunk");
2758 chunks_
.push_back(chunk
);
2759 width_
= chunks_
.back().width
;
2764 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2766 cover("kernel.rtlil.sigspec.init.wire");
2768 chunks_
.push_back(RTLIL::SigChunk(wire
));
2769 width_
= chunks_
.back().width
;
2774 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2776 cover("kernel.rtlil.sigspec.init.wire_part");
2778 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2779 width_
= chunks_
.back().width
;
2784 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2786 cover("kernel.rtlil.sigspec.init.str");
2788 chunks_
.push_back(RTLIL::SigChunk(str
));
2789 width_
= chunks_
.back().width
;
2794 RTLIL::SigSpec::SigSpec(int val
, int width
)
2796 cover("kernel.rtlil.sigspec.init.int");
2798 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2804 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2806 cover("kernel.rtlil.sigspec.init.state");
2808 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2814 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2816 cover("kernel.rtlil.sigspec.init.bit");
2818 if (bit
.wire
== NULL
)
2819 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2821 for (int i
= 0; i
< width
; i
++)
2822 chunks_
.push_back(bit
);
2828 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2830 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2834 for (auto &c
: chunks
)
2839 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2841 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2845 for (auto &bit
: bits
)
2850 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2852 cover("kernel.rtlil.sigspec.init.pool_bits");
2856 for (auto &bit
: bits
)
2861 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2863 cover("kernel.rtlil.sigspec.init.stdset_bits");
2867 for (auto &bit
: bits
)
2872 RTLIL::SigSpec::SigSpec(bool bit
)
2874 cover("kernel.rtlil.sigspec.init.bool");
2882 void RTLIL::SigSpec::pack() const
2884 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2886 if (that
->bits_
.empty())
2889 cover("kernel.rtlil.sigspec.convert.pack");
2890 log_assert(that
->chunks_
.empty());
2892 std::vector
<RTLIL::SigBit
> old_bits
;
2893 old_bits
.swap(that
->bits_
);
2895 RTLIL::SigChunk
*last
= NULL
;
2896 int last_end_offset
= 0;
2898 for (auto &bit
: old_bits
) {
2899 if (last
&& bit
.wire
== last
->wire
) {
2900 if (bit
.wire
== NULL
) {
2901 last
->data
.push_back(bit
.data
);
2904 } else if (last_end_offset
== bit
.offset
) {
2910 that
->chunks_
.push_back(bit
);
2911 last
= &that
->chunks_
.back();
2912 last_end_offset
= bit
.offset
+ 1;
2918 void RTLIL::SigSpec::unpack() const
2920 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2922 if (that
->chunks_
.empty())
2925 cover("kernel.rtlil.sigspec.convert.unpack");
2926 log_assert(that
->bits_
.empty());
2928 that
->bits_
.reserve(that
->width_
);
2929 for (auto &c
: that
->chunks_
)
2930 for (int i
= 0; i
< c
.width
; i
++)
2931 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2933 that
->chunks_
.clear();
2937 void RTLIL::SigSpec::updhash() const
2939 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2941 if (that
->hash_
!= 0)
2944 cover("kernel.rtlil.sigspec.hash");
2947 that
->hash_
= mkhash_init
;
2948 for (auto &c
: that
->chunks_
)
2949 if (c
.wire
== NULL
) {
2950 for (auto &v
: c
.data
)
2951 that
->hash_
= mkhash(that
->hash_
, v
);
2953 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
2954 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
2955 that
->hash_
= mkhash(that
->hash_
, c
.width
);
2958 if (that
->hash_
== 0)
2962 void RTLIL::SigSpec::sort()
2965 cover("kernel.rtlil.sigspec.sort");
2966 std::sort(bits_
.begin(), bits_
.end());
2969 void RTLIL::SigSpec::sort_and_unify()
2972 cover("kernel.rtlil.sigspec.sort_and_unify");
2974 // A copy of the bits vector is used to prevent duplicating the logic from
2975 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
2976 // that isn't showing up as significant in profiles.
2977 std::vector
<SigBit
> unique_bits
= bits_
;
2978 std::sort(unique_bits
.begin(), unique_bits
.end());
2979 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
2980 unique_bits
.erase(last
, unique_bits
.end());
2982 *this = unique_bits
;
2985 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2987 replace(pattern
, with
, this);
2990 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2992 log_assert(other
!= NULL
);
2993 log_assert(width_
== other
->width_
);
2994 log_assert(pattern
.width_
== with
.width_
);
3001 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3002 if (pattern
.bits_
[i
].wire
!= NULL
) {
3003 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3004 if (bits_
[j
] == pattern
.bits_
[i
]) {
3005 other
->bits_
[j
] = with
.bits_
[i
];
3014 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3016 replace(rules
, this);
3019 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3021 cover("kernel.rtlil.sigspec.replace_dict");
3023 log_assert(other
!= NULL
);
3024 log_assert(width_
== other
->width_
);
3029 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3030 auto it
= rules
.find(bits_
[i
]);
3031 if (it
!= rules
.end())
3032 other
->bits_
[i
] = it
->second
;
3038 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3040 replace(rules
, this);
3043 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3045 cover("kernel.rtlil.sigspec.replace_map");
3047 log_assert(other
!= NULL
);
3048 log_assert(width_
== other
->width_
);
3053 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3054 auto it
= rules
.find(bits_
[i
]);
3055 if (it
!= rules
.end())
3056 other
->bits_
[i
] = it
->second
;
3062 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3064 remove2(pattern
, NULL
);
3067 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3069 RTLIL::SigSpec tmp
= *this;
3070 tmp
.remove2(pattern
, other
);
3073 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3076 cover("kernel.rtlil.sigspec.remove_other");
3078 cover("kernel.rtlil.sigspec.remove");
3081 if (other
!= NULL
) {
3082 log_assert(width_
== other
->width_
);
3086 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3088 if (bits_
[i
].wire
== NULL
) continue;
3090 for (auto &pattern_chunk
: pattern
.chunks())
3091 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3092 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3093 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3094 bits_
.erase(bits_
.begin() + i
);
3096 if (other
!= NULL
) {
3097 other
->bits_
.erase(other
->bits_
.begin() + i
);
3107 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3109 remove2(pattern
, NULL
);
3112 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3114 RTLIL::SigSpec tmp
= *this;
3115 tmp
.remove2(pattern
, other
);
3118 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3121 cover("kernel.rtlil.sigspec.remove_other");
3123 cover("kernel.rtlil.sigspec.remove");
3127 if (other
!= NULL
) {
3128 log_assert(width_
== other
->width_
);
3132 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3133 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3134 bits_
.erase(bits_
.begin() + i
);
3136 if (other
!= NULL
) {
3137 other
->bits_
.erase(other
->bits_
.begin() + i
);
3146 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3149 cover("kernel.rtlil.sigspec.remove_other");
3151 cover("kernel.rtlil.sigspec.remove");
3155 if (other
!= NULL
) {
3156 log_assert(width_
== other
->width_
);
3160 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3161 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3162 bits_
.erase(bits_
.begin() + i
);
3164 if (other
!= NULL
) {
3165 other
->bits_
.erase(other
->bits_
.begin() + i
);
3174 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3177 cover("kernel.rtlil.sigspec.extract_other");
3179 cover("kernel.rtlil.sigspec.extract");
3181 log_assert(other
== NULL
|| width_
== other
->width_
);
3184 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3186 for (auto& pattern_chunk
: pattern
.chunks()) {
3188 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3189 for (int i
= 0; i
< width_
; i
++)
3190 if (bits_match
[i
].wire
&&
3191 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3192 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3193 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3194 ret
.append_bit(bits_other
[i
]);
3196 for (int i
= 0; i
< width_
; i
++)
3197 if (bits_match
[i
].wire
&&
3198 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3199 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3200 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3201 ret
.append_bit(bits_match
[i
]);
3209 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3212 cover("kernel.rtlil.sigspec.extract_other");
3214 cover("kernel.rtlil.sigspec.extract");
3216 log_assert(other
== NULL
|| width_
== other
->width_
);
3218 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3222 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3223 for (int i
= 0; i
< width_
; i
++)
3224 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3225 ret
.append_bit(bits_other
[i
]);
3227 for (int i
= 0; i
< width_
; i
++)
3228 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3229 ret
.append_bit(bits_match
[i
]);
3236 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3238 cover("kernel.rtlil.sigspec.replace_pos");
3243 log_assert(offset
>= 0);
3244 log_assert(with
.width_
>= 0);
3245 log_assert(offset
+with
.width_
<= width_
);
3247 for (int i
= 0; i
< with
.width_
; i
++)
3248 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3253 void RTLIL::SigSpec::remove_const()
3257 cover("kernel.rtlil.sigspec.remove_const.packed");
3259 std::vector
<RTLIL::SigChunk
> new_chunks
;
3260 new_chunks
.reserve(GetSize(chunks_
));
3263 for (auto &chunk
: chunks_
)
3264 if (chunk
.wire
!= NULL
) {
3265 new_chunks
.push_back(chunk
);
3266 width_
+= chunk
.width
;
3269 chunks_
.swap(new_chunks
);
3273 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3275 std::vector
<RTLIL::SigBit
> new_bits
;
3276 new_bits
.reserve(width_
);
3278 for (auto &bit
: bits_
)
3279 if (bit
.wire
!= NULL
)
3280 new_bits
.push_back(bit
);
3282 bits_
.swap(new_bits
);
3283 width_
= bits_
.size();
3289 void RTLIL::SigSpec::remove(int offset
, int length
)
3291 cover("kernel.rtlil.sigspec.remove_pos");
3295 log_assert(offset
>= 0);
3296 log_assert(length
>= 0);
3297 log_assert(offset
+ length
<= width_
);
3299 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3300 width_
= bits_
.size();
3305 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3308 cover("kernel.rtlil.sigspec.extract_pos");
3309 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3312 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3314 if (signal
.width_
== 0)
3322 cover("kernel.rtlil.sigspec.append");
3324 if (packed() != signal
.packed()) {
3330 for (auto &other_c
: signal
.chunks_
)
3332 auto &my_last_c
= chunks_
.back();
3333 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3334 auto &this_data
= my_last_c
.data
;
3335 auto &other_data
= other_c
.data
;
3336 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3337 my_last_c
.width
+= other_c
.width
;
3339 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3340 my_last_c
.width
+= other_c
.width
;
3342 chunks_
.push_back(other_c
);
3345 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3347 width_
+= signal
.width_
;
3351 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3355 cover("kernel.rtlil.sigspec.append_bit.packed");
3357 if (chunks_
.size() == 0)
3358 chunks_
.push_back(bit
);
3360 if (bit
.wire
== NULL
)
3361 if (chunks_
.back().wire
== NULL
) {
3362 chunks_
.back().data
.push_back(bit
.data
);
3363 chunks_
.back().width
++;
3365 chunks_
.push_back(bit
);
3367 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3368 chunks_
.back().width
++;
3370 chunks_
.push_back(bit
);
3374 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3375 bits_
.push_back(bit
);
3382 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3384 cover("kernel.rtlil.sigspec.extend_u0");
3389 remove(width
, width_
- width
);
3391 if (width_
< width
) {
3392 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3394 padding
= RTLIL::State::S0
;
3395 while (width_
< width
)
3401 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3403 cover("kernel.rtlil.sigspec.repeat");
3406 for (int i
= 0; i
< num
; i
++)
3412 void RTLIL::SigSpec::check() const
3416 cover("kernel.rtlil.sigspec.check.skip");
3420 cover("kernel.rtlil.sigspec.check.packed");
3423 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3424 const RTLIL::SigChunk chunk
= chunks_
[i
];
3425 if (chunk
.wire
== NULL
) {
3427 log_assert(chunks_
[i
-1].wire
!= NULL
);
3428 log_assert(chunk
.offset
== 0);
3429 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3431 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3432 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3433 log_assert(chunk
.offset
>= 0);
3434 log_assert(chunk
.width
>= 0);
3435 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3436 log_assert(chunk
.data
.size() == 0);
3440 log_assert(w
== width_
);
3441 log_assert(bits_
.empty());
3445 cover("kernel.rtlil.sigspec.check.unpacked");
3447 log_assert(width_
== GetSize(bits_
));
3448 log_assert(chunks_
.empty());
3453 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3455 cover("kernel.rtlil.sigspec.comp_lt");
3460 if (width_
!= other
.width_
)
3461 return width_
< other
.width_
;
3466 if (chunks_
.size() != other
.chunks_
.size())
3467 return chunks_
.size() < other
.chunks_
.size();
3472 if (hash_
!= other
.hash_
)
3473 return hash_
< other
.hash_
;
3475 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3476 if (chunks_
[i
] != other
.chunks_
[i
]) {
3477 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3478 return chunks_
[i
] < other
.chunks_
[i
];
3481 cover("kernel.rtlil.sigspec.comp_lt.equal");
3485 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3487 cover("kernel.rtlil.sigspec.comp_eq");
3492 if (width_
!= other
.width_
)
3498 if (chunks_
.size() != chunks_
.size())
3504 if (hash_
!= other
.hash_
)
3507 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3508 if (chunks_
[i
] != other
.chunks_
[i
]) {
3509 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3513 cover("kernel.rtlil.sigspec.comp_eq.equal");
3517 bool RTLIL::SigSpec::is_wire() const
3519 cover("kernel.rtlil.sigspec.is_wire");
3522 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3525 bool RTLIL::SigSpec::is_chunk() const
3527 cover("kernel.rtlil.sigspec.is_chunk");
3530 return GetSize(chunks_
) == 1;
3533 bool RTLIL::SigSpec::is_fully_const() const
3535 cover("kernel.rtlil.sigspec.is_fully_const");
3538 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3539 if (it
->width
> 0 && it
->wire
!= NULL
)
3544 bool RTLIL::SigSpec::is_fully_zero() const
3546 cover("kernel.rtlil.sigspec.is_fully_zero");
3549 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3550 if (it
->width
> 0 && it
->wire
!= NULL
)
3552 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3553 if (it
->data
[i
] != RTLIL::State::S0
)
3559 bool RTLIL::SigSpec::is_fully_ones() const
3561 cover("kernel.rtlil.sigspec.is_fully_ones");
3564 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3565 if (it
->width
> 0 && it
->wire
!= NULL
)
3567 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3568 if (it
->data
[i
] != RTLIL::State::S1
)
3574 bool RTLIL::SigSpec::is_fully_def() const
3576 cover("kernel.rtlil.sigspec.is_fully_def");
3579 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3580 if (it
->width
> 0 && it
->wire
!= NULL
)
3582 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3583 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3589 bool RTLIL::SigSpec::is_fully_undef() const
3591 cover("kernel.rtlil.sigspec.is_fully_undef");
3594 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3595 if (it
->width
> 0 && it
->wire
!= NULL
)
3597 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3598 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3604 bool RTLIL::SigSpec::has_const() const
3606 cover("kernel.rtlil.sigspec.has_const");
3609 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3610 if (it
->width
> 0 && it
->wire
== NULL
)
3615 bool RTLIL::SigSpec::has_marked_bits() const
3617 cover("kernel.rtlil.sigspec.has_marked_bits");
3620 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3621 if (it
->width
> 0 && it
->wire
== NULL
) {
3622 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3623 if (it
->data
[i
] == RTLIL::State::Sm
)
3629 bool RTLIL::SigSpec::as_bool() const
3631 cover("kernel.rtlil.sigspec.as_bool");
3634 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3636 return RTLIL::Const(chunks_
[0].data
).as_bool();
3640 int RTLIL::SigSpec::as_int(bool is_signed
) const
3642 cover("kernel.rtlil.sigspec.as_int");
3645 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3647 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3651 std::string
RTLIL::SigSpec::as_string() const
3653 cover("kernel.rtlil.sigspec.as_string");
3657 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3658 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3659 if (chunk
.wire
!= NULL
)
3660 for (int j
= 0; j
< chunk
.width
; j
++)
3663 str
+= RTLIL::Const(chunk
.data
).as_string();
3668 RTLIL::Const
RTLIL::SigSpec::as_const() const
3670 cover("kernel.rtlil.sigspec.as_const");
3673 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3675 return chunks_
[0].data
;
3676 return RTLIL::Const();
3679 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3681 cover("kernel.rtlil.sigspec.as_wire");
3684 log_assert(is_wire());
3685 return chunks_
[0].wire
;
3688 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3690 cover("kernel.rtlil.sigspec.as_chunk");
3693 log_assert(is_chunk());
3697 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3699 cover("kernel.rtlil.sigspec.as_bit");
3701 log_assert(width_
== 1);
3703 return RTLIL::SigBit(*chunks_
.begin());
3708 bool RTLIL::SigSpec::match(std::string pattern
) const
3710 cover("kernel.rtlil.sigspec.match");
3713 std::string str
= as_string();
3714 log_assert(pattern
.size() == str
.size());
3716 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3717 if (pattern
[i
] == ' ')
3719 if (pattern
[i
] == '*') {
3720 if (str
[i
] != 'z' && str
[i
] != 'x')
3724 if (pattern
[i
] != str
[i
])
3731 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3733 cover("kernel.rtlil.sigspec.to_sigbit_set");
3736 std::set
<RTLIL::SigBit
> sigbits
;
3737 for (auto &c
: chunks_
)
3738 for (int i
= 0; i
< c
.width
; i
++)
3739 sigbits
.insert(RTLIL::SigBit(c
, i
));
3743 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3745 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3748 pool
<RTLIL::SigBit
> sigbits
;
3749 for (auto &c
: chunks_
)
3750 for (int i
= 0; i
< c
.width
; i
++)
3751 sigbits
.insert(RTLIL::SigBit(c
, i
));
3755 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3757 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3763 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3765 cover("kernel.rtlil.sigspec.to_sigbit_map");
3770 log_assert(width_
== other
.width_
);
3772 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3773 for (int i
= 0; i
< width_
; i
++)
3774 new_map
[bits_
[i
]] = other
.bits_
[i
];
3779 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3781 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3786 log_assert(width_
== other
.width_
);
3788 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3789 for (int i
= 0; i
< width_
; i
++)
3790 new_map
[bits_
[i
]] = other
.bits_
[i
];
3795 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3797 size_t start
= 0, end
= 0;
3798 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3799 tokens
.push_back(text
.substr(start
, end
- start
));
3802 tokens
.push_back(text
.substr(start
));
3805 static int sigspec_parse_get_dummy_line_num()
3810 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3812 cover("kernel.rtlil.sigspec.parse");
3814 AST::current_filename
= "input";
3815 AST::use_internal_line_num();
3816 AST::set_line_num(0);
3818 std::vector
<std::string
> tokens
;
3819 sigspec_parse_split(tokens
, str
, ',');
3821 sig
= RTLIL::SigSpec();
3822 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3824 std::string netname
= tokens
[tokidx
];
3825 std::string indices
;
3827 if (netname
.size() == 0)
3830 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3831 cover("kernel.rtlil.sigspec.parse.const");
3832 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3833 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3836 sig
.append(RTLIL::Const(ast
->bits
));
3844 cover("kernel.rtlil.sigspec.parse.net");
3846 if (netname
[0] != '$' && netname
[0] != '\\')
3847 netname
= "\\" + netname
;
3849 if (module
->wires_
.count(netname
) == 0) {
3850 size_t indices_pos
= netname
.size()-1;
3851 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3854 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3855 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3857 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3859 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3860 indices
= netname
.substr(indices_pos
);
3861 netname
= netname
.substr(0, indices_pos
);
3866 if (module
->wires_
.count(netname
) == 0)
3869 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3870 if (!indices
.empty()) {
3871 std::vector
<std::string
> index_tokens
;
3872 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3873 if (index_tokens
.size() == 1) {
3874 cover("kernel.rtlil.sigspec.parse.bit_sel");
3875 int a
= atoi(index_tokens
.at(0).c_str());
3876 if (a
< 0 || a
>= wire
->width
)
3878 sig
.append(RTLIL::SigSpec(wire
, a
));
3880 cover("kernel.rtlil.sigspec.parse.part_sel");
3881 int a
= atoi(index_tokens
.at(0).c_str());
3882 int b
= atoi(index_tokens
.at(1).c_str());
3887 if (a
< 0 || a
>= wire
->width
)
3889 if (b
< 0 || b
>= wire
->width
)
3891 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3900 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3902 if (str
.empty() || str
[0] != '@')
3903 return parse(sig
, module
, str
);
3905 cover("kernel.rtlil.sigspec.parse.sel");
3907 str
= RTLIL::escape_id(str
.substr(1));
3908 if (design
->selection_vars
.count(str
) == 0)
3911 sig
= RTLIL::SigSpec();
3912 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3913 for (auto &it
: module
->wires_
)
3914 if (sel
.selected_member(module
->name
, it
.first
))
3915 sig
.append(it
.second
);
3920 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3923 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3924 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3929 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3930 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3934 if (lhs
.chunks_
.size() == 1) {
3935 char *p
= (char*)str
.c_str(), *endptr
;
3936 long int val
= strtol(p
, &endptr
, 10);
3937 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3938 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3939 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3944 return parse(sig
, module
, str
);
3947 RTLIL::CaseRule::~CaseRule()
3949 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3953 bool RTLIL::CaseRule::empty() const
3955 return actions
.empty() && switches
.empty();
3958 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3960 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3961 new_caserule
->compare
= compare
;
3962 new_caserule
->actions
= actions
;
3963 for (auto &it
: switches
)
3964 new_caserule
->switches
.push_back(it
->clone());
3965 return new_caserule
;
3968 RTLIL::SwitchRule::~SwitchRule()
3970 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3974 bool RTLIL::SwitchRule::empty() const
3976 return cases
.empty();
3979 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3981 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3982 new_switchrule
->signal
= signal
;
3983 new_switchrule
->attributes
= attributes
;
3984 for (auto &it
: cases
)
3985 new_switchrule
->cases
.push_back(it
->clone());
3986 return new_switchrule
;
3990 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3992 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3993 new_syncrule
->type
= type
;
3994 new_syncrule
->signal
= signal
;
3995 new_syncrule
->actions
= actions
;
3996 return new_syncrule
;
3999 RTLIL::Process::~Process()
4001 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4005 RTLIL::Process
*RTLIL::Process::clone() const
4007 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4009 new_proc
->name
= name
;
4010 new_proc
->attributes
= attributes
;
4012 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4013 new_proc
->root_case
= *rc_ptr
;
4014 rc_ptr
->switches
.clear();
4017 for (auto &it
: syncs
)
4018 new_proc
->syncs
.push_back(it
->clone());
4024 RTLIL::Memory::~Memory()
4026 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4028 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4029 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4031 return &all_memorys
;