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"
31 flags
= RTLIL::CONST_FLAG_NONE
;
34 RTLIL::Const::Const(std::string str
)
36 flags
= RTLIL::CONST_FLAG_STRING
;
37 for (int i
= str
.size()-1; i
>= 0; i
--) {
38 unsigned char ch
= str
[i
];
39 for (int j
= 0; j
< 8; j
++) {
40 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
46 RTLIL::Const::Const(int val
, int width
)
48 flags
= RTLIL::CONST_FLAG_NONE
;
49 for (int i
= 0; i
< width
; i
++) {
50 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
55 RTLIL::Const::Const(RTLIL::State bit
, int width
)
57 flags
= RTLIL::CONST_FLAG_NONE
;
58 for (int i
= 0; i
< width
; i
++)
62 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
64 if (bits
.size() != other
.bits
.size())
65 return bits
.size() < other
.bits
.size();
66 for (size_t i
= 0; i
< bits
.size(); i
++)
67 if (bits
[i
] != other
.bits
[i
])
68 return bits
[i
] < other
.bits
[i
];
72 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
74 return bits
== other
.bits
;
77 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
79 return bits
!= other
.bits
;
82 bool RTLIL::Const::as_bool() const
84 for (size_t i
= 0; i
< bits
.size(); i
++)
85 if (bits
[i
] == RTLIL::S1
)
90 int RTLIL::Const::as_int() const
93 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
94 if (bits
[i
] == RTLIL::S1
)
99 std::string
RTLIL::Const::as_string() const
102 for (size_t i
= bits
.size(); i
> 0; i
--)
104 case S0
: ret
+= "0"; break;
105 case S1
: ret
+= "1"; break;
106 case Sx
: ret
+= "x"; break;
107 case Sz
: ret
+= "z"; break;
108 case Sa
: ret
+= "-"; break;
109 case Sm
: ret
+= "m"; break;
114 std::string
RTLIL::Const::decode_string() const
117 std::vector
<char> string_chars
;
118 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
120 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
121 if (bits
[i
+ j
] == RTLIL::State::S1
)
124 string_chars
.push_back(ch
);
126 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
127 string
+= string_chars
[i
];
131 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
135 if (selected_modules
.count(mod_name
) > 0)
137 if (selected_members
.count(mod_name
) > 0)
142 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
146 if (selected_modules
.count(mod_name
) > 0)
151 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
155 if (selected_modules
.count(mod_name
) > 0)
157 if (selected_members
.count(mod_name
) > 0)
158 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
163 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
165 if (full_selection
) {
166 selected_modules
.clear();
167 selected_members
.clear();
171 std::vector
<RTLIL::IdString
> del_list
, add_list
;
174 for (auto mod_name
: selected_modules
) {
175 if (design
->modules_
.count(mod_name
) == 0)
176 del_list
.push_back(mod_name
);
177 selected_members
.erase(mod_name
);
179 for (auto mod_name
: del_list
)
180 selected_modules
.erase(mod_name
);
183 for (auto &it
: selected_members
)
184 if (design
->modules_
.count(it
.first
) == 0)
185 del_list
.push_back(it
.first
);
186 for (auto mod_name
: del_list
)
187 selected_members
.erase(mod_name
);
189 for (auto &it
: selected_members
) {
191 for (auto memb_name
: it
.second
)
192 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
193 del_list
.push_back(memb_name
);
194 for (auto memb_name
: del_list
)
195 it
.second
.erase(memb_name
);
200 for (auto &it
: selected_members
)
201 if (it
.second
.size() == 0)
202 del_list
.push_back(it
.first
);
203 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
204 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
205 add_list
.push_back(it
.first
);
206 for (auto mod_name
: del_list
)
207 selected_members
.erase(mod_name
);
208 for (auto mod_name
: add_list
) {
209 selected_members
.erase(mod_name
);
210 selected_modules
.insert(mod_name
);
213 if (selected_modules
.size() == design
->modules_
.size()) {
214 full_selection
= true;
215 selected_modules
.clear();
216 selected_members
.clear();
220 RTLIL::Design::Design()
222 refcount_modules_
= 0;
225 RTLIL::Design::~Design()
227 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
231 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
233 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
236 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
238 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
241 void RTLIL::Design::add(RTLIL::Module
*module
)
243 log_assert(modules_
.count(module
->name
) == 0);
244 log_assert(refcount_modules_
== 0);
245 modules_
[module
->name
] = module
;
246 module
->design
= this;
248 for (auto mon
: monitors
)
249 mon
->notify_module_add(module
);
252 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
254 log_assert(modules_
.count(name
) == 0);
255 log_assert(refcount_modules_
== 0);
257 RTLIL::Module
*module
= new RTLIL::Module
;
258 modules_
[name
] = module
;
259 module
->design
= this;
262 for (auto mon
: monitors
)
263 mon
->notify_module_add(module
);
268 void RTLIL::Design::remove(RTLIL::Module
*module
)
270 for (auto mon
: monitors
)
271 mon
->notify_module_del(module
);
273 log_assert(modules_
.at(module
->name
) == module
);
274 modules_
.erase(module
->name
);
278 void RTLIL::Design::check()
281 for (auto &it
: modules_
) {
282 log_assert(this == it
.second
->design
);
283 log_assert(it
.first
== it
.second
->name
);
284 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
290 void RTLIL::Design::optimize()
292 for (auto &it
: modules_
)
293 it
.second
->optimize();
294 for (auto &it
: selection_stack
)
296 for (auto &it
: selection_vars
)
297 it
.second
.optimize(this);
300 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
302 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
304 if (selection_stack
.size() == 0)
306 return selection_stack
.back().selected_module(mod_name
);
309 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
311 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
313 if (selection_stack
.size() == 0)
315 return selection_stack
.back().selected_whole_module(mod_name
);
318 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
320 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
322 if (selection_stack
.size() == 0)
324 return selection_stack
.back().selected_member(mod_name
, memb_name
);
327 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
329 return selected_module(mod
->name
);
332 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
334 return selected_whole_module(mod
->name
);
337 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
339 std::vector
<RTLIL::Module
*> result
;
340 result
.reserve(modules_
.size());
341 for (auto &it
: modules_
)
342 if (selected_module(it
.first
))
343 result
.push_back(it
.second
);
347 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
349 std::vector
<RTLIL::Module
*> result
;
350 result
.reserve(modules_
.size());
351 for (auto &it
: modules_
)
352 if (selected_whole_module(it
.first
))
353 result
.push_back(it
.second
);
357 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
359 std::vector
<RTLIL::Module
*> result
;
360 result
.reserve(modules_
.size());
361 for (auto &it
: modules_
)
362 if (selected_whole_module(it
.first
))
363 result
.push_back(it
.second
);
364 else if (selected_module(it
.first
))
365 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
369 RTLIL::Module::Module()
376 RTLIL::Module::~Module()
378 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
380 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
382 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
384 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
388 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
390 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
393 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
395 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
400 struct InternalCellChecker
402 RTLIL::Module
*module
;
404 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
406 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
408 void error(int linenr
)
413 FILE *f
= open_memstream(&ptr
, &size
);
414 ILANG_BACKEND::dump_cell(f
, " ", cell
);
418 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
419 module
? module
->name
.c_str() : "", module
? "." : "",
420 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
423 int param(const char *name
)
425 if (cell
->parameters
.count(name
) == 0)
427 expected_params
.insert(name
);
428 return cell
->parameters
.at(name
).as_int();
431 int param_bool(const char *name
)
434 if (cell
->parameters
.at(name
).bits
.size() > 32)
436 if (v
!= 0 && v
!= 1)
441 void param_bits(const char *name
, int width
)
444 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
448 void port(const char *name
, int width
)
450 if (!cell
->has(name
))
452 if (cell
->get(name
).size() != width
)
454 expected_ports
.insert(name
);
457 void check_expected(bool check_matched_sign
= true)
459 for (auto ¶
: cell
->parameters
)
460 if (expected_params
.count(para
.first
) == 0)
462 for (auto &conn
: cell
->connections())
463 if (expected_ports
.count(conn
.first
) == 0)
466 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
467 bool a_is_signed
= param("\\A_SIGNED") != 0;
468 bool b_is_signed
= param("\\B_SIGNED") != 0;
469 if (a_is_signed
!= b_is_signed
)
474 void check_gate(const char *ports
)
476 if (cell
->parameters
.size() != 0)
479 for (const char *p
= ports
; *p
; p
++) {
480 char portname
[3] = { '\\', *p
, 0 };
481 if (!cell
->has(portname
))
483 if (cell
->get(portname
).size() != 1)
487 for (auto &conn
: cell
->connections()) {
488 if (conn
.first
.size() != 2 || conn
.first
.at(0) != '\\')
490 if (strchr(ports
, conn
.first
.at(1)) == NULL
)
497 if (cell
->type
[0] != '$' || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
498 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
501 if (cell
->type
== "$not" || cell
->type
== "$pos" || cell
->type
== "$bu0" || cell
->type
== "$neg") {
502 param_bool("\\A_SIGNED");
503 port("\\A", param("\\A_WIDTH"));
504 port("\\Y", param("\\Y_WIDTH"));
509 if (cell
->type
== "$and" || cell
->type
== "$or" || cell
->type
== "$xor" || cell
->type
== "$xnor") {
510 param_bool("\\A_SIGNED");
511 param_bool("\\B_SIGNED");
512 port("\\A", param("\\A_WIDTH"));
513 port("\\B", param("\\B_WIDTH"));
514 port("\\Y", param("\\Y_WIDTH"));
519 if (cell
->type
== "$reduce_and" || cell
->type
== "$reduce_or" || cell
->type
== "$reduce_xor" ||
520 cell
->type
== "$reduce_xnor" || cell
->type
== "$reduce_bool") {
521 param_bool("\\A_SIGNED");
522 port("\\A", param("\\A_WIDTH"));
523 port("\\Y", param("\\Y_WIDTH"));
528 if (cell
->type
== "$shl" || cell
->type
== "$shr" || cell
->type
== "$sshl" || cell
->type
== "$sshr" ||
529 cell
->type
== "$shift" || cell
->type
== "$shiftx") {
530 param_bool("\\A_SIGNED");
531 param_bool("\\B_SIGNED");
532 port("\\A", param("\\A_WIDTH"));
533 port("\\B", param("\\B_WIDTH"));
534 port("\\Y", param("\\Y_WIDTH"));
535 check_expected(false);
539 if (cell
->type
== "$lt" || cell
->type
== "$le" || cell
->type
== "$eq" || cell
->type
== "$ne" ||
540 cell
->type
== "$eqx" || cell
->type
== "$nex" || cell
->type
== "$ge" || cell
->type
== "$gt") {
541 param_bool("\\A_SIGNED");
542 param_bool("\\B_SIGNED");
543 port("\\A", param("\\A_WIDTH"));
544 port("\\B", param("\\B_WIDTH"));
545 port("\\Y", param("\\Y_WIDTH"));
550 if (cell
->type
== "$add" || cell
->type
== "$sub" || cell
->type
== "$mul" || cell
->type
== "$div" ||
551 cell
->type
== "$mod" || cell
->type
== "$pow") {
552 param_bool("\\A_SIGNED");
553 param_bool("\\B_SIGNED");
554 port("\\A", param("\\A_WIDTH"));
555 port("\\B", param("\\B_WIDTH"));
556 port("\\Y", param("\\Y_WIDTH"));
557 check_expected(cell
->type
!= "$pow");
561 if (cell
->type
== "$logic_not") {
562 param_bool("\\A_SIGNED");
563 port("\\A", param("\\A_WIDTH"));
564 port("\\Y", param("\\Y_WIDTH"));
569 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
570 param_bool("\\A_SIGNED");
571 param_bool("\\B_SIGNED");
572 port("\\A", param("\\A_WIDTH"));
573 port("\\B", param("\\B_WIDTH"));
574 port("\\Y", param("\\Y_WIDTH"));
575 check_expected(false);
579 if (cell
->type
== "$slice") {
581 port("\\A", param("\\A_WIDTH"));
582 port("\\Y", param("\\Y_WIDTH"));
583 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
589 if (cell
->type
== "$concat") {
590 port("\\A", param("\\A_WIDTH"));
591 port("\\B", param("\\B_WIDTH"));
592 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
597 if (cell
->type
== "$mux") {
598 port("\\A", param("\\WIDTH"));
599 port("\\B", param("\\WIDTH"));
601 port("\\Y", param("\\WIDTH"));
606 if (cell
->type
== "$pmux" || cell
->type
== "$safe_pmux") {
607 port("\\A", param("\\WIDTH"));
608 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
609 port("\\S", param("\\S_WIDTH"));
610 port("\\Y", param("\\WIDTH"));
615 if (cell
->type
== "$lut") {
617 port("\\I", param("\\WIDTH"));
623 if (cell
->type
== "$sr") {
624 param_bool("\\SET_POLARITY");
625 param_bool("\\CLR_POLARITY");
626 port("\\SET", param("\\WIDTH"));
627 port("\\CLR", param("\\WIDTH"));
628 port("\\Q", param("\\WIDTH"));
633 if (cell
->type
== "$dff") {
634 param_bool("\\CLK_POLARITY");
636 port("\\D", param("\\WIDTH"));
637 port("\\Q", param("\\WIDTH"));
642 if (cell
->type
== "$dffsr") {
643 param_bool("\\CLK_POLARITY");
644 param_bool("\\SET_POLARITY");
645 param_bool("\\CLR_POLARITY");
647 port("\\SET", param("\\WIDTH"));
648 port("\\CLR", param("\\WIDTH"));
649 port("\\D", param("\\WIDTH"));
650 port("\\Q", param("\\WIDTH"));
655 if (cell
->type
== "$adff") {
656 param_bool("\\CLK_POLARITY");
657 param_bool("\\ARST_POLARITY");
658 param_bits("\\ARST_VALUE", param("\\WIDTH"));
661 port("\\D", param("\\WIDTH"));
662 port("\\Q", param("\\WIDTH"));
667 if (cell
->type
== "$dlatch") {
668 param_bool("\\EN_POLARITY");
670 port("\\D", param("\\WIDTH"));
671 port("\\Q", param("\\WIDTH"));
676 if (cell
->type
== "$dlatchsr") {
677 param_bool("\\EN_POLARITY");
678 param_bool("\\SET_POLARITY");
679 param_bool("\\CLR_POLARITY");
681 port("\\SET", param("\\WIDTH"));
682 port("\\CLR", param("\\WIDTH"));
683 port("\\D", param("\\WIDTH"));
684 port("\\Q", param("\\WIDTH"));
689 if (cell
->type
== "$fsm") {
691 param_bool("\\CLK_POLARITY");
692 param_bool("\\ARST_POLARITY");
693 param("\\STATE_BITS");
694 param("\\STATE_NUM");
695 param("\\STATE_NUM_LOG2");
696 param("\\STATE_RST");
697 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
698 param("\\TRANS_NUM");
699 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
702 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
703 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
708 if (cell
->type
== "$memrd") {
710 param_bool("\\CLK_ENABLE");
711 param_bool("\\CLK_POLARITY");
712 param_bool("\\TRANSPARENT");
714 port("\\ADDR", param("\\ABITS"));
715 port("\\DATA", param("\\WIDTH"));
720 if (cell
->type
== "$memwr") {
722 param_bool("\\CLK_ENABLE");
723 param_bool("\\CLK_POLARITY");
726 port("\\EN", param("\\WIDTH"));
727 port("\\ADDR", param("\\ABITS"));
728 port("\\DATA", param("\\WIDTH"));
733 if (cell
->type
== "$mem") {
737 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
738 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
739 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
740 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
741 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
742 port("\\RD_CLK", param("\\RD_PORTS"));
743 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
744 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
745 port("\\WR_CLK", param("\\WR_PORTS"));
746 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
747 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
748 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
753 if (cell
->type
== "$assert") {
760 if (cell
->type
== "$_INV_") { check_gate("AY"); return; }
761 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
762 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
763 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
764 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
766 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
767 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
768 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
769 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
771 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
772 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
774 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
775 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
776 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
777 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
778 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
779 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
780 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
781 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
783 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
784 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
785 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
786 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
787 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
788 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
789 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
790 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
792 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
793 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
795 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
796 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
797 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
798 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
799 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
800 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
801 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
802 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
810 void RTLIL::Module::check()
813 for (auto &it
: wires_
) {
814 log_assert(this == it
.second
->module
);
815 log_assert(it
.first
== it
.second
->name
);
816 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
817 log_assert(it
.second
->width
>= 0);
818 log_assert(it
.second
->port_id
>= 0);
819 for (auto &it2
: it
.second
->attributes
) {
820 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
824 for (auto &it
: memories
) {
825 log_assert(it
.first
== it
.second
->name
);
826 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
827 log_assert(it
.second
->width
>= 0);
828 log_assert(it
.second
->size
>= 0);
829 for (auto &it2
: it
.second
->attributes
) {
830 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
834 for (auto &it
: cells_
) {
835 log_assert(this == it
.second
->module
);
836 log_assert(it
.first
== it
.second
->name
);
837 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
838 log_assert(it
.second
->type
.size() > 0 && (it
.second
->type
[0] == '\\' || it
.second
->type
[0] == '$'));
839 for (auto &it2
: it
.second
->connections()) {
840 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
843 for (auto &it2
: it
.second
->attributes
) {
844 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
846 for (auto &it2
: it
.second
->parameters
) {
847 log_assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
849 InternalCellChecker
checker(this, it
.second
);
853 for (auto &it
: processes
) {
854 log_assert(it
.first
== it
.second
->name
);
855 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
856 // FIXME: More checks here..
859 for (auto &it
: connections_
) {
860 log_assert(it
.first
.size() == it
.second
.size());
865 for (auto &it
: attributes
) {
866 log_assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
871 void RTLIL::Module::optimize()
875 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
877 log_assert(new_mod
->refcount_wires_
== 0);
878 log_assert(new_mod
->refcount_cells_
== 0);
880 new_mod
->connections_
= connections_
;
881 new_mod
->attributes
= attributes
;
883 for (auto &it
: wires_
)
884 new_mod
->addWire(it
.first
, it
.second
);
886 for (auto &it
: memories
)
887 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
889 for (auto &it
: cells_
)
890 new_mod
->addCell(it
.first
, it
.second
);
892 for (auto &it
: processes
)
893 new_mod
->processes
[it
.first
] = it
.second
->clone();
895 struct RewriteSigSpecWorker
898 void operator()(RTLIL::SigSpec
&sig
)
900 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
901 for (auto &c
: chunks
)
903 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
908 RewriteSigSpecWorker rewriteSigSpecWorker
;
909 rewriteSigSpecWorker
.mod
= new_mod
;
910 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
913 RTLIL::Module
*RTLIL::Module::clone() const
915 RTLIL::Module
*new_mod
= new RTLIL::Module
;
916 new_mod
->name
= name
;
921 bool RTLIL::Module::has_memories() const
923 return !memories
.empty();
926 bool RTLIL::Module::has_processes() const
928 return !processes
.empty();
931 bool RTLIL::Module::has_memories_warn() const
933 if (!memories
.empty())
934 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
935 return !memories
.empty();
938 bool RTLIL::Module::has_processes_warn() const
940 if (!processes
.empty())
941 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
942 return !processes
.empty();
945 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
947 std::vector
<RTLIL::Wire
*> result
;
948 result
.reserve(wires_
.size());
949 for (auto &it
: wires_
)
950 if (design
->selected(this, it
.second
))
951 result
.push_back(it
.second
);
955 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
957 std::vector
<RTLIL::Cell
*> result
;
958 result
.reserve(wires_
.size());
959 for (auto &it
: cells_
)
960 if (design
->selected(this, it
.second
))
961 result
.push_back(it
.second
);
965 void RTLIL::Module::add(RTLIL::Wire
*wire
)
967 log_assert(!wire
->name
.empty());
968 log_assert(count_id(wire
->name
) == 0);
969 log_assert(refcount_wires_
== 0);
970 wires_
[wire
->name
] = wire
;
974 void RTLIL::Module::add(RTLIL::Cell
*cell
)
976 log_assert(!cell
->name
.empty());
977 log_assert(count_id(cell
->name
) == 0);
978 log_assert(refcount_cells_
== 0);
979 cells_
[cell
->name
] = cell
;
984 struct DeleteWireWorker
986 RTLIL::Module
*module
;
987 const std::set
<RTLIL::Wire
*> *wires_p
;
989 void operator()(RTLIL::SigSpec
&sig
) {
990 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
991 for (auto &c
: chunks
)
992 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
993 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1002 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1004 std::set
<RTLIL::Wire
*> wires_
;
1005 wires_
.insert(wire
);
1010 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1012 log_assert(refcount_wires_
== 0);
1014 DeleteWireWorker delete_wire_worker
;
1015 delete_wire_worker
.module
= this;
1016 delete_wire_worker
.wires_p
= &wires
;
1017 rewrite_sigspecs(delete_wire_worker
);
1019 for (auto &it
: wires
) {
1020 log_assert(wires_
.count(it
->name
) != 0);
1021 wires_
.erase(it
->name
);
1026 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1028 log_assert(cells_
.count(cell
->name
) != 0);
1029 log_assert(refcount_cells_
== 0);
1030 cells_
.erase(cell
->name
);
1034 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1036 log_assert(wires_
[wire
->name
] == wire
);
1037 log_assert(refcount_wires_
== 0);
1038 wires_
.erase(wire
->name
);
1039 wire
->name
= new_name
;
1043 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1045 log_assert(cells_
[cell
->name
] == cell
);
1046 log_assert(refcount_wires_
== 0);
1047 cells_
.erase(cell
->name
);
1048 cell
->name
= new_name
;
1052 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1054 log_assert(count_id(old_name
) != 0);
1055 if (wires_
.count(old_name
))
1056 rename(wires_
.at(old_name
), new_name
);
1057 else if (cells_
.count(old_name
))
1058 rename(cells_
.at(old_name
), new_name
);
1063 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1065 if (a
->port_id
&& !b
->port_id
)
1067 if (!a
->port_id
&& b
->port_id
)
1070 if (a
->port_id
== b
->port_id
)
1071 return a
->name
< b
->name
;
1072 return a
->port_id
< b
->port_id
;
1075 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1077 for (auto mon
: monitors
)
1078 mon
->notify_connect(this, conn
);
1081 for (auto mon
: design
->monitors
)
1082 mon
->notify_connect(this, conn
);
1084 connections_
.push_back(conn
);
1087 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1089 connect(RTLIL::SigSig(lhs
, rhs
));
1092 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1094 for (auto mon
: monitors
)
1095 mon
->notify_new_connections(this, new_conn
);
1098 for (auto mon
: design
->monitors
)
1099 mon
->notify_new_connections(this, new_conn
);
1101 connections_
= new_conn
;
1104 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1106 return connections_
;
1109 void RTLIL::Module::fixup_ports()
1111 std::vector
<RTLIL::Wire
*> all_ports
;
1113 for (auto &w
: wires_
)
1114 if (w
.second
->port_input
|| w
.second
->port_output
)
1115 all_ports
.push_back(w
.second
);
1117 w
.second
->port_id
= 0;
1119 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1120 for (size_t i
= 0; i
< all_ports
.size(); i
++)
1121 all_ports
[i
]->port_id
= i
+1;
1124 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1126 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1128 wire
->width
= width
;
1133 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1135 RTLIL::Wire
*wire
= addWire(name
);
1136 wire
->width
= other
->width
;
1137 wire
->start_offset
= other
->start_offset
;
1138 wire
->port_id
= other
->port_id
;
1139 wire
->port_input
= other
->port_input
;
1140 wire
->port_output
= other
->port_output
;
1141 wire
->upto
= other
->upto
;
1142 wire
->attributes
= other
->attributes
;
1146 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1148 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1155 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1157 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1158 cell
->connections_
= other
->connections_
;
1159 cell
->parameters
= other
->parameters
;
1160 cell
->attributes
= other
->attributes
;
1164 #define DEF_METHOD(_func, _y_size, _type) \
1165 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1166 RTLIL::Cell *cell = addCell(name, _type); \
1167 cell->parameters["\\A_SIGNED"] = is_signed; \
1168 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1169 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1170 cell->set("\\A", sig_a); \
1171 cell->set("\\Y", sig_y); \
1174 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1175 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1176 add ## _func(name, sig_a, sig_y, is_signed); \
1179 DEF_METHOD(Not
, sig_a
.size(), "$not")
1180 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1181 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1182 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1183 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1184 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1185 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1186 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1187 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1188 DEF_METHOD(LogicNot
, 1, "$logic_not")
1191 #define DEF_METHOD(_func, _y_size, _type) \
1192 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1193 RTLIL::Cell *cell = addCell(name, _type); \
1194 cell->parameters["\\A_SIGNED"] = is_signed; \
1195 cell->parameters["\\B_SIGNED"] = is_signed; \
1196 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1197 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1198 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1199 cell->set("\\A", sig_a); \
1200 cell->set("\\B", sig_b); \
1201 cell->set("\\Y", sig_y); \
1204 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1205 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1206 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1209 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1210 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1211 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1212 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1213 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1214 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1215 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1216 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1217 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1218 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1219 DEF_METHOD(Lt
, 1, "$lt")
1220 DEF_METHOD(Le
, 1, "$le")
1221 DEF_METHOD(Eq
, 1, "$eq")
1222 DEF_METHOD(Ne
, 1, "$ne")
1223 DEF_METHOD(Eqx
, 1, "$eqx")
1224 DEF_METHOD(Nex
, 1, "$nex")
1225 DEF_METHOD(Ge
, 1, "$ge")
1226 DEF_METHOD(Gt
, 1, "$gt")
1227 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1228 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1229 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1230 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1231 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1232 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1233 DEF_METHOD(LogicOr
, 1, "$logic_or")
1236 #define DEF_METHOD(_func, _type, _pmux) \
1237 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) { \
1238 RTLIL::Cell *cell = addCell(name, _type); \
1239 cell->parameters["\\WIDTH"] = sig_a.size(); \
1240 cell->parameters["\\WIDTH"] = sig_b.size(); \
1241 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1242 cell->set("\\A", sig_a); \
1243 cell->set("\\B", sig_b); \
1244 cell->set("\\S", sig_s); \
1245 cell->set("\\Y", sig_y); \
1248 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1249 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1250 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1253 DEF_METHOD(Mux
, "$mux", 0)
1254 DEF_METHOD(Pmux
, "$pmux", 1)
1255 DEF_METHOD(SafePmux
, "$safe_pmux", 1)
1258 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1259 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1260 RTLIL::Cell *cell = addCell(name, _type); \
1261 cell->set("\\" #_P1, sig1); \
1262 cell->set("\\" #_P2, sig2); \
1265 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
1266 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
1267 add ## _func(name, sig1, sig2); \
1270 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1271 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1272 RTLIL::Cell *cell = addCell(name, _type); \
1273 cell->set("\\" #_P1, sig1); \
1274 cell->set("\\" #_P2, sig2); \
1275 cell->set("\\" #_P3, sig3); \
1278 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1279 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1280 add ## _func(name, sig1, sig2, sig3); \
1283 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1284 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1285 RTLIL::Cell *cell = addCell(name, _type); \
1286 cell->set("\\" #_P1, sig1); \
1287 cell->set("\\" #_P2, sig2); \
1288 cell->set("\\" #_P3, sig3); \
1289 cell->set("\\" #_P4, sig4); \
1292 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1293 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1294 add ## _func(name, sig1, sig2, sig3, sig4); \
1297 DEF_METHOD_2(InvGate
, "$_INV_", A
, Y
)
1298 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1299 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1300 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1301 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1306 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
)
1308 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1309 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1310 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1311 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1312 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1313 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1314 cell
->set("\\A", sig_a
);
1315 cell
->set("\\B", sig_b
);
1316 cell
->set("\\Y", sig_y
);
1320 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1322 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1323 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1324 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1325 cell
->parameters
["\\OFFSET"] = offset
;
1326 cell
->set("\\A", sig_a
);
1327 cell
->set("\\Y", sig_y
);
1331 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1333 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1334 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1335 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1336 cell
->set("\\A", sig_a
);
1337 cell
->set("\\B", sig_b
);
1338 cell
->set("\\Y", sig_y
);
1342 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1344 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1345 cell
->parameters
["\\LUT"] = lut
;
1346 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1347 cell
->set("\\I", sig_i
);
1348 cell
->set("\\O", sig_o
);
1352 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1354 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1355 cell
->set("\\A", sig_a
);
1356 cell
->set("\\EN", sig_en
);
1360 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
)
1362 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1363 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1364 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1365 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1366 cell
->set("\\SET", sig_set
);
1367 cell
->set("\\CLR", sig_clr
);
1368 cell
->set("\\Q", sig_q
);
1372 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1374 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1375 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1376 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1377 cell
->set("\\CLK", sig_clk
);
1378 cell
->set("\\D", sig_d
);
1379 cell
->set("\\Q", sig_q
);
1383 RTLIL::Cell
* RTLIL::Module::addDffsr(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
= addCell(name
, "$dffsr");
1387 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1388 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1389 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1390 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1391 cell
->set("\\CLK", sig_clk
);
1392 cell
->set("\\SET", sig_set
);
1393 cell
->set("\\CLR", sig_clr
);
1394 cell
->set("\\D", sig_d
);
1395 cell
->set("\\Q", sig_q
);
1399 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1400 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1402 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1403 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1404 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1405 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1406 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1407 cell
->set("\\CLK", sig_clk
);
1408 cell
->set("\\ARST", sig_arst
);
1409 cell
->set("\\D", sig_d
);
1410 cell
->set("\\Q", sig_q
);
1414 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1416 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1417 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1418 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1419 cell
->set("\\EN", sig_en
);
1420 cell
->set("\\D", sig_d
);
1421 cell
->set("\\Q", sig_q
);
1425 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1426 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1428 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1429 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1430 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1431 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1432 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1433 cell
->set("\\EN", sig_en
);
1434 cell
->set("\\SET", sig_set
);
1435 cell
->set("\\CLR", sig_clr
);
1436 cell
->set("\\D", sig_d
);
1437 cell
->set("\\Q", sig_q
);
1441 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1443 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1444 cell
->set("\\C", sig_clk
);
1445 cell
->set("\\D", sig_d
);
1446 cell
->set("\\Q", sig_q
);
1450 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1451 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1453 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1454 cell
->set("\\C", sig_clk
);
1455 cell
->set("\\S", sig_set
);
1456 cell
->set("\\R", sig_clr
);
1457 cell
->set("\\D", sig_d
);
1458 cell
->set("\\Q", sig_q
);
1462 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1463 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1465 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1466 cell
->set("\\C", sig_clk
);
1467 cell
->set("\\R", sig_arst
);
1468 cell
->set("\\D", sig_d
);
1469 cell
->set("\\Q", sig_q
);
1473 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1475 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1476 cell
->set("\\E", sig_en
);
1477 cell
->set("\\D", sig_d
);
1478 cell
->set("\\Q", sig_q
);
1482 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1483 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1485 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1486 cell
->set("\\E", sig_en
);
1487 cell
->set("\\S", sig_set
);
1488 cell
->set("\\R", sig_clr
);
1489 cell
->set("\\D", sig_d
);
1490 cell
->set("\\Q", sig_q
);
1502 port_output
= false;
1506 RTLIL::Memory::Memory()
1512 bool RTLIL::Cell::has(RTLIL::IdString portname
) const
1514 return connections_
.count(portname
) != 0;
1517 void RTLIL::Cell::unset(RTLIL::IdString portname
)
1519 std::pair
<RTLIL::IdString
, RTLIL::SigSpec
> new_conn(portname
, RTLIL::SigSpec());
1521 for (auto mon
: module
->monitors
)
1522 mon
->notify_cell_connect(this, new_conn
);
1525 for (auto mon
: module
->design
->monitors
)
1526 mon
->notify_cell_connect(this, new_conn
);
1528 connections_
.erase(portname
);
1531 void RTLIL::Cell::set(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1533 std::pair
<RTLIL::IdString
, RTLIL::SigSpec
> new_conn(portname
, signal
);
1535 for (auto mon
: module
->monitors
)
1536 mon
->notify_cell_connect(this, new_conn
);
1539 for (auto mon
: module
->design
->monitors
)
1540 mon
->notify_cell_connect(this, new_conn
);
1542 connections_
[portname
] = signal
;
1545 const RTLIL::SigSpec
&RTLIL::Cell::get(RTLIL::IdString portname
) const
1547 return connections_
.at(portname
);
1550 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1552 return connections_
;
1555 void RTLIL::Cell::check()
1558 InternalCellChecker
checker(NULL
, this);
1563 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1565 if (type
[0] != '$' || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1566 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1569 if (type
== "$mux" || type
== "$pmux" || type
== "$safe_pmux")
1571 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1572 if (type
== "$pmux" || type
== "$safe_pmux")
1573 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1578 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1580 if (connections_
.count("\\A")) {
1581 if (signedness_ab
) {
1583 parameters
["\\A_SIGNED"] = true;
1584 else if (parameters
.count("\\A_SIGNED") == 0)
1585 parameters
["\\A_SIGNED"] = false;
1587 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1590 if (connections_
.count("\\B")) {
1591 if (signedness_ab
) {
1593 parameters
["\\B_SIGNED"] = true;
1594 else if (parameters
.count("\\B_SIGNED") == 0)
1595 parameters
["\\B_SIGNED"] = false;
1597 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1600 if (connections_
.count("\\Y"))
1601 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1606 RTLIL::SigChunk::SigChunk()
1613 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1617 width
= data
.bits
.size();
1621 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1623 log_assert(wire
!= nullptr);
1625 this->width
= wire
->width
;
1629 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1631 log_assert(wire
!= nullptr);
1633 this->width
= width
;
1634 this->offset
= offset
;
1637 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1640 data
= RTLIL::Const(str
);
1641 width
= data
.bits
.size();
1645 RTLIL::SigChunk::SigChunk(int val
, int width
)
1648 data
= RTLIL::Const(val
, width
);
1649 this->width
= data
.bits
.size();
1653 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1656 data
= RTLIL::Const(bit
, width
);
1657 this->width
= data
.bits
.size();
1661 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1665 data
= RTLIL::Const(bit
.data
);
1666 offset
= bit
.offset
;
1670 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1672 RTLIL::SigChunk ret
;
1675 ret
.offset
= this->offset
+ offset
;
1678 for (int i
= 0; i
< length
; i
++)
1679 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1685 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1687 if (wire
&& other
.wire
)
1688 if (wire
->name
!= other
.wire
->name
)
1689 return wire
->name
< other
.wire
->name
;
1691 if (wire
!= other
.wire
)
1692 return wire
< other
.wire
;
1694 if (offset
!= other
.offset
)
1695 return offset
< other
.offset
;
1697 if (width
!= other
.width
)
1698 return width
< other
.width
;
1700 return data
.bits
< other
.data
.bits
;
1703 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1705 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1707 if (data
.bits
!= other
.data
.bits
)
1712 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1719 RTLIL::SigSpec::SigSpec()
1725 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1730 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1732 cover("kernel.rtlil.sigspec.init.list");
1737 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1738 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1742 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1744 cover("kernel.rtlil.sigspec.assign");
1746 width_
= other
.width_
;
1747 hash_
= other
.hash_
;
1748 chunks_
= other
.chunks_
;
1751 if (!other
.bits_
.empty())
1753 RTLIL::SigChunk
*last
= NULL
;
1754 int last_end_offset
= 0;
1756 for (auto &bit
: other
.bits_
) {
1757 if (last
&& bit
.wire
== last
->wire
) {
1758 if (bit
.wire
== NULL
) {
1759 last
->data
.bits
.push_back(bit
.data
);
1762 } else if (last_end_offset
== bit
.offset
) {
1768 chunks_
.push_back(bit
);
1769 last
= &chunks_
.back();
1770 last_end_offset
= bit
.offset
+ 1;
1779 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1781 cover("kernel.rtlil.sigspec.init.const");
1783 chunks_
.push_back(RTLIL::SigChunk(value
));
1784 width_
= chunks_
.back().width
;
1789 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1791 cover("kernel.rtlil.sigspec.init.chunk");
1793 chunks_
.push_back(chunk
);
1794 width_
= chunks_
.back().width
;
1799 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1801 cover("kernel.rtlil.sigspec.init.wire");
1803 chunks_
.push_back(RTLIL::SigChunk(wire
));
1804 width_
= chunks_
.back().width
;
1809 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1811 cover("kernel.rtlil.sigspec.init.wire_part");
1813 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1814 width_
= chunks_
.back().width
;
1819 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1821 cover("kernel.rtlil.sigspec.init.str");
1823 chunks_
.push_back(RTLIL::SigChunk(str
));
1824 width_
= chunks_
.back().width
;
1829 RTLIL::SigSpec::SigSpec(int val
, int width
)
1831 cover("kernel.rtlil.sigspec.init.int");
1833 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1839 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1841 cover("kernel.rtlil.sigspec.init.state");
1843 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1849 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1851 cover("kernel.rtlil.sigspec.init.bit");
1853 if (bit
.wire
== NULL
)
1854 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1856 for (int i
= 0; i
< width
; i
++)
1857 chunks_
.push_back(bit
);
1863 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1865 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
1869 for (auto &c
: chunks
)
1874 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1876 cover("kernel.rtlil.sigspec.init.stdvec_bits");
1880 for (auto &bit
: bits
)
1885 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1887 cover("kernel.rtlil.sigspec.init.stdset_bits");
1891 for (auto &bit
: bits
)
1896 void RTLIL::SigSpec::pack() const
1898 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1900 if (that
->bits_
.empty())
1903 cover("kernel.rtlil.sigspec.convert.pack");
1904 log_assert(that
->chunks_
.empty());
1906 std::vector
<RTLIL::SigBit
> old_bits
;
1907 old_bits
.swap(that
->bits_
);
1909 RTLIL::SigChunk
*last
= NULL
;
1910 int last_end_offset
= 0;
1912 for (auto &bit
: old_bits
) {
1913 if (last
&& bit
.wire
== last
->wire
) {
1914 if (bit
.wire
== NULL
) {
1915 last
->data
.bits
.push_back(bit
.data
);
1918 } else if (last_end_offset
== bit
.offset
) {
1924 that
->chunks_
.push_back(bit
);
1925 last
= &that
->chunks_
.back();
1926 last_end_offset
= bit
.offset
+ 1;
1932 void RTLIL::SigSpec::unpack() const
1934 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1936 if (that
->chunks_
.empty())
1939 cover("kernel.rtlil.sigspec.convert.unpack");
1940 log_assert(that
->bits_
.empty());
1942 that
->bits_
.reserve(that
->width_
);
1943 for (auto &c
: that
->chunks_
)
1944 for (int i
= 0; i
< c
.width
; i
++)
1945 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
1947 that
->chunks_
.clear();
1951 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
1953 void RTLIL::SigSpec::hash() const
1955 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1957 if (that
->hash_
!= 0)
1960 cover("kernel.rtlil.sigspec.hash");
1964 for (auto &c
: that
->chunks_
)
1965 if (c
.wire
== NULL
) {
1966 for (auto &v
: c
.data
.bits
)
1967 DJB2(that
->hash_
, v
);
1969 for (auto &v
: c
.wire
->name
)
1970 DJB2(that
->hash_
, v
);
1971 DJB2(that
->hash_
, c
.offset
);
1972 DJB2(that
->hash_
, c
.width
);
1975 if (that
->hash_
== 0)
1979 void RTLIL::SigSpec::sort()
1982 cover("kernel.rtlil.sigspec.sort");
1983 std::sort(bits_
.begin(), bits_
.end());
1986 void RTLIL::SigSpec::sort_and_unify()
1988 cover("kernel.rtlil.sigspec.sort_and_unify");
1989 *this = this->to_sigbit_set();
1992 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
1994 replace(pattern
, with
, this);
1997 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
1999 cover("kernel.rtlil.sigspec.replace");
2005 log_assert(other
!= NULL
);
2006 log_assert(width_
== other
->width_
);
2009 log_assert(pattern
.width_
== with
.width_
);
2011 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
2012 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2013 if (pattern
.bits_
[i
].wire
!= NULL
)
2014 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2016 for (int i
= 0; i
< SIZE(bits_
); i
++)
2017 if (pattern_map
.count(bits_
[i
]))
2018 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
2023 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2025 remove2(pattern
, NULL
);
2028 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2030 RTLIL::SigSpec tmp
= *this;
2031 tmp
.remove2(pattern
, other
);
2034 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2037 cover("kernel.rtlil.sigspec.remove_other");
2039 cover("kernel.rtlil.sigspec.remove");
2043 if (other
!= NULL
) {
2044 log_assert(width_
== other
->width_
);
2048 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2049 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2051 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2052 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
2055 new_other_bits
.push_back(other
->bits_
[i
]);
2056 new_bits
.push_back(bits_
[i
]);
2059 bits_
.swap(new_bits
);
2060 width_
= SIZE(bits_
);
2062 if (other
!= NULL
) {
2063 other
->bits_
.swap(new_other_bits
);
2064 other
->width_
= SIZE(other
->bits_
);
2070 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
2073 cover("kernel.rtlil.sigspec.extract_other");
2075 cover("kernel.rtlil.sigspec.extract");
2083 log_assert(other
== NULL
|| width_
== other
->width_
);
2085 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
2086 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2090 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2091 for (int i
= 0; i
< width_
; i
++)
2092 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2093 ret
.append_bit(bits_other
[i
]);
2095 for (int i
= 0; i
< width_
; i
++)
2096 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
2097 ret
.append_bit(bits_match
[i
]);
2104 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2106 cover("kernel.rtlil.sigspec.replace_pos");
2111 log_assert(offset
>= 0);
2112 log_assert(with
.width_
>= 0);
2113 log_assert(offset
+with
.width_
<= width_
);
2115 for (int i
= 0; i
< with
.width_
; i
++)
2116 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2121 void RTLIL::SigSpec::remove_const()
2125 cover("kernel.rtlil.sigspec.remove_const.packed");
2127 std::vector
<RTLIL::SigChunk
> new_chunks
;
2128 new_chunks
.reserve(SIZE(chunks_
));
2131 for (auto &chunk
: chunks_
)
2132 if (chunk
.wire
!= NULL
) {
2133 new_chunks
.push_back(chunk
);
2134 width_
+= chunk
.width
;
2137 chunks_
.swap(new_chunks
);
2141 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2143 std::vector
<RTLIL::SigBit
> new_bits
;
2144 new_bits
.reserve(width_
);
2146 for (auto &bit
: bits_
)
2147 if (bit
.wire
!= NULL
)
2148 new_bits
.push_back(bit
);
2150 bits_
.swap(new_bits
);
2151 width_
= bits_
.size();
2157 void RTLIL::SigSpec::remove(int offset
, int length
)
2159 cover("kernel.rtlil.sigspec.remove_pos");
2163 log_assert(offset
>= 0);
2164 log_assert(length
>= 0);
2165 log_assert(offset
+ length
<= width_
);
2167 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2168 width_
= bits_
.size();
2173 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2176 cover("kernel.rtlil.sigspec.extract_pos");
2177 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2180 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2182 if (signal
.width_
== 0)
2190 cover("kernel.rtlil.sigspec.append");
2192 if (packed() != signal
.packed()) {
2198 for (auto &other_c
: signal
.chunks_
)
2200 auto &my_last_c
= chunks_
.back();
2201 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2202 auto &this_data
= my_last_c
.data
.bits
;
2203 auto &other_data
= other_c
.data
.bits
;
2204 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2205 my_last_c
.width
+= other_c
.width
;
2207 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2208 my_last_c
.width
+= other_c
.width
;
2210 chunks_
.push_back(other_c
);
2213 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2215 width_
+= signal
.width_
;
2219 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2223 cover("kernel.rtlil.sigspec.append_bit.packed");
2225 if (chunks_
.size() == 0)
2226 chunks_
.push_back(bit
);
2228 if (bit
.wire
== NULL
)
2229 if (chunks_
.back().wire
== NULL
) {
2230 chunks_
.back().data
.bits
.push_back(bit
.data
);
2231 chunks_
.back().width
++;
2233 chunks_
.push_back(bit
);
2235 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2236 chunks_
.back().width
++;
2238 chunks_
.push_back(bit
);
2242 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2243 bits_
.push_back(bit
);
2250 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2252 cover("kernel.rtlil.sigspec.extend");
2257 remove(width
, width_
- width
);
2259 if (width_
< width
) {
2260 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2261 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2262 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2263 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2264 while (width_
< width
)
2269 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2271 cover("kernel.rtlil.sigspec.extend_u0");
2276 remove(width
, width_
- width
);
2278 if (width_
< width
) {
2279 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2281 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2282 while (width_
< width
)
2288 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2290 cover("kernel.rtlil.sigspec.repeat");
2293 for (int i
= 0; i
< num
; i
++)
2299 void RTLIL::SigSpec::check() const
2303 cover("kernel.rtlil.sigspec.check.skip");
2307 cover("kernel.rtlil.sigspec.check.packed");
2310 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2311 const RTLIL::SigChunk chunk
= chunks_
[i
];
2312 if (chunk
.wire
== NULL
) {
2314 log_assert(chunks_
[i
-1].wire
!= NULL
);
2315 log_assert(chunk
.offset
== 0);
2316 log_assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2318 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2319 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2320 log_assert(chunk
.offset
>= 0);
2321 log_assert(chunk
.width
>= 0);
2322 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2323 log_assert(chunk
.data
.bits
.size() == 0);
2327 log_assert(w
== width_
);
2328 log_assert(bits_
.empty());
2332 cover("kernel.rtlil.sigspec.check.unpacked");
2334 log_assert(width_
== SIZE(bits_
));
2335 log_assert(chunks_
.empty());
2340 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2342 cover("kernel.rtlil.sigspec.comp_lt");
2347 if (width_
!= other
.width_
)
2348 return width_
< other
.width_
;
2353 if (chunks_
.size() != other
.chunks_
.size())
2354 return chunks_
.size() < other
.chunks_
.size();
2359 if (hash_
!= other
.hash_
)
2360 return hash_
< other
.hash_
;
2362 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2363 if (chunks_
[i
] != other
.chunks_
[i
]) {
2364 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2365 return chunks_
[i
] < other
.chunks_
[i
];
2368 cover("kernel.rtlil.sigspec.comp_lt.equal");
2372 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2374 cover("kernel.rtlil.sigspec.comp_eq");
2379 if (width_
!= other
.width_
)
2385 if (chunks_
.size() != chunks_
.size())
2391 if (hash_
!= other
.hash_
)
2394 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2395 if (chunks_
[i
] != other
.chunks_
[i
]) {
2396 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2400 cover("kernel.rtlil.sigspec.comp_eq.equal");
2404 bool RTLIL::SigSpec::is_wire() const
2406 cover("kernel.rtlil.sigspec.is_wire");
2409 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2412 bool RTLIL::SigSpec::is_chunk() const
2414 cover("kernel.rtlil.sigspec.is_chunk");
2417 return SIZE(chunks_
) == 1;
2420 bool RTLIL::SigSpec::is_fully_const() const
2422 cover("kernel.rtlil.sigspec.is_fully_const");
2425 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2426 if (it
->width
> 0 && it
->wire
!= NULL
)
2431 bool RTLIL::SigSpec::is_fully_def() const
2433 cover("kernel.rtlil.sigspec.is_fully_def");
2436 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2437 if (it
->width
> 0 && it
->wire
!= NULL
)
2439 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2440 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2446 bool RTLIL::SigSpec::is_fully_undef() const
2448 cover("kernel.rtlil.sigspec.is_fully_undef");
2451 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2452 if (it
->width
> 0 && it
->wire
!= NULL
)
2454 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2455 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2461 bool RTLIL::SigSpec::has_marked_bits() const
2463 cover("kernel.rtlil.sigspec.has_marked_bits");
2466 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2467 if (it
->width
> 0 && it
->wire
== NULL
) {
2468 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2469 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2475 bool RTLIL::SigSpec::as_bool() const
2477 cover("kernel.rtlil.sigspec.as_bool");
2480 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2482 return chunks_
[0].data
.as_bool();
2486 int RTLIL::SigSpec::as_int() const
2488 cover("kernel.rtlil.sigspec.as_int");
2491 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2493 return chunks_
[0].data
.as_int();
2497 std::string
RTLIL::SigSpec::as_string() const
2499 cover("kernel.rtlil.sigspec.as_string");
2503 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2504 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2505 if (chunk
.wire
!= NULL
)
2506 for (int j
= 0; j
< chunk
.width
; j
++)
2509 str
+= chunk
.data
.as_string();
2514 RTLIL::Const
RTLIL::SigSpec::as_const() const
2516 cover("kernel.rtlil.sigspec.as_const");
2519 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2521 return chunks_
[0].data
;
2522 return RTLIL::Const();
2525 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2527 cover("kernel.rtlil.sigspec.as_wire");
2530 log_assert(is_wire());
2531 return chunks_
[0].wire
;
2534 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2536 cover("kernel.rtlil.sigspec.as_chunk");
2539 log_assert(is_chunk());
2543 bool RTLIL::SigSpec::match(std::string pattern
) const
2545 cover("kernel.rtlil.sigspec.match");
2548 std::string str
= as_string();
2549 log_assert(pattern
.size() == str
.size());
2551 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2552 if (pattern
[i
] == ' ')
2554 if (pattern
[i
] == '*') {
2555 if (str
[i
] != 'z' && str
[i
] != 'x')
2559 if (pattern
[i
] != str
[i
])
2566 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2568 cover("kernel.rtlil.sigspec.to_sigbit_set");
2571 std::set
<RTLIL::SigBit
> sigbits
;
2572 for (auto &c
: chunks_
)
2573 for (int i
= 0; i
< c
.width
; i
++)
2574 sigbits
.insert(RTLIL::SigBit(c
, i
));
2578 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2580 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2586 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2588 cover("kernel.rtlil.sigspec.to_single_sigbit");
2591 log_assert(width_
== 1);
2592 for (auto &c
: chunks_
)
2594 return RTLIL::SigBit(c
);
2598 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2600 size_t start
= 0, end
= 0;
2601 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2602 tokens
.push_back(text
.substr(start
, end
- start
));
2605 tokens
.push_back(text
.substr(start
));
2608 static int sigspec_parse_get_dummy_line_num()
2613 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2615 cover("kernel.rtlil.sigspec.parse");
2617 std::vector
<std::string
> tokens
;
2618 sigspec_parse_split(tokens
, str
, ',');
2620 sig
= RTLIL::SigSpec();
2621 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2623 std::string netname
= tokens
[tokidx
];
2624 std::string indices
;
2626 if (netname
.size() == 0)
2629 if ('0' <= netname
[0] && netname
[0] <= '9') {
2630 cover("kernel.rtlil.sigspec.parse.const");
2631 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2632 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2635 sig
.append(RTLIL::Const(ast
->bits
));
2643 cover("kernel.rtlil.sigspec.parse.net");
2645 if (netname
[0] != '$' && netname
[0] != '\\')
2646 netname
= "\\" + netname
;
2648 if (module
->wires_
.count(netname
) == 0) {
2649 size_t indices_pos
= netname
.size()-1;
2650 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2653 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2654 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2656 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2658 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2659 indices
= netname
.substr(indices_pos
);
2660 netname
= netname
.substr(0, indices_pos
);
2665 if (module
->wires_
.count(netname
) == 0)
2668 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2669 if (!indices
.empty()) {
2670 std::vector
<std::string
> index_tokens
;
2671 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2672 if (index_tokens
.size() == 1) {
2673 cover("kernel.rtlil.sigspec.parse.bit_sel");
2674 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2676 cover("kernel.rtlil.sigspec.parse.part_sel");
2677 int a
= atoi(index_tokens
.at(0).c_str());
2678 int b
= atoi(index_tokens
.at(1).c_str());
2683 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2692 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2694 if (str
.empty() || str
[0] != '@')
2695 return parse(sig
, module
, str
);
2697 cover("kernel.rtlil.sigspec.parse.sel");
2699 str
= RTLIL::escape_id(str
.substr(1));
2700 if (design
->selection_vars
.count(str
) == 0)
2703 sig
= RTLIL::SigSpec();
2704 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2705 for (auto &it
: module
->wires_
)
2706 if (sel
.selected_member(module
->name
, it
.first
))
2707 sig
.append(it
.second
);
2712 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2715 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2716 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2721 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2722 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2726 if (lhs
.chunks_
.size() == 1) {
2727 char *p
= (char*)str
.c_str(), *endptr
;
2728 long long int val
= strtoll(p
, &endptr
, 10);
2729 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2730 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2731 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2736 return parse(sig
, module
, str
);
2739 RTLIL::CaseRule::~CaseRule()
2741 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2745 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2747 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2748 new_caserule
->compare
= compare
;
2749 new_caserule
->actions
= actions
;
2750 for (auto &it
: switches
)
2751 new_caserule
->switches
.push_back(it
->clone());
2752 return new_caserule
;
2755 RTLIL::SwitchRule::~SwitchRule()
2757 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2761 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2763 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2764 new_switchrule
->signal
= signal
;
2765 new_switchrule
->attributes
= attributes
;
2766 for (auto &it
: cases
)
2767 new_switchrule
->cases
.push_back(it
->clone());
2768 return new_switchrule
;
2772 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2774 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2775 new_syncrule
->type
= type
;
2776 new_syncrule
->signal
= signal
;
2777 new_syncrule
->actions
= actions
;
2778 return new_syncrule
;
2781 RTLIL::Process::~Process()
2783 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2787 RTLIL::Process
*RTLIL::Process::clone() const
2789 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2791 new_proc
->name
= name
;
2792 new_proc
->attributes
= attributes
;
2794 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2795 new_proc
->root_case
= *rc_ptr
;
2796 rc_ptr
->switches
.clear();
2799 for (auto &it
: syncs
)
2800 new_proc
->syncs
.push_back(it
->clone());