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/rtlil/rtlil_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
= {
187 RTLIL::Const::Const()
189 flags
= RTLIL::CONST_FLAG_NONE
;
192 RTLIL::Const::Const(std::string str
)
194 flags
= RTLIL::CONST_FLAG_STRING
;
195 for (int i
= str
.size()-1; i
>= 0; i
--) {
196 unsigned char ch
= str
[i
];
197 for (int j
= 0; j
< 8; j
++) {
198 bits
.push_back((ch
& 1) != 0 ? State::S1
: State::S0
);
204 RTLIL::Const::Const(int val
, int width
)
206 flags
= RTLIL::CONST_FLAG_NONE
;
207 for (int i
= 0; i
< width
; i
++) {
208 bits
.push_back((val
& 1) != 0 ? State::S1
: State::S0
);
213 RTLIL::Const::Const(RTLIL::State bit
, int width
)
215 flags
= RTLIL::CONST_FLAG_NONE
;
216 for (int i
= 0; i
< width
; i
++)
220 RTLIL::Const::Const(const std::vector
<bool> &bits
)
222 flags
= RTLIL::CONST_FLAG_NONE
;
223 for (const auto &b
: bits
)
224 this->bits
.emplace_back(b
? State::S1
: State::S0
);
227 RTLIL::Const::Const(const RTLIL::Const
&c
)
230 for (const auto &b
: c
.bits
)
231 this->bits
.push_back(b
);
234 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
236 if (bits
.size() != other
.bits
.size())
237 return bits
.size() < other
.bits
.size();
238 for (size_t i
= 0; i
< bits
.size(); i
++)
239 if (bits
[i
] != other
.bits
[i
])
240 return bits
[i
] < other
.bits
[i
];
244 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
246 return bits
== other
.bits
;
249 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
251 return bits
!= other
.bits
;
254 bool RTLIL::Const::as_bool() const
256 for (size_t i
= 0; i
< bits
.size(); i
++)
257 if (bits
[i
] == State::S1
)
262 int RTLIL::Const::as_int(bool is_signed
) const
265 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
266 if (bits
[i
] == State::S1
)
268 if (is_signed
&& bits
.back() == State::S1
)
269 for (size_t i
= bits
.size(); i
< 32; i
++)
274 std::string
RTLIL::Const::as_string() const
277 ret
.reserve(bits
.size());
278 for (size_t i
= bits
.size(); i
> 0; i
--)
280 case S0
: ret
+= "0"; break;
281 case S1
: ret
+= "1"; break;
282 case Sx
: ret
+= "x"; break;
283 case Sz
: ret
+= "z"; break;
284 case Sa
: ret
+= "-"; break;
285 case Sm
: ret
+= "m"; break;
290 RTLIL::Const
RTLIL::Const::from_string(const std::string
&str
)
293 c
.bits
.reserve(str
.size());
294 for (auto it
= str
.rbegin(); it
!= str
.rend(); it
++)
296 case '0': c
.bits
.push_back(State::S0
); break;
297 case '1': c
.bits
.push_back(State::S1
); break;
298 case 'x': c
.bits
.push_back(State::Sx
); break;
299 case 'z': c
.bits
.push_back(State::Sz
); break;
300 case 'm': c
.bits
.push_back(State::Sm
); break;
301 default: c
.bits
.push_back(State::Sa
);
306 std::string
RTLIL::Const::decode_string() const
309 string
.reserve(GetSize(bits
)/8);
310 for (int i
= 0; i
< GetSize(bits
); i
+= 8) {
312 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
313 if (bits
[i
+ j
] == RTLIL::State::S1
)
318 std::reverse(string
.begin(), string
.end());
322 bool RTLIL::Const::is_fully_zero() const
324 cover("kernel.rtlil.const.is_fully_zero");
326 for (const auto &bit
: bits
)
327 if (bit
!= RTLIL::State::S0
)
333 bool RTLIL::Const::is_fully_ones() const
335 cover("kernel.rtlil.const.is_fully_ones");
337 for (const auto &bit
: bits
)
338 if (bit
!= RTLIL::State::S1
)
344 bool RTLIL::Const::is_fully_def() const
346 cover("kernel.rtlil.const.is_fully_def");
348 for (const auto &bit
: bits
)
349 if (bit
!= RTLIL::State::S0
&& bit
!= RTLIL::State::S1
)
355 bool RTLIL::Const::is_fully_undef() const
357 cover("kernel.rtlil.const.is_fully_undef");
359 for (const auto &bit
: bits
)
360 if (bit
!= RTLIL::State::Sx
&& bit
!= RTLIL::State::Sz
)
366 bool RTLIL::AttrObject::has_attribute(RTLIL::IdString id
) const
368 return attributes
.count(id
);
371 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id
, bool value
)
374 attributes
[id
] = RTLIL::Const(1);
376 attributes
.erase(id
);
379 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id
) const
381 const auto it
= attributes
.find(id
);
382 if (it
== attributes
.end())
384 return it
->second
.as_bool();
387 void RTLIL::AttrObject::set_string_attribute(RTLIL::IdString id
, string value
)
390 attributes
.erase(id
);
392 attributes
[id
] = value
;
395 string
RTLIL::AttrObject::get_string_attribute(RTLIL::IdString id
) const
398 const auto it
= attributes
.find(id
);
399 if (it
!= attributes
.end())
400 value
= it
->second
.decode_string();
404 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
407 for (const auto &s
: data
) {
408 if (!attrval
.empty())
412 set_string_attribute(id
, attrval
);
415 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
)
417 pool
<string
> union_data
= get_strpool_attribute(id
);
418 union_data
.insert(data
.begin(), data
.end());
419 if (!union_data
.empty())
420 set_strpool_attribute(id
, union_data
);
423 pool
<string
> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id
) const
426 if (attributes
.count(id
) != 0)
427 for (auto s
: split_tokens(get_string_attribute(id
), "|"))
432 void RTLIL::AttrObject::set_hdlname_attribute(const vector
<string
> &hierarchy
)
435 for (const auto &ident
: hierarchy
) {
436 if (!attrval
.empty())
440 set_string_attribute(ID::hdlname
, attrval
);
443 vector
<string
> RTLIL::AttrObject::get_hdlname_attribute() const
445 return split_tokens(get_string_attribute(ID::hdlname
), " ");
448 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
452 if (selected_modules
.count(mod_name
) > 0)
454 if (selected_members
.count(mod_name
) > 0)
459 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
463 if (selected_modules
.count(mod_name
) > 0)
468 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
472 if (selected_modules
.count(mod_name
) > 0)
474 if (selected_members
.count(mod_name
) > 0)
475 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
480 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
482 if (full_selection
) {
483 selected_modules
.clear();
484 selected_members
.clear();
488 std::vector
<RTLIL::IdString
> del_list
, add_list
;
491 for (auto mod_name
: selected_modules
) {
492 if (design
->modules_
.count(mod_name
) == 0)
493 del_list
.push_back(mod_name
);
494 selected_members
.erase(mod_name
);
496 for (auto mod_name
: del_list
)
497 selected_modules
.erase(mod_name
);
500 for (auto &it
: selected_members
)
501 if (design
->modules_
.count(it
.first
) == 0)
502 del_list
.push_back(it
.first
);
503 for (auto mod_name
: del_list
)
504 selected_members
.erase(mod_name
);
506 for (auto &it
: selected_members
) {
508 for (auto memb_name
: it
.second
)
509 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
510 del_list
.push_back(memb_name
);
511 for (auto memb_name
: del_list
)
512 it
.second
.erase(memb_name
);
517 for (auto &it
: selected_members
)
518 if (it
.second
.size() == 0)
519 del_list
.push_back(it
.first
);
520 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
521 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
522 add_list
.push_back(it
.first
);
523 for (auto mod_name
: del_list
)
524 selected_members
.erase(mod_name
);
525 for (auto mod_name
: add_list
) {
526 selected_members
.erase(mod_name
);
527 selected_modules
.insert(mod_name
);
530 if (selected_modules
.size() == design
->modules_
.size()) {
531 full_selection
= true;
532 selected_modules
.clear();
533 selected_members
.clear();
537 RTLIL::Design::Design()
538 : verilog_defines (new define_map_t
)
540 static unsigned int hashidx_count
= 123456789;
541 hashidx_count
= mkhash_xorshift(hashidx_count
);
542 hashidx_
= hashidx_count
;
544 refcount_modules_
= 0;
545 selection_stack
.push_back(RTLIL::Selection());
548 RTLIL::Design::get_all_designs()->insert(std::pair
<unsigned int, RTLIL::Design
*>(hashidx_
, this));
552 RTLIL::Design::~Design()
554 for (auto it
= modules_
.begin(); it
!= modules_
.end(); ++it
)
556 for (auto n
: verilog_packages
)
558 for (auto n
: verilog_globals
)
561 RTLIL::Design::get_all_designs()->erase(hashidx_
);
566 static std::map
<unsigned int, RTLIL::Design
*> all_designs
;
567 std::map
<unsigned int, RTLIL::Design
*> *RTLIL::Design::get_all_designs(void)
573 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
575 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
578 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
580 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
583 RTLIL::Module
*RTLIL::Design::top_module()
585 RTLIL::Module
*module
= nullptr;
586 int module_count
= 0;
588 for (auto mod
: selected_modules()) {
589 if (mod
->get_bool_attribute(ID::top
))
595 return module_count
== 1 ? module
: nullptr;
598 void RTLIL::Design::add(RTLIL::Module
*module
)
600 log_assert(modules_
.count(module
->name
) == 0);
601 log_assert(refcount_modules_
== 0);
602 modules_
[module
->name
] = module
;
603 module
->design
= this;
605 for (auto mon
: monitors
)
606 mon
->notify_module_add(module
);
609 log("#X# New Module: %s\n", log_id(module
));
610 log_backtrace("-X- ", yosys_xtrace
-1);
614 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
616 if (modules_
.count(name
) != 0)
617 log_error("Attempted to add new module named '%s', but a module by that name already exists\n", name
.c_str());
618 log_assert(refcount_modules_
== 0);
620 RTLIL::Module
*module
= new RTLIL::Module
;
621 modules_
[name
] = module
;
622 module
->design
= this;
625 for (auto mon
: monitors
)
626 mon
->notify_module_add(module
);
629 log("#X# New Module: %s\n", log_id(module
));
630 log_backtrace("-X- ", yosys_xtrace
-1);
636 void RTLIL::Design::scratchpad_unset(const std::string
&varname
)
638 scratchpad
.erase(varname
);
641 void RTLIL::Design::scratchpad_set_int(const std::string
&varname
, int value
)
643 scratchpad
[varname
] = stringf("%d", value
);
646 void RTLIL::Design::scratchpad_set_bool(const std::string
&varname
, bool value
)
648 scratchpad
[varname
] = value
? "true" : "false";
651 void RTLIL::Design::scratchpad_set_string(const std::string
&varname
, std::string value
)
653 scratchpad
[varname
] = std::move(value
);
656 int RTLIL::Design::scratchpad_get_int(const std::string
&varname
, int default_value
) const
658 auto it
= scratchpad
.find(varname
);
659 if (it
== scratchpad
.end())
660 return default_value
;
662 const std::string
&str
= it
->second
;
664 if (str
== "0" || str
== "false")
667 if (str
== "1" || str
== "true")
670 char *endptr
= nullptr;
671 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
672 return *endptr
? default_value
: parsed_value
;
675 bool RTLIL::Design::scratchpad_get_bool(const std::string
&varname
, bool default_value
) const
677 auto it
= scratchpad
.find(varname
);
678 if (it
== scratchpad
.end())
679 return default_value
;
681 const std::string
&str
= it
->second
;
683 if (str
== "0" || str
== "false")
686 if (str
== "1" || str
== "true")
689 return default_value
;
692 std::string
RTLIL::Design::scratchpad_get_string(const std::string
&varname
, const std::string
&default_value
) const
694 auto it
= scratchpad
.find(varname
);
695 if (it
== scratchpad
.end())
696 return default_value
;
701 void RTLIL::Design::remove(RTLIL::Module
*module
)
703 for (auto mon
: monitors
)
704 mon
->notify_module_del(module
);
707 log("#X# Remove Module: %s\n", log_id(module
));
708 log_backtrace("-X- ", yosys_xtrace
-1);
711 log_assert(modules_
.at(module
->name
) == module
);
712 log_assert(refcount_modules_
== 0);
713 modules_
.erase(module
->name
);
717 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
719 modules_
.erase(module
->name
);
720 module
->name
= new_name
;
724 void RTLIL::Design::sort()
727 modules_
.sort(sort_by_id_str());
728 for (auto &it
: modules_
)
732 void RTLIL::Design::check()
735 for (auto &it
: modules_
) {
736 log_assert(this == it
.second
->design
);
737 log_assert(it
.first
== it
.second
->name
);
738 log_assert(!it
.first
.empty());
744 void RTLIL::Design::optimize()
746 for (auto &it
: modules_
)
747 it
.second
->optimize();
748 for (auto &it
: selection_stack
)
750 for (auto &it
: selection_vars
)
751 it
.second
.optimize(this);
754 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
756 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
758 if (selection_stack
.size() == 0)
760 return selection_stack
.back().selected_module(mod_name
);
763 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
765 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
767 if (selection_stack
.size() == 0)
769 return selection_stack
.back().selected_whole_module(mod_name
);
772 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
774 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
776 if (selection_stack
.size() == 0)
778 return selection_stack
.back().selected_member(mod_name
, memb_name
);
781 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
783 return selected_module(mod
->name
);
786 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
788 return selected_whole_module(mod
->name
);
791 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
793 std::vector
<RTLIL::Module
*> result
;
794 result
.reserve(modules_
.size());
795 for (auto &it
: modules_
)
796 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
797 result
.push_back(it
.second
);
801 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
803 std::vector
<RTLIL::Module
*> result
;
804 result
.reserve(modules_
.size());
805 for (auto &it
: modules_
)
806 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
807 result
.push_back(it
.second
);
811 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn(bool include_wb
) const
813 std::vector
<RTLIL::Module
*> result
;
814 result
.reserve(modules_
.size());
815 for (auto &it
: modules_
)
816 if (it
.second
->get_blackbox_attribute(include_wb
))
818 else if (selected_whole_module(it
.first
))
819 result
.push_back(it
.second
);
820 else if (selected_module(it
.first
))
821 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
825 RTLIL::Module::Module()
827 static unsigned int hashidx_count
= 123456789;
828 hashidx_count
= mkhash_xorshift(hashidx_count
);
829 hashidx_
= hashidx_count
;
836 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
840 RTLIL::Module::~Module()
842 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
844 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
846 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
848 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
851 RTLIL::Module::get_all_modules()->erase(hashidx_
);
856 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
857 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
863 void RTLIL::Module::makeblackbox()
865 pool
<RTLIL::Wire
*> delwires
;
867 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
868 if (!it
->second
->port_input
&& !it
->second
->port_output
)
869 delwires
.insert(it
->second
);
871 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
875 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
879 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
883 connections_
.clear();
886 set_bool_attribute(ID::blackbox
);
889 void RTLIL::Module::reprocess_module(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Module
*> &)
891 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
894 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, bool mayfail
)
897 return RTLIL::IdString();
898 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
902 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
)
905 return RTLIL::IdString();
906 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
909 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
911 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
916 struct InternalCellChecker
918 RTLIL::Module
*module
;
920 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
922 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
924 void error(int linenr
)
926 std::stringstream buf
;
927 RTLIL_BACKEND::dump_cell(buf
, " ", cell
);
929 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
930 module
? module
->name
.c_str() : "", module
? "." : "",
931 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
934 int param(RTLIL::IdString name
)
936 auto it
= cell
->parameters
.find(name
);
937 if (it
== cell
->parameters
.end())
939 expected_params
.insert(name
);
940 return it
->second
.as_int();
943 int param_bool(RTLIL::IdString name
)
946 if (GetSize(cell
->parameters
.at(name
)) > 32)
948 if (v
!= 0 && v
!= 1)
953 int param_bool(RTLIL::IdString name
, bool expected
)
955 int v
= param_bool(name
);
961 void param_bits(RTLIL::IdString name
, int width
)
964 if (GetSize(cell
->parameters
.at(name
).bits
) != width
)
968 void port(RTLIL::IdString name
, int width
)
970 auto it
= cell
->connections_
.find(name
);
971 if (it
== cell
->connections_
.end())
973 if (GetSize(it
->second
) != width
)
975 expected_ports
.insert(name
);
978 void check_expected(bool check_matched_sign
= false)
980 for (auto ¶
: cell
->parameters
)
981 if (expected_params
.count(para
.first
) == 0)
983 for (auto &conn
: cell
->connections())
984 if (expected_ports
.count(conn
.first
) == 0)
987 if (check_matched_sign
) {
988 log_assert(expected_params
.count(ID::A_SIGNED
) != 0 && expected_params
.count(ID::B_SIGNED
) != 0);
989 bool a_is_signed
= cell
->parameters
.at(ID::A_SIGNED
).as_bool();
990 bool b_is_signed
= cell
->parameters
.at(ID::B_SIGNED
).as_bool();
991 if (a_is_signed
!= b_is_signed
)
998 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
999 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
1002 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
1003 param_bool(ID::A_SIGNED
);
1004 port(ID::A
, param(ID::A_WIDTH
));
1005 port(ID::Y
, param(ID::Y_WIDTH
));
1010 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
1011 param_bool(ID::A_SIGNED
);
1012 param_bool(ID::B_SIGNED
);
1013 port(ID::A
, param(ID::A_WIDTH
));
1014 port(ID::B
, param(ID::B_WIDTH
));
1015 port(ID::Y
, param(ID::Y_WIDTH
));
1016 check_expected(true);
1020 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
1021 param_bool(ID::A_SIGNED
);
1022 port(ID::A
, param(ID::A_WIDTH
));
1023 port(ID::Y
, param(ID::Y_WIDTH
));
1028 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
1029 param_bool(ID::A_SIGNED
);
1030 param_bool(ID::B_SIGNED
, /*expected=*/false);
1031 port(ID::A
, param(ID::A_WIDTH
));
1032 port(ID::B
, param(ID::B_WIDTH
));
1033 port(ID::Y
, param(ID::Y_WIDTH
));
1034 check_expected(/*check_matched_sign=*/false);
1038 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
1039 if (cell
->type
== ID($shiftx
)) {
1040 param_bool(ID::A_SIGNED
, /*expected=*/false);
1042 param_bool(ID::A_SIGNED
);
1044 param_bool(ID::B_SIGNED
);
1045 port(ID::A
, param(ID::A_WIDTH
));
1046 port(ID::B
, param(ID::B_WIDTH
));
1047 port(ID::Y
, param(ID::Y_WIDTH
));
1048 check_expected(/*check_matched_sign=*/false);
1052 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
1053 param_bool(ID::A_SIGNED
);
1054 param_bool(ID::B_SIGNED
);
1055 port(ID::A
, param(ID::A_WIDTH
));
1056 port(ID::B
, param(ID::B_WIDTH
));
1057 port(ID::Y
, param(ID::Y_WIDTH
));
1058 check_expected(true);
1062 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($divfloor
), ID($modfloor
), ID($pow
))) {
1063 param_bool(ID::A_SIGNED
);
1064 param_bool(ID::B_SIGNED
);
1065 port(ID::A
, param(ID::A_WIDTH
));
1066 port(ID::B
, param(ID::B_WIDTH
));
1067 port(ID::Y
, param(ID::Y_WIDTH
));
1068 check_expected(cell
->type
!= ID($pow
));
1072 if (cell
->type
== ID($fa
)) {
1073 port(ID::A
, param(ID::WIDTH
));
1074 port(ID::B
, param(ID::WIDTH
));
1075 port(ID::C
, param(ID::WIDTH
));
1076 port(ID::X
, param(ID::WIDTH
));
1077 port(ID::Y
, param(ID::WIDTH
));
1082 if (cell
->type
== ID($lcu
)) {
1083 port(ID::P
, param(ID::WIDTH
));
1084 port(ID::G
, param(ID::WIDTH
));
1086 port(ID::CO
, param(ID::WIDTH
));
1091 if (cell
->type
== ID($alu
)) {
1092 param_bool(ID::A_SIGNED
);
1093 param_bool(ID::B_SIGNED
);
1094 port(ID::A
, param(ID::A_WIDTH
));
1095 port(ID::B
, param(ID::B_WIDTH
));
1098 port(ID::X
, param(ID::Y_WIDTH
));
1099 port(ID::Y
, param(ID::Y_WIDTH
));
1100 port(ID::CO
, param(ID::Y_WIDTH
));
1101 check_expected(true);
1105 if (cell
->type
== ID($macc
)) {
1107 param(ID::CONFIG_WIDTH
);
1108 port(ID::A
, param(ID::A_WIDTH
));
1109 port(ID::B
, param(ID::B_WIDTH
));
1110 port(ID::Y
, param(ID::Y_WIDTH
));
1112 Macc().from_cell(cell
);
1116 if (cell
->type
== ID($logic_not
)) {
1117 param_bool(ID::A_SIGNED
);
1118 port(ID::A
, param(ID::A_WIDTH
));
1119 port(ID::Y
, param(ID::Y_WIDTH
));
1124 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
1125 param_bool(ID::A_SIGNED
);
1126 param_bool(ID::B_SIGNED
);
1127 port(ID::A
, param(ID::A_WIDTH
));
1128 port(ID::B
, param(ID::B_WIDTH
));
1129 port(ID::Y
, param(ID::Y_WIDTH
));
1130 check_expected(/*check_matched_sign=*/false);
1134 if (cell
->type
== ID($slice
)) {
1136 port(ID::A
, param(ID::A_WIDTH
));
1137 port(ID::Y
, param(ID::Y_WIDTH
));
1138 if (param(ID::OFFSET
) + param(ID::Y_WIDTH
) > param(ID::A_WIDTH
))
1144 if (cell
->type
== ID($concat
)) {
1145 port(ID::A
, param(ID::A_WIDTH
));
1146 port(ID::B
, param(ID::B_WIDTH
));
1147 port(ID::Y
, param(ID::A_WIDTH
) + param(ID::B_WIDTH
));
1152 if (cell
->type
== ID($mux
)) {
1153 port(ID::A
, param(ID::WIDTH
));
1154 port(ID::B
, param(ID::WIDTH
));
1156 port(ID::Y
, param(ID::WIDTH
));
1161 if (cell
->type
== ID($pmux
)) {
1162 port(ID::A
, param(ID::WIDTH
));
1163 port(ID::B
, param(ID::WIDTH
) * param(ID::S_WIDTH
));
1164 port(ID::S
, param(ID::S_WIDTH
));
1165 port(ID::Y
, param(ID::WIDTH
));
1170 if (cell
->type
== ID($lut
)) {
1172 port(ID::A
, param(ID::WIDTH
));
1178 if (cell
->type
== ID($sop
)) {
1181 port(ID::A
, param(ID::WIDTH
));
1187 if (cell
->type
== ID($sr
)) {
1188 param_bool(ID::SET_POLARITY
);
1189 param_bool(ID::CLR_POLARITY
);
1190 port(ID::SET
, param(ID::WIDTH
));
1191 port(ID::CLR
, param(ID::WIDTH
));
1192 port(ID::Q
, param(ID::WIDTH
));
1197 if (cell
->type
== ID($ff
)) {
1198 port(ID::D
, param(ID::WIDTH
));
1199 port(ID::Q
, param(ID::WIDTH
));
1204 if (cell
->type
== ID($dff
)) {
1205 param_bool(ID::CLK_POLARITY
);
1207 port(ID::D
, param(ID::WIDTH
));
1208 port(ID::Q
, param(ID::WIDTH
));
1213 if (cell
->type
== ID($dffe
)) {
1214 param_bool(ID::CLK_POLARITY
);
1215 param_bool(ID::EN_POLARITY
);
1218 port(ID::D
, param(ID::WIDTH
));
1219 port(ID::Q
, param(ID::WIDTH
));
1224 if (cell
->type
== ID($dffsr
)) {
1225 param_bool(ID::CLK_POLARITY
);
1226 param_bool(ID::SET_POLARITY
);
1227 param_bool(ID::CLR_POLARITY
);
1229 port(ID::SET
, param(ID::WIDTH
));
1230 port(ID::CLR
, param(ID::WIDTH
));
1231 port(ID::D
, param(ID::WIDTH
));
1232 port(ID::Q
, param(ID::WIDTH
));
1237 if (cell
->type
== ID($dffsre
)) {
1238 param_bool(ID::CLK_POLARITY
);
1239 param_bool(ID::SET_POLARITY
);
1240 param_bool(ID::CLR_POLARITY
);
1241 param_bool(ID::EN_POLARITY
);
1244 port(ID::SET
, param(ID::WIDTH
));
1245 port(ID::CLR
, param(ID::WIDTH
));
1246 port(ID::D
, param(ID::WIDTH
));
1247 port(ID::Q
, param(ID::WIDTH
));
1252 if (cell
->type
== ID($adff
)) {
1253 param_bool(ID::CLK_POLARITY
);
1254 param_bool(ID::ARST_POLARITY
);
1255 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1258 port(ID::D
, param(ID::WIDTH
));
1259 port(ID::Q
, param(ID::WIDTH
));
1264 if (cell
->type
== ID($sdff
)) {
1265 param_bool(ID::CLK_POLARITY
);
1266 param_bool(ID::SRST_POLARITY
);
1267 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1270 port(ID::D
, param(ID::WIDTH
));
1271 port(ID::Q
, param(ID::WIDTH
));
1276 if (cell
->type
.in(ID($sdffe
), ID($sdffce
))) {
1277 param_bool(ID::CLK_POLARITY
);
1278 param_bool(ID::EN_POLARITY
);
1279 param_bool(ID::SRST_POLARITY
);
1280 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1284 port(ID::D
, param(ID::WIDTH
));
1285 port(ID::Q
, param(ID::WIDTH
));
1290 if (cell
->type
== ID($adffe
)) {
1291 param_bool(ID::CLK_POLARITY
);
1292 param_bool(ID::EN_POLARITY
);
1293 param_bool(ID::ARST_POLARITY
);
1294 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1298 port(ID::D
, param(ID::WIDTH
));
1299 port(ID::Q
, param(ID::WIDTH
));
1304 if (cell
->type
== ID($dlatch
)) {
1305 param_bool(ID::EN_POLARITY
);
1307 port(ID::D
, param(ID::WIDTH
));
1308 port(ID::Q
, param(ID::WIDTH
));
1313 if (cell
->type
== ID($adlatch
)) {
1314 param_bool(ID::EN_POLARITY
);
1315 param_bool(ID::ARST_POLARITY
);
1316 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1319 port(ID::D
, param(ID::WIDTH
));
1320 port(ID::Q
, param(ID::WIDTH
));
1325 if (cell
->type
== ID($dlatchsr
)) {
1326 param_bool(ID::EN_POLARITY
);
1327 param_bool(ID::SET_POLARITY
);
1328 param_bool(ID::CLR_POLARITY
);
1330 port(ID::SET
, param(ID::WIDTH
));
1331 port(ID::CLR
, param(ID::WIDTH
));
1332 port(ID::D
, param(ID::WIDTH
));
1333 port(ID::Q
, param(ID::WIDTH
));
1338 if (cell
->type
== ID($fsm
)) {
1340 param_bool(ID::CLK_POLARITY
);
1341 param_bool(ID::ARST_POLARITY
);
1342 param(ID::STATE_BITS
);
1343 param(ID::STATE_NUM
);
1344 param(ID::STATE_NUM_LOG2
);
1345 param(ID::STATE_RST
);
1346 param_bits(ID::STATE_TABLE
, param(ID::STATE_BITS
) * param(ID::STATE_NUM
));
1347 param(ID::TRANS_NUM
);
1348 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
)));
1351 port(ID::CTRL_IN
, param(ID::CTRL_IN_WIDTH
));
1352 port(ID::CTRL_OUT
, param(ID::CTRL_OUT_WIDTH
));
1357 if (cell
->type
== ID($memrd
)) {
1359 param_bool(ID::CLK_ENABLE
);
1360 param_bool(ID::CLK_POLARITY
);
1361 param_bool(ID::TRANSPARENT
);
1364 port(ID::ADDR
, param(ID::ABITS
));
1365 port(ID::DATA
, param(ID::WIDTH
));
1370 if (cell
->type
== ID($memwr
)) {
1372 param_bool(ID::CLK_ENABLE
);
1373 param_bool(ID::CLK_POLARITY
);
1374 param(ID::PRIORITY
);
1376 port(ID::EN
, param(ID::WIDTH
));
1377 port(ID::ADDR
, param(ID::ABITS
));
1378 port(ID::DATA
, param(ID::WIDTH
));
1383 if (cell
->type
== ID($meminit
)) {
1385 param(ID::PRIORITY
);
1386 port(ID::ADDR
, param(ID::ABITS
));
1387 port(ID::DATA
, param(ID::WIDTH
) * param(ID::WORDS
));
1392 if (cell
->type
== ID($mem
)) {
1397 param_bits(ID::RD_CLK_ENABLE
, max(1, param(ID::RD_PORTS
)));
1398 param_bits(ID::RD_CLK_POLARITY
, max(1, param(ID::RD_PORTS
)));
1399 param_bits(ID::RD_TRANSPARENT
, max(1, param(ID::RD_PORTS
)));
1400 param_bits(ID::WR_CLK_ENABLE
, max(1, param(ID::WR_PORTS
)));
1401 param_bits(ID::WR_CLK_POLARITY
, max(1, param(ID::WR_PORTS
)));
1402 port(ID::RD_CLK
, param(ID::RD_PORTS
));
1403 port(ID::RD_EN
, param(ID::RD_PORTS
));
1404 port(ID::RD_ADDR
, param(ID::RD_PORTS
) * param(ID::ABITS
));
1405 port(ID::RD_DATA
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1406 port(ID::WR_CLK
, param(ID::WR_PORTS
));
1407 port(ID::WR_EN
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1408 port(ID::WR_ADDR
, param(ID::WR_PORTS
) * param(ID::ABITS
));
1409 port(ID::WR_DATA
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1414 if (cell
->type
== ID($tribuf
)) {
1415 port(ID::A
, param(ID::WIDTH
));
1416 port(ID::Y
, param(ID::WIDTH
));
1422 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1429 if (cell
->type
== ID($initstate
)) {
1435 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1436 port(ID::Y
, param(ID::WIDTH
));
1441 if (cell
->type
== ID($equiv
)) {
1449 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1450 param_bool(ID::FULL
);
1451 param_bool(ID::SRC_DST_PEN
);
1452 param_bool(ID::SRC_DST_POL
);
1453 param(ID::T_RISE_MIN
);
1454 param(ID::T_RISE_TYP
);
1455 param(ID::T_RISE_MAX
);
1456 param(ID::T_FALL_MIN
);
1457 param(ID::T_FALL_TYP
);
1458 param(ID::T_FALL_MAX
);
1460 port(ID::SRC
, param(ID::SRC_WIDTH
));
1461 port(ID::DST
, param(ID::DST_WIDTH
));
1462 if (cell
->type
== ID($specify3
)) {
1463 param_bool(ID::EDGE_EN
);
1464 param_bool(ID::EDGE_POL
);
1465 param_bool(ID::DAT_DST_PEN
);
1466 param_bool(ID::DAT_DST_POL
);
1467 port(ID::DAT
, param(ID::DST_WIDTH
));
1473 if (cell
->type
== ID($specrule
)) {
1475 param_bool(ID::SRC_PEN
);
1476 param_bool(ID::SRC_POL
);
1477 param_bool(ID::DST_PEN
);
1478 param_bool(ID::DST_POL
);
1479 param(ID::T_LIMIT_MIN
);
1480 param(ID::T_LIMIT_TYP
);
1481 param(ID::T_LIMIT_MAX
);
1482 param(ID::T_LIMIT2_MIN
);
1483 param(ID::T_LIMIT2_TYP
);
1484 param(ID::T_LIMIT2_MAX
);
1485 port(ID::SRC_EN
, 1);
1486 port(ID::DST_EN
, 1);
1487 port(ID::SRC
, param(ID::SRC_WIDTH
));
1488 port(ID::DST
, param(ID::DST_WIDTH
));
1493 if (cell
->type
== ID($_BUF_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1494 if (cell
->type
== ID($_NOT_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1495 if (cell
->type
== ID($_AND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1496 if (cell
->type
== ID($_NAND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1497 if (cell
->type
== ID($_OR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1498 if (cell
->type
== ID($_NOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1499 if (cell
->type
== ID($_XOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1500 if (cell
->type
== ID($_XNOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1501 if (cell
->type
== ID($_ANDNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1502 if (cell
->type
== ID($_ORNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1503 if (cell
->type
== ID($_MUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1504 if (cell
->type
== ID($_NMUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1505 if (cell
->type
== ID($_AOI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1506 if (cell
->type
== ID($_OAI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1507 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; }
1508 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; }
1510 if (cell
->type
== ID($_TBUF_
)) { port(ID::A
,1); port(ID::Y
,1); port(ID::E
,1); check_expected(); return; }
1512 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; }
1513 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; }
1514 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; }
1516 if (cell
->type
.in(ID($_SR_NN_
), ID($_SR_NP_
), ID($_SR_PN_
), ID($_SR_PP_
)))
1517 { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1519 if (cell
->type
== ID($_FF_
)) { port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1521 if (cell
->type
.in(ID($_DFF_N_
), ID($_DFF_P_
)))
1522 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); check_expected(); return; }
1524 if (cell
->type
.in(ID($_DFFE_NN_
), ID($_DFFE_NP_
), ID($_DFFE_PN_
), ID($_DFFE_PP_
)))
1525 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1528 ID($_DFF_NN0_
), ID($_DFF_NN1_
), ID($_DFF_NP0_
), ID($_DFF_NP1_
),
1529 ID($_DFF_PN0_
), ID($_DFF_PN1_
), ID($_DFF_PP0_
), ID($_DFF_PP1_
)))
1530 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1533 ID($_DFFE_NN0N_
), ID($_DFFE_NN0P_
), ID($_DFFE_NN1N_
), ID($_DFFE_NN1P_
),
1534 ID($_DFFE_NP0N_
), ID($_DFFE_NP0P_
), ID($_DFFE_NP1N_
), ID($_DFFE_NP1P_
),
1535 ID($_DFFE_PN0N_
), ID($_DFFE_PN0P_
), ID($_DFFE_PN1N_
), ID($_DFFE_PN1P_
),
1536 ID($_DFFE_PP0N_
), ID($_DFFE_PP0P_
), ID($_DFFE_PP1N_
), ID($_DFFE_PP1P_
)))
1537 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1540 ID($_DFFSR_NNN_
), ID($_DFFSR_NNP_
), ID($_DFFSR_NPN_
), ID($_DFFSR_NPP_
),
1541 ID($_DFFSR_PNN_
), ID($_DFFSR_PNP_
), ID($_DFFSR_PPN_
), ID($_DFFSR_PPP_
)))
1542 { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1545 ID($_DFFSRE_NNNN_
), ID($_DFFSRE_NNNP_
), ID($_DFFSRE_NNPN_
), ID($_DFFSRE_NNPP_
),
1546 ID($_DFFSRE_NPNN_
), ID($_DFFSRE_NPNP_
), ID($_DFFSRE_NPPN_
), ID($_DFFSRE_NPPP_
),
1547 ID($_DFFSRE_PNNN_
), ID($_DFFSRE_PNNP_
), ID($_DFFSRE_PNPN_
), ID($_DFFSRE_PNPP_
),
1548 ID($_DFFSRE_PPNN_
), ID($_DFFSRE_PPNP_
), ID($_DFFSRE_PPPN_
), ID($_DFFSRE_PPPP_
)))
1549 { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::E
,1); port(ID::Q
,1); check_expected(); return; }
1552 ID($_SDFF_NN0_
), ID($_SDFF_NN1_
), ID($_SDFF_NP0_
), ID($_SDFF_NP1_
),
1553 ID($_SDFF_PN0_
), ID($_SDFF_PN1_
), ID($_SDFF_PP0_
), ID($_SDFF_PP1_
)))
1554 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1557 ID($_SDFFE_NN0N_
), ID($_SDFFE_NN0P_
), ID($_SDFFE_NN1N_
), ID($_SDFFE_NN1P_
),
1558 ID($_SDFFE_NP0N_
), ID($_SDFFE_NP0P_
), ID($_SDFFE_NP1N_
), ID($_SDFFE_NP1P_
),
1559 ID($_SDFFE_PN0N_
), ID($_SDFFE_PN0P_
), ID($_SDFFE_PN1N_
), ID($_SDFFE_PN1P_
),
1560 ID($_SDFFE_PP0N_
), ID($_SDFFE_PP0P_
), ID($_SDFFE_PP1N_
), ID($_SDFFE_PP1P_
),
1561 ID($_SDFFCE_NN0N_
), ID($_SDFFCE_NN0P_
), ID($_SDFFCE_NN1N_
), ID($_SDFFCE_NN1P_
),
1562 ID($_SDFFCE_NP0N_
), ID($_SDFFCE_NP0P_
), ID($_SDFFCE_NP1N_
), ID($_SDFFCE_NP1P_
),
1563 ID($_SDFFCE_PN0N_
), ID($_SDFFCE_PN0P_
), ID($_SDFFCE_PN1N_
), ID($_SDFFCE_PN1P_
),
1564 ID($_SDFFCE_PP0N_
), ID($_SDFFCE_PP0P_
), ID($_SDFFCE_PP1N_
), ID($_SDFFCE_PP1P_
)))
1565 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1567 if (cell
->type
.in(ID($_DLATCH_N_
), ID($_DLATCH_P_
)))
1568 { port(ID::E
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1571 ID($_DLATCH_NN0_
), ID($_DLATCH_NN1_
), ID($_DLATCH_NP0_
), ID($_DLATCH_NP1_
),
1572 ID($_DLATCH_PN0_
), ID($_DLATCH_PN1_
), ID($_DLATCH_PP0_
), ID($_DLATCH_PP1_
)))
1573 { port(ID::E
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1576 ID($_DLATCHSR_NNN_
), ID($_DLATCHSR_NNP_
), ID($_DLATCHSR_NPN_
), ID($_DLATCHSR_NPP_
),
1577 ID($_DLATCHSR_PNN_
), ID($_DLATCHSR_PNP_
), ID($_DLATCHSR_PPN_
), ID($_DLATCHSR_PPP_
)))
1578 { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1586 void RTLIL::Module::sort()
1588 wires_
.sort(sort_by_id_str());
1589 cells_
.sort(sort_by_id_str());
1590 parameter_default_values
.sort(sort_by_id_str());
1591 memories
.sort(sort_by_id_str());
1592 processes
.sort(sort_by_id_str());
1593 for (auto &it
: cells_
)
1595 for (auto &it
: wires_
)
1596 it
.second
->attributes
.sort(sort_by_id_str());
1597 for (auto &it
: memories
)
1598 it
.second
->attributes
.sort(sort_by_id_str());
1601 void RTLIL::Module::check()
1604 std::vector
<bool> ports_declared
;
1605 for (auto &it
: wires_
) {
1606 log_assert(this == it
.second
->module
);
1607 log_assert(it
.first
== it
.second
->name
);
1608 log_assert(!it
.first
.empty());
1609 log_assert(it
.second
->width
>= 0);
1610 log_assert(it
.second
->port_id
>= 0);
1611 for (auto &it2
: it
.second
->attributes
)
1612 log_assert(!it2
.first
.empty());
1613 if (it
.second
->port_id
) {
1614 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1615 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1616 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1617 if (GetSize(ports_declared
) < it
.second
->port_id
)
1618 ports_declared
.resize(it
.second
->port_id
);
1619 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1620 ports_declared
[it
.second
->port_id
-1] = true;
1622 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1624 for (auto port_declared
: ports_declared
)
1625 log_assert(port_declared
== true);
1626 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1628 for (auto &it
: memories
) {
1629 log_assert(it
.first
== it
.second
->name
);
1630 log_assert(!it
.first
.empty());
1631 log_assert(it
.second
->width
>= 0);
1632 log_assert(it
.second
->size
>= 0);
1633 for (auto &it2
: it
.second
->attributes
)
1634 log_assert(!it2
.first
.empty());
1637 for (auto &it
: cells_
) {
1638 log_assert(this == it
.second
->module
);
1639 log_assert(it
.first
== it
.second
->name
);
1640 log_assert(!it
.first
.empty());
1641 log_assert(!it
.second
->type
.empty());
1642 for (auto &it2
: it
.second
->connections()) {
1643 log_assert(!it2
.first
.empty());
1646 for (auto &it2
: it
.second
->attributes
)
1647 log_assert(!it2
.first
.empty());
1648 for (auto &it2
: it
.second
->parameters
)
1649 log_assert(!it2
.first
.empty());
1650 InternalCellChecker
checker(this, it
.second
);
1654 for (auto &it
: processes
) {
1655 log_assert(it
.first
== it
.second
->name
);
1656 log_assert(!it
.first
.empty());
1657 log_assert(it
.second
->root_case
.compare
.empty());
1658 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1659 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1660 for (auto &switch_it
: all_cases
[i
]->switches
) {
1661 for (auto &case_it
: switch_it
->cases
) {
1662 for (auto &compare_it
: case_it
->compare
) {
1663 log_assert(switch_it
->signal
.size() == compare_it
.size());
1665 all_cases
.push_back(case_it
);
1669 for (auto &sync_it
: it
.second
->syncs
) {
1670 switch (sync_it
->type
) {
1676 log_assert(!sync_it
->signal
.empty());
1681 log_assert(sync_it
->signal
.empty());
1687 for (auto &it
: connections_
) {
1688 log_assert(it
.first
.size() == it
.second
.size());
1689 log_assert(!it
.first
.has_const());
1694 for (auto &it
: attributes
)
1695 log_assert(!it
.first
.empty());
1699 void RTLIL::Module::optimize()
1703 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1705 log_assert(new_mod
->refcount_wires_
== 0);
1706 log_assert(new_mod
->refcount_cells_
== 0);
1708 new_mod
->avail_parameters
= avail_parameters
;
1709 new_mod
->parameter_default_values
= parameter_default_values
;
1711 for (auto &conn
: connections_
)
1712 new_mod
->connect(conn
);
1714 for (auto &attr
: attributes
)
1715 new_mod
->attributes
[attr
.first
] = attr
.second
;
1717 for (auto &it
: wires_
)
1718 new_mod
->addWire(it
.first
, it
.second
);
1720 for (auto &it
: memories
)
1721 new_mod
->addMemory(it
.first
, it
.second
);
1723 for (auto &it
: cells_
)
1724 new_mod
->addCell(it
.first
, it
.second
);
1726 for (auto &it
: processes
)
1727 new_mod
->addProcess(it
.first
, it
.second
);
1729 struct RewriteSigSpecWorker
1732 void operator()(RTLIL::SigSpec
&sig
)
1735 for (auto &c
: sig
.chunks_
)
1737 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1741 RewriteSigSpecWorker rewriteSigSpecWorker
;
1742 rewriteSigSpecWorker
.mod
= new_mod
;
1743 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1744 new_mod
->fixup_ports();
1747 RTLIL::Module
*RTLIL::Module::clone() const
1749 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1750 new_mod
->name
= name
;
1755 bool RTLIL::Module::has_memories() const
1757 return !memories
.empty();
1760 bool RTLIL::Module::has_processes() const
1762 return !processes
.empty();
1765 bool RTLIL::Module::has_memories_warn() const
1767 if (!memories
.empty())
1768 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1769 return !memories
.empty();
1772 bool RTLIL::Module::has_processes_warn() const
1774 if (!processes
.empty())
1775 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1776 return !processes
.empty();
1779 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1781 std::vector
<RTLIL::Wire
*> result
;
1782 result
.reserve(wires_
.size());
1783 for (auto &it
: wires_
)
1784 if (design
->selected(this, it
.second
))
1785 result
.push_back(it
.second
);
1789 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1791 std::vector
<RTLIL::Cell
*> result
;
1792 result
.reserve(cells_
.size());
1793 for (auto &it
: cells_
)
1794 if (design
->selected(this, it
.second
))
1795 result
.push_back(it
.second
);
1799 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1801 log_assert(!wire
->name
.empty());
1802 log_assert(count_id(wire
->name
) == 0);
1803 log_assert(refcount_wires_
== 0);
1804 wires_
[wire
->name
] = wire
;
1805 wire
->module
= this;
1808 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1810 log_assert(!cell
->name
.empty());
1811 log_assert(count_id(cell
->name
) == 0);
1812 log_assert(refcount_cells_
== 0);
1813 cells_
[cell
->name
] = cell
;
1814 cell
->module
= this;
1817 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1819 log_assert(refcount_wires_
== 0);
1821 struct DeleteWireWorker
1823 RTLIL::Module
*module
;
1824 const pool
<RTLIL::Wire
*> *wires_p
;
1826 void operator()(RTLIL::SigSpec
&sig
) {
1828 for (auto &c
: sig
.chunks_
)
1829 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1830 c
.wire
= module
->addWire(stringf("$delete_wire$%d", autoidx
++), c
.width
);
1835 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1836 log_assert(GetSize(lhs
) == GetSize(rhs
));
1839 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1840 RTLIL::SigBit
&lhs_bit
= lhs
.bits_
[i
];
1841 RTLIL::SigBit
&rhs_bit
= rhs
.bits_
[i
];
1842 if ((lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
)) || (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))) {
1843 lhs_bit
= State::Sx
;
1844 rhs_bit
= State::Sx
;
1850 DeleteWireWorker delete_wire_worker
;
1851 delete_wire_worker
.module
= this;
1852 delete_wire_worker
.wires_p
= &wires
;
1853 rewrite_sigspecs2(delete_wire_worker
);
1855 for (auto &it
: wires
) {
1856 log_assert(wires_
.count(it
->name
) != 0);
1857 wires_
.erase(it
->name
);
1862 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1864 while (!cell
->connections_
.empty())
1865 cell
->unsetPort(cell
->connections_
.begin()->first
);
1867 log_assert(cells_
.count(cell
->name
) != 0);
1868 log_assert(refcount_cells_
== 0);
1869 cells_
.erase(cell
->name
);
1873 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1875 log_assert(wires_
[wire
->name
] == wire
);
1876 log_assert(refcount_wires_
== 0);
1877 wires_
.erase(wire
->name
);
1878 wire
->name
= new_name
;
1882 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1884 log_assert(cells_
[cell
->name
] == cell
);
1885 log_assert(refcount_wires_
== 0);
1886 cells_
.erase(cell
->name
);
1887 cell
->name
= new_name
;
1891 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1893 log_assert(count_id(old_name
) != 0);
1894 if (wires_
.count(old_name
))
1895 rename(wires_
.at(old_name
), new_name
);
1896 else if (cells_
.count(old_name
))
1897 rename(cells_
.at(old_name
), new_name
);
1902 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1904 log_assert(wires_
[w1
->name
] == w1
);
1905 log_assert(wires_
[w2
->name
] == w2
);
1906 log_assert(refcount_wires_
== 0);
1908 wires_
.erase(w1
->name
);
1909 wires_
.erase(w2
->name
);
1911 std::swap(w1
->name
, w2
->name
);
1913 wires_
[w1
->name
] = w1
;
1914 wires_
[w2
->name
] = w2
;
1917 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1919 log_assert(cells_
[c1
->name
] == c1
);
1920 log_assert(cells_
[c2
->name
] == c2
);
1921 log_assert(refcount_cells_
== 0);
1923 cells_
.erase(c1
->name
);
1924 cells_
.erase(c2
->name
);
1926 std::swap(c1
->name
, c2
->name
);
1928 cells_
[c1
->name
] = c1
;
1929 cells_
[c2
->name
] = c2
;
1932 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1935 return uniquify(name
, index
);
1938 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1941 if (count_id(name
) == 0)
1947 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1948 if (count_id(new_name
) == 0)
1954 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1956 if (a
->port_id
&& !b
->port_id
)
1958 if (!a
->port_id
&& b
->port_id
)
1961 if (a
->port_id
== b
->port_id
)
1962 return a
->name
< b
->name
;
1963 return a
->port_id
< b
->port_id
;
1966 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1968 for (auto mon
: monitors
)
1969 mon
->notify_connect(this, conn
);
1972 for (auto mon
: design
->monitors
)
1973 mon
->notify_connect(this, conn
);
1975 // ignore all attempts to assign constants to other constants
1976 if (conn
.first
.has_const()) {
1977 RTLIL::SigSig new_conn
;
1978 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1979 if (conn
.first
[i
].wire
) {
1980 new_conn
.first
.append(conn
.first
[i
]);
1981 new_conn
.second
.append(conn
.second
[i
]);
1983 if (GetSize(new_conn
.first
))
1989 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
));
1990 log_backtrace("-X- ", yosys_xtrace
-1);
1993 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1994 connections_
.push_back(conn
);
1997 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1999 connect(RTLIL::SigSig(lhs
, rhs
));
2002 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
2004 for (auto mon
: monitors
)
2005 mon
->notify_connect(this, new_conn
);
2008 for (auto mon
: design
->monitors
)
2009 mon
->notify_connect(this, new_conn
);
2012 log("#X# New connections vector in %s:\n", log_id(this));
2013 for (auto &conn
: new_conn
)
2014 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
2015 log_backtrace("-X- ", yosys_xtrace
-1);
2018 connections_
= new_conn
;
2021 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
2023 return connections_
;
2026 void RTLIL::Module::fixup_ports()
2028 std::vector
<RTLIL::Wire
*> all_ports
;
2030 for (auto &w
: wires_
)
2031 if (w
.second
->port_input
|| w
.second
->port_output
)
2032 all_ports
.push_back(w
.second
);
2034 w
.second
->port_id
= 0;
2036 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
2039 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
2040 ports
.push_back(all_ports
[i
]->name
);
2041 all_ports
[i
]->port_id
= i
+1;
2045 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
2047 RTLIL::Wire
*wire
= new RTLIL::Wire
;
2049 wire
->width
= width
;
2054 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
2056 RTLIL::Wire
*wire
= addWire(name
);
2057 wire
->width
= other
->width
;
2058 wire
->start_offset
= other
->start_offset
;
2059 wire
->port_id
= other
->port_id
;
2060 wire
->port_input
= other
->port_input
;
2061 wire
->port_output
= other
->port_output
;
2062 wire
->upto
= other
->upto
;
2063 wire
->is_signed
= other
->is_signed
;
2064 wire
->attributes
= other
->attributes
;
2068 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
2070 RTLIL::Cell
*cell
= new RTLIL::Cell
;
2077 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
2079 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
2080 cell
->connections_
= other
->connections_
;
2081 cell
->parameters
= other
->parameters
;
2082 cell
->attributes
= other
->attributes
;
2086 RTLIL::Memory
*RTLIL::Module::addMemory(RTLIL::IdString name
, const RTLIL::Memory
*other
)
2088 RTLIL::Memory
*mem
= new RTLIL::Memory
;
2090 mem
->width
= other
->width
;
2091 mem
->start_offset
= other
->start_offset
;
2092 mem
->size
= other
->size
;
2093 mem
->attributes
= other
->attributes
;
2094 memories
[mem
->name
] = mem
;
2098 RTLIL::Process
*RTLIL::Module::addProcess(RTLIL::IdString name
, const RTLIL::Process
*other
)
2100 RTLIL::Process
*proc
= other
->clone();
2102 processes
[name
] = proc
;
2106 #define DEF_METHOD(_func, _y_size, _type) \
2107 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) { \
2108 RTLIL::Cell *cell = addCell(name, _type); \
2109 cell->parameters[ID::A_SIGNED] = is_signed; \
2110 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2111 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2112 cell->setPort(ID::A, sig_a); \
2113 cell->setPort(ID::Y, sig_y); \
2114 cell->set_src_attribute(src); \
2117 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed, const std::string &src) { \
2118 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2119 add ## _func(name, sig_a, sig_y, is_signed, src); \
2122 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
2123 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
2124 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
2125 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
2126 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
2127 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
2128 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
2129 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
2130 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
2133 #define DEF_METHOD(_func, _y_size, _type) \
2134 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) { \
2135 RTLIL::Cell *cell = addCell(name, _type); \
2136 cell->parameters[ID::A_SIGNED] = is_signed; \
2137 cell->parameters[ID::B_SIGNED] = is_signed; \
2138 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2139 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2140 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2141 cell->setPort(ID::A, sig_a); \
2142 cell->setPort(ID::B, sig_b); \
2143 cell->setPort(ID::Y, sig_y); \
2144 cell->set_src_attribute(src); \
2147 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) { \
2148 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2149 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2152 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
2153 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
2154 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
2155 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
2156 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
2157 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
2158 DEF_METHOD(Lt
, 1, ID($lt
))
2159 DEF_METHOD(Le
, 1, ID($le
))
2160 DEF_METHOD(Eq
, 1, ID($eq
))
2161 DEF_METHOD(Ne
, 1, ID($ne
))
2162 DEF_METHOD(Eqx
, 1, ID($eqx
))
2163 DEF_METHOD(Nex
, 1, ID($nex
))
2164 DEF_METHOD(Ge
, 1, ID($ge
))
2165 DEF_METHOD(Gt
, 1, ID($gt
))
2166 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
2167 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
2168 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
2169 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
2170 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
2171 DEF_METHOD(DivFloor
, max(sig_a
.size(), sig_b
.size()), ID($divfloor
))
2172 DEF_METHOD(ModFloor
, max(sig_a
.size(), sig_b
.size()), ID($modfloor
))
2173 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
2174 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
2177 #define DEF_METHOD(_func, _y_size, _type) \
2178 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) { \
2179 RTLIL::Cell *cell = addCell(name, _type); \
2180 cell->parameters[ID::A_SIGNED] = is_signed; \
2181 cell->parameters[ID::B_SIGNED] = false; \
2182 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2183 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2184 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2185 cell->setPort(ID::A, sig_a); \
2186 cell->setPort(ID::B, sig_b); \
2187 cell->setPort(ID::Y, sig_y); \
2188 cell->set_src_attribute(src); \
2191 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) { \
2192 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2193 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2196 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
2197 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
2198 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
2199 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
2202 #define DEF_METHOD(_func, _type, _pmux) \
2203 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) { \
2204 RTLIL::Cell *cell = addCell(name, _type); \
2205 cell->parameters[ID::WIDTH] = sig_a.size(); \
2206 if (_pmux) cell->parameters[ID::S_WIDTH] = sig_s.size(); \
2207 cell->setPort(ID::A, sig_a); \
2208 cell->setPort(ID::B, sig_b); \
2209 cell->setPort(ID::S, sig_s); \
2210 cell->setPort(ID::Y, sig_y); \
2211 cell->set_src_attribute(src); \
2214 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) { \
2215 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
2216 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
2219 DEF_METHOD(Mux
, ID($mux
), 0)
2220 DEF_METHOD(Pmux
, ID($pmux
), 1)
2223 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
2224 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2225 RTLIL::Cell *cell = addCell(name, _type); \
2226 cell->setPort("\\" #_P1, sig1); \
2227 cell->setPort("\\" #_P2, sig2); \
2228 cell->set_src_attribute(src); \
2231 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const std::string &src) { \
2232 RTLIL::SigBit sig2 = addWire(NEW_ID); \
2233 add ## _func(name, sig1, sig2, src); \
2236 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
2237 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) { \
2238 RTLIL::Cell *cell = addCell(name, _type); \
2239 cell->setPort("\\" #_P1, sig1); \
2240 cell->setPort("\\" #_P2, sig2); \
2241 cell->setPort("\\" #_P3, sig3); \
2242 cell->set_src_attribute(src); \
2245 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2246 RTLIL::SigBit sig3 = addWire(NEW_ID); \
2247 add ## _func(name, sig1, sig2, sig3, src); \
2250 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
2251 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) { \
2252 RTLIL::Cell *cell = addCell(name, _type); \
2253 cell->setPort("\\" #_P1, sig1); \
2254 cell->setPort("\\" #_P2, sig2); \
2255 cell->setPort("\\" #_P3, sig3); \
2256 cell->setPort("\\" #_P4, sig4); \
2257 cell->set_src_attribute(src); \
2260 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2261 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2262 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2265 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2266 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) { \
2267 RTLIL::Cell *cell = addCell(name, _type); \
2268 cell->setPort("\\" #_P1, sig1); \
2269 cell->setPort("\\" #_P2, sig2); \
2270 cell->setPort("\\" #_P3, sig3); \
2271 cell->setPort("\\" #_P4, sig4); \
2272 cell->setPort("\\" #_P5, sig5); \
2273 cell->set_src_attribute(src); \
2276 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) { \
2277 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2278 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2281 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2282 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2283 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2284 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2285 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2286 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2287 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2288 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2289 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2290 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2291 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2292 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2293 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2294 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2295 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2296 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2302 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
)
2304 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2305 cell
->parameters
[ID::A_SIGNED
] = a_signed
;
2306 cell
->parameters
[ID::B_SIGNED
] = b_signed
;
2307 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2308 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2309 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2310 cell
->setPort(ID::A
, sig_a
);
2311 cell
->setPort(ID::B
, sig_b
);
2312 cell
->setPort(ID::Y
, sig_y
);
2313 cell
->set_src_attribute(src
);
2317 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
)
2319 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2320 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2321 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2322 cell
->parameters
[ID::OFFSET
] = offset
;
2323 cell
->setPort(ID::A
, sig_a
);
2324 cell
->setPort(ID::Y
, sig_y
);
2325 cell
->set_src_attribute(src
);
2329 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
)
2331 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2332 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2333 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2334 cell
->setPort(ID::A
, sig_a
);
2335 cell
->setPort(ID::B
, sig_b
);
2336 cell
->setPort(ID::Y
, sig_y
);
2337 cell
->set_src_attribute(src
);
2341 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
)
2343 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2344 cell
->parameters
[ID::LUT
] = lut
;
2345 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2346 cell
->setPort(ID::A
, sig_a
);
2347 cell
->setPort(ID::Y
, sig_y
);
2348 cell
->set_src_attribute(src
);
2352 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
)
2354 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2355 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2356 cell
->setPort(ID::A
, sig_a
);
2357 cell
->setPort(ID::EN
, sig_en
);
2358 cell
->setPort(ID::Y
, sig_y
);
2359 cell
->set_src_attribute(src
);
2363 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2365 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2366 cell
->setPort(ID::A
, sig_a
);
2367 cell
->setPort(ID::EN
, sig_en
);
2368 cell
->set_src_attribute(src
);
2372 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2374 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2375 cell
->setPort(ID::A
, sig_a
);
2376 cell
->setPort(ID::EN
, sig_en
);
2377 cell
->set_src_attribute(src
);
2381 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2383 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2384 cell
->setPort(ID::A
, sig_a
);
2385 cell
->setPort(ID::EN
, sig_en
);
2386 cell
->set_src_attribute(src
);
2390 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2392 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2393 cell
->setPort(ID::A
, sig_a
);
2394 cell
->setPort(ID::EN
, sig_en
);
2395 cell
->set_src_attribute(src
);
2399 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2401 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2402 cell
->setPort(ID::A
, sig_a
);
2403 cell
->setPort(ID::EN
, sig_en
);
2404 cell
->set_src_attribute(src
);
2408 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
)
2410 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2411 cell
->setPort(ID::A
, sig_a
);
2412 cell
->setPort(ID::B
, sig_b
);
2413 cell
->setPort(ID::Y
, sig_y
);
2414 cell
->set_src_attribute(src
);
2418 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
)
2420 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2421 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2422 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2423 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2424 cell
->setPort(ID::SET
, sig_set
);
2425 cell
->setPort(ID::CLR
, sig_clr
);
2426 cell
->setPort(ID::Q
, sig_q
);
2427 cell
->set_src_attribute(src
);
2431 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2433 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2434 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2435 cell
->setPort(ID::D
, sig_d
);
2436 cell
->setPort(ID::Q
, sig_q
);
2437 cell
->set_src_attribute(src
);
2441 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
)
2443 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2444 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2445 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2446 cell
->setPort(ID::CLK
, sig_clk
);
2447 cell
->setPort(ID::D
, sig_d
);
2448 cell
->setPort(ID::Q
, sig_q
);
2449 cell
->set_src_attribute(src
);
2453 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
)
2455 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2456 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2457 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2458 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2459 cell
->setPort(ID::CLK
, sig_clk
);
2460 cell
->setPort(ID::EN
, sig_en
);
2461 cell
->setPort(ID::D
, sig_d
);
2462 cell
->setPort(ID::Q
, sig_q
);
2463 cell
->set_src_attribute(src
);
2467 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2468 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2470 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2471 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2472 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2473 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2474 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2475 cell
->setPort(ID::CLK
, sig_clk
);
2476 cell
->setPort(ID::SET
, sig_set
);
2477 cell
->setPort(ID::CLR
, sig_clr
);
2478 cell
->setPort(ID::D
, sig_d
);
2479 cell
->setPort(ID::Q
, sig_q
);
2480 cell
->set_src_attribute(src
);
2484 RTLIL::Cell
* RTLIL::Module::addDffsre(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2485 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2487 RTLIL::Cell
*cell
= addCell(name
, ID($dffsre
));
2488 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2489 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2490 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2491 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2492 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2493 cell
->setPort(ID::CLK
, sig_clk
);
2494 cell
->setPort(ID::EN
, sig_en
);
2495 cell
->setPort(ID::SET
, sig_set
);
2496 cell
->setPort(ID::CLR
, sig_clr
);
2497 cell
->setPort(ID::D
, sig_d
);
2498 cell
->setPort(ID::Q
, sig_q
);
2499 cell
->set_src_attribute(src
);
2503 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
,
2504 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2506 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2507 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2508 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2509 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2510 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2511 cell
->setPort(ID::CLK
, sig_clk
);
2512 cell
->setPort(ID::ARST
, sig_arst
);
2513 cell
->setPort(ID::D
, sig_d
);
2514 cell
->setPort(ID::Q
, sig_q
);
2515 cell
->set_src_attribute(src
);
2519 RTLIL::Cell
* RTLIL::Module::addAdffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2520 RTLIL::Const arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2522 RTLIL::Cell
*cell
= addCell(name
, ID($adffe
));
2523 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2524 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2525 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2526 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2527 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2528 cell
->setPort(ID::CLK
, sig_clk
);
2529 cell
->setPort(ID::EN
, sig_en
);
2530 cell
->setPort(ID::ARST
, sig_arst
);
2531 cell
->setPort(ID::D
, sig_d
);
2532 cell
->setPort(ID::Q
, sig_q
);
2533 cell
->set_src_attribute(src
);
2537 RTLIL::Cell
* RTLIL::Module::addSdff(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2538 RTLIL::Const srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
2540 RTLIL::Cell
*cell
= addCell(name
, ID($sdff
));
2541 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2542 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2543 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2544 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2545 cell
->setPort(ID::CLK
, sig_clk
);
2546 cell
->setPort(ID::SRST
, sig_srst
);
2547 cell
->setPort(ID::D
, sig_d
);
2548 cell
->setPort(ID::Q
, sig_q
);
2549 cell
->set_src_attribute(src
);
2553 RTLIL::Cell
* RTLIL::Module::addSdffe(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2554 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2556 RTLIL::Cell
*cell
= addCell(name
, ID($sdffe
));
2557 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2558 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2559 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2560 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2561 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2562 cell
->setPort(ID::CLK
, sig_clk
);
2563 cell
->setPort(ID::EN
, sig_en
);
2564 cell
->setPort(ID::SRST
, sig_srst
);
2565 cell
->setPort(ID::D
, sig_d
);
2566 cell
->setPort(ID::Q
, sig_q
);
2567 cell
->set_src_attribute(src
);
2571 RTLIL::Cell
* RTLIL::Module::addSdffce(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2572 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2574 RTLIL::Cell
*cell
= addCell(name
, ID($sdffce
));
2575 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2576 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2577 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2578 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2579 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2580 cell
->setPort(ID::CLK
, sig_clk
);
2581 cell
->setPort(ID::EN
, sig_en
);
2582 cell
->setPort(ID::SRST
, sig_srst
);
2583 cell
->setPort(ID::D
, sig_d
);
2584 cell
->setPort(ID::Q
, sig_q
);
2585 cell
->set_src_attribute(src
);
2589 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
)
2591 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2592 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2593 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2594 cell
->setPort(ID::EN
, sig_en
);
2595 cell
->setPort(ID::D
, sig_d
);
2596 cell
->setPort(ID::Q
, sig_q
);
2597 cell
->set_src_attribute(src
);
2601 RTLIL::Cell
* RTLIL::Module::addAdlatch(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2602 RTLIL::Const arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2604 RTLIL::Cell
*cell
= addCell(name
, ID($adlatch
));
2605 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2606 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2607 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2608 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2609 cell
->setPort(ID::EN
, sig_en
);
2610 cell
->setPort(ID::ARST
, sig_arst
);
2611 cell
->setPort(ID::D
, sig_d
);
2612 cell
->setPort(ID::Q
, sig_q
);
2613 cell
->set_src_attribute(src
);
2617 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2618 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2620 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2621 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2622 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2623 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2624 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2625 cell
->setPort(ID::EN
, sig_en
);
2626 cell
->setPort(ID::SET
, sig_set
);
2627 cell
->setPort(ID::CLR
, sig_clr
);
2628 cell
->setPort(ID::D
, sig_d
);
2629 cell
->setPort(ID::Q
, sig_q
);
2630 cell
->set_src_attribute(src
);
2634 RTLIL::Cell
* RTLIL::Module::addSrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2635 const RTLIL::SigSpec
&sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2637 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SR_%c%c_", set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2638 cell
->setPort(ID::S
, sig_set
);
2639 cell
->setPort(ID::R
, sig_clr
);
2640 cell
->setPort(ID::Q
, sig_q
);
2641 cell
->set_src_attribute(src
);
2645 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2647 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2648 cell
->setPort(ID::D
, sig_d
);
2649 cell
->setPort(ID::Q
, sig_q
);
2650 cell
->set_src_attribute(src
);
2654 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
)
2656 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2657 cell
->setPort(ID::C
, sig_clk
);
2658 cell
->setPort(ID::D
, sig_d
);
2659 cell
->setPort(ID::Q
, sig_q
);
2660 cell
->set_src_attribute(src
);
2664 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
)
2666 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2667 cell
->setPort(ID::C
, sig_clk
);
2668 cell
->setPort(ID::E
, sig_en
);
2669 cell
->setPort(ID::D
, sig_d
);
2670 cell
->setPort(ID::Q
, sig_q
);
2671 cell
->set_src_attribute(src
);
2675 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2676 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2678 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2679 cell
->setPort(ID::C
, sig_clk
);
2680 cell
->setPort(ID::S
, sig_set
);
2681 cell
->setPort(ID::R
, sig_clr
);
2682 cell
->setPort(ID::D
, sig_d
);
2683 cell
->setPort(ID::Q
, sig_q
);
2684 cell
->set_src_attribute(src
);
2688 RTLIL::Cell
* RTLIL::Module::addDffsreGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2689 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2691 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSRE_%c%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2692 cell
->setPort(ID::C
, sig_clk
);
2693 cell
->setPort(ID::S
, sig_set
);
2694 cell
->setPort(ID::R
, sig_clr
);
2695 cell
->setPort(ID::E
, sig_en
);
2696 cell
->setPort(ID::D
, sig_d
);
2697 cell
->setPort(ID::Q
, sig_q
);
2698 cell
->set_src_attribute(src
);
2702 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
,
2703 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2705 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2706 cell
->setPort(ID::C
, sig_clk
);
2707 cell
->setPort(ID::R
, sig_arst
);
2708 cell
->setPort(ID::D
, sig_d
);
2709 cell
->setPort(ID::Q
, sig_q
);
2710 cell
->set_src_attribute(src
);
2714 RTLIL::Cell
* RTLIL::Module::addAdffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2715 bool arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2717 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
2718 cell
->setPort(ID::C
, sig_clk
);
2719 cell
->setPort(ID::R
, sig_arst
);
2720 cell
->setPort(ID::E
, sig_en
);
2721 cell
->setPort(ID::D
, sig_d
);
2722 cell
->setPort(ID::Q
, sig_q
);
2723 cell
->set_src_attribute(src
);
2727 RTLIL::Cell
* RTLIL::Module::addSdffGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2728 bool srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
2730 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFF_%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0'));
2731 cell
->setPort(ID::C
, sig_clk
);
2732 cell
->setPort(ID::R
, sig_srst
);
2733 cell
->setPort(ID::D
, sig_d
);
2734 cell
->setPort(ID::Q
, sig_q
);
2735 cell
->set_src_attribute(src
);
2739 RTLIL::Cell
* RTLIL::Module::addSdffeGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2740 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2742 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFFE_%c%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
2743 cell
->setPort(ID::C
, sig_clk
);
2744 cell
->setPort(ID::R
, sig_srst
);
2745 cell
->setPort(ID::E
, sig_en
);
2746 cell
->setPort(ID::D
, sig_d
);
2747 cell
->setPort(ID::Q
, sig_q
);
2748 cell
->set_src_attribute(src
);
2752 RTLIL::Cell
* RTLIL::Module::addSdffceGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_srst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2753 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2755 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFFCE_%c%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0', en_polarity
? 'P' : 'N'));
2756 cell
->setPort(ID::C
, sig_clk
);
2757 cell
->setPort(ID::R
, sig_srst
);
2758 cell
->setPort(ID::E
, sig_en
);
2759 cell
->setPort(ID::D
, sig_d
);
2760 cell
->setPort(ID::Q
, sig_q
);
2761 cell
->set_src_attribute(src
);
2765 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
)
2767 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2768 cell
->setPort(ID::E
, sig_en
);
2769 cell
->setPort(ID::D
, sig_d
);
2770 cell
->setPort(ID::Q
, sig_q
);
2771 cell
->set_src_attribute(src
);
2775 RTLIL::Cell
* RTLIL::Module::addAdlatchGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_arst
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
,
2776 bool arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2778 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c%c%c_", en_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2779 cell
->setPort(ID::E
, sig_en
);
2780 cell
->setPort(ID::R
, sig_arst
);
2781 cell
->setPort(ID::D
, sig_d
);
2782 cell
->setPort(ID::Q
, sig_q
);
2783 cell
->set_src_attribute(src
);
2787 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2788 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2790 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2791 cell
->setPort(ID::E
, sig_en
);
2792 cell
->setPort(ID::S
, sig_set
);
2793 cell
->setPort(ID::R
, sig_clr
);
2794 cell
->setPort(ID::D
, sig_d
);
2795 cell
->setPort(ID::Q
, sig_q
);
2796 cell
->set_src_attribute(src
);
2800 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2802 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2803 Cell
*cell
= addCell(name
, ID($anyconst
));
2804 cell
->setParam(ID::WIDTH
, width
);
2805 cell
->setPort(ID::Y
, sig
);
2806 cell
->set_src_attribute(src
);
2810 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2812 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2813 Cell
*cell
= addCell(name
, ID($anyseq
));
2814 cell
->setParam(ID::WIDTH
, width
);
2815 cell
->setPort(ID::Y
, sig
);
2816 cell
->set_src_attribute(src
);
2820 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2822 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2823 Cell
*cell
= addCell(name
, ID($allconst
));
2824 cell
->setParam(ID::WIDTH
, width
);
2825 cell
->setPort(ID::Y
, sig
);
2826 cell
->set_src_attribute(src
);
2830 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2832 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2833 Cell
*cell
= addCell(name
, ID($allseq
));
2834 cell
->setParam(ID::WIDTH
, width
);
2835 cell
->setPort(ID::Y
, sig
);
2836 cell
->set_src_attribute(src
);
2840 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2842 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2843 Cell
*cell
= addCell(name
, ID($initstate
));
2844 cell
->setPort(ID::Y
, sig
);
2845 cell
->set_src_attribute(src
);
2851 static unsigned int hashidx_count
= 123456789;
2852 hashidx_count
= mkhash_xorshift(hashidx_count
);
2853 hashidx_
= hashidx_count
;
2860 port_output
= false;
2865 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2869 RTLIL::Wire::~Wire()
2872 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2877 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2878 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2884 RTLIL::Memory::Memory()
2886 static unsigned int hashidx_count
= 123456789;
2887 hashidx_count
= mkhash_xorshift(hashidx_count
);
2888 hashidx_
= hashidx_count
;
2894 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2898 RTLIL::Cell::Cell() : module(nullptr)
2900 static unsigned int hashidx_count
= 123456789;
2901 hashidx_count
= mkhash_xorshift(hashidx_count
);
2902 hashidx_
= hashidx_count
;
2904 // log("#memtrace# %p\n", this);
2908 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2912 RTLIL::Cell::~Cell()
2915 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2920 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2921 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2927 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2929 return connections_
.count(portname
) != 0;
2932 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2934 RTLIL::SigSpec signal
;
2935 auto conn_it
= connections_
.find(portname
);
2937 if (conn_it
!= connections_
.end())
2939 for (auto mon
: module
->monitors
)
2940 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2943 for (auto mon
: module
->design
->monitors
)
2944 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2947 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2948 log_backtrace("-X- ", yosys_xtrace
-1);
2951 connections_
.erase(conn_it
);
2955 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2957 auto r
= connections_
.insert(portname
);
2958 auto conn_it
= r
.first
;
2959 if (!r
.second
&& conn_it
->second
== signal
)
2962 for (auto mon
: module
->monitors
)
2963 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2966 for (auto mon
: module
->design
->monitors
)
2967 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2970 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2971 log_backtrace("-X- ", yosys_xtrace
-1);
2974 conn_it
->second
= std::move(signal
);
2977 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2979 return connections_
.at(portname
);
2982 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2984 return connections_
;
2987 bool RTLIL::Cell::known() const
2989 if (yosys_celltypes
.cell_known(type
))
2991 if (module
&& module
->design
&& module
->design
->module(type
))
2996 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2998 if (yosys_celltypes
.cell_known(type
))
2999 return yosys_celltypes
.cell_input(type
, portname
);
3000 if (module
&& module
->design
) {
3001 RTLIL::Module
*m
= module
->design
->module(type
);
3002 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3003 return w
&& w
->port_input
;
3008 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
3010 if (yosys_celltypes
.cell_known(type
))
3011 return yosys_celltypes
.cell_output(type
, portname
);
3012 if (module
&& module
->design
) {
3013 RTLIL::Module
*m
= module
->design
->module(type
);
3014 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3015 return w
&& w
->port_output
;
3020 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
3022 return parameters
.count(paramname
) != 0;
3025 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
3027 parameters
.erase(paramname
);
3030 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
3032 parameters
[paramname
] = std::move(value
);
3035 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
3037 const auto &it
= parameters
.find(paramname
);
3038 if (it
!= parameters
.end())
3040 if (module
&& module
->design
) {
3041 RTLIL::Module
*m
= module
->design
->module(type
);
3043 return m
->parameter_default_values
.at(paramname
);
3045 throw std::out_of_range("Cell::getParam()");
3048 void RTLIL::Cell::sort()
3050 connections_
.sort(sort_by_id_str());
3051 parameters
.sort(sort_by_id_str());
3052 attributes
.sort(sort_by_id_str());
3055 void RTLIL::Cell::check()
3058 InternalCellChecker
checker(NULL
, this);
3063 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
3065 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
3066 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
3069 if (type
== ID($mux
) || type
== ID($pmux
)) {
3070 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3071 if (type
== ID($pmux
))
3072 parameters
[ID::S_WIDTH
] = GetSize(connections_
[ID::S
]);
3077 if (type
== ID($lut
) || type
== ID($sop
)) {
3078 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::A
]);
3082 if (type
== ID($fa
)) {
3083 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3087 if (type
== ID($lcu
)) {
3088 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::CO
]);
3092 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
3094 if (connections_
.count(ID::A
)) {
3095 if (signedness_ab
) {
3097 parameters
[ID::A_SIGNED
] = true;
3098 else if (parameters
.count(ID::A_SIGNED
) == 0)
3099 parameters
[ID::A_SIGNED
] = false;
3101 parameters
[ID::A_WIDTH
] = GetSize(connections_
[ID::A
]);
3104 if (connections_
.count(ID::B
)) {
3105 if (signedness_ab
) {
3107 parameters
[ID::B_SIGNED
] = true;
3108 else if (parameters
.count(ID::B_SIGNED
) == 0)
3109 parameters
[ID::B_SIGNED
] = false;
3111 parameters
[ID::B_WIDTH
] = GetSize(connections_
[ID::B
]);
3114 if (connections_
.count(ID::Y
))
3115 parameters
[ID::Y_WIDTH
] = GetSize(connections_
[ID::Y
]);
3117 if (connections_
.count(ID::Q
))
3118 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Q
]);
3123 RTLIL::SigChunk::SigChunk()
3130 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
3134 width
= GetSize(data
);
3138 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
3140 log_assert(wire
!= nullptr);
3142 this->width
= wire
->width
;
3146 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
3148 log_assert(wire
!= nullptr);
3150 this->width
= width
;
3151 this->offset
= offset
;
3154 RTLIL::SigChunk::SigChunk(const std::string
&str
)
3157 data
= RTLIL::Const(str
).bits
;
3158 width
= GetSize(data
);
3162 RTLIL::SigChunk::SigChunk(int val
, int width
)
3165 data
= RTLIL::Const(val
, width
).bits
;
3166 this->width
= GetSize(data
);
3170 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
3173 data
= RTLIL::Const(bit
, width
).bits
;
3174 this->width
= GetSize(data
);
3178 RTLIL::SigChunk::SigChunk(const RTLIL::SigBit
&bit
)
3183 data
= RTLIL::Const(bit
.data
).bits
;
3185 offset
= bit
.offset
;
3189 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
)
3194 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
3196 RTLIL::SigChunk ret
;
3199 ret
.offset
= this->offset
+ offset
;
3202 for (int i
= 0; i
< length
; i
++)
3203 ret
.data
.push_back(data
[offset
+i
]);
3209 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
3211 if (wire
&& other
.wire
)
3212 if (wire
->name
!= other
.wire
->name
)
3213 return wire
->name
< other
.wire
->name
;
3215 if (wire
!= other
.wire
)
3216 return wire
< other
.wire
;
3218 if (offset
!= other
.offset
)
3219 return offset
< other
.offset
;
3221 if (width
!= other
.width
)
3222 return width
< other
.width
;
3224 return data
< other
.data
;
3227 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
3229 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
3232 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
3239 RTLIL::SigSpec::SigSpec()
3245 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
3250 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
3252 cover("kernel.rtlil.sigspec.init.list");
3257 log_assert(parts
.size() > 0);
3258 auto ie
= parts
.begin();
3259 auto it
= ie
+ parts
.size() - 1;
3264 RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
3266 cover("kernel.rtlil.sigspec.assign");
3268 width_
= other
.width_
;
3269 hash_
= other
.hash_
;
3270 chunks_
= other
.chunks_
;
3271 bits_
= other
.bits_
;
3275 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
3277 cover("kernel.rtlil.sigspec.init.const");
3279 if (GetSize(value
) != 0) {
3280 chunks_
.emplace_back(value
);
3281 width_
= chunks_
.back().width
;
3289 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
3291 cover("kernel.rtlil.sigspec.init.chunk");
3293 if (chunk
.width
!= 0) {
3294 chunks_
.emplace_back(chunk
);
3295 width_
= chunks_
.back().width
;
3303 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
3305 cover("kernel.rtlil.sigspec.init.wire");
3307 if (wire
->width
!= 0) {
3308 chunks_
.emplace_back(wire
);
3309 width_
= chunks_
.back().width
;
3317 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
3319 cover("kernel.rtlil.sigspec.init.wire_part");
3322 chunks_
.emplace_back(wire
, offset
, width
);
3323 width_
= chunks_
.back().width
;
3331 RTLIL::SigSpec::SigSpec(const std::string
&str
)
3333 cover("kernel.rtlil.sigspec.init.str");
3335 if (str
.size() != 0) {
3336 chunks_
.emplace_back(str
);
3337 width_
= chunks_
.back().width
;
3345 RTLIL::SigSpec::SigSpec(int val
, int width
)
3347 cover("kernel.rtlil.sigspec.init.int");
3350 chunks_
.emplace_back(val
, width
);
3356 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
3358 cover("kernel.rtlil.sigspec.init.state");
3361 chunks_
.emplace_back(bit
, width
);
3367 RTLIL::SigSpec::SigSpec(const RTLIL::SigBit
&bit
, int width
)
3369 cover("kernel.rtlil.sigspec.init.bit");
3372 if (bit
.wire
== NULL
)
3373 chunks_
.emplace_back(bit
.data
, width
);
3375 for (int i
= 0; i
< width
; i
++)
3376 chunks_
.push_back(bit
);
3383 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigChunk
> &chunks
)
3385 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
3389 for (const auto &c
: chunks
)
3394 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigBit
> &bits
)
3396 cover("kernel.rtlil.sigspec.init.stdvec_bits");
3400 for (const auto &bit
: bits
)
3405 RTLIL::SigSpec::SigSpec(const pool
<RTLIL::SigBit
> &bits
)
3407 cover("kernel.rtlil.sigspec.init.pool_bits");
3411 for (const auto &bit
: bits
)
3416 RTLIL::SigSpec::SigSpec(const std::set
<RTLIL::SigBit
> &bits
)
3418 cover("kernel.rtlil.sigspec.init.stdset_bits");
3422 for (const auto &bit
: bits
)
3427 RTLIL::SigSpec::SigSpec(bool bit
)
3429 cover("kernel.rtlil.sigspec.init.bool");
3433 append(SigBit(bit
));
3437 void RTLIL::SigSpec::pack() const
3439 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3441 if (that
->bits_
.empty())
3444 cover("kernel.rtlil.sigspec.convert.pack");
3445 log_assert(that
->chunks_
.empty());
3447 std::vector
<RTLIL::SigBit
> old_bits
;
3448 old_bits
.swap(that
->bits_
);
3450 RTLIL::SigChunk
*last
= NULL
;
3451 int last_end_offset
= 0;
3453 for (auto &bit
: old_bits
) {
3454 if (last
&& bit
.wire
== last
->wire
) {
3455 if (bit
.wire
== NULL
) {
3456 last
->data
.push_back(bit
.data
);
3459 } else if (last_end_offset
== bit
.offset
) {
3465 that
->chunks_
.push_back(bit
);
3466 last
= &that
->chunks_
.back();
3467 last_end_offset
= bit
.offset
+ 1;
3473 void RTLIL::SigSpec::unpack() const
3475 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3477 if (that
->chunks_
.empty())
3480 cover("kernel.rtlil.sigspec.convert.unpack");
3481 log_assert(that
->bits_
.empty());
3483 that
->bits_
.reserve(that
->width_
);
3484 for (auto &c
: that
->chunks_
)
3485 for (int i
= 0; i
< c
.width
; i
++)
3486 that
->bits_
.emplace_back(c
, i
);
3488 that
->chunks_
.clear();
3492 void RTLIL::SigSpec::updhash() const
3494 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3496 if (that
->hash_
!= 0)
3499 cover("kernel.rtlil.sigspec.hash");
3502 that
->hash_
= mkhash_init
;
3503 for (auto &c
: that
->chunks_
)
3504 if (c
.wire
== NULL
) {
3505 for (auto &v
: c
.data
)
3506 that
->hash_
= mkhash(that
->hash_
, v
);
3508 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3509 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3510 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3513 if (that
->hash_
== 0)
3517 void RTLIL::SigSpec::sort()
3520 cover("kernel.rtlil.sigspec.sort");
3521 std::sort(bits_
.begin(), bits_
.end());
3524 void RTLIL::SigSpec::sort_and_unify()
3527 cover("kernel.rtlil.sigspec.sort_and_unify");
3529 // A copy of the bits vector is used to prevent duplicating the logic from
3530 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3531 // that isn't showing up as significant in profiles.
3532 std::vector
<SigBit
> unique_bits
= bits_
;
3533 std::sort(unique_bits
.begin(), unique_bits
.end());
3534 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3535 unique_bits
.erase(last
, unique_bits
.end());
3537 *this = unique_bits
;
3540 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3542 replace(pattern
, with
, this);
3545 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3547 log_assert(other
!= NULL
);
3548 log_assert(width_
== other
->width_
);
3549 log_assert(pattern
.width_
== with
.width_
);
3556 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3557 if (pattern
.bits_
[i
].wire
!= NULL
) {
3558 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3559 if (bits_
[j
] == pattern
.bits_
[i
]) {
3560 other
->bits_
[j
] = with
.bits_
[i
];
3569 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3571 replace(rules
, this);
3574 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3576 cover("kernel.rtlil.sigspec.replace_dict");
3578 log_assert(other
!= NULL
);
3579 log_assert(width_
== other
->width_
);
3581 if (rules
.empty()) return;
3585 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3586 auto it
= rules
.find(bits_
[i
]);
3587 if (it
!= rules
.end())
3588 other
->bits_
[i
] = it
->second
;
3594 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3596 replace(rules
, this);
3599 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3601 cover("kernel.rtlil.sigspec.replace_map");
3603 log_assert(other
!= NULL
);
3604 log_assert(width_
== other
->width_
);
3606 if (rules
.empty()) return;
3610 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3611 auto it
= rules
.find(bits_
[i
]);
3612 if (it
!= rules
.end())
3613 other
->bits_
[i
] = it
->second
;
3619 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
3621 remove2(pattern
, NULL
);
3624 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3626 RTLIL::SigSpec tmp
= *this;
3627 tmp
.remove2(pattern
, other
);
3630 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3633 cover("kernel.rtlil.sigspec.remove_other");
3635 cover("kernel.rtlil.sigspec.remove");
3638 if (other
!= NULL
) {
3639 log_assert(width_
== other
->width_
);
3643 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3645 if (bits_
[i
].wire
== NULL
) continue;
3647 for (auto &pattern_chunk
: pattern
.chunks())
3648 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3649 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3650 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3651 bits_
.erase(bits_
.begin() + i
);
3653 if (other
!= NULL
) {
3654 other
->bits_
.erase(other
->bits_
.begin() + i
);
3664 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3666 remove2(pattern
, NULL
);
3669 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3671 RTLIL::SigSpec tmp
= *this;
3672 tmp
.remove2(pattern
, other
);
3675 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3678 cover("kernel.rtlil.sigspec.remove_other");
3680 cover("kernel.rtlil.sigspec.remove");
3684 if (other
!= NULL
) {
3685 log_assert(width_
== other
->width_
);
3689 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3690 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3691 bits_
.erase(bits_
.begin() + i
);
3693 if (other
!= NULL
) {
3694 other
->bits_
.erase(other
->bits_
.begin() + i
);
3703 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3706 cover("kernel.rtlil.sigspec.remove_other");
3708 cover("kernel.rtlil.sigspec.remove");
3712 if (other
!= NULL
) {
3713 log_assert(width_
== other
->width_
);
3717 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3718 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3719 bits_
.erase(bits_
.begin() + i
);
3721 if (other
!= NULL
) {
3722 other
->bits_
.erase(other
->bits_
.begin() + i
);
3731 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3734 cover("kernel.rtlil.sigspec.extract_other");
3736 cover("kernel.rtlil.sigspec.extract");
3738 log_assert(other
== NULL
|| width_
== other
->width_
);
3741 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3743 for (auto& pattern_chunk
: pattern
.chunks()) {
3745 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3746 for (int i
= 0; i
< width_
; i
++)
3747 if (bits_match
[i
].wire
&&
3748 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3749 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3750 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3751 ret
.append(bits_other
[i
]);
3753 for (int i
= 0; i
< width_
; i
++)
3754 if (bits_match
[i
].wire
&&
3755 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3756 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3757 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3758 ret
.append(bits_match
[i
]);
3766 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3769 cover("kernel.rtlil.sigspec.extract_other");
3771 cover("kernel.rtlil.sigspec.extract");
3773 log_assert(other
== NULL
|| width_
== other
->width_
);
3775 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3779 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3780 for (int i
= 0; i
< width_
; i
++)
3781 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3782 ret
.append(bits_other
[i
]);
3784 for (int i
= 0; i
< width_
; i
++)
3785 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3786 ret
.append(bits_match
[i
]);
3793 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3795 cover("kernel.rtlil.sigspec.replace_pos");
3800 log_assert(offset
>= 0);
3801 log_assert(with
.width_
>= 0);
3802 log_assert(offset
+with
.width_
<= width_
);
3804 for (int i
= 0; i
< with
.width_
; i
++)
3805 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3810 void RTLIL::SigSpec::remove_const()
3814 cover("kernel.rtlil.sigspec.remove_const.packed");
3816 std::vector
<RTLIL::SigChunk
> new_chunks
;
3817 new_chunks
.reserve(GetSize(chunks_
));
3820 for (auto &chunk
: chunks_
)
3821 if (chunk
.wire
!= NULL
) {
3822 if (!new_chunks
.empty() &&
3823 new_chunks
.back().wire
== chunk
.wire
&&
3824 new_chunks
.back().offset
+ new_chunks
.back().width
== chunk
.offset
) {
3825 new_chunks
.back().width
+= chunk
.width
;
3827 new_chunks
.push_back(chunk
);
3829 width_
+= chunk
.width
;
3832 chunks_
.swap(new_chunks
);
3836 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3838 std::vector
<RTLIL::SigBit
> new_bits
;
3839 new_bits
.reserve(width_
);
3841 for (auto &bit
: bits_
)
3842 if (bit
.wire
!= NULL
)
3843 new_bits
.push_back(bit
);
3845 bits_
.swap(new_bits
);
3846 width_
= bits_
.size();
3852 void RTLIL::SigSpec::remove(int offset
, int length
)
3854 cover("kernel.rtlil.sigspec.remove_pos");
3858 log_assert(offset
>= 0);
3859 log_assert(length
>= 0);
3860 log_assert(offset
+ length
<= width_
);
3862 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3863 width_
= bits_
.size();
3868 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3871 cover("kernel.rtlil.sigspec.extract_pos");
3872 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3875 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3877 if (signal
.width_
== 0)
3885 cover("kernel.rtlil.sigspec.append");
3887 if (packed() != signal
.packed()) {
3893 for (auto &other_c
: signal
.chunks_
)
3895 auto &my_last_c
= chunks_
.back();
3896 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3897 auto &this_data
= my_last_c
.data
;
3898 auto &other_data
= other_c
.data
;
3899 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3900 my_last_c
.width
+= other_c
.width
;
3902 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3903 my_last_c
.width
+= other_c
.width
;
3905 chunks_
.push_back(other_c
);
3908 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3910 width_
+= signal
.width_
;
3914 void RTLIL::SigSpec::append(const RTLIL::SigBit
&bit
)
3918 cover("kernel.rtlil.sigspec.append_bit.packed");
3920 if (chunks_
.size() == 0)
3921 chunks_
.push_back(bit
);
3923 if (bit
.wire
== NULL
)
3924 if (chunks_
.back().wire
== NULL
) {
3925 chunks_
.back().data
.push_back(bit
.data
);
3926 chunks_
.back().width
++;
3928 chunks_
.push_back(bit
);
3930 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3931 chunks_
.back().width
++;
3933 chunks_
.push_back(bit
);
3937 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3938 bits_
.push_back(bit
);
3945 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3947 cover("kernel.rtlil.sigspec.extend_u0");
3952 remove(width
, width_
- width
);
3954 if (width_
< width
) {
3955 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3957 padding
= RTLIL::State::S0
;
3958 while (width_
< width
)
3964 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3966 cover("kernel.rtlil.sigspec.repeat");
3969 for (int i
= 0; i
< num
; i
++)
3975 void RTLIL::SigSpec::check() const
3979 cover("kernel.rtlil.sigspec.check.skip");
3983 cover("kernel.rtlil.sigspec.check.packed");
3986 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3987 const RTLIL::SigChunk
&chunk
= chunks_
[i
];
3988 log_assert(chunk
.width
!= 0);
3989 if (chunk
.wire
== NULL
) {
3991 log_assert(chunks_
[i
-1].wire
!= NULL
);
3992 log_assert(chunk
.offset
== 0);
3993 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3995 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3996 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3997 log_assert(chunk
.offset
>= 0);
3998 log_assert(chunk
.width
>= 0);
3999 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
4000 log_assert(chunk
.data
.size() == 0);
4004 log_assert(w
== width_
);
4005 log_assert(bits_
.empty());
4009 cover("kernel.rtlil.sigspec.check.unpacked");
4011 log_assert(width_
== GetSize(bits_
));
4012 log_assert(chunks_
.empty());
4017 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
4019 cover("kernel.rtlil.sigspec.comp_lt");
4024 if (width_
!= other
.width_
)
4025 return width_
< other
.width_
;
4030 if (chunks_
.size() != other
.chunks_
.size())
4031 return chunks_
.size() < other
.chunks_
.size();
4036 if (hash_
!= other
.hash_
)
4037 return hash_
< other
.hash_
;
4039 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4040 if (chunks_
[i
] != other
.chunks_
[i
]) {
4041 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
4042 return chunks_
[i
] < other
.chunks_
[i
];
4045 cover("kernel.rtlil.sigspec.comp_lt.equal");
4049 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
4051 cover("kernel.rtlil.sigspec.comp_eq");
4056 if (width_
!= other
.width_
)
4059 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
4060 // since the RHS will contain one SigChunk of width 0 causing
4061 // the size check below to fail
4068 if (chunks_
.size() != other
.chunks_
.size())
4074 if (hash_
!= other
.hash_
)
4077 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4078 if (chunks_
[i
] != other
.chunks_
[i
]) {
4079 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
4083 cover("kernel.rtlil.sigspec.comp_eq.equal");
4087 bool RTLIL::SigSpec::is_wire() const
4089 cover("kernel.rtlil.sigspec.is_wire");
4092 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
4095 bool RTLIL::SigSpec::is_chunk() const
4097 cover("kernel.rtlil.sigspec.is_chunk");
4100 return GetSize(chunks_
) == 1;
4103 bool RTLIL::SigSpec::is_fully_const() const
4105 cover("kernel.rtlil.sigspec.is_fully_const");
4108 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4109 if (it
->width
> 0 && it
->wire
!= NULL
)
4114 bool RTLIL::SigSpec::is_fully_zero() const
4116 cover("kernel.rtlil.sigspec.is_fully_zero");
4119 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4120 if (it
->width
> 0 && it
->wire
!= NULL
)
4122 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4123 if (it
->data
[i
] != RTLIL::State::S0
)
4129 bool RTLIL::SigSpec::is_fully_ones() const
4131 cover("kernel.rtlil.sigspec.is_fully_ones");
4134 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4135 if (it
->width
> 0 && it
->wire
!= NULL
)
4137 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4138 if (it
->data
[i
] != RTLIL::State::S1
)
4144 bool RTLIL::SigSpec::is_fully_def() const
4146 cover("kernel.rtlil.sigspec.is_fully_def");
4149 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4150 if (it
->width
> 0 && it
->wire
!= NULL
)
4152 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4153 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
4159 bool RTLIL::SigSpec::is_fully_undef() const
4161 cover("kernel.rtlil.sigspec.is_fully_undef");
4164 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4165 if (it
->width
> 0 && it
->wire
!= NULL
)
4167 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4168 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
4174 bool RTLIL::SigSpec::has_const() const
4176 cover("kernel.rtlil.sigspec.has_const");
4179 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4180 if (it
->width
> 0 && it
->wire
== NULL
)
4185 bool RTLIL::SigSpec::has_marked_bits() const
4187 cover("kernel.rtlil.sigspec.has_marked_bits");
4190 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4191 if (it
->width
> 0 && it
->wire
== NULL
) {
4192 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4193 if (it
->data
[i
] == RTLIL::State::Sm
)
4199 bool RTLIL::SigSpec::as_bool() const
4201 cover("kernel.rtlil.sigspec.as_bool");
4204 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4206 return RTLIL::Const(chunks_
[0].data
).as_bool();
4210 int RTLIL::SigSpec::as_int(bool is_signed
) const
4212 cover("kernel.rtlil.sigspec.as_int");
4215 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4217 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
4221 std::string
RTLIL::SigSpec::as_string() const
4223 cover("kernel.rtlil.sigspec.as_string");
4227 str
.reserve(size());
4228 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
4229 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
4230 if (chunk
.wire
!= NULL
)
4231 str
.append(chunk
.width
, '?');
4233 str
+= RTLIL::Const(chunk
.data
).as_string();
4238 RTLIL::Const
RTLIL::SigSpec::as_const() const
4240 cover("kernel.rtlil.sigspec.as_const");
4243 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4245 return chunks_
[0].data
;
4246 return RTLIL::Const();
4249 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
4251 cover("kernel.rtlil.sigspec.as_wire");
4254 log_assert(is_wire());
4255 return chunks_
[0].wire
;
4258 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
4260 cover("kernel.rtlil.sigspec.as_chunk");
4263 log_assert(is_chunk());
4267 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
4269 cover("kernel.rtlil.sigspec.as_bit");
4271 log_assert(width_
== 1);
4273 return RTLIL::SigBit(*chunks_
.begin());
4278 bool RTLIL::SigSpec::match(const char* pattern
) const
4280 cover("kernel.rtlil.sigspec.match");
4283 log_assert(int(strlen(pattern
)) == GetSize(bits_
));
4285 for (auto it
= bits_
.rbegin(); it
!= bits_
.rend(); it
++, pattern
++) {
4286 if (*pattern
== ' ')
4288 if (*pattern
== '*') {
4289 if (*it
!= State::Sz
&& *it
!= State::Sx
)
4293 if (*pattern
== '0') {
4294 if (*it
!= State::S0
)
4297 if (*pattern
== '1') {
4298 if (*it
!= State::S1
)
4307 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
4309 cover("kernel.rtlil.sigspec.to_sigbit_set");
4312 std::set
<RTLIL::SigBit
> sigbits
;
4313 for (auto &c
: chunks_
)
4314 for (int i
= 0; i
< c
.width
; i
++)
4315 sigbits
.insert(RTLIL::SigBit(c
, i
));
4319 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
4321 cover("kernel.rtlil.sigspec.to_sigbit_pool");
4324 pool
<RTLIL::SigBit
> sigbits
;
4325 sigbits
.reserve(size());
4326 for (auto &c
: chunks_
)
4327 for (int i
= 0; i
< c
.width
; i
++)
4328 sigbits
.insert(RTLIL::SigBit(c
, i
));
4332 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
4334 cover("kernel.rtlil.sigspec.to_sigbit_vector");
4340 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
4342 cover("kernel.rtlil.sigspec.to_sigbit_map");
4347 log_assert(width_
== other
.width_
);
4349 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4350 for (int i
= 0; i
< width_
; i
++)
4351 new_map
[bits_
[i
]] = other
.bits_
[i
];
4356 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
4358 cover("kernel.rtlil.sigspec.to_sigbit_dict");
4363 log_assert(width_
== other
.width_
);
4365 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4366 new_map
.reserve(size());
4367 for (int i
= 0; i
< width_
; i
++)
4368 new_map
[bits_
[i
]] = other
.bits_
[i
];
4373 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
4375 size_t start
= 0, end
= 0;
4376 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
4377 tokens
.push_back(text
.substr(start
, end
- start
));
4380 tokens
.push_back(text
.substr(start
));
4383 static int sigspec_parse_get_dummy_line_num()
4388 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4390 cover("kernel.rtlil.sigspec.parse");
4392 AST::current_filename
= "input";
4394 std::vector
<std::string
> tokens
;
4395 sigspec_parse_split(tokens
, str
, ',');
4397 sig
= RTLIL::SigSpec();
4398 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
4400 std::string netname
= tokens
[tokidx
];
4401 std::string indices
;
4403 if (netname
.size() == 0)
4406 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
4407 cover("kernel.rtlil.sigspec.parse.const");
4408 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
4409 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
4412 sig
.append(RTLIL::Const(ast
->bits
));
4420 cover("kernel.rtlil.sigspec.parse.net");
4422 if (netname
[0] != '$' && netname
[0] != '\\')
4423 netname
= "\\" + netname
;
4425 if (module
->wires_
.count(netname
) == 0) {
4426 size_t indices_pos
= netname
.size()-1;
4427 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
4430 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4431 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
4433 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4435 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
4436 indices
= netname
.substr(indices_pos
);
4437 netname
= netname
.substr(0, indices_pos
);
4442 if (module
->wires_
.count(netname
) == 0)
4445 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
4446 if (!indices
.empty()) {
4447 std::vector
<std::string
> index_tokens
;
4448 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
4449 if (index_tokens
.size() == 1) {
4450 cover("kernel.rtlil.sigspec.parse.bit_sel");
4451 int a
= atoi(index_tokens
.at(0).c_str());
4452 if (a
< 0 || a
>= wire
->width
)
4454 sig
.append(RTLIL::SigSpec(wire
, a
));
4456 cover("kernel.rtlil.sigspec.parse.part_sel");
4457 int a
= atoi(index_tokens
.at(0).c_str());
4458 int b
= atoi(index_tokens
.at(1).c_str());
4463 if (a
< 0 || a
>= wire
->width
)
4465 if (b
< 0 || b
>= wire
->width
)
4467 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4476 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4478 if (str
.empty() || str
[0] != '@')
4479 return parse(sig
, module
, str
);
4481 cover("kernel.rtlil.sigspec.parse.sel");
4483 str
= RTLIL::escape_id(str
.substr(1));
4484 if (design
->selection_vars
.count(str
) == 0)
4487 sig
= RTLIL::SigSpec();
4488 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4489 for (auto &it
: module
->wires_
)
4490 if (sel
.selected_member(module
->name
, it
.first
))
4491 sig
.append(it
.second
);
4496 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4499 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4500 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4505 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4506 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4510 if (lhs
.chunks_
.size() == 1) {
4511 char *p
= (char*)str
.c_str(), *endptr
;
4512 long int val
= strtol(p
, &endptr
, 10);
4513 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4514 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4515 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4520 return parse(sig
, module
, str
);
4523 RTLIL::CaseRule::~CaseRule()
4525 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4529 bool RTLIL::CaseRule::empty() const
4531 return actions
.empty() && switches
.empty();
4534 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4536 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4537 new_caserule
->compare
= compare
;
4538 new_caserule
->actions
= actions
;
4539 for (auto &it
: switches
)
4540 new_caserule
->switches
.push_back(it
->clone());
4541 return new_caserule
;
4544 RTLIL::SwitchRule::~SwitchRule()
4546 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4550 bool RTLIL::SwitchRule::empty() const
4552 return cases
.empty();
4555 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4557 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4558 new_switchrule
->signal
= signal
;
4559 new_switchrule
->attributes
= attributes
;
4560 for (auto &it
: cases
)
4561 new_switchrule
->cases
.push_back(it
->clone());
4562 return new_switchrule
;
4566 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4568 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4569 new_syncrule
->type
= type
;
4570 new_syncrule
->signal
= signal
;
4571 new_syncrule
->actions
= actions
;
4572 new_syncrule
->mem_write_actions
= mem_write_actions
;
4573 return new_syncrule
;
4576 RTLIL::Process::~Process()
4578 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4582 RTLIL::Process
*RTLIL::Process::clone() const
4584 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4586 new_proc
->name
= name
;
4587 new_proc
->attributes
= attributes
;
4589 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4590 new_proc
->root_case
= *rc_ptr
;
4591 rc_ptr
->switches
.clear();
4594 for (auto &it
: syncs
)
4595 new_proc
->syncs
.push_back(it
->clone());
4601 RTLIL::Memory::~Memory()
4603 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4605 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4606 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4608 return &all_memorys
;