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(" generate an output netlist suitable for ISE (enables -iopad)\n");
70 log(" do not use block RAM cells in output netlist\n");
73 log(" do not use distributed RAM cells in output netlist\n");
76 log(" do not use distributed SRL cells in output netlist\n");
79 log(" do not use XORCY/MUXCY/CARRY4 cells in output netlist\n");
82 log(" do not use MUXF[78] resources to implement LUTs larger than LUT6s\n");
85 log(" do not use DSP48E1s to implement multipliers and associated logic\n");
87 log(" enable I/O buffer insertion (selected automatically by -ise)\n");
90 log(" disable I/O buffer insertion (only useful with -ise)\n");
93 log(" disable automatic clock buffer insertion\n");
95 log(" -widemux <int>\n");
96 log(" enable inference of hard multiplexer resources (MUXF[78]) for muxes at or\n");
97 log(" above this number of inputs (minimum value 2, recommended value >= 5).\n");
98 log(" default: 0 (no inference)\n");
100 log(" -run <from_label>:<to_label>\n");
101 log(" only run the commands between the labels (see below). an empty\n");
102 log(" from label is synonymous to 'begin', and empty to label is\n");
103 log(" synonymous to the end of the command list.\n");
106 log(" flatten design before synthesis\n");
109 log(" run 'abc' with -dff option\n");
112 log(" use new ABC9 flow (EXPERIMENTAL)\n");
115 log("The following commands are executed by this synthesis command:\n");
120 std::string top_opt
, edif_file
, blif_file
, family
;
121 bool flatten
, retime
, vpr
, ise
, iopad
, noiopad
, noclkbuf
, nobram
, nolutram
, nosrl
, nocarry
, nowidelut
, nodsp
, abc9
;
122 bool flatten_before_abc
;
125 void clear_flags() YS_OVERRIDE
127 top_opt
= "-auto-top";
146 flatten_before_abc
= false;
150 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
152 std::string run_from
, run_to
;
156 for (argidx
= 1; argidx
< args
.size(); argidx
++)
158 if (args
[argidx
] == "-top" && argidx
+1 < args
.size()) {
159 top_opt
= "-top " + args
[++argidx
];
162 if ((args
[argidx
] == "-family" || args
[argidx
] == "-arch") && argidx
+1 < args
.size()) {
163 family
= args
[++argidx
];
166 if (args
[argidx
] == "-edif" && argidx
+1 < args
.size()) {
167 edif_file
= args
[++argidx
];
170 if (args
[argidx
] == "-blif" && argidx
+1 < args
.size()) {
171 blif_file
= args
[++argidx
];
174 if (args
[argidx
] == "-run" && argidx
+1 < args
.size()) {
175 size_t pos
= args
[argidx
+1].find(':');
176 if (pos
== std::string::npos
)
178 run_from
= args
[++argidx
].substr(0, pos
);
179 run_to
= args
[argidx
].substr(pos
+1);
182 if (args
[argidx
] == "-flatten") {
186 if (args
[argidx
] == "-flatten_before_abc") {
187 flatten_before_abc
= true;
190 if (args
[argidx
] == "-retime") {
194 if (args
[argidx
] == "-nocarry") {
198 if (args
[argidx
] == "-nowidelut") {
202 if (args
[argidx
] == "-vpr") {
206 if (args
[argidx
] == "-ise") {
210 if (args
[argidx
] == "-iopad") {
214 if (args
[argidx
] == "-noiopad") {
218 if (args
[argidx
] == "-noclkbuf") {
222 if (args
[argidx
] == "-nocarry") {
226 if (args
[argidx
] == "-nobram") {
230 if (args
[argidx
] == "-nolutram" || /*deprecated alias*/ args
[argidx
] == "-nodram") {
234 if (args
[argidx
] == "-nosrl") {
238 if (args
[argidx
] == "-widemux" && argidx
+1 < args
.size()) {
239 widemux
= atoi(args
[++argidx
].c_str());
242 if (args
[argidx
] == "-abc9") {
246 if (args
[argidx
] == "-nodsp") {
252 extra_args(args
, argidx
, design
);
254 if (family
!= "xcup" && family
!= "xcu" && family
!= "xc7" && family
!= "xc6s")
255 log_cmd_error("Invalid Xilinx -family setting: '%s'.\n", family
.c_str());
257 if (widemux
!= 0 && widemux
< 2)
258 log_cmd_error("-widemux value must be 0 or >= 2.\n");
260 if (!design
->full_selection())
261 log_cmd_error("This command only operates on fully selected designs!\n");
264 log_cmd_error("-retime option not currently compatible with -abc9!\n");
266 log_header(design
, "Executing SYNTH_XILINX pass.\n");
269 run_script(design
, run_from
, run_to
);
274 void script() YS_OVERRIDE
276 if (check_label("begin")) {
278 run("read_verilog -lib -D_EXPLICIT_CARRY +/xilinx/cells_sim.v");
280 run("read_verilog -lib +/xilinx/cells_sim.v");
282 run("read_verilog -lib +/xilinx/cells_xtra.v");
285 run("read_verilog -lib +/xilinx/{family}_brams_bb.v");
286 } else if (family
== "xc6s") {
287 run("read_verilog -lib +/xilinx/xc6s_brams_bb.v");
288 } else if (family
== "xc7") {
289 run("read_verilog -lib +/xilinx/xc7_brams_bb.v");
292 run(stringf("hierarchy -check %s", top_opt
.c_str()));
295 if (check_label("prepare")) {
297 if (flatten
|| help_mode
)
298 run("flatten", "(with '-flatten')");
304 run("wreduce [-keepdc]", "(option for '-widemux')");
306 run("wreduce" + std::string(widemux
> 0 ? " -keepdc" : ""));
310 if (widemux
> 0 || help_mode
)
311 run("muxpack", " ('-widemux' only)");
313 // xilinx_srl looks for $shiftx cells for identifying variable-length
314 // shift registers, so attempt to convert $pmux-es to this
315 // Also: wide multiplexer inference benefits from this too
316 if (!(nosrl
&& widemux
== 0) || help_mode
) {
317 run("pmux2shiftx", "(skip if '-nosrl' and '-widemux=0')");
318 run("clean", " (skip if '-nosrl' and '-widemux=0')");
321 run("techmap -map +/cmp2lut.v -D LUT_WIDTH=6");
324 if (check_label("map_dsp")) {
325 if (!nodsp
|| help_mode
) {
326 // NB: Xilinx multipliers are signed only
327 run("techmap -map +/mul2dsp.v -map +/xilinx/dsp_map.v -D DSP_A_MAXWIDTH=25 -D DSP_A_MAXWIDTH_PARTIAL=18 -D DSP_B_MAXWIDTH=18 -D DSP_SIGNEDONLY=1 -D DSP_NAME=$__MUL25X18", "(skip if '-nodsp')");
328 // Necessary for optimising away $shl cells, as well as $add cells
329 // that have inputs with 1'b0 LSBs
330 run("opt_expr -fine", " (skip if '-nodsp')");
331 run("xilinx_dsp", " (skip if '-nodsp')");
332 run("chtype -set $mul t:$__soft_mul"," (skip if '-nodsp')");
336 if (check_label("coarse")) {
342 run("memory -nomap");
346 if (check_label("map_bram", "(skip if '-nobram')")) {
348 run("memory_bram -rules +/xilinx/{family}_brams.txt");
349 run("techmap -map +/xilinx/{family}_brams_map.v");
350 } else if (!nobram
) {
351 if (family
== "xc6s") {
352 run("memory_bram -rules +/xilinx/xc6s_brams.txt");
353 run("techmap -map +/xilinx/xc6s_brams_map.v");
354 } else if (family
== "xc7") {
355 run("memory_bram -rules +/xilinx/xc7_brams.txt");
356 run("techmap -map +/xilinx/xc7_brams_map.v");
358 log_warning("Block RAM inference not yet supported for family %s.\n", family
.c_str());
363 if (check_label("map_lutram", "(skip if '-nolutram')")) {
364 if (!nolutram
|| help_mode
) {
365 run("memory_bram -rules +/xilinx/lutrams.txt");
366 run("techmap -map +/xilinx/lutrams_map.v");
370 if (check_label("map_ffram")) {
372 run("opt -fast -mux_bool -undriven -fine"); // Necessary to omit -mux_undef otherwise muxcover
373 // performs less efficiently
375 run("opt -fast -full");
379 if (check_label("fine")) {
383 run("simplemap t:$mux", " ('-widemux' only)");
384 run("muxcover <internal options>, ('-widemux' only)");
386 else if (widemux
> 0) {
387 run("simplemap t:$mux");
388 constexpr int cost_mux2
= 100;
389 std::string muxcover_args
= stringf(" -nodecode -mux2=%d", cost_mux2
);
391 case 2: muxcover_args
+= stringf(" -mux4=%d -mux8=%d -mux16=%d", cost_mux2
+1, cost_mux2
+2, cost_mux2
+3); break;
393 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;
397 case 8: muxcover_args
+= stringf(" -mux8=%d -mux16=%d", cost_mux2
*(widemux
-1)-1, cost_mux2
*(widemux
-1)); break;
405 default: muxcover_args
+= stringf(" -mux16=%d", cost_mux2
*(widemux
-1)-1); break;
407 run("muxcover " + muxcover_args
);
411 if (!nosrl
|| help_mode
)
412 run("xilinx_srl -variable -minlen 3", "(skip if '-nosrl')");
414 std::string techmap_args
= " -map +/techmap.v";
416 techmap_args
+= " [-map +/xilinx/mux_map.v]";
417 else if (widemux
> 0)
418 techmap_args
+= stringf(" -D MIN_MUX_INPUTS=%d -map +/xilinx/mux_map.v", widemux
);
420 techmap_args
+= " [-map +/xilinx/arith_map.v]";
422 techmap_args
+= " -map +/xilinx/arith_map.v";
424 techmap_args
+= " -D _EXPLICIT_CARRY";
426 techmap_args
+= " -D _CLB_CARRY";
428 run("techmap " + techmap_args
);
432 if (check_label("map_cells")) {
433 std::string techmap_args
= "-map +/techmap.v -map +/xilinx/cells_map.v";
435 techmap_args
+= stringf(" -D MIN_MUX_INPUTS=%d", widemux
);
436 run("techmap " + techmap_args
);
440 if (check_label("map_ffs")) {
441 if (abc9
|| help_mode
) {
442 run("techmap -map +/xilinx/ff_map.v", "('-abc9' only)");
443 run("dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT "
444 "-ff FDRE_1 Q INIT -ff FDCE_1 Q INIT -ff FDPE_1 Q INIT -ff FDSE_1 Q INIT", "('-abc9' only)");
448 if (check_label("map_luts")) {
449 run("opt_expr -mux_undef");
450 if (flatten_before_abc
)
453 run("abc -luts 2:2,3,6:5[,10,20] [-dff]", "(option for 'nowidelut'; option for '-retime')");
456 log_warning("'synth_xilinx -abc9' currently supports '-family xc7' only.\n");
457 run("techmap -map +/xilinx/abc_map.v -max_iter 1");
458 run("read_verilog -icells -lib +/xilinx/abc_model.v");
460 run("abc9 -lut +/xilinx/abc_xc7_nowide.lut -box +/xilinx/abc_xc7.box -W " + std::to_string(XC7_WIRE_DELAY
));
462 run("abc9 -lut +/xilinx/abc_xc7.lut -box +/xilinx/abc_xc7.box -W " + std::to_string(XC7_WIRE_DELAY
));
466 run("abc -luts 2:2,3,6:5" + string(retime
? " -dff" : ""));
468 run("abc -luts 2:2,3,6:5,10,20" + string(retime
? " -dff" : ""));
472 // This shregmap call infers fixed length shift registers after abc
473 // has performed any necessary retiming
474 if (!nosrl
|| help_mode
)
475 run("xilinx_srl -fixed -minlen 3", "(skip if '-nosrl')");
476 std::string techmap_args
= "-map +/xilinx/lut_map.v -map +/xilinx/cells_map.v";
478 techmap_args
+= " [-map +/xilinx/ff_map.v]";
480 techmap_args
+= " -map +/xilinx/abc_unmap.v";
482 techmap_args
+= " -map +/xilinx/ff_map.v";
483 run("techmap " + techmap_args
);
484 if (!abc9
|| help_mode
)
485 run("dffinit -ff FDRE Q INIT -ff FDCE Q INIT -ff FDPE Q INIT -ff FDSE Q INIT "
486 "-ff FDRE_1 Q INIT -ff FDCE_1 Q INIT -ff FDPE_1 Q INIT -ff FDSE_1 Q INIT", "(without '-abc9' only)");
490 if (check_label("finalize")) {
491 bool do_iopad
= iopad
|| (ise
&& !noiopad
);
492 if (help_mode
|| !noclkbuf
) {
493 if (help_mode
|| do_iopad
)
494 run("clkbufmap -buf BUFG O:I -inpad IBUFG O:I", "(skip if '-noclkbuf', '-inpad' passed if '-iopad' or '-ise' and not '-noiopad')");
496 run("clkbufmap -buf BUFG O:I");
499 run("iopadmap -bits -outpad OBUF I:O -inpad IBUF O:I A:top", "(only if '-iopad' or '-ise' and not '-noiopad')");
502 if (check_label("check")) {
503 run("hierarchy -check");
504 run("stat -tech xilinx");
505 run("check -noinit");
508 if (check_label("edif")) {
509 if (!edif_file
.empty() || help_mode
)
510 run(stringf("write_edif -pvector bra %s", edif_file
.c_str()));
513 if (check_label("blif")) {
514 if (!blif_file
.empty() || help_mode
)
515 run(stringf("write_blif %s", edif_file
.c_str()));
520 PRIVATE_NAMESPACE_END