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/compatibility.h"
21 #include "kernel/rtlil.h"
22 #include "kernel/log.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
29 int RTLIL::autoidx
= 1;
33 flags
= RTLIL::CONST_FLAG_NONE
;
36 RTLIL::Const::Const(std::string str
)
38 flags
= RTLIL::CONST_FLAG_STRING
;
39 for (int i
= str
.size()-1; i
>= 0; i
--) {
40 unsigned char ch
= str
[i
];
41 for (int j
= 0; j
< 8; j
++) {
42 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
48 RTLIL::Const::Const(int val
, int width
)
50 flags
= RTLIL::CONST_FLAG_NONE
;
51 for (int i
= 0; i
< width
; i
++) {
52 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
57 RTLIL::Const::Const(RTLIL::State bit
, int width
)
59 flags
= RTLIL::CONST_FLAG_NONE
;
60 for (int i
= 0; i
< width
; i
++)
64 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
66 if (bits
.size() != other
.bits
.size())
67 return bits
.size() < other
.bits
.size();
68 for (size_t i
= 0; i
< bits
.size(); i
++)
69 if (bits
[i
] != other
.bits
[i
])
70 return bits
[i
] < other
.bits
[i
];
74 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
76 return bits
== other
.bits
;
79 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
81 return bits
!= other
.bits
;
84 bool RTLIL::Const::as_bool() const
86 for (size_t i
= 0; i
< bits
.size(); i
++)
87 if (bits
[i
] == RTLIL::S1
)
92 int RTLIL::Const::as_int() const
95 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
96 if (bits
[i
] == RTLIL::S1
)
101 std::string
RTLIL::Const::as_string() const
104 for (size_t i
= bits
.size(); i
> 0; i
--)
106 case S0
: ret
+= "0"; break;
107 case S1
: ret
+= "1"; break;
108 case Sx
: ret
+= "x"; break;
109 case Sz
: ret
+= "z"; break;
110 case Sa
: ret
+= "-"; break;
111 case Sm
: ret
+= "m"; break;
116 std::string
RTLIL::Const::decode_string() const
119 std::vector
<char> string_chars
;
120 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
122 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
123 if (bits
[i
+ j
] == RTLIL::State::S1
)
126 string_chars
.push_back(ch
);
128 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
129 string
+= string_chars
[i
];
133 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
137 if (selected_modules
.count(mod_name
) > 0)
139 if (selected_members
.count(mod_name
) > 0)
144 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
148 if (selected_modules
.count(mod_name
) > 0)
153 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
157 if (selected_modules
.count(mod_name
) > 0)
159 if (selected_members
.count(mod_name
) > 0)
160 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
165 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
167 if (full_selection
) {
168 selected_modules
.clear();
169 selected_members
.clear();
173 std::vector
<RTLIL::IdString
> del_list
, add_list
;
176 for (auto mod_name
: selected_modules
) {
177 if (design
->modules_
.count(mod_name
) == 0)
178 del_list
.push_back(mod_name
);
179 selected_members
.erase(mod_name
);
181 for (auto mod_name
: del_list
)
182 selected_modules
.erase(mod_name
);
185 for (auto &it
: selected_members
)
186 if (design
->modules_
.count(it
.first
) == 0)
187 del_list
.push_back(it
.first
);
188 for (auto mod_name
: del_list
)
189 selected_members
.erase(mod_name
);
191 for (auto &it
: selected_members
) {
193 for (auto memb_name
: it
.second
)
194 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
195 del_list
.push_back(memb_name
);
196 for (auto memb_name
: del_list
)
197 it
.second
.erase(memb_name
);
202 for (auto &it
: selected_members
)
203 if (it
.second
.size() == 0)
204 del_list
.push_back(it
.first
);
205 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
206 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
207 add_list
.push_back(it
.first
);
208 for (auto mod_name
: del_list
)
209 selected_members
.erase(mod_name
);
210 for (auto mod_name
: add_list
) {
211 selected_members
.erase(mod_name
);
212 selected_modules
.insert(mod_name
);
215 if (selected_modules
.size() == design
->modules_
.size()) {
216 full_selection
= true;
217 selected_modules
.clear();
218 selected_members
.clear();
222 RTLIL::Design::~Design()
224 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
228 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
230 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
233 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
235 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
238 void RTLIL::Design::add(RTLIL::Module
*module
)
240 log_assert(modules_
.count(module
->name
) == 0);
241 log_assert(refcount_modules_
== 0);
242 modules_
[module
->name
] = module
;
245 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
247 log_assert(modules_
.count(name
) == 0);
248 log_assert(refcount_modules_
== 0);
249 modules_
[name
] = new RTLIL::Module
;
250 modules_
[name
]->name
= name
;
251 return modules_
[name
];
254 void RTLIL::Design::remove(RTLIL::Module
*module
)
256 log_assert(modules_
.at(module
->name
) == module
);
257 modules_
.erase(module
->name
);
261 void RTLIL::Design::check()
264 for (auto &it
: modules_
) {
265 log_assert(it
.first
== it
.second
->name
);
266 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
272 void RTLIL::Design::optimize()
274 for (auto &it
: modules_
)
275 it
.second
->optimize();
276 for (auto &it
: selection_stack
)
278 for (auto &it
: selection_vars
)
279 it
.second
.optimize(this);
282 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
284 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
286 if (selection_stack
.size() == 0)
288 return selection_stack
.back().selected_module(mod_name
);
291 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
293 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
295 if (selection_stack
.size() == 0)
297 return selection_stack
.back().selected_whole_module(mod_name
);
300 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
302 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
304 if (selection_stack
.size() == 0)
306 return selection_stack
.back().selected_member(mod_name
, memb_name
);
309 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
311 return selected_module(mod
->name
);
314 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
316 return selected_whole_module(mod
->name
);
319 RTLIL::Module::Module()
325 RTLIL::Module::~Module()
327 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
329 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
331 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
333 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
337 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
339 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
342 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
344 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
349 struct InternalCellChecker
351 RTLIL::Module
*module
;
353 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
355 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
357 void error(int linenr
)
362 FILE *f
= open_memstream(&ptr
, &size
);
363 ILANG_BACKEND::dump_cell(f
, " ", cell
);
367 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
368 module
? module
->name
.c_str() : "", module
? "." : "",
369 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
372 int param(const char *name
)
374 if (cell
->parameters
.count(name
) == 0)
376 expected_params
.insert(name
);
377 return cell
->parameters
.at(name
).as_int();
380 int param_bool(const char *name
)
383 if (cell
->parameters
.at(name
).bits
.size() > 32)
385 if (v
!= 0 && v
!= 1)
390 void param_bits(const char *name
, int width
)
393 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
397 void port(const char *name
, int width
)
399 if (!cell
->has(name
))
401 if (cell
->get(name
).size() != width
)
403 expected_ports
.insert(name
);
406 void check_expected(bool check_matched_sign
= true)
408 for (auto ¶
: cell
->parameters
)
409 if (expected_params
.count(para
.first
) == 0)
411 for (auto &conn
: cell
->connections())
412 if (expected_ports
.count(conn
.first
) == 0)
415 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
416 bool a_is_signed
= param("\\A_SIGNED") != 0;
417 bool b_is_signed
= param("\\B_SIGNED") != 0;
418 if (a_is_signed
!= b_is_signed
)
423 void check_gate(const char *ports
)
425 if (cell
->parameters
.size() != 0)
428 for (const char *p
= ports
; *p
; p
++) {
429 char portname
[3] = { '\\', *p
, 0 };
430 if (!cell
->has(portname
))
432 if (cell
->get(portname
).size() != 1)
436 for (auto &conn
: cell
->connections()) {
437 if (conn
.first
.size() != 2 || conn
.first
.at(0) != '\\')
439 if (strchr(ports
, conn
.first
.at(1)) == NULL
)
446 if (cell
->type
[0] != '$' || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
447 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
450 if (cell
->type
== "$not" || cell
->type
== "$pos" || cell
->type
== "$bu0" || cell
->type
== "$neg") {
451 param_bool("\\A_SIGNED");
452 port("\\A", param("\\A_WIDTH"));
453 port("\\Y", param("\\Y_WIDTH"));
458 if (cell
->type
== "$and" || cell
->type
== "$or" || cell
->type
== "$xor" || cell
->type
== "$xnor") {
459 param_bool("\\A_SIGNED");
460 param_bool("\\B_SIGNED");
461 port("\\A", param("\\A_WIDTH"));
462 port("\\B", param("\\B_WIDTH"));
463 port("\\Y", param("\\Y_WIDTH"));
468 if (cell
->type
== "$reduce_and" || cell
->type
== "$reduce_or" || cell
->type
== "$reduce_xor" ||
469 cell
->type
== "$reduce_xnor" || cell
->type
== "$reduce_bool") {
470 param_bool("\\A_SIGNED");
471 port("\\A", param("\\A_WIDTH"));
472 port("\\Y", param("\\Y_WIDTH"));
477 if (cell
->type
== "$shl" || cell
->type
== "$shr" || cell
->type
== "$sshl" || cell
->type
== "$sshr" ||
478 cell
->type
== "$shift" || cell
->type
== "$shiftx") {
479 param_bool("\\A_SIGNED");
480 param_bool("\\B_SIGNED");
481 port("\\A", param("\\A_WIDTH"));
482 port("\\B", param("\\B_WIDTH"));
483 port("\\Y", param("\\Y_WIDTH"));
484 check_expected(false);
488 if (cell
->type
== "$lt" || cell
->type
== "$le" || cell
->type
== "$eq" || cell
->type
== "$ne" ||
489 cell
->type
== "$eqx" || cell
->type
== "$nex" || cell
->type
== "$ge" || cell
->type
== "$gt") {
490 param_bool("\\A_SIGNED");
491 param_bool("\\B_SIGNED");
492 port("\\A", param("\\A_WIDTH"));
493 port("\\B", param("\\B_WIDTH"));
494 port("\\Y", param("\\Y_WIDTH"));
499 if (cell
->type
== "$add" || cell
->type
== "$sub" || cell
->type
== "$mul" || cell
->type
== "$div" ||
500 cell
->type
== "$mod" || cell
->type
== "$pow") {
501 param_bool("\\A_SIGNED");
502 param_bool("\\B_SIGNED");
503 port("\\A", param("\\A_WIDTH"));
504 port("\\B", param("\\B_WIDTH"));
505 port("\\Y", param("\\Y_WIDTH"));
506 check_expected(cell
->type
!= "$pow");
510 if (cell
->type
== "$logic_not") {
511 param_bool("\\A_SIGNED");
512 port("\\A", param("\\A_WIDTH"));
513 port("\\Y", param("\\Y_WIDTH"));
518 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
519 param_bool("\\A_SIGNED");
520 param_bool("\\B_SIGNED");
521 port("\\A", param("\\A_WIDTH"));
522 port("\\B", param("\\B_WIDTH"));
523 port("\\Y", param("\\Y_WIDTH"));
524 check_expected(false);
528 if (cell
->type
== "$slice") {
530 port("\\A", param("\\A_WIDTH"));
531 port("\\Y", param("\\Y_WIDTH"));
532 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
538 if (cell
->type
== "$concat") {
539 port("\\A", param("\\A_WIDTH"));
540 port("\\B", param("\\B_WIDTH"));
541 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
546 if (cell
->type
== "$mux") {
547 port("\\A", param("\\WIDTH"));
548 port("\\B", param("\\WIDTH"));
550 port("\\Y", param("\\WIDTH"));
555 if (cell
->type
== "$pmux" || cell
->type
== "$safe_pmux") {
556 port("\\A", param("\\WIDTH"));
557 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
558 port("\\S", param("\\S_WIDTH"));
559 port("\\Y", param("\\WIDTH"));
564 if (cell
->type
== "$lut") {
566 port("\\I", param("\\WIDTH"));
572 if (cell
->type
== "$sr") {
573 param_bool("\\SET_POLARITY");
574 param_bool("\\CLR_POLARITY");
575 port("\\SET", param("\\WIDTH"));
576 port("\\CLR", param("\\WIDTH"));
577 port("\\Q", param("\\WIDTH"));
582 if (cell
->type
== "$dff") {
583 param_bool("\\CLK_POLARITY");
585 port("\\D", param("\\WIDTH"));
586 port("\\Q", param("\\WIDTH"));
591 if (cell
->type
== "$dffsr") {
592 param_bool("\\CLK_POLARITY");
593 param_bool("\\SET_POLARITY");
594 param_bool("\\CLR_POLARITY");
596 port("\\SET", param("\\WIDTH"));
597 port("\\CLR", param("\\WIDTH"));
598 port("\\D", param("\\WIDTH"));
599 port("\\Q", param("\\WIDTH"));
604 if (cell
->type
== "$adff") {
605 param_bool("\\CLK_POLARITY");
606 param_bool("\\ARST_POLARITY");
607 param_bits("\\ARST_VALUE", param("\\WIDTH"));
610 port("\\D", param("\\WIDTH"));
611 port("\\Q", param("\\WIDTH"));
616 if (cell
->type
== "$dlatch") {
617 param_bool("\\EN_POLARITY");
619 port("\\D", param("\\WIDTH"));
620 port("\\Q", param("\\WIDTH"));
625 if (cell
->type
== "$dlatchsr") {
626 param_bool("\\EN_POLARITY");
627 param_bool("\\SET_POLARITY");
628 param_bool("\\CLR_POLARITY");
630 port("\\SET", param("\\WIDTH"));
631 port("\\CLR", param("\\WIDTH"));
632 port("\\D", param("\\WIDTH"));
633 port("\\Q", param("\\WIDTH"));
638 if (cell
->type
== "$fsm") {
640 param_bool("\\CLK_POLARITY");
641 param_bool("\\ARST_POLARITY");
642 param("\\STATE_BITS");
643 param("\\STATE_NUM");
644 param("\\STATE_NUM_LOG2");
645 param("\\STATE_RST");
646 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
647 param("\\TRANS_NUM");
648 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
651 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
652 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
657 if (cell
->type
== "$memrd") {
659 param_bool("\\CLK_ENABLE");
660 param_bool("\\CLK_POLARITY");
661 param_bool("\\TRANSPARENT");
663 port("\\ADDR", param("\\ABITS"));
664 port("\\DATA", param("\\WIDTH"));
669 if (cell
->type
== "$memwr") {
671 param_bool("\\CLK_ENABLE");
672 param_bool("\\CLK_POLARITY");
675 port("\\EN", param("\\WIDTH"));
676 port("\\ADDR", param("\\ABITS"));
677 port("\\DATA", param("\\WIDTH"));
682 if (cell
->type
== "$mem") {
686 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
687 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
688 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
689 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
690 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
691 port("\\RD_CLK", param("\\RD_PORTS"));
692 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
693 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
694 port("\\WR_CLK", param("\\WR_PORTS"));
695 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
696 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
697 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
702 if (cell
->type
== "$assert") {
709 if (cell
->type
== "$_INV_") { check_gate("AY"); return; }
710 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
711 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
712 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
713 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
715 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
716 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
717 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
718 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
720 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
721 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
723 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
724 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
725 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
726 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
727 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
728 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
729 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
730 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
732 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
733 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
734 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
735 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
736 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
737 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
738 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
739 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
741 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
742 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
744 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
745 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
746 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
747 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
748 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
749 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
750 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
751 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
759 void RTLIL::Module::check()
762 for (auto &it
: wires_
) {
763 log_assert(it
.first
== it
.second
->name
);
764 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
765 log_assert(it
.second
->width
>= 0);
766 log_assert(it
.second
->port_id
>= 0);
767 for (auto &it2
: it
.second
->attributes
) {
768 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
772 for (auto &it
: memories
) {
773 log_assert(it
.first
== it
.second
->name
);
774 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
775 log_assert(it
.second
->width
>= 0);
776 log_assert(it
.second
->size
>= 0);
777 for (auto &it2
: it
.second
->attributes
) {
778 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
782 for (auto &it
: cells_
) {
783 log_assert(it
.first
== it
.second
->name
);
784 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
785 log_assert(it
.second
->type
.size() > 0 && (it
.second
->type
[0] == '\\' || it
.second
->type
[0] == '$'));
786 for (auto &it2
: it
.second
->connections()) {
787 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
790 for (auto &it2
: it
.second
->attributes
) {
791 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
793 for (auto &it2
: it
.second
->parameters
) {
794 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
796 InternalCellChecker
checker(this, it
.second
);
800 for (auto &it
: processes
) {
801 log_assert(it
.first
== it
.second
->name
);
802 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
803 // FIXME: More checks here..
806 for (auto &it
: connections_
) {
807 log_assert(it
.first
.size() == it
.second
.size());
812 for (auto &it
: attributes
) {
813 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
818 void RTLIL::Module::optimize()
822 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
824 log_assert(new_mod
->refcount_wires_
== 0);
825 log_assert(new_mod
->refcount_cells_
== 0);
827 new_mod
->connections_
= connections_
;
828 new_mod
->attributes
= attributes
;
830 for (auto &it
: wires_
)
831 new_mod
->addWire(it
.first
, it
.second
);
833 for (auto &it
: memories
)
834 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
836 for (auto &it
: cells_
)
837 new_mod
->addCell(it
.first
, it
.second
);
839 for (auto &it
: processes
)
840 new_mod
->processes
[it
.first
] = it
.second
->clone();
842 struct RewriteSigSpecWorker
845 void operator()(RTLIL::SigSpec
&sig
)
847 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
848 for (auto &c
: chunks
)
850 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
855 RewriteSigSpecWorker rewriteSigSpecWorker
;
856 rewriteSigSpecWorker
.mod
= new_mod
;
857 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
860 RTLIL::Module
*RTLIL::Module::clone() const
862 RTLIL::Module
*new_mod
= new RTLIL::Module
;
863 new_mod
->name
= name
;
868 void RTLIL::Module::add(RTLIL::Wire
*wire
)
870 log_assert(!wire
->name
.empty());
871 log_assert(count_id(wire
->name
) == 0);
872 log_assert(refcount_wires_
== 0);
873 wires_
[wire
->name
] = wire
;
876 void RTLIL::Module::add(RTLIL::Cell
*cell
)
878 log_assert(!cell
->name
.empty());
879 log_assert(count_id(cell
->name
) == 0);
880 log_assert(refcount_cells_
== 0);
881 cells_
[cell
->name
] = cell
;
885 struct DeleteWireWorker
887 RTLIL::Module
*module
;
888 const std::set
<RTLIL::Wire
*> *wires_p
;
890 void operator()(RTLIL::SigSpec
&sig
) {
891 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
892 for (auto &c
: chunks
)
893 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
894 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
903 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
905 std::set
<RTLIL::Wire
*> wires_
;
911 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
913 log_assert(refcount_wires_
== 0);
915 DeleteWireWorker delete_wire_worker
;
916 delete_wire_worker
.module
= this;
917 delete_wire_worker
.wires_p
= &wires
;
918 rewrite_sigspecs(delete_wire_worker
);
920 for (auto &it
: wires
) {
921 log_assert(wires_
.count(it
->name
) != 0);
922 wires_
.erase(it
->name
);
927 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
929 log_assert(cells_
.count(cell
->name
) != 0);
930 log_assert(refcount_cells_
== 0);
931 cells_
.erase(cell
->name
);
935 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
937 log_assert(wires_
[wire
->name
] == wire
);
938 log_assert(refcount_wires_
== 0);
939 wires_
.erase(wire
->name
);
940 wire
->name
= new_name
;
944 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
946 log_assert(cells_
[cell
->name
] == cell
);
947 log_assert(refcount_wires_
== 0);
948 cells_
.erase(cell
->name
);
949 cell
->name
= new_name
;
953 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
955 log_assert(count_id(old_name
) != 0);
956 if (wires_
.count(old_name
))
957 rename(wires_
.at(old_name
), new_name
);
958 else if (cells_
.count(old_name
))
959 rename(cells_
.at(old_name
), new_name
);
964 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
966 if (a
->port_id
&& !b
->port_id
)
968 if (!a
->port_id
&& b
->port_id
)
971 if (a
->port_id
== b
->port_id
)
972 return a
->name
< b
->name
;
973 return a
->port_id
< b
->port_id
;
976 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
978 connections_
.push_back(conn
);
981 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
983 connections_
.push_back(RTLIL::SigSig(lhs
, rhs
));
986 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
991 void RTLIL::Module::fixup_ports()
993 std::vector
<RTLIL::Wire
*> all_ports
;
995 for (auto &w
: wires_
)
996 if (w
.second
->port_input
|| w
.second
->port_output
)
997 all_ports
.push_back(w
.second
);
999 w
.second
->port_id
= 0;
1001 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1002 for (size_t i
= 0; i
< all_ports
.size(); i
++)
1003 all_ports
[i
]->port_id
= i
+1;
1006 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1008 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1010 wire
->width
= width
;
1015 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1017 RTLIL::Wire
*wire
= addWire(name
);
1018 wire
->width
= other
->width
;
1019 wire
->start_offset
= other
->start_offset
;
1020 wire
->port_id
= other
->port_id
;
1021 wire
->port_input
= other
->port_input
;
1022 wire
->port_output
= other
->port_output
;
1023 wire
->upto
= other
->upto
;
1024 wire
->attributes
= other
->attributes
;
1028 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1030 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1037 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1039 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1040 cell
->connections_
= other
->connections_
;
1041 cell
->parameters
= other
->parameters
;
1042 cell
->attributes
= other
->attributes
;
1046 #define DEF_METHOD(_func, _y_size, _type) \
1047 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1048 RTLIL::Cell *cell = new RTLIL::Cell; \
1049 cell->name = name; \
1050 cell->type = _type; \
1051 cell->parameters["\\A_SIGNED"] = is_signed; \
1052 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1053 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1054 cell->set("\\A", sig_a); \
1055 cell->set("\\Y", sig_y); \
1059 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1060 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1061 add ## _func(name, sig_a, sig_y, is_signed); \
1064 DEF_METHOD(Not
, sig_a
.size(), "$not")
1065 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1066 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1067 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1068 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1069 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1070 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1071 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1072 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1073 DEF_METHOD(LogicNot
, 1, "$logic_not")
1076 #define DEF_METHOD(_func, _y_size, _type) \
1077 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1078 RTLIL::Cell *cell = new RTLIL::Cell; \
1079 cell->name = name; \
1080 cell->type = _type; \
1081 cell->parameters["\\A_SIGNED"] = is_signed; \
1082 cell->parameters["\\B_SIGNED"] = is_signed; \
1083 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1084 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1085 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1086 cell->set("\\A", sig_a); \
1087 cell->set("\\B", sig_b); \
1088 cell->set("\\Y", sig_y); \
1092 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1093 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1094 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1097 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1098 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1099 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1100 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1101 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1102 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1103 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1104 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1105 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1106 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1107 DEF_METHOD(Lt
, 1, "$lt")
1108 DEF_METHOD(Le
, 1, "$le")
1109 DEF_METHOD(Eq
, 1, "$eq")
1110 DEF_METHOD(Ne
, 1, "$ne")
1111 DEF_METHOD(Eqx
, 1, "$eqx")
1112 DEF_METHOD(Nex
, 1, "$nex")
1113 DEF_METHOD(Ge
, 1, "$ge")
1114 DEF_METHOD(Gt
, 1, "$gt")
1115 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1116 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1117 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1118 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1119 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1120 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1121 DEF_METHOD(LogicOr
, 1, "$logic_or")
1124 #define DEF_METHOD(_func, _type, _pmux) \
1125 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) { \
1126 RTLIL::Cell *cell = new RTLIL::Cell; \
1127 cell->name = name; \
1128 cell->type = _type; \
1129 cell->parameters["\\WIDTH"] = sig_a.size(); \
1130 cell->parameters["\\WIDTH"] = sig_b.size(); \
1131 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1132 cell->set("\\A", sig_a); \
1133 cell->set("\\B", sig_b); \
1134 cell->set("\\S", sig_s); \
1135 cell->set("\\Y", sig_y); \
1139 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1140 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1141 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1144 DEF_METHOD(Mux
, "$mux", 0)
1145 DEF_METHOD(Pmux
, "$pmux", 1)
1146 DEF_METHOD(SafePmux
, "$safe_pmux", 1)
1149 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1150 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1151 RTLIL::Cell *cell = new RTLIL::Cell; \
1152 cell->name = name; \
1153 cell->type = _type; \
1154 cell->set("\\" #_P1, sig1); \
1155 cell->set("\\" #_P2, sig2); \
1159 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
1160 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
1161 add ## _func(name, sig1, sig2); \
1164 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1165 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1166 RTLIL::Cell *cell = new RTLIL::Cell; \
1167 cell->name = name; \
1168 cell->type = _type; \
1169 cell->set("\\" #_P1, sig1); \
1170 cell->set("\\" #_P2, sig2); \
1171 cell->set("\\" #_P3, sig3); \
1175 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1176 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1177 add ## _func(name, sig1, sig2, sig3); \
1180 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1181 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1182 RTLIL::Cell *cell = new RTLIL::Cell; \
1183 cell->name = name; \
1184 cell->type = _type; \
1185 cell->set("\\" #_P1, sig1); \
1186 cell->set("\\" #_P2, sig2); \
1187 cell->set("\\" #_P3, sig3); \
1188 cell->set("\\" #_P4, sig4); \
1192 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1193 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1194 add ## _func(name, sig1, sig2, sig3, sig4); \
1197 DEF_METHOD_2(InvGate
, "$_INV_", A
, Y
)
1198 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1199 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1200 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1201 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1206 RTLIL::Cell
* RTLIL::Module::addPow(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool a_signed
, bool b_signed
)
1208 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1210 cell
->type
= "$pow";
1211 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1212 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1213 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1214 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1215 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1216 cell
->set("\\A", sig_a
);
1217 cell
->set("\\B", sig_b
);
1218 cell
->set("\\Y", sig_y
);
1223 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1225 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1227 cell
->type
= "$slice";
1228 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1229 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1230 cell
->parameters
["\\OFFSET"] = offset
;
1231 cell
->set("\\A", sig_a
);
1232 cell
->set("\\Y", sig_y
);
1237 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1239 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1241 cell
->type
= "$concat";
1242 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1243 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1244 cell
->set("\\A", sig_a
);
1245 cell
->set("\\B", sig_b
);
1246 cell
->set("\\Y", sig_y
);
1251 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1253 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1255 cell
->type
= "$lut";
1256 cell
->parameters
["\\LUT"] = lut
;
1257 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1258 cell
->set("\\I", sig_i
);
1259 cell
->set("\\O", sig_o
);
1264 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1266 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1268 cell
->type
= "$assert";
1269 cell
->set("\\A", sig_a
);
1270 cell
->set("\\EN", sig_en
);
1275 RTLIL::Cell
* RTLIL::Module::addSr(RTLIL::IdString name
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
, RTLIL::SigSpec sig_q
, bool set_polarity
, bool clr_polarity
)
1277 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1280 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1281 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1282 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1283 cell
->set("\\SET", sig_set
);
1284 cell
->set("\\CLR", sig_clr
);
1285 cell
->set("\\Q", sig_q
);
1290 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1292 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1294 cell
->type
= "$dff";
1295 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1296 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1297 cell
->set("\\CLK", sig_clk
);
1298 cell
->set("\\D", sig_d
);
1299 cell
->set("\\Q", sig_q
);
1304 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1305 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1307 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1309 cell
->type
= "$dffsr";
1310 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1311 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1312 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1313 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1314 cell
->set("\\CLK", sig_clk
);
1315 cell
->set("\\SET", sig_set
);
1316 cell
->set("\\CLR", sig_clr
);
1317 cell
->set("\\D", sig_d
);
1318 cell
->set("\\Q", sig_q
);
1323 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1324 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1326 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1328 cell
->type
= "$adff";
1329 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1330 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1331 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1332 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1333 cell
->set("\\CLK", sig_clk
);
1334 cell
->set("\\ARST", sig_arst
);
1335 cell
->set("\\D", sig_d
);
1336 cell
->set("\\Q", sig_q
);
1341 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1343 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1345 cell
->type
= "$dlatch";
1346 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1347 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1348 cell
->set("\\EN", sig_en
);
1349 cell
->set("\\D", sig_d
);
1350 cell
->set("\\Q", sig_q
);
1355 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1356 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1358 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1360 cell
->type
= "$dlatchsr";
1361 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1362 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1363 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1364 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1365 cell
->set("\\EN", sig_en
);
1366 cell
->set("\\SET", sig_set
);
1367 cell
->set("\\CLR", sig_clr
);
1368 cell
->set("\\D", sig_d
);
1369 cell
->set("\\Q", sig_q
);
1374 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1376 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1378 cell
->type
= stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N');
1379 cell
->set("\\C", sig_clk
);
1380 cell
->set("\\D", sig_d
);
1381 cell
->set("\\Q", sig_q
);
1386 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1387 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1389 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1391 cell
->type
= stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1392 cell
->set("\\C", sig_clk
);
1393 cell
->set("\\S", sig_set
);
1394 cell
->set("\\R", sig_clr
);
1395 cell
->set("\\D", sig_d
);
1396 cell
->set("\\Q", sig_q
);
1401 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1402 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1404 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1406 cell
->type
= stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0');
1407 cell
->set("\\C", sig_clk
);
1408 cell
->set("\\R", sig_arst
);
1409 cell
->set("\\D", sig_d
);
1410 cell
->set("\\Q", sig_q
);
1415 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1417 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1419 cell
->type
= stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N');
1420 cell
->set("\\E", sig_en
);
1421 cell
->set("\\D", sig_d
);
1422 cell
->set("\\Q", sig_q
);
1427 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1428 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1430 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1432 cell
->type
= stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1433 cell
->set("\\E", sig_en
);
1434 cell
->set("\\S", sig_set
);
1435 cell
->set("\\R", sig_clr
);
1436 cell
->set("\\D", sig_d
);
1437 cell
->set("\\Q", sig_q
);
1449 port_output
= false;
1453 RTLIL::Memory::Memory()
1459 bool RTLIL::Cell::has(RTLIL::IdString portname
)
1461 return connections_
.count(portname
) != 0;
1464 void RTLIL::Cell::unset(RTLIL::IdString portname
)
1466 connections_
.erase(portname
);
1469 void RTLIL::Cell::set(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1471 connections_
[portname
] = signal
;
1474 const RTLIL::SigSpec
&RTLIL::Cell::get(RTLIL::IdString portname
) const
1476 return connections_
.at(portname
);
1479 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1481 return connections_
;
1484 void RTLIL::Cell::check()
1487 InternalCellChecker
checker(NULL
, this);
1492 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1494 if (type
[0] != '$' || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1495 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1498 if (type
== "$mux" || type
== "$pmux" || type
== "$safe_pmux")
1500 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1501 if (type
== "$pmux" || type
== "$safe_pmux")
1502 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1507 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1509 if (connections_
.count("\\A")) {
1510 if (signedness_ab
) {
1512 parameters
["\\A_SIGNED"] = true;
1513 else if (parameters
.count("\\A_SIGNED") == 0)
1514 parameters
["\\A_SIGNED"] = false;
1516 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1519 if (connections_
.count("\\B")) {
1520 if (signedness_ab
) {
1522 parameters
["\\B_SIGNED"] = true;
1523 else if (parameters
.count("\\B_SIGNED") == 0)
1524 parameters
["\\B_SIGNED"] = false;
1526 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1529 if (connections_
.count("\\Y"))
1530 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1535 RTLIL::SigChunk::SigChunk()
1542 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1546 width
= data
.bits
.size();
1550 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1552 log_assert(wire
!= nullptr);
1554 this->width
= wire
->width
;
1558 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1560 log_assert(wire
!= nullptr);
1562 this->width
= width
;
1563 this->offset
= offset
;
1566 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1569 data
= RTLIL::Const(str
);
1570 width
= data
.bits
.size();
1574 RTLIL::SigChunk::SigChunk(int val
, int width
)
1577 data
= RTLIL::Const(val
, width
);
1578 this->width
= data
.bits
.size();
1582 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1585 data
= RTLIL::Const(bit
, width
);
1586 this->width
= data
.bits
.size();
1590 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1594 data
= RTLIL::Const(bit
.data
);
1595 offset
= bit
.offset
;
1599 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1601 RTLIL::SigChunk ret
;
1604 ret
.offset
= this->offset
+ offset
;
1607 for (int i
= 0; i
< length
; i
++)
1608 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1614 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1616 if (wire
&& other
.wire
)
1617 if (wire
->name
!= other
.wire
->name
)
1618 return wire
->name
< other
.wire
->name
;
1620 if (wire
!= other
.wire
)
1621 return wire
< other
.wire
;
1623 if (offset
!= other
.offset
)
1624 return offset
< other
.offset
;
1626 if (width
!= other
.width
)
1627 return width
< other
.width
;
1629 return data
.bits
< other
.data
.bits
;
1632 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1634 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1636 if (data
.bits
!= other
.data
.bits
)
1641 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1648 RTLIL::SigSpec::SigSpec()
1654 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1659 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1661 cover("kernel.rtlil.sigspec.init.list");
1666 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1667 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1671 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1673 cover("kernel.rtlil.sigspec.assign");
1675 width_
= other
.width_
;
1676 hash_
= other
.hash_
;
1677 chunks_
= other
.chunks_
;
1680 if (!other
.bits_
.empty())
1682 RTLIL::SigChunk
*last
= NULL
;
1683 int last_end_offset
= 0;
1685 for (auto &bit
: other
.bits_
) {
1686 if (last
&& bit
.wire
== last
->wire
) {
1687 if (bit
.wire
== NULL
) {
1688 last
->data
.bits
.push_back(bit
.data
);
1691 } else if (last_end_offset
== bit
.offset
) {
1697 chunks_
.push_back(bit
);
1698 last
= &chunks_
.back();
1699 last_end_offset
= bit
.offset
+ 1;
1708 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1710 cover("kernel.rtlil.sigspec.init.const");
1712 chunks_
.push_back(RTLIL::SigChunk(value
));
1713 width_
= chunks_
.back().width
;
1718 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1720 cover("kernel.rtlil.sigspec.init.chunk");
1722 chunks_
.push_back(chunk
);
1723 width_
= chunks_
.back().width
;
1728 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1730 cover("kernel.rtlil.sigspec.init.wire");
1732 chunks_
.push_back(RTLIL::SigChunk(wire
));
1733 width_
= chunks_
.back().width
;
1738 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1740 cover("kernel.rtlil.sigspec.init.wire_part");
1742 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1743 width_
= chunks_
.back().width
;
1748 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1750 cover("kernel.rtlil.sigspec.init.str");
1752 chunks_
.push_back(RTLIL::SigChunk(str
));
1753 width_
= chunks_
.back().width
;
1758 RTLIL::SigSpec::SigSpec(int val
, int width
)
1760 cover("kernel.rtlil.sigspec.init.int");
1762 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1768 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1770 cover("kernel.rtlil.sigspec.init.state");
1772 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1778 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1780 cover("kernel.rtlil.sigspec.init.bit");
1782 if (bit
.wire
== NULL
)
1783 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1785 for (int i
= 0; i
< width
; i
++)
1786 chunks_
.push_back(bit
);
1792 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1794 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1798 for (auto &c
: chunks
)
1803 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1805 cover("kernel.rtlil.sigspec.init.stdvec_bits");
1809 for (auto &bit
: bits
)
1814 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1816 cover("kernel.rtlil.sigspec.init.stdset_bits");
1820 for (auto &bit
: bits
)
1825 void RTLIL::SigSpec::pack() const
1827 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1829 if (that
->bits_
.empty())
1832 cover("kernel.rtlil.sigspec.convert.pack");
1833 log_assert(that
->chunks_
.empty());
1835 std::vector
<RTLIL::SigBit
> old_bits
;
1836 old_bits
.swap(that
->bits_
);
1838 RTLIL::SigChunk
*last
= NULL
;
1839 int last_end_offset
= 0;
1841 for (auto &bit
: old_bits
) {
1842 if (last
&& bit
.wire
== last
->wire
) {
1843 if (bit
.wire
== NULL
) {
1844 last
->data
.bits
.push_back(bit
.data
);
1847 } else if (last_end_offset
== bit
.offset
) {
1853 that
->chunks_
.push_back(bit
);
1854 last
= &that
->chunks_
.back();
1855 last_end_offset
= bit
.offset
+ 1;
1861 void RTLIL::SigSpec::unpack() const
1863 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1865 if (that
->chunks_
.empty())
1868 cover("kernel.rtlil.sigspec.convert.unpack");
1869 log_assert(that
->bits_
.empty());
1871 that
->bits_
.reserve(that
->width_
);
1872 for (auto &c
: that
->chunks_
)
1873 for (int i
= 0; i
< c
.width
; i
++)
1874 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
1876 that
->chunks_
.clear();
1880 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
1882 void RTLIL::SigSpec::hash() const
1884 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1886 if (that
->hash_
!= 0)
1889 cover("kernel.rtlil.sigspec.hash");
1893 for (auto &c
: that
->chunks_
)
1894 if (c
.wire
== NULL
) {
1895 for (auto &v
: c
.data
.bits
)
1896 DJB2(that
->hash_
, v
);
1898 for (auto &v
: c
.wire
->name
)
1899 DJB2(that
->hash_
, v
);
1900 DJB2(that
->hash_
, c
.offset
);
1901 DJB2(that
->hash_
, c
.width
);
1904 if (that
->hash_
== 0)
1908 void RTLIL::SigSpec::sort()
1911 cover("kernel.rtlil.sigspec.sort");
1912 std::sort(bits_
.begin(), bits_
.end());
1915 void RTLIL::SigSpec::sort_and_unify()
1917 cover("kernel.rtlil.sigspec.sort_and_unify");
1918 *this = this->to_sigbit_set();
1921 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
1923 replace(pattern
, with
, this);
1926 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
1928 cover("kernel.rtlil.sigspec.replace");
1934 log_assert(other
!= NULL
);
1935 log_assert(width_
== other
->width_
);
1938 log_assert(pattern
.width_
== with
.width_
);
1940 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
1941 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
1942 if (pattern
.bits_
[i
].wire
!= NULL
)
1943 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
1945 for (int i
= 0; i
< SIZE(bits_
); i
++)
1946 if (pattern_map
.count(bits_
[i
]))
1947 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
1952 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
1954 remove2(pattern
, NULL
);
1957 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
1959 RTLIL::SigSpec tmp
= *this;
1960 tmp
.remove2(pattern
, other
);
1963 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
1966 cover("kernel.rtlil.sigspec.remove_other");
1968 cover("kernel.rtlil.sigspec.remove");
1972 if (other
!= NULL
) {
1973 log_assert(width_
== other
->width_
);
1977 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
1978 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
1980 for (int i
= 0; i
< SIZE(bits_
); i
++) {
1981 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
1984 new_other_bits
.push_back(other
->bits_
[i
]);
1985 new_bits
.push_back(bits_
[i
]);
1988 bits_
.swap(new_bits
);
1989 width_
= SIZE(bits_
);
1991 if (other
!= NULL
) {
1992 other
->bits_
.swap(new_other_bits
);
1993 other
->width_
= SIZE(other
->bits_
);
1999 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
2002 cover("kernel.rtlil.sigspec.extract_other");
2004 cover("kernel.rtlil.sigspec.extract");
2012 log_assert(other
== NULL
|| width_
== other
->width_
);
2014 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
2015 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2019 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2020 for (int i
= 0; i
< width_
; i
++)
2021 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2022 ret
.append_bit(bits_other
[i
]);
2024 for (int i
= 0; i
< width_
; i
++)
2025 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2026 ret
.append_bit(bits_match
[i
]);
2033 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2035 cover("kernel.rtlil.sigspec.replace_pos");
2040 log_assert(offset
>= 0);
2041 log_assert(with
.width_
>= 0);
2042 log_assert(offset
+with
.width_
<= width_
);
2044 for (int i
= 0; i
< with
.width_
; i
++)
2045 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2050 void RTLIL::SigSpec::remove_const()
2054 cover("kernel.rtlil.sigspec.remove_const.packed");
2056 std::vector
<RTLIL::SigChunk
> new_chunks
;
2057 new_chunks
.reserve(SIZE(chunks_
));
2060 for (auto &chunk
: chunks_
)
2061 if (chunk
.wire
!= NULL
) {
2062 new_chunks
.push_back(chunk
);
2063 width_
+= chunk
.width
;
2066 chunks_
.swap(new_chunks
);
2070 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2072 std::vector
<RTLIL::SigBit
> new_bits
;
2073 new_bits
.reserve(width_
);
2075 for (auto &bit
: bits_
)
2076 if (bit
.wire
!= NULL
)
2077 new_bits
.push_back(bit
);
2079 bits_
.swap(new_bits
);
2080 width_
= bits_
.size();
2086 void RTLIL::SigSpec::remove(int offset
, int length
)
2088 cover("kernel.rtlil.sigspec.remove_pos");
2092 log_assert(offset
>= 0);
2093 log_assert(length
>= 0);
2094 log_assert(offset
+ length
<= width_
);
2096 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2097 width_
= bits_
.size();
2102 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2105 cover("kernel.rtlil.sigspec.extract_pos");
2106 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2109 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2111 if (signal
.width_
== 0)
2119 cover("kernel.rtlil.sigspec.append");
2121 if (packed() != signal
.packed()) {
2127 for (auto &other_c
: signal
.chunks_
)
2129 auto &my_last_c
= chunks_
.back();
2130 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2131 auto &this_data
= my_last_c
.data
.bits
;
2132 auto &other_data
= other_c
.data
.bits
;
2133 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2134 my_last_c
.width
+= other_c
.width
;
2136 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2137 my_last_c
.width
+= other_c
.width
;
2139 chunks_
.push_back(other_c
);
2142 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2144 width_
+= signal
.width_
;
2148 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2152 cover("kernel.rtlil.sigspec.append_bit.packed");
2154 if (chunks_
.size() == 0)
2155 chunks_
.push_back(bit
);
2157 if (bit
.wire
== NULL
)
2158 if (chunks_
.back().wire
== NULL
) {
2159 chunks_
.back().data
.bits
.push_back(bit
.data
);
2160 chunks_
.back().width
++;
2162 chunks_
.push_back(bit
);
2164 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2165 chunks_
.back().width
++;
2167 chunks_
.push_back(bit
);
2171 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2172 bits_
.push_back(bit
);
2179 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2181 cover("kernel.rtlil.sigspec.extend");
2186 remove(width
, width_
- width
);
2188 if (width_
< width
) {
2189 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2190 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2191 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2192 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2193 while (width_
< width
)
2198 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2200 cover("kernel.rtlil.sigspec.extend_u0");
2205 remove(width
, width_
- width
);
2207 if (width_
< width
) {
2208 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2210 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2211 while (width_
< width
)
2217 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2219 cover("kernel.rtlil.sigspec.repeat");
2222 for (int i
= 0; i
< num
; i
++)
2228 void RTLIL::SigSpec::check() const
2232 cover("kernel.rtlil.sigspec.check.skip");
2236 cover("kernel.rtlil.sigspec.check.packed");
2239 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2240 const RTLIL::SigChunk chunk
= chunks_
[i
];
2241 if (chunk
.wire
== NULL
) {
2243 log_assert(chunks_
[i
-1].wire
!= NULL
);
2244 log_assert(chunk
.offset
== 0);
2245 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2247 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2248 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2249 log_assert(chunk
.offset
>= 0);
2250 log_assert(chunk
.width
>= 0);
2251 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2252 log_assert(chunk
.data
.bits
.size() == 0);
2256 log_assert(w
== width_
);
2257 log_assert(bits_
.empty());
2261 cover("kernel.rtlil.sigspec.check.unpacked");
2263 log_assert(width_
== SIZE(bits_
));
2264 log_assert(chunks_
.empty());
2269 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2271 cover("kernel.rtlil.sigspec.comp_lt");
2276 if (width_
!= other
.width_
)
2277 return width_
< other
.width_
;
2282 if (chunks_
.size() != other
.chunks_
.size())
2283 return chunks_
.size() < other
.chunks_
.size();
2288 if (hash_
!= other
.hash_
)
2289 return hash_
< other
.hash_
;
2291 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2292 if (chunks_
[i
] != other
.chunks_
[i
]) {
2293 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2294 return chunks_
[i
] < other
.chunks_
[i
];
2297 cover("kernel.rtlil.sigspec.comp_lt.equal");
2301 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2303 cover("kernel.rtlil.sigspec.comp_eq");
2308 if (width_
!= other
.width_
)
2314 if (chunks_
.size() != chunks_
.size())
2320 if (hash_
!= other
.hash_
)
2323 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2324 if (chunks_
[i
] != other
.chunks_
[i
]) {
2325 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2329 cover("kernel.rtlil.sigspec.comp_eq.equal");
2333 bool RTLIL::SigSpec::is_wire() const
2335 cover("kernel.rtlil.sigspec.is_wire");
2338 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2341 bool RTLIL::SigSpec::is_chunk() const
2343 cover("kernel.rtlil.sigspec.is_chunk");
2346 return SIZE(chunks_
) == 1;
2349 bool RTLIL::SigSpec::is_fully_const() const
2351 cover("kernel.rtlil.sigspec.is_fully_const");
2354 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2355 if (it
->width
> 0 && it
->wire
!= NULL
)
2360 bool RTLIL::SigSpec::is_fully_def() const
2362 cover("kernel.rtlil.sigspec.is_fully_def");
2365 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2366 if (it
->width
> 0 && it
->wire
!= NULL
)
2368 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2369 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2375 bool RTLIL::SigSpec::is_fully_undef() const
2377 cover("kernel.rtlil.sigspec.is_fully_undef");
2380 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2381 if (it
->width
> 0 && it
->wire
!= NULL
)
2383 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2384 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2390 bool RTLIL::SigSpec::has_marked_bits() const
2392 cover("kernel.rtlil.sigspec.has_marked_bits");
2395 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2396 if (it
->width
> 0 && it
->wire
== NULL
) {
2397 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2398 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2404 bool RTLIL::SigSpec::as_bool() const
2406 cover("kernel.rtlil.sigspec.as_bool");
2409 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2411 return chunks_
[0].data
.as_bool();
2415 int RTLIL::SigSpec::as_int() const
2417 cover("kernel.rtlil.sigspec.as_int");
2420 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2422 return chunks_
[0].data
.as_int();
2426 std::string
RTLIL::SigSpec::as_string() const
2428 cover("kernel.rtlil.sigspec.as_string");
2432 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2433 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2434 if (chunk
.wire
!= NULL
)
2435 for (int j
= 0; j
< chunk
.width
; j
++)
2438 str
+= chunk
.data
.as_string();
2443 RTLIL::Const
RTLIL::SigSpec::as_const() const
2445 cover("kernel.rtlil.sigspec.as_const");
2448 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2450 return chunks_
[0].data
;
2451 return RTLIL::Const();
2454 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2456 cover("kernel.rtlil.sigspec.as_wire");
2459 log_assert(is_wire());
2460 return chunks_
[0].wire
;
2463 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2465 cover("kernel.rtlil.sigspec.as_chunk");
2468 log_assert(is_chunk());
2472 bool RTLIL::SigSpec::match(std::string pattern
) const
2474 cover("kernel.rtlil.sigspec.match");
2477 std::string str
= as_string();
2478 log_assert(pattern
.size() == str
.size());
2480 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2481 if (pattern
[i
] == ' ')
2483 if (pattern
[i
] == '*') {
2484 if (str
[i
] != 'z' && str
[i
] != 'x')
2488 if (pattern
[i
] != str
[i
])
2495 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2497 cover("kernel.rtlil.sigspec.to_sigbit_set");
2500 std::set
<RTLIL::SigBit
> sigbits
;
2501 for (auto &c
: chunks_
)
2502 for (int i
= 0; i
< c
.width
; i
++)
2503 sigbits
.insert(RTLIL::SigBit(c
, i
));
2507 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2509 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2515 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2517 cover("kernel.rtlil.sigspec.to_single_sigbit");
2520 log_assert(width_
== 1);
2521 for (auto &c
: chunks_
)
2523 return RTLIL::SigBit(c
);
2527 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2529 size_t start
= 0, end
= 0;
2530 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2531 tokens
.push_back(text
.substr(start
, end
- start
));
2534 tokens
.push_back(text
.substr(start
));
2537 static int sigspec_parse_get_dummy_line_num()
2542 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2544 cover("kernel.rtlil.sigspec.parse");
2546 std::vector
<std::string
> tokens
;
2547 sigspec_parse_split(tokens
, str
, ',');
2549 sig
= RTLIL::SigSpec();
2550 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2552 std::string netname
= tokens
[tokidx
];
2553 std::string indices
;
2555 if (netname
.size() == 0)
2558 if ('0' <= netname
[0] && netname
[0] <= '9') {
2559 cover("kernel.rtlil.sigspec.parse.const");
2560 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2561 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2564 sig
.append(RTLIL::Const(ast
->bits
));
2572 cover("kernel.rtlil.sigspec.parse.net");
2574 if (netname
[0] != '$' && netname
[0] != '\\')
2575 netname
= "\\" + netname
;
2577 if (module
->wires_
.count(netname
) == 0) {
2578 size_t indices_pos
= netname
.size()-1;
2579 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2582 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2583 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2585 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2587 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2588 indices
= netname
.substr(indices_pos
);
2589 netname
= netname
.substr(0, indices_pos
);
2594 if (module
->wires_
.count(netname
) == 0)
2597 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2598 if (!indices
.empty()) {
2599 std::vector
<std::string
> index_tokens
;
2600 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2601 if (index_tokens
.size() == 1) {
2602 cover("kernel.rtlil.sigspec.parse.bit_sel");
2603 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2605 cover("kernel.rtlil.sigspec.parse.part_sel");
2606 int a
= atoi(index_tokens
.at(0).c_str());
2607 int b
= atoi(index_tokens
.at(1).c_str());
2612 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2621 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2623 if (str
.empty() || str
[0] != '@')
2624 return parse(sig
, module
, str
);
2626 cover("kernel.rtlil.sigspec.parse.sel");
2628 str
= RTLIL::escape_id(str
.substr(1));
2629 if (design
->selection_vars
.count(str
) == 0)
2632 sig
= RTLIL::SigSpec();
2633 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2634 for (auto &it
: module
->wires_
)
2635 if (sel
.selected_member(module
->name
, it
.first
))
2636 sig
.append(it
.second
);
2641 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2644 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2645 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2650 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2651 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2655 if (lhs
.chunks_
.size() == 1) {
2656 char *p
= (char*)str
.c_str(), *endptr
;
2657 long long int val
= strtoll(p
, &endptr
, 10);
2658 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2659 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2660 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2665 return parse(sig
, module
, str
);
2668 RTLIL::CaseRule::~CaseRule()
2670 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2674 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2676 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2677 new_caserule
->compare
= compare
;
2678 new_caserule
->actions
= actions
;
2679 for (auto &it
: switches
)
2680 new_caserule
->switches
.push_back(it
->clone());
2681 return new_caserule
;
2684 RTLIL::SwitchRule::~SwitchRule()
2686 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2690 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2692 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2693 new_switchrule
->signal
= signal
;
2694 new_switchrule
->attributes
= attributes
;
2695 for (auto &it
: cases
)
2696 new_switchrule
->cases
.push_back(it
->clone());
2697 return new_switchrule
;
2701 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2703 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2704 new_syncrule
->type
= type
;
2705 new_syncrule
->signal
= signal
;
2706 new_syncrule
->actions
= actions
;
2707 return new_syncrule
;
2710 RTLIL::Process::~Process()
2712 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2716 RTLIL::Process
*RTLIL::Process::clone() const
2718 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2720 new_proc
->name
= name
;
2721 new_proc
->attributes
= attributes
;
2723 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2724 new_proc
->root_case
= *rc_ptr
;
2725 rc_ptr
->switches
.clear();
2728 for (auto &it
: syncs
)
2729 new_proc
->syncs
.push_back(it
->clone());