yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
show.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 #include "kernel/register.h"
21 #include "kernel/celltypes.h"
22 #include "kernel/log.h"
23 #include <string.h>
24 
25 #ifndef _WIN32
26 # include <dirent.h>
27 #endif
28 
29 #ifdef YOSYS_ENABLE_READLINE
30 # include <readline/readline.h>
31 #endif
32 
35 
36 using RTLIL::id2cstr;
37 
38 #undef CLUSTER_CELLS_AND_PORTBOXES
39 
40 struct ShowWorker
41 {
43 
44  std::vector<std::string> dot_escape_store;
45  std::map<RTLIL::IdString, int> dot_id2num_store;
46  std::map<RTLIL::IdString, int> autonames;
48 
49  struct net_conn { std::set<std::string> in, out; int bits; std::string color; };
50  std::map<std::string, net_conn> net_conn_map;
51 
52  FILE *f;
55  uint32_t currentColor;
58  bool stretchIO;
61  bool notitle;
63 
64  const std::vector<std::pair<std::string, RTLIL::Selection>> &color_selections;
65  const std::vector<std::pair<std::string, RTLIL::Selection>> &label_selections;
66 
67  static uint32_t xorshift32(uint32_t x) {
68  x ^= x << 13;
69  x ^= x >> 17;
70  x ^= x << 5;
71  return x;
72  }
73 
74  std::string nextColor()
75  {
76  if (currentColor == 0)
77  return "color=\"black\"";
78  return stringf("colorscheme=\"dark28\", color=\"%d\", fontcolor=\"%d\"", currentColor%8+1, currentColor%8+1);
79  }
80 
81  std::string nextColor(std::string presetColor)
82  {
83  if (presetColor.empty())
84  return nextColor();
85  return presetColor;
86  }
87 
88  std::string nextColor(RTLIL::SigSpec sig, std::string defaultColor)
89  {
90  sig.sort_and_unify();
91  for (auto &c : sig.chunks()) {
92  if (c.wire != NULL)
93  for (auto &s : color_selections)
94  if (s.second.selected_members.count(module->name) > 0 && s.second.selected_members.at(module->name).count(c.wire->name) > 0)
95  return stringf("color=\"%s\"", s.first.c_str());
96  }
97  return defaultColor;
98  }
99 
100  std::string nextColor(const RTLIL::SigSig &conn, std::string defaultColor)
101  {
102  return nextColor(conn.first, nextColor(conn.second, defaultColor));
103  }
104 
105  std::string nextColor(const RTLIL::SigSpec &sig)
106  {
107  return nextColor(sig, nextColor());
108  }
109 
110  std::string nextColor(const RTLIL::SigSig &conn)
111  {
112  return nextColor(conn, nextColor());
113  }
114 
115  std::string widthLabel(int bits)
116  {
117  if (bits <= 1)
118  return "label=\"\"";
119  if (!genWidthLabels)
120  return "style=\"setlinewidth(3)\", label=\"\"";
121  return stringf("style=\"setlinewidth(3)\", label=\"<%d>\"", bits);
122  }
123 
124  const char *findColor(std::string member_name)
125  {
126  for (auto &s : color_selections)
127  if (s.second.selected_member(module->name, member_name)) {
128  dot_escape_store.push_back(stringf(", color=\"%s\"", s.first.c_str()));
129  return dot_escape_store.back().c_str();
130  }
131  return "";
132  }
133 
134  const char *findLabel(std::string member_name)
135  {
136  for (auto &s : label_selections)
137  if (s.second.selected_member(module->name, member_name))
138  return escape(s.first);
139  return escape(member_name, true);
140  }
141 
142  const char *escape(std::string id, bool is_name = false)
143  {
144  if (id.size() == 0)
145  return "";
146 
147  if (id[0] == '$' && is_name) {
148  if (enumerateIds) {
149  if (autonames.count(id) == 0) {
150  autonames[id] = autonames.size() + 1;
151  log("Generated short name for internal identifier: _%d_ -> %s\n", autonames[id], id.c_str());
152  }
153  id = stringf("_%d_", autonames[id]);
154  } else if (abbreviateIds) {
155  const char *p = id.c_str();
156  const char *q = strrchr(p, '$');
157  id = std::string(q);
158  }
159  }
160 
161  if (id[0] == '\\')
162  id = id.substr(1);
163 
164  std::string str;
165  for (char ch : id) {
166  if (ch == '\\' || ch == '"')
167  str += "\\";
168  str += ch;
169  }
170 
171  dot_escape_store.push_back(str);
172  return dot_escape_store.back().c_str();
173  }
174 
176  {
177  if (dot_id2num_store.count(id) > 0)
178  return dot_id2num_store[id];
179  return dot_id2num_store[id] = dot_id2num_store.size() + 1;
180  }
181 
182  std::string gen_signode_simple(RTLIL::SigSpec sig, bool range_check = true)
183  {
184  if (GetSize(sig) == 0) {
185  fprintf(f, "v%d [ label=\"\" ];\n", single_idx_count);
186  return stringf("v%d", single_idx_count++);
187  }
188 
189  if (sig.is_chunk()) {
190  const RTLIL::SigChunk &c = sig.as_chunk();
191  if (c.wire != NULL && design->selected_member(module->name, c.wire->name)) {
192  if (!range_check || c.wire->width == c.width)
193  return stringf("n%d", id2num(c.wire->name));
194  } else {
195  fprintf(f, "v%d [ label=\"%s\" ];\n", single_idx_count, findLabel(log_signal(c)));
196  return stringf("v%d", single_idx_count++);
197  }
198  }
199 
200  return std::string();
201  }
202 
203  std::string gen_portbox(std::string port, RTLIL::SigSpec sig, bool driver, std::string *node = NULL)
204  {
205  std::string code;
206  std::string net = gen_signode_simple(sig);
207  if (net.empty())
208  {
209  std::string label_string;
210  int pos = sig.size()-1;
211  int idx = single_idx_count++;
212  for (int rep, i = int(sig.chunks().size())-1; i >= 0; i -= rep) {
213  const RTLIL::SigChunk &c = sig.chunks().at(i);
214  net = gen_signode_simple(c, false);
215  log_assert(!net.empty());
216  for (rep = 1; i-rep >= 0 && c == sig.chunks().at(i-rep); rep++) {}
217  std::string repinfo = rep > 1 ? stringf("%dx ", rep) : "";
218  if (driver) {
219  label_string += stringf("<s%d> %d:%d - %s%d:%d |", i, pos, pos-c.width+1, repinfo.c_str(), c.offset+c.width-1, c.offset);
220  net_conn_map[net].in.insert(stringf("x%d:s%d", idx, i));
221  net_conn_map[net].bits = rep*c.width;
222  net_conn_map[net].color = nextColor(c, net_conn_map[net].color);
223  } else {
224  label_string += stringf("<s%d> %s%d:%d - %d:%d |", i, repinfo.c_str(), c.offset+c.width-1, c.offset, pos, pos-rep*c.width+1);
225  net_conn_map[net].out.insert(stringf("x%d:s%d", idx, i));
226  net_conn_map[net].bits = rep*c.width;
227  net_conn_map[net].color = nextColor(c, net_conn_map[net].color);
228  }
229  pos -= rep * c.width;
230  }
231  if (label_string[label_string.size()-1] == '|')
232  label_string = label_string.substr(0, label_string.size()-1);
233  code += stringf("x%d [ shape=record, style=rounded, label=\"%s\" ];\n", idx, label_string.c_str());
234  if (!port.empty()) {
236  if (driver)
237  code += stringf("%s:e -> x%d:w [arrowhead=odiamond, arrowtail=odiamond, dir=both, %s, %s];\n", port.c_str(), idx, nextColor(sig).c_str(), widthLabel(sig.size()).c_str());
238  else
239  code += stringf("x%d:e -> %s:w [arrowhead=odiamond, arrowtail=odiamond, dir=both, %s, %s];\n", idx, port.c_str(), nextColor(sig).c_str(), widthLabel(sig.size()).c_str());
240  }
241  if (node != NULL)
242  *node = stringf("x%d", idx);
243  }
244  else
245  {
246  if (!port.empty()) {
247  if (driver)
248  net_conn_map[net].in.insert(port);
249  else
250  net_conn_map[net].out.insert(port);
251  net_conn_map[net].bits = sig.size();
252  net_conn_map[net].color = nextColor(sig, net_conn_map[net].color);
253  }
254  if (node != NULL)
255  *node = net;
256  }
257  return code;
258  }
259 
260  void collect_proc_signals(std::vector<RTLIL::SigSpec> &obj, std::set<RTLIL::SigSpec> &signals)
261  {
262  for (auto &it : obj)
263  if (!it.is_fully_const())
264  signals.insert(it);
265  }
266 
267  void collect_proc_signals(std::vector<RTLIL::SigSig> &obj, std::set<RTLIL::SigSpec> &input_signals, std::set<RTLIL::SigSpec> &output_signals)
268  {
269  for (auto &it : obj) {
270  output_signals.insert(it.first);
271  if (!it.second.is_fully_const())
272  input_signals.insert(it.second);
273  }
274  }
275 
276  void collect_proc_signals(RTLIL::CaseRule *obj, std::set<RTLIL::SigSpec> &input_signals, std::set<RTLIL::SigSpec> &output_signals)
277  {
278  collect_proc_signals(obj->compare, input_signals);
279  collect_proc_signals(obj->actions, input_signals, output_signals);
280  for (auto it : obj->switches)
281  collect_proc_signals(it, input_signals, output_signals);
282  }
283 
284  void collect_proc_signals(RTLIL::SwitchRule *obj, std::set<RTLIL::SigSpec> &input_signals, std::set<RTLIL::SigSpec> &output_signals)
285  {
286  input_signals.insert(obj->signal);
287  for (auto it : obj->cases)
288  collect_proc_signals(it, input_signals, output_signals);
289  }
290 
291  void collect_proc_signals(RTLIL::SyncRule *obj, std::set<RTLIL::SigSpec> &input_signals, std::set<RTLIL::SigSpec> &output_signals)
292  {
293  input_signals.insert(obj->signal);
294  collect_proc_signals(obj->actions, input_signals, output_signals);
295  }
296 
297  void collect_proc_signals(RTLIL::Process *obj, std::set<RTLIL::SigSpec> &input_signals, std::set<RTLIL::SigSpec> &output_signals)
298  {
299  collect_proc_signals(&obj->root_case, input_signals, output_signals);
300  for (auto it : obj->syncs)
301  collect_proc_signals(it, input_signals, output_signals);
302  }
303 
305  {
306  single_idx_count = 0;
307  dot_escape_store.clear();
308  dot_id2num_store.clear();
309  net_conn_map.clear();
310 
311  fprintf(f, "digraph \"%s\" {\n", escape(module->name.str()));
312  if (!notitle)
313  fprintf(f, "label=\"%s\";\n", escape(module->name.str()));
314  fprintf(f, "rankdir=\"LR\";\n");
315  fprintf(f, "remincross=true;\n");
316 
317  std::set<std::string> all_sources, all_sinks;
318 
319  std::map<std::string, std::string> wires_on_demand;
320  for (auto &it : module->wires_) {
321  if (!design->selected_member(module->name, it.first))
322  continue;
323  const char *shape = "diamond";
324  if (it.second->port_input || it.second->port_output)
325  shape = "octagon";
326  if (it.first[0] == '\\') {
327  fprintf(f, "n%d [ shape=%s, label=\"%s\", %s, fontcolor=\"black\" ];\n",
328  id2num(it.first), shape, findLabel(it.first.str()),
329  nextColor(RTLIL::SigSpec(it.second), "color=\"black\"").c_str());
330  if (it.second->port_input)
331  all_sources.insert(stringf("n%d", id2num(it.first)));
332  else if (it.second->port_output)
333  all_sinks.insert(stringf("n%d", id2num(it.first)));
334  } else {
335  wires_on_demand[stringf("n%d", id2num(it.first))] = it.first.str();
336  }
337  }
338 
339  if (stretchIO)
340  {
341  fprintf(f, "{ rank=\"source\";");
342  for (auto n : all_sources)
343  fprintf(f, " %s;", n.c_str());
344  fprintf(f, "}\n");
345 
346  fprintf(f, "{ rank=\"sink\";");
347  for (auto n : all_sinks)
348  fprintf(f, " %s;", n.c_str());
349  fprintf(f, "}\n");
350  }
351 
352  for (auto &it : module->cells_)
353  {
354  if (!design->selected_member(module->name, it.first))
355  continue;
356 
357  std::vector<RTLIL::IdString> in_ports, out_ports;
358 
359  for (auto &conn : it.second->connections()) {
360  if (!ct.cell_output(it.second->type, conn.first))
361  in_ports.push_back(conn.first);
362  else
363  out_ports.push_back(conn.first);
364  }
365 
366  std::sort(in_ports.begin(), in_ports.end(), RTLIL::sort_by_id_str());
367  std::sort(out_ports.begin(), out_ports.end(), RTLIL::sort_by_id_str());
368 
369  std::string label_string = "{{";
370 
371  for (auto &p : in_ports)
372  label_string += stringf("<p%d> %s%s|", id2num(p), escape(p.str()),
373  genSignedLabels && it.second->hasParam(p.str() + "_SIGNED") &&
374  it.second->getParam(p.str() + "_SIGNED").as_bool() ? "*" : "");
375  if (label_string[label_string.size()-1] == '|')
376  label_string = label_string.substr(0, label_string.size()-1);
377 
378  label_string += stringf("}|%s\\n%s|{", findLabel(it.first.str()), escape(it.second->type.str()));
379 
380  for (auto &p : out_ports)
381  label_string += stringf("<p%d> %s|", id2num(p), escape(p.str()));
382  if (label_string[label_string.size()-1] == '|')
383  label_string = label_string.substr(0, label_string.size()-1);
384 
385  label_string += "}}";
386 
387  std::string code;
388  for (auto &conn : it.second->connections()) {
389  code += gen_portbox(stringf("c%d:p%d", id2num(it.first), id2num(conn.first)),
390  conn.second, ct.cell_output(it.second->type, conn.first));
391  }
392 
393 #ifdef CLUSTER_CELLS_AND_PORTBOXES
394  if (!code.empty())
395  fprintf(f, "subgraph cluster_c%d {\nc%d [ shape=record, label=\"%s\"%s ];\n%s}\n",
396  id2num(it.first), id2num(it.first), label_string.c_str(), findColor(it.first), code.c_str());
397  else
398 #endif
399  fprintf(f, "c%d [ shape=record, label=\"%s\"%s ];\n%s",
400  id2num(it.first), label_string.c_str(), findColor(it.first.str()), code.c_str());
401  }
402 
403  for (auto &it : module->processes)
404  {
405  RTLIL::Process *proc = it.second;
406 
407  if (!design->selected_member(module->name, proc->name))
408  continue;
409 
410  std::set<RTLIL::SigSpec> input_signals, output_signals;
411  collect_proc_signals(proc, input_signals, output_signals);
412 
413  int pidx = single_idx_count++;
414  input_signals.erase(RTLIL::SigSpec());
415  output_signals.erase(RTLIL::SigSpec());
416 
417  for (auto &sig : input_signals) {
418  std::string code, node;
419  code += gen_portbox("", sig, false, &node);
420  fprintf(f, "%s", code.c_str());
421  net_conn_map[node].out.insert(stringf("p%d", pidx));
422  net_conn_map[node].bits = sig.size();
423  net_conn_map[node].color = nextColor(sig, net_conn_map[node].color);
424  }
425 
426  for (auto &sig : output_signals) {
427  std::string code, node;
428  code += gen_portbox("", sig, true, &node);
429  fprintf(f, "%s", code.c_str());
430  net_conn_map[node].in.insert(stringf("p%d", pidx));
431  net_conn_map[node].bits = sig.size();
432  net_conn_map[node].color = nextColor(sig, net_conn_map[node].color);
433  }
434 
435  std::string proc_src = RTLIL::unescape_id(proc->name);
436  if (proc->attributes.count("\\src") > 0)
437  proc_src = proc->attributes.at("\\src").decode_string();
438  fprintf(f, "p%d [shape=box, style=rounded, label=\"PROC %s\\n%s\"];\n", pidx, findLabel(proc->name.str()), proc_src.c_str());
439  }
440 
441  for (auto &conn : module->connections())
442  {
443  bool found_lhs_wire = false;
444  for (auto &c : conn.first.chunks()) {
445  if (c.wire == NULL || design->selected_member(module->name, c.wire->name))
446  found_lhs_wire = true;
447  }
448  bool found_rhs_wire = false;
449  for (auto &c : conn.second.chunks()) {
450  if (c.wire == NULL || design->selected_member(module->name, c.wire->name))
451  found_rhs_wire = true;
452  }
453  if (!found_lhs_wire || !found_rhs_wire)
454  continue;
455 
456  std::string code, left_node, right_node;
457  code += gen_portbox("", conn.second, false, &left_node);
458  code += gen_portbox("", conn.first, true, &right_node);
459  fprintf(f, "%s", code.c_str());
460 
461  if (left_node[0] == 'x' && right_node[0] == 'x') {
463  fprintf(f, "%s:e -> %s:w [arrowhead=odiamond, arrowtail=odiamond, dir=both, %s, %s];\n", left_node.c_str(), right_node.c_str(), nextColor(conn).c_str(), widthLabel(conn.first.size()).c_str());
464  } else {
465  net_conn_map[right_node].bits = conn.first.size();
466  net_conn_map[right_node].color = nextColor(conn, net_conn_map[right_node].color);
467  net_conn_map[left_node].bits = conn.first.size();
468  net_conn_map[left_node].color = nextColor(conn, net_conn_map[left_node].color);
469  if (left_node[0] == 'x') {
470  net_conn_map[right_node].in.insert(left_node);
471  } else if (right_node[0] == 'x') {
472  net_conn_map[left_node].out.insert(right_node);
473  } else {
474  net_conn_map[right_node].in.insert(stringf("x%d:e", single_idx_count));
475  net_conn_map[left_node].out.insert(stringf("x%d:w", single_idx_count));
476  fprintf(f, "x%d [shape=box, style=rounded, label=\"BUF\"];\n", single_idx_count++);
477  }
478  }
479  }
480 
481  for (auto &it : net_conn_map)
482  {
484  if (wires_on_demand.count(it.first) > 0) {
485  if (it.second.in.size() == 1 && it.second.out.size() > 1 && it.second.in.begin()->substr(0, 1) == "p")
486  it.second.out.erase(*it.second.in.begin());
487  if (it.second.in.size() == 1 && it.second.out.size() == 1) {
488  std::string from = *it.second.in.begin(), to = *it.second.out.begin();
489  if (from != to || from.substr(0, 1) != "p")
490  fprintf(f, "%s:e -> %s:w [%s, %s];\n", from.c_str(), to.c_str(), nextColor(it.second.color).c_str(), widthLabel(it.second.bits).c_str());
491  continue;
492  }
493  if (it.second.in.size() == 0 || it.second.out.size() == 0)
494  fprintf(f, "%s [ shape=diamond, label=\"%s\" ];\n", it.first.c_str(), findLabel(wires_on_demand[it.first]));
495  else
496  fprintf(f, "%s [ shape=point ];\n", it.first.c_str());
497  }
498  for (auto &it2 : it.second.in)
499  fprintf(f, "%s:e -> %s:w [%s, %s];\n", it2.c_str(), it.first.c_str(), nextColor(it.second.color).c_str(), widthLabel(it.second.bits).c_str());
500  for (auto &it2 : it.second.out)
501  fprintf(f, "%s:e -> %s:w [%s, %s];\n", it.first.c_str(), it2.c_str(), nextColor(it.second.color).c_str(), widthLabel(it.second.bits).c_str());
502  }
503 
504  fprintf(f, "}\n");
505  }
506 
507  ShowWorker(FILE *f, RTLIL::Design *design, std::vector<RTLIL::Design*> &libs, uint32_t colorSeed, bool genWidthLabels,
508  bool genSignedLabels, bool stretchIO, bool enumerateIds, bool abbreviateIds, bool notitle,
509  const std::vector<std::pair<std::string, RTLIL::Selection>> &color_selections,
510  const std::vector<std::pair<std::string, RTLIL::Selection>> &label_selections) :
511  f(f), design(design), currentColor(colorSeed), genWidthLabels(genWidthLabels),
512  genSignedLabels(genSignedLabels), stretchIO(stretchIO), enumerateIds(enumerateIds), abbreviateIds(abbreviateIds),
514  {
517  ct.setup_stdcells();
519  ct.setup_design(design);
520 
521  for (auto lib : libs)
522  ct.setup_design(lib);
523 
524  design->optimize();
525  page_counter = 0;
526  for (auto &mod_it : design->modules_)
527  {
528  module = mod_it.second;
529  if (!design->selected_module(module->name))
530  continue;
531  if (design->selected_whole_module(module->name)) {
532  if (module->get_bool_attribute("\\blackbox")) {
533  log("Skipping blackbox module %s.\n", id2cstr(module->name));
534  continue;
535  } else
536  if (module->cells_.empty() && module->connections().empty() && module->processes.empty()) {
537  log("Skipping empty module %s.\n", id2cstr(module->name));
538  continue;
539  } else
540  log("Dumping module %s to page %d.\n", id2cstr(module->name), ++page_counter);
541  } else
542  log("Dumping selected parts of module %s to page %d.\n", id2cstr(module->name), ++page_counter);
543  handle_module();
544  }
545  }
546 };
547 
548 struct ShowPass : public Pass {
549  ShowPass() : Pass("show", "generate schematics using graphviz") { }
550  virtual void help()
551  {
552  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
553  log("\n");
554  log(" show [options] [selection]\n");
555  log("\n");
556  log("Create a graphviz DOT file for the selected part of the design and compile it\n");
557  log("to a graphics file (usually SVG or PostScript).\n");
558  log("\n");
559  log(" -viewer <viewer>\n");
560  log(" Run the specified command with the graphics file as parameter.\n");
561  log("\n");
562  log(" -format <format>\n");
563  log(" Generate a graphics file in the specified format.\n");
564  log(" Usually <format> is 'svg' or 'ps'.\n");
565  log("\n");
566  log(" -lib <verilog_or_ilang_file>\n");
567  log(" Use the specified library file for determining whether cell ports are\n");
568  log(" inputs or outputs. This option can be used multiple times to specify\n");
569  log(" more than one library.\n");
570  log("\n");
571  log(" -prefix <prefix>\n");
572  log(" generate <prefix>.* instead of ~/.yosys_show.*\n");
573  log("\n");
574  log(" -color <color> <object>\n");
575  log(" assign the specified color to the specified object. The object can be\n");
576  log(" a single selection wildcard expressions or a saved set of objects in\n");
577  log(" the @<name> syntax (see \"help select\" for details).\n");
578  log("\n");
579  log(" -label <text> <object>\n");
580  log(" assign the specified label text to the specified object. The object can\n");
581  log(" be a single selection wildcard expressions or a saved set of objects in\n");
582  log(" the @<name> syntax (see \"help select\" for details).\n");
583  log("\n");
584  log(" -colors <seed>\n");
585  log(" Randomly assign colors to the wires. The integer argument is the seed\n");
586  log(" for the random number generator. Change the seed value if the colored\n");
587  log(" graph still is ambigous. A seed of zero deactivates the coloring.\n");
588  log("\n");
589  log(" -width\n");
590  log(" annotate busses with a label indicating the width of the bus.\n");
591  log("\n");
592  log(" -signed\n");
593  log(" mark ports (A, B) that are declarted as signed (using the [AB]_SIGNED\n");
594  log(" cell parameter) with an asterisk next to the port name.\n");
595  log("\n");
596  log(" -stretch\n");
597  log(" stretch the graph so all inputs are on the left side and all outputs\n");
598  log(" (including inout ports) are on the right side.\n");
599  log("\n");
600  log(" -pause\n");
601  log(" wait for the use to press enter to before returning\n");
602  log("\n");
603  log(" -enum\n");
604  log(" enumerate objects with internal ($-prefixed) names\n");
605  log("\n");
606  log(" -long\n");
607  log(" do not abbeviate objects with internal ($-prefixed) names\n");
608  log("\n");
609  log(" -notitle\n");
610  log(" do not add the module name as graph title to the dot file\n");
611  log("\n");
612  log("When no <format> is specified, 'dot' is used. When no <format> and <viewer> is\n");
613  log("specified, 'xdot' is used to display the schematic.\n");
614  log("\n");
615  log("The generated output files are '~/.yosys_show.dot' and '~/.yosys_show.<format>',\n");
616  log("unless another prefix is specified using -prefix <prefix>.\n");
617  log("\n");
618  }
619  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
620  {
621  log_header("Generating Graphviz representation of design.\n");
622  log_push();
623 
624  std::vector<std::pair<std::string, RTLIL::Selection>> color_selections;
625  std::vector<std::pair<std::string, RTLIL::Selection>> label_selections;
626 
627  std::string format;
628  std::string viewer_exe;
629  std::string prefix = stringf("%s/.yosys_show", getenv("HOME") ? getenv("HOME") : ".");
630  std::vector<std::string> libfiles;
631  std::vector<RTLIL::Design*> libs;
632  uint32_t colorSeed = 0;
633  bool flag_width = false;
634  bool flag_signed = false;
635  bool flag_stretch = false;
636  bool flag_pause = false;
637  bool flag_enum = false;
638  bool flag_abbeviate = true;
639  bool flag_notitle = false;
640 
641  size_t argidx;
642  for (argidx = 1; argidx < args.size(); argidx++)
643  {
644  std::string arg = args[argidx];
645  if (arg == "-viewer" && argidx+1 < args.size()) {
646  viewer_exe = args[++argidx];
647  continue;
648  }
649  if (arg == "-lib" && argidx+1 < args.size()) {
650  libfiles.push_back(args[++argidx]);
651  continue;
652  }
653  if (arg == "-prefix" && argidx+1 < args.size()) {
654  prefix = args[++argidx];
655  continue;
656  }
657  if (arg == "-color" && argidx+2 < args.size()) {
658  std::pair<std::string, RTLIL::Selection> data;
659  data.first = args[++argidx], argidx++;
660  handle_extra_select_args(this, args, argidx, argidx+1, design);
661  data.second = design->selection_stack.back();
662  design->selection_stack.pop_back();
663  color_selections.push_back(data);
664  continue;
665  }
666  if (arg == "-label" && argidx+2 < args.size()) {
667  std::pair<std::string, RTLIL::Selection> data;
668  data.first = args[++argidx], argidx++;
669  handle_extra_select_args(this, args, argidx, argidx+1, design);
670  data.second = design->selection_stack.back();
671  design->selection_stack.pop_back();
672  label_selections.push_back(data);
673  continue;
674  }
675  if (arg == "-colors" && argidx+1 < args.size()) {
676  colorSeed = atoi(args[++argidx].c_str());
677  for (int i = 0; i < 100; i++)
678  colorSeed = ShowWorker::xorshift32(colorSeed);
679  continue;
680  }
681  if (arg == "-format" && argidx+1 < args.size()) {
682  format = args[++argidx];
683  continue;
684  }
685  if (arg == "-width") {
686  flag_width= true;
687  continue;
688  }
689  if (arg == "-signed") {
690  flag_signed= true;
691  continue;
692  }
693  if (arg == "-stretch") {
694  flag_stretch= true;
695  continue;
696  }
697  if (arg == "-pause") {
698  flag_pause= true;
699  continue;
700  }
701  if (arg == "-enum") {
702  flag_enum = true;
703  flag_abbeviate = false;
704  continue;
705  }
706  if (arg == "-long") {
707  flag_enum = false;
708  flag_abbeviate = false;
709  continue;
710  }
711  if (arg == "-notitle") {
712  flag_notitle = true;
713  continue;
714  }
715  break;
716  }
717  extra_args(args, argidx, design);
718 
719  if (format != "ps") {
720  int modcount = 0;
721  for (auto &mod_it : design->modules_) {
722  if (mod_it.second->get_bool_attribute("\\blackbox"))
723  continue;
724  if (mod_it.second->cells_.empty() && mod_it.second->connections().empty())
725  continue;
726  if (design->selected_module(mod_it.first))
727  modcount++;
728  }
729  if (modcount > 1)
730  log_cmd_error("For formats different than 'ps' only one module must be selected.\n");
731  }
732 
733  for (auto filename : libfiles) {
734  std::ifstream f;
735  f.open(filename.c_str());
736  if (f.fail())
737  log_error("Can't open lib file `%s'.\n", filename.c_str());
738  RTLIL::Design *lib = new RTLIL::Design;
739  Frontend::frontend_call(lib, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
740  libs.push_back(lib);
741  }
742 
743  if (libs.size() > 0)
744  log_header("Continuing show pass.\n");
745 
746  std::string dot_file = stringf("%s.dot", prefix.c_str());
747  std::string out_file = stringf("%s.%s", prefix.c_str(), format.empty() ? "svg" : format.c_str());
748 
749  log("Writing dot description to `%s'.\n", dot_file.c_str());
750  FILE *f = fopen(dot_file.c_str(), "w");
751  if (f == NULL) {
752  for (auto lib : libs)
753  delete lib;
754  log_cmd_error("Can't open dot file `%s' for writing.\n", dot_file.c_str());
755  }
756  ShowWorker worker(f, design, libs, colorSeed, flag_width, flag_signed, flag_stretch, flag_enum, flag_abbeviate, flag_notitle, color_selections, label_selections);
757  fclose(f);
758 
759  for (auto lib : libs)
760  delete lib;
761 
762  if (worker.page_counter == 0)
763  log_cmd_error("Nothing there to show.\n");
764 
765  if (format != "dot" && !format.empty()) {
766  std::string cmd = stringf("dot -T%s -o '%s' '%s'", format.c_str(), out_file.c_str(), dot_file.c_str());
767  log("Exec: %s\n", cmd.c_str());
768  if (run_command(cmd) != 0)
769  log_cmd_error("Shell command failed!\n");
770  }
771 
772  if (!viewer_exe.empty()) {
773  std::string cmd = stringf("%s '%s' &", viewer_exe.c_str(), out_file.c_str());
774  log("Exec: %s\n", cmd.c_str());
775  if (run_command(cmd) != 0)
776  log_cmd_error("Shell command failed!\n");
777  } else
778  if (format.empty()) {
779  std::string cmd = stringf("fuser -s '%s' || xdot '%s' < '%s' &", dot_file.c_str(), dot_file.c_str(), dot_file.c_str());
780  log("Exec: %s\n", cmd.c_str());
781  if (run_command(cmd) != 0)
782  log_cmd_error("Shell command failed!\n");
783  }
784 
785  if (flag_pause) {
786  #ifdef YOSYS_ENABLE_READLINE
787  char *input = NULL;
788  while ((input = readline("Press ENTER to continue (or type 'shell' to open a shell)> ")) != NULL) {
789  if (input[strspn(input, " \t\r\n")] == 0)
790  break;
791  char *p = input + strspn(input, " \t\r\n");
792  if (!strcmp(p, "shell")) {
793  Pass::call(design, "shell");
794  break;
795  }
796  }
797  #else
798  log_cmd_error("This version of yosys is built without readline support => 'show -pause' is not available.\n");
799  #endif
800  }
801 
802  log_pop();
803  }
804 } ShowPass;
805 
void collect_proc_signals(RTLIL::SyncRule *obj, std::set< RTLIL::SigSpec > &input_signals, std::set< RTLIL::SigSpec > &output_signals)
Definition: show.cc:291
std::string nextColor(const RTLIL::SigSpec &sig)
Definition: show.cc:105
RTLIL::Module * module
Definition: show.cc:54
std::string str() const
Definition: rtlil.h:182
std::string color
Definition: show.cc:49
std::vector< RTLIL::Selection > selection_stack
Definition: rtlil.h:509
bool stretchIO
Definition: show.cc:58
void setup_stdcells()
Definition: celltypes.h:132
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
void handle_extra_select_args(Pass *pass, std::vector< std::string > args, size_t argidx, size_t args_size, RTLIL::Design *design)
Definition: select.cc:803
bool selected_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:379
RTLIL::Design * design
Definition: show.cc:53
void setup_internals_mem()
Definition: celltypes.h:115
void log_header(const char *format,...)
Definition: log.cc:188
const std::vector< RTLIL::SigSig > & connections() const
Definition: rtlil.cc:1307
std::string gen_signode_simple(RTLIL::SigSpec sig, bool range_check=true)
Definition: show.cc:182
std::map< RTLIL::IdString, int > dot_id2num_store
Definition: show.cc:45
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
const char * findColor(std::string member_name)
Definition: show.cc:124
RTLIL_ATTRIBUTE_MEMBERS std::vector< RTLIL::CaseRule * > cases
Definition: rtlil.h:1134
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
static void frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
Definition: register.cc:375
static std::string idx(std::string str)
Definition: test_autotb.cc:57
std::map< RTLIL::IdString, int > autonames
Definition: show.cc:46
ShowPass()
Definition: show.cc:549
int width
Definition: rtlil.h:826
std::string nextColor(const RTLIL::SigSig &conn)
Definition: show.cc:110
std::map< std::string, net_conn > net_conn_map
Definition: show.cc:50
void log_error(const char *format,...)
Definition: log.cc:204
void log_pop()
Definition: log.cc:237
const char * findLabel(std::string member_name)
Definition: show.cc:134
const std::vector< std::pair< std::string, RTLIL::Selection > > & label_selections
Definition: show.cc:65
RTLIL::SigSpec signal
Definition: rtlil.h:1145
int size() const
Definition: rtlil.h:1019
bool abbreviateIds
Definition: show.cc:60
int page_counter
Definition: show.cc:62
ShowPass ShowPass
std::vector< RTLIL::SigSpec > compare
Definition: rtlil.h:1119
bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
Definition: rtlil.cc:397
tuple n
Definition: fsm/generate.py:59
const std::vector< std::pair< std::string, RTLIL::Selection > > & color_selections
Definition: show.cc:64
int run_command(const std::string &command, std::function< void(const std::string &)> process_line)
Definition: yosys.cc:195
virtual void help()
Definition: show.cc:550
std::string nextColor(std::string presetColor)
Definition: show.cc:81
RTLIL::Wire * wire
Definition: rtlil.h:885
std::set< std::string > in
Definition: show.cc:49
bool is_chunk() const
Definition: rtlil.cc:2755
RTLIL::SigSpec signal
Definition: rtlil.h:1132
void collect_proc_signals(std::vector< RTLIL::SigSig > &obj, std::set< RTLIL::SigSpec > &input_signals, std::set< RTLIL::SigSpec > &output_signals)
Definition: show.cc:267
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
bool notitle
Definition: show.cc:61
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
std::string nextColor(RTLIL::SigSpec sig, std::string defaultColor)
Definition: show.cc:88
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
void optimize()
Definition: rtlil.cc:369
#define log_assert(_assert_expr_)
Definition: log.h:85
bool genWidthLabels
Definition: show.cc:56
RTLIL::IdString name
Definition: rtlil.h:599
std::string nextColor(const RTLIL::SigSig &conn, std::string defaultColor)
Definition: show.cc:100
RTLIL::SigChunk as_chunk() const
Definition: rtlil.cc:2877
bool selected_whole_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:388
void collect_proc_signals(RTLIL::Process *obj, std::set< RTLIL::SigSpec > &input_signals, std::set< RTLIL::SigSpec > &output_signals)
Definition: show.cc:297
const char * escape(std::string id, bool is_name=false)
Definition: show.cc:142
std::set< std::string > out
Definition: show.cc:49
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
RTLIL::IdString name
Definition: rtlil.h:825
std::vector< RTLIL::SigSig > actions
Definition: rtlil.h:1146
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
void log_cmd_error(const char *format,...)
Definition: log.cc:211
std::vector< std::string > dot_escape_store
Definition: show.cc:44
static uint32_t xorshift32(uint32_t x)
Definition: show.cc:67
RTLIL::IdString name
Definition: rtlil.h:1154
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
void sort_and_unify()
Definition: rtlil.cc:2291
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
bool enumerateIds
Definition: show.cc:59
FILE * f
Definition: show.cc:52
void log(const char *format,...)
Definition: log.cc:180
std::vector< RTLIL::SyncRule * > syncs
Definition: rtlil.h:1157
void collect_proc_signals(std::vector< RTLIL::SigSpec > &obj, std::set< RTLIL::SigSpec > &signals)
Definition: show.cc:260
static int input(void)
void setup_internals()
Definition: celltypes.h:83
void collect_proc_signals(RTLIL::SwitchRule *obj, std::set< RTLIL::SigSpec > &input_signals, std::set< RTLIL::SigSpec > &output_signals)
Definition: show.cc:284
std::string widthLabel(int bits)
Definition: show.cc:115
void log_push()
Definition: log.cc:232
std::string gen_portbox(std::string port, RTLIL::SigSpec sig, bool driver, std::string *node=NULL)
Definition: show.cc:203
void setup_design(RTLIL::Design *design)
Definition: celltypes.h:77
void setup_stdcells_mem()
Definition: celltypes.h:149
std::vector< RTLIL::SigSig > actions
Definition: rtlil.h:1120
int single_idx_count
Definition: show.cc:47
int id2num(RTLIL::IdString id)
Definition: show.cc:175
std::string id(RTLIL::IdString internal_id, bool may_rename=true)
bool genSignedLabels
Definition: show.cc:57
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
std::vector< RTLIL::SwitchRule * > switches
Definition: rtlil.h:1121
void handle_module()
Definition: show.cc:304
static void call(RTLIL::Design *design, std::string command)
Definition: register.cc:146
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
CellTypes ct
Definition: show.cc:42
uint32_t currentColor
Definition: show.cc:55
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: show.cc:619
void collect_proc_signals(RTLIL::CaseRule *obj, std::set< RTLIL::SigSpec > &input_signals, std::set< RTLIL::SigSpec > &output_signals)
Definition: show.cc:276
const std::vector< RTLIL::SigChunk > & chunks() const
Definition: rtlil.h:1016
ShowWorker(FILE *f, RTLIL::Design *design, std::vector< RTLIL::Design * > &libs, uint32_t colorSeed, bool genWidthLabels, bool genSignedLabels, bool stretchIO, bool enumerateIds, bool abbreviateIds, bool notitle, const std::vector< std::pair< std::string, RTLIL::Selection >> &color_selections, const std::vector< std::pair< std::string, RTLIL::Selection >> &label_selections)
Definition: show.cc:507
RTLIL_ATTRIBUTE_MEMBERS RTLIL::CaseRule root_case
Definition: rtlil.h:1156
std::string nextColor()
Definition: show.cc:74