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/sigtools.h"
24 PRIVATE_NAMESPACE_BEGIN
26 struct Clk2fflogicPass
: public Pass
{
27 Clk2fflogicPass() : Pass("clk2fflogic", "convert clocked FFs to generic $ff cells") { }
30 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
32 log(" clk2fflogic [options] [selection]\n");
34 log("This command replaces clocked flip-flops with generic $ff cells that use the\n");
35 log("implicit global clock. This is useful for formal verification of designs with\n");
36 log("multiple clocks.\n");
39 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
41 // bool flag_noinit = false;
43 log_header(design
, "Executing CLK2FFLOGIC pass (convert clocked FFs to generic $ff cells).\n");
46 for (argidx
= 1; argidx
< args
.size(); argidx
++)
48 // if (args[argidx] == "-noinit") {
49 // flag_noinit = true;
54 extra_args(args
, argidx
, design
);
56 for (auto module
: design
->selected_modules())
58 SigMap
sigmap(module
);
59 dict
<SigBit
, State
> initbits
;
60 pool
<SigBit
> del_initbits
;
62 for (auto wire
: module
->wires())
63 if (wire
->attributes
.count("\\init") > 0)
65 Const initval
= wire
->attributes
.at("\\init");
66 SigSpec initsig
= sigmap(wire
);
68 for (int i
= 0; i
< GetSize(initval
) && i
< GetSize(initsig
); i
++)
69 if (initval
[i
] == State::S0
|| initval
[i
] == State::S1
)
70 initbits
[initsig
[i
]] = initval
[i
];
73 for (auto cell
: vector
<Cell
*>(module
->selected_cells()))
75 if (cell
->type
.in("$dff", "$adff"))
77 bool clkpol
= cell
->parameters
["\\CLK_POLARITY"].as_bool();
79 SigSpec clk
= cell
->getPort("\\CLK");
80 Wire
*past_clk
= module
->addWire(NEW_ID
);
81 past_clk
->attributes
["\\init"] = clkpol
? State::S1
: State::S0
;
82 module
->addFf(NEW_ID
, clk
, past_clk
);
84 SigSpec sig_d
= cell
->getPort("\\D");
85 SigSpec sig_q
= cell
->getPort("\\Q");
87 log("Replacing %s.%s (%s): CLK=%s, D=%s, Q=%s\n",
88 log_id(module
), log_id(cell
), log_id(cell
->type
),
89 log_signal(clk
), log_signal(sig_d
), log_signal(sig_q
));
91 SigSpec clock_edge_pattern
;
94 clock_edge_pattern
.append_bit(State::S0
);
95 clock_edge_pattern
.append_bit(State::S1
);
97 clock_edge_pattern
.append_bit(State::S1
);
98 clock_edge_pattern
.append_bit(State::S0
);
101 SigSpec clock_edge
= module
->Eqx(NEW_ID
, {clk
, SigSpec(past_clk
)}, clock_edge_pattern
);
103 Wire
*past_d
= module
->addWire(NEW_ID
, GetSize(sig_d
));
104 Wire
*past_q
= module
->addWire(NEW_ID
, GetSize(sig_q
));
105 module
->addFf(NEW_ID
, sig_d
, past_d
);
106 module
->addFf(NEW_ID
, sig_q
, past_q
);
108 if (cell
->type
== "$adff")
110 SigSpec arst
= cell
->getPort("\\ARST");
111 SigSpec qval
= module
->Mux(NEW_ID
, past_q
, past_d
, clock_edge
);
112 Const rstval
= cell
->parameters
["\\ARST_VALUE"];
114 if (cell
->parameters
["\\ARST_POLARITY"].as_bool())
115 module
->addMux(NEW_ID
, qval
, rstval
, arst
, sig_q
);
117 module
->addMux(NEW_ID
, rstval
, qval
, arst
, sig_q
);
121 module
->addMux(NEW_ID
, past_q
, past_d
, clock_edge
, sig_q
);
125 bool assign_initval
= false;
126 for (int i
= 0; i
< GetSize(sig_d
); i
++) {
127 SigBit qbit
= sigmap(sig_q
[i
]);
128 if (initbits
.count(qbit
)) {
129 initval
.bits
.push_back(initbits
.at(qbit
));
130 del_initbits
.insert(qbit
);
132 initval
.bits
.push_back(State::Sx
);
133 if (initval
.bits
.back() != State::Sx
)
134 assign_initval
= true;
137 if (assign_initval
) {
138 past_d
->attributes
["\\init"] = initval
;
139 past_q
->attributes
["\\init"] = initval
;
142 module
->remove(cell
);
147 for (auto wire
: module
->wires())
148 if (wire
->attributes
.count("\\init") > 0)
150 bool delete_initattr
= true;
151 Const initval
= wire
->attributes
.at("\\init");
152 SigSpec initsig
= sigmap(wire
);
154 for (int i
= 0; i
< GetSize(initval
) && i
< GetSize(initsig
); i
++)
155 if (del_initbits
.count(initsig
[i
]) > 0)
156 initval
[i
] = State::Sx
;
157 else if (initval
[i
] != State::Sx
)
158 delete_initattr
= false;
161 wire
->attributes
.erase("\\init");
163 wire
->attributes
.at("\\init") = initval
;
170 PRIVATE_NAMESPACE_END