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
== "$_BUF_") { check_gate("AY"); return; }
1198 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
1199 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
1200 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
1201 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
1202 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
1203 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
1204 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
1205 if (cell
->type
== "$_ANDNOT_") { check_gate("ABY"); return; }
1206 if (cell
->type
== "$_ORNOT_") { check_gate("ABY"); return; }
1207 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
1208 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
1209 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
1210 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
1211 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
1213 if (cell
->type
== "$_TBUF_") { check_gate("AYE"); return; }
1215 if (cell
->type
== "$_MUX4_") { check_gate("ABCDSTY"); return; }
1216 if (cell
->type
== "$_MUX8_") { check_gate("ABCDEFGHSTUY"); return; }
1217 if (cell
->type
== "$_MUX16_") { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1219 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
1220 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
1221 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
1222 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
1224 if (cell
->type
== "$_FF_") { check_gate("DQ"); return; }
1225 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
1226 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
1228 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
1229 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
1230 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
1231 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
1233 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
1234 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
1235 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
1236 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
1237 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
1238 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
1239 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
1240 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
1242 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
1243 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
1244 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
1245 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
1246 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
1247 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
1248 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
1249 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
1251 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
1252 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
1254 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
1255 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
1256 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
1257 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
1258 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
1259 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
1260 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
1261 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
1269 void RTLIL::Module::sort()
1271 wires_
.sort(sort_by_id_str());
1272 cells_
.sort(sort_by_id_str());
1273 avail_parameters
.sort(sort_by_id_str());
1274 memories
.sort(sort_by_id_str());
1275 processes
.sort(sort_by_id_str());
1276 for (auto &it
: cells_
)
1278 for (auto &it
: wires_
)
1279 it
.second
->attributes
.sort(sort_by_id_str());
1280 for (auto &it
: memories
)
1281 it
.second
->attributes
.sort(sort_by_id_str());
1284 void RTLIL::Module::check()
1287 std::vector
<bool> ports_declared
;
1288 for (auto &it
: wires_
) {
1289 log_assert(this == it
.second
->module
);
1290 log_assert(it
.first
== it
.second
->name
);
1291 log_assert(!it
.first
.empty());
1292 log_assert(it
.second
->width
>= 0);
1293 log_assert(it
.second
->port_id
>= 0);
1294 for (auto &it2
: it
.second
->attributes
)
1295 log_assert(!it2
.first
.empty());
1296 if (it
.second
->port_id
) {
1297 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1298 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1299 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1300 if (GetSize(ports_declared
) < it
.second
->port_id
)
1301 ports_declared
.resize(it
.second
->port_id
);
1302 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1303 ports_declared
[it
.second
->port_id
-1] = true;
1305 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1307 for (auto port_declared
: ports_declared
)
1308 log_assert(port_declared
== true);
1309 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1311 for (auto &it
: memories
) {
1312 log_assert(it
.first
== it
.second
->name
);
1313 log_assert(!it
.first
.empty());
1314 log_assert(it
.second
->width
>= 0);
1315 log_assert(it
.second
->size
>= 0);
1316 for (auto &it2
: it
.second
->attributes
)
1317 log_assert(!it2
.first
.empty());
1320 for (auto &it
: cells_
) {
1321 log_assert(this == it
.second
->module
);
1322 log_assert(it
.first
== it
.second
->name
);
1323 log_assert(!it
.first
.empty());
1324 log_assert(!it
.second
->type
.empty());
1325 for (auto &it2
: it
.second
->connections()) {
1326 log_assert(!it2
.first
.empty());
1329 for (auto &it2
: it
.second
->attributes
)
1330 log_assert(!it2
.first
.empty());
1331 for (auto &it2
: it
.second
->parameters
)
1332 log_assert(!it2
.first
.empty());
1333 InternalCellChecker
checker(this, it
.second
);
1337 for (auto &it
: processes
) {
1338 log_assert(it
.first
== it
.second
->name
);
1339 log_assert(!it
.first
.empty());
1340 // FIXME: More checks here..
1343 for (auto &it
: connections_
) {
1344 log_assert(it
.first
.size() == it
.second
.size());
1345 log_assert(!it
.first
.has_const());
1350 for (auto &it
: attributes
)
1351 log_assert(!it
.first
.empty());
1355 void RTLIL::Module::optimize()
1359 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1361 log_assert(new_mod
->refcount_wires_
== 0);
1362 log_assert(new_mod
->refcount_cells_
== 0);
1364 new_mod
->avail_parameters
= avail_parameters
;
1366 for (auto &conn
: connections_
)
1367 new_mod
->connect(conn
);
1369 for (auto &attr
: attributes
)
1370 new_mod
->attributes
[attr
.first
] = attr
.second
;
1372 for (auto &it
: wires_
)
1373 new_mod
->addWire(it
.first
, it
.second
);
1375 for (auto &it
: memories
)
1376 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1378 for (auto &it
: cells_
)
1379 new_mod
->addCell(it
.first
, it
.second
);
1381 for (auto &it
: processes
)
1382 new_mod
->processes
[it
.first
] = it
.second
->clone();
1384 struct RewriteSigSpecWorker
1387 void operator()(RTLIL::SigSpec
&sig
)
1389 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1390 for (auto &c
: chunks
)
1392 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1397 RewriteSigSpecWorker rewriteSigSpecWorker
;
1398 rewriteSigSpecWorker
.mod
= new_mod
;
1399 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1400 new_mod
->fixup_ports();
1403 RTLIL::Module
*RTLIL::Module::clone() const
1405 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1406 new_mod
->name
= name
;
1411 bool RTLIL::Module::has_memories() const
1413 return !memories
.empty();
1416 bool RTLIL::Module::has_processes() const
1418 return !processes
.empty();
1421 bool RTLIL::Module::has_memories_warn() const
1423 if (!memories
.empty())
1424 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1425 return !memories
.empty();
1428 bool RTLIL::Module::has_processes_warn() const
1430 if (!processes
.empty())
1431 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1432 return !processes
.empty();
1435 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1437 std::vector
<RTLIL::Wire
*> result
;
1438 result
.reserve(wires_
.size());
1439 for (auto &it
: wires_
)
1440 if (design
->selected(this, it
.second
))
1441 result
.push_back(it
.second
);
1445 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1447 std::vector
<RTLIL::Cell
*> result
;
1448 result
.reserve(wires_
.size());
1449 for (auto &it
: cells_
)
1450 if (design
->selected(this, it
.second
))
1451 result
.push_back(it
.second
);
1455 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1457 log_assert(!wire
->name
.empty());
1458 log_assert(count_id(wire
->name
) == 0);
1459 log_assert(refcount_wires_
== 0);
1460 wires_
[wire
->name
] = wire
;
1461 wire
->module
= this;
1464 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1466 log_assert(!cell
->name
.empty());
1467 log_assert(count_id(cell
->name
) == 0);
1468 log_assert(refcount_cells_
== 0);
1469 cells_
[cell
->name
] = cell
;
1470 cell
->module
= this;
1474 struct DeleteWireWorker
1476 RTLIL::Module
*module
;
1477 const pool
<RTLIL::Wire
*> *wires_p
;
1479 void operator()(RTLIL::SigSpec
&sig
) {
1480 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1481 for (auto &c
: chunks
)
1482 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1483 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1491 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1493 log_assert(refcount_wires_
== 0);
1495 DeleteWireWorker delete_wire_worker
;
1496 delete_wire_worker
.module
= this;
1497 delete_wire_worker
.wires_p
= &wires
;
1498 rewrite_sigspecs(delete_wire_worker
);
1500 for (auto &it
: wires
) {
1501 log_assert(wires_
.count(it
->name
) != 0);
1502 wires_
.erase(it
->name
);
1507 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1509 while (!cell
->connections_
.empty())
1510 cell
->unsetPort(cell
->connections_
.begin()->first
);
1512 log_assert(cells_
.count(cell
->name
) != 0);
1513 log_assert(refcount_cells_
== 0);
1514 cells_
.erase(cell
->name
);
1518 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1520 log_assert(wires_
[wire
->name
] == wire
);
1521 log_assert(refcount_wires_
== 0);
1522 wires_
.erase(wire
->name
);
1523 wire
->name
= new_name
;
1527 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1529 log_assert(cells_
[cell
->name
] == cell
);
1530 log_assert(refcount_wires_
== 0);
1531 cells_
.erase(cell
->name
);
1532 cell
->name
= new_name
;
1536 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1538 log_assert(count_id(old_name
) != 0);
1539 if (wires_
.count(old_name
))
1540 rename(wires_
.at(old_name
), new_name
);
1541 else if (cells_
.count(old_name
))
1542 rename(cells_
.at(old_name
), new_name
);
1547 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1549 log_assert(wires_
[w1
->name
] == w1
);
1550 log_assert(wires_
[w2
->name
] == w2
);
1551 log_assert(refcount_wires_
== 0);
1553 wires_
.erase(w1
->name
);
1554 wires_
.erase(w2
->name
);
1556 std::swap(w1
->name
, w2
->name
);
1558 wires_
[w1
->name
] = w1
;
1559 wires_
[w2
->name
] = w2
;
1562 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1564 log_assert(cells_
[c1
->name
] == c1
);
1565 log_assert(cells_
[c2
->name
] == c2
);
1566 log_assert(refcount_cells_
== 0);
1568 cells_
.erase(c1
->name
);
1569 cells_
.erase(c2
->name
);
1571 std::swap(c1
->name
, c2
->name
);
1573 cells_
[c1
->name
] = c1
;
1574 cells_
[c2
->name
] = c2
;
1577 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1580 return uniquify(name
, index
);
1583 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1586 if (count_id(name
) == 0)
1592 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1593 if (count_id(new_name
) == 0)
1599 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1601 if (a
->port_id
&& !b
->port_id
)
1603 if (!a
->port_id
&& b
->port_id
)
1606 if (a
->port_id
== b
->port_id
)
1607 return a
->name
< b
->name
;
1608 return a
->port_id
< b
->port_id
;
1611 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1613 for (auto mon
: monitors
)
1614 mon
->notify_connect(this, conn
);
1617 for (auto mon
: design
->monitors
)
1618 mon
->notify_connect(this, conn
);
1620 // ignore all attempts to assign constants to other constants
1621 if (conn
.first
.has_const()) {
1622 RTLIL::SigSig new_conn
;
1623 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1624 if (conn
.first
[i
].wire
) {
1625 new_conn
.first
.append(conn
.first
[i
]);
1626 new_conn
.second
.append(conn
.second
[i
]);
1628 if (GetSize(new_conn
.first
))
1634 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
));
1635 log_backtrace("-X- ", yosys_xtrace
-1);
1638 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1639 connections_
.push_back(conn
);
1642 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1644 connect(RTLIL::SigSig(lhs
, rhs
));
1647 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1649 for (auto mon
: monitors
)
1650 mon
->notify_connect(this, new_conn
);
1653 for (auto mon
: design
->monitors
)
1654 mon
->notify_connect(this, new_conn
);
1657 log("#X# New connections vector in %s:\n", log_id(this));
1658 for (auto &conn
: new_conn
)
1659 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1660 log_backtrace("-X- ", yosys_xtrace
-1);
1663 connections_
= new_conn
;
1666 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1668 return connections_
;
1671 void RTLIL::Module::fixup_ports()
1673 std::vector
<RTLIL::Wire
*> all_ports
;
1675 for (auto &w
: wires_
)
1676 if (w
.second
->port_input
|| w
.second
->port_output
)
1677 all_ports
.push_back(w
.second
);
1679 w
.second
->port_id
= 0;
1681 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1684 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1685 ports
.push_back(all_ports
[i
]->name
);
1686 all_ports
[i
]->port_id
= i
+1;
1690 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1692 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1694 wire
->width
= width
;
1699 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1701 RTLIL::Wire
*wire
= addWire(name
);
1702 wire
->width
= other
->width
;
1703 wire
->start_offset
= other
->start_offset
;
1704 wire
->port_id
= other
->port_id
;
1705 wire
->port_input
= other
->port_input
;
1706 wire
->port_output
= other
->port_output
;
1707 wire
->upto
= other
->upto
;
1708 wire
->attributes
= other
->attributes
;
1712 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1714 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1721 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1723 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1724 cell
->connections_
= other
->connections_
;
1725 cell
->parameters
= other
->parameters
;
1726 cell
->attributes
= other
->attributes
;
1730 #define DEF_METHOD(_func, _y_size, _type) \
1731 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1732 RTLIL::Cell *cell = addCell(name, _type); \
1733 cell->parameters["\\A_SIGNED"] = is_signed; \
1734 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1735 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1736 cell->setPort("\\A", sig_a); \
1737 cell->setPort("\\Y", sig_y); \
1738 cell->set_src_attribute(src); \
1741 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1742 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1743 add ## _func(name, sig_a, sig_y, is_signed, src); \
1746 DEF_METHOD(Not
, sig_a
.size(), "$not")
1747 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1748 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1749 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1750 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1751 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1752 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1753 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1754 DEF_METHOD(LogicNot
, 1, "$logic_not")
1757 #define DEF_METHOD(_func, _y_size, _type) \
1758 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) { \
1759 RTLIL::Cell *cell = addCell(name, _type); \
1760 cell->parameters["\\A_SIGNED"] = is_signed; \
1761 cell->parameters["\\B_SIGNED"] = is_signed; \
1762 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1763 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1764 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1765 cell->setPort("\\A", sig_a); \
1766 cell->setPort("\\B", sig_b); \
1767 cell->setPort("\\Y", sig_y); \
1768 cell->set_src_attribute(src); \
1771 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1772 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1773 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1776 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), "$and")
1777 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), "$or")
1778 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), "$xor")
1779 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), "$xnor")
1780 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1781 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1782 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1783 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1784 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1785 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1786 DEF_METHOD(Lt
, 1, "$lt")
1787 DEF_METHOD(Le
, 1, "$le")
1788 DEF_METHOD(Eq
, 1, "$eq")
1789 DEF_METHOD(Ne
, 1, "$ne")
1790 DEF_METHOD(Eqx
, 1, "$eqx")
1791 DEF_METHOD(Nex
, 1, "$nex")
1792 DEF_METHOD(Ge
, 1, "$ge")
1793 DEF_METHOD(Gt
, 1, "$gt")
1794 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), "$add")
1795 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), "$sub")
1796 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), "$mul")
1797 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), "$div")
1798 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), "$mod")
1799 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1800 DEF_METHOD(LogicOr
, 1, "$logic_or")
1803 #define DEF_METHOD(_func, _type, _pmux) \
1804 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) { \
1805 RTLIL::Cell *cell = addCell(name, _type); \
1806 cell->parameters["\\WIDTH"] = sig_a.size(); \
1807 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1808 cell->setPort("\\A", sig_a); \
1809 cell->setPort("\\B", sig_b); \
1810 cell->setPort("\\S", sig_s); \
1811 cell->setPort("\\Y", sig_y); \
1812 cell->set_src_attribute(src); \
1815 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1816 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1817 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1820 DEF_METHOD(Mux
, "$mux", 0)
1821 DEF_METHOD(Pmux
, "$pmux", 1)
1824 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1825 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1826 RTLIL::Cell *cell = addCell(name, _type); \
1827 cell->setPort("\\" #_P1, sig1); \
1828 cell->setPort("\\" #_P2, sig2); \
1829 cell->set_src_attribute(src); \
1832 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1833 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1834 add ## _func(name, sig1, sig2, src); \
1837 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1838 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1839 RTLIL::Cell *cell = addCell(name, _type); \
1840 cell->setPort("\\" #_P1, sig1); \
1841 cell->setPort("\\" #_P2, sig2); \
1842 cell->setPort("\\" #_P3, sig3); \
1843 cell->set_src_attribute(src); \
1846 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1847 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1848 add ## _func(name, sig1, sig2, sig3, src); \
1851 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1852 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) { \
1853 RTLIL::Cell *cell = addCell(name, _type); \
1854 cell->setPort("\\" #_P1, sig1); \
1855 cell->setPort("\\" #_P2, sig2); \
1856 cell->setPort("\\" #_P3, sig3); \
1857 cell->setPort("\\" #_P4, sig4); \
1858 cell->set_src_attribute(src); \
1861 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1862 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1863 add ## _func(name, sig1, sig2, sig3, sig4, src); \
1866 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1867 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) { \
1868 RTLIL::Cell *cell = addCell(name, _type); \
1869 cell->setPort("\\" #_P1, sig1); \
1870 cell->setPort("\\" #_P2, sig2); \
1871 cell->setPort("\\" #_P3, sig3); \
1872 cell->setPort("\\" #_P4, sig4); \
1873 cell->setPort("\\" #_P5, sig5); \
1874 cell->set_src_attribute(src); \
1877 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1878 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1879 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
1882 DEF_METHOD_2(BufGate
, "$_BUF_", A
, Y
)
1883 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1884 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1885 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1886 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1887 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1888 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1889 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1890 DEF_METHOD_3(AndnotGate
, "$_ANDNOT_", A
, B
, Y
)
1891 DEF_METHOD_3(OrnotGate
, "$_ORNOT_", A
, B
, Y
)
1892 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1893 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1894 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1895 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1896 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1902 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
)
1904 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1905 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1906 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1907 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1908 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1909 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1910 cell
->setPort("\\A", sig_a
);
1911 cell
->setPort("\\B", sig_b
);
1912 cell
->setPort("\\Y", sig_y
);
1913 cell
->set_src_attribute(src
);
1917 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
, const std::string
&src
)
1919 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1920 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1921 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1922 cell
->parameters
["\\OFFSET"] = offset
;
1923 cell
->setPort("\\A", sig_a
);
1924 cell
->setPort("\\Y", sig_y
);
1925 cell
->set_src_attribute(src
);
1929 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1931 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1932 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1933 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1934 cell
->setPort("\\A", sig_a
);
1935 cell
->setPort("\\B", sig_b
);
1936 cell
->setPort("\\Y", sig_y
);
1937 cell
->set_src_attribute(src
);
1941 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
, const std::string
&src
)
1943 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1944 cell
->parameters
["\\LUT"] = lut
;
1945 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1946 cell
->setPort("\\A", sig_a
);
1947 cell
->setPort("\\Y", sig_y
);
1948 cell
->set_src_attribute(src
);
1952 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
, const std::string
&src
)
1954 RTLIL::Cell
*cell
= addCell(name
, "$tribuf");
1955 cell
->parameters
["\\WIDTH"] = sig_a
.size();
1956 cell
->setPort("\\A", sig_a
);
1957 cell
->setPort("\\EN", sig_en
);
1958 cell
->setPort("\\Y", sig_y
);
1959 cell
->set_src_attribute(src
);
1963 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1965 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1966 cell
->setPort("\\A", sig_a
);
1967 cell
->setPort("\\EN", sig_en
);
1968 cell
->set_src_attribute(src
);
1972 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1974 RTLIL::Cell
*cell
= addCell(name
, "$assume");
1975 cell
->setPort("\\A", sig_a
);
1976 cell
->setPort("\\EN", sig_en
);
1977 cell
->set_src_attribute(src
);
1981 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1983 RTLIL::Cell
*cell
= addCell(name
, "$live");
1984 cell
->setPort("\\A", sig_a
);
1985 cell
->setPort("\\EN", sig_en
);
1986 cell
->set_src_attribute(src
);
1990 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
1992 RTLIL::Cell
*cell
= addCell(name
, "$fair");
1993 cell
->setPort("\\A", sig_a
);
1994 cell
->setPort("\\EN", sig_en
);
1995 cell
->set_src_attribute(src
);
1999 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, const std::string
&src
)
2001 RTLIL::Cell
*cell
= addCell(name
, "$cover");
2002 cell
->setPort("\\A", sig_a
);
2003 cell
->setPort("\\EN", sig_en
);
2004 cell
->set_src_attribute(src
);
2008 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, const std::string
&src
)
2010 RTLIL::Cell
*cell
= addCell(name
, "$equiv");
2011 cell
->setPort("\\A", sig_a
);
2012 cell
->setPort("\\B", sig_b
);
2013 cell
->setPort("\\Y", sig_y
);
2014 cell
->set_src_attribute(src
);
2018 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
)
2020 RTLIL::Cell
*cell
= addCell(name
, "$sr");
2021 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2022 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2023 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2024 cell
->setPort("\\SET", sig_set
);
2025 cell
->setPort("\\CLR", sig_clr
);
2026 cell
->setPort("\\Q", sig_q
);
2027 cell
->set_src_attribute(src
);
2031 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2033 RTLIL::Cell
*cell
= addCell(name
, "$ff");
2034 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2035 cell
->setPort("\\D", sig_d
);
2036 cell
->setPort("\\Q", sig_q
);
2037 cell
->set_src_attribute(src
);
2041 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
)
2043 RTLIL::Cell
*cell
= addCell(name
, "$dff");
2044 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2045 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2046 cell
->setPort("\\CLK", sig_clk
);
2047 cell
->setPort("\\D", sig_d
);
2048 cell
->setPort("\\Q", sig_q
);
2049 cell
->set_src_attribute(src
);
2053 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
)
2055 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
2056 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2057 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2058 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2059 cell
->setPort("\\CLK", sig_clk
);
2060 cell
->setPort("\\EN", sig_en
);
2061 cell
->setPort("\\D", sig_d
);
2062 cell
->setPort("\\Q", sig_q
);
2063 cell
->set_src_attribute(src
);
2067 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2068 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2070 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
2071 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2072 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2073 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2074 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2075 cell
->setPort("\\CLK", sig_clk
);
2076 cell
->setPort("\\SET", sig_set
);
2077 cell
->setPort("\\CLR", sig_clr
);
2078 cell
->setPort("\\D", sig_d
);
2079 cell
->setPort("\\Q", sig_q
);
2080 cell
->set_src_attribute(src
);
2084 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2085 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2087 RTLIL::Cell
*cell
= addCell(name
, "$adff");
2088 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
2089 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
2090 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
2091 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2092 cell
->setPort("\\CLK", sig_clk
);
2093 cell
->setPort("\\ARST", sig_arst
);
2094 cell
->setPort("\\D", sig_d
);
2095 cell
->setPort("\\Q", sig_q
);
2096 cell
->set_src_attribute(src
);
2100 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
)
2102 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
2103 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2104 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2105 cell
->setPort("\\EN", sig_en
);
2106 cell
->setPort("\\D", sig_d
);
2107 cell
->setPort("\\Q", sig_q
);
2108 cell
->set_src_attribute(src
);
2112 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2113 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2115 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
2116 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
2117 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
2118 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
2119 cell
->parameters
["\\WIDTH"] = sig_q
.size();
2120 cell
->setPort("\\EN", sig_en
);
2121 cell
->setPort("\\SET", sig_set
);
2122 cell
->setPort("\\CLR", sig_clr
);
2123 cell
->setPort("\\D", sig_d
);
2124 cell
->setPort("\\Q", sig_q
);
2125 cell
->set_src_attribute(src
);
2129 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, const std::string
&src
)
2131 RTLIL::Cell
*cell
= addCell(name
, "$_FF_");
2132 cell
->setPort("\\D", sig_d
);
2133 cell
->setPort("\\Q", sig_q
);
2134 cell
->set_src_attribute(src
);
2138 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
)
2140 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2141 cell
->setPort("\\C", sig_clk
);
2142 cell
->setPort("\\D", sig_d
);
2143 cell
->setPort("\\Q", sig_q
);
2144 cell
->set_src_attribute(src
);
2148 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
)
2150 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2151 cell
->setPort("\\C", sig_clk
);
2152 cell
->setPort("\\E", sig_en
);
2153 cell
->setPort("\\D", sig_d
);
2154 cell
->setPort("\\Q", sig_q
);
2155 cell
->set_src_attribute(src
);
2159 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2160 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2162 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2163 cell
->setPort("\\C", sig_clk
);
2164 cell
->setPort("\\S", sig_set
);
2165 cell
->setPort("\\R", sig_clr
);
2166 cell
->setPort("\\D", sig_d
);
2167 cell
->setPort("\\Q", sig_q
);
2168 cell
->set_src_attribute(src
);
2172 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
2173 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2175 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2176 cell
->setPort("\\C", sig_clk
);
2177 cell
->setPort("\\R", sig_arst
);
2178 cell
->setPort("\\D", sig_d
);
2179 cell
->setPort("\\Q", sig_q
);
2180 cell
->set_src_attribute(src
);
2184 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
)
2186 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2187 cell
->setPort("\\E", sig_en
);
2188 cell
->setPort("\\D", sig_d
);
2189 cell
->setPort("\\Q", sig_q
);
2190 cell
->set_src_attribute(src
);
2194 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
2195 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2197 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2198 cell
->setPort("\\E", sig_en
);
2199 cell
->setPort("\\S", sig_set
);
2200 cell
->setPort("\\R", sig_clr
);
2201 cell
->setPort("\\D", sig_d
);
2202 cell
->setPort("\\Q", sig_q
);
2203 cell
->set_src_attribute(src
);
2207 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2209 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2210 Cell
*cell
= addCell(name
, "$anyconst");
2211 cell
->setParam("\\WIDTH", width
);
2212 cell
->setPort("\\Y", sig
);
2213 cell
->set_src_attribute(src
);
2217 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2219 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2220 Cell
*cell
= addCell(name
, "$anyseq");
2221 cell
->setParam("\\WIDTH", width
);
2222 cell
->setPort("\\Y", sig
);
2223 cell
->set_src_attribute(src
);
2227 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2229 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2230 Cell
*cell
= addCell(name
, "$allconst");
2231 cell
->setParam("\\WIDTH", width
);
2232 cell
->setPort("\\Y", sig
);
2233 cell
->set_src_attribute(src
);
2237 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2239 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2240 Cell
*cell
= addCell(name
, "$allseq");
2241 cell
->setParam("\\WIDTH", width
);
2242 cell
->setPort("\\Y", sig
);
2243 cell
->set_src_attribute(src
);
2247 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2249 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2250 Cell
*cell
= addCell(name
, "$initstate");
2251 cell
->setPort("\\Y", sig
);
2252 cell
->set_src_attribute(src
);
2258 static unsigned int hashidx_count
= 123456789;
2259 hashidx_count
= mkhash_xorshift(hashidx_count
);
2260 hashidx_
= hashidx_count
;
2267 port_output
= false;
2271 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2275 RTLIL::Wire::~Wire()
2278 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2283 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2284 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2290 RTLIL::Memory::Memory()
2292 static unsigned int hashidx_count
= 123456789;
2293 hashidx_count
= mkhash_xorshift(hashidx_count
);
2294 hashidx_
= hashidx_count
;
2300 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2304 RTLIL::Cell::Cell() : module(nullptr)
2306 static unsigned int hashidx_count
= 123456789;
2307 hashidx_count
= mkhash_xorshift(hashidx_count
);
2308 hashidx_
= hashidx_count
;
2310 // log("#memtrace# %p\n", this);
2314 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2318 RTLIL::Cell::~Cell()
2321 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2326 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2327 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2333 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2335 return connections_
.count(portname
) != 0;
2338 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2340 RTLIL::SigSpec signal
;
2341 auto conn_it
= connections_
.find(portname
);
2343 if (conn_it
!= connections_
.end())
2345 for (auto mon
: module
->monitors
)
2346 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2349 for (auto mon
: module
->design
->monitors
)
2350 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2353 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2354 log_backtrace("-X- ", yosys_xtrace
-1);
2357 connections_
.erase(conn_it
);
2361 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2363 auto conn_it
= connections_
.find(portname
);
2365 if (conn_it
== connections_
.end()) {
2366 connections_
[portname
] = RTLIL::SigSpec();
2367 conn_it
= connections_
.find(portname
);
2368 log_assert(conn_it
!= connections_
.end());
2370 if (conn_it
->second
== signal
)
2373 for (auto mon
: module
->monitors
)
2374 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2377 for (auto mon
: module
->design
->monitors
)
2378 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2381 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2382 log_backtrace("-X- ", yosys_xtrace
-1);
2385 conn_it
->second
= signal
;
2388 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2390 return connections_
.at(portname
);
2393 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2395 return connections_
;
2398 bool RTLIL::Cell::known() const
2400 if (yosys_celltypes
.cell_known(type
))
2402 if (module
&& module
->design
&& module
->design
->module(type
))
2407 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2409 if (yosys_celltypes
.cell_known(type
))
2410 return yosys_celltypes
.cell_input(type
, portname
);
2411 if (module
&& module
->design
) {
2412 RTLIL::Module
*m
= module
->design
->module(type
);
2413 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2414 return w
&& w
->port_input
;
2419 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2421 if (yosys_celltypes
.cell_known(type
))
2422 return yosys_celltypes
.cell_output(type
, portname
);
2423 if (module
&& module
->design
) {
2424 RTLIL::Module
*m
= module
->design
->module(type
);
2425 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2426 return w
&& w
->port_output
;
2431 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2433 return parameters
.count(paramname
) != 0;
2436 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2438 parameters
.erase(paramname
);
2441 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2443 parameters
[paramname
] = value
;
2446 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2448 return parameters
.at(paramname
);
2451 void RTLIL::Cell::sort()
2453 connections_
.sort(sort_by_id_str());
2454 parameters
.sort(sort_by_id_str());
2455 attributes
.sort(sort_by_id_str());
2458 void RTLIL::Cell::check()
2461 InternalCellChecker
checker(NULL
, this);
2466 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2468 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" || type
.substr(0,10) == "$fmcombine" ||
2469 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
2472 if (type
== "$mux" || type
== "$pmux") {
2473 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2474 if (type
== "$pmux")
2475 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
2480 if (type
== "$lut" || type
== "$sop") {
2481 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
2485 if (type
== "$fa") {
2486 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2490 if (type
== "$lcu") {
2491 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
2495 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
2497 if (connections_
.count("\\A")) {
2498 if (signedness_ab
) {
2500 parameters
["\\A_SIGNED"] = true;
2501 else if (parameters
.count("\\A_SIGNED") == 0)
2502 parameters
["\\A_SIGNED"] = false;
2504 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
2507 if (connections_
.count("\\B")) {
2508 if (signedness_ab
) {
2510 parameters
["\\B_SIGNED"] = true;
2511 else if (parameters
.count("\\B_SIGNED") == 0)
2512 parameters
["\\B_SIGNED"] = false;
2514 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
2517 if (connections_
.count("\\Y"))
2518 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
2520 if (connections_
.count("\\Q"))
2521 parameters
["\\WIDTH"] = GetSize(connections_
["\\Q"]);
2526 RTLIL::SigChunk::SigChunk()
2533 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2537 width
= GetSize(data
);
2541 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2543 log_assert(wire
!= nullptr);
2545 this->width
= wire
->width
;
2549 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2551 log_assert(wire
!= nullptr);
2553 this->width
= width
;
2554 this->offset
= offset
;
2557 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2560 data
= RTLIL::Const(str
).bits
;
2561 width
= GetSize(data
);
2565 RTLIL::SigChunk::SigChunk(int val
, int width
)
2568 data
= RTLIL::Const(val
, width
).bits
;
2569 this->width
= GetSize(data
);
2573 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2576 data
= RTLIL::Const(bit
, width
).bits
;
2577 this->width
= GetSize(data
);
2581 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2586 data
= RTLIL::Const(bit
.data
).bits
;
2588 offset
= bit
.offset
;
2592 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
) : data(sigchunk
.data
)
2594 wire
= sigchunk
.wire
;
2595 data
= sigchunk
.data
;
2596 width
= sigchunk
.width
;
2597 offset
= sigchunk
.offset
;
2600 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2602 RTLIL::SigChunk ret
;
2605 ret
.offset
= this->offset
+ offset
;
2608 for (int i
= 0; i
< length
; i
++)
2609 ret
.data
.push_back(data
[offset
+i
]);
2615 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2617 if (wire
&& other
.wire
)
2618 if (wire
->name
!= other
.wire
->name
)
2619 return wire
->name
< other
.wire
->name
;
2621 if (wire
!= other
.wire
)
2622 return wire
< other
.wire
;
2624 if (offset
!= other
.offset
)
2625 return offset
< other
.offset
;
2627 if (width
!= other
.width
)
2628 return width
< other
.width
;
2630 return data
< other
.data
;
2633 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2635 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2638 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2645 RTLIL::SigSpec::SigSpec()
2651 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2656 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2658 cover("kernel.rtlil.sigspec.init.list");
2663 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2664 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2668 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2670 cover("kernel.rtlil.sigspec.assign");
2672 width_
= other
.width_
;
2673 hash_
= other
.hash_
;
2674 chunks_
= other
.chunks_
;
2677 if (!other
.bits_
.empty())
2679 RTLIL::SigChunk
*last
= NULL
;
2680 int last_end_offset
= 0;
2682 for (auto &bit
: other
.bits_
) {
2683 if (last
&& bit
.wire
== last
->wire
) {
2684 if (bit
.wire
== NULL
) {
2685 last
->data
.push_back(bit
.data
);
2688 } else if (last_end_offset
== bit
.offset
) {
2694 chunks_
.push_back(bit
);
2695 last
= &chunks_
.back();
2696 last_end_offset
= bit
.offset
+ 1;
2705 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2707 cover("kernel.rtlil.sigspec.init.const");
2709 chunks_
.push_back(RTLIL::SigChunk(value
));
2710 width_
= chunks_
.back().width
;
2715 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2717 cover("kernel.rtlil.sigspec.init.chunk");
2719 chunks_
.push_back(chunk
);
2720 width_
= chunks_
.back().width
;
2725 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2727 cover("kernel.rtlil.sigspec.init.wire");
2729 chunks_
.push_back(RTLIL::SigChunk(wire
));
2730 width_
= chunks_
.back().width
;
2735 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2737 cover("kernel.rtlil.sigspec.init.wire_part");
2739 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2740 width_
= chunks_
.back().width
;
2745 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2747 cover("kernel.rtlil.sigspec.init.str");
2749 chunks_
.push_back(RTLIL::SigChunk(str
));
2750 width_
= chunks_
.back().width
;
2755 RTLIL::SigSpec::SigSpec(int val
, int width
)
2757 cover("kernel.rtlil.sigspec.init.int");
2759 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2765 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2767 cover("kernel.rtlil.sigspec.init.state");
2769 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2775 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2777 cover("kernel.rtlil.sigspec.init.bit");
2779 if (bit
.wire
== NULL
)
2780 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2782 for (int i
= 0; i
< width
; i
++)
2783 chunks_
.push_back(bit
);
2789 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2791 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2795 for (auto &c
: chunks
)
2800 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2802 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2806 for (auto &bit
: bits
)
2811 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2813 cover("kernel.rtlil.sigspec.init.pool_bits");
2817 for (auto &bit
: bits
)
2822 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2824 cover("kernel.rtlil.sigspec.init.stdset_bits");
2828 for (auto &bit
: bits
)
2833 RTLIL::SigSpec::SigSpec(bool bit
)
2835 cover("kernel.rtlil.sigspec.init.bool");
2843 void RTLIL::SigSpec::pack() const
2845 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2847 if (that
->bits_
.empty())
2850 cover("kernel.rtlil.sigspec.convert.pack");
2851 log_assert(that
->chunks_
.empty());
2853 std::vector
<RTLIL::SigBit
> old_bits
;
2854 old_bits
.swap(that
->bits_
);
2856 RTLIL::SigChunk
*last
= NULL
;
2857 int last_end_offset
= 0;
2859 for (auto &bit
: old_bits
) {
2860 if (last
&& bit
.wire
== last
->wire
) {
2861 if (bit
.wire
== NULL
) {
2862 last
->data
.push_back(bit
.data
);
2865 } else if (last_end_offset
== bit
.offset
) {
2871 that
->chunks_
.push_back(bit
);
2872 last
= &that
->chunks_
.back();
2873 last_end_offset
= bit
.offset
+ 1;
2879 void RTLIL::SigSpec::unpack() const
2881 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2883 if (that
->chunks_
.empty())
2886 cover("kernel.rtlil.sigspec.convert.unpack");
2887 log_assert(that
->bits_
.empty());
2889 that
->bits_
.reserve(that
->width_
);
2890 for (auto &c
: that
->chunks_
)
2891 for (int i
= 0; i
< c
.width
; i
++)
2892 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2894 that
->chunks_
.clear();
2898 void RTLIL::SigSpec::updhash() const
2900 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2902 if (that
->hash_
!= 0)
2905 cover("kernel.rtlil.sigspec.hash");
2908 that
->hash_
= mkhash_init
;
2909 for (auto &c
: that
->chunks_
)
2910 if (c
.wire
== NULL
) {
2911 for (auto &v
: c
.data
)
2912 that
->hash_
= mkhash(that
->hash_
, v
);
2914 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
2915 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
2916 that
->hash_
= mkhash(that
->hash_
, c
.width
);
2919 if (that
->hash_
== 0)
2923 void RTLIL::SigSpec::sort()
2926 cover("kernel.rtlil.sigspec.sort");
2927 std::sort(bits_
.begin(), bits_
.end());
2930 void RTLIL::SigSpec::sort_and_unify()
2933 cover("kernel.rtlil.sigspec.sort_and_unify");
2935 // A copy of the bits vector is used to prevent duplicating the logic from
2936 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
2937 // that isn't showing up as significant in profiles.
2938 std::vector
<SigBit
> unique_bits
= bits_
;
2939 std::sort(unique_bits
.begin(), unique_bits
.end());
2940 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
2941 unique_bits
.erase(last
, unique_bits
.end());
2943 *this = unique_bits
;
2946 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2948 replace(pattern
, with
, this);
2951 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2953 log_assert(other
!= NULL
);
2954 log_assert(width_
== other
->width_
);
2955 log_assert(pattern
.width_
== with
.width_
);
2962 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
2963 if (pattern
.bits_
[i
].wire
!= NULL
) {
2964 for (int j
= 0; j
< GetSize(bits_
); j
++) {
2965 if (bits_
[j
] == pattern
.bits_
[i
]) {
2966 other
->bits_
[j
] = with
.bits_
[i
];
2975 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2977 replace(rules
, this);
2980 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2982 cover("kernel.rtlil.sigspec.replace_dict");
2984 log_assert(other
!= NULL
);
2985 log_assert(width_
== other
->width_
);
2990 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2991 auto it
= rules
.find(bits_
[i
]);
2992 if (it
!= rules
.end())
2993 other
->bits_
[i
] = it
->second
;
2999 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3001 replace(rules
, this);
3004 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3006 cover("kernel.rtlil.sigspec.replace_map");
3008 log_assert(other
!= NULL
);
3009 log_assert(width_
== other
->width_
);
3014 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3015 auto it
= rules
.find(bits_
[i
]);
3016 if (it
!= rules
.end())
3017 other
->bits_
[i
] = it
->second
;
3023 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3025 remove2(pattern
, NULL
);
3028 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3030 RTLIL::SigSpec tmp
= *this;
3031 tmp
.remove2(pattern
, other
);
3034 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3037 cover("kernel.rtlil.sigspec.remove_other");
3039 cover("kernel.rtlil.sigspec.remove");
3042 if (other
!= NULL
) {
3043 log_assert(width_
== other
->width_
);
3047 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3049 if (bits_
[i
].wire
== NULL
) continue;
3051 for (auto &pattern_chunk
: pattern
.chunks())
3052 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3053 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3054 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3055 bits_
.erase(bits_
.begin() + i
);
3057 if (other
!= NULL
) {
3058 other
->bits_
.erase(other
->bits_
.begin() + i
);
3068 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3070 remove2(pattern
, NULL
);
3073 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3075 RTLIL::SigSpec tmp
= *this;
3076 tmp
.remove2(pattern
, other
);
3079 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3082 cover("kernel.rtlil.sigspec.remove_other");
3084 cover("kernel.rtlil.sigspec.remove");
3088 if (other
!= NULL
) {
3089 log_assert(width_
== other
->width_
);
3093 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3094 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3095 bits_
.erase(bits_
.begin() + i
);
3097 if (other
!= NULL
) {
3098 other
->bits_
.erase(other
->bits_
.begin() + i
);
3107 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3110 cover("kernel.rtlil.sigspec.remove_other");
3112 cover("kernel.rtlil.sigspec.remove");
3116 if (other
!= NULL
) {
3117 log_assert(width_
== other
->width_
);
3121 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3122 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3123 bits_
.erase(bits_
.begin() + i
);
3125 if (other
!= NULL
) {
3126 other
->bits_
.erase(other
->bits_
.begin() + i
);
3135 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3138 cover("kernel.rtlil.sigspec.extract_other");
3140 cover("kernel.rtlil.sigspec.extract");
3142 log_assert(other
== NULL
|| width_
== other
->width_
);
3145 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3147 for (auto& pattern_chunk
: pattern
.chunks()) {
3149 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3150 for (int i
= 0; i
< width_
; i
++)
3151 if (bits_match
[i
].wire
&&
3152 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3153 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3154 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3155 ret
.append_bit(bits_other
[i
]);
3157 for (int i
= 0; i
< width_
; i
++)
3158 if (bits_match
[i
].wire
&&
3159 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3160 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3161 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3162 ret
.append_bit(bits_match
[i
]);
3170 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3173 cover("kernel.rtlil.sigspec.extract_other");
3175 cover("kernel.rtlil.sigspec.extract");
3177 log_assert(other
== NULL
|| width_
== other
->width_
);
3179 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3183 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3184 for (int i
= 0; i
< width_
; i
++)
3185 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3186 ret
.append_bit(bits_other
[i
]);
3188 for (int i
= 0; i
< width_
; i
++)
3189 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3190 ret
.append_bit(bits_match
[i
]);
3197 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3199 cover("kernel.rtlil.sigspec.replace_pos");
3204 log_assert(offset
>= 0);
3205 log_assert(with
.width_
>= 0);
3206 log_assert(offset
+with
.width_
<= width_
);
3208 for (int i
= 0; i
< with
.width_
; i
++)
3209 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3214 void RTLIL::SigSpec::remove_const()
3218 cover("kernel.rtlil.sigspec.remove_const.packed");
3220 std::vector
<RTLIL::SigChunk
> new_chunks
;
3221 new_chunks
.reserve(GetSize(chunks_
));
3224 for (auto &chunk
: chunks_
)
3225 if (chunk
.wire
!= NULL
) {
3226 new_chunks
.push_back(chunk
);
3227 width_
+= chunk
.width
;
3230 chunks_
.swap(new_chunks
);
3234 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3236 std::vector
<RTLIL::SigBit
> new_bits
;
3237 new_bits
.reserve(width_
);
3239 for (auto &bit
: bits_
)
3240 if (bit
.wire
!= NULL
)
3241 new_bits
.push_back(bit
);
3243 bits_
.swap(new_bits
);
3244 width_
= bits_
.size();
3250 void RTLIL::SigSpec::remove(int offset
, int length
)
3252 cover("kernel.rtlil.sigspec.remove_pos");
3256 log_assert(offset
>= 0);
3257 log_assert(length
>= 0);
3258 log_assert(offset
+ length
<= width_
);
3260 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3261 width_
= bits_
.size();
3266 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3269 cover("kernel.rtlil.sigspec.extract_pos");
3270 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3273 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3275 if (signal
.width_
== 0)
3283 cover("kernel.rtlil.sigspec.append");
3285 if (packed() != signal
.packed()) {
3291 for (auto &other_c
: signal
.chunks_
)
3293 auto &my_last_c
= chunks_
.back();
3294 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3295 auto &this_data
= my_last_c
.data
;
3296 auto &other_data
= other_c
.data
;
3297 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3298 my_last_c
.width
+= other_c
.width
;
3300 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3301 my_last_c
.width
+= other_c
.width
;
3303 chunks_
.push_back(other_c
);
3306 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3308 width_
+= signal
.width_
;
3312 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
3316 cover("kernel.rtlil.sigspec.append_bit.packed");
3318 if (chunks_
.size() == 0)
3319 chunks_
.push_back(bit
);
3321 if (bit
.wire
== NULL
)
3322 if (chunks_
.back().wire
== NULL
) {
3323 chunks_
.back().data
.push_back(bit
.data
);
3324 chunks_
.back().width
++;
3326 chunks_
.push_back(bit
);
3328 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3329 chunks_
.back().width
++;
3331 chunks_
.push_back(bit
);
3335 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3336 bits_
.push_back(bit
);
3343 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3345 cover("kernel.rtlil.sigspec.extend_u0");
3350 remove(width
, width_
- width
);
3352 if (width_
< width
) {
3353 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3355 padding
= RTLIL::State::S0
;
3356 while (width_
< width
)
3362 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3364 cover("kernel.rtlil.sigspec.repeat");
3367 for (int i
= 0; i
< num
; i
++)
3373 void RTLIL::SigSpec::check() const
3377 cover("kernel.rtlil.sigspec.check.skip");
3381 cover("kernel.rtlil.sigspec.check.packed");
3384 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3385 const RTLIL::SigChunk chunk
= chunks_
[i
];
3386 if (chunk
.wire
== NULL
) {
3388 log_assert(chunks_
[i
-1].wire
!= NULL
);
3389 log_assert(chunk
.offset
== 0);
3390 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3392 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3393 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3394 log_assert(chunk
.offset
>= 0);
3395 log_assert(chunk
.width
>= 0);
3396 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3397 log_assert(chunk
.data
.size() == 0);
3401 log_assert(w
== width_
);
3402 log_assert(bits_
.empty());
3406 cover("kernel.rtlil.sigspec.check.unpacked");
3408 log_assert(width_
== GetSize(bits_
));
3409 log_assert(chunks_
.empty());
3414 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3416 cover("kernel.rtlil.sigspec.comp_lt");
3421 if (width_
!= other
.width_
)
3422 return width_
< other
.width_
;
3427 if (chunks_
.size() != other
.chunks_
.size())
3428 return chunks_
.size() < other
.chunks_
.size();
3433 if (hash_
!= other
.hash_
)
3434 return hash_
< other
.hash_
;
3436 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3437 if (chunks_
[i
] != other
.chunks_
[i
]) {
3438 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3439 return chunks_
[i
] < other
.chunks_
[i
];
3442 cover("kernel.rtlil.sigspec.comp_lt.equal");
3446 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3448 cover("kernel.rtlil.sigspec.comp_eq");
3453 if (width_
!= other
.width_
)
3459 if (chunks_
.size() != chunks_
.size())
3465 if (hash_
!= other
.hash_
)
3468 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3469 if (chunks_
[i
] != other
.chunks_
[i
]) {
3470 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3474 cover("kernel.rtlil.sigspec.comp_eq.equal");
3478 bool RTLIL::SigSpec::is_wire() const
3480 cover("kernel.rtlil.sigspec.is_wire");
3483 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3486 bool RTLIL::SigSpec::is_chunk() const
3488 cover("kernel.rtlil.sigspec.is_chunk");
3491 return GetSize(chunks_
) == 1;
3494 bool RTLIL::SigSpec::is_fully_const() const
3496 cover("kernel.rtlil.sigspec.is_fully_const");
3499 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3500 if (it
->width
> 0 && it
->wire
!= NULL
)
3505 bool RTLIL::SigSpec::is_fully_zero() const
3507 cover("kernel.rtlil.sigspec.is_fully_zero");
3510 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3511 if (it
->width
> 0 && it
->wire
!= NULL
)
3513 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3514 if (it
->data
[i
] != RTLIL::State::S0
)
3520 bool RTLIL::SigSpec::is_fully_ones() const
3522 cover("kernel.rtlil.sigspec.is_fully_ones");
3525 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3526 if (it
->width
> 0 && it
->wire
!= NULL
)
3528 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3529 if (it
->data
[i
] != RTLIL::State::S1
)
3535 bool RTLIL::SigSpec::is_fully_def() const
3537 cover("kernel.rtlil.sigspec.is_fully_def");
3540 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3541 if (it
->width
> 0 && it
->wire
!= NULL
)
3543 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3544 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3550 bool RTLIL::SigSpec::is_fully_undef() const
3552 cover("kernel.rtlil.sigspec.is_fully_undef");
3555 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3556 if (it
->width
> 0 && it
->wire
!= NULL
)
3558 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3559 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3565 bool RTLIL::SigSpec::has_const() const
3567 cover("kernel.rtlil.sigspec.has_const");
3570 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3571 if (it
->width
> 0 && it
->wire
== NULL
)
3576 bool RTLIL::SigSpec::has_marked_bits() const
3578 cover("kernel.rtlil.sigspec.has_marked_bits");
3581 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3582 if (it
->width
> 0 && it
->wire
== NULL
) {
3583 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3584 if (it
->data
[i
] == RTLIL::State::Sm
)
3590 bool RTLIL::SigSpec::as_bool() const
3592 cover("kernel.rtlil.sigspec.as_bool");
3595 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3597 return RTLIL::Const(chunks_
[0].data
).as_bool();
3601 int RTLIL::SigSpec::as_int(bool is_signed
) const
3603 cover("kernel.rtlil.sigspec.as_int");
3606 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3608 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3612 std::string
RTLIL::SigSpec::as_string() const
3614 cover("kernel.rtlil.sigspec.as_string");
3618 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3619 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3620 if (chunk
.wire
!= NULL
)
3621 for (int j
= 0; j
< chunk
.width
; j
++)
3624 str
+= RTLIL::Const(chunk
.data
).as_string();
3629 RTLIL::Const
RTLIL::SigSpec::as_const() const
3631 cover("kernel.rtlil.sigspec.as_const");
3634 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3636 return chunks_
[0].data
;
3637 return RTLIL::Const();
3640 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3642 cover("kernel.rtlil.sigspec.as_wire");
3645 log_assert(is_wire());
3646 return chunks_
[0].wire
;
3649 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3651 cover("kernel.rtlil.sigspec.as_chunk");
3654 log_assert(is_chunk());
3658 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3660 cover("kernel.rtlil.sigspec.as_bit");
3662 log_assert(width_
== 1);
3664 return RTLIL::SigBit(*chunks_
.begin());
3669 bool RTLIL::SigSpec::match(std::string pattern
) const
3671 cover("kernel.rtlil.sigspec.match");
3674 std::string str
= as_string();
3675 log_assert(pattern
.size() == str
.size());
3677 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3678 if (pattern
[i
] == ' ')
3680 if (pattern
[i
] == '*') {
3681 if (str
[i
] != 'z' && str
[i
] != 'x')
3685 if (pattern
[i
] != str
[i
])
3692 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3694 cover("kernel.rtlil.sigspec.to_sigbit_set");
3697 std::set
<RTLIL::SigBit
> sigbits
;
3698 for (auto &c
: chunks_
)
3699 for (int i
= 0; i
< c
.width
; i
++)
3700 sigbits
.insert(RTLIL::SigBit(c
, i
));
3704 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3706 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3709 pool
<RTLIL::SigBit
> sigbits
;
3710 for (auto &c
: chunks_
)
3711 for (int i
= 0; i
< c
.width
; i
++)
3712 sigbits
.insert(RTLIL::SigBit(c
, i
));
3716 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3718 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3724 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3726 cover("kernel.rtlil.sigspec.to_sigbit_map");
3731 log_assert(width_
== other
.width_
);
3733 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3734 for (int i
= 0; i
< width_
; i
++)
3735 new_map
[bits_
[i
]] = other
.bits_
[i
];
3740 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3742 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3747 log_assert(width_
== other
.width_
);
3749 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3750 for (int i
= 0; i
< width_
; i
++)
3751 new_map
[bits_
[i
]] = other
.bits_
[i
];
3756 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3758 size_t start
= 0, end
= 0;
3759 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3760 tokens
.push_back(text
.substr(start
, end
- start
));
3763 tokens
.push_back(text
.substr(start
));
3766 static int sigspec_parse_get_dummy_line_num()
3771 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3773 cover("kernel.rtlil.sigspec.parse");
3775 AST::current_filename
= "input";
3776 AST::use_internal_line_num();
3777 AST::set_line_num(0);
3779 std::vector
<std::string
> tokens
;
3780 sigspec_parse_split(tokens
, str
, ',');
3782 sig
= RTLIL::SigSpec();
3783 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3785 std::string netname
= tokens
[tokidx
];
3786 std::string indices
;
3788 if (netname
.size() == 0)
3791 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3792 cover("kernel.rtlil.sigspec.parse.const");
3793 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3794 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3797 sig
.append(RTLIL::Const(ast
->bits
));
3805 cover("kernel.rtlil.sigspec.parse.net");
3807 if (netname
[0] != '$' && netname
[0] != '\\')
3808 netname
= "\\" + netname
;
3810 if (module
->wires_
.count(netname
) == 0) {
3811 size_t indices_pos
= netname
.size()-1;
3812 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3815 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3816 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3818 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3820 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3821 indices
= netname
.substr(indices_pos
);
3822 netname
= netname
.substr(0, indices_pos
);
3827 if (module
->wires_
.count(netname
) == 0)
3830 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3831 if (!indices
.empty()) {
3832 std::vector
<std::string
> index_tokens
;
3833 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3834 if (index_tokens
.size() == 1) {
3835 cover("kernel.rtlil.sigspec.parse.bit_sel");
3836 int a
= atoi(index_tokens
.at(0).c_str());
3837 if (a
< 0 || a
>= wire
->width
)
3839 sig
.append(RTLIL::SigSpec(wire
, a
));
3841 cover("kernel.rtlil.sigspec.parse.part_sel");
3842 int a
= atoi(index_tokens
.at(0).c_str());
3843 int b
= atoi(index_tokens
.at(1).c_str());
3848 if (a
< 0 || a
>= wire
->width
)
3850 if (b
< 0 || b
>= wire
->width
)
3852 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3861 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3863 if (str
.empty() || str
[0] != '@')
3864 return parse(sig
, module
, str
);
3866 cover("kernel.rtlil.sigspec.parse.sel");
3868 str
= RTLIL::escape_id(str
.substr(1));
3869 if (design
->selection_vars
.count(str
) == 0)
3872 sig
= RTLIL::SigSpec();
3873 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3874 for (auto &it
: module
->wires_
)
3875 if (sel
.selected_member(module
->name
, it
.first
))
3876 sig
.append(it
.second
);
3881 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3884 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3885 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3890 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3891 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3895 if (lhs
.chunks_
.size() == 1) {
3896 char *p
= (char*)str
.c_str(), *endptr
;
3897 long int val
= strtol(p
, &endptr
, 10);
3898 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3899 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3900 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3905 return parse(sig
, module
, str
);
3908 RTLIL::CaseRule::~CaseRule()
3910 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3914 bool RTLIL::CaseRule::empty() const
3916 return actions
.empty() && switches
.empty();
3919 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3921 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3922 new_caserule
->compare
= compare
;
3923 new_caserule
->actions
= actions
;
3924 for (auto &it
: switches
)
3925 new_caserule
->switches
.push_back(it
->clone());
3926 return new_caserule
;
3929 RTLIL::SwitchRule::~SwitchRule()
3931 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3935 bool RTLIL::SwitchRule::empty() const
3937 return cases
.empty();
3940 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3942 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3943 new_switchrule
->signal
= signal
;
3944 new_switchrule
->attributes
= attributes
;
3945 for (auto &it
: cases
)
3946 new_switchrule
->cases
.push_back(it
->clone());
3947 return new_switchrule
;
3951 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3953 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3954 new_syncrule
->type
= type
;
3955 new_syncrule
->signal
= signal
;
3956 new_syncrule
->actions
= actions
;
3957 return new_syncrule
;
3960 RTLIL::Process::~Process()
3962 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3966 RTLIL::Process
*RTLIL::Process::clone() const
3968 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3970 new_proc
->name
= name
;
3971 new_proc
->attributes
= attributes
;
3973 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3974 new_proc
->root_case
= *rc_ptr
;
3975 rc_ptr
->switches
.clear();
3978 for (auto &it
: syncs
)
3979 new_proc
->syncs
.push_back(it
->clone());
3985 RTLIL::Memory::~Memory()
3987 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
3989 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
3990 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
3992 return &all_memorys
;