Merge pull request #859 from smunaut/ice40_braminit
[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 infering of block rams\n");
68 log("\n");
69 log(" -nodram\n");
70 log(" disable infering of distributed rams\n");
71 log("\n");
72 log(" -run <from_label>:<to_label>\n");
73 log(" only run the commands between the labels (see below). an empty\n");
74 log(" from label is synonymous to 'begin', and empty to label is\n");
75 log(" synonymous to the end of the command list.\n");
76 log("\n");
77 log(" -flatten\n");
78 log(" flatten design before synthesis\n");
79 log("\n");
80 log(" -retime\n");
81 log(" run 'abc' with -dff option\n");
82 log("\n");
83 log("\n");
84 log("The following commands are executed by this synthesis command:\n");
85 log("\n");
86 log(" begin:\n");
87 log(" read_verilog -lib +/xilinx/cells_sim.v\n");
88 log(" read_verilog -lib +/xilinx/cells_xtra.v\n");
89 log(" read_verilog -lib +/xilinx/brams_bb.v\n");
90 log(" hierarchy -check -top <top>\n");
91 log("\n");
92 log(" flatten: (only if -flatten)\n");
93 log(" proc\n");
94 log(" flatten\n");
95 log("\n");
96 log(" coarse:\n");
97 log(" synth -run coarse\n");
98 log("\n");
99 log(" bram: (only executed when '-nobram' is not given)\n");
100 log(" memory_bram -rules +/xilinx/brams.txt\n");
101 log(" techmap -map +/xilinx/brams_map.v\n");
102 log("\n");
103 log(" dram: (only executed when '-nodram' is not given)\n");
104 log(" memory_bram -rules +/xilinx/drams.txt\n");
105 log(" techmap -map +/xilinx/drams_map.v\n");
106 log("\n");
107 log(" fine:\n");
108 log(" opt -fast -full\n");
109 log(" memory_map\n");
110 log(" dffsr2dff\n");
111 log(" dff2dffe\n");
112 log(" opt -full\n");
113 log(" techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v\n");
114 log(" opt -fast\n");
115 log("\n");
116 log(" map_luts:\n");
117 log(" abc -luts 2:2,3,6:5,10,20 [-dff] (without '-vpr' only!)\n");
118 log(" abc -lut 5 [-dff] (with '-vpr' only!)\n");
119 log(" clean\n");
120 log("\n");
121 log(" map_cells:\n");
122 log(" techmap -map +/xilinx/cells_map.v\n");
123 log(" dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT\n");
124 log(" clean\n");
125 log("\n");
126 log(" check:\n");
127 log(" hierarchy -check\n");
128 log(" stat\n");
129 log(" check -noinit\n");
130 log("\n");
131 log(" edif: (only if -edif)\n");
132 log(" write_edif <file-name>\n");
133 log("\n");
134 log(" blif: (only if -blif)\n");
135 log(" write_blif <file-name>\n");
136 log("\n");
137 }
138 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
139 {
140 std::string top_opt = "-auto-top";
141 std::string edif_file;
142 std::string blif_file;
143 std::string run_from, run_to;
144 bool flatten = false;
145 bool retime = false;
146 bool vpr = false;
147 bool nobram = false;
148 bool nodram = false;
149
150 size_t argidx;
151 for (argidx = 1; argidx < args.size(); argidx++)
152 {
153 if (args[argidx] == "-top" && argidx+1 < args.size()) {
154 top_opt = "-top " + args[++argidx];
155 continue;
156 }
157 if (args[argidx] == "-edif" && argidx+1 < args.size()) {
158 edif_file = args[++argidx];
159 continue;
160 }
161 if (args[argidx] == "-blif" && argidx+1 < args.size()) {
162 blif_file = args[++argidx];
163 continue;
164 }
165 if (args[argidx] == "-run" && argidx+1 < args.size()) {
166 size_t pos = args[argidx+1].find(':');
167 if (pos == std::string::npos)
168 break;
169 run_from = args[++argidx].substr(0, pos);
170 run_to = args[argidx].substr(pos+1);
171 continue;
172 }
173 if (args[argidx] == "-flatten") {
174 flatten = true;
175 continue;
176 }
177 if (args[argidx] == "-retime") {
178 retime = true;
179 continue;
180 }
181 if (args[argidx] == "-vpr") {
182 vpr = true;
183 continue;
184 }
185 if (args[argidx] == "-nobram") {
186 nobram = true;
187 continue;
188 }
189 if (args[argidx] == "-nodram") {
190 nodram = true;
191 continue;
192 }
193 break;
194 }
195 extra_args(args, argidx, design);
196
197 if (!design->full_selection())
198 log_cmd_error("This command only operates on fully selected designs!\n");
199
200 bool active = run_from.empty();
201
202 log_header(design, "Executing SYNTH_XILINX pass.\n");
203 log_push();
204
205 if (check_label(active, run_from, run_to, "begin"))
206 {
207 if (vpr) {
208 Pass::call(design, "read_verilog -lib -D_EXPLICIT_CARRY +/xilinx/cells_sim.v");
209 } else {
210 Pass::call(design, "read_verilog -lib +/xilinx/cells_sim.v");
211 }
212
213 Pass::call(design, "read_verilog -lib +/xilinx/cells_xtra.v");
214
215 if (!nobram) {
216 Pass::call(design, "read_verilog -lib +/xilinx/brams_bb.v");
217 }
218
219 Pass::call(design, stringf("hierarchy -check %s", top_opt.c_str()));
220 }
221
222 if (flatten && check_label(active, run_from, run_to, "flatten"))
223 {
224 Pass::call(design, "proc");
225 Pass::call(design, "flatten");
226 }
227
228 if (check_label(active, run_from, run_to, "coarse"))
229 {
230 Pass::call(design, "synth -run coarse");
231 }
232
233 if (check_label(active, run_from, run_to, "bram"))
234 {
235 if (!nobram) {
236 Pass::call(design, "memory_bram -rules +/xilinx/brams.txt");
237 Pass::call(design, "techmap -map +/xilinx/brams_map.v");
238 }
239 }
240
241 if (check_label(active, run_from, run_to, "dram"))
242 {
243 if (!nodram) {
244 Pass::call(design, "memory_bram -rules +/xilinx/drams.txt");
245 Pass::call(design, "techmap -map +/xilinx/drams_map.v");
246 }
247 }
248
249 if (check_label(active, run_from, run_to, "fine"))
250 {
251 Pass::call(design, "opt -fast -full");
252 Pass::call(design, "memory_map");
253 Pass::call(design, "dffsr2dff");
254 Pass::call(design, "dff2dffe");
255 Pass::call(design, "opt -full");
256
257 if (vpr) {
258 Pass::call(design, "techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v -D _EXPLICIT_CARRY");
259 } else {
260 Pass::call(design, "techmap -map +/techmap.v -map +/xilinx/arith_map.v -map +/xilinx/ff_map.v");
261 }
262
263 Pass::call(design, "hierarchy -check");
264 Pass::call(design, "opt -fast");
265 }
266
267 if (check_label(active, run_from, run_to, "map_luts"))
268 {
269 Pass::call(design, "abc -luts 2:2,3,6:5,10,20" + string(retime ? " -dff" : ""));
270 Pass::call(design, "clean");
271 Pass::call(design, "techmap -map +/xilinx/lut_map.v");
272 }
273
274 if (check_label(active, run_from, run_to, "map_cells"))
275 {
276 Pass::call(design, "techmap -map +/xilinx/cells_map.v");
277 Pass::call(design, "dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT");
278 Pass::call(design, "clean");
279 }
280
281 if (check_label(active, run_from, run_to, "check"))
282 {
283 Pass::call(design, "hierarchy -check");
284 Pass::call(design, "stat");
285 Pass::call(design, "check -noinit");
286 }
287
288 if (check_label(active, run_from, run_to, "edif"))
289 {
290 if (!edif_file.empty())
291 Pass::call(design, stringf("write_edif -pvector bra %s", edif_file.c_str()));
292 }
293 if (check_label(active, run_from, run_to, "blif"))
294 {
295 if (!blif_file.empty())
296 Pass::call(design, stringf("write_blif %s", edif_file.c_str()));
297 }
298
299 log_pop();
300 }
301 } SynthXilinxPass;
302
303 PRIVATE_NAMESPACE_END