1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
5 * Copyright (C) 2018 David Shah <david@symbioticeda.com>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 #include "nextpnr.h"
22
23 #ifndef DESIGN_UTILS_H
24 #define DESIGN_UTILS_H
25
26 #include <algorithm>
27
28 NEXTPNR_NAMESPACE_BEGIN
29
30 /*
31 Utilities for design manipulation, intended for use inside packing algorithms
32 */
33
34 // Disconnect a net (if connected) from old, and connect it to rep
35 void replace_port(CellInfo *old_cell, IdString old_name, CellInfo *rep_cell, IdString rep_name);
36
37 // If a net drives a given port of a cell matching a predicate (in many
38 // cases more than one cell type, e.g. SB_DFFxx so a predicate is used), return
39 // the first instance of that cell (otherwise nullptr). If exclusive is set to
40 // true, then this cell must be the only load. If ignore_cell is set, that cell
41 // is not considered
42 template <typename F1>
43 CellInfo *net_only_drives(const Context *ctx, NetInfo *net, F1 cell_pred, IdString port, bool exclusive = false,
44 CellInfo *exclude = nullptr)
45 {
46 if (net == nullptr)
47 return nullptr;
48 if (exclusive) {
49 if (exclude == nullptr) {
50 if (net->users.size() != 1)
51 return nullptr;
52 } else {
53 if (net->users.size() > 2) {
54 return nullptr;
55 } else if (net->users.size() == 2) {
56 if (std::find_if(net->users.begin(), net->users.end(),
57 [exclude](const PortRef &ref) { return ref.cell == exclude; }) == net->users.end())
58 return nullptr;
59 }
60 }
61 }
62 for (const auto &load : net->users) {
63 if (load.cell != exclude && cell_pred(ctx, load.cell) && load.port == port) {
64 return load.cell;
65 }
66 }
67 return nullptr;
68 }
69
70 // If a net is driven by a given port of a cell matching a predicate, return
71 // that cell, otherwise nullptr
net_driven_by(const Context * ctx,const NetInfo * net,F1 cell_pred,IdString port)72 template <typename F1> CellInfo *net_driven_by(const Context *ctx, const NetInfo *net, F1 cell_pred, IdString port)
73 {
74 if (net == nullptr)
75 return nullptr;
76 if (net->driver.cell == nullptr)
77 return nullptr;
78 if (cell_pred(ctx, net->driver.cell) && net->driver.port == port) {
79 return net->driver.cell;
80 } else {
81 return nullptr;
82 }
83 }
84
85 // Connect a net to a port
86 void connect_port(const Context *ctx, NetInfo *net, CellInfo *cell, IdString port_name);
87
88 // Disconnect a net from a port
89 void disconnect_port(const Context *ctx, CellInfo *cell, IdString port_name);
90
91 // Connect two ports together
92 void connect_ports(Context *ctx, CellInfo *cell1, IdString port1_name, CellInfo *cell2, IdString port2_name);
93
94 // Rename a port if it exists on a cell
95 void rename_port(Context *ctx, CellInfo *cell, IdString old_name, IdString new_name);
96
97 // Rename a net without invalidating pointers to it
98 void rename_net(Context *ctx, NetInfo *net, IdString new_name);
99
100 void print_utilisation(const Context *ctx);
101
102 NEXTPNR_NAMESPACE_END
103
104 #endif
105