Added dffsr support to proc_dff pass
[yosys.git] / passes / proc / proc_dff.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/register.h"
21 #include "kernel/sigtools.h"
22 #include "kernel/consteval.h"
23 #include "kernel/log.h"
24 #include <sstream>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 static RTLIL::SigSpec find_any_lvalue(const RTLIL::Process *proc)
30 {
31 RTLIL::SigSpec lvalue;
32
33 for (auto sync : proc->syncs)
34 for (auto &action : sync->actions)
35 if (action.first.width > 0) {
36 lvalue = action.first;
37 lvalue.sort_and_unify();
38 break;
39 }
40
41 for (auto sync : proc->syncs) {
42 RTLIL::SigSpec this_lvalue;
43 for (auto &action : sync->actions)
44 this_lvalue.append(action.first);
45 this_lvalue.sort_and_unify();
46 RTLIL::SigSpec common_sig = this_lvalue.extract(lvalue);
47 if (common_sig.width > 0)
48 lvalue = common_sig;
49 }
50
51 return lvalue;
52 }
53
54 static void gen_dffsr(RTLIL::Module *mod, RTLIL::SigSpec sig_in, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_out,
55 bool clk_polarity, bool set_polarity, RTLIL::SigSpec clk, RTLIL::SigSpec set, RTLIL::Process *proc)
56 {
57 std::stringstream sstr;
58 sstr << "$procdff$" << (RTLIL::autoidx++);
59
60 RTLIL::SigSpec sig_set_inv = NEW_WIRE(mod, sig_in.width);
61 RTLIL::SigSpec sig_sr_set = NEW_WIRE(mod, sig_in.width);
62 RTLIL::SigSpec sig_sr_clr = NEW_WIRE(mod, sig_in.width);
63
64 RTLIL::Cell *inv_set = new RTLIL::Cell;
65 inv_set->name = NEW_ID;
66 inv_set->type = "$not";
67 inv_set->parameters["\\A_WIDTH"] = RTLIL::Const(sig_in.width);
68 inv_set->parameters["\\Y_WIDTH"] = RTLIL::Const(sig_in.width);
69 inv_set->connections["\\A"] = sig_set;
70 inv_set->connections["\\Y"] = sig_set_inv;
71 mod->add(inv_set);
72
73 RTLIL::Cell *mux_sr_set = new RTLIL::Cell;
74 mux_sr_set->name = NEW_ID;
75 mux_sr_set->type = "$mux";
76 mux_sr_set->parameters["\\WIDTH"] = RTLIL::Const(sig_in.width);
77 mux_sr_set->connections[set_polarity ? "\\A" : "\\B"] = RTLIL::Const(0, sig_in.width);
78 mux_sr_set->connections[set_polarity ? "\\B" : "\\A"] = sig_set;
79 mux_sr_set->connections["\\Y"] = sig_sr_set;
80 mux_sr_set->connections["\\S"] = set;
81 mod->add(mux_sr_set);
82
83 RTLIL::Cell *mux_sr_clr = new RTLIL::Cell;
84 mux_sr_clr->name = NEW_ID;
85 mux_sr_clr->type = "$mux";
86 mux_sr_clr->parameters["\\WIDTH"] = RTLIL::Const(sig_in.width);
87 mux_sr_clr->connections[set_polarity ? "\\A" : "\\B"] = RTLIL::Const(0, sig_in.width);
88 mux_sr_clr->connections[set_polarity ? "\\B" : "\\A"] = sig_set_inv;
89 mux_sr_clr->connections["\\Y"] = sig_sr_clr;
90 mux_sr_clr->connections["\\S"] = set;
91 mod->add(mux_sr_clr);
92
93 RTLIL::Cell *cell = new RTLIL::Cell;
94 cell->name = sstr.str();
95 cell->type = "$dffsr";
96 cell->attributes = proc->attributes;
97 mod->add(cell);
98
99 cell->parameters["\\WIDTH"] = RTLIL::Const(sig_in.width);
100 cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity, 1);
101 cell->parameters["\\SET_POLARITY"] = RTLIL::Const(true, 1);
102 cell->parameters["\\CLR_POLARITY"] = RTLIL::Const(true, 1);
103
104 cell->connections["\\D"] = sig_in;
105 cell->connections["\\Q"] = sig_out;
106 cell->connections["\\CLK"] = clk;
107 cell->connections["\\SET"] = sig_sr_set;
108 cell->connections["\\CLR"] = sig_sr_clr;
109
110 log(" created %s cell `%s' with %s edge clock and %s level non-const reset.\n", cell->type.c_str(), cell->name.c_str(),
111 clk_polarity ? "positive" : "negative", set_polarity ? "positive" : "negative");
112 }
113
114 static void gen_dff(RTLIL::Module *mod, RTLIL::SigSpec sig_in, RTLIL::Const val_rst, RTLIL::SigSpec sig_out,
115 bool clk_polarity, bool arst_polarity, RTLIL::SigSpec clk, RTLIL::SigSpec *arst, RTLIL::Process *proc)
116 {
117 std::stringstream sstr;
118 sstr << "$procdff$" << (RTLIL::autoidx++);
119
120 RTLIL::Cell *cell = new RTLIL::Cell;
121 cell->name = sstr.str();
122 cell->type = arst ? "$adff" : "$dff";
123 cell->attributes = proc->attributes;
124 mod->cells[cell->name] = cell;
125
126 cell->parameters["\\WIDTH"] = RTLIL::Const(sig_in.width);
127 if (arst) {
128 cell->parameters["\\ARST_POLARITY"] = RTLIL::Const(arst_polarity, 1);
129 cell->parameters["\\ARST_VALUE"] = val_rst;
130 }
131 cell->parameters["\\CLK_POLARITY"] = RTLIL::Const(clk_polarity, 1);
132
133 cell->connections["\\D"] = sig_in;
134 cell->connections["\\Q"] = sig_out;
135 if (arst)
136 cell->connections["\\ARST"] = *arst;
137 cell->connections["\\CLK"] = clk;
138
139 log(" created %s cell `%s' with %s edge clock", cell->type.c_str(), cell->name.c_str(), clk_polarity ? "positive" : "negative");
140 if (arst)
141 log(" and %s level reset", arst_polarity ? "positive" : "negative");
142 log(".\n");
143 }
144
145 static void proc_dff(RTLIL::Module *mod, RTLIL::Process *proc, ConstEval &ce)
146 {
147 while (1)
148 {
149 RTLIL::SigSpec sig = find_any_lvalue(proc);
150
151 if (sig.width == 0)
152 break;
153
154 log("Creating register for signal `%s.%s' using process `%s.%s'.\n",
155 mod->name.c_str(), log_signal(sig), mod->name.c_str(), proc->name.c_str());
156
157 RTLIL::SigSpec insig = RTLIL::SigSpec(RTLIL::State::Sz, sig.width);
158 RTLIL::SigSpec rstval = RTLIL::SigSpec(RTLIL::State::Sz, sig.width);
159 RTLIL::SyncRule *sync_level = NULL;
160 RTLIL::SyncRule *sync_edge = NULL;
161 RTLIL::SyncRule *sync_always = NULL;
162
163 for (auto sync : proc->syncs)
164 for (auto &action : sync->actions)
165 {
166 if (action.first.extract(sig).width == 0)
167 continue;
168
169 if (sync->type == RTLIL::SyncType::ST0 || sync->type == RTLIL::SyncType::ST1) {
170 if (sync_level != NULL && sync_level != sync)
171 log_error("Multiple level sensitive events found for this signal!\n");
172 sig.replace(action.first, action.second, &rstval);
173 sync_level = sync;
174 }
175 else if (sync->type == RTLIL::SyncType::STp || sync->type == RTLIL::SyncType::STn) {
176 if (sync_edge != NULL && sync_edge != sync)
177 log_error("Multiple edge sensitive events found for this signal!\n");
178 sig.replace(action.first, action.second, &insig);
179 sync_edge = sync;
180 }
181 else if (sync->type == RTLIL::SyncType::STa) {
182 if (sync_always != NULL && sync_always != sync)
183 log_error("Multiple always events found for this signal!\n");
184 sig.replace(action.first, action.second, &insig);
185 sync_always = sync;
186 }
187 else {
188 log_error("Event with any-edge sensitivity found for this signal!\n");
189 }
190
191 action.first.remove2(sig, &action.second);
192 }
193
194 ce.assign_map.apply(insig);
195 ce.assign_map.apply(rstval);
196 ce.assign_map.apply(sig);
197
198 insig.optimize();
199 rstval.optimize();
200 sig.optimize();
201
202 if (sync_always) {
203 if (sync_edge || sync_level)
204 log_error("Mixed always event with edge and/or level sensitive events!\n");
205 log(" created direct connection (no actual register cell created).\n");
206 mod->connections.push_back(RTLIL::SigSig(sig, insig));
207 continue;
208 }
209
210 if (!sync_edge)
211 log_error("Missing edge-sensitive event for this signal!\n");
212
213 if (!rstval.is_fully_const() && !ce.eval(rstval))
214 {
215 log("WARNING: Async reset value `%s' is not constant!\n", log_signal(rstval));
216 gen_dffsr(mod, insig, rstval, sig,
217 sync_edge->type == RTLIL::SyncType::STp,
218 sync_level && sync_level->type == RTLIL::SyncType::ST1,
219 sync_edge->signal, sync_level->signal, proc);
220 } else
221 gen_dff(mod, insig, rstval.chunks[0].data, sig,
222 sync_edge->type == RTLIL::SyncType::STp,
223 sync_level && sync_level->type == RTLIL::SyncType::ST1,
224 sync_edge->signal, sync_level ? &sync_level->signal : NULL, proc);
225 }
226 }
227
228 struct ProcDffPass : public Pass {
229 ProcDffPass() : Pass("proc_dff", "extract flip-flops from processes") { }
230 virtual void help()
231 {
232 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
233 log("\n");
234 log(" proc_dff [selection]\n");
235 log("\n");
236 log("This pass identifies flip-flops in the processes and converts them to\n");
237 log("d-type flip-flop cells.\n");
238 log("\n");
239 }
240 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
241 {
242 log_header("Executing PROC_DFF pass (convert process syncs to FFs).\n");
243
244 extra_args(args, 1, design);
245
246 for (auto &mod_it : design->modules)
247 if (design->selected(mod_it.second)) {
248 ConstEval ce(mod_it.second);
249 for (auto &proc_it : mod_it.second->processes)
250 if (design->selected(mod_it.second, proc_it.second))
251 proc_dff(mod_it.second, proc_it.second, ce);
252 }
253 }
254 } ProcDffPass;
255