Add new builtin FF types
[yosys.git] / passes / cmds / show.cc
index 58acd302d07ebde009addeb27614b18a59c84e4e..cbed08a3f62e8d1675b82f9fecf4cbadec4faa99 100644 (file)
 #  include <dirent.h>
 #endif
 
+#ifdef __APPLE__
+#  include <unistd.h>
+#endif
+
 #ifdef YOSYS_ENABLE_READLINE
 #  include <readline/readline.h>
 #endif
@@ -37,8 +41,6 @@
 USING_YOSYS_NAMESPACE
 PRIVATE_NAMESPACE_BEGIN
 
-using RTLIL::id2cstr;
-
 #undef CLUSTER_CELLS_AND_PORTBOXES
 
 struct ShowWorker
@@ -97,7 +99,7 @@ struct ShowWorker
        {
                sig.sort_and_unify();
                for (auto &c : sig.chunks()) {
-                       if (c.wire != NULL)
+                       if (c.wire != nullptr)
                                for (auto &s : color_selections)
                                        if (s.second.selected_members.count(module->name) > 0 && s.second.selected_members.at(module->name).count(c.wire->name) > 0)
                                                return stringf("color=\"%s\"", s.first.c_str());
@@ -214,7 +216,7 @@ struct ShowWorker
 
                if (sig.is_chunk()) {
                        const RTLIL::SigChunk &c = sig.as_chunk();
-                       if (c.wire != NULL && design->selected_member(module->name, c.wire->name)) {
+                       if (c.wire != nullptr && design->selected_member(module->name, c.wire->name)) {
                                if (!range_check || c.wire->width == c.width)
                                                return stringf("n%d", id2num(c.wire->name));
                        } else {
@@ -226,7 +228,7 @@ struct ShowWorker
                return std::string();
        }
 
-       std::string gen_portbox(std::string port, RTLIL::SigSpec sig, bool driver, std::string *node = NULL)
+       std::string gen_portbox(std::string port, RTLIL::SigSpec sig, bool driver, std::string *node = nullptr)
        {
                std::string code;
                std::string net = gen_signode_simple(sig);
@@ -237,15 +239,34 @@ struct ShowWorker
                        int idx = single_idx_count++;
                        for (int rep, i = int(sig.chunks().size())-1; i >= 0; i -= rep) {
                                const RTLIL::SigChunk &c = sig.chunks().at(i);
-                               net = gen_signode_simple(c, false);
-                               log_assert(!net.empty());
+                               if (!driver && c.wire == nullptr) {
+                                       RTLIL::State s1 = c.data.front();
+                                       for (auto s2 : c.data)
+                                               if (s1 != s2)
+                                                       goto not_const_stream;
+                                       net.clear();
+                               } else {
+                       not_const_stream:
+                                       net = gen_signode_simple(c, false);
+                                       log_assert(!net.empty());
+                               }
                                for (rep = 1; i-rep >= 0 && c == sig.chunks().at(i-rep); rep++) {}
                                std::string repinfo = rep > 1 ? stringf("%dx ", rep) : "";
                                if (driver) {
+                                       log_assert(!net.empty());
                                        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);
                                        net_conn_map[net].in.insert(stringf("x%d:s%d", idx, i));
                                        net_conn_map[net].bits = rep*c.width;
                                        net_conn_map[net].color = nextColor(c, net_conn_map[net].color);
+                               } else
+                               if (net.empty()) {
+                                       log_assert(rep == 1);
+                                       label_string += stringf("%c -&gt; %d:%d |",
+                                                       c.data.front() == State::S0 ? '0' :
+                                                       c.data.front() == State::S1 ? '1' :
+                                                       c.data.front() == State::Sx ? 'X' :
+                                                       c.data.front() == State::Sz ? 'Z' : '?',
+                                                       pos, pos-rep*c.width+1);
                                } else {
                                        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);
                                        net_conn_map[net].out.insert(stringf("x%d:s%d", idx, i));
@@ -264,7 +285,7 @@ struct ShowWorker
                                else
                                        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());
                        }
-                       if (node != NULL)
+                       if (node != nullptr)
                                *node = stringf("x%d", idx);
                }
                else
@@ -277,7 +298,7 @@ struct ShowWorker
                                net_conn_map[net].bits = sig.size();
                                net_conn_map[net].color = nextColor(sig, net_conn_map[net].color);
                        }
-                       if (node != NULL)
+                       if (node != nullptr)
                                *node = net;
                }
                return code;
@@ -343,22 +364,20 @@ struct ShowWorker
                std::set<std::string> all_sources, all_sinks;
 
                std::map<std::string, std::string> wires_on_demand;
-               for (auto &it : module->wires_) {
-                       if (!design->selected_member(module->name, it.first))
-                               continue;
+               for (auto wire : module->selected_wires()) {
                        const char *shape = "diamond";
-                       if (it.second->port_input || it.second->port_output)
+                       if (wire->port_input || wire->port_output)
                                shape = "octagon";
-                       if (it.first[0] == '\\') {
+                       if (wire->name[0] == '\\') {
                                fprintf(f, "n%d [ shape=%s, label=\"%s\", %s, fontcolor=\"black\" ];\n",
-                                               id2num(it.first), shape, findLabel(it.first.str()),
-                                               nextColor(RTLIL::SigSpec(it.second), "color=\"black\"").c_str());
-                               if (it.second->port_input)
-                                       all_sources.insert(stringf("n%d", id2num(it.first)));
-                               else if (it.second->port_output)
-                                       all_sinks.insert(stringf("n%d", id2num(it.first)));
+                                               id2num(wire->name), shape, findLabel(wire->name.str()),
+                                               nextColor(RTLIL::SigSpec(wire), "color=\"black\"").c_str());
+                               if (wire->port_input)
+                                       all_sources.insert(stringf("n%d", id2num(wire->name)));
+                               else if (wire->port_output)
+                                       all_sinks.insert(stringf("n%d", id2num(wire->name)));
                        } else {
-                               wires_on_demand[stringf("n%d", id2num(it.first))] = it.first.str();
+                               wires_on_demand[stringf("n%d", id2num(wire->name))] = wire->name.str();
                        }
                }
 
@@ -375,15 +394,12 @@ struct ShowWorker
                        fprintf(f, "}\n");
                }
 
-               for (auto &it : module->cells_)
+               for (auto cell : module->selected_cells())
                {
-                       if (!design->selected_member(module->name, it.first))
-                               continue;
-
                        std::vector<RTLIL::IdString> in_ports, out_ports;
 
-                       for (auto &conn : it.second->connections()) {
-                               if (!ct.cell_output(it.second->type, conn.first))
+                       for (auto &conn : cell->connections()) {
+                               if (!ct.cell_output(cell->type, conn.first))
                                        in_ports.push_back(conn.first);
                                else
                                        out_ports.push_back(conn.first);
@@ -396,12 +412,12 @@ struct ShowWorker
 
                        for (auto &p : in_ports)
                                label_string += stringf("<p%d> %s%s|", id2num(p), escape(p.str()),
-                                               genSignedLabels && it.second->hasParam(p.str() + "_SIGNED") &&
-                                               it.second->getParam(p.str() + "_SIGNED").as_bool() ? "*" : "");
+                                               genSignedLabels && cell->hasParam(p.str() + "_SIGNED") &&
+                                               cell->getParam(p.str() + "_SIGNED").as_bool() ? "*" : "");
                        if (label_string[label_string.size()-1] == '|')
                                label_string = label_string.substr(0, label_string.size()-1);
 
-                       label_string += stringf("}|%s\\n%s|{", findLabel(it.first.str()), escape(it.second->type.str()));
+                       label_string += stringf("}|%s\\n%s|{", findLabel(cell->name.str()), escape(cell->type.str()));
 
                        for (auto &p : out_ports)
                                label_string += stringf("<p%d> %s|", id2num(p), escape(p.str()));
@@ -411,19 +427,19 @@ struct ShowWorker
                        label_string += "}}";
 
                        std::string code;
-                       for (auto &conn : it.second->connections()) {
-                               code += gen_portbox(stringf("c%d:p%d", id2num(it.first), id2num(conn.first)),
-                                               conn.second, ct.cell_output(it.second->type, conn.first));
+                       for (auto &conn : cell->connections()) {
+                               code += gen_portbox(stringf("c%d:p%d", id2num(cell->name), id2num(conn.first)),
+                                               conn.second, ct.cell_output(cell->type, conn.first));
                        }
 
 #ifdef CLUSTER_CELLS_AND_PORTBOXES
                        if (!code.empty())
                                fprintf(f, "subgraph cluster_c%d {\nc%d [ shape=record, label=\"%s\"%s ];\n%s}\n",
-                                               id2num(it.first), id2num(it.first), label_string.c_str(), findColor(it.first), code.c_str());
+                                               id2num(cell->name), id2num(cell->name), label_string.c_str(), findColor(cell->name), code.c_str());
                        else
 #endif
                                fprintf(f, "c%d [ shape=record, label=\"%s\"%s ];\n%s",
-                                               id2num(it.first), label_string.c_str(), findColor(it.first.str()), code.c_str());
+                                               id2num(cell->name), label_string.c_str(), findColor(cell->name.str()), code.c_str());
                }
 
                for (auto &it : module->processes)
@@ -459,8 +475,8 @@ struct ShowWorker
                        }
 
                        std::string proc_src = RTLIL::unescape_id(proc->name);
-                       if (proc->attributes.count("\\src") > 0)
-                               proc_src = proc->attributes.at("\\src").decode_string();
+                       if (proc->attributes.count(ID::src) > 0)
+                               proc_src = proc->attributes.at(ID::src).decode_string();
                        fprintf(f, "p%d [shape=box, style=rounded, label=\"PROC %s\\n%s\"];\n", pidx, findLabel(proc->name.str()), proc_src.c_str());
                }
 
@@ -468,12 +484,12 @@ struct ShowWorker
                {
                        bool found_lhs_wire = false;
                        for (auto &c : conn.first.chunks()) {
-                               if (c.wire == NULL || design->selected_member(module->name, c.wire->name))
+                               if (c.wire == nullptr || design->selected_member(module->name, c.wire->name))
                                        found_lhs_wire = true;
                        }
                        bool found_rhs_wire = false;
                        for (auto &c : conn.second.chunks()) {
-                               if (c.wire == NULL || design->selected_member(module->name, c.wire->name))
+                               if (c.wire == nullptr || design->selected_member(module->name, c.wire->name))
                                        found_rhs_wire = true;
                        }
                        if (!found_lhs_wire || !found_rhs_wire)
@@ -508,11 +524,11 @@ struct ShowWorker
                {
                        currentColor = xorshift32(currentColor);
                        if (wires_on_demand.count(it.first) > 0) {
-                               if (it.second.in.size() == 1 && it.second.out.size() > 1 && it.second.in.begin()->substr(0, 1) == "p")
+                               if (it.second.in.size() == 1 && it.second.out.size() > 1 && it.second.in.begin()->compare(0, 1, "p") == 0)
                                        it.second.out.erase(*it.second.in.begin());
                                if (it.second.in.size() == 1 && it.second.out.size() == 1) {
                                        std::string from = *it.second.in.begin(), to = *it.second.out.begin();
-                                       if (from != to || from.substr(0, 1) != "p")
+                                       if (from != to || from.compare(0, 1, "p") != 0)
                                                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());
                                        continue;
                                }
@@ -549,23 +565,21 @@ struct ShowWorker
 
                design->optimize();
                page_counter = 0;
-               for (auto &mod_it : design->modules_)
+               for (auto mod : design->selected_modules())
                {
-                       module = mod_it.second;
-                       if (!design->selected_module(module->name))
-                               continue;
+                       module = mod;
                        if (design->selected_whole_module(module->name)) {
-                               if (module->get_bool_attribute("\\blackbox")) {
-                                       // log("Skipping blackbox module %s.\n", id2cstr(module->name));
+                               if (module->get_blackbox_attribute()) {
+                                       // log("Skipping blackbox module %s.\n", log_id(module->name));
                                        continue;
                                } else
-                               if (module->cells_.empty() && module->connections().empty() && module->processes.empty()) {
-                                       log("Skipping empty module %s.\n", id2cstr(module->name));
+                               if (module->cells().size() == 0 && module->connections().empty() && module->processes.empty()) {
+                                       log("Skipping empty module %s.\n", log_id(module->name));
                                        continue;
                                } else
-                                       log("Dumping module %s to page %d.\n", id2cstr(module->name), ++page_counter);
+                                       log("Dumping module %s to page %d.\n", log_id(module->name), ++page_counter);
                        } else
-                               log("Dumping selected parts of module %s to page %d.\n", id2cstr(module->name), ++page_counter);
+                               log("Dumping selected parts of module %s to page %d.\n", log_id(module->name), ++page_counter);
                        handle_module();
                }
        }
@@ -573,7 +587,7 @@ struct ShowWorker
 
 struct ShowPass : public Pass {
        ShowPass() : Pass("show", "generate schematics using graphviz") { }
-       void help() YS_OVERRIDE
+       void help() override
        {
                //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
                log("\n");
@@ -645,6 +659,10 @@ struct ShowPass : public Pass {
                log("    -notitle\n");
                log("        do not add the module name as graph title to the dot file\n");
                log("\n");
+               log("    -nobg\n");
+               log("        don't run viewer in the background, IE wait for the viewer tool to\n");
+               log("        exit before returning\n");
+               log("\n");
                log("When no <format> is specified, 'dot' is used. When no <format> and <viewer> is\n");
                log("specified, 'xdot' is used to display the schematic (POSIX systems only).\n");
                log("\n");
@@ -656,7 +674,7 @@ struct ShowPass : public Pass {
                log("the 'show' command is executed.\n");
                log("\n");
        }
-       void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
+       void execute(std::vector<std::string> args, RTLIL::Design *design) override
        {
                log_header(design, "Generating Graphviz representation of design.\n");
                log_push();
@@ -664,7 +682,7 @@ struct ShowPass : public Pass {
                std::vector<std::pair<std::string, RTLIL::Selection>> color_selections;
                std::vector<std::pair<std::string, RTLIL::Selection>> label_selections;
 
-#if defined(EMSCRIPTEN) || defined(_WIN32)
+#if defined(_WIN32) || defined(YOSYS_DISABLE_SPAWN)
                std::string format = "dot";
                std::string prefix = "show";
 #else
@@ -683,6 +701,7 @@ struct ShowPass : public Pass {
                bool flag_abbreviate = true;
                bool flag_notitle = false;
                bool custom_prefix = false;
+               std::string background = "&";
                RTLIL::IdString colorattr;
 
                size_t argidx;
@@ -764,19 +783,22 @@ struct ShowPass : public Pass {
                                flag_notitle = true;
                                continue;
                        }
+                       if (arg == "-nobg") {
+                               background= "";
+                               continue;
+                       }
                        break;
                }
                extra_args(args, argidx, design);
 
                if (format != "ps" && format != "dot") {
                        int modcount = 0;
-                       for (auto &mod_it : design->modules_) {
-                               if (mod_it.second->get_bool_attribute("\\blackbox"))
+                       for (auto module : design->selected_modules()) {
+                               if (module->get_blackbox_attribute())
                                        continue;
-                               if (mod_it.second->cells_.empty() && mod_it.second->connections().empty())
+                               if (module->cells().size() == 0 && module->connections().empty())
                                        continue;
-                               if (design->selected_module(mod_it.first))
-                                       modcount++;
+                               modcount++;
                        }
                        if (modcount > 1)
                                log_cmd_error("For formats different than 'ps' or 'dot' only one module must be selected.\n");
@@ -789,7 +811,7 @@ struct ShowPass : public Pass {
                        if (f.fail())
                                log_error("Can't open lib file `%s'.\n", filename.c_str());
                        RTLIL::Design *lib = new RTLIL::Design;
-                       Frontend::frontend_call(lib, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
+                       Frontend::frontend_call(lib, &f, filename, (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".il") == 0 ? "ilang" : "verilog"));
                        libs.push_back(lib);
                }
 
@@ -803,7 +825,7 @@ struct ShowPass : public Pass {
                FILE *f = fopen(dot_file.c_str(), "w");
                if (custom_prefix)
                        yosys_output_files.insert(dot_file);
-               if (f == NULL) {
+               if (f == nullptr) {
                        for (auto lib : libs)
                                delete lib;
                        log_cmd_error("Can't open dot file `%s' for writing.\n", dot_file.c_str());
@@ -827,36 +849,44 @@ struct ShowPass : public Pass {
                        std::string cmd = stringf(DOT_CMD, format.c_str(), dot_file.c_str(), out_file.c_str(), out_file.c_str(), out_file.c_str());
                        #undef DOT_CMD
                        log("Exec: %s\n", cmd.c_str());
-                       if (run_command(cmd) != 0)
-                               log_cmd_error("Shell command failed!\n");
+                       #if !defined(YOSYS_DISABLE_SPAWN)
+                               if (run_command(cmd) != 0)
+                                       log_cmd_error("Shell command failed!\n");
+                       #endif
                }
 
+               #if defined(YOSYS_DISABLE_SPAWN)
+                       log_assert(viewer_exe.empty() && !format.empty());
+               #else
                if (!viewer_exe.empty()) {
                        #ifdef _WIN32
                                // system()/cmd.exe does not understand single quotes nor
                                // background tasks on Windows. So we have to pause yosys
                                // until the viewer exits.
-                               #define VIEW_CMD "%s \"%s\""
+                               std::string cmd = stringf("%s \"%s\"", viewer_exe.c_str(), out_file.c_str());
                        #else
-                               #define VIEW_CMD "%s '%s' &"
+                               std::string cmd = stringf("%s '%s' %s", viewer_exe.c_str(), out_file.c_str(), background.c_str());
                        #endif
-                       std::string cmd = stringf(VIEW_CMD, viewer_exe.c_str(), out_file.c_str());
-                       #undef VIEW_CMD
                        log("Exec: %s\n", cmd.c_str());
                        if (run_command(cmd) != 0)
                                log_cmd_error("Shell command failed!\n");
                } else
                if (format.empty()) {
-                       std::string cmd = stringf("{ test -f '%s.pid' && fuser -s '%s.pid'; } || ( echo $$ >&3; exec xdot '%s'; ) 3> '%s.pid' &", dot_file.c_str(), dot_file.c_str(), dot_file.c_str(), dot_file.c_str());
+                       #ifdef __APPLE__
+                       std::string cmd = stringf("ps -fu %d | grep -q '[ ]%s' || xdot '%s' %s", getuid(), dot_file.c_str(), dot_file.c_str(), background.c_str());
+                       #else
+                       std::string cmd = stringf("{ test -f '%s.pid' && fuser -s '%s.pid' 2> /dev/null; } || ( echo $$ >&3; exec xdot '%s'; ) 3> '%s.pid' %s", dot_file.c_str(), dot_file.c_str(), dot_file.c_str(), dot_file.c_str(), background.c_str());
+                       #endif
                        log("Exec: %s\n", cmd.c_str());
                        if (run_command(cmd) != 0)
                                log_cmd_error("Shell command failed!\n");
                }
+               #endif
 
                if (flag_pause) {
                #ifdef YOSYS_ENABLE_READLINE
-                       char *input = NULL;
-                       while ((input = readline("Press ENTER to continue (or type 'shell' to open a shell)> ")) != NULL) {
+                       char *input = nullptr;
+                       while ((input = readline("Press ENTER to continue (or type 'shell' to open a shell)> ")) != nullptr) {
                                if (input[strspn(input, " \t\r\n")] == 0)
                                        break;
                                char *p = input + strspn(input, " \t\r\n");