yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
blif.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]] Berkeley Logic Interchange Format (BLIF)
21 // University of California. Berkeley. July 28, 1992
22 // http://www.ece.cmu.edu/~ee760/760docs/blif.pdf
23 
24 #include "kernel/rtlil.h"
25 #include "kernel/register.h"
26 #include "kernel/sigtools.h"
27 #include "kernel/celltypes.h"
28 #include "kernel/log.h"
29 #include <string>
30 
33 
35 {
37  bool conn_mode;
39  bool gates_mode;
40  bool param_mode;
42 
43  std::string buf_type, buf_in, buf_out;
44  std::map<RTLIL::IdString, std::pair<RTLIL::IdString, RTLIL::IdString>> unbuf_types;
46 
47  BlifDumperConfig() : icells_mode(false), conn_mode(false), impltf_mode(false), gates_mode(false), param_mode(false), blackbox_mode(false) { }
48 };
49 
50 struct BlifDumper
51 {
52  std::ostream &f;
57 
59  f(f), module(module), design(design), config(config), ct(design)
60  {
61  }
62 
63  std::vector<std::string> cstr_buf;
64 
65  const char *cstr(RTLIL::IdString id)
66  {
67  std::string str = RTLIL::unescape_id(id);
68  for (size_t i = 0; i < str.size(); i++)
69  if (str[i] == '#' || str[i] == '=')
70  str[i] = '?';
71  cstr_buf.push_back(str);
72  return cstr_buf.back().c_str();
73  }
74 
75  const char *cstr(RTLIL::SigBit sig)
76  {
77  if (sig.wire == NULL) {
78  if (sig == RTLIL::State::S0) return config->false_type == "-" ? config->false_out.c_str() : "$false";
79  if (sig == RTLIL::State::S1) return config->true_type == "-" ? config->true_out.c_str() : "$true";
80  return config->undef_type == "-" ? config->undef_out.c_str() : "$undef";
81  }
82 
83  std::string str = RTLIL::unescape_id(sig.wire->name);
84  for (size_t i = 0; i < str.size(); i++)
85  if (str[i] == '#' || str[i] == '=')
86  str[i] = '?';
87 
88  if (sig.wire->width != 1)
89  str += stringf("[%d]", sig.offset);
90 
91  cstr_buf.push_back(str);
92  return cstr_buf.back().c_str();
93  }
94 
95  const char *subckt_or_gate(std::string cell_type)
96  {
97  if (!config->gates_mode)
98  return "subckt";
99  if (!design->modules_.count(RTLIL::escape_id(cell_type)))
100  return "gate";
101  if (design->modules_.at(RTLIL::escape_id(cell_type))->get_bool_attribute("\\blackbox"))
102  return "gate";
103  return "subckt";
104  }
105 
106  void dump()
107  {
108  f << stringf("\n");
109  f << stringf(".model %s\n", cstr(module->name));
110 
111  std::map<int, RTLIL::Wire*> inputs, outputs;
112 
113  for (auto &wire_it : module->wires_) {
114  RTLIL::Wire *wire = wire_it.second;
115  if (wire->port_input)
116  inputs[wire->port_id] = wire;
117  if (wire->port_output)
118  outputs[wire->port_id] = wire;
119  }
120 
121  f << stringf(".inputs");
122  for (auto &it : inputs) {
123  RTLIL::Wire *wire = it.second;
124  for (int i = 0; i < wire->width; i++)
125  f << stringf(" %s", cstr(RTLIL::SigSpec(wire, i)));
126  }
127  f << stringf("\n");
128 
129  f << stringf(".outputs");
130  for (auto &it : outputs) {
131  RTLIL::Wire *wire = it.second;
132  for (int i = 0; i < wire->width; i++)
133  f << stringf(" %s", cstr(RTLIL::SigSpec(wire, i)));
134  }
135  f << stringf("\n");
136 
137  if (module->get_bool_attribute("\\blackbox")) {
138  f << stringf(".blackbox\n");
139  f << stringf(".end\n");
140  return;
141  }
142 
143  if (!config->impltf_mode) {
144  if (!config->false_type.empty()) {
145  if (config->false_type != "-")
146  f << stringf(".%s %s %s=$false\n", subckt_or_gate(config->false_type),
147  config->false_type.c_str(), config->false_out.c_str());
148  } else
149  f << stringf(".names $false\n");
150  if (!config->true_type.empty()) {
151  if (config->true_type != "-")
152  f << stringf(".%s %s %s=$true\n", subckt_or_gate(config->true_type),
153  config->true_type.c_str(), config->true_out.c_str());
154  } else
155  f << stringf(".names $true\n1\n");
156  if (!config->undef_type.empty()) {
157  if (config->undef_type != "-")
158  f << stringf(".%s %s %s=$undef\n", subckt_or_gate(config->undef_type),
159  config->undef_type.c_str(), config->undef_out.c_str());
160  } else
161  f << stringf(".names $undef\n");
162  }
163 
164  for (auto &cell_it : module->cells_)
165  {
166  RTLIL::Cell *cell = cell_it.second;
167 
168  if (config->unbuf_types.count(cell->type)) {
169  auto portnames = config->unbuf_types.at(cell->type);
170  f << stringf(".names %s %s\n1 1\n",
171  cstr(cell->getPort(portnames.first)), cstr(cell->getPort(portnames.second)));
172  continue;
173  }
174 
175  if (!config->icells_mode && cell->type == "$_NOT_") {
176  f << stringf(".names %s %s\n0 1\n",
177  cstr(cell->getPort("\\A")), cstr(cell->getPort("\\Y")));
178  continue;
179  }
180 
181  if (!config->icells_mode && cell->type == "$_AND_") {
182  f << stringf(".names %s %s %s\n11 1\n",
183  cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
184  continue;
185  }
186 
187  if (!config->icells_mode && cell->type == "$_OR_") {
188  f << stringf(".names %s %s %s\n1- 1\n-1 1\n",
189  cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
190  continue;
191  }
192 
193  if (!config->icells_mode && cell->type == "$_XOR_") {
194  f << stringf(".names %s %s %s\n10 1\n01 1\n",
195  cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")), cstr(cell->getPort("\\Y")));
196  continue;
197  }
198 
199  if (!config->icells_mode && cell->type == "$_MUX_") {
200  f << stringf(".names %s %s %s %s\n1-0 1\n-11 1\n",
201  cstr(cell->getPort("\\A")), cstr(cell->getPort("\\B")),
202  cstr(cell->getPort("\\S")), cstr(cell->getPort("\\Y")));
203  continue;
204  }
205 
206  if (!config->icells_mode && cell->type == "$_DFF_N_") {
207  f << stringf(".latch %s %s fe %s\n",
208  cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")), cstr(cell->getPort("\\C")));
209  continue;
210  }
211 
212  if (!config->icells_mode && cell->type == "$_DFF_P_") {
213  f << stringf(".latch %s %s re %s\n",
214  cstr(cell->getPort("\\D")), cstr(cell->getPort("\\Q")), cstr(cell->getPort("\\C")));
215  continue;
216  }
217 
218  if (!config->icells_mode && cell->type == "$lut") {
219  f << stringf(".names");
220  auto &inputs = cell->getPort("\\A");
221  auto width = cell->parameters.at("\\WIDTH").as_int();
222  log_assert(inputs.size() == width);
223  for (int i = 0; i < inputs.size(); i++) {
224  f << stringf(" %s", cstr(inputs.extract(i, 1)));
225  }
226  auto &output = cell->getPort("\\Y");
227  log_assert(output.size() == 1);
228  f << stringf(" %s", cstr(output));
229  f << stringf("\n");
230  auto mask = cell->parameters.at("\\LUT").as_string();
231  for (int i = 0; i < (1 << width); i++) {
232  if (mask[i] == '0') continue;
233  for (int j = width-1; j >= 0; j--) {
234  f << ((i>>j)&1 ? '1' : '0');
235  }
236  f << stringf(" %c\n", mask[i]);
237  }
238  continue;
239  }
240 
241  f << stringf(".%s %s", subckt_or_gate(cell->type.str()), cstr(cell->type));
242  for (auto &conn : cell->connections())
243  for (int i = 0; i < conn.second.size(); i++) {
244  if (conn.second.size() == 1)
245  f << stringf(" %s", cstr(conn.first));
246  else
247  f << stringf(" %s[%d]", cstr(conn.first), i);
248  f << stringf("=%s", cstr(conn.second.extract(i, 1)));
249  }
250  f << stringf("\n");
251 
252  if (config->param_mode)
253  for (auto &param : cell->parameters) {
254  f << stringf(".param %s ", RTLIL::id2cstr(param.first));
255  if (param.second.flags & RTLIL::CONST_FLAG_STRING) {
256  std::string str = param.second.decode_string();
257  f << stringf("\"");
258  for (char ch : str)
259  if (ch == '"' || ch == '\\')
260  f << stringf("\\%c", ch);
261  else if (ch < 32 || ch >= 127)
262  f << stringf("\\%03o", ch);
263  else
264  f << stringf("%c", ch);
265  f << stringf("\"\n");
266  } else
267  f << stringf("%s\n", param.second.as_string().c_str());
268  }
269  }
270 
271  for (auto &conn : module->connections())
272  for (int i = 0; i < conn.first.size(); i++)
273  if (config->conn_mode)
274  f << stringf(".conn %s %s\n", cstr(conn.second.extract(i, 1)), cstr(conn.first.extract(i, 1)));
275  else if (!config->buf_type.empty())
276  f << stringf(".%s %s %s=%s %s=%s\n", subckt_or_gate(config->buf_type), config->buf_type.c_str(), config->buf_in.c_str(), cstr(conn.second.extract(i, 1)),
277  config->buf_out.c_str(), cstr(conn.first.extract(i, 1)));
278  else
279  f << stringf(".names %s %s\n1 1\n", cstr(conn.second.extract(i, 1)), cstr(conn.first.extract(i, 1)));
280 
281 
282  f << stringf(".end\n");
283  }
284 
286  {
287  BlifDumper dumper(f, module, design, &config);
288  dumper.dump();
289  }
290 };
291 
292 struct BlifBackend : public Backend {
293  BlifBackend() : Backend("blif", "write design to BLIF file") { }
294  virtual void help()
295  {
296  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
297  log("\n");
298  log(" write_blif [options] [filename]\n");
299  log("\n");
300  log("Write the current design to an BLIF file.\n");
301  log("\n");
302  log(" -top top_module\n");
303  log(" set the specified module as design top module\n");
304  log("\n");
305  log(" -buf <cell-type> <in-port> <out-port>\n");
306  log(" use cells of type <cell-type> with the specified port names for buffers\n");
307  log("\n");
308  log(" -unbuf <cell-type> <in-port> <out-port>\n");
309  log(" replace buffer cells with the specified name and port names with\n");
310  log(" a .names statement that models a buffer\n");
311  log("\n");
312  log(" -true <cell-type> <out-port>\n");
313  log(" -false <cell-type> <out-port>\n");
314  log(" -undef <cell-type> <out-port>\n");
315  log(" use the specified cell types to drive nets that are constant 1, 0, or\n");
316  log(" undefined. when '-' is used as <cell-type>, then <out-port> specifies\n");
317  log(" the wire name to be used for the constant signal and no cell driving\n");
318  log(" that wire is generated.\n");
319  log("\n");
320  log("The following options can be useful when the generated file is not going to be\n");
321  log("read by a BLIF parser but a custom tool. It is recommended to not name the output\n");
322  log("file *.blif when any of this options is used.\n");
323  log("\n");
324  log(" -icells\n");
325  log(" do not translate Yosys's internal gates to generic BLIF logic\n");
326  log(" functions. Instead create .subckt or .gate lines for all cells.\n");
327  log("\n");
328  log(" -gates\n");
329  log(" print .gate instead of .subckt lines for all cells that are not\n");
330  log(" instantiations of other modules from this design.\n");
331  log("\n");
332  log(" -conn\n");
333  log(" do not generate buffers for connected wires. instead use the\n");
334  log(" non-standard .conn statement.\n");
335  log("\n");
336  log(" -param\n");
337  log(" use the non-standard .param statement to write module parameters\n");
338  log("\n");
339  log(" -blackbox\n");
340  log(" write blackbox cells with .blackbox statement.\n");
341  log("\n");
342  log(" -impltf\n");
343  log(" do not write definitions for the $true, $false and $undef wires.\n");
344  log("\n");
345  }
346  virtual void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
347  {
348  std::string top_module_name;
349  std::string buf_type, buf_in, buf_out;
350  std::string true_type, true_out;
351  std::string false_type, false_out;
352  BlifDumperConfig config;
353 
354  log_header("Executing BLIF backend.\n");
355 
356  size_t argidx;
357  for (argidx = 1; argidx < args.size(); argidx++)
358  {
359  if (args[argidx] == "-top" && argidx+1 < args.size()) {
360  top_module_name = args[++argidx];
361  continue;
362  }
363  if (args[argidx] == "-buf" && argidx+3 < args.size()) {
364  config.buf_type = args[++argidx];
365  config.buf_in = args[++argidx];
366  config.buf_out = args[++argidx];
367  continue;
368  }
369  if (args[argidx] == "-unbuf" && argidx+3 < args.size()) {
370  RTLIL::IdString unbuf_type = RTLIL::escape_id(args[++argidx]);
371  RTLIL::IdString unbuf_in = RTLIL::escape_id(args[++argidx]);
372  RTLIL::IdString unbuf_out = RTLIL::escape_id(args[++argidx]);
373  config.unbuf_types[unbuf_type] = std::pair<RTLIL::IdString, RTLIL::IdString>(unbuf_in, unbuf_out);
374  continue;
375  }
376  if (args[argidx] == "-true" && argidx+2 < args.size()) {
377  config.true_type = args[++argidx];
378  config.true_out = args[++argidx];
379  continue;
380  }
381  if (args[argidx] == "-false" && argidx+2 < args.size()) {
382  config.false_type = args[++argidx];
383  config.false_out = args[++argidx];
384  continue;
385  }
386  if (args[argidx] == "-undef" && argidx+2 < args.size()) {
387  config.undef_type = args[++argidx];
388  config.undef_out = args[++argidx];
389  continue;
390  }
391  if (args[argidx] == "-icells") {
392  config.icells_mode = true;
393  continue;
394  }
395  if (args[argidx] == "-gates") {
396  config.gates_mode = true;
397  continue;
398  }
399  if (args[argidx] == "-conn") {
400  config.conn_mode = true;
401  continue;
402  }
403  if (args[argidx] == "-param") {
404  config.param_mode = true;
405  continue;
406  }
407  if (args[argidx] == "-blackbox") {
408  config.blackbox_mode = true;
409  continue;
410  }
411  if (args[argidx] == "-impltf") {
412  config.impltf_mode = true;
413  continue;
414  }
415  break;
416  }
417  extra_args(f, filename, args, argidx);
418 
419  if (top_module_name.empty())
420  for (auto & mod_it:design->modules_)
421  if (mod_it.second->get_bool_attribute("\\top"))
422  top_module_name = mod_it.first.str();
423 
424  *f << stringf("# Generated by %s\n", yosys_version_str);
425 
426  std::vector<RTLIL::Module*> mod_list;
427 
428  for (auto module_it : design->modules_)
429  {
430  RTLIL::Module *module = module_it.second;
431  if (module->get_bool_attribute("\\blackbox") && !config.blackbox_mode)
432  continue;
433 
434  if (module->processes.size() != 0)
435  log_error("Found unmapped processes in module %s: unmapped processes are not supported in BLIF backend!\n", RTLIL::id2cstr(module->name));
436  if (module->memories.size() != 0)
437  log_error("Found munmapped emories in module %s: unmapped memories are not supported in BLIF backend!\n", RTLIL::id2cstr(module->name));
438 
439  if (module->name == RTLIL::escape_id(top_module_name)) {
440  BlifDumper::dump(*f, module, design, config);
441  top_module_name.clear();
442  continue;
443  }
444 
445  mod_list.push_back(module);
446  }
447 
448  if (!top_module_name.empty())
449  log_error("Can't find top module `%s'!\n", top_module_name.c_str());
450 
451  for (auto module : mod_list)
452  BlifDumper::dump(*f, module, design, config);
453  }
454 } BlifBackend;
455 
const char * yosys_version_str
RTLIL::Wire * wire
Definition: rtlil.h:907
std::map< RTLIL::IdString, std::pair< RTLIL::IdString, RTLIL::IdString > > unbuf_types
Definition: blif.cc:44
std::string str() const
Definition: rtlil.h:182
std::string undef_out
Definition: blif.cc:45
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
static void dump(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, BlifDumperConfig &config)
Definition: blif.cc:285
BlifDumperConfig * config
Definition: blif.cc:55
bool impltf_mode
Definition: blif.cc:38
std::string true_out
Definition: blif.cc:45
bool conn_mode
Definition: blif.cc:37
void log_header(const char *format,...)
Definition: log.cc:188
const std::vector< RTLIL::SigSig > & connections() const
Definition: rtlil.cc:1307
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
bool port_input
Definition: rtlil.h:827
int width
Definition: rtlil.h:826
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
std::vector< std::string > cstr_buf
Definition: blif.cc:63
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
std::ostream & f
Definition: blif.cc:52
std::string true_type
Definition: blif.cc:45
int port_id
Definition: rtlil.h:826
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
CellTypes ct
Definition: blif.cc:56
int offset
Definition: rtlil.h:910
bool param_mode
Definition: blif.cc:40
static std::string escape_id(std::string str)
Definition: rtlil.h:251
bool port_output
Definition: rtlil.h:827
bool blackbox_mode
Definition: blif.cc:41
bool icells_mode
Definition: blif.cc:36
void extra_args(std::ostream *&f, std::string &filename, std::vector< std::string > args, size_t argidx)
Definition: register.cc:439
void dump()
Definition: blif.cc:106
BlifDumperConfig()
Definition: blif.cc:47
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
virtual void execute(std::ostream *&f, std::string filename, std::vector< std::string > args, RTLIL::Design *design)
Definition: blif.cc:346
RTLIL::Design * design
Definition: blif.cc:54
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
BlifBackend()
Definition: blif.cc:293
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::IdString name
Definition: rtlil.h:599
std::string false_type
Definition: blif.cc:45
BlifDumper(std::ostream &f, RTLIL::Module *module, RTLIL::Design *design, BlifDumperConfig *config)
Definition: blif.cc:58
const char * cstr(RTLIL::SigBit sig)
Definition: blif.cc:75
RTLIL::Module * module
Definition: blif.cc:53
#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::string false_out
Definition: blif.cc:45
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
BlifBackend BlifBackend
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
std::string buf_out
Definition: blif.cc:43
const char * subckt_or_gate(std::string cell_type)
Definition: blif.cc:95
void log(const char *format,...)
Definition: log.cc:180
std::string undef_type
Definition: blif.cc:45
std::string buf_in
Definition: blif.cc:43
bool gates_mode
Definition: blif.cc:39
const char * cstr(RTLIL::IdString id)
Definition: blif.cc:65
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
virtual void help()
Definition: blif.cc:294
std::string buf_type
Definition: blif.cc:43