Merge pull request #521 from azonenberg/for_clifford
[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 SynthXilinxPass() : Pass("synth_xilinx", "synthesis for Xilinx FPGAs") { }
39 virtual void help()
40 {
41 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
42 log("\n");
43 log(" synth_xilinx [options]\n");
44 log("\n");
45 log("This command runs synthesis for Xilinx FPGAs. This command does not operate on\n");
46 log("partly selected designs. At the moment this command creates netlists that are\n");
47 log("compatible with 7-Series Xilinx devices.\n");
48 log("\n");
49 log(" -top <module>\n");
50 log(" use the specified module as top module\n");
51 log("\n");
52 log(" -edif <file>\n");
53 log(" write the design to the specified edif 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(" -flatten\n");
62 log(" 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("\n");
68 log("The following commands are executed by this synthesis command:\n");
69 log("\n");
70 log(" begin:\n");
71 log(" read_verilog -lib +/xilinx/cells_sim.v\n");
72 log(" read_verilog -lib +/xilinx/cells_xtra.v\n");
73 log(" read_verilog -lib +/xilinx/brams_bb.v\n");
74 log(" hierarchy -check -top <top>\n");
75 log("\n");
76 log(" flatten: (only if -flatten)\n");
77 log(" proc\n");
78 log(" flatten\n");
79 log("\n");
80 log(" coarse:\n");
81 log(" synth -run coarse\n");
82 log("\n");
83 log(" bram:\n");
84 log(" memory_bram -rules +/xilinx/brams.txt\n");
85 log(" techmap -map +/xilinx/brams_map.v\n");
86 log("\n");
87 log(" dram:\n");
88 log(" memory_bram -rules +/xilinx/drams.txt\n");
89 log(" techmap -map +/xilinx/drams_map.v\n");
90 log("\n");
91 log(" fine:\n");
92 log(" opt -fast -full\n");
93 log(" memory_map\n");
94 log(" dffsr2dff\n");
95 log(" dff2dffe\n");
96 log(" opt -full\n");
97 log(" techmap -map +/techmap.v -map +/xilinx/arith_map.v\n");
98 log(" opt -fast\n");
99 log("\n");
100 log(" map_luts:\n");
101 log(" abc -luts 2:2,3,6:5,10,20 [-dff]\n");
102 log(" clean\n");
103 log("\n");
104 log(" map_cells:\n");
105 log(" techmap -map +/xilinx/cells_map.v\n");
106 log(" dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT\n");
107 log(" clean\n");
108 log("\n");
109 log(" check:\n");
110 log(" hierarchy -check\n");
111 log(" stat\n");
112 log(" check -noinit\n");
113 log("\n");
114 log(" edif: (only if -edif)\n");
115 log(" write_edif <file-name>\n");
116 log("\n");
117 }
118 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
119 {
120 std::string top_opt = "-auto-top";
121 std::string edif_file;
122 std::string run_from, run_to;
123 bool flatten = false;
124 bool retime = false;
125
126 size_t argidx;
127 for (argidx = 1; argidx < args.size(); argidx++)
128 {
129 if (args[argidx] == "-top" && argidx+1 < args.size()) {
130 top_opt = "-top " + args[++argidx];
131 continue;
132 }
133 if (args[argidx] == "-edif" && argidx+1 < args.size()) {
134 edif_file = args[++argidx];
135 continue;
136 }
137 if (args[argidx] == "-run" && argidx+1 < args.size()) {
138 size_t pos = args[argidx+1].find(':');
139 if (pos == std::string::npos)
140 break;
141 run_from = args[++argidx].substr(0, pos);
142 run_to = args[argidx].substr(pos+1);
143 continue;
144 }
145 if (args[argidx] == "-flatten") {
146 flatten = true;
147 continue;
148 }
149 if (args[argidx] == "-retime") {
150 retime = true;
151 continue;
152 }
153 break;
154 }
155 extra_args(args, argidx, design);
156
157 if (!design->full_selection())
158 log_cmd_error("This comannd only operates on fully selected designs!\n");
159
160 bool active = run_from.empty();
161
162 log_header(design, "Executing SYNTH_XILINX pass.\n");
163 log_push();
164
165 if (check_label(active, run_from, run_to, "begin"))
166 {
167 Pass::call(design, "read_verilog -lib +/xilinx/cells_sim.v");
168 Pass::call(design, "read_verilog -lib +/xilinx/cells_xtra.v");
169 Pass::call(design, "read_verilog -lib +/xilinx/brams_bb.v");
170 Pass::call(design, stringf("hierarchy -check %s", top_opt.c_str()));
171 }
172
173 if (flatten && check_label(active, run_from, run_to, "flatten"))
174 {
175 Pass::call(design, "proc");
176 Pass::call(design, "flatten");
177 }
178
179 if (check_label(active, run_from, run_to, "coarse"))
180 {
181 Pass::call(design, "synth -run coarse");
182 }
183
184 if (check_label(active, run_from, run_to, "bram"))
185 {
186 Pass::call(design, "memory_bram -rules +/xilinx/brams.txt");
187 Pass::call(design, "techmap -map +/xilinx/brams_map.v");
188 }
189
190 if (check_label(active, run_from, run_to, "dram"))
191 {
192 Pass::call(design, "memory_bram -rules +/xilinx/drams.txt");
193 Pass::call(design, "techmap -map +/xilinx/drams_map.v");
194 }
195
196 if (check_label(active, run_from, run_to, "fine"))
197 {
198 Pass::call(design, "opt -fast -full");
199 Pass::call(design, "memory_map");
200 Pass::call(design, "dffsr2dff");
201 Pass::call(design, "dff2dffe");
202 Pass::call(design, "opt -full");
203 Pass::call(design, "techmap -map +/techmap.v -map +/xilinx/arith_map.v");
204 Pass::call(design, "opt -fast");
205 }
206
207 if (check_label(active, run_from, run_to, "map_luts"))
208 {
209 Pass::call(design, "abc -luts 2:2,3,6:5,10,20" + string(retime ? " -dff" : ""));
210 Pass::call(design, "clean");
211 }
212
213 if (check_label(active, run_from, run_to, "map_cells"))
214 {
215 Pass::call(design, "techmap -map +/xilinx/cells_map.v");
216 Pass::call(design, "dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT");
217 Pass::call(design, "clean");
218 }
219
220 if (check_label(active, run_from, run_to, "check"))
221 {
222 Pass::call(design, "hierarchy -check");
223 Pass::call(design, "stat");
224 Pass::call(design, "check -noinit");
225 }
226
227 if (check_label(active, run_from, run_to, "edif"))
228 {
229 if (!edif_file.empty())
230 Pass::call(design, stringf("write_edif %s", edif_file.c_str()));
231 }
232
233 log_pop();
234 }
235 } SynthXilinxPass;
236
237 PRIVATE_NAMESPACE_END