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 log_assert(modules_
.count(name
) == 0);
617 log_assert(refcount_modules_
== 0);
619 RTLIL::Module
*module
= new RTLIL::Module
;
620 modules_
[name
] = module
;
621 module
->design
= this;
624 for (auto mon
: monitors
)
625 mon
->notify_module_add(module
);
628 log("#X# New Module: %s\n", log_id(module
));
629 log_backtrace("-X- ", yosys_xtrace
-1);
635 void RTLIL::Design::scratchpad_unset(const std::string
&varname
)
637 scratchpad
.erase(varname
);
640 void RTLIL::Design::scratchpad_set_int(const std::string
&varname
, int value
)
642 scratchpad
[varname
] = stringf("%d", value
);
645 void RTLIL::Design::scratchpad_set_bool(const std::string
&varname
, bool value
)
647 scratchpad
[varname
] = value
? "true" : "false";
650 void RTLIL::Design::scratchpad_set_string(const std::string
&varname
, std::string value
)
652 scratchpad
[varname
] = std::move(value
);
655 int RTLIL::Design::scratchpad_get_int(const std::string
&varname
, int default_value
) const
657 auto it
= scratchpad
.find(varname
);
658 if (it
== scratchpad
.end())
659 return default_value
;
661 const std::string
&str
= it
->second
;
663 if (str
== "0" || str
== "false")
666 if (str
== "1" || str
== "true")
669 char *endptr
= nullptr;
670 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
671 return *endptr
? default_value
: parsed_value
;
674 bool RTLIL::Design::scratchpad_get_bool(const std::string
&varname
, bool default_value
) const
676 auto it
= scratchpad
.find(varname
);
677 if (it
== scratchpad
.end())
678 return default_value
;
680 const std::string
&str
= it
->second
;
682 if (str
== "0" || str
== "false")
685 if (str
== "1" || str
== "true")
688 return default_value
;
691 std::string
RTLIL::Design::scratchpad_get_string(const std::string
&varname
, const std::string
&default_value
) const
693 auto it
= scratchpad
.find(varname
);
694 if (it
== scratchpad
.end())
695 return default_value
;
700 void RTLIL::Design::remove(RTLIL::Module
*module
)
702 for (auto mon
: monitors
)
703 mon
->notify_module_del(module
);
706 log("#X# Remove Module: %s\n", log_id(module
));
707 log_backtrace("-X- ", yosys_xtrace
-1);
710 log_assert(modules_
.at(module
->name
) == module
);
711 log_assert(refcount_modules_
== 0);
712 modules_
.erase(module
->name
);
716 void RTLIL::Design::rename(RTLIL::Module
*module
, RTLIL::IdString new_name
)
718 modules_
.erase(module
->name
);
719 module
->name
= new_name
;
723 void RTLIL::Design::sort()
726 modules_
.sort(sort_by_id_str());
727 for (auto &it
: modules_
)
731 void RTLIL::Design::check()
734 for (auto &it
: modules_
) {
735 log_assert(this == it
.second
->design
);
736 log_assert(it
.first
== it
.second
->name
);
737 log_assert(!it
.first
.empty());
743 void RTLIL::Design::optimize()
745 for (auto &it
: modules_
)
746 it
.second
->optimize();
747 for (auto &it
: selection_stack
)
749 for (auto &it
: selection_vars
)
750 it
.second
.optimize(this);
753 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
755 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
757 if (selection_stack
.size() == 0)
759 return selection_stack
.back().selected_module(mod_name
);
762 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
764 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
766 if (selection_stack
.size() == 0)
768 return selection_stack
.back().selected_whole_module(mod_name
);
771 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
773 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
775 if (selection_stack
.size() == 0)
777 return selection_stack
.back().selected_member(mod_name
, memb_name
);
780 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
782 return selected_module(mod
->name
);
785 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
787 return selected_whole_module(mod
->name
);
790 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
792 std::vector
<RTLIL::Module
*> result
;
793 result
.reserve(modules_
.size());
794 for (auto &it
: modules_
)
795 if (selected_module(it
.first
) && !it
.second
->get_blackbox_attribute())
796 result
.push_back(it
.second
);
800 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
802 std::vector
<RTLIL::Module
*> result
;
803 result
.reserve(modules_
.size());
804 for (auto &it
: modules_
)
805 if (selected_whole_module(it
.first
) && !it
.second
->get_blackbox_attribute())
806 result
.push_back(it
.second
);
810 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
812 std::vector
<RTLIL::Module
*> result
;
813 result
.reserve(modules_
.size());
814 for (auto &it
: modules_
)
815 if (it
.second
->get_blackbox_attribute())
817 else if (selected_whole_module(it
.first
))
818 result
.push_back(it
.second
);
819 else if (selected_module(it
.first
))
820 log_warning("Ignoring partially selected module %s.\n", log_id(it
.first
));
824 RTLIL::Module::Module()
826 static unsigned int hashidx_count
= 123456789;
827 hashidx_count
= mkhash_xorshift(hashidx_count
);
828 hashidx_
= hashidx_count
;
835 RTLIL::Module::get_all_modules()->insert(std::pair
<unsigned int, RTLIL::Module
*>(hashidx_
, this));
839 RTLIL::Module::~Module()
841 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
843 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
845 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
847 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
850 RTLIL::Module::get_all_modules()->erase(hashidx_
);
855 static std::map
<unsigned int, RTLIL::Module
*> all_modules
;
856 std::map
<unsigned int, RTLIL::Module
*> *RTLIL::Module::get_all_modules(void)
862 void RTLIL::Module::makeblackbox()
864 pool
<RTLIL::Wire
*> delwires
;
866 for (auto it
= wires_
.begin(); it
!= wires_
.end(); ++it
)
867 if (!it
->second
->port_input
&& !it
->second
->port_output
)
868 delwires
.insert(it
->second
);
870 for (auto it
= memories
.begin(); it
!= memories
.end(); ++it
)
874 for (auto it
= cells_
.begin(); it
!= cells_
.end(); ++it
)
878 for (auto it
= processes
.begin(); it
!= processes
.end(); ++it
)
882 connections_
.clear();
885 set_bool_attribute(ID::blackbox
);
888 void RTLIL::Module::reprocess_module(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Module
*> &)
890 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name
));
893 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, const dict
<RTLIL::IdString
, RTLIL::Const
> &, bool mayfail
)
896 return RTLIL::IdString();
897 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
901 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
)
904 return RTLIL::IdString();
905 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
908 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
910 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
915 struct InternalCellChecker
917 RTLIL::Module
*module
;
919 pool
<RTLIL::IdString
> expected_params
, expected_ports
;
921 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
923 void error(int linenr
)
925 std::stringstream buf
;
926 RTLIL_BACKEND::dump_cell(buf
, " ", cell
);
928 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
929 module
? module
->name
.c_str() : "", module
? "." : "",
930 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
933 int param(RTLIL::IdString name
)
935 auto it
= cell
->parameters
.find(name
);
936 if (it
== cell
->parameters
.end())
938 expected_params
.insert(name
);
939 return it
->second
.as_int();
942 int param_bool(RTLIL::IdString name
)
945 if (GetSize(cell
->parameters
.at(name
)) > 32)
947 if (v
!= 0 && v
!= 1)
952 int param_bool(RTLIL::IdString name
, bool expected
)
954 int v
= param_bool(name
);
960 void param_bits(RTLIL::IdString name
, int width
)
963 if (GetSize(cell
->parameters
.at(name
).bits
) != width
)
967 void port(RTLIL::IdString name
, int width
)
969 auto it
= cell
->connections_
.find(name
);
970 if (it
== cell
->connections_
.end())
972 if (GetSize(it
->second
) != width
)
974 expected_ports
.insert(name
);
977 void check_expected(bool check_matched_sign
= false)
979 for (auto ¶
: cell
->parameters
)
980 if (expected_params
.count(para
.first
) == 0)
982 for (auto &conn
: cell
->connections())
983 if (expected_ports
.count(conn
.first
) == 0)
986 if (check_matched_sign
) {
987 log_assert(expected_params
.count(ID::A_SIGNED
) != 0 && expected_params
.count(ID::B_SIGNED
) != 0);
988 bool a_is_signed
= cell
->parameters
.at(ID::A_SIGNED
).as_bool();
989 bool b_is_signed
= cell
->parameters
.at(ID::B_SIGNED
).as_bool();
990 if (a_is_signed
!= b_is_signed
)
997 if (!cell
->type
.begins_with("$") || cell
->type
.begins_with("$__") || cell
->type
.begins_with("$paramod") || cell
->type
.begins_with("$fmcombine") ||
998 cell
->type
.begins_with("$verific$") || cell
->type
.begins_with("$array:") || cell
->type
.begins_with("$extern:"))
1001 if (cell
->type
.in(ID($
not), ID($pos
), ID($neg
))) {
1002 param_bool(ID::A_SIGNED
);
1003 port(ID::A
, param(ID::A_WIDTH
));
1004 port(ID::Y
, param(ID::Y_WIDTH
));
1009 if (cell
->type
.in(ID($
and), ID($
or), ID($
xor), ID($xnor
))) {
1010 param_bool(ID::A_SIGNED
);
1011 param_bool(ID::B_SIGNED
);
1012 port(ID::A
, param(ID::A_WIDTH
));
1013 port(ID::B
, param(ID::B_WIDTH
));
1014 port(ID::Y
, param(ID::Y_WIDTH
));
1015 check_expected(true);
1019 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_xor
), ID($reduce_xnor
), ID($reduce_bool
))) {
1020 param_bool(ID::A_SIGNED
);
1021 port(ID::A
, param(ID::A_WIDTH
));
1022 port(ID::Y
, param(ID::Y_WIDTH
));
1027 if (cell
->type
.in(ID($shl
), ID($shr
), ID($sshl
), ID($sshr
))) {
1028 param_bool(ID::A_SIGNED
);
1029 param_bool(ID::B_SIGNED
, /*expected=*/false);
1030 port(ID::A
, param(ID::A_WIDTH
));
1031 port(ID::B
, param(ID::B_WIDTH
));
1032 port(ID::Y
, param(ID::Y_WIDTH
));
1033 check_expected(/*check_matched_sign=*/false);
1037 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
1038 if (cell
->type
== ID($shiftx
)) {
1039 param_bool(ID::A_SIGNED
, /*expected=*/false);
1041 param_bool(ID::A_SIGNED
);
1043 param_bool(ID::B_SIGNED
);
1044 port(ID::A
, param(ID::A_WIDTH
));
1045 port(ID::B
, param(ID::B_WIDTH
));
1046 port(ID::Y
, param(ID::Y_WIDTH
));
1047 check_expected(/*check_matched_sign=*/false);
1051 if (cell
->type
.in(ID($lt
), ID($le
), ID($eq
), ID($ne
), ID($eqx
), ID($nex
), ID($ge
), ID($gt
))) {
1052 param_bool(ID::A_SIGNED
);
1053 param_bool(ID::B_SIGNED
);
1054 port(ID::A
, param(ID::A_WIDTH
));
1055 port(ID::B
, param(ID::B_WIDTH
));
1056 port(ID::Y
, param(ID::Y_WIDTH
));
1057 check_expected(true);
1061 if (cell
->type
.in(ID($add
), ID($sub
), ID($mul
), ID($div
), ID($mod
), ID($divfloor
), ID($modfloor
), ID($pow
))) {
1062 param_bool(ID::A_SIGNED
);
1063 param_bool(ID::B_SIGNED
);
1064 port(ID::A
, param(ID::A_WIDTH
));
1065 port(ID::B
, param(ID::B_WIDTH
));
1066 port(ID::Y
, param(ID::Y_WIDTH
));
1067 check_expected(cell
->type
!= ID($pow
));
1071 if (cell
->type
== ID($fa
)) {
1072 port(ID::A
, param(ID::WIDTH
));
1073 port(ID::B
, param(ID::WIDTH
));
1074 port(ID::C
, param(ID::WIDTH
));
1075 port(ID::X
, param(ID::WIDTH
));
1076 port(ID::Y
, param(ID::WIDTH
));
1081 if (cell
->type
== ID($lcu
)) {
1082 port(ID::P
, param(ID::WIDTH
));
1083 port(ID::G
, param(ID::WIDTH
));
1085 port(ID::CO
, param(ID::WIDTH
));
1090 if (cell
->type
== ID($alu
)) {
1091 param_bool(ID::A_SIGNED
);
1092 param_bool(ID::B_SIGNED
);
1093 port(ID::A
, param(ID::A_WIDTH
));
1094 port(ID::B
, param(ID::B_WIDTH
));
1097 port(ID::X
, param(ID::Y_WIDTH
));
1098 port(ID::Y
, param(ID::Y_WIDTH
));
1099 port(ID::CO
, param(ID::Y_WIDTH
));
1100 check_expected(true);
1104 if (cell
->type
== ID($macc
)) {
1106 param(ID::CONFIG_WIDTH
);
1107 port(ID::A
, param(ID::A_WIDTH
));
1108 port(ID::B
, param(ID::B_WIDTH
));
1109 port(ID::Y
, param(ID::Y_WIDTH
));
1111 Macc().from_cell(cell
);
1115 if (cell
->type
== ID($logic_not
)) {
1116 param_bool(ID::A_SIGNED
);
1117 port(ID::A
, param(ID::A_WIDTH
));
1118 port(ID::Y
, param(ID::Y_WIDTH
));
1123 if (cell
->type
.in(ID($logic_and
), ID($logic_or
))) {
1124 param_bool(ID::A_SIGNED
);
1125 param_bool(ID::B_SIGNED
);
1126 port(ID::A
, param(ID::A_WIDTH
));
1127 port(ID::B
, param(ID::B_WIDTH
));
1128 port(ID::Y
, param(ID::Y_WIDTH
));
1129 check_expected(/*check_matched_sign=*/false);
1133 if (cell
->type
== ID($slice
)) {
1135 port(ID::A
, param(ID::A_WIDTH
));
1136 port(ID::Y
, param(ID::Y_WIDTH
));
1137 if (param(ID::OFFSET
) + param(ID::Y_WIDTH
) > param(ID::A_WIDTH
))
1143 if (cell
->type
== ID($concat
)) {
1144 port(ID::A
, param(ID::A_WIDTH
));
1145 port(ID::B
, param(ID::B_WIDTH
));
1146 port(ID::Y
, param(ID::A_WIDTH
) + param(ID::B_WIDTH
));
1151 if (cell
->type
== ID($mux
)) {
1152 port(ID::A
, param(ID::WIDTH
));
1153 port(ID::B
, param(ID::WIDTH
));
1155 port(ID::Y
, param(ID::WIDTH
));
1160 if (cell
->type
== ID($pmux
)) {
1161 port(ID::A
, param(ID::WIDTH
));
1162 port(ID::B
, param(ID::WIDTH
) * param(ID::S_WIDTH
));
1163 port(ID::S
, param(ID::S_WIDTH
));
1164 port(ID::Y
, param(ID::WIDTH
));
1169 if (cell
->type
== ID($lut
)) {
1171 port(ID::A
, param(ID::WIDTH
));
1177 if (cell
->type
== ID($sop
)) {
1180 port(ID::A
, param(ID::WIDTH
));
1186 if (cell
->type
== ID($sr
)) {
1187 param_bool(ID::SET_POLARITY
);
1188 param_bool(ID::CLR_POLARITY
);
1189 port(ID::SET
, param(ID::WIDTH
));
1190 port(ID::CLR
, param(ID::WIDTH
));
1191 port(ID::Q
, param(ID::WIDTH
));
1196 if (cell
->type
== ID($ff
)) {
1197 port(ID::D
, param(ID::WIDTH
));
1198 port(ID::Q
, param(ID::WIDTH
));
1203 if (cell
->type
== ID($dff
)) {
1204 param_bool(ID::CLK_POLARITY
);
1206 port(ID::D
, param(ID::WIDTH
));
1207 port(ID::Q
, param(ID::WIDTH
));
1212 if (cell
->type
== ID($dffe
)) {
1213 param_bool(ID::CLK_POLARITY
);
1214 param_bool(ID::EN_POLARITY
);
1217 port(ID::D
, param(ID::WIDTH
));
1218 port(ID::Q
, param(ID::WIDTH
));
1223 if (cell
->type
== ID($dffsr
)) {
1224 param_bool(ID::CLK_POLARITY
);
1225 param_bool(ID::SET_POLARITY
);
1226 param_bool(ID::CLR_POLARITY
);
1228 port(ID::SET
, param(ID::WIDTH
));
1229 port(ID::CLR
, param(ID::WIDTH
));
1230 port(ID::D
, param(ID::WIDTH
));
1231 port(ID::Q
, param(ID::WIDTH
));
1236 if (cell
->type
== ID($dffsre
)) {
1237 param_bool(ID::CLK_POLARITY
);
1238 param_bool(ID::SET_POLARITY
);
1239 param_bool(ID::CLR_POLARITY
);
1240 param_bool(ID::EN_POLARITY
);
1243 port(ID::SET
, param(ID::WIDTH
));
1244 port(ID::CLR
, param(ID::WIDTH
));
1245 port(ID::D
, param(ID::WIDTH
));
1246 port(ID::Q
, param(ID::WIDTH
));
1251 if (cell
->type
== ID($adff
)) {
1252 param_bool(ID::CLK_POLARITY
);
1253 param_bool(ID::ARST_POLARITY
);
1254 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1257 port(ID::D
, param(ID::WIDTH
));
1258 port(ID::Q
, param(ID::WIDTH
));
1263 if (cell
->type
== ID($sdff
)) {
1264 param_bool(ID::CLK_POLARITY
);
1265 param_bool(ID::SRST_POLARITY
);
1266 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1269 port(ID::D
, param(ID::WIDTH
));
1270 port(ID::Q
, param(ID::WIDTH
));
1275 if (cell
->type
.in(ID($sdffe
), ID($sdffce
))) {
1276 param_bool(ID::CLK_POLARITY
);
1277 param_bool(ID::EN_POLARITY
);
1278 param_bool(ID::SRST_POLARITY
);
1279 param_bits(ID::SRST_VALUE
, param(ID::WIDTH
));
1283 port(ID::D
, param(ID::WIDTH
));
1284 port(ID::Q
, param(ID::WIDTH
));
1289 if (cell
->type
== ID($adffe
)) {
1290 param_bool(ID::CLK_POLARITY
);
1291 param_bool(ID::EN_POLARITY
);
1292 param_bool(ID::ARST_POLARITY
);
1293 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1297 port(ID::D
, param(ID::WIDTH
));
1298 port(ID::Q
, param(ID::WIDTH
));
1303 if (cell
->type
== ID($dlatch
)) {
1304 param_bool(ID::EN_POLARITY
);
1306 port(ID::D
, param(ID::WIDTH
));
1307 port(ID::Q
, param(ID::WIDTH
));
1312 if (cell
->type
== ID($adlatch
)) {
1313 param_bool(ID::EN_POLARITY
);
1314 param_bool(ID::ARST_POLARITY
);
1315 param_bits(ID::ARST_VALUE
, param(ID::WIDTH
));
1318 port(ID::D
, param(ID::WIDTH
));
1319 port(ID::Q
, param(ID::WIDTH
));
1324 if (cell
->type
== ID($dlatchsr
)) {
1325 param_bool(ID::EN_POLARITY
);
1326 param_bool(ID::SET_POLARITY
);
1327 param_bool(ID::CLR_POLARITY
);
1329 port(ID::SET
, param(ID::WIDTH
));
1330 port(ID::CLR
, param(ID::WIDTH
));
1331 port(ID::D
, param(ID::WIDTH
));
1332 port(ID::Q
, param(ID::WIDTH
));
1337 if (cell
->type
== ID($fsm
)) {
1339 param_bool(ID::CLK_POLARITY
);
1340 param_bool(ID::ARST_POLARITY
);
1341 param(ID::STATE_BITS
);
1342 param(ID::STATE_NUM
);
1343 param(ID::STATE_NUM_LOG2
);
1344 param(ID::STATE_RST
);
1345 param_bits(ID::STATE_TABLE
, param(ID::STATE_BITS
) * param(ID::STATE_NUM
));
1346 param(ID::TRANS_NUM
);
1347 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
)));
1350 port(ID::CTRL_IN
, param(ID::CTRL_IN_WIDTH
));
1351 port(ID::CTRL_OUT
, param(ID::CTRL_OUT_WIDTH
));
1356 if (cell
->type
== ID($memrd
)) {
1358 param_bool(ID::CLK_ENABLE
);
1359 param_bool(ID::CLK_POLARITY
);
1360 param_bool(ID::TRANSPARENT
);
1363 port(ID::ADDR
, param(ID::ABITS
));
1364 port(ID::DATA
, param(ID::WIDTH
));
1369 if (cell
->type
== ID($memwr
)) {
1371 param_bool(ID::CLK_ENABLE
);
1372 param_bool(ID::CLK_POLARITY
);
1373 param(ID::PRIORITY
);
1375 port(ID::EN
, param(ID::WIDTH
));
1376 port(ID::ADDR
, param(ID::ABITS
));
1377 port(ID::DATA
, param(ID::WIDTH
));
1382 if (cell
->type
== ID($meminit
)) {
1384 param(ID::PRIORITY
);
1385 port(ID::ADDR
, param(ID::ABITS
));
1386 port(ID::DATA
, param(ID::WIDTH
) * param(ID::WORDS
));
1391 if (cell
->type
== ID($mem
)) {
1396 param_bits(ID::RD_CLK_ENABLE
, max(1, param(ID::RD_PORTS
)));
1397 param_bits(ID::RD_CLK_POLARITY
, max(1, param(ID::RD_PORTS
)));
1398 param_bits(ID::RD_TRANSPARENT
, max(1, param(ID::RD_PORTS
)));
1399 param_bits(ID::WR_CLK_ENABLE
, max(1, param(ID::WR_PORTS
)));
1400 param_bits(ID::WR_CLK_POLARITY
, max(1, param(ID::WR_PORTS
)));
1401 port(ID::RD_CLK
, param(ID::RD_PORTS
));
1402 port(ID::RD_EN
, param(ID::RD_PORTS
));
1403 port(ID::RD_ADDR
, param(ID::RD_PORTS
) * param(ID::ABITS
));
1404 port(ID::RD_DATA
, param(ID::RD_PORTS
) * param(ID::WIDTH
));
1405 port(ID::WR_CLK
, param(ID::WR_PORTS
));
1406 port(ID::WR_EN
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1407 port(ID::WR_ADDR
, param(ID::WR_PORTS
) * param(ID::ABITS
));
1408 port(ID::WR_DATA
, param(ID::WR_PORTS
) * param(ID::WIDTH
));
1413 if (cell
->type
== ID($tribuf
)) {
1414 port(ID::A
, param(ID::WIDTH
));
1415 port(ID::Y
, param(ID::WIDTH
));
1421 if (cell
->type
.in(ID($
assert), ID($assume
), ID($live
), ID($fair
), ID($cover
))) {
1428 if (cell
->type
== ID($initstate
)) {
1434 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
))) {
1435 port(ID::Y
, param(ID::WIDTH
));
1440 if (cell
->type
== ID($equiv
)) {
1448 if (cell
->type
.in(ID($specify2
), ID($specify3
))) {
1449 param_bool(ID::FULL
);
1450 param_bool(ID::SRC_DST_PEN
);
1451 param_bool(ID::SRC_DST_POL
);
1452 param(ID::T_RISE_MIN
);
1453 param(ID::T_RISE_TYP
);
1454 param(ID::T_RISE_MAX
);
1455 param(ID::T_FALL_MIN
);
1456 param(ID::T_FALL_TYP
);
1457 param(ID::T_FALL_MAX
);
1459 port(ID::SRC
, param(ID::SRC_WIDTH
));
1460 port(ID::DST
, param(ID::DST_WIDTH
));
1461 if (cell
->type
== ID($specify3
)) {
1462 param_bool(ID::EDGE_EN
);
1463 param_bool(ID::EDGE_POL
);
1464 param_bool(ID::DAT_DST_PEN
);
1465 param_bool(ID::DAT_DST_POL
);
1466 port(ID::DAT
, param(ID::DST_WIDTH
));
1472 if (cell
->type
== ID($specrule
)) {
1474 param_bool(ID::SRC_PEN
);
1475 param_bool(ID::SRC_POL
);
1476 param_bool(ID::DST_PEN
);
1477 param_bool(ID::DST_POL
);
1478 param(ID::T_LIMIT_MIN
);
1479 param(ID::T_LIMIT_TYP
);
1480 param(ID::T_LIMIT_MAX
);
1481 param(ID::T_LIMIT2_MIN
);
1482 param(ID::T_LIMIT2_TYP
);
1483 param(ID::T_LIMIT2_MAX
);
1484 port(ID::SRC_EN
, 1);
1485 port(ID::DST_EN
, 1);
1486 port(ID::SRC
, param(ID::SRC_WIDTH
));
1487 port(ID::DST
, param(ID::DST_WIDTH
));
1492 if (cell
->type
== ID($_BUF_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1493 if (cell
->type
== ID($_NOT_
)) { port(ID::A
,1); port(ID::Y
,1); check_expected(); return; }
1494 if (cell
->type
== ID($_AND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1495 if (cell
->type
== ID($_NAND_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1496 if (cell
->type
== ID($_OR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1497 if (cell
->type
== ID($_NOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1498 if (cell
->type
== ID($_XOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1499 if (cell
->type
== ID($_XNOR_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1500 if (cell
->type
== ID($_ANDNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1501 if (cell
->type
== ID($_ORNOT_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::Y
,1); check_expected(); return; }
1502 if (cell
->type
== ID($_MUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1503 if (cell
->type
== ID($_NMUX_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::S
,1); port(ID::Y
,1); check_expected(); return; }
1504 if (cell
->type
== ID($_AOI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1505 if (cell
->type
== ID($_OAI3_
)) { port(ID::A
,1); port(ID::B
,1); port(ID::C
,1); port(ID::Y
,1); check_expected(); return; }
1506 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; }
1507 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; }
1509 if (cell
->type
== ID($_TBUF_
)) { port(ID::A
,1); port(ID::Y
,1); port(ID::E
,1); check_expected(); return; }
1511 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; }
1512 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; }
1513 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; }
1515 if (cell
->type
.in(ID($_SR_NN_
), ID($_SR_NP_
), ID($_SR_PN_
), ID($_SR_PP_
)))
1516 { port(ID::S
,1); port(ID::R
,1); port(ID::Q
,1); check_expected(); return; }
1518 if (cell
->type
== ID($_FF_
)) { port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1520 if (cell
->type
.in(ID($_DFF_N_
), ID($_DFF_P_
)))
1521 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); check_expected(); return; }
1523 if (cell
->type
.in(ID($_DFFE_NN_
), ID($_DFFE_NP_
), ID($_DFFE_PN_
), ID($_DFFE_PP_
)))
1524 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::E
,1); check_expected(); return; }
1527 ID($_DFF_NN0_
), ID($_DFF_NN1_
), ID($_DFF_NP0_
), ID($_DFF_NP1_
),
1528 ID($_DFF_PN0_
), ID($_DFF_PN1_
), ID($_DFF_PP0_
), ID($_DFF_PP1_
)))
1529 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1532 ID($_DFFE_NN0N_
), ID($_DFFE_NN0P_
), ID($_DFFE_NN1N_
), ID($_DFFE_NN1P_
),
1533 ID($_DFFE_NP0N_
), ID($_DFFE_NP0P_
), ID($_DFFE_NP1N_
), ID($_DFFE_NP1P_
),
1534 ID($_DFFE_PN0N_
), ID($_DFFE_PN0P_
), ID($_DFFE_PN1N_
), ID($_DFFE_PN1P_
),
1535 ID($_DFFE_PP0N_
), ID($_DFFE_PP0P_
), ID($_DFFE_PP1N_
), ID($_DFFE_PP1P_
)))
1536 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1539 ID($_DFFSR_NNN_
), ID($_DFFSR_NNP_
), ID($_DFFSR_NPN_
), ID($_DFFSR_NPP_
),
1540 ID($_DFFSR_PNN_
), ID($_DFFSR_PNP_
), ID($_DFFSR_PPN_
), ID($_DFFSR_PPP_
)))
1541 { port(ID::C
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1544 ID($_DFFSRE_NNNN_
), ID($_DFFSRE_NNNP_
), ID($_DFFSRE_NNPN_
), ID($_DFFSRE_NNPP_
),
1545 ID($_DFFSRE_NPNN_
), ID($_DFFSRE_NPNP_
), ID($_DFFSRE_NPPN_
), ID($_DFFSRE_NPPP_
),
1546 ID($_DFFSRE_PNNN_
), ID($_DFFSRE_PNNP_
), ID($_DFFSRE_PNPN_
), ID($_DFFSRE_PNPP_
),
1547 ID($_DFFSRE_PPNN_
), ID($_DFFSRE_PPNP_
), ID($_DFFSRE_PPPN_
), ID($_DFFSRE_PPPP_
)))
1548 { 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; }
1551 ID($_SDFF_NN0_
), ID($_SDFF_NN1_
), ID($_SDFF_NP0_
), ID($_SDFF_NP1_
),
1552 ID($_SDFF_PN0_
), ID($_SDFF_PN1_
), ID($_SDFF_PP0_
), ID($_SDFF_PP1_
)))
1553 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); check_expected(); return; }
1556 ID($_SDFFE_NN0N_
), ID($_SDFFE_NN0P_
), ID($_SDFFE_NN1N_
), ID($_SDFFE_NN1P_
),
1557 ID($_SDFFE_NP0N_
), ID($_SDFFE_NP0P_
), ID($_SDFFE_NP1N_
), ID($_SDFFE_NP1P_
),
1558 ID($_SDFFE_PN0N_
), ID($_SDFFE_PN0P_
), ID($_SDFFE_PN1N_
), ID($_SDFFE_PN1P_
),
1559 ID($_SDFFE_PP0N_
), ID($_SDFFE_PP0P_
), ID($_SDFFE_PP1N_
), ID($_SDFFE_PP1P_
),
1560 ID($_SDFFCE_NN0N_
), ID($_SDFFCE_NN0P_
), ID($_SDFFCE_NN1N_
), ID($_SDFFCE_NN1P_
),
1561 ID($_SDFFCE_NP0N_
), ID($_SDFFCE_NP0P_
), ID($_SDFFCE_NP1N_
), ID($_SDFFCE_NP1P_
),
1562 ID($_SDFFCE_PN0N_
), ID($_SDFFCE_PN0P_
), ID($_SDFFCE_PN1N_
), ID($_SDFFCE_PN1P_
),
1563 ID($_SDFFCE_PP0N_
), ID($_SDFFCE_PP0P_
), ID($_SDFFCE_PP1N_
), ID($_SDFFCE_PP1P_
)))
1564 { port(ID::D
,1); port(ID::Q
,1); port(ID::C
,1); port(ID::R
,1); port(ID::E
,1); check_expected(); return; }
1566 if (cell
->type
.in(ID($_DLATCH_N_
), ID($_DLATCH_P_
)))
1567 { port(ID::E
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1570 ID($_DLATCH_NN0_
), ID($_DLATCH_NN1_
), ID($_DLATCH_NP0_
), ID($_DLATCH_NP1_
),
1571 ID($_DLATCH_PN0_
), ID($_DLATCH_PN1_
), ID($_DLATCH_PP0_
), ID($_DLATCH_PP1_
)))
1572 { port(ID::E
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1575 ID($_DLATCHSR_NNN_
), ID($_DLATCHSR_NNP_
), ID($_DLATCHSR_NPN_
), ID($_DLATCHSR_NPP_
),
1576 ID($_DLATCHSR_PNN_
), ID($_DLATCHSR_PNP_
), ID($_DLATCHSR_PPN_
), ID($_DLATCHSR_PPP_
)))
1577 { port(ID::E
,1); port(ID::S
,1); port(ID::R
,1); port(ID::D
,1); port(ID::Q
,1); check_expected(); return; }
1585 void RTLIL::Module::sort()
1587 wires_
.sort(sort_by_id_str());
1588 cells_
.sort(sort_by_id_str());
1589 parameter_default_values
.sort(sort_by_id_str());
1590 memories
.sort(sort_by_id_str());
1591 processes
.sort(sort_by_id_str());
1592 for (auto &it
: cells_
)
1594 for (auto &it
: wires_
)
1595 it
.second
->attributes
.sort(sort_by_id_str());
1596 for (auto &it
: memories
)
1597 it
.second
->attributes
.sort(sort_by_id_str());
1600 void RTLIL::Module::check()
1603 std::vector
<bool> ports_declared
;
1604 for (auto &it
: wires_
) {
1605 log_assert(this == it
.second
->module
);
1606 log_assert(it
.first
== it
.second
->name
);
1607 log_assert(!it
.first
.empty());
1608 log_assert(it
.second
->width
>= 0);
1609 log_assert(it
.second
->port_id
>= 0);
1610 for (auto &it2
: it
.second
->attributes
)
1611 log_assert(!it2
.first
.empty());
1612 if (it
.second
->port_id
) {
1613 log_assert(GetSize(ports
) >= it
.second
->port_id
);
1614 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
1615 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
1616 if (GetSize(ports_declared
) < it
.second
->port_id
)
1617 ports_declared
.resize(it
.second
->port_id
);
1618 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
1619 ports_declared
[it
.second
->port_id
-1] = true;
1621 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
1623 for (auto port_declared
: ports_declared
)
1624 log_assert(port_declared
== true);
1625 log_assert(GetSize(ports
) == GetSize(ports_declared
));
1627 for (auto &it
: memories
) {
1628 log_assert(it
.first
== it
.second
->name
);
1629 log_assert(!it
.first
.empty());
1630 log_assert(it
.second
->width
>= 0);
1631 log_assert(it
.second
->size
>= 0);
1632 for (auto &it2
: it
.second
->attributes
)
1633 log_assert(!it2
.first
.empty());
1636 for (auto &it
: cells_
) {
1637 log_assert(this == it
.second
->module
);
1638 log_assert(it
.first
== it
.second
->name
);
1639 log_assert(!it
.first
.empty());
1640 log_assert(!it
.second
->type
.empty());
1641 for (auto &it2
: it
.second
->connections()) {
1642 log_assert(!it2
.first
.empty());
1645 for (auto &it2
: it
.second
->attributes
)
1646 log_assert(!it2
.first
.empty());
1647 for (auto &it2
: it
.second
->parameters
)
1648 log_assert(!it2
.first
.empty());
1649 InternalCellChecker
checker(this, it
.second
);
1653 for (auto &it
: processes
) {
1654 log_assert(it
.first
== it
.second
->name
);
1655 log_assert(!it
.first
.empty());
1656 log_assert(it
.second
->root_case
.compare
.empty());
1657 std::vector
<CaseRule
*> all_cases
= {&it
.second
->root_case
};
1658 for (size_t i
= 0; i
< all_cases
.size(); i
++) {
1659 for (auto &switch_it
: all_cases
[i
]->switches
) {
1660 for (auto &case_it
: switch_it
->cases
) {
1661 for (auto &compare_it
: case_it
->compare
) {
1662 log_assert(switch_it
->signal
.size() == compare_it
.size());
1664 all_cases
.push_back(case_it
);
1668 for (auto &sync_it
: it
.second
->syncs
) {
1669 switch (sync_it
->type
) {
1675 log_assert(!sync_it
->signal
.empty());
1680 log_assert(sync_it
->signal
.empty());
1686 for (auto &it
: connections_
) {
1687 log_assert(it
.first
.size() == it
.second
.size());
1688 log_assert(!it
.first
.has_const());
1693 for (auto &it
: attributes
)
1694 log_assert(!it
.first
.empty());
1698 void RTLIL::Module::optimize()
1702 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
1704 log_assert(new_mod
->refcount_wires_
== 0);
1705 log_assert(new_mod
->refcount_cells_
== 0);
1707 new_mod
->avail_parameters
= avail_parameters
;
1708 new_mod
->parameter_default_values
= parameter_default_values
;
1710 for (auto &conn
: connections_
)
1711 new_mod
->connect(conn
);
1713 for (auto &attr
: attributes
)
1714 new_mod
->attributes
[attr
.first
] = attr
.second
;
1716 for (auto &it
: wires_
)
1717 new_mod
->addWire(it
.first
, it
.second
);
1719 for (auto &it
: memories
)
1720 new_mod
->addMemory(it
.first
, it
.second
);
1722 for (auto &it
: cells_
)
1723 new_mod
->addCell(it
.first
, it
.second
);
1725 for (auto &it
: processes
)
1726 new_mod
->addProcess(it
.first
, it
.second
);
1728 struct RewriteSigSpecWorker
1731 void operator()(RTLIL::SigSpec
&sig
)
1734 for (auto &c
: sig
.chunks_
)
1736 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1740 RewriteSigSpecWorker rewriteSigSpecWorker
;
1741 rewriteSigSpecWorker
.mod
= new_mod
;
1742 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1743 new_mod
->fixup_ports();
1746 RTLIL::Module
*RTLIL::Module::clone() const
1748 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1749 new_mod
->name
= name
;
1754 bool RTLIL::Module::has_memories() const
1756 return !memories
.empty();
1759 bool RTLIL::Module::has_processes() const
1761 return !processes
.empty();
1764 bool RTLIL::Module::has_memories_warn() const
1766 if (!memories
.empty())
1767 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1768 return !memories
.empty();
1771 bool RTLIL::Module::has_processes_warn() const
1773 if (!processes
.empty())
1774 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1775 return !processes
.empty();
1778 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1780 std::vector
<RTLIL::Wire
*> result
;
1781 result
.reserve(wires_
.size());
1782 for (auto &it
: wires_
)
1783 if (design
->selected(this, it
.second
))
1784 result
.push_back(it
.second
);
1788 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1790 std::vector
<RTLIL::Cell
*> result
;
1791 result
.reserve(cells_
.size());
1792 for (auto &it
: cells_
)
1793 if (design
->selected(this, it
.second
))
1794 result
.push_back(it
.second
);
1798 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1800 log_assert(!wire
->name
.empty());
1801 log_assert(count_id(wire
->name
) == 0);
1802 log_assert(refcount_wires_
== 0);
1803 wires_
[wire
->name
] = wire
;
1804 wire
->module
= this;
1807 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1809 log_assert(!cell
->name
.empty());
1810 log_assert(count_id(cell
->name
) == 0);
1811 log_assert(refcount_cells_
== 0);
1812 cells_
[cell
->name
] = cell
;
1813 cell
->module
= this;
1816 void RTLIL::Module::remove(const pool
<RTLIL::Wire
*> &wires
)
1818 log_assert(refcount_wires_
== 0);
1820 struct DeleteWireWorker
1822 RTLIL::Module
*module
;
1823 const pool
<RTLIL::Wire
*> *wires_p
;
1825 void operator()(RTLIL::SigSpec
&sig
) {
1827 for (auto &c
: sig
.chunks_
)
1828 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1829 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1834 void operator()(RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&rhs
) {
1835 log_assert(GetSize(lhs
) == GetSize(rhs
));
1838 for (int i
= 0; i
< GetSize(lhs
); i
++) {
1839 RTLIL::SigBit
&lhs_bit
= lhs
.bits_
[i
];
1840 RTLIL::SigBit
&rhs_bit
= rhs
.bits_
[i
];
1841 if ((lhs_bit
.wire
!= nullptr && wires_p
->count(lhs_bit
.wire
)) || (rhs_bit
.wire
!= nullptr && wires_p
->count(rhs_bit
.wire
))) {
1842 lhs_bit
= State::Sx
;
1843 rhs_bit
= State::Sx
;
1849 DeleteWireWorker delete_wire_worker
;
1850 delete_wire_worker
.module
= this;
1851 delete_wire_worker
.wires_p
= &wires
;
1852 rewrite_sigspecs2(delete_wire_worker
);
1854 for (auto &it
: wires
) {
1855 log_assert(wires_
.count(it
->name
) != 0);
1856 wires_
.erase(it
->name
);
1861 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1863 while (!cell
->connections_
.empty())
1864 cell
->unsetPort(cell
->connections_
.begin()->first
);
1866 log_assert(cells_
.count(cell
->name
) != 0);
1867 log_assert(refcount_cells_
== 0);
1868 cells_
.erase(cell
->name
);
1872 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1874 log_assert(wires_
[wire
->name
] == wire
);
1875 log_assert(refcount_wires_
== 0);
1876 wires_
.erase(wire
->name
);
1877 wire
->name
= new_name
;
1881 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1883 log_assert(cells_
[cell
->name
] == cell
);
1884 log_assert(refcount_wires_
== 0);
1885 cells_
.erase(cell
->name
);
1886 cell
->name
= new_name
;
1890 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1892 log_assert(count_id(old_name
) != 0);
1893 if (wires_
.count(old_name
))
1894 rename(wires_
.at(old_name
), new_name
);
1895 else if (cells_
.count(old_name
))
1896 rename(cells_
.at(old_name
), new_name
);
1901 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1903 log_assert(wires_
[w1
->name
] == w1
);
1904 log_assert(wires_
[w2
->name
] == w2
);
1905 log_assert(refcount_wires_
== 0);
1907 wires_
.erase(w1
->name
);
1908 wires_
.erase(w2
->name
);
1910 std::swap(w1
->name
, w2
->name
);
1912 wires_
[w1
->name
] = w1
;
1913 wires_
[w2
->name
] = w2
;
1916 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1918 log_assert(cells_
[c1
->name
] == c1
);
1919 log_assert(cells_
[c2
->name
] == c2
);
1920 log_assert(refcount_cells_
== 0);
1922 cells_
.erase(c1
->name
);
1923 cells_
.erase(c2
->name
);
1925 std::swap(c1
->name
, c2
->name
);
1927 cells_
[c1
->name
] = c1
;
1928 cells_
[c2
->name
] = c2
;
1931 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1934 return uniquify(name
, index
);
1937 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1940 if (count_id(name
) == 0)
1946 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1947 if (count_id(new_name
) == 0)
1953 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1955 if (a
->port_id
&& !b
->port_id
)
1957 if (!a
->port_id
&& b
->port_id
)
1960 if (a
->port_id
== b
->port_id
)
1961 return a
->name
< b
->name
;
1962 return a
->port_id
< b
->port_id
;
1965 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1967 for (auto mon
: monitors
)
1968 mon
->notify_connect(this, conn
);
1971 for (auto mon
: design
->monitors
)
1972 mon
->notify_connect(this, conn
);
1974 // ignore all attempts to assign constants to other constants
1975 if (conn
.first
.has_const()) {
1976 RTLIL::SigSig new_conn
;
1977 for (int i
= 0; i
< GetSize(conn
.first
); i
++)
1978 if (conn
.first
[i
].wire
) {
1979 new_conn
.first
.append(conn
.first
[i
]);
1980 new_conn
.second
.append(conn
.second
[i
]);
1982 if (GetSize(new_conn
.first
))
1988 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
));
1989 log_backtrace("-X- ", yosys_xtrace
-1);
1992 log_assert(GetSize(conn
.first
) == GetSize(conn
.second
));
1993 connections_
.push_back(conn
);
1996 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1998 connect(RTLIL::SigSig(lhs
, rhs
));
2001 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
2003 for (auto mon
: monitors
)
2004 mon
->notify_connect(this, new_conn
);
2007 for (auto mon
: design
->monitors
)
2008 mon
->notify_connect(this, new_conn
);
2011 log("#X# New connections vector in %s:\n", log_id(this));
2012 for (auto &conn
: new_conn
)
2013 log("#X# %s = %s (%d bits)\n", log_signal(conn
.first
), log_signal(conn
.second
), GetSize(conn
.first
));
2014 log_backtrace("-X- ", yosys_xtrace
-1);
2017 connections_
= new_conn
;
2020 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
2022 return connections_
;
2025 void RTLIL::Module::fixup_ports()
2027 std::vector
<RTLIL::Wire
*> all_ports
;
2029 for (auto &w
: wires_
)
2030 if (w
.second
->port_input
|| w
.second
->port_output
)
2031 all_ports
.push_back(w
.second
);
2033 w
.second
->port_id
= 0;
2035 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
2038 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
2039 ports
.push_back(all_ports
[i
]->name
);
2040 all_ports
[i
]->port_id
= i
+1;
2044 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
2046 RTLIL::Wire
*wire
= new RTLIL::Wire
;
2048 wire
->width
= width
;
2053 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
2055 RTLIL::Wire
*wire
= addWire(name
);
2056 wire
->width
= other
->width
;
2057 wire
->start_offset
= other
->start_offset
;
2058 wire
->port_id
= other
->port_id
;
2059 wire
->port_input
= other
->port_input
;
2060 wire
->port_output
= other
->port_output
;
2061 wire
->upto
= other
->upto
;
2062 wire
->is_signed
= other
->is_signed
;
2063 wire
->attributes
= other
->attributes
;
2067 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
2069 RTLIL::Cell
*cell
= new RTLIL::Cell
;
2076 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
2078 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
2079 cell
->connections_
= other
->connections_
;
2080 cell
->parameters
= other
->parameters
;
2081 cell
->attributes
= other
->attributes
;
2085 RTLIL::Memory
*RTLIL::Module::addMemory(RTLIL::IdString name
, const RTLIL::Memory
*other
)
2087 RTLIL::Memory
*mem
= new RTLIL::Memory
;
2089 mem
->width
= other
->width
;
2090 mem
->start_offset
= other
->start_offset
;
2091 mem
->size
= other
->size
;
2092 mem
->attributes
= other
->attributes
;
2093 memories
[mem
->name
] = mem
;
2097 RTLIL::Process
*RTLIL::Module::addProcess(RTLIL::IdString name
, const RTLIL::Process
*other
)
2099 RTLIL::Process
*proc
= other
->clone();
2101 processes
[name
] = proc
;
2105 #define DEF_METHOD(_func, _y_size, _type) \
2106 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) { \
2107 RTLIL::Cell *cell = addCell(name, _type); \
2108 cell->parameters[ID::A_SIGNED] = is_signed; \
2109 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2110 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2111 cell->setPort(ID::A, sig_a); \
2112 cell->setPort(ID::Y, sig_y); \
2113 cell->set_src_attribute(src); \
2116 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed, const std::string &src) { \
2117 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2118 add ## _func(name, sig_a, sig_y, is_signed, src); \
2121 DEF_METHOD(Not
, sig_a
.size(), ID($
not))
2122 DEF_METHOD(Pos
, sig_a
.size(), ID($pos
))
2123 DEF_METHOD(Neg
, sig_a
.size(), ID($neg
))
2124 DEF_METHOD(ReduceAnd
, 1, ID($reduce_and
))
2125 DEF_METHOD(ReduceOr
, 1, ID($reduce_or
))
2126 DEF_METHOD(ReduceXor
, 1, ID($reduce_xor
))
2127 DEF_METHOD(ReduceXnor
, 1, ID($reduce_xnor
))
2128 DEF_METHOD(ReduceBool
, 1, ID($reduce_bool
))
2129 DEF_METHOD(LogicNot
, 1, ID($logic_not
))
2132 #define DEF_METHOD(_func, _y_size, _type) \
2133 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) { \
2134 RTLIL::Cell *cell = addCell(name, _type); \
2135 cell->parameters[ID::A_SIGNED] = is_signed; \
2136 cell->parameters[ID::B_SIGNED] = is_signed; \
2137 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2138 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2139 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2140 cell->setPort(ID::A, sig_a); \
2141 cell->setPort(ID::B, sig_b); \
2142 cell->setPort(ID::Y, sig_y); \
2143 cell->set_src_attribute(src); \
2146 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) { \
2147 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2148 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2151 DEF_METHOD(And
, max(sig_a
.size(), sig_b
.size()), ID($
and))
2152 DEF_METHOD(Or
, max(sig_a
.size(), sig_b
.size()), ID($
or))
2153 DEF_METHOD(Xor
, max(sig_a
.size(), sig_b
.size()), ID($
xor))
2154 DEF_METHOD(Xnor
, max(sig_a
.size(), sig_b
.size()), ID($xnor
))
2155 DEF_METHOD(Shift
, sig_a
.size(), ID($shift
))
2156 DEF_METHOD(Shiftx
, sig_a
.size(), ID($shiftx
))
2157 DEF_METHOD(Lt
, 1, ID($lt
))
2158 DEF_METHOD(Le
, 1, ID($le
))
2159 DEF_METHOD(Eq
, 1, ID($eq
))
2160 DEF_METHOD(Ne
, 1, ID($ne
))
2161 DEF_METHOD(Eqx
, 1, ID($eqx
))
2162 DEF_METHOD(Nex
, 1, ID($nex
))
2163 DEF_METHOD(Ge
, 1, ID($ge
))
2164 DEF_METHOD(Gt
, 1, ID($gt
))
2165 DEF_METHOD(Add
, max(sig_a
.size(), sig_b
.size()), ID($add
))
2166 DEF_METHOD(Sub
, max(sig_a
.size(), sig_b
.size()), ID($sub
))
2167 DEF_METHOD(Mul
, max(sig_a
.size(), sig_b
.size()), ID($mul
))
2168 DEF_METHOD(Div
, max(sig_a
.size(), sig_b
.size()), ID($div
))
2169 DEF_METHOD(Mod
, max(sig_a
.size(), sig_b
.size()), ID($mod
))
2170 DEF_METHOD(DivFloor
, max(sig_a
.size(), sig_b
.size()), ID($divfloor
))
2171 DEF_METHOD(ModFloor
, max(sig_a
.size(), sig_b
.size()), ID($modfloor
))
2172 DEF_METHOD(LogicAnd
, 1, ID($logic_and
))
2173 DEF_METHOD(LogicOr
, 1, ID($logic_or
))
2176 #define DEF_METHOD(_func, _y_size, _type) \
2177 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) { \
2178 RTLIL::Cell *cell = addCell(name, _type); \
2179 cell->parameters[ID::A_SIGNED] = is_signed; \
2180 cell->parameters[ID::B_SIGNED] = false; \
2181 cell->parameters[ID::A_WIDTH] = sig_a.size(); \
2182 cell->parameters[ID::B_WIDTH] = sig_b.size(); \
2183 cell->parameters[ID::Y_WIDTH] = sig_y.size(); \
2184 cell->setPort(ID::A, sig_a); \
2185 cell->setPort(ID::B, sig_b); \
2186 cell->setPort(ID::Y, sig_y); \
2187 cell->set_src_attribute(src); \
2190 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) { \
2191 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
2192 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
2195 DEF_METHOD(Shl
, sig_a
.size(), ID($shl
))
2196 DEF_METHOD(Shr
, sig_a
.size(), ID($shr
))
2197 DEF_METHOD(Sshl
, sig_a
.size(), ID($sshl
))
2198 DEF_METHOD(Sshr
, sig_a
.size(), ID($sshr
))
2201 #define DEF_METHOD(_func, _type, _pmux) \
2202 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) { \
2203 RTLIL::Cell *cell = addCell(name, _type); \
2204 cell->parameters[ID::WIDTH] = sig_a.size(); \
2205 if (_pmux) cell->parameters[ID::S_WIDTH] = sig_s.size(); \
2206 cell->setPort(ID::A, sig_a); \
2207 cell->setPort(ID::B, sig_b); \
2208 cell->setPort(ID::S, sig_s); \
2209 cell->setPort(ID::Y, sig_y); \
2210 cell->set_src_attribute(src); \
2213 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) { \
2214 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
2215 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
2218 DEF_METHOD(Mux
, ID($mux
), 0)
2219 DEF_METHOD(Pmux
, ID($pmux
), 1)
2222 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
2223 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2224 RTLIL::Cell *cell = addCell(name, _type); \
2225 cell->setPort("\\" #_P1, sig1); \
2226 cell->setPort("\\" #_P2, sig2); \
2227 cell->set_src_attribute(src); \
2230 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const std::string &src) { \
2231 RTLIL::SigBit sig2 = addWire(NEW_ID); \
2232 add ## _func(name, sig1, sig2, src); \
2235 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
2236 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) { \
2237 RTLIL::Cell *cell = addCell(name, _type); \
2238 cell->setPort("\\" #_P1, sig1); \
2239 cell->setPort("\\" #_P2, sig2); \
2240 cell->setPort("\\" #_P3, sig3); \
2241 cell->set_src_attribute(src); \
2244 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const std::string &src) { \
2245 RTLIL::SigBit sig3 = addWire(NEW_ID); \
2246 add ## _func(name, sig1, sig2, sig3, src); \
2249 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
2250 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) { \
2251 RTLIL::Cell *cell = addCell(name, _type); \
2252 cell->setPort("\\" #_P1, sig1); \
2253 cell->setPort("\\" #_P2, sig2); \
2254 cell->setPort("\\" #_P3, sig3); \
2255 cell->setPort("\\" #_P4, sig4); \
2256 cell->set_src_attribute(src); \
2259 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, const RTLIL::SigBit &sig1, const RTLIL::SigBit &sig2, const RTLIL::SigBit &sig3, const std::string &src) { \
2260 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2261 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2264 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2265 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) { \
2266 RTLIL::Cell *cell = addCell(name, _type); \
2267 cell->setPort("\\" #_P1, sig1); \
2268 cell->setPort("\\" #_P2, sig2); \
2269 cell->setPort("\\" #_P3, sig3); \
2270 cell->setPort("\\" #_P4, sig4); \
2271 cell->setPort("\\" #_P5, sig5); \
2272 cell->set_src_attribute(src); \
2275 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) { \
2276 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2277 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2280 DEF_METHOD_2(BufGate
, ID($_BUF_
), A
, Y
)
2281 DEF_METHOD_2(NotGate
, ID($_NOT_
), A
, Y
)
2282 DEF_METHOD_3(AndGate
, ID($_AND_
), A
, B
, Y
)
2283 DEF_METHOD_3(NandGate
, ID($_NAND_
), A
, B
, Y
)
2284 DEF_METHOD_3(OrGate
, ID($_OR_
), A
, B
, Y
)
2285 DEF_METHOD_3(NorGate
, ID($_NOR_
), A
, B
, Y
)
2286 DEF_METHOD_3(XorGate
, ID($_XOR_
), A
, B
, Y
)
2287 DEF_METHOD_3(XnorGate
, ID($_XNOR_
), A
, B
, Y
)
2288 DEF_METHOD_3(AndnotGate
, ID($_ANDNOT_
), A
, B
, Y
)
2289 DEF_METHOD_3(OrnotGate
, ID($_ORNOT_
), A
, B
, Y
)
2290 DEF_METHOD_4(MuxGate
, ID($_MUX_
), A
, B
, S
, Y
)
2291 DEF_METHOD_4(NmuxGate
, ID($_NMUX_
), A
, B
, S
, Y
)
2292 DEF_METHOD_4(Aoi3Gate
, ID($_AOI3_
), A
, B
, C
, Y
)
2293 DEF_METHOD_4(Oai3Gate
, ID($_OAI3_
), A
, B
, C
, Y
)
2294 DEF_METHOD_5(Aoi4Gate
, ID($_AOI4_
), A
, B
, C
, D
, Y
)
2295 DEF_METHOD_5(Oai4Gate
, ID($_OAI4_
), A
, B
, C
, D
, Y
)
2301 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
)
2303 RTLIL::Cell
*cell
= addCell(name
, ID($pow
));
2304 cell
->parameters
[ID::A_SIGNED
] = a_signed
;
2305 cell
->parameters
[ID::B_SIGNED
] = b_signed
;
2306 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2307 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2308 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2309 cell
->setPort(ID::A
, sig_a
);
2310 cell
->setPort(ID::B
, sig_b
);
2311 cell
->setPort(ID::Y
, sig_y
);
2312 cell
->set_src_attribute(src
);
2316 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
)
2318 RTLIL::Cell
*cell
= addCell(name
, ID($slice
));
2319 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2320 cell
->parameters
[ID::Y_WIDTH
] = sig_y
.size();
2321 cell
->parameters
[ID::OFFSET
] = offset
;
2322 cell
->setPort(ID::A
, sig_a
);
2323 cell
->setPort(ID::Y
, sig_y
);
2324 cell
->set_src_attribute(src
);
2328 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
)
2330 RTLIL::Cell
*cell
= addCell(name
, ID($concat
));
2331 cell
->parameters
[ID::A_WIDTH
] = sig_a
.size();
2332 cell
->parameters
[ID::B_WIDTH
] = sig_b
.size();
2333 cell
->setPort(ID::A
, sig_a
);
2334 cell
->setPort(ID::B
, sig_b
);
2335 cell
->setPort(ID::Y
, sig_y
);
2336 cell
->set_src_attribute(src
);
2340 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
)
2342 RTLIL::Cell
*cell
= addCell(name
, ID($lut
));
2343 cell
->parameters
[ID::LUT
] = lut
;
2344 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2345 cell
->setPort(ID::A
, sig_a
);
2346 cell
->setPort(ID::Y
, sig_y
);
2347 cell
->set_src_attribute(src
);
2351 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
)
2353 RTLIL::Cell
*cell
= addCell(name
, ID($tribuf
));
2354 cell
->parameters
[ID::WIDTH
] = sig_a
.size();
2355 cell
->setPort(ID::A
, sig_a
);
2356 cell
->setPort(ID::EN
, sig_en
);
2357 cell
->setPort(ID::Y
, sig_y
);
2358 cell
->set_src_attribute(src
);
2362 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2364 RTLIL::Cell
*cell
= addCell(name
, ID($
assert));
2365 cell
->setPort(ID::A
, sig_a
);
2366 cell
->setPort(ID::EN
, sig_en
);
2367 cell
->set_src_attribute(src
);
2371 RTLIL::Cell
* RTLIL::Module::addAssume(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2373 RTLIL::Cell
*cell
= addCell(name
, ID($assume
));
2374 cell
->setPort(ID::A
, sig_a
);
2375 cell
->setPort(ID::EN
, sig_en
);
2376 cell
->set_src_attribute(src
);
2380 RTLIL::Cell
* RTLIL::Module::addLive(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2382 RTLIL::Cell
*cell
= addCell(name
, ID($live
));
2383 cell
->setPort(ID::A
, sig_a
);
2384 cell
->setPort(ID::EN
, sig_en
);
2385 cell
->set_src_attribute(src
);
2389 RTLIL::Cell
* RTLIL::Module::addFair(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2391 RTLIL::Cell
*cell
= addCell(name
, ID($fair
));
2392 cell
->setPort(ID::A
, sig_a
);
2393 cell
->setPort(ID::EN
, sig_en
);
2394 cell
->set_src_attribute(src
);
2398 RTLIL::Cell
* RTLIL::Module::addCover(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_a
, const RTLIL::SigSpec
&sig_en
, const std::string
&src
)
2400 RTLIL::Cell
*cell
= addCell(name
, ID($cover
));
2401 cell
->setPort(ID::A
, sig_a
);
2402 cell
->setPort(ID::EN
, sig_en
);
2403 cell
->set_src_attribute(src
);
2407 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
)
2409 RTLIL::Cell
*cell
= addCell(name
, ID($equiv
));
2410 cell
->setPort(ID::A
, sig_a
);
2411 cell
->setPort(ID::B
, sig_b
);
2412 cell
->setPort(ID::Y
, sig_y
);
2413 cell
->set_src_attribute(src
);
2417 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
)
2419 RTLIL::Cell
*cell
= addCell(name
, ID($sr
));
2420 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2421 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2422 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2423 cell
->setPort(ID::SET
, sig_set
);
2424 cell
->setPort(ID::CLR
, sig_clr
);
2425 cell
->setPort(ID::Q
, sig_q
);
2426 cell
->set_src_attribute(src
);
2430 RTLIL::Cell
* RTLIL::Module::addFf(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2432 RTLIL::Cell
*cell
= addCell(name
, ID($ff
));
2433 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2434 cell
->setPort(ID::D
, sig_d
);
2435 cell
->setPort(ID::Q
, sig_q
);
2436 cell
->set_src_attribute(src
);
2440 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
)
2442 RTLIL::Cell
*cell
= addCell(name
, ID($dff
));
2443 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2444 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2445 cell
->setPort(ID::CLK
, sig_clk
);
2446 cell
->setPort(ID::D
, sig_d
);
2447 cell
->setPort(ID::Q
, sig_q
);
2448 cell
->set_src_attribute(src
);
2452 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
)
2454 RTLIL::Cell
*cell
= addCell(name
, ID($dffe
));
2455 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2456 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2457 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2458 cell
->setPort(ID::CLK
, sig_clk
);
2459 cell
->setPort(ID::EN
, sig_en
);
2460 cell
->setPort(ID::D
, sig_d
);
2461 cell
->setPort(ID::Q
, sig_q
);
2462 cell
->set_src_attribute(src
);
2466 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2467 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2469 RTLIL::Cell
*cell
= addCell(name
, ID($dffsr
));
2470 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2471 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2472 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2473 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2474 cell
->setPort(ID::CLK
, sig_clk
);
2475 cell
->setPort(ID::SET
, sig_set
);
2476 cell
->setPort(ID::CLR
, sig_clr
);
2477 cell
->setPort(ID::D
, sig_d
);
2478 cell
->setPort(ID::Q
, sig_q
);
2479 cell
->set_src_attribute(src
);
2483 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
,
2484 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
)
2486 RTLIL::Cell
*cell
= addCell(name
, ID($dffsre
));
2487 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2488 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2489 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2490 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2491 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2492 cell
->setPort(ID::CLK
, sig_clk
);
2493 cell
->setPort(ID::EN
, sig_en
);
2494 cell
->setPort(ID::SET
, sig_set
);
2495 cell
->setPort(ID::CLR
, sig_clr
);
2496 cell
->setPort(ID::D
, sig_d
);
2497 cell
->setPort(ID::Q
, sig_q
);
2498 cell
->set_src_attribute(src
);
2502 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
,
2503 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2505 RTLIL::Cell
*cell
= addCell(name
, ID($adff
));
2506 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2507 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2508 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2509 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2510 cell
->setPort(ID::CLK
, sig_clk
);
2511 cell
->setPort(ID::ARST
, sig_arst
);
2512 cell
->setPort(ID::D
, sig_d
);
2513 cell
->setPort(ID::Q
, sig_q
);
2514 cell
->set_src_attribute(src
);
2518 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
,
2519 RTLIL::Const arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2521 RTLIL::Cell
*cell
= addCell(name
, ID($adffe
));
2522 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2523 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2524 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2525 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2526 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2527 cell
->setPort(ID::CLK
, sig_clk
);
2528 cell
->setPort(ID::EN
, sig_en
);
2529 cell
->setPort(ID::ARST
, sig_arst
);
2530 cell
->setPort(ID::D
, sig_d
);
2531 cell
->setPort(ID::Q
, sig_q
);
2532 cell
->set_src_attribute(src
);
2536 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
,
2537 RTLIL::Const srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
2539 RTLIL::Cell
*cell
= addCell(name
, ID($sdff
));
2540 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2541 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2542 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2543 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2544 cell
->setPort(ID::CLK
, sig_clk
);
2545 cell
->setPort(ID::SRST
, sig_srst
);
2546 cell
->setPort(ID::D
, sig_d
);
2547 cell
->setPort(ID::Q
, sig_q
);
2548 cell
->set_src_attribute(src
);
2552 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
,
2553 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2555 RTLIL::Cell
*cell
= addCell(name
, ID($sdffe
));
2556 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2557 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2558 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2559 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2560 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2561 cell
->setPort(ID::CLK
, sig_clk
);
2562 cell
->setPort(ID::EN
, sig_en
);
2563 cell
->setPort(ID::SRST
, sig_srst
);
2564 cell
->setPort(ID::D
, sig_d
);
2565 cell
->setPort(ID::Q
, sig_q
);
2566 cell
->set_src_attribute(src
);
2570 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
,
2571 RTLIL::Const srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2573 RTLIL::Cell
*cell
= addCell(name
, ID($sdffce
));
2574 cell
->parameters
[ID::CLK_POLARITY
] = clk_polarity
;
2575 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2576 cell
->parameters
[ID::SRST_POLARITY
] = srst_polarity
;
2577 cell
->parameters
[ID::SRST_VALUE
] = srst_value
;
2578 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2579 cell
->setPort(ID::CLK
, sig_clk
);
2580 cell
->setPort(ID::EN
, sig_en
);
2581 cell
->setPort(ID::SRST
, sig_srst
);
2582 cell
->setPort(ID::D
, sig_d
);
2583 cell
->setPort(ID::Q
, sig_q
);
2584 cell
->set_src_attribute(src
);
2588 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
)
2590 RTLIL::Cell
*cell
= addCell(name
, ID($dlatch
));
2591 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2592 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2593 cell
->setPort(ID::EN
, sig_en
);
2594 cell
->setPort(ID::D
, sig_d
);
2595 cell
->setPort(ID::Q
, sig_q
);
2596 cell
->set_src_attribute(src
);
2600 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
,
2601 RTLIL::Const arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2603 RTLIL::Cell
*cell
= addCell(name
, ID($adlatch
));
2604 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2605 cell
->parameters
[ID::ARST_POLARITY
] = arst_polarity
;
2606 cell
->parameters
[ID::ARST_VALUE
] = arst_value
;
2607 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2608 cell
->setPort(ID::EN
, sig_en
);
2609 cell
->setPort(ID::ARST
, sig_arst
);
2610 cell
->setPort(ID::D
, sig_d
);
2611 cell
->setPort(ID::Q
, sig_q
);
2612 cell
->set_src_attribute(src
);
2616 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2617 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2619 RTLIL::Cell
*cell
= addCell(name
, ID($dlatchsr
));
2620 cell
->parameters
[ID::EN_POLARITY
] = en_polarity
;
2621 cell
->parameters
[ID::SET_POLARITY
] = set_polarity
;
2622 cell
->parameters
[ID::CLR_POLARITY
] = clr_polarity
;
2623 cell
->parameters
[ID::WIDTH
] = sig_q
.size();
2624 cell
->setPort(ID::EN
, sig_en
);
2625 cell
->setPort(ID::SET
, sig_set
);
2626 cell
->setPort(ID::CLR
, sig_clr
);
2627 cell
->setPort(ID::D
, sig_d
);
2628 cell
->setPort(ID::Q
, sig_q
);
2629 cell
->set_src_attribute(src
);
2633 RTLIL::Cell
* RTLIL::Module::addSrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2634 const RTLIL::SigSpec
&sig_q
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2636 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SR_%c%c_", set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2637 cell
->setPort(ID::S
, sig_set
);
2638 cell
->setPort(ID::R
, sig_clr
);
2639 cell
->setPort(ID::Q
, sig_q
);
2640 cell
->set_src_attribute(src
);
2644 RTLIL::Cell
* RTLIL::Module::addFfGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_d
, const RTLIL::SigSpec
&sig_q
, const std::string
&src
)
2646 RTLIL::Cell
*cell
= addCell(name
, ID($_FF_
));
2647 cell
->setPort(ID::D
, sig_d
);
2648 cell
->setPort(ID::Q
, sig_q
);
2649 cell
->set_src_attribute(src
);
2653 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
)
2655 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
2656 cell
->setPort(ID::C
, sig_clk
);
2657 cell
->setPort(ID::D
, sig_d
);
2658 cell
->setPort(ID::Q
, sig_q
);
2659 cell
->set_src_attribute(src
);
2663 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
)
2665 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFE_%c%c_", clk_polarity
? 'P' : 'N', en_polarity
? 'P' : 'N'));
2666 cell
->setPort(ID::C
, sig_clk
);
2667 cell
->setPort(ID::E
, sig_en
);
2668 cell
->setPort(ID::D
, sig_d
);
2669 cell
->setPort(ID::Q
, sig_q
);
2670 cell
->set_src_attribute(src
);
2674 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_clk
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2675 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2677 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2678 cell
->setPort(ID::C
, sig_clk
);
2679 cell
->setPort(ID::S
, sig_set
);
2680 cell
->setPort(ID::R
, sig_clr
);
2681 cell
->setPort(ID::D
, sig_d
);
2682 cell
->setPort(ID::Q
, sig_q
);
2683 cell
->set_src_attribute(src
);
2687 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
,
2688 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
)
2690 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'));
2691 cell
->setPort(ID::C
, sig_clk
);
2692 cell
->setPort(ID::S
, sig_set
);
2693 cell
->setPort(ID::R
, sig_clr
);
2694 cell
->setPort(ID::E
, sig_en
);
2695 cell
->setPort(ID::D
, sig_d
);
2696 cell
->setPort(ID::Q
, sig_q
);
2697 cell
->set_src_attribute(src
);
2701 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
,
2702 bool arst_value
, bool clk_polarity
, bool arst_polarity
, const std::string
&src
)
2704 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2705 cell
->setPort(ID::C
, sig_clk
);
2706 cell
->setPort(ID::R
, sig_arst
);
2707 cell
->setPort(ID::D
, sig_d
);
2708 cell
->setPort(ID::Q
, sig_q
);
2709 cell
->set_src_attribute(src
);
2713 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
,
2714 bool arst_value
, bool clk_polarity
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2716 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'));
2717 cell
->setPort(ID::C
, sig_clk
);
2718 cell
->setPort(ID::R
, sig_arst
);
2719 cell
->setPort(ID::E
, sig_en
);
2720 cell
->setPort(ID::D
, sig_d
);
2721 cell
->setPort(ID::Q
, sig_q
);
2722 cell
->set_src_attribute(src
);
2726 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
,
2727 bool srst_value
, bool clk_polarity
, bool srst_polarity
, const std::string
&src
)
2729 RTLIL::Cell
*cell
= addCell(name
, stringf("$_SDFF_%c%c%c_", clk_polarity
? 'P' : 'N', srst_polarity
? 'P' : 'N', srst_value
? '1' : '0'));
2730 cell
->setPort(ID::C
, sig_clk
);
2731 cell
->setPort(ID::R
, sig_srst
);
2732 cell
->setPort(ID::D
, sig_d
);
2733 cell
->setPort(ID::Q
, sig_q
);
2734 cell
->set_src_attribute(src
);
2738 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
,
2739 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2741 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'));
2742 cell
->setPort(ID::C
, sig_clk
);
2743 cell
->setPort(ID::R
, sig_srst
);
2744 cell
->setPort(ID::E
, sig_en
);
2745 cell
->setPort(ID::D
, sig_d
);
2746 cell
->setPort(ID::Q
, sig_q
);
2747 cell
->set_src_attribute(src
);
2751 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
,
2752 bool srst_value
, bool clk_polarity
, bool en_polarity
, bool srst_polarity
, const std::string
&src
)
2754 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'));
2755 cell
->setPort(ID::C
, sig_clk
);
2756 cell
->setPort(ID::R
, sig_srst
);
2757 cell
->setPort(ID::E
, sig_en
);
2758 cell
->setPort(ID::D
, sig_d
);
2759 cell
->setPort(ID::Q
, sig_q
);
2760 cell
->set_src_attribute(src
);
2764 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
)
2766 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
2767 cell
->setPort(ID::E
, sig_en
);
2768 cell
->setPort(ID::D
, sig_d
);
2769 cell
->setPort(ID::Q
, sig_q
);
2770 cell
->set_src_attribute(src
);
2774 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
,
2775 bool arst_value
, bool en_polarity
, bool arst_polarity
, const std::string
&src
)
2777 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c%c%c_", en_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
2778 cell
->setPort(ID::E
, sig_en
);
2779 cell
->setPort(ID::R
, sig_arst
);
2780 cell
->setPort(ID::D
, sig_d
);
2781 cell
->setPort(ID::Q
, sig_q
);
2782 cell
->set_src_attribute(src
);
2786 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, const RTLIL::SigSpec
&sig_en
, const RTLIL::SigSpec
&sig_set
, const RTLIL::SigSpec
&sig_clr
,
2787 RTLIL::SigSpec sig_d
, const RTLIL::SigSpec
&sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
, const std::string
&src
)
2789 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
2790 cell
->setPort(ID::E
, sig_en
);
2791 cell
->setPort(ID::S
, sig_set
);
2792 cell
->setPort(ID::R
, sig_clr
);
2793 cell
->setPort(ID::D
, sig_d
);
2794 cell
->setPort(ID::Q
, sig_q
);
2795 cell
->set_src_attribute(src
);
2799 RTLIL::SigSpec
RTLIL::Module::Anyconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2801 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2802 Cell
*cell
= addCell(name
, ID($anyconst
));
2803 cell
->setParam(ID::WIDTH
, width
);
2804 cell
->setPort(ID::Y
, sig
);
2805 cell
->set_src_attribute(src
);
2809 RTLIL::SigSpec
RTLIL::Module::Anyseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2811 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2812 Cell
*cell
= addCell(name
, ID($anyseq
));
2813 cell
->setParam(ID::WIDTH
, width
);
2814 cell
->setPort(ID::Y
, sig
);
2815 cell
->set_src_attribute(src
);
2819 RTLIL::SigSpec
RTLIL::Module::Allconst(RTLIL::IdString name
, int width
, const std::string
&src
)
2821 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2822 Cell
*cell
= addCell(name
, ID($allconst
));
2823 cell
->setParam(ID::WIDTH
, width
);
2824 cell
->setPort(ID::Y
, sig
);
2825 cell
->set_src_attribute(src
);
2829 RTLIL::SigSpec
RTLIL::Module::Allseq(RTLIL::IdString name
, int width
, const std::string
&src
)
2831 RTLIL::SigSpec sig
= addWire(NEW_ID
, width
);
2832 Cell
*cell
= addCell(name
, ID($allseq
));
2833 cell
->setParam(ID::WIDTH
, width
);
2834 cell
->setPort(ID::Y
, sig
);
2835 cell
->set_src_attribute(src
);
2839 RTLIL::SigSpec
RTLIL::Module::Initstate(RTLIL::IdString name
, const std::string
&src
)
2841 RTLIL::SigSpec sig
= addWire(NEW_ID
);
2842 Cell
*cell
= addCell(name
, ID($initstate
));
2843 cell
->setPort(ID::Y
, sig
);
2844 cell
->set_src_attribute(src
);
2850 static unsigned int hashidx_count
= 123456789;
2851 hashidx_count
= mkhash_xorshift(hashidx_count
);
2852 hashidx_
= hashidx_count
;
2859 port_output
= false;
2864 RTLIL::Wire::get_all_wires()->insert(std::pair
<unsigned int, RTLIL::Wire
*>(hashidx_
, this));
2868 RTLIL::Wire::~Wire()
2871 RTLIL::Wire::get_all_wires()->erase(hashidx_
);
2876 static std::map
<unsigned int, RTLIL::Wire
*> all_wires
;
2877 std::map
<unsigned int, RTLIL::Wire
*> *RTLIL::Wire::get_all_wires(void)
2883 RTLIL::Memory::Memory()
2885 static unsigned int hashidx_count
= 123456789;
2886 hashidx_count
= mkhash_xorshift(hashidx_count
);
2887 hashidx_
= hashidx_count
;
2893 RTLIL::Memory::get_all_memorys()->insert(std::pair
<unsigned int, RTLIL::Memory
*>(hashidx_
, this));
2897 RTLIL::Cell::Cell() : module(nullptr)
2899 static unsigned int hashidx_count
= 123456789;
2900 hashidx_count
= mkhash_xorshift(hashidx_count
);
2901 hashidx_
= hashidx_count
;
2903 // log("#memtrace# %p\n", this);
2907 RTLIL::Cell::get_all_cells()->insert(std::pair
<unsigned int, RTLIL::Cell
*>(hashidx_
, this));
2911 RTLIL::Cell::~Cell()
2914 RTLIL::Cell::get_all_cells()->erase(hashidx_
);
2919 static std::map
<unsigned int, RTLIL::Cell
*> all_cells
;
2920 std::map
<unsigned int, RTLIL::Cell
*> *RTLIL::Cell::get_all_cells(void)
2926 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
2928 return connections_
.count(portname
) != 0;
2931 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
2933 RTLIL::SigSpec signal
;
2934 auto conn_it
= connections_
.find(portname
);
2936 if (conn_it
!= connections_
.end())
2938 for (auto mon
: module
->monitors
)
2939 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2942 for (auto mon
: module
->design
->monitors
)
2943 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2946 log("#X# Unconnect %s.%s.%s\n", log_id(this->module
), log_id(this), log_id(portname
));
2947 log_backtrace("-X- ", yosys_xtrace
-1);
2950 connections_
.erase(conn_it
);
2954 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
2956 auto r
= connections_
.insert(portname
);
2957 auto conn_it
= r
.first
;
2958 if (!r
.second
&& conn_it
->second
== signal
)
2961 for (auto mon
: module
->monitors
)
2962 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2965 for (auto mon
: module
->design
->monitors
)
2966 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
2969 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module
), log_id(this), log_id(portname
), log_signal(signal
), GetSize(signal
));
2970 log_backtrace("-X- ", yosys_xtrace
-1);
2973 conn_it
->second
= std::move(signal
);
2976 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
2978 return connections_
.at(portname
);
2981 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
2983 return connections_
;
2986 bool RTLIL::Cell::known() const
2988 if (yosys_celltypes
.cell_known(type
))
2990 if (module
&& module
->design
&& module
->design
->module(type
))
2995 bool RTLIL::Cell::input(RTLIL::IdString portname
) const
2997 if (yosys_celltypes
.cell_known(type
))
2998 return yosys_celltypes
.cell_input(type
, portname
);
2999 if (module
&& module
->design
) {
3000 RTLIL::Module
*m
= module
->design
->module(type
);
3001 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3002 return w
&& w
->port_input
;
3007 bool RTLIL::Cell::output(RTLIL::IdString portname
) const
3009 if (yosys_celltypes
.cell_known(type
))
3010 return yosys_celltypes
.cell_output(type
, portname
);
3011 if (module
&& module
->design
) {
3012 RTLIL::Module
*m
= module
->design
->module(type
);
3013 RTLIL::Wire
*w
= m
? m
->wire(portname
) : nullptr;
3014 return w
&& w
->port_output
;
3019 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
3021 return parameters
.count(paramname
) != 0;
3024 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
3026 parameters
.erase(paramname
);
3029 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
3031 parameters
[paramname
] = std::move(value
);
3034 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
3036 const auto &it
= parameters
.find(paramname
);
3037 if (it
!= parameters
.end())
3039 if (module
&& module
->design
) {
3040 RTLIL::Module
*m
= module
->design
->module(type
);
3042 return m
->parameter_default_values
.at(paramname
);
3044 throw std::out_of_range("Cell::getParam()");
3047 void RTLIL::Cell::sort()
3049 connections_
.sort(sort_by_id_str());
3050 parameters
.sort(sort_by_id_str());
3051 attributes
.sort(sort_by_id_str());
3054 void RTLIL::Cell::check()
3057 InternalCellChecker
checker(NULL
, this);
3062 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
3064 if (!type
.begins_with("$") || type
.begins_with("$_") || type
.begins_with("$paramod") || type
.begins_with("$fmcombine") ||
3065 type
.begins_with("$verific$") || type
.begins_with("$array:") || type
.begins_with("$extern:"))
3068 if (type
== ID($mux
) || type
== ID($pmux
)) {
3069 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3070 if (type
== ID($pmux
))
3071 parameters
[ID::S_WIDTH
] = GetSize(connections_
[ID::S
]);
3076 if (type
== ID($lut
) || type
== ID($sop
)) {
3077 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::A
]);
3081 if (type
== ID($fa
)) {
3082 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Y
]);
3086 if (type
== ID($lcu
)) {
3087 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::CO
]);
3091 bool signedness_ab
= !type
.in(ID($slice
), ID($concat
), ID($macc
));
3093 if (connections_
.count(ID::A
)) {
3094 if (signedness_ab
) {
3096 parameters
[ID::A_SIGNED
] = true;
3097 else if (parameters
.count(ID::A_SIGNED
) == 0)
3098 parameters
[ID::A_SIGNED
] = false;
3100 parameters
[ID::A_WIDTH
] = GetSize(connections_
[ID::A
]);
3103 if (connections_
.count(ID::B
)) {
3104 if (signedness_ab
) {
3106 parameters
[ID::B_SIGNED
] = true;
3107 else if (parameters
.count(ID::B_SIGNED
) == 0)
3108 parameters
[ID::B_SIGNED
] = false;
3110 parameters
[ID::B_WIDTH
] = GetSize(connections_
[ID::B
]);
3113 if (connections_
.count(ID::Y
))
3114 parameters
[ID::Y_WIDTH
] = GetSize(connections_
[ID::Y
]);
3116 if (connections_
.count(ID::Q
))
3117 parameters
[ID::WIDTH
] = GetSize(connections_
[ID::Q
]);
3122 RTLIL::SigChunk::SigChunk()
3129 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
3133 width
= GetSize(data
);
3137 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
3139 log_assert(wire
!= nullptr);
3141 this->width
= wire
->width
;
3145 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
3147 log_assert(wire
!= nullptr);
3149 this->width
= width
;
3150 this->offset
= offset
;
3153 RTLIL::SigChunk::SigChunk(const std::string
&str
)
3156 data
= RTLIL::Const(str
).bits
;
3157 width
= GetSize(data
);
3161 RTLIL::SigChunk::SigChunk(int val
, int width
)
3164 data
= RTLIL::Const(val
, width
).bits
;
3165 this->width
= GetSize(data
);
3169 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
3172 data
= RTLIL::Const(bit
, width
).bits
;
3173 this->width
= GetSize(data
);
3177 RTLIL::SigChunk::SigChunk(const RTLIL::SigBit
&bit
)
3182 data
= RTLIL::Const(bit
.data
).bits
;
3184 offset
= bit
.offset
;
3188 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk
&sigchunk
)
3193 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
3195 RTLIL::SigChunk ret
;
3198 ret
.offset
= this->offset
+ offset
;
3201 for (int i
= 0; i
< length
; i
++)
3202 ret
.data
.push_back(data
[offset
+i
]);
3208 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
3210 if (wire
&& other
.wire
)
3211 if (wire
->name
!= other
.wire
->name
)
3212 return wire
->name
< other
.wire
->name
;
3214 if (wire
!= other
.wire
)
3215 return wire
< other
.wire
;
3217 if (offset
!= other
.offset
)
3218 return offset
< other
.offset
;
3220 if (width
!= other
.width
)
3221 return width
< other
.width
;
3223 return data
< other
.data
;
3226 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
3228 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
3231 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
3238 RTLIL::SigSpec::SigSpec()
3244 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
3249 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
3251 cover("kernel.rtlil.sigspec.init.list");
3256 log_assert(parts
.size() > 0);
3257 auto ie
= parts
.begin();
3258 auto it
= ie
+ parts
.size() - 1;
3263 RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
3265 cover("kernel.rtlil.sigspec.assign");
3267 width_
= other
.width_
;
3268 hash_
= other
.hash_
;
3269 chunks_
= other
.chunks_
;
3270 bits_
= other
.bits_
;
3274 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
3276 cover("kernel.rtlil.sigspec.init.const");
3278 chunks_
.emplace_back(value
);
3279 width_
= chunks_
.back().width
;
3284 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
3286 cover("kernel.rtlil.sigspec.init.chunk");
3288 chunks_
.emplace_back(chunk
);
3289 width_
= chunks_
.back().width
;
3294 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
3296 cover("kernel.rtlil.sigspec.init.wire");
3298 chunks_
.emplace_back(wire
);
3299 width_
= chunks_
.back().width
;
3304 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
3306 cover("kernel.rtlil.sigspec.init.wire_part");
3308 chunks_
.emplace_back(wire
, offset
, width
);
3309 width_
= chunks_
.back().width
;
3314 RTLIL::SigSpec::SigSpec(const std::string
&str
)
3316 cover("kernel.rtlil.sigspec.init.str");
3318 chunks_
.emplace_back(str
);
3319 width_
= chunks_
.back().width
;
3324 RTLIL::SigSpec::SigSpec(int val
, int width
)
3326 cover("kernel.rtlil.sigspec.init.int");
3328 chunks_
.emplace_back(val
, width
);
3334 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
3336 cover("kernel.rtlil.sigspec.init.state");
3338 chunks_
.emplace_back(bit
, width
);
3344 RTLIL::SigSpec::SigSpec(const RTLIL::SigBit
&bit
, int width
)
3346 cover("kernel.rtlil.sigspec.init.bit");
3348 if (bit
.wire
== NULL
)
3349 chunks_
.emplace_back(bit
.data
, width
);
3351 for (int i
= 0; i
< width
; i
++)
3352 chunks_
.push_back(bit
);
3358 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigChunk
> &chunks
)
3360 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
3364 for (const auto &c
: chunks
)
3369 RTLIL::SigSpec::SigSpec(const std::vector
<RTLIL::SigBit
> &bits
)
3371 cover("kernel.rtlil.sigspec.init.stdvec_bits");
3375 for (const auto &bit
: bits
)
3380 RTLIL::SigSpec::SigSpec(const pool
<RTLIL::SigBit
> &bits
)
3382 cover("kernel.rtlil.sigspec.init.pool_bits");
3386 for (const auto &bit
: bits
)
3391 RTLIL::SigSpec::SigSpec(const std::set
<RTLIL::SigBit
> &bits
)
3393 cover("kernel.rtlil.sigspec.init.stdset_bits");
3397 for (const auto &bit
: bits
)
3402 RTLIL::SigSpec::SigSpec(bool bit
)
3404 cover("kernel.rtlil.sigspec.init.bool");
3408 append(SigBit(bit
));
3412 void RTLIL::SigSpec::pack() const
3414 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3416 if (that
->bits_
.empty())
3419 cover("kernel.rtlil.sigspec.convert.pack");
3420 log_assert(that
->chunks_
.empty());
3422 std::vector
<RTLIL::SigBit
> old_bits
;
3423 old_bits
.swap(that
->bits_
);
3425 RTLIL::SigChunk
*last
= NULL
;
3426 int last_end_offset
= 0;
3428 for (auto &bit
: old_bits
) {
3429 if (last
&& bit
.wire
== last
->wire
) {
3430 if (bit
.wire
== NULL
) {
3431 last
->data
.push_back(bit
.data
);
3434 } else if (last_end_offset
== bit
.offset
) {
3440 that
->chunks_
.push_back(bit
);
3441 last
= &that
->chunks_
.back();
3442 last_end_offset
= bit
.offset
+ 1;
3448 void RTLIL::SigSpec::unpack() const
3450 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3452 if (that
->chunks_
.empty())
3455 cover("kernel.rtlil.sigspec.convert.unpack");
3456 log_assert(that
->bits_
.empty());
3458 that
->bits_
.reserve(that
->width_
);
3459 for (auto &c
: that
->chunks_
)
3460 for (int i
= 0; i
< c
.width
; i
++)
3461 that
->bits_
.emplace_back(c
, i
);
3463 that
->chunks_
.clear();
3467 void RTLIL::SigSpec::updhash() const
3469 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
3471 if (that
->hash_
!= 0)
3474 cover("kernel.rtlil.sigspec.hash");
3477 that
->hash_
= mkhash_init
;
3478 for (auto &c
: that
->chunks_
)
3479 if (c
.wire
== NULL
) {
3480 for (auto &v
: c
.data
)
3481 that
->hash_
= mkhash(that
->hash_
, v
);
3483 that
->hash_
= mkhash(that
->hash_
, c
.wire
->name
.index_
);
3484 that
->hash_
= mkhash(that
->hash_
, c
.offset
);
3485 that
->hash_
= mkhash(that
->hash_
, c
.width
);
3488 if (that
->hash_
== 0)
3492 void RTLIL::SigSpec::sort()
3495 cover("kernel.rtlil.sigspec.sort");
3496 std::sort(bits_
.begin(), bits_
.end());
3499 void RTLIL::SigSpec::sort_and_unify()
3502 cover("kernel.rtlil.sigspec.sort_and_unify");
3504 // A copy of the bits vector is used to prevent duplicating the logic from
3505 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3506 // that isn't showing up as significant in profiles.
3507 std::vector
<SigBit
> unique_bits
= bits_
;
3508 std::sort(unique_bits
.begin(), unique_bits
.end());
3509 auto last
= std::unique(unique_bits
.begin(), unique_bits
.end());
3510 unique_bits
.erase(last
, unique_bits
.end());
3512 *this = unique_bits
;
3515 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
3517 replace(pattern
, with
, this);
3520 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
3522 log_assert(other
!= NULL
);
3523 log_assert(width_
== other
->width_
);
3524 log_assert(pattern
.width_
== with
.width_
);
3531 for (int i
= 0; i
< GetSize(pattern
.bits_
); i
++) {
3532 if (pattern
.bits_
[i
].wire
!= NULL
) {
3533 for (int j
= 0; j
< GetSize(bits_
); j
++) {
3534 if (bits_
[j
] == pattern
.bits_
[i
]) {
3535 other
->bits_
[j
] = with
.bits_
[i
];
3544 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3546 replace(rules
, this);
3549 void RTLIL::SigSpec::replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3551 cover("kernel.rtlil.sigspec.replace_dict");
3553 log_assert(other
!= NULL
);
3554 log_assert(width_
== other
->width_
);
3556 if (rules
.empty()) return;
3560 for (int i
= 0; i
< GetSize(bits_
); i
++) {
3561 auto it
= rules
.find(bits_
[i
]);
3562 if (it
!= rules
.end())
3563 other
->bits_
[i
] = it
->second
;
3569 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
3571 replace(rules
, this);
3574 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
3576 cover("kernel.rtlil.sigspec.replace_map");
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::remove(const RTLIL::SigSpec
&pattern
)
3596 remove2(pattern
, NULL
);
3599 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
3601 RTLIL::SigSpec tmp
= *this;
3602 tmp
.remove2(pattern
, other
);
3605 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
3608 cover("kernel.rtlil.sigspec.remove_other");
3610 cover("kernel.rtlil.sigspec.remove");
3613 if (other
!= NULL
) {
3614 log_assert(width_
== other
->width_
);
3618 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--)
3620 if (bits_
[i
].wire
== NULL
) continue;
3622 for (auto &pattern_chunk
: pattern
.chunks())
3623 if (bits_
[i
].wire
== pattern_chunk
.wire
&&
3624 bits_
[i
].offset
>= pattern_chunk
.offset
&&
3625 bits_
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
) {
3626 bits_
.erase(bits_
.begin() + i
);
3628 if (other
!= NULL
) {
3629 other
->bits_
.erase(other
->bits_
.begin() + i
);
3639 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
)
3641 remove2(pattern
, NULL
);
3644 void RTLIL::SigSpec::remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
3646 RTLIL::SigSpec tmp
= *this;
3647 tmp
.remove2(pattern
, other
);
3650 void RTLIL::SigSpec::remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3653 cover("kernel.rtlil.sigspec.remove_other");
3655 cover("kernel.rtlil.sigspec.remove");
3659 if (other
!= NULL
) {
3660 log_assert(width_
== other
->width_
);
3664 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3665 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3666 bits_
.erase(bits_
.begin() + i
);
3668 if (other
!= NULL
) {
3669 other
->bits_
.erase(other
->bits_
.begin() + i
);
3678 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
3681 cover("kernel.rtlil.sigspec.remove_other");
3683 cover("kernel.rtlil.sigspec.remove");
3687 if (other
!= NULL
) {
3688 log_assert(width_
== other
->width_
);
3692 for (int i
= GetSize(bits_
) - 1; i
>= 0; i
--) {
3693 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
])) {
3694 bits_
.erase(bits_
.begin() + i
);
3696 if (other
!= NULL
) {
3697 other
->bits_
.erase(other
->bits_
.begin() + i
);
3706 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
3709 cover("kernel.rtlil.sigspec.extract_other");
3711 cover("kernel.rtlil.sigspec.extract");
3713 log_assert(other
== NULL
|| width_
== other
->width_
);
3716 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3718 for (auto& pattern_chunk
: pattern
.chunks()) {
3720 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3721 for (int i
= 0; i
< width_
; i
++)
3722 if (bits_match
[i
].wire
&&
3723 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3724 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3725 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3726 ret
.append(bits_other
[i
]);
3728 for (int i
= 0; i
< width_
; i
++)
3729 if (bits_match
[i
].wire
&&
3730 bits_match
[i
].wire
== pattern_chunk
.wire
&&
3731 bits_match
[i
].offset
>= pattern_chunk
.offset
&&
3732 bits_match
[i
].offset
< pattern_chunk
.offset
+ pattern_chunk
.width
)
3733 ret
.append(bits_match
[i
]);
3741 RTLIL::SigSpec
RTLIL::SigSpec::extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
3744 cover("kernel.rtlil.sigspec.extract_other");
3746 cover("kernel.rtlil.sigspec.extract");
3748 log_assert(other
== NULL
|| width_
== other
->width_
);
3750 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
3754 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
3755 for (int i
= 0; i
< width_
; i
++)
3756 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3757 ret
.append(bits_other
[i
]);
3759 for (int i
= 0; i
< width_
; i
++)
3760 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
3761 ret
.append(bits_match
[i
]);
3768 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
3770 cover("kernel.rtlil.sigspec.replace_pos");
3775 log_assert(offset
>= 0);
3776 log_assert(with
.width_
>= 0);
3777 log_assert(offset
+with
.width_
<= width_
);
3779 for (int i
= 0; i
< with
.width_
; i
++)
3780 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
3785 void RTLIL::SigSpec::remove_const()
3789 cover("kernel.rtlil.sigspec.remove_const.packed");
3791 std::vector
<RTLIL::SigChunk
> new_chunks
;
3792 new_chunks
.reserve(GetSize(chunks_
));
3795 for (auto &chunk
: chunks_
)
3796 if (chunk
.wire
!= NULL
) {
3797 new_chunks
.push_back(chunk
);
3798 width_
+= chunk
.width
;
3801 chunks_
.swap(new_chunks
);
3805 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3807 std::vector
<RTLIL::SigBit
> new_bits
;
3808 new_bits
.reserve(width_
);
3810 for (auto &bit
: bits_
)
3811 if (bit
.wire
!= NULL
)
3812 new_bits
.push_back(bit
);
3814 bits_
.swap(new_bits
);
3815 width_
= bits_
.size();
3821 void RTLIL::SigSpec::remove(int offset
, int length
)
3823 cover("kernel.rtlil.sigspec.remove_pos");
3827 log_assert(offset
>= 0);
3828 log_assert(length
>= 0);
3829 log_assert(offset
+ length
<= width_
);
3831 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3832 width_
= bits_
.size();
3837 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
3840 cover("kernel.rtlil.sigspec.extract_pos");
3841 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
3844 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
3846 if (signal
.width_
== 0)
3854 cover("kernel.rtlil.sigspec.append");
3856 if (packed() != signal
.packed()) {
3862 for (auto &other_c
: signal
.chunks_
)
3864 auto &my_last_c
= chunks_
.back();
3865 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
3866 auto &this_data
= my_last_c
.data
;
3867 auto &other_data
= other_c
.data
;
3868 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
3869 my_last_c
.width
+= other_c
.width
;
3871 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
3872 my_last_c
.width
+= other_c
.width
;
3874 chunks_
.push_back(other_c
);
3877 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
3879 width_
+= signal
.width_
;
3883 void RTLIL::SigSpec::append(const RTLIL::SigBit
&bit
)
3887 cover("kernel.rtlil.sigspec.append_bit.packed");
3889 if (chunks_
.size() == 0)
3890 chunks_
.push_back(bit
);
3892 if (bit
.wire
== NULL
)
3893 if (chunks_
.back().wire
== NULL
) {
3894 chunks_
.back().data
.push_back(bit
.data
);
3895 chunks_
.back().width
++;
3897 chunks_
.push_back(bit
);
3899 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
3900 chunks_
.back().width
++;
3902 chunks_
.push_back(bit
);
3906 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3907 bits_
.push_back(bit
);
3914 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
3916 cover("kernel.rtlil.sigspec.extend_u0");
3921 remove(width
, width_
- width
);
3923 if (width_
< width
) {
3924 RTLIL::SigBit padding
= width_
> 0 ? (*this)[width_
- 1] : RTLIL::State::Sx
;
3926 padding
= RTLIL::State::S0
;
3927 while (width_
< width
)
3933 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
3935 cover("kernel.rtlil.sigspec.repeat");
3938 for (int i
= 0; i
< num
; i
++)
3944 void RTLIL::SigSpec::check() const
3948 cover("kernel.rtlil.sigspec.check.skip");
3952 cover("kernel.rtlil.sigspec.check.packed");
3955 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
3956 const RTLIL::SigChunk
&chunk
= chunks_
[i
];
3957 if (chunk
.wire
== NULL
) {
3959 log_assert(chunks_
[i
-1].wire
!= NULL
);
3960 log_assert(chunk
.offset
== 0);
3961 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
3963 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
3964 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
3965 log_assert(chunk
.offset
>= 0);
3966 log_assert(chunk
.width
>= 0);
3967 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
3968 log_assert(chunk
.data
.size() == 0);
3972 log_assert(w
== width_
);
3973 log_assert(bits_
.empty());
3977 cover("kernel.rtlil.sigspec.check.unpacked");
3979 log_assert(width_
== GetSize(bits_
));
3980 log_assert(chunks_
.empty());
3985 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
3987 cover("kernel.rtlil.sigspec.comp_lt");
3992 if (width_
!= other
.width_
)
3993 return width_
< other
.width_
;
3998 if (chunks_
.size() != other
.chunks_
.size())
3999 return chunks_
.size() < other
.chunks_
.size();
4004 if (hash_
!= other
.hash_
)
4005 return hash_
< other
.hash_
;
4007 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4008 if (chunks_
[i
] != other
.chunks_
[i
]) {
4009 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
4010 return chunks_
[i
] < other
.chunks_
[i
];
4013 cover("kernel.rtlil.sigspec.comp_lt.equal");
4017 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
4019 cover("kernel.rtlil.sigspec.comp_eq");
4024 if (width_
!= other
.width_
)
4027 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
4028 // since the RHS will contain one SigChunk of width 0 causing
4029 // the size check below to fail
4036 if (chunks_
.size() != other
.chunks_
.size())
4042 if (hash_
!= other
.hash_
)
4045 for (size_t i
= 0; i
< chunks_
.size(); i
++)
4046 if (chunks_
[i
] != other
.chunks_
[i
]) {
4047 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
4051 cover("kernel.rtlil.sigspec.comp_eq.equal");
4055 bool RTLIL::SigSpec::is_wire() const
4057 cover("kernel.rtlil.sigspec.is_wire");
4060 return GetSize(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
4063 bool RTLIL::SigSpec::is_chunk() const
4065 cover("kernel.rtlil.sigspec.is_chunk");
4068 return GetSize(chunks_
) == 1;
4071 bool RTLIL::SigSpec::is_fully_const() const
4073 cover("kernel.rtlil.sigspec.is_fully_const");
4076 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4077 if (it
->width
> 0 && it
->wire
!= NULL
)
4082 bool RTLIL::SigSpec::is_fully_zero() const
4084 cover("kernel.rtlil.sigspec.is_fully_zero");
4087 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4088 if (it
->width
> 0 && it
->wire
!= NULL
)
4090 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4091 if (it
->data
[i
] != RTLIL::State::S0
)
4097 bool RTLIL::SigSpec::is_fully_ones() const
4099 cover("kernel.rtlil.sigspec.is_fully_ones");
4102 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4103 if (it
->width
> 0 && it
->wire
!= NULL
)
4105 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4106 if (it
->data
[i
] != RTLIL::State::S1
)
4112 bool RTLIL::SigSpec::is_fully_def() const
4114 cover("kernel.rtlil.sigspec.is_fully_def");
4117 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4118 if (it
->width
> 0 && it
->wire
!= NULL
)
4120 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4121 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
4127 bool RTLIL::SigSpec::is_fully_undef() const
4129 cover("kernel.rtlil.sigspec.is_fully_undef");
4132 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
4133 if (it
->width
> 0 && it
->wire
!= NULL
)
4135 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4136 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
4142 bool RTLIL::SigSpec::has_const() const
4144 cover("kernel.rtlil.sigspec.has_const");
4147 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4148 if (it
->width
> 0 && it
->wire
== NULL
)
4153 bool RTLIL::SigSpec::has_marked_bits() const
4155 cover("kernel.rtlil.sigspec.has_marked_bits");
4158 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
4159 if (it
->width
> 0 && it
->wire
== NULL
) {
4160 for (size_t i
= 0; i
< it
->data
.size(); i
++)
4161 if (it
->data
[i
] == RTLIL::State::Sm
)
4167 bool RTLIL::SigSpec::as_bool() const
4169 cover("kernel.rtlil.sigspec.as_bool");
4172 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4174 return RTLIL::Const(chunks_
[0].data
).as_bool();
4178 int RTLIL::SigSpec::as_int(bool is_signed
) const
4180 cover("kernel.rtlil.sigspec.as_int");
4183 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4185 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
4189 std::string
RTLIL::SigSpec::as_string() const
4191 cover("kernel.rtlil.sigspec.as_string");
4195 str
.reserve(size());
4196 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
4197 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
4198 if (chunk
.wire
!= NULL
)
4199 str
.append(chunk
.width
, '?');
4201 str
+= RTLIL::Const(chunk
.data
).as_string();
4206 RTLIL::Const
RTLIL::SigSpec::as_const() const
4208 cover("kernel.rtlil.sigspec.as_const");
4211 log_assert(is_fully_const() && GetSize(chunks_
) <= 1);
4213 return chunks_
[0].data
;
4214 return RTLIL::Const();
4217 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
4219 cover("kernel.rtlil.sigspec.as_wire");
4222 log_assert(is_wire());
4223 return chunks_
[0].wire
;
4226 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
4228 cover("kernel.rtlil.sigspec.as_chunk");
4231 log_assert(is_chunk());
4235 RTLIL::SigBit
RTLIL::SigSpec::as_bit() const
4237 cover("kernel.rtlil.sigspec.as_bit");
4239 log_assert(width_
== 1);
4241 return RTLIL::SigBit(*chunks_
.begin());
4246 bool RTLIL::SigSpec::match(const char* pattern
) const
4248 cover("kernel.rtlil.sigspec.match");
4251 log_assert(int(strlen(pattern
)) == GetSize(bits_
));
4253 for (auto it
= bits_
.rbegin(); it
!= bits_
.rend(); it
++, pattern
++) {
4254 if (*pattern
== ' ')
4256 if (*pattern
== '*') {
4257 if (*it
!= State::Sz
&& *it
!= State::Sx
)
4261 if (*pattern
== '0') {
4262 if (*it
!= State::S0
)
4265 if (*pattern
== '1') {
4266 if (*it
!= State::S1
)
4275 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
4277 cover("kernel.rtlil.sigspec.to_sigbit_set");
4280 std::set
<RTLIL::SigBit
> sigbits
;
4281 for (auto &c
: chunks_
)
4282 for (int i
= 0; i
< c
.width
; i
++)
4283 sigbits
.insert(RTLIL::SigBit(c
, i
));
4287 pool
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_pool() const
4289 cover("kernel.rtlil.sigspec.to_sigbit_pool");
4292 pool
<RTLIL::SigBit
> sigbits
;
4293 sigbits
.reserve(size());
4294 for (auto &c
: chunks_
)
4295 for (int i
= 0; i
< c
.width
; i
++)
4296 sigbits
.insert(RTLIL::SigBit(c
, i
));
4300 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
4302 cover("kernel.rtlil.sigspec.to_sigbit_vector");
4308 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
4310 cover("kernel.rtlil.sigspec.to_sigbit_map");
4315 log_assert(width_
== other
.width_
);
4317 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4318 for (int i
= 0; i
< width_
; i
++)
4319 new_map
[bits_
[i
]] = other
.bits_
[i
];
4324 dict
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec
&other
) const
4326 cover("kernel.rtlil.sigspec.to_sigbit_dict");
4331 log_assert(width_
== other
.width_
);
4333 dict
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
4334 new_map
.reserve(size());
4335 for (int i
= 0; i
< width_
; i
++)
4336 new_map
[bits_
[i
]] = other
.bits_
[i
];
4341 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
4343 size_t start
= 0, end
= 0;
4344 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
4345 tokens
.push_back(text
.substr(start
, end
- start
));
4348 tokens
.push_back(text
.substr(start
));
4351 static int sigspec_parse_get_dummy_line_num()
4356 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4358 cover("kernel.rtlil.sigspec.parse");
4360 AST::current_filename
= "input";
4362 std::vector
<std::string
> tokens
;
4363 sigspec_parse_split(tokens
, str
, ',');
4365 sig
= RTLIL::SigSpec();
4366 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
4368 std::string netname
= tokens
[tokidx
];
4369 std::string indices
;
4371 if (netname
.size() == 0)
4374 if (('0' <= netname
[0] && netname
[0] <= '9') || netname
[0] == '\'') {
4375 cover("kernel.rtlil.sigspec.parse.const");
4376 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
4377 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
4380 sig
.append(RTLIL::Const(ast
->bits
));
4388 cover("kernel.rtlil.sigspec.parse.net");
4390 if (netname
[0] != '$' && netname
[0] != '\\')
4391 netname
= "\\" + netname
;
4393 if (module
->wires_
.count(netname
) == 0) {
4394 size_t indices_pos
= netname
.size()-1;
4395 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
4398 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4399 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
4401 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
4403 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
4404 indices
= netname
.substr(indices_pos
);
4405 netname
= netname
.substr(0, indices_pos
);
4410 if (module
->wires_
.count(netname
) == 0)
4413 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
4414 if (!indices
.empty()) {
4415 std::vector
<std::string
> index_tokens
;
4416 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
4417 if (index_tokens
.size() == 1) {
4418 cover("kernel.rtlil.sigspec.parse.bit_sel");
4419 int a
= atoi(index_tokens
.at(0).c_str());
4420 if (a
< 0 || a
>= wire
->width
)
4422 sig
.append(RTLIL::SigSpec(wire
, a
));
4424 cover("kernel.rtlil.sigspec.parse.part_sel");
4425 int a
= atoi(index_tokens
.at(0).c_str());
4426 int b
= atoi(index_tokens
.at(1).c_str());
4431 if (a
< 0 || a
>= wire
->width
)
4433 if (b
< 0 || b
>= wire
->width
)
4435 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
4444 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
4446 if (str
.empty() || str
[0] != '@')
4447 return parse(sig
, module
, str
);
4449 cover("kernel.rtlil.sigspec.parse.sel");
4451 str
= RTLIL::escape_id(str
.substr(1));
4452 if (design
->selection_vars
.count(str
) == 0)
4455 sig
= RTLIL::SigSpec();
4456 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
4457 for (auto &it
: module
->wires_
)
4458 if (sel
.selected_member(module
->name
, it
.first
))
4459 sig
.append(it
.second
);
4464 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
4467 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4468 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
4473 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4474 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
4478 if (lhs
.chunks_
.size() == 1) {
4479 char *p
= (char*)str
.c_str(), *endptr
;
4480 long int val
= strtol(p
, &endptr
, 10);
4481 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
4482 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
4483 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4488 return parse(sig
, module
, str
);
4491 RTLIL::CaseRule::~CaseRule()
4493 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
4497 bool RTLIL::CaseRule::empty() const
4499 return actions
.empty() && switches
.empty();
4502 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
4504 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
4505 new_caserule
->compare
= compare
;
4506 new_caserule
->actions
= actions
;
4507 for (auto &it
: switches
)
4508 new_caserule
->switches
.push_back(it
->clone());
4509 return new_caserule
;
4512 RTLIL::SwitchRule::~SwitchRule()
4514 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
4518 bool RTLIL::SwitchRule::empty() const
4520 return cases
.empty();
4523 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
4525 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
4526 new_switchrule
->signal
= signal
;
4527 new_switchrule
->attributes
= attributes
;
4528 for (auto &it
: cases
)
4529 new_switchrule
->cases
.push_back(it
->clone());
4530 return new_switchrule
;
4534 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
4536 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
4537 new_syncrule
->type
= type
;
4538 new_syncrule
->signal
= signal
;
4539 new_syncrule
->actions
= actions
;
4540 return new_syncrule
;
4543 RTLIL::Process::~Process()
4545 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
4549 RTLIL::Process
*RTLIL::Process::clone() const
4551 RTLIL::Process
*new_proc
= new RTLIL::Process
;
4553 new_proc
->name
= name
;
4554 new_proc
->attributes
= attributes
;
4556 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
4557 new_proc
->root_case
= *rc_ptr
;
4558 rc_ptr
->switches
.clear();
4561 for (auto &it
: syncs
)
4562 new_proc
->syncs
.push_back(it
->clone());
4568 RTLIL::Memory::~Memory()
4570 RTLIL::Memory::get_all_memorys()->erase(hashidx_
);
4572 static std::map
<unsigned int, RTLIL::Memory
*> all_memorys
;
4573 std::map
<unsigned int, RTLIL::Memory
*> *RTLIL::Memory::get_all_memorys(void)
4575 return &all_memorys
;