44 for (
int i = str.size()-1; i >= 0; i--) {
45 unsigned char ch = str[i];
46 for (
int j = 0; j < 8; j++) {
56 for (
int i = 0; i < width; i++) {
65 for (
int i = 0; i < width; i++)
78 if (bits.size() != other.
bits.size())
79 return bits.size() < other.
bits.size();
80 for (
size_t i = 0; i < bits.size(); i++)
81 if (bits[i] != other.
bits[i])
82 return bits[i] < other.
bits[i];
88 return bits == other.
bits;
93 return bits != other.
bits;
98 for (
size_t i = 0; i < bits.size(); i++)
107 for (
size_t i = 0; i < bits.size() && i < 32; i++)
110 if (is_signed && bits.back() ==
RTLIL::S1)
111 for (
size_t i = bits.size(); i < 32; i++)
119 for (
size_t i = bits.size(); i > 0; i--)
121 case S0: ret +=
"0";
break;
122 case S1: ret +=
"1";
break;
123 case Sx: ret +=
"x";
break;
124 case Sz: ret +=
"z";
break;
125 case Sa: ret +=
"-";
break;
126 case Sm: ret +=
"m";
break;
134 std::vector <char> string_chars;
135 for (
int i = 0; i < int (bits.size()); i += 8) {
137 for (
int j = 0; j < 8 && i + j < int (bits.size()); j++)
141 string_chars.push_back(ch);
143 for (
int i =
int (string_chars.size()) - 1; i >= 0; i--)
144 string += string_chars[i];
152 if (selected_modules.count(mod_name) > 0)
154 if (selected_members.count(mod_name) > 0)
163 if (selected_modules.count(mod_name) > 0)
172 if (selected_modules.count(mod_name) > 0)
174 if (selected_members.count(mod_name) > 0)
175 if (selected_members.at(mod_name).count(memb_name) > 0)
182 if (full_selection) {
183 selected_modules.clear();
184 selected_members.clear();
188 std::vector<RTLIL::IdString> del_list, add_list;
191 for (
auto mod_name : selected_modules) {
192 if (design->
modules_.count(mod_name) == 0)
193 del_list.push_back(mod_name);
194 selected_members.erase(mod_name);
196 for (
auto mod_name : del_list)
197 selected_modules.erase(mod_name);
200 for (
auto &it : selected_members)
201 if (design->
modules_.count(it.first) == 0)
202 del_list.push_back(it.first);
203 for (
auto mod_name : del_list)
204 selected_members.erase(mod_name);
206 for (
auto &it : selected_members) {
208 for (
auto memb_name : it.second)
209 if (design->
modules_[it.first]->count_id(memb_name) == 0)
210 del_list.push_back(memb_name);
211 for (
auto memb_name : del_list)
212 it.second.erase(memb_name);
217 for (
auto &it : selected_members)
218 if (it.second.size() == 0)
219 del_list.push_back(it.first);
220 else if (it.second.size() == design->
modules_[it.first]->wires_.size() + design->
modules_[it.first]->memories.size() +
221 design->
modules_[it.first]->cells_.size() + design->
modules_[it.first]->processes.size())
222 add_list.push_back(it.first);
223 for (
auto mod_name : del_list)
224 selected_members.erase(mod_name);
225 for (
auto mod_name : add_list) {
226 selected_members.erase(mod_name);
227 selected_modules.insert(mod_name);
230 if (selected_modules.size() == design->
modules_.size()) {
231 full_selection =
true;
232 selected_modules.clear();
233 selected_members.clear();
239 refcount_modules_ = 0;
245 for (
auto it = modules_.begin(); it != modules_.end(); it++)
256 return modules_.count(name) ? modules_.at(name) :
NULL;
266 for (
auto mon : monitors)
267 mon->notify_module_add(module);
280 for (
auto mon : monitors)
281 mon->notify_module_add(module);
288 scratchpad.erase(varname);
293 scratchpad[varname] =
stringf(
"%d", value);
298 scratchpad[varname] = value ?
"true" :
"false";
303 scratchpad[varname] = value;
308 if (scratchpad.count(varname) == 0)
309 return default_value;
311 std::string str = scratchpad.at(varname);
313 if (str ==
"0" || str ==
"false")
316 if (str ==
"1" || str ==
"true")
319 char *endptr =
nullptr;
320 long int parsed_value = strtol(str.c_str(), &endptr, 10);
321 return *endptr ? default_value : parsed_value;
326 if (scratchpad.count(varname) == 0)
327 return default_value;
329 std::string str = scratchpad.at(varname);
331 if (str ==
"0" || str ==
"false")
334 if (str ==
"1" || str ==
"true")
337 return default_value;
342 if (scratchpad.count(varname) == 0)
343 return default_value;
344 return scratchpad.at(varname);
349 for (
auto mon : monitors)
350 mon->notify_module_del(module);
353 modules_.erase(module->
name);
360 for (
auto &it : modules_) {
371 for (
auto &it : modules_)
372 it.second->optimize();
373 for (
auto &it : selection_stack)
375 for (
auto &it : selection_vars)
376 it.second.optimize(
this);
381 if (!selected_active_module.empty() && mod_name != selected_active_module)
383 if (selection_stack.size() == 0)
385 return selection_stack.back().selected_module(mod_name);
390 if (!selected_active_module.empty() && mod_name != selected_active_module)
392 if (selection_stack.size() == 0)
394 return selection_stack.back().selected_whole_module(mod_name);
399 if (!selected_active_module.empty() && mod_name != selected_active_module)
401 if (selection_stack.size() == 0)
403 return selection_stack.back().selected_member(mod_name, memb_name);
408 return selected_module(mod->
name);
413 return selected_whole_module(mod->
name);
418 std::vector<RTLIL::Module*> result;
419 result.reserve(modules_.size());
420 for (
auto &it : modules_)
421 if (selected_module(it.first))
422 result.push_back(it.second);
428 std::vector<RTLIL::Module*> result;
429 result.reserve(modules_.size());
430 for (
auto &it : modules_)
431 if (selected_whole_module(it.first))
432 result.push_back(it.second);
438 std::vector<RTLIL::Module*> result;
439 result.reserve(modules_.size());
440 for (
auto &it : modules_)
441 if (selected_whole_module(it.first))
442 result.push_back(it.second);
443 else if (selected_module(it.first))
457 for (
auto it = wires_.begin(); it != wires_.end(); it++)
459 for (
auto it = memories.begin(); it != memories.end(); it++)
461 for (
auto it = cells_.begin(); it != cells_.end(); it++)
463 for (
auto it = processes.begin(); it != processes.end(); it++)
469 log_error(
"Module `%s' is used with parameters but is not parametric!\n",
id2cstr(name));
474 return wires_.count(
id) + memories.count(
id) + cells_.count(
id) + processes.count(
id);
479 struct InternalCellChecker
483 std::set<RTLIL::IdString> expected_params, expected_ports;
487 void error(
int linenr)
489 std::stringstream buf;
492 log_error(
"Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
494 cell->
name.
c_str(), cell->type.c_str(), __FILE__, linenr, buf.str().c_str());
497 int param(
const char *name)
499 if (cell->parameters.count(name) == 0)
501 expected_params.insert(name);
502 return cell->parameters.at(name).as_int();
508 if (cell->parameters.at(name).bits.size() > 32)
510 if (v != 0 && v != 1)
515 void param_bits(
const char *name,
int width)
518 if (
int(cell->parameters.at(name).bits.size()) != width)
522 void port(
const char *name,
int width)
524 if (!cell->hasPort(name))
526 if (cell->getPort(name).size() != width)
528 expected_ports.insert(name);
531 void check_expected(
bool check_matched_sign =
true)
533 for (
auto ¶ : cell->parameters)
534 if (expected_params.count(para.first) == 0)
536 for (
auto &conn : cell->connections())
537 if (expected_ports.count(conn.first) == 0)
540 if (expected_params.count(
"\\A_SIGNED") != 0 && expected_params.count(
"\\B_SIGNED") && check_matched_sign) {
541 bool a_is_signed = param(
"\\A_SIGNED") != 0;
542 bool b_is_signed = param(
"\\B_SIGNED") != 0;
543 if (a_is_signed != b_is_signed)
548 void check_gate(
const char *ports)
550 if (cell->parameters.size() != 0)
553 for (
const char *p = ports; *p; p++) {
554 char portname[3] = {
'\\', *p, 0 };
555 if (!cell->hasPort(portname))
557 if (cell->getPort(portname).size() != 1)
561 for (
auto &conn : cell->connections()) {
562 if (conn.first.size() != 2 || conn.first[0] !=
'\\')
564 if (strchr(ports, conn.first[1]) ==
NULL)
571 if (cell->type.substr(0, 1) !=
"$" || cell->type.substr(0, 3) ==
"$__" || cell->type.substr(0, 8) ==
"$paramod" ||
572 cell->type.substr(0, 9) ==
"$verific$" || cell->type.substr(0, 7) ==
"$array:" || cell->type.substr(0, 8) ==
"$extern:")
575 if (cell->type.in(
"$not",
"$pos",
"$neg")) {
577 port(
"\\A", param(
"\\A_WIDTH"));
578 port(
"\\Y", param(
"\\Y_WIDTH"));
583 if (cell->type.in(
"$and",
"$or",
"$xor",
"$xnor")) {
586 port(
"\\A", param(
"\\A_WIDTH"));
587 port(
"\\B", param(
"\\B_WIDTH"));
588 port(
"\\Y", param(
"\\Y_WIDTH"));
593 if (cell->type.in(
"$reduce_and",
"$reduce_or",
"$reduce_xor",
"$reduce_xnor",
"$reduce_bool")) {
595 port(
"\\A", param(
"\\A_WIDTH"));
596 port(
"\\Y", param(
"\\Y_WIDTH"));
601 if (cell->type.in(
"$shl",
"$shr",
"$sshl",
"$sshr",
"$shift",
"$shiftx")) {
604 port(
"\\A", param(
"\\A_WIDTH"));
605 port(
"\\B", param(
"\\B_WIDTH"));
606 port(
"\\Y", param(
"\\Y_WIDTH"));
607 check_expected(
false);
611 if (cell->type.in(
"$lt",
"$le",
"$eq",
"$ne",
"$eqx",
"$nex",
"$ge",
"$gt")) {
614 port(
"\\A", param(
"\\A_WIDTH"));
615 port(
"\\B", param(
"\\B_WIDTH"));
616 port(
"\\Y", param(
"\\Y_WIDTH"));
621 if (cell->type.in(
"$add",
"$sub",
"$mul",
"$div",
"$mod",
"$pow")) {
624 port(
"\\A", param(
"\\A_WIDTH"));
625 port(
"\\B", param(
"\\B_WIDTH"));
626 port(
"\\Y", param(
"\\Y_WIDTH"));
627 check_expected(cell->type !=
"$pow");
631 if (cell->type ==
"$fa") {
632 port(
"\\A", param(
"\\WIDTH"));
633 port(
"\\B", param(
"\\WIDTH"));
634 port(
"\\C", param(
"\\WIDTH"));
635 port(
"\\X", param(
"\\WIDTH"));
636 port(
"\\Y", param(
"\\WIDTH"));
641 if (cell->type ==
"$lcu") {
642 port(
"\\P", param(
"\\WIDTH"));
643 port(
"\\G", param(
"\\WIDTH"));
645 port(
"\\CO", param(
"\\WIDTH"));
650 if (cell->type ==
"$alu") {
653 port(
"\\A", param(
"\\A_WIDTH"));
654 port(
"\\B", param(
"\\B_WIDTH"));
657 port(
"\\X", param(
"\\Y_WIDTH"));
658 port(
"\\Y", param(
"\\Y_WIDTH"));
659 port(
"\\CO", param(
"\\Y_WIDTH"));
664 if (cell->type ==
"$macc") {
666 param(
"\\CONFIG_WIDTH");
667 port(
"\\A", param(
"\\A_WIDTH"));
668 port(
"\\B", param(
"\\B_WIDTH"));
669 port(
"\\Y", param(
"\\Y_WIDTH"));
675 if (cell->type ==
"$logic_not") {
677 port(
"\\A", param(
"\\A_WIDTH"));
678 port(
"\\Y", param(
"\\Y_WIDTH"));
683 if (cell->type ==
"$logic_and" || cell->type ==
"$logic_or") {
686 port(
"\\A", param(
"\\A_WIDTH"));
687 port(
"\\B", param(
"\\B_WIDTH"));
688 port(
"\\Y", param(
"\\Y_WIDTH"));
689 check_expected(
false);
693 if (cell->type ==
"$slice") {
695 port(
"\\A", param(
"\\A_WIDTH"));
696 port(
"\\Y", param(
"\\Y_WIDTH"));
697 if (param(
"\\OFFSET") + param(
"\\Y_WIDTH") > param(
"\\A_WIDTH"))
703 if (cell->type ==
"$concat") {
704 port(
"\\A", param(
"\\A_WIDTH"));
705 port(
"\\B", param(
"\\B_WIDTH"));
706 port(
"\\Y", param(
"\\A_WIDTH") + param(
"\\B_WIDTH"));
711 if (cell->type ==
"$mux") {
712 port(
"\\A", param(
"\\WIDTH"));
713 port(
"\\B", param(
"\\WIDTH"));
715 port(
"\\Y", param(
"\\WIDTH"));
720 if (cell->type ==
"$pmux") {
721 port(
"\\A", param(
"\\WIDTH"));
722 port(
"\\B", param(
"\\WIDTH") * param(
"\\S_WIDTH"));
723 port(
"\\S", param(
"\\S_WIDTH"));
724 port(
"\\Y", param(
"\\WIDTH"));
729 if (cell->type ==
"$lut") {
731 port(
"\\A", param(
"\\WIDTH"));
737 if (cell->type ==
"$sr") {
740 port(
"\\SET", param(
"\\WIDTH"));
741 port(
"\\CLR", param(
"\\WIDTH"));
742 port(
"\\Q", param(
"\\WIDTH"));
747 if (cell->type ==
"$dff") {
750 port(
"\\D", param(
"\\WIDTH"));
751 port(
"\\Q", param(
"\\WIDTH"));
756 if (cell->type ==
"$dffe") {
761 port(
"\\D", param(
"\\WIDTH"));
762 port(
"\\Q", param(
"\\WIDTH"));
767 if (cell->type ==
"$dffsr") {
772 port(
"\\SET", param(
"\\WIDTH"));
773 port(
"\\CLR", param(
"\\WIDTH"));
774 port(
"\\D", param(
"\\WIDTH"));
775 port(
"\\Q", param(
"\\WIDTH"));
780 if (cell->type ==
"$adff") {
783 param_bits(
"\\ARST_VALUE", param(
"\\WIDTH"));
786 port(
"\\D", param(
"\\WIDTH"));
787 port(
"\\Q", param(
"\\WIDTH"));
792 if (cell->type ==
"$dlatch") {
795 port(
"\\D", param(
"\\WIDTH"));
796 port(
"\\Q", param(
"\\WIDTH"));
801 if (cell->type ==
"$dlatchsr") {
806 port(
"\\SET", param(
"\\WIDTH"));
807 port(
"\\CLR", param(
"\\WIDTH"));
808 port(
"\\D", param(
"\\WIDTH"));
809 port(
"\\Q", param(
"\\WIDTH"));
814 if (cell->type ==
"$fsm") {
818 param(
"\\STATE_BITS");
819 param(
"\\STATE_NUM");
820 param(
"\\STATE_NUM_LOG2");
821 param(
"\\STATE_RST");
822 param_bits(
"\\STATE_TABLE", param(
"\\STATE_BITS") * param(
"\\STATE_NUM"));
823 param(
"\\TRANS_NUM");
824 param_bits(
"\\TRANS_TABLE", param(
"\\TRANS_NUM") * (2*param(
"\\STATE_NUM_LOG2") + param(
"\\CTRL_IN_WIDTH") + param(
"\\CTRL_OUT_WIDTH")));
827 port(
"\\CTRL_IN", param(
"\\CTRL_IN_WIDTH"));
828 port(
"\\CTRL_OUT", param(
"\\CTRL_OUT_WIDTH"));
833 if (cell->type ==
"$memrd") {
839 port(
"\\ADDR", param(
"\\ABITS"));
840 port(
"\\DATA", param(
"\\WIDTH"));
845 if (cell->type ==
"$memwr") {
851 port(
"\\EN", param(
"\\WIDTH"));
852 port(
"\\ADDR", param(
"\\ABITS"));
853 port(
"\\DATA", param(
"\\WIDTH"));
858 if (cell->type ==
"$mem") {
862 param_bits(
"\\RD_CLK_ENABLE", param(
"\\RD_PORTS"));
863 param_bits(
"\\RD_CLK_POLARITY", param(
"\\RD_PORTS"));
864 param_bits(
"\\RD_TRANSPARENT", param(
"\\RD_PORTS"));
865 param_bits(
"\\WR_CLK_ENABLE", param(
"\\WR_PORTS"));
866 param_bits(
"\\WR_CLK_POLARITY", param(
"\\WR_PORTS"));
867 port(
"\\RD_CLK", param(
"\\RD_PORTS"));
868 port(
"\\RD_ADDR", param(
"\\RD_PORTS") * param(
"\\ABITS"));
869 port(
"\\RD_DATA", param(
"\\RD_PORTS") * param(
"\\WIDTH"));
870 port(
"\\WR_CLK", param(
"\\WR_PORTS"));
871 port(
"\\WR_EN", param(
"\\WR_PORTS") * param(
"\\WIDTH"));
872 port(
"\\WR_ADDR", param(
"\\WR_PORTS") * param(
"\\ABITS"));
873 port(
"\\WR_DATA", param(
"\\WR_PORTS") * param(
"\\WIDTH"));
878 if (cell->type ==
"$assert") {
885 if (cell->type ==
"$_BUF_") { check_gate(
"AY");
return; }
886 if (cell->type ==
"$_NOT_") { check_gate(
"AY");
return; }
887 if (cell->type ==
"$_AND_") { check_gate(
"ABY");
return; }
888 if (cell->type ==
"$_NAND_") { check_gate(
"ABY");
return; }
889 if (cell->type ==
"$_OR_") { check_gate(
"ABY");
return; }
890 if (cell->type ==
"$_NOR_") { check_gate(
"ABY");
return; }
891 if (cell->type ==
"$_XOR_") { check_gate(
"ABY");
return; }
892 if (cell->type ==
"$_XNOR_") { check_gate(
"ABY");
return; }
893 if (cell->type ==
"$_MUX_") { check_gate(
"ABSY");
return; }
894 if (cell->type ==
"$_AOI3_") { check_gate(
"ABCY");
return; }
895 if (cell->type ==
"$_OAI3_") { check_gate(
"ABCY");
return; }
896 if (cell->type ==
"$_AOI4_") { check_gate(
"ABCDY");
return; }
897 if (cell->type ==
"$_OAI4_") { check_gate(
"ABCDY");
return; }
899 if (cell->type ==
"$_SR_NN_") { check_gate(
"SRQ");
return; }
900 if (cell->type ==
"$_SR_NP_") { check_gate(
"SRQ");
return; }
901 if (cell->type ==
"$_SR_PN_") { check_gate(
"SRQ");
return; }
902 if (cell->type ==
"$_SR_PP_") { check_gate(
"SRQ");
return; }
904 if (cell->type ==
"$_DFF_N_") { check_gate(
"DQC");
return; }
905 if (cell->type ==
"$_DFF_P_") { check_gate(
"DQC");
return; }
907 if (cell->type ==
"$_DFFE_NN_") { check_gate(
"DQCE");
return; }
908 if (cell->type ==
"$_DFFE_NP_") { check_gate(
"DQCE");
return; }
909 if (cell->type ==
"$_DFFE_PN_") { check_gate(
"DQCE");
return; }
910 if (cell->type ==
"$_DFFE_PP_") { check_gate(
"DQCE");
return; }
912 if (cell->type ==
"$_DFF_NN0_") { check_gate(
"DQCR");
return; }
913 if (cell->type ==
"$_DFF_NN1_") { check_gate(
"DQCR");
return; }
914 if (cell->type ==
"$_DFF_NP0_") { check_gate(
"DQCR");
return; }
915 if (cell->type ==
"$_DFF_NP1_") { check_gate(
"DQCR");
return; }
916 if (cell->type ==
"$_DFF_PN0_") { check_gate(
"DQCR");
return; }
917 if (cell->type ==
"$_DFF_PN1_") { check_gate(
"DQCR");
return; }
918 if (cell->type ==
"$_DFF_PP0_") { check_gate(
"DQCR");
return; }
919 if (cell->type ==
"$_DFF_PP1_") { check_gate(
"DQCR");
return; }
921 if (cell->type ==
"$_DFFSR_NNN_") { check_gate(
"CSRDQ");
return; }
922 if (cell->type ==
"$_DFFSR_NNP_") { check_gate(
"CSRDQ");
return; }
923 if (cell->type ==
"$_DFFSR_NPN_") { check_gate(
"CSRDQ");
return; }
924 if (cell->type ==
"$_DFFSR_NPP_") { check_gate(
"CSRDQ");
return; }
925 if (cell->type ==
"$_DFFSR_PNN_") { check_gate(
"CSRDQ");
return; }
926 if (cell->type ==
"$_DFFSR_PNP_") { check_gate(
"CSRDQ");
return; }
927 if (cell->type ==
"$_DFFSR_PPN_") { check_gate(
"CSRDQ");
return; }
928 if (cell->type ==
"$_DFFSR_PPP_") { check_gate(
"CSRDQ");
return; }
930 if (cell->type ==
"$_DLATCH_N_") { check_gate(
"EDQ");
return; }
931 if (cell->type ==
"$_DLATCH_P_") { check_gate(
"EDQ");
return; }
933 if (cell->type ==
"$_DLATCHSR_NNN_") { check_gate(
"ESRDQ");
return; }
934 if (cell->type ==
"$_DLATCHSR_NNP_") { check_gate(
"ESRDQ");
return; }
935 if (cell->type ==
"$_DLATCHSR_NPN_") { check_gate(
"ESRDQ");
return; }
936 if (cell->type ==
"$_DLATCHSR_NPP_") { check_gate(
"ESRDQ");
return; }
937 if (cell->type ==
"$_DLATCHSR_PNN_") { check_gate(
"ESRDQ");
return; }
938 if (cell->type ==
"$_DLATCHSR_PNP_") { check_gate(
"ESRDQ");
return; }
939 if (cell->type ==
"$_DLATCHSR_PPN_") { check_gate(
"ESRDQ");
return; }
940 if (cell->type ==
"$_DLATCHSR_PPP_") { check_gate(
"ESRDQ");
return; }
951 std::vector<bool> ports_declared;
952 for (
auto &it : wires_) {
958 for (
auto &it2 : it.second->attributes)
960 if (it.second->port_id) {
962 log_assert(ports.at(it.second->port_id-1) == it.first);
963 log_assert(it.second->port_input || it.second->port_output);
964 if (
GetSize(ports_declared) < it.second->port_id)
965 ports_declared.resize(it.second->port_id);
966 log_assert(ports_declared[it.second->port_id-1] ==
false);
967 ports_declared[it.second->port_id-1] =
true;
969 log_assert(!it.second->port_input && !it.second->port_output);
971 for (
auto port_declared : ports_declared)
975 for (
auto &it : memories) {
980 for (
auto &it2 : it.second->attributes)
984 for (
auto &it : cells_) {
989 for (
auto &it2 : it.second->connections()) {
993 for (
auto &it2 : it.second->attributes)
995 for (
auto &it2 : it.second->parameters)
997 InternalCellChecker checker(
this, it.second);
1001 for (
auto &it : processes) {
1007 for (
auto &it : connections_) {
1008 log_assert(it.first.size() == it.second.size());
1013 for (
auto &it : attributes)
1028 new_mod->attributes = attributes;
1030 for (
auto &it : wires_)
1031 new_mod->
addWire(it.first, it.second);
1033 for (
auto &it : memories)
1036 for (
auto &it : cells_)
1037 new_mod->
addCell(it.first, it.second);
1039 for (
auto &it : processes)
1040 new_mod->
processes[it.first] = it.second->clone();
1042 struct RewriteSigSpecWorker
1047 std::vector<RTLIL::SigChunk> chunks = sig.
chunks();
1048 for (
auto &c : chunks)
1050 c.wire = mod->wires_.at(c.wire->name);
1055 RewriteSigSpecWorker rewriteSigSpecWorker;
1056 rewriteSigSpecWorker.mod = new_mod;
1064 new_mod->
name = name;
1071 return !memories.empty();
1076 return !processes.empty();
1081 if (!memories.empty())
1082 log_warning(
"Ignoring module %s because it contains memories (run 'memory' command first).\n",
log_id(
this));
1083 return !memories.empty();
1088 if (!processes.empty())
1089 log_warning(
"Ignoring module %s because it contains processes (run 'proc' command first).\n",
log_id(
this));
1090 return !processes.empty();
1095 std::vector<RTLIL::Wire*> result;
1096 result.reserve(wires_.size());
1097 for (
auto &it : wires_)
1098 if (design->selected(
this, it.second))
1099 result.push_back(it.second);
1105 std::vector<RTLIL::Cell*> result;
1106 result.reserve(wires_.size());
1107 for (
auto &it : cells_)
1108 if (design->selected(
this, it.second))
1109 result.push_back(it.second);
1118 wires_[wire->
name] = wire;
1127 cells_[cell->
name] = cell;
1132 struct DeleteWireWorker
1135 const std::set<RTLIL::Wire*> *wires_p;
1138 std::vector<RTLIL::SigChunk> chunks = sig;
1139 for (
auto &c : chunks)
1140 if (c.wire !=
NULL && wires_p->count(c.wire)) {
1152 std::setPort<RTLIL::Wire*> wires_;
1153 wires_.insert(wire);
1162 DeleteWireWorker delete_wire_worker;
1163 delete_wire_worker.module =
this;
1164 delete_wire_worker.wires_p = &wires;
1165 rewrite_sigspecs(delete_wire_worker);
1167 for (
auto &it : wires) {
1169 wires_.erase(it->name);
1181 cells_.erase(cell->
name);
1189 wires_.erase(wire->
name);
1190 wire->
name = new_name;
1198 cells_.erase(cell->
name);
1199 cell->
name = new_name;
1206 if (wires_.count(old_name))
1207 rename(wires_.at(old_name), new_name);
1208 else if (cells_.count(old_name))
1209 rename(cells_.at(old_name), new_name);
1220 wires_.erase(w1->
name);
1221 wires_.erase(w2->
name);
1225 wires_[w1->
name] = w1;
1226 wires_[w2->
name] = w2;
1235 cells_.erase(c1->
name);
1236 cells_.erase(c2->
name);
1240 cells_[c1->
name] = c1;
1241 cells_[c2->
name] = c2;
1247 return uniquify(name, index);
1253 if (count_id(name) == 0)
1260 if (count_id(new_name) == 0)
1280 for (
auto mon : monitors)
1281 mon->notify_connect(
this, conn);
1284 for (
auto mon : design->monitors)
1285 mon->notify_connect(
this, conn);
1287 connections_.push_back(conn);
1297 for (
auto mon : monitors)
1298 mon->notify_connect(
this, new_conn);
1301 for (
auto mon : design->monitors)
1302 mon->notify_connect(
this, new_conn);
1304 connections_ = new_conn;
1309 return connections_;
1314 std::vector<RTLIL::Wire*> all_ports;
1316 for (
auto &w : wires_)
1317 if (w.second->port_input || w.second->port_output)
1318 all_ports.push_back(w.second);
1320 w.second->port_id = 0;
1325 for (
size_t i = 0; i < all_ports.size(); i++) {
1326 ports.push_back(all_ports[i]->name);
1327 all_ports[i]->port_id = i+1;
1335 wire->
width = width;
1349 wire->attributes = other->attributes;
1367 cell->attributes = other->attributes;
1371 #define DEF_METHOD(_func, _y_size, _type) \
1372 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1373 RTLIL::Cell *cell = addCell(name, _type); \
1374 cell->parameters["\\A_SIGNED"] = is_signed; \
1375 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1376 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1377 cell->setPort("\\A", sig_a); \
1378 cell->setPort("\\Y", sig_y); \
1381 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1382 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1383 add ## _func(name, sig_a, sig_y, is_signed); \
1397 #define DEF_METHOD(_func, _y_size, _type) \
1398 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1399 RTLIL::Cell *cell = addCell(name, _type); \
1400 cell->parameters["\\A_SIGNED"] = is_signed; \
1401 cell->parameters["\\B_SIGNED"] = is_signed; \
1402 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1403 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1404 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1405 cell->setPort("\\A", sig_a); \
1406 cell->setPort("\\B", sig_b); \
1407 cell->setPort("\\Y", sig_y); \
1410 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1411 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1412 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1415 DEF_METHOD(And, std::max(sig_a.size(), sig_b.size()),
"$and")
1416 DEF_METHOD(Or, std::max(sig_a.size(), sig_b.size()), "$or")
1417 DEF_METHOD(Xor, std::max(sig_a.size(), sig_b.size()), "$xor")
1418 DEF_METHOD(Xnor, std::max(sig_a.size(), sig_b.size()), "$xnor")
1433 DEF_METHOD(Add, std::max(sig_a.size(), sig_b.size()), "$add")
1434 DEF_METHOD(Sub, std::max(sig_a.size(), sig_b.size()), "$sub")
1435 DEF_METHOD(Mul, std::max(sig_a.size(), sig_b.size()), "$mul")
1436 DEF_METHOD(Div, std::max(sig_a.size(), sig_b.size()), "$div")
1442 #define DEF_METHOD(_func, _type, _pmux) \
1443 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y) { \
1444 RTLIL::Cell *cell = addCell(name, _type); \
1445 cell->parameters["\\WIDTH"] = sig_a.size(); \
1446 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1447 cell->setPort("\\A", sig_a); \
1448 cell->setPort("\\B", sig_b); \
1449 cell->setPort("\\S", sig_s); \
1450 cell->setPort("\\Y", sig_y); \
1453 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1454 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1455 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1462 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1463 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1464 RTLIL::Cell *cell = addCell(name, _type); \
1465 cell->setPort("\\" #_P1, sig1); \
1466 cell->setPort("\\" #_P2, sig2); \
1469 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1470 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1471 add ## _func(name, sig1, sig2); \
1474 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1475 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1476 RTLIL::Cell *cell = addCell(name, _type); \
1477 cell->setPort("\\" #_P1, sig1); \
1478 cell->setPort("\\" #_P2, sig2); \
1479 cell->setPort("\\" #_P3, sig3); \
1482 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1483 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1484 add ## _func(name, sig1, sig2, sig3); \
1487 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1488 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1489 RTLIL::Cell *cell = addCell(name, _type); \
1490 cell->setPort("\\" #_P1, sig1); \
1491 cell->setPort("\\" #_P2, sig2); \
1492 cell->setPort("\\" #_P3, sig3); \
1493 cell->setPort("\\" #_P4, sig4); \
1496 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1497 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1498 add ## _func(name, sig1, sig2, sig3, sig4); \
1501 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1502 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1503 RTLIL::Cell *cell = addCell(name, _type); \
1504 cell->setPort("\\" #_P1, sig1); \
1505 cell->setPort("\\" #_P2, sig2); \
1506 cell->setPort("\\" #_P3, sig3); \
1507 cell->setPort("\\" #_P4, sig4); \
1508 cell->setPort("\\" #_P5, sig5); \
1511 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1512 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1513 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1583 cell->
setPort(
"\\EN", sig_en);
1590 cell->
parameters[
"\\SET_POLARITY"] = set_polarity;
1591 cell->
parameters[
"\\CLR_POLARITY"] = clr_polarity;
1593 cell->
setPort(
"\\SET", sig_set);
1594 cell->
setPort(
"\\CLR", sig_clr);
1604 cell->
setPort(
"\\CLK", sig_clk);
1614 cell->
parameters[
"\\EN_POLARITY"] = en_polarity;
1616 cell->
setPort(
"\\CLK", sig_clk);
1617 cell->
setPort(
"\\EN", sig_en);
1628 cell->
parameters[
"\\SET_POLARITY"] = set_polarity;
1629 cell->
parameters[
"\\CLR_POLARITY"] = clr_polarity;
1631 cell->
setPort(
"\\CLK", sig_clk);
1632 cell->
setPort(
"\\SET", sig_set);
1633 cell->
setPort(
"\\CLR", sig_clr);
1644 cell->
parameters[
"\\ARST_POLARITY"] = arst_polarity;
1645 cell->
parameters[
"\\ARST_VALUE"] = arst_value;
1647 cell->
setPort(
"\\CLK", sig_clk);
1648 cell->
setPort(
"\\ARST", sig_arst);
1657 cell->
parameters[
"\\EN_POLARITY"] = en_polarity;
1659 cell->
setPort(
"\\EN", sig_en);
1669 cell->
parameters[
"\\EN_POLARITY"] = en_polarity;
1670 cell->
parameters[
"\\SET_POLARITY"] = set_polarity;
1671 cell->
parameters[
"\\CLR_POLARITY"] = clr_polarity;
1673 cell->
setPort(
"\\EN", sig_en);
1674 cell->
setPort(
"\\SET", sig_set);
1675 cell->
setPort(
"\\CLR", sig_clr);
1684 cell->
setPort(
"\\C", sig_clk);
1692 RTLIL::Cell *cell = addCell(name,
stringf(
"$_DFFE_%c%c_", clk_polarity ?
'P' :
'N', en_polarity ?
'P' :
'N'));
1693 cell->
setPort(
"\\C", sig_clk);
1703 RTLIL::Cell *cell = addCell(name,
stringf(
"$_DFFSR_%c%c%c_", clk_polarity ?
'P' :
'N', set_polarity ?
'P' :
'N', clr_polarity ?
'P' :
'N'));
1704 cell->
setPort(
"\\C", sig_clk);
1705 cell->
setPort(
"\\S", sig_set);
1706 cell->
setPort(
"\\R", sig_clr);
1713 bool arst_value,
bool clk_polarity,
bool arst_polarity)
1715 RTLIL::Cell *cell = addCell(name,
stringf(
"$_DFF_%c%c%c_", clk_polarity ?
'P' :
'N', arst_polarity ?
'P' :
'N', arst_value ?
'1' :
'0'));
1716 cell->
setPort(
"\\C", sig_clk);
1717 cell->
setPort(
"\\R", sig_arst);
1735 RTLIL::Cell *cell = addCell(name,
stringf(
"$_DLATCHSR_%c%c%c_", en_polarity ?
'P' :
'N', set_polarity ?
'P' :
'N', clr_polarity ?
'P' :
'N'));
1737 cell->
setPort(
"\\S", sig_set);
1738 cell->
setPort(
"\\R", sig_clr);
1752 port_output =
false;
1768 return connections_.count(portname) != 0;
1774 auto conn_it = connections_.find(portname);
1776 if (conn_it != connections_.end())
1779 mon->notify_connect(
this, conn_it->first, conn_it->second, signal);
1783 mon->notify_connect(
this, conn_it->first, conn_it->second, signal);
1785 connections_.erase(conn_it);
1791 auto conn_it = connections_.find(portname);
1793 if (conn_it == connections_.end()) {
1795 conn_it = connections_.find(portname);
1800 mon->notify_connect(
this, conn_it->first, conn_it->second, signal);
1804 mon->notify_connect(
this, conn_it->first, conn_it->second, signal);
1806 conn_it->second = signal;
1811 return connections_.at(portname);
1816 return connections_;
1821 return parameters.count(paramname) != 0;
1826 parameters.erase(paramname);
1831 parameters[paramname] = value;
1836 return parameters.at(paramname);
1842 InternalCellChecker checker(
NULL,
this);
1849 if (type.substr(0, 1) !=
"$" || type.substr(0, 2) ==
"$_" || type.substr(0, 8) ==
"$paramod" ||
1850 type.substr(0, 9) ==
"$verific$" || type.substr(0, 7) ==
"$array:" || type.substr(0, 8) ==
"$extern:")
1853 if (type ==
"$mux" || type ==
"$pmux") {
1854 parameters[
"\\WIDTH"] =
GetSize(connections_[
"\\Y"]);
1855 if (type ==
"$pmux")
1856 parameters[
"\\S_WIDTH"] =
GetSize(connections_[
"\\S"]);
1861 if (type ==
"$lut") {
1862 parameters[
"\\WIDTH"] =
GetSize(connections_[
"\\A"]);
1866 if (type ==
"$fa") {
1867 parameters[
"\\WIDTH"] =
GetSize(connections_[
"\\Y"]);
1871 if (type ==
"$lcu") {
1872 parameters[
"\\WIDTH"] =
GetSize(connections_[
"\\CO"]);
1876 bool signedness_ab = !type.in(
"$slice",
"$concat",
"$macc");
1878 if (connections_.count(
"\\A")) {
1879 if (signedness_ab) {
1881 parameters[
"\\A_SIGNED"] =
true;
1882 else if (parameters.count(
"\\A_SIGNED") == 0)
1883 parameters[
"\\A_SIGNED"] =
false;
1885 parameters[
"\\A_WIDTH"] =
GetSize(connections_[
"\\A"]);
1888 if (connections_.count(
"\\B")) {
1889 if (signedness_ab) {
1891 parameters[
"\\B_SIGNED"] =
true;
1892 else if (parameters.count(
"\\B_SIGNED") == 0)
1893 parameters[
"\\B_SIGNED"] =
false;
1895 parameters[
"\\B_WIDTH"] =
GetSize(connections_[
"\\B"]);
1898 if (connections_.count(
"\\Y"))
1899 parameters[
"\\Y_WIDTH"] =
GetSize(connections_[
"\\Y"]);
1923 this->width = wire->
width;
1931 this->width = width;
1932 this->offset = offset;
1975 ret.
offset = this->offset + offset;
1978 for (
int i = 0; i < length; i++)
1979 ret.
data.push_back(data[offset+i]);
1987 if (wire && other.
wire)
1991 if (wire != other.
wire)
1992 return wire < other.
wire;
1994 if (offset != other.
offset)
1995 return offset < other.
offset;
1997 if (width != other.
width)
1998 return width < other.
width;
2000 return data < other.
data;
2005 return wire == other.
wire && width == other.
width && offset == other.
offset && data == other.
data;
2028 cover(
"kernel.rtlil.sigspec.init.list");
2033 std::vector<RTLIL::SigSpec> parts_vec(parts.begin(), parts.end());
2034 for (
auto it = parts_vec.rbegin(); it != parts_vec.rend(); it++)
2040 cover(
"kernel.rtlil.sigspec.assign");
2043 hash_ = other.
hash_;
2047 if (!other.
bits_.empty())
2050 int last_end_offset = 0;
2052 for (
auto &bit : other.
bits_) {
2053 if (last && bit.
wire == last->
wire) {
2054 if (bit.wire ==
NULL) {
2055 last->
data.push_back(bit.data);
2058 }
else if (last_end_offset == bit.offset) {
2064 chunks_.push_back(bit);
2065 last = &chunks_.back();
2066 last_end_offset = bit.
offset + 1;
2077 cover(
"kernel.rtlil.sigspec.init.const");
2080 width_ = chunks_.back().width;
2087 cover(
"kernel.rtlil.sigspec.init.chunk");
2089 chunks_.push_back(chunk);
2090 width_ = chunks_.back().width;
2097 cover(
"kernel.rtlil.sigspec.init.wire");
2100 width_ = chunks_.back().width;
2107 cover(
"kernel.rtlil.sigspec.init.wire_part");
2110 width_ = chunks_.back().width;
2117 cover(
"kernel.rtlil.sigspec.init.str");
2120 width_ = chunks_.back().width;
2127 cover(
"kernel.rtlil.sigspec.init.int");
2137 cover(
"kernel.rtlil.sigspec.init.state");
2147 cover(
"kernel.rtlil.sigspec.init.bit");
2152 for (
int i = 0; i < width; i++)
2153 chunks_.push_back(bit);
2161 cover(
"kernel.rtlil.sigspec.init.stdvec_chunks");
2165 for (
auto &c : chunks)
2172 cover(
"kernel.rtlil.sigspec.init.stdvec_bits");
2176 for (
auto &bit : bits)
2183 cover(
"kernel.rtlil.sigspec.init.stdset_bits");
2187 for (
auto &bit : bits)
2194 cover(
"kernel.rtlil.sigspec.init.bool");
2206 if (that->
bits_.empty())
2209 cover(
"kernel.rtlil.sigspec.convert.pack");
2212 std::vector<RTLIL::SigBit> old_bits;
2213 old_bits.swap(that->
bits_);
2216 int last_end_offset = 0;
2218 for (
auto &bit : old_bits) {
2219 if (last && bit.
wire == last->
wire) {
2220 if (bit.wire ==
NULL) {
2221 last->
data.push_back(bit.data);
2224 }
else if (last_end_offset == bit.offset) {
2232 last_end_offset = bit.offset + 1;
2245 cover(
"kernel.rtlil.sigspec.convert.unpack");
2250 for (
int i = 0; i < c.width; i++)
2257 #define DJB2(_hash, _value) (_hash) = (((_hash) << 5) + (_hash)) + (_value)
2263 if (that->
hash_ != 0)
2266 cover(
"kernel.rtlil.sigspec.hash");
2271 if (c.wire ==
NULL) {
2272 for (
auto &v : c.data)
2280 if (that->
hash_ == 0)
2287 cover(
"kernel.rtlil.sigspec.sort");
2293 cover(
"kernel.rtlil.sigspec.sort_and_unify");
2294 *
this = this->to_sigbit_set();
2309 std::map<RTLIL::SigBit, RTLIL::SigBit> rules;
2325 cover(
"kernel.rtlil.sigspec.replace");
2333 for (
int i = 0; i <
GetSize(bits_); i++) {
2334 auto it = rules.find(bits_[i]);
2335 if (it != rules.end())
2336 other->
bits_[i] = it->second;
2344 remove2(pattern,
NULL);
2355 std::set<RTLIL::SigBit> pattern_bits = pattern.
to_sigbit_set();
2356 remove2(pattern_bits, other);
2361 remove2(pattern,
NULL);
2373 cover(
"kernel.rtlil.sigspec.remove_other");
2375 cover(
"kernel.rtlil.sigspec.remove");
2379 if (other !=
NULL) {
2384 std::vector<RTLIL::SigBit> new_bits, new_other_bits;
2386 new_bits.resize(
GetSize(bits_));
2388 new_other_bits.resize(
GetSize(bits_));
2391 for (
int i = 0; i <
GetSize(bits_); i++) {
2392 if (bits_[i].wire !=
NULL && pattern.count(bits_[i]))
2395 new_other_bits[k] = other->
bits_[i];
2396 new_bits[k++] = bits_[i];
2401 new_other_bits.resize(k);
2403 bits_.swap(new_bits);
2406 if (other !=
NULL) {
2407 other->
bits_.swap(new_other_bits);
2416 std::set<RTLIL::SigBit> pattern_bits = pattern.
to_sigbit_set();
2417 return extract(pattern_bits, other);
2423 cover(
"kernel.rtlil.sigspec.extract_other");
2425 cover(
"kernel.rtlil.sigspec.extract");
2429 std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
2434 for (
int i = 0; i < width_; i++)
2435 if (bits_match[i].wire && pattern.count(bits_match[i]))
2438 for (
int i = 0; i < width_; i++)
2439 if (bits_match[i].wire && pattern.count(bits_match[i]))
2449 cover(
"kernel.rtlil.sigspec.replace_pos");
2458 for (
int i = 0; i < with.
width_; i++)
2459 bits_.at(offset + i) = with.
bits_.at(i);
2468 cover(
"kernel.rtlil.sigspec.remove_const.packed");
2470 std::vector<RTLIL::SigChunk> new_chunks;
2471 new_chunks.reserve(
GetSize(chunks_));
2474 for (
auto &chunk : chunks_)
2475 if (chunk.wire !=
NULL) {
2476 new_chunks.push_back(chunk);
2477 width_ += chunk.width;
2480 chunks_.swap(new_chunks);
2484 cover(
"kernel.rtlil.sigspec.remove_const.unpacked");
2486 std::vector<RTLIL::SigBit> new_bits;
2487 new_bits.reserve(width_);
2489 for (
auto &bit : bits_)
2490 if (bit.wire !=
NULL)
2491 new_bits.push_back(bit);
2493 bits_.swap(new_bits);
2494 width_ = bits_.size();
2502 cover(
"kernel.rtlil.sigspec.remove_pos");
2510 bits_.erase(bits_.begin() + offset, bits_.begin() + offset + length);
2511 width_ = bits_.size();
2519 cover(
"kernel.rtlil.sigspec.extract_pos");
2520 return std::vector<RTLIL::SigBit>(bits_.begin() + offset, bits_.begin() + offset + length);
2533 cover(
"kernel.rtlil.sigspec.append");
2535 if (packed() != signal.
packed()) {
2541 for (
auto &other_c : signal.
chunks_)
2543 auto &my_last_c = chunks_.back();
2544 if (my_last_c.wire ==
NULL && other_c.wire ==
NULL) {
2545 auto &this_data = my_last_c.data;
2546 auto &other_data = other_c.data;
2547 this_data.insert(this_data.end(), other_data.begin(), other_data.end());
2548 my_last_c.width += other_c.width;
2550 if (my_last_c.wire == other_c.wire && my_last_c.offset + my_last_c.width == other_c.offset) {
2551 my_last_c.width += other_c.width;
2553 chunks_.push_back(other_c);
2556 bits_.insert(bits_.end(), signal.
bits_.begin(), signal.
bits_.end());
2566 cover(
"kernel.rtlil.sigspec.append_bit.packed");
2568 if (chunks_.size() == 0)
2569 chunks_.push_back(bit);
2572 if (chunks_.back().wire ==
NULL) {
2573 chunks_.back().data.push_back(bit.
data);
2574 chunks_.back().width++;
2576 chunks_.push_back(bit);
2578 if (chunks_.back().wire == bit.
wire && chunks_.back().offset + chunks_.back().
width == bit.
offset)
2579 chunks_.back().width++;
2581 chunks_.push_back(bit);
2585 cover(
"kernel.rtlil.sigspec.append_bit.unpacked");
2586 bits_.push_back(bit);
2595 cover(
"kernel.rtlil.sigspec.extend");
2600 remove(width, width_ - width);
2602 if (width_ < width) {
2607 while (width_ < width)
2614 cover(
"kernel.rtlil.sigspec.extend_u0");
2619 remove(width, width_ - width);
2621 if (width_ < width) {
2625 while (width_ < width)
2633 cover(
"kernel.rtlil.sigspec.repeat");
2636 for (
int i = 0; i < num; i++)
2646 cover(
"kernel.rtlil.sigspec.check.skip");
2650 cover(
"kernel.rtlil.sigspec.check.packed");
2653 for (
size_t i = 0; i < chunks_.size(); i++) {
2661 if (i > 0 && chunks_[i-1].wire == chunk.
wire)
2675 cover(
"kernel.rtlil.sigspec.check.unpacked");
2685 cover(
"kernel.rtlil.sigspec.comp_lt");
2690 if (width_ != other.
width_)
2691 return width_ < other.
width_;
2696 if (chunks_.size() != other.
chunks_.size())
2697 return chunks_.size() < other.
chunks_.size();
2702 if (hash_ != other.
hash_)
2703 return hash_ < other.
hash_;
2705 for (
size_t i = 0; i < chunks_.size(); i++)
2706 if (chunks_[i] != other.
chunks_[i]) {
2707 cover(
"kernel.rtlil.sigspec.comp_lt.hash_collision");
2708 return chunks_[i] < other.
chunks_[i];
2711 cover(
"kernel.rtlil.sigspec.comp_lt.equal");
2717 cover(
"kernel.rtlil.sigspec.comp_eq");
2722 if (width_ != other.
width_)
2728 if (chunks_.size() != chunks_.size())
2734 if (hash_ != other.
hash_)
2737 for (
size_t i = 0; i < chunks_.size(); i++)
2738 if (chunks_[i] != other.
chunks_[i]) {
2739 cover(
"kernel.rtlil.sigspec.comp_eq.hash_collision");
2743 cover(
"kernel.rtlil.sigspec.comp_eq.equal");
2749 cover(
"kernel.rtlil.sigspec.is_wire");
2752 return GetSize(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
2757 cover(
"kernel.rtlil.sigspec.is_chunk");
2765 cover(
"kernel.rtlil.sigspec.is_fully_const");
2768 for (
auto it = chunks_.begin(); it != chunks_.end(); it++)
2769 if (it->width > 0 && it->wire !=
NULL)
2776 cover(
"kernel.rtlil.sigspec.is_fully_def");
2779 for (
auto it = chunks_.begin(); it != chunks_.end(); it++) {
2780 if (it->width > 0 && it->wire !=
NULL)
2782 for (
size_t i = 0; i < it->data.size(); i++)
2791 cover(
"kernel.rtlil.sigspec.is_fully_undef");
2794 for (
auto it = chunks_.begin(); it != chunks_.end(); it++) {
2795 if (it->width > 0 && it->wire !=
NULL)
2797 for (
size_t i = 0; i < it->data.size(); i++)
2806 cover(
"kernel.rtlil.sigspec.has_marked_bits");
2809 for (
auto it = chunks_.begin(); it != chunks_.end(); it++)
2810 if (it->width > 0 && it->wire ==
NULL) {
2811 for (
size_t i = 0; i < it->data.size(); i++)
2820 cover(
"kernel.rtlil.sigspec.as_bool");
2831 cover(
"kernel.rtlil.sigspec.as_int");
2842 cover(
"kernel.rtlil.sigspec.as_string");
2846 for (
size_t i = chunks_.size(); i > 0; i--) {
2849 for (
int j = 0; j < chunk.
width; j++)
2859 cover(
"kernel.rtlil.sigspec.as_const");
2864 return chunks_[0].data;
2870 cover(
"kernel.rtlil.sigspec.as_wire");
2874 return chunks_[0].wire;
2879 cover(
"kernel.rtlil.sigspec.as_chunk");
2888 cover(
"kernel.rtlil.sigspec.match");
2891 std::string str = as_string();
2894 for (
size_t i = 0; i < pattern.size(); i++) {
2895 if (pattern[i] ==
' ')
2897 if (pattern[i] ==
'*') {
2898 if (str[i] !=
'z' && str[i] !=
'x')
2902 if (pattern[i] != str[i])
2911 cover(
"kernel.rtlil.sigspec.to_sigbit_set");
2914 std::set<RTLIL::SigBit> sigbits;
2915 for (
auto &c : chunks_)
2916 for (
int i = 0; i < c.width; i++)
2923 cover(
"kernel.rtlil.sigspec.to_sigbit_vector");
2931 cover(
"kernel.rtlil.sigspec.to_sigbit_map");
2938 std::map<RTLIL::SigBit, RTLIL::SigBit> new_map;
2939 for (
int i = 0; i < width_; i++)
2940 new_map[bits_[i]] = other.
bits_[i];
2947 cover(
"kernel.rtlil.sigspec.to_single_sigbit");
2951 for (
auto &c : chunks_)
2959 size_t start = 0, end = 0;
2960 while ((end = text.find(sep, start)) != std::string::npos) {
2961 tokens.push_back(text.substr(start, end - start));
2964 tokens.push_back(text.substr(start));
2974 cover(
"kernel.rtlil.sigspec.parse");
2976 std::vector<std::string> tokens;
2980 for (
int tokidx =
int(tokens.size())-1; tokidx >= 0; tokidx--)
2982 std::string
netname = tokens[tokidx];
2983 std::string indices;
2985 if (netname.size() == 0)
2988 if ((
'0' <= netname[0] && netname[0] <=
'9') || netname[0] ==
'\'') {
2989 cover(
"kernel.rtlil.sigspec.parse.const");
3002 cover(
"kernel.rtlil.sigspec.parse.net");
3004 if (netname[0] !=
'$' && netname[0] !=
'\\')
3007 if (module->
wires_.count(netname) == 0) {
3008 size_t indices_pos = netname.size()-1;
3009 if (indices_pos > 2 && netname[indices_pos] ==
']')
3012 while (indices_pos > 0 && (
'0' <= netname[indices_pos] && netname[indices_pos] <=
'9')) indices_pos--;
3013 if (indices_pos > 0 && netname[indices_pos] ==
':') {
3015 while (indices_pos > 0 && (
'0' <= netname[indices_pos] && netname[indices_pos] <=
'9')) indices_pos--;
3017 if (indices_pos > 0 && netname[indices_pos] ==
'[') {
3018 indices = netname.substr(indices_pos);
3019 netname = netname.substr(0, indices_pos);
3024 if (module->
wires_.count(netname) == 0)
3028 if (!indices.empty()) {
3029 std::vector<std::string> index_tokens;
3031 if (index_tokens.size() == 1) {
3032 cover(
"kernel.rtlil.sigspec.parse.bit_sel");
3033 int a = atoi(index_tokens.at(0).c_str());
3034 if (a < 0 || a >= wire->
width)
3038 cover(
"kernel.rtlil.sigspec.parse.part_sel");
3039 int a = atoi(index_tokens.at(0).c_str());
3040 int b = atoi(index_tokens.at(1).c_str());
3045 if (a < 0 || a >= wire->
width)
3047 if (b < 0 || b >= wire->
width)
3060 if (str.empty() || str[0] !=
'@')
3061 return parse(sig, module, str);
3063 cover(
"kernel.rtlil.sigspec.parse.sel");
3071 for (
auto &it : module->
wires_)
3081 cover(
"kernel.rtlil.sigspec.parse.rhs_zeros");
3087 cover(
"kernel.rtlil.sigspec.parse.rhs_ones");
3092 if (lhs.
chunks_.size() == 1) {
3093 char *p = (
char*)str.c_str(), *endptr;
3094 long int val = strtol(p, &endptr, 10);
3095 if (endptr && endptr != p && *endptr == 0) {
3097 cover(
"kernel.rtlil.sigspec.parse.rhs_dec");
3102 return parse(sig, module, str);
3107 for (
auto it = switches.begin(); it != switches.end(); it++)
3114 new_caserule->
compare = compare;
3115 new_caserule->
actions = actions;
3116 for (
auto &it : switches)
3117 new_caserule->
switches.push_back(it->clone());
3118 return new_caserule;
3123 for (
auto it = cases.begin(); it != cases.end(); it++)
3130 new_switchrule->
signal = signal;
3131 new_switchrule->attributes = attributes;
3132 for (
auto &it : cases)
3133 new_switchrule->
cases.push_back(it->clone());
3134 return new_switchrule;
3141 new_syncrule->
type = type;
3142 new_syncrule->
signal = signal;
3143 new_syncrule->
actions = actions;
3144 return new_syncrule;
3149 for (
auto it = syncs.begin(); it != syncs.end(); it++)
3157 new_proc->
name = name;
3158 new_proc->attributes = attributes;
3165 for (
auto &it : syncs)
3166 new_proc->
syncs.push_back(it->clone());
bool selected_module(RTLIL::IdString mod_name) const
void fixup_parameters(bool set_a_signed=false, bool set_b_signed=false)
const char * c_str() const
bool is_fully_def() const
void cloneInto(RTLIL::Module *new_mod) const
void dump_cell(std::ostream &f, std::string indent, const RTLIL::Cell *cell)
bool is_fully_undef() const
static void append(const vec< T > &from, vec< T > &to)
void unsetPort(RTLIL::IdString portname)
RTLIL::Cell * addSlice(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const offset)
void scratchpad_set_int(std::string varname, int value)
bool selected_whole_module(RTLIL::IdString mod_name) const
std::string stringf(const char *fmt,...)
void sort(T *array, int size, LessThan lt)
void new_connections(const std::vector< RTLIL::SigSig > &new_conn)
void log_warning(const char *format,...)
#define DEF_METHOD_3(_func, _type, _P1, _P2, _P3)
bool selected_module(RTLIL::IdString mod_name) const
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
void add(RTLIL::Module *module)
bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
std::set< RTLIL::Monitor * > monitors
static bool parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str)
void setParam(RTLIL::IdString paramname, RTLIL::Const value)
RTLIL::Cell * addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true)
const std::vector< RTLIL::SigSig > & connections() const
#define YOSYS_NAMESPACE_END
RTLIL::Const as_const() const
#define DJB2(_hash, _value)
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
void rewrite_sigspecs(T functor)
std::set< RTLIL::SigBit > to_sigbit_set() const
void scratchpad_unset(std::string varname)
RTLIL::SigChunk extract(int offset, int length) const
RTLIL_ATTRIBUTE_MEMBERS std::vector< RTLIL::CaseRule * > cases
RTLIL::Cell * addDlatchGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true)
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
RTLIL::SyncRule * clone() const
std::map< RTLIL::IdString, RTLIL::Memory * > memories
RTLIL::Cell * addLut(RTLIL::IdString name, RTLIL::SigSpec sig_i, RTLIL::SigSpec sig_o, RTLIL::Const lut)
void unsetParam(RTLIL::IdString paramname)
std::vector< RTLIL::SigBit > bits_
void log_error(const char *format,...)
void swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2)
std::string scratchpad_get_string(std::string varname, std::string default_value=std::string()) const
std::map< RTLIL::IdString, RTLIL::Const > parameters
void extend_u0(int width, bool is_signed=false)
void remove(const RTLIL::SigSpec &pattern)
#define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5)
#define DEF_METHOD(_func, _y_size, _type)
bool operator<(const RTLIL::SigSpec &other) const
std::vector< RTLIL::SigSpec > compare
std::vector< RTLIL::Module * > selected_whole_modules_warn() const
bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map< RTLIL::IdString, RTLIL::Const > parameters)
bool has_processes_warn() const
std::vector< RTLIL::SigSig > connections_
bool operator<(const RTLIL::SigChunk &other) const
int scratchpad_get_int(std::string varname, int default_value=0) const
static std::string escape_id(std::string str)
bool match(std::string pattern) const
RTLIL_ATTRIBUTE_MEMBERS Module()
#define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4)
RTLIL::Cell * addDlatch(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true)
RTLIL::Cell * addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true)
void connect(const RTLIL::SigSig &conn)
void append_bit(const RTLIL::SigBit &bit)
void add(RTLIL::Wire *wire)
bool operator!=(const RTLIL::SigChunk &other) const
std::map< RTLIL::IdString, RTLIL::Selection > selection_vars
std::vector< RTLIL::SigChunk > chunks_
std::string as_string() const
static std::vector< int > global_free_idx_list_
RTLIL_ATTRIBUTE_MEMBERS bool hasPort(RTLIL::IdString portname) const
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
#define DEF_METHOD_2(_func, _type, _P1, _P2)
int GetSize(RTLIL::Wire *wire)
std::vector< RTLIL::Wire * > selected_wires() const
RTLIL::Cell * addAssert(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en)
const BigUnsigned & check(const BigUnsigned &x)
std::string decode_string() const
#define log_assert(_assert_expr_)
bool is_fully_const() const
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
RTLIL::SigChunk as_chunk() const
bool selected_whole_module(RTLIL::IdString mod_name) const
std::vector< RTLIL::Cell * > selected_cells() const
const RTLIL::SigSpec & operator=(const RTLIL::SigSpec &other)
static uint32_t hash(uint32_t x)
RTLIL::Cell * addConcat(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y)
RTLIL::Cell * addDlatchsr(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true, bool set_polarity=true, bool clr_polarity=true)
static std::vector< char * > global_id_storage_
bool scratchpad_get_bool(std::string varname, bool default_value=false) const
bool operator<(const RTLIL::Const &other) const
void from_cell(RTLIL::Cell *cell)
RTLIL::Cell * addAdff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, RTLIL::Const arst_value, bool clk_polarity=true, bool arst_polarity=true)
RTLIL::Process * clone() const
void remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other)
RTLIL::SigBit to_single_sigbit() const
static std::map< char *, int, char_ptr_cmp > global_id_index_
bool operator==(const RTLIL::SigChunk &other) const
std::vector< RTLIL::SigSig > actions
static const char * id2cstr(const RTLIL::IdString &str)
RTLIL::CaseRule * clone() const
int as_int(bool is_signed=false) const
RTLIL::Cell * addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity=true, bool clr_polarity=true)
std::vector< RTLIL::Module * > selected_whole_modules() const
RTLIL::Module * addModule(RTLIL::IdString name)
RTLIL::Module * module(RTLIL::IdString name)
std::map< RTLIL::IdString, RTLIL::Process * > processes
RTLIL::Cell * addDffsr(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool set_polarity=true, bool clr_polarity=true)
virtual size_t count_id(RTLIL::IdString id)
RTLIL::ObjRange< RTLIL::Module * > modules()
bool has_memories() const
std::map< RTLIL::IdString, RTLIL::Module * > modules_
void replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
#define YOSYS_NAMESPACE_BEGIN
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN std::string netname(std::set< std::string > &conntypes_code, std::set< std::string > &celltypes_code, std::set< std::string > &constcells_code, RTLIL::SigSpec sig)
int as_int(bool is_signed=false) const
void remove(const std::set< RTLIL::Wire * > &wires)
static int sigspec_parse_get_dummy_line_num()
RTLIL::IdString uniquify(RTLIL::IdString name)
RTLIL::Cell * addDffsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool set_polarity=true, bool clr_polarity=true)
std::vector< RTLIL::SyncRule * > syncs
RTLIL::SwitchRule * clone() const
const RTLIL::Const & getParam(RTLIL::IdString paramname) const
static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
RTLIL::SigSpec repeat(int num) const
void scratchpad_set_bool(std::string varname, bool value)
std::vector< RTLIL::State > bits
std::string as_string() const
void append(const RTLIL::SigSpec &signal)
std::vector< RTLIL::Module * > selected_modules() const
bool operator==(const RTLIL::Const &other) const
bool hasParam(RTLIL::IdString paramname) const
static bool parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
void extend(int width, bool is_signed=false)
void optimize(RTLIL::Design *design)
std::map< RTLIL::IdString, RTLIL::SigSpec > connections_
RTLIL::Cell * addDlatchsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity=true, bool set_polarity=true, bool clr_polarity=true)
AST::AstNode * const2ast(std::string code, char case_type=0, bool warn_z=false)
static struct RTLIL::IdString::destruct_guard_t destruct_guard
bool has_memories_warn() const
bool operator!=(const RTLIL::Const &other) const
std::vector< RTLIL::State > bits
std::vector< RTLIL::SigSig > actions
std::set< RTLIL::Monitor * > monitors
void remove(RTLIL::Module *module)
RTLIL::Cell * addDffe(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool en_polarity=true)
RTLIL::Wire * as_wire() const
std::map< RTLIL::SigBit, RTLIL::SigBit > to_sigbit_map(const RTLIL::SigSpec &other) const
std::vector< RTLIL::SwitchRule * > switches
std::pair< SigSpec, SigSpec > SigSig
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
RTLIL::Cell * addDffeGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity=true, bool en_polarity=true)
bool has_marked_bits() const
std::vector< RTLIL::State > data
const char * log_id(RTLIL::IdString str)
static bool parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
bool operator==(const RTLIL::SigSpec &other) const
std::vector< RTLIL::SigBit > to_sigbit_vector() const
RTLIL::Cell * addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed=false, bool b_signed=false)
void rename(RTLIL::Wire *wire, RTLIL::IdString new_name)
bool has_processes() const
const std::vector< RTLIL::SigChunk > & chunks() const
static std::vector< int > global_refcount_storage_
void scratchpad_set_string(std::string varname, std::string value)
RTLIL_ATTRIBUTE_MEMBERS RTLIL::CaseRule root_case
RTLIL::Cell * addAdffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool arst_value=false, bool clk_polarity=true, bool arst_polarity=true)
virtual RTLIL::Module * clone() const
static void sigspec_parse_split(std::vector< std::string > &tokens, const std::string &text, char sep)