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 "kernel/macc.h"
22 #include "frontends/verilog/verilog_frontend.h"
23 #include "backends/ilang/ilang_backend.h"
30 std::vector
<int> RTLIL::IdString::global_refcount_storage_
;
31 std::vector
<char*> RTLIL::IdString::global_id_storage_
;
32 std::map
<char*, int, RTLIL::IdString::char_ptr_cmp
> RTLIL::IdString::global_id_index_
;
33 std::vector
<int> RTLIL::IdString::global_free_idx_list_
;
37 flags
= RTLIL::CONST_FLAG_NONE
;
40 RTLIL::Const::Const(std::string str
)
42 flags
= RTLIL::CONST_FLAG_STRING
;
43 for (int i
= str
.size()-1; i
>= 0; i
--) {
44 unsigned char ch
= str
[i
];
45 for (int j
= 0; j
< 8; j
++) {
46 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
52 RTLIL::Const::Const(int val
, int width
)
54 flags
= RTLIL::CONST_FLAG_NONE
;
55 for (int i
= 0; i
< width
; i
++) {
56 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
61 RTLIL::Const::Const(RTLIL::State bit
, int width
)
63 flags
= RTLIL::CONST_FLAG_NONE
;
64 for (int i
= 0; i
< width
; i
++)
68 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
70 if (bits
.size() != other
.bits
.size())
71 return bits
.size() < other
.bits
.size();
72 for (size_t i
= 0; i
< bits
.size(); i
++)
73 if (bits
[i
] != other
.bits
[i
])
74 return bits
[i
] < other
.bits
[i
];
78 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
80 return bits
== other
.bits
;
83 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
85 return bits
!= other
.bits
;
88 bool RTLIL::Const::as_bool() const
90 for (size_t i
= 0; i
< bits
.size(); i
++)
91 if (bits
[i
] == RTLIL::S1
)
96 int RTLIL::Const::as_int(bool is_signed
) const
99 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
100 if (bits
[i
] == RTLIL::S1
)
102 if (is_signed
&& bits
.back() == RTLIL::S1
)
103 for (size_t i
= bits
.size(); i
< 32; i
++)
108 std::string
RTLIL::Const::as_string() const
111 for (size_t i
= bits
.size(); i
> 0; i
--)
113 case S0
: ret
+= "0"; break;
114 case S1
: ret
+= "1"; break;
115 case Sx
: ret
+= "x"; break;
116 case Sz
: ret
+= "z"; break;
117 case Sa
: ret
+= "-"; break;
118 case Sm
: ret
+= "m"; break;
123 std::string
RTLIL::Const::decode_string() const
126 std::vector
<char> string_chars
;
127 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
129 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
130 if (bits
[i
+ j
] == RTLIL::State::S1
)
133 string_chars
.push_back(ch
);
135 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
136 string
+= string_chars
[i
];
140 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
144 if (selected_modules
.count(mod_name
) > 0)
146 if (selected_members
.count(mod_name
) > 0)
151 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
155 if (selected_modules
.count(mod_name
) > 0)
160 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
164 if (selected_modules
.count(mod_name
) > 0)
166 if (selected_members
.count(mod_name
) > 0)
167 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
172 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
174 if (full_selection
) {
175 selected_modules
.clear();
176 selected_members
.clear();
180 std::vector
<RTLIL::IdString
> del_list
, add_list
;
183 for (auto mod_name
: selected_modules
) {
184 if (design
->modules_
.count(mod_name
) == 0)
185 del_list
.push_back(mod_name
);
186 selected_members
.erase(mod_name
);
188 for (auto mod_name
: del_list
)
189 selected_modules
.erase(mod_name
);
192 for (auto &it
: selected_members
)
193 if (design
->modules_
.count(it
.first
) == 0)
194 del_list
.push_back(it
.first
);
195 for (auto mod_name
: del_list
)
196 selected_members
.erase(mod_name
);
198 for (auto &it
: selected_members
) {
200 for (auto memb_name
: it
.second
)
201 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
202 del_list
.push_back(memb_name
);
203 for (auto memb_name
: del_list
)
204 it
.second
.erase(memb_name
);
209 for (auto &it
: selected_members
)
210 if (it
.second
.size() == 0)
211 del_list
.push_back(it
.first
);
212 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
213 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
214 add_list
.push_back(it
.first
);
215 for (auto mod_name
: del_list
)
216 selected_members
.erase(mod_name
);
217 for (auto mod_name
: add_list
) {
218 selected_members
.erase(mod_name
);
219 selected_modules
.insert(mod_name
);
222 if (selected_modules
.size() == design
->modules_
.size()) {
223 full_selection
= true;
224 selected_modules
.clear();
225 selected_members
.clear();
229 RTLIL::Design::Design()
231 refcount_modules_
= 0;
232 selection_stack
.push_back(RTLIL::Selection());
235 RTLIL::Design::~Design()
237 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
241 RTLIL::ObjRange
<RTLIL::Module
*> RTLIL::Design::modules()
243 return RTLIL::ObjRange
<RTLIL::Module
*>(&modules_
, &refcount_modules_
);
246 RTLIL::Module
*RTLIL::Design::module(RTLIL::IdString name
)
248 return modules_
.count(name
) ? modules_
.at(name
) : NULL
;
251 void RTLIL::Design::add(RTLIL::Module
*module
)
253 log_assert(modules_
.count(module
->name
) == 0);
254 log_assert(refcount_modules_
== 0);
255 modules_
[module
->name
] = module
;
256 module
->design
= this;
258 for (auto mon
: monitors
)
259 mon
->notify_module_add(module
);
262 RTLIL::Module
*RTLIL::Design::addModule(RTLIL::IdString name
)
264 log_assert(modules_
.count(name
) == 0);
265 log_assert(refcount_modules_
== 0);
267 RTLIL::Module
*module
= new RTLIL::Module
;
268 modules_
[name
] = module
;
269 module
->design
= this;
272 for (auto mon
: monitors
)
273 mon
->notify_module_add(module
);
278 void RTLIL::Design::scratchpad_unset(std::string varname
)
280 scratchpad
.erase(varname
);
283 void RTLIL::Design::scratchpad_set_int(std::string varname
, int value
)
285 scratchpad
[varname
] = stringf("%d", value
);
288 void RTLIL::Design::scratchpad_set_bool(std::string varname
, bool value
)
290 scratchpad
[varname
] = value
? "true" : "false";
293 void RTLIL::Design::scratchpad_set_string(std::string varname
, std::string value
)
295 scratchpad
[varname
] = value
;
298 int RTLIL::Design::scratchpad_get_int(std::string varname
, int default_value
) const
300 if (scratchpad
.count(varname
) == 0)
301 return default_value
;
303 std::string str
= scratchpad
.at(varname
);
305 if (str
== "0" || str
== "false")
308 if (str
== "1" || str
== "true")
311 char *endptr
= nullptr;
312 long int parsed_value
= strtol(str
.c_str(), &endptr
, 10);
313 return *endptr
? default_value
: parsed_value
;
316 bool RTLIL::Design::scratchpad_get_bool(std::string varname
, bool default_value
) const
318 if (scratchpad
.count(varname
) == 0)
319 return default_value
;
321 std::string str
= scratchpad
.at(varname
);
323 if (str
== "0" || str
== "false")
326 if (str
== "1" || str
== "true")
329 return default_value
;
332 std::string
RTLIL::Design::scratchpad_get_string(std::string varname
, std::string default_value
) const
334 if (scratchpad
.count(varname
) == 0)
335 return default_value
;
336 return scratchpad
.at(varname
);
339 void RTLIL::Design::remove(RTLIL::Module
*module
)
341 for (auto mon
: monitors
)
342 mon
->notify_module_del(module
);
344 log_assert(modules_
.at(module
->name
) == module
);
345 modules_
.erase(module
->name
);
349 void RTLIL::Design::check()
352 for (auto &it
: modules_
) {
353 log_assert(this == it
.second
->design
);
354 log_assert(it
.first
== it
.second
->name
);
355 log_assert(!it
.first
.empty());
361 void RTLIL::Design::optimize()
363 for (auto &it
: modules_
)
364 it
.second
->optimize();
365 for (auto &it
: selection_stack
)
367 for (auto &it
: selection_vars
)
368 it
.second
.optimize(this);
371 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
373 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
375 if (selection_stack
.size() == 0)
377 return selection_stack
.back().selected_module(mod_name
);
380 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
382 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
384 if (selection_stack
.size() == 0)
386 return selection_stack
.back().selected_whole_module(mod_name
);
389 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
391 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
393 if (selection_stack
.size() == 0)
395 return selection_stack
.back().selected_member(mod_name
, memb_name
);
398 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
400 return selected_module(mod
->name
);
403 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
405 return selected_whole_module(mod
->name
);
408 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_modules() const
410 std::vector
<RTLIL::Module
*> result
;
411 result
.reserve(modules_
.size());
412 for (auto &it
: modules_
)
413 if (selected_module(it
.first
))
414 result
.push_back(it
.second
);
418 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules() const
420 std::vector
<RTLIL::Module
*> result
;
421 result
.reserve(modules_
.size());
422 for (auto &it
: modules_
)
423 if (selected_whole_module(it
.first
))
424 result
.push_back(it
.second
);
428 std::vector
<RTLIL::Module
*> RTLIL::Design::selected_whole_modules_warn() const
430 std::vector
<RTLIL::Module
*> result
;
431 result
.reserve(modules_
.size());
432 for (auto &it
: modules_
)
433 if (selected_whole_module(it
.first
))
434 result
.push_back(it
.second
);
435 else if (selected_module(it
.first
))
436 log("Warning: Ignoring partially selected module %s.\n", log_id(it
.first
));
440 RTLIL::Module::Module()
447 RTLIL::Module::~Module()
449 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
451 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
453 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
455 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
459 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
461 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
464 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
466 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
471 struct InternalCellChecker
473 RTLIL::Module
*module
;
475 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
477 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
479 void error(int linenr
)
481 std::stringstream buf
;
482 ILANG_BACKEND::dump_cell(buf
, " ", cell
);
484 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
485 module
? module
->name
.c_str() : "", module
? "." : "",
486 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, buf
.str().c_str());
489 int param(const char *name
)
491 if (cell
->parameters
.count(name
) == 0)
493 expected_params
.insert(name
);
494 return cell
->parameters
.at(name
).as_int();
497 int param_bool(const char *name
)
500 if (cell
->parameters
.at(name
).bits
.size() > 32)
502 if (v
!= 0 && v
!= 1)
507 void param_bits(const char *name
, int width
)
510 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
514 void port(const char *name
, int width
)
516 if (!cell
->hasPort(name
))
518 if (cell
->getPort(name
).size() != width
)
520 expected_ports
.insert(name
);
523 void check_expected(bool check_matched_sign
= true)
525 for (auto ¶
: cell
->parameters
)
526 if (expected_params
.count(para
.first
) == 0)
528 for (auto &conn
: cell
->connections())
529 if (expected_ports
.count(conn
.first
) == 0)
532 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
533 bool a_is_signed
= param("\\A_SIGNED") != 0;
534 bool b_is_signed
= param("\\B_SIGNED") != 0;
535 if (a_is_signed
!= b_is_signed
)
540 void check_gate(const char *ports
)
542 if (cell
->parameters
.size() != 0)
545 for (const char *p
= ports
; *p
; p
++) {
546 char portname
[3] = { '\\', *p
, 0 };
547 if (!cell
->hasPort(portname
))
549 if (cell
->getPort(portname
).size() != 1)
553 for (auto &conn
: cell
->connections()) {
554 if (conn
.first
.size() != 2 || conn
.first
[0] != '\\')
556 if (strchr(ports
, conn
.first
[1]) == NULL
)
563 if (cell
->type
.substr(0, 1) != "$" || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
564 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:" || cell
->type
.substr(0, 8) == "$extern:")
567 if (cell
->type
.in("$not", "$pos", "$neg")) {
568 param_bool("\\A_SIGNED");
569 port("\\A", param("\\A_WIDTH"));
570 port("\\Y", param("\\Y_WIDTH"));
575 if (cell
->type
.in("$and", "$or", "$xor", "$xnor")) {
576 param_bool("\\A_SIGNED");
577 param_bool("\\B_SIGNED");
578 port("\\A", param("\\A_WIDTH"));
579 port("\\B", param("\\B_WIDTH"));
580 port("\\Y", param("\\Y_WIDTH"));
585 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_xor", "$reduce_xnor", "$reduce_bool")) {
586 param_bool("\\A_SIGNED");
587 port("\\A", param("\\A_WIDTH"));
588 port("\\Y", param("\\Y_WIDTH"));
593 if (cell
->type
.in("$shl", "$shr", "$sshl", "$sshr", "$shift", "$shiftx")) {
594 param_bool("\\A_SIGNED");
595 param_bool("\\B_SIGNED");
596 port("\\A", param("\\A_WIDTH"));
597 port("\\B", param("\\B_WIDTH"));
598 port("\\Y", param("\\Y_WIDTH"));
599 check_expected(false);
603 if (cell
->type
.in("$lt", "$le", "$eq", "$ne", "$eqx", "$nex", "$ge", "$gt")) {
604 param_bool("\\A_SIGNED");
605 param_bool("\\B_SIGNED");
606 port("\\A", param("\\A_WIDTH"));
607 port("\\B", param("\\B_WIDTH"));
608 port("\\Y", param("\\Y_WIDTH"));
613 if (cell
->type
.in("$add", "$sub", "$mul", "$div", "$mod", "$pow")) {
614 param_bool("\\A_SIGNED");
615 param_bool("\\B_SIGNED");
616 port("\\A", param("\\A_WIDTH"));
617 port("\\B", param("\\B_WIDTH"));
618 port("\\Y", param("\\Y_WIDTH"));
619 check_expected(cell
->type
!= "$pow");
623 if (cell
->type
== "$fa") {
624 port("\\A", param("\\WIDTH"));
625 port("\\B", param("\\WIDTH"));
626 port("\\C", param("\\WIDTH"));
627 port("\\X", param("\\WIDTH"));
628 port("\\Y", param("\\WIDTH"));
633 if (cell
->type
== "$lcu") {
634 port("\\P", param("\\WIDTH"));
635 port("\\G", param("\\WIDTH"));
637 port("\\CO", param("\\WIDTH"));
642 if (cell
->type
== "$alu") {
643 param_bool("\\A_SIGNED");
644 param_bool("\\B_SIGNED");
645 port("\\A", param("\\A_WIDTH"));
646 port("\\B", param("\\B_WIDTH"));
649 port("\\X", param("\\Y_WIDTH"));
650 port("\\Y", param("\\Y_WIDTH"));
651 port("\\CO", param("\\Y_WIDTH"));
656 if (cell
->type
== "$macc") {
658 param("\\CONFIG_WIDTH");
659 port("\\A", param("\\A_WIDTH"));
660 port("\\B", param("\\B_WIDTH"));
661 port("\\Y", param("\\Y_WIDTH"));
663 Macc().from_cell(cell
);
667 if (cell
->type
== "$logic_not") {
668 param_bool("\\A_SIGNED");
669 port("\\A", param("\\A_WIDTH"));
670 port("\\Y", param("\\Y_WIDTH"));
675 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
676 param_bool("\\A_SIGNED");
677 param_bool("\\B_SIGNED");
678 port("\\A", param("\\A_WIDTH"));
679 port("\\B", param("\\B_WIDTH"));
680 port("\\Y", param("\\Y_WIDTH"));
681 check_expected(false);
685 if (cell
->type
== "$slice") {
687 port("\\A", param("\\A_WIDTH"));
688 port("\\Y", param("\\Y_WIDTH"));
689 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
695 if (cell
->type
== "$concat") {
696 port("\\A", param("\\A_WIDTH"));
697 port("\\B", param("\\B_WIDTH"));
698 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
703 if (cell
->type
== "$mux") {
704 port("\\A", param("\\WIDTH"));
705 port("\\B", param("\\WIDTH"));
707 port("\\Y", param("\\WIDTH"));
712 if (cell
->type
== "$pmux") {
713 port("\\A", param("\\WIDTH"));
714 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
715 port("\\S", param("\\S_WIDTH"));
716 port("\\Y", param("\\WIDTH"));
721 if (cell
->type
== "$lut") {
723 port("\\A", param("\\WIDTH"));
729 if (cell
->type
== "$sr") {
730 param_bool("\\SET_POLARITY");
731 param_bool("\\CLR_POLARITY");
732 port("\\SET", param("\\WIDTH"));
733 port("\\CLR", param("\\WIDTH"));
734 port("\\Q", param("\\WIDTH"));
739 if (cell
->type
== "$dff") {
740 param_bool("\\CLK_POLARITY");
742 port("\\D", param("\\WIDTH"));
743 port("\\Q", param("\\WIDTH"));
748 if (cell
->type
== "$dffsr") {
749 param_bool("\\CLK_POLARITY");
750 param_bool("\\SET_POLARITY");
751 param_bool("\\CLR_POLARITY");
753 port("\\SET", param("\\WIDTH"));
754 port("\\CLR", param("\\WIDTH"));
755 port("\\D", param("\\WIDTH"));
756 port("\\Q", param("\\WIDTH"));
761 if (cell
->type
== "$adff") {
762 param_bool("\\CLK_POLARITY");
763 param_bool("\\ARST_POLARITY");
764 param_bits("\\ARST_VALUE", param("\\WIDTH"));
767 port("\\D", param("\\WIDTH"));
768 port("\\Q", param("\\WIDTH"));
773 if (cell
->type
== "$dlatch") {
774 param_bool("\\EN_POLARITY");
776 port("\\D", param("\\WIDTH"));
777 port("\\Q", param("\\WIDTH"));
782 if (cell
->type
== "$dlatchsr") {
783 param_bool("\\EN_POLARITY");
784 param_bool("\\SET_POLARITY");
785 param_bool("\\CLR_POLARITY");
787 port("\\SET", param("\\WIDTH"));
788 port("\\CLR", param("\\WIDTH"));
789 port("\\D", param("\\WIDTH"));
790 port("\\Q", param("\\WIDTH"));
795 if (cell
->type
== "$fsm") {
797 param_bool("\\CLK_POLARITY");
798 param_bool("\\ARST_POLARITY");
799 param("\\STATE_BITS");
800 param("\\STATE_NUM");
801 param("\\STATE_NUM_LOG2");
802 param("\\STATE_RST");
803 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
804 param("\\TRANS_NUM");
805 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
808 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
809 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
814 if (cell
->type
== "$memrd") {
816 param_bool("\\CLK_ENABLE");
817 param_bool("\\CLK_POLARITY");
818 param_bool("\\TRANSPARENT");
820 port("\\ADDR", param("\\ABITS"));
821 port("\\DATA", param("\\WIDTH"));
826 if (cell
->type
== "$memwr") {
828 param_bool("\\CLK_ENABLE");
829 param_bool("\\CLK_POLARITY");
832 port("\\EN", param("\\WIDTH"));
833 port("\\ADDR", param("\\ABITS"));
834 port("\\DATA", param("\\WIDTH"));
839 if (cell
->type
== "$mem") {
843 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
844 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
845 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
846 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
847 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
848 port("\\RD_CLK", param("\\RD_PORTS"));
849 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
850 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
851 port("\\WR_CLK", param("\\WR_PORTS"));
852 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
853 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
854 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
859 if (cell
->type
== "$assert") {
866 if (cell
->type
== "$_NOT_") { check_gate("AY"); return; }
867 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
868 if (cell
->type
== "$_NAND_") { check_gate("ABY"); return; }
869 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
870 if (cell
->type
== "$_NOR_") { check_gate("ABY"); return; }
871 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
872 if (cell
->type
== "$_XNOR_") { check_gate("ABY"); return; }
873 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
874 if (cell
->type
== "$_AOI3_") { check_gate("ABCY"); return; }
875 if (cell
->type
== "$_OAI3_") { check_gate("ABCY"); return; }
876 if (cell
->type
== "$_AOI4_") { check_gate("ABCDY"); return; }
877 if (cell
->type
== "$_OAI4_") { check_gate("ABCDY"); return; }
879 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
880 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
881 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
882 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
884 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
885 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
887 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
888 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
889 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
890 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
891 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
892 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
893 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
894 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
896 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
897 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
898 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
899 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
900 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
901 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
902 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
903 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
905 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
906 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
908 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
909 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
910 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
911 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
912 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
913 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
914 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
915 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
923 void RTLIL::Module::check()
926 std::vector
<bool> ports_declared
;
927 for (auto &it
: wires_
) {
928 log_assert(this == it
.second
->module
);
929 log_assert(it
.first
== it
.second
->name
);
930 log_assert(!it
.first
.empty());
931 log_assert(it
.second
->width
>= 0);
932 log_assert(it
.second
->port_id
>= 0);
933 for (auto &it2
: it
.second
->attributes
)
934 log_assert(!it2
.first
.empty());
935 if (it
.second
->port_id
) {
936 log_assert(SIZE(ports
) >= it
.second
->port_id
);
937 log_assert(ports
.at(it
.second
->port_id
-1) == it
.first
);
938 log_assert(it
.second
->port_input
|| it
.second
->port_output
);
939 if (SIZE(ports_declared
) < it
.second
->port_id
)
940 ports_declared
.resize(it
.second
->port_id
);
941 log_assert(ports_declared
[it
.second
->port_id
-1] == false);
942 ports_declared
[it
.second
->port_id
-1] = true;
944 log_assert(!it
.second
->port_input
&& !it
.second
->port_output
);
946 for (auto port_declared
: ports_declared
)
947 log_assert(port_declared
== true);
948 log_assert(SIZE(ports
) == SIZE(ports_declared
));
950 for (auto &it
: memories
) {
951 log_assert(it
.first
== it
.second
->name
);
952 log_assert(!it
.first
.empty());
953 log_assert(it
.second
->width
>= 0);
954 log_assert(it
.second
->size
>= 0);
955 for (auto &it2
: it
.second
->attributes
)
956 log_assert(!it2
.first
.empty());
959 for (auto &it
: cells_
) {
960 log_assert(this == it
.second
->module
);
961 log_assert(it
.first
== it
.second
->name
);
962 log_assert(!it
.first
.empty());
963 log_assert(!it
.second
->type
.empty());
964 for (auto &it2
: it
.second
->connections()) {
965 log_assert(!it2
.first
.empty());
968 for (auto &it2
: it
.second
->attributes
)
969 log_assert(!it2
.first
.empty());
970 for (auto &it2
: it
.second
->parameters
)
971 log_assert(!it2
.first
.empty());
972 InternalCellChecker
checker(this, it
.second
);
976 for (auto &it
: processes
) {
977 log_assert(it
.first
== it
.second
->name
);
978 log_assert(!it
.first
.empty());
979 // FIXME: More checks here..
982 for (auto &it
: connections_
) {
983 log_assert(it
.first
.size() == it
.second
.size());
988 for (auto &it
: attributes
)
989 log_assert(!it
.first
.empty());
993 void RTLIL::Module::optimize()
997 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
999 log_assert(new_mod
->refcount_wires_
== 0);
1000 log_assert(new_mod
->refcount_cells_
== 0);
1002 new_mod
->connections_
= connections_
;
1003 new_mod
->attributes
= attributes
;
1005 for (auto &it
: wires_
)
1006 new_mod
->addWire(it
.first
, it
.second
);
1008 for (auto &it
: memories
)
1009 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
1011 for (auto &it
: cells_
)
1012 new_mod
->addCell(it
.first
, it
.second
);
1014 for (auto &it
: processes
)
1015 new_mod
->processes
[it
.first
] = it
.second
->clone();
1017 struct RewriteSigSpecWorker
1020 void operator()(RTLIL::SigSpec
&sig
)
1022 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
1023 for (auto &c
: chunks
)
1025 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
1030 RewriteSigSpecWorker rewriteSigSpecWorker
;
1031 rewriteSigSpecWorker
.mod
= new_mod
;
1032 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
1033 new_mod
->fixup_ports();
1036 RTLIL::Module
*RTLIL::Module::clone() const
1038 RTLIL::Module
*new_mod
= new RTLIL::Module
;
1039 new_mod
->name
= name
;
1044 bool RTLIL::Module::has_memories() const
1046 return !memories
.empty();
1049 bool RTLIL::Module::has_processes() const
1051 return !processes
.empty();
1054 bool RTLIL::Module::has_memories_warn() const
1056 if (!memories
.empty())
1057 log("Warning: Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1058 return !memories
.empty();
1061 bool RTLIL::Module::has_processes_warn() const
1063 if (!processes
.empty())
1064 log("Warning: Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1065 return !processes
.empty();
1068 std::vector
<RTLIL::Wire
*> RTLIL::Module::selected_wires() const
1070 std::vector
<RTLIL::Wire
*> result
;
1071 result
.reserve(wires_
.size());
1072 for (auto &it
: wires_
)
1073 if (design
->selected(this, it
.second
))
1074 result
.push_back(it
.second
);
1078 std::vector
<RTLIL::Cell
*> RTLIL::Module::selected_cells() const
1080 std::vector
<RTLIL::Cell
*> result
;
1081 result
.reserve(wires_
.size());
1082 for (auto &it
: cells_
)
1083 if (design
->selected(this, it
.second
))
1084 result
.push_back(it
.second
);
1088 void RTLIL::Module::add(RTLIL::Wire
*wire
)
1090 log_assert(!wire
->name
.empty());
1091 log_assert(count_id(wire
->name
) == 0);
1092 log_assert(refcount_wires_
== 0);
1093 wires_
[wire
->name
] = wire
;
1094 wire
->module
= this;
1097 void RTLIL::Module::add(RTLIL::Cell
*cell
)
1099 log_assert(!cell
->name
.empty());
1100 log_assert(count_id(cell
->name
) == 0);
1101 log_assert(refcount_cells_
== 0);
1102 cells_
[cell
->name
] = cell
;
1103 cell
->module
= this;
1107 struct DeleteWireWorker
1109 RTLIL::Module
*module
;
1110 const std::set
<RTLIL::Wire
*> *wires_p
;
1112 void operator()(RTLIL::SigSpec
&sig
) {
1113 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
1114 for (auto &c
: chunks
)
1115 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
1116 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
1125 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
1127 std::setPort
<RTLIL::Wire
*> wires_
;
1128 wires_
.insert(wire
);
1133 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
1135 log_assert(refcount_wires_
== 0);
1137 DeleteWireWorker delete_wire_worker
;
1138 delete_wire_worker
.module
= this;
1139 delete_wire_worker
.wires_p
= &wires
;
1140 rewrite_sigspecs(delete_wire_worker
);
1142 for (auto &it
: wires
) {
1143 log_assert(wires_
.count(it
->name
) != 0);
1144 wires_
.erase(it
->name
);
1149 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
1151 while (!cell
->connections_
.empty())
1152 cell
->unsetPort(cell
->connections_
.begin()->first
);
1154 log_assert(cells_
.count(cell
->name
) != 0);
1155 log_assert(refcount_cells_
== 0);
1156 cells_
.erase(cell
->name
);
1160 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
1162 log_assert(wires_
[wire
->name
] == wire
);
1163 log_assert(refcount_wires_
== 0);
1164 wires_
.erase(wire
->name
);
1165 wire
->name
= new_name
;
1169 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
1171 log_assert(cells_
[cell
->name
] == cell
);
1172 log_assert(refcount_wires_
== 0);
1173 cells_
.erase(cell
->name
);
1174 cell
->name
= new_name
;
1178 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
1180 log_assert(count_id(old_name
) != 0);
1181 if (wires_
.count(old_name
))
1182 rename(wires_
.at(old_name
), new_name
);
1183 else if (cells_
.count(old_name
))
1184 rename(cells_
.at(old_name
), new_name
);
1189 void RTLIL::Module::swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
)
1191 log_assert(wires_
[w1
->name
] == w1
);
1192 log_assert(wires_
[w2
->name
] == w2
);
1193 log_assert(refcount_wires_
== 0);
1195 wires_
.erase(w1
->name
);
1196 wires_
.erase(w2
->name
);
1198 std::swap(w1
->name
, w2
->name
);
1200 wires_
[w1
->name
] = w1
;
1201 wires_
[w2
->name
] = w2
;
1204 void RTLIL::Module::swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
)
1206 log_assert(cells_
[c1
->name
] == c1
);
1207 log_assert(cells_
[c2
->name
] == c2
);
1208 log_assert(refcount_cells_
== 0);
1210 cells_
.erase(c1
->name
);
1211 cells_
.erase(c2
->name
);
1213 std::swap(c1
->name
, c2
->name
);
1215 cells_
[c1
->name
] = c1
;
1216 cells_
[c2
->name
] = c2
;
1219 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
)
1222 return uniquify(name
, index
);
1225 RTLIL::IdString
RTLIL::Module::uniquify(RTLIL::IdString name
, int &index
)
1228 if (count_id(name
) == 0)
1234 RTLIL::IdString new_name
= stringf("%s_%d", name
.c_str(), index
);
1235 if (count_id(new_name
) == 0)
1241 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
1243 if (a
->port_id
&& !b
->port_id
)
1245 if (!a
->port_id
&& b
->port_id
)
1248 if (a
->port_id
== b
->port_id
)
1249 return a
->name
< b
->name
;
1250 return a
->port_id
< b
->port_id
;
1253 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
1255 for (auto mon
: monitors
)
1256 mon
->notify_connect(this, conn
);
1259 for (auto mon
: design
->monitors
)
1260 mon
->notify_connect(this, conn
);
1262 connections_
.push_back(conn
);
1265 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
1267 connect(RTLIL::SigSig(lhs
, rhs
));
1270 void RTLIL::Module::new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
)
1272 for (auto mon
: monitors
)
1273 mon
->notify_connect(this, new_conn
);
1276 for (auto mon
: design
->monitors
)
1277 mon
->notify_connect(this, new_conn
);
1279 connections_
= new_conn
;
1282 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
1284 return connections_
;
1287 void RTLIL::Module::fixup_ports()
1289 std::vector
<RTLIL::Wire
*> all_ports
;
1291 for (auto &w
: wires_
)
1292 if (w
.second
->port_input
|| w
.second
->port_output
)
1293 all_ports
.push_back(w
.second
);
1295 w
.second
->port_id
= 0;
1297 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
1300 for (size_t i
= 0; i
< all_ports
.size(); i
++) {
1301 ports
.push_back(all_ports
[i
]->name
);
1302 all_ports
[i
]->port_id
= i
+1;
1306 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
1308 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1310 wire
->width
= width
;
1315 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
1317 RTLIL::Wire
*wire
= addWire(name
);
1318 wire
->width
= other
->width
;
1319 wire
->start_offset
= other
->start_offset
;
1320 wire
->port_id
= other
->port_id
;
1321 wire
->port_input
= other
->port_input
;
1322 wire
->port_output
= other
->port_output
;
1323 wire
->upto
= other
->upto
;
1324 wire
->attributes
= other
->attributes
;
1328 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
1330 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1337 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1339 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1340 cell
->connections_
= other
->connections_
;
1341 cell
->parameters
= other
->parameters
;
1342 cell
->attributes
= other
->attributes
;
1346 #define DEF_METHOD(_func, _y_size, _type) \
1347 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1348 RTLIL::Cell *cell = addCell(name, _type); \
1349 cell->parameters["\\A_SIGNED"] = is_signed; \
1350 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1351 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1352 cell->setPort("\\A", sig_a); \
1353 cell->setPort("\\Y", sig_y); \
1356 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1357 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1358 add ## _func(name, sig_a, sig_y, is_signed); \
1361 DEF_METHOD(Not
, sig_a
.size(), "$not")
1362 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1363 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1364 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1365 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1366 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1367 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1368 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1369 DEF_METHOD(LogicNot
, 1, "$logic_not")
1372 #define DEF_METHOD(_func, _y_size, _type) \
1373 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1374 RTLIL::Cell *cell = addCell(name, _type); \
1375 cell->parameters["\\A_SIGNED"] = is_signed; \
1376 cell->parameters["\\B_SIGNED"] = is_signed; \
1377 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1378 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1379 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1380 cell->setPort("\\A", sig_a); \
1381 cell->setPort("\\B", sig_b); \
1382 cell->setPort("\\Y", sig_y); \
1385 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1386 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1387 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1390 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1391 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1392 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1393 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1394 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1395 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1396 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1397 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1398 DEF_METHOD(Shift
, sig_a
.size(), "$shift")
1399 DEF_METHOD(Shiftx
, sig_a
.size(), "$shiftx")
1400 DEF_METHOD(Lt
, 1, "$lt")
1401 DEF_METHOD(Le
, 1, "$le")
1402 DEF_METHOD(Eq
, 1, "$eq")
1403 DEF_METHOD(Ne
, 1, "$ne")
1404 DEF_METHOD(Eqx
, 1, "$eqx")
1405 DEF_METHOD(Nex
, 1, "$nex")
1406 DEF_METHOD(Ge
, 1, "$ge")
1407 DEF_METHOD(Gt
, 1, "$gt")
1408 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1409 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1410 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1411 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1412 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1413 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1414 DEF_METHOD(LogicOr
, 1, "$logic_or")
1417 #define DEF_METHOD(_func, _type, _pmux) \
1418 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) { \
1419 RTLIL::Cell *cell = addCell(name, _type); \
1420 cell->parameters["\\WIDTH"] = sig_a.size(); \
1421 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1422 cell->setPort("\\A", sig_a); \
1423 cell->setPort("\\B", sig_b); \
1424 cell->setPort("\\S", sig_s); \
1425 cell->setPort("\\Y", sig_y); \
1428 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1429 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1430 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1433 DEF_METHOD(Mux
, "$mux", 0)
1434 DEF_METHOD(Pmux
, "$pmux", 1)
1437 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1438 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1439 RTLIL::Cell *cell = addCell(name, _type); \
1440 cell->setPort("\\" #_P1, sig1); \
1441 cell->setPort("\\" #_P2, sig2); \
1444 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1) { \
1445 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1446 add ## _func(name, sig1, sig2); \
1449 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1450 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1451 RTLIL::Cell *cell = addCell(name, _type); \
1452 cell->setPort("\\" #_P1, sig1); \
1453 cell->setPort("\\" #_P2, sig2); \
1454 cell->setPort("\\" #_P3, sig3); \
1457 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2) { \
1458 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1459 add ## _func(name, sig1, sig2, sig3); \
1462 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1463 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1464 RTLIL::Cell *cell = addCell(name, _type); \
1465 cell->setPort("\\" #_P1, sig1); \
1466 cell->setPort("\\" #_P2, sig2); \
1467 cell->setPort("\\" #_P3, sig3); \
1468 cell->setPort("\\" #_P4, sig4); \
1471 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3) { \
1472 RTLIL::SigBit sig4 = addWire(NEW_ID); \
1473 add ## _func(name, sig1, sig2, sig3, sig4); \
1476 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
1477 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5) { \
1478 RTLIL::Cell *cell = addCell(name, _type); \
1479 cell->setPort("\\" #_P1, sig1); \
1480 cell->setPort("\\" #_P2, sig2); \
1481 cell->setPort("\\" #_P3, sig3); \
1482 cell->setPort("\\" #_P4, sig4); \
1483 cell->setPort("\\" #_P5, sig5); \
1486 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4) { \
1487 RTLIL::SigBit sig5 = addWire(NEW_ID); \
1488 add ## _func(name, sig1, sig2, sig3, sig4, sig5); \
1491 DEF_METHOD_2(NotGate
, "$_NOT_", A
, Y
)
1492 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1493 DEF_METHOD_3(NandGate
, "$_NAND_", A
, B
, Y
)
1494 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1495 DEF_METHOD_3(NorGate
, "$_NOR_", A
, B
, Y
)
1496 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1497 DEF_METHOD_3(XnorGate
, "$_XNOR_", A
, B
, Y
)
1498 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1499 DEF_METHOD_4(Aoi3Gate
, "$_AOI3_", A
, B
, C
, Y
)
1500 DEF_METHOD_4(Oai3Gate
, "$_OAI3_", A
, B
, C
, Y
)
1501 DEF_METHOD_5(Aoi4Gate
, "$_AOI4_", A
, B
, C
, D
, Y
)
1502 DEF_METHOD_5(Oai4Gate
, "$_OAI4_", A
, B
, C
, D
, Y
)
1508 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
)
1510 RTLIL::Cell
*cell
= addCell(name
, "$pow");
1511 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1512 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1513 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1514 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1515 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1516 cell
->setPort("\\A", sig_a
);
1517 cell
->setPort("\\B", sig_b
);
1518 cell
->setPort("\\Y", sig_y
);
1522 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1524 RTLIL::Cell
*cell
= addCell(name
, "$slice");
1525 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1526 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1527 cell
->parameters
["\\OFFSET"] = offset
;
1528 cell
->setPort("\\A", sig_a
);
1529 cell
->setPort("\\Y", sig_y
);
1533 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1535 RTLIL::Cell
*cell
= addCell(name
, "$concat");
1536 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1537 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1538 cell
->setPort("\\A", sig_a
);
1539 cell
->setPort("\\B", sig_b
);
1540 cell
->setPort("\\Y", sig_y
);
1544 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1546 RTLIL::Cell
*cell
= addCell(name
, "$lut");
1547 cell
->parameters
["\\LUT"] = lut
;
1548 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1549 cell
->setPort("\\A", sig_i
);
1550 cell
->setPort("\\Y", sig_o
);
1554 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1556 RTLIL::Cell
*cell
= addCell(name
, "$assert");
1557 cell
->setPort("\\A", sig_a
);
1558 cell
->setPort("\\EN", sig_en
);
1562 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
)
1564 RTLIL::Cell
*cell
= addCell(name
, "$sr");
1565 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1566 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1567 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1568 cell
->setPort("\\SET", sig_set
);
1569 cell
->setPort("\\CLR", sig_clr
);
1570 cell
->setPort("\\Q", sig_q
);
1574 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1576 RTLIL::Cell
*cell
= addCell(name
, "$dff");
1577 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1578 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1579 cell
->setPort("\\CLK", sig_clk
);
1580 cell
->setPort("\\D", sig_d
);
1581 cell
->setPort("\\Q", sig_q
);
1585 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1586 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1588 RTLIL::Cell
*cell
= addCell(name
, "$dffsr");
1589 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1590 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1591 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1592 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1593 cell
->setPort("\\CLK", sig_clk
);
1594 cell
->setPort("\\SET", sig_set
);
1595 cell
->setPort("\\CLR", sig_clr
);
1596 cell
->setPort("\\D", sig_d
);
1597 cell
->setPort("\\Q", sig_q
);
1601 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1602 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1604 RTLIL::Cell
*cell
= addCell(name
, "$adff");
1605 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1606 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1607 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1608 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1609 cell
->setPort("\\CLK", sig_clk
);
1610 cell
->setPort("\\ARST", sig_arst
);
1611 cell
->setPort("\\D", sig_d
);
1612 cell
->setPort("\\Q", sig_q
);
1616 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1618 RTLIL::Cell
*cell
= addCell(name
, "$dlatch");
1619 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1620 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1621 cell
->setPort("\\EN", sig_en
);
1622 cell
->setPort("\\D", sig_d
);
1623 cell
->setPort("\\Q", sig_q
);
1627 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1628 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1630 RTLIL::Cell
*cell
= addCell(name
, "$dlatchsr");
1631 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1632 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1633 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1634 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1635 cell
->setPort("\\EN", sig_en
);
1636 cell
->setPort("\\SET", sig_set
);
1637 cell
->setPort("\\CLR", sig_clr
);
1638 cell
->setPort("\\D", sig_d
);
1639 cell
->setPort("\\Q", sig_q
);
1643 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1645 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N'));
1646 cell
->setPort("\\C", sig_clk
);
1647 cell
->setPort("\\D", sig_d
);
1648 cell
->setPort("\\Q", sig_q
);
1652 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1653 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1655 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1656 cell
->setPort("\\C", sig_clk
);
1657 cell
->setPort("\\S", sig_set
);
1658 cell
->setPort("\\R", sig_clr
);
1659 cell
->setPort("\\D", sig_d
);
1660 cell
->setPort("\\Q", sig_q
);
1664 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1665 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1667 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0'));
1668 cell
->setPort("\\C", sig_clk
);
1669 cell
->setPort("\\R", sig_arst
);
1670 cell
->setPort("\\D", sig_d
);
1671 cell
->setPort("\\Q", sig_q
);
1675 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1677 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N'));
1678 cell
->setPort("\\E", sig_en
);
1679 cell
->setPort("\\D", sig_d
);
1680 cell
->setPort("\\Q", sig_q
);
1684 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1685 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1687 RTLIL::Cell
*cell
= addCell(name
, stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N'));
1688 cell
->setPort("\\E", sig_en
);
1689 cell
->setPort("\\S", sig_set
);
1690 cell
->setPort("\\R", sig_clr
);
1691 cell
->setPort("\\D", sig_d
);
1692 cell
->setPort("\\Q", sig_q
);
1704 port_output
= false;
1708 RTLIL::Memory::Memory()
1714 RTLIL::Cell::Cell() : module(nullptr)
1718 bool RTLIL::Cell::hasPort(RTLIL::IdString portname
) const
1720 return connections_
.count(portname
) != 0;
1723 void RTLIL::Cell::unsetPort(RTLIL::IdString portname
)
1725 RTLIL::SigSpec signal
;
1726 auto conn_it
= connections_
.find(portname
);
1728 if (conn_it
!= connections_
.end())
1730 for (auto mon
: module
->monitors
)
1731 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1734 for (auto mon
: module
->design
->monitors
)
1735 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1737 connections_
.erase(conn_it
);
1741 void RTLIL::Cell::setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1743 auto conn_it
= connections_
.find(portname
);
1745 if (conn_it
== connections_
.end()) {
1746 connections_
[portname
] = RTLIL::SigSpec();
1747 conn_it
= connections_
.find(portname
);
1748 log_assert(conn_it
!= connections_
.end());
1751 for (auto mon
: module
->monitors
)
1752 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1755 for (auto mon
: module
->design
->monitors
)
1756 mon
->notify_connect(this, conn_it
->first
, conn_it
->second
, signal
);
1758 conn_it
->second
= signal
;
1761 const RTLIL::SigSpec
&RTLIL::Cell::getPort(RTLIL::IdString portname
) const
1763 return connections_
.at(portname
);
1766 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1768 return connections_
;
1771 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname
) const
1773 return parameters
.count(paramname
);
1776 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname
)
1778 parameters
.erase(paramname
);
1781 void RTLIL::Cell::setParam(RTLIL::IdString paramname
, RTLIL::Const value
)
1783 parameters
[paramname
] = value
;
1786 const RTLIL::Const
&RTLIL::Cell::getParam(RTLIL::IdString paramname
) const
1788 return parameters
.at(paramname
);
1791 void RTLIL::Cell::check()
1794 InternalCellChecker
checker(NULL
, this);
1799 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1801 if (type
.substr(0, 1) != "$" || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1802 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:" || type
.substr(0, 8) == "$extern:")
1805 if (type
== "$mux" || type
== "$pmux") {
1806 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1807 if (type
== "$pmux")
1808 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1813 if (type
== "$lut") {
1814 parameters
["\\WIDTH"] = SIZE(connections_
["\\A"]);
1818 if (type
== "$fa") {
1819 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1823 if (type
== "$lcu") {
1824 parameters
["\\WIDTH"] = SIZE(connections_
["\\CO"]);
1828 bool signedness_ab
= !type
.in("$slice", "$concat", "$macc");
1830 if (connections_
.count("\\A")) {
1831 if (signedness_ab
) {
1833 parameters
["\\A_SIGNED"] = true;
1834 else if (parameters
.count("\\A_SIGNED") == 0)
1835 parameters
["\\A_SIGNED"] = false;
1837 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1840 if (connections_
.count("\\B")) {
1841 if (signedness_ab
) {
1843 parameters
["\\B_SIGNED"] = true;
1844 else if (parameters
.count("\\B_SIGNED") == 0)
1845 parameters
["\\B_SIGNED"] = false;
1847 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1850 if (connections_
.count("\\Y"))
1851 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1856 RTLIL::SigChunk::SigChunk()
1863 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1871 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1873 log_assert(wire
!= nullptr);
1875 this->width
= wire
->width
;
1879 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1881 log_assert(wire
!= nullptr);
1883 this->width
= width
;
1884 this->offset
= offset
;
1887 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1890 data
= RTLIL::Const(str
).bits
;
1895 RTLIL::SigChunk::SigChunk(int val
, int width
)
1898 data
= RTLIL::Const(val
, width
).bits
;
1899 this->width
= SIZE(data
);
1903 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1906 data
= RTLIL::Const(bit
, width
).bits
;
1907 this->width
= SIZE(data
);
1911 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1916 data
= RTLIL::Const(bit
.data
).bits
;
1918 offset
= bit
.offset
;
1922 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1924 RTLIL::SigChunk ret
;
1927 ret
.offset
= this->offset
+ offset
;
1930 for (int i
= 0; i
< length
; i
++)
1931 ret
.data
.push_back(data
[offset
+i
]);
1937 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1939 if (wire
&& other
.wire
)
1940 if (wire
->name
!= other
.wire
->name
)
1941 return wire
->name
< other
.wire
->name
;
1943 if (wire
!= other
.wire
)
1944 return wire
< other
.wire
;
1946 if (offset
!= other
.offset
)
1947 return offset
< other
.offset
;
1949 if (width
!= other
.width
)
1950 return width
< other
.width
;
1952 return data
< other
.data
;
1955 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1957 return wire
== other
.wire
&& width
== other
.width
&& offset
== other
.offset
&& data
== other
.data
;
1960 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1967 RTLIL::SigSpec::SigSpec()
1973 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1978 RTLIL::SigSpec::SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
)
1980 cover("kernel.rtlil.sigspec.init.list");
1985 std::vector
<RTLIL::SigSpec
> parts_vec(parts
.begin(), parts
.end());
1986 for (auto it
= parts_vec
.rbegin(); it
!= parts_vec
.rend(); it
++)
1990 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1992 cover("kernel.rtlil.sigspec.assign");
1994 width_
= other
.width_
;
1995 hash_
= other
.hash_
;
1996 chunks_
= other
.chunks_
;
1999 if (!other
.bits_
.empty())
2001 RTLIL::SigChunk
*last
= NULL
;
2002 int last_end_offset
= 0;
2004 for (auto &bit
: other
.bits_
) {
2005 if (last
&& bit
.wire
== last
->wire
) {
2006 if (bit
.wire
== NULL
) {
2007 last
->data
.push_back(bit
.data
);
2010 } else if (last_end_offset
== bit
.offset
) {
2016 chunks_
.push_back(bit
);
2017 last
= &chunks_
.back();
2018 last_end_offset
= bit
.offset
+ 1;
2027 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
2029 cover("kernel.rtlil.sigspec.init.const");
2031 chunks_
.push_back(RTLIL::SigChunk(value
));
2032 width_
= chunks_
.back().width
;
2037 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
2039 cover("kernel.rtlil.sigspec.init.chunk");
2041 chunks_
.push_back(chunk
);
2042 width_
= chunks_
.back().width
;
2047 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
2049 cover("kernel.rtlil.sigspec.init.wire");
2051 chunks_
.push_back(RTLIL::SigChunk(wire
));
2052 width_
= chunks_
.back().width
;
2057 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
2059 cover("kernel.rtlil.sigspec.init.wire_part");
2061 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
2062 width_
= chunks_
.back().width
;
2067 RTLIL::SigSpec::SigSpec(const std::string
&str
)
2069 cover("kernel.rtlil.sigspec.init.str");
2071 chunks_
.push_back(RTLIL::SigChunk(str
));
2072 width_
= chunks_
.back().width
;
2077 RTLIL::SigSpec::SigSpec(int val
, int width
)
2079 cover("kernel.rtlil.sigspec.init.int");
2081 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
2087 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
2089 cover("kernel.rtlil.sigspec.init.state");
2091 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
2097 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
2099 cover("kernel.rtlil.sigspec.init.bit");
2101 if (bit
.wire
== NULL
)
2102 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
2104 for (int i
= 0; i
< width
; i
++)
2105 chunks_
.push_back(bit
);
2111 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
2113 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2117 for (auto &c
: chunks
)
2122 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
2124 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2128 for (auto &bit
: bits
)
2133 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
2135 cover("kernel.rtlil.sigspec.init.stdset_bits");
2139 for (auto &bit
: bits
)
2144 void RTLIL::SigSpec::pack() const
2146 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2148 if (that
->bits_
.empty())
2151 cover("kernel.rtlil.sigspec.convert.pack");
2152 log_assert(that
->chunks_
.empty());
2154 std::vector
<RTLIL::SigBit
> old_bits
;
2155 old_bits
.swap(that
->bits_
);
2157 RTLIL::SigChunk
*last
= NULL
;
2158 int last_end_offset
= 0;
2160 for (auto &bit
: old_bits
) {
2161 if (last
&& bit
.wire
== last
->wire
) {
2162 if (bit
.wire
== NULL
) {
2163 last
->data
.push_back(bit
.data
);
2166 } else if (last_end_offset
== bit
.offset
) {
2172 that
->chunks_
.push_back(bit
);
2173 last
= &that
->chunks_
.back();
2174 last_end_offset
= bit
.offset
+ 1;
2180 void RTLIL::SigSpec::unpack() const
2182 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2184 if (that
->chunks_
.empty())
2187 cover("kernel.rtlil.sigspec.convert.unpack");
2188 log_assert(that
->bits_
.empty());
2190 that
->bits_
.reserve(that
->width_
);
2191 for (auto &c
: that
->chunks_
)
2192 for (int i
= 0; i
< c
.width
; i
++)
2193 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
2195 that
->chunks_
.clear();
2199 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
2201 void RTLIL::SigSpec::hash() const
2203 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
2205 if (that
->hash_
!= 0)
2208 cover("kernel.rtlil.sigspec.hash");
2212 for (auto &c
: that
->chunks_
)
2213 if (c
.wire
== NULL
) {
2214 for (auto &v
: c
.data
)
2215 DJB2(that
->hash_
, v
);
2217 DJB2(that
->hash_
, c
.wire
->name
.index_
);
2218 DJB2(that
->hash_
, c
.offset
);
2219 DJB2(that
->hash_
, c
.width
);
2222 if (that
->hash_
== 0)
2226 void RTLIL::SigSpec::sort()
2229 cover("kernel.rtlil.sigspec.sort");
2230 std::sort(bits_
.begin(), bits_
.end());
2233 void RTLIL::SigSpec::sort_and_unify()
2235 cover("kernel.rtlil.sigspec.sort_and_unify");
2236 *this = this->to_sigbit_set();
2239 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
2241 replace(pattern
, with
, this);
2244 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
2246 log_assert(pattern
.width_
== with
.width_
);
2251 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> rules
;
2253 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
2254 if (pattern
.bits_
[i
].wire
!= NULL
)
2255 rules
[pattern
.bits_
[i
]] = with
.bits_
[i
];
2257 replace(rules
, other
);
2260 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
)
2262 replace(rules
, this);
2265 void RTLIL::SigSpec::replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const
2267 cover("kernel.rtlil.sigspec.replace");
2269 log_assert(other
!= NULL
);
2270 log_assert(width_
== other
->width_
);
2275 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2276 auto it
= rules
.find(bits_
[i
]);
2277 if (it
!= rules
.end())
2278 other
->bits_
[i
] = it
->second
;
2284 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
2286 remove2(pattern
, NULL
);
2289 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
2291 RTLIL::SigSpec tmp
= *this;
2292 tmp
.remove2(pattern
, other
);
2295 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
2297 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2298 remove2(pattern_bits
, other
);
2301 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
)
2303 remove2(pattern
, NULL
);
2306 void RTLIL::SigSpec::remove(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const
2308 RTLIL::SigSpec tmp
= *this;
2309 tmp
.remove2(pattern
, other
);
2312 void RTLIL::SigSpec::remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
)
2315 cover("kernel.rtlil.sigspec.remove_other");
2317 cover("kernel.rtlil.sigspec.remove");
2321 if (other
!= NULL
) {
2322 log_assert(width_
== other
->width_
);
2326 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
2328 new_bits
.resize(SIZE(bits_
));
2330 new_other_bits
.resize(SIZE(bits_
));
2333 for (int i
= 0; i
< SIZE(bits_
); i
++) {
2334 if (bits_
[i
].wire
!= NULL
&& pattern
.count(bits_
[i
]))
2337 new_other_bits
[k
] = other
->bits_
[i
];
2338 new_bits
[k
++] = bits_
[i
];
2343 new_other_bits
.resize(k
);
2345 bits_
.swap(new_bits
);
2346 width_
= SIZE(bits_
);
2348 if (other
!= NULL
) {
2349 other
->bits_
.swap(new_other_bits
);
2350 other
->width_
= SIZE(other
->bits_
);
2356 RTLIL::SigSpec
RTLIL::SigSpec::extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
) const
2358 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
2359 return extract(pattern_bits
, other
);
2362 RTLIL::SigSpec
RTLIL::SigSpec::extract(const std::set
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
) const
2365 cover("kernel.rtlil.sigspec.extract_other");
2367 cover("kernel.rtlil.sigspec.extract");
2369 log_assert(other
== NULL
|| width_
== other
->width_
);
2371 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
2375 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
2376 for (int i
= 0; i
< width_
; i
++)
2377 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2378 ret
.append_bit(bits_other
[i
]);
2380 for (int i
= 0; i
< width_
; i
++)
2381 if (bits_match
[i
].wire
&& pattern
.count(bits_match
[i
]))
2382 ret
.append_bit(bits_match
[i
]);
2389 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
2391 cover("kernel.rtlil.sigspec.replace_pos");
2396 log_assert(offset
>= 0);
2397 log_assert(with
.width_
>= 0);
2398 log_assert(offset
+with
.width_
<= width_
);
2400 for (int i
= 0; i
< with
.width_
; i
++)
2401 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
2406 void RTLIL::SigSpec::remove_const()
2410 cover("kernel.rtlil.sigspec.remove_const.packed");
2412 std::vector
<RTLIL::SigChunk
> new_chunks
;
2413 new_chunks
.reserve(SIZE(chunks_
));
2416 for (auto &chunk
: chunks_
)
2417 if (chunk
.wire
!= NULL
) {
2418 new_chunks
.push_back(chunk
);
2419 width_
+= chunk
.width
;
2422 chunks_
.swap(new_chunks
);
2426 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2428 std::vector
<RTLIL::SigBit
> new_bits
;
2429 new_bits
.reserve(width_
);
2431 for (auto &bit
: bits_
)
2432 if (bit
.wire
!= NULL
)
2433 new_bits
.push_back(bit
);
2435 bits_
.swap(new_bits
);
2436 width_
= bits_
.size();
2442 void RTLIL::SigSpec::remove(int offset
, int length
)
2444 cover("kernel.rtlil.sigspec.remove_pos");
2448 log_assert(offset
>= 0);
2449 log_assert(length
>= 0);
2450 log_assert(offset
+ length
<= width_
);
2452 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2453 width_
= bits_
.size();
2458 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2461 cover("kernel.rtlil.sigspec.extract_pos");
2462 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2465 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2467 if (signal
.width_
== 0)
2475 cover("kernel.rtlil.sigspec.append");
2477 if (packed() != signal
.packed()) {
2483 for (auto &other_c
: signal
.chunks_
)
2485 auto &my_last_c
= chunks_
.back();
2486 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2487 auto &this_data
= my_last_c
.data
;
2488 auto &other_data
= other_c
.data
;
2489 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2490 my_last_c
.width
+= other_c
.width
;
2492 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2493 my_last_c
.width
+= other_c
.width
;
2495 chunks_
.push_back(other_c
);
2498 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2500 width_
+= signal
.width_
;
2504 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2508 cover("kernel.rtlil.sigspec.append_bit.packed");
2510 if (chunks_
.size() == 0)
2511 chunks_
.push_back(bit
);
2513 if (bit
.wire
== NULL
)
2514 if (chunks_
.back().wire
== NULL
) {
2515 chunks_
.back().data
.push_back(bit
.data
);
2516 chunks_
.back().width
++;
2518 chunks_
.push_back(bit
);
2520 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2521 chunks_
.back().width
++;
2523 chunks_
.push_back(bit
);
2527 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2528 bits_
.push_back(bit
);
2535 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2537 cover("kernel.rtlil.sigspec.extend");
2542 remove(width
, width_
- width
);
2544 if (width_
< width
) {
2545 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2546 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2547 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2548 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2549 while (width_
< width
)
2554 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2556 cover("kernel.rtlil.sigspec.extend_u0");
2561 remove(width
, width_
- width
);
2563 if (width_
< width
) {
2564 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2566 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2567 while (width_
< width
)
2573 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2575 cover("kernel.rtlil.sigspec.repeat");
2578 for (int i
= 0; i
< num
; i
++)
2584 void RTLIL::SigSpec::check() const
2588 cover("kernel.rtlil.sigspec.check.skip");
2592 cover("kernel.rtlil.sigspec.check.packed");
2595 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2596 const RTLIL::SigChunk chunk
= chunks_
[i
];
2597 if (chunk
.wire
== NULL
) {
2599 log_assert(chunks_
[i
-1].wire
!= NULL
);
2600 log_assert(chunk
.offset
== 0);
2601 log_assert(chunk
.data
.size() == (size_t)chunk
.width
);
2603 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2604 log_assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2605 log_assert(chunk
.offset
>= 0);
2606 log_assert(chunk
.width
>= 0);
2607 log_assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2608 log_assert(chunk
.data
.size() == 0);
2612 log_assert(w
== width_
);
2613 log_assert(bits_
.empty());
2617 cover("kernel.rtlil.sigspec.check.unpacked");
2619 log_assert(width_
== SIZE(bits_
));
2620 log_assert(chunks_
.empty());
2625 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2627 cover("kernel.rtlil.sigspec.comp_lt");
2632 if (width_
!= other
.width_
)
2633 return width_
< other
.width_
;
2638 if (chunks_
.size() != other
.chunks_
.size())
2639 return chunks_
.size() < other
.chunks_
.size();
2644 if (hash_
!= other
.hash_
)
2645 return hash_
< other
.hash_
;
2647 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2648 if (chunks_
[i
] != other
.chunks_
[i
]) {
2649 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2650 return chunks_
[i
] < other
.chunks_
[i
];
2653 cover("kernel.rtlil.sigspec.comp_lt.equal");
2657 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2659 cover("kernel.rtlil.sigspec.comp_eq");
2664 if (width_
!= other
.width_
)
2670 if (chunks_
.size() != chunks_
.size())
2676 if (hash_
!= other
.hash_
)
2679 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2680 if (chunks_
[i
] != other
.chunks_
[i
]) {
2681 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2685 cover("kernel.rtlil.sigspec.comp_eq.equal");
2689 bool RTLIL::SigSpec::is_wire() const
2691 cover("kernel.rtlil.sigspec.is_wire");
2694 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2697 bool RTLIL::SigSpec::is_chunk() const
2699 cover("kernel.rtlil.sigspec.is_chunk");
2702 return SIZE(chunks_
) == 1;
2705 bool RTLIL::SigSpec::is_fully_const() const
2707 cover("kernel.rtlil.sigspec.is_fully_const");
2710 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2711 if (it
->width
> 0 && it
->wire
!= NULL
)
2716 bool RTLIL::SigSpec::is_fully_def() const
2718 cover("kernel.rtlil.sigspec.is_fully_def");
2721 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2722 if (it
->width
> 0 && it
->wire
!= NULL
)
2724 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2725 if (it
->data
[i
] != RTLIL::State::S0
&& it
->data
[i
] != RTLIL::State::S1
)
2731 bool RTLIL::SigSpec::is_fully_undef() const
2733 cover("kernel.rtlil.sigspec.is_fully_undef");
2736 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2737 if (it
->width
> 0 && it
->wire
!= NULL
)
2739 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2740 if (it
->data
[i
] != RTLIL::State::Sx
&& it
->data
[i
] != RTLIL::State::Sz
)
2746 bool RTLIL::SigSpec::has_marked_bits() const
2748 cover("kernel.rtlil.sigspec.has_marked_bits");
2751 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2752 if (it
->width
> 0 && it
->wire
== NULL
) {
2753 for (size_t i
= 0; i
< it
->data
.size(); i
++)
2754 if (it
->data
[i
] == RTLIL::State::Sm
)
2760 bool RTLIL::SigSpec::as_bool() const
2762 cover("kernel.rtlil.sigspec.as_bool");
2765 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2767 return RTLIL::Const(chunks_
[0].data
).as_bool();
2771 int RTLIL::SigSpec::as_int(bool is_signed
) const
2773 cover("kernel.rtlil.sigspec.as_int");
2776 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2778 return RTLIL::Const(chunks_
[0].data
).as_int(is_signed
);
2782 std::string
RTLIL::SigSpec::as_string() const
2784 cover("kernel.rtlil.sigspec.as_string");
2788 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2789 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2790 if (chunk
.wire
!= NULL
)
2791 for (int j
= 0; j
< chunk
.width
; j
++)
2794 str
+= RTLIL::Const(chunk
.data
).as_string();
2799 RTLIL::Const
RTLIL::SigSpec::as_const() const
2801 cover("kernel.rtlil.sigspec.as_const");
2804 log_assert(is_fully_const() && SIZE(chunks_
) <= 1);
2806 return chunks_
[0].data
;
2807 return RTLIL::Const();
2810 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2812 cover("kernel.rtlil.sigspec.as_wire");
2815 log_assert(is_wire());
2816 return chunks_
[0].wire
;
2819 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2821 cover("kernel.rtlil.sigspec.as_chunk");
2824 log_assert(is_chunk());
2828 bool RTLIL::SigSpec::match(std::string pattern
) const
2830 cover("kernel.rtlil.sigspec.match");
2833 std::string str
= as_string();
2834 log_assert(pattern
.size() == str
.size());
2836 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2837 if (pattern
[i
] == ' ')
2839 if (pattern
[i
] == '*') {
2840 if (str
[i
] != 'z' && str
[i
] != 'x')
2844 if (pattern
[i
] != str
[i
])
2851 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2853 cover("kernel.rtlil.sigspec.to_sigbit_set");
2856 std::set
<RTLIL::SigBit
> sigbits
;
2857 for (auto &c
: chunks_
)
2858 for (int i
= 0; i
< c
.width
; i
++)
2859 sigbits
.insert(RTLIL::SigBit(c
, i
));
2863 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2865 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2871 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec
&other
) const
2873 cover("kernel.rtlil.sigspec.to_sigbit_map");
2878 log_assert(width_
== other
.width_
);
2880 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_map
;
2881 for (int i
= 0; i
< width_
; i
++)
2882 new_map
[bits_
[i
]] = other
.bits_
[i
];
2887 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2889 cover("kernel.rtlil.sigspec.to_single_sigbit");
2892 log_assert(width_
== 1);
2893 for (auto &c
: chunks_
)
2895 return RTLIL::SigBit(c
);
2899 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2901 size_t start
= 0, end
= 0;
2902 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2903 tokens
.push_back(text
.substr(start
, end
- start
));
2906 tokens
.push_back(text
.substr(start
));
2909 static int sigspec_parse_get_dummy_line_num()
2914 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2916 cover("kernel.rtlil.sigspec.parse");
2918 std::vector
<std::string
> tokens
;
2919 sigspec_parse_split(tokens
, str
, ',');
2921 sig
= RTLIL::SigSpec();
2922 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2924 std::string netname
= tokens
[tokidx
];
2925 std::string indices
;
2927 if (netname
.size() == 0)
2930 if ('0' <= netname
[0] && netname
[0] <= '9') {
2931 cover("kernel.rtlil.sigspec.parse.const");
2932 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2933 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2936 sig
.append(RTLIL::Const(ast
->bits
));
2944 cover("kernel.rtlil.sigspec.parse.net");
2946 if (netname
[0] != '$' && netname
[0] != '\\')
2947 netname
= "\\" + netname
;
2949 if (module
->wires_
.count(netname
) == 0) {
2950 size_t indices_pos
= netname
.size()-1;
2951 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2954 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2955 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2957 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2959 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2960 indices
= netname
.substr(indices_pos
);
2961 netname
= netname
.substr(0, indices_pos
);
2966 if (module
->wires_
.count(netname
) == 0)
2969 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2970 if (!indices
.empty()) {
2971 std::vector
<std::string
> index_tokens
;
2972 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2973 if (index_tokens
.size() == 1) {
2974 cover("kernel.rtlil.sigspec.parse.bit_sel");
2975 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2977 cover("kernel.rtlil.sigspec.parse.part_sel");
2978 int a
= atoi(index_tokens
.at(0).c_str());
2979 int b
= atoi(index_tokens
.at(1).c_str());
2984 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2993 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2995 if (str
.empty() || str
[0] != '@')
2996 return parse(sig
, module
, str
);
2998 cover("kernel.rtlil.sigspec.parse.sel");
3000 str
= RTLIL::escape_id(str
.substr(1));
3001 if (design
->selection_vars
.count(str
) == 0)
3004 sig
= RTLIL::SigSpec();
3005 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
3006 for (auto &it
: module
->wires_
)
3007 if (sel
.selected_member(module
->name
, it
.first
))
3008 sig
.append(it
.second
);
3013 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
3016 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
3017 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
3022 cover("kernel.rtlil.sigspec.parse.rhs_ones");
3023 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
3027 if (lhs
.chunks_
.size() == 1) {
3028 char *p
= (char*)str
.c_str(), *endptr
;
3029 long long int val
= strtoll(p
, &endptr
, 10);
3030 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
3031 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
3032 cover("kernel.rtlil.sigspec.parse.rhs_dec");
3037 return parse(sig
, module
, str
);
3040 RTLIL::CaseRule::~CaseRule()
3042 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
3046 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
3048 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
3049 new_caserule
->compare
= compare
;
3050 new_caserule
->actions
= actions
;
3051 for (auto &it
: switches
)
3052 new_caserule
->switches
.push_back(it
->clone());
3053 return new_caserule
;
3056 RTLIL::SwitchRule::~SwitchRule()
3058 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
3062 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
3064 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
3065 new_switchrule
->signal
= signal
;
3066 new_switchrule
->attributes
= attributes
;
3067 for (auto &it
: cases
)
3068 new_switchrule
->cases
.push_back(it
->clone());
3069 return new_switchrule
;
3073 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
3075 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
3076 new_syncrule
->type
= type
;
3077 new_syncrule
->signal
= signal
;
3078 new_syncrule
->actions
= actions
;
3079 return new_syncrule
;
3082 RTLIL::Process::~Process()
3084 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
3088 RTLIL::Process
*RTLIL::Process::clone() const
3090 RTLIL::Process
*new_proc
= new RTLIL::Process
;
3092 new_proc
->name
= name
;
3093 new_proc
->attributes
= attributes
;
3095 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
3096 new_proc
->root_case
= *rc_ptr
;
3097 rc_ptr
->switches
.clear();
3100 for (auto &it
: syncs
)
3101 new_proc
->syncs
.push_back(it
->clone());