Forgot to slice
[yosys.git] / passes / pmgen / xilinx_srl.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * (C) 2019 Eddie Hung <eddie@fpgeh.com>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 #include "kernel/yosys.h"
22 #include "kernel/sigtools.h"
23
24 USING_YOSYS_NAMESPACE
25 PRIVATE_NAMESPACE_BEGIN
26
27 // for peepopt_pm
28 bool did_something;
29
30 #include "passes/pmgen/xilinx_srl_pm.h"
31 #include "passes/pmgen/ice40_dsp_pm.h"
32 #include "passes/pmgen/peepopt_pm.h"
33
34 void run_fixed(xilinx_srl_pm &pm)
35 {
36 auto &st = pm.st_fixed;
37 auto &ud = pm.ud_fixed;
38 auto param_def = [&ud](Cell *cell, IdString param) {
39 auto def = ud.default_params.at(std::make_pair(cell->type,param));
40 return cell->parameters.at(param, def);
41 };
42
43 log("Found fixed chain of length %d (%s):\n", GetSize(ud.longest_chain), log_id(st.first->type));
44
45 auto last_cell = ud.longest_chain.back();
46
47 SigSpec initval;
48 for (auto cell : ud.longest_chain) {
49 log_debug(" %s\n", log_id(cell));
50 if (cell->type.in(ID($_DFF_N_), ID($_DFF_P_), ID($_DFFE_NN_), ID($_DFFE_NP_), ID($_DFFE_PN_), ID($_DFFE_PP_))) {
51 SigBit Q = cell->getPort(ID(Q));
52 log_assert(Q.wire);
53 auto it = Q.wire->attributes.find(ID(init));
54 if (it != Q.wire->attributes.end()) {
55 initval.append(it->second[Q.offset]);
56 }
57 else
58 initval.append(State::Sx);
59 }
60 else if (cell->type.in(ID(FDRE), ID(FDRE_1)))
61 initval.append(param_def(cell, ID(INIT)));
62 else
63 log_abort();
64 if (cell != last_cell)
65 pm.autoremove(cell);
66 }
67
68 Cell *c = last_cell;
69 SigBit Q = st.first->getPort(ID(Q));
70 c->setPort(ID(Q), Q);
71
72 if (c->type.in(ID($_DFF_N_), ID($_DFF_P_), ID($_DFFE_NN_), ID($_DFFE_NP_), ID($_DFFE_PN_), ID($_DFFE_PP_), ID(FDRE), ID(FDRE_1))) {
73 c->parameters.clear();
74 c->setParam(ID(DEPTH), GetSize(ud.longest_chain));
75 c->setParam(ID(INIT), initval.as_const());
76 if (c->type.in(ID($_DFF_P_), ID($_DFFE_PN_), ID($_DFFE_PP_)))
77 c->setParam(ID(CLKPOL), 1);
78 else if (c->type.in(ID($_DFF_N_), ID($DFFE_NN_), ID($_DFFE_NP_), ID(FDRE_1)))
79 c->setParam(ID(CLKPOL), 0);
80 else if (c->type.in(ID(FDRE)))
81 c->setParam(ID(CLKPOL), param_def(c, ID(IS_C_INVERTED)).as_bool() ? 0 : 1);
82 else
83 log_abort();
84 if (c->type.in(ID($_DFFE_NP_), ID($_DFFE_PP_)))
85 c->setParam(ID(ENPOL), 1);
86 else if (c->type.in(ID($_DFFE_NN_), ID($_DFFE_PN_)))
87 c->setParam(ID(ENPOL), 0);
88 else
89 c->setParam(ID(ENPOL), 2);
90 if (c->type.in(ID($_DFF_N_), ID($_DFF_P_)))
91 c->setPort(ID(E), State::S1);
92 c->setPort(ID(L), GetSize(ud.longest_chain)-1);
93 c->type = ID($__XILINX_SHREG_);
94 }
95 else
96 log_abort();
97
98 log(" -> %s (%s)\n", log_id(c), log_id(c->type));
99 }
100
101 void run_variable(xilinx_srl_pm &pm)
102 {
103 auto &st = pm.st_variable;
104 auto &ud = pm.ud_variable;
105
106 log("Found variable chain of length %d (%s):\n", GetSize(ud.chain), log_id(st.first->type));
107
108 auto last_cell = ud.chain.back().first;
109 auto last_slice = ud.chain.back().second;
110
111 SigSpec initval;
112 for (const auto &i : ud.chain) {
113 auto cell = i.first;
114 auto slice = i.second;
115 log_debug(" %s\n", log_id(cell));
116 if (cell->type.in(ID($_DFF_N_), ID($_DFF_P_), ID($_DFFE_NN_), ID($_DFFE_NP_), ID($_DFFE_PN_), ID($_DFFE_PP_), ID($dff), ID($dffe))) {
117 SigBit Q = cell->getPort(ID(Q))[slice];
118 log_assert(Q.wire);
119 auto it = Q.wire->attributes.find(ID(init));
120 if (it != Q.wire->attributes.end()) {
121 initval.append(it->second[Q.offset]);
122 }
123 else
124 initval.append(State::Sx);
125 }
126 else
127 log_abort();
128 if (cell != last_cell)
129 cell->connections_.at(ID(Q))[slice] = pm.module->addWire(NEW_ID);
130 }
131 pm.autoremove(st.shiftx);
132
133 Cell *c = last_cell;
134 SigBit Q = st.first->getPort(ID(Q))[last_slice];
135 c->setPort(ID(Q), Q);
136
137 if (c->type.in(ID($_DFF_N_), ID($_DFF_P_), ID($_DFFE_NN_), ID($_DFFE_NP_), ID($_DFFE_PN_), ID($_DFFE_PP_), ID($dff), ID($dffe))) {
138 Const clkpol, enpol;
139 if (c->type.in(ID($_DFF_P_), ID($_DFFE_PN_), ID($_DFFE_PP_)))
140 clkpol = 1;
141 else if (c->type.in(ID($_DFF_N_), ID($DFFE_NN_), ID($_DFFE_NP_)))
142 clkpol = 0;
143 else if (c->type.in(ID($dff), ID($dffe))) {
144 clkpol = c->getParam(ID(CLK_POLARITY));
145 c->setPort(ID(C), c->getPort(ID(CLK)));
146 c->unsetPort(ID(CLK));
147 }
148 else
149 log_abort();
150 if (c->type.in(ID($_DFFE_NP_), ID($_DFFE_PP_)))
151 enpol = 1;
152 else if (c->type.in(ID($_DFFE_NN_), ID($_DFFE_PN_)))
153 enpol = 0;
154 else if (c->type.in(ID($dffe))) {
155 enpol = c->getParam(ID(EN_POLARITY));
156 c->setPort(ID(E), c->getPort(ID(EN)));
157 c->unsetPort(ID(EN));
158 }
159 else
160 enpol = 2;
161 c->parameters.clear();
162 c->setParam(ID(DEPTH), GetSize(ud.chain));
163 c->setParam(ID(INIT), initval.as_const());
164 c->setParam(ID(CLKPOL), clkpol);
165 c->setParam(ID(ENPOL), enpol);
166 if (c->type.in(ID($_DFF_N_), ID($_DFF_P_), ID($dff)))
167 c->setPort(ID(E), State::S1);
168 c->setPort(ID(L), st.shiftx->getPort(ID(B)));
169 c->setPort(ID(Q), st.shiftx->getPort(ID(Y)));
170 c->type = ID($__XILINX_SHREG_);
171 }
172 else
173 log_abort();
174
175 log(" -> %s (%s)\n", log_id(c), log_id(c->type));
176
177 }
178
179 struct XilinxSrlPass : public Pass {
180 XilinxSrlPass() : Pass("xilinx_srl", "Xilinx shift register extraction") { }
181 void help() YS_OVERRIDE
182 {
183 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
184 log("\n");
185 log(" xilinx_srl [options] [selection]\n");
186 log("\n");
187 log("This pass converts chains of built-in flops ($_DFF_[NP]_, $_DFFE_*) as well as\n");
188 log("Xilinx flops (FDRE, FDRE_1) into a $__XILINX_SHREG cell. Chains must be of the\n");
189 log("same type, clock, clock polarity, enable, enable polarity (when relevant).\n");
190 log("Flops with resets cannot be mapped to Xilinx devices and will not be inferred.");
191 log("\n");
192 log(" -minlen N\n");
193 log(" min length of shift register (default = 3)\n");
194 log("\n");
195 log(" -fixed\n");
196 log(" infer fixed-length shift registers.\n");
197 log("\n");
198 log(" -variable\n");
199 log(" infer variable-length shift registers (i.e. fixed-length shifts where\n");
200 log(" each element also fans-out to a $shiftx cell.\n");
201 log("\n");
202 }
203
204 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
205 {
206 log_header(design, "Executing XILINX_SRL pass (Xilinx shift register extraction).\n");
207
208 bool fixed = false;
209 bool variable = false;
210 int minlen = 3;
211
212 size_t argidx;
213 for (argidx = 1; argidx < args.size(); argidx++)
214 {
215 if (args[argidx] == "-minlen" && argidx+1 < args.size()) {
216 minlen = atoi(args[++argidx].c_str());
217 continue;
218 }
219 if (args[argidx] == "-fixed") {
220 fixed = true;
221 continue;
222 }
223 if (args[argidx] == "-variable") {
224 variable = true;
225 continue;
226 }
227 break;
228 }
229 extra_args(args, argidx, design);
230
231 if (!fixed && !variable)
232 log_cmd_error("'-fixed' and/or '-variable' must be specified.\n");
233
234 for (auto module : design->selected_modules()) {
235 auto pm = xilinx_srl_pm(module, module->selected_cells());
236 pm.ud_fixed.minlen = minlen;
237 pm.ud_variable.minlen = minlen;
238
239 if (fixed) {
240 // TODO: How to get these automatically?
241 pm.ud_fixed.default_params[std::make_pair(ID(FDRE),ID(INIT))] = State::S0;
242 pm.ud_fixed.default_params[std::make_pair(ID(FDRE),ID(IS_C_INVERTED))] = State::S0;
243 pm.ud_fixed.default_params[std::make_pair(ID(FDRE),ID(IS_D_INVERTED))] = State::S0;
244 pm.ud_fixed.default_params[std::make_pair(ID(FDRE),ID(IS_R_INVERTED))] = State::S0;
245 pm.run_fixed(run_fixed);
246 }
247 if (variable)
248 pm.run_variable(run_variable);
249 }
250 }
251 } XilinxSrlPass;
252
253 PRIVATE_NAMESPACE_END