Merge remote-tracking branch 'origin/master' into xc7srl
[yosys.git] / techlibs / xilinx / synth_xilinx.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/celltypes.h"
22 #include "kernel/rtlil.h"
23 #include "kernel/log.h"
24
25 USING_YOSYS_NAMESPACE
26 PRIVATE_NAMESPACE_BEGIN
27
28 bool check_label(bool &active, std::string run_from, std::string run_to, std::string label)
29 {
30 if (label == run_from)
31 active = true;
32 if (label == run_to)
33 active = false;
34 return active;
35 }
36
37 struct SynthXilinxPass : public Pass
38 {
39 SynthXilinxPass() : Pass("synth_xilinx", "synthesis for Xilinx FPGAs") { }
40
41 void help() YS_OVERRIDE
42 {
43 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
44 log("\n");
45 log(" synth_xilinx [options]\n");
46 log("\n");
47 log("This command runs synthesis for Xilinx FPGAs. This command does not operate on\n");
48 log("partly selected designs. At the moment this command creates netlists that are\n");
49 log("compatible with 7-Series Xilinx devices.\n");
50 log("\n");
51 log(" -top <module>\n");
52 log(" use the specified module as top module\n");
53 log("\n");
54 log(" -edif <file>\n");
55 log(" write the design to the specified edif file. writing of an output file\n");
56 log(" is omitted if this parameter is not specified.\n");
57 log("\n");
58 log(" -blif <file>\n");
59 log(" write the design to the specified BLIF file. writing of an output file\n");
60 log(" is omitted if this parameter is not specified.\n");
61 log("\n");
62 log(" -vpr\n");
63 log(" generate an output netlist (and BLIF file) suitable for VPR\n");
64 log(" (this feature is experimental and incomplete)\n");
65 log("\n");
66 log(" -nobram\n");
67 log(" disable inference of block rams\n");
68 log("\n");
69 log(" -nodram\n");
70 log(" disable inference of distributed rams\n");
71 log("\n");
72 log(" -nobram\n");
73 log(" disable inference of shift registers\n");
74 log("\n");
75 log(" -run <from_label>:<to_label>\n");
76 log(" only run the commands between the labels (see below). an empty\n");
77 log(" from label is synonymous to 'begin', and empty to label is\n");
78 log(" synonymous to the end of the command list.\n");
79 log("\n");
80 log(" -flatten\n");
81 log(" flatten design before synthesis\n");
82 log("\n");
83 log(" -retime\n");
84 log(" run 'abc' with -dff option\n");
85 log("\n");
86 log("\n");
87 log("The following commands are executed by this synthesis command:\n");
88 log("\n");
89 log(" begin:\n");
90 log(" read_verilog -lib +/xilinx/cells_sim.v\n");
91 log(" read_verilog -lib +/xilinx/cells_xtra.v\n");
92 log(" read_verilog -lib +/xilinx/brams_bb.v\n");
93 log(" hierarchy -check -top <top>\n");
94 log("\n");
95 log(" flatten: (only if -flatten)\n");
96 log(" proc\n");
97 log(" flatten\n");
98 log("\n");
99 log(" coarse:\n");
100 log(" synth -run coarse\n");
101 log("\n");
102 log(" bram: (only executed when '-nobram' is not given)\n");
103 log(" memory_bram -rules +/xilinx/brams.txt\n");
104 log(" techmap -map +/xilinx/brams_map.v\n");
105 log("\n");
106 log(" dram: (only executed when '-nodram' is not given)\n");
107 log(" memory_bram -rules +/xilinx/drams.txt\n");
108 log(" techmap -map +/xilinx/drams_map.v\n");
109 log("\n");
110 log(" fine:\n");
111 log(" opt -fast -full\n");
112 log(" memory_map\n");
113 log(" dffsr2dff\n");
114 log(" dff2dffe\n");
115 log(" opt -full\n");
116 log(" simplemap t:$dff* (only without -nosrl)\n");
117 log(" shregmap -tech xilinx -minlen 3 (only without -nosrl)\n");
118 log(" techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v\n");
119 log(" opt -fast\n");
120 log("\n");
121 log(" map_luts:\n");
122 log(" abc -luts 2:2,3,6:5,10,20 [-dff] (without '-vpr' only!)\n");
123 log(" abc -lut 5 [-dff] (with '-vpr' only!)\n");
124 log(" clean\n");
125 log("\n");
126 log(" map_cells:\n");
127 log(" techmap -map +/xilinx/cells_map.v\n");
128 log(" dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT \\\n");
129 log(" -ff FDRE_1 Q INIT -ff FDCE_1 Q INIT -ff FDPE_1 Q INIT -ff FDSE_1 Q INIT\n");
130 log(" clean\n");
131 log("\n");
132 log(" check:\n");
133 log(" hierarchy -check\n");
134 log(" stat\n");
135 log(" check -noinit\n");
136 log("\n");
137 log(" edif: (only if -edif)\n");
138 log(" write_edif <file-name>\n");
139 log("\n");
140 log(" blif: (only if -blif)\n");
141 log(" write_blif <file-name>\n");
142 log("\n");
143 }
144 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
145 {
146 std::string top_opt = "-auto-top";
147 std::string edif_file;
148 std::string blif_file;
149 std::string run_from, run_to;
150 bool flatten = false;
151 bool retime = false;
152 bool vpr = false;
153 bool nobram = false;
154 bool nodram = false;
155 bool nosrl = false;
156
157 size_t argidx;
158 for (argidx = 1; argidx < args.size(); argidx++)
159 {
160 if (args[argidx] == "-top" && argidx+1 < args.size()) {
161 top_opt = "-top " + args[++argidx];
162 continue;
163 }
164 if (args[argidx] == "-edif" && argidx+1 < args.size()) {
165 edif_file = args[++argidx];
166 continue;
167 }
168 if (args[argidx] == "-blif" && argidx+1 < args.size()) {
169 blif_file = args[++argidx];
170 continue;
171 }
172 if (args[argidx] == "-run" && argidx+1 < args.size()) {
173 size_t pos = args[argidx+1].find(':');
174 if (pos == std::string::npos)
175 break;
176 run_from = args[++argidx].substr(0, pos);
177 run_to = args[argidx].substr(pos+1);
178 continue;
179 }
180 if (args[argidx] == "-flatten") {
181 flatten = true;
182 continue;
183 }
184 if (args[argidx] == "-retime") {
185 retime = true;
186 continue;
187 }
188 if (args[argidx] == "-vpr") {
189 vpr = true;
190 continue;
191 }
192 if (args[argidx] == "-nobram") {
193 nobram = true;
194 continue;
195 }
196 if (args[argidx] == "-nodram") {
197 nodram = true;
198 continue;
199 }
200 if (args[argidx] == "-nosrl") {
201 nosrl = true;
202 continue;
203 }
204 break;
205 }
206 extra_args(args, argidx, design);
207
208 if (!design->full_selection())
209 log_cmd_error("This command only operates on fully selected designs!\n");
210
211 bool active = run_from.empty();
212
213 log_header(design, "Executing SYNTH_XILINX pass.\n");
214 log_push();
215
216 if (check_label(active, run_from, run_to, "begin"))
217 {
218 if (vpr) {
219 Pass::call(design, "read_verilog -lib -D_EXPLICIT_CARRY +/xilinx/cells_sim.v");
220 } else {
221 Pass::call(design, "read_verilog -lib +/xilinx/cells_sim.v");
222 }
223
224 Pass::call(design, "read_verilog -lib +/xilinx/cells_xtra.v");
225
226 if (!nobram) {
227 Pass::call(design, "read_verilog -lib +/xilinx/brams_bb.v");
228 }
229
230 Pass::call(design, stringf("hierarchy -check %s", top_opt.c_str()));
231 }
232
233 if (flatten && check_label(active, run_from, run_to, "flatten"))
234 {
235 Pass::call(design, "proc");
236 Pass::call(design, "flatten");
237 }
238
239 if (check_label(active, run_from, run_to, "coarse"))
240 {
241 Pass::call(design, "synth -run coarse");
242 }
243
244 if (check_label(active, run_from, run_to, "bram"))
245 {
246 if (!nobram) {
247 Pass::call(design, "memory_bram -rules +/xilinx/brams.txt");
248 Pass::call(design, "techmap -map +/xilinx/brams_map.v");
249 }
250 }
251
252 if (check_label(active, run_from, run_to, "dram"))
253 {
254 if (!nodram) {
255 Pass::call(design, "memory_bram -rules +/xilinx/drams.txt");
256 Pass::call(design, "techmap -map +/xilinx/drams_map.v");
257 }
258 }
259
260 if (check_label(active, run_from, run_to, "fine"))
261 {
262 Pass::call(design, "opt -fast -full");
263 Pass::call(design, "memory_map");
264 Pass::call(design, "dffsr2dff");
265 Pass::call(design, "dff2dffe");
266 Pass::call(design, "opt -full");
267
268 if (!nosrl) {
269 Pass::call(design, "simplemap t:$dff*");
270 Pass::call(design, "shregmap -tech xilinx -minlen 3");
271 }
272
273 if (vpr) {
274 Pass::call(design, "techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v -D _EXPLICIT_CARRY");
275 } else {
276 Pass::call(design, "techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v");
277 }
278
279 Pass::call(design, "hierarchy -check");
280 Pass::call(design, "opt -fast");
281 }
282
283 if (check_label(active, run_from, run_to, "map_luts"))
284 {
285 Pass::call(design, "abc -luts 2:2,3,6:5,10,20" + string(retime ? " -dff" : ""));
286 Pass::call(design, "clean");
287 Pass::call(design, "techmap -map +/xilinx/lut_map.v");
288 }
289
290 if (check_label(active, run_from, run_to, "map_cells"))
291 {
292 Pass::call(design, "techmap -map +/xilinx/cells_map.v");
293 Pass::call(design, "dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT "
294 "-ff FDRE_1 Q INIT -ff FDCE_1 Q INIT -ff FDPE_1 Q INIT -ff FDSE_1 Q INIT");
295 Pass::call(design, "clean");
296 }
297
298 if (check_label(active, run_from, run_to, "check"))
299 {
300 Pass::call(design, "hierarchy -check");
301 Pass::call(design, "stat");
302 Pass::call(design, "check -noinit");
303 }
304
305 if (check_label(active, run_from, run_to, "edif"))
306 {
307 if (!edif_file.empty())
308 Pass::call(design, stringf("write_edif -pvector bra %s", edif_file.c_str()));
309 }
310 if (check_label(active, run_from, run_to, "blif"))
311 {
312 if (!blif_file.empty())
313 Pass::call(design, stringf("write_blif %s", edif_file.c_str()));
314 }
315
316 log_pop();
317 }
318 } SynthXilinxPass;
319
320 PRIVATE_NAMESPACE_END