1 /*
2  *  yosys -- Yosys Open SYnthesis Suite
3  *
4  *  Copyright (C) 2012  Claire Xenia Wolf <claire@yosyshq.com>
5  *  Copyright (C) 2019  Dan Ravensloft <dan.ravensloft@gmail.com>
6  *
7  *  Permission to use, copy, modify, and/or distribute this software for any
8  *  purpose with or without fee is hereby granted, provided that the above
9  *  copyright notice and this permission notice appear in all copies.
10  *
11  *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 #include "kernel/celltypes.h"
22 #include "kernel/log.h"
23 #include "kernel/register.h"
24 #include "kernel/rtlil.h"
25 
26 USING_YOSYS_NAMESPACE
27 PRIVATE_NAMESPACE_BEGIN
28 
29 struct SynthIntelALMPass : public ScriptPass {
SynthIntelALMPassSynthIntelALMPass30 	SynthIntelALMPass() : ScriptPass("synth_intel_alm", "synthesis for ALM-based Intel (Altera) FPGAs.") {}
31 
helpSynthIntelALMPass32 	void help() override
33 	{
34 		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
35 		log("\n");
36 		log("    synth_intel_alm [options]\n");
37 		log("\n");
38 		log("This command runs synthesis for ALM-based Intel FPGAs.\n");
39 		log("\n");
40 		log("    -top <module>\n");
41 		log("        use the specified module as top module\n");
42 		log("\n");
43 		log("    -family <family>\n");
44 		log("        target one of:\n");
45 		log("        \"cyclonev\"    - Cyclone V (default)\n");
46 		log("        \"arriav\"      - Arria V (non-GZ)");
47 		log("        \"cyclone10gx\" - Cyclone 10GX\n");
48 		log("\n");
49 		log("    -vqm <file>\n");
50 		log("        write the design to the specified Verilog Quartus Mapping File. Writing of an\n");
51 		log("        output file is omitted if this parameter is not specified. Implies -quartus.\n");
52 		log("\n");
53 		log("    -noflatten\n");
54 		log("        do not flatten design before synthesis; useful for per-module area statistics\n");
55 		log("\n");
56 		log("    -quartus\n");
57 		log("        output a netlist using Quartus cells instead of MISTRAL_* cells\n");
58 		log("\n");
59 		log("    -dff\n");
60 		log("        pass DFFs to ABC to perform sequential logic optimisations (EXPERIMENTAL)\n");
61 		log("\n");
62 		log("    -run <from_label>:<to_label>\n");
63 		log("        only run the commands between the labels (see below). an empty\n");
64 		log("        from label is synonymous to 'begin', and empty to label is\n");
65 		log("        synonymous to the end of the command list.\n");
66 		log("\n");
67 		log("    -nolutram\n");
68 		log("        do not use LUT RAM cells in output netlist\n");
69 		log("\n");
70 		log("    -nobram\n");
71 		log("        do not use block RAM cells in output netlist\n");
72 		log("\n");
73 		log("    -nodsp\n");
74 		log("        do not map multipliers to MISTRAL_MUL cells\n");
75 		log("\n");
76 		log("    -noiopad\n");
77 		log("        do not instantiate IO buffers\n");
78 		log("\n");
79 		log("    -noclkbuf\n");
80 		log("        do not insert global clock buffers\n");
81 		log("\n");
82 		log("The following commands are executed by this synthesis command:\n");
83 		help_script();
84 		log("\n");
85 	}
86 
87 	string top_opt, family_opt, bram_type, vout_file;
88 	bool flatten, quartus, nolutram, nobram, dff, nodsp, noiopad, noclkbuf;
89 
clear_flagsSynthIntelALMPass90 	void clear_flags() override
91 	{
92 		top_opt = "-auto-top";
93 		family_opt = "cyclonev";
94 		bram_type = "m10k";
95 		vout_file = "";
96 		flatten = true;
97 		quartus = false;
98 		nolutram = false;
99 		nobram = false;
100 		dff = false;
101 		nodsp = false;
102 		noiopad = false;
103 		noclkbuf = false;
104 	}
105 
executeSynthIntelALMPass106 	void execute(std::vector<std::string> args, RTLIL::Design *design) override
107 	{
108 		string run_from, run_to;
109 		clear_flags();
110 
111 		size_t argidx;
112 		for (argidx = 1; argidx < args.size(); argidx++) {
113 			if (args[argidx] == "-family" && argidx + 1 < args.size()) {
114 				family_opt = args[++argidx];
115 				continue;
116 			}
117 			if (args[argidx] == "-top" && argidx + 1 < args.size()) {
118 				top_opt = "-top " + args[++argidx];
119 				continue;
120 			}
121 			if (args[argidx] == "-vqm" && argidx + 1 < args.size()) {
122 				quartus = true;
123 				vout_file = args[++argidx];
124 				continue;
125 			}
126 			if (args[argidx] == "-run" && argidx + 1 < args.size()) {
127 				size_t pos = args[argidx + 1].find(':');
128 				if (pos == std::string::npos)
129 					break;
130 				run_from = args[++argidx].substr(0, pos);
131 				run_to = args[argidx].substr(pos + 1);
132 				continue;
133 			}
134 			if (args[argidx] == "-quartus") {
135 				quartus = true;
136 				continue;
137 			}
138 			if (args[argidx] == "-nolutram") {
139 				nolutram = true;
140 				continue;
141 			}
142 			if (args[argidx] == "-nobram") {
143 				nobram = true;
144 				continue;
145 			}
146 			if (args[argidx] == "-nodsp") {
147 				nodsp = true;
148 				continue;
149 			}
150 			if (args[argidx] == "-noflatten") {
151 				flatten = false;
152 				continue;
153 			}
154 			if (args[argidx] == "-dff") {
155 				dff = true;
156 				continue;
157 			}
158 			if (args[argidx] == "-noiopad") {
159 				noiopad = true;
160 				continue;
161 			}
162 			if (args[argidx] == "-noclkbuf") {
163 				noclkbuf = true;
164 				continue;
165 			}
166 			break;
167 		}
168 		extra_args(args, argidx, design);
169 
170 		if (!design->full_selection())
171 			log_cmd_error("This command only operates on fully selected designs!\n");
172 
173 		if (family_opt == "cyclonev" || family_opt == "arriav") {
174 			bram_type = "m10k";
175 		} else if (family_opt == "cyclone10gx") {
176 			bram_type = "m20k";
177 		} else if (family_opt == "arriva") {
178 			// I have typoed "arriav" as "arriva" (a local bus company)
179 			// so many times I thought it would be funny to have an easter egg.
180 			log_cmd_error("synth_intel_alm cannot synthesize for bus companies. (did you mean '-family arriav'?)\n");
181 		} else {
182 			log_cmd_error("Invalid family specified: '%s'\n", family_opt.c_str());
183 		}
184 
185 		log_header(design, "Executing SYNTH_INTEL_ALM pass.\n");
186 		log_push();
187 
188 		run_script(design, run_from, run_to);
189 
190 		log_pop();
191 	}
192 
scriptSynthIntelALMPass193 	void script() override
194 	{
195 		if (help_mode) {
196 			family_opt = "<family>";
197 			bram_type = "<bram_type>";
198 		}
199 
200 		if (check_label("begin")) {
201 			if (family_opt == "cyclonev")
202 				run(stringf("read_verilog -sv -lib +/intel_alm/%s/cells_sim.v", family_opt.c_str()));
203 			run(stringf("read_verilog -specify -lib -D %s +/intel_alm/common/alm_sim.v", family_opt.c_str()));
204 			run(stringf("read_verilog -specify -lib -D %s +/intel_alm/common/dff_sim.v", family_opt.c_str()));
205 			run(stringf("read_verilog -specify -lib -D %s +/intel_alm/common/dsp_sim.v", family_opt.c_str()));
206 			run(stringf("read_verilog -specify -lib -D %s +/intel_alm/common/mem_sim.v", family_opt.c_str()));
207 			run(stringf("read_verilog -specify -lib -D %s +/intel_alm/common/misc_sim.v", family_opt.c_str()));
208 			run(stringf("read_verilog -specify -lib -D %s -icells +/intel_alm/common/abc9_model.v", family_opt.c_str()));
209 			// Misc and common cells
210 			run("read_verilog -lib +/intel/common/altpll_bb.v");
211 			run("read_verilog -lib +/intel_alm/common/megafunction_bb.v");
212 			run(stringf("hierarchy -check %s", help_mode ? "-top <top>" : top_opt.c_str()));
213 		}
214 
215 		if (check_label("coarse")) {
216 			run("proc");
217 			if (flatten || help_mode)
218 				run("flatten", "(skip if -noflatten)");
219 			run("tribuf -logic");
220 			run("deminout");
221 			run("opt_expr");
222 			run("opt_clean");
223 			run("check");
224 			run("opt -nodffe -nosdff");
225 			run("fsm");
226 			run("opt");
227 			run("wreduce");
228 			run("peepopt");
229 			run("opt_clean");
230 			run("share");
231 			run("techmap -map +/cmp2lut.v -D LUT_WIDTH=6");
232 			run("opt_expr");
233 			run("opt_clean");
234 			if (help_mode) {
235 				run("techmap -map +/mul2dsp.v [...]", "(unless -nodsp)");
236 			} else if (!nodsp) {
237 				// Cyclone V/Arria V supports 9x9 multiplication, Cyclone 10 GX does not.
238 				run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=27 -D DSP_B_MAXWIDTH=27  -D DSP_A_MINWIDTH=19 -D DSP_B_MINWIDTH=4 -D DSP_NAME=__MUL27X27");
239 				run("chtype -set $mul t:$__soft_mul");
240 				run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=27 -D DSP_B_MAXWIDTH=27  -D DSP_A_MINWIDTH=4 -D DSP_B_MINWIDTH=19 -D DSP_NAME=__MUL27X27");
241 				run("chtype -set $mul t:$__soft_mul");
242 				if (family_opt == "cyclonev" || family_opt == "arriav") {
243 					run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=18 -D DSP_B_MAXWIDTH=18  -D DSP_A_MINWIDTH=10 -D DSP_B_MINWIDTH=4 -D DSP_NAME=__MUL18X18");
244 					run("chtype -set $mul t:$__soft_mul");
245 					run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=18 -D DSP_B_MAXWIDTH=18  -D DSP_A_MINWIDTH=4 -D DSP_B_MINWIDTH=10 -D DSP_NAME=__MUL18X18");
246 					run("chtype -set $mul t:$__soft_mul");
247 					run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=9 -D DSP_B_MAXWIDTH=9  -D DSP_A_MINWIDTH=4 -D DSP_B_MINWIDTH=4 -D DSP_NAME=__MUL9X9");
248 					run("chtype -set $mul t:$__soft_mul");
249 				} else if (family_opt == "cyclone10gx") {
250 					run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=18 -D DSP_B_MAXWIDTH=18  -D DSP_A_MINWIDTH=4 -D DSP_B_MINWIDTH=4 -D DSP_NAME=__MUL18X18");
251 					run("chtype -set $mul t:$__soft_mul");
252 				}
253 			}
254 			run("alumacc");
255 			if (!noiopad)
256 				run("iopadmap -bits -outpad MISTRAL_OB I:PAD -inpad MISTRAL_IB O:PAD -toutpad MISTRAL_IO OE:O:PAD -tinoutpad MISTRAL_IO OE:O:I:PAD A:top", "(unless -noiopad)");
257 			run("techmap -map +/intel_alm/common/arith_alm_map.v -map +/intel_alm/common/dsp_map.v");
258 			run("opt");
259 			run("memory -nomap");
260 			run("opt_clean");
261 		}
262 
263 		if (!nobram && check_label("map_bram", "(skip if -nobram)")) {
264 			run(stringf("memory_bram -rules +/intel_alm/common/bram_%s.txt", bram_type.c_str()));
265 			if (help_mode || bram_type != "m10k")
266 				run(stringf("techmap -map +/intel_alm/common/bram_%s_map.v", bram_type.c_str()));
267 		}
268 
269 		if (!nolutram && check_label("map_lutram", "(skip if -nolutram)")) {
270 			run("memory_bram -rules +/intel_alm/common/lutram_mlab.txt", "(for Cyclone V / Cyclone 10GX)");
271 		}
272 
273 		if (check_label("map_ffram")) {
274 			run("memory_map");
275 			run("opt -full");
276 		}
277 
278 		if (check_label("map_ffs")) {
279 			run("techmap");
280 			run("dfflegalize -cell $_DFFE_PN0P_ 0 -cell $_SDFFCE_PP0P_ 0");
281 			run("techmap -map +/intel_alm/common/dff_map.v");
282 			run("opt -full -undriven -mux_undef");
283 			run("clean -purge");
284 			if (!noclkbuf)
285 				run("clkbufmap -buf MISTRAL_CLKBUF Q:A", "(unless -noclkbuf)");
286 		}
287 
288 		if (check_label("map_luts")) {
289 			run("techmap -map +/intel_alm/common/abc9_map.v");
290 			run(stringf("abc9 %s -maxlut 6 -W 600", help_mode ? "[-dff]" : dff ? "-dff" : ""));
291 			run("techmap -map +/intel_alm/common/abc9_unmap.v");
292 			run("techmap -map +/intel_alm/common/alm_map.v");
293 			run("opt -fast");
294 			run("autoname");
295 			run("clean");
296 		}
297 
298 		if (check_label("check")) {
299 			run("hierarchy -check");
300 			run("stat");
301 			run("check");
302 			run("blackbox =A:whitebox");
303 		}
304 
305 		if (check_label("quartus")) {
306 			if (quartus || help_mode) {
307 				// Quartus ICEs if you have a wire which has `[]` in its name,
308 				// which Yosys produces when building memories out of flops.
309 				run("rename -hide w:*[* w:*]*");
310 				// VQM mode does not support 'x, so replace those with zero.
311 				run("setundef -zero");
312 				// VQM mode does not support multi-bit constant assignments
313 				// (e.g. 2'b00 is an error), so as a workaround use references
314 				// to constant driver cells, which Quartus accepts.
315 				run("hilomap -singleton -hicell __MISTRAL_VCC Q -locell __MISTRAL_GND Q");
316 				// Rename from Yosys-internal MISTRAL_* cells to Quartus cells.
317 				run(stringf("techmap -D %s -map +/intel_alm/common/quartus_rename.v", family_opt.c_str()));
318 			}
319 		}
320 
321 		if (check_label("vqm")) {
322 			if (!vout_file.empty() || help_mode) {
323 				run(stringf("write_verilog -attr2comment -defparam -nohex -decimal %s", help_mode ? "<file-name>" : vout_file.c_str()));
324 			}
325 		}
326 	}
327 } SynthIntelALMPass;
328 
329 PRIVATE_NAMESPACE_END
330