Debian Codegraph @

himbaechel/uarch/gowin/gowin_utils.cc

raw
1 #include "log.h"
2 #include "nextpnr.h"
3 #include "util.h"
4
5 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
6 #include "himbaechel_constids.h"
7 #include "himbaechel_helpers.h"
8
9 #include <queue>
10 #include "gowin.h"
11 #include "gowin_utils.h"
12
13 NEXTPNR_NAMESPACE_BEGIN
14
15 // Remove [ and ] from port names
16 void GowinUtils::remove_brackets(CellInfo *ci)
17 {
18 std::vector<IdString> orig_port_names;
19 for (auto &port : ci->ports) {
20 orig_port_names.push_back(port.first);
21 }
22
23 for (auto pname : orig_port_names) {
24 IdString new_name;
25 std::string stripped_name;
26
27 for (auto c : pname.str(ctx)) {
28 if (c != '[' && c != ']') {
29 stripped_name += c;
30 }
31 }
32 new_name = ctx->id(stripped_name);
33 if (new_name != pname) {
34 ci->renamePort(pname, new_name);
35 }
36 }
37 }
38
39 // clock sources
40 bool GowinUtils::driver_is_clksrc(const PortRef &driver)
41 {
42 // dedicated pins
43 if (CellTypePort(driver) == CellTypePort(id_IBUF, id_O)) {
44
45 NPNR_ASSERT(driver.cell->bel != BelId());
46 IdStringList pin_func = get_pin_funcs(driver.cell->bel);
47 for (size_t i = 0; i < pin_func.size(); ++i) {
48 if (ctx->debug) {
49 log_info("bel:%s, pin func: %zu:%s\n", ctx->nameOfBel(driver.cell->bel), i,
50 pin_func[i].str(ctx).c_str());
51 }
52 if (pin_func[i].str(ctx).rfind("GCLKT", 0) == 0) {
53 if (ctx->debug) {
54 log_info("Clock pin:%s:%s\n", ctx->getBelName(driver.cell->bel).str(ctx).c_str(),
55 pin_func[i].c_str(ctx));
56 }
57 return true;
58 }
59 }
60 }
61 // OSC outputs
62 if (driver.cell->type.in(id_OSC, id_OSCA, id_OSCZ, id_OSCH, id_OSCF, id_OSCW, id_OSCO)) {
63 if (driver.port == id_OSCOUT) {
64 if (ctx->debug) {
65 if (driver.cell->bel != BelId()) {
66 log_info("OSC out bel:%s:%s\n", ctx->nameOfBel(driver.cell->bel), driver.port.c_str(ctx));
67 } else {
68 log_info("OSC out:%s:%s\n", ctx->nameOf(driver.cell), driver.port.c_str(ctx));
69 }
70 }
71 return true;
72 }
73 }
74 // PLL outputs
75 if (driver.cell->type.in(id_rPLL, id_PLLVR, id_PLLA)) {
76 if (driver.port.in(id_CLKOUT, id_CLKOUTD, id_CLKOUTD3, id_CLKOUTP, id_CLKOUT0, id_CLKOUT1, id_CLKOUT2,
77 id_CLKOUT3, id_CLKOUT4, id_CLKOUT5, id_CLKOUT6, id_CLKOUT7)) {
78 if (ctx->debug) {
79 if (driver.cell->bel != BelId()) {
80 log_info("PLL out bel:%s:%s\n", ctx->nameOfBel(driver.cell->bel), driver.port.c_str(ctx));
81 } else {
82 log_info("PLL out:%s:%s\n", ctx->nameOf(driver.cell), driver.port.c_str(ctx));
83 }
84 }
85 return true;
86 }
87 }
88 // HCLK outputs
89 if (driver.cell->type.in(id_CLKDIV, id_CLKDIV2)) {
90 if (driver.port.in(id_CLKOUT)) {
91 if (ctx->debug) {
92 if (driver.cell->bel != BelId()) {
93 log_info("%s out bel:%s:%s:%s\n", driver.cell->type.c_str(ctx),
94 ctx->getBelName(driver.cell->bel).str(ctx).c_str(), driver.port.c_str(ctx),
95 ctx->nameOfWire(ctx->getBelPinWire(driver.cell->bel, driver.port)));
96 } else {
97 log_info("%s out:%s:%s\n", driver.cell->type.c_str(ctx), ctx->nameOf(driver.cell),
98 driver.port.c_str(ctx));
99 }
100 }
101 return true;
102 }
103 }
104 // DLLDLY outputs
105 if (driver.cell->type == id_DLLDLY) {
106 if (driver.port.in(id_CLKOUT)) {
107 if (ctx->debug) {
108 if (driver.cell->bel != BelId()) {
109 log_info("%s out bel:%s:%s\n", driver.cell->type.c_str(ctx),
110 ctx->getBelName(driver.cell->bel).str(ctx).c_str(), driver.port.c_str(ctx));
111 } else {
112 log_info("%s out:%s:%s\n", driver.cell->type.c_str(ctx), ctx->nameOf(driver.cell),
113 driver.port.c_str(ctx));
114 }
115 }
116 return true;
117 }
118 }
119 return false;
120 }
121
122 // Segments
123 int GowinUtils::get_segments_count(void) const
124 {
125 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
126 return extra->segments.ssize();
127 }
128
129 void GowinUtils::get_segment_region(int s_i, int &seg_idx, int &x, int &min_x, int &min_y, int &max_x, int &max_y) const
130 {
131 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
132 seg_idx = extra->segments[s_i].seg_idx;
133 x = extra->segments[s_i].x;
134 min_x = extra->segments[s_i].min_x;
135 min_y = extra->segments[s_i].min_y;
136 max_x = extra->segments[s_i].max_x;
137 max_y = extra->segments[s_i].max_y;
138 }
139
140 void GowinUtils::get_segment_wires_loc(int s_i, Loc &top, Loc &bottom) const
141 {
142 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
143 top.x = bottom.x = extra->segments[s_i].x;
144 top.y = extra->segments[s_i].top_row;
145 bottom.y = extra->segments[s_i].bottom_row;
146 }
147
148 void GowinUtils::get_segment_wires(int s_i, WireId &top, WireId &bottom) const
149 {
150 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
151 Loc top_loc, bottom_loc;
152 get_segment_wires_loc(s_i, top_loc, bottom_loc);
153
154 IdString tile = ctx->idf("X%dY%d", top_loc.x, top_loc.y);
155 IdStringList name = IdStringList::concat(tile, IdString(extra->segments[s_i].top_wire));
156 top = ctx->getWireByName(name);
157 tile = ctx->idf("X%dY%d", bottom_loc.x, bottom_loc.y);
158 name = IdStringList::concat(tile, IdString(extra->segments[s_i].bottom_wire));
159 bottom = ctx->getWireByName(name);
160 }
161
162 void GowinUtils::get_segment_top_gate_wires(int s_i, WireId &wire0, WireId &wire1) const
163 {
164 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
165 Loc top_loc, bottom_loc;
166 get_segment_wires_loc(s_i, top_loc, bottom_loc);
167
168 IdString tile = ctx->idf("X%dY%d", top_loc.x, top_loc.y);
169 IdStringList name;
170 wire0 = WireId();
171 IdString wire_name = IdString(extra->segments[s_i].top_gate_wire[0]);
172 if (wire_name != IdString()) {
173 name = IdStringList::concat(tile, wire_name);
174 wire0 = ctx->getWireByName(name);
175 }
176 wire1 = WireId();
177 wire_name = IdString(extra->segments[s_i].top_gate_wire[1]);
178 if (wire_name != IdString()) {
179 name = IdStringList::concat(tile, wire_name);
180 wire1 = ctx->getWireByName(name);
181 }
182 }
183
184 void GowinUtils::get_segment_bottom_gate_wires(int s_i, WireId &wire0, WireId &wire1) const
185 {
186 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
187 Loc top_loc, bottom_loc;
188 get_segment_wires_loc(s_i, top_loc, bottom_loc);
189
190 IdString tile = ctx->idf("X%dY%d", bottom_loc.x, bottom_loc.y);
191 IdStringList name;
192 wire0 = WireId();
193 IdString wire_name = IdString(extra->segments[s_i].bottom_gate_wire[0]);
194 if (wire_name != IdString()) {
195 name = IdStringList::concat(tile, wire_name);
196 wire0 = ctx->getWireByName(name);
197 }
198 wire1 = WireId();
199 wire_name = IdString(extra->segments[s_i].bottom_gate_wire[1]);
200 if (wire_name != IdString()) {
201 name = IdStringList::concat(tile, wire_name);
202 wire1 = ctx->getWireByName(name);
203 }
204 }
205
206 // tile extra data
207 IdString GowinUtils::get_tile_class(int x, int y)
208 {
209 int tile = tile_by_xy(ctx->chip_info, x, y);
210 const Tile_extra_data_POD *extra =
211 reinterpret_cast<const Tile_extra_data_POD *>(chip_tile_info(ctx->chip_info, tile).extra_data.get());
212 return IdString(extra->class_id);
213 }
214
215 // oser16/ides16 aux cell offsets
216 Loc GowinUtils::get_tile_io16_offs(int x, int y)
217 {
218 int tile = tile_by_xy(ctx->chip_info, x, y);
219 const Tile_extra_data_POD *extra =
220 reinterpret_cast<const Tile_extra_data_POD *>(chip_tile_info(ctx->chip_info, tile).extra_data.get());
221 return Loc(extra->io16_x_off, extra->io16_y_off, 0);
222 }
223
224 // oser16/ides16 aux cell offsets
225 bool GowinUtils::get_i3c_capable(int x, int y)
226 {
227 int tile = tile_by_xy(ctx->chip_info, x, y);
228 const Tile_extra_data_POD *extra =
229 reinterpret_cast<const Tile_extra_data_POD *>(chip_tile_info(ctx->chip_info, tile).extra_data.get());
230 return extra->tile_flags & Tile_extra_data_POD::TILE_I3C_CAPABLE_IO;
231 }
232
233 // pin functions: GCLKT_4, SSPI_CS, READY etc
234 IdStringList GowinUtils::get_pin_funcs(BelId io_bel)
235 {
236 IdStringList bel_name = ctx->getBelName(io_bel);
237
238 const PadInfoPOD *pins = ctx->package_info->pads.get();
239 size_t len = ctx->package_info->pads.ssize();
240 for (size_t i = 0; i < len; i++) {
241 const PadInfoPOD *pin = &pins[i];
242 if (IdString(pin->tile) == bel_name[0] && IdString(pin->bel) == bel_name[1]) {
243 return IdStringList::parse(ctx, IdString(pin->pad_function).str(ctx));
244 }
245 }
246 return IdStringList();
247 }
248
249 // PLL pads
250 BelId GowinUtils::get_pll_bel(BelId io_bel, IdString type)
251 {
252 IdStringList bel_name = ctx->getBelName(io_bel);
253
254 const PadInfoPOD *pins = ctx->package_info->pads.get();
255 size_t len = ctx->package_info->pads.ssize();
256 for (size_t i = 0; i < len; i++) {
257 const PadInfoPOD *pin = &pins[i];
258 if (IdString(pin->tile) == bel_name[0] && IdString(pin->bel) == bel_name[1]) {
259 const Pad_extra_data_POD *extra = reinterpret_cast<const Pad_extra_data_POD *>(pin->extra_data.get());
260 if (extra != nullptr && IdString(extra->pll_type) == type) {
261 return ctx->getBelByName(IdStringList::concat(IdString(extra->pll_tile), IdString(extra->pll_bel)));
262 }
263 }
264 }
265 return BelId();
266 }
267
268 BelId GowinUtils::get_dqce_bel(IdString spine_name)
269 {
270 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
271 for (auto &spine_bel : extra->dqce_bels) {
272 if (IdString(spine_bel.spine) == spine_name) {
273 return ctx->getBelByLocation(Loc(spine_bel.bel_x, spine_bel.bel_y, spine_bel.bel_z));
274 }
275 }
276 return BelId();
277 }
278
279 BelId GowinUtils::get_dcs_bel(IdString spine_name)
280 {
281 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
282 for (auto &spine_bel : extra->dcs_bels) {
283 if (IdString(spine_bel.spine) == spine_name) {
284 return ctx->getBelByLocation(Loc(spine_bel.bel_x, spine_bel.bel_y, spine_bel.bel_z));
285 }
286 }
287 return BelId();
288 }
289
290 BelId GowinUtils::get_dlldly_bel(BelId io_bel)
291 {
292 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
293 for (auto &io : extra->io_dlldly_bels) {
294 if (IdStringList::parse(ctx, (IdString(io.io)).str(ctx)) == ctx->getBelName(io_bel)) {
295 return ctx->getBelByName(IdStringList::parse(ctx, (IdString(io.dlldly)).str(ctx)));
296 }
297 }
298 return BelId();
299 }
300
301 BelId GowinUtils::get_dhcen_bel(WireId hclkin_wire, IdString &side)
302 {
303 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
304 for (auto &wire_bel : extra->dhcen_bels) {
305 IdString dst = IdString(wire_bel.pip_dst);
306 IdString src = IdString(wire_bel.pip_src);
307 IdStringList pip = IdStringList::concat(IdStringList::concat(IdString(wire_bel.pip_xy), dst), src);
308 WireId wire = ctx->getPipDstWire(ctx->getPipByName(pip));
309 if (wire == hclkin_wire) {
310 side = IdString(wire_bel.side);
311 return ctx->getBelByLocation(Loc(wire_bel.bel_x, wire_bel.bel_y, wire_bel.bel_z));
312 }
313 }
314 return BelId();
315 }
316
317 IdString GowinUtils::get_dcs_prefix(void)
318 {
319 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
320 return IdString(extra->dcs_prefix);
321 }
322
323 bool GowinUtils::is_simple_io_bel(BelId bel)
324 {
325 return chip_bel_info(ctx->chip_info, bel).flags & BelFlags::FLAG_SIMPLE_IO;
326 }
327
328 Loc GowinUtils::get_pair_iologic_bel(Loc loc)
329 {
330 int const z[] = {1, 0, 3, 2};
331 loc.z = BelZ::IOLOGICA_Z + z[(loc.z - BelZ::IOLOGICA_Z)];
332 return loc;
333 }
334
335 BelId GowinUtils::get_io_bel_from_iologic(BelId bel)
336 {
337 Loc loc = ctx->getBelLocation(bel);
338 loc.z = BelZ::IOBA_Z + ((loc.z - BelZ::IOLOGICA_Z) & 1);
339 return ctx->getBelByLocation(loc);
340 }
341
342 bool GowinUtils::is_diff_io_supported(IdString type)
343 {
344 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
345 for (auto &dtype : extra->diff_io_types) {
346 if (IdString(dtype) == type) {
347 return true;
348 }
349 }
350 return false;
351 }
352
353 bool GowinUtils::has_bottom_io_cnds(void)
354 {
355 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
356 return extra->bottom_io.conditions.size() != 0;
357 }
358
359 IdString GowinUtils::get_bottom_io_wire_a_net(int8_t condition)
360 {
361 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
362 return IdString(extra->bottom_io.conditions[condition].wire_a_net);
363 }
364
365 IdString GowinUtils::get_bottom_io_wire_b_net(int8_t condition)
366 {
367 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
368 return IdString(extra->bottom_io.conditions[condition].wire_b_net);
369 }
370
371 bool GowinUtils::has_BANDGAP(void)
372 {
373 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
374 return extra->chip_flags & Extra_chip_data_POD::HAS_BANDGAP;
375 }
376
377 bool GowinUtils::has_PINCFG(void) const
378 {
379 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
380 return extra->chip_flags & Extra_chip_data_POD::HAS_PINCFG;
381 }
382
383 bool GowinUtils::need_CFGPINS_INVERSION(void) const
384 {
385 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
386 return extra->chip_flags & Extra_chip_data_POD::NEED_CFGPINS_INVERSION;
387 }
388
389 bool GowinUtils::has_I2CCFG(void) const
390 {
391 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
392 return extra->chip_flags & Extra_chip_data_POD::HAS_I2CCFG;
393 }
394
395 bool GowinUtils::has_DFF67(void) const
396 {
397 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
398 return extra->chip_flags & Extra_chip_data_POD::HAS_DFF67;
399 }
400
401 bool GowinUtils::has_spine_enable_nets(void) const
402 {
403 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
404 return extra->spine_select_wires_top.ssize() || extra->spine_select_wires_bottom.ssize();
405 }
406
407 bool GowinUtils::get_spine_select_wire(WireId spine, std::vector<std::pair<WireId, int>> &wires)
408 {
409 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
410
411 wires.clear();
412 for (auto &rec : wire_in_bottom_half(spine) ? extra->spine_select_wires_bottom : extra->spine_select_wires_top) {
413 if (IdString(rec.spine) == ctx->getWireName(spine)[1]) {
414 IdString tile = ctx->idf("X%dY%d", rec.x, rec.y);
415 IdStringList name = IdStringList::concat(tile, IdString(rec.wire));
416 wires.push_back(std::make_pair(ctx->getWireByName(name), rec.vcc_gnd));
417 }
418 }
419 return !wires.empty();
420 }
421
422 bool GowinUtils::has_CIN_MUX(void) const
423 {
424 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
425 return extra->chip_flags & Extra_chip_data_POD::HAS_CIN_MUX;
426 }
427
428 bool GowinUtils::has_SP32(void)
429 {
430 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
431 return extra->chip_flags & Extra_chip_data_POD::HAS_SP32;
432 }
433
434 bool GowinUtils::need_SP_fix(void)
435 {
436 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
437 return extra->chip_flags & Extra_chip_data_POD::NEED_SP_FIX;
438 }
439
440 bool GowinUtils::need_SDP_fix(void)
441 {
442 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
443 return extra->chip_flags & Extra_chip_data_POD::NEED_SDP_FIX;
444 }
445
446 bool GowinUtils::need_BSRAM_OUTREG_fix(void)
447 {
448 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
449 return extra->chip_flags & Extra_chip_data_POD::NEED_BSRAM_OUTREG_FIX;
450 }
451
452 bool GowinUtils::need_BSRAM_DP_CE_fix(void)
453 {
454 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
455 return extra->chip_flags & Extra_chip_data_POD::NEED_BSRAM_DP_CE_FIX;
456 }
457
458 bool GowinUtils::need_BSRAM_RESET_fix(void)
459 {
460 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
461 return extra->chip_flags & Extra_chip_data_POD::NEED_BSRAM_RESET_FIX;
462 }
463
464 bool GowinUtils::need_BLKSEL_fix(void)
465 {
466 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
467 return extra->chip_flags & Extra_chip_data_POD::NEED_BLKSEL_FIX;
468 }
469
470 bool GowinUtils::has_PLL_HCLK(void)
471 {
472 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
473 return extra->chip_flags & Extra_chip_data_POD::HAS_PLL_HCLK;
474 }
475
476 bool GowinUtils::has_CLKDIV_HCLK(void)
477 {
478 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
479 return extra->chip_flags & Extra_chip_data_POD::HAS_CLKDIV_HCLK;
480 }
481
482 IdString GowinUtils::create_aux_name(IdString main_name, int idx, const char *str_suffix)
483 {
484 return idx ? ctx->idf("%s%s%d", main_name.c_str(ctx), str_suffix, idx)
485 : ctx->idf("%s%s", main_name.c_str(ctx), str_suffix);
486 }
487
488 std::unique_ptr<CellInfo> GowinUtils::create_cell(IdString name, IdString type)
489 {
490 NPNR_ASSERT(!ctx->cells.count(name));
491 return std::make_unique<CellInfo>(ctx, name, type);
492 }
493
494 // DSP
495 bool GowinUtils::has_5A_DSP(void) const
496 {
497 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
498 return extra->chip_flags & Extra_chip_data_POD::HAS_5A_DSP;
499 }
500
501 Loc GowinUtils::get_dsp_next_9_in_chain(Loc from) const
502 {
503 Loc res;
504 res.y = from.y;
505 if (get_dsp_18_idx(from.z) == 0) {
506 res.x = from.x;
507 res.z = from.z + 4;
508 return res;
509 }
510 if (get_dsp_macro_num(from.z)) {
511 // next DSP
512 res.x = from.x + 9;
513 res.z = from.z & (~0x24);
514 } else {
515 // next macro
516 res.x = from.x;
517 res.z = get_dsp_next_macro(from.z) & (~4);
518 }
519 return res;
520 }
521
522 Loc GowinUtils::get_dsp_next_macro_in_chain(Loc from) const
523 {
524 Loc res;
525 res.y = from.y;
526 if (get_dsp_macro_num(from.z)) {
527 // next DSP
528 res.x = from.x + 9;
529 res.z = from.z & (~0x20);
530 } else {
531 // next macro
532 res.x = from.x;
533 res.z = get_dsp_next_macro(from.z);
534 }
535 return res;
536 }
537
538 Loc GowinUtils::get_dsp_next_in_chain_5a(Loc from) const
539 {
540 Loc res;
541 res.y = from.y;
542 // next DSP
543 int off = 3;
544 if (from.y == get_center_row() && (from.x + 5) == get_center_col()) {
545 off = 9;
546 }
547 res.x = from.x + off;
548 res.z = from.z;
549 return res;
550 }
551
552 Loc GowinUtils::get_dsp_next_in_chain(Loc from, IdString dsp_type) const
553 {
554 if (dsp_type.in(id_PADD9, id_PADD18, id_MULT9X9, id_MULT18X18)) {
555 return get_dsp_next_9_in_chain(from);
556 }
557 if (dsp_type.in(id_ALU54D, id_MULTALU18X18, id_MULTALU36X18, id_MULTADDALU18X18)) {
558 return get_dsp_next_macro_in_chain(from);
559 }
560 if (dsp_type.in(id_MULTADDALU12X12, id_MULTALU27X18)) {
561 return get_dsp_next_in_chain_5a(from);
562 }
563 NPNR_ASSERT_FALSE("Unknown DSP cell type.");
564 }
565
566 CellInfo *GowinUtils::dsp_bus_src(const CellInfo *ci, const char *bus_prefix, int wire_num) const
567 {
568 bool connected_to_const = false; // and disconnected too
569 CellInfo *connected_to_cell = nullptr;
570
571 for (int i = 0; i < wire_num; ++i) {
572 const NetInfo *net = ci->getPort(ctx->idf("%s%d", bus_prefix, i));
573 if (connected_to_cell == nullptr) {
574 if (net == nullptr || net->driver.cell == nullptr || net->name == ctx->id("$PACKER_VCC") ||
575 net->name == ctx->id("$PACKER_GND")) {
576 connected_to_const = true;
577 continue;
578 } else {
579 if (connected_to_const) {
580 log_error("The %s cell %s bus is connected simultaneously to constants and to another DSP.\n",
581 ctx->nameOf(ci), bus_prefix);
582 }
583 }
584 }
585 if (net == nullptr || !is_dsp(net->driver.cell)) {
586 log_error("The %s cell %s bus is not connected to another DSP.\n", ctx->nameOf(ci), bus_prefix);
587 }
588 if (connected_to_cell != nullptr && net->driver.cell != connected_to_cell) {
589 log_error("The %s cell %s bus is connected to different DSPs: %s and %s.\n", ctx->nameOf(ci), bus_prefix,
590 ctx->nameOf(connected_to_cell), ctx->nameOf(net->driver.cell));
591 }
592 connected_to_cell = net->driver.cell;
593 }
594 if (connected_to_const) {
595 return nullptr;
596 }
597 return connected_to_cell;
598 }
599
600 CellInfo *GowinUtils::dsp_bus_dst(const CellInfo *ci, const char *bus_prefix, int wire_num) const
601 {
602 bool disconnected = false; // and disconnected too
603 CellInfo *connected_to_cell = nullptr;
604
605 for (int i = 0; i < wire_num; ++i) {
606 const NetInfo *net = ci->getPort(ctx->idf("%s%d", bus_prefix, i));
607 if (connected_to_cell == nullptr) {
608 if (net == nullptr || net->users.entries() == 0) {
609 disconnected = true;
610 continue;
611 } else {
612 if (disconnected) {
613 log_error("The %s cell %s bus is partially disconnected.\n", ctx->nameOf(ci), bus_prefix);
614 }
615 }
616 }
617 if (net->users.entries() > 1) {
618 log_error("Net %s has >1 users.\n", ctx->nameOf(net));
619 }
620
621 CellInfo *dst = (*net->users.begin()).cell;
622 if (net == nullptr || !is_dsp(dst)) {
623 log_error("The %s cell %s bus is not connected to another DSP.\n", ctx->nameOf(ci), bus_prefix);
624 }
625 if (connected_to_cell != nullptr && dst != connected_to_cell) {
626 log_error("The %s cell %s bus is connected to different DSPs: %s and %s.\n", ctx->nameOf(ci), bus_prefix,
627 ctx->nameOf(connected_to_cell), ctx->nameOf(dst));
628 }
629 connected_to_cell = dst;
630 }
631 if (disconnected) {
632 return nullptr;
633 }
634 return connected_to_cell;
635 }
636
637 // Use the upper CLKDIV as the id for a hclk section
638 IdStringList GowinUtils::get_hclk_id(BelId hclk_bel) const
639 {
640 IdString bel_type = ctx->getBelType(hclk_bel);
641 NPNR_ASSERT(hclk_bel != BelId() && bel_type.in(id_CLKDIV2, id_CLKDIV));
642 Loc id_loc = Loc(ctx->getBelLocation(hclk_bel));
643 if (bel_type == id_CLKDIV) {
644 return get_hclk_id(get_clkdiv2_for_clkdiv(hclk_bel));
645 } else if (id_loc.z == BelZ::CLKDIV2_0_Z || id_loc.z == BelZ::CLKDIV2_2_Z)
646 return ctx->getBelName(hclk_bel);
647 else
648 return ctx->getBelName(ctx->getBelByLocation(Loc(id_loc.x, id_loc.y, id_loc.z - 1)));
649 return IdStringList();
650 }
651
652 // Get the clkdiv in the same section as a clkdiv2
653 BelId GowinUtils::get_clkdiv_for_clkdiv2(BelId clkdiv2_bel) const
654 {
655 NPNR_ASSERT(clkdiv2_bel != BelId() && (ctx->getBelType(clkdiv2_bel) == id_CLKDIV2));
656 Loc clkdiv_loc = ctx->getBelLocation(clkdiv2_bel);
657 clkdiv_loc.z = BelZ::CLKDIV_0_Z + (clkdiv_loc.z - BelZ::CLKDIV2_0_Z);
658 return ctx->getBelByLocation(clkdiv_loc);
659 }
660
661 // Get the clkdiv2 in the same section as a clkdiv
662 BelId GowinUtils::get_clkdiv2_for_clkdiv(BelId clkdiv_bel) const
663 {
664 NPNR_ASSERT(clkdiv_bel != BelId() && (ctx->getBelType(clkdiv_bel) == id_CLKDIV));
665 Loc clkdiv_loc = ctx->getBelLocation(clkdiv_bel);
666 Loc clkdiv2_loc = Loc(clkdiv_loc.x, clkdiv_loc.y, BelZ::CLKDIV2_0_Z + (clkdiv_loc.z - BelZ::CLKDIV_0_Z));
667 return ctx->getBelByLocation(clkdiv2_loc);
668 }
669
670 // Get the clkdiv in the neighbouring section to a clkdiv
671 BelId GowinUtils::get_other_hclk_clkdiv(BelId clkdiv_bel) const
672 {
673 NPNR_ASSERT(clkdiv_bel != BelId() && (ctx->getBelType(clkdiv_bel) == id_CLKDIV));
674 Loc other_loc = Loc(ctx->getBelLocation(clkdiv_bel));
675 int dz = BelZ::CLKDIV_1_Z - BelZ::CLKDIV_0_Z;
676 if (other_loc.z == BelZ::CLKDIV_1_Z || other_loc.z == BelZ::CLKDIV_3_Z)
677 dz = -dz;
678 other_loc.z += dz;
679 return ctx->getBelByLocation(other_loc);
680 }
681
682 // Get the clkdiv2 in the neighbouring section to a clkdiv2
683 BelId GowinUtils::get_other_hclk_clkdiv2(BelId clkdiv2_bel) const
684 {
685 NPNR_ASSERT(clkdiv2_bel != BelId() && (ctx->getBelType(clkdiv2_bel) == id_CLKDIV2));
686 Loc other_loc = Loc(ctx->getBelLocation(clkdiv2_bel));
687 int dz = BelZ::CLKDIV2_1_Z - BelZ::CLKDIV2_0_Z;
688 if (other_loc.z == BelZ::CLKDIV2_1_Z || other_loc.z == BelZ::CLKDIV2_3_Z)
689 dz = -dz;
690 other_loc.z += dz;
691 return ctx->getBelByLocation(other_loc);
692 }
693
694 // Credit: https://cp-algorithms.com/graph/kuhn_maximum_bipartite_matching.html
695 std::vector<int> GowinUtils::kuhn_find_maximum_bipartite_matching(int n, int k, std::vector<std::vector<int>> &g)
696 {
697 std::vector<int> mt;
698 std::vector<bool> used(n);
699
700 auto try_kuhn = [&](int v, auto &try_kuhn_ref) -> bool {
701 if (used[v])
702 return false;
703 used[v] = true;
704 for (int to : g[v]) {
705 if (mt[to] == -1 || try_kuhn_ref(mt[to], try_kuhn_ref)) {
706 mt[to] = v;
707 return true;
708 }
709 }
710 return false;
711 };
712
713 mt.assign(k, -1);
714 for (int v = 0; v < n; ++v) {
715 used.assign(n, false);
716 try_kuhn(v, try_kuhn);
717 }
718
719 return mt;
720 }
721
722 // original implementation: nextpnr/machxo2/pack.cc
723 // Using a BFS, search for bels of a given type either upstream or downstream of another cell
724 void GowinUtils::find_connected_bels(const CellInfo *cell, IdString port, IdString dest_type, IdString dest_pin,
725 int iter_limit, std::vector<BelId> &candidates)
726 {
727 int iter = 0;
728 std::queue<WireId> visit;
729 pool<WireId> seen_wires;
730 pool<BelId> seen_bels;
731
732 BelId bel = cell->bel;
733 if (bel == BelId())
734 return;
735 WireId start_wire = ctx->getBelPinWire(bel, port);
736 NPNR_ASSERT(start_wire != WireId());
737 PortType dir = ctx->getBelPinType(bel, port);
738
739 visit.push(start_wire);
740
741 while (!visit.empty() && (iter++ < iter_limit)) {
742 WireId cursor = visit.front();
743 visit.pop();
744 // Check to see if we have reached a valid bel pin
745 for (auto bp : ctx->getWireBelPins(cursor)) {
746 if (ctx->getBelType(bp.bel) != dest_type)
747 continue;
748 if (dest_pin != IdString() && bp.pin != dest_pin)
749 continue;
750 if (seen_bels.count(bp.bel))
751 continue;
752 seen_bels.insert(bp.bel);
753 candidates.push_back(bp.bel);
754 }
755 // Search in the appropriate direction up/downstream of the cursor
756 if (dir == PORT_OUT) {
757 for (PipId p : ctx->getPipsDownhill(cursor))
758 if (ctx->checkPipAvail(p)) {
759 WireId dst = ctx->getPipDstWire(p);
760 if (seen_wires.count(dst))
761 continue;
762 seen_wires.insert(dst);
763 visit.push(dst);
764 }
765 } else {
766 for (PipId p : ctx->getPipsUphill(cursor))
767 if (ctx->checkPipAvail(p)) {
768 WireId src = ctx->getPipSrcWire(p);
769 if (seen_wires.count(src))
770 continue;
771 seen_wires.insert(src);
772 visit.push(src);
773 }
774 }
775 }
776 }
777
778 // Get spec locations
779 int GowinUtils::get_center_row(void) const
780 {
781 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
782 return extra->center_row;
783 }
784 int GowinUtils::get_center_col(void) const
785 {
786 const Extra_chip_data_POD *extra = reinterpret_cast<const Extra_chip_data_POD *>(ctx->chip_info->extra_data.get());
787 return extra->center_col;
788 }
789
790 NEXTPNR_NAMESPACE_END