Added "synth_ice40 -abc2"
[yosys.git] / techlibs / ice40 / synth_ice40.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 SynthIce40Pass : public Pass {
38 SynthIce40Pass() : Pass("synth_ice40", "synthesis for iCE40 FPGAs") { }
39 virtual void help()
40 {
41 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
42 log("\n");
43 log(" synth_ice40 [options]\n");
44 log("\n");
45 log("This command runs synthesis for iCE40 FPGAs. This work is experimental.\n");
46 log("\n");
47 log(" -top <module>\n");
48 log(" use the specified module as top module (default='top')\n");
49 log("\n");
50 log(" -blif <file>\n");
51 log(" write the design to the specified BLIF file. writing of an output file\n");
52 log(" is omitted if this parameter is not specified.\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(" -run <from_label>:<to_label>\n");
59 log(" only run the commands between the labels (see below). an empty\n");
60 log(" from label is synonymous to 'begin', and empty to label is\n");
61 log(" synonymous to the end of the command list.\n");
62 log("\n");
63 log(" -noflatten\n");
64 log(" do not flatten design before synthesis\n");
65 log("\n");
66 log(" -retime\n");
67 log(" run 'abc' with -dff option\n");
68 log("\n");
69 log(" -nocarry\n");
70 log(" do not use SB_CARRY cells in output netlist\n");
71 log("\n");
72 log(" -nobram\n");
73 log(" do not use SB_RAM40_4K* cells in output netlist\n");
74 log("\n");
75 log(" -abc2\n");
76 log(" run two passes of 'abc' for slightly improved logic density\n");
77 log("\n");
78 log("\n");
79 log("The following commands are executed by this synthesis command:\n");
80 log("\n");
81 log(" begin:\n");
82 log(" read_verilog -lib +/ice40/cells_sim.v\n");
83 log(" hierarchy -check -top <top>\n");
84 log("\n");
85 log(" flatten: (unless -noflatten)\n");
86 log(" proc\n");
87 log(" flatten\n");
88 log(" tribuf -logic\n");
89 log("\n");
90 log(" coarse:\n");
91 log(" synth -run coarse\n");
92 log("\n");
93 log(" bram: (skip if -nobram)\n");
94 log(" memory_bram -rules +/ice40/brams.txt\n");
95 log(" techmap -map +/ice40/brams_map.v\n");
96 log("\n");
97 log(" fine:\n");
98 log(" opt -fast -mux_undef -undriven -fine\n");
99 log(" memory_map\n");
100 log(" opt -undriven -fine\n");
101 log(" techmap -map +/techmap.v [-map +/ice40/arith_map.v]\n");
102 log(" abc -dff (only if -retime)\n");
103 log(" ice40_opt\n");
104 log("\n");
105 log(" map_ffs:\n");
106 log(" dff2dffe -direct-match $_DFF_*\n");
107 log(" techmap -map +/ice40/cells_map.v\n");
108 log(" opt_const -mux_undef\n");
109 log(" simplemap\n");
110 log(" ice40_ffinit\n");
111 log(" ice40_ffssr\n");
112 log(" ice40_opt -full\n");
113 log("\n");
114 log(" map_luts:\n");
115 log(" abc (only if -abc2)\n");
116 log(" abc -lut 4\n");
117 log(" clean\n");
118 log("\n");
119 log(" map_cells:\n");
120 log(" techmap -map +/ice40/cells_map.v\n");
121 log(" clean\n");
122 log("\n");
123 log(" check:\n");
124 log(" hierarchy -check\n");
125 log(" stat\n");
126 log(" check -noinit\n");
127 log("\n");
128 log(" blif:\n");
129 log(" write_blif -gates -attr -param <file-name>\n");
130 log("\n");
131 log(" edif:\n");
132 log(" write_edif <file-name>\n");
133 log("\n");
134 }
135 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
136 {
137 std::string top_opt = "-auto-top";
138 std::string run_from, run_to;
139 std::string blif_file, edif_file;
140 bool nocarry = false;
141 bool nobram = false;
142 bool flatten = true;
143 bool retime = false;
144 bool abc2 = false;
145
146 size_t argidx;
147 for (argidx = 1; argidx < args.size(); argidx++)
148 {
149 if (args[argidx] == "-top" && argidx+1 < args.size()) {
150 top_opt = "-top " + args[++argidx];
151 continue;
152 }
153 if (args[argidx] == "-blif" && argidx+1 < args.size()) {
154 blif_file = 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] == "-run" && argidx+1 < args.size()) {
162 size_t pos = args[argidx+1].find(':');
163 if (pos == std::string::npos)
164 break;
165 run_from = args[++argidx].substr(0, pos);
166 run_to = args[argidx].substr(pos+1);
167 continue;
168 }
169 if (args[argidx] == "-flatten") {
170 flatten = true;
171 continue;
172 }
173 if (args[argidx] == "-noflatten") {
174 flatten = false;
175 continue;
176 }
177 if (args[argidx] == "-retime") {
178 retime = true;
179 continue;
180 }
181 if (args[argidx] == "-nocarry") {
182 nocarry = true;
183 continue;
184 }
185 if (args[argidx] == "-nobram") {
186 nobram = true;
187 continue;
188 }
189 if (args[argidx] == "-abc2") {
190 abc2 = true;
191 continue;
192 }
193 break;
194 }
195 extra_args(args, argidx, design);
196
197 if (!design->full_selection())
198 log_cmd_error("This comannd only operates on fully selected designs!\n");
199
200 bool active = run_from.empty();
201
202 log_header("Executing SYNTH_ICE40 pass.\n");
203 log_push();
204
205 if (check_label(active, run_from, run_to, "begin"))
206 {
207 Pass::call(design, "read_verilog -lib +/ice40/cells_sim.v");
208 Pass::call(design, stringf("hierarchy -check %s", top_opt.c_str()));
209 }
210
211 if (flatten && check_label(active, run_from, run_to, "flatten"))
212 {
213 Pass::call(design, "proc");
214 Pass::call(design, "flatten");
215 Pass::call(design, "tribuf -logic");
216 }
217
218 if (check_label(active, run_from, run_to, "coarse"))
219 {
220 Pass::call(design, "synth -run coarse");
221 }
222
223 if (!nobram && check_label(active, run_from, run_to, "bram"))
224 {
225 Pass::call(design, "memory_bram -rules +/ice40/brams.txt");
226 Pass::call(design, "techmap -map +/ice40/brams_map.v");
227 }
228
229 if (check_label(active, run_from, run_to, "fine"))
230 {
231 Pass::call(design, "opt -fast -mux_undef -undriven -fine");
232 Pass::call(design, "memory_map");
233 Pass::call(design, "opt -undriven -fine");
234 if (nocarry)
235 Pass::call(design, "techmap");
236 else
237 Pass::call(design, "techmap -map +/techmap.v -map +/ice40/arith_map.v");
238 if (retime)
239 Pass::call(design, "abc -dff");
240 Pass::call(design, "ice40_opt");
241 }
242
243 if (check_label(active, run_from, run_to, "map_ffs"))
244 {
245 Pass::call(design, "dff2dffe -direct-match $_DFF_*");
246 Pass::call(design, "techmap -map +/ice40/cells_map.v");
247 Pass::call(design, "opt_const -mux_undef");
248 Pass::call(design, "simplemap");
249 Pass::call(design, "ice40_ffinit");
250 Pass::call(design, "ice40_ffssr");
251 Pass::call(design, "ice40_opt -full");
252 }
253
254 if (check_label(active, run_from, run_to, "map_luts"))
255 {
256 if (abc2)
257 Pass::call(design, "abc");
258 Pass::call(design, "abc -lut 4");
259 Pass::call(design, "clean");
260 }
261
262 if (check_label(active, run_from, run_to, "map_cells"))
263 {
264 Pass::call(design, "techmap -map +/ice40/cells_map.v");
265 Pass::call(design, "clean");
266 }
267
268 if (check_label(active, run_from, run_to, "check"))
269 {
270 Pass::call(design, "hierarchy -check");
271 Pass::call(design, "stat");
272 Pass::call(design, "check -noinit");
273 }
274
275 if (check_label(active, run_from, run_to, "blif"))
276 {
277 if (!blif_file.empty())
278 Pass::call(design, stringf("write_blif -gates -attr -param %s", blif_file.c_str()));
279 }
280
281 if (check_label(active, run_from, run_to, "edif"))
282 {
283 if (!edif_file.empty())
284 Pass::call(design, stringf("write_edif %s", edif_file.c_str()));
285 }
286
287 log_pop();
288 }
289 } SynthIce40Pass;
290
291 PRIVATE_NAMESPACE_END