ice40_opt bugfix
[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(" -run <from_label>:<to_label>\n");
51 log(" only run the commands between the labels (see below). an empty\n");
52 log(" from label is synonymous to 'begin', and empty to label is\n");
53 log(" synonymous to the end of the command list.\n");
54 log("\n");
55 log(" -flatten\n");
56 log(" flatten design before synthesis\n");
57 log("\n");
58 log(" -retime\n");
59 log(" run 'abc' with -dff option\n");
60 log("\n");
61 log(" -nocarry\n");
62 log(" do not use SB_CARRY cells in output netlist\n");
63 log("\n");
64 log(" -nobram\n");
65 log(" do not use SB_RAM40_4K* cells in output netlist\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 +/ice40/cells_sim.v\n");
72 log(" hierarchy -check -top <top>\n");
73 log("\n");
74 log(" flatten: (only if -flatten)\n");
75 log(" proc\n");
76 log(" flatten\n");
77 log("\n");
78 log(" coarse:\n");
79 log(" synth -run coarse\n");
80 log("\n");
81 log(" bram: (skip if -nobram)\n");
82 log(" memory_bram -rules +/ice40/brams.txt\n");
83 log(" techmap -map +/ice40/brams_map.v\n");
84 log("\n");
85 log(" fine:\n");
86 log(" opt -fast -mux_undef -undriven -fine\n");
87 log(" memory_map\n");
88 log(" opt -undriven -fine\n");
89 log(" techmap -map +/techmap.v [-map +/ice40/arith_map.v]\n");
90 log(" abc -dff (only if -retime)\n");
91 log(" ice40_opt\n");
92 log("\n");
93 log(" map_ffs:\n");
94 log(" dff2dffe -direct-match $_DFF_*\n");
95 log(" techmap -map +/ice40/cells_map.v\n");
96 log(" opt_const -mux_undef\n");
97 log(" simplemap\n");
98 log(" ice40_ffssr\n");
99 log(" ice40_opt -full\n");
100 log("\n");
101 log(" map_luts:\n");
102 log(" abc -lut 4\n");
103 log(" clean\n");
104 log("\n");
105 log(" map_cells:\n");
106 log(" techmap -map +/ice40/cells_map.v\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 }
115 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
116 {
117 std::string top_opt = "-auto-top";
118 std::string run_from, run_to;
119 bool nocarry = false;
120 bool nobram = false;
121 bool flatten = false;
122 bool retime = false;
123
124 size_t argidx;
125 for (argidx = 1; argidx < args.size(); argidx++)
126 {
127 if (args[argidx] == "-top" && argidx+1 < args.size()) {
128 top_opt = "-top " + args[++argidx];
129 continue;
130 }
131 if (args[argidx] == "-run" && argidx+1 < args.size()) {
132 size_t pos = args[argidx+1].find(':');
133 if (pos == std::string::npos)
134 break;
135 run_from = args[++argidx].substr(0, pos);
136 run_to = args[argidx].substr(pos+1);
137 continue;
138 }
139 if (args[argidx] == "-flatten") {
140 flatten = true;
141 continue;
142 }
143 if (args[argidx] == "-retime") {
144 retime = true;
145 continue;
146 }
147 if (args[argidx] == "-nocarry") {
148 nocarry = true;
149 continue;
150 }
151 if (args[argidx] == "-nobram") {
152 nobram = true;
153 continue;
154 }
155 break;
156 }
157 extra_args(args, argidx, design);
158
159 if (!design->full_selection())
160 log_cmd_error("This comannd only operates on fully selected designs!\n");
161
162 bool active = run_from.empty();
163
164 log_header("Executing SYNTH_ICE40 pass.\n");
165 log_push();
166
167 if (check_label(active, run_from, run_to, "begin"))
168 {
169 Pass::call(design, "read_verilog -lib +/ice40/cells_sim.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 (!nobram && check_label(active, run_from, run_to, "bram"))
185 {
186 Pass::call(design, "memory_bram -rules +/ice40/brams.txt");
187 Pass::call(design, "techmap -map +/ice40/brams_map.v");
188 }
189
190 if (check_label(active, run_from, run_to, "fine"))
191 {
192 Pass::call(design, "opt -fast -mux_undef -undriven -fine");
193 Pass::call(design, "memory_map");
194 Pass::call(design, "opt -undriven -fine");
195 if (nocarry)
196 Pass::call(design, "techmap");
197 else
198 Pass::call(design, "techmap -map +/techmap.v -map +/ice40/arith_map.v");
199 if (retime)
200 Pass::call(design, "abc -dff");
201 Pass::call(design, "ice40_opt");
202 }
203
204 if (check_label(active, run_from, run_to, "map_ffs"))
205 {
206 Pass::call(design, "dff2dffe -direct-match $_DFF_*");
207 Pass::call(design, "techmap -map +/ice40/cells_map.v");
208 Pass::call(design, "opt_const -mux_undef");
209 Pass::call(design, "simplemap");
210 Pass::call(design, "ice40_ffssr");
211 Pass::call(design, "ice40_opt -full");
212 }
213
214 if (check_label(active, run_from, run_to, "map_luts"))
215 {
216 Pass::call(design, "abc -lut 4");
217 Pass::call(design, "clean");
218 }
219
220 if (check_label(active, run_from, run_to, "map_cells"))
221 {
222 Pass::call(design, "techmap -map +/ice40/cells_map.v");
223 Pass::call(design, "clean");
224 }
225
226 if (check_label(active, run_from, run_to, "check"))
227 {
228 Pass::call(design, "hierarchy -check");
229 Pass::call(design, "stat");
230 Pass::call(design, "check -noinit");
231 }
232
233 log_pop();
234 }
235 } SynthIce40Pass;
236
237 PRIVATE_NAMESPACE_END