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 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1657 cover("kernel.rtlil.sigspec.assign");
1659 width_
= other
.width_
;
1660 hash_
= other
.hash_
;
1661 chunks_
= other
.chunks_
;
1664 if (!other
.bits_
.empty())
1666 RTLIL::SigChunk
*last
= NULL
;
1667 int last_end_offset
= 0;
1669 for (auto &bit
: other
.bits_
) {
1670 if (last
&& bit
.wire
== last
->wire
) {
1671 if (bit
.wire
== NULL
) {
1672 last
->data
.bits
.push_back(bit
.data
);
1675 } else if (last_end_offset
== bit
.offset
) {
1681 chunks_
.push_back(bit
);
1682 last
= &chunks_
.back();
1683 last_end_offset
= bit
.offset
+ 1;
1692 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1694 cover("kernel.rtlil.sigspec.init.const");
1696 chunks_
.push_back(RTLIL::SigChunk(value
));
1697 width_
= chunks_
.back().width
;
1702 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1704 cover("kernel.rtlil.sigspec.init.chunk");
1706 chunks_
.push_back(chunk
);
1707 width_
= chunks_
.back().width
;
1712 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1714 cover("kernel.rtlil.sigspec.init.wire");
1716 chunks_
.push_back(RTLIL::SigChunk(wire
));
1717 width_
= chunks_
.back().width
;
1722 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1724 cover("kernel.rtlil.sigspec.init.wire_part");
1726 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1727 width_
= chunks_
.back().width
;
1732 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1734 cover("kernel.rtlil.sigspec.init.str");
1736 chunks_
.push_back(RTLIL::SigChunk(str
));
1737 width_
= chunks_
.back().width
;
1742 RTLIL::SigSpec::SigSpec(int val
, int width
)
1744 cover("kernel.rtlil.sigspec.init.int");
1746 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1752 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1754 cover("kernel.rtlil.sigspec.init.state");
1756 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1762 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1764 cover("kernel.rtlil.sigspec.init.bit");
1766 if (bit
.wire
== NULL
)
1767 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1769 for (int i
= 0; i
< width
; i
++)
1770 chunks_
.push_back(bit
);
1776 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1778 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1782 for (auto &c
: chunks
)
1787 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1789 cover("kernel.rtlil.sigspec.init.stdvec_bits");
1793 for (auto &bit
: bits
)
1798 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1800 cover("kernel.rtlil.sigspec.init.stdset_bits");
1804 for (auto &bit
: bits
)
1809 void RTLIL::SigSpec::pack() const
1811 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1813 if (that
->bits_
.empty())
1816 cover("kernel.rtlil.sigspec.convert.pack");
1817 log_assert(that
->chunks_
.empty());
1819 std::vector
<RTLIL::SigBit
> old_bits
;
1820 old_bits
.swap(that
->bits_
);
1822 RTLIL::SigChunk
*last
= NULL
;
1823 int last_end_offset
= 0;
1825 for (auto &bit
: old_bits
) {
1826 if (last
&& bit
.wire
== last
->wire
) {
1827 if (bit
.wire
== NULL
) {
1828 last
->data
.bits
.push_back(bit
.data
);
1831 } else if (last_end_offset
== bit
.offset
) {
1837 that
->chunks_
.push_back(bit
);
1838 last
= &that
->chunks_
.back();
1839 last_end_offset
= bit
.offset
+ 1;
1845 void RTLIL::SigSpec::unpack() const
1847 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1849 if (that
->chunks_
.empty())
1852 cover("kernel.rtlil.sigspec.convert.unpack");
1853 log_assert(that
->bits_
.empty());
1855 that
->bits_
.reserve(that
->width_
);
1856 for (auto &c
: that
->chunks_
)
1857 for (int i
= 0; i
< c
.width
; i
++)
1858 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
1860 that
->chunks_
.clear();
1864 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
1866 void RTLIL::SigSpec::hash() const
1868 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1870 if (that
->hash_
!= 0)
1873 cover("kernel.rtlil.sigspec.hash");
1877 for (auto &c
: that
->chunks_
)
1878 if (c
.wire
== NULL
) {
1879 for (auto &v
: c
.data
.bits
)
1880 DJB2(that
->hash_
, v
);
1882 for (auto &v
: c
.wire
->name
)
1883 DJB2(that
->hash_
, v
);
1884 DJB2(that
->hash_
, c
.offset
);
1885 DJB2(that
->hash_
, c
.width
);
1888 if (that
->hash_
== 0)
1892 void RTLIL::SigSpec::sort()
1895 cover("kernel.rtlil.sigspec.sort");
1896 std::sort(bits_
.begin(), bits_
.end());
1899 void RTLIL::SigSpec::sort_and_unify()
1901 cover("kernel.rtlil.sigspec.sort_and_unify");
1902 *this = this->to_sigbit_set();
1905 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
1907 replace(pattern
, with
, this);
1910 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
1912 cover("kernel.rtlil.sigspec.replace");
1918 assert(other
!= NULL
);
1919 assert(width_
== other
->width_
);
1922 assert(pattern
.width_
== with
.width_
);
1924 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
1925 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
1926 if (pattern
.bits_
[i
].wire
!= NULL
)
1927 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
1929 for (int i
= 0; i
< SIZE(bits_
); i
++)
1930 if (pattern_map
.count(bits_
[i
]))
1931 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
1936 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
1938 remove2(pattern
, NULL
);
1941 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
1943 RTLIL::SigSpec tmp
= *this;
1944 tmp
.remove2(pattern
, other
);
1947 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
1950 cover("kernel.rtlil.sigspec.remove_other");
1952 cover("kernel.rtlil.sigspec.remove");
1956 if (other
!= NULL
) {
1957 assert(width_
== other
->width_
);
1961 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
1962 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
1964 for (int i
= 0; i
< SIZE(bits_
); i
++) {
1965 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
1968 new_other_bits
.push_back(other
->bits_
[i
]);
1969 new_bits
.push_back(bits_
[i
]);
1972 bits_
.swap(new_bits
);
1973 width_
= SIZE(bits_
);
1975 if (other
!= NULL
) {
1976 other
->bits_
.swap(new_other_bits
);
1977 other
->width_
= SIZE(other
->bits_
);
1983 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
1986 cover("kernel.rtlil.sigspec.extract_other");
1988 cover("kernel.rtlil.sigspec.extract");
1996 assert(other
== NULL
|| width_
== other
->width_
);
1998 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
1999 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2003 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2004 for (int i
= 0; i
< width_
; i
++)
2005 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2006 ret
.append_bit(bits_other
[i
]);
2008 for (int i
= 0; i
< width_
; i
++)
2009 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2010 ret
.append_bit(bits_match
[i
]);
2017 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2019 cover("kernel.rtlil.sigspec.replace_pos");
2024 assert(offset
>= 0);
2025 assert(with
.width_
>= 0);
2026 assert(offset
+with
.width_
<= width_
);
2028 for (int i
= 0; i
< with
.width_
; i
++)
2029 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2034 void RTLIL::SigSpec::remove_const()
2038 cover("kernel.rtlil.sigspec.remove_const.packed");
2040 std::vector
<RTLIL::SigChunk
> new_chunks
;
2041 new_chunks
.reserve(SIZE(chunks_
));
2044 for (auto &chunk
: chunks_
)
2045 if (chunk
.wire
!= NULL
) {
2046 new_chunks
.push_back(chunk
);
2047 width_
+= chunk
.width
;
2050 chunks_
.swap(new_chunks
);
2054 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2056 std::vector
<RTLIL::SigBit
> new_bits
;
2057 new_bits
.reserve(width_
);
2059 for (auto &bit
: bits_
)
2060 if (bit
.wire
!= NULL
)
2061 new_bits
.push_back(bit
);
2063 bits_
.swap(new_bits
);
2064 width_
= bits_
.size();
2070 void RTLIL::SigSpec::remove(int offset
, int length
)
2072 cover("kernel.rtlil.sigspec.remove_pos");
2076 assert(offset
>= 0);
2077 assert(length
>= 0);
2078 assert(offset
+ length
<= width_
);
2080 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2081 width_
= bits_
.size();
2086 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2089 cover("kernel.rtlil.sigspec.extract_pos");
2090 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2093 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2095 if (signal
.width_
== 0)
2103 cover("kernel.rtlil.sigspec.append");
2105 if (packed() != signal
.packed()) {
2111 for (auto &other_c
: signal
.chunks_
)
2113 auto &my_last_c
= chunks_
.back();
2114 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2115 auto &this_data
= my_last_c
.data
.bits
;
2116 auto &other_data
= other_c
.data
.bits
;
2117 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2118 my_last_c
.width
+= other_c
.width
;
2120 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2121 my_last_c
.width
+= other_c
.width
;
2123 chunks_
.push_back(other_c
);
2126 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2128 width_
+= signal
.width_
;
2132 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2136 cover("kernel.rtlil.sigspec.append_bit.packed");
2138 if (chunks_
.size() == 0)
2139 chunks_
.push_back(bit
);
2141 if (bit
.wire
== NULL
)
2142 if (chunks_
.back().wire
== NULL
) {
2143 chunks_
.back().data
.bits
.push_back(bit
.data
);
2144 chunks_
.back().width
++;
2146 chunks_
.push_back(bit
);
2148 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2149 chunks_
.back().width
++;
2151 chunks_
.push_back(bit
);
2155 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2156 bits_
.push_back(bit
);
2163 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2165 cover("kernel.rtlil.sigspec.extend");
2170 remove(width
, width_
- width
);
2172 if (width_
< width
) {
2173 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2174 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2175 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2176 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2177 while (width_
< width
)
2182 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2184 cover("kernel.rtlil.sigspec.extend_u0");
2189 remove(width
, width_
- width
);
2191 if (width_
< width
) {
2192 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2194 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2195 while (width_
< width
)
2201 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2203 cover("kernel.rtlil.sigspec.repeat");
2206 for (int i
= 0; i
< num
; i
++)
2212 void RTLIL::SigSpec::check() const
2216 cover("kernel.rtlil.sigspec.check.skip");
2220 cover("kernel.rtlil.sigspec.check.packed");
2223 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2224 const RTLIL::SigChunk chunk
= chunks_
[i
];
2225 if (chunk
.wire
== NULL
) {
2227 assert(chunks_
[i
-1].wire
!= NULL
);
2228 assert(chunk
.offset
== 0);
2229 assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2231 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2232 assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2233 assert(chunk
.offset
>= 0);
2234 assert(chunk
.width
>= 0);
2235 assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2236 assert(chunk
.data
.bits
.size() == 0);
2240 assert(w
== width_
);
2241 assert(bits_
.empty());
2245 cover("kernel.rtlil.sigspec.check.unpacked");
2247 assert(width_
== SIZE(bits_
));
2248 assert(chunks_
.empty());
2253 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2255 cover("kernel.rtlil.sigspec.comp_lt");
2260 if (width_
!= other
.width_
)
2261 return width_
< other
.width_
;
2266 if (chunks_
.size() != other
.chunks_
.size())
2267 return chunks_
.size() < other
.chunks_
.size();
2272 if (hash_
!= other
.hash_
)
2273 return hash_
< other
.hash_
;
2275 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2276 if (chunks_
[i
] != other
.chunks_
[i
]) {
2277 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2278 return chunks_
[i
] < other
.chunks_
[i
];
2281 cover("kernel.rtlil.sigspec.comp_lt.equal");
2285 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2287 cover("kernel.rtlil.sigspec.comp_eq");
2292 if (width_
!= other
.width_
)
2298 if (chunks_
.size() != chunks_
.size())
2304 if (hash_
!= other
.hash_
)
2307 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2308 if (chunks_
[i
] != other
.chunks_
[i
]) {
2309 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2313 cover("kernel.rtlil.sigspec.comp_eq.equal");
2317 bool RTLIL::SigSpec::is_wire() const
2319 cover("kernel.rtlil.sigspec.is_wire");
2322 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2325 bool RTLIL::SigSpec::is_chunk() const
2327 cover("kernel.rtlil.sigspec.is_chunk");
2330 return SIZE(chunks_
) == 1;
2333 bool RTLIL::SigSpec::is_fully_const() const
2335 cover("kernel.rtlil.sigspec.is_fully_const");
2338 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2339 if (it
->width
> 0 && it
->wire
!= NULL
)
2344 bool RTLIL::SigSpec::is_fully_def() const
2346 cover("kernel.rtlil.sigspec.is_fully_def");
2349 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2350 if (it
->width
> 0 && it
->wire
!= NULL
)
2352 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2353 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2359 bool RTLIL::SigSpec::is_fully_undef() const
2361 cover("kernel.rtlil.sigspec.is_fully_undef");
2364 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2365 if (it
->width
> 0 && it
->wire
!= NULL
)
2367 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2368 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2374 bool RTLIL::SigSpec::has_marked_bits() const
2376 cover("kernel.rtlil.sigspec.has_marked_bits");
2379 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2380 if (it
->width
> 0 && it
->wire
== NULL
) {
2381 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2382 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2388 bool RTLIL::SigSpec::as_bool() const
2390 cover("kernel.rtlil.sigspec.as_bool");
2393 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2395 return chunks_
[0].data
.as_bool();
2399 int RTLIL::SigSpec::as_int() const
2401 cover("kernel.rtlil.sigspec.as_int");
2404 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2406 return chunks_
[0].data
.as_int();
2410 std::string
RTLIL::SigSpec::as_string() const
2412 cover("kernel.rtlil.sigspec.as_string");
2416 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2417 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2418 if (chunk
.wire
!= NULL
)
2419 for (int j
= 0; j
< chunk
.width
; j
++)
2422 str
+= chunk
.data
.as_string();
2427 RTLIL::Const
RTLIL::SigSpec::as_const() const
2429 cover("kernel.rtlil.sigspec.as_const");
2432 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2434 return chunks_
[0].data
;
2435 return RTLIL::Const();
2438 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2440 cover("kernel.rtlil.sigspec.as_wire");
2444 return chunks_
[0].wire
;
2447 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2449 cover("kernel.rtlil.sigspec.as_chunk");
2456 bool RTLIL::SigSpec::match(std::string pattern
) const
2458 cover("kernel.rtlil.sigspec.match");
2461 std::string str
= as_string();
2462 assert(pattern
.size() == str
.size());
2464 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2465 if (pattern
[i
] == ' ')
2467 if (pattern
[i
] == '*') {
2468 if (str
[i
] != 'z' && str
[i
] != 'x')
2472 if (pattern
[i
] != str
[i
])
2479 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2481 cover("kernel.rtlil.sigspec.to_sigbit_set");
2484 std::set
<RTLIL::SigBit
> sigbits
;
2485 for (auto &c
: chunks_
)
2486 for (int i
= 0; i
< c
.width
; i
++)
2487 sigbits
.insert(RTLIL::SigBit(c
, i
));
2491 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2493 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2499 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2501 cover("kernel.rtlil.sigspec.to_single_sigbit");
2504 log_assert(width_
== 1);
2505 for (auto &c
: chunks_
)
2507 return RTLIL::SigBit(c
);
2511 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2513 size_t start
= 0, end
= 0;
2514 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2515 tokens
.push_back(text
.substr(start
, end
- start
));
2518 tokens
.push_back(text
.substr(start
));
2521 static int sigspec_parse_get_dummy_line_num()
2526 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2528 cover("kernel.rtlil.sigspec.parse");
2530 std::vector
<std::string
> tokens
;
2531 sigspec_parse_split(tokens
, str
, ',');
2533 sig
= RTLIL::SigSpec();
2534 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2536 std::string netname
= tokens
[tokidx
];
2537 std::string indices
;
2539 if (netname
.size() == 0)
2542 if ('0' <= netname
[0] && netname
[0] <= '9') {
2543 cover("kernel.rtlil.sigspec.parse.const");
2544 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2545 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2548 sig
.append(RTLIL::Const(ast
->bits
));
2556 cover("kernel.rtlil.sigspec.parse.net");
2558 if (netname
[0] != '$' && netname
[0] != '\\')
2559 netname
= "\\" + netname
;
2561 if (module
->wires_
.count(netname
) == 0) {
2562 size_t indices_pos
= netname
.size()-1;
2563 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2566 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2567 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2569 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2571 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2572 indices
= netname
.substr(indices_pos
);
2573 netname
= netname
.substr(0, indices_pos
);
2578 if (module
->wires_
.count(netname
) == 0)
2581 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2582 if (!indices
.empty()) {
2583 std::vector
<std::string
> index_tokens
;
2584 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2585 if (index_tokens
.size() == 1) {
2586 cover("kernel.rtlil.sigspec.parse.bit_sel");
2587 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2589 cover("kernel.rtlil.sigspec.parse.part_sel");
2590 int a
= atoi(index_tokens
.at(0).c_str());
2591 int b
= atoi(index_tokens
.at(1).c_str());
2596 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2605 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2607 if (str
.empty() || str
[0] != '@')
2608 return parse(sig
, module
, str
);
2610 cover("kernel.rtlil.sigspec.parse.sel");
2612 str
= RTLIL::escape_id(str
.substr(1));
2613 if (design
->selection_vars
.count(str
) == 0)
2616 sig
= RTLIL::SigSpec();
2617 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2618 for (auto &it
: module
->wires_
)
2619 if (sel
.selected_member(module
->name
, it
.first
))
2620 sig
.append(it
.second
);
2625 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2628 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2629 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2634 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2635 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2639 if (lhs
.chunks_
.size() == 1) {
2640 char *p
= (char*)str
.c_str(), *endptr
;
2641 long long int val
= strtoll(p
, &endptr
, 10);
2642 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2643 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2644 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2649 return parse(sig
, module
, str
);
2652 RTLIL::CaseRule::~CaseRule()
2654 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2658 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2660 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2661 new_caserule
->compare
= compare
;
2662 new_caserule
->actions
= actions
;
2663 for (auto &it
: switches
)
2664 new_caserule
->switches
.push_back(it
->clone());
2665 return new_caserule
;
2668 RTLIL::SwitchRule::~SwitchRule()
2670 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2674 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2676 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2677 new_switchrule
->signal
= signal
;
2678 new_switchrule
->attributes
= attributes
;
2679 for (auto &it
: cases
)
2680 new_switchrule
->cases
.push_back(it
->clone());
2681 return new_switchrule
;
2685 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2687 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2688 new_syncrule
->type
= type
;
2689 new_syncrule
->signal
= signal
;
2690 new_syncrule
->actions
= actions
;
2691 return new_syncrule
;
2694 RTLIL::Process::~Process()
2696 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2700 RTLIL::Process
*RTLIL::Process::clone() const
2702 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2704 new_proc
->name
= name
;
2705 new_proc
->attributes
= attributes
;
2707 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2708 new_proc
->root_case
= *rc_ptr
;
2709 rc_ptr
->switches
.clear();
2712 for (auto &it
: syncs
)
2713 new_proc
->syncs
.push_back(it
->clone());