yosys/techlibs/microchip/synth_microchip.cc

554 lines
17 KiB
C++
Raw Normal View History

2024-07-02 14:44:30 -05:00
/*
ISC License
Copyright (C) 2024 Microchip Technology Inc. and its subsidiaries
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "kernel/celltypes.h"
#include "kernel/log.h"
2024-07-04 10:53:41 -05:00
#include "kernel/register.h"
#include "kernel/rtlil.h"
2024-07-02 14:44:30 -05:00
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
2024-07-04 10:53:41 -05:00
struct SynthMicrochipPass : public ScriptPass {
SynthMicrochipPass() : ScriptPass("synth_microchip", "synthesis for Microchip FPGAs") {}
2024-07-02 14:44:30 -05:00
void help() override
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
2024-07-04 10:53:41 -05:00
log(" synth_microchip [options]\n");
2024-07-02 14:44:30 -05:00
log("\n");
log("This command runs synthesis for Microchip FPGAs. This command creates \n");
log("netlists that are compatible with Microchip PolarFire devices. \n");
2024-07-02 14:44:30 -05:00
log("\n");
log(" -top <module>\n");
log(" use the specified module as the top module\n");
log("\n");
log(" -family <family>\n");
log(" Run synthesis for the specified Microchip architecture. \n");
log(" Generate the synthesis netlist for the specified family.\n");
log(" supported values:\n");
2024-07-04 12:25:15 -05:00
log(" - polarfire: PolarFire\n");
2024-07-02 14:44:30 -05:00
log("\n");
log(" -edif <file>\n");
log(" Write the design to the specified edif file. Writing of an output file\n");
log(" is omitted if this parameter is not specified.\n");
log("\n");
log(" -blif <file>\n");
log(" Write the design to the specified BLIF file. Writing of an output file\n");
2024-07-04 10:53:41 -05:00
log(" is omitted if this parameter is not specified.\n");
2024-07-02 14:44:30 -05:00
log("\n");
log(" -vlog <file>\n");
log(" write the design to the specified Verilog file. writing of an output\n");
log(" file is omitted if this parameter is not specified.\n");
2024-07-02 14:44:30 -05:00
log(" -nobram\n");
log(" Do not use block RAM cells in output netlist\n");
log("\n");
log(" -nocarry\n");
log(" Do not use ARI1 cells in output netlist\n");
log("\n");
log(" -nodsp\n");
log(" Do not use MATH blocks to implement multipliers and associated logic\n");
log("\n");
log(" -noiopad\n");
log(" Disable I/O buffer insertion (useful for hierarchical or \n");
log(" out-of-context flows)\n");
log("\n");
log(" -noclkbuf\n");
log(" Disable automatic clock buffer insertion\n");
log("\n");
log(" -run <from_label>:<to_label>\n");
log(" Only run the commands between the labels (see below). an empty\n");
log(" 'from_label' is synonymous to 'begin', and empty 'to_label' is\n");
log(" synonymous to the end of the command list.\n");
log("\n");
log(" -noflatten\n");
log(" do not flatten design before synthesis\n");
2024-07-02 14:44:30 -05:00
log("\n");
log(" -dff\n");
log(" Run 'abc'/'abc9' with -dff option\n");
log("\n");
log(" -retime\n");
log(" Run 'abc' with '-D 1' option to enable flip-flop retiming.\n");
log(" implies -dff.\n");
log("\n");
2024-07-08 14:10:44 -05:00
log(" -noabc9\n");
log(" Use classic ABC flow instead of ABC9\n");
2024-07-02 14:44:30 -05:00
log("\n");
log(" -discard-ffinit\n");
log(" discard FF init value instead of emitting an error\n");
log("\n");
2024-07-02 14:44:30 -05:00
log("\n");
log("The following commands are executed by this synthesis command:\n");
help_script();
log("\n");
}
std::string top_opt, edif_file, blif_file, vlog_file, family;
2024-07-02 14:44:30 -05:00
bool flatten, retime, noiopad, noclkbuf, nobram, nocarry, nowidelut, nodsp;
bool abc9, dff;
bool discard_ffinit;
2024-07-02 14:44:30 -05:00
int lut_size;
// debug dump switches
bool debug_memory, debug_carry;
void clear_flags() override
{
top_opt = "-auto-top";
edif_file.clear();
blif_file.clear();
vlog_file.clear();
2024-07-04 12:25:15 -05:00
family = "polarfire";
flatten = true;
2024-07-02 14:44:30 -05:00
retime = false;
noiopad = false;
noclkbuf = false;
nocarry = false;
nobram = false;
nowidelut = false;
nodsp = false;
abc9 = true;
2024-07-02 14:44:30 -05:00
dff = false;
lut_size = 4;
discard_ffinit = false;
2024-07-02 14:44:30 -05:00
debug_memory = false;
debug_carry = false;
}
void execute(std::vector<std::string> args, RTLIL::Design *design) override
{
std::string run_from, run_to;
clear_flags();
size_t argidx;
2024-07-04 10:53:41 -05:00
for (argidx = 1; argidx < args.size(); argidx++) {
if (args[argidx] == "-top" && argidx + 1 < args.size()) {
2024-07-02 14:44:30 -05:00
top_opt = "-top " + args[++argidx];
continue;
}
2024-07-04 10:53:41 -05:00
if ((args[argidx] == "-family" || args[argidx] == "-arch") && argidx + 1 < args.size()) {
2024-07-02 14:44:30 -05:00
family = args[++argidx];
continue;
}
2024-07-04 10:53:41 -05:00
if (args[argidx] == "-edif" && argidx + 1 < args.size()) {
2024-07-02 14:44:30 -05:00
edif_file = args[++argidx];
continue;
}
2024-07-04 10:53:41 -05:00
if (args[argidx] == "-blif" && argidx + 1 < args.size()) {
2024-07-02 14:44:30 -05:00
blif_file = args[++argidx];
continue;
}
if (args[argidx] == "-vlog" && argidx + 1 < args.size()) {
vlog_file = args[++argidx];
continue;
}
2024-07-04 10:53:41 -05:00
if (args[argidx] == "-run" && argidx + 1 < args.size()) {
size_t pos = args[argidx + 1].find(':');
2024-07-02 14:44:30 -05:00
if (pos == std::string::npos)
break;
run_from = args[++argidx].substr(0, pos);
2024-07-04 10:53:41 -05:00
run_to = args[argidx].substr(pos + 1);
2024-07-02 14:44:30 -05:00
continue;
}
if (args[argidx] == "-noflatten") {
flatten = false;
2024-07-02 14:44:30 -05:00
continue;
}
if (args[argidx] == "-retime") {
dff = true;
retime = true;
continue;
}
if (args[argidx] == "-nocarry") {
nocarry = true;
continue;
}
if (args[argidx] == "-nowidelut") {
nowidelut = true;
continue;
}
if (args[argidx] == "-iopad") {
continue;
}
if (args[argidx] == "-noiopad") {
noiopad = true;
continue;
}
if (args[argidx] == "-noclkbuf") {
noclkbuf = true;
continue;
}
if (args[argidx] == "-nocarry") {
nocarry = true;
continue;
}
if (args[argidx] == "-nobram") {
nobram = true;
continue;
}
2024-07-08 14:10:44 -05:00
if (args[argidx] == "-noabc9") {
abc9 = false;
2024-07-02 14:44:30 -05:00
continue;
}
if (args[argidx] == "-nodsp") {
nodsp = true;
continue;
}
if (args[argidx] == "-dff") {
dff = true;
continue;
}
if (args[argidx] == "-debug_memory") {
debug_memory = true;
continue;
}
if (args[argidx] == "-debug_carry") {
debug_carry = true;
continue;
}
if (args[argidx] == "-discard-ffinit") {
discard_ffinit = true;
continue;
}
2024-07-02 14:44:30 -05:00
break;
}
extra_args(args, argidx, design);
2024-07-04 11:45:04 -05:00
if (family == "polarfire") {
2024-07-02 14:44:30 -05:00
lut_size = 4;
} else {
log_cmd_error("Invalid Microchip -family setting: '%s'.\n", family.c_str());
2024-07-04 10:53:41 -05:00
}
2024-07-02 14:44:30 -05:00
if (!design->full_selection())
log_cmd_error("This command only operates on fully selected designs!\n");
if (abc9 && retime)
log_cmd_error("-retime option not currently compatible with -abc9!\n");
2024-07-04 10:53:41 -05:00
log_header(design, "Executing SYNTH_MICROCHIP pass.\n");
2024-07-02 14:44:30 -05:00
log_push();
run_script(design, run_from, run_to);
log_pop();
}
void script() override
{
std::string lut_size_s = std::to_string(lut_size);
if (help_mode)
lut_size_s = "[4]";
if (check_label("begin")) {
std::string read_args;
2024-07-04 10:53:41 -05:00
read_args += " -lib -specify +/microchip/cells_sim.v";
2024-07-02 14:44:30 -05:00
run("read_verilog" + read_args);
run(stringf("hierarchy -check %s", top_opt.c_str()));
}
if (check_label("prepare")) {
run("proc");
if (flatten || help_mode)
run("flatten", "(with '-flatten')");
if (active_design)
active_design->scratchpad_unset("tribuf.added_something");
run("tribuf -logic");
if (noiopad && active_design && active_design->scratchpad_get_bool("tribuf.added_something"))
log_error("Tristate buffers are unsupported without the '-iopad' option.\n");
run("deminout");
run("opt_expr");
run("opt_clean");
run("check");
run("opt -nodffe -nosdff");
run("fsm");
run("opt");
run("wreduce");
run("peepopt");
run("opt_clean");
}
if (check_label("map_dsp", "(skip if '-nodsp')")) {
if (!nodsp || help_mode) {
2024-07-04 10:53:41 -05:00
run("memory_dff"); // microchip_dsp will merge registers, reserve memory port registers first
2024-07-02 14:44:30 -05:00
if (help_mode)
2024-07-04 10:53:41 -05:00
run("techmap -map +/mul2dsp.v -map +/microchip/{family}_dsp_map.v {options}");
2024-07-04 12:25:15 -05:00
else if (family == "polarfire") // Microchip - map multipliers to DSP
run("techmap -map +/mul2dsp.v -map +/microchip/polarfire_dsp_map.v -D DSP_A_MAXWIDTH=18 -D DSP_B_MAXWIDTH=18 "
2024-07-04 10:53:41 -05:00
"-D DSP_A_MAXWIDTH_PARTIAL=18 " // Partial multipliers are intentionally
// limited to 18x18 in order to take
// advantage of the (PCOUT >> 17) -> PCIN
// dedicated cascade chain capability
"-D DSP_A_MINWIDTH=2 -D DSP_B_MINWIDTH=2 " // Blocks Nx1 multipliers
"-D DSP_Y_MINWIDTH=9 "
"-D DSP_SIGNEDONLY=1 -D DSP_NAME=$__MUL18X18");
2024-07-02 14:44:30 -05:00
run("select a:mul2dsp");
run("setattr -unset mul2dsp");
run("opt_expr -fine");
run("wreduce");
run("select -clear");
if (help_mode)
2024-07-04 10:53:41 -05:00
run("microchip_dsp -family <family>");
2024-07-04 12:25:15 -05:00
else if (family == "polarfire") // Microchip - absorb cells into DSP
2024-07-04 10:53:41 -05:00
run("microchip_dsp -family " + family);
2024-07-02 14:44:30 -05:00
run("chtype -set $mul t:$__soft_mul");
}
}
if (check_label("coarse")) {
run("techmap -map +/cmp2lut.v -map +/cmp2lcu.v -D LUT_WIDTH=" + lut_size_s);
run("alumacc");
run("share");
run("opt");
run("memory -nomap");
run("opt_clean");
if (discard_ffinit || help_mode)
run("attrmap -remove init", "(only if -discard-ffinit)");
2024-07-02 14:44:30 -05:00
}
if (check_label("map_memory")) {
std::string params = "";
2024-07-04 10:53:41 -05:00
std::string LSRAM_map = "+/microchip/LSRAM_map.v";
std::string uSRAM_map = "+/microchip/uSRAM_map.v";
if (debug_memory)
run("write_verilog -noexpr memory_map_pre.vm");
2024-07-02 14:44:30 -05:00
if (help_mode) {
params = " [...]";
} else {
2024-07-04 12:25:15 -05:00
if (family == "polarfire") {
2024-07-02 14:44:30 -05:00
// cost of a single bit for memory lowered to soft logic
2024-07-04 10:53:41 -05:00
params += " -logic-cost-rom 0.015625";
2024-07-02 14:44:30 -05:00
2024-07-04 10:53:41 -05:00
params += " -lib +/microchip/LSRAM.txt";
params += " -lib +/microchip/uSRAM.txt";
LSRAM_map = "+/microchip/LSRAM_map.v";
uSRAM_map = "+/microchip/uSRAM_map.v";
2024-07-02 14:44:30 -05:00
}
2024-07-04 10:53:41 -05:00
if (nobram)
params += " -no-auto-block";
2024-07-02 14:44:30 -05:00
}
// transform memories into intermediate cells
// Cost based transformation. The cost is assigned by us for each cell.
run("memory_libmap" + params);
2024-07-04 10:53:41 -05:00
if (debug_memory)
run("write_verilog -noexpr memory_map_libmap.vm");
2024-07-02 14:44:30 -05:00
// map intermediate cells to actual RAM macros
// NOTE: order doesnt matter here
run("techmap -map " + LSRAM_map);
run("techmap -map " + uSRAM_map);
2024-07-04 10:53:41 -05:00
if (debug_memory)
run("write_verilog -noexpr memory_map_final.vm");
2024-07-02 14:44:30 -05:00
}
if (check_label("map_ffram")) {
run("opt -fast -full");
// blast unmapped RAM to flops or LUTs
run("memory_map");
}
if (check_label("fine")) {
run("opt -full");
2024-07-04 10:53:41 -05:00
if (debug_carry)
run("write_verilog -noexpr ARI1_cells.vm");
2024-07-02 14:44:30 -05:00
if (!nocarry) {
// converts $mux -> $_MUX_ to allow muxcover to work
run("simplemap t:$mux");
// converts $and/$or/$xor to gate representation for extract_reduce to work
run("simplemap t:$xor"); // only mapping reduce_xor
// mapping based on Yosys internal gates
2024-07-04 10:53:41 -05:00
if (debug_carry)
run("write_verilog -noexpr ARI1_pre.vm");
2024-07-02 14:44:30 -05:00
// collapse $_AND_/$_OR_/$_XOR_ chains into reduction cells
run("extract_reduce");
2024-07-04 10:53:41 -05:00
if (debug_carry)
run("write_verilog -noexpr ARI1_extract_reduce.vm");
2024-07-02 14:44:30 -05:00
// pack mux trees into $_MUX4_
run("muxcover -nodecode -mux4=220");
2024-07-04 10:53:41 -05:00
if (debug_carry)
run("write_verilog -noexpr ARI1_muxcover.vm");
run("techmap -map +/microchip/arith_map.v");
if (debug_carry)
run("write_verilog -noexpr ARI1_post.vm");
2024-07-02 14:44:30 -05:00
}
// convert all remaining cells to gates
run("techmap -map +/techmap.v");
run("opt -fast");
}
if (check_label("map_cells")) {
// Needs to be done before logic optimization, so that inverters (inserted
// here because of negative-polarity output enable) are handled.
if (help_mode || !noiopad) {
2024-07-04 10:53:41 -05:00
run("iopadmap -bits -inpad INBUF Y:PAD -outpad OUTBUF D:PAD -toutpad TRIBUFF E:D:PAD -tinoutpad BIBUF E:Y:D:PAD",
"(unless -noiobs)");
2024-07-02 14:44:30 -05:00
}
2024-07-04 10:53:41 -05:00
std::string techmap_args = "-map +/techmap.v -map +/microchip/cells_map.v";
2024-07-02 14:44:30 -05:00
run("techmap " + techmap_args);
run("clean");
}
if (check_label("map_ffs")) {
// dfflegalize : Converts FFs to types supported by the target
// this can convert less capable cells into more capable cells (e.g. dff -> dffe)
// Based on PolarFire® FPGA Macro Library Guide
// D-flop:
// active high enable
// active low clear or active low set
// Latch:
// active low clear or active low set
// SLE (can implement D-flop/Latch):
// active high EN
// active low async load (set/reset) with static load configuration via ADn (Q = ~ADn)
// active low sync load (set/reset) with static load configuration via SD
// static latch configuration bit
// init not supported
// Yosys internal cell description
// see: https://yosyshq.readthedocs.io/projects/yosys/en/latest/yosys_internals/formats/cell_library.html
// see: common/simcells.v
// $_DFF_[NP]_ (regular dff)
// $_DFFE_[NP][NP]_ (enable)
// $_DFF_[NP][NP][01]_ (async reset to 0/1)
// $_DFFE_[NP][NP][01][NP]_ (async reset to 0/1 + enable)
// $_ALDFF_[NP][NP]_ (async load)
// $_ALDFFE_[NP][NP][NP]_ (async load + enable)
// $_DFFSR_[NP][NP][NP]_ (async set & reset)
// $_DFFSRE_[NP][NP][NP][NP]_ (async set & reset + enable)
// $_SDFF_[NP][NP][01]_ (sync reset to 0/1)
// $_SDFFE_[NP][NP][01][NP]_ (sync reset to 0/1 + enable, reset prioritize over enable)
// $_SDFFCE_[NP][NP][01][NP]_ (sync reset to 0/1 + enable, enable prioritize over reset)
// $_SR_[NP][NP]_ (set/reset latch)
// $_DLATCH_[NP]_ (D-latch)
// $_DLATCH_[NP][NP][01]_ (D-latch + reset to 0/1)
// $_DLATCHSR_[NP][NP][NP]_ (D-latch + set + reset)
2024-07-04 12:25:15 -05:00
if (family == "polarfire") {
2024-07-02 14:44:30 -05:00
std::string params = "";
// D-flop with async reset and enable
// posedge CLK, active low reset to 1 or 0, active high EN
params += " -cell $_DFFE_PN?P_ x";
2024-07-02 14:44:30 -05:00
// D-flop with sync reset and enable, enable takes priority over reset
// posedge CLK, active low reset to 1 or 0, active high EN
params += " -cell $_SDFFCE_PN?P_ x";
2024-07-02 14:44:30 -05:00
// D-latch + reset to 0/1
// posedge CLK, active low reset to 1 or 0
params += " -cell $_DLATCH_PN?_ x";
2024-07-02 14:44:30 -05:00
run("dfflegalize" + params, "(Converts FFs to supported types)");
}
if (abc9 || help_mode) {
if (dff || help_mode)
run("zinit -all w:* t:$_SDFFCE_*", "('-dff' only)");
2024-07-04 10:53:41 -05:00
run("techmap -D NO_LUT -map +/microchip/cells_map.v", "('-abc9' only)");
2024-07-02 14:44:30 -05:00
}
}
if (check_label("map_luts")) {
run("opt_expr -mux_undef -noclkinv");
if (help_mode)
run("abc -luts 2:2,3,6:5[,10,20] [-dff] [-D 1]", "(option for '-nowidelut', '-dff', '-retime')");
else if (abc9) {
2024-07-04 10:53:41 -05:00
2024-07-02 14:44:30 -05:00
std::string abc9_opts;
2024-07-08 14:10:44 -05:00
// for the if command in abc to specify wire delay between adjacent LUTs (default = 0)
2024-07-04 10:53:41 -05:00
// NOTE: should not have 0 wire delay between LUTs,
2024-07-02 14:44:30 -05:00
// otherwise abc might use LUT2+LUT3 instead of single LUT4
abc9_opts += " -W 300";
if (nowidelut)
abc9_opts += stringf(" -maxlut %d", lut_size);
if (dff)
abc9_opts += " -dff";
run("abc9" + abc9_opts);
2024-07-04 10:53:41 -05:00
} else {
2024-07-02 14:44:30 -05:00
std::string abc_opts = " -lut " + lut_size_s;
if (dff)
abc_opts += " -dff";
if (retime)
abc_opts += " -D 1";
run("abc" + abc_opts);
}
run("clean");
if (help_mode || !abc9)
2024-07-04 10:53:41 -05:00
run("techmap -D NO_LUT -map +/microchip/cells_map.v", "(only if not '-abc9')");
std::string techmap_args = "-map +/microchip/cells_map.v -D FINAL_MAP";
2024-07-02 14:44:30 -05:00
techmap_args += " -D LUT_WIDTH=" + lut_size_s;
run("techmap " + techmap_args);
2024-07-04 10:53:41 -05:00
if (help_mode || lut_size == 4)
run("microchip_dffopt");
2024-07-02 14:44:30 -05:00
}
2024-07-04 10:53:41 -05:00
run("clkbufmap -buf CLKINT Y:A -inpad CLKBUF Y:PAD");
2024-07-02 14:44:30 -05:00
2024-07-04 10:53:41 -05:00
run("clean -purge");
2024-07-02 14:44:30 -05:00
if (check_label("check")) {
run("hierarchy -check");
run("stat");
run("check -noinit");
run("blackbox =A:whitebox");
}
if (check_label("edif")) {
if (!edif_file.empty() || help_mode)
run(stringf("write_edif -pvector bra %s", edif_file.c_str()));
}
if (check_label("blif")) {
if (!blif_file.empty() || help_mode)
run(stringf("write_blif %s", blif_file.c_str()));
}
if (check_label("vlog"))
{
if (!vlog_file.empty() || help_mode)
run(stringf("write_verilog %s", help_mode ? "<file-name>" : vlog_file.c_str()));
}
2024-07-02 14:44:30 -05:00
}
2024-07-04 10:53:41 -05:00
} SynthMicrochipPass;
2024-07-02 14:44:30 -05:00
PRIVATE_NAMESPACE_END