2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/compatibility.h"
21 #include "kernel/rtlil.h"
22 #include "kernel/log.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
30 int RTLIL::autoidx
= 1;
34 flags
= RTLIL::CONST_FLAG_NONE
;
37 RTLIL::Const::Const(std::string str
)
39 flags
= RTLIL::CONST_FLAG_STRING
;
40 for (int i
= str
.size()-1; i
>= 0; i
--) {
41 unsigned char ch
= str
[i
];
42 for (int j
= 0; j
< 8; j
++) {
43 bits
.push_back((ch
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
49 RTLIL::Const::Const(int val
, int width
)
51 flags
= RTLIL::CONST_FLAG_NONE
;
52 for (int i
= 0; i
< width
; i
++) {
53 bits
.push_back((val
& 1) != 0 ? RTLIL::S1
: RTLIL::S0
);
58 RTLIL::Const::Const(RTLIL::State bit
, int width
)
60 flags
= RTLIL::CONST_FLAG_NONE
;
61 for (int i
= 0; i
< width
; i
++)
65 bool RTLIL::Const::operator <(const RTLIL::Const
&other
) const
67 if (bits
.size() != other
.bits
.size())
68 return bits
.size() < other
.bits
.size();
69 for (size_t i
= 0; i
< bits
.size(); i
++)
70 if (bits
[i
] != other
.bits
[i
])
71 return bits
[i
] < other
.bits
[i
];
75 bool RTLIL::Const::operator ==(const RTLIL::Const
&other
) const
77 return bits
== other
.bits
;
80 bool RTLIL::Const::operator !=(const RTLIL::Const
&other
) const
82 return bits
!= other
.bits
;
85 bool RTLIL::Const::as_bool() const
87 for (size_t i
= 0; i
< bits
.size(); i
++)
88 if (bits
[i
] == RTLIL::S1
)
93 int RTLIL::Const::as_int() const
96 for (size_t i
= 0; i
< bits
.size() && i
< 32; i
++)
97 if (bits
[i
] == RTLIL::S1
)
102 std::string
RTLIL::Const::as_string() const
105 for (size_t i
= bits
.size(); i
> 0; i
--)
107 case S0
: ret
+= "0"; break;
108 case S1
: ret
+= "1"; break;
109 case Sx
: ret
+= "x"; break;
110 case Sz
: ret
+= "z"; break;
111 case Sa
: ret
+= "-"; break;
112 case Sm
: ret
+= "m"; break;
117 std::string
RTLIL::Const::decode_string() const
120 std::vector
<char> string_chars
;
121 for (int i
= 0; i
< int (bits
.size()); i
+= 8) {
123 for (int j
= 0; j
< 8 && i
+ j
< int (bits
.size()); j
++)
124 if (bits
[i
+ j
] == RTLIL::State::S1
)
127 string_chars
.push_back(ch
);
129 for (int i
= int (string_chars
.size()) - 1; i
>= 0; i
--)
130 string
+= string_chars
[i
];
134 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name
) const
138 if (selected_modules
.count(mod_name
) > 0)
140 if (selected_members
.count(mod_name
) > 0)
145 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name
) const
149 if (selected_modules
.count(mod_name
) > 0)
154 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
158 if (selected_modules
.count(mod_name
) > 0)
160 if (selected_members
.count(mod_name
) > 0)
161 if (selected_members
.at(mod_name
).count(memb_name
) > 0)
166 void RTLIL::Selection::optimize(RTLIL::Design
*design
)
168 if (full_selection
) {
169 selected_modules
.clear();
170 selected_members
.clear();
174 std::vector
<RTLIL::IdString
> del_list
, add_list
;
177 for (auto mod_name
: selected_modules
) {
178 if (design
->modules_
.count(mod_name
) == 0)
179 del_list
.push_back(mod_name
);
180 selected_members
.erase(mod_name
);
182 for (auto mod_name
: del_list
)
183 selected_modules
.erase(mod_name
);
186 for (auto &it
: selected_members
)
187 if (design
->modules_
.count(it
.first
) == 0)
188 del_list
.push_back(it
.first
);
189 for (auto mod_name
: del_list
)
190 selected_members
.erase(mod_name
);
192 for (auto &it
: selected_members
) {
194 for (auto memb_name
: it
.second
)
195 if (design
->modules_
[it
.first
]->count_id(memb_name
) == 0)
196 del_list
.push_back(memb_name
);
197 for (auto memb_name
: del_list
)
198 it
.second
.erase(memb_name
);
203 for (auto &it
: selected_members
)
204 if (it
.second
.size() == 0)
205 del_list
.push_back(it
.first
);
206 else if (it
.second
.size() == design
->modules_
[it
.first
]->wires_
.size() + design
->modules_
[it
.first
]->memories
.size() +
207 design
->modules_
[it
.first
]->cells_
.size() + design
->modules_
[it
.first
]->processes
.size())
208 add_list
.push_back(it
.first
);
209 for (auto mod_name
: del_list
)
210 selected_members
.erase(mod_name
);
211 for (auto mod_name
: add_list
) {
212 selected_members
.erase(mod_name
);
213 selected_modules
.insert(mod_name
);
216 if (selected_modules
.size() == design
->modules_
.size()) {
217 full_selection
= true;
218 selected_modules
.clear();
219 selected_members
.clear();
223 RTLIL::Design::~Design()
225 for (auto it
= modules_
.begin(); it
!= modules_
.end(); it
++)
229 void RTLIL::Design::check()
232 for (auto &it
: modules_
) {
233 assert(it
.first
== it
.second
->name
);
234 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
240 void RTLIL::Design::optimize()
242 for (auto &it
: modules_
)
243 it
.second
->optimize();
244 for (auto &it
: selection_stack
)
246 for (auto &it
: selection_vars
)
247 it
.second
.optimize(this);
250 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name
) const
252 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
254 if (selection_stack
.size() == 0)
256 return selection_stack
.back().selected_module(mod_name
);
259 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name
) const
261 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
263 if (selection_stack
.size() == 0)
265 return selection_stack
.back().selected_whole_module(mod_name
);
268 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const
270 if (!selected_active_module
.empty() && mod_name
!= selected_active_module
)
272 if (selection_stack
.size() == 0)
274 return selection_stack
.back().selected_member(mod_name
, memb_name
);
277 bool RTLIL::Design::selected_module(RTLIL::Module
*mod
) const
279 return selected_module(mod
->name
);
282 bool RTLIL::Design::selected_whole_module(RTLIL::Module
*mod
) const
284 return selected_whole_module(mod
->name
);
287 RTLIL::Module::Module()
293 RTLIL::Module::~Module()
295 for (auto it
= wires_
.begin(); it
!= wires_
.end(); it
++)
297 for (auto it
= memories
.begin(); it
!= memories
.end(); it
++)
299 for (auto it
= cells_
.begin(); it
!= cells_
.end(); it
++)
301 for (auto it
= processes
.begin(); it
!= processes
.end(); it
++)
305 RTLIL::IdString
RTLIL::Module::derive(RTLIL::Design
*, std::map
<RTLIL::IdString
, RTLIL::Const
>)
307 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name
));
310 size_t RTLIL::Module::count_id(RTLIL::IdString id
)
312 return wires_
.count(id
) + memories
.count(id
) + cells_
.count(id
) + processes
.count(id
);
317 struct InternalCellChecker
319 RTLIL::Module
*module
;
321 std::set
<RTLIL::IdString
> expected_params
, expected_ports
;
323 InternalCellChecker(RTLIL::Module
*module
, RTLIL::Cell
*cell
) : module(module
), cell(cell
) { }
325 void error(int linenr
)
330 FILE *f
= open_memstream(&ptr
, &size
);
331 ILANG_BACKEND::dump_cell(f
, " ", cell
);
335 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
336 module
? module
->name
.c_str() : "", module
? "." : "",
337 cell
->name
.c_str(), cell
->type
.c_str(), __FILE__
, linenr
, ptr
);
340 int param(const char *name
)
342 if (cell
->parameters
.count(name
) == 0)
344 expected_params
.insert(name
);
345 return cell
->parameters
.at(name
).as_int();
348 int param_bool(const char *name
)
351 if (cell
->parameters
.at(name
).bits
.size() > 32)
353 if (v
!= 0 && v
!= 1)
358 void param_bits(const char *name
, int width
)
361 if (int(cell
->parameters
.at(name
).bits
.size()) != width
)
365 void port(const char *name
, int width
)
367 if (!cell
->has(name
))
369 if (cell
->get(name
).size() != width
)
371 expected_ports
.insert(name
);
374 void check_expected(bool check_matched_sign
= true)
376 for (auto ¶
: cell
->parameters
)
377 if (expected_params
.count(para
.first
) == 0)
379 for (auto &conn
: cell
->connections())
380 if (expected_ports
.count(conn
.first
) == 0)
383 if (expected_params
.count("\\A_SIGNED") != 0 && expected_params
.count("\\B_SIGNED") && check_matched_sign
) {
384 bool a_is_signed
= param("\\A_SIGNED") != 0;
385 bool b_is_signed
= param("\\B_SIGNED") != 0;
386 if (a_is_signed
!= b_is_signed
)
391 void check_gate(const char *ports
)
393 if (cell
->parameters
.size() != 0)
396 for (const char *p
= ports
; *p
; p
++) {
397 char portname
[3] = { '\\', *p
, 0 };
398 if (!cell
->has(portname
))
400 if (cell
->get(portname
).size() != 1)
404 for (auto &conn
: cell
->connections()) {
405 if (conn
.first
.size() != 2 || conn
.first
.at(0) != '\\')
407 if (strchr(ports
, conn
.first
.at(1)) == NULL
)
414 if (cell
->type
[0] != '$' || cell
->type
.substr(0, 3) == "$__" || cell
->type
.substr(0, 8) == "$paramod" ||
415 cell
->type
.substr(0, 9) == "$verific$" || cell
->type
.substr(0, 7) == "$array:")
418 if (cell
->type
== "$not" || cell
->type
== "$pos" || cell
->type
== "$bu0" || cell
->type
== "$neg") {
419 param_bool("\\A_SIGNED");
420 port("\\A", param("\\A_WIDTH"));
421 port("\\Y", param("\\Y_WIDTH"));
426 if (cell
->type
== "$and" || cell
->type
== "$or" || cell
->type
== "$xor" || cell
->type
== "$xnor") {
427 param_bool("\\A_SIGNED");
428 param_bool("\\B_SIGNED");
429 port("\\A", param("\\A_WIDTH"));
430 port("\\B", param("\\B_WIDTH"));
431 port("\\Y", param("\\Y_WIDTH"));
436 if (cell
->type
== "$reduce_and" || cell
->type
== "$reduce_or" || cell
->type
== "$reduce_xor" ||
437 cell
->type
== "$reduce_xnor" || cell
->type
== "$reduce_bool") {
438 param_bool("\\A_SIGNED");
439 port("\\A", param("\\A_WIDTH"));
440 port("\\Y", param("\\Y_WIDTH"));
445 if (cell
->type
== "$shl" || cell
->type
== "$shr" || cell
->type
== "$sshl" || cell
->type
== "$sshr") {
446 param_bool("\\A_SIGNED");
447 param_bool("\\B_SIGNED");
448 port("\\A", param("\\A_WIDTH"));
449 port("\\B", param("\\B_WIDTH"));
450 port("\\Y", param("\\Y_WIDTH"));
451 check_expected(false);
455 if (cell
->type
== "$lt" || cell
->type
== "$le" || cell
->type
== "$eq" || cell
->type
== "$ne" ||
456 cell
->type
== "$eqx" || cell
->type
== "$nex" || cell
->type
== "$ge" || cell
->type
== "$gt") {
457 param_bool("\\A_SIGNED");
458 param_bool("\\B_SIGNED");
459 port("\\A", param("\\A_WIDTH"));
460 port("\\B", param("\\B_WIDTH"));
461 port("\\Y", param("\\Y_WIDTH"));
466 if (cell
->type
== "$add" || cell
->type
== "$sub" || cell
->type
== "$mul" || cell
->type
== "$div" ||
467 cell
->type
== "$mod" || cell
->type
== "$pow") {
468 param_bool("\\A_SIGNED");
469 param_bool("\\B_SIGNED");
470 port("\\A", param("\\A_WIDTH"));
471 port("\\B", param("\\B_WIDTH"));
472 port("\\Y", param("\\Y_WIDTH"));
473 check_expected(cell
->type
!= "$pow");
477 if (cell
->type
== "$logic_not") {
478 param_bool("\\A_SIGNED");
479 port("\\A", param("\\A_WIDTH"));
480 port("\\Y", param("\\Y_WIDTH"));
485 if (cell
->type
== "$logic_and" || cell
->type
== "$logic_or") {
486 param_bool("\\A_SIGNED");
487 param_bool("\\B_SIGNED");
488 port("\\A", param("\\A_WIDTH"));
489 port("\\B", param("\\B_WIDTH"));
490 port("\\Y", param("\\Y_WIDTH"));
491 check_expected(false);
495 if (cell
->type
== "$slice") {
497 port("\\A", param("\\A_WIDTH"));
498 port("\\Y", param("\\Y_WIDTH"));
499 if (param("\\OFFSET") + param("\\Y_WIDTH") > param("\\A_WIDTH"))
505 if (cell
->type
== "$concat") {
506 port("\\A", param("\\A_WIDTH"));
507 port("\\B", param("\\B_WIDTH"));
508 port("\\Y", param("\\A_WIDTH") + param("\\B_WIDTH"));
513 if (cell
->type
== "$mux") {
514 port("\\A", param("\\WIDTH"));
515 port("\\B", param("\\WIDTH"));
517 port("\\Y", param("\\WIDTH"));
522 if (cell
->type
== "$pmux" || cell
->type
== "$safe_pmux") {
523 port("\\A", param("\\WIDTH"));
524 port("\\B", param("\\WIDTH") * param("\\S_WIDTH"));
525 port("\\S", param("\\S_WIDTH"));
526 port("\\Y", param("\\WIDTH"));
531 if (cell
->type
== "$lut") {
533 port("\\I", param("\\WIDTH"));
539 if (cell
->type
== "$sr") {
540 param_bool("\\SET_POLARITY");
541 param_bool("\\CLR_POLARITY");
542 port("\\SET", param("\\WIDTH"));
543 port("\\CLR", param("\\WIDTH"));
544 port("\\Q", param("\\WIDTH"));
549 if (cell
->type
== "$dff") {
550 param_bool("\\CLK_POLARITY");
552 port("\\D", param("\\WIDTH"));
553 port("\\Q", param("\\WIDTH"));
558 if (cell
->type
== "$dffsr") {
559 param_bool("\\CLK_POLARITY");
560 param_bool("\\SET_POLARITY");
561 param_bool("\\CLR_POLARITY");
563 port("\\SET", param("\\WIDTH"));
564 port("\\CLR", param("\\WIDTH"));
565 port("\\D", param("\\WIDTH"));
566 port("\\Q", param("\\WIDTH"));
571 if (cell
->type
== "$adff") {
572 param_bool("\\CLK_POLARITY");
573 param_bool("\\ARST_POLARITY");
574 param_bits("\\ARST_VALUE", param("\\WIDTH"));
577 port("\\D", param("\\WIDTH"));
578 port("\\Q", param("\\WIDTH"));
583 if (cell
->type
== "$dlatch") {
584 param_bool("\\EN_POLARITY");
586 port("\\D", param("\\WIDTH"));
587 port("\\Q", param("\\WIDTH"));
592 if (cell
->type
== "$dlatchsr") {
593 param_bool("\\EN_POLARITY");
594 param_bool("\\SET_POLARITY");
595 param_bool("\\CLR_POLARITY");
597 port("\\SET", param("\\WIDTH"));
598 port("\\CLR", param("\\WIDTH"));
599 port("\\D", param("\\WIDTH"));
600 port("\\Q", param("\\WIDTH"));
605 if (cell
->type
== "$fsm") {
607 param_bool("\\CLK_POLARITY");
608 param_bool("\\ARST_POLARITY");
609 param("\\STATE_BITS");
610 param("\\STATE_NUM");
611 param("\\STATE_NUM_LOG2");
612 param("\\STATE_RST");
613 param_bits("\\STATE_TABLE", param("\\STATE_BITS") * param("\\STATE_NUM"));
614 param("\\TRANS_NUM");
615 param_bits("\\TRANS_TABLE", param("\\TRANS_NUM") * (2*param("\\STATE_NUM_LOG2") + param("\\CTRL_IN_WIDTH") + param("\\CTRL_OUT_WIDTH")));
618 port("\\CTRL_IN", param("\\CTRL_IN_WIDTH"));
619 port("\\CTRL_OUT", param("\\CTRL_OUT_WIDTH"));
624 if (cell
->type
== "$memrd") {
626 param_bool("\\CLK_ENABLE");
627 param_bool("\\CLK_POLARITY");
628 param_bool("\\TRANSPARENT");
630 port("\\ADDR", param("\\ABITS"));
631 port("\\DATA", param("\\WIDTH"));
636 if (cell
->type
== "$memwr") {
638 param_bool("\\CLK_ENABLE");
639 param_bool("\\CLK_POLARITY");
642 port("\\EN", param("\\WIDTH"));
643 port("\\ADDR", param("\\ABITS"));
644 port("\\DATA", param("\\WIDTH"));
649 if (cell
->type
== "$mem") {
653 param_bits("\\RD_CLK_ENABLE", param("\\RD_PORTS"));
654 param_bits("\\RD_CLK_POLARITY", param("\\RD_PORTS"));
655 param_bits("\\RD_TRANSPARENT", param("\\RD_PORTS"));
656 param_bits("\\WR_CLK_ENABLE", param("\\WR_PORTS"));
657 param_bits("\\WR_CLK_POLARITY", param("\\WR_PORTS"));
658 port("\\RD_CLK", param("\\RD_PORTS"));
659 port("\\RD_ADDR", param("\\RD_PORTS") * param("\\ABITS"));
660 port("\\RD_DATA", param("\\RD_PORTS") * param("\\WIDTH"));
661 port("\\WR_CLK", param("\\WR_PORTS"));
662 port("\\WR_EN", param("\\WR_PORTS") * param("\\WIDTH"));
663 port("\\WR_ADDR", param("\\WR_PORTS") * param("\\ABITS"));
664 port("\\WR_DATA", param("\\WR_PORTS") * param("\\WIDTH"));
669 if (cell
->type
== "$assert") {
676 if (cell
->type
== "$_INV_") { check_gate("AY"); return; }
677 if (cell
->type
== "$_AND_") { check_gate("ABY"); return; }
678 if (cell
->type
== "$_OR_") { check_gate("ABY"); return; }
679 if (cell
->type
== "$_XOR_") { check_gate("ABY"); return; }
680 if (cell
->type
== "$_MUX_") { check_gate("ABSY"); return; }
682 if (cell
->type
== "$_SR_NN_") { check_gate("SRQ"); return; }
683 if (cell
->type
== "$_SR_NP_") { check_gate("SRQ"); return; }
684 if (cell
->type
== "$_SR_PN_") { check_gate("SRQ"); return; }
685 if (cell
->type
== "$_SR_PP_") { check_gate("SRQ"); return; }
687 if (cell
->type
== "$_DFF_N_") { check_gate("DQC"); return; }
688 if (cell
->type
== "$_DFF_P_") { check_gate("DQC"); return; }
690 if (cell
->type
== "$_DFF_NN0_") { check_gate("DQCR"); return; }
691 if (cell
->type
== "$_DFF_NN1_") { check_gate("DQCR"); return; }
692 if (cell
->type
== "$_DFF_NP0_") { check_gate("DQCR"); return; }
693 if (cell
->type
== "$_DFF_NP1_") { check_gate("DQCR"); return; }
694 if (cell
->type
== "$_DFF_PN0_") { check_gate("DQCR"); return; }
695 if (cell
->type
== "$_DFF_PN1_") { check_gate("DQCR"); return; }
696 if (cell
->type
== "$_DFF_PP0_") { check_gate("DQCR"); return; }
697 if (cell
->type
== "$_DFF_PP1_") { check_gate("DQCR"); return; }
699 if (cell
->type
== "$_DFFSR_NNN_") { check_gate("CSRDQ"); return; }
700 if (cell
->type
== "$_DFFSR_NNP_") { check_gate("CSRDQ"); return; }
701 if (cell
->type
== "$_DFFSR_NPN_") { check_gate("CSRDQ"); return; }
702 if (cell
->type
== "$_DFFSR_NPP_") { check_gate("CSRDQ"); return; }
703 if (cell
->type
== "$_DFFSR_PNN_") { check_gate("CSRDQ"); return; }
704 if (cell
->type
== "$_DFFSR_PNP_") { check_gate("CSRDQ"); return; }
705 if (cell
->type
== "$_DFFSR_PPN_") { check_gate("CSRDQ"); return; }
706 if (cell
->type
== "$_DFFSR_PPP_") { check_gate("CSRDQ"); return; }
708 if (cell
->type
== "$_DLATCH_N_") { check_gate("EDQ"); return; }
709 if (cell
->type
== "$_DLATCH_P_") { check_gate("EDQ"); return; }
711 if (cell
->type
== "$_DLATCHSR_NNN_") { check_gate("ESRDQ"); return; }
712 if (cell
->type
== "$_DLATCHSR_NNP_") { check_gate("ESRDQ"); return; }
713 if (cell
->type
== "$_DLATCHSR_NPN_") { check_gate("ESRDQ"); return; }
714 if (cell
->type
== "$_DLATCHSR_NPP_") { check_gate("ESRDQ"); return; }
715 if (cell
->type
== "$_DLATCHSR_PNN_") { check_gate("ESRDQ"); return; }
716 if (cell
->type
== "$_DLATCHSR_PNP_") { check_gate("ESRDQ"); return; }
717 if (cell
->type
== "$_DLATCHSR_PPN_") { check_gate("ESRDQ"); return; }
718 if (cell
->type
== "$_DLATCHSR_PPP_") { check_gate("ESRDQ"); return; }
726 void RTLIL::Module::check()
729 for (auto &it
: wires_
) {
730 assert(it
.first
== it
.second
->name
);
731 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
732 assert(it
.second
->width
>= 0);
733 assert(it
.second
->port_id
>= 0);
734 for (auto &it2
: it
.second
->attributes
) {
735 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
739 for (auto &it
: memories
) {
740 assert(it
.first
== it
.second
->name
);
741 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
742 assert(it
.second
->width
>= 0);
743 assert(it
.second
->size
>= 0);
744 for (auto &it2
: it
.second
->attributes
) {
745 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
749 for (auto &it
: cells_
) {
750 assert(it
.first
== it
.second
->name
);
751 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
752 assert(it
.second
->type
.size() > 0 && (it
.second
->type
[0] == '\\' || it
.second
->type
[0] == '$'));
753 for (auto &it2
: it
.second
->connections()) {
754 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
757 for (auto &it2
: it
.second
->attributes
) {
758 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
760 for (auto &it2
: it
.second
->parameters
) {
761 assert(it2
.first
.size() > 0 && (it2
.first
[0] == '\\' || it2
.first
[0] == '$'));
763 InternalCellChecker
checker(this, it
.second
);
767 for (auto &it
: processes
) {
768 assert(it
.first
== it
.second
->name
);
769 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
770 // FIXME: More checks here..
773 for (auto &it
: connections_
) {
774 assert(it
.first
.size() == it
.second
.size());
779 for (auto &it
: attributes
) {
780 assert(it
.first
.size() > 0 && (it
.first
[0] == '\\' || it
.first
[0] == '$'));
785 void RTLIL::Module::optimize()
789 void RTLIL::Module::cloneInto(RTLIL::Module
*new_mod
) const
791 log_assert(new_mod
->refcount_wires_
== 0);
792 log_assert(new_mod
->refcount_cells_
== 0);
794 new_mod
->name
= name
;
795 new_mod
->connections_
= connections_
;
796 new_mod
->attributes
= attributes
;
798 for (auto &it
: wires_
)
799 new_mod
->addWire(it
.first
, it
.second
);
801 for (auto &it
: memories
)
802 new_mod
->memories
[it
.first
] = new RTLIL::Memory(*it
.second
);
804 for (auto &it
: cells_
)
805 new_mod
->addCell(it
.first
, it
.second
);
807 for (auto &it
: processes
)
808 new_mod
->processes
[it
.first
] = it
.second
->clone();
810 struct RewriteSigSpecWorker
813 void operator()(RTLIL::SigSpec
&sig
)
815 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
816 for (auto &c
: chunks
)
818 c
.wire
= mod
->wires_
.at(c
.wire
->name
);
823 RewriteSigSpecWorker rewriteSigSpecWorker
;
824 rewriteSigSpecWorker
.mod
= new_mod
;
825 new_mod
->rewrite_sigspecs(rewriteSigSpecWorker
);
828 RTLIL::Module
*RTLIL::Module::clone() const
830 RTLIL::Module
*new_mod
= new RTLIL::Module
;
835 void RTLIL::Module::add(RTLIL::Wire
*wire
)
837 log_assert(!wire
->name
.empty());
838 log_assert(count_id(wire
->name
) == 0);
839 log_assert(refcount_wires_
== 0);
840 wires_
[wire
->name
] = wire
;
843 void RTLIL::Module::add(RTLIL::Cell
*cell
)
845 log_assert(!cell
->name
.empty());
846 log_assert(count_id(cell
->name
) == 0);
847 log_assert(refcount_cells_
== 0);
848 cells_
[cell
->name
] = cell
;
852 struct DeleteWireWorker
854 RTLIL::Module
*module
;
855 const std::set
<RTLIL::Wire
*> *wires_p
;
857 void operator()(RTLIL::SigSpec
&sig
) {
858 std::vector
<RTLIL::SigChunk
> chunks
= sig
;
859 for (auto &c
: chunks
)
860 if (c
.wire
!= NULL
&& wires_p
->count(c
.wire
)) {
861 c
.wire
= module
->addWire(NEW_ID
, c
.width
);
870 void RTLIL::Module::remove(RTLIL::Wire
*wire
)
872 std::set
<RTLIL::Wire
*> wires_
;
878 void RTLIL::Module::remove(const std::set
<RTLIL::Wire
*> &wires
)
880 log_assert(refcount_wires_
== 0);
882 DeleteWireWorker delete_wire_worker
;
883 delete_wire_worker
.module
= this;
884 delete_wire_worker
.wires_p
= &wires
;
885 rewrite_sigspecs(delete_wire_worker
);
887 for (auto &it
: wires
) {
888 log_assert(wires_
.count(it
->name
) != 0);
889 wires_
.erase(it
->name
);
894 void RTLIL::Module::remove(RTLIL::Cell
*cell
)
896 log_assert(cells_
.count(cell
->name
) != 0);
897 log_assert(refcount_cells_
== 0);
898 cells_
.erase(cell
->name
);
902 void RTLIL::Module::rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
)
904 assert(wires_
[wire
->name
] == wire
);
905 log_assert(refcount_wires_
== 0);
906 wires_
.erase(wire
->name
);
907 wire
->name
= new_name
;
911 void RTLIL::Module::rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
)
913 assert(cells_
[cell
->name
] == cell
);
914 log_assert(refcount_wires_
== 0);
915 cells_
.erase(cell
->name
);
916 cell
->name
= new_name
;
920 void RTLIL::Module::rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
)
922 assert(count_id(old_name
) != 0);
923 if (wires_
.count(old_name
))
924 rename(wires_
.at(old_name
), new_name
);
925 else if (cells_
.count(old_name
))
926 rename(cells_
.at(old_name
), new_name
);
931 static bool fixup_ports_compare(const RTLIL::Wire
*a
, const RTLIL::Wire
*b
)
933 if (a
->port_id
&& !b
->port_id
)
935 if (!a
->port_id
&& b
->port_id
)
938 if (a
->port_id
== b
->port_id
)
939 return a
->name
< b
->name
;
940 return a
->port_id
< b
->port_id
;
943 void RTLIL::Module::connect(const RTLIL::SigSig
&conn
)
945 connections_
.push_back(conn
);
948 void RTLIL::Module::connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
)
950 connections_
.push_back(RTLIL::SigSig(lhs
, rhs
));
953 const std::vector
<RTLIL::SigSig
> &RTLIL::Module::connections() const
958 void RTLIL::Module::fixup_ports()
960 std::vector
<RTLIL::Wire
*> all_ports
;
962 for (auto &w
: wires_
)
963 if (w
.second
->port_input
|| w
.second
->port_output
)
964 all_ports
.push_back(w
.second
);
966 w
.second
->port_id
= 0;
968 std::sort(all_ports
.begin(), all_ports
.end(), fixup_ports_compare
);
969 for (size_t i
= 0; i
< all_ports
.size(); i
++)
970 all_ports
[i
]->port_id
= i
+1;
973 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, int width
)
975 RTLIL::Wire
*wire
= new RTLIL::Wire
;
982 RTLIL::Wire
*RTLIL::Module::addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
)
984 RTLIL::Wire
*wire
= addWire(name
);
985 wire
->width
= other
->width
;
986 wire
->start_offset
= other
->start_offset
;
987 wire
->port_id
= other
->port_id
;
988 wire
->port_input
= other
->port_input
;
989 wire
->port_output
= other
->port_output
;
990 wire
->attributes
= other
->attributes
;
994 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, RTLIL::IdString type
)
996 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1003 RTLIL::Cell
*RTLIL::Module::addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
)
1005 RTLIL::Cell
*cell
= addCell(name
, other
->type
);
1006 cell
->connections_
= other
->connections_
;
1007 cell
->parameters
= other
->parameters
;
1008 cell
->attributes
= other
->attributes
;
1012 #define DEF_METHOD(_func, _y_size, _type) \
1013 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed) { \
1014 RTLIL::Cell *cell = new RTLIL::Cell; \
1015 cell->name = name; \
1016 cell->type = _type; \
1017 cell->parameters["\\A_SIGNED"] = is_signed; \
1018 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1019 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1020 cell->set("\\A", sig_a); \
1021 cell->set("\\Y", sig_y); \
1025 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed) { \
1026 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1027 add ## _func(name, sig_a, sig_y, is_signed); \
1030 DEF_METHOD(Not
, sig_a
.size(), "$not")
1031 DEF_METHOD(Pos
, sig_a
.size(), "$pos")
1032 DEF_METHOD(Bu0
, sig_a
.size(), "$bu0")
1033 DEF_METHOD(Neg
, sig_a
.size(), "$neg")
1034 DEF_METHOD(ReduceAnd
, 1, "$reduce_and")
1035 DEF_METHOD(ReduceOr
, 1, "$reduce_or")
1036 DEF_METHOD(ReduceXor
, 1, "$reduce_xor")
1037 DEF_METHOD(ReduceXnor
, 1, "$reduce_xnor")
1038 DEF_METHOD(ReduceBool
, 1, "$reduce_bool")
1039 DEF_METHOD(LogicNot
, 1, "$logic_not")
1042 #define DEF_METHOD(_func, _y_size, _type) \
1043 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed) { \
1044 RTLIL::Cell *cell = new RTLIL::Cell; \
1045 cell->name = name; \
1046 cell->type = _type; \
1047 cell->parameters["\\A_SIGNED"] = is_signed; \
1048 cell->parameters["\\B_SIGNED"] = is_signed; \
1049 cell->parameters["\\A_WIDTH"] = sig_a.size(); \
1050 cell->parameters["\\B_WIDTH"] = sig_b.size(); \
1051 cell->parameters["\\Y_WIDTH"] = sig_y.size(); \
1052 cell->set("\\A", sig_a); \
1053 cell->set("\\B", sig_b); \
1054 cell->set("\\Y", sig_y); \
1058 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed) { \
1059 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1060 add ## _func(name, sig_a, sig_b, sig_y, is_signed); \
1063 DEF_METHOD(And
, std::max(sig_a
.size(), sig_b
.size()), "$and")
1064 DEF_METHOD(Or
, std::max(sig_a
.size(), sig_b
.size()), "$or")
1065 DEF_METHOD(Xor
, std::max(sig_a
.size(), sig_b
.size()), "$xor")
1066 DEF_METHOD(Xnor
, std::max(sig_a
.size(), sig_b
.size()), "$xnor")
1067 DEF_METHOD(Shl
, sig_a
.size(), "$shl")
1068 DEF_METHOD(Shr
, sig_a
.size(), "$shr")
1069 DEF_METHOD(Sshl
, sig_a
.size(), "$sshl")
1070 DEF_METHOD(Sshr
, sig_a
.size(), "$sshr")
1071 DEF_METHOD(Lt
, 1, "$lt")
1072 DEF_METHOD(Le
, 1, "$le")
1073 DEF_METHOD(Eq
, 1, "$eq")
1074 DEF_METHOD(Ne
, 1, "$ne")
1075 DEF_METHOD(Eqx
, 1, "$eqx")
1076 DEF_METHOD(Nex
, 1, "$nex")
1077 DEF_METHOD(Ge
, 1, "$ge")
1078 DEF_METHOD(Gt
, 1, "$gt")
1079 DEF_METHOD(Add
, std::max(sig_a
.size(), sig_b
.size()), "$add")
1080 DEF_METHOD(Sub
, std::max(sig_a
.size(), sig_b
.size()), "$sub")
1081 DEF_METHOD(Mul
, std::max(sig_a
.size(), sig_b
.size()), "$mul")
1082 DEF_METHOD(Div
, std::max(sig_a
.size(), sig_b
.size()), "$div")
1083 DEF_METHOD(Mod
, std::max(sig_a
.size(), sig_b
.size()), "$mod")
1084 DEF_METHOD(LogicAnd
, 1, "$logic_and")
1085 DEF_METHOD(LogicOr
, 1, "$logic_or")
1088 #define DEF_METHOD(_func, _type, _pmux) \
1089 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) { \
1090 RTLIL::Cell *cell = new RTLIL::Cell; \
1091 cell->name = name; \
1092 cell->type = _type; \
1093 cell->parameters["\\WIDTH"] = sig_a.size(); \
1094 cell->parameters["\\WIDTH"] = sig_b.size(); \
1095 if (_pmux) cell->parameters["\\S_WIDTH"] = sig_s.size(); \
1096 cell->set("\\A", sig_a); \
1097 cell->set("\\B", sig_b); \
1098 cell->set("\\S", sig_s); \
1099 cell->set("\\Y", sig_y); \
1103 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s) { \
1104 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1105 add ## _func(name, sig_a, sig_b, sig_s, sig_y); \
1108 DEF_METHOD(Mux
, "$mux", 0)
1109 DEF_METHOD(Pmux
, "$pmux", 1)
1110 DEF_METHOD(SafePmux
, "$safe_pmux", 1)
1113 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1114 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1115 RTLIL::Cell *cell = new RTLIL::Cell; \
1116 cell->name = name; \
1117 cell->type = _type; \
1118 cell->set("\\" #_P1, sig1); \
1119 cell->set("\\" #_P2, sig2); \
1123 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1) { \
1124 RTLIL::SigSpec sig2 = addWire(NEW_ID); \
1125 add ## _func(name, sig1, sig2); \
1128 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1129 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1130 RTLIL::Cell *cell = new RTLIL::Cell; \
1131 cell->name = name; \
1132 cell->type = _type; \
1133 cell->set("\\" #_P1, sig1); \
1134 cell->set("\\" #_P2, sig2); \
1135 cell->set("\\" #_P3, sig3); \
1139 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2) { \
1140 RTLIL::SigSpec sig3 = addWire(NEW_ID); \
1141 add ## _func(name, sig1, sig2, sig3); \
1144 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1145 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3, RTLIL::SigSpec sig4) { \
1146 RTLIL::Cell *cell = new RTLIL::Cell; \
1147 cell->name = name; \
1148 cell->type = _type; \
1149 cell->set("\\" #_P1, sig1); \
1150 cell->set("\\" #_P2, sig2); \
1151 cell->set("\\" #_P3, sig3); \
1152 cell->set("\\" #_P4, sig4); \
1156 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig1, RTLIL::SigSpec sig2, RTLIL::SigSpec sig3) { \
1157 RTLIL::SigSpec sig4 = addWire(NEW_ID); \
1158 add ## _func(name, sig1, sig2, sig3, sig4); \
1161 DEF_METHOD_2(InvGate
, "$_INV_", A
, Y
)
1162 DEF_METHOD_3(AndGate
, "$_AND_", A
, B
, Y
)
1163 DEF_METHOD_3(OrGate
, "$_OR_", A
, B
, Y
)
1164 DEF_METHOD_3(XorGate
, "$_XOR_", A
, B
, Y
)
1165 DEF_METHOD_4(MuxGate
, "$_MUX_", A
, B
, S
, Y
)
1170 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
)
1172 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1174 cell
->type
= "$pow";
1175 cell
->parameters
["\\A_SIGNED"] = a_signed
;
1176 cell
->parameters
["\\B_SIGNED"] = b_signed
;
1177 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1178 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1179 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1180 cell
->set("\\A", sig_a
);
1181 cell
->set("\\B", sig_b
);
1182 cell
->set("\\Y", sig_y
);
1187 RTLIL::Cell
* RTLIL::Module::addSlice(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
)
1189 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1191 cell
->type
= "$slice";
1192 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1193 cell
->parameters
["\\Y_WIDTH"] = sig_y
.size();
1194 cell
->parameters
["\\OFFSET"] = offset
;
1195 cell
->set("\\A", sig_a
);
1196 cell
->set("\\Y", sig_y
);
1201 RTLIL::Cell
* RTLIL::Module::addConcat(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
)
1203 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1205 cell
->type
= "$concat";
1206 cell
->parameters
["\\A_WIDTH"] = sig_a
.size();
1207 cell
->parameters
["\\B_WIDTH"] = sig_b
.size();
1208 cell
->set("\\A", sig_a
);
1209 cell
->set("\\B", sig_b
);
1210 cell
->set("\\Y", sig_y
);
1215 RTLIL::Cell
* RTLIL::Module::addLut(RTLIL::IdString name
, RTLIL::SigSpec sig_i
, RTLIL::SigSpec sig_o
, RTLIL::Const lut
)
1217 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1219 cell
->type
= "$lut";
1220 cell
->parameters
["\\LUT"] = lut
;
1221 cell
->parameters
["\\WIDTH"] = sig_i
.size();
1222 cell
->set("\\I", sig_i
);
1223 cell
->set("\\O", sig_o
);
1228 RTLIL::Cell
* RTLIL::Module::addAssert(RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
)
1230 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1232 cell
->type
= "$assert";
1233 cell
->set("\\A", sig_a
);
1234 cell
->set("\\EN", sig_en
);
1239 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
)
1241 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1244 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1245 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1246 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1247 cell
->set("\\SET", sig_set
);
1248 cell
->set("\\CLR", sig_clr
);
1249 cell
->set("\\Q", sig_q
);
1254 RTLIL::Cell
* RTLIL::Module::addDff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1256 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1258 cell
->type
= "$dff";
1259 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1260 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1261 cell
->set("\\CLK", sig_clk
);
1262 cell
->set("\\D", sig_d
);
1263 cell
->set("\\Q", sig_q
);
1268 RTLIL::Cell
* RTLIL::Module::addDffsr(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1269 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1271 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1273 cell
->type
= "$dffsr";
1274 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1275 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1276 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1277 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1278 cell
->set("\\CLK", sig_clk
);
1279 cell
->set("\\SET", sig_set
);
1280 cell
->set("\\CLR", sig_clr
);
1281 cell
->set("\\D", sig_d
);
1282 cell
->set("\\Q", sig_q
);
1287 RTLIL::Cell
* RTLIL::Module::addAdff(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1288 RTLIL::Const arst_value
, bool clk_polarity
, bool arst_polarity
)
1290 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1292 cell
->type
= "$adff";
1293 cell
->parameters
["\\CLK_POLARITY"] = clk_polarity
;
1294 cell
->parameters
["\\ARST_POLARITY"] = arst_polarity
;
1295 cell
->parameters
["\\ARST_VALUE"] = arst_value
;
1296 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1297 cell
->set("\\CLK", sig_clk
);
1298 cell
->set("\\ARST", sig_arst
);
1299 cell
->set("\\D", sig_d
);
1300 cell
->set("\\Q", sig_q
);
1305 RTLIL::Cell
* RTLIL::Module::addDlatch(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1307 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1309 cell
->type
= "$dlatch";
1310 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1311 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1312 cell
->set("\\EN", sig_en
);
1313 cell
->set("\\D", sig_d
);
1314 cell
->set("\\Q", sig_q
);
1319 RTLIL::Cell
* RTLIL::Module::addDlatchsr(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1320 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1322 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1324 cell
->type
= "$dlatchsr";
1325 cell
->parameters
["\\EN_POLARITY"] = en_polarity
;
1326 cell
->parameters
["\\SET_POLARITY"] = set_polarity
;
1327 cell
->parameters
["\\CLR_POLARITY"] = clr_polarity
;
1328 cell
->parameters
["\\WIDTH"] = sig_q
.size();
1329 cell
->set("\\EN", sig_en
);
1330 cell
->set("\\SET", sig_set
);
1331 cell
->set("\\CLR", sig_clr
);
1332 cell
->set("\\D", sig_d
);
1333 cell
->set("\\Q", sig_q
);
1338 RTLIL::Cell
* RTLIL::Module::addDffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
)
1340 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1342 cell
->type
= stringf("$_DFF_%c_", clk_polarity
? 'P' : 'N');
1343 cell
->set("\\C", sig_clk
);
1344 cell
->set("\\D", sig_d
);
1345 cell
->set("\\Q", sig_q
);
1350 RTLIL::Cell
* RTLIL::Module::addDffsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1351 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
, bool set_polarity
, bool clr_polarity
)
1353 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1355 cell
->type
= stringf("$_DFFSR_%c%c%c_", clk_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1356 cell
->set("\\C", sig_clk
);
1357 cell
->set("\\S", sig_set
);
1358 cell
->set("\\R", sig_clr
);
1359 cell
->set("\\D", sig_d
);
1360 cell
->set("\\Q", sig_q
);
1365 RTLIL::Cell
* RTLIL::Module::addAdffGate(RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1366 bool arst_value
, bool clk_polarity
, bool arst_polarity
)
1368 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1370 cell
->type
= stringf("$_DFF_%c%c%c_", clk_polarity
? 'P' : 'N', arst_polarity
? 'P' : 'N', arst_value
? '1' : '0');
1371 cell
->set("\\C", sig_clk
);
1372 cell
->set("\\R", sig_arst
);
1373 cell
->set("\\D", sig_d
);
1374 cell
->set("\\Q", sig_q
);
1379 RTLIL::Cell
* RTLIL::Module::addDlatchGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
)
1381 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1383 cell
->type
= stringf("$_DLATCH_%c_", en_polarity
? 'P' : 'N');
1384 cell
->set("\\E", sig_en
);
1385 cell
->set("\\D", sig_d
);
1386 cell
->set("\\Q", sig_q
);
1391 RTLIL::Cell
* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1392 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
, bool set_polarity
, bool clr_polarity
)
1394 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1396 cell
->type
= stringf("$_DLATCHSR_%c%c%c_", en_polarity
? 'P' : 'N', set_polarity
? 'P' : 'N', clr_polarity
? 'P' : 'N');
1397 cell
->set("\\E", sig_en
);
1398 cell
->set("\\S", sig_set
);
1399 cell
->set("\\R", sig_clr
);
1400 cell
->set("\\D", sig_d
);
1401 cell
->set("\\Q", sig_q
);
1413 port_output
= false;
1416 RTLIL::Memory::Memory()
1422 bool RTLIL::Cell::has(RTLIL::IdString portname
)
1424 return connections_
.count(portname
) != 0;
1427 void RTLIL::Cell::unset(RTLIL::IdString portname
)
1429 connections_
.erase(portname
);
1432 void RTLIL::Cell::set(RTLIL::IdString portname
, RTLIL::SigSpec signal
)
1434 connections_
[portname
] = signal
;
1437 const RTLIL::SigSpec
&RTLIL::Cell::get(RTLIL::IdString portname
) const
1439 return connections_
.at(portname
);
1442 const std::map
<RTLIL::IdString
, RTLIL::SigSpec
> &RTLIL::Cell::connections() const
1444 return connections_
;
1447 void RTLIL::Cell::check()
1450 InternalCellChecker
checker(NULL
, this);
1455 void RTLIL::Cell::fixup_parameters(bool set_a_signed
, bool set_b_signed
)
1457 if (type
[0] != '$' || type
.substr(0, 2) == "$_" || type
.substr(0, 8) == "$paramod" ||
1458 type
.substr(0, 9) == "$verific$" || type
.substr(0, 7) == "$array:")
1461 if (type
== "$mux" || type
== "$pmux" || type
== "$safe_pmux")
1463 parameters
["\\WIDTH"] = SIZE(connections_
["\\Y"]);
1464 if (type
== "$pmux" || type
== "$safe_pmux")
1465 parameters
["\\S_WIDTH"] = SIZE(connections_
["\\S"]);
1470 bool signedness_ab
= type
!= "$slice" && type
!= "$concat";
1472 if (connections_
.count("\\A")) {
1473 if (signedness_ab
) {
1475 parameters
["\\A_SIGNED"] = true;
1476 else if (parameters
.count("\\A_SIGNED") == 0)
1477 parameters
["\\A_SIGNED"] = false;
1479 parameters
["\\A_WIDTH"] = SIZE(connections_
["\\A"]);
1482 if (connections_
.count("\\B")) {
1483 if (signedness_ab
) {
1485 parameters
["\\B_SIGNED"] = true;
1486 else if (parameters
.count("\\B_SIGNED") == 0)
1487 parameters
["\\B_SIGNED"] = false;
1489 parameters
["\\B_WIDTH"] = SIZE(connections_
["\\B"]);
1492 if (connections_
.count("\\Y"))
1493 parameters
["\\Y_WIDTH"] = SIZE(connections_
["\\Y"]);
1498 RTLIL::SigChunk::SigChunk()
1505 RTLIL::SigChunk::SigChunk(const RTLIL::Const
&value
)
1509 width
= data
.bits
.size();
1513 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
)
1515 log_assert(wire
!= nullptr);
1517 this->width
= wire
->width
;
1521 RTLIL::SigChunk::SigChunk(RTLIL::Wire
*wire
, int offset
, int width
)
1523 log_assert(wire
!= nullptr);
1525 this->width
= width
;
1526 this->offset
= offset
;
1529 RTLIL::SigChunk::SigChunk(const std::string
&str
)
1532 data
= RTLIL::Const(str
);
1533 width
= data
.bits
.size();
1537 RTLIL::SigChunk::SigChunk(int val
, int width
)
1540 data
= RTLIL::Const(val
, width
);
1541 this->width
= data
.bits
.size();
1545 RTLIL::SigChunk::SigChunk(RTLIL::State bit
, int width
)
1548 data
= RTLIL::Const(bit
, width
);
1549 this->width
= data
.bits
.size();
1553 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit
)
1557 data
= RTLIL::Const(bit
.data
);
1558 offset
= bit
.offset
;
1562 RTLIL::SigChunk
RTLIL::SigChunk::extract(int offset
, int length
) const
1564 RTLIL::SigChunk ret
;
1567 ret
.offset
= this->offset
+ offset
;
1570 for (int i
= 0; i
< length
; i
++)
1571 ret
.data
.bits
.push_back(data
.bits
[offset
+i
]);
1577 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk
&other
) const
1579 if (wire
&& other
.wire
)
1580 if (wire
->name
!= other
.wire
->name
)
1581 return wire
->name
< other
.wire
->name
;
1583 if (wire
!= other
.wire
)
1584 return wire
< other
.wire
;
1586 if (offset
!= other
.offset
)
1587 return offset
< other
.offset
;
1589 if (width
!= other
.width
)
1590 return width
< other
.width
;
1592 return data
.bits
< other
.data
.bits
;
1595 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk
&other
) const
1597 if (wire
!= other
.wire
|| width
!= other
.width
|| offset
!= other
.offset
)
1599 if (data
.bits
!= other
.data
.bits
)
1604 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk
&other
) const
1611 RTLIL::SigSpec::SigSpec()
1617 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec
&other
)
1622 const RTLIL::SigSpec
&RTLIL::SigSpec::operator=(const RTLIL::SigSpec
&other
)
1624 cover("kernel.rtlil.sigspec.assign");
1626 width_
= other
.width_
;
1627 hash_
= other
.hash_
;
1628 chunks_
= other
.chunks_
;
1631 if (!other
.bits_
.empty())
1633 RTLIL::SigChunk
*last
= NULL
;
1634 int last_end_offset
= 0;
1636 for (auto &bit
: other
.bits_
) {
1637 if (last
&& bit
.wire
== last
->wire
) {
1638 if (bit
.wire
== NULL
) {
1639 last
->data
.bits
.push_back(bit
.data
);
1642 } else if (last_end_offset
== bit
.offset
) {
1648 chunks_
.push_back(bit
);
1649 last
= &chunks_
.back();
1650 last_end_offset
= bit
.offset
+ 1;
1659 RTLIL::SigSpec::SigSpec(const RTLIL::Const
&value
)
1661 chunks_
.push_back(RTLIL::SigChunk(value
));
1662 width_
= chunks_
.back().width
;
1667 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk
&chunk
)
1669 chunks_
.push_back(chunk
);
1670 width_
= chunks_
.back().width
;
1675 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
)
1677 chunks_
.push_back(RTLIL::SigChunk(wire
));
1678 width_
= chunks_
.back().width
;
1683 RTLIL::SigSpec::SigSpec(RTLIL::Wire
*wire
, int offset
, int width
)
1685 chunks_
.push_back(RTLIL::SigChunk(wire
, offset
, width
));
1686 width_
= chunks_
.back().width
;
1691 RTLIL::SigSpec::SigSpec(const std::string
&str
)
1693 chunks_
.push_back(RTLIL::SigChunk(str
));
1694 width_
= chunks_
.back().width
;
1699 RTLIL::SigSpec::SigSpec(int val
, int width
)
1701 chunks_
.push_back(RTLIL::SigChunk(val
, width
));
1707 RTLIL::SigSpec::SigSpec(RTLIL::State bit
, int width
)
1709 chunks_
.push_back(RTLIL::SigChunk(bit
, width
));
1715 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit
, int width
)
1717 if (bit
.wire
== NULL
)
1718 chunks_
.push_back(RTLIL::SigChunk(bit
.data
, width
));
1720 for (int i
= 0; i
< width
; i
++)
1721 chunks_
.push_back(bit
);
1727 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigChunk
> chunks
)
1731 for (auto &c
: chunks
)
1736 RTLIL::SigSpec::SigSpec(std::vector
<RTLIL::SigBit
> bits
)
1740 for (auto &bit
: bits
)
1745 RTLIL::SigSpec::SigSpec(std::set
<RTLIL::SigBit
> bits
)
1749 for (auto &bit
: bits
)
1754 void RTLIL::SigSpec::pack() const
1756 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1758 if (that
->bits_
.empty())
1761 cover("kernel.rtlil.sigspec.convert.pack");
1762 log_assert(that
->chunks_
.empty());
1764 std::vector
<RTLIL::SigBit
> old_bits
;
1765 old_bits
.swap(that
->bits_
);
1767 RTLIL::SigChunk
*last
= NULL
;
1768 int last_end_offset
= 0;
1770 for (auto &bit
: old_bits
) {
1771 if (last
&& bit
.wire
== last
->wire
) {
1772 if (bit
.wire
== NULL
) {
1773 last
->data
.bits
.push_back(bit
.data
);
1776 } else if (last_end_offset
== bit
.offset
) {
1782 that
->chunks_
.push_back(bit
);
1783 last
= &that
->chunks_
.back();
1784 last_end_offset
= bit
.offset
+ 1;
1790 void RTLIL::SigSpec::unpack() const
1792 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1794 if (that
->chunks_
.empty())
1797 cover("kernel.rtlil.sigspec.convert.unpack");
1798 log_assert(that
->bits_
.empty());
1800 that
->bits_
.reserve(that
->width_
);
1801 for (auto &c
: that
->chunks_
)
1802 for (int i
= 0; i
< c
.width
; i
++)
1803 that
->bits_
.push_back(RTLIL::SigBit(c
, i
));
1805 that
->chunks_
.clear();
1809 #define DJB2(_hash, _value) do { (_hash) = (((_hash) << 5) + (_hash)) + (_value); } while (0)
1811 void RTLIL::SigSpec::hash() const
1813 RTLIL::SigSpec
*that
= (RTLIL::SigSpec
*)this;
1815 if (that
->hash_
!= 0)
1818 cover("kernel.rtlil.sigspec.hash");
1822 for (auto &c
: that
->chunks_
)
1823 if (c
.wire
== NULL
) {
1824 for (auto &v
: c
.data
.bits
)
1825 DJB2(that
->hash_
, v
);
1827 for (auto &v
: c
.wire
->name
)
1828 DJB2(that
->hash_
, v
);
1829 DJB2(that
->hash_
, c
.offset
);
1830 DJB2(that
->hash_
, c
.width
);
1833 if (that
->hash_
== 0)
1837 void RTLIL::SigSpec::sort()
1840 cover("kernel.rtlil.sigspec.sort");
1841 std::sort(bits_
.begin(), bits_
.end());
1844 void RTLIL::SigSpec::sort_and_unify()
1846 cover("kernel.rtlil.sigspec.sort_and_unify");
1847 *this = this->to_sigbit_set();
1850 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
)
1852 replace(pattern
, with
, this);
1855 void RTLIL::SigSpec::replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const
1857 cover("kernel.rtlil.sigspec.replace");
1863 assert(other
!= NULL
);
1864 assert(width_
== other
->width_
);
1867 assert(pattern
.width_
== with
.width_
);
1869 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> pattern_map
;
1870 for (int i
= 0; i
< SIZE(pattern
.bits_
); i
++)
1871 if (pattern
.bits_
[i
].wire
!= NULL
)
1872 pattern_map
[pattern
.bits_
[i
]] = with
.bits_
[i
];
1874 for (int i
= 0; i
< SIZE(bits_
); i
++)
1875 if (pattern_map
.count(bits_
[i
]))
1876 other
->bits_
[i
] = pattern_map
.at(bits_
[i
]);
1881 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
)
1883 remove2(pattern
, NULL
);
1886 void RTLIL::SigSpec::remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const
1888 RTLIL::SigSpec tmp
= *this;
1889 tmp
.remove2(pattern
, other
);
1892 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
)
1895 cover("kernel.rtlil.sigspec.remove_other");
1897 cover("kernel.rtlil.sigspec.remove");
1901 if (other
!= NULL
) {
1902 assert(width_
== other
->width_
);
1906 std::set
<RTLIL::SigBit
> pattern_bits
= pattern
.to_sigbit_set();
1907 std::vector
<RTLIL::SigBit
> new_bits
, new_other_bits
;
1909 for (int i
= 0; i
< SIZE(bits_
); i
++) {
1910 if (bits_
[i
].wire
!= NULL
&& pattern_bits
.count(bits_
[i
]))
1913 new_other_bits
.push_back(other
->bits_
[i
]);
1914 new_bits
.push_back(bits_
[i
]);
1917 bits_
.swap(new_bits
);
1918 width_
= SIZE(bits_
);
1920 if (other
!= NULL
) {
1921 other
->bits_
.swap(new_other_bits
);
1922 other
->width_
= SIZE(other
->bits_
);
1928 RTLIL::SigSpec
RTLIL::SigSpec::extract(RTLIL::SigSpec pattern
, const RTLIL::SigSpec
*other
) const
1931 cover("kernel.rtlil.sigspec.extract_other");
1933 cover("kernel.rtlil.sigspec.extract");
1941 assert(other
== NULL
|| width_
== other
->width_
);
1943 std::set
<RTLIL::SigBit
> pat
= pattern
.to_sigbit_set();
1944 std::vector
<RTLIL::SigBit
> bits_match
= to_sigbit_vector();
1948 std::vector
<RTLIL::SigBit
> bits_other
= other
->to_sigbit_vector();
1949 for (int i
= 0; i
< width_
; i
++)
1950 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
1951 ret
.append_bit(bits_other
[i
]);
1953 for (int i
= 0; i
< width_
; i
++)
1954 if (bits_match
[i
].wire
&& pat
.count(bits_match
[i
]))
1955 ret
.append_bit(bits_match
[i
]);
1962 void RTLIL::SigSpec::replace(int offset
, const RTLIL::SigSpec
&with
)
1964 cover("kernel.rtlil.sigspec.replace_pos");
1969 assert(offset
>= 0);
1970 assert(with
.width_
>= 0);
1971 assert(offset
+with
.width_
<= width_
);
1973 for (int i
= 0; i
< with
.width_
; i
++)
1974 bits_
.at(offset
+ i
) = with
.bits_
.at(i
);
1979 void RTLIL::SigSpec::remove_const()
1983 cover("kernel.rtlil.sigspec.remove_const.packed");
1985 std::vector
<RTLIL::SigChunk
> new_chunks
;
1986 new_chunks
.reserve(SIZE(chunks_
));
1989 for (auto &chunk
: chunks_
)
1990 if (chunk
.wire
!= NULL
) {
1991 new_chunks
.push_back(chunk
);
1992 width_
+= chunk
.width
;
1995 chunks_
.swap(new_chunks
);
1999 cover("kernel.rtlil.sigspec.remove_const.unpacked");
2001 std::vector
<RTLIL::SigBit
> new_bits
;
2002 new_bits
.reserve(width_
);
2004 for (auto &bit
: bits_
)
2005 if (bit
.wire
!= NULL
)
2006 new_bits
.push_back(bit
);
2008 bits_
.swap(new_bits
);
2009 width_
= bits_
.size();
2015 void RTLIL::SigSpec::remove(int offset
, int length
)
2017 cover("kernel.rtlil.sigspec.remove_pos");
2021 assert(offset
>= 0);
2022 assert(length
>= 0);
2023 assert(offset
+ length
<= width_
);
2025 bits_
.erase(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2026 width_
= bits_
.size();
2031 RTLIL::SigSpec
RTLIL::SigSpec::extract(int offset
, int length
) const
2034 cover("kernel.rtlil.sigspec.extract_pos");
2035 return std::vector
<RTLIL::SigBit
>(bits_
.begin() + offset
, bits_
.begin() + offset
+ length
);
2038 void RTLIL::SigSpec::append(const RTLIL::SigSpec
&signal
)
2040 if (signal
.width_
== 0)
2048 cover("kernel.rtlil.sigspec.append");
2050 if (packed() != signal
.packed()) {
2056 for (auto &other_c
: signal
.chunks_
)
2058 auto &my_last_c
= chunks_
.back();
2059 if (my_last_c
.wire
== NULL
&& other_c
.wire
== NULL
) {
2060 auto &this_data
= my_last_c
.data
.bits
;
2061 auto &other_data
= other_c
.data
.bits
;
2062 this_data
.insert(this_data
.end(), other_data
.begin(), other_data
.end());
2063 my_last_c
.width
+= other_c
.width
;
2065 if (my_last_c
.wire
== other_c
.wire
&& my_last_c
.offset
+ my_last_c
.width
== other_c
.offset
) {
2066 my_last_c
.width
+= other_c
.width
;
2068 chunks_
.push_back(other_c
);
2071 bits_
.insert(bits_
.end(), signal
.bits_
.begin(), signal
.bits_
.end());
2073 width_
+= signal
.width_
;
2077 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit
&bit
)
2081 cover("kernel.rtlil.sigspec.append_bit.packed");
2083 if (chunks_
.size() == 0)
2084 chunks_
.push_back(bit
);
2086 if (bit
.wire
== NULL
)
2087 if (chunks_
.back().wire
== NULL
) {
2088 chunks_
.back().data
.bits
.push_back(bit
.data
);
2089 chunks_
.back().width
++;
2091 chunks_
.push_back(bit
);
2093 if (chunks_
.back().wire
== bit
.wire
&& chunks_
.back().offset
+ chunks_
.back().width
== bit
.offset
)
2094 chunks_
.back().width
++;
2096 chunks_
.push_back(bit
);
2100 cover("kernel.rtlil.sigspec.append_bit.unpacked");
2101 bits_
.push_back(bit
);
2108 void RTLIL::SigSpec::extend(int width
, bool is_signed
)
2110 cover("kernel.rtlil.sigspec.extend");
2115 remove(width
, width_
- width
);
2117 if (width_
< width
) {
2118 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2119 if (!is_signed
&& padding
!= RTLIL::SigSpec(RTLIL::State::Sx
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sz
) &&
2120 padding
!= RTLIL::SigSpec(RTLIL::State::Sa
) && padding
!= RTLIL::SigSpec(RTLIL::State::Sm
))
2121 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2122 while (width_
< width
)
2127 void RTLIL::SigSpec::extend_u0(int width
, bool is_signed
)
2129 cover("kernel.rtlil.sigspec.extend_u0");
2134 remove(width
, width_
- width
);
2136 if (width_
< width
) {
2137 RTLIL::SigSpec padding
= width_
> 0 ? extract(width_
- 1, 1) : RTLIL::SigSpec(RTLIL::State::S0
);
2139 padding
= RTLIL::SigSpec(RTLIL::State::S0
);
2140 while (width_
< width
)
2146 RTLIL::SigSpec
RTLIL::SigSpec::repeat(int num
) const
2148 cover("kernel.rtlil.sigspec.repeat");
2151 for (int i
= 0; i
< num
; i
++)
2157 void RTLIL::SigSpec::check() const
2161 cover("kernel.rtlil.sigspec.check.skip");
2165 cover("kernel.rtlil.sigspec.check.packed");
2168 for (size_t i
= 0; i
< chunks_
.size(); i
++) {
2169 const RTLIL::SigChunk chunk
= chunks_
[i
];
2170 if (chunk
.wire
== NULL
) {
2172 assert(chunks_
[i
-1].wire
!= NULL
);
2173 assert(chunk
.offset
== 0);
2174 assert(chunk
.data
.bits
.size() == (size_t)chunk
.width
);
2176 if (i
> 0 && chunks_
[i
-1].wire
== chunk
.wire
)
2177 assert(chunk
.offset
!= chunks_
[i
-1].offset
+ chunks_
[i
-1].width
);
2178 assert(chunk
.offset
>= 0);
2179 assert(chunk
.width
>= 0);
2180 assert(chunk
.offset
+ chunk
.width
<= chunk
.wire
->width
);
2181 assert(chunk
.data
.bits
.size() == 0);
2185 assert(w
== width_
);
2186 assert(bits_
.empty());
2190 cover("kernel.rtlil.sigspec.check.unpacked");
2192 assert(width_
== SIZE(bits_
));
2193 assert(chunks_
.empty());
2198 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec
&other
) const
2200 cover("kernel.rtlil.sigspec.comp_lt");
2205 if (width_
!= other
.width_
)
2206 return width_
< other
.width_
;
2211 if (chunks_
.size() != other
.chunks_
.size())
2212 return chunks_
.size() < other
.chunks_
.size();
2217 if (hash_
!= other
.hash_
)
2218 return hash_
< other
.hash_
;
2220 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2221 if (chunks_
[i
] != other
.chunks_
[i
]) {
2222 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
2223 return chunks_
[i
] < other
.chunks_
[i
];
2226 cover("kernel.rtlil.sigspec.comp_lt.equal");
2230 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec
&other
) const
2232 cover("kernel.rtlil.sigspec.comp_eq");
2237 if (width_
!= other
.width_
)
2243 if (chunks_
.size() != chunks_
.size())
2249 if (hash_
!= other
.hash_
)
2252 for (size_t i
= 0; i
< chunks_
.size(); i
++)
2253 if (chunks_
[i
] != other
.chunks_
[i
]) {
2254 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
2258 cover("kernel.rtlil.sigspec.comp_eq.equal");
2262 bool RTLIL::SigSpec::is_wire() const
2264 cover("kernel.rtlil.sigspec.is_wire");
2267 return SIZE(chunks_
) == 1 && chunks_
[0].wire
&& chunks_
[0].wire
->width
== width_
;
2270 bool RTLIL::SigSpec::is_chunk() const
2272 cover("kernel.rtlil.sigspec.is_chunk");
2275 return SIZE(chunks_
) == 1;
2278 bool RTLIL::SigSpec::is_fully_const() const
2280 cover("kernel.rtlil.sigspec.is_fully_const");
2283 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2284 if (it
->width
> 0 && it
->wire
!= NULL
)
2289 bool RTLIL::SigSpec::is_fully_def() const
2291 cover("kernel.rtlil.sigspec.is_fully_def");
2294 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2295 if (it
->width
> 0 && it
->wire
!= NULL
)
2297 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2298 if (it
->data
.bits
[i
] != RTLIL::State::S0
&& it
->data
.bits
[i
] != RTLIL::State::S1
)
2304 bool RTLIL::SigSpec::is_fully_undef() const
2306 cover("kernel.rtlil.sigspec.is_fully_undef");
2309 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++) {
2310 if (it
->width
> 0 && it
->wire
!= NULL
)
2312 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2313 if (it
->data
.bits
[i
] != RTLIL::State::Sx
&& it
->data
.bits
[i
] != RTLIL::State::Sz
)
2319 bool RTLIL::SigSpec::has_marked_bits() const
2321 cover("kernel.rtlil.sigspec.has_marked_bits");
2324 for (auto it
= chunks_
.begin(); it
!= chunks_
.end(); it
++)
2325 if (it
->width
> 0 && it
->wire
== NULL
) {
2326 for (size_t i
= 0; i
< it
->data
.bits
.size(); i
++)
2327 if (it
->data
.bits
[i
] == RTLIL::State::Sm
)
2333 bool RTLIL::SigSpec::as_bool() const
2335 cover("kernel.rtlil.sigspec.as_bool");
2338 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2340 return chunks_
[0].data
.as_bool();
2344 int RTLIL::SigSpec::as_int() const
2346 cover("kernel.rtlil.sigspec.as_int");
2349 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2351 return chunks_
[0].data
.as_int();
2355 std::string
RTLIL::SigSpec::as_string() const
2357 cover("kernel.rtlil.sigspec.as_string");
2361 for (size_t i
= chunks_
.size(); i
> 0; i
--) {
2362 const RTLIL::SigChunk
&chunk
= chunks_
[i
-1];
2363 if (chunk
.wire
!= NULL
)
2364 for (int j
= 0; j
< chunk
.width
; j
++)
2367 str
+= chunk
.data
.as_string();
2372 RTLIL::Const
RTLIL::SigSpec::as_const() const
2374 cover("kernel.rtlil.sigspec.as_const");
2377 assert(is_fully_const() && SIZE(chunks_
) <= 1);
2379 return chunks_
[0].data
;
2380 return RTLIL::Const();
2383 RTLIL::Wire
*RTLIL::SigSpec::as_wire() const
2385 cover("kernel.rtlil.sigspec.as_wire");
2389 return chunks_
[0].wire
;
2392 RTLIL::SigChunk
RTLIL::SigSpec::as_chunk() const
2394 cover("kernel.rtlil.sigspec.as_chunk");
2401 bool RTLIL::SigSpec::match(std::string pattern
) const
2403 cover("kernel.rtlil.sigspec.match");
2406 std::string str
= as_string();
2407 assert(pattern
.size() == str
.size());
2409 for (size_t i
= 0; i
< pattern
.size(); i
++) {
2410 if (pattern
[i
] == ' ')
2412 if (pattern
[i
] == '*') {
2413 if (str
[i
] != 'z' && str
[i
] != 'x')
2417 if (pattern
[i
] != str
[i
])
2424 std::set
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_set() const
2426 cover("kernel.rtlil.sigspec.to_sigbit_set");
2429 std::set
<RTLIL::SigBit
> sigbits
;
2430 for (auto &c
: chunks_
)
2431 for (int i
= 0; i
< c
.width
; i
++)
2432 sigbits
.insert(RTLIL::SigBit(c
, i
));
2436 std::vector
<RTLIL::SigBit
> RTLIL::SigSpec::to_sigbit_vector() const
2438 cover("kernel.rtlil.sigspec.to_sigbit_vector");
2444 RTLIL::SigBit
RTLIL::SigSpec::to_single_sigbit() const
2446 cover("kernel.rtlil.sigspec.to_single_sigbit");
2449 log_assert(width_
== 1);
2450 for (auto &c
: chunks_
)
2452 return RTLIL::SigBit(c
);
2456 static void sigspec_parse_split(std::vector
<std::string
> &tokens
, const std::string
&text
, char sep
)
2458 size_t start
= 0, end
= 0;
2459 while ((end
= text
.find(sep
, start
)) != std::string::npos
) {
2460 tokens
.push_back(text
.substr(start
, end
- start
));
2463 tokens
.push_back(text
.substr(start
));
2466 static int sigspec_parse_get_dummy_line_num()
2471 bool RTLIL::SigSpec::parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2473 cover("kernel.rtlil.sigspec.parse");
2475 std::vector
<std::string
> tokens
;
2476 sigspec_parse_split(tokens
, str
, ',');
2478 sig
= RTLIL::SigSpec();
2479 for (int tokidx
= int(tokens
.size())-1; tokidx
>= 0; tokidx
--)
2481 std::string netname
= tokens
[tokidx
];
2482 std::string indices
;
2484 if (netname
.size() == 0)
2487 if ('0' <= netname
[0] && netname
[0] <= '9') {
2488 cover("kernel.rtlil.sigspec.parse.const");
2489 AST::get_line_num
= sigspec_parse_get_dummy_line_num
;
2490 AST::AstNode
*ast
= VERILOG_FRONTEND::const2ast(netname
);
2493 sig
.append(RTLIL::Const(ast
->bits
));
2501 cover("kernel.rtlil.sigspec.parse.net");
2503 if (netname
[0] != '$' && netname
[0] != '\\')
2504 netname
= "\\" + netname
;
2506 if (module
->wires_
.count(netname
) == 0) {
2507 size_t indices_pos
= netname
.size()-1;
2508 if (indices_pos
> 2 && netname
[indices_pos
] == ']')
2511 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2512 if (indices_pos
> 0 && netname
[indices_pos
] == ':') {
2514 while (indices_pos
> 0 && ('0' <= netname
[indices_pos
] && netname
[indices_pos
] <= '9')) indices_pos
--;
2516 if (indices_pos
> 0 && netname
[indices_pos
] == '[') {
2517 indices
= netname
.substr(indices_pos
);
2518 netname
= netname
.substr(0, indices_pos
);
2523 if (module
->wires_
.count(netname
) == 0)
2526 RTLIL::Wire
*wire
= module
->wires_
.at(netname
);
2527 if (!indices
.empty()) {
2528 std::vector
<std::string
> index_tokens
;
2529 sigspec_parse_split(index_tokens
, indices
.substr(1, indices
.size()-2), ':');
2530 if (index_tokens
.size() == 1) {
2531 cover("kernel.rtlil.sigspec.parse.bit_sel");
2532 sig
.append(RTLIL::SigSpec(wire
, atoi(index_tokens
.at(0).c_str())));
2534 cover("kernel.rtlil.sigspec.parse.part_sel");
2535 int a
= atoi(index_tokens
.at(0).c_str());
2536 int b
= atoi(index_tokens
.at(1).c_str());
2541 sig
.append(RTLIL::SigSpec(wire
, a
, b
-a
+1));
2550 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
)
2552 if (str
.empty() || str
[0] != '@')
2553 return parse(sig
, module
, str
);
2555 cover("kernel.rtlil.sigspec.parse.sel");
2557 str
= RTLIL::escape_id(str
.substr(1));
2558 if (design
->selection_vars
.count(str
) == 0)
2561 sig
= RTLIL::SigSpec();
2562 RTLIL::Selection
&sel
= design
->selection_vars
.at(str
);
2563 for (auto &it
: module
->wires_
)
2564 if (sel
.selected_member(module
->name
, it
.first
))
2565 sig
.append(it
.second
);
2570 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
)
2573 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
2574 sig
= RTLIL::SigSpec(RTLIL::State::S0
, lhs
.width_
);
2579 cover("kernel.rtlil.sigspec.parse.rhs_ones");
2580 sig
= RTLIL::SigSpec(RTLIL::State::S1
, lhs
.width_
);
2584 if (lhs
.chunks_
.size() == 1) {
2585 char *p
= (char*)str
.c_str(), *endptr
;
2586 long long int val
= strtoll(p
, &endptr
, 10);
2587 if (endptr
&& endptr
!= p
&& *endptr
== 0) {
2588 sig
= RTLIL::SigSpec(val
, lhs
.width_
);
2589 cover("kernel.rtlil.sigspec.parse.rhs_dec");
2594 return parse(sig
, module
, str
);
2597 RTLIL::CaseRule::~CaseRule()
2599 for (auto it
= switches
.begin(); it
!= switches
.end(); it
++)
2603 RTLIL::CaseRule
*RTLIL::CaseRule::clone() const
2605 RTLIL::CaseRule
*new_caserule
= new RTLIL::CaseRule
;
2606 new_caserule
->compare
= compare
;
2607 new_caserule
->actions
= actions
;
2608 for (auto &it
: switches
)
2609 new_caserule
->switches
.push_back(it
->clone());
2610 return new_caserule
;
2613 RTLIL::SwitchRule::~SwitchRule()
2615 for (auto it
= cases
.begin(); it
!= cases
.end(); it
++)
2619 RTLIL::SwitchRule
*RTLIL::SwitchRule::clone() const
2621 RTLIL::SwitchRule
*new_switchrule
= new RTLIL::SwitchRule
;
2622 new_switchrule
->signal
= signal
;
2623 new_switchrule
->attributes
= attributes
;
2624 for (auto &it
: cases
)
2625 new_switchrule
->cases
.push_back(it
->clone());
2626 return new_switchrule
;
2630 RTLIL::SyncRule
*RTLIL::SyncRule::clone() const
2632 RTLIL::SyncRule
*new_syncrule
= new RTLIL::SyncRule
;
2633 new_syncrule
->type
= type
;
2634 new_syncrule
->signal
= signal
;
2635 new_syncrule
->actions
= actions
;
2636 return new_syncrule
;
2639 RTLIL::Process::~Process()
2641 for (auto it
= syncs
.begin(); it
!= syncs
.end(); it
++)
2645 RTLIL::Process
*RTLIL::Process::clone() const
2647 RTLIL::Process
*new_proc
= new RTLIL::Process
;
2649 new_proc
->name
= name
;
2650 new_proc
->attributes
= attributes
;
2652 RTLIL::CaseRule
*rc_ptr
= root_case
.clone();
2653 new_proc
->root_case
= *rc_ptr
;
2654 rc_ptr
->switches
.clear();
2657 for (auto &it
: syncs
)
2658 new_proc
->syncs
.push_back(it
->clone());