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 "frontends/verilog/preproc.h"
25 #include "backends/ilang/ilang_backend.h"
32 RTLIL::IdString::destruct_guard_t
RTLIL::IdString::destruct_guard
;
33 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
34 dict
<char*, int, hash_cstr_ops
> RTLIL::IdString::global_id_index_
;
35 #ifndef YOSYS_NO_IDS_REFCNT
36 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
37 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
39 #ifdef YOSYS_USE_STICKY_IDS
40 int RTLIL::IdString::last_created_idx_
[8];
41 int RTLIL::IdString::last_created_idx_ptr_
;
44 #define X(_id) IdString RTLIL::ID::_id;
45 #include "kernel/constids.inc"
48 dict
<std::string
, std::string
> RTLIL::constpad
;
50 const pool
<IdString
> &RTLIL::builtin_ff_cell_types() {
51 static const pool
<IdString
> res
= {
99 flags
= RTLIL::CONST_FLAG_NONE
;
102 RTLIL::Const::Const(std::string str
)
104 flags
= RTLIL::CONST_FLAG_STRING
;
105 for (int i
= str
.size()-1; i
>= 0; i
--) {
106 unsigned char ch
= str
[i
];
107 for (int j
= 0; j
< 8; j
++) {
108 bits
.push_back((ch
& 1) != 0 ? State::S1
: State::S0
);
114 RTLIL::Const::Const(int val
, int width
)
116 flags
= RTLIL::CONST_FLAG_NONE
;
117 for (int i
= 0; i
< width
; i
++) {
118 bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
123 RTLIL::Const::Const(RTLIL::State bit
, int width
)
125 flags
= RTLIL::CONST_FLAG_NONE
;
126 for (int i
= 0; i
< width
; i
++)
130 RTLIL::Const::Const(const std::vector
<bool> &bits
)
132 flags
= RTLIL::CONST_FLAG_NONE
;
133 for (const auto &b
: bits
)
134 this->bits
.emplace_back(b
? State::S1
: State::S0
);
137 RTLIL::Const::Const(const RTLIL::Const
&c
)
140 for (const auto &b
: c
.bits
)
141 this->bits
.push_back(b
);
144 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
146 if (bits
.size() != other
.bits
.size())
147 return bits
.size() < other
.bits
.size();
148 for (size_t i
= 0; i
< bits
.size(); i
++)
149 if (bits
[i
] != other
.bits
[i
])
150 return bits
[i
] < other
.bits
[i
];
154 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
156 return bits
== other
.bits
;
159 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
161 return bits
!= other
.bits
;
164 bool RTLIL::Const::as_bool() const
166 for (size_t i
= 0; i
< bits
.size(); i
++)
167 if (bits
[i
] == State::S1
)
172 int RTLIL::Const::as_int(bool is_signed
) const
175 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
176 if (bits
[i
] == State::S1
)
178 if (is_signed
&& bits
.back() == State::S1
)
179 for (size_t i
= bits
.size(); i
< 32; i
++)
184 std::string
RTLIL::Const::as_string() const
187 ret
.reserve(bits
.size());
188 for (size_t i
= bits
.size(); i
> 0; i
--)
190 case S0
: ret
+= "0"; break;
191 case S1
: ret
+= "1"; break;
192 case Sx
: ret
+= "x"; break;
193 case Sz
: ret
+= "z"; break;
194 case Sa
: ret
+= "-"; break;
195 case Sm
: ret
+= "m"; break;
200 RTLIL::Const
RTLIL::Const::from_string(const std::string
&str
)
203 c
.bits
.reserve(str
.size());
204 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
206 case '0': c
.bits
.push_back(State::S0
); break;
207 case '1': c
.bits
.push_back(State::S1
); break;
208 case 'x': c
.bits
.push_back(State::Sx
); break;
209 case 'z': c
.bits
.push_back(State::Sz
); break;
210 case 'm': c
.bits
.push_back(State::Sm
); break;
211 default: c
.bits
.push_back(State::Sa
);
216 std::string
RTLIL::Const::decode_string() const
219 string
.reserve(GetSize(bits
)/8);
220 for (int i
= 0; i
< GetSize(bits
); i
+= 8) {
222 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
223 if (bits
[i
+ j
] == RTLIL::State::S1
)
228 std::reverse(string
.begin(), string
.end());
232 bool RTLIL::Const::is_fully_zero() const
234 cover("kernel.rtlil.const.is_fully_zero");
236 for (const auto &bit
: bits
)
237 if (bit
!= RTLIL::State::S0
)
243 bool RTLIL::Const::is_fully_ones() const
245 cover("kernel.rtlil.const.is_fully_ones");
247 for (const auto &bit
: bits
)
248 if (bit
!= RTLIL::State::S1
)
254 bool RTLIL::Const::is_fully_def() const
256 cover("kernel.rtlil.const.is_fully_def");
258 for (const auto &bit
: bits
)
259 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
265 bool RTLIL::Const::is_fully_undef() const
267 cover("kernel.rtlil.const.is_fully_undef");
269 for (const auto &bit
: bits
)
270 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
276 bool RTLIL::AttrObject::has_attribute(RTLIL::IdString id
) const
278 return attributes
.count(id
);
281 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
284 attributes
[id
] = RTLIL::Const(1);
286 attributes
.erase(id
);
289 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
291 const auto it
= attributes
.find(id
);
292 if (it
== attributes
.end())
294 return it
->second
.as_bool();
297 void RTLIL::AttrObject::set_string_attribute(RTLIL::IdString id
, string value
)
300 attributes
.erase(id
);
302 attributes
[id
] = value
;
305 string
RTLIL::AttrObject::get_string_attribute(RTLIL::IdString id
) const
308 const auto it
= attributes
.find(id
);
309 if (it
!= attributes
.end())
310 value
= it
->second
.decode_string();
314 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
317 for (const auto &s
: data
) {
318 if (!attrval
.empty())
322 set_string_attribute(id
, attrval
);
325 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
327 pool
<string
> union_data
= get_strpool_attribute(id
);
328 union_data
.insert(data
.begin(), data
.end());
329 if (!union_data
.empty())
330 set_strpool_attribute(id
, union_data
);
333 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
336 if (attributes
.count(id
) != 0)
337 for (auto s
: split_tokens(get_string_attribute(id
), "|"))
342 void RTLIL::AttrObject::set_hdlname_attribute(const vector
<string
> &hierarchy
)
345 for (const auto &ident
: hierarchy
) {
346 if (!attrval
.empty())
350 set_string_attribute(ID::hdlname
, attrval
);
353 vector
<string
> RTLIL::AttrObject::get_hdlname_attribute() const
355 return split_tokens(get_string_attribute(ID::hdlname
), " ");
358 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
362 if (selected_modules
.count(mod_name
) > 0)
364 if (selected_members
.count(mod_name
) > 0)
369 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
373 if (selected_modules
.count(mod_name
) > 0)
378 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
382 if (selected_modules
.count(mod_name
) > 0)
384 if (selected_members
.count(mod_name
) > 0)
385 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
390 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
392 if (full_selection
) {
393 selected_modules
.clear();
394 selected_members
.clear();
398 std::vector
<RTLIL::IdString
> del_list
, add_list
;
401 for (auto mod_name
: selected_modules
) {
402 if (design
->modules_
.count(mod_name
) == 0)
403 del_list
.push_back(mod_name
);
404 selected_members
.erase(mod_name
);
406 for (auto mod_name
: del_list
)
407 selected_modules
.erase(mod_name
);
410 for (auto &it
: selected_members
)
411 if (design
->modules_
.count(it
.first
) == 0)
412 del_list
.push_back(it
.first
);
413 for (auto mod_name
: del_list
)
414 selected_members
.erase(mod_name
);
416 for (auto &it
: selected_members
) {
418 for (auto memb_name
: it
.second
)
419 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
420 del_list
.push_back(memb_name
);
421 for (auto memb_name
: del_list
)
422 it
.second
.erase(memb_name
);
427 for (auto &it
: selected_members
)
428 if (it
.second
.size() == 0)
429 del_list
.push_back(it
.first
);
430 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
431 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
432 add_list
.push_back(it
.first
);
433 for (auto mod_name
: del_list
)
434 selected_members
.erase(mod_name
);
435 for (auto mod_name
: add_list
) {
436 selected_members
.erase(mod_name
);
437 selected_modules
.insert(mod_name
);
440 if (selected_modules
.size() == design
->modules_
.size()) {
441 full_selection
= true;
442 selected_modules
.clear();
443 selected_members
.clear();
447 RTLIL::Design::Design()
448 : verilog_defines (new define_map_t
)
450 static unsigned int hashidx_count
= 123456789;
451 hashidx_count
= mkhash_xorshift(hashidx_count
);
452 hashidx_
= hashidx_count
;
454 refcount_modules_
= 0;
455 selection_stack
.push_back(RTLIL::Selection());
458 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
462 RTLIL::Design::~Design()
464 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
466 for (auto n
: verilog_packages
)
468 for (auto n
: verilog_globals
)
471 RTLIL::Design::get_all_designs()->erase(hashidx_
);
476 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
477 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
483 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
485 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
488 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
490 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
493 RTLIL::Module
*RTLIL::Design::top_module()
495 RTLIL::Module
*module
= nullptr;
496 int module_count
= 0;
498 for (auto mod
: selected_modules()) {
499 if (mod
->get_bool_attribute(ID::top
))
505 return module_count
== 1 ? module
: nullptr;
508 void RTLIL::Design::add(RTLIL::Module
*module
)
510 log_assert(modules_
.count(module
->name
) == 0);
511 log_assert(refcount_modules_
== 0);
512 modules_
[module
->name
] = module
;
513 module
->design
= this;
515 for (auto mon
: monitors
)
516 mon
->notify_module_add(module
);
519 log("#X# New Module: %s\n", log_id(module
));
520 log_backtrace("-X- ", yosys_xtrace
-1);
524 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
526 log_assert(modules_
.count(name
) == 0);
527 log_assert(refcount_modules_
== 0);
529 RTLIL::Module
*module
= new RTLIL::Module
;
530 modules_
[name
] = module
;
531 module
->design
= this;
534 for (auto mon
: monitors
)
535 mon
->notify_module_add(module
);
538 log("#X# New Module: %s\n", log_id(module
));
539 log_backtrace("-X- ", yosys_xtrace
-1);
545 void RTLIL::Design::scratchpad_unset(const std::string
&varname
)
547 scratchpad
.erase(varname
);
550 void RTLIL::Design::scratchpad_set_int(const std::string
&varname
, int value
)
552 scratchpad
[varname
] = stringf("%d", value
);
555 void RTLIL::Design::scratchpad_set_bool(const std::string
&varname
, bool value
)
557 scratchpad
[varname
] = value
? "true" : "false";
560 void RTLIL::Design::scratchpad_set_string(const std::string
&varname
, std::string value
)
562 scratchpad
[varname
] = std::move(value
);
565 int RTLIL::Design::scratchpad_get_int(const std::string
&varname
, int default_value
) const
567 auto it
= scratchpad
.find(varname
);
568 if (it
== scratchpad
.end())
569 return default_value
;
571 const std::string
&str
= it
->second
;
573 if (str
== "0" || str
== "false")
576 if (str
== "1" || str
== "true")
579 char *endptr
= nullptr;
580 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
581 return *endptr
? default_value
: parsed_value
;
584 bool RTLIL::Design::scratchpad_get_bool(const std::string
&varname
, bool default_value
) const
586 auto it
= scratchpad
.find(varname
);
587 if (it
== scratchpad
.end())
588 return default_value
;
590 const std::string
&str
= it
->second
;
592 if (str
== "0" || str
== "false")
595 if (str
== "1" || str
== "true")
598 return default_value
;
601 std::string
RTLIL::Design::scratchpad_get_string(const std::string
&varname
, const std::string
&default_value
) const
603 auto it
= scratchpad
.find(varname
);
604 if (it
== scratchpad
.end())
605 return default_value
;
610 void RTLIL::Design::remove(RTLIL::Module
*module
)
612 for (auto mon
: monitors
)
613 mon
->notify_module_del(module
);
616 log("#X# Remove Module: %s\n", log_id(module
));
617 log_backtrace("-X- ", yosys_xtrace
-1);
620 log_assert(modules_
.at(module
->name
) == module
);
621 log_assert(refcount_modules_
== 0);
622 modules_
.erase(module
->name
);
626 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
628 modules_
.erase(module
->name
);
629 module
->name
= new_name
;
633 void RTLIL::Design::sort()
636 modules_
.sort(sort_by_id_str());
637 for (auto &it
: modules_
)
641 void RTLIL::Design::check()
644 for (auto &it
: modules_
) {
645 log_assert(this == it
.second
->design
);
646 log_assert(it
.first
== it
.second
->name
);
647 log_assert(!it
.first
.empty());
653 void RTLIL::Design::optimize()
655 for (auto &it
: modules_
)
656 it
.second
->optimize();
657 for (auto &it
: selection_stack
)
659 for (auto &it
: selection_vars
)
660 it
.second
.optimize(this);
663 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
665 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
667 if (selection_stack
.size() == 0)
669 return selection_stack
.back().selected_module(mod_name
);
672 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
674 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
676 if (selection_stack
.size() == 0)
678 return selection_stack
.back().selected_whole_module(mod_name
);
681 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
683 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
685 if (selection_stack
.size() == 0)
687 return selection_stack
.back().selected_member(mod_name
, memb_name
);
690 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
692 return selected_module(mod
->name
);
695 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
697 return selected_whole_module(mod
->name
);
700 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
702 std::vector
<RTLIL::Module
*> result
;
703 result
.reserve(modules_
.size());
704 for (auto &it
: modules_
)
705 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
706 result
.push_back(it
.second
);
710 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
712 std::vector
<RTLIL::Module
*> result
;
713 result
.reserve(modules_
.size());
714 for (auto &it
: modules_
)
715 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
716 result
.push_back(it
.second
);
720 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
722 std::vector
<RTLIL::Module
*> result
;
723 result
.reserve(modules_
.size());
724 for (auto &it
: modules_
)
725 if (it
.second
->get_blackbox_attribute())
727 else if (selected_whole_module(it
.first
))
728 result
.push_back(it
.second
);
729 else if (selected_module(it
.first
))
730 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
734 RTLIL::Module::Module()
736 static unsigned int hashidx_count
= 123456789;
737 hashidx_count
= mkhash_xorshift(hashidx_count
);
738 hashidx_
= hashidx_count
;
745 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
749 RTLIL::Module::~Module()
751 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
753 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
755 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
757 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
760 RTLIL::Module::get_all_modules()->erase(hashidx_
);
765 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
766 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
772 void RTLIL::Module::makeblackbox()
774 pool
<RTLIL::Wire
*> delwires
;
776 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
777 if (!it
->second
->port_input
&& !it
->second
->port_output
)
778 delwires
.insert(it
->second
);
780 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
784 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
788 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
792 connections_
.clear();
795 set_bool_attribute(ID::blackbox
);
798 void RTLIL::Module::reprocess_module(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Module
*> &)
800 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
803 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, bool mayfail
)
806 return RTLIL::IdString();
807 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
811 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, const dict
<RTLIL::IdString
, RTLIL::Module
*> &, const dict
<RTLIL::IdString
, RTLIL::IdString
> &, bool mayfail
)
814 return RTLIL::IdString();
815 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
818 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
820 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
825 struct InternalCellChecker
827 RTLIL::Module
*module
;
829 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
831 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
833 void error(int linenr
)
835 std::stringstream buf
;
836 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
838 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
839 module
? module
->name
.c_str() : "", module
? "." : "",
840 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
843 int param(RTLIL::IdString name
)
845 auto it
= cell
->parameters
.find(name
);
846 if (it
== cell
->parameters
.end())
848 expected_params
.insert(name
);
849 return it
->second
.as_int();
852 int param_bool(RTLIL::IdString name
)
855 if (GetSize(cell
->parameters
.at(name
)) > 32)
857 if (v
!= 0 && v
!= 1)
862 int param_bool(RTLIL::IdString name
, bool expected
)
864 int v
= param_bool(name
);
870 void param_bits(RTLIL::IdString name
, int width
)
873 if (GetSize(cell
->parameters
.at(name
).bits
) != width
)
877 void port(RTLIL::IdString name
, int width
)
879 auto it
= cell
->connections_
.find(name
);
880 if (it
== cell
->connections_
.end())
882 if (GetSize(it
->second
) != width
)
884 expected_ports
.insert(name
);
887 void check_expected(bool check_matched_sign
= false)
889 for (auto ¶
: cell
->parameters
)
890 if (expected_params
.count(para
.first
) == 0)
892 for (auto &conn
: cell
->connections())
893 if (expected_ports
.count(conn
.first
) == 0)
896 if (check_matched_sign
) {
897 log_assert(expected_params
.count(ID::A_SIGNED
) != 0 && expected_params
.count(ID::B_SIGNED
) != 0);
898 bool a_is_signed
= cell
->parameters
.at(ID::A_SIGNED
).as_bool();
899 bool b_is_signed
= cell
->parameters
.at(ID::B_SIGNED
).as_bool();
900 if (a_is_signed
!= b_is_signed
)
907 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
908 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
911 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
912 param_bool(ID::A_SIGNED
);
913 port(ID::A
, param(ID::A_WIDTH
));
914 port(ID::Y
, param(ID::Y_WIDTH
));
919 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
920 param_bool(ID::A_SIGNED
);
921 param_bool(ID::B_SIGNED
);
922 port(ID::A
, param(ID::A_WIDTH
));
923 port(ID::B
, param(ID::B_WIDTH
));
924 port(ID::Y
, param(ID::Y_WIDTH
));
925 check_expected(true);
929 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
930 param_bool(ID::A_SIGNED
);
931 port(ID::A
, param(ID::A_WIDTH
));
932 port(ID::Y
, param(ID::Y_WIDTH
));
937 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
938 param_bool(ID::A_SIGNED
);
939 param_bool(ID::B_SIGNED
, /*expected=*/false);
940 port(ID::A
, param(ID::A_WIDTH
));
941 port(ID::B
, param(ID::B_WIDTH
));
942 port(ID::Y
, param(ID::Y_WIDTH
));
943 check_expected(/*check_matched_sign=*/false);
947 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
948 param_bool(ID::A_SIGNED
);
949 param_bool(ID::B_SIGNED
);
950 port(ID::A
, param(ID::A_WIDTH
));
951 port(ID::B
, param(ID::B_WIDTH
));
952 port(ID::Y
, param(ID::Y_WIDTH
));
953 check_expected(/*check_matched_sign=*/false);
957 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
958 param_bool(ID::A_SIGNED
);
959 param_bool(ID::B_SIGNED
);
960 port(ID::A
, param(ID::A_WIDTH
));
961 port(ID::B
, param(ID::B_WIDTH
));
962 port(ID::Y
, param(ID::Y_WIDTH
));
963 check_expected(true);
967 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($divfloor
), ID($modfloor
), ID($pow
))) {
968 param_bool(ID::A_SIGNED
);
969 param_bool(ID::B_SIGNED
);
970 port(ID::A
, param(ID::A_WIDTH
));
971 port(ID::B
, param(ID::B_WIDTH
));
972 port(ID::Y
, param(ID::Y_WIDTH
));
973 check_expected(cell
->type
!= ID($pow
));
977 if (cell
->type
== ID($fa
)) {
978 port(ID::A
, param(ID::WIDTH
));
979 port(ID::B
, param(ID::WIDTH
));
980 port(ID::C
, param(ID::WIDTH
));
981 port(ID::X
, param(ID::WIDTH
));
982 port(ID::Y
, param(ID::WIDTH
));
987 if (cell
->type
== ID($lcu
)) {
988 port(ID::P
, param(ID::WIDTH
));
989 port(ID::G
, param(ID::WIDTH
));
991 port(ID::CO
, param(ID::WIDTH
));
996 if (cell
->type
== ID($alu
)) {
997 param_bool(ID::A_SIGNED
);
998 param_bool(ID::B_SIGNED
);
999 port(ID::A
, param(ID::A_WIDTH
));
1000 port(ID::B
, param(ID::B_WIDTH
));
1003 port(ID::X
, param(ID::Y_WIDTH
));
1004 port(ID::Y
, param(ID::Y_WIDTH
));
1005 port(ID::CO
, param(ID::Y_WIDTH
));
1006 check_expected(true);
1010 if (cell
->type
== ID($macc
)) {
1012 param(ID::CONFIG_WIDTH
);
1013 port(ID::A
, param(ID::A_WIDTH
));
1014 port(ID::B
, param(ID::B_WIDTH
));
1015 port(ID::Y
, param(ID::Y_WIDTH
));
1017 Macc().from_cell(cell
);
1021 if (cell
->type
== ID($logic_not
)) {
1022 param_bool(ID::A_SIGNED
);
1023 port(ID::A
, param(ID::A_WIDTH
));
1024 port(ID::Y
, param(ID::Y_WIDTH
));
1029 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
1030 param_bool(ID::A_SIGNED
);
1031 param_bool(ID::B_SIGNED
);
1032 port(ID::A
, param(ID::A_WIDTH
));
1033 port(ID::B
, param(ID::B_WIDTH
));
1034 port(ID::Y
, param(ID::Y_WIDTH
));
1035 check_expected(/*check_matched_sign=*/false);
1039 if (cell
->type
== ID($slice
)) {
1041 port(ID::A
, param(ID::A_WIDTH
));
1042 port(ID::Y
, param(ID::Y_WIDTH
));
1043 if (param(ID::OFFSET
) + param(ID::Y_WIDTH
) > param(ID::A_WIDTH
))
1049 if (cell
->type
== ID($concat
)) {
1050 port(ID::A
, param(ID::A_WIDTH
));
1051 port(ID::B
, param(ID::B_WIDTH
));
1052 port(ID::Y
, param(ID::A_WIDTH
) + param(ID::B_WIDTH
));
1057 if (cell
->type
== ID($mux
)) {
1058 port(ID::A
, param(ID::WIDTH
));
1059 port(ID::B
, param(ID::WIDTH
));
1061 port(ID::Y
, param(ID::WIDTH
));
1066 if (cell
->type
== ID($pmux
)) {
1067 port(ID::A
, param(ID::WIDTH
));
1068 port(ID::B
, param(ID::WIDTH
) * param(ID::S_WIDTH
));
1069 port(ID::S
, param(ID::S_WIDTH
));
1070 port(ID::Y
, param(ID::WIDTH
));
1075 if (cell
->type
== ID($lut
)) {
1077 port(ID::A
, param(ID::WIDTH
));
1083 if (cell
->type
== ID($sop
)) {
1086 port(ID::A
, param(ID::WIDTH
));
1092 if (cell
->type
== ID($sr
)) {
1093 param_bool(ID::SET_POLARITY
);
1094 param_bool(ID::CLR_POLARITY
);
1095 port(ID::SET
, param(ID::WIDTH
));
1096 port(ID::CLR
, param(ID::WIDTH
));
1097 port(ID::Q
, param(ID::WIDTH
));
1102 if (cell
->type
== ID($ff
)) {
1103 port(ID::D
, param(ID::WIDTH
));
1104 port(ID::Q
, param(ID::WIDTH
));
1109 if (cell
->type
== ID($dff
)) {
1110 param_bool(ID::CLK_POLARITY
);
1112 port(ID::D
, param(ID::WIDTH
));
1113 port(ID::Q
, param(ID::WIDTH
));
1118 if (cell
->type
== ID($dffe
)) {
1119 param_bool(ID::CLK_POLARITY
);
1120 param_bool(ID::EN_POLARITY
);
1123 port(ID::D
, param(ID::WIDTH
));
1124 port(ID::Q
, param(ID::WIDTH
));
1129 if (cell
->type
== ID($dffsr
)) {
1130 param_bool(ID::CLK_POLARITY
);
1131 param_bool(ID::SET_POLARITY
);
1132 param_bool(ID::CLR_POLARITY
);
1134 port(ID::SET
, param(ID::WIDTH
));
1135 port(ID::CLR
, param(ID::WIDTH
));
1136 port(ID::D
, param(ID::WIDTH
));
1137 port(ID::Q
, param(ID::WIDTH
));
1142 if (cell
->type
== ID($adff
)) {
1143 param_bool(ID::CLK_POLARITY
);
1144 param_bool(ID::ARST_POLARITY
);
1145 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1148 port(ID::D
, param(ID::WIDTH
));
1149 port(ID::Q
, param(ID::WIDTH
));
1154 if (cell
->type
== ID($dlatch
)) {
1155 param_bool(ID::EN_POLARITY
);
1157 port(ID::D
, param(ID::WIDTH
));
1158 port(ID::Q
, param(ID::WIDTH
));
1163 if (cell
->type
== ID($dlatchsr
)) {
1164 param_bool(ID::EN_POLARITY
);
1165 param_bool(ID::SET_POLARITY
);
1166 param_bool(ID::CLR_POLARITY
);
1168 port(ID::SET
, param(ID::WIDTH
));
1169 port(ID::CLR
, param(ID::WIDTH
));
1170 port(ID::D
, param(ID::WIDTH
));
1171 port(ID::Q
, param(ID::WIDTH
));
1176 if (cell
->type
== ID($fsm
)) {
1178 param_bool(ID::CLK_POLARITY
);
1179 param_bool(ID::ARST_POLARITY
);
1180 param(ID::STATE_BITS
);
1181 param(ID::STATE_NUM
);
1182 param(ID::STATE_NUM_LOG2
);
1183 param(ID::STATE_RST
);
1184 param_bits(ID::STATE_TABLE
, param(ID::STATE_BITS
) * param(ID::STATE_NUM
));
1185 param(ID::TRANS_NUM
);
1186 param_bits(ID::TRANS_TABLE
, param(ID::TRANS_NUM
) * (2*param(ID::STATE_NUM_LOG2
) + param(ID::CTRL_IN_WIDTH
) + param(ID::CTRL_OUT_WIDTH
)));
1189 port(ID::CTRL_IN
, param(ID::CTRL_IN_WIDTH
));
1190 port(ID::CTRL_OUT
, param(ID::CTRL_OUT_WIDTH
));
1195 if (cell
->type
== ID($memrd
)) {
1197 param_bool(ID::CLK_ENABLE
);
1198 param_bool(ID::CLK_POLARITY
);
1199 param_bool(ID::TRANSPARENT
);
1202 port(ID::ADDR
, param(ID::ABITS
));
1203 port(ID::DATA
, param(ID::WIDTH
));
1208 if (cell
->type
== ID($memwr
)) {
1210 param_bool(ID::CLK_ENABLE
);
1211 param_bool(ID::CLK_POLARITY
);
1212 param(ID::PRIORITY
);
1214 port(ID::EN
, param(ID::WIDTH
));
1215 port(ID::ADDR
, param(ID::ABITS
));
1216 port(ID::DATA
, param(ID::WIDTH
));
1221 if (cell
->type
== ID($meminit
)) {
1223 param(ID::PRIORITY
);
1224 port(ID::ADDR
, param(ID::ABITS
));
1225 port(ID::DATA
, param(ID::WIDTH
) * param(ID::WORDS
));
1230 if (cell
->type
== ID($mem
)) {
1235 param_bits(ID::RD_CLK_ENABLE
, max(1, param(ID::RD_PORTS
)));
1236 param_bits(ID::RD_CLK_POLARITY
, max(1, param(ID::RD_PORTS
)));
1237 param_bits(ID::RD_TRANSPARENT
, max(1, param(ID::RD_PORTS
)));
1238 param_bits(ID::WR_CLK_ENABLE
, max(1, param(ID::WR_PORTS
)));
1239 param_bits(ID::WR_CLK_POLARITY
, max(1, param(ID::WR_PORTS
)));
1240 port(ID::RD_CLK
, param(ID::RD_PORTS
));
1241 port(ID::RD_EN
, param(ID::RD_PORTS
));
1242 port(ID::RD_ADDR
, param(ID::RD_PORTS
) * param(ID::ABITS
));
1243 port(ID::RD_DATA
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1244 port(ID::WR_CLK
, param(ID::WR_PORTS
));
1245 port(ID::WR_EN
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1246 port(ID::WR_ADDR
, param(ID::WR_PORTS
) * param(ID::ABITS
));
1247 port(ID::WR_DATA
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1252 if (cell
->type
== ID($tribuf
)) {
1253 port(ID::A
, param(ID::WIDTH
));
1254 port(ID::Y
, param(ID::WIDTH
));
1260 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1267 if (cell
->type
== ID($initstate
)) {
1273 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1274 port(ID::Y
, param(ID::WIDTH
));
1279 if (cell
->type
== ID($equiv
)) {
1287 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1288 param_bool(ID::FULL
);
1289 param_bool(ID::SRC_DST_PEN
);
1290 param_bool(ID::SRC_DST_POL
);
1291 param(ID::T_RISE_MIN
);
1292 param(ID::T_RISE_TYP
);
1293 param(ID::T_RISE_MAX
);
1294 param(ID::T_FALL_MIN
);
1295 param(ID::T_FALL_TYP
);
1296 param(ID::T_FALL_MAX
);
1298 port(ID::SRC
, param(ID::SRC_WIDTH
));
1299 port(ID::DST
, param(ID::DST_WIDTH
));
1300 if (cell
->type
== ID($specify3
)) {
1301 param_bool(ID::EDGE_EN
);
1302 param_bool(ID::EDGE_POL
);
1303 param_bool(ID::DAT_DST_PEN
);
1304 param_bool(ID::DAT_DST_POL
);
1305 port(ID::DAT
, param(ID::DST_WIDTH
));
1311 if (cell
->type
== ID($specrule
)) {
1313 param_bool(ID::SRC_PEN
);
1314 param_bool(ID::SRC_POL
);
1315 param_bool(ID::DST_PEN
);
1316 param_bool(ID::DST_POL
);
1317 param(ID::T_LIMIT_MIN
);
1318 param(ID::T_LIMIT_TYP
);
1319 param(ID::T_LIMIT_MAX
);
1320 param(ID::T_LIMIT2_MIN
);
1321 param(ID::T_LIMIT2_TYP
);
1322 param(ID::T_LIMIT2_MAX
);
1323 port(ID::SRC_EN
, 1);
1324 port(ID::DST_EN
, 1);
1325 port(ID::SRC
, param(ID::SRC_WIDTH
));
1326 port(ID::DST
, param(ID::DST_WIDTH
));
1331 if (cell
->type
== ID($_BUF_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1332 if (cell
->type
== ID($_NOT_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1333 if (cell
->type
== ID($_AND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1334 if (cell
->type
== ID($_NAND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1335 if (cell
->type
== ID($_OR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1336 if (cell
->type
== ID($_NOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1337 if (cell
->type
== ID($_XOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1338 if (cell
->type
== ID($_XNOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1339 if (cell
->type
== ID($_ANDNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1340 if (cell
->type
== ID($_ORNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1341 if (cell
->type
== ID($_MUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1342 if (cell
->type
== ID($_NMUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1343 if (cell
->type
== ID($_AOI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1344 if (cell
->type
== ID($_OAI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1345 if (cell
->type
== ID($_AOI4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::Y
,1); check_expected(); return; }
1346 if (cell
->type
== ID($_OAI4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::Y
,1); check_expected(); return; }
1348 if (cell
->type
== ID($_TBUF_
)) { port(ID::A
,1); port(ID::Y
,1); port(ID::E
,1); check_expected(); return; }
1350 if (cell
->type
== ID($_MUX4_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::S
,1); port(ID::T
,1); port(ID::Y
,1); check_expected(); return; }
1351 if (cell
->type
== ID($_MUX8_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::E
,1); port(ID::F
,1); port(ID::G
,1); port(ID::H
,1); port(ID::S
,1); port(ID::T
,1); port(ID::U
,1); port(ID::Y
,1); check_expected(); return; }
1352 if (cell
->type
== ID($_MUX16_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::D
,1); port(ID::E
,1); port(ID::F
,1); port(ID::G
,1); port(ID::H
,1); port(ID::I
,1); port(ID::J
,1); port(ID::K
,1); port(ID::L
,1); port(ID::M
,1); port(ID::N
,1); port(ID::O
,1); port(ID::P
,1); port(ID::S
,1); port(ID::T
,1); port(ID::U
,1); port(ID::V
,1); port(ID::Y
,1); check_expected(); return; }
1354 if (cell
->type
== ID($_SR_NN_
)) { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1355 if (cell
->type
== ID($_SR_NP_
)) { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1356 if (cell
->type
== ID($_SR_PN_
)) { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1357 if (cell
->type
== ID($_SR_PP_
)) { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1359 if (cell
->type
== ID($_FF_
)) { port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1360 if (cell
->type
== ID($_DFF_N_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); check_expected(); return; }
1361 if (cell
->type
== ID($_DFF_P_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); check_expected(); return; }
1363 if (cell
->type
== ID($_DFFE_NN_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1364 if (cell
->type
== ID($_DFFE_NP_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1365 if (cell
->type
== ID($_DFFE_PN_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1366 if (cell
->type
== ID($_DFFE_PP_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1368 if (cell
->type
== ID($_DFF_NN0_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1369 if (cell
->type
== ID($_DFF_NN1_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1370 if (cell
->type
== ID($_DFF_NP0_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1371 if (cell
->type
== ID($_DFF_NP1_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1372 if (cell
->type
== ID($_DFF_PN0_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1373 if (cell
->type
== ID($_DFF_PN1_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1374 if (cell
->type
== ID($_DFF_PP0_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1375 if (cell
->type
== ID($_DFF_PP1_
)) { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1377 if (cell
->type
== ID($_DFFSR_NNN_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1378 if (cell
->type
== ID($_DFFSR_NNP_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1379 if (cell
->type
== ID($_DFFSR_NPN_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1380 if (cell
->type
== ID($_DFFSR_NPP_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1381 if (cell
->type
== ID($_DFFSR_PNN_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1382 if (cell
->type
== ID($_DFFSR_PNP_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1383 if (cell
->type
== ID($_DFFSR_PPN_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1384 if (cell
->type
== ID($_DFFSR_PPP_
)) { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1386 if (cell
->type
== ID($_DLATCH_N_
)) { port(ID::E
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1387 if (cell
->type
== ID($_DLATCH_P_
)) { port(ID::E
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1389 if (cell
->type
== ID($_DLATCHSR_NNN_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1390 if (cell
->type
== ID($_DLATCHSR_NNP_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1391 if (cell
->type
== ID($_DLATCHSR_NPN_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1392 if (cell
->type
== ID($_DLATCHSR_NPP_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1393 if (cell
->type
== ID($_DLATCHSR_PNN_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1394 if (cell
->type
== ID($_DLATCHSR_PNP_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1395 if (cell
->type
== ID($_DLATCHSR_PPN_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1396 if (cell
->type
== ID($_DLATCHSR_PPP_
)) { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1404 void RTLIL::Module::sort()
1406 wires_
.sort(sort_by_id_str());
1407 cells_
.sort(sort_by_id_str());
1408 parameter_default_values
.sort(sort_by_id_str());
1409 memories
.sort(sort_by_id_str());
1410 processes
.sort(sort_by_id_str());
1411 for (auto &it
: cells_
)
1413 for (auto &it
: wires_
)
1414 it
.second
->attributes
.sort(sort_by_id_str());
1415 for (auto &it
: memories
)
1416 it
.second
->attributes
.sort(sort_by_id_str());
1419 void RTLIL::Module::check()
1422 std::vector
<bool> ports_declared
;
1423 for (auto &it
: wires_
) {
1424 log_assert(this == it
.second
->module
);
1425 log_assert(it
.first
== it
.second
->name
);
1426 log_assert(!it
.first
.empty());
1427 log_assert(it
.second
->width
>= 0);
1428 log_assert(it
.second
->port_id
>= 0);
1429 for (auto &it2
: it
.second
->attributes
)
1430 log_assert(!it2
.first
.empty());
1431 if (it
.second
->port_id
) {
1432 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1433 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1434 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1435 if (GetSize(ports_declared
) < it
.second
->port_id
)
1436 ports_declared
.resize(it
.second
->port_id
);
1437 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1438 ports_declared
[it
.second
->port_id
-1] = true;
1440 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1442 for (auto port_declared
: ports_declared
)
1443 log_assert(port_declared
== true);
1444 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1446 for (auto &it
: memories
) {
1447 log_assert(it
.first
== it
.second
->name
);
1448 log_assert(!it
.first
.empty());
1449 log_assert(it
.second
->width
>= 0);
1450 log_assert(it
.second
->size
>= 0);
1451 for (auto &it2
: it
.second
->attributes
)
1452 log_assert(!it2
.first
.empty());
1455 for (auto &it
: cells_
) {
1456 log_assert(this == it
.second
->module
);
1457 log_assert(it
.first
== it
.second
->name
);
1458 log_assert(!it
.first
.empty());
1459 log_assert(!it
.second
->type
.empty());
1460 for (auto &it2
: it
.second
->connections()) {
1461 log_assert(!it2
.first
.empty());
1464 for (auto &it2
: it
.second
->attributes
)
1465 log_assert(!it2
.first
.empty());
1466 for (auto &it2
: it
.second
->parameters
)
1467 log_assert(!it2
.first
.empty());
1468 InternalCellChecker
checker(this, it
.second
);
1472 for (auto &it
: processes
) {
1473 log_assert(it
.first
== it
.second
->name
);
1474 log_assert(!it
.first
.empty());
1475 log_assert(it
.second
->root_case
.compare
.empty());
1476 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1477 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1478 for (auto &switch_it
: all_cases
[i
]->switches
) {
1479 for (auto &case_it
: switch_it
->cases
) {
1480 for (auto &compare_it
: case_it
->compare
) {
1481 log_assert(switch_it
->signal
.size() == compare_it
.size());
1483 all_cases
.push_back(case_it
);
1487 for (auto &sync_it
: it
.second
->syncs
) {
1488 switch (sync_it
->type
) {
1494 log_assert(!sync_it
->signal
.empty());
1499 log_assert(sync_it
->signal
.empty());
1505 for (auto &it
: connections_
) {
1506 log_assert(it
.first
.size() == it
.second
.size());
1507 log_assert(!it
.first
.has_const());
1512 for (auto &it
: attributes
)
1513 log_assert(!it
.first
.empty());
1517 void RTLIL::Module::optimize()
1521 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1523 log_assert(new_mod
->refcount_wires_
== 0);
1524 log_assert(new_mod
->refcount_cells_
== 0);
1526 new_mod
->avail_parameters
= avail_parameters
;
1527 new_mod
->parameter_default_values
= parameter_default_values
;
1529 for (auto &conn
: connections_
)
1530 new_mod
->connect(conn
);
1532 for (auto &attr
: attributes
)
1533 new_mod
->attributes
[attr
.first
] = attr
.second
;
1535 for (auto &it
: wires_
)
1536 new_mod
->addWire(it
.first
, it
.second
);
1538 for (auto &it
: memories
)
1539 new_mod
->addMemory(it
.first
, it
.second
);
1541 for (auto &it
: cells_
)
1542 new_mod
->addCell(it
.first
, it
.second
);
1544 for (auto &it
: processes
)
1545 new_mod
->addProcess(it
.first
, it
.second
);
1547 struct RewriteSigSpecWorker
1550 void operator()(RTLIL::SigSpec
&sig
)
1553 for (auto &c
: sig
.chunks_
)
1555 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1559 RewriteSigSpecWorker rewriteSigSpecWorker
;
1560 rewriteSigSpecWorker
.mod
= new_mod
;
1561 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1562 new_mod
->fixup_ports();
1565 RTLIL::Module
*RTLIL::Module::clone() const
1567 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1568 new_mod
->name
= name
;
1573 bool RTLIL::Module::has_memories() const
1575 return !memories
.empty();
1578 bool RTLIL::Module::has_processes() const
1580 return !processes
.empty();
1583 bool RTLIL::Module::has_memories_warn() const
1585 if (!memories
.empty())
1586 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1587 return !memories
.empty();
1590 bool RTLIL::Module::has_processes_warn() const
1592 if (!processes
.empty())
1593 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1594 return !processes
.empty();
1597 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1599 std::vector
<RTLIL::Wire
*> result
;
1600 result
.reserve(wires_
.size());
1601 for (auto &it
: wires_
)
1602 if (design
->selected(this, it
.second
))
1603 result
.push_back(it
.second
);
1607 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1609 std::vector
<RTLIL::Cell
*> result
;
1610 result
.reserve(cells_
.size());
1611 for (auto &it
: cells_
)
1612 if (design
->selected(this, it
.second
))
1613 result
.push_back(it
.second
);
1617 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1619 log_assert(!wire
->name
.empty());
1620 log_assert(count_id(wire
->name
) == 0);
1621 log_assert(refcount_wires_
== 0);
1622 wires_
[wire
->name
] = wire
;
1623 wire
->module
= this;
1626 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1628 log_assert(!cell
->name
.empty());
1629 log_assert(count_id(cell
->name
) == 0);
1630 log_assert(refcount_cells_
== 0);
1631 cells_
[cell
->name
] = cell
;
1632 cell
->module
= this;
1635 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1637 log_assert(refcount_wires_
== 0);
1639 struct DeleteWireWorker
1641 RTLIL::Module
*module
;
1642 const pool
<RTLIL::Wire
*> *wires_p
;
1644 void operator()(RTLIL::SigSpec
&sig
) {
1646 for (auto &c
: sig
.chunks_
)
1647 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1648 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1653 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1654 log_assert(GetSize(lhs
) == GetSize(rhs
));
1657 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1658 RTLIL::SigBit
&lhs_bit
= lhs
.bits_
[i
];
1659 RTLIL::SigBit
&rhs_bit
= rhs
.bits_
[i
];
1660 if ((lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
)) || (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))) {
1661 lhs_bit
= State::Sx
;
1662 rhs_bit
= State::Sx
;
1668 DeleteWireWorker delete_wire_worker
;
1669 delete_wire_worker
.module
= this;
1670 delete_wire_worker
.wires_p
= &wires
;
1671 rewrite_sigspecs2(delete_wire_worker
);
1673 for (auto &it
: wires
) {
1674 log_assert(wires_
.count(it
->name
) != 0);
1675 wires_
.erase(it
->name
);
1680 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1682 while (!cell
->connections_
.empty())
1683 cell
->unsetPort(cell
->connections_
.begin()->first
);
1685 log_assert(cells_
.count(cell
->name
) != 0);
1686 log_assert(refcount_cells_
== 0);
1687 cells_
.erase(cell
->name
);
1691 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1693 log_assert(wires_
[wire
->name
] == wire
);
1694 log_assert(refcount_wires_
== 0);
1695 wires_
.erase(wire
->name
);
1696 wire
->name
= new_name
;
1700 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1702 log_assert(cells_
[cell
->name
] == cell
);
1703 log_assert(refcount_wires_
== 0);
1704 cells_
.erase(cell
->name
);
1705 cell
->name
= new_name
;
1709 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1711 log_assert(count_id(old_name
) != 0);
1712 if (wires_
.count(old_name
))
1713 rename(wires_
.at(old_name
), new_name
);
1714 else if (cells_
.count(old_name
))
1715 rename(cells_
.at(old_name
), new_name
);
1720 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1722 log_assert(wires_
[w1
->name
] == w1
);
1723 log_assert(wires_
[w2
->name
] == w2
);
1724 log_assert(refcount_wires_
== 0);
1726 wires_
.erase(w1
->name
);
1727 wires_
.erase(w2
->name
);
1729 std::swap(w1
->name
, w2
->name
);
1731 wires_
[w1
->name
] = w1
;
1732 wires_
[w2
->name
] = w2
;
1735 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1737 log_assert(cells_
[c1
->name
] == c1
);
1738 log_assert(cells_
[c2
->name
] == c2
);
1739 log_assert(refcount_cells_
== 0);
1741 cells_
.erase(c1
->name
);
1742 cells_
.erase(c2
->name
);
1744 std::swap(c1
->name
, c2
->name
);
1746 cells_
[c1
->name
] = c1
;
1747 cells_
[c2
->name
] = c2
;
1750 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1753 return uniquify(name
, index
);
1756 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1759 if (count_id(name
) == 0)
1765 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1766 if (count_id(new_name
) == 0)
1772 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1774 if (a
->port_id
&& !b
->port_id
)
1776 if (!a
->port_id
&& b
->port_id
)
1779 if (a
->port_id
== b
->port_id
)
1780 return a
->name
< b
->name
;
1781 return a
->port_id
< b
->port_id
;
1784 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1786 for (auto mon
: monitors
)
1787 mon
->notify_connect(this, conn
);
1790 for (auto mon
: design
->monitors
)
1791 mon
->notify_connect(this, conn
);
1793 // ignore all attempts to assign constants to other constants
1794 if (conn
.first
.has_const()) {
1795 RTLIL::SigSig new_conn
;
1796 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1797 if (conn
.first
[i
].wire
) {
1798 new_conn
.first
.append(conn
.first
[i
]);
1799 new_conn
.second
.append(conn
.second
[i
]);
1801 if (GetSize(new_conn
.first
))
1807 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
));
1808 log_backtrace("-X- ", yosys_xtrace
-1);
1811 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1812 connections_
.push_back(conn
);
1815 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1817 connect(RTLIL::SigSig(lhs
, rhs
));
1820 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1822 for (auto mon
: monitors
)
1823 mon
->notify_connect(this, new_conn
);
1826 for (auto mon
: design
->monitors
)
1827 mon
->notify_connect(this, new_conn
);
1830 log("#X# New connections vector in %s:\n", log_id(this));
1831 for (auto &conn
: new_conn
)
1832 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
1833 log_backtrace("-X- ", yosys_xtrace
-1);
1836 connections_
= new_conn
;
1839 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1841 return connections_
;
1844 void RTLIL::Module::fixup_ports()
1846 std::vector
<RTLIL::Wire
*> all_ports
;
1848 for (auto &w
: wires_
)
1849 if (w
.second
->port_input
|| w
.second
->port_output
)
1850 all_ports
.push_back(w
.second
);
1852 w
.second
->port_id
= 0;
1854 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1857 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1858 ports
.push_back(all_ports
[i
]->name
);
1859 all_ports
[i
]->port_id
= i
+1;
1863 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1865 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1867 wire
->width
= width
;
1872 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1874 RTLIL::Wire
*wire
= addWire(name
);
1875 wire
->width
= other
->width
;
1876 wire
->start_offset
= other
->start_offset
;
1877 wire
->port_id
= other
->port_id
;
1878 wire
->port_input
= other
->port_input
;
1879 wire
->port_output
= other
->port_output
;
1880 wire
->upto
= other
->upto
;
1881 wire
->is_signed
= other
->is_signed
;
1882 wire
->attributes
= other
->attributes
;
1886 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1888 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1895 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1897 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1898 cell
->connections_
= other
->connections_
;
1899 cell
->parameters
= other
->parameters
;
1900 cell
->attributes
= other
->attributes
;
1904 RTLIL::Memory
*RTLIL::Module::addMemory(RTLIL::IdString name
, const RTLIL::Memory
*other
)
1906 RTLIL::Memory
*mem
= new RTLIL::Memory
;
1908 mem
->width
= other
->width
;
1909 mem
->start_offset
= other
->start_offset
;
1910 mem
->size
= other
->size
;
1911 mem
->attributes
= other
->attributes
;
1912 memories
[mem
->name
] = mem
;
1916 RTLIL::Process
*RTLIL::Module::addProcess(RTLIL::IdString name
, const RTLIL::Process
*other
)
1918 RTLIL::Process
*proc
= other
->clone();
1920 processes
[name
] = proc
;
1924 #define DEF_METHOD(_func, _y_size, _type) \
1925 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
1926 RTLIL::Cell *cell = addCell(name, _type); \
1927 cell->parameters[ID::A_SIGNED] = is_signed; \
1928 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
1929 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
1930 cell->setPort(ID::A, sig_a); \
1931 cell->setPort(ID::Y, sig_y); \
1932 cell->set_src_attribute(src); \
1935 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed, const std::string &src) { \
1936 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1937 add ## _func(name, sig_a, sig_y, is_signed, src); \
1940 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
1941 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
1942 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
1943 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
1944 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
1945 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
1946 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
1947 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
1948 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
1951 #define DEF_METHOD(_func, _y_size, _type) \
1952 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
1953 RTLIL::Cell *cell = addCell(name, _type); \
1954 cell->parameters[ID::A_SIGNED] = is_signed; \
1955 cell->parameters[ID::B_SIGNED] = is_signed; \
1956 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
1957 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
1958 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
1959 cell->setPort(ID::A, sig_a); \
1960 cell->setPort(ID::B, sig_b); \
1961 cell->setPort(ID::Y, sig_y); \
1962 cell->set_src_attribute(src); \
1965 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed, const std::string &src) { \
1966 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1967 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1970 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
1971 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
1972 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
1973 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
1974 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
1975 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
1976 DEF_METHOD(Lt
, 1, ID($lt
))
1977 DEF_METHOD(Le
, 1, ID($le
))
1978 DEF_METHOD(Eq
, 1, ID($eq
))
1979 DEF_METHOD(Ne
, 1, ID($ne
))
1980 DEF_METHOD(Eqx
, 1, ID($eqx
))
1981 DEF_METHOD(Nex
, 1, ID($nex
))
1982 DEF_METHOD(Ge
, 1, ID($ge
))
1983 DEF_METHOD(Gt
, 1, ID($gt
))
1984 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
1985 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
1986 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
1987 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
1988 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
1989 DEF_METHOD(DivFloor
, max(sig_a
.size(), sig_b
.size()), ID($divfloor
))
1990 DEF_METHOD(ModFloor
, max(sig_a
.size(), sig_b
.size()), ID($modfloor
))
1991 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
1992 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
1995 #define DEF_METHOD(_func, _y_size, _type) \
1996 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed, const std::string &src) { \
1997 RTLIL::Cell *cell = addCell(name, _type); \
1998 cell->parameters[ID::A_SIGNED] = is_signed; \
1999 cell->parameters[ID::B_SIGNED] = false; \
2000 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2001 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2002 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2003 cell->setPort(ID::A, sig_a); \
2004 cell->setPort(ID::B, sig_b); \
2005 cell->setPort(ID::Y, sig_y); \
2006 cell->set_src_attribute(src); \
2009 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed, const std::string &src) { \
2010 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2011 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2014 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
2015 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
2016 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
2017 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
2020 #define DEF_METHOD(_func, _type, _pmux) \
2021 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const RTLIL::SigSpec &sig_y, const std::string &src) { \
2022 RTLIL::Cell *cell = addCell(name, _type); \
2023 cell->parameters[ID::WIDTH] = sig_a.size(); \
2024 if (_pmux) cell->parameters[ID::S_WIDTH] = sig_s.size(); \
2025 cell->setPort(ID::A, sig_a); \
2026 cell->setPort(ID::B, sig_b); \
2027 cell->setPort(ID::S, sig_s); \
2028 cell->setPort(ID::Y, sig_y); \
2029 cell->set_src_attribute(src); \
2032 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const std::string &src) { \
2033 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
2034 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
2037 DEF_METHOD(Mux
, ID($mux
), 0)
2038 DEF_METHOD(Pmux
, ID($pmux
), 1)
2041 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
2042 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2043 RTLIL::Cell *cell = addCell(name, _type); \
2044 cell->setPort("\\" #_P1, sig1); \
2045 cell->setPort("\\" #_P2, sig2); \
2046 cell->set_src_attribute(src); \
2049 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const std::string &src) { \
2050 RTLIL::SigBit sig2 = addWire(NEW_ID); \
2051 add ## _func(name, sig1, sig2, src); \
2054 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
2055 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2056 RTLIL::Cell *cell = addCell(name, _type); \
2057 cell->setPort("\\" #_P1, sig1); \
2058 cell->setPort("\\" #_P2, sig2); \
2059 cell->setPort("\\" #_P3, sig3); \
2060 cell->set_src_attribute(src); \
2063 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2064 RTLIL::SigBit sig3 = addWire(NEW_ID); \
2065 add ## _func(name, sig1, sig2, sig3, src); \
2068 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
2069 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const std::string &src) { \
2070 RTLIL::Cell *cell = addCell(name, _type); \
2071 cell->setPort("\\" #_P1, sig1); \
2072 cell->setPort("\\" #_P2, sig2); \
2073 cell->setPort("\\" #_P3, sig3); \
2074 cell->setPort("\\" #_P4, sig4); \
2075 cell->set_src_attribute(src); \
2078 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2079 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2080 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2083 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2084 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const RTLIL::SigBit &sig5, const std::string &src) { \
2085 RTLIL::Cell *cell = addCell(name, _type); \
2086 cell->setPort("\\" #_P1, sig1); \
2087 cell->setPort("\\" #_P2, sig2); \
2088 cell->setPort("\\" #_P3, sig3); \
2089 cell->setPort("\\" #_P4, sig4); \
2090 cell->setPort("\\" #_P5, sig5); \
2091 cell->set_src_attribute(src); \
2094 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const RTLIL::SigBit &sig4, const std::string &src) { \
2095 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2096 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2099 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2100 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2101 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2102 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2103 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2104 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2105 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2106 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2107 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2108 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2109 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2110 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2111 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2112 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2113 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2114 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2120 RTLIL::Cell
* RTLIL::Module::addPow(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, bool a_signed
, bool b_signed
, const std::string
&src
)
2122 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2123 cell
->parameters
[ID::A_SIGNED
] = a_signed
;
2124 cell
->parameters
[ID::B_SIGNED
] = b_signed
;
2125 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2126 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2127 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2128 cell
->setPort(ID::A
, sig_a
);
2129 cell
->setPort(ID::B
, sig_b
);
2130 cell
->setPort(ID::Y
, sig_y
);
2131 cell
->set_src_attribute(src
);
2135 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_y
, RTLIL::Const offset
, const std::string
&src
)
2137 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2138 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2139 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2140 cell
->parameters
[ID::OFFSET
] = offset
;
2141 cell
->setPort(ID::A
, sig_a
);
2142 cell
->setPort(ID::Y
, sig_y
);
2143 cell
->set_src_attribute(src
);
2147 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2149 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2150 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2151 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2152 cell
->setPort(ID::A
, sig_a
);
2153 cell
->setPort(ID::B
, sig_b
);
2154 cell
->setPort(ID::Y
, sig_y
);
2155 cell
->set_src_attribute(src
);
2159 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_y
, RTLIL::Const lut
, const std::string
&src
)
2161 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2162 cell
->parameters
[ID::LUT
] = lut
;
2163 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2164 cell
->setPort(ID::A
, sig_a
);
2165 cell
->setPort(ID::Y
, sig_y
);
2166 cell
->set_src_attribute(src
);
2170 RTLIL::Cell
* RTLIL::Module::addTribuf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2172 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2173 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2174 cell
->setPort(ID::A
, sig_a
);
2175 cell
->setPort(ID::EN
, sig_en
);
2176 cell
->setPort(ID::Y
, sig_y
);
2177 cell
->set_src_attribute(src
);
2181 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2183 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2184 cell
->setPort(ID::A
, sig_a
);
2185 cell
->setPort(ID::EN
, sig_en
);
2186 cell
->set_src_attribute(src
);
2190 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2192 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2193 cell
->setPort(ID::A
, sig_a
);
2194 cell
->setPort(ID::EN
, sig_en
);
2195 cell
->set_src_attribute(src
);
2199 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2201 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2202 cell
->setPort(ID::A
, sig_a
);
2203 cell
->setPort(ID::EN
, sig_en
);
2204 cell
->set_src_attribute(src
);
2208 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2210 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2211 cell
->setPort(ID::A
, sig_a
);
2212 cell
->setPort(ID::EN
, sig_en
);
2213 cell
->set_src_attribute(src
);
2217 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2219 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2220 cell
->setPort(ID::A
, sig_a
);
2221 cell
->setPort(ID::EN
, sig_en
);
2222 cell
->set_src_attribute(src
);
2226 RTLIL::Cell
* RTLIL::Module::addEquiv(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_b
, const RTLIL::SigSpec
&sig_y
, const std::string
&src
)
2228 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2229 cell
->setPort(ID::A
, sig_a
);
2230 cell
->setPort(ID::B
, sig_b
);
2231 cell
->setPort(ID::Y
, sig_y
);
2232 cell
->set_src_attribute(src
);
2236 RTLIL::Cell
* RTLIL::Module::addSr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
, const RTLIL::SigSpec
&sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2238 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2239 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2240 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2241 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2242 cell
->setPort(ID::SET
, sig_set
);
2243 cell
->setPort(ID::CLR
, sig_clr
);
2244 cell
->setPort(ID::Q
, sig_q
);
2245 cell
->set_src_attribute(src
);
2249 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2251 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2252 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2253 cell
->setPort(ID::D
, sig_d
);
2254 cell
->setPort(ID::Q
, sig_q
);
2255 cell
->set_src_attribute(src
);
2259 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, const std::string
&src
)
2261 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2262 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2263 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2264 cell
->setPort(ID::CLK
, sig_clk
);
2265 cell
->setPort(ID::D
, sig_d
);
2266 cell
->setPort(ID::Q
, sig_q
);
2267 cell
->set_src_attribute(src
);
2271 RTLIL::Cell
* RTLIL::Module::addDffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2273 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2274 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2275 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2276 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2277 cell
->setPort(ID::CLK
, sig_clk
);
2278 cell
->setPort(ID::EN
, sig_en
);
2279 cell
->setPort(ID::D
, sig_d
);
2280 cell
->setPort(ID::Q
, sig_q
);
2281 cell
->set_src_attribute(src
);
2285 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2286 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2288 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2289 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2290 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2291 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2292 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2293 cell
->setPort(ID::CLK
, sig_clk
);
2294 cell
->setPort(ID::SET
, sig_set
);
2295 cell
->setPort(ID::CLR
, sig_clr
);
2296 cell
->setPort(ID::D
, sig_d
);
2297 cell
->setPort(ID::Q
, sig_q
);
2298 cell
->set_src_attribute(src
);
2302 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2303 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2305 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2306 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2307 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2308 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2309 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2310 cell
->setPort(ID::CLK
, sig_clk
);
2311 cell
->setPort(ID::ARST
, sig_arst
);
2312 cell
->setPort(ID::D
, sig_d
);
2313 cell
->setPort(ID::Q
, sig_q
);
2314 cell
->set_src_attribute(src
);
2318 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, const std::string
&src
)
2320 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2321 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2322 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2323 cell
->setPort(ID::EN
, sig_en
);
2324 cell
->setPort(ID::D
, sig_d
);
2325 cell
->setPort(ID::Q
, sig_q
);
2326 cell
->set_src_attribute(src
);
2330 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2331 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2333 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2334 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2335 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2336 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2337 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2338 cell
->setPort(ID::EN
, sig_en
);
2339 cell
->setPort(ID::SET
, sig_set
);
2340 cell
->setPort(ID::CLR
, sig_clr
);
2341 cell
->setPort(ID::D
, sig_d
);
2342 cell
->setPort(ID::Q
, sig_q
);
2343 cell
->set_src_attribute(src
);
2347 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2349 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2350 cell
->setPort(ID::D
, sig_d
);
2351 cell
->setPort(ID::Q
, sig_q
);
2352 cell
->set_src_attribute(src
);
2356 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, const std::string
&src
)
2358 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2359 cell
->setPort(ID::C
, sig_clk
);
2360 cell
->setPort(ID::D
, sig_d
);
2361 cell
->setPort(ID::Q
, sig_q
);
2362 cell
->set_src_attribute(src
);
2366 RTLIL::Cell
* RTLIL::Module::addDffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, const std::string
&src
)
2368 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2369 cell
->setPort(ID::C
, sig_clk
);
2370 cell
->setPort(ID::E
, sig_en
);
2371 cell
->setPort(ID::D
, sig_d
);
2372 cell
->setPort(ID::Q
, sig_q
);
2373 cell
->set_src_attribute(src
);
2377 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2378 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2380 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2381 cell
->setPort(ID::C
, sig_clk
);
2382 cell
->setPort(ID::S
, sig_set
);
2383 cell
->setPort(ID::R
, sig_clr
);
2384 cell
->setPort(ID::D
, sig_d
);
2385 cell
->setPort(ID::Q
, sig_q
);
2386 cell
->set_src_attribute(src
);
2390 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2391 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2393 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2394 cell
->setPort(ID::C
, sig_clk
);
2395 cell
->setPort(ID::R
, sig_arst
);
2396 cell
->setPort(ID::D
, sig_d
);
2397 cell
->setPort(ID::Q
, sig_q
);
2398 cell
->set_src_attribute(src
);
2402 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, const std::string
&src
)
2404 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2405 cell
->setPort(ID::E
, sig_en
);
2406 cell
->setPort(ID::D
, sig_d
);
2407 cell
->setPort(ID::Q
, sig_q
);
2408 cell
->set_src_attribute(src
);
2412 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2413 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2415 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2416 cell
->setPort(ID::E
, sig_en
);
2417 cell
->setPort(ID::S
, sig_set
);
2418 cell
->setPort(ID::R
, sig_clr
);
2419 cell
->setPort(ID::D
, sig_d
);
2420 cell
->setPort(ID::Q
, sig_q
);
2421 cell
->set_src_attribute(src
);
2425 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2427 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2428 Cell
*cell
= addCell(name
, ID($anyconst
));
2429 cell
->setParam(ID::WIDTH
, width
);
2430 cell
->setPort(ID::Y
, sig
);
2431 cell
->set_src_attribute(src
);
2435 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2437 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2438 Cell
*cell
= addCell(name
, ID($anyseq
));
2439 cell
->setParam(ID::WIDTH
, width
);
2440 cell
->setPort(ID::Y
, sig
);
2441 cell
->set_src_attribute(src
);
2445 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2447 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2448 Cell
*cell
= addCell(name
, ID($allconst
));
2449 cell
->setParam(ID::WIDTH
, width
);
2450 cell
->setPort(ID::Y
, sig
);
2451 cell
->set_src_attribute(src
);
2455 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2457 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2458 Cell
*cell
= addCell(name
, ID($allseq
));
2459 cell
->setParam(ID::WIDTH
, width
);
2460 cell
->setPort(ID::Y
, sig
);
2461 cell
->set_src_attribute(src
);
2465 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2467 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2468 Cell
*cell
= addCell(name
, ID($initstate
));
2469 cell
->setPort(ID::Y
, sig
);
2470 cell
->set_src_attribute(src
);
2476 static unsigned int hashidx_count
= 123456789;
2477 hashidx_count
= mkhash_xorshift(hashidx_count
);
2478 hashidx_
= hashidx_count
;
2485 port_output
= false;
2490 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2494 RTLIL::Wire::~Wire()
2497 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2502 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2503 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2509 RTLIL::Memory::Memory()
2511 static unsigned int hashidx_count
= 123456789;
2512 hashidx_count
= mkhash_xorshift(hashidx_count
);
2513 hashidx_
= hashidx_count
;
2519 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2523 RTLIL::Cell::Cell() : module(nullptr)
2525 static unsigned int hashidx_count
= 123456789;
2526 hashidx_count
= mkhash_xorshift(hashidx_count
);
2527 hashidx_
= hashidx_count
;
2529 // log("#memtrace# %p\n", this);
2533 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2537 RTLIL::Cell::~Cell()
2540 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2545 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2546 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2552 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2554 return connections_
.count(portname
) != 0;
2557 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2559 RTLIL::SigSpec signal
;
2560 auto conn_it
= connections_
.find(portname
);
2562 if (conn_it
!= connections_
.end())
2564 for (auto mon
: module
->monitors
)
2565 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2568 for (auto mon
: module
->design
->monitors
)
2569 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2572 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2573 log_backtrace("-X- ", yosys_xtrace
-1);
2576 connections_
.erase(conn_it
);
2580 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2582 auto r
= connections_
.insert(portname
);
2583 auto conn_it
= r
.first
;
2584 if (!r
.second
&& conn_it
->second
== signal
)
2587 for (auto mon
: module
->monitors
)
2588 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2591 for (auto mon
: module
->design
->monitors
)
2592 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2595 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2596 log_backtrace("-X- ", yosys_xtrace
-1);
2599 conn_it
->second
= std::move(signal
);
2602 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2604 return connections_
.at(portname
);
2607 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2609 return connections_
;
2612 bool RTLIL::Cell::known() const
2614 if (yosys_celltypes
.cell_known(type
))
2616 if (module
&& module
->design
&& module
->design
->module(type
))
2621 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2623 if (yosys_celltypes
.cell_known(type
))
2624 return yosys_celltypes
.cell_input(type
, portname
);
2625 if (module
&& module
->design
) {
2626 RTLIL::Module
*m
= module
->design
->module(type
);
2627 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2628 return w
&& w
->port_input
;
2633 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
2635 if (yosys_celltypes
.cell_known(type
))
2636 return yosys_celltypes
.cell_output(type
, portname
);
2637 if (module
&& module
->design
) {
2638 RTLIL::Module
*m
= module
->design
->module(type
);
2639 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
2640 return w
&& w
->port_output
;
2645 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
2647 return parameters
.count(paramname
) != 0;
2650 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
2652 parameters
.erase(paramname
);
2655 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
2657 parameters
[paramname
] = std::move(value
);
2660 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
2662 const auto &it
= parameters
.find(paramname
);
2663 if (it
!= parameters
.end())
2665 if (module
&& module
->design
) {
2666 RTLIL::Module
*m
= module
->design
->module(type
);
2668 return m
->parameter_default_values
.at(paramname
);
2670 throw std::out_of_range("Cell::getParam()");
2673 void RTLIL::Cell::sort()
2675 connections_
.sort(sort_by_id_str());
2676 parameters
.sort(sort_by_id_str());
2677 attributes
.sort(sort_by_id_str());
2680 void RTLIL::Cell::check()
2683 InternalCellChecker
checker(NULL
, this);
2688 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
2690 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
2691 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
2694 if (type
== ID($mux
) || type
== ID($pmux
)) {
2695 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
2696 if (type
== ID($pmux
))
2697 parameters
[ID::S_WIDTH
] = GetSize(connections_
[ID::S
]);
2702 if (type
== ID($lut
) || type
== ID($sop
)) {
2703 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::A
]);
2707 if (type
== ID($fa
)) {
2708 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
2712 if (type
== ID($lcu
)) {
2713 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::CO
]);
2717 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
2719 if (connections_
.count(ID::A
)) {
2720 if (signedness_ab
) {
2722 parameters
[ID::A_SIGNED
] = true;
2723 else if (parameters
.count(ID::A_SIGNED
) == 0)
2724 parameters
[ID::A_SIGNED
] = false;
2726 parameters
[ID::A_WIDTH
] = GetSize(connections_
[ID::A
]);
2729 if (connections_
.count(ID::B
)) {
2730 if (signedness_ab
) {
2732 parameters
[ID::B_SIGNED
] = true;
2733 else if (parameters
.count(ID::B_SIGNED
) == 0)
2734 parameters
[ID::B_SIGNED
] = false;
2736 parameters
[ID::B_WIDTH
] = GetSize(connections_
[ID::B
]);
2739 if (connections_
.count(ID::Y
))
2740 parameters
[ID::Y_WIDTH
] = GetSize(connections_
[ID::Y
]);
2742 if (connections_
.count(ID::Q
))
2743 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Q
]);
2748 RTLIL::SigChunk::SigChunk()
2755 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
2759 width
= GetSize(data
);
2763 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
2765 log_assert(wire
!= nullptr);
2767 this->width
= wire
->width
;
2771 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
2773 log_assert(wire
!= nullptr);
2775 this->width
= width
;
2776 this->offset
= offset
;
2779 RTLIL::SigChunk::SigChunk(const std::string
&str
)
2782 data
= RTLIL::Const(str
).bits
;
2783 width
= GetSize(data
);
2787 RTLIL::SigChunk::SigChunk(int val
, int width
)
2790 data
= RTLIL::Const(val
, width
).bits
;
2791 this->width
= GetSize(data
);
2795 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
2798 data
= RTLIL::Const(bit
, width
).bits
;
2799 this->width
= GetSize(data
);
2803 RTLIL::SigChunk::SigChunk(const RTLIL::SigBit
&bit
)
2808 data
= RTLIL::Const(bit
.data
).bits
;
2810 offset
= bit
.offset
;
2814 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
)
2819 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
2821 RTLIL::SigChunk ret
;
2824 ret
.offset
= this->offset
+ offset
;
2827 for (int i
= 0; i
< length
; i
++)
2828 ret
.data
.push_back(data
[offset
+i
]);
2834 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
2836 if (wire
&& other
.wire
)
2837 if (wire
->name
!= other
.wire
->name
)
2838 return wire
->name
< other
.wire
->name
;
2840 if (wire
!= other
.wire
)
2841 return wire
< other
.wire
;
2843 if (offset
!= other
.offset
)
2844 return offset
< other
.offset
;
2846 if (width
!= other
.width
)
2847 return width
< other
.width
;
2849 return data
< other
.data
;
2852 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
2854 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
2857 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
2864 RTLIL::SigSpec::SigSpec()
2870 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
2875 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
2877 cover("kernel.rtlil.sigspec.init.list");
2882 log_assert(parts
.size() > 0);
2883 auto ie
= parts
.begin();
2884 auto it
= ie
+ parts
.size() - 1;
2889 RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
2891 cover("kernel.rtlil.sigspec.assign");
2893 width_
= other
.width_
;
2894 hash_
= other
.hash_
;
2895 chunks_
= other
.chunks_
;
2896 bits_
= other
.bits_
;
2900 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2902 cover("kernel.rtlil.sigspec.init.const");
2904 chunks_
.emplace_back(value
);
2905 width_
= chunks_
.back().width
;
2910 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2912 cover("kernel.rtlil.sigspec.init.chunk");
2914 chunks_
.emplace_back(chunk
);
2915 width_
= chunks_
.back().width
;
2920 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2922 cover("kernel.rtlil.sigspec.init.wire");
2924 chunks_
.emplace_back(wire
);
2925 width_
= chunks_
.back().width
;
2930 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2932 cover("kernel.rtlil.sigspec.init.wire_part");
2934 chunks_
.emplace_back(wire
, offset
, width
);
2935 width_
= chunks_
.back().width
;
2940 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2942 cover("kernel.rtlil.sigspec.init.str");
2944 chunks_
.emplace_back(str
);
2945 width_
= chunks_
.back().width
;
2950 RTLIL::SigSpec::SigSpec(int val
, int width
)
2952 cover("kernel.rtlil.sigspec.init.int");
2954 chunks_
.emplace_back(val
, width
);
2960 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2962 cover("kernel.rtlil.sigspec.init.state");
2964 chunks_
.emplace_back(bit
, width
);
2970 RTLIL::SigSpec::SigSpec(const RTLIL::SigBit
&bit
, int width
)
2972 cover("kernel.rtlil.sigspec.init.bit");
2974 if (bit
.wire
== NULL
)
2975 chunks_
.emplace_back(bit
.data
, width
);
2977 for (int i
= 0; i
< width
; i
++)
2978 chunks_
.push_back(bit
);
2984 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigChunk
> &chunks
)
2986 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2990 for (const auto &c
: chunks
)
2995 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigBit
> &bits
)
2997 cover("kernel.rtlil.sigspec.init.stdvec_bits");
3001 for (const auto &bit
: bits
)
3006 RTLIL::SigSpec::SigSpec(const pool
<RTLIL::SigBit
> &bits
)
3008 cover("kernel.rtlil.sigspec.init.pool_bits");
3012 for (const auto &bit
: bits
)
3017 RTLIL::SigSpec::SigSpec(const std::set
<RTLIL::SigBit
> &bits
)
3019 cover("kernel.rtlil.sigspec.init.stdset_bits");
3023 for (const auto &bit
: bits
)
3028 RTLIL::SigSpec::SigSpec(bool bit
)
3030 cover("kernel.rtlil.sigspec.init.bool");
3034 append(SigBit(bit
));
3038 void RTLIL::SigSpec::pack() const
3040 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3042 if (that
->bits_
.empty())
3045 cover("kernel.rtlil.sigspec.convert.pack");
3046 log_assert(that
->chunks_
.empty());
3048 std::vector
<RTLIL::SigBit
> old_bits
;
3049 old_bits
.swap(that
->bits_
);
3051 RTLIL::SigChunk
*last
= NULL
;
3052 int last_end_offset
= 0;
3054 for (auto &bit
: old_bits
) {
3055 if (last
&& bit
.wire
== last
->wire
) {
3056 if (bit
.wire
== NULL
) {
3057 last
->data
.push_back(bit
.data
);
3060 } else if (last_end_offset
== bit
.offset
) {
3066 that
->chunks_
.push_back(bit
);
3067 last
= &that
->chunks_
.back();
3068 last_end_offset
= bit
.offset
+ 1;
3074 void RTLIL::SigSpec::unpack() const
3076 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3078 if (that
->chunks_
.empty())
3081 cover("kernel.rtlil.sigspec.convert.unpack");
3082 log_assert(that
->bits_
.empty());
3084 that
->bits_
.reserve(that
->width_
);
3085 for (auto &c
: that
->chunks_
)
3086 for (int i
= 0; i
< c
.width
; i
++)
3087 that
->bits_
.emplace_back(c
, i
);
3089 that
->chunks_
.clear();
3093 void RTLIL::SigSpec::updhash() const
3095 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3097 if (that
->hash_
!= 0)
3100 cover("kernel.rtlil.sigspec.hash");
3103 that
->hash_
= mkhash_init
;
3104 for (auto &c
: that
->chunks_
)
3105 if (c
.wire
== NULL
) {
3106 for (auto &v
: c
.data
)
3107 that
->hash_
= mkhash(that
->hash_
, v
);
3109 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3110 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3111 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3114 if (that
->hash_
== 0)
3118 void RTLIL::SigSpec::sort()
3121 cover("kernel.rtlil.sigspec.sort");
3122 std::sort(bits_
.begin(), bits_
.end());
3125 void RTLIL::SigSpec::sort_and_unify()
3128 cover("kernel.rtlil.sigspec.sort_and_unify");
3130 // A copy of the bits vector is used to prevent duplicating the logic from
3131 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3132 // that isn't showing up as significant in profiles.
3133 std::vector
<SigBit
> unique_bits
= bits_
;
3134 std::sort(unique_bits
.begin(), unique_bits
.end());
3135 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3136 unique_bits
.erase(last
, unique_bits
.end());
3138 *this = unique_bits
;
3141 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3143 replace(pattern
, with
, this);
3146 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3148 log_assert(other
!= NULL
);
3149 log_assert(width_
== other
->width_
);
3150 log_assert(pattern
.width_
== with
.width_
);
3157 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3158 if (pattern
.bits_
[i
].wire
!= NULL
) {
3159 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3160 if (bits_
[j
] == pattern
.bits_
[i
]) {
3161 other
->bits_
[j
] = with
.bits_
[i
];
3170 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3172 replace(rules
, this);
3175 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3177 cover("kernel.rtlil.sigspec.replace_dict");
3179 log_assert(other
!= NULL
);
3180 log_assert(width_
== other
->width_
);
3182 if (rules
.empty()) return;
3186 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3187 auto it
= rules
.find(bits_
[i
]);
3188 if (it
!= rules
.end())
3189 other
->bits_
[i
] = it
->second
;
3195 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3197 replace(rules
, this);
3200 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3202 cover("kernel.rtlil.sigspec.replace_map");
3204 log_assert(other
!= NULL
);
3205 log_assert(width_
== other
->width_
);
3207 if (rules
.empty()) return;
3211 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3212 auto it
= rules
.find(bits_
[i
]);
3213 if (it
!= rules
.end())
3214 other
->bits_
[i
] = it
->second
;
3220 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3222 remove2(pattern
, NULL
);
3225 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3227 RTLIL::SigSpec tmp
= *this;
3228 tmp
.remove2(pattern
, other
);
3231 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3234 cover("kernel.rtlil.sigspec.remove_other");
3236 cover("kernel.rtlil.sigspec.remove");
3239 if (other
!= NULL
) {
3240 log_assert(width_
== other
->width_
);
3244 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3246 if (bits_
[i
].wire
== NULL
) continue;
3248 for (auto &pattern_chunk
: pattern
.chunks())
3249 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3250 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3251 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3252 bits_
.erase(bits_
.begin() + i
);
3254 if (other
!= NULL
) {
3255 other
->bits_
.erase(other
->bits_
.begin() + i
);
3265 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3267 remove2(pattern
, NULL
);
3270 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3272 RTLIL::SigSpec tmp
= *this;
3273 tmp
.remove2(pattern
, other
);
3276 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3279 cover("kernel.rtlil.sigspec.remove_other");
3281 cover("kernel.rtlil.sigspec.remove");
3285 if (other
!= NULL
) {
3286 log_assert(width_
== other
->width_
);
3290 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3291 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3292 bits_
.erase(bits_
.begin() + i
);
3294 if (other
!= NULL
) {
3295 other
->bits_
.erase(other
->bits_
.begin() + i
);
3304 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3307 cover("kernel.rtlil.sigspec.remove_other");
3309 cover("kernel.rtlil.sigspec.remove");
3313 if (other
!= NULL
) {
3314 log_assert(width_
== other
->width_
);
3318 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3319 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3320 bits_
.erase(bits_
.begin() + i
);
3322 if (other
!= NULL
) {
3323 other
->bits_
.erase(other
->bits_
.begin() + i
);
3332 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3335 cover("kernel.rtlil.sigspec.extract_other");
3337 cover("kernel.rtlil.sigspec.extract");
3339 log_assert(other
== NULL
|| width_
== other
->width_
);
3342 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3344 for (auto& pattern_chunk
: pattern
.chunks()) {
3346 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3347 for (int i
= 0; i
< width_
; i
++)
3348 if (bits_match
[i
].wire
&&
3349 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3350 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3351 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3352 ret
.append(bits_other
[i
]);
3354 for (int i
= 0; i
< width_
; i
++)
3355 if (bits_match
[i
].wire
&&
3356 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3357 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3358 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3359 ret
.append(bits_match
[i
]);
3367 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3370 cover("kernel.rtlil.sigspec.extract_other");
3372 cover("kernel.rtlil.sigspec.extract");
3374 log_assert(other
== NULL
|| width_
== other
->width_
);
3376 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3380 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3381 for (int i
= 0; i
< width_
; i
++)
3382 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3383 ret
.append(bits_other
[i
]);
3385 for (int i
= 0; i
< width_
; i
++)
3386 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3387 ret
.append(bits_match
[i
]);
3394 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3396 cover("kernel.rtlil.sigspec.replace_pos");
3401 log_assert(offset
>= 0);
3402 log_assert(with
.width_
>= 0);
3403 log_assert(offset
+with
.width_
<= width_
);
3405 for (int i
= 0; i
< with
.width_
; i
++)
3406 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3411 void RTLIL::SigSpec::remove_const()
3415 cover("kernel.rtlil.sigspec.remove_const.packed");
3417 std::vector
<RTLIL::SigChunk
> new_chunks
;
3418 new_chunks
.reserve(GetSize(chunks_
));
3421 for (auto &chunk
: chunks_
)
3422 if (chunk
.wire
!= NULL
) {
3423 new_chunks
.push_back(chunk
);
3424 width_
+= chunk
.width
;
3427 chunks_
.swap(new_chunks
);
3431 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3433 std::vector
<RTLIL::SigBit
> new_bits
;
3434 new_bits
.reserve(width_
);
3436 for (auto &bit
: bits_
)
3437 if (bit
.wire
!= NULL
)
3438 new_bits
.push_back(bit
);
3440 bits_
.swap(new_bits
);
3441 width_
= bits_
.size();
3447 void RTLIL::SigSpec::remove(int offset
, int length
)
3449 cover("kernel.rtlil.sigspec.remove_pos");
3453 log_assert(offset
>= 0);
3454 log_assert(length
>= 0);
3455 log_assert(offset
+ length
<= width_
);
3457 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3458 width_
= bits_
.size();
3463 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3466 cover("kernel.rtlil.sigspec.extract_pos");
3467 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3470 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3472 if (signal
.width_
== 0)
3480 cover("kernel.rtlil.sigspec.append");
3482 if (packed() != signal
.packed()) {
3488 for (auto &other_c
: signal
.chunks_
)
3490 auto &my_last_c
= chunks_
.back();
3491 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3492 auto &this_data
= my_last_c
.data
;
3493 auto &other_data
= other_c
.data
;
3494 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3495 my_last_c
.width
+= other_c
.width
;
3497 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3498 my_last_c
.width
+= other_c
.width
;
3500 chunks_
.push_back(other_c
);
3503 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3505 width_
+= signal
.width_
;
3509 void RTLIL::SigSpec::append(const RTLIL::SigBit
&bit
)
3513 cover("kernel.rtlil.sigspec.append_bit.packed");
3515 if (chunks_
.size() == 0)
3516 chunks_
.push_back(bit
);
3518 if (bit
.wire
== NULL
)
3519 if (chunks_
.back().wire
== NULL
) {
3520 chunks_
.back().data
.push_back(bit
.data
);
3521 chunks_
.back().width
++;
3523 chunks_
.push_back(bit
);
3525 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3526 chunks_
.back().width
++;
3528 chunks_
.push_back(bit
);
3532 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3533 bits_
.push_back(bit
);
3540 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3542 cover("kernel.rtlil.sigspec.extend_u0");
3547 remove(width
, width_
- width
);
3549 if (width_
< width
) {
3550 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3552 padding
= RTLIL::State::S0
;
3553 while (width_
< width
)
3559 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3561 cover("kernel.rtlil.sigspec.repeat");
3564 for (int i
= 0; i
< num
; i
++)
3570 void RTLIL::SigSpec::check() const
3574 cover("kernel.rtlil.sigspec.check.skip");
3578 cover("kernel.rtlil.sigspec.check.packed");
3581 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3582 const RTLIL::SigChunk
&chunk
= chunks_
[i
];
3583 if (chunk
.wire
== NULL
) {
3585 log_assert(chunks_
[i
-1].wire
!= NULL
);
3586 log_assert(chunk
.offset
== 0);
3587 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3589 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3590 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3591 log_assert(chunk
.offset
>= 0);
3592 log_assert(chunk
.width
>= 0);
3593 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3594 log_assert(chunk
.data
.size() == 0);
3598 log_assert(w
== width_
);
3599 log_assert(bits_
.empty());
3603 cover("kernel.rtlil.sigspec.check.unpacked");
3605 log_assert(width_
== GetSize(bits_
));
3606 log_assert(chunks_
.empty());
3611 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3613 cover("kernel.rtlil.sigspec.comp_lt");
3618 if (width_
!= other
.width_
)
3619 return width_
< other
.width_
;
3624 if (chunks_
.size() != other
.chunks_
.size())
3625 return chunks_
.size() < other
.chunks_
.size();
3630 if (hash_
!= other
.hash_
)
3631 return hash_
< other
.hash_
;
3633 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3634 if (chunks_
[i
] != other
.chunks_
[i
]) {
3635 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3636 return chunks_
[i
] < other
.chunks_
[i
];
3639 cover("kernel.rtlil.sigspec.comp_lt.equal");
3643 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
3645 cover("kernel.rtlil.sigspec.comp_eq");
3650 if (width_
!= other
.width_
)
3653 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
3654 // since the RHS will contain one SigChunk of width 0 causing
3655 // the size check below to fail
3662 if (chunks_
.size() != other
.chunks_
.size())
3668 if (hash_
!= other
.hash_
)
3671 for (size_t i
= 0; i
< chunks_
.size(); i
++)
3672 if (chunks_
[i
] != other
.chunks_
[i
]) {
3673 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3677 cover("kernel.rtlil.sigspec.comp_eq.equal");
3681 bool RTLIL::SigSpec::is_wire() const
3683 cover("kernel.rtlil.sigspec.is_wire");
3686 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
3689 bool RTLIL::SigSpec::is_chunk() const
3691 cover("kernel.rtlil.sigspec.is_chunk");
3694 return GetSize(chunks_
) == 1;
3697 bool RTLIL::SigSpec::is_fully_const() const
3699 cover("kernel.rtlil.sigspec.is_fully_const");
3702 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3703 if (it
->width
> 0 && it
->wire
!= NULL
)
3708 bool RTLIL::SigSpec::is_fully_zero() const
3710 cover("kernel.rtlil.sigspec.is_fully_zero");
3713 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3714 if (it
->width
> 0 && it
->wire
!= NULL
)
3716 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3717 if (it
->data
[i
] != RTLIL::State::S0
)
3723 bool RTLIL::SigSpec::is_fully_ones() const
3725 cover("kernel.rtlil.sigspec.is_fully_ones");
3728 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3729 if (it
->width
> 0 && it
->wire
!= NULL
)
3731 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3732 if (it
->data
[i
] != RTLIL::State::S1
)
3738 bool RTLIL::SigSpec::is_fully_def() const
3740 cover("kernel.rtlil.sigspec.is_fully_def");
3743 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3744 if (it
->width
> 0 && it
->wire
!= NULL
)
3746 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3747 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
3753 bool RTLIL::SigSpec::is_fully_undef() const
3755 cover("kernel.rtlil.sigspec.is_fully_undef");
3758 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
3759 if (it
->width
> 0 && it
->wire
!= NULL
)
3761 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3762 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
3768 bool RTLIL::SigSpec::has_const() const
3770 cover("kernel.rtlil.sigspec.has_const");
3773 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3774 if (it
->width
> 0 && it
->wire
== NULL
)
3779 bool RTLIL::SigSpec::has_marked_bits() const
3781 cover("kernel.rtlil.sigspec.has_marked_bits");
3784 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
3785 if (it
->width
> 0 && it
->wire
== NULL
) {
3786 for (size_t i
= 0; i
< it
->data
.size(); i
++)
3787 if (it
->data
[i
] == RTLIL::State::Sm
)
3793 bool RTLIL::SigSpec::as_bool() const
3795 cover("kernel.rtlil.sigspec.as_bool");
3798 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3800 return RTLIL::Const(chunks_
[0].data
).as_bool();
3804 int RTLIL::SigSpec::as_int(bool is_signed
) const
3806 cover("kernel.rtlil.sigspec.as_int");
3809 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3811 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
3815 std::string
RTLIL::SigSpec::as_string() const
3817 cover("kernel.rtlil.sigspec.as_string");
3821 str
.reserve(size());
3822 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
3823 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
3824 if (chunk
.wire
!= NULL
)
3825 str
.append(chunk
.width
, '?');
3827 str
+= RTLIL::Const(chunk
.data
).as_string();
3832 RTLIL::Const
RTLIL::SigSpec::as_const() const
3834 cover("kernel.rtlil.sigspec.as_const");
3837 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
3839 return chunks_
[0].data
;
3840 return RTLIL::Const();
3843 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
3845 cover("kernel.rtlil.sigspec.as_wire");
3848 log_assert(is_wire());
3849 return chunks_
[0].wire
;
3852 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
3854 cover("kernel.rtlil.sigspec.as_chunk");
3857 log_assert(is_chunk());
3861 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
3863 cover("kernel.rtlil.sigspec.as_bit");
3865 log_assert(width_
== 1);
3867 return RTLIL::SigBit(*chunks_
.begin());
3872 bool RTLIL::SigSpec::match(const char* pattern
) const
3874 cover("kernel.rtlil.sigspec.match");
3877 log_assert(int(strlen(pattern
)) == GetSize(bits_
));
3879 for (auto it
= bits_
.rbegin(); it
!= bits_
.rend(); it
++, pattern
++) {
3880 if (*pattern
== ' ')
3882 if (*pattern
== '*') {
3883 if (*it
!= State::Sz
&& *it
!= State::Sx
)
3887 if (*pattern
== '0') {
3888 if (*it
!= State::S0
)
3891 if (*pattern
== '1') {
3892 if (*it
!= State::S1
)
3901 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
3903 cover("kernel.rtlil.sigspec.to_sigbit_set");
3906 std::set
<RTLIL::SigBit
> sigbits
;
3907 for (auto &c
: chunks_
)
3908 for (int i
= 0; i
< c
.width
; i
++)
3909 sigbits
.insert(RTLIL::SigBit(c
, i
));
3913 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
3915 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3918 pool
<RTLIL::SigBit
> sigbits
;
3919 sigbits
.reserve(size());
3920 for (auto &c
: chunks_
)
3921 for (int i
= 0; i
< c
.width
; i
++)
3922 sigbits
.insert(RTLIL::SigBit(c
, i
));
3926 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
3928 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3934 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
3936 cover("kernel.rtlil.sigspec.to_sigbit_map");
3941 log_assert(width_
== other
.width_
);
3943 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3944 for (int i
= 0; i
< width_
; i
++)
3945 new_map
[bits_
[i
]] = other
.bits_
[i
];
3950 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
3952 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3957 log_assert(width_
== other
.width_
);
3959 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
3960 new_map
.reserve(size());
3961 for (int i
= 0; i
< width_
; i
++)
3962 new_map
[bits_
[i
]] = other
.bits_
[i
];
3967 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
3969 size_t start
= 0, end
= 0;
3970 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
3971 tokens
.push_back(text
.substr(start
, end
- start
));
3974 tokens
.push_back(text
.substr(start
));
3977 static int sigspec_parse_get_dummy_line_num()
3982 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3984 cover("kernel.rtlil.sigspec.parse");
3986 AST::current_filename
= "input";
3988 std::vector
<std::string
> tokens
;
3989 sigspec_parse_split(tokens
, str
, ',');
3991 sig
= RTLIL::SigSpec();
3992 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
3994 std::string netname
= tokens
[tokidx
];
3995 std::string indices
;
3997 if (netname
.size() == 0)
4000 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
4001 cover("kernel.rtlil.sigspec.parse.const");
4002 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
4003 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
4006 sig
.append(RTLIL::Const(ast
->bits
));
4014 cover("kernel.rtlil.sigspec.parse.net");
4016 if (netname
[0] != '$' && netname
[0] != '\\')
4017 netname
= "\\" + netname
;
4019 if (module
->wires_
.count(netname
) == 0) {
4020 size_t indices_pos
= netname
.size()-1;
4021 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
4024 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4025 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
4027 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4029 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
4030 indices
= netname
.substr(indices_pos
);
4031 netname
= netname
.substr(0, indices_pos
);
4036 if (module
->wires_
.count(netname
) == 0)
4039 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
4040 if (!indices
.empty()) {
4041 std::vector
<std::string
> index_tokens
;
4042 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
4043 if (index_tokens
.size() == 1) {
4044 cover("kernel.rtlil.sigspec.parse.bit_sel");
4045 int a
= atoi(index_tokens
.at(0).c_str());
4046 if (a
< 0 || a
>= wire
->width
)
4048 sig
.append(RTLIL::SigSpec(wire
, a
));
4050 cover("kernel.rtlil.sigspec.parse.part_sel");
4051 int a
= atoi(index_tokens
.at(0).c_str());
4052 int b
= atoi(index_tokens
.at(1).c_str());
4057 if (a
< 0 || a
>= wire
->width
)
4059 if (b
< 0 || b
>= wire
->width
)
4061 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4070 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4072 if (str
.empty() || str
[0] != '@')
4073 return parse(sig
, module
, str
);
4075 cover("kernel.rtlil.sigspec.parse.sel");
4077 str
= RTLIL::escape_id(str
.substr(1));
4078 if (design
->selection_vars
.count(str
) == 0)
4081 sig
= RTLIL::SigSpec();
4082 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4083 for (auto &it
: module
->wires_
)
4084 if (sel
.selected_member(module
->name
, it
.first
))
4085 sig
.append(it
.second
);
4090 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4093 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4094 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4099 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4100 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4104 if (lhs
.chunks_
.size() == 1) {
4105 char *p
= (char*)str
.c_str(), *endptr
;
4106 long int val
= strtol(p
, &endptr
, 10);
4107 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4108 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4109 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4114 return parse(sig
, module
, str
);
4117 RTLIL::CaseRule::~CaseRule()
4119 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4123 bool RTLIL::CaseRule::empty() const
4125 return actions
.empty() && switches
.empty();
4128 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4130 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4131 new_caserule
->compare
= compare
;
4132 new_caserule
->actions
= actions
;
4133 for (auto &it
: switches
)
4134 new_caserule
->switches
.push_back(it
->clone());
4135 return new_caserule
;
4138 RTLIL::SwitchRule::~SwitchRule()
4140 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4144 bool RTLIL::SwitchRule::empty() const
4146 return cases
.empty();
4149 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4151 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4152 new_switchrule
->signal
= signal
;
4153 new_switchrule
->attributes
= attributes
;
4154 for (auto &it
: cases
)
4155 new_switchrule
->cases
.push_back(it
->clone());
4156 return new_switchrule
;
4160 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4162 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4163 new_syncrule
->type
= type
;
4164 new_syncrule
->signal
= signal
;
4165 new_syncrule
->actions
= actions
;
4166 return new_syncrule
;
4169 RTLIL::Process::~Process()
4171 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4175 RTLIL::Process
*RTLIL::Process::clone() const
4177 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4179 new_proc
->name
= name
;
4180 new_proc
->attributes
= attributes
;
4182 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4183 new_proc
->root_case
= *rc_ptr
;
4184 rc_ptr
->switches
.clear();
4187 for (auto &it
: syncs
)
4188 new_proc
->syncs
.push_back(it
->clone());
4194 RTLIL::Memory::~Memory()
4196 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4198 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4199 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4201 return &all_memorys
;