/home/runner/work/LabNeura/LabNeura/include/labneura/eda/netlist.h
Line | Count | Source |
1 | | #ifndef LABNEURA_EDA_NETLIST_H |
2 | | #define LABNEURA_EDA_NETLIST_H |
3 | | |
4 | | #include "labneura/eda/cell_library.h" |
5 | | #include "labneura/eda/expression.h" |
6 | | |
7 | | #include <cstddef> |
8 | | #include <cstdint> |
9 | | #include <map> |
10 | | #include <memory> |
11 | | #include <optional> |
12 | | #include <string> |
13 | | #include <utility> |
14 | | #include <vector> |
15 | | |
16 | | namespace labneura::eda { |
17 | | |
18 | | namespace detail { |
19 | | struct ParsedVerilogContext; |
20 | | } |
21 | | |
22 | | enum class PortDirection { |
23 | | Input, |
24 | | Output, |
25 | | InOut |
26 | | }; |
27 | | |
28 | | enum class NetType { |
29 | | Input, |
30 | | Output, |
31 | | InOut, |
32 | | Internal |
33 | | }; |
34 | | |
35 | | class Port { |
36 | | public: |
37 | | Port(std::string name, PortDirection direction, std::size_t width = 1, |
38 | | std::optional<std::int64_t> left_index = std::nullopt, |
39 | | std::optional<std::int64_t> right_index = std::nullopt, |
40 | | std::optional<std::int64_t> unpacked_left_index = std::nullopt, |
41 | | std::optional<std::int64_t> unpacked_right_index = std::nullopt, |
42 | | std::size_t packed_width = 0) |
43 | 5.95k | : name_(std::move(name)), |
44 | 5.95k | direction_(direction), |
45 | 5.95k | width_(width), |
46 | 5.95k | left_index_(left_index), |
47 | 5.95k | right_index_(right_index), |
48 | 5.95k | unpacked_left_index_(unpacked_left_index), |
49 | 5.95k | unpacked_right_index_(unpacked_right_index), |
50 | 5.95k | packed_width_(packed_width == 0 ? width : packed_width) {} |
51 | | |
52 | 9.98k | const std::string& get_name() const noexcept { return name_; } |
53 | 22.7k | PortDirection get_direction() const noexcept { return direction_; } |
54 | 37 | std::size_t get_width() const noexcept { return width_; } |
55 | 61 | const std::optional<std::int64_t>& get_left_index() const noexcept { |
56 | 61 | return left_index_; |
57 | 61 | } |
58 | 61 | const std::optional<std::int64_t>& get_right_index() const noexcept { |
59 | 61 | return right_index_; |
60 | 61 | } |
61 | 63 | const std::optional<std::int64_t>& get_unpacked_left_index() const noexcept { |
62 | 63 | return unpacked_left_index_; |
63 | 63 | } |
64 | 38 | const std::optional<std::int64_t>& get_unpacked_right_index() const noexcept { |
65 | 38 | return unpacked_right_index_; |
66 | 38 | } |
67 | 59 | std::size_t get_packed_width() const noexcept { return packed_width_; } |
68 | | |
69 | | private: |
70 | | std::string name_; |
71 | | PortDirection direction_; |
72 | | std::size_t width_; |
73 | | std::optional<std::int64_t> left_index_; |
74 | | std::optional<std::int64_t> right_index_; |
75 | | std::optional<std::int64_t> unpacked_left_index_; |
76 | | std::optional<std::int64_t> unpacked_right_index_; |
77 | | std::size_t packed_width_; |
78 | | }; |
79 | | |
80 | | class Net { |
81 | | public: |
82 | | Net(std::string name, NetType kind, std::size_t width = 1, |
83 | | std::optional<std::int64_t> left_index = std::nullopt, |
84 | | std::optional<std::int64_t> right_index = std::nullopt, |
85 | | std::optional<std::int64_t> unpacked_left_index = std::nullopt, |
86 | | std::optional<std::int64_t> unpacked_right_index = std::nullopt, |
87 | | std::size_t packed_width = 0) |
88 | 79.6k | : name_(std::move(name)), |
89 | 79.6k | kind_(kind), |
90 | 79.6k | width_(width), |
91 | 79.6k | left_index_(left_index), |
92 | 79.6k | right_index_(right_index), |
93 | 79.6k | unpacked_left_index_(unpacked_left_index), |
94 | 79.6k | unpacked_right_index_(unpacked_right_index), |
95 | 79.6k | packed_width_(packed_width == 0 ? width : packed_width) {} |
96 | | |
97 | 117k | const std::string& get_name() const noexcept { return name_; } |
98 | 87 | NetType get_kind() const noexcept { return kind_; } |
99 | 31 | std::size_t get_width() const noexcept { return width_; } |
100 | 31 | const std::optional<std::int64_t>& get_left_index() const noexcept { |
101 | 31 | return left_index_; |
102 | 31 | } |
103 | 31 | const std::optional<std::int64_t>& get_right_index() const noexcept { |
104 | 31 | return right_index_; |
105 | 31 | } |
106 | 29 | const std::optional<std::int64_t>& get_unpacked_left_index() const noexcept { |
107 | 29 | return unpacked_left_index_; |
108 | 29 | } |
109 | 29 | const std::optional<std::int64_t>& get_unpacked_right_index() const noexcept { |
110 | 29 | return unpacked_right_index_; |
111 | 29 | } |
112 | 29 | std::size_t get_packed_width() const noexcept { return packed_width_; } |
113 | | |
114 | | private: |
115 | | std::string name_; |
116 | | NetType kind_; |
117 | | std::size_t width_; |
118 | | std::optional<std::int64_t> left_index_; |
119 | | std::optional<std::int64_t> right_index_; |
120 | | std::optional<std::int64_t> unpacked_left_index_; |
121 | | std::optional<std::int64_t> unpacked_right_index_; |
122 | | std::size_t packed_width_; |
123 | | }; |
124 | | |
125 | | class PinConnection { |
126 | | public: |
127 | | PinConnection(std::string pin, std::string net) |
128 | 295k | : pin_(std::move(pin)), net_(std::move(net)) {} |
129 | | |
130 | 1.09M | const std::string& get_pin() const noexcept { return pin_; } |
131 | 866k | const std::string& get_net() const noexcept { return net_; } |
132 | | |
133 | | private: |
134 | | std::string pin_; |
135 | | std::string net_; |
136 | | }; |
137 | | |
138 | | struct UserDefinedPrimitiveRow { |
139 | | std::string inputs; |
140 | | char output; |
141 | | }; |
142 | | |
143 | | class Instance { |
144 | | public: |
145 | | Instance(std::string name, std::string cell_type, |
146 | | std::vector<PinConnection> connections, |
147 | | std::optional<StandardCell> cell = std::nullopt, |
148 | | std::map<std::string, std::string> parameters = {}, |
149 | | bool user_defined_primitive = false, |
150 | | std::vector<UserDefinedPrimitiveRow> primitive_table = {}, |
151 | | bool sequential_primitive = false) |
152 | 93.9k | : name_(std::move(name)), |
153 | 93.9k | cell_type_(std::move(cell_type)), |
154 | 93.9k | connections_(std::move(connections)), |
155 | 93.9k | cell_(std::move(cell)), |
156 | 93.9k | parameters_(std::move(parameters)), |
157 | 93.9k | user_defined_primitive_(user_defined_primitive), |
158 | 93.9k | primitive_table_(std::move(primitive_table)), |
159 | 93.9k | sequential_primitive_(sequential_primitive) {} |
160 | | |
161 | 6.28k | const std::string& get_name() const noexcept { return name_; } |
162 | 429k | const std::string& get_cell_type() const noexcept { return cell_type_; } |
163 | 360k | const std::vector<PinConnection>& get_connections() const noexcept { |
164 | 360k | return connections_; |
165 | 360k | } |
166 | 7.49k | const std::optional<StandardCell>& get_cell() const noexcept { return cell_; } |
167 | | // Cell parameters, e.g. the truth table "INIT" of an FPGA LUT. |
168 | 61.5k | const std::map<std::string, std::string>& get_parameters() const noexcept { |
169 | 61.5k | return parameters_; |
170 | 61.5k | } |
171 | 125 | bool is_user_defined_primitive() const noexcept { |
172 | 125 | return user_defined_primitive_; |
173 | 125 | } |
174 | | const std::vector<UserDefinedPrimitiveRow>& get_primitive_table() const |
175 | 23 | noexcept { |
176 | 23 | return primitive_table_; |
177 | 23 | } |
178 | 22 | bool is_sequential_primitive() const noexcept { |
179 | 22 | return sequential_primitive_; |
180 | 22 | } |
181 | | |
182 | | private: |
183 | | std::string name_; |
184 | | std::string cell_type_; |
185 | | std::vector<PinConnection> connections_; |
186 | | std::optional<StandardCell> cell_; |
187 | | std::map<std::string, std::string> parameters_; |
188 | | bool user_defined_primitive_ = false; |
189 | | std::vector<UserDefinedPrimitiveRow> primitive_table_; |
190 | | bool sequential_primitive_ = false; |
191 | | }; |
192 | | |
193 | | class ContinuousAssignment { |
194 | | public: |
195 | | ContinuousAssignment(Expression lhs, Expression rhs) |
196 | 1.89k | : lhs_(std::move(lhs)), rhs_(std::move(rhs)) {} |
197 | | |
198 | 8 | const std::string& get_lhs() const noexcept { return lhs_.get_source(); } |
199 | 6 | const std::string& get_rhs() const noexcept { return rhs_.get_source(); } |
200 | 354 | const Expression& get_lhs_expression() const noexcept { return lhs_; } |
201 | 179 | const Expression& get_rhs_expression() const noexcept { return rhs_; } |
202 | | |
203 | | private: |
204 | | Expression lhs_; |
205 | | Expression rhs_; |
206 | | }; |
207 | | |
208 | | class Project; |
209 | | |
210 | | class Module { |
211 | | public: |
212 | 199 | Module() = default; |
213 | | Module(std::string name, std::vector<Port> ports, std::vector<Net> nets, |
214 | | std::vector<Instance> instances, |
215 | | std::vector<ContinuousAssignment> assignments = {}, |
216 | | bool behavioral = false, std::string hierarchy_instance_name = {}, |
217 | | std::vector<Module> child_modules = {}) |
218 | 945 | : name_(std::move(name)), |
219 | 945 | ports_(std::move(ports)), |
220 | 945 | nets_(std::move(nets)), |
221 | 945 | instances_(std::move(instances)), |
222 | 945 | assignments_(std::move(assignments)), |
223 | 945 | behavioral_(behavioral), |
224 | 945 | hierarchy_instance_name_(std::move(hierarchy_instance_name)), |
225 | 945 | child_modules_(std::move(child_modules)) {} |
226 | | |
227 | 920 | const std::string& get_name() const noexcept { return name_; } |
228 | | // Empty for a standalone module definition; otherwise the instance name |
229 | | // that connects this hierarchy node to its parent. |
230 | 370 | const std::string& get_hierarchy_instance_name() const noexcept { |
231 | 370 | return hierarchy_instance_name_; |
232 | 370 | } |
233 | 4.42k | const std::vector<Port>& get_ports() const noexcept { return ports_; } |
234 | 678 | const std::vector<Net>& get_nets() const noexcept { return nets_; } |
235 | 8.60k | const std::vector<Instance>& get_instances() const noexcept { |
236 | 8.60k | return instances_; |
237 | 8.60k | } |
238 | | // Elaborated child module instances, in the same scope as get_instances(). |
239 | 955 | const std::vector<Module>& get_child_modules() const noexcept { |
240 | 955 | return child_modules_; |
241 | 955 | } |
242 | 714 | const std::vector<ContinuousAssignment>& get_assignments() const noexcept { |
243 | 714 | return assignments_; |
244 | 714 | } |
245 | 10 | std::size_t get_port_count() const noexcept { return ports_.size(); } |
246 | 6 | std::size_t get_wire_count() const noexcept { |
247 | 6 | std::size_t count = 0; |
248 | 16 | for (const auto& net : nets_) { |
249 | 16 | if (net.get_kind() == NetType::Internal) ++count; |
250 | 16 | } |
251 | 6 | return count; |
252 | 6 | } |
253 | 13 | std::size_t get_instance_count() const noexcept { return instances_.size(); } |
254 | | // True when the RTL contains always blocks that generic_map must lower. |
255 | 763 | bool is_behavioral() const noexcept { return behavioral_; } |
256 | | private: |
257 | | std::string name_; |
258 | | std::vector<Port> ports_; |
259 | | std::vector<Net> nets_; |
260 | | std::vector<Instance> instances_; |
261 | | std::vector<ContinuousAssignment> assignments_; |
262 | | bool behavioral_ = false; |
263 | | std::string hierarchy_instance_name_; |
264 | | std::vector<Module> child_modules_; |
265 | | std::shared_ptr<detail::ParsedVerilogContext> parsed_context_; |
266 | | |
267 | | friend Module parse_verilog(const std::string& path); |
268 | | friend Module read_verilog(const std::string& path); |
269 | | friend Project parse_verilog_directory(const std::string& directory); |
270 | | friend Module generic_map(const Module& rtl_module); |
271 | | friend Module generic_map( |
272 | | const Project& project, const std::string& top, |
273 | | const std::map<std::string, std::string>& parameters); |
274 | | }; |
275 | | |
276 | | class Project { |
277 | | public: |
278 | 12 | Project() = default; |
279 | | |
280 | 31 | const std::vector<Module>& get_modules() const noexcept { return modules_; } |
281 | 8 | std::size_t get_module_count() const noexcept { return modules_.size(); } |
282 | 2 | std::size_t get_total_port_count() const noexcept { |
283 | 2 | std::size_t count = 0; |
284 | 5 | for (const auto& module : modules_) count += module.get_port_count(); |
285 | 2 | return count; |
286 | 2 | } |
287 | 1 | std::size_t get_total_wire_count() const noexcept { |
288 | 1 | std::size_t count = 0; |
289 | 3 | for (const auto& module : modules_) count += module.get_wire_count(); |
290 | 1 | return count; |
291 | 1 | } |
292 | 3 | std::size_t get_total_instance_count() const noexcept { |
293 | 3 | std::size_t count = 0; |
294 | 10 | for (const auto& module : modules_) count += module.get_instance_count(); |
295 | 3 | return count; |
296 | 3 | } |
297 | 2 | const std::string& get_ast_json() const noexcept { return ast_json_; } |
298 | 2 | const std::vector<std::string>& get_source_files() const noexcept { |
299 | 2 | return source_files_; |
300 | 2 | } |
301 | | |
302 | | private: |
303 | | std::vector<Module> modules_; |
304 | | std::string ast_json_; |
305 | | std::vector<std::string> source_files_; |
306 | | std::shared_ptr<detail::ParsedVerilogContext> parsed_context_; |
307 | | |
308 | | friend Project parse_verilog_directory(const std::string& directory); |
309 | | friend Module generic_map( |
310 | | const Project& project, const std::string& top, |
311 | | const std::map<std::string, std::string>& parameters); |
312 | | friend Module generic_map(const Module& rtl_module); |
313 | | }; |
314 | | |
315 | | Module parse_verilog(const std::string& path); |
316 | | Project parse_verilog_directory(const std::string& directory); |
317 | | // Reads a structural Verilog netlist into the same parsed Module model. |
318 | | Module read_verilog(const std::string& path); |
319 | | // Lowers a parsed module, or the hierarchy below `top` in a parsed project, |
320 | | // into technology-independent generic cells. `parameters` overrides top-level |
321 | | // module parameters (for example {"ENABLE_MUL": "1"}). |
322 | | Module generic_map(const Module& rtl_module); |
323 | | Module generic_map(const Project& project, const std::string& top = "", |
324 | | const std::map<std::string, std::string>& parameters = {}); |
325 | | Module tech_map(const Module& generic_module, |
326 | | const StandardCellLibrary& cell_library); |
327 | | Module tech_map(const Project& project, const StandardCellLibrary& cell_library, |
328 | | const std::string& top = "", |
329 | | const std::map<std::string, std::string>& parameters = {}); |
330 | | // Maps a generic netlist onto `lut_size`-input LUTs (2..6) with a depth-optimal, |
331 | | // area-recovering cut-based mapper. Flops, latches and tristates pass through. |
332 | | // LUT cells are named LUT<n>, use pins I0..I<n-1> and O, and carry the truth |
333 | | // table as hex parameter "INIT" (bit i = output for input pattern i, I0 = LSB). |
334 | | // Every mapped instance carries its `cell` (area, pins, timing arcs) taken from |
335 | | // `library`, which defaults to the built-in generic FPGA architecture model. |
336 | | const StandardCellLibrary& get_default_fpga_library(); |
337 | | Module fpga_map(const Module& generic_module, std::size_t lut_size = 4, |
338 | | const StandardCellLibrary& library = get_default_fpga_library()); |
339 | | Module fpga_map(const Project& project, const std::string& top = "", |
340 | | const std::map<std::string, std::string>& parameters = {}, |
341 | | std::size_t lut_size = 4, |
342 | | const StandardCellLibrary& library = get_default_fpga_library()); |
343 | | // Writes a LUT netlist as BLIF for tools such as Yosys, ABC or VPR. |
344 | | std::string to_blif(const Module& module); |
345 | | void write_blif(const Module& module, const std::string& path); |
346 | | std::string to_json(const Module& module); |
347 | | void write_json(const Module& module, const std::string& path); |
348 | | Module read_json(const std::string& path); |
349 | | std::string to_verilog(const Module& module); |
350 | | void write_verilog(const Module& module, const std::string& path); |
351 | | // Graphviz DOT view: cells and ports are nodes, nets are driver-to-sink edges. |
352 | | std::string to_dot(const Module& module); |
353 | | void write_dot(const Module& module, const std::string& path); |
354 | | |
355 | | } // namespace labneura::eda |
356 | | |
357 | | #endif |