Merge remote-tracking branch 'origin/eddie/muxpack' into xc7mux
[yosys.git] / techlibs / ecp5 / synth_ecp5.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2018 Clifford Wolf <dave@ds0.me>
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/register.h"
22 #include "kernel/celltypes.h"
23 #include "kernel/rtlil.h"
24 #include "kernel/log.h"
25
26 USING_YOSYS_NAMESPACE
27 PRIVATE_NAMESPACE_BEGIN
28
29 struct SynthEcp5Pass : public ScriptPass
30 {
31 SynthEcp5Pass() : ScriptPass("synth_ecp5", "synthesis for ECP5 FPGAs") { }
32
33 void help() YS_OVERRIDE
34 {
35 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
36 log("\n");
37 log(" synth_ecp5 [options]\n");
38 log("\n");
39 log("This command runs synthesis for ECP5 FPGAs.\n");
40 log("\n");
41 log(" -top <module>\n");
42 log(" use the specified module as top module\n");
43 log("\n");
44 log(" -blif <file>\n");
45 log(" write the design to the specified BLIF file. writing of an output file\n");
46 log(" is omitted if this parameter is not specified.\n");
47 log("\n");
48 log(" -edif <file>\n");
49 log(" write the design to the specified EDIF file. writing of an output file\n");
50 log(" is omitted if this parameter is not specified.\n");
51 log("\n");
52 log(" -json <file>\n");
53 log(" write the design to the specified JSON file. writing of an output file\n");
54 log(" is omitted if this parameter is not specified.\n");
55 log("\n");
56 log(" -run <from_label>:<to_label>\n");
57 log(" only run the commands between the labels (see below). an empty\n");
58 log(" from label is synonymous to 'begin', and empty to label is\n");
59 log(" synonymous to the end of the command list.\n");
60 log("\n");
61 log(" -noflatten\n");
62 log(" do not flatten design before synthesis\n");
63 log("\n");
64 log(" -retime\n");
65 log(" run 'abc' with -dff option\n");
66 log("\n");
67 log(" -noccu2\n");
68 log(" do not use CCU2 cells in output netlist\n");
69 log("\n");
70 log(" -nodffe\n");
71 log(" do not use flipflops with CE in output netlist\n");
72 log("\n");
73 log(" -nobram\n");
74 log(" do not use BRAM cells in output netlist\n");
75 log("\n");
76 log(" -nodram\n");
77 log(" do not use distributed RAM cells in output netlist\n");
78 log("\n");
79 log(" -nomux\n");
80 log(" do not use PFU muxes to implement LUTs larger than LUT4s\n");
81 log("\n");
82 log(" -abc2\n");
83 log(" run two passes of 'abc' for slightly improved logic density\n");
84 log("\n");
85 log(" -vpr\n");
86 log(" generate an output netlist (and BLIF file) suitable for VPR\n");
87 log(" (this feature is experimental and incomplete)\n");
88 log("\n");
89 log("\n");
90 log("The following commands are executed by this synthesis command:\n");
91 help_script();
92 log("\n");
93 }
94
95 string top_opt, blif_file, edif_file, json_file;
96 bool noccu2, nodffe, nobram, nodram, nomux, flatten, retime, abc2, vpr;
97
98 void clear_flags() YS_OVERRIDE
99 {
100 top_opt = "-auto-top";
101 blif_file = "";
102 edif_file = "";
103 json_file = "";
104 noccu2 = false;
105 nodffe = false;
106 nobram = false;
107 nodram = false;
108 nomux = false;
109 flatten = true;
110 retime = false;
111 abc2 = false;
112 vpr = false;
113 }
114
115 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
116 {
117 string run_from, run_to;
118 clear_flags();
119
120 size_t argidx;
121 for (argidx = 1; argidx < args.size(); argidx++)
122 {
123 if (args[argidx] == "-top" && argidx+1 < args.size()) {
124 top_opt = "-top " + args[++argidx];
125 continue;
126 }
127 if (args[argidx] == "-blif" && argidx+1 < args.size()) {
128 blif_file = args[++argidx];
129 continue;
130 }
131 if (args[argidx] == "-edif" && argidx+1 < args.size()) {
132 edif_file = args[++argidx];
133 continue;
134 }
135 if (args[argidx] == "-json" && argidx+1 < args.size()) {
136 json_file = args[++argidx];
137 continue;
138 }
139 if (args[argidx] == "-run" && argidx+1 < args.size()) {
140 size_t pos = args[argidx+1].find(':');
141 if (pos == std::string::npos)
142 break;
143 run_from = args[++argidx].substr(0, pos);
144 run_to = args[argidx].substr(pos+1);
145 continue;
146 }
147 if (args[argidx] == "-flatten") {
148 flatten = true;
149 continue;
150 }
151 if (args[argidx] == "-noflatten") {
152 flatten = false;
153 continue;
154 }
155 if (args[argidx] == "-retime") {
156 retime = true;
157 continue;
158 }
159 if (args[argidx] == "-noccu2") {
160 noccu2 = true;
161 continue;
162 }
163 if (args[argidx] == "-nodffe") {
164 nodffe = true;
165 continue;
166 }
167 if (args[argidx] == "-nobram") {
168 nobram = true;
169 continue;
170 }
171 if (args[argidx] == "-nodram") {
172 nodram = true;
173 continue;
174 }
175 if (args[argidx] == "-nomux") {
176 nomux = true;
177 continue;
178 }
179 if (args[argidx] == "-abc2") {
180 abc2 = true;
181 continue;
182 }
183 if (args[argidx] == "-vpr") {
184 vpr = true;
185 continue;
186 }
187 break;
188 }
189 extra_args(args, argidx, design);
190
191 if (!design->full_selection())
192 log_cmd_error("This command only operates on fully selected designs!\n");
193
194 log_header(design, "Executing SYNTH_ECP5 pass.\n");
195 log_push();
196
197 run_script(design, run_from, run_to);
198
199 log_pop();
200 }
201
202 void script() YS_OVERRIDE
203 {
204 if (check_label("begin"))
205 {
206 run("read_verilog -lib +/ecp5/cells_sim.v +/ecp5/cells_bb.v");
207 run(stringf("hierarchy -check %s", help_mode ? "-top <top>" : top_opt.c_str()));
208 }
209
210 if (flatten && check_label("flatten", "(unless -noflatten)"))
211 {
212 run("proc");
213 run("flatten");
214 run("tribuf -logic");
215 run("deminout");
216 }
217
218 if (check_label("coarse"))
219 {
220 run("synth -run coarse");
221 }
222
223 if (!nobram && check_label("bram", "(skip if -nobram)"))
224 {
225 run("memory_bram -rules +/ecp5/bram.txt");
226 run("techmap -map +/ecp5/brams_map.v");
227 }
228
229 if (!nodram && check_label("dram", "(skip if -nodram)"))
230 {
231 run("memory_bram -rules +/ecp5/dram.txt");
232 run("techmap -map +/ecp5/drams_map.v");
233 }
234
235 if (check_label("fine"))
236 {
237 run("opt -fast -mux_undef -undriven -fine");
238 run("memory_map");
239 run("opt -undriven -fine");
240 if (noccu2)
241 run("techmap");
242 else
243 run("techmap -map +/techmap.v -map +/ecp5/arith_map.v");
244 if (retime || help_mode)
245 run("abc -dff", "(only if -retime)");
246 }
247
248 if (check_label("map_ffs"))
249 {
250 run("dffsr2dff");
251 run("dff2dffs");
252 run("opt_clean");
253 if (!nodffe)
254 run("dff2dffe -direct-match $_DFF_* -direct-match $__DFFS_*");
255 run("techmap -D NO_LUT -map +/ecp5/cells_map.v");
256 run("opt_expr -undriven -mux_undef");
257 run("simplemap");
258 run("ecp5_ffinit");
259 }
260
261 if (check_label("map_luts"))
262 {
263 if (abc2 || help_mode) {
264 run("abc", " (only if -abc2)");
265 }
266 run("techmap -map +/ecp5/latches_map.v");
267 if (nomux)
268 run("abc -lut 4 -dress");
269 else
270 run("abc -lut 4:7 -dress");
271 run("clean");
272 }
273
274 if (check_label("map_cells"))
275 {
276 if (vpr)
277 run("techmap -D NO_LUT -map +/ecp5/cells_map.v");
278 else
279 run("techmap -map +/ecp5/cells_map.v", "(with -D NO_LUT in vpr mode)");
280
281 run("clean");
282 }
283
284 if (check_label("check"))
285 {
286 run("hierarchy -check");
287 run("stat");
288 run("check -noinit");
289 }
290
291 if (check_label("blif"))
292 {
293 if (!blif_file.empty() || help_mode) {
294 if (vpr || help_mode) {
295 run(stringf("opt_clean -purge"),
296 " (vpr mode)");
297 run(stringf("write_blif -attr -cname -conn -param %s",
298 help_mode ? "<file-name>" : blif_file.c_str()),
299 " (vpr mode)");
300 }
301 if (!vpr)
302 run(stringf("write_blif -gates -attr -param %s",
303 help_mode ? "<file-name>" : blif_file.c_str()),
304 " (non-vpr mode)");
305 }
306 }
307
308 if (check_label("edif"))
309 {
310 if (!edif_file.empty() || help_mode)
311 run(stringf("write_edif %s", help_mode ? "<file-name>" : edif_file.c_str()));
312 }
313
314 if (check_label("json"))
315 {
316 if (!json_file.empty() || help_mode)
317 run(stringf("write_json %s", help_mode ? "<file-name>" : json_file.c_str()));
318 }
319 }
320 } SynthEcp5Pass;
321
322 PRIVATE_NAMESPACE_END