/home/runner/work/LabNeura/LabNeura/src/labneura/eda/netlist/netlist_graph.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/eda/netlist.h" |
2 | | |
3 | | #include <fstream> |
4 | | #include <map> |
5 | | #include <set> |
6 | | #include <sstream> |
7 | | #include <stdexcept> |
8 | | #include <string> |
9 | | #include <vector> |
10 | | |
11 | | namespace labneura::eda { |
12 | | namespace { |
13 | | |
14 | 5.19k | std::string escaped(const std::string& text) { |
15 | 5.19k | std::string out; |
16 | 34.1k | for (const char c : text) { |
17 | 34.1k | if (c == '"' || c == '\\') out += '\\'; |
18 | 34.1k | out += c; |
19 | 34.1k | } |
20 | 5.19k | return out; |
21 | 5.19k | } |
22 | | |
23 | 4.42k | std::string quoted(const std::string& text) { return "\"" + escaped(text) + "\""; } |
24 | | |
25 | 1.14k | bool is_output_pin(const Instance& instance, const std::string& pin) { |
26 | 1.14k | if (instance.get_cell()) { |
27 | 594 | if (const auto* found = instance.get_cell()->find_pin(pin)) { |
28 | 594 | return found->get_direction() == "output"; |
29 | 594 | } |
30 | 594 | } |
31 | 549 | static const std::set<std::string> outputs = { |
32 | 549 | "Y", "Q", "X", "Z", "ZN", "QN", "Q_N", "HI", "LO", "SUM", "COUT", "O"}; |
33 | 549 | return outputs.count(pin) > 0; |
34 | 1.14k | } |
35 | | |
36 | | // Strip a bit select so "bus[3]" and "bus" refer to one label group. |
37 | 515 | std::string base_net(const std::string& net) { |
38 | 515 | const auto bracket = net.find('['); |
39 | 515 | return bracket == std::string::npos ? net : net.substr(0, bracket); |
40 | 515 | } |
41 | | |
42 | | } // namespace |
43 | | |
44 | 45 | std::string to_dot(const Module& module) { |
45 | 45 | std::ostringstream out; |
46 | 45 | out << "digraph " << quoted(module.get_name()) << " {\n rankdir=LR;\n"; |
47 | | |
48 | 45 | std::set<std::string> input_ports; |
49 | 45 | std::set<std::string> output_ports; |
50 | 216 | for (const auto& port : module.get_ports()) { |
51 | 216 | const bool input = port.get_direction() == PortDirection::Input; |
52 | 216 | (input ? input_ports : output_ports).insert(port.get_name()); |
53 | 216 | out << " " << quoted("port:" + port.get_name()) |
54 | 216 | << " [shape=" << (input ? "invhouse" : "house") << ", label=" |
55 | 216 | << quoted(port.get_name()) << "];\n"; |
56 | 216 | } |
57 | 385 | for (const auto& instance : module.get_instances()) { |
58 | 385 | out << " " << quoted(instance.get_name()) << " [shape=box, label=\"" |
59 | 385 | << escaped(instance.get_name()) << "\\n" |
60 | 385 | << escaped(instance.get_cell_type()) << "\"];\n"; |
61 | 385 | } |
62 | | |
63 | 45 | struct Endpoint { |
64 | 45 | std::string node; |
65 | 45 | std::string pin; |
66 | 45 | }; |
67 | 45 | std::map<std::string, std::vector<Endpoint>> drivers; |
68 | 45 | std::map<std::string, std::vector<Endpoint>> sinks; |
69 | | // Nets are matched per bit ("count[2]"), so bits never cross-connect; a port |
70 | | // node stands in for every bit of its port. |
71 | 45 | std::set<std::string> port_nets_seen; |
72 | 385 | for (const auto& instance : module.get_instances()) { |
73 | 1.14k | for (const auto& connection : instance.get_connections()) { |
74 | 1.14k | const auto& net = connection.get_net(); |
75 | 1.14k | auto& side = is_output_pin(instance, connection.get_pin()) ? drivers : sinks; |
76 | 1.14k | side[net].push_back({instance.get_name(), connection.get_pin()}); |
77 | 1.14k | if (port_nets_seen.insert(net).second) { |
78 | 515 | const auto base = base_net(net); |
79 | 515 | if (input_ports.count(base)) drivers[net].push_back({"port:" + base, ""}); |
80 | 515 | if (output_ports.count(base)) sinks[net].push_back({"port:" + base, ""}); |
81 | 515 | } |
82 | 1.14k | } |
83 | 385 | } |
84 | 515 | for (const auto& [net, from] : drivers) { |
85 | 515 | const auto found = sinks.find(net); |
86 | 515 | if (found == sinks.end()) continue; |
87 | 515 | for (const auto& source : from) { |
88 | 890 | for (const auto& target : found->second) { |
89 | 890 | out << " " << quoted(source.node) << " -> " << quoted(target.node) |
90 | 890 | << " [label=" << quoted(net) << ", headlabel=" |
91 | 890 | << quoted(target.pin) << "];\n"; |
92 | 890 | } |
93 | 515 | } |
94 | 515 | } |
95 | 45 | out << "}\n"; |
96 | 45 | return out.str(); |
97 | 45 | } |
98 | | |
99 | 4 | void write_dot(const Module& module, const std::string& path) { |
100 | 4 | std::ofstream file(path); |
101 | 4 | if (!file) throw std::runtime_error("cannot open graph output: " + path); |
102 | 3 | file << to_dot(module); |
103 | 3 | } |
104 | | |
105 | | } // namespace labneura::eda |