Debian Codegraph @

himbaechel/uarch/gowin/gowin_utils.h

raw
1 #ifndef GOWIN_UTILS_H
2 #define GOWIN_UTILS_H
3
4 #include "design_utils.h"
5 #include "idstringlist.h"
6 #include "nextpnr_namespaces.h"
7 #include "nextpnr_types.h"
8
9 NEXTPNR_NAMESPACE_BEGIN
10
11 namespace BelFlags {
12 static constexpr uint32_t FLAG_SIMPLE_IO = 0x100;
13 }
14
15 namespace WireFlags {
16 static constexpr uint32_t FLAG_CLOCK_GATE = 0x1;
17 static constexpr uint32_t FLAG_BOTTOM_HALF = 0x2;
18 } // namespace WireFlags
19
20 struct GowinUtils
21 {
22 Context *ctx;
23
24 GowinUtils() {}
25
26 void init(Context *ctx) { this->ctx = ctx; }
27
28 // tile
29 IdString get_tile_class(int x, int y);
30 Loc get_tile_io16_offs(int x, int y);
31 bool get_i3c_capable(int x, int y);
32 inline Loc get_wire_loc(WireId wire) const
33 {
34 Loc loc;
35 tile_xy(ctx->chip_info, wire.tile, loc.x, loc.y);
36 return loc;
37 }
38
39 // pin functions: GCLKT_4, SSPI_CS, READY etc
40 IdStringList get_pin_funcs(BelId io_bel);
41
42 // PLL pads (type - CLKIN, FeedBack, etc)
43 BelId get_pll_bel(BelId io_bel, IdString type);
44
45 // Bels and pips
46 bool is_simple_io_bel(BelId bel);
47 Loc get_pair_iologic_bel(Loc loc);
48 BelId get_io_bel_from_iologic(BelId bel);
49 BelId get_dqce_bel(IdString spine_name);
50 BelId get_dcs_bel(IdString spine_name);
51 BelId get_dhcen_bel(WireId hclkin_wire, IdString &side);
52 BelId get_dlldly_bel(BelId io_bel);
53 IdString get_dcs_prefix(void);
54
55 // Segments
56 int get_segments_count(void) const;
57 void get_segment_region(int s_i, int &seg_idx, int &x, int &min_x, int &min_y, int &max_x, int &max_y) const;
58 void get_segment_wires_loc(int s_i, Loc &top, Loc &bottom) const;
59 void get_segment_wires(int s_i, WireId &top, WireId &bottom) const;
60 void get_segment_top_gate_wires(int s_i, WireId &wire0, WireId &wire1) const;
61 void get_segment_bottom_gate_wires(int s_i, WireId &wire0, WireId &wire1) const;
62
63 // ports
64 inline bool port_used(CellInfo *cell, IdString port_name)
65 {
66 if (!nextpnr_himbaechel::port_used(cell, port_name)) {
67 return false;
68 }
69 NetInfo *ni = cell->ports.at(port_name).net;
70 if (ni->driver.cell == nullptr) {
71 return false;
72 }
73 return ni->users.entries() != 0;
74 }
75 // Remove [ and ] from port names
76 void remove_brackets(CellInfo *ci);
77
78 // net sources
79 inline bool driver_is_io(const PortRef &driver) { return CellTypePort(driver) == CellTypePort(id_IBUF, id_O); }
80 inline bool driver_is_buf(const PortRef &driver) { return CellTypePort(driver) == CellTypePort(id_BUFG, id_O); }
81 inline bool driver_is_dqce(const PortRef &driver)
82 {
83 return CellTypePort(driver) == CellTypePort(id_DQCE, id_CLKOUT);
84 }
85 inline bool driver_is_dcs(const PortRef &driver) { return CellTypePort(driver) == CellTypePort(id_DCS, id_CLKOUT); }
86 inline bool driver_is_dhcen(const PortRef &driver)
87 {
88 return CellTypePort(driver) == CellTypePort(id_DHCEN, id_CLKOUT);
89 }
90 inline bool driver_is_mipi(const PortRef &driver)
91 {
92 return CellTypePort(driver) == CellTypePort(id_IOBUF, id_O) && driver.cell->params.count(id_MIPI_IBUF);
93 }
94 bool driver_is_clksrc(const PortRef &driver);
95
96 // clock nets
97 bool wire_is_clock_gate(WireId wire) const
98 {
99 return chip_wire_info(ctx->chip_info, wire).flags & WireFlags::FLAG_CLOCK_GATE;
100 }
101
102 bool wire_in_bottom_half(WireId wire) const
103 {
104 return chip_wire_info(ctx->chip_info, wire).flags & WireFlags::FLAG_BOTTOM_HALF;
105 }
106 bool get_spine_select_wire(WireId spine, std::vector<std::pair<WireId, int>> &);
107
108 // BSRAM
109 bool has_SP32(void);
110 bool need_SP_fix(void);
111 bool need_SDP_fix(void);
112 bool need_BSRAM_OUTREG_fix(void);
113 bool need_BSRAM_DP_CE_fix(void);
114 bool need_BSRAM_RESET_fix(void);
115 bool need_BLKSEL_fix(void);
116 bool has_PLL_HCLK(void);
117 bool has_CLKDIV_HCLK(void);
118
119 // Power saving
120 bool has_BANDGAP(void);
121
122 // Pin function configuration via wires
123 bool has_PINCFG(void) const;
124 bool need_CFGPINS_INVERSION(void) const;
125 bool has_I2CCFG(void) const;
126
127 // Logic cell structure
128 bool has_DFF67(void) const;
129
130 // ALU
131 bool has_CIN_MUX(void) const;
132
133 // Clock MUX
134 bool has_spine_enable_nets(void) const;
135
136 // DSP
137 bool has_5A_DSP(void) const;
138 inline int get_dsp_18_z(int z) const { return z & (~3); }
139 inline int get_dsp_9_idx(int z) const { return z & 3; }
140 inline int get_dsp_18_idx(int z) const { return z & 4; }
141 inline int get_dsp_paired_9(int z) const { return (3 - get_dsp_9_idx(z)) | (z & (~3)); }
142 inline int get_dsp_paired_12(int z) const { return BelZ::MULT12X12_1_Z - (z & 1); }
143 inline int get_dsp_mult_from_padd(int padd_z) const { return padd_z + 8; }
144 inline int get_dsp_padd_from_mult(int mult_z) const { return mult_z - 8; }
145 inline int get_dsp_next_macro(int z) const { return z + 32; }
146 inline int get_dsp(int z) const { return BelZ::DSP_Z; }
147 inline int get_dsp_macro(int z) const { return (z & 0x20) + BelZ::DSP_0_Z; }
148 inline int get_dsp_macro_num(int z) const { return (z & 0x20) >> 5; }
149 Loc get_dsp_next_9_in_chain(Loc from) const;
150 Loc get_dsp_next_macro_in_chain(Loc from) const;
151 Loc get_dsp_next_in_chain(Loc from, IdString dsp_type) const;
152 Loc get_dsp_next_in_chain_5a(Loc from) const;
153
154 // check bus.
155 // This is necessary to find the head in the DSP chain - these buses are
156 // not switched in the hardware, but in software you can leave them
157 // unconnected or connect them to VCC or VSS, which is the same - as I
158 // already said, they are hard-wired and we are only discovering the fact
159 // that they are not connected to another DSP in the chain.
160 CellInfo *dsp_bus_src(const CellInfo *ci, const char *bus_prefix, int wire_num) const;
161 CellInfo *dsp_bus_dst(const CellInfo *ci, const char *bus_prefix, int wire_num) const;
162
163 bool is_diff_io_supported(IdString type);
164 bool has_bottom_io_cnds(void);
165 IdString get_bottom_io_wire_a_net(int8_t condition);
166 IdString get_bottom_io_wire_b_net(int8_t condition);
167
168 // wires
169 inline bool is_wire_type_default(IdString wire_type) { return wire_type == IdString(); }
170 // If wire is an important part of the global network (like SPINExx)
171 inline bool is_global_wire(WireId wire) const
172 {
173 return ctx->getWireName(wire)[1].in(
174 id_SPINE0, id_SPINE1, id_SPINE2, id_SPINE3, id_SPINE4, id_SPINE5, id_SPINE6, id_SPINE7, id_SPINE8,
175 id_SPINE9, id_SPINE10, id_SPINE11, id_SPINE12, id_SPINE13, id_SPINE14, id_SPINE15, id_SPINE16,
176 id_SPINE17, id_SPINE18, id_SPINE19, id_SPINE20, id_SPINE21, id_SPINE22, id_SPINE23, id_SPINE24,
177 id_SPINE25, id_SPINE26, id_SPINE27, id_SPINE28, id_SPINE29, id_SPINE30, id_SPINE31);
178 }
179
180 // pips
181 inline bool is_global_pip(PipId pip) const
182 {
183 return is_global_wire(ctx->getPipSrcWire(pip)) || is_global_wire(ctx->getPipDstWire(pip));
184 }
185
186 inline bool is_segment_pip(PipId pip) const { return ctx->getWireType(ctx->getPipDstWire(pip)) == id_LW_TAP; }
187
188 // construct name
189 IdString create_aux_name(IdString main_name, int idx = 0, const char *str_suffix = "_aux$");
190
191 // make cell but do not include it in the list of chip cells.
192 std::unique_ptr<CellInfo> create_cell(IdString name, IdString type);
193
194 // HCLK
195 BelId get_clkdiv_for_clkdiv2(BelId clkdiv2_bel) const;
196 BelId get_other_hclk_clkdiv2(BelId clkdiv2_bel) const;
197 BelId get_other_hclk_clkdiv(BelId clkdiv_bel) const;
198 BelId get_clkdiv2_for_clkdiv(BelId clkdiv_bel) const;
199 IdStringList get_hclk_id(BelId hclk_bel) const; // use the upper CLKDIV2 (CLKDIV2_0 orCLKDIV2_2) as an id
200
201 // Find Bels connected to a bound cell
202 void find_connected_bels(const CellInfo *cell, IdString port, IdString dest_type, IdString dest_pin, int iter_limit,
203 std::vector<BelId> &candidates);
204
205 // Find a maximum bipartite matching
206 template <typename T1, typename T2> std::map<T1, T2> find_maximum_bipartite_matching(std::map<T1, std::set<T2>> &G)
207 {
208 std::map<int, T1> U;
209 std::map<int, T2> V;
210 std::map<T2, int> V_IDX;
211 std::vector<std::vector<int>> int_graph(G.size());
212
213 int u_idx = 0;
214 int v_idx = 0;
215
216 // Translate the input graph to an integer graph
217 for (auto row : G) {
218 U.insert(std::pair(u_idx, row.first));
219 for (auto v : row.second) {
220 if (V_IDX.find(v) == V_IDX.end()) {
221 V_IDX[v] = v_idx;
222 V[v_idx] = v;
223 v_idx++;
224 }
225 int_graph[u_idx].push_back(V_IDX[v]);
226 }
227 u_idx++;
228 }
229
230 std::vector<int> int_matching = kuhn_find_maximum_bipartite_matching(u_idx, v_idx, int_graph);
231 std::map<T1, T2> ret_matching;
232
233 int m_idx = 0;
234 for (auto val : int_matching) {
235 if (val >= 0) { // elements that are not matched have a value of -1
236 ret_matching[U[val]] = V[m_idx];
237 }
238 m_idx++;
239 }
240
241 return ret_matching;
242 }
243
244 // Find a maximum matching in a bipartite graph, g
245 std::vector<int> kuhn_find_maximum_bipartite_matching(int n, int k, std::vector<std::vector<int>> &g);
246
247 // Get spec locations
248 int get_center_row(void) const;
249 int get_center_col(void) const;
250 };
251
252 NEXTPNR_NAMESPACE_END
253
254 #endif