2013-08-22 04:34:55 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-08-22 04:34:55 -05:00
|
|
|
* 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.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-08-22 04:34:55 -05:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-03 14:41:39 -06:00
|
|
|
// [[CITE]] EDIF Version 2 0 0 Grammar
|
|
|
|
// http://web.archive.org/web/20050730021644/http://www.edif.org/documentation/BNF_GRAMMAR/index.html
|
|
|
|
|
2013-08-22 04:34:55 -05:00
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2014-02-21 06:40:43 -06:00
|
|
|
#define EDIF_DEF(_id) edif_names(RTLIL::unescape_id(_id), true).c_str()
|
2017-02-24 06:18:49 -06:00
|
|
|
#define EDIF_DEFR(_id, _ren, _bl, _br) edif_names(RTLIL::unescape_id(_id), true, _ren, _bl, _br).c_str()
|
2014-02-21 06:40:43 -06:00
|
|
|
#define EDIF_REF(_id) edif_names(RTLIL::unescape_id(_id), false).c_str()
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
struct EdifNames
|
2013-08-22 07:30:33 -05:00
|
|
|
{
|
2017-02-25 09:29:27 -06:00
|
|
|
int counter;
|
|
|
|
char delim_left, delim_right;
|
|
|
|
std::set<std::string> generated_names, used_names;
|
|
|
|
std::map<std::string, std::string> name_map;
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
EdifNames() : counter(1), delim_left('['), delim_right(']') { }
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
std::string operator()(std::string id, bool define, bool port_rename = false, int range_left = 0, int range_right = 0)
|
|
|
|
{
|
|
|
|
if (define) {
|
|
|
|
std::string new_id = operator()(id, false);
|
|
|
|
if (port_rename)
|
|
|
|
return stringf("(rename %s \"%s%c%d:%d%c\")", new_id.c_str(), id.c_str(), delim_left, range_left, range_right, delim_right);
|
|
|
|
return new_id != id ? stringf("(rename %s \"%s\")", new_id.c_str(), id.c_str()) : id;
|
|
|
|
}
|
2014-02-21 06:40:43 -06:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
if (name_map.count(id) > 0)
|
|
|
|
return name_map.at(id);
|
|
|
|
if (generated_names.count(id) > 0)
|
|
|
|
goto do_rename;
|
|
|
|
if (id == "GND" || id == "VCC")
|
|
|
|
goto do_rename;
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
for (size_t i = 0; i < id.size(); i++) {
|
|
|
|
if ('A' <= id[i] && id[i] <= 'Z')
|
|
|
|
continue;
|
|
|
|
if ('a' <= id[i] && id[i] <= 'z')
|
|
|
|
continue;
|
|
|
|
if ('0' <= id[i] && id[i] <= '9' && i > 0)
|
|
|
|
continue;
|
|
|
|
if (id[i] == '_' && i > 0 && i != id.size()-1)
|
|
|
|
continue;
|
|
|
|
goto do_rename;
|
|
|
|
}
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
used_names.insert(id);
|
|
|
|
return id;
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2017-02-25 09:29:27 -06:00
|
|
|
do_rename:;
|
|
|
|
std::string gen_name;
|
|
|
|
while (1) {
|
|
|
|
gen_name = stringf("id%05d", counter++);
|
|
|
|
if (generated_names.count(gen_name) == 0 &&
|
|
|
|
used_names.count(gen_name) == 0)
|
|
|
|
break;
|
2013-08-22 07:30:33 -05:00
|
|
|
}
|
2017-02-25 09:29:27 -06:00
|
|
|
generated_names.insert(gen_name);
|
|
|
|
name_map[id] = gen_name;
|
|
|
|
return gen_name;
|
|
|
|
}
|
|
|
|
};
|
2013-08-22 07:30:33 -05:00
|
|
|
|
2013-08-22 04:34:55 -05:00
|
|
|
struct EdifBackend : public Backend {
|
|
|
|
EdifBackend() : Backend("edif", "write design to EDIF netlist file") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-08-22 04:34:55 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" write_edif [options] [filename]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Write the current design to an EDIF netlist file.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -top top_module\n");
|
|
|
|
log(" set the specified module as design top module\n");
|
|
|
|
log("\n");
|
2016-03-08 14:30:45 -06:00
|
|
|
log(" -nogndvcc\n");
|
|
|
|
log(" do not create \"GND\" and \"VCC\" cells. (this will produce an error\n");
|
|
|
|
log(" if the design contains constant nets. use \"hilomap\" to map to custom\n");
|
|
|
|
log(" constant drivers first)\n");
|
|
|
|
log("\n");
|
2019-01-17 06:33:11 -06:00
|
|
|
log(" -gndvccy\n");
|
2022-08-23 17:28:27 -05:00
|
|
|
log(" create \"GND\" and \"VCC\" cells with \"Y\" outputs. (the default is\n");
|
|
|
|
log(" \"G\" for \"GND\" and \"P\" for \"VCC\".)\n");
|
2019-01-17 06:33:11 -06:00
|
|
|
log("\n");
|
2018-10-05 02:41:18 -05:00
|
|
|
log(" -attrprop\n");
|
|
|
|
log(" create EDIF properties for cell attributes\n");
|
|
|
|
log("\n");
|
2020-04-20 09:00:37 -05:00
|
|
|
log(" -keep\n");
|
|
|
|
log(" create extra KEEP nets by allowing a cell to drive multiple nets.\n");
|
|
|
|
log("\n");
|
2017-02-24 06:18:49 -06:00
|
|
|
log(" -pvector {par|bra|ang}\n");
|
2017-02-25 09:35:53 -06:00
|
|
|
log(" sets the delimiting character for module port rename clauses to\n");
|
|
|
|
log(" parentheses, square brackets, or angle brackets.\n");
|
2017-02-23 12:42:37 -06:00
|
|
|
log("\n");
|
2019-11-12 10:38:00 -06:00
|
|
|
log(" -lsbidx\n");
|
|
|
|
log(" use index 0 for the LSB bit of a net or port instead of MSB.\n");
|
|
|
|
log("\n");
|
2013-09-17 06:07:12 -05:00
|
|
|
log("Unfortunately there are different \"flavors\" of the EDIF file format. This\n");
|
|
|
|
log("command generates EDIF files for the Xilinx place&route tools. It might be\n");
|
|
|
|
log("necessary to make small modifications to this command when a different tool\n");
|
|
|
|
log("is targeted.\n");
|
|
|
|
log("\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
|
2013-08-22 04:34:55 -05:00
|
|
|
{
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing EDIF backend.\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
std::string top_module_name;
|
2017-02-24 06:18:49 -06:00
|
|
|
bool port_rename = false;
|
2018-10-05 02:41:18 -05:00
|
|
|
bool attr_properties = false;
|
2019-11-12 10:38:00 -06:00
|
|
|
bool lsbidx = false;
|
2015-02-01 08:43:35 -06:00
|
|
|
std::map<RTLIL::IdString, std::map<RTLIL::IdString, int>> lib_cell_ports;
|
2020-04-20 09:00:37 -05:00
|
|
|
bool nogndvcc = false, gndvccy = false, keepmode = false;
|
2013-08-22 04:34:55 -05:00
|
|
|
CellTypes ct(design);
|
2013-08-22 07:30:33 -05:00
|
|
|
EdifNames edif_names;
|
2013-08-22 04:34:55 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-top" && argidx+1 < args.size()) {
|
|
|
|
top_module_name = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-08 14:30:45 -06:00
|
|
|
if (args[argidx] == "-nogndvcc") {
|
|
|
|
nogndvcc = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-17 06:33:11 -06:00
|
|
|
if (args[argidx] == "-gndvccy") {
|
|
|
|
gndvccy = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-05 02:41:18 -05:00
|
|
|
if (args[argidx] == "-attrprop") {
|
|
|
|
attr_properties = true;
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
if (args[argidx] == "-keep") {
|
|
|
|
keepmode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-24 06:18:49 -06:00
|
|
|
if (args[argidx] == "-pvector" && argidx+1 < args.size()) {
|
2017-02-23 12:42:37 -06:00
|
|
|
std::string parray;
|
2017-02-24 06:18:49 -06:00
|
|
|
port_rename = true;
|
2017-02-23 12:42:37 -06:00
|
|
|
parray = args[++argidx];
|
|
|
|
if (parray == "par") {
|
2017-02-24 06:18:49 -06:00
|
|
|
edif_names.delim_left = '(';edif_names.delim_right = ')';
|
2017-02-23 12:42:37 -06:00
|
|
|
} else if (parray == "ang") {
|
2017-02-24 06:18:49 -06:00
|
|
|
edif_names.delim_left = '<';edif_names.delim_right = '>';
|
2017-02-23 12:42:37 -06:00
|
|
|
} else {
|
2017-02-24 06:18:49 -06:00
|
|
|
edif_names.delim_left = '[';edif_names.delim_right = ']';
|
2017-02-23 12:42:37 -06:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-12 10:38:00 -06:00
|
|
|
if (args[argidx] == "-lsbidx") {
|
|
|
|
lsbidx = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-22 04:34:55 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
2013-11-23 22:03:43 -06:00
|
|
|
if (top_module_name.empty())
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto module : design->modules())
|
2020-03-12 14:57:01 -05:00
|
|
|
if (module->get_bool_attribute(ID::top))
|
2020-03-31 23:37:07 -05:00
|
|
|
top_module_name = module->name.str();
|
2013-11-23 22:03:43 -06:00
|
|
|
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto module : design->modules())
|
2013-08-22 04:34:55 -05:00
|
|
|
{
|
2019-11-14 08:55:21 -06:00
|
|
|
lib_cell_ports[module->name];
|
|
|
|
|
|
|
|
for (auto port : module->ports)
|
|
|
|
{
|
|
|
|
Wire *wire = module->wire(port);
|
|
|
|
lib_cell_ports[module->name][port] = std::max(lib_cell_ports[module->name][port], GetSize(wire));
|
|
|
|
}
|
|
|
|
|
2019-04-18 10:42:12 -05:00
|
|
|
if (module->get_blackbox_attribute())
|
2013-08-22 04:34:55 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (top_module_name.empty())
|
2014-08-02 11:58:40 -05:00
|
|
|
top_module_name = module->name.str();
|
2013-08-22 04:34:55 -05:00
|
|
|
|
|
|
|
if (module->processes.size() != 0)
|
2020-03-31 23:37:07 -05:00
|
|
|
log_error("Found unmapped processes in module %s: unmapped processes are not supported in EDIF backend!\n", log_id(module->name));
|
2013-08-22 04:34:55 -05:00
|
|
|
if (module->memories.size() != 0)
|
2020-03-31 23:37:07 -05:00
|
|
|
log_error("Found unmapped memories in module %s: unmapped memories are not supported in EDIF backend!\n", log_id(module->name));
|
2013-08-22 04:34:55 -05:00
|
|
|
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto cell : module->cells())
|
2013-08-22 04:34:55 -05:00
|
|
|
{
|
2020-03-31 23:37:07 -05:00
|
|
|
if (design->module(cell->type) == nullptr || design->module(cell->type)->get_blackbox_attribute()) {
|
2013-08-22 04:34:55 -05:00
|
|
|
lib_cell_ports[cell->type];
|
2015-02-01 08:43:35 -06:00
|
|
|
for (auto p : cell->connections())
|
2019-11-14 08:55:21 -06:00
|
|
|
lib_cell_ports[cell->type][p.first] = std::max(lib_cell_ports[cell->type][p.first], GetSize(p.second));
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top_module_name.empty())
|
|
|
|
log_error("No module found in design!\n");
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf("(edif %s\n", EDIF_DEF(top_module_name));
|
|
|
|
*f << stringf(" (edifVersion 2 0 0)\n");
|
|
|
|
*f << stringf(" (edifLevel 0)\n");
|
|
|
|
*f << stringf(" (keywordMap (keywordLevel 0))\n");
|
|
|
|
*f << stringf(" (comment \"Generated by %s\")\n", yosys_version_str);
|
|
|
|
|
|
|
|
*f << stringf(" (external LIB\n");
|
|
|
|
*f << stringf(" (edifLevel 0)\n");
|
|
|
|
*f << stringf(" (technology (numberDefinition))\n");
|
|
|
|
|
2016-03-08 14:30:45 -06:00
|
|
|
if (!nogndvcc)
|
|
|
|
{
|
|
|
|
*f << stringf(" (cell GND\n");
|
|
|
|
*f << stringf(" (cellType GENERIC)\n");
|
|
|
|
*f << stringf(" (view VIEW_NETLIST\n");
|
|
|
|
*f << stringf(" (viewType NETLIST)\n");
|
2019-01-17 06:33:11 -06:00
|
|
|
*f << stringf(" (interface (port %c (direction OUTPUT)))\n", gndvccy ? 'Y' : 'G');
|
2016-03-08 14:30:45 -06:00
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
|
|
|
|
|
|
|
*f << stringf(" (cell VCC\n");
|
|
|
|
*f << stringf(" (cellType GENERIC)\n");
|
|
|
|
*f << stringf(" (view VIEW_NETLIST\n");
|
|
|
|
*f << stringf(" (viewType NETLIST)\n");
|
2019-01-17 06:33:11 -06:00
|
|
|
*f << stringf(" (interface (port %c (direction OUTPUT)))\n", gndvccy ? 'Y' : 'P');
|
2016-03-08 14:30:45 -06:00
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
|
|
|
}
|
2013-09-17 06:07:12 -05:00
|
|
|
|
2013-08-22 04:34:55 -05:00
|
|
|
for (auto &cell_it : lib_cell_ports) {
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (cell %s\n", EDIF_DEF(cell_it.first));
|
|
|
|
*f << stringf(" (cellType GENERIC)\n");
|
|
|
|
*f << stringf(" (view VIEW_NETLIST\n");
|
|
|
|
*f << stringf(" (viewType NETLIST)\n");
|
|
|
|
*f << stringf(" (interface\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
for (auto &port_it : cell_it.second) {
|
|
|
|
const char *dir = "INOUT";
|
|
|
|
if (ct.cell_known(cell_it.first)) {
|
2015-02-01 08:43:35 -06:00
|
|
|
if (!ct.cell_output(cell_it.first, port_it.first))
|
2013-08-22 04:34:55 -05:00
|
|
|
dir = "INPUT";
|
2015-02-01 08:43:35 -06:00
|
|
|
else if (!ct.cell_input(cell_it.first, port_it.first))
|
2013-08-22 04:34:55 -05:00
|
|
|
dir = "OUTPUT";
|
|
|
|
}
|
2020-02-01 08:27:27 -06:00
|
|
|
int width = port_it.second;
|
|
|
|
int start = 0;
|
|
|
|
bool upto = false;
|
|
|
|
auto m = design->module(cell_it.first);
|
|
|
|
if (m) {
|
|
|
|
auto w = m->wire(port_it.first);
|
|
|
|
if (w) {
|
|
|
|
width = GetSize(w);
|
|
|
|
start = w->start_offset;
|
|
|
|
upto = w->upto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (width == 1)
|
2015-02-01 08:43:35 -06:00
|
|
|
*f << stringf(" (port %s (direction %s))\n", EDIF_DEF(port_it.first), dir);
|
2017-02-24 06:18:49 -06:00
|
|
|
else {
|
2020-02-01 08:27:27 -06:00
|
|
|
int b[2];
|
|
|
|
b[upto ? 0 : 1] = start;
|
|
|
|
b[upto ? 1 : 0] = start+width-1;
|
|
|
|
*f << stringf(" (port (array %s %d) (direction %s))\n", EDIF_DEFR(port_it.first, port_rename, b[0], b[1]), width, dir);
|
2017-02-23 12:42:37 -06:00
|
|
|
}
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
|
2013-11-03 15:01:32 -06:00
|
|
|
std::vector<RTLIL::Module*> sorted_modules;
|
|
|
|
|
|
|
|
// extract module dependencies
|
|
|
|
std::map<RTLIL::Module*, std::set<RTLIL::Module*>> module_deps;
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto module : design->modules()) {
|
|
|
|
module_deps[module] = std::set<RTLIL::Module*>();
|
|
|
|
for (auto cell : module->cells())
|
|
|
|
if (design->module(cell->type) != nullptr)
|
|
|
|
module_deps[module].insert(design->module(cell->type));
|
2013-11-03 15:01:32 -06:00
|
|
|
}
|
|
|
|
|
2013-11-04 01:34:15 -06:00
|
|
|
// simple good-enough topological sort
|
|
|
|
// (O(n*m) on n elements and depth m)
|
2013-11-03 15:01:32 -06:00
|
|
|
while (module_deps.size() > 0) {
|
|
|
|
size_t sorted_modules_idx = sorted_modules.size();
|
|
|
|
for (auto &it : module_deps) {
|
|
|
|
for (auto &dep : it.second)
|
|
|
|
if (module_deps.count(dep) > 0)
|
2013-11-04 01:34:15 -06:00
|
|
|
goto not_ready_yet;
|
2020-03-31 23:37:07 -05:00
|
|
|
// log("Next in topological sort: %s\n", log_id(it.first->name));
|
2013-11-03 15:01:32 -06:00
|
|
|
sorted_modules.push_back(it.first);
|
2013-11-04 01:34:15 -06:00
|
|
|
not_ready_yet:;
|
2013-11-03 15:01:32 -06:00
|
|
|
}
|
|
|
|
if (sorted_modules_idx == sorted_modules.size())
|
2020-03-31 23:37:07 -05:00
|
|
|
log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", log_id(module_deps.begin()->first->name));
|
2013-11-03 15:01:32 -06:00
|
|
|
while (sorted_modules_idx < sorted_modules.size())
|
|
|
|
module_deps.erase(sorted_modules.at(sorted_modules_idx++));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (library DESIGN\n");
|
|
|
|
*f << stringf(" (edifLevel 0)\n");
|
|
|
|
*f << stringf(" (technology (numberDefinition))\n");
|
2020-01-10 05:33:58 -06:00
|
|
|
|
|
|
|
auto add_prop = [&](IdString name, Const val) {
|
|
|
|
if ((val.flags & RTLIL::CONST_FLAG_STRING) != 0)
|
|
|
|
*f << stringf("\n (property %s (string \"%s\"))", EDIF_DEF(name), val.decode_string().c_str());
|
|
|
|
else if (val.bits.size() <= 32 && RTLIL::SigSpec(val).is_fully_def())
|
|
|
|
*f << stringf("\n (property %s (integer %u))", EDIF_DEF(name), val.as_int());
|
|
|
|
else {
|
|
|
|
std::string hex_string = "";
|
|
|
|
for (size_t i = 0; i < val.bits.size(); i += 4) {
|
|
|
|
int digit_value = 0;
|
|
|
|
if (i+0 < val.bits.size() && val.bits.at(i+0) == RTLIL::State::S1) digit_value |= 1;
|
|
|
|
if (i+1 < val.bits.size() && val.bits.at(i+1) == RTLIL::State::S1) digit_value |= 2;
|
|
|
|
if (i+2 < val.bits.size() && val.bits.at(i+2) == RTLIL::State::S1) digit_value |= 4;
|
|
|
|
if (i+3 < val.bits.size() && val.bits.at(i+3) == RTLIL::State::S1) digit_value |= 8;
|
|
|
|
char digit_str[2] = { "0123456789abcdef"[digit_value], 0 };
|
|
|
|
hex_string = std::string(digit_str) + hex_string;
|
|
|
|
}
|
|
|
|
*f << stringf("\n (property %s (string \"%d'h%s\"))", EDIF_DEF(name), GetSize(val.bits), hex_string.c_str());
|
|
|
|
}
|
2020-09-14 05:43:18 -05:00
|
|
|
};
|
2013-11-03 15:01:32 -06:00
|
|
|
for (auto module : sorted_modules)
|
2013-08-22 04:34:55 -05:00
|
|
|
{
|
2019-04-18 10:42:12 -05:00
|
|
|
if (module->get_blackbox_attribute())
|
2013-08-22 04:34:55 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SigMap sigmap(module);
|
2020-01-29 07:07:11 -06:00
|
|
|
std::map<RTLIL::SigSpec, std::set<std::pair<std::string, bool>>> net_join_db;
|
2013-08-22 04:34:55 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (cell %s\n", EDIF_DEF(module->name));
|
|
|
|
*f << stringf(" (cellType GENERIC)\n");
|
|
|
|
*f << stringf(" (view VIEW_NETLIST\n");
|
|
|
|
*f << stringf(" (viewType NETLIST)\n");
|
|
|
|
*f << stringf(" (interface\n");
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2020-04-21 05:35:25 -05:00
|
|
|
for (auto cell : module->cells()) {
|
|
|
|
for (auto &conn : cell->connections())
|
|
|
|
if (cell->output(conn.first))
|
|
|
|
sigmap.add(conn.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
for (auto b1 : SigSpec(wire))
|
|
|
|
{
|
|
|
|
auto b2 = sigmap(b1);
|
|
|
|
|
|
|
|
if (b1 == b2 || !b2.wire)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
log_assert(b1.wire != nullptr);
|
|
|
|
|
|
|
|
Wire *w1 = b1.wire;
|
|
|
|
Wire *w2 = b2.wire;
|
|
|
|
|
|
|
|
{
|
|
|
|
int c1 = w1->get_bool_attribute(ID::keep);
|
|
|
|
int c2 = w2->get_bool_attribute(ID::keep);
|
|
|
|
|
|
|
|
if (c1 > c2) goto promote;
|
|
|
|
if (c1 < c2) goto nopromote;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-09-14 05:43:18 -05:00
|
|
|
int c1 = w1->name.isPublic();
|
|
|
|
int c2 = w2->name.isPublic();
|
2020-04-21 05:35:25 -05:00
|
|
|
|
|
|
|
if (c1 > c2) goto promote;
|
|
|
|
if (c1 < c2) goto nopromote;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto count_nontrivial_attr = [](Wire *w) {
|
|
|
|
int count = w->attributes.size();
|
|
|
|
count -= w->attributes.count(ID::src);
|
|
|
|
count -= w->attributes.count(ID::unused_bits);
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
|
|
|
int c1 = count_nontrivial_attr(w1);
|
|
|
|
int c2 = count_nontrivial_attr(w2);
|
|
|
|
|
|
|
|
if (c1 > c2) goto promote;
|
|
|
|
if (c1 < c2) goto nopromote;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int c1 = w1->port_id ? INT_MAX - w1->port_id : 0;
|
|
|
|
int c2 = w2->port_id ? INT_MAX - w2->port_id : 0;
|
|
|
|
|
|
|
|
if (c1 > c2) goto promote;
|
|
|
|
if (c1 < c2) goto nopromote;
|
|
|
|
}
|
|
|
|
|
|
|
|
nopromote:
|
|
|
|
if (0)
|
|
|
|
promote:
|
|
|
|
sigmap.add(b1);
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto wire : module->wires()) {
|
2013-08-22 04:34:55 -05:00
|
|
|
if (wire->port_id == 0)
|
|
|
|
continue;
|
|
|
|
const char *dir = "INOUT";
|
|
|
|
if (!wire->port_output)
|
|
|
|
dir = "INPUT";
|
|
|
|
else if (!wire->port_input)
|
|
|
|
dir = "OUTPUT";
|
2013-08-27 07:22:11 -05:00
|
|
|
if (wire->width == 1) {
|
2020-01-10 05:33:58 -06:00
|
|
|
*f << stringf(" (port %s (direction %s)", EDIF_DEF(wire->name), dir);
|
|
|
|
if (attr_properties)
|
|
|
|
for (auto &p : wire->attributes)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
*f << ")\n";
|
2013-08-27 07:22:11 -05:00
|
|
|
RTLIL::SigSpec sig = sigmap(RTLIL::SigSpec(wire));
|
2020-01-29 07:07:11 -06:00
|
|
|
net_join_db[sig].insert(make_pair(stringf("(portRef %s)", EDIF_REF(wire->name)), wire->port_input));
|
2013-08-27 07:22:11 -05:00
|
|
|
} else {
|
2017-02-25 09:28:34 -06:00
|
|
|
int b[2];
|
|
|
|
b[wire->upto ? 0 : 1] = wire->start_offset;
|
|
|
|
b[wire->upto ? 1 : 0] = wire->start_offset + GetSize(wire) - 1;
|
2020-01-10 05:33:58 -06:00
|
|
|
*f << stringf(" (port (array %s %d) (direction %s)", EDIF_DEFR(wire->name, port_rename, b[0], b[1]), wire->width, dir);
|
|
|
|
if (attr_properties)
|
|
|
|
for (auto &p : wire->attributes)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
|
|
|
|
*f << ")\n";
|
2013-08-27 07:22:11 -05:00
|
|
|
for (int i = 0; i < wire->width; i++) {
|
2014-07-23 02:48:26 -05:00
|
|
|
RTLIL::SigSpec sig = sigmap(RTLIL::SigSpec(wire, i));
|
2019-11-12 10:38:00 -06:00
|
|
|
net_join_db[sig].insert(make_pair(stringf("(portRef (member %s %d))", EDIF_REF(wire->name), lsbidx ? i : GetSize(wire)-i-1), wire->port_input));
|
2013-08-27 07:22:11 -05:00
|
|
|
}
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" (contents\n");
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2016-03-08 14:30:45 -06:00
|
|
|
if (!nogndvcc) {
|
|
|
|
*f << stringf(" (instance GND (viewRef VIEW_NETLIST (cellRef GND (libraryRef LIB))))\n");
|
|
|
|
*f << stringf(" (instance VCC (viewRef VIEW_NETLIST (cellRef VCC (libraryRef LIB))))\n");
|
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2020-03-31 23:37:07 -05:00
|
|
|
for (auto cell : module->cells()) {
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (instance %s\n", EDIF_DEF(cell->name));
|
|
|
|
*f << stringf(" (viewRef VIEW_NETLIST (cellRef %s%s))", EDIF_REF(cell->type),
|
2013-08-22 04:34:55 -05:00
|
|
|
lib_cell_ports.count(cell->type) > 0 ? " (libraryRef LIB)" : "");
|
2018-10-05 02:41:18 -05:00
|
|
|
for (auto &p : cell->parameters)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
if (attr_properties)
|
|
|
|
for (auto &p : cell->attributes)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(")\n");
|
2014-07-26 07:32:50 -05:00
|
|
|
for (auto &p : cell->connections()) {
|
2013-08-22 04:34:55 -05:00
|
|
|
RTLIL::SigSpec sig = sigmap(p.second);
|
2014-10-10 09:59:44 -05:00
|
|
|
for (int i = 0; i < GetSize(sig); i++)
|
2017-02-14 05:49:35 -06:00
|
|
|
if (sig[i].wire == NULL && sig[i] != RTLIL::State::S0 && sig[i] != RTLIL::State::S1)
|
|
|
|
log_warning("Bit %d of cell port %s.%s.%s driven by %s will be left unconnected in EDIF output.\n",
|
|
|
|
i, log_id(module), log_id(cell), log_id(p.first), log_signal(sig[i]));
|
2017-02-25 09:28:34 -06:00
|
|
|
else {
|
2019-11-12 10:38:00 -06:00
|
|
|
int member_idx = lsbidx ? i : GetSize(sig)-i-1;
|
2017-02-25 09:28:34 -06:00
|
|
|
auto m = design->module(cell->type);
|
2020-02-01 08:27:27 -06:00
|
|
|
int width = sig.size();
|
2017-02-25 09:28:34 -06:00
|
|
|
if (m) {
|
|
|
|
auto w = m->wire(p.first);
|
2020-02-01 08:27:27 -06:00
|
|
|
if (w) {
|
2019-11-12 10:38:00 -06:00
|
|
|
member_idx = lsbidx ? i : GetSize(w)-i-1;
|
2020-02-01 08:27:27 -06:00
|
|
|
width = GetSize(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (width == 1)
|
|
|
|
net_join_db[sig[i]].insert(make_pair(stringf("(portRef %s (instanceRef %s))", EDIF_REF(p.first), EDIF_REF(cell->name)), cell->output(p.first)));
|
|
|
|
else {
|
|
|
|
net_join_db[sig[i]].insert(make_pair(stringf("(portRef (member %s %d) (instanceRef %s))",
|
|
|
|
EDIF_REF(p.first), member_idx, EDIF_REF(cell->name)), cell->output(p.first)));
|
2017-02-25 09:28:34 -06:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2013-08-22 04:34:55 -05:00
|
|
|
for (auto &it : net_join_db) {
|
2014-07-24 15:47:57 -05:00
|
|
|
RTLIL::SigBit sig = it.first;
|
2017-07-11 10:38:19 -05:00
|
|
|
if (sig.wire == NULL && sig != RTLIL::State::S0 && sig != RTLIL::State::S1) {
|
|
|
|
if (sig == RTLIL::State::Sx) {
|
|
|
|
for (auto &ref : it.second)
|
2020-01-29 07:07:11 -06:00
|
|
|
log_warning("Exporting x-bit on %s as zero bit.\n", ref.first.c_str());
|
2017-07-11 10:38:19 -05:00
|
|
|
sig = RTLIL::State::S0;
|
2020-01-13 07:49:31 -06:00
|
|
|
} else if (sig == RTLIL::State::Sz) {
|
|
|
|
continue;
|
2017-07-11 10:38:19 -05:00
|
|
|
} else {
|
|
|
|
for (auto &ref : it.second)
|
2020-01-29 07:07:11 -06:00
|
|
|
log_error("Don't know how to handle %s on %s.\n", log_signal(sig), ref.first.c_str());
|
2017-07-11 10:38:19 -05:00
|
|
|
log_abort();
|
|
|
|
}
|
|
|
|
}
|
2017-02-25 09:28:34 -06:00
|
|
|
std::string netname;
|
|
|
|
if (sig == RTLIL::State::S0)
|
|
|
|
netname = "GND_NET";
|
|
|
|
else if (sig == RTLIL::State::S1)
|
|
|
|
netname = "VCC_NET";
|
|
|
|
else {
|
|
|
|
netname = log_signal(sig);
|
|
|
|
for (size_t i = 0; i < netname.size(); i++)
|
|
|
|
if (netname[i] == ' ' || netname[i] == '\\')
|
|
|
|
netname.erase(netname.begin() + i--);
|
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (net %s (joined\n", EDIF_DEF(netname));
|
2013-08-22 04:34:55 -05:00
|
|
|
for (auto &ref : it.second)
|
2020-04-20 09:00:37 -05:00
|
|
|
*f << stringf(" %s\n", ref.first.c_str());
|
2014-07-24 15:47:57 -05:00
|
|
|
if (sig.wire == NULL) {
|
2016-03-08 14:30:45 -06:00
|
|
|
if (nogndvcc)
|
|
|
|
log_error("Design contains constant nodes (map with \"hilomap\" first).\n");
|
2014-07-24 15:47:57 -05:00
|
|
|
if (sig == RTLIL::State::S0)
|
2019-01-17 06:33:11 -06:00
|
|
|
*f << stringf(" (portRef %c (instanceRef GND))\n", gndvccy ? 'Y' : 'G');
|
2014-07-24 15:47:57 -05:00
|
|
|
if (sig == RTLIL::State::S1)
|
2019-01-17 06:33:11 -06:00
|
|
|
*f << stringf(" (portRef %c (instanceRef VCC))\n", gndvccy ? 'Y' : 'P');
|
2020-09-14 05:43:18 -05:00
|
|
|
}
|
2020-01-10 05:33:58 -06:00
|
|
|
*f << stringf(" )");
|
|
|
|
if (attr_properties && sig.wire != NULL)
|
|
|
|
for (auto &p : sig.wire->attributes)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
*f << stringf("\n )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
2020-01-29 07:07:11 -06:00
|
|
|
if (!wire->get_bool_attribute(ID::keep))
|
|
|
|
continue;
|
2020-04-20 09:00:37 -05:00
|
|
|
|
|
|
|
for(int i = 0; i < wire->width; i++)
|
|
|
|
{
|
2020-01-29 07:07:11 -06:00
|
|
|
SigBit raw_sig = RTLIL::SigSpec(wire, i);
|
|
|
|
SigBit mapped_sig = sigmap(raw_sig);
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2020-01-29 07:07:11 -06:00
|
|
|
if (raw_sig == mapped_sig || net_join_db.count(mapped_sig) == 0)
|
|
|
|
continue;
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2020-01-29 07:07:11 -06:00
|
|
|
std::string netname = log_signal(raw_sig);
|
|
|
|
for (size_t i = 0; i < netname.size(); i++)
|
|
|
|
if (netname[i] == ' ' || netname[i] == '\\')
|
|
|
|
netname.erase(netname.begin() + i--);
|
2020-04-20 09:00:37 -05:00
|
|
|
|
|
|
|
if (keepmode)
|
|
|
|
{
|
|
|
|
*f << stringf(" (net %s (joined\n", EDIF_DEF(netname));
|
|
|
|
|
|
|
|
auto &refs = net_join_db.at(mapped_sig);
|
|
|
|
for (auto &ref : refs)
|
|
|
|
if (ref.second)
|
|
|
|
*f << stringf(" %s\n", ref.first.c_str());
|
|
|
|
*f << stringf(" )");
|
|
|
|
|
|
|
|
if (attr_properties && raw_sig.wire != NULL)
|
|
|
|
for (auto &p : raw_sig.wire->attributes)
|
|
|
|
add_prop(p.first, p.second);
|
|
|
|
|
|
|
|
*f << stringf("\n )\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_warning("Ignoring conflicting 'keep' property on net %s. Use -keep to generate the extra net nevertheless.\n", EDIF_DEF(netname));
|
|
|
|
}
|
2020-01-29 07:07:11 -06:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 09:00:37 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
|
|
|
*f << stringf(" )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(" (design %s\n", EDIF_DEF(top_module_name));
|
|
|
|
*f << stringf(" (cellRef %s (libraryRef DESIGN))\n", EDIF_REF(top_module_name));
|
|
|
|
*f << stringf(" )\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
|
2014-08-23 06:54:21 -05:00
|
|
|
*f << stringf(")\n");
|
2013-08-22 04:34:55 -05:00
|
|
|
}
|
|
|
|
} EdifBackend;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|