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"
30 int RTLIL::autoidx
= 1;
34 flags
= RTLIL::CONST_FLAG_NONE
;
37 RTLIL::Const::Const(std::string str
)
39 flags
= RTLIL::CONST_FLAG_STRING
;
40 for (int i
= str
.size()-1; i
>= 0; i
--) {
41 unsigned char ch
= str
[i
];
42 for (int j
= 0; j
< 8; j
++) {
43 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
49 RTLIL::Const::Const(int val
, int width
)
51 flags
= RTLIL::CONST_FLAG_NONE
;
52 for (int i
= 0; i
< width
; i
++) {
53 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
58 RTLIL::Const::Const(RTLIL::State bit
, int width
)
60 flags
= RTLIL::CONST_FLAG_NONE
;
61 for (int i
= 0; i
< width
; i
++)
65 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
67 if (bits
.size() != other
.bits
.size())
68 return bits
.size() < other
.bits
.size();
69 for (size_t i
= 0; i
< bits
.size(); i
++)
70 if (bits
[i
] != other
.bits
[i
])
71 return bits
[i
] < other
.bits
[i
];
75 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
77 return bits
== other
.bits
;
80 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
82 return bits
!= other
.bits
;
85 bool RTLIL::Const::as_bool() const
87 for (size_t i
= 0; i
< bits
.size(); i
++)
88 if (bits
[i
] == RTLIL::S1
)
93 int RTLIL::Const::as_int() const
96 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
97 if (bits
[i
] == RTLIL::S1
)
102 std::string
RTLIL::Const::as_string() const
105 for (size_t i
= bits
.size(); i
> 0; i
--)
107 case S0
: ret
+= "0"; break;
108 case S1
: ret
+= "1"; break;
109 case Sx
: ret
+= "x"; break;
110 case Sz
: ret
+= "z"; break;
111 case Sa
: ret
+= "-"; break;
112 case Sm
: ret
+= "m"; break;
117 std::string
RTLIL::Const::decode_string() const
120 std::vector
<char> string_chars
;
121 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
123 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
124 if (bits
[i
+ j
] == RTLIL::State::S1
)
127 string_chars
.push_back(ch
);
129 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
130 string
+= string_chars
[i
];
134 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
138 if (selected_modules
.count(mod_name
) > 0)
140 if (selected_members
.count(mod_name
) > 0)
145 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
149 if (selected_modules
.count(mod_name
) > 0)
154 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
158 if (selected_modules
.count(mod_name
) > 0)
160 if (selected_members
.count(mod_name
) > 0)
161 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
166 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
168 if (full_selection
) {
169 selected_modules
.clear();
170 selected_members
.clear();
174 std::vector
<RTLIL::IdString
> del_list
, add_list
;
177 for (auto mod_name
: selected_modules
) {
178 if (design
->modules_
.count(mod_name
) == 0)
179 del_list
.push_back(mod_name
);
180 selected_members
.erase(mod_name
);
182 for (auto mod_name
: del_list
)
183 selected_modules
.erase(mod_name
);
186 for (auto &it
: selected_members
)
187 if (design
->modules_
.count(it
.first
) == 0)
188 del_list
.push_back(it
.first
);
189 for (auto mod_name
: del_list
)
190 selected_members
.erase(mod_name
);
192 for (auto &it
: selected_members
) {
194 for (auto memb_name
: it
.second
)
195 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
196 del_list
.push_back(memb_name
);
197 for (auto memb_name
: del_list
)
198 it
.second
.erase(memb_name
);
203 for (auto &it
: selected_members
)
204 if (it
.second
.size() == 0)
205 del_list
.push_back(it
.first
);
206 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
207 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
208 add_list
.push_back(it
.first
);
209 for (auto mod_name
: del_list
)
210 selected_members
.erase(mod_name
);
211 for (auto mod_name
: add_list
) {
212 selected_members
.erase(mod_name
);
213 selected_modules
.insert(mod_name
);
216 if (selected_modules
.size() == design
->modules_
.size()) {
217 full_selection
= true;
218 selected_modules
.clear();
219 selected_members
.clear();
223 RTLIL::Design::~Design()
225 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
229 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
231 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
234 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
236 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
239 void RTLIL::Design::add(RTLIL::Module
*module
)
241 assert(modules_
.count(module
->name
) == 0);
242 assert(refcount_modules_
== 0);
243 modules_
[module
->name
] = module
;
246 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
248 assert(modules_
.count(name
) == 0);
249 assert(refcount_modules_
== 0);
250 modules_
[name
] = new RTLIL::Module
;
251 modules_
[name
]->name
= name
;
252 return modules_
[name
];
255 void RTLIL::Design::remove(RTLIL::Module
*module
)
257 assert(modules_
.at(module
->name
) == module
);
258 modules_
.erase(module
->name
);
262 void RTLIL::Design::check()
265 for (auto &it
: modules_
) {
266 assert(it
.first
== it
.second
->name
);
267 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
273 void RTLIL::Design::optimize()
275 for (auto &it
: modules_
)
276 it
.second
->optimize();
277 for (auto &it
: selection_stack
)
279 for (auto &it
: selection_vars
)
280 it
.second
.optimize(this);
283 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
285 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
287 if (selection_stack
.size() == 0)
289 return selection_stack
.back().selected_module(mod_name
);
292 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
294 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
296 if (selection_stack
.size() == 0)
298 return selection_stack
.back().selected_whole_module(mod_name
);
301 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
303 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
305 if (selection_stack
.size() == 0)
307 return selection_stack
.back().selected_member(mod_name
, memb_name
);
310 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
312 return selected_module(mod
->name
);
315 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
317 return selected_whole_module(mod
->name
);
320 RTLIL::Module::Module()
326 RTLIL::Module::~Module()
328 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
330 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
332 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
334 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
338 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
340 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
343 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
345 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
350 struct InternalCellChecker
352 RTLIL::Module
*module
;
354 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
356 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
358 void error(int linenr
)
363 FILE *f
= open_memstream(&ptr
, &size
);
364 ILANG_BACKEND::dump_cell(f
, " ", cell
);
368 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
369 module
? module
->name
.c_str() : "", module
? "." : "",
370 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
373 int param(const char *name
)
375 if (cell
->parameters
.count(name
) == 0)
377 expected_params
.insert(name
);
378 return cell
->parameters
.at(name
).as_int();
381 int param_bool(const char *name
)
384 if (cell
->parameters
.at(name
).bits
.size() > 32)
386 if (v
!= 0 && v
!= 1)
391 void param_bits(const char *name
, int width
)
394 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
398 void port(const char *name
, int width
)
400 if (!cell
->has(name
))
402 if (cell
->get(name
).size() != width
)
404 expected_ports
.insert(name
);
407 void check_expected(bool check_matched_sign
= true)
409 for (auto ¶
: cell
->parameters
)
410 if (expected_params
.count(para
.first
) == 0)
412 for (auto &conn
: cell
->connections())
413 if (expected_ports
.count(conn
.first
) == 0)
416 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
417 bool a_is_signed
= param("\\A_SIGNED") != 0;
418 bool b_is_signed
= param("\\B_SIGNED") != 0;
419 if (a_is_signed
!= b_is_signed
)
424 void check_gate(const char *ports
)
426 if (cell
->parameters
.size() != 0)
429 for (const char *p
= ports
; *p
; p
++) {
430 char portname
[3] = { '\\', *p
, 0 };
431 if (!cell
->has(portname
))
433 if (cell
->get(portname
).size() != 1)
437 for (auto &conn
: cell
->connections()) {
438 if (conn
.first
.size() != 2 || conn
.first
.at(0) != '\\')
440 if (strchr(ports
, conn
.first
.at(1)) == NULL
)
447 if (cell
->type
[0] != '$' || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
448 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
451 if (cell
->type
== "$not" || cell
->type
== "$pos" || cell
->type
== "$bu0" || cell
->type
== "$neg") {
452 param_bool("\\A_SIGNED");
453 port("\\A", param("\\A_WIDTH"));
454 port("\\Y", param("\\Y_WIDTH"));
459 if (cell
->type
== "$and" || cell
->type
== "$or" || cell
->type
== "$xor" || cell
->type
== "$xnor") {
460 param_bool("\\A_SIGNED");
461 param_bool("\\B_SIGNED");
462 port("\\A", param("\\A_WIDTH"));
463 port("\\B", param("\\B_WIDTH"));
464 port("\\Y", param("\\Y_WIDTH"));
469 if (cell
->type
== "$reduce_and" || cell
->type
== "$reduce_or" || cell
->type
== "$reduce_xor" ||
470 cell
->type
== "$reduce_xnor" || cell
->type
== "$reduce_bool") {
471 param_bool("\\A_SIGNED");
472 port("\\A", param("\\A_WIDTH"));
473 port("\\Y", param("\\Y_WIDTH"));
478 if (cell
->type
== "$shl" || cell
->type
== "$shr" || cell
->type
== "$sshl" || cell
->type
== "$sshr") {
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 assert(it
.first
== it
.second
->name
);
764 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
765 assert(it
.second
->width
>= 0);
766 assert(it
.second
->port_id
>= 0);
767 for (auto &it2
: it
.second
->attributes
) {
768 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
772 for (auto &it
: memories
) {
773 assert(it
.first
== it
.second
->name
);
774 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
775 assert(it
.second
->width
>= 0);
776 assert(it
.second
->size
>= 0);
777 for (auto &it2
: it
.second
->attributes
) {
778 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
782 for (auto &it
: cells_
) {
783 assert(it
.first
== it
.second
->name
);
784 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
785 assert(it
.second
->type
.size() > 0 && (it
.second
->type
[0] == '\\' || it
.second
->type
[0] == '$'));
786 for (auto &it2
: it
.second
->connections()) {
787 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
790 for (auto &it2
: it
.second
->attributes
) {
791 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
793 for (auto &it2
: it
.second
->parameters
) {
794 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
796 InternalCellChecker
checker(this, it
.second
);
800 for (auto &it
: processes
) {
801 assert(it
.first
== it
.second
->name
);
802 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
803 // FIXME: More checks here..
806 for (auto &it
: connections_
) {
807 assert(it
.first
.size() == it
.second
.size());
812 for (auto &it
: attributes
) {
813 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 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 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 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
->attributes
= other
->attributes
;
1027 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1029 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1036 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1038 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1039 cell
->connections_
= other
->connections_
;
1040 cell
->parameters
= other
->parameters
;
1041 cell
->attributes
= other
->attributes
;
1045 #define DEF_METHOD(_func, _y_size, _type) \
1046 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1047 RTLIL::Cell *cell = new RTLIL::Cell; \
1048 cell->name = name; \
1049 cell->type = _type; \
1050 cell->parameters["\\A_SIGNED"] = is_signed; \
1051 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1052 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1053 cell->set("\\A", sig_a); \
1054 cell->set("\\Y", sig_y); \
1058 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1059 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1060 add ## _func(name, sig_a, sig_y, is_signed); \
1063 DEF_METHOD(Not
, sig_a
.size(), "$not")
1064 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1065 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1066 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1067 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1068 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1069 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1070 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1071 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1072 DEF_METHOD(LogicNot
, 1, "$logic_not")
1075 #define DEF_METHOD(_func, _y_size, _type) \
1076 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1077 RTLIL::Cell *cell = new RTLIL::Cell; \
1078 cell->name = name; \
1079 cell->type = _type; \
1080 cell->parameters["\\A_SIGNED"] = is_signed; \
1081 cell->parameters["\\B_SIGNED"] = is_signed; \
1082 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1083 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1084 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1085 cell->set("\\A", sig_a); \
1086 cell->set("\\B", sig_b); \
1087 cell->set("\\Y", sig_y); \
1091 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1092 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1093 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1096 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1097 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1098 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1099 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1100 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1101 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1102 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1103 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1104 DEF_METHOD(Lt
, 1, "$lt")
1105 DEF_METHOD(Le
, 1, "$le")
1106 DEF_METHOD(Eq
, 1, "$eq")
1107 DEF_METHOD(Ne
, 1, "$ne")
1108 DEF_METHOD(Eqx
, 1, "$eqx")
1109 DEF_METHOD(Nex
, 1, "$nex")
1110 DEF_METHOD(Ge
, 1, "$ge")
1111 DEF_METHOD(Gt
, 1, "$gt")
1112 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1113 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1114 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1115 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1116 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1117 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1118 DEF_METHOD(LogicOr
, 1, "$logic_or")
1121 #define DEF_METHOD(_func, _type, _pmux) \
1122 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) { \
1123 RTLIL::Cell *cell = new RTLIL::Cell; \
1124 cell->name = name; \
1125 cell->type = _type; \
1126 cell->parameters["\\WIDTH"] = sig_a.size(); \
1127 cell->parameters["\\WIDTH"] = sig_b.size(); \
1128 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1129 cell->set("\\A", sig_a); \
1130 cell->set("\\B", sig_b); \
1131 cell->set("\\S", sig_s); \
1132 cell->set("\\Y", sig_y); \
1136 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1137 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1138 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1141 DEF_METHOD(Mux
, "$mux", 0)
1142 DEF_METHOD(Pmux
, "$pmux", 1)
1143 DEF_METHOD(SafePmux
, "$safe_pmux", 1)
1146 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1147 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1148 RTLIL::Cell *cell = new RTLIL::Cell; \
1149 cell->name = name; \
1150 cell->type = _type; \
1151 cell->set("\\" #_P1, sig1); \
1152 cell->set("\\" #_P2, sig2); \
1156 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
1157 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
1158 add ## _func(name, sig1, sig2); \
1161 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1162 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1163 RTLIL::Cell *cell = new RTLIL::Cell; \
1164 cell->name = name; \
1165 cell->type = _type; \
1166 cell->set("\\" #_P1, sig1); \
1167 cell->set("\\" #_P2, sig2); \
1168 cell->set("\\" #_P3, sig3); \
1172 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1173 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1174 add ## _func(name, sig1, sig2, sig3); \
1177 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1178 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1179 RTLIL::Cell *cell = new RTLIL::Cell; \
1180 cell->name = name; \
1181 cell->type = _type; \
1182 cell->set("\\" #_P1, sig1); \
1183 cell->set("\\" #_P2, sig2); \
1184 cell->set("\\" #_P3, sig3); \
1185 cell->set("\\" #_P4, sig4); \
1189 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1190 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1191 add ## _func(name, sig1, sig2, sig3, sig4); \
1194 DEF_METHOD_2(InvGate
, "$_INV_", A
, Y
)
1195 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1196 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1197 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1198 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1203 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
)
1205 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1207 cell
->type
= "$pow";
1208 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1209 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1210 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1211 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1212 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1213 cell
->set("\\A", sig_a
);
1214 cell
->set("\\B", sig_b
);
1215 cell
->set("\\Y", sig_y
);
1220 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1222 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1224 cell
->type
= "$slice";
1225 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1226 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1227 cell
->parameters
["\\OFFSET"] = offset
;
1228 cell
->set("\\A", sig_a
);
1229 cell
->set("\\Y", sig_y
);
1234 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1236 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1238 cell
->type
= "$concat";
1239 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1240 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1241 cell
->set("\\A", sig_a
);
1242 cell
->set("\\B", sig_b
);
1243 cell
->set("\\Y", sig_y
);
1248 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1250 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1252 cell
->type
= "$lut";
1253 cell
->parameters
["\\LUT"] = lut
;
1254 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1255 cell
->set("\\I", sig_i
);
1256 cell
->set("\\O", sig_o
);
1261 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1263 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1265 cell
->type
= "$assert";
1266 cell
->set("\\A", sig_a
);
1267 cell
->set("\\EN", sig_en
);
1272 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
)
1274 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1277 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1278 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1279 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1280 cell
->set("\\SET", sig_set
);
1281 cell
->set("\\CLR", sig_clr
);
1282 cell
->set("\\Q", sig_q
);
1287 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1289 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1291 cell
->type
= "$dff";
1292 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1293 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1294 cell
->set("\\CLK", sig_clk
);
1295 cell
->set("\\D", sig_d
);
1296 cell
->set("\\Q", sig_q
);
1301 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1302 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1304 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1306 cell
->type
= "$dffsr";
1307 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1308 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1309 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1310 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1311 cell
->set("\\CLK", sig_clk
);
1312 cell
->set("\\SET", sig_set
);
1313 cell
->set("\\CLR", sig_clr
);
1314 cell
->set("\\D", sig_d
);
1315 cell
->set("\\Q", sig_q
);
1320 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1321 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1323 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1325 cell
->type
= "$adff";
1326 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1327 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1328 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1329 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1330 cell
->set("\\CLK", sig_clk
);
1331 cell
->set("\\ARST", sig_arst
);
1332 cell
->set("\\D", sig_d
);
1333 cell
->set("\\Q", sig_q
);
1338 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1340 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1342 cell
->type
= "$dlatch";
1343 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1344 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1345 cell
->set("\\EN", sig_en
);
1346 cell
->set("\\D", sig_d
);
1347 cell
->set("\\Q", sig_q
);
1352 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1353 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1355 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1357 cell
->type
= "$dlatchsr";
1358 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1359 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1360 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1361 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1362 cell
->set("\\EN", sig_en
);
1363 cell
->set("\\SET", sig_set
);
1364 cell
->set("\\CLR", sig_clr
);
1365 cell
->set("\\D", sig_d
);
1366 cell
->set("\\Q", sig_q
);
1371 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1373 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1375 cell
->type
= stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N');
1376 cell
->set("\\C", sig_clk
);
1377 cell
->set("\\D", sig_d
);
1378 cell
->set("\\Q", sig_q
);
1383 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1384 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1386 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1388 cell
->type
= stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1389 cell
->set("\\C", sig_clk
);
1390 cell
->set("\\S", sig_set
);
1391 cell
->set("\\R", sig_clr
);
1392 cell
->set("\\D", sig_d
);
1393 cell
->set("\\Q", sig_q
);
1398 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1399 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1401 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1403 cell
->type
= stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0');
1404 cell
->set("\\C", sig_clk
);
1405 cell
->set("\\R", sig_arst
);
1406 cell
->set("\\D", sig_d
);
1407 cell
->set("\\Q", sig_q
);
1412 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1414 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1416 cell
->type
= stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N');
1417 cell
->set("\\E", sig_en
);
1418 cell
->set("\\D", sig_d
);
1419 cell
->set("\\Q", sig_q
);
1424 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1425 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1427 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1429 cell
->type
= stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1430 cell
->set("\\E", sig_en
);
1431 cell
->set("\\S", sig_set
);
1432 cell
->set("\\R", sig_clr
);
1433 cell
->set("\\D", sig_d
);
1434 cell
->set("\\Q", sig_q
);
1446 port_output
= false;
1449 RTLIL::Memory::Memory()
1455 bool RTLIL::Cell::has(RTLIL::IdString portname
)
1457 return connections_
.count(portname
) != 0;
1460 void RTLIL::Cell::unset(RTLIL::IdString portname
)
1462 connections_
.erase(portname
);
1465 void RTLIL::Cell::set(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1467 connections_
[portname
] = signal
;
1470 const RTLIL::SigSpec
&RTLIL::Cell::get(RTLIL::IdString portname
) const
1472 return connections_
.at(portname
);
1475 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1477 return connections_
;
1480 void RTLIL::Cell::check()
1483 InternalCellChecker
checker(NULL
, this);
1488 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1490 if (type
[0] != '$' || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1491 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1494 if (type
== "$mux" || type
== "$pmux" || type
== "$safe_pmux")
1496 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1497 if (type
== "$pmux" || type
== "$safe_pmux")
1498 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1503 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1505 if (connections_
.count("\\A")) {
1506 if (signedness_ab
) {
1508 parameters
["\\A_SIGNED"] = true;
1509 else if (parameters
.count("\\A_SIGNED") == 0)
1510 parameters
["\\A_SIGNED"] = false;
1512 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1515 if (connections_
.count("\\B")) {
1516 if (signedness_ab
) {
1518 parameters
["\\B_SIGNED"] = true;
1519 else if (parameters
.count("\\B_SIGNED") == 0)
1520 parameters
["\\B_SIGNED"] = false;
1522 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1525 if (connections_
.count("\\Y"))
1526 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1531 RTLIL::SigChunk::SigChunk()
1538 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1542 width
= data
.bits
.size();
1546 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1548 log_assert(wire
!= nullptr);
1550 this->width
= wire
->width
;
1554 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1556 log_assert(wire
!= nullptr);
1558 this->width
= width
;
1559 this->offset
= offset
;
1562 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1565 data
= RTLIL::Const(str
);
1566 width
= data
.bits
.size();
1570 RTLIL::SigChunk::SigChunk(int val
, int width
)
1573 data
= RTLIL::Const(val
, width
);
1574 this->width
= data
.bits
.size();
1578 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1581 data
= RTLIL::Const(bit
, width
);
1582 this->width
= data
.bits
.size();
1586 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1590 data
= RTLIL::Const(bit
.data
);
1591 offset
= bit
.offset
;
1595 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1597 RTLIL::SigChunk ret
;
1600 ret
.offset
= this->offset
+ offset
;
1603 for (int i
= 0; i
< length
; i
++)
1604 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1610 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1612 if (wire
&& other
.wire
)
1613 if (wire
->name
!= other
.wire
->name
)
1614 return wire
->name
< other
.wire
->name
;
1616 if (wire
!= other
.wire
)
1617 return wire
< other
.wire
;
1619 if (offset
!= other
.offset
)
1620 return offset
< other
.offset
;
1622 if (width
!= other
.width
)
1623 return width
< other
.width
;
1625 return data
.bits
< other
.data
.bits
;
1628 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1630 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1632 if (data
.bits
!= other
.data
.bits
)
1637 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1644 RTLIL::SigSpec::SigSpec()
1650 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1655 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1657 cover("kernel.rtlil.sigspec.init.list");
1662 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1663 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1667 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1669 cover("kernel.rtlil.sigspec.assign");
1671 width_
= other
.width_
;
1672 hash_
= other
.hash_
;
1673 chunks_
= other
.chunks_
;
1676 if (!other
.bits_
.empty())
1678 RTLIL::SigChunk
*last
= NULL
;
1679 int last_end_offset
= 0;
1681 for (auto &bit
: other
.bits_
) {
1682 if (last
&& bit
.wire
== last
->wire
) {
1683 if (bit
.wire
== NULL
) {
1684 last
->data
.bits
.push_back(bit
.data
);
1687 } else if (last_end_offset
== bit
.offset
) {
1693 chunks_
.push_back(bit
);
1694 last
= &chunks_
.back();
1695 last_end_offset
= bit
.offset
+ 1;
1704 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1706 cover("kernel.rtlil.sigspec.init.const");
1708 chunks_
.push_back(RTLIL::SigChunk(value
));
1709 width_
= chunks_
.back().width
;
1714 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1716 cover("kernel.rtlil.sigspec.init.chunk");
1718 chunks_
.push_back(chunk
);
1719 width_
= chunks_
.back().width
;
1724 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1726 cover("kernel.rtlil.sigspec.init.wire");
1728 chunks_
.push_back(RTLIL::SigChunk(wire
));
1729 width_
= chunks_
.back().width
;
1734 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1736 cover("kernel.rtlil.sigspec.init.wire_part");
1738 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1739 width_
= chunks_
.back().width
;
1744 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1746 cover("kernel.rtlil.sigspec.init.str");
1748 chunks_
.push_back(RTLIL::SigChunk(str
));
1749 width_
= chunks_
.back().width
;
1754 RTLIL::SigSpec::SigSpec(int val
, int width
)
1756 cover("kernel.rtlil.sigspec.init.int");
1758 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1764 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1766 cover("kernel.rtlil.sigspec.init.state");
1768 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1774 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1776 cover("kernel.rtlil.sigspec.init.bit");
1778 if (bit
.wire
== NULL
)
1779 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1781 for (int i
= 0; i
< width
; i
++)
1782 chunks_
.push_back(bit
);
1788 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1790 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1794 for (auto &c
: chunks
)
1799 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1801 cover("kernel.rtlil.sigspec.init.stdvec_bits");
1805 for (auto &bit
: bits
)
1810 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1812 cover("kernel.rtlil.sigspec.init.stdset_bits");
1816 for (auto &bit
: bits
)
1821 void RTLIL::SigSpec::pack() const
1823 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1825 if (that
->bits_
.empty())
1828 cover("kernel.rtlil.sigspec.convert.pack");
1829 log_assert(that
->chunks_
.empty());
1831 std::vector
<RTLIL::SigBit
> old_bits
;
1832 old_bits
.swap(that
->bits_
);
1834 RTLIL::SigChunk
*last
= NULL
;
1835 int last_end_offset
= 0;
1837 for (auto &bit
: old_bits
) {
1838 if (last
&& bit
.wire
== last
->wire
) {
1839 if (bit
.wire
== NULL
) {
1840 last
->data
.bits
.push_back(bit
.data
);
1843 } else if (last_end_offset
== bit
.offset
) {
1849 that
->chunks_
.push_back(bit
);
1850 last
= &that
->chunks_
.back();
1851 last_end_offset
= bit
.offset
+ 1;
1857 void RTLIL::SigSpec::unpack() const
1859 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1861 if (that
->chunks_
.empty())
1864 cover("kernel.rtlil.sigspec.convert.unpack");
1865 log_assert(that
->bits_
.empty());
1867 that
->bits_
.reserve(that
->width_
);
1868 for (auto &c
: that
->chunks_
)
1869 for (int i
= 0; i
< c
.width
; i
++)
1870 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
1872 that
->chunks_
.clear();
1876 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
1878 void RTLIL::SigSpec::hash() const
1880 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1882 if (that
->hash_
!= 0)
1885 cover("kernel.rtlil.sigspec.hash");
1889 for (auto &c
: that
->chunks_
)
1890 if (c
.wire
== NULL
) {
1891 for (auto &v
: c
.data
.bits
)
1892 DJB2(that
->hash_
, v
);
1894 for (auto &v
: c
.wire
->name
)
1895 DJB2(that
->hash_
, v
);
1896 DJB2(that
->hash_
, c
.offset
);
1897 DJB2(that
->hash_
, c
.width
);
1900 if (that
->hash_
== 0)
1904 void RTLIL::SigSpec::sort()
1907 cover("kernel.rtlil.sigspec.sort");
1908 std::sort(bits_
.begin(), bits_
.end());
1911 void RTLIL::SigSpec::sort_and_unify()
1913 cover("kernel.rtlil.sigspec.sort_and_unify");
1914 *this = this->to_sigbit_set();
1917 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
1919 replace(pattern
, with
, this);
1922 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
1924 cover("kernel.rtlil.sigspec.replace");
1930 assert(other
!= NULL
);
1931 assert(width_
== other
->width_
);
1934 assert(pattern
.width_
== with
.width_
);
1936 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
1937 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
1938 if (pattern
.bits_
[i
].wire
!= NULL
)
1939 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
1941 for (int i
= 0; i
< SIZE(bits_
); i
++)
1942 if (pattern_map
.count(bits_
[i
]))
1943 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
1948 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
1950 remove2(pattern
, NULL
);
1953 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
1955 RTLIL::SigSpec tmp
= *this;
1956 tmp
.remove2(pattern
, other
);
1959 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
1962 cover("kernel.rtlil.sigspec.remove_other");
1964 cover("kernel.rtlil.sigspec.remove");
1968 if (other
!= NULL
) {
1969 assert(width_
== other
->width_
);
1973 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
1974 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
1976 for (int i
= 0; i
< SIZE(bits_
); i
++) {
1977 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
1980 new_other_bits
.push_back(other
->bits_
[i
]);
1981 new_bits
.push_back(bits_
[i
]);
1984 bits_
.swap(new_bits
);
1985 width_
= SIZE(bits_
);
1987 if (other
!= NULL
) {
1988 other
->bits_
.swap(new_other_bits
);
1989 other
->width_
= SIZE(other
->bits_
);
1995 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
1998 cover("kernel.rtlil.sigspec.extract_other");
2000 cover("kernel.rtlil.sigspec.extract");
2008 assert(other
== NULL
|| width_
== other
->width_
);
2010 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
2011 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2015 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2016 for (int i
= 0; i
< width_
; i
++)
2017 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2018 ret
.append_bit(bits_other
[i
]);
2020 for (int i
= 0; i
< width_
; i
++)
2021 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2022 ret
.append_bit(bits_match
[i
]);
2029 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2031 cover("kernel.rtlil.sigspec.replace_pos");
2036 assert(offset
>= 0);
2037 assert(with
.width_
>= 0);
2038 assert(offset
+with
.width_
<= width_
);
2040 for (int i
= 0; i
< with
.width_
; i
++)
2041 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2046 void RTLIL::SigSpec::remove_const()
2050 cover("kernel.rtlil.sigspec.remove_const.packed");
2052 std::vector
<RTLIL::SigChunk
> new_chunks
;
2053 new_chunks
.reserve(SIZE(chunks_
));
2056 for (auto &chunk
: chunks_
)
2057 if (chunk
.wire
!= NULL
) {
2058 new_chunks
.push_back(chunk
);
2059 width_
+= chunk
.width
;
2062 chunks_
.swap(new_chunks
);
2066 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2068 std::vector
<RTLIL::SigBit
> new_bits
;
2069 new_bits
.reserve(width_
);
2071 for (auto &bit
: bits_
)
2072 if (bit
.wire
!= NULL
)
2073 new_bits
.push_back(bit
);
2075 bits_
.swap(new_bits
);
2076 width_
= bits_
.size();
2082 void RTLIL::SigSpec::remove(int offset
, int length
)
2084 cover("kernel.rtlil.sigspec.remove_pos");
2088 assert(offset
>= 0);
2089 assert(length
>= 0);
2090 assert(offset
+ length
<= width_
);
2092 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2093 width_
= bits_
.size();
2098 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2101 cover("kernel.rtlil.sigspec.extract_pos");
2102 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2105 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2107 if (signal
.width_
== 0)
2115 cover("kernel.rtlil.sigspec.append");
2117 if (packed() != signal
.packed()) {
2123 for (auto &other_c
: signal
.chunks_
)
2125 auto &my_last_c
= chunks_
.back();
2126 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2127 auto &this_data
= my_last_c
.data
.bits
;
2128 auto &other_data
= other_c
.data
.bits
;
2129 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2130 my_last_c
.width
+= other_c
.width
;
2132 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2133 my_last_c
.width
+= other_c
.width
;
2135 chunks_
.push_back(other_c
);
2138 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2140 width_
+= signal
.width_
;
2144 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2148 cover("kernel.rtlil.sigspec.append_bit.packed");
2150 if (chunks_
.size() == 0)
2151 chunks_
.push_back(bit
);
2153 if (bit
.wire
== NULL
)
2154 if (chunks_
.back().wire
== NULL
) {
2155 chunks_
.back().data
.bits
.push_back(bit
.data
);
2156 chunks_
.back().width
++;
2158 chunks_
.push_back(bit
);
2160 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2161 chunks_
.back().width
++;
2163 chunks_
.push_back(bit
);
2167 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2168 bits_
.push_back(bit
);
2175 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2177 cover("kernel.rtlil.sigspec.extend");
2182 remove(width
, width_
- width
);
2184 if (width_
< width
) {
2185 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2186 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2187 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2188 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2189 while (width_
< width
)
2194 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2196 cover("kernel.rtlil.sigspec.extend_u0");
2201 remove(width
, width_
- width
);
2203 if (width_
< width
) {
2204 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2206 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2207 while (width_
< width
)
2213 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2215 cover("kernel.rtlil.sigspec.repeat");
2218 for (int i
= 0; i
< num
; i
++)
2224 void RTLIL::SigSpec::check() const
2228 cover("kernel.rtlil.sigspec.check.skip");
2232 cover("kernel.rtlil.sigspec.check.packed");
2235 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2236 const RTLIL::SigChunk chunk
= chunks_
[i
];
2237 if (chunk
.wire
== NULL
) {
2239 assert(chunks_
[i
-1].wire
!= NULL
);
2240 assert(chunk
.offset
== 0);
2241 assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2243 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2244 assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2245 assert(chunk
.offset
>= 0);
2246 assert(chunk
.width
>= 0);
2247 assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2248 assert(chunk
.data
.bits
.size() == 0);
2252 assert(w
== width_
);
2253 assert(bits_
.empty());
2257 cover("kernel.rtlil.sigspec.check.unpacked");
2259 assert(width_
== SIZE(bits_
));
2260 assert(chunks_
.empty());
2265 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2267 cover("kernel.rtlil.sigspec.comp_lt");
2272 if (width_
!= other
.width_
)
2273 return width_
< other
.width_
;
2278 if (chunks_
.size() != other
.chunks_
.size())
2279 return chunks_
.size() < other
.chunks_
.size();
2284 if (hash_
!= other
.hash_
)
2285 return hash_
< other
.hash_
;
2287 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2288 if (chunks_
[i
] != other
.chunks_
[i
]) {
2289 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2290 return chunks_
[i
] < other
.chunks_
[i
];
2293 cover("kernel.rtlil.sigspec.comp_lt.equal");
2297 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2299 cover("kernel.rtlil.sigspec.comp_eq");
2304 if (width_
!= other
.width_
)
2310 if (chunks_
.size() != chunks_
.size())
2316 if (hash_
!= other
.hash_
)
2319 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2320 if (chunks_
[i
] != other
.chunks_
[i
]) {
2321 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2325 cover("kernel.rtlil.sigspec.comp_eq.equal");
2329 bool RTLIL::SigSpec::is_wire() const
2331 cover("kernel.rtlil.sigspec.is_wire");
2334 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2337 bool RTLIL::SigSpec::is_chunk() const
2339 cover("kernel.rtlil.sigspec.is_chunk");
2342 return SIZE(chunks_
) == 1;
2345 bool RTLIL::SigSpec::is_fully_const() const
2347 cover("kernel.rtlil.sigspec.is_fully_const");
2350 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2351 if (it
->width
> 0 && it
->wire
!= NULL
)
2356 bool RTLIL::SigSpec::is_fully_def() const
2358 cover("kernel.rtlil.sigspec.is_fully_def");
2361 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2362 if (it
->width
> 0 && it
->wire
!= NULL
)
2364 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2365 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2371 bool RTLIL::SigSpec::is_fully_undef() const
2373 cover("kernel.rtlil.sigspec.is_fully_undef");
2376 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2377 if (it
->width
> 0 && it
->wire
!= NULL
)
2379 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2380 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2386 bool RTLIL::SigSpec::has_marked_bits() const
2388 cover("kernel.rtlil.sigspec.has_marked_bits");
2391 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2392 if (it
->width
> 0 && it
->wire
== NULL
) {
2393 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2394 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2400 bool RTLIL::SigSpec::as_bool() const
2402 cover("kernel.rtlil.sigspec.as_bool");
2405 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2407 return chunks_
[0].data
.as_bool();
2411 int RTLIL::SigSpec::as_int() const
2413 cover("kernel.rtlil.sigspec.as_int");
2416 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2418 return chunks_
[0].data
.as_int();
2422 std::string
RTLIL::SigSpec::as_string() const
2424 cover("kernel.rtlil.sigspec.as_string");
2428 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2429 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2430 if (chunk
.wire
!= NULL
)
2431 for (int j
= 0; j
< chunk
.width
; j
++)
2434 str
+= chunk
.data
.as_string();
2439 RTLIL::Const
RTLIL::SigSpec::as_const() const
2441 cover("kernel.rtlil.sigspec.as_const");
2444 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2446 return chunks_
[0].data
;
2447 return RTLIL::Const();
2450 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2452 cover("kernel.rtlil.sigspec.as_wire");
2456 return chunks_
[0].wire
;
2459 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2461 cover("kernel.rtlil.sigspec.as_chunk");
2468 bool RTLIL::SigSpec::match(std::string pattern
) const
2470 cover("kernel.rtlil.sigspec.match");
2473 std::string str
= as_string();
2474 assert(pattern
.size() == str
.size());
2476 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2477 if (pattern
[i
] == ' ')
2479 if (pattern
[i
] == '*') {
2480 if (str
[i
] != 'z' && str
[i
] != 'x')
2484 if (pattern
[i
] != str
[i
])
2491 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2493 cover("kernel.rtlil.sigspec.to_sigbit_set");
2496 std::set
<RTLIL::SigBit
> sigbits
;
2497 for (auto &c
: chunks_
)
2498 for (int i
= 0; i
< c
.width
; i
++)
2499 sigbits
.insert(RTLIL::SigBit(c
, i
));
2503 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2505 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2511 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2513 cover("kernel.rtlil.sigspec.to_single_sigbit");
2516 log_assert(width_
== 1);
2517 for (auto &c
: chunks_
)
2519 return RTLIL::SigBit(c
);
2523 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2525 size_t start
= 0, end
= 0;
2526 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2527 tokens
.push_back(text
.substr(start
, end
- start
));
2530 tokens
.push_back(text
.substr(start
));
2533 static int sigspec_parse_get_dummy_line_num()
2538 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2540 cover("kernel.rtlil.sigspec.parse");
2542 std::vector
<std::string
> tokens
;
2543 sigspec_parse_split(tokens
, str
, ',');
2545 sig
= RTLIL::SigSpec();
2546 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2548 std::string netname
= tokens
[tokidx
];
2549 std::string indices
;
2551 if (netname
.size() == 0)
2554 if ('0' <= netname
[0] && netname
[0] <= '9') {
2555 cover("kernel.rtlil.sigspec.parse.const");
2556 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2557 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2560 sig
.append(RTLIL::Const(ast
->bits
));
2568 cover("kernel.rtlil.sigspec.parse.net");
2570 if (netname
[0] != '$' && netname
[0] != '\\')
2571 netname
= "\\" + netname
;
2573 if (module
->wires_
.count(netname
) == 0) {
2574 size_t indices_pos
= netname
.size()-1;
2575 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2578 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2579 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2581 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2583 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2584 indices
= netname
.substr(indices_pos
);
2585 netname
= netname
.substr(0, indices_pos
);
2590 if (module
->wires_
.count(netname
) == 0)
2593 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2594 if (!indices
.empty()) {
2595 std::vector
<std::string
> index_tokens
;
2596 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2597 if (index_tokens
.size() == 1) {
2598 cover("kernel.rtlil.sigspec.parse.bit_sel");
2599 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2601 cover("kernel.rtlil.sigspec.parse.part_sel");
2602 int a
= atoi(index_tokens
.at(0).c_str());
2603 int b
= atoi(index_tokens
.at(1).c_str());
2608 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2617 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2619 if (str
.empty() || str
[0] != '@')
2620 return parse(sig
, module
, str
);
2622 cover("kernel.rtlil.sigspec.parse.sel");
2624 str
= RTLIL::escape_id(str
.substr(1));
2625 if (design
->selection_vars
.count(str
) == 0)
2628 sig
= RTLIL::SigSpec();
2629 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2630 for (auto &it
: module
->wires_
)
2631 if (sel
.selected_member(module
->name
, it
.first
))
2632 sig
.append(it
.second
);
2637 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2640 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2641 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2646 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2647 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2651 if (lhs
.chunks_
.size() == 1) {
2652 char *p
= (char*)str
.c_str(), *endptr
;
2653 long long int val
= strtoll(p
, &endptr
, 10);
2654 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2655 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2656 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2661 return parse(sig
, module
, str
);
2664 RTLIL::CaseRule::~CaseRule()
2666 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2670 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2672 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2673 new_caserule
->compare
= compare
;
2674 new_caserule
->actions
= actions
;
2675 for (auto &it
: switches
)
2676 new_caserule
->switches
.push_back(it
->clone());
2677 return new_caserule
;
2680 RTLIL::SwitchRule::~SwitchRule()
2682 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2686 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2688 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2689 new_switchrule
->signal
= signal
;
2690 new_switchrule
->attributes
= attributes
;
2691 for (auto &it
: cases
)
2692 new_switchrule
->cases
.push_back(it
->clone());
2693 return new_switchrule
;
2697 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2699 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2700 new_syncrule
->type
= type
;
2701 new_syncrule
->signal
= signal
;
2702 new_syncrule
->actions
= actions
;
2703 return new_syncrule
;
2706 RTLIL::Process::~Process()
2708 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2712 RTLIL::Process
*RTLIL::Process::clone() const
2714 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2716 new_proc
->name
= name
;
2717 new_proc
->attributes
= attributes
;
2719 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2720 new_proc
->root_case
= *rc_ptr
;
2721 rc_ptr
->switches
.clear();
2724 for (auto &it
: syncs
)
2725 new_proc
->syncs
.push_back(it
->clone());