2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 * (C) 2019 Eddie Hung <eddie@fpgeh.com>
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.
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.
21 #include "kernel/register.h"
22 #include "kernel/celltypes.h"
23 #include "kernel/rtlil.h"
24 #include "kernel/log.h"
27 PRIVATE_NAMESPACE_BEGIN
29 #define XC7_WIRE_DELAY 300 // Number with which ABC will map a 6-input gate
30 // to one LUT6 (instead of a LUT5 + LUT2)
32 struct SynthXilinxPass
: public ScriptPass
34 SynthXilinxPass() : ScriptPass("synth_xilinx", "synthesis for Xilinx FPGAs") { }
36 void help() YS_OVERRIDE
38 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
40 log(" synth_xilinx [options]\n");
42 log("This command runs synthesis for Xilinx FPGAs. This command does not operate on\n");
43 log("partly selected designs. At the moment this command creates netlists that are\n");
44 log("compatible with 7-Series Xilinx devices.\n");
46 log(" -top <module>\n");
47 log(" use the specified module as top module\n");
49 log(" -family {xcup|xcu|xc7|xc6s}\n");
50 log(" run synthesis for the specified Xilinx architecture\n");
51 log(" generate the synthesis netlist for the specified family.\n");
52 log(" default: xc7\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");
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");
63 log(" generate an output netlist (and BLIF file) suitable for VPR\n");
64 log(" (this feature is experimental and incomplete)\n");
67 log(" disable inference of block rams\n");
70 log(" disable inference of distributed rams\n");
73 log(" disable inference of shift registers\n");
76 log(" do not use XORCY/MUXCY/CARRY4 cells in output netlist\n");
79 log(" do not use MUXF[78] resources to implement LUTs larger than LUT6s\n");
82 log(" do not use DSP48E1s to implement multipliers and associated logic\n");
84 log(" -widemux <int>\n");
85 log(" enable inference of hard multiplexer resources (MUXF[78]) for muxes at or\n");
86 log(" above this number of inputs (minimum value 2, recommended value >= 5).\n");
87 log(" default: 0 (no inference)\n");
89 log(" -run <from_label>:<to_label>\n");
90 log(" only run the commands between the labels (see below). an empty\n");
91 log(" from label is synonymous to 'begin', and empty to label is\n");
92 log(" synonymous to the end of the command list.\n");
95 log(" flatten design before synthesis\n");
98 log(" run 'abc' with -dff option\n");
101 log(" use new ABC9 flow (EXPERIMENTAL)\n");
104 log("The following commands are executed by this synthesis command:\n");
109 std::string top_opt
, edif_file
, blif_file
, family
;
110 bool flatten
, retime
, vpr
, nobram
, nodram
, nosrl
, nocarry
, nowidelut
, nodsp
, abc9
;
113 void clear_flags() YS_OVERRIDE
115 top_opt
= "-auto-top";
133 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
135 std::string run_from
, run_to
;
139 for (argidx
= 1; argidx
< args
.size(); argidx
++)
141 if (args
[argidx
] == "-top" && argidx
+1 < args
.size()) {
142 top_opt
= "-top " + args
[++argidx
];
145 if ((args
[argidx
] == "-family" || args
[argidx
] == "-arch") && argidx
+1 < args
.size()) {
146 family
= args
[++argidx
];
149 if (args
[argidx
] == "-edif" && argidx
+1 < args
.size()) {
150 edif_file
= args
[++argidx
];
153 if (args
[argidx
] == "-blif" && argidx
+1 < args
.size()) {
154 blif_file
= args
[++argidx
];
157 if (args
[argidx
] == "-run" && argidx
+1 < args
.size()) {
158 size_t pos
= args
[argidx
+1].find(':');
159 if (pos
== std::string::npos
)
161 run_from
= args
[++argidx
].substr(0, pos
);
162 run_to
= args
[argidx
].substr(pos
+1);
165 if (args
[argidx
] == "-flatten") {
169 if (args
[argidx
] == "-retime") {
173 if (args
[argidx
] == "-nocarry") {
177 if (args
[argidx
] == "-nowidelut") {
181 if (args
[argidx
] == "-vpr") {
185 if (args
[argidx
] == "-nocarry") {
189 if (args
[argidx
] == "-nobram") {
193 if (args
[argidx
] == "-nodram") {
197 if (args
[argidx
] == "-nosrl") {
201 if (args
[argidx
] == "-widemux" && argidx
+1 < args
.size()) {
202 widemux
= std::stoi(args
[++argidx
]);
205 if (args
[argidx
] == "-abc9") {
209 if (args
[argidx
] == "-nodsp") {
215 extra_args(args
, argidx
, design
);
217 if (family
!= "xcup" && family
!= "xcu" && family
!= "xc7" && family
!= "xc6s")
218 log_cmd_error("Invalid Xilinx -family setting: '%s'.\n", family
.c_str());
220 if (widemux
!= 0 && widemux
< 2)
221 log_cmd_error("-widemux value must be 0 or >= 2.\n");
223 if (!design
->full_selection())
224 log_cmd_error("This command only operates on fully selected designs!\n");
227 log_cmd_error("-retime option not currently compatible with -abc9!\n");
229 log_header(design
, "Executing SYNTH_XILINX pass.\n");
232 run_script(design
, run_from
, run_to
);
237 void script() YS_OVERRIDE
239 if (check_label("begin")) {
241 run("read_verilog -lib -icells -D _ABC -D_EXPLICIT_CARRY +/xilinx/cells_sim.v");
243 run("read_verilog -lib -icells -D _ABC +/xilinx/cells_sim.v");
245 run("read_verilog -lib +/xilinx/cells_xtra.v");
248 run("read_verilog -lib +/xilinx/{family}_brams_bb.v");
249 } else if (family
== "xc6s") {
250 run("read_verilog -lib +/xilinx/xc6s_brams_bb.v");
251 } else if (family
== "xc7") {
252 run("read_verilog -lib +/xilinx/xc7_brams_bb.v");
255 run(stringf("hierarchy -check %s", top_opt
.c_str()));
258 if (check_label("coarse")) {
260 if (flatten
|| help_mode
)
261 run("flatten", "(with '-flatten')");
267 run("wreduce [-keepdc]", "(option for '-widemux')");
269 run("wreduce" + std::string(widemux
> 0 ? " -keepdc" : ""));
273 if (widemux
> 0 || help_mode
)
274 run("muxpack", " ('-widemux' only)");
276 // shregmap -tech xilinx can cope with $shiftx and $mux
277 // cells for identifying variable-length shift registers,
278 // so attempt to convert $pmux-es to the former
279 // Also: wide multiplexer inference benefits from this too
280 if (!(nosrl
&& widemux
== 0) || help_mode
) {
281 run("pmux2shiftx", "(skip if '-nosrl' and '-widemux=0')");
282 run("clean", " (skip if '-nosrl' and '-widemux=0')");
285 run("techmap -map +/cmp2lut.v -D LUT_WIDTH=6");
287 // The actual behaviour of the Xilinx DSP is a signed 25x18 multiply
288 // Due to current limitations of mul2dsp, we are actually mapping as a 24x17
289 // unsigned multiply with MSBs set to 1'b0
291 if (!nodsp
|| help_mode
)
292 run("techmap -map +/mul2dsp.v -D DSP_A_MAXWIDTH=25 -D DSP_A_SIGNEDONLY=1 -D DSP_B_MAXWIDTH=18 -D DSP_B_SIGNEDONLY=1 -D DSP_NAME=$__MUL25X18");
299 run("memory -nomap");
303 if (check_label("bram", "(skip if '-nobram')")) {
305 run("memory_bram -rules +/xilinx/{family}_brams.txt");
306 run("techmap -map +/xilinx/{family}_brams_map.v");
307 } else if (!nobram
) {
308 if (family
== "xc6s") {
309 run("memory_bram -rules +/xilinx/xc6s_brams.txt");
310 run("techmap -map +/xilinx/xc6s_brams_map.v");
311 } else if (family
== "xc7") {
312 run("memory_bram -rules +/xilinx/xc7_brams.txt");
313 run("techmap -map +/xilinx/xc7_brams_map.v");
315 log_warning("Block RAM inference not yet supported for family %s.\n", family
.c_str());
320 if (check_label("dram", "(skip if '-nodram')")) {
321 if (!nodram
|| help_mode
) {
322 run("memory_bram -rules +/xilinx/drams.txt");
323 run("techmap -map +/xilinx/drams_map.v");
327 if (check_label("fine")) {
329 run("opt -fast -mux_bool -undriven -fine"); // Necessary to omit -mux_undef otherwise muxcover
330 // performs less efficiently
332 run("opt -fast -full");
336 if (help_mode
|| !nodsp
) {
337 run("techmap -map +/xilinx/dsp_map.v", "(skip if '-nodsp')");
338 run("xilinx_dsp", " (skip if '-nodsp')");
341 run("simplemap t:$mux", " ('-widemux' only)");
342 run("muxcover <internal options>, ('-widemux' only)");
344 else if (widemux
> 0) {
345 run("simplemap t:$mux");
346 constexpr int cost_mux2
= 100;
347 std::string muxcover_args
= stringf(" -nodecode -mux2=%d", cost_mux2
);
349 case 2: muxcover_args
+= stringf(" -mux4=%d -mux8=%d -mux16=%d", cost_mux2
+1, cost_mux2
+2, cost_mux2
+3); break;
351 case 4: muxcover_args
+= stringf(" -mux4=%d -mux8=%d -mux16=%d", cost_mux2
*(widemux
-1)-2, cost_mux2
*(widemux
-1)-1, cost_mux2
*(widemux
-1)); break;
355 case 8: muxcover_args
+= stringf(" -mux8=%d -mux16=%d", cost_mux2
*(widemux
-1)-1, cost_mux2
*(widemux
-1)); break;
363 default: muxcover_args
+= stringf(" -mux16=%d", cost_mux2
*(widemux
-1)-1); break;
365 run("muxcover " + muxcover_args
);
369 if (!nosrl
|| help_mode
) {
370 // shregmap operates on bit-level flops, not word-level,
371 // so break those down here
372 run("simplemap t:$dff t:$dffe", " (skip if '-nosrl')");
373 // shregmap with '-tech xilinx' infers variable length shift regs
374 run("shregmap -tech xilinx -minlen 3", "(skip if '-nosrl')");
377 std::string techmap_args
= " -map +/techmap.v";
379 techmap_args
+= " [-map +/xilinx/mux_map.v]";
380 else if (widemux
> 0)
381 techmap_args
+= stringf(" -D MIN_MUX_INPUTS=%d -map +/xilinx/mux_map.v", widemux
);
383 techmap_args
+= " [-map +/xilinx/arith_map.v]";
385 techmap_args
+= " -map +/xilinx/arith_map.v";
387 techmap_args
+= " -D _EXPLICIT_CARRY";
389 techmap_args
+= " -D _CLB_CARRY";
391 run("techmap " + techmap_args
);
395 if (check_label("map_cells")) {
396 std::string techmap_args
= "-map +/techmap.v -D _ABC -map +/xilinx/cells_map.v";
398 techmap_args
+= stringf(" -D MIN_MUX_INPUTS=%d", widemux
);
399 run("techmap " + techmap_args
);
403 if (check_label("map_luts")) {
404 run("opt_expr -mux_undef");
406 run("abc -luts 2:2,3,6:5[,10,20] [-dff]", "(option for 'nowidelut', option for '-retime')");
409 log_warning("'synth_xilinx -abc9' currently supports '-family xc7' only.\n");
411 run("abc9 -lut +/xilinx/abc_xc7_nowide.lut -box +/xilinx/abc_xc7.box -W " + std::to_string(XC7_WIRE_DELAY
));
413 run("abc9 -lut +/xilinx/abc_xc7.lut -box +/xilinx/abc_xc7.box -W " + std::to_string(XC7_WIRE_DELAY
));
417 run("abc -luts 2:2,3,6:5" + string(retime
? " -dff" : ""));
419 run("abc -luts 2:2,3,6:5,10,20" + string(retime
? " -dff" : ""));
423 // This shregmap call infers fixed length shift registers after abc
424 // has performed any necessary retiming
425 if (!nosrl
|| help_mode
)
426 run("shregmap -minlen 3 -init -params -enpol any_or_none", "(skip if '-nosrl')");
427 run("techmap -map +/xilinx/lut_map.v -map +/xilinx/ff_map.v -map +/xilinx/cells_map.v");
428 run("dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT "
429 "-ff FDRE_1 Q INIT -ff FDCE_1 Q INIT -ff FDPE_1 Q INIT -ff FDSE_1 Q INIT");
433 if (check_label("check")) {
434 run("hierarchy -check");
435 run("stat -tech xilinx");
436 run("check -noinit");
439 if (check_label("edif")) {
440 if (!edif_file
.empty() || help_mode
)
441 run(stringf("write_edif -pvector bra %s", edif_file
.c_str()));
444 if (check_label("blif")) {
445 if (!blif_file
.empty() || help_mode
)
446 run(stringf("write_blif %s", edif_file
.c_str()));
451 PRIVATE_NAMESPACE_END