yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
edif.cc
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  */
19 
20 // [[CITE]] EDIF Version 2 0 0 Grammar
21 // http://web.archive.org/web/20050730021644/http://www.edif.org/documentation/BNF_GRAMMAR/index.html
22 
23 #include "kernel/rtlil.h"
24 #include "kernel/register.h"
25 #include "kernel/sigtools.h"
26 #include "kernel/celltypes.h"
27 #include "kernel/log.h"
28 #include <string>
29 
32 
33 #define EDIF_DEF(_id) edif_names(RTLIL::unescape_id(_id), true).c_str()
34 #define EDIF_REF(_id) edif_names(RTLIL::unescape_id(_id), false).c_str()
35 
36 namespace
37 {
38  struct EdifNames
39  {
40  int counter;
41  std::set<std::string> generated_names, used_names;
42  std::map<std::string, std::string> name_map;
43 
44  EdifNames() : counter(1) { }
45 
46  std::string operator()(std::string id, bool define)
47  {
48  if (define) {
49  std::string new_id = operator()(id, false);
50  return new_id != id ? stringf("(rename %s \"%s\")", new_id.c_str(), id.c_str()) : id;
51  }
52 
53  if (name_map.count(id) > 0)
54  return name_map.at(id);
55  if (generated_names.count(id) > 0)
56  goto do_rename;
57  if (id == "GND" || id == "VCC")
58  goto do_rename;
59 
60  for (size_t i = 0; i < id.size(); i++) {
61  if ('A' <= id[i] && id[i] <= 'Z')
62  continue;
63  if ('a' <= id[i] && id[i] <= 'z')
64  continue;
65  if ('0' <= id[i] && id[i] <= '9' && i > 0)
66  continue;
67  if (id[i] == '_' && i > 0 && i != id.size()-1)
68  continue;
69  goto do_rename;
70  }
71 
72  used_names.insert(id);
73  return id;
74 
75  do_rename:;
76  std::string gen_name;
77  while (1) {
78  gen_name = stringf("id%05d", counter++);
79  if (generated_names.count(gen_name) == 0 &&
80  used_names.count(gen_name) == 0)
81  break;
82  }
83  generated_names.insert(gen_name);
84  name_map[id] = gen_name;
85  return gen_name;
86  }
87  };
88 }
89 
90 struct EdifBackend : public Backend {
91  EdifBackend() : Backend("edif", "write design to EDIF netlist file") { }
92  virtual void help()
93  {
94  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
95  log("\n");
96  log(" write_edif [options] [filename]\n");
97  log("\n");
98  log("Write the current design to an EDIF netlist file.\n");
99  log("\n");
100  log(" -top top_module\n");
101  log(" set the specified module as design top module\n");
102  log("\n");
103  log("Unfortunately there are different \"flavors\" of the EDIF file format. This\n");
104  log("command generates EDIF files for the Xilinx place&route tools. It might be\n");
105  log("necessary to make small modifications to this command when a different tool\n");
106  log("is targeted.\n");
107  log("\n");
108  }
109  virtual void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
110  {
111  log_header("Executing EDIF backend.\n");
112 
113  std::string top_module_name;
114  std::map<RTLIL::IdString, std::set<RTLIL::IdString>> lib_cell_ports;
115  CellTypes ct(design);
116  EdifNames edif_names;
117 
118  size_t argidx;
119  for (argidx = 1; argidx < args.size(); argidx++)
120  {
121  if (args[argidx] == "-top" && argidx+1 < args.size()) {
122  top_module_name = args[++argidx];
123  continue;
124  }
125  break;
126  }
127  extra_args(f, filename, args, argidx);
128 
129  if (top_module_name.empty())
130  for (auto & mod_it:design->modules_)
131  if (mod_it.second->get_bool_attribute("\\top"))
132  top_module_name = mod_it.first.str();
133 
134  for (auto module_it : design->modules_)
135  {
136  RTLIL::Module *module = module_it.second;
137  if (module->get_bool_attribute("\\blackbox"))
138  continue;
139 
140  if (top_module_name.empty())
141  top_module_name = module->name.str();
142 
143  if (module->processes.size() != 0)
144  log_error("Found unmapped processes in module %s: unmapped processes are not supported in EDIF backend!\n", RTLIL::id2cstr(module->name));
145  if (module->memories.size() != 0)
146  log_error("Found munmapped emories in module %s: unmapped memories are not supported in EDIF backend!\n", RTLIL::id2cstr(module->name));
147 
148  for (auto cell_it : module->cells_)
149  {
150  RTLIL::Cell *cell = cell_it.second;
151  if (!design->modules_.count(cell->type) || design->modules_.at(cell->type)->get_bool_attribute("\\blackbox")) {
152  lib_cell_ports[cell->type];
153  for (auto p : cell->connections()) {
154  if (p.second.size() > 1)
155  log_error("Found multi-bit port %s on library cell %s.%s (%s): not supported in EDIF backend!\n",
156  RTLIL::id2cstr(p.first), RTLIL::id2cstr(module->name), RTLIL::id2cstr(cell->name), RTLIL::id2cstr(cell->type));
157  lib_cell_ports[cell->type].insert(p.first);
158  }
159  }
160  }
161  }
162 
163  if (top_module_name.empty())
164  log_error("No module found in design!\n");
165 
166  *f << stringf("(edif %s\n", EDIF_DEF(top_module_name));
167  *f << stringf(" (edifVersion 2 0 0)\n");
168  *f << stringf(" (edifLevel 0)\n");
169  *f << stringf(" (keywordMap (keywordLevel 0))\n");
170  *f << stringf(" (comment \"Generated by %s\")\n", yosys_version_str);
171 
172  *f << stringf(" (external LIB\n");
173  *f << stringf(" (edifLevel 0)\n");
174  *f << stringf(" (technology (numberDefinition))\n");
175 
176  *f << stringf(" (cell GND\n");
177  *f << stringf(" (cellType GENERIC)\n");
178  *f << stringf(" (view VIEW_NETLIST\n");
179  *f << stringf(" (viewType NETLIST)\n");
180  *f << stringf(" (interface (port G (direction OUTPUT)))\n");
181  *f << stringf(" )\n");
182  *f << stringf(" )\n");
183 
184  *f << stringf(" (cell VCC\n");
185  *f << stringf(" (cellType GENERIC)\n");
186  *f << stringf(" (view VIEW_NETLIST\n");
187  *f << stringf(" (viewType NETLIST)\n");
188  *f << stringf(" (interface (port P (direction OUTPUT)))\n");
189  *f << stringf(" )\n");
190  *f << stringf(" )\n");
191 
192  for (auto &cell_it : lib_cell_ports) {
193  *f << stringf(" (cell %s\n", EDIF_DEF(cell_it.first));
194  *f << stringf(" (cellType GENERIC)\n");
195  *f << stringf(" (view VIEW_NETLIST\n");
196  *f << stringf(" (viewType NETLIST)\n");
197  *f << stringf(" (interface\n");
198  for (auto &port_it : cell_it.second) {
199  const char *dir = "INOUT";
200  if (ct.cell_known(cell_it.first)) {
201  if (!ct.cell_output(cell_it.first, port_it))
202  dir = "INPUT";
203  else if (!ct.cell_input(cell_it.first, port_it))
204  dir = "OUTPUT";
205  }
206  *f << stringf(" (port %s (direction %s))\n", EDIF_DEF(port_it), dir);
207  }
208  *f << stringf(" )\n");
209  *f << stringf(" )\n");
210  *f << stringf(" )\n");
211  }
212  *f << stringf(" )\n");
213 
214  std::vector<RTLIL::Module*> sorted_modules;
215 
216  // extract module dependencies
217  std::map<RTLIL::Module*, std::set<RTLIL::Module*>> module_deps;
218  for (auto &mod_it : design->modules_) {
219  module_deps[mod_it.second] = std::set<RTLIL::Module*>();
220  for (auto &cell_it : mod_it.second->cells_)
221  if (design->modules_.count(cell_it.second->type) > 0)
222  module_deps[mod_it.second].insert(design->modules_.at(cell_it.second->type));
223  }
224 
225  // simple good-enough topological sort
226  // (O(n*m) on n elements and depth m)
227  while (module_deps.size() > 0) {
228  size_t sorted_modules_idx = sorted_modules.size();
229  for (auto &it : module_deps) {
230  for (auto &dep : it.second)
231  if (module_deps.count(dep) > 0)
232  goto not_ready_yet;
233  // log("Next in topological sort: %s\n", RTLIL::id2cstr(it.first->name));
234  sorted_modules.push_back(it.first);
235  not_ready_yet:;
236  }
237  if (sorted_modules_idx == sorted_modules.size())
238  log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", RTLIL::id2cstr(module_deps.begin()->first->name));
239  while (sorted_modules_idx < sorted_modules.size())
240  module_deps.erase(sorted_modules.at(sorted_modules_idx++));
241  }
242 
243 
244  *f << stringf(" (library DESIGN\n");
245  *f << stringf(" (edifLevel 0)\n");
246  *f << stringf(" (technology (numberDefinition))\n");
247  for (auto module : sorted_modules)
248  {
249  if (module->get_bool_attribute("\\blackbox"))
250  continue;
251 
252  SigMap sigmap(module);
253  std::map<RTLIL::SigSpec, std::set<std::string>> net_join_db;
254 
255  *f << stringf(" (cell %s\n", EDIF_DEF(module->name));
256  *f << stringf(" (cellType GENERIC)\n");
257  *f << stringf(" (view VIEW_NETLIST\n");
258  *f << stringf(" (viewType NETLIST)\n");
259  *f << stringf(" (interface\n");
260  for (auto &wire_it : module->wires_) {
261  RTLIL::Wire *wire = wire_it.second;
262  if (wire->port_id == 0)
263  continue;
264  const char *dir = "INOUT";
265  if (!wire->port_output)
266  dir = "INPUT";
267  else if (!wire->port_input)
268  dir = "OUTPUT";
269  if (wire->width == 1) {
270  *f << stringf(" (port %s (direction %s))\n", EDIF_DEF(wire->name), dir);
271  RTLIL::SigSpec sig = sigmap(RTLIL::SigSpec(wire));
272  net_join_db[sig].insert(stringf("(portRef %s)", EDIF_REF(wire->name)));
273  } else {
274  *f << stringf(" (port (array %s %d) (direction %s))\n", EDIF_DEF(wire->name), wire->width, dir);
275  for (int i = 0; i < wire->width; i++) {
276  RTLIL::SigSpec sig = sigmap(RTLIL::SigSpec(wire, i));
277  net_join_db[sig].insert(stringf("(portRef (member %s %d))", EDIF_REF(wire->name), i));
278  }
279  }
280  }
281  *f << stringf(" )\n");
282  *f << stringf(" (contents\n");
283  *f << stringf(" (instance GND (viewRef VIEW_NETLIST (cellRef GND (libraryRef LIB))))\n");
284  *f << stringf(" (instance VCC (viewRef VIEW_NETLIST (cellRef VCC (libraryRef LIB))))\n");
285  for (auto &cell_it : module->cells_) {
286  RTLIL::Cell *cell = cell_it.second;
287  *f << stringf(" (instance %s\n", EDIF_DEF(cell->name));
288  *f << stringf(" (viewRef VIEW_NETLIST (cellRef %s%s))", EDIF_REF(cell->type),
289  lib_cell_ports.count(cell->type) > 0 ? " (libraryRef LIB)" : "");
290  for (auto &p : cell->parameters)
291  if ((p.second.flags & RTLIL::CONST_FLAG_STRING) != 0)
292  *f << stringf("\n (property %s (string \"%s\"))", EDIF_DEF(p.first), p.second.decode_string().c_str());
293  else if (p.second.bits.size() <= 32 && RTLIL::SigSpec(p.second).is_fully_def())
294  *f << stringf("\n (property %s (integer %u))", EDIF_DEF(p.first), p.second.as_int());
295  else {
296  std::string hex_string = "";
297  for (size_t i = 0; i < p.second.bits.size(); i += 4) {
298  int digit_value = 0;
299  if (i+0 < p.second.bits.size() && p.second.bits.at(i+0) == RTLIL::State::S1) digit_value |= 1;
300  if (i+1 < p.second.bits.size() && p.second.bits.at(i+1) == RTLIL::State::S1) digit_value |= 2;
301  if (i+2 < p.second.bits.size() && p.second.bits.at(i+2) == RTLIL::State::S1) digit_value |= 4;
302  if (i+3 < p.second.bits.size() && p.second.bits.at(i+3) == RTLIL::State::S1) digit_value |= 8;
303  char digit_str[2] = { "0123456789abcdef"[digit_value], 0 };
304  hex_string = std::string(digit_str) + hex_string;
305  }
306  *f << stringf("\n (property %s (string \"%s\"))", EDIF_DEF(p.first), hex_string.c_str());
307  }
308  *f << stringf(")\n");
309  for (auto &p : cell->connections()) {
310  RTLIL::SigSpec sig = sigmap(p.second);
311  for (int i = 0; i < GetSize(sig); i++)
312  if (sig.size() == 1)
313  net_join_db[sig[i]].insert(stringf("(portRef %s (instanceRef %s))", EDIF_REF(p.first), EDIF_REF(cell->name)));
314  else
315  net_join_db[sig[i]].insert(stringf("(portRef (member %s %d) (instanceRef %s))", EDIF_REF(p.first), i, EDIF_REF(cell->name)));
316  }
317  }
318  for (auto &it : net_join_db) {
319  RTLIL::SigBit sig = it.first;
320  if (sig.wire == NULL && sig != RTLIL::State::S0 && sig != RTLIL::State::S1)
321  continue;
322  std::string netname = log_signal(sig);
323  for (size_t i = 0; i < netname.size(); i++)
324  if (netname[i] == ' ' || netname[i] == '\\')
325  netname.erase(netname.begin() + i--);
326  *f << stringf(" (net %s (joined\n", EDIF_DEF(netname));
327  for (auto &ref : it.second)
328  *f << stringf(" %s\n", ref.c_str());
329  if (sig.wire == NULL) {
330  if (sig == RTLIL::State::S0)
331  *f << stringf(" (portRef G (instanceRef GND))\n");
332  if (sig == RTLIL::State::S1)
333  *f << stringf(" (portRef P (instanceRef VCC))\n");
334  }
335  *f << stringf(" ))\n");
336  }
337  *f << stringf(" )\n");
338  *f << stringf(" )\n");
339  *f << stringf(" )\n");
340  }
341  *f << stringf(" )\n");
342 
343  *f << stringf(" (design %s\n", EDIF_DEF(top_module_name));
344  *f << stringf(" (cellRef %s (libraryRef DESIGN))\n", EDIF_REF(top_module_name));
345  *f << stringf(" )\n");
346 
347  *f << stringf(")\n");
348  }
349 } EdifBackend;
350 
const char * yosys_version_str
virtual void execute(std::ostream *&f, std::string filename, std::vector< std::string > args, RTLIL::Design *design)
Definition: edif.cc:109
RTLIL::Wire * wire
Definition: rtlil.h:907
bool is_fully_def() const
Definition: rtlil.cc:2774
std::string str() const
Definition: rtlil.h:182
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
void log_header(const char *format,...)
Definition: log.cc:188
CellTypes ct
Definition: opt_clean.cc:33
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
RTLIL::IdString name
Definition: rtlil.h:853
bool port_input
Definition: rtlil.h:827
int width
Definition: rtlil.h:826
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
int port_id
Definition: rtlil.h:826
RTLIL::IdString type
Definition: rtlil.h:854
RTLIL::IdString new_id(std::string file, int line, std::string func)
Definition: yosys.cc:378
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
int size() const
Definition: rtlil.h:1019
bool port_output
Definition: rtlil.h:827
void extra_args(std::ostream *&f, std::string &filename, std::vector< std::string > args, size_t argidx)
Definition: register.cc:439
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
#define EDIF_REF(_id)
Definition: edif.cc:34
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::IdString name
Definition: rtlil.h:599
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
RTLIL::IdString name
Definition: rtlil.h:825
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN std::string netname(std::set< std::string > &conntypes_code, std::set< std::string > &celltypes_code, std::set< std::string > &constcells_code, RTLIL::SigSpec sig)
Definition: intersynth.cc:30
EdifBackend EdifBackend
virtual void help()
Definition: edif.cc:92
void log(const char *format,...)
Definition: log.cc:180
bool cell_input(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:199
std::string id(RTLIL::IdString internal_id, bool may_rename=true)
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
EdifBackend()
Definition: edif.cc:91
#define EDIF_DEF(_id)
Definition: edif.cc:33