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_
;
39 flags
= RTLIL::CONST_FLAG_NONE
;
42 RTLIL::Const::Const(std::string str
)
44 flags
= RTLIL::CONST_FLAG_STRING
;
45 for (int i
= str
.size()-1; i
>= 0; i
--) {
46 unsigned char ch
= str
[i
];
47 for (int j
= 0; j
< 8; j
++) {
48 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
54 RTLIL::Const::Const(int val
, int width
)
56 flags
= RTLIL::CONST_FLAG_NONE
;
57 for (int i
= 0; i
< width
; i
++) {
58 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
63 RTLIL::Const::Const(RTLIL::State bit
, int width
)
65 flags
= RTLIL::CONST_FLAG_NONE
;
66 for (int i
= 0; i
< width
; i
++)
70 RTLIL::Const::Const(const std::vector
<bool> &bits
)
72 flags
= RTLIL::CONST_FLAG_NONE
;
74 this->bits
.push_back(b
? RTLIL::S1
: RTLIL::S0
);
77 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
79 if (bits
.size() != other
.bits
.size())
80 return bits
.size() < other
.bits
.size();
81 for (size_t i
= 0; i
< bits
.size(); i
++)
82 if (bits
[i
] != other
.bits
[i
])
83 return bits
[i
] < other
.bits
[i
];
87 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
89 return bits
== other
.bits
;
92 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
94 return bits
!= other
.bits
;
97 bool RTLIL::Const::as_bool() const
99 for (size_t i
= 0; i
< bits
.size(); i
++)
100 if (bits
[i
] == RTLIL::S1
)
105 int RTLIL::Const::as_int(bool is_signed
) const
108 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
109 if (bits
[i
] == RTLIL::S1
)
111 if (is_signed
&& bits
.back() == RTLIL::S1
)
112 for (size_t i
= bits
.size(); i
< 32; i
++)
117 std::string
RTLIL::Const::as_string() const
120 for (size_t i
= bits
.size(); i
> 0; i
--)
122 case S0
: ret
+= "0"; break;
123 case S1
: ret
+= "1"; break;
124 case Sx
: ret
+= "x"; break;
125 case Sz
: ret
+= "z"; break;
126 case Sa
: ret
+= "-"; break;
127 case Sm
: ret
+= "m"; break;
132 RTLIL::Const
RTLIL::Const::from_string(std::string str
)
135 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
137 case '0': c
.bits
.push_back(State::S0
); break;
138 case '1': c
.bits
.push_back(State::S1
); break;
139 case 'x': c
.bits
.push_back(State::Sx
); break;
140 case 'z': c
.bits
.push_back(State::Sz
); break;
141 case 'm': c
.bits
.push_back(State::Sm
); break;
142 default: c
.bits
.push_back(State::Sa
);
147 std::string
RTLIL::Const::decode_string() const
150 std::vector
<char> string_chars
;
151 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
153 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
154 if (bits
[i
+ j
] == RTLIL::State::S1
)
157 string_chars
.push_back(ch
);
159 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
160 string
+= string_chars
[i
];
164 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
)
166 attributes
[id
] = RTLIL::Const(1);
169 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
171 if (attributes
.count(id
) == 0)
173 return attributes
.at(id
).as_bool();
176 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
179 for (auto &s
: data
) {
180 if (!attrval
.empty())
184 attributes
[id
] = RTLIL::Const(attrval
);
187 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
189 pool
<string
> union_data
= get_strpool_attribute(id
);
190 union_data
.insert(data
.begin(), data
.end());
191 if (!union_data
.empty())
192 set_strpool_attribute(id
, union_data
);
195 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
198 if (attributes
.count(id
) != 0)
199 for (auto s
: split_tokens(attributes
.at(id
).decode_string(), "|"))
204 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
208 if (selected_modules
.count(mod_name
) > 0)
210 if (selected_members
.count(mod_name
) > 0)
215 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
219 if (selected_modules
.count(mod_name
) > 0)
224 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
228 if (selected_modules
.count(mod_name
) > 0)
230 if (selected_members
.count(mod_name
) > 0)
231 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
236 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
238 if (full_selection
) {
239 selected_modules
.clear();
240 selected_members
.clear();
244 std::vector
<RTLIL::IdString
> del_list
, add_list
;
247 for (auto mod_name
: selected_modules
) {
248 if (design
->modules_
.count(mod_name
) == 0)
249 del_list
.push_back(mod_name
);
250 selected_members
.erase(mod_name
);
252 for (auto mod_name
: del_list
)
253 selected_modules
.erase(mod_name
);
256 for (auto &it
: selected_members
)
257 if (design
->modules_
.count(it
.first
) == 0)
258 del_list
.push_back(it
.first
);
259 for (auto mod_name
: del_list
)
260 selected_members
.erase(mod_name
);
262 for (auto &it
: selected_members
) {
264 for (auto memb_name
: it
.second
)
265 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
266 del_list
.push_back(memb_name
);
267 for (auto memb_name
: del_list
)
268 it
.second
.erase(memb_name
);
273 for (auto &it
: selected_members
)
274 if (it
.second
.size() == 0)
275 del_list
.push_back(it
.first
);
276 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
277 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
278 add_list
.push_back(it
.first
);
279 for (auto mod_name
: del_list
)
280 selected_members
.erase(mod_name
);
281 for (auto mod_name
: add_list
) {
282 selected_members
.erase(mod_name
);
283 selected_modules
.insert(mod_name
);
286 if (selected_modules
.size() == design
->modules_
.size()) {
287 full_selection
= true;
288 selected_modules
.clear();
289 selected_members
.clear();
293 RTLIL::Design::Design()
295 static unsigned int hashidx_count
= 123456789;
296 hashidx_count
= mkhash_xorshift(hashidx_count
);
297 hashidx_
= hashidx_count
;
299 refcount_modules_
= 0;
300 selection_stack
.push_back(RTLIL::Selection());
303 RTLIL::Design::~Design()
305 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
309 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
311 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
314 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
316 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
319 RTLIL::Module
*RTLIL::Design::top_module()
321 RTLIL::Module
*module
= nullptr;
322 int module_count
= 0;
324 for (auto mod
: selected_modules()) {
325 if (mod
->get_bool_attribute("\\top"))
331 return module_count
== 1 ? module
: nullptr;
334 void RTLIL::Design::add(RTLIL::Module
*module
)
336 log_assert(modules_
.count(module
->name
) == 0);
337 log_assert(refcount_modules_
== 0);
338 modules_
[module
->name
] = module
;
339 module
->design
= this;
341 for (auto mon
: monitors
)
342 mon
->notify_module_add(module
);
345 log("#X# New Module: %s\n", log_id(module
));
346 log_backtrace("-X- ", yosys_xtrace
-1);
350 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
352 log_assert(modules_
.count(name
) == 0);
353 log_assert(refcount_modules_
== 0);
355 RTLIL::Module
*module
= new RTLIL::Module
;
356 modules_
[name
] = module
;
357 module
->design
= this;
360 for (auto mon
: monitors
)
361 mon
->notify_module_add(module
);
364 log("#X# New Module: %s\n", log_id(module
));
365 log_backtrace("-X- ", yosys_xtrace
-1);
371 void RTLIL::Design::scratchpad_unset(std::string varname
)
373 scratchpad
.erase(varname
);
376 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
378 scratchpad
[varname
] = stringf("%d", value
);
381 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
383 scratchpad
[varname
] = value
? "true" : "false";
386 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
388 scratchpad
[varname
] = value
;
391 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
393 if (scratchpad
.count(varname
) == 0)
394 return default_value
;
396 std::string str
= scratchpad
.at(varname
);
398 if (str
== "0" || str
== "false")
401 if (str
== "1" || str
== "true")
404 char *endptr
= nullptr;
405 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
406 return *endptr
? default_value
: parsed_value
;
409 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
411 if (scratchpad
.count(varname
) == 0)
412 return default_value
;
414 std::string str
= scratchpad
.at(varname
);
416 if (str
== "0" || str
== "false")
419 if (str
== "1" || str
== "true")
422 return default_value
;
425 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
427 if (scratchpad
.count(varname
) == 0)
428 return default_value
;
429 return scratchpad
.at(varname
);
432 void RTLIL::Design::remove(RTLIL::Module
*module
)
434 for (auto mon
: monitors
)
435 mon
->notify_module_del(module
);
438 log("#X# Remove Module: %s\n", log_id(module
));
439 log_backtrace("-X- ", yosys_xtrace
-1);
442 log_assert(modules_
.at(module
->name
) == module
);
443 modules_
.erase(module
->name
);
447 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
449 modules_
.erase(module
->name
);
450 module
->name
= new_name
;
454 void RTLIL::Design::sort()
457 modules_
.sort(sort_by_id_str());
458 for (auto &it
: modules_
)
462 void RTLIL::Design::check()
465 for (auto &it
: modules_
) {
466 log_assert(this == it
.second
->design
);
467 log_assert(it
.first
== it
.second
->name
);
468 log_assert(!it
.first
.empty());
474 void RTLIL::Design::optimize()
476 for (auto &it
: modules_
)
477 it
.second
->optimize();
478 for (auto &it
: selection_stack
)
480 for (auto &it
: selection_vars
)
481 it
.second
.optimize(this);
484 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
486 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
488 if (selection_stack
.size() == 0)
490 return selection_stack
.back().selected_module(mod_name
);
493 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
495 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
497 if (selection_stack
.size() == 0)
499 return selection_stack
.back().selected_whole_module(mod_name
);
502 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
504 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
506 if (selection_stack
.size() == 0)
508 return selection_stack
.back().selected_member(mod_name
, memb_name
);
511 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
513 return selected_module(mod
->name
);
516 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
518 return selected_whole_module(mod
->name
);
521 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
523 std::vector
<RTLIL::Module
*> result
;
524 result
.reserve(modules_
.size());
525 for (auto &it
: modules_
)
526 if (selected_module(it
.first
) && !it
.second
->get_bool_attribute("\\blackbox"))
527 result
.push_back(it
.second
);
531 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
533 std::vector
<RTLIL::Module
*> result
;
534 result
.reserve(modules_
.size());
535 for (auto &it
: modules_
)
536 if (selected_whole_module(it
.first
) && !it
.second
->get_bool_attribute("\\blackbox"))
537 result
.push_back(it
.second
);
541 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
543 std::vector
<RTLIL::Module
*> result
;
544 result
.reserve(modules_
.size());
545 for (auto &it
: modules_
)
546 if (it
.second
->get_bool_attribute("\\blackbox"))
548 else if (selected_whole_module(it
.first
))
549 result
.push_back(it
.second
);
550 else if (selected_module(it
.first
))
551 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
555 RTLIL::Module::Module()
557 static unsigned int hashidx_count
= 123456789;
558 hashidx_count
= mkhash_xorshift(hashidx_count
);
559 hashidx_
= hashidx_count
;
566 RTLIL::Module::~Module()
568 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
570 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
572 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
574 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
578 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, dict
<RTLIL::IdString
, RTLIL::Const
>)
580 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
583 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
585 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
590 struct InternalCellChecker
592 RTLIL::Module
*module
;
594 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
596 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
598 void error(int linenr
)
600 std::stringstream buf
;
601 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
603 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
604 module
? module
->name
.c_str() : "", module
? "." : "",
605 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
608 int param(const char *name
)
610 if (cell
->parameters
.count(name
) == 0)
612 expected_params
.insert(name
);
613 return cell
->parameters
.at(name
).as_int();
616 int param_bool(const char *name
)
619 if (cell
->parameters
.at(name
).bits
.size() > 32)
621 if (v
!= 0 && v
!= 1)
626 void param_bits(const char *name
, int width
)
629 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
633 void port(const char *name
, int width
)
635 if (!cell
->hasPort(name
))
637 if (cell
->getPort(name
).size() != width
)
639 expected_ports
.insert(name
);
642 void check_expected(bool check_matched_sign
= true)
644 for (auto ¶
: cell
->parameters
)
645 if (expected_params
.count(para
.first
) == 0)
647 for (auto &conn
: cell
->connections())
648 if (expected_ports
.count(conn
.first
) == 0)
651 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
652 bool a_is_signed
= param("\\A_SIGNED") != 0;
653 bool b_is_signed
= param("\\B_SIGNED") != 0;
654 if (a_is_signed
!= b_is_signed
)
659 void check_gate(const char *ports
)
661 if (cell
->parameters
.size() != 0)
664 for (const char *p
= ports
; *p
; p
++) {
665 char portname
[3] = { '\\', *p
, 0 };
666 if (!cell
->hasPort(portname
))
668 if (cell
->getPort(portname
).size() != 1)
672 for (auto &conn
: cell
->connections()) {
673 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
675 if (strchr(ports
, conn
.first
[1]) == NULL
)
682 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
683 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
686 if (cell
->type
.in("$not", "$pos", "$neg")) {
687 param_bool("\\A_SIGNED");
688 port("\\A", param("\\A_WIDTH"));
689 port("\\Y", param("\\Y_WIDTH"));
694 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
695 param_bool("\\A_SIGNED");
696 param_bool("\\B_SIGNED");
697 port("\\A", param("\\A_WIDTH"));
698 port("\\B", param("\\B_WIDTH"));
699 port("\\Y", param("\\Y_WIDTH"));
704 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
705 param_bool("\\A_SIGNED");
706 port("\\A", param("\\A_WIDTH"));
707 port("\\Y", param("\\Y_WIDTH"));
712 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
713 param_bool("\\A_SIGNED");
714 param_bool("\\B_SIGNED");
715 port("\\A", param("\\A_WIDTH"));
716 port("\\B", param("\\B_WIDTH"));
717 port("\\Y", param("\\Y_WIDTH"));
718 check_expected(false);
722 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
723 param_bool("\\A_SIGNED");
724 param_bool("\\B_SIGNED");
725 port("\\A", param("\\A_WIDTH"));
726 port("\\B", param("\\B_WIDTH"));
727 port("\\Y", param("\\Y_WIDTH"));
732 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
733 param_bool("\\A_SIGNED");
734 param_bool("\\B_SIGNED");
735 port("\\A", param("\\A_WIDTH"));
736 port("\\B", param("\\B_WIDTH"));
737 port("\\Y", param("\\Y_WIDTH"));
738 check_expected(cell
->type
!= "$pow");
742 if (cell
->type
== "$fa") {
743 port("\\A", param("\\WIDTH"));
744 port("\\B", param("\\WIDTH"));
745 port("\\C", param("\\WIDTH"));
746 port("\\X", param("\\WIDTH"));
747 port("\\Y", param("\\WIDTH"));
752 if (cell
->type
== "$lcu") {
753 port("\\P", param("\\WIDTH"));
754 port("\\G", param("\\WIDTH"));
756 port("\\CO", param("\\WIDTH"));
761 if (cell
->type
== "$alu") {
762 param_bool("\\A_SIGNED");
763 param_bool("\\B_SIGNED");
764 port("\\A", param("\\A_WIDTH"));
765 port("\\B", param("\\B_WIDTH"));
768 port("\\X", param("\\Y_WIDTH"));
769 port("\\Y", param("\\Y_WIDTH"));
770 port("\\CO", param("\\Y_WIDTH"));
775 if (cell
->type
== "$macc") {
777 param("\\CONFIG_WIDTH");
778 port("\\A", param("\\A_WIDTH"));
779 port("\\B", param("\\B_WIDTH"));
780 port("\\Y", param("\\Y_WIDTH"));
782 Macc().from_cell(cell
);
786 if (cell
->type
== "$logic_not") {
787 param_bool("\\A_SIGNED");
788 port("\\A", param("\\A_WIDTH"));
789 port("\\Y", param("\\Y_WIDTH"));
794 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
795 param_bool("\\A_SIGNED");
796 param_bool("\\B_SIGNED");
797 port("\\A", param("\\A_WIDTH"));
798 port("\\B", param("\\B_WIDTH"));
799 port("\\Y", param("\\Y_WIDTH"));
800 check_expected(false);
804 if (cell
->type
== "$slice") {
806 port("\\A", param("\\A_WIDTH"));
807 port("\\Y", param("\\Y_WIDTH"));
808 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
814 if (cell
->type
== "$concat") {
815 port("\\A", param("\\A_WIDTH"));
816 port("\\B", param("\\B_WIDTH"));
817 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
822 if (cell
->type
== "$mux") {
823 port("\\A", param("\\WIDTH"));
824 port("\\B", param("\\WIDTH"));
826 port("\\Y", param("\\WIDTH"));
831 if (cell
->type
== "$pmux") {
832 port("\\A", param("\\WIDTH"));
833 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
834 port("\\S", param("\\S_WIDTH"));
835 port("\\Y", param("\\WIDTH"));
840 if (cell
->type
== "$lut") {
842 port("\\A", param("\\WIDTH"));
848 if (cell
->type
== "$sr") {
849 param_bool("\\SET_POLARITY");
850 param_bool("\\CLR_POLARITY");
851 port("\\SET", param("\\WIDTH"));
852 port("\\CLR", param("\\WIDTH"));
853 port("\\Q", param("\\WIDTH"));
858 if (cell
->type
== "$dff") {
859 param_bool("\\CLK_POLARITY");
861 port("\\D", param("\\WIDTH"));
862 port("\\Q", param("\\WIDTH"));
867 if (cell
->type
== "$dffe") {
868 param_bool("\\CLK_POLARITY");
869 param_bool("\\EN_POLARITY");
872 port("\\D", param("\\WIDTH"));
873 port("\\Q", param("\\WIDTH"));
878 if (cell
->type
== "$dffsr") {
879 param_bool("\\CLK_POLARITY");
880 param_bool("\\SET_POLARITY");
881 param_bool("\\CLR_POLARITY");
883 port("\\SET", param("\\WIDTH"));
884 port("\\CLR", param("\\WIDTH"));
885 port("\\D", param("\\WIDTH"));
886 port("\\Q", param("\\WIDTH"));
891 if (cell
->type
== "$adff") {
892 param_bool("\\CLK_POLARITY");
893 param_bool("\\ARST_POLARITY");
894 param_bits("\\ARST_VALUE", param("\\WIDTH"));
897 port("\\D", param("\\WIDTH"));
898 port("\\Q", param("\\WIDTH"));
903 if (cell
->type
== "$dlatch") {
904 param_bool("\\EN_POLARITY");
906 port("\\D", param("\\WIDTH"));
907 port("\\Q", param("\\WIDTH"));
912 if (cell
->type
== "$dlatchsr") {
913 param_bool("\\EN_POLARITY");
914 param_bool("\\SET_POLARITY");
915 param_bool("\\CLR_POLARITY");
917 port("\\SET", param("\\WIDTH"));
918 port("\\CLR", param("\\WIDTH"));
919 port("\\D", param("\\WIDTH"));
920 port("\\Q", param("\\WIDTH"));
925 if (cell
->type
== "$fsm") {
927 param_bool("\\CLK_POLARITY");
928 param_bool("\\ARST_POLARITY");
929 param("\\STATE_BITS");
930 param("\\STATE_NUM");
931 param("\\STATE_NUM_LOG2");
932 param("\\STATE_RST");
933 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
934 param("\\TRANS_NUM");
935 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
938 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
939 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
944 if (cell
->type
== "$memrd") {
946 param_bool("\\CLK_ENABLE");
947 param_bool("\\CLK_POLARITY");
948 param_bool("\\TRANSPARENT");
950 port("\\ADDR", param("\\ABITS"));
951 port("\\DATA", param("\\WIDTH"));
956 if (cell
->type
== "$memwr") {
958 param_bool("\\CLK_ENABLE");
959 param_bool("\\CLK_POLARITY");
962 port("\\EN", param("\\WIDTH"));
963 port("\\ADDR", param("\\ABITS"));
964 port("\\DATA", param("\\WIDTH"));
969 if (cell
->type
== "$meminit") {
972 port("\\ADDR", param("\\ABITS"));
973 port("\\DATA", param("\\WIDTH"));
978 if (cell
->type
== "$mem") {
983 param_bits("\\RD_CLK_ENABLE", std::max(1, param("\\RD_PORTS")));
984 param_bits("\\RD_CLK_POLARITY", std::max(1, param("\\RD_PORTS")));
985 param_bits("\\RD_TRANSPARENT", std::max(1, param("\\RD_PORTS")));
986 param_bits("\\WR_CLK_ENABLE", std::max(1, param("\\WR_PORTS")));
987 param_bits("\\WR_CLK_POLARITY", std::max(1, param("\\WR_PORTS")));
988 port("\\RD_CLK", param("\\RD_PORTS"));
989 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
990 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
991 port("\\WR_CLK", param("\\WR_PORTS"));
992 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
993 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
994 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
999 if (cell
->type
== "$assert") {
1006 if (cell
->type
== "$assume") {
1013 if (cell
->type
== "$equiv") {
1021 if (cell
->type
== "$_BUF_") { check_gate("AY"); return; }
1022 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
1023 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
1024 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
1025 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
1026 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
1027 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
1028 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
1029 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
1030 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
1031 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
1032 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
1033 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
1035 if (cell
->type
== "$_MUX4_") { check_gate("ABCDSTY"); return; }
1036 if (cell
->type
== "$_MUX8_") { check_gate("ABCDEFGHSTUY"); return; }
1037 if (cell
->type
== "$_MUX16_") { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1039 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
1040 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
1041 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
1042 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
1044 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
1045 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
1047 if (cell
->type
== "$_DFFE_NN_") { check_gate("DQCE"); return; }
1048 if (cell
->type
== "$_DFFE_NP_") { check_gate("DQCE"); return; }
1049 if (cell
->type
== "$_DFFE_PN_") { check_gate("DQCE"); return; }
1050 if (cell
->type
== "$_DFFE_PP_") { check_gate("DQCE"); return; }
1052 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
1053 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
1054 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
1055 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
1056 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
1057 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
1058 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
1059 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
1061 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
1062 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
1063 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
1064 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
1065 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
1066 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
1067 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
1068 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
1070 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
1071 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
1073 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
1074 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
1075 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
1076 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
1077 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
1078 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
1079 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
1080 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
1088 void RTLIL::Module::sort()
1090 wires_
.sort(sort_by_id_str());
1091 cells_
.sort(sort_by_id_str());
1092 avail_parameters
.sort(sort_by_id_str());
1093 memories
.sort(sort_by_id_str());
1094 processes
.sort(sort_by_id_str());
1095 for (auto &it
: cells_
)
1097 for (auto &it
: wires_
)
1098 it
.second
->attributes
.sort(sort_by_id_str());
1099 for (auto &it
: memories
)
1100 it
.second
->attributes
.sort(sort_by_id_str());
1103 void RTLIL::Module::check()
1106 std::vector
<bool> ports_declared
;
1107 for (auto &it
: wires_
) {
1108 log_assert(this == it
.second
->module
);
1109 log_assert(it
.first
== it
.second
->name
);
1110 log_assert(!it
.first
.empty());
1111 log_assert(it
.second
->width
>= 0);
1112 log_assert(it
.second
->port_id
>= 0);
1113 for (auto &it2
: it
.second
->attributes
)
1114 log_assert(!it2
.first
.empty());
1115 if (it
.second
->port_id
) {
1116 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1117 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1118 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1119 if (GetSize(ports_declared
) < it
.second
->port_id
)
1120 ports_declared
.resize(it
.second
->port_id
);
1121 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1122 ports_declared
[it
.second
->port_id
-1] = true;
1124 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1126 for (auto port_declared
: ports_declared
)
1127 log_assert(port_declared
== true);
1128 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1130 for (auto &it
: memories
) {
1131 log_assert(it
.first
== it
.second
->name
);
1132 log_assert(!it
.first
.empty());
1133 log_assert(it
.second
->width
>= 0);
1134 log_assert(it
.second
->size
>= 0);
1135 for (auto &it2
: it
.second
->attributes
)
1136 log_assert(!it2
.first
.empty());
1139 for (auto &it
: cells_
) {
1140 log_assert(this == it
.second
->module
);
1141 log_assert(it
.first
== it
.second
->name
);
1142 log_assert(!it
.first
.empty());
1143 log_assert(!it
.second
->type
.empty());
1144 for (auto &it2
: it
.second
->connections()) {
1145 log_assert(!it2
.first
.empty());
1148 for (auto &it2
: it
.second
->attributes
)
1149 log_assert(!it2
.first
.empty());
1150 for (auto &it2
: it
.second
->parameters
)
1151 log_assert(!it2
.first
.empty());
1152 InternalCellChecker
checker(this, it
.second
);
1156 for (auto &it
: processes
) {
1157 log_assert(it
.first
== it
.second
->name
);
1158 log_assert(!it
.first
.empty());
1159 // FIXME: More checks here..
1162 for (auto &it
: connections_
) {
1163 log_assert(it
.first
.size() == it
.second
.size());
1164 log_assert(!it
.first
.has_const());
1169 for (auto &it
: attributes
)
1170 log_assert(!it
.first
.empty());
1174 void RTLIL::Module::optimize()
1178 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1180 log_assert(new_mod
->refcount_wires_
== 0);
1181 log_assert(new_mod
->refcount_cells_
== 0);
1183 new_mod
->avail_parameters
= avail_parameters
;
1185 for (auto &conn
: connections_
)
1186 new_mod
->connect(conn
);
1188 for (auto &attr
: attributes
)
1189 new_mod
->attributes
[attr
.first
] = attr
.second
;
1191 for (auto &it
: wires_
)
1192 new_mod
->addWire(it
.first
, it
.second
);
1194 for (auto &it
: memories
)
1195 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1197 for (auto &it
: cells_
)
1198 new_mod
->addCell(it
.first
, it
.second
);
1200 for (auto &it
: processes
)
1201 new_mod
->processes
[it
.first
] = it
.second
->clone();
1203 struct RewriteSigSpecWorker
1206 void operator()(RTLIL::SigSpec
&sig
)
1208 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1209 for (auto &c
: chunks
)
1211 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1216 RewriteSigSpecWorker rewriteSigSpecWorker
;
1217 rewriteSigSpecWorker
.mod
= new_mod
;
1218 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1219 new_mod
->fixup_ports();
1222 RTLIL::Module
*RTLIL::Module::clone() const
1224 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1225 new_mod
->name
= name
;
1230 bool RTLIL::Module::has_memories() const
1232 return !memories
.empty();
1235 bool RTLIL::Module::has_processes() const
1237 return !processes
.empty();
1240 bool RTLIL::Module::has_memories_warn() const
1242 if (!memories
.empty())
1243 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1244 return !memories
.empty();
1247 bool RTLIL::Module::has_processes_warn() const
1249 if (!processes
.empty())
1250 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1251 return !processes
.empty();
1254 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1256 std::vector
<RTLIL::Wire
*> result
;
1257 result
.reserve(wires_
.size());
1258 for (auto &it
: wires_
)
1259 if (design
->selected(this, it
.second
))
1260 result
.push_back(it
.second
);
1264 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1266 std::vector
<RTLIL::Cell
*> result
;
1267 result
.reserve(wires_
.size());
1268 for (auto &it
: cells_
)
1269 if (design
->selected(this, it
.second
))
1270 result
.push_back(it
.second
);
1274 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1276 log_assert(!wire
->name
.empty());
1277 log_assert(count_id(wire
->name
) == 0);
1278 log_assert(refcount_wires_
== 0);
1279 wires_
[wire
->name
] = wire
;
1280 wire
->module
= this;
1283 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1285 log_assert(!cell
->name
.empty());
1286 log_assert(count_id(cell
->name
) == 0);
1287 log_assert(refcount_cells_
== 0);
1288 cells_
[cell
->name
] = cell
;
1289 cell
->module
= this;
1293 struct DeleteWireWorker
1295 RTLIL::Module
*module
;
1296 const pool
<RTLIL::Wire
*> *wires_p
;
1298 void operator()(RTLIL::SigSpec
&sig
) {
1299 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1300 for (auto &c
: chunks
)
1301 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1302 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1310 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1312 log_assert(refcount_wires_
== 0);
1314 DeleteWireWorker delete_wire_worker
;
1315 delete_wire_worker
.module
= this;
1316 delete_wire_worker
.wires_p
= &wires
;
1317 rewrite_sigspecs(delete_wire_worker
);
1319 for (auto &it
: wires
) {
1320 log_assert(wires_
.count(it
->name
) != 0);
1321 wires_
.erase(it
->name
);
1326 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1328 while (!cell
->connections_
.empty())
1329 cell
->unsetPort(cell
->connections_
.begin()->first
);
1331 log_assert(cells_
.count(cell
->name
) != 0);
1332 log_assert(refcount_cells_
== 0);
1333 cells_
.erase(cell
->name
);
1337 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1339 log_assert(wires_
[wire
->name
] == wire
);
1340 log_assert(refcount_wires_
== 0);
1341 wires_
.erase(wire
->name
);
1342 wire
->name
= new_name
;
1346 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1348 log_assert(cells_
[cell
->name
] == cell
);
1349 log_assert(refcount_wires_
== 0);
1350 cells_
.erase(cell
->name
);
1351 cell
->name
= new_name
;
1355 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1357 log_assert(count_id(old_name
) != 0);
1358 if (wires_
.count(old_name
))
1359 rename(wires_
.at(old_name
), new_name
);
1360 else if (cells_
.count(old_name
))
1361 rename(cells_
.at(old_name
), new_name
);
1366 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1368 log_assert(wires_
[w1
->name
] == w1
);
1369 log_assert(wires_
[w2
->name
] == w2
);
1370 log_assert(refcount_wires_
== 0);
1372 wires_
.erase(w1
->name
);
1373 wires_
.erase(w2
->name
);
1375 std::swap(w1
->name
, w2
->name
);
1377 wires_
[w1
->name
] = w1
;
1378 wires_
[w2
->name
] = w2
;
1381 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1383 log_assert(cells_
[c1
->name
] == c1
);
1384 log_assert(cells_
[c2
->name
] == c2
);
1385 log_assert(refcount_cells_
== 0);
1387 cells_
.erase(c1
->name
);
1388 cells_
.erase(c2
->name
);
1390 std::swap(c1
->name
, c2
->name
);
1392 cells_
[c1
->name
] = c1
;
1393 cells_
[c2
->name
] = c2
;
1396 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1399 return uniquify(name
, index
);
1402 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1405 if (count_id(name
) == 0)
1411 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1412 if (count_id(new_name
) == 0)
1418 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1420 if (a
->port_id
&& !b
->port_id
)
1422 if (!a
->port_id
&& b
->port_id
)
1425 if (a
->port_id
== b
->port_id
)
1426 return a
->name
< b
->name
;
1427 return a
->port_id
< b
->port_id
;
1430 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1432 for (auto mon
: monitors
)
1433 mon
->notify_connect(this, conn
);
1436 for (auto mon
: design
->monitors
)
1437 mon
->notify_connect(this, conn
);
1440 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
));
1441 log_backtrace("-X- ", yosys_xtrace
-1);
1444 connections_
.push_back(conn
);
1447 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1449 connect(RTLIL::SigSig(lhs
, rhs
));
1452 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1454 for (auto mon
: monitors
)
1455 mon
->notify_connect(this, new_conn
);
1458 for (auto mon
: design
->monitors
)
1459 mon
->notify_connect(this, new_conn
);
1462 log("#X# New connections vector in %s:\n", log_id(this));
1463 for (auto &conn
: new_conn
)
1464 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1465 log_backtrace("-X- ", yosys_xtrace
-1);
1468 connections_
= new_conn
;
1471 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1473 return connections_
;
1476 void RTLIL::Module::fixup_ports()
1478 std::vector
<RTLIL::Wire
*> all_ports
;
1480 for (auto &w
: wires_
)
1481 if (w
.second
->port_input
|| w
.second
->port_output
)
1482 all_ports
.push_back(w
.second
);
1484 w
.second
->port_id
= 0;
1486 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1489 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1490 ports
.push_back(all_ports
[i
]->name
);
1491 all_ports
[i
]->port_id
= i
+1;
1495 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1497 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1499 wire
->width
= width
;
1504 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1506 RTLIL::Wire
*wire
= addWire(name
);
1507 wire
->width
= other
->width
;
1508 wire
->start_offset
= other
->start_offset
;
1509 wire
->port_id
= other
->port_id
;
1510 wire
->port_input
= other
->port_input
;
1511 wire
->port_output
= other
->port_output
;
1512 wire
->upto
= other
->upto
;
1513 wire
->attributes
= other
->attributes
;
1517 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1519 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1526 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1528 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1529 cell
->connections_
= other
->connections_
;
1530 cell
->parameters
= other
->parameters
;
1531 cell
->attributes
= other
->attributes
;
1535 #define DEF_METHOD(_func, _y_size, _type) \
1536 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1537 RTLIL::Cell *cell = addCell(name, _type); \
1538 cell->parameters["\\A_SIGNED"] = is_signed; \
1539 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1540 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1541 cell->setPort("\\A", sig_a); \
1542 cell->setPort("\\Y", sig_y); \
1545 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1546 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1547 add ## _func(name, sig_a, sig_y, is_signed); \
1550 DEF_METHOD(Not
, sig_a
.size(), "$not")
1551 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1552 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1553 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1554 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1555 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1556 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1557 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1558 DEF_METHOD(LogicNot
, 1, "$logic_not")
1561 #define DEF_METHOD(_func, _y_size, _type) \
1562 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1563 RTLIL::Cell *cell = addCell(name, _type); \
1564 cell->parameters["\\A_SIGNED"] = is_signed; \
1565 cell->parameters["\\B_SIGNED"] = is_signed; \
1566 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1567 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1568 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1569 cell->setPort("\\A", sig_a); \
1570 cell->setPort("\\B", sig_b); \
1571 cell->setPort("\\Y", sig_y); \
1574 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1575 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1576 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1579 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1580 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1581 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1582 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1583 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1584 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1585 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1586 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1587 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1588 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1589 DEF_METHOD(Lt
, 1, "$lt")
1590 DEF_METHOD(Le
, 1, "$le")
1591 DEF_METHOD(Eq
, 1, "$eq")
1592 DEF_METHOD(Ne
, 1, "$ne")
1593 DEF_METHOD(Eqx
, 1, "$eqx")
1594 DEF_METHOD(Nex
, 1, "$nex")
1595 DEF_METHOD(Ge
, 1, "$ge")
1596 DEF_METHOD(Gt
, 1, "$gt")
1597 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1598 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1599 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1600 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1601 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1602 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1603 DEF_METHOD(LogicOr
, 1, "$logic_or")
1606 #define DEF_METHOD(_func, _type, _pmux) \
1607 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) { \
1608 RTLIL::Cell *cell = addCell(name, _type); \
1609 cell->parameters["\\WIDTH"] = sig_a.size(); \
1610 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1611 cell->setPort("\\A", sig_a); \
1612 cell->setPort("\\B", sig_b); \
1613 cell->setPort("\\S", sig_s); \
1614 cell->setPort("\\Y", sig_y); \
1617 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1618 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1619 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1622 DEF_METHOD(Mux
, "$mux", 0)
1623 DEF_METHOD(Pmux
, "$pmux", 1)
1626 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1627 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1628 RTLIL::Cell *cell = addCell(name, _type); \
1629 cell->setPort("\\" #_P1, sig1); \
1630 cell->setPort("\\" #_P2, sig2); \
1633 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1634 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1635 add ## _func(name, sig1, sig2); \
1638 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1639 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1640 RTLIL::Cell *cell = addCell(name, _type); \
1641 cell->setPort("\\" #_P1, sig1); \
1642 cell->setPort("\\" #_P2, sig2); \
1643 cell->setPort("\\" #_P3, sig3); \
1646 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1647 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1648 add ## _func(name, sig1, sig2, sig3); \
1651 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1652 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1653 RTLIL::Cell *cell = addCell(name, _type); \
1654 cell->setPort("\\" #_P1, sig1); \
1655 cell->setPort("\\" #_P2, sig2); \
1656 cell->setPort("\\" #_P3, sig3); \
1657 cell->setPort("\\" #_P4, sig4); \
1660 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1661 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1662 add ## _func(name, sig1, sig2, sig3, sig4); \
1665 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1666 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1667 RTLIL::Cell *cell = addCell(name, _type); \
1668 cell->setPort("\\" #_P1, sig1); \
1669 cell->setPort("\\" #_P2, sig2); \
1670 cell->setPort("\\" #_P3, sig3); \
1671 cell->setPort("\\" #_P4, sig4); \
1672 cell->setPort("\\" #_P5, sig5); \
1675 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1676 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1677 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1680 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1681 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1682 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1683 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1684 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1685 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1686 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1687 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1688 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1689 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1690 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1691 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1697 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
)
1699 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1700 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1701 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1702 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1703 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1704 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1705 cell
->setPort("\\A", sig_a
);
1706 cell
->setPort("\\B", sig_b
);
1707 cell
->setPort("\\Y", sig_y
);
1711 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1713 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1714 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1715 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1716 cell
->parameters
["\\OFFSET"] = offset
;
1717 cell
->setPort("\\A", sig_a
);
1718 cell
->setPort("\\Y", sig_y
);
1722 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1724 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1725 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1726 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1727 cell
->setPort("\\A", sig_a
);
1728 cell
->setPort("\\B", sig_b
);
1729 cell
->setPort("\\Y", sig_y
);
1733 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1735 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1736 cell
->parameters
["\\LUT"] = lut
;
1737 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1738 cell
->setPort("\\A", sig_i
);
1739 cell
->setPort("\\Y", sig_o
);
1743 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1745 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1746 cell
->setPort("\\A", sig_a
);
1747 cell
->setPort("\\EN", sig_en
);
1751 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1753 RTLIL::Cell
*cell
= addCell(name
, "$equiv");
1754 cell
->setPort("\\A", sig_a
);
1755 cell
->setPort("\\B", sig_b
);
1756 cell
->setPort("\\Y", sig_y
);
1760 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
)
1762 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1763 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1764 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1765 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1766 cell
->setPort("\\SET", sig_set
);
1767 cell
->setPort("\\CLR", sig_clr
);
1768 cell
->setPort("\\Q", sig_q
);
1772 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1774 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1775 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1776 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1777 cell
->setPort("\\CLK", sig_clk
);
1778 cell
->setPort("\\D", sig_d
);
1779 cell
->setPort("\\Q", sig_q
);
1783 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
)
1785 RTLIL::Cell
*cell
= addCell(name
, "$dffe");
1786 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1787 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1788 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1789 cell
->setPort("\\CLK", sig_clk
);
1790 cell
->setPort("\\EN", sig_en
);
1791 cell
->setPort("\\D", sig_d
);
1792 cell
->setPort("\\Q", sig_q
);
1796 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1797 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1799 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1800 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1801 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1802 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1803 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1804 cell
->setPort("\\CLK", sig_clk
);
1805 cell
->setPort("\\SET", sig_set
);
1806 cell
->setPort("\\CLR", sig_clr
);
1807 cell
->setPort("\\D", sig_d
);
1808 cell
->setPort("\\Q", sig_q
);
1812 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1813 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1815 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1816 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1817 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1818 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1819 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1820 cell
->setPort("\\CLK", sig_clk
);
1821 cell
->setPort("\\ARST", sig_arst
);
1822 cell
->setPort("\\D", sig_d
);
1823 cell
->setPort("\\Q", sig_q
);
1827 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1829 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1830 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1831 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1832 cell
->setPort("\\EN", sig_en
);
1833 cell
->setPort("\\D", sig_d
);
1834 cell
->setPort("\\Q", sig_q
);
1838 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1839 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1841 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1842 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1843 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1844 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1845 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1846 cell
->setPort("\\EN", sig_en
);
1847 cell
->setPort("\\SET", sig_set
);
1848 cell
->setPort("\\CLR", sig_clr
);
1849 cell
->setPort("\\D", sig_d
);
1850 cell
->setPort("\\Q", sig_q
);
1854 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1856 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1857 cell
->setPort("\\C", sig_clk
);
1858 cell
->setPort("\\D", sig_d
);
1859 cell
->setPort("\\Q", sig_q
);
1863 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
)
1865 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
1866 cell
->setPort("\\C", sig_clk
);
1867 cell
->setPort("\\E", sig_en
);
1868 cell
->setPort("\\D", sig_d
);
1869 cell
->setPort("\\Q", sig_q
);
1873 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1874 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1876 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1877 cell
->setPort("\\C", sig_clk
);
1878 cell
->setPort("\\S", sig_set
);
1879 cell
->setPort("\\R", sig_clr
);
1880 cell
->setPort("\\D", sig_d
);
1881 cell
->setPort("\\Q", sig_q
);
1885 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1886 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1888 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1889 cell
->setPort("\\C", sig_clk
);
1890 cell
->setPort("\\R", sig_arst
);
1891 cell
->setPort("\\D", sig_d
);
1892 cell
->setPort("\\Q", sig_q
);
1896 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1898 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1899 cell
->setPort("\\E", sig_en
);
1900 cell
->setPort("\\D", sig_d
);
1901 cell
->setPort("\\Q", sig_q
);
1905 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1906 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1908 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1909 cell
->setPort("\\E", sig_en
);
1910 cell
->setPort("\\S", sig_set
);
1911 cell
->setPort("\\R", sig_clr
);
1912 cell
->setPort("\\D", sig_d
);
1913 cell
->setPort("\\Q", sig_q
);
1920 static unsigned int hashidx_count
= 123456789;
1921 hashidx_count
= mkhash_xorshift(hashidx_count
);
1922 hashidx_
= hashidx_count
;
1929 port_output
= false;
1933 RTLIL::Memory::Memory()
1935 static unsigned int hashidx_count
= 123456789;
1936 hashidx_count
= mkhash_xorshift(hashidx_count
);
1937 hashidx_
= hashidx_count
;
1943 RTLIL::Cell::Cell() : module(nullptr)
1945 static unsigned int hashidx_count
= 123456789;
1946 hashidx_count
= mkhash_xorshift(hashidx_count
);
1947 hashidx_
= hashidx_count
;
1949 // log("#memtrace# %p\n", this);
1953 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1955 return connections_
.count(portname
) != 0;
1958 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1960 RTLIL::SigSpec signal
;
1961 auto conn_it
= connections_
.find(portname
);
1963 if (conn_it
!= connections_
.end())
1965 for (auto mon
: module
->monitors
)
1966 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1969 for (auto mon
: module
->design
->monitors
)
1970 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1973 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
1974 log_backtrace("-X- ", yosys_xtrace
-1);
1977 connections_
.erase(conn_it
);
1981 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1983 auto conn_it
= connections_
.find(portname
);
1985 if (conn_it
== connections_
.end()) {
1986 connections_
[portname
] = RTLIL::SigSpec();
1987 conn_it
= connections_
.find(portname
);
1988 log_assert(conn_it
!= connections_
.end());
1990 if (conn_it
->second
== signal
)
1993 for (auto mon
: module
->monitors
)
1994 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1997 for (auto mon
: module
->design
->monitors
)
1998 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2001 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2002 log_backtrace("-X- ", yosys_xtrace
-1);
2005 conn_it
->second
= signal
;
2008 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2010 return connections_
.at(portname
);
2013 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2015 return connections_
;
2018 bool RTLIL::Cell::known() const
2020 if (yosys_celltypes
.cell_known(type
))
2022 if (module
&& module
->design
&& module
->design
->module(type
))
2027 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2029 if (yosys_celltypes
.cell_known(type
))
2030 return yosys_celltypes
.cell_input(type
, portname
);
2031 if (module
&& module
->design
) {
2032 RTLIL::Module
*m
= module
->design
->module(type
);
2033 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2034 return w
&& w
->port_input
;
2039 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2041 if (yosys_celltypes
.cell_known(type
))
2042 return yosys_celltypes
.cell_output(type
, portname
);
2043 if (module
&& module
->design
) {
2044 RTLIL::Module
*m
= module
->design
->module(type
);
2045 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2046 return w
&& w
->port_output
;
2051 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2053 return parameters
.count(paramname
) != 0;
2056 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2058 parameters
.erase(paramname
);
2061 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2063 parameters
[paramname
] = value
;
2066 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2068 return parameters
.at(paramname
);
2071 void RTLIL::Cell::sort()
2073 connections_
.sort(sort_by_id_str());
2074 parameters
.sort(sort_by_id_str());
2075 attributes
.sort(sort_by_id_str());
2078 void RTLIL::Cell::check()
2081 InternalCellChecker
checker(NULL
, this);
2086 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2088 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
2089 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
2092 if (type
== "$mux" || type
== "$pmux") {
2093 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2094 if (type
== "$pmux")
2095 parameters
["\\S_WIDTH"] = GetSize(connections_
["\\S"]);
2100 if (type
== "$lut") {
2101 parameters
["\\WIDTH"] = GetSize(connections_
["\\A"]);
2105 if (type
== "$fa") {
2106 parameters
["\\WIDTH"] = GetSize(connections_
["\\Y"]);
2110 if (type
== "$lcu") {
2111 parameters
["\\WIDTH"] = GetSize(connections_
["\\CO"]);
2115 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
2117 if (connections_
.count("\\A")) {
2118 if (signedness_ab
) {
2120 parameters
["\\A_SIGNED"] = true;
2121 else if (parameters
.count("\\A_SIGNED") == 0)
2122 parameters
["\\A_SIGNED"] = false;
2124 parameters
["\\A_WIDTH"] = GetSize(connections_
["\\A"]);
2127 if (connections_
.count("\\B")) {
2128 if (signedness_ab
) {
2130 parameters
["\\B_SIGNED"] = true;
2131 else if (parameters
.count("\\B_SIGNED") == 0)
2132 parameters
["\\B_SIGNED"] = false;
2134 parameters
["\\B_WIDTH"] = GetSize(connections_
["\\B"]);
2137 if (connections_
.count("\\Y"))
2138 parameters
["\\Y_WIDTH"] = GetSize(connections_
["\\Y"]);
2143 RTLIL::SigChunk::SigChunk()
2150 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2154 width
= GetSize(data
);
2158 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2160 log_assert(wire
!= nullptr);
2162 this->width
= wire
->width
;
2166 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2168 log_assert(wire
!= nullptr);
2170 this->width
= width
;
2171 this->offset
= offset
;
2174 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2177 data
= RTLIL::Const(str
).bits
;
2178 width
= GetSize(data
);
2182 RTLIL::SigChunk::SigChunk(int val
, int width
)
2185 data
= RTLIL::Const(val
, width
).bits
;
2186 this->width
= GetSize(data
);
2190 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2193 data
= RTLIL::Const(bit
, width
).bits
;
2194 this->width
= GetSize(data
);
2198 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
2203 data
= RTLIL::Const(bit
.data
).bits
;
2205 offset
= bit
.offset
;
2209 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2211 RTLIL::SigChunk ret
;
2214 ret
.offset
= this->offset
+ offset
;
2217 for (int i
= 0; i
< length
; i
++)
2218 ret
.data
.push_back(data
[offset
+i
]);
2224 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2226 if (wire
&& other
.wire
)
2227 if (wire
->name
!= other
.wire
->name
)
2228 return wire
->name
< other
.wire
->name
;
2230 if (wire
!= other
.wire
)
2231 return wire
< other
.wire
;
2233 if (offset
!= other
.offset
)
2234 return offset
< other
.offset
;
2236 if (width
!= other
.width
)
2237 return width
< other
.width
;
2239 return data
< other
.data
;
2242 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2244 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2247 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2254 RTLIL::SigSpec::SigSpec()
2260 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2265 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2267 cover("kernel.rtlil.sigspec.init.list");
2272 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
2273 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
2277 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2279 cover("kernel.rtlil.sigspec.assign");
2281 width_
= other
.width_
;
2282 hash_
= other
.hash_
;
2283 chunks_
= other
.chunks_
;
2286 if (!other
.bits_
.empty())
2288 RTLIL::SigChunk
*last
= NULL
;
2289 int last_end_offset
= 0;
2291 for (auto &bit
: other
.bits_
) {
2292 if (last
&& bit
.wire
== last
->wire
) {
2293 if (bit
.wire
== NULL
) {
2294 last
->data
.push_back(bit
.data
);
2297 } else if (last_end_offset
== bit
.offset
) {
2303 chunks_
.push_back(bit
);
2304 last
= &chunks_
.back();
2305 last_end_offset
= bit
.offset
+ 1;
2314 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2316 cover("kernel.rtlil.sigspec.init.const");
2318 chunks_
.push_back(RTLIL::SigChunk(value
));
2319 width_
= chunks_
.back().width
;
2324 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2326 cover("kernel.rtlil.sigspec.init.chunk");
2328 chunks_
.push_back(chunk
);
2329 width_
= chunks_
.back().width
;
2334 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2336 cover("kernel.rtlil.sigspec.init.wire");
2338 chunks_
.push_back(RTLIL::SigChunk(wire
));
2339 width_
= chunks_
.back().width
;
2344 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2346 cover("kernel.rtlil.sigspec.init.wire_part");
2348 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2349 width_
= chunks_
.back().width
;
2354 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2356 cover("kernel.rtlil.sigspec.init.str");
2358 chunks_
.push_back(RTLIL::SigChunk(str
));
2359 width_
= chunks_
.back().width
;
2364 RTLIL::SigSpec::SigSpec(int val
, int width
)
2366 cover("kernel.rtlil.sigspec.init.int");
2368 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2374 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2376 cover("kernel.rtlil.sigspec.init.state");
2378 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2384 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2386 cover("kernel.rtlil.sigspec.init.bit");
2388 if (bit
.wire
== NULL
)
2389 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2391 for (int i
= 0; i
< width
; i
++)
2392 chunks_
.push_back(bit
);
2398 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2400 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2404 for (auto &c
: chunks
)
2409 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2411 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2415 for (auto &bit
: bits
)
2420 RTLIL::SigSpec::SigSpec(pool
<RTLIL::SigBit
> bits
)
2422 cover("kernel.rtlil.sigspec.init.pool_bits");
2426 for (auto &bit
: bits
)
2431 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2433 cover("kernel.rtlil.sigspec.init.stdset_bits");
2437 for (auto &bit
: bits
)
2442 RTLIL::SigSpec::SigSpec(bool bit
)
2444 cover("kernel.rtlil.sigspec.init.bool");
2452 void RTLIL::SigSpec::pack() const
2454 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2456 if (that
->bits_
.empty())
2459 cover("kernel.rtlil.sigspec.convert.pack");
2460 log_assert(that
->chunks_
.empty());
2462 std::vector
<RTLIL::SigBit
> old_bits
;
2463 old_bits
.swap(that
->bits_
);
2465 RTLIL::SigChunk
*last
= NULL
;
2466 int last_end_offset
= 0;
2468 for (auto &bit
: old_bits
) {
2469 if (last
&& bit
.wire
== last
->wire
) {
2470 if (bit
.wire
== NULL
) {
2471 last
->data
.push_back(bit
.data
);
2474 } else if (last_end_offset
== bit
.offset
) {
2480 that
->chunks_
.push_back(bit
);
2481 last
= &that
->chunks_
.back();
2482 last_end_offset
= bit
.offset
+ 1;
2488 void RTLIL::SigSpec::unpack() const
2490 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2492 if (that
->chunks_
.empty())
2495 cover("kernel.rtlil.sigspec.convert.unpack");
2496 log_assert(that
->bits_
.empty());
2498 that
->bits_
.reserve(that
->width_
);
2499 for (auto &c
: that
->chunks_
)
2500 for (int i
= 0; i
< c
.width
; i
++)
2501 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2503 that
->chunks_
.clear();
2507 void RTLIL::SigSpec::updhash() const
2509 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2511 if (that
->hash_
!= 0)
2514 cover("kernel.rtlil.sigspec.hash");
2517 that
->hash_
= mkhash_init
;
2518 for (auto &c
: that
->chunks_
)
2519 if (c
.wire
== NULL
) {
2520 for (auto &v
: c
.data
)
2521 that
->hash_
= mkhash(that
->hash_
, v
);
2523 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
2524 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
2525 that
->hash_
= mkhash(that
->hash_
, c
.width
);
2528 if (that
->hash_
== 0)
2532 void RTLIL::SigSpec::sort()
2535 cover("kernel.rtlil.sigspec.sort");
2536 std::sort(bits_
.begin(), bits_
.end());
2539 void RTLIL::SigSpec::sort_and_unify()
2541 cover("kernel.rtlil.sigspec.sort_and_unify");
2542 *this = this->to_sigbit_set();
2545 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2547 replace(pattern
, with
, this);
2550 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2552 log_assert(pattern
.width_
== with
.width_
);
2557 dict
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2559 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++)
2560 if (pattern
.bits_
[i
].wire
!= NULL
)
2561 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2563 replace(rules
, other
);
2566 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2568 replace(rules
, this);
2571 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2573 cover("kernel.rtlil.sigspec.replace_dict");
2575 log_assert(other
!= NULL
);
2576 log_assert(width_
== other
->width_
);
2581 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2582 auto it
= rules
.find(bits_
[i
]);
2583 if (it
!= rules
.end())
2584 other
->bits_
[i
] = it
->second
;
2590 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2592 replace(rules
, this);
2595 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2597 cover("kernel.rtlil.sigspec.replace_map");
2599 log_assert(other
!= NULL
);
2600 log_assert(width_
== other
->width_
);
2605 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2606 auto it
= rules
.find(bits_
[i
]);
2607 if (it
!= rules
.end())
2608 other
->bits_
[i
] = it
->second
;
2614 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2616 remove2(pattern
, NULL
);
2619 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2621 RTLIL::SigSpec tmp
= *this;
2622 tmp
.remove2(pattern
, other
);
2625 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2627 pool
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_pool();
2628 remove2(pattern_bits
, other
);
2631 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
2633 remove2(pattern
, NULL
);
2636 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2638 RTLIL::SigSpec tmp
= *this;
2639 tmp
.remove2(pattern
, other
);
2642 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2645 cover("kernel.rtlil.sigspec.remove_other");
2647 cover("kernel.rtlil.sigspec.remove");
2651 if (other
!= NULL
) {
2652 log_assert(width_
== other
->width_
);
2656 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2658 new_bits
.resize(GetSize(bits_
));
2660 new_other_bits
.resize(GetSize(bits_
));
2663 for (int i
= 0; i
< GetSize(bits_
); i
++) {
2664 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2667 new_other_bits
[k
] = other
->bits_
[i
];
2668 new_bits
[k
++] = bits_
[i
];
2673 new_other_bits
.resize(k
);
2675 bits_
.swap(new_bits
);
2676 width_
= GetSize(bits_
);
2678 if (other
!= NULL
) {
2679 other
->bits_
.swap(new_other_bits
);
2680 other
->width_
= GetSize(other
->bits_
);
2686 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2688 pool
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_pool();
2689 return extract(pattern_bits
, other
);
2692 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2695 cover("kernel.rtlil.sigspec.extract_other");
2697 cover("kernel.rtlil.sigspec.extract");
2699 log_assert(other
== NULL
|| width_
== other
->width_
);
2701 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2705 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2706 for (int i
= 0; i
< width_
; i
++)
2707 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2708 ret
.append_bit(bits_other
[i
]);
2710 for (int i
= 0; i
< width_
; i
++)
2711 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2712 ret
.append_bit(bits_match
[i
]);
2719 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2721 cover("kernel.rtlil.sigspec.replace_pos");
2726 log_assert(offset
>= 0);
2727 log_assert(with
.width_
>= 0);
2728 log_assert(offset
+with
.width_
<= width_
);
2730 for (int i
= 0; i
< with
.width_
; i
++)
2731 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2736 void RTLIL::SigSpec::remove_const()
2740 cover("kernel.rtlil.sigspec.remove_const.packed");
2742 std::vector
<RTLIL::SigChunk
> new_chunks
;
2743 new_chunks
.reserve(GetSize(chunks_
));
2746 for (auto &chunk
: chunks_
)
2747 if (chunk
.wire
!= NULL
) {
2748 new_chunks
.push_back(chunk
);
2749 width_
+= chunk
.width
;
2752 chunks_
.swap(new_chunks
);
2756 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2758 std::vector
<RTLIL::SigBit
> new_bits
;
2759 new_bits
.reserve(width_
);
2761 for (auto &bit
: bits_
)
2762 if (bit
.wire
!= NULL
)
2763 new_bits
.push_back(bit
);
2765 bits_
.swap(new_bits
);
2766 width_
= bits_
.size();
2772 void RTLIL::SigSpec::remove(int offset
, int length
)
2774 cover("kernel.rtlil.sigspec.remove_pos");
2778 log_assert(offset
>= 0);
2779 log_assert(length
>= 0);
2780 log_assert(offset
+ length
<= width_
);
2782 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2783 width_
= bits_
.size();
2788 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2791 cover("kernel.rtlil.sigspec.extract_pos");
2792 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2795 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2797 if (signal
.width_
== 0)
2805 cover("kernel.rtlil.sigspec.append");
2807 if (packed() != signal
.packed()) {
2813 for (auto &other_c
: signal
.chunks_
)
2815 auto &my_last_c
= chunks_
.back();
2816 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2817 auto &this_data
= my_last_c
.data
;
2818 auto &other_data
= other_c
.data
;
2819 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2820 my_last_c
.width
+= other_c
.width
;
2822 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2823 my_last_c
.width
+= other_c
.width
;
2825 chunks_
.push_back(other_c
);
2828 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2830 width_
+= signal
.width_
;
2834 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2838 cover("kernel.rtlil.sigspec.append_bit.packed");
2840 if (chunks_
.size() == 0)
2841 chunks_
.push_back(bit
);
2843 if (bit
.wire
== NULL
)
2844 if (chunks_
.back().wire
== NULL
) {
2845 chunks_
.back().data
.push_back(bit
.data
);
2846 chunks_
.back().width
++;
2848 chunks_
.push_back(bit
);
2850 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2851 chunks_
.back().width
++;
2853 chunks_
.push_back(bit
);
2857 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2858 bits_
.push_back(bit
);
2865 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2867 cover("kernel.rtlil.sigspec.extend_u0");
2872 remove(width
, width_
- width
);
2874 if (width_
< width
) {
2875 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::S0
;
2877 padding
= RTLIL::State::S0
;
2878 while (width_
< width
)
2884 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2886 cover("kernel.rtlil.sigspec.repeat");
2889 for (int i
= 0; i
< num
; i
++)
2895 void RTLIL::SigSpec::check() const
2899 cover("kernel.rtlil.sigspec.check.skip");
2903 cover("kernel.rtlil.sigspec.check.packed");
2906 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2907 const RTLIL::SigChunk chunk
= chunks_
[i
];
2908 if (chunk
.wire
== NULL
) {
2910 log_assert(chunks_
[i
-1].wire
!= NULL
);
2911 log_assert(chunk
.offset
== 0);
2912 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
2914 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2915 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2916 log_assert(chunk
.offset
>= 0);
2917 log_assert(chunk
.width
>= 0);
2918 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2919 log_assert(chunk
.data
.size() == 0);
2923 log_assert(w
== width_
);
2924 log_assert(bits_
.empty());
2928 cover("kernel.rtlil.sigspec.check.unpacked");
2930 log_assert(width_
== GetSize(bits_
));
2931 log_assert(chunks_
.empty());
2936 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2938 cover("kernel.rtlil.sigspec.comp_lt");
2943 if (width_
!= other
.width_
)
2944 return width_
< other
.width_
;
2949 if (chunks_
.size() != other
.chunks_
.size())
2950 return chunks_
.size() < other
.chunks_
.size();
2955 if (hash_
!= other
.hash_
)
2956 return hash_
< other
.hash_
;
2958 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2959 if (chunks_
[i
] != other
.chunks_
[i
]) {
2960 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2961 return chunks_
[i
] < other
.chunks_
[i
];
2964 cover("kernel.rtlil.sigspec.comp_lt.equal");
2968 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2970 cover("kernel.rtlil.sigspec.comp_eq");
2975 if (width_
!= other
.width_
)
2981 if (chunks_
.size() != chunks_
.size())
2987 if (hash_
!= other
.hash_
)
2990 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2991 if (chunks_
[i
] != other
.chunks_
[i
]) {
2992 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2996 cover("kernel.rtlil.sigspec.comp_eq.equal");
3000 bool RTLIL::SigSpec::is_wire() const
3002 cover("kernel.rtlil.sigspec.is_wire");
3005 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3008 bool RTLIL::SigSpec::is_chunk() const
3010 cover("kernel.rtlil.sigspec.is_chunk");
3013 return GetSize(chunks_
) == 1;
3016 bool RTLIL::SigSpec::is_fully_const() const
3018 cover("kernel.rtlil.sigspec.is_fully_const");
3021 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3022 if (it
->width
> 0 && it
->wire
!= NULL
)
3027 bool RTLIL::SigSpec::is_fully_zero() const
3029 cover("kernel.rtlil.sigspec.is_fully_zero");
3032 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3033 if (it
->width
> 0 && it
->wire
!= NULL
)
3035 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3036 if (it
->data
[i
] != RTLIL::State::S0
)
3042 bool RTLIL::SigSpec::is_fully_def() const
3044 cover("kernel.rtlil.sigspec.is_fully_def");
3047 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3048 if (it
->width
> 0 && it
->wire
!= NULL
)
3050 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3051 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3057 bool RTLIL::SigSpec::is_fully_undef() const
3059 cover("kernel.rtlil.sigspec.is_fully_undef");
3062 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3063 if (it
->width
> 0 && it
->wire
!= NULL
)
3065 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3066 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3072 bool RTLIL::SigSpec::has_const() const
3074 cover("kernel.rtlil.sigspec.has_const");
3077 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3078 if (it
->width
> 0 && it
->wire
== NULL
)
3083 bool RTLIL::SigSpec::has_marked_bits() const
3085 cover("kernel.rtlil.sigspec.has_marked_bits");
3088 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3089 if (it
->width
> 0 && it
->wire
== NULL
) {
3090 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3091 if (it
->data
[i
] == RTLIL::State::Sm
)
3097 bool RTLIL::SigSpec::as_bool() const
3099 cover("kernel.rtlil.sigspec.as_bool");
3102 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3104 return RTLIL::Const(chunks_
[0].data
).as_bool();
3108 int RTLIL::SigSpec::as_int(bool is_signed
) const
3110 cover("kernel.rtlil.sigspec.as_int");
3113 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3115 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3119 std::string
RTLIL::SigSpec::as_string() const
3121 cover("kernel.rtlil.sigspec.as_string");
3125 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3126 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3127 if (chunk
.wire
!= NULL
)
3128 for (int j
= 0; j
< chunk
.width
; j
++)
3131 str
+= RTLIL::Const(chunk
.data
).as_string();
3136 RTLIL::Const
RTLIL::SigSpec::as_const() const
3138 cover("kernel.rtlil.sigspec.as_const");
3141 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3143 return chunks_
[0].data
;
3144 return RTLIL::Const();
3147 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3149 cover("kernel.rtlil.sigspec.as_wire");
3152 log_assert(is_wire());
3153 return chunks_
[0].wire
;
3156 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3158 cover("kernel.rtlil.sigspec.as_chunk");
3161 log_assert(is_chunk());
3165 bool RTLIL::SigSpec::match(std::string pattern
) const
3167 cover("kernel.rtlil.sigspec.match");
3170 std::string str
= as_string();
3171 log_assert(pattern
.size() == str
.size());
3173 for (size_t i
= 0; i
< pattern
.size(); i
++) {
3174 if (pattern
[i
] == ' ')
3176 if (pattern
[i
] == '*') {
3177 if (str
[i
] != 'z' && str
[i
] != 'x')
3181 if (pattern
[i
] != str
[i
])
3188 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3190 cover("kernel.rtlil.sigspec.to_sigbit_set");
3193 std::set
<RTLIL::SigBit
> sigbits
;
3194 for (auto &c
: chunks_
)
3195 for (int i
= 0; i
< c
.width
; i
++)
3196 sigbits
.insert(RTLIL::SigBit(c
, i
));
3200 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3202 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3205 pool
<RTLIL::SigBit
> sigbits
;
3206 for (auto &c
: chunks_
)
3207 for (int i
= 0; i
< c
.width
; i
++)
3208 sigbits
.insert(RTLIL::SigBit(c
, i
));
3212 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3214 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3220 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3222 cover("kernel.rtlil.sigspec.to_sigbit_map");
3227 log_assert(width_
== other
.width_
);
3229 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3230 for (int i
= 0; i
< width_
; i
++)
3231 new_map
[bits_
[i
]] = other
.bits_
[i
];
3236 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3238 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3243 log_assert(width_
== other
.width_
);
3245 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3246 for (int i
= 0; i
< width_
; i
++)
3247 new_map
[bits_
[i
]] = other
.bits_
[i
];
3252 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
3254 cover("kernel.rtlil.sigspec.to_single_sigbit");
3257 log_assert(width_
== 1);
3258 for (auto &c
: chunks_
)
3260 return RTLIL::SigBit(c
);
3264 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3266 size_t start
= 0, end
= 0;
3267 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3268 tokens
.push_back(text
.substr(start
, end
- start
));
3271 tokens
.push_back(text
.substr(start
));
3274 static int sigspec_parse_get_dummy_line_num()
3279 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3281 cover("kernel.rtlil.sigspec.parse");
3283 std::vector
<std::string
> tokens
;
3284 sigspec_parse_split(tokens
, str
, ',');
3286 sig
= RTLIL::SigSpec();
3287 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3289 std::string netname
= tokens
[tokidx
];
3290 std::string indices
;
3292 if (netname
.size() == 0)
3295 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
3296 cover("kernel.rtlil.sigspec.parse.const");
3297 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
3298 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
3301 sig
.append(RTLIL::Const(ast
->bits
));
3309 cover("kernel.rtlil.sigspec.parse.net");
3311 if (netname
[0] != '$' && netname
[0] != '\\')
3312 netname
= "\\" + netname
;
3314 if (module
->wires_
.count(netname
) == 0) {
3315 size_t indices_pos
= netname
.size()-1;
3316 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
3319 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3320 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
3322 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
3324 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
3325 indices
= netname
.substr(indices_pos
);
3326 netname
= netname
.substr(0, indices_pos
);
3331 if (module
->wires_
.count(netname
) == 0)
3334 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
3335 if (!indices
.empty()) {
3336 std::vector
<std::string
> index_tokens
;
3337 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
3338 if (index_tokens
.size() == 1) {
3339 cover("kernel.rtlil.sigspec.parse.bit_sel");
3340 int a
= atoi(index_tokens
.at(0).c_str());
3341 if (a
< 0 || a
>= wire
->width
)
3343 sig
.append(RTLIL::SigSpec(wire
, a
));
3345 cover("kernel.rtlil.sigspec.parse.part_sel");
3346 int a
= atoi(index_tokens
.at(0).c_str());
3347 int b
= atoi(index_tokens
.at(1).c_str());
3352 if (a
< 0 || a
>= wire
->width
)
3354 if (b
< 0 || b
>= wire
->width
)
3356 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
3365 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
3367 if (str
.empty() || str
[0] != '@')
3368 return parse(sig
, module
, str
);
3370 cover("kernel.rtlil.sigspec.parse.sel");
3372 str
= RTLIL::escape_id(str
.substr(1));
3373 if (design
->selection_vars
.count(str
) == 0)
3376 sig
= RTLIL::SigSpec();
3377 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3378 for (auto &it
: module
->wires_
)
3379 if (sel
.selected_member(module
->name
, it
.first
))
3380 sig
.append(it
.second
);
3385 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3388 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3389 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3394 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3395 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3399 if (lhs
.chunks_
.size() == 1) {
3400 char *p
= (char*)str
.c_str(), *endptr
;
3401 long int val
= strtol(p
, &endptr
, 10);
3402 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3403 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3404 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3409 return parse(sig
, module
, str
);
3412 RTLIL::CaseRule::~CaseRule()
3414 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3418 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3420 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3421 new_caserule
->compare
= compare
;
3422 new_caserule
->actions
= actions
;
3423 for (auto &it
: switches
)
3424 new_caserule
->switches
.push_back(it
->clone());
3425 return new_caserule
;
3428 RTLIL::SwitchRule::~SwitchRule()
3430 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3434 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3436 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3437 new_switchrule
->signal
= signal
;
3438 new_switchrule
->attributes
= attributes
;
3439 for (auto &it
: cases
)
3440 new_switchrule
->cases
.push_back(it
->clone());
3441 return new_switchrule
;
3445 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3447 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3448 new_syncrule
->type
= type
;
3449 new_syncrule
->signal
= signal
;
3450 new_syncrule
->actions
= actions
;
3451 return new_syncrule
;
3454 RTLIL::Process::~Process()
3456 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3460 RTLIL::Process
*RTLIL::Process::clone() const
3462 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3464 new_proc
->name
= name
;
3465 new_proc
->attributes
= attributes
;
3467 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3468 new_proc
->root_case
= *rc_ptr
;
3469 rc_ptr
->switches
.clear();
3472 for (auto &it
: syncs
)
3473 new_proc
->syncs
.push_back(it
->clone());