/home/runner/work/LabNeura/LabNeura/src/labneura/eda/mapping/fpga/fpga_mapping.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/eda/netlist.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <cctype> |
5 | | #include <cstdint> |
6 | | #include <iomanip> |
7 | | #include <iterator> |
8 | | #include <map> |
9 | | #include <optional> |
10 | | #include <regex> |
11 | | #include <set> |
12 | | #include <sstream> |
13 | | #include <stdexcept> |
14 | | #include <string> |
15 | | #include <unordered_map> |
16 | | #include <utility> |
17 | | #include <vector> |
18 | | |
19 | | namespace labneura::eda { |
20 | | namespace { |
21 | | |
22 | | constexpr std::size_t kCutsPerNode = 8; |
23 | | |
24 | | enum class Gate { And, Or, Xor, Nand, Nor, Xnor, Maj, Not, Buf, Mux }; |
25 | | |
26 | 71.6k | std::string upper(std::string text) { |
27 | 258k | for (auto& character : text) { |
28 | 258k | character = static_cast<char>(std::toupper(static_cast<unsigned char>(character))); |
29 | 258k | } |
30 | 71.6k | return text; |
31 | 71.6k | } |
32 | | |
33 | 205k | bool is_output_pin(const std::string& pin) { |
34 | 205k | return pin == "Y" || pin == "Q" || pin == "X" || pin == "Z" || pin == "ZN" || |
35 | 205k | pin == "QN" || pin == "Q_N"; |
36 | 205k | } |
37 | | |
38 | 35.8k | bool combinational_gate(const std::string& type, Gate& gate) { |
39 | 35.8k | static const std::regex wide(R"(^(AND|OR|XOR|NAND|NOR|XNOR|MAJ)([2-9][0-9]*)?$)"); |
40 | 35.8k | const auto name = upper(type); |
41 | 35.8k | std::smatch match; |
42 | 35.8k | if (std::regex_match(name, match, wide)) { |
43 | 14.8k | const auto& kind = match[1].str(); |
44 | 14.8k | gate = kind == "AND" ? Gate::And : kind == "OR" ? Gate::Or |
45 | 4.68k | : kind == "XOR" ? Gate::Xor : kind == "NAND" ? Gate::Nand |
46 | 0 | : kind == "NOR" ? Gate::Nor : kind == "XNOR" ? Gate::Xnor : Gate::Maj; |
47 | 14.8k | return true; |
48 | 14.8k | } |
49 | 20.9k | if (name == "INV" || name == "NOT") { gate = Gate::Not; return true; } |
50 | 19.8k | if (name == "BUF") { gate = Gate::Buf; return true; } |
51 | 9.31k | if (name == "MUX2") { gate = Gate::Mux; return true; } |
52 | 1.45k | return false; |
53 | 9.31k | } |
54 | | |
55 | | // One combinational gate of the input netlist. |
56 | | struct Node { |
57 | | Gate gate; |
58 | | std::string output; |
59 | | std::vector<int> inputs; // node ids; constants and primary inputs are leaves |
60 | | int depth = 0; |
61 | | double flow = 0.0; |
62 | | std::vector<std::vector<int>> cuts; |
63 | | std::vector<int> best; |
64 | | bool required = false; |
65 | | int fanout = 0; |
66 | | }; |
67 | | |
68 | | class Mapper { |
69 | | public: |
70 | | Mapper(const Module& module, std::size_t k, const StandardCellLibrary& library) |
71 | 95 | : module_(module), k_(k), library_(library) {} |
72 | | |
73 | 95 | Module run() { |
74 | 95 | classify(); |
75 | 95 | order(); |
76 | 95 | enumerate_cuts(); |
77 | 95 | select(); |
78 | 95 | return emit(); |
79 | 95 | } |
80 | | |
81 | | private: |
82 | | const Module& module_; |
83 | | std::size_t k_; |
84 | | const StandardCellLibrary& library_; |
85 | | |
86 | 12.2k | std::optional<StandardCell> cell_for(const std::string& type) const { |
87 | 12.2k | if (const auto* cell = library_.find_cell(type)) return *cell; |
88 | 165 | return std::nullopt; |
89 | 12.2k | } |
90 | | |
91 | | std::vector<Node> nodes_; |
92 | | std::unordered_map<std::string, int> node_of_net_; // comb output net -> node |
93 | | std::unordered_map<std::string, int> constant_; // net -> 0/1 |
94 | | std::vector<const Instance*> passthrough_; |
95 | | std::vector<std::string> sink_nets_; // nets that must exist |
96 | | std::vector<std::vector<std::string>> leaf_nets_; // per node: net names for leaves |
97 | | std::vector<int> topo_; |
98 | | // A leaf is identified by its net name; interior nodes by node id. |
99 | | std::unordered_map<std::string, int> leaf_id_; |
100 | | std::vector<std::string> leaf_name_; |
101 | | |
102 | 25.1k | int leaf(const std::string& net) { |
103 | 25.1k | const auto found = leaf_id_.find(net); |
104 | 25.1k | if (found != leaf_id_.end()) return found->second; |
105 | 5.09k | const int id = -1 - static_cast<int>(leaf_name_.size()); |
106 | 5.09k | leaf_id_.emplace(net, id); |
107 | 5.09k | leaf_name_.push_back(net); |
108 | 5.09k | return id; |
109 | 25.1k | } |
110 | | |
111 | 95 | void classify() { |
112 | 95 | std::vector<std::pair<const Instance*, Gate>> gates; |
113 | 35.8k | for (const auto& instance : module_.get_instances()) { |
114 | 35.8k | Gate gate; |
115 | 35.8k | const auto type = upper(instance.get_cell_type()); |
116 | 35.8k | if (type == "TIEHI" || type == "TIELO") { |
117 | 58 | for (const auto& connection : instance.get_connections()) { |
118 | 58 | if (is_output_pin(connection.get_pin())) { |
119 | 58 | constant_[connection.get_net()] = type == "TIEHI" ? 1 : 0; |
120 | 58 | } |
121 | 58 | } |
122 | 58 | passthrough_.push_back(&instance); |
123 | 35.8k | } else if (combinational_gate(instance.get_cell_type(), gate)) { |
124 | 34.3k | gates.emplace_back(&instance, gate); |
125 | 34.3k | } else { |
126 | 1.45k | passthrough_.push_back(&instance); |
127 | 6.02k | for (const auto& connection : instance.get_connections()) { |
128 | 6.02k | if (!is_output_pin(connection.get_pin())) { |
129 | 4.60k | sink_nets_.push_back(connection.get_net()); |
130 | 4.60k | } |
131 | 6.02k | } |
132 | 1.45k | } |
133 | 35.8k | } |
134 | 1.20k | for (const auto& port : module_.get_ports()) { |
135 | 1.20k | if (port.get_direction() == PortDirection::Input) continue; |
136 | 521 | output_ports_.insert(port.get_name()); |
137 | 521 | } |
138 | 95 | nodes_.reserve(gates.size()); |
139 | 34.3k | for (const auto& [instance, gate] : gates) { |
140 | 34.3k | Node node; |
141 | 34.3k | node.gate = gate; |
142 | 99.5k | for (const auto& connection : instance->get_connections()) { |
143 | 99.5k | if (is_output_pin(connection.get_pin())) node.output = connection.get_net(); |
144 | 99.5k | } |
145 | 34.3k | if (node.output.empty()) { |
146 | 0 | throw std::runtime_error("gate '" + instance->get_name() + "' has no output"); |
147 | 0 | } |
148 | 34.3k | if (node_of_net_.count(node.output)) { |
149 | 0 | throw std::runtime_error("net '" + node.output + "' has several drivers"); |
150 | 0 | } |
151 | 34.3k | node_of_net_.emplace(node.output, static_cast<int>(nodes_.size())); |
152 | 34.3k | nodes_.push_back(std::move(node)); |
153 | 34.3k | } |
154 | 34.4k | for (std::size_t i = 0; i < gates.size(); ++i) { |
155 | 34.3k | const auto& instance = *gates[i].first; |
156 | 34.3k | std::vector<std::pair<std::string, int>> pins; |
157 | 99.5k | for (const auto& connection : instance.get_connections()) { |
158 | 99.5k | if (is_output_pin(connection.get_pin())) continue; |
159 | 65.1k | const auto& net = connection.get_net(); |
160 | 65.1k | const auto driver = node_of_net_.find(net); |
161 | 65.1k | const int source = driver != node_of_net_.end() ? driver->second : leaf(net); |
162 | 65.1k | pins.emplace_back(connection.get_pin(), source); |
163 | 65.1k | } |
164 | 34.3k | if (nodes_[i].gate == Gate::Mux) { |
165 | | // Fixed order: S, A1, A0. |
166 | 15.7k | std::sort(pins.begin(), pins.end(), [](const auto& a, const auto& b) { |
167 | 31.4k | const auto rank = [](const std::string& pin) { |
168 | 31.4k | return pin == "S" ? 0 : pin == "A1" ? 1 : 2; |
169 | 31.4k | }; |
170 | 15.7k | return rank(a.first) < rank(b.first); |
171 | 15.7k | }); |
172 | 7.86k | } |
173 | 65.1k | for (const auto& pin : pins) nodes_[i].inputs.push_back(pin.second); |
174 | 34.3k | } |
175 | 95 | const std::size_t roots = gates.size(); |
176 | 34.4k | for (std::size_t i = 0; i < roots; ++i) split_wide(i); |
177 | 95 | } |
178 | | |
179 | | // Rewrites an n-input AND/OR/XOR family gate as a balanced tree of 2-input gates |
180 | | // so that the cut enumeration can repack it into LUTs of any size. |
181 | 34.3k | void split_wide(std::size_t index) { |
182 | 34.3k | const Gate gate = nodes_[index].gate; |
183 | 34.3k | const bool family = gate == Gate::And || gate == Gate::Or || gate == Gate::Xor || |
184 | 34.3k | gate == Gate::Nand || gate == Gate::Nor || gate == Gate::Xnor; |
185 | 34.3k | if (!family || nodes_[index].inputs.size() <= 2) return; |
186 | 100 | const Gate inner = (gate == Gate::And || gate == Gate::Nand) ? Gate::And |
187 | 100 | : (gate == Gate::Or || gate == Gate::Nor) ? Gate::Or : Gate::Xor; |
188 | 100 | std::vector<int> level = nodes_[index].inputs; |
189 | 232 | while (level.size() > 2) { |
190 | 132 | std::vector<int> next; |
191 | 360 | for (std::size_t i = 0; i + 1 < level.size(); i += 2) { |
192 | 228 | Node node; |
193 | 228 | node.gate = inner; |
194 | 228 | node.output = "__fpga_t" + std::to_string(nodes_.size()); |
195 | 228 | node.inputs = {level[i], level[i + 1]}; |
196 | 228 | nodes_.push_back(std::move(node)); |
197 | 228 | next.push_back(static_cast<int>(nodes_.size()) - 1); |
198 | 228 | } |
199 | 132 | if (level.size() % 2) next.push_back(level.back()); |
200 | 132 | level = std::move(next); |
201 | 132 | } |
202 | 100 | nodes_[index].inputs = level; |
203 | 100 | } |
204 | | |
205 | 95 | void order() { |
206 | | // Kahn's algorithm so that cones of any depth are handled iteratively. |
207 | 95 | std::vector<int> pending(nodes_.size(), 0); |
208 | 95 | std::vector<std::vector<int>> users(nodes_.size()); |
209 | 34.6k | for (std::size_t i = 0; i < nodes_.size(); ++i) { |
210 | 65.3k | for (const int input : nodes_[i].inputs) { |
211 | 65.3k | if (input >= 0) { |
212 | 40.2k | ++pending[i]; |
213 | 40.2k | users[static_cast<std::size_t>(input)].push_back(static_cast<int>(i)); |
214 | 40.2k | } |
215 | 65.3k | if (input >= 0) ++nodes_[static_cast<std::size_t>(input)].fanout; |
216 | 65.3k | } |
217 | 34.5k | } |
218 | 95 | std::vector<int> ready; |
219 | 34.6k | for (std::size_t i = 0; i < nodes_.size(); ++i) { |
220 | 34.5k | if (pending[i] == 0) ready.push_back(static_cast<int>(i)); |
221 | 34.5k | } |
222 | 34.6k | while (!ready.empty()) { |
223 | 34.5k | const int id = ready.back(); |
224 | 34.5k | ready.pop_back(); |
225 | 34.5k | topo_.push_back(id); |
226 | 40.2k | for (const int user : users[static_cast<std::size_t>(id)]) { |
227 | 40.2k | if (--pending[static_cast<std::size_t>(user)] == 0) ready.push_back(user); |
228 | 40.2k | } |
229 | 34.5k | } |
230 | 95 | if (topo_.size() != nodes_.size()) { |
231 | 0 | throw std::runtime_error("fpga_map: combinational loop in the netlist"); |
232 | 0 | } |
233 | 95 | } |
234 | | |
235 | | std::set<std::string> output_ports_; |
236 | | |
237 | 221k | bool is_constant_leaf(int id) const { |
238 | 221k | return id < 0 && constant_.count(leaf_name_[static_cast<std::size_t>(-1 - id)]); |
239 | 221k | } |
240 | | |
241 | 16.6M | double leaf_flow(int id) const { |
242 | 16.6M | return id >= 0 ? nodes_[static_cast<std::size_t>(id)].flow / |
243 | 9.34M | std::max(1, nodes_[static_cast<std::size_t>(id)].fanout) |
244 | 16.6M | : 0.0; |
245 | 16.6M | } |
246 | 19.7M | int leaf_depth(int id) const { |
247 | 19.7M | return id >= 0 ? nodes_[static_cast<std::size_t>(id)].depth : 0; |
248 | 19.7M | } |
249 | | |
250 | | static bool merge(const std::vector<int>& a, const std::vector<int>& b, |
251 | 1.22M | std::size_t limit, std::vector<int>& out) { |
252 | 1.22M | out.clear(); |
253 | 1.22M | std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out)); |
254 | 1.22M | return out.size() <= limit; |
255 | 1.22M | } |
256 | | |
257 | 95 | void enumerate_cuts() { |
258 | 34.5k | for (const int id : topo_) { |
259 | 34.5k | auto& node = nodes_[static_cast<std::size_t>(id)]; |
260 | 34.5k | std::vector<std::vector<int>> partial = {{}}; |
261 | 65.3k | for (const int input : node.inputs) { |
262 | 65.3k | std::vector<std::vector<int>> choices; |
263 | 65.3k | if (is_constant_leaf(input)) { |
264 | 8.77k | choices.push_back({}); |
265 | 56.6k | } else { |
266 | 56.6k | choices.push_back({input}); |
267 | 56.6k | if (input >= 0) { |
268 | 234k | for (const auto& cut : nodes_[static_cast<std::size_t>(input)].cuts) { |
269 | 234k | choices.push_back(cut); |
270 | 234k | } |
271 | 40.2k | } |
272 | 56.6k | } |
273 | 65.3k | std::vector<std::vector<int>> next; |
274 | 65.3k | std::vector<int> merged; |
275 | 203k | for (const auto& base : partial) { |
276 | 1.22M | for (const auto& choice : choices) { |
277 | 1.22M | if (!merge(base, choice, k_, merged)) continue; |
278 | 478k | next.push_back(merged); |
279 | 478k | } |
280 | 203k | } |
281 | 65.3k | std::sort(next.begin(), next.end()); |
282 | 65.3k | next.erase(std::unique(next.begin(), next.end()), next.end()); |
283 | 65.3k | partial = rank(std::move(next), 4 * kCutsPerNode); |
284 | 65.3k | } |
285 | 34.5k | node.cuts = rank(std::move(partial), kCutsPerNode); |
286 | 34.5k | if (node.cuts.empty()) { |
287 | 0 | throw std::runtime_error("fpga_map: no feasible cut for net " + node.output); |
288 | 0 | } |
289 | 34.5k | node.best = node.cuts.front(); |
290 | 34.5k | node.depth = depth_of(node.best); |
291 | 34.5k | node.flow = flow_of(node.best); |
292 | 34.5k | } |
293 | 95 | } |
294 | | |
295 | 4.13M | int depth_of(const std::vector<int>& cut) const { |
296 | 4.13M | int depth = 0; |
297 | 19.7M | for (const int leaf_id : cut) depth = std::max(depth, leaf_depth(leaf_id)); |
298 | 4.13M | return depth + 1; |
299 | 4.13M | } |
300 | 3.43M | double flow_of(const std::vector<int>& cut) const { |
301 | 3.43M | double flow = 1.0; |
302 | 16.6M | for (const int leaf_id : cut) flow += leaf_flow(leaf_id); |
303 | 3.43M | return flow; |
304 | 3.43M | } |
305 | | |
306 | | // Best first: lowest depth, then area flow, then fewest leaves. |
307 | | std::vector<std::vector<int>> rank(std::vector<std::vector<int>> cuts, |
308 | 99.9k | std::size_t keep) const { |
309 | 2.04M | std::stable_sort(cuts.begin(), cuts.end(), [&](const auto& a, const auto& b) { |
310 | 2.04M | const int da = depth_of(a), db = depth_of(b); |
311 | 2.04M | if (da != db) return da < db; |
312 | 1.69M | const double fa = flow_of(a), fb = flow_of(b); |
313 | 1.69M | if (fa != fb) return fa < fb; |
314 | 244k | return a.size() < b.size(); |
315 | 1.69M | }); |
316 | 99.9k | if (cuts.size() > keep) cuts.resize(keep); |
317 | 99.9k | return cuts; |
318 | 99.9k | } |
319 | | |
320 | 8.32k | void require(const std::string& net, std::vector<int>& stack) { |
321 | 8.32k | const auto found = node_of_net_.find(net); |
322 | 8.32k | if (found == node_of_net_.end()) return; |
323 | 5.05k | auto& node = nodes_[static_cast<std::size_t>(found->second)]; |
324 | 5.05k | if (!node.required) { |
325 | 5.01k | node.required = true; |
326 | 5.01k | stack.push_back(found->second); |
327 | 5.01k | } |
328 | 5.05k | } |
329 | | |
330 | 95 | void select() { |
331 | 95 | std::vector<int> stack; |
332 | 4.60k | for (const auto& net : sink_nets_) require(net, stack); |
333 | 34.3k | for (const auto& [net, id] : node_of_net_) { |
334 | 34.3k | const auto bracket = net.find('['); |
335 | 34.3k | if (output_ports_.count(bracket == std::string::npos ? net : net.substr(0, bracket))) { |
336 | 3.72k | require(net, stack); |
337 | 3.72k | } |
338 | 34.3k | } |
339 | 10.8k | while (!stack.empty()) { |
340 | 10.7k | const int id = stack.back(); |
341 | 10.7k | stack.pop_back(); |
342 | 37.9k | for (const int cut_leaf : nodes_[static_cast<std::size_t>(id)].best) { |
343 | 37.9k | if (cut_leaf >= 0) { |
344 | 15.6k | auto& inner = nodes_[static_cast<std::size_t>(cut_leaf)]; |
345 | 15.6k | if (!inner.required) { |
346 | 5.73k | inner.required = true; |
347 | 5.73k | stack.push_back(cut_leaf); |
348 | 5.73k | } |
349 | 15.6k | } |
350 | 37.9k | } |
351 | 10.7k | } |
352 | 95 | } |
353 | | |
354 | | // 64-bit parallel evaluation: bit p of a word is the value for input pattern p. |
355 | | static constexpr std::uint64_t kPattern[6] = { |
356 | | 0xAAAAAAAAAAAAAAAAULL, 0xCCCCCCCCCCCCCCCCULL, 0xF0F0F0F0F0F0F0F0ULL, |
357 | | 0xFF00FF00FF00FF00ULL, 0xFFFF0000FFFF0000ULL, 0xFFFFFFFF00000000ULL}; |
358 | | |
359 | 55.5k | std::uint64_t apply(const Node& node, const std::vector<std::uint64_t>& in) const { |
360 | 55.5k | std::uint64_t value = 0; |
361 | 55.5k | switch (node.gate) { |
362 | 23.5k | case Gate::And: case Gate::Nand: |
363 | 23.5k | value = ~0ULL; |
364 | 47.0k | for (const auto word : in) value &= word; |
365 | 23.5k | return node.gate == Gate::And ? value : ~value; |
366 | 4.01k | case Gate::Or: case Gate::Nor: |
367 | 8.03k | for (const auto word : in) value |= word; |
368 | 4.01k | return node.gate == Gate::Or ? value : ~value; |
369 | 5.12k | case Gate::Xor: case Gate::Xnor: |
370 | 10.2k | for (const auto word : in) value ^= word; |
371 | 5.12k | return node.gate == Gate::Xor ? value : ~value; |
372 | 0 | case Gate::Maj: { |
373 | 0 | std::uint64_t result = 0; |
374 | 0 | for (std::size_t bit = 0; bit < 64; ++bit) { |
375 | 0 | std::size_t ones = 0; |
376 | 0 | for (const auto word : in) ones += (word >> bit) & 1U; |
377 | 0 | if (ones * 2 > in.size()) result |= 1ULL << bit; |
378 | 0 | } |
379 | 0 | return result; |
380 | 5.12k | } |
381 | 9.84k | case Gate::Not: return ~in.at(0); |
382 | 2.78k | case Gate::Buf: return in.at(0); |
383 | 10.2k | case Gate::Mux: return (in.at(0) & in.at(1)) | (~in.at(0) & in.at(2)); |
384 | 55.5k | } |
385 | 0 | return 0; |
386 | 55.5k | } |
387 | | |
388 | 10.7k | std::uint64_t truth_table(int root, const std::vector<int>& cut) const { |
389 | 10.7k | std::unordered_map<int, std::uint64_t> value; |
390 | 48.6k | for (std::size_t i = 0; i < cut.size(); ++i) value[cut[i]] = kPattern[i]; |
391 | | // Iterative post-order over the cone. |
392 | 10.7k | std::vector<std::pair<int, bool>> stack = {{root, false}}; |
393 | 122k | while (!stack.empty()) { |
394 | 111k | const auto [id, expanded] = stack.back(); |
395 | 111k | stack.pop_back(); |
396 | 111k | if (value.count(id)) continue; |
397 | 111k | const auto& node = nodes_[static_cast<std::size_t>(id)]; |
398 | 111k | if (!expanded) { |
399 | 55.5k | stack.emplace_back(id, true); |
400 | 108k | for (const int input : node.inputs) { |
401 | 108k | if (!value.count(input) && !is_constant_leaf(input)) { |
402 | 45.3k | stack.emplace_back(input, false); |
403 | 45.3k | } |
404 | 108k | } |
405 | 55.5k | continue; |
406 | 55.5k | } |
407 | 55.5k | std::vector<std::uint64_t> in; |
408 | 108k | for (const int input : node.inputs) { |
409 | 108k | if (is_constant_leaf(input)) { |
410 | 2.43k | in.push_back(constant_.at(leaf_name_[static_cast<std::size_t>(-1 - input)]) |
411 | 2.43k | ? ~0ULL : 0ULL); |
412 | 106k | } else { |
413 | 106k | in.push_back(value.at(input)); |
414 | 106k | } |
415 | 108k | } |
416 | 55.5k | value[id] = apply(node, in); |
417 | 55.5k | } |
418 | 10.7k | return value.at(root); |
419 | 10.7k | } |
420 | | |
421 | 37.9k | std::string net_name(int id) const { |
422 | 37.9k | return id >= 0 ? nodes_[static_cast<std::size_t>(id)].output |
423 | 37.9k | : leaf_name_[static_cast<std::size_t>(-1 - id)]; |
424 | 37.9k | } |
425 | | |
426 | 95 | Module emit() { |
427 | 95 | std::vector<Instance> instances; |
428 | 1.51k | for (const auto* instance : passthrough_) { |
429 | 1.51k | instances.emplace_back(instance->get_name(), instance->get_cell_type(), |
430 | 1.51k | instance->get_connections(), |
431 | 1.51k | cell_for(instance->get_cell_type()), |
432 | 1.51k | instance->get_parameters()); |
433 | 1.51k | } |
434 | 95 | std::size_t counter = 0; |
435 | 34.5k | for (const int id : topo_) { |
436 | 34.5k | const auto& node = nodes_[static_cast<std::size_t>(id)]; |
437 | 34.5k | if (!node.required) continue; |
438 | 10.7k | const auto& cut = node.best; |
439 | 10.7k | std::uint64_t table = truth_table(id, cut); |
440 | 10.7k | const std::size_t width = std::max<std::size_t>(cut.size(), 1); |
441 | 10.7k | const std::uint64_t rows = 1ULL << width; |
442 | 10.7k | const std::uint64_t mask = rows == 64 ? ~0ULL : (1ULL << rows) - 1; |
443 | 10.7k | table &= mask; |
444 | 10.7k | if (cut.empty()) { |
445 | 2.42k | const std::string tie = table & 1 ? "TIEHI" : "TIELO"; |
446 | 2.42k | instances.emplace_back("__lut" + std::to_string(counter++), tie, |
447 | 2.42k | std::vector<PinConnection>{{"Y", node.output}}, |
448 | 2.42k | cell_for(tie)); |
449 | 2.42k | continue; |
450 | 2.42k | } |
451 | 8.32k | std::vector<PinConnection> pins; |
452 | 46.2k | for (std::size_t i = 0; i < cut.size(); ++i) { |
453 | 37.9k | pins.emplace_back("I" + std::to_string(i), net_name(cut[i])); |
454 | 37.9k | } |
455 | 8.32k | pins.emplace_back("O", node.output); |
456 | 8.32k | std::ostringstream init; |
457 | 8.32k | init << std::hex << std::uppercase << std::setfill('0') |
458 | 8.32k | << std::setw(static_cast<int>((rows + 3) / 4)) << table; |
459 | 8.32k | instances.emplace_back("__lut" + std::to_string(counter++), |
460 | 8.32k | "LUT" + std::to_string(cut.size()), std::move(pins), |
461 | 8.32k | cell_for("LUT" + std::to_string(cut.size())), |
462 | 8.32k | std::map<std::string, std::string>{{"INIT", init.str()}}); |
463 | 8.32k | } |
464 | 95 | std::vector<Net> nets; |
465 | 95 | std::set<std::string> declared; |
466 | 30.1k | for (const auto& net : module_.get_nets()) { |
467 | 30.1k | nets.push_back(net); |
468 | 30.1k | declared.insert(net.get_name()); |
469 | 30.1k | } |
470 | 12.2k | for (const auto& instance : instances) { |
471 | 54.7k | for (const auto& connection : instance.get_connections()) { |
472 | 54.7k | const auto& name = connection.get_net(); |
473 | 54.7k | if (declared.insert(name).second) nets.emplace_back(name, NetType::Internal); |
474 | 54.7k | } |
475 | 12.2k | } |
476 | 95 | return Module(module_.get_name(), module_.get_ports(), std::move(nets), |
477 | 95 | std::move(instances)); |
478 | 95 | } |
479 | | }; |
480 | | |
481 | | } // namespace |
482 | | |
483 | | Module fpga_map(const Module& generic_module, std::size_t lut_size, |
484 | 99 | const StandardCellLibrary& library) { |
485 | 99 | if (lut_size < 2 || lut_size > 6) { |
486 | 3 | throw std::invalid_argument("lut_size must be between 2 and 6"); |
487 | 3 | } |
488 | 96 | if (generic_module.is_behavioral()) { |
489 | 1 | throw std::invalid_argument( |
490 | 1 | "fpga_map needs a generic netlist; run generic_map first"); |
491 | 1 | } |
492 | 95 | auto mapped = Mapper(generic_module, lut_size, library).run(); |
493 | 95 | std::vector<Module> child_modules; |
494 | 95 | child_modules.reserve(generic_module.get_child_modules().size()); |
495 | 95 | for (const auto& child : generic_module.get_child_modules()) { |
496 | 32 | child_modules.push_back(fpga_map(child, lut_size, library)); |
497 | 32 | } |
498 | 95 | return Module(mapped.get_name(), mapped.get_ports(), mapped.get_nets(), |
499 | 95 | mapped.get_instances(), mapped.get_assignments(), |
500 | 95 | mapped.is_behavioral(), |
501 | 95 | generic_module.get_hierarchy_instance_name(), |
502 | 95 | std::move(child_modules)); |
503 | 96 | } |
504 | | |
505 | | Module fpga_map(const Project& project, const std::string& top, |
506 | | const std::map<std::string, std::string>& parameters, |
507 | 2 | std::size_t lut_size, const StandardCellLibrary& library) { |
508 | 2 | return fpga_map(generic_map(project, top, parameters), lut_size, library); |
509 | 2 | } |
510 | | |
511 | | } // namespace labneura::eda |