2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "frontends/verilog/verilog_frontend.h"
22 #include "backends/ilang/ilang_backend.h"
29 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
30 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
31 std::map
<char*, int, RTLIL::IdString::char_ptr_cmp
> RTLIL::IdString::global_id_index_
;
32 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
36 flags
= RTLIL::CONST_FLAG_NONE
;
39 RTLIL::Const::Const(std::string str
)
41 flags
= RTLIL::CONST_FLAG_STRING
;
42 for (int i
= str
.size()-1; i
>= 0; i
--) {
43 unsigned char ch
= str
[i
];
44 for (int j
= 0; j
< 8; j
++) {
45 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
51 RTLIL::Const::Const(int val
, int width
)
53 flags
= RTLIL::CONST_FLAG_NONE
;
54 for (int i
= 0; i
< width
; i
++) {
55 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
60 RTLIL::Const::Const(RTLIL::State bit
, int width
)
62 flags
= RTLIL::CONST_FLAG_NONE
;
63 for (int i
= 0; i
< width
; i
++)
67 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
69 if (bits
.size() != other
.bits
.size())
70 return bits
.size() < other
.bits
.size();
71 for (size_t i
= 0; i
< bits
.size(); i
++)
72 if (bits
[i
] != other
.bits
[i
])
73 return bits
[i
] < other
.bits
[i
];
77 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
79 return bits
== other
.bits
;
82 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
84 return bits
!= other
.bits
;
87 bool RTLIL::Const::as_bool() const
89 for (size_t i
= 0; i
< bits
.size(); i
++)
90 if (bits
[i
] == RTLIL::S1
)
95 int RTLIL::Const::as_int() const
98 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
99 if (bits
[i
] == RTLIL::S1
)
104 std::string
RTLIL::Const::as_string() const
107 for (size_t i
= bits
.size(); i
> 0; i
--)
109 case S0
: ret
+= "0"; break;
110 case S1
: ret
+= "1"; break;
111 case Sx
: ret
+= "x"; break;
112 case Sz
: ret
+= "z"; break;
113 case Sa
: ret
+= "-"; break;
114 case Sm
: ret
+= "m"; break;
119 std::string
RTLIL::Const::decode_string() const
122 std::vector
<char> string_chars
;
123 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
125 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
126 if (bits
[i
+ j
] == RTLIL::State::S1
)
129 string_chars
.push_back(ch
);
131 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
132 string
+= string_chars
[i
];
136 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
140 if (selected_modules
.count(mod_name
) > 0)
142 if (selected_members
.count(mod_name
) > 0)
147 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
151 if (selected_modules
.count(mod_name
) > 0)
156 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
160 if (selected_modules
.count(mod_name
) > 0)
162 if (selected_members
.count(mod_name
) > 0)
163 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
168 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
170 if (full_selection
) {
171 selected_modules
.clear();
172 selected_members
.clear();
176 std::vector
<RTLIL::IdString
> del_list
, add_list
;
179 for (auto mod_name
: selected_modules
) {
180 if (design
->modules_
.count(mod_name
) == 0)
181 del_list
.push_back(mod_name
);
182 selected_members
.erase(mod_name
);
184 for (auto mod_name
: del_list
)
185 selected_modules
.erase(mod_name
);
188 for (auto &it
: selected_members
)
189 if (design
->modules_
.count(it
.first
) == 0)
190 del_list
.push_back(it
.first
);
191 for (auto mod_name
: del_list
)
192 selected_members
.erase(mod_name
);
194 for (auto &it
: selected_members
) {
196 for (auto memb_name
: it
.second
)
197 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
198 del_list
.push_back(memb_name
);
199 for (auto memb_name
: del_list
)
200 it
.second
.erase(memb_name
);
205 for (auto &it
: selected_members
)
206 if (it
.second
.size() == 0)
207 del_list
.push_back(it
.first
);
208 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
209 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
210 add_list
.push_back(it
.first
);
211 for (auto mod_name
: del_list
)
212 selected_members
.erase(mod_name
);
213 for (auto mod_name
: add_list
) {
214 selected_members
.erase(mod_name
);
215 selected_modules
.insert(mod_name
);
218 if (selected_modules
.size() == design
->modules_
.size()) {
219 full_selection
= true;
220 selected_modules
.clear();
221 selected_members
.clear();
225 RTLIL::Design::Design()
227 refcount_modules_
= 0;
230 RTLIL::Design::~Design()
232 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
236 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
238 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
241 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
243 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
246 void RTLIL::Design::add(RTLIL::Module
*module
)
248 log_assert(modules_
.count(module
->name
) == 0);
249 log_assert(refcount_modules_
== 0);
250 modules_
[module
->name
] = module
;
251 module
->design
= this;
253 for (auto mon
: monitors
)
254 mon
->notify_module_add(module
);
257 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
259 log_assert(modules_
.count(name
) == 0);
260 log_assert(refcount_modules_
== 0);
262 RTLIL::Module
*module
= new RTLIL::Module
;
263 modules_
[name
] = module
;
264 module
->design
= this;
267 for (auto mon
: monitors
)
268 mon
->notify_module_add(module
);
273 void RTLIL::Design::remove(RTLIL::Module
*module
)
275 for (auto mon
: monitors
)
276 mon
->notify_module_del(module
);
278 log_assert(modules_
.at(module
->name
) == module
);
279 modules_
.erase(module
->name
);
283 void RTLIL::Design::check()
286 for (auto &it
: modules_
) {
287 log_assert(this == it
.second
->design
);
288 log_assert(it
.first
== it
.second
->name
);
289 log_assert(!it
.first
.empty());
295 void RTLIL::Design::optimize()
297 for (auto &it
: modules_
)
298 it
.second
->optimize();
299 for (auto &it
: selection_stack
)
301 for (auto &it
: selection_vars
)
302 it
.second
.optimize(this);
305 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
307 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
309 if (selection_stack
.size() == 0)
311 return selection_stack
.back().selected_module(mod_name
);
314 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
316 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
318 if (selection_stack
.size() == 0)
320 return selection_stack
.back().selected_whole_module(mod_name
);
323 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
325 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
327 if (selection_stack
.size() == 0)
329 return selection_stack
.back().selected_member(mod_name
, memb_name
);
332 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
334 return selected_module(mod
->name
);
337 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
339 return selected_whole_module(mod
->name
);
342 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
344 std::vector
<RTLIL::Module
*> result
;
345 result
.reserve(modules_
.size());
346 for (auto &it
: modules_
)
347 if (selected_module(it
.first
))
348 result
.push_back(it
.second
);
352 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
354 std::vector
<RTLIL::Module
*> result
;
355 result
.reserve(modules_
.size());
356 for (auto &it
: modules_
)
357 if (selected_whole_module(it
.first
))
358 result
.push_back(it
.second
);
362 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
364 std::vector
<RTLIL::Module
*> result
;
365 result
.reserve(modules_
.size());
366 for (auto &it
: modules_
)
367 if (selected_whole_module(it
.first
))
368 result
.push_back(it
.second
);
369 else if (selected_module(it
.first
))
370 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
374 RTLIL::Module::Module()
381 RTLIL::Module::~Module()
383 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
385 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
387 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
389 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
393 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
395 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
398 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
400 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
405 struct InternalCellChecker
407 RTLIL::Module
*module
;
409 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
411 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
413 void error(int linenr
)
418 FILE *f
= open_memstream(&ptr
, &size
);
419 ILANG_BACKEND::dump_cell(f
, " ", cell
);
423 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
424 module
? module
->name
.c_str() : "", module
? "." : "",
425 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
428 int param(const char *name
)
430 if (cell
->parameters
.count(name
) == 0)
432 expected_params
.insert(name
);
433 return cell
->parameters
.at(name
).as_int();
436 int param_bool(const char *name
)
439 if (cell
->parameters
.at(name
).bits
.size() > 32)
441 if (v
!= 0 && v
!= 1)
446 void param_bits(const char *name
, int width
)
449 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
453 void port(const char *name
, int width
)
455 if (!cell
->hasPort(name
))
457 if (cell
->getPort(name
).size() != width
)
459 expected_ports
.insert(name
);
462 void check_expected(bool check_matched_sign
= true)
464 for (auto ¶
: cell
->parameters
)
465 if (expected_params
.count(para
.first
) == 0)
467 for (auto &conn
: cell
->connections())
468 if (expected_ports
.count(conn
.first
) == 0)
471 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
472 bool a_is_signed
= param("\\A_SIGNED") != 0;
473 bool b_is_signed
= param("\\B_SIGNED") != 0;
474 if (a_is_signed
!= b_is_signed
)
479 void check_gate(const char *ports
)
481 if (cell
->parameters
.size() != 0)
484 for (const char *p
= ports
; *p
; p
++) {
485 char portname
[3] = { '\\', *p
, 0 };
486 if (!cell
->hasPort(portname
))
488 if (cell
->getPort(portname
).size() != 1)
492 for (auto &conn
: cell
->connections()) {
493 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
495 if (strchr(ports
, conn
.first
[1]) == NULL
)
502 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
503 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
506 if (cell
->type
== "$not" || cell
->type
== "$pos" || cell
->type
== "$bu0" || cell
->type
== "$neg") {
507 param_bool("\\A_SIGNED");
508 port("\\A", param("\\A_WIDTH"));
509 port("\\Y", param("\\Y_WIDTH"));
514 if (cell
->type
== "$and" || cell
->type
== "$or" || cell
->type
== "$xor" || cell
->type
== "$xnor") {
515 param_bool("\\A_SIGNED");
516 param_bool("\\B_SIGNED");
517 port("\\A", param("\\A_WIDTH"));
518 port("\\B", param("\\B_WIDTH"));
519 port("\\Y", param("\\Y_WIDTH"));
524 if (cell
->type
== "$reduce_and" || cell
->type
== "$reduce_or" || cell
->type
== "$reduce_xor" ||
525 cell
->type
== "$reduce_xnor" || cell
->type
== "$reduce_bool") {
526 param_bool("\\A_SIGNED");
527 port("\\A", param("\\A_WIDTH"));
528 port("\\Y", param("\\Y_WIDTH"));
533 if (cell
->type
== "$shl" || cell
->type
== "$shr" || cell
->type
== "$sshl" || cell
->type
== "$sshr" ||
534 cell
->type
== "$shift" || cell
->type
== "$shiftx") {
535 param_bool("\\A_SIGNED");
536 param_bool("\\B_SIGNED");
537 port("\\A", param("\\A_WIDTH"));
538 port("\\B", param("\\B_WIDTH"));
539 port("\\Y", param("\\Y_WIDTH"));
540 check_expected(false);
544 if (cell
->type
== "$lt" || cell
->type
== "$le" || cell
->type
== "$eq" || cell
->type
== "$ne" ||
545 cell
->type
== "$eqx" || cell
->type
== "$nex" || cell
->type
== "$ge" || cell
->type
== "$gt") {
546 param_bool("\\A_SIGNED");
547 param_bool("\\B_SIGNED");
548 port("\\A", param("\\A_WIDTH"));
549 port("\\B", param("\\B_WIDTH"));
550 port("\\Y", param("\\Y_WIDTH"));
555 if (cell
->type
== "$add" || cell
->type
== "$sub" || cell
->type
== "$mul" || cell
->type
== "$div" ||
556 cell
->type
== "$mod" || cell
->type
== "$pow") {
557 param_bool("\\A_SIGNED");
558 param_bool("\\B_SIGNED");
559 port("\\A", param("\\A_WIDTH"));
560 port("\\B", param("\\B_WIDTH"));
561 port("\\Y", param("\\Y_WIDTH"));
562 check_expected(cell
->type
!= "$pow");
566 if (cell
->type
== "$logic_not") {
567 param_bool("\\A_SIGNED");
568 port("\\A", param("\\A_WIDTH"));
569 port("\\Y", param("\\Y_WIDTH"));
574 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
575 param_bool("\\A_SIGNED");
576 param_bool("\\B_SIGNED");
577 port("\\A", param("\\A_WIDTH"));
578 port("\\B", param("\\B_WIDTH"));
579 port("\\Y", param("\\Y_WIDTH"));
580 check_expected(false);
584 if (cell
->type
== "$slice") {
586 port("\\A", param("\\A_WIDTH"));
587 port("\\Y", param("\\Y_WIDTH"));
588 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
594 if (cell
->type
== "$concat") {
595 port("\\A", param("\\A_WIDTH"));
596 port("\\B", param("\\B_WIDTH"));
597 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
602 if (cell
->type
== "$mux") {
603 port("\\A", param("\\WIDTH"));
604 port("\\B", param("\\WIDTH"));
606 port("\\Y", param("\\WIDTH"));
611 if (cell
->type
== "$pmux" || cell
->type
== "$safe_pmux") {
612 port("\\A", param("\\WIDTH"));
613 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
614 port("\\S", param("\\S_WIDTH"));
615 port("\\Y", param("\\WIDTH"));
620 if (cell
->type
== "$lut") {
622 port("\\I", param("\\WIDTH"));
628 if (cell
->type
== "$sr") {
629 param_bool("\\SET_POLARITY");
630 param_bool("\\CLR_POLARITY");
631 port("\\SET", param("\\WIDTH"));
632 port("\\CLR", param("\\WIDTH"));
633 port("\\Q", param("\\WIDTH"));
638 if (cell
->type
== "$dff") {
639 param_bool("\\CLK_POLARITY");
641 port("\\D", param("\\WIDTH"));
642 port("\\Q", param("\\WIDTH"));
647 if (cell
->type
== "$dffsr") {
648 param_bool("\\CLK_POLARITY");
649 param_bool("\\SET_POLARITY");
650 param_bool("\\CLR_POLARITY");
652 port("\\SET", param("\\WIDTH"));
653 port("\\CLR", param("\\WIDTH"));
654 port("\\D", param("\\WIDTH"));
655 port("\\Q", param("\\WIDTH"));
660 if (cell
->type
== "$adff") {
661 param_bool("\\CLK_POLARITY");
662 param_bool("\\ARST_POLARITY");
663 param_bits("\\ARST_VALUE", param("\\WIDTH"));
666 port("\\D", param("\\WIDTH"));
667 port("\\Q", param("\\WIDTH"));
672 if (cell
->type
== "$dlatch") {
673 param_bool("\\EN_POLARITY");
675 port("\\D", param("\\WIDTH"));
676 port("\\Q", param("\\WIDTH"));
681 if (cell
->type
== "$dlatchsr") {
682 param_bool("\\EN_POLARITY");
683 param_bool("\\SET_POLARITY");
684 param_bool("\\CLR_POLARITY");
686 port("\\SET", param("\\WIDTH"));
687 port("\\CLR", param("\\WIDTH"));
688 port("\\D", param("\\WIDTH"));
689 port("\\Q", param("\\WIDTH"));
694 if (cell
->type
== "$fsm") {
696 param_bool("\\CLK_POLARITY");
697 param_bool("\\ARST_POLARITY");
698 param("\\STATE_BITS");
699 param("\\STATE_NUM");
700 param("\\STATE_NUM_LOG2");
701 param("\\STATE_RST");
702 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
703 param("\\TRANS_NUM");
704 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
707 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
708 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
713 if (cell
->type
== "$memrd") {
715 param_bool("\\CLK_ENABLE");
716 param_bool("\\CLK_POLARITY");
717 param_bool("\\TRANSPARENT");
719 port("\\ADDR", param("\\ABITS"));
720 port("\\DATA", param("\\WIDTH"));
725 if (cell
->type
== "$memwr") {
727 param_bool("\\CLK_ENABLE");
728 param_bool("\\CLK_POLARITY");
731 port("\\EN", param("\\WIDTH"));
732 port("\\ADDR", param("\\ABITS"));
733 port("\\DATA", param("\\WIDTH"));
738 if (cell
->type
== "$mem") {
742 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
743 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
744 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
745 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
746 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
747 port("\\RD_CLK", param("\\RD_PORTS"));
748 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
749 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
750 port("\\WR_CLK", param("\\WR_PORTS"));
751 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
752 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
753 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
758 if (cell
->type
== "$assert") {
765 if (cell
->type
== "$_INV_") { check_gate("AY"); return; }
766 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
767 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
768 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
769 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
771 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
772 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
773 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
774 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
776 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
777 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
779 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
780 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
781 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
782 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
783 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
784 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
785 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
786 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
788 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
789 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
790 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
791 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
792 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
793 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
794 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
795 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
797 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
798 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
800 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
801 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
802 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
803 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
804 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
805 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
806 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
807 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
815 void RTLIL::Module::check()
818 std::vector
<bool> ports_declared
;
819 for (auto &it
: wires_
) {
820 log_assert(this == it
.second
->module
);
821 log_assert(it
.first
== it
.second
->name
);
822 log_assert(!it
.first
.empty());
823 log_assert(it
.second
->width
>= 0);
824 log_assert(it
.second
->port_id
>= 0);
825 for (auto &it2
: it
.second
->attributes
)
826 log_assert(!it2
.first
.empty());
827 if (it
.second
->port_id
) {
828 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
829 if (SIZE(ports_declared
) < it
.second
->port_id
)
830 ports_declared
.resize(it
.second
->port_id
);
831 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
832 ports_declared
[it
.second
->port_id
-1] = true;
834 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
836 for (auto port_declared
: ports_declared
)
837 log_assert(port_declared
== true);
839 for (auto &it
: memories
) {
840 log_assert(it
.first
== it
.second
->name
);
841 log_assert(!it
.first
.empty());
842 log_assert(it
.second
->width
>= 0);
843 log_assert(it
.second
->size
>= 0);
844 for (auto &it2
: it
.second
->attributes
)
845 log_assert(!it2
.first
.empty());
848 for (auto &it
: cells_
) {
849 log_assert(this == it
.second
->module
);
850 log_assert(it
.first
== it
.second
->name
);
851 log_assert(!it
.first
.empty());
852 log_assert(!it
.second
->type
.empty());
853 for (auto &it2
: it
.second
->connections()) {
854 log_assert(!it2
.first
.empty());
857 for (auto &it2
: it
.second
->attributes
)
858 log_assert(!it2
.first
.empty());
859 for (auto &it2
: it
.second
->parameters
)
860 log_assert(!it2
.first
.empty());
861 InternalCellChecker
checker(this, it
.second
);
865 for (auto &it
: processes
) {
866 log_assert(it
.first
== it
.second
->name
);
867 log_assert(!it
.first
.empty());
868 // FIXME: More checks here..
871 for (auto &it
: connections_
) {
872 log_assert(it
.first
.size() == it
.second
.size());
877 for (auto &it
: attributes
)
878 log_assert(!it
.first
.empty());
882 void RTLIL::Module::optimize()
886 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
888 log_assert(new_mod
->refcount_wires_
== 0);
889 log_assert(new_mod
->refcount_cells_
== 0);
891 new_mod
->connections_
= connections_
;
892 new_mod
->attributes
= attributes
;
894 for (auto &it
: wires_
)
895 new_mod
->addWire(it
.first
, it
.second
);
897 for (auto &it
: memories
)
898 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
900 for (auto &it
: cells_
)
901 new_mod
->addCell(it
.first
, it
.second
);
903 for (auto &it
: processes
)
904 new_mod
->processes
[it
.first
] = it
.second
->clone();
906 struct RewriteSigSpecWorker
909 void operator()(RTLIL::SigSpec
&sig
)
911 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
912 for (auto &c
: chunks
)
914 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
919 RewriteSigSpecWorker rewriteSigSpecWorker
;
920 rewriteSigSpecWorker
.mod
= new_mod
;
921 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
924 RTLIL::Module
*RTLIL::Module::clone() const
926 RTLIL::Module
*new_mod
= new RTLIL::Module
;
927 new_mod
->name
= name
;
932 bool RTLIL::Module::has_memories() const
934 return !memories
.empty();
937 bool RTLIL::Module::has_processes() const
939 return !processes
.empty();
942 bool RTLIL::Module::has_memories_warn() const
944 if (!memories
.empty())
945 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
946 return !memories
.empty();
949 bool RTLIL::Module::has_processes_warn() const
951 if (!processes
.empty())
952 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
953 return !processes
.empty();
956 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
958 std::vector
<RTLIL::Wire
*> result
;
959 result
.reserve(wires_
.size());
960 for (auto &it
: wires_
)
961 if (design
->selected(this, it
.second
))
962 result
.push_back(it
.second
);
966 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
968 std::vector
<RTLIL::Cell
*> result
;
969 result
.reserve(wires_
.size());
970 for (auto &it
: cells_
)
971 if (design
->selected(this, it
.second
))
972 result
.push_back(it
.second
);
976 void RTLIL::Module::add(RTLIL::Wire
*wire
)
978 log_assert(!wire
->name
.empty());
979 log_assert(count_id(wire
->name
) == 0);
980 log_assert(refcount_wires_
== 0);
981 wires_
[wire
->name
] = wire
;
985 void RTLIL::Module::add(RTLIL::Cell
*cell
)
987 log_assert(!cell
->name
.empty());
988 log_assert(count_id(cell
->name
) == 0);
989 log_assert(refcount_cells_
== 0);
990 cells_
[cell
->name
] = cell
;
995 struct DeleteWireWorker
997 RTLIL::Module
*module
;
998 const std::set
<RTLIL::Wire
*> *wires_p
;
1000 void operator()(RTLIL::SigSpec
&sig
) {
1001 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1002 for (auto &c
: chunks
)
1003 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1004 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1013 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1015 std::setPort
<RTLIL::Wire
*> wires_
;
1016 wires_
.insert(wire
);
1021 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1023 log_assert(refcount_wires_
== 0);
1025 DeleteWireWorker delete_wire_worker
;
1026 delete_wire_worker
.module
= this;
1027 delete_wire_worker
.wires_p
= &wires
;
1028 rewrite_sigspecs(delete_wire_worker
);
1030 for (auto &it
: wires
) {
1031 log_assert(wires_
.count(it
->name
) != 0);
1032 wires_
.erase(it
->name
);
1037 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1039 log_assert(cells_
.count(cell
->name
) != 0);
1040 log_assert(refcount_cells_
== 0);
1041 cells_
.erase(cell
->name
);
1045 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1047 log_assert(wires_
[wire
->name
] == wire
);
1048 log_assert(refcount_wires_
== 0);
1049 wires_
.erase(wire
->name
);
1050 wire
->name
= new_name
;
1054 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1056 log_assert(cells_
[cell
->name
] == cell
);
1057 log_assert(refcount_wires_
== 0);
1058 cells_
.erase(cell
->name
);
1059 cell
->name
= new_name
;
1063 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1065 log_assert(count_id(old_name
) != 0);
1066 if (wires_
.count(old_name
))
1067 rename(wires_
.at(old_name
), new_name
);
1068 else if (cells_
.count(old_name
))
1069 rename(cells_
.at(old_name
), new_name
);
1074 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1076 log_assert(wires_
[w1
->name
] == w1
);
1077 log_assert(wires_
[w2
->name
] == w2
);
1078 log_assert(refcount_wires_
== 0);
1080 wires_
.erase(w1
->name
);
1081 wires_
.erase(w2
->name
);
1083 std::swap(w1
->name
, w2
->name
);
1085 wires_
[w1
->name
] = w1
;
1086 wires_
[w2
->name
] = w2
;
1089 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1091 log_assert(cells_
[c1
->name
] == c1
);
1092 log_assert(cells_
[c2
->name
] == c2
);
1093 log_assert(refcount_cells_
== 0);
1095 cells_
.erase(c1
->name
);
1096 cells_
.erase(c2
->name
);
1098 std::swap(c1
->name
, c2
->name
);
1100 cells_
[c1
->name
] = c1
;
1101 cells_
[c2
->name
] = c2
;
1104 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1106 if (a
->port_id
&& !b
->port_id
)
1108 if (!a
->port_id
&& b
->port_id
)
1111 if (a
->port_id
== b
->port_id
)
1112 return a
->name
< b
->name
;
1113 return a
->port_id
< b
->port_id
;
1116 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1118 for (auto mon
: monitors
)
1119 mon
->notify_connect(this, conn
);
1122 for (auto mon
: design
->monitors
)
1123 mon
->notify_connect(this, conn
);
1125 connections_
.push_back(conn
);
1128 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1130 connect(RTLIL::SigSig(lhs
, rhs
));
1133 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1135 for (auto mon
: monitors
)
1136 mon
->notify_connect(this, new_conn
);
1139 for (auto mon
: design
->monitors
)
1140 mon
->notify_connect(this, new_conn
);
1142 connections_
= new_conn
;
1145 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1147 return connections_
;
1150 void RTLIL::Module::fixup_ports()
1152 std::vector
<RTLIL::Wire
*> all_ports
;
1154 for (auto &w
: wires_
)
1155 if (w
.second
->port_input
|| w
.second
->port_output
)
1156 all_ports
.push_back(w
.second
);
1158 w
.second
->port_id
= 0;
1160 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1161 for (size_t i
= 0; i
< all_ports
.size(); i
++)
1162 all_ports
[i
]->port_id
= i
+1;
1165 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1167 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1169 wire
->width
= width
;
1174 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1176 RTLIL::Wire
*wire
= addWire(name
);
1177 wire
->width
= other
->width
;
1178 wire
->start_offset
= other
->start_offset
;
1179 wire
->port_id
= other
->port_id
;
1180 wire
->port_input
= other
->port_input
;
1181 wire
->port_output
= other
->port_output
;
1182 wire
->upto
= other
->upto
;
1183 wire
->attributes
= other
->attributes
;
1187 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1189 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1196 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1198 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1199 cell
->connections_
= other
->connections_
;
1200 cell
->parameters
= other
->parameters
;
1201 cell
->attributes
= other
->attributes
;
1205 #define DEF_METHOD(_func, _y_size, _type) \
1206 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1207 RTLIL::Cell *cell = addCell(name, _type); \
1208 cell->parameters["\\A_SIGNED"] = is_signed; \
1209 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1210 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1211 cell->setPort("\\A", sig_a); \
1212 cell->setPort("\\Y", sig_y); \
1215 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1216 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1217 add ## _func(name, sig_a, sig_y, is_signed); \
1220 DEF_METHOD(Not
, sig_a
.size(), "$not")
1221 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1222 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1223 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1224 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1225 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1226 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1227 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1228 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1229 DEF_METHOD(LogicNot
, 1, "$logic_not")
1232 #define DEF_METHOD(_func, _y_size, _type) \
1233 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1234 RTLIL::Cell *cell = addCell(name, _type); \
1235 cell->parameters["\\A_SIGNED"] = is_signed; \
1236 cell->parameters["\\B_SIGNED"] = is_signed; \
1237 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1238 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1239 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1240 cell->setPort("\\A", sig_a); \
1241 cell->setPort("\\B", sig_b); \
1242 cell->setPort("\\Y", sig_y); \
1245 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1246 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1247 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1250 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1251 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1252 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1253 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1254 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1255 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1256 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1257 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1258 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1259 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1260 DEF_METHOD(Lt
, 1, "$lt")
1261 DEF_METHOD(Le
, 1, "$le")
1262 DEF_METHOD(Eq
, 1, "$eq")
1263 DEF_METHOD(Ne
, 1, "$ne")
1264 DEF_METHOD(Eqx
, 1, "$eqx")
1265 DEF_METHOD(Nex
, 1, "$nex")
1266 DEF_METHOD(Ge
, 1, "$ge")
1267 DEF_METHOD(Gt
, 1, "$gt")
1268 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1269 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1270 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1271 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1272 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1273 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1274 DEF_METHOD(LogicOr
, 1, "$logic_or")
1277 #define DEF_METHOD(_func, _type, _pmux) \
1278 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) { \
1279 RTLIL::Cell *cell = addCell(name, _type); \
1280 cell->parameters["\\WIDTH"] = sig_a.size(); \
1281 cell->parameters["\\WIDTH"] = sig_b.size(); \
1282 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1283 cell->setPort("\\A", sig_a); \
1284 cell->setPort("\\B", sig_b); \
1285 cell->setPort("\\S", sig_s); \
1286 cell->setPort("\\Y", sig_y); \
1289 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1290 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1291 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1294 DEF_METHOD(Mux
, "$mux", 0)
1295 DEF_METHOD(Pmux
, "$pmux", 1)
1296 DEF_METHOD(SafePmux
, "$safe_pmux", 1)
1299 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1300 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1301 RTLIL::Cell *cell = addCell(name, _type); \
1302 cell->setPort("\\" #_P1, sig1); \
1303 cell->setPort("\\" #_P2, sig2); \
1306 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
1307 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
1308 add ## _func(name, sig1, sig2); \
1311 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1312 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1313 RTLIL::Cell *cell = addCell(name, _type); \
1314 cell->setPort("\\" #_P1, sig1); \
1315 cell->setPort("\\" #_P2, sig2); \
1316 cell->setPort("\\" #_P3, sig3); \
1319 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1320 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1321 add ## _func(name, sig1, sig2, sig3); \
1324 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1325 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1326 RTLIL::Cell *cell = addCell(name, _type); \
1327 cell->setPort("\\" #_P1, sig1); \
1328 cell->setPort("\\" #_P2, sig2); \
1329 cell->setPort("\\" #_P3, sig3); \
1330 cell->setPort("\\" #_P4, sig4); \
1333 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1334 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1335 add ## _func(name, sig1, sig2, sig3, sig4); \
1338 DEF_METHOD_2(InvGate
, "$_INV_", A
, Y
)
1339 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1340 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1341 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1342 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1347 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
)
1349 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1350 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1351 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1352 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1353 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1354 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1355 cell
->setPort("\\A", sig_a
);
1356 cell
->setPort("\\B", sig_b
);
1357 cell
->setPort("\\Y", sig_y
);
1361 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1363 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1364 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1365 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1366 cell
->parameters
["\\OFFSET"] = offset
;
1367 cell
->setPort("\\A", sig_a
);
1368 cell
->setPort("\\Y", sig_y
);
1372 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1374 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1375 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1376 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1377 cell
->setPort("\\A", sig_a
);
1378 cell
->setPort("\\B", sig_b
);
1379 cell
->setPort("\\Y", sig_y
);
1383 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1385 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1386 cell
->parameters
["\\LUT"] = lut
;
1387 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1388 cell
->setPort("\\I", sig_i
);
1389 cell
->setPort("\\O", sig_o
);
1393 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1395 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1396 cell
->setPort("\\A", sig_a
);
1397 cell
->setPort("\\EN", sig_en
);
1401 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
)
1403 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1404 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1405 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1406 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1407 cell
->setPort("\\SET", sig_set
);
1408 cell
->setPort("\\CLR", sig_clr
);
1409 cell
->setPort("\\Q", sig_q
);
1413 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1415 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1416 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1417 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1418 cell
->setPort("\\CLK", sig_clk
);
1419 cell
->setPort("\\D", sig_d
);
1420 cell
->setPort("\\Q", sig_q
);
1424 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1425 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1427 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1428 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1429 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1430 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1431 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1432 cell
->setPort("\\CLK", sig_clk
);
1433 cell
->setPort("\\SET", sig_set
);
1434 cell
->setPort("\\CLR", sig_clr
);
1435 cell
->setPort("\\D", sig_d
);
1436 cell
->setPort("\\Q", sig_q
);
1440 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1441 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1443 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1444 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1445 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1446 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1447 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1448 cell
->setPort("\\CLK", sig_clk
);
1449 cell
->setPort("\\ARST", sig_arst
);
1450 cell
->setPort("\\D", sig_d
);
1451 cell
->setPort("\\Q", sig_q
);
1455 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1457 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1458 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1459 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1460 cell
->setPort("\\EN", sig_en
);
1461 cell
->setPort("\\D", sig_d
);
1462 cell
->setPort("\\Q", sig_q
);
1466 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1467 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1469 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1470 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1471 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1472 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1473 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1474 cell
->setPort("\\EN", sig_en
);
1475 cell
->setPort("\\SET", sig_set
);
1476 cell
->setPort("\\CLR", sig_clr
);
1477 cell
->setPort("\\D", sig_d
);
1478 cell
->setPort("\\Q", sig_q
);
1482 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1484 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1485 cell
->setPort("\\C", sig_clk
);
1486 cell
->setPort("\\D", sig_d
);
1487 cell
->setPort("\\Q", sig_q
);
1491 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1492 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1494 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1495 cell
->setPort("\\C", sig_clk
);
1496 cell
->setPort("\\S", sig_set
);
1497 cell
->setPort("\\R", sig_clr
);
1498 cell
->setPort("\\D", sig_d
);
1499 cell
->setPort("\\Q", sig_q
);
1503 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1504 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1506 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1507 cell
->setPort("\\C", sig_clk
);
1508 cell
->setPort("\\R", sig_arst
);
1509 cell
->setPort("\\D", sig_d
);
1510 cell
->setPort("\\Q", sig_q
);
1514 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1516 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1517 cell
->setPort("\\E", sig_en
);
1518 cell
->setPort("\\D", sig_d
);
1519 cell
->setPort("\\Q", sig_q
);
1523 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1524 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1526 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1527 cell
->setPort("\\E", sig_en
);
1528 cell
->setPort("\\S", sig_set
);
1529 cell
->setPort("\\R", sig_clr
);
1530 cell
->setPort("\\D", sig_d
);
1531 cell
->setPort("\\Q", sig_q
);
1543 port_output
= false;
1547 RTLIL::Memory::Memory()
1553 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1555 return connections_
.count(portname
) != 0;
1558 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1560 RTLIL::SigSpec signal
;
1561 auto conn_it
= connections_
.find(portname
);
1563 if (conn_it
!= connections_
.end())
1565 for (auto mon
: module
->monitors
)
1566 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1569 for (auto mon
: module
->design
->monitors
)
1570 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1572 connections_
.erase(conn_it
);
1576 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1578 auto conn_it
= connections_
.find(portname
);
1580 if (conn_it
== connections_
.end()) {
1581 connections_
[portname
] = RTLIL::SigSpec();
1582 conn_it
= connections_
.find(portname
);
1583 log_assert(conn_it
!= connections_
.end());
1586 for (auto mon
: module
->monitors
)
1587 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1590 for (auto mon
: module
->design
->monitors
)
1591 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1593 conn_it
->second
= signal
;
1596 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1598 return connections_
.at(portname
);
1601 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1603 return connections_
;
1606 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1608 return parameters
.count(paramname
);
1611 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1613 parameters
.erase(paramname
);
1616 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1618 parameters
[paramname
] = value
;
1621 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1623 return parameters
.at(paramname
);
1626 void RTLIL::Cell::check()
1629 InternalCellChecker
checker(NULL
, this);
1634 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1636 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1637 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1640 if (type
== "$mux" || type
== "$pmux" || type
== "$safe_pmux")
1642 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1643 if (type
== "$pmux" || type
== "$safe_pmux")
1644 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1649 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1651 if (connections_
.count("\\A")) {
1652 if (signedness_ab
) {
1654 parameters
["\\A_SIGNED"] = true;
1655 else if (parameters
.count("\\A_SIGNED") == 0)
1656 parameters
["\\A_SIGNED"] = false;
1658 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1661 if (connections_
.count("\\B")) {
1662 if (signedness_ab
) {
1664 parameters
["\\B_SIGNED"] = true;
1665 else if (parameters
.count("\\B_SIGNED") == 0)
1666 parameters
["\\B_SIGNED"] = false;
1668 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1671 if (connections_
.count("\\Y"))
1672 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1677 RTLIL::SigChunk::SigChunk()
1684 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1688 width
= data
.bits
.size();
1692 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1694 log_assert(wire
!= nullptr);
1696 this->width
= wire
->width
;
1700 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1702 log_assert(wire
!= nullptr);
1704 this->width
= width
;
1705 this->offset
= offset
;
1708 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1711 data
= RTLIL::Const(str
);
1712 width
= data
.bits
.size();
1716 RTLIL::SigChunk::SigChunk(int val
, int width
)
1719 data
= RTLIL::Const(val
, width
);
1720 this->width
= data
.bits
.size();
1724 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1727 data
= RTLIL::Const(bit
, width
);
1728 this->width
= data
.bits
.size();
1732 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1737 data
= RTLIL::Const(bit
.data
);
1739 offset
= bit
.offset
;
1743 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1745 RTLIL::SigChunk ret
;
1748 ret
.offset
= this->offset
+ offset
;
1751 for (int i
= 0; i
< length
; i
++)
1752 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1758 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1760 if (wire
&& other
.wire
)
1761 if (wire
->name
!= other
.wire
->name
)
1762 return wire
->name
< other
.wire
->name
;
1764 if (wire
!= other
.wire
)
1765 return wire
< other
.wire
;
1767 if (offset
!= other
.offset
)
1768 return offset
< other
.offset
;
1770 if (width
!= other
.width
)
1771 return width
< other
.width
;
1773 return data
.bits
< other
.data
.bits
;
1776 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1778 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1780 if (data
.bits
!= other
.data
.bits
)
1785 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1792 RTLIL::SigSpec::SigSpec()
1798 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1803 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1805 cover("kernel.rtlil.sigspec.init.list");
1810 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1811 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1815 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1817 cover("kernel.rtlil.sigspec.assign");
1819 width_
= other
.width_
;
1820 hash_
= other
.hash_
;
1821 chunks_
= other
.chunks_
;
1824 if (!other
.bits_
.empty())
1826 RTLIL::SigChunk
*last
= NULL
;
1827 int last_end_offset
= 0;
1829 for (auto &bit
: other
.bits_
) {
1830 if (last
&& bit
.wire
== last
->wire
) {
1831 if (bit
.wire
== NULL
) {
1832 last
->data
.bits
.push_back(bit
.data
);
1835 } else if (last_end_offset
== bit
.offset
) {
1841 chunks_
.push_back(bit
);
1842 last
= &chunks_
.back();
1843 last_end_offset
= bit
.offset
+ 1;
1852 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1854 cover("kernel.rtlil.sigspec.init.const");
1856 chunks_
.push_back(RTLIL::SigChunk(value
));
1857 width_
= chunks_
.back().width
;
1862 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1864 cover("kernel.rtlil.sigspec.init.chunk");
1866 chunks_
.push_back(chunk
);
1867 width_
= chunks_
.back().width
;
1872 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1874 cover("kernel.rtlil.sigspec.init.wire");
1876 chunks_
.push_back(RTLIL::SigChunk(wire
));
1877 width_
= chunks_
.back().width
;
1882 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1884 cover("kernel.rtlil.sigspec.init.wire_part");
1886 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1887 width_
= chunks_
.back().width
;
1892 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1894 cover("kernel.rtlil.sigspec.init.str");
1896 chunks_
.push_back(RTLIL::SigChunk(str
));
1897 width_
= chunks_
.back().width
;
1902 RTLIL::SigSpec::SigSpec(int val
, int width
)
1904 cover("kernel.rtlil.sigspec.init.int");
1906 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1912 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1914 cover("kernel.rtlil.sigspec.init.state");
1916 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1922 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1924 cover("kernel.rtlil.sigspec.init.bit");
1926 if (bit
.wire
== NULL
)
1927 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1929 for (int i
= 0; i
< width
; i
++)
1930 chunks_
.push_back(bit
);
1936 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1938 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1942 for (auto &c
: chunks
)
1947 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1949 cover("kernel.rtlil.sigspec.init.stdvec_bits");
1953 for (auto &bit
: bits
)
1958 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1960 cover("kernel.rtlil.sigspec.init.stdset_bits");
1964 for (auto &bit
: bits
)
1969 void RTLIL::SigSpec::pack() const
1971 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1973 if (that
->bits_
.empty())
1976 cover("kernel.rtlil.sigspec.convert.pack");
1977 log_assert(that
->chunks_
.empty());
1979 std::vector
<RTLIL::SigBit
> old_bits
;
1980 old_bits
.swap(that
->bits_
);
1982 RTLIL::SigChunk
*last
= NULL
;
1983 int last_end_offset
= 0;
1985 for (auto &bit
: old_bits
) {
1986 if (last
&& bit
.wire
== last
->wire
) {
1987 if (bit
.wire
== NULL
) {
1988 last
->data
.bits
.push_back(bit
.data
);
1991 } else if (last_end_offset
== bit
.offset
) {
1997 that
->chunks_
.push_back(bit
);
1998 last
= &that
->chunks_
.back();
1999 last_end_offset
= bit
.offset
+ 1;
2005 void RTLIL::SigSpec::unpack() const
2007 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2009 if (that
->chunks_
.empty())
2012 cover("kernel.rtlil.sigspec.convert.unpack");
2013 log_assert(that
->bits_
.empty());
2015 that
->bits_
.reserve(that
->width_
);
2016 for (auto &c
: that
->chunks_
)
2017 for (int i
= 0; i
< c
.width
; i
++)
2018 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2020 that
->chunks_
.clear();
2024 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2026 void RTLIL::SigSpec::hash() const
2028 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2030 if (that
->hash_
!= 0)
2033 cover("kernel.rtlil.sigspec.hash");
2037 for (auto &c
: that
->chunks_
)
2038 if (c
.wire
== NULL
) {
2039 for (auto &v
: c
.data
.bits
)
2040 DJB2(that
->hash_
, v
);
2042 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2043 DJB2(that
->hash_
, c
.offset
);
2044 DJB2(that
->hash_
, c
.width
);
2047 if (that
->hash_
== 0)
2051 void RTLIL::SigSpec::sort()
2054 cover("kernel.rtlil.sigspec.sort");
2055 std::sort(bits_
.begin(), bits_
.end());
2058 void RTLIL::SigSpec::sort_and_unify()
2060 cover("kernel.rtlil.sigspec.sort_and_unify");
2061 *this = this->to_sigbit_set();
2064 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2066 replace(pattern
, with
, this);
2069 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2071 cover("kernel.rtlil.sigspec.replace");
2077 log_assert(other
!= NULL
);
2078 log_assert(width_
== other
->width_
);
2081 log_assert(pattern
.width_
== with
.width_
);
2083 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
2084 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2085 if (pattern
.bits_
[i
].wire
!= NULL
)
2086 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2088 for (int i
= 0; i
< SIZE(bits_
); i
++)
2089 if (pattern_map
.count(bits_
[i
]))
2090 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
2095 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2097 remove2(pattern
, NULL
);
2100 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2102 RTLIL::SigSpec tmp
= *this;
2103 tmp
.remove2(pattern
, other
);
2106 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2109 cover("kernel.rtlil.sigspec.remove_other");
2111 cover("kernel.rtlil.sigspec.remove");
2115 if (other
!= NULL
) {
2116 log_assert(width_
== other
->width_
);
2120 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2121 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2123 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2124 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
2127 new_other_bits
.push_back(other
->bits_
[i
]);
2128 new_bits
.push_back(bits_
[i
]);
2131 bits_
.swap(new_bits
);
2132 width_
= SIZE(bits_
);
2134 if (other
!= NULL
) {
2135 other
->bits_
.swap(new_other_bits
);
2136 other
->width_
= SIZE(other
->bits_
);
2142 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
2145 cover("kernel.rtlil.sigspec.extract_other");
2147 cover("kernel.rtlil.sigspec.extract");
2155 log_assert(other
== NULL
|| width_
== other
->width_
);
2157 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
2158 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2162 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2163 for (int i
= 0; i
< width_
; i
++)
2164 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2165 ret
.append_bit(bits_other
[i
]);
2167 for (int i
= 0; i
< width_
; i
++)
2168 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2169 ret
.append_bit(bits_match
[i
]);
2176 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2178 cover("kernel.rtlil.sigspec.replace_pos");
2183 log_assert(offset
>= 0);
2184 log_assert(with
.width_
>= 0);
2185 log_assert(offset
+with
.width_
<= width_
);
2187 for (int i
= 0; i
< with
.width_
; i
++)
2188 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2193 void RTLIL::SigSpec::remove_const()
2197 cover("kernel.rtlil.sigspec.remove_const.packed");
2199 std::vector
<RTLIL::SigChunk
> new_chunks
;
2200 new_chunks
.reserve(SIZE(chunks_
));
2203 for (auto &chunk
: chunks_
)
2204 if (chunk
.wire
!= NULL
) {
2205 new_chunks
.push_back(chunk
);
2206 width_
+= chunk
.width
;
2209 chunks_
.swap(new_chunks
);
2213 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2215 std::vector
<RTLIL::SigBit
> new_bits
;
2216 new_bits
.reserve(width_
);
2218 for (auto &bit
: bits_
)
2219 if (bit
.wire
!= NULL
)
2220 new_bits
.push_back(bit
);
2222 bits_
.swap(new_bits
);
2223 width_
= bits_
.size();
2229 void RTLIL::SigSpec::remove(int offset
, int length
)
2231 cover("kernel.rtlil.sigspec.remove_pos");
2235 log_assert(offset
>= 0);
2236 log_assert(length
>= 0);
2237 log_assert(offset
+ length
<= width_
);
2239 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2240 width_
= bits_
.size();
2245 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2248 cover("kernel.rtlil.sigspec.extract_pos");
2249 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2252 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2254 if (signal
.width_
== 0)
2262 cover("kernel.rtlil.sigspec.append");
2264 if (packed() != signal
.packed()) {
2270 for (auto &other_c
: signal
.chunks_
)
2272 auto &my_last_c
= chunks_
.back();
2273 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2274 auto &this_data
= my_last_c
.data
.bits
;
2275 auto &other_data
= other_c
.data
.bits
;
2276 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2277 my_last_c
.width
+= other_c
.width
;
2279 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2280 my_last_c
.width
+= other_c
.width
;
2282 chunks_
.push_back(other_c
);
2285 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2287 width_
+= signal
.width_
;
2291 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2295 cover("kernel.rtlil.sigspec.append_bit.packed");
2297 if (chunks_
.size() == 0)
2298 chunks_
.push_back(bit
);
2300 if (bit
.wire
== NULL
)
2301 if (chunks_
.back().wire
== NULL
) {
2302 chunks_
.back().data
.bits
.push_back(bit
.data
);
2303 chunks_
.back().width
++;
2305 chunks_
.push_back(bit
);
2307 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2308 chunks_
.back().width
++;
2310 chunks_
.push_back(bit
);
2314 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2315 bits_
.push_back(bit
);
2322 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2324 cover("kernel.rtlil.sigspec.extend");
2329 remove(width
, width_
- width
);
2331 if (width_
< width
) {
2332 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2333 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2334 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2335 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2336 while (width_
< width
)
2341 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2343 cover("kernel.rtlil.sigspec.extend_u0");
2348 remove(width
, width_
- width
);
2350 if (width_
< width
) {
2351 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2353 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2354 while (width_
< width
)
2360 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2362 cover("kernel.rtlil.sigspec.repeat");
2365 for (int i
= 0; i
< num
; i
++)
2371 void RTLIL::SigSpec::check() const
2375 cover("kernel.rtlil.sigspec.check.skip");
2379 cover("kernel.rtlil.sigspec.check.packed");
2382 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2383 const RTLIL::SigChunk chunk
= chunks_
[i
];
2384 if (chunk
.wire
== NULL
) {
2386 log_assert(chunks_
[i
-1].wire
!= NULL
);
2387 log_assert(chunk
.offset
== 0);
2388 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2390 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2391 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2392 log_assert(chunk
.offset
>= 0);
2393 log_assert(chunk
.width
>= 0);
2394 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2395 log_assert(chunk
.data
.bits
.size() == 0);
2399 log_assert(w
== width_
);
2400 log_assert(bits_
.empty());
2404 cover("kernel.rtlil.sigspec.check.unpacked");
2406 log_assert(width_
== SIZE(bits_
));
2407 log_assert(chunks_
.empty());
2412 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2414 cover("kernel.rtlil.sigspec.comp_lt");
2419 if (width_
!= other
.width_
)
2420 return width_
< other
.width_
;
2425 if (chunks_
.size() != other
.chunks_
.size())
2426 return chunks_
.size() < other
.chunks_
.size();
2431 if (hash_
!= other
.hash_
)
2432 return hash_
< other
.hash_
;
2434 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2435 if (chunks_
[i
] != other
.chunks_
[i
]) {
2436 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2437 return chunks_
[i
] < other
.chunks_
[i
];
2440 cover("kernel.rtlil.sigspec.comp_lt.equal");
2444 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2446 cover("kernel.rtlil.sigspec.comp_eq");
2451 if (width_
!= other
.width_
)
2457 if (chunks_
.size() != chunks_
.size())
2463 if (hash_
!= other
.hash_
)
2466 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2467 if (chunks_
[i
] != other
.chunks_
[i
]) {
2468 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2472 cover("kernel.rtlil.sigspec.comp_eq.equal");
2476 bool RTLIL::SigSpec::is_wire() const
2478 cover("kernel.rtlil.sigspec.is_wire");
2481 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2484 bool RTLIL::SigSpec::is_chunk() const
2486 cover("kernel.rtlil.sigspec.is_chunk");
2489 return SIZE(chunks_
) == 1;
2492 bool RTLIL::SigSpec::is_fully_const() const
2494 cover("kernel.rtlil.sigspec.is_fully_const");
2497 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2498 if (it
->width
> 0 && it
->wire
!= NULL
)
2503 bool RTLIL::SigSpec::is_fully_def() const
2505 cover("kernel.rtlil.sigspec.is_fully_def");
2508 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2509 if (it
->width
> 0 && it
->wire
!= NULL
)
2511 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2512 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2518 bool RTLIL::SigSpec::is_fully_undef() const
2520 cover("kernel.rtlil.sigspec.is_fully_undef");
2523 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2524 if (it
->width
> 0 && it
->wire
!= NULL
)
2526 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2527 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2533 bool RTLIL::SigSpec::has_marked_bits() const
2535 cover("kernel.rtlil.sigspec.has_marked_bits");
2538 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2539 if (it
->width
> 0 && it
->wire
== NULL
) {
2540 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2541 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2547 bool RTLIL::SigSpec::as_bool() const
2549 cover("kernel.rtlil.sigspec.as_bool");
2552 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2554 return chunks_
[0].data
.as_bool();
2558 int RTLIL::SigSpec::as_int() const
2560 cover("kernel.rtlil.sigspec.as_int");
2563 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2565 return chunks_
[0].data
.as_int();
2569 std::string
RTLIL::SigSpec::as_string() const
2571 cover("kernel.rtlil.sigspec.as_string");
2575 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2576 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2577 if (chunk
.wire
!= NULL
)
2578 for (int j
= 0; j
< chunk
.width
; j
++)
2581 str
+= chunk
.data
.as_string();
2586 RTLIL::Const
RTLIL::SigSpec::as_const() const
2588 cover("kernel.rtlil.sigspec.as_const");
2591 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2593 return chunks_
[0].data
;
2594 return RTLIL::Const();
2597 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2599 cover("kernel.rtlil.sigspec.as_wire");
2602 log_assert(is_wire());
2603 return chunks_
[0].wire
;
2606 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2608 cover("kernel.rtlil.sigspec.as_chunk");
2611 log_assert(is_chunk());
2615 bool RTLIL::SigSpec::match(std::string pattern
) const
2617 cover("kernel.rtlil.sigspec.match");
2620 std::string str
= as_string();
2621 log_assert(pattern
.size() == str
.size());
2623 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2624 if (pattern
[i
] == ' ')
2626 if (pattern
[i
] == '*') {
2627 if (str
[i
] != 'z' && str
[i
] != 'x')
2631 if (pattern
[i
] != str
[i
])
2638 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2640 cover("kernel.rtlil.sigspec.to_sigbit_set");
2643 std::set
<RTLIL::SigBit
> sigbits
;
2644 for (auto &c
: chunks_
)
2645 for (int i
= 0; i
< c
.width
; i
++)
2646 sigbits
.insert(RTLIL::SigBit(c
, i
));
2650 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2652 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2658 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2660 cover("kernel.rtlil.sigspec.to_single_sigbit");
2663 log_assert(width_
== 1);
2664 for (auto &c
: chunks_
)
2666 return RTLIL::SigBit(c
);
2670 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2672 size_t start
= 0, end
= 0;
2673 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2674 tokens
.push_back(text
.substr(start
, end
- start
));
2677 tokens
.push_back(text
.substr(start
));
2680 static int sigspec_parse_get_dummy_line_num()
2685 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2687 cover("kernel.rtlil.sigspec.parse");
2689 std::vector
<std::string
> tokens
;
2690 sigspec_parse_split(tokens
, str
, ',');
2692 sig
= RTLIL::SigSpec();
2693 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2695 std::string netname
= tokens
[tokidx
];
2696 std::string indices
;
2698 if (netname
.size() == 0)
2701 if ('0' <= netname
[0] && netname
[0] <= '9') {
2702 cover("kernel.rtlil.sigspec.parse.const");
2703 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2704 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2707 sig
.append(RTLIL::Const(ast
->bits
));
2715 cover("kernel.rtlil.sigspec.parse.net");
2717 if (netname
[0] != '$' && netname
[0] != '\\')
2718 netname
= "\\" + netname
;
2720 if (module
->wires_
.count(netname
) == 0) {
2721 size_t indices_pos
= netname
.size()-1;
2722 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2725 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2726 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2728 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2730 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2731 indices
= netname
.substr(indices_pos
);
2732 netname
= netname
.substr(0, indices_pos
);
2737 if (module
->wires_
.count(netname
) == 0)
2740 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2741 if (!indices
.empty()) {
2742 std::vector
<std::string
> index_tokens
;
2743 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2744 if (index_tokens
.size() == 1) {
2745 cover("kernel.rtlil.sigspec.parse.bit_sel");
2746 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2748 cover("kernel.rtlil.sigspec.parse.part_sel");
2749 int a
= atoi(index_tokens
.at(0).c_str());
2750 int b
= atoi(index_tokens
.at(1).c_str());
2755 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2764 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2766 if (str
.empty() || str
[0] != '@')
2767 return parse(sig
, module
, str
);
2769 cover("kernel.rtlil.sigspec.parse.sel");
2771 str
= RTLIL::escape_id(str
.substr(1));
2772 if (design
->selection_vars
.count(str
) == 0)
2775 sig
= RTLIL::SigSpec();
2776 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2777 for (auto &it
: module
->wires_
)
2778 if (sel
.selected_member(module
->name
, it
.first
))
2779 sig
.append(it
.second
);
2784 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2787 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2788 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2793 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2794 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2798 if (lhs
.chunks_
.size() == 1) {
2799 char *p
= (char*)str
.c_str(), *endptr
;
2800 long long int val
= strtoll(p
, &endptr
, 10);
2801 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2802 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2803 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2808 return parse(sig
, module
, str
);
2811 RTLIL::CaseRule::~CaseRule()
2813 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2817 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2819 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2820 new_caserule
->compare
= compare
;
2821 new_caserule
->actions
= actions
;
2822 for (auto &it
: switches
)
2823 new_caserule
->switches
.push_back(it
->clone());
2824 return new_caserule
;
2827 RTLIL::SwitchRule::~SwitchRule()
2829 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2833 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2835 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2836 new_switchrule
->signal
= signal
;
2837 new_switchrule
->attributes
= attributes
;
2838 for (auto &it
: cases
)
2839 new_switchrule
->cases
.push_back(it
->clone());
2840 return new_switchrule
;
2844 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2846 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2847 new_syncrule
->type
= type
;
2848 new_syncrule
->signal
= signal
;
2849 new_syncrule
->actions
= actions
;
2850 return new_syncrule
;
2853 RTLIL::Process::~Process()
2855 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2859 RTLIL::Process
*RTLIL::Process::clone() const
2861 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2863 new_proc
->name
= name
;
2864 new_proc
->attributes
= attributes
;
2866 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2867 new_proc
->root_case
= *rc_ptr
;
2868 rc_ptr
->switches
.clear();
2871 for (auto &it
: syncs
)
2872 new_proc
->syncs
.push_back(it
->clone());