Debian Codegraph @

himbaechel/uarch/gowin/pack_io.cc

raw
1 #include "design_utils.h"
2 #include "log.h"
3 #include "nextpnr.h"
4
5 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
6 #include "himbaechel_constids.h"
7 #include "himbaechel_helpers.h"
8
9 #include "gowin.h"
10 #include "gowin_utils.h"
11 #include "pack.h"
12
13 #include <cinttypes>
14
15 NEXTPNR_NAMESPACE_BEGIN
16
17 // ===================================
18 // IO
19 // ===================================
20 // create IOB connections for gowin_pack
21 // can be called repeatedly when switching inputs, disabled outputs do not change
22 void GowinPacker::make_iob_nets(CellInfo &iob)
23 {
24 for (const auto &port : iob.ports) {
25 const NetInfo *net = iob.getPort(port.first);
26 std::string connected_net = "NET";
27 if (net != nullptr) {
28 if (ctx->verbose) {
29 log_info("%s: %s - %s\n", ctx->nameOf(&iob), port.first.c_str(ctx), ctx->nameOf(net));
30 }
31 if (net->name == ctx->id("$PACKER_VCC")) {
32 connected_net = "VCC";
33 } else if (net->name == ctx->id("$PACKER_GND")) {
34 connected_net = "GND";
35 }
36 iob.setParam(ctx->idf("NET_%s", port.first.c_str(ctx)), connected_net);
37 }
38 }
39 }
40
41 void GowinPacker::config_simple_io(CellInfo &ci)
42 {
43 if (ci.type.in(id_TBUF, id_IOBUF)) {
44 return;
45 }
46 log_info("simple:%s\n", ctx->nameOf(&ci));
47 ci.addInput(id_OEN);
48 if (ci.type == id_OBUF) {
49 ci.connectPort(id_OEN, ctx->nets.at(ctx->id("$PACKER_GND")).get());
50 } else {
51 NPNR_ASSERT(ci.type == id_IBUF);
52 ci.connectPort(id_OEN, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
53 }
54 }
55
56 void GowinPacker::config_bottom_row(CellInfo &ci, Loc loc, uint8_t cnd)
57 {
58 if (!gwu.has_bottom_io_cnds()) {
59 return;
60 }
61 if (!ci.type.in(id_OBUF, id_TBUF, id_IOBUF)) {
62 return;
63 }
64 if (loc.z != BelZ::IOBA_Z) {
65 return;
66 }
67 auto connect_io_wire = [&](IdString port, IdString net_name) {
68 // XXX it is very convenient that nothing terrible happens in case
69 // of absence/presence of a port
70 ci.disconnectPort(port);
71 ci.addInput(port);
72 if (net_name == id_VSS) {
73 ci.connectPort(port, ctx->nets.at(ctx->id("$PACKER_GND")).get());
74 } else {
75 NPNR_ASSERT(net_name == id_VCC);
76 ci.connectPort(port, ctx->nets.at(ctx->id("$PACKER_VCC")).get());
77 }
78 };
79
80 IdString wire_a_net = gwu.get_bottom_io_wire_a_net(cnd);
81 connect_io_wire(id_BOTTOM_IO_PORT_A, wire_a_net);
82
83 IdString wire_b_net = gwu.get_bottom_io_wire_b_net(cnd);
84 connect_io_wire(id_BOTTOM_IO_PORT_B, wire_b_net);
85 }
86
87 // Attributes of deleted cells are copied
88 void GowinPacker::trim_nextpnr_iobs(void)
89 {
90 // Trim nextpnr IOBs - assume IO buffer insertion has been done in synthesis
91 const pool<CellTypePort> top_ports{
92 CellTypePort(id_IBUF, id_I),
93 CellTypePort(id_OBUF, id_O),
94 CellTypePort(id_TBUF, id_O),
95 CellTypePort(id_IOBUF, id_IO),
96 };
97 std::vector<IdString> to_remove;
98 for (auto &cell : ctx->cells) {
99 auto &ci = *cell.second;
100 if (!ci.type.in(ctx->id("$nextpnr_ibuf"), ctx->id("$nextpnr_obuf"), ctx->id("$nextpnr_iobuf")))
101 continue;
102 NetInfo *i = ci.getPort(id_I);
103 if (i && i->driver.cell) {
104 if (!top_ports.count(CellTypePort(i->driver)))
105 log_error("Top-level port '%s' driven by illegal port %s.%s\n", ctx->nameOf(&ci),
106 ctx->nameOf(i->driver.cell), ctx->nameOf(i->driver.port));
107 for (const auto &attr : ci.attrs) {
108 i->driver.cell->setAttr(attr.first, attr.second);
109 }
110 }
111 NetInfo *o = ci.getPort(id_O);
112 if (o) {
113 for (auto &usr : o->users) {
114 if (!top_ports.count(CellTypePort(usr)))
115 log_error("Top-level port '%s' driving illegal port %s.%s\n", ctx->nameOf(&ci),
116 ctx->nameOf(usr.cell), ctx->nameOf(usr.port));
117 for (const auto &attr : ci.attrs) {
118 usr.cell->setAttr(attr.first, attr.second);
119 }
120 // network/port attributes that can be set in the
121 // restriction file and that need to be transferred to real
122 // networks before nextpnr buffers are removed.
123 NetInfo *dst_net = usr.cell->getPort(id_O);
124 if (dst_net != nullptr) {
125 for (const auto &attr : o->attrs) {
126 if (!attr.first.in(id_CLOCK)) {
127 continue;
128 }
129 dst_net->attrs[attr.first] = attr.second;
130 }
131 }
132 }
133 }
134 NetInfo *io = ci.getPort(id_IO);
135 if (io && io->driver.cell) {
136 if (!top_ports.count(CellTypePort(io->driver)))
137 log_error("Top-level port '%s' driven by illegal port %s.%s\n", ctx->nameOf(&ci),
138 ctx->nameOf(io->driver.cell), ctx->nameOf(io->driver.port));
139 for (const auto &attr : ci.attrs) {
140 io->driver.cell->setAttr(attr.first, attr.second);
141 }
142 }
143 ci.disconnectPort(id_I);
144 ci.disconnectPort(id_O);
145 ci.disconnectPort(id_IO);
146 to_remove.push_back(ci.name);
147 }
148 for (IdString cell_name : to_remove)
149 ctx->cells.erase(cell_name);
150 }
151
152 BelId GowinPacker::bind_io(CellInfo &ci)
153 {
154 BelId bel = ctx->getBelByNameStr(ci.attrs.at(id_BEL).as_string());
155 if (bel == BelId()) {
156 log_error("No bel named %s\n", ci.attrs.at(id_BEL).as_string().c_str());
157 }
158 if (!ctx->checkBelAvail(bel)) {
159 log_error("Can't place %s at %s because it's already taken by %s\n", ctx->nameOf(&ci), ctx->nameOfBel(bel),
160 ctx->nameOf(ctx->getBoundBelCell(bel)));
161 }
162 ci.unsetAttr(id_BEL);
163 ctx->bindBel(bel, &ci, PlaceStrength::STRENGTH_LOCKED);
164 return bel;
165 }
166
167 void GowinPacker::pack_iobs(void)
168 {
169 log_info("Pack IOBs...\n");
170 trim_nextpnr_iobs();
171 std::vector<IdString> cells_to_remove;
172
173 for (auto &cell : ctx->cells) {
174 CellInfo &ci = *cell.second;
175 if (!is_io(&ci)) {
176 continue;
177 }
178 // Special case of OBUF without input - we delete such things.
179 if (ci.type == id_OBUF && !ci.getPort(id_I)) {
180 ci.disconnectPort(id_O);
181 cells_to_remove.push_back(ci.name);
182 continue;
183 }
184
185 if (ci.attrs.count(id_BEL) == 0) {
186 log_error("Unconstrained IO:%s\n", ctx->nameOf(&ci));
187 }
188 BelId io_bel = bind_io(ci);
189 Loc io_loc = ctx->getBelLocation(io_bel);
190 if (io_loc.y == ctx->getGridDimY() - 1) {
191 config_bottom_row(ci, io_loc);
192 }
193 if (gwu.is_simple_io_bel(io_bel)) {
194 config_simple_io(ci);
195 }
196 make_iob_nets(ci);
197 }
198
199 for (auto cell : cells_to_remove) {
200 ctx->cells.erase(cell);
201 }
202 }
203
204 // ===================================
205 // Differential IO
206 // ===================================
207
208 std::pair<CellInfo *, CellInfo *> GowinPacker::get_pn_cells(const CellInfo &ci)
209 {
210 CellInfo *p, *n;
211 switch (ci.type.hash()) {
212 case ID_ELVDS_TBUF: /* fall-through */
213 case ID_TLVDS_TBUF: /* fall-through */
214 case ID_ELVDS_OBUF: /* fall-through */
215 case ID_TLVDS_OBUF:
216 p = net_only_drives(ctx, ci.ports.at(id_O).net, is_iob, id_I, true);
217 n = net_only_drives(ctx, ci.ports.at(id_OB).net, is_iob, id_I, true);
218 break;
219 case ID_TLVDS_IBUF_ADC: /* fall-through */
220 case ID_ELVDS_IBUF: /* fall-through */
221 case ID_TLVDS_IBUF:
222 p = net_driven_by(ctx, ci.ports.at(id_I).net, is_iob, id_O);
223 n = net_driven_by(ctx, ci.ports.at(id_IB).net, is_iob, id_O);
224 break;
225 case ID_ELVDS_IOBUF: /* fall-through */
226 case ID_TLVDS_IOBUF:
227 p = net_only_drives(ctx, ci.ports.at(id_IO).net, is_iob, id_I);
228 n = net_only_drives(ctx, ci.ports.at(id_IOB).net, is_iob, id_I);
229 break;
230 default:
231 log_error("Bad diff IO '%s' type '%s'\n", ctx->nameOf(&ci), ci.type.c_str(ctx));
232 }
233 return std::make_pair(p, n);
234 }
235
236 void GowinPacker::mark_iobs_as_diff(CellInfo &ci, std::pair<CellInfo *, CellInfo *> &pn_cells)
237 {
238 pn_cells.first->setParam(id_DIFF, std::string("P"));
239 pn_cells.first->setParam(id_DIFF_TYPE, ci.type.str(ctx));
240 pn_cells.second->setParam(id_DIFF, std::string("N"));
241 pn_cells.second->setParam(id_DIFF_TYPE, ci.type.str(ctx));
242 if (ci.params.count(id_ADC_IO)) {
243 pn_cells.first->setParam(id_ADC_IO, ci.params.at(id_ADC_IO));
244 pn_cells.second->setParam(id_ADC_IO, ci.params.at(id_ADC_IO));
245 }
246 }
247
248 void GowinPacker::switch_diff_ports(CellInfo &ci, std::pair<CellInfo *, CellInfo *> &pn_cells,
249 std::vector<IdString> &nets_to_remove)
250 {
251 CellInfo *iob_p = pn_cells.first;
252 CellInfo *iob_n = pn_cells.second;
253
254 if (ci.type.in(id_TLVDS_TBUF, id_TLVDS_OBUF, id_ELVDS_TBUF, id_ELVDS_OBUF)) {
255 nets_to_remove.push_back(ci.getPort(id_O)->name);
256 ci.disconnectPort(id_O);
257 nets_to_remove.push_back(ci.getPort(id_OB)->name);
258 ci.disconnectPort(id_OB);
259 nets_to_remove.push_back(iob_n->getPort(id_I)->name);
260 iob_n->disconnectPort(id_I);
261
262 if (ci.type.in(id_TLVDS_TBUF, id_ELVDS_TBUF)) {
263 NetInfo *oen_net = iob_n->getPort(id_OEN);
264 if (oen_net != nullptr) {
265 nets_to_remove.push_back(oen_net->name);
266 }
267 iob_n->disconnectPort(id_OEN);
268 iob_p->disconnectPort(id_OEN);
269 ci.movePortTo(id_OEN, iob_p, id_OEN);
270
271 // MIPI
272 if (ci.params.count(id_MIPI_OBUF)) {
273 iob_p->setParam(id_MIPI_OBUF, 1);
274 iob_n->setParam(id_MIPI_OBUF, 1);
275 ci.movePortTo(id_IB, iob_n, id_I);
276 iob_p->copyPortTo(id_OEN, iob_n, id_OEN);
277 }
278 }
279 iob_p->disconnectPort(id_I);
280 ci.movePortTo(id_I, iob_p, id_I);
281 return;
282 }
283 if (ci.type.in(id_TLVDS_IBUF, id_ELVDS_IBUF)) {
284 nets_to_remove.push_back(ci.getPort(id_I)->name);
285 ci.disconnectPort(id_I);
286 nets_to_remove.push_back(ci.getPort(id_IB)->name);
287 ci.disconnectPort(id_IB);
288 iob_n->disconnectPort(id_O);
289 iob_p->disconnectPort(id_O);
290 ci.movePortTo(id_O, iob_p, id_O);
291 return;
292 }
293 if (ci.type.in(id_TLVDS_IOBUF, id_ELVDS_IOBUF)) {
294 nets_to_remove.push_back(ci.getPort(id_IO)->name);
295 ci.disconnectPort(id_IO);
296 nets_to_remove.push_back(ci.getPort(id_IOB)->name);
297 ci.disconnectPort(id_IOB);
298 nets_to_remove.push_back(iob_n->getPort(id_I)->name);
299 iob_n->disconnectPort(id_I);
300 iob_n->disconnectPort(id_OEN);
301
302 iob_p->disconnectPort(id_OEN);
303 ci.movePortTo(id_OEN, iob_p, id_OEN);
304 iob_p->disconnectPort(id_I);
305 ci.movePortTo(id_I, iob_p, id_I);
306 iob_p->disconnectPort(id_O);
307 ci.movePortTo(id_O, iob_p, id_O);
308 return;
309 }
310 if (ci.type.in(id_TLVDS_IBUF_ADC)) {
311 nets_to_remove.push_back(ci.getPort(id_I)->name);
312 ci.disconnectPort(id_I);
313 nets_to_remove.push_back(ci.getPort(id_IB)->name);
314 ci.disconnectPort(id_IB);
315 iob_p->disconnectPort(id_O);
316 iob_n->disconnectPort(id_O);
317
318 ci.movePortTo(id_ADCEN, iob_p, id_ADCEN);
319 return;
320 }
321 }
322
323 // ===================================
324 // I3C
325 // ===================================
326 void GowinPacker::pack_i3c(void)
327 {
328 log_info("Pack I3C IOs...\n");
329 std::vector<IdString> cells_to_remove;
330
331 for (auto &cell : ctx->cells) {
332 CellInfo &ci = *cell.second;
333 if (!is_i3c(&ci)) {
334 continue;
335 }
336 // check for I3C-capable pin A
337 CellInfo *iob = net_only_drives(ctx, ci.ports.at(id_IO).net, is_iob, id_I);
338 if (iob == nullptr || iob->bel == BelId()) {
339 log_error("I3C %s IO is not connected to the input pin or the pin is not constrained.\n", ctx->nameOf(&ci));
340 }
341 BelId iob_bel = iob->bel;
342 Loc iob_loc = ctx->getBelLocation(iob_bel);
343
344 if (!gwu.get_i3c_capable(iob_loc.x, iob_loc.y)) {
345 log_error("Can't place %s. Not I3C capable X%dY%d.\n", ctx->nameOf(&ci), iob_loc.x, iob_loc.y);
346 }
347 ci.disconnectPort(id_IO);
348 iob->disconnectPort(id_I);
349 ci.movePortTo(id_I, iob, id_I);
350 ci.movePortTo(id_O, iob, id_O);
351 iob->disconnectPort(id_OEN);
352 ci.movePortTo(id_MODESEL, iob, id_OEN);
353
354 iob->setParam(id_I3C_IOBUF, 1);
355 cells_to_remove.push_back(ci.name);
356 }
357
358 for (auto cell : cells_to_remove) {
359 ctx->cells.erase(cell);
360 }
361 }
362
363 // ===================================
364 // MIPI IO
365 // ===================================
366 void GowinPacker::pack_mipi(void)
367 {
368 log_info("Pack MIPI IOs...\n");
369 std::vector<std::unique_ptr<CellInfo>> new_cells;
370
371 for (auto &cell : ctx->cells) {
372 CellInfo &ci = *cell.second;
373 if (!is_mipi(&ci)) {
374 continue;
375 }
376 switch (ci.type.hash()) {
377 case ID_MIPI_OBUF_A: /* fall-through */
378 case ID_MIPI_OBUF: {
379 // check for MIPI-capable pin
380 CellInfo *out_iob = net_only_drives(ctx, ci.ports.at(id_O).net, is_iob, id_I, true);
381 if (out_iob == nullptr || out_iob->bel == BelId()) {
382 log_error("MIPI %s is not connected to the output pin or the pin is not constrained.\n",
383 ctx->nameOf(&ci));
384 }
385 if (out_iob->params.count(id_I3C_IOBUF)) {
386 log_error("Can't place MIPI %s. Conflict with I3C %s.\n", ctx->nameOf(&ci), ctx->nameOf(out_iob));
387 }
388 BelId iob_bel = out_iob->bel;
389 Loc iob_loc = ctx->getBelLocation(iob_bel);
390 iob_loc.z = BelZ::MIPIOBUF_Z;
391 BelId mipi_bel = ctx->getBelByLocation(iob_loc);
392 if (mipi_bel == BelId()) {
393 log_error("Can't place MIPI %s at X%dY%d/IOBA.\n", ctx->nameOf(&ci), iob_loc.x, iob_loc.y);
394 }
395
396 if (ci.type == id_MIPI_OBUF_A) {
397 // if serialization is used then IL and input of serializator must be in the same network
398 NetInfo *i_net = ci.getPort(id_I);
399 NetInfo *il_net = ci.getPort(id_IL);
400 if (i_net != il_net) {
401 if (i_net != nullptr && is_iologico(i_net->driver.cell)) {
402 if (i_net->driver.cell->getPort(id_D0) != ci.getPort(id_IL)) {
403 log_error("MIPI %s port IL and IOLOGIC %s port D0 are in differrent networks!\n",
404 ctx->nameOf(&ci), ctx->nameOf(i_net->driver.cell));
405 }
406 } else {
407 log_error("MIPI %s ports IL and I are in differrent networks!\n", ctx->nameOf(&ci));
408 }
409 }
410 ci.disconnectPort(id_IL);
411 }
412
413 ctx->bindBel(mipi_bel, &ci, PlaceStrength::STRENGTH_LOCKED);
414
415 // Create TBUF with additional input IB
416 IdString mipi_tbuf_name = gwu.create_aux_name(ci.name);
417 new_cells.push_back(gwu.create_cell(mipi_tbuf_name, id_TLVDS_TBUF));
418
419 CellInfo *mipi_tbuf = new_cells.back().get();
420 mipi_tbuf->addInput(id_I);
421 mipi_tbuf->addInput(id_IB);
422 mipi_tbuf->addOutput(id_O);
423 mipi_tbuf->addOutput(id_OB);
424 mipi_tbuf->addInput(id_OEN);
425 ci.movePortTo(id_I, mipi_tbuf, id_I);
426 ci.movePortTo(id_IB, mipi_tbuf, id_IB);
427 ci.movePortTo(id_O, mipi_tbuf, id_O);
428 ci.movePortTo(id_OB, mipi_tbuf, id_OB);
429 ci.movePortTo(id_MODESEL, mipi_tbuf, id_OEN);
430
431 mipi_tbuf->setParam(id_MIPI_OBUF, 1);
432 } break;
433 case ID_MIPI_IBUF: {
434 // check for MIPI-capable pin A
435 CellInfo *in_iob = net_only_drives(ctx, ci.ports.at(id_IO).net, is_iob, id_I);
436 if (in_iob == nullptr || in_iob->bel == BelId()) {
437 log_error("MIPI %s IO is not connected to the input pin or the pin is not constrained.\n",
438 ctx->nameOf(&ci));
439 }
440 // check A IO placing
441 if (in_iob->params.count(id_I3C_IOBUF)) {
442 log_error("Can't place MIPI %s. Conflict with I3C %s.\n", ctx->nameOf(&ci), ctx->nameOf(in_iob));
443 }
444 BelId iob_bel = in_iob->bel;
445 Loc iob_loc = ctx->getBelLocation(iob_bel);
446 if (iob_loc.z != BelZ::IOBA_Z) {
447 log_error("MIPI %s IO pin must be connected to the A IO pin.\n", ctx->nameOf(&ci));
448 }
449
450 iob_loc.z = BelZ::MIPIIBUF_Z;
451 BelId mipi_bel = ctx->getBelByLocation(iob_loc);
452 if (mipi_bel == BelId()) {
453 log_error("Can't place MIPI %s at X%dY%d/IOBA.\n", ctx->nameOf(&ci), iob_loc.x, iob_loc.y);
454 }
455
456 // check for MIPI-capable pin B
457 CellInfo *inb_iob = net_only_drives(ctx, ci.ports.at(id_IOB).net, is_iob, id_I);
458 if (inb_iob == nullptr || inb_iob->bel == BelId()) {
459 log_error("MIPI %s IOB is not connected to the input pin or the pin is not constrained.\n",
460 ctx->nameOf(&ci));
461 }
462 // check B IO placing
463 if (inb_iob->params.count(id_I3C_IOBUF)) {
464 log_error("Can't place MIPI %s. Conflict with I3C %s.\n", ctx->nameOf(&ci), ctx->nameOf(inb_iob));
465 }
466 BelId iobb_bel = inb_iob->bel;
467 Loc iobb_loc = ctx->getBelLocation(iobb_bel);
468 if (iobb_loc.z != BelZ::IOBB_Z || iobb_loc.x != iob_loc.x || iobb_loc.y != iob_loc.y) {
469 log_error("MIPI %s IOB pin must be connected to the B IO pin.\n", ctx->nameOf(&ci));
470 }
471 // MIPI IBUF uses next pair of IOs too
472 Loc iob_next_loc(iob_loc);
473 ++iob_next_loc.x;
474 iob_next_loc.z = BelZ::IOBA_Z;
475 CellInfo *inc_iob = ctx->getBoundBelCell(ctx->getBelByLocation(iob_next_loc));
476 iob_next_loc.z = BelZ::IOBB_Z;
477 CellInfo *other_cell_b = ctx->getBoundBelCell(ctx->getBelByLocation(iob_next_loc));
478 if (inc_iob != nullptr || other_cell_b != nullptr) {
479 log_error("MIPI %s cannot be placed in same IO with %s.\n", ctx->nameOf(&ci),
480 inc_iob == nullptr ? ctx->nameOf(other_cell_b) : ctx->nameOf(inc_iob));
481 }
482
483 ctx->bindBel(mipi_bel, &ci, PlaceStrength::STRENGTH_LOCKED);
484
485 // reconnect wires
486 // A
487 ci.disconnectPort(id_IO);
488 in_iob->disconnectPort(id_I);
489 ci.movePortTo(id_I, in_iob, id_I);
490 ci.movePortTo(id_OH, in_iob, id_O);
491 in_iob->disconnectPort(id_OEN);
492 ci.movePortTo(id_OEN, in_iob, id_OEN);
493 // B
494 ci.disconnectPort(id_IO);
495 inb_iob->disconnectPort(id_I);
496 ci.movePortTo(id_IB, inb_iob, id_I);
497 ci.movePortTo(id_OB, inb_iob, id_O);
498 inb_iob->disconnectPort(id_OEN);
499 ci.movePortTo(id_OENB, inb_iob, id_OEN);
500 // MIPI enable (?)
501 ci.addInput(ctx->id("MIPIEN0"));
502 ci.connectPort(ctx->id("MIPIEN0"), ctx->nets.at(ctx->id("$PACKER_GND")).get());
503 ci.addInput(ctx->id("MIPIEN1"));
504 ci.connectPort(ctx->id("MIPIEN1"), ctx->nets.at(ctx->id("$PACKER_VCC")).get());
505
506 in_iob->setParam(id_MIPI_IBUF, 1);
507 inb_iob->setParam(id_MIPI_IBUF, 1);
508 } break;
509 default:
510 log_error("MIPI %s is not implemented.\n", ci.type.c_str(ctx));
511 }
512 }
513 for (auto &ncell : new_cells) {
514 ctx->cells[ncell->name] = std::move(ncell);
515 }
516 }
517
518 void GowinPacker::pack_diff_iobs(void)
519 {
520 log_info("Pack diff IOBs...\n");
521 std::vector<IdString> cells_to_remove, nets_to_remove;
522
523 for (auto &cell : ctx->cells) {
524 CellInfo &ci = *cell.second;
525 if (!is_diffio(&ci)) {
526 continue;
527 }
528 if (!gwu.is_diff_io_supported(ci.type)) {
529 log_error("%s is not supported\n", ci.type.c_str(ctx));
530 }
531 cells_to_remove.push_back(ci.name);
532 auto pn_cells = get_pn_cells(ci);
533 NPNR_ASSERT(pn_cells.first != nullptr && pn_cells.second != nullptr);
534
535 mark_iobs_as_diff(ci, pn_cells);
536 switch_diff_ports(ci, pn_cells, nets_to_remove);
537 }
538
539 for (auto cell : cells_to_remove) {
540 ctx->cells.erase(cell);
541 }
542 for (auto net : nets_to_remove) {
543 ctx->nets.erase(net);
544 }
545 }
546
547 static bool is_ff(const Context *ctx, CellInfo *cell) { return is_dff(cell); }
548
549 static bool incompatible_ffs(IdString type_a, IdString type_b)
550 {
551 return type_a != type_b &&
552 ((type_a == id_DFFS && type_b != id_DFFR) || (type_a == id_DFFR && type_b != id_DFFS) ||
553 (type_a == id_DFFSE && type_b != id_DFFRE) || (type_a == id_DFFRE && type_b != id_DFFSE) ||
554 (type_a == id_DFFP && type_b != id_DFFC) || (type_a == id_DFFC && type_b != id_DFFP) ||
555 (type_a == id_DFFPE && type_b != id_DFFCE) || (type_a == id_DFFCE && type_b != id_DFFPE) ||
556 (type_a == id_DFFNS && type_b != id_DFFNR) || (type_a == id_DFFNR && type_b != id_DFFNS) ||
557 (type_a == id_DFFNSE && type_b != id_DFFNRE) || (type_a == id_DFFNRE && type_b != id_DFFNSE) ||
558 (type_a == id_DFFNP && type_b != id_DFFNC) || (type_a == id_DFFNC && type_b != id_DFFNP) ||
559 (type_a == id_DFFNPE && type_b != id_DFFNCE) || (type_a == id_DFFNCE && type_b != id_DFFNPE) ||
560 (type_a == id_DFF && type_b != id_DFF) || (type_a == id_DFFN && type_b != id_DFFN) ||
561 (type_a == id_DFFE && type_b != id_DFFE) || (type_a == id_DFFNE && type_b != id_DFFNE));
562 }
563
564 void GowinPacker::pack_io_regs(void)
565 {
566 log_info("Pack FFs into IO cells...\n");
567 std::vector<IdString> cells_to_remove;
568 std::vector<IdString> nets_to_remove;
569 std::vector<std::unique_ptr<CellInfo>> new_cells;
570
571 for (auto &cell : ctx->cells) {
572 CellInfo &ci = *cell.second;
573 if (!is_io(&ci)) {
574 continue;
575 }
576 if (ci.attrs.count(id_NOIOBFF)) {
577 if (ctx->debug) {
578 log_info(" NOIOBFF attribute at %s. Skipping FF placement.\n", ctx->nameOf(&ci));
579 }
580 continue;
581 }
582
583 // In the case of placing multiple registers in the IO it should be
584 // noted that the CLK, ClockEnable and LocalSetReset nets must
585 // match.
586 const NetInfo *clk_net = nullptr;
587 const NetInfo *ce_net = nullptr;
588 const NetInfo *lsr_net = nullptr;
589 IdString reg_type;
590
591 // input reg in IO
592 CellInfo *iologic_i = nullptr;
593 if ((ci.type == id_IBUF && (ctx->settings.count(id_IREG_IN_IOB) || ci.attrs.count(id_IOBFF))) ||
594 (ci.type == id_IOBUF && (ctx->settings.count(id_IOREG_IN_IOB) || ci.attrs.count(id_IOBFF)))) {
595
596 if (ci.getPort(id_O) == nullptr) {
597 continue;
598 }
599 // OBUF O -> D FF
600 CellInfo *ff = net_only_drives(ctx, ci.ports.at(id_O).net, is_ff, id_D);
601 if (ff == nullptr) {
602 if (ci.attrs.count(id_IOBFF)) {
603 log_warning("Port O of %s is not connected to FF.\n", ctx->nameOf(&ci));
604 }
605 continue;
606 }
607 if (ci.ports.at(id_O).net->users.entries() != 1) {
608 if (ci.attrs.count(id_IOBFF)) {
609 log_warning("Port O of %s is the driver of %s multi-sink network.\n", ctx->nameOf(&ci),
610 ctx->nameOf(ci.ports.at(id_O).net));
611 }
612 continue;
613 }
614 BelId l_bel = get_iologici_bel(&ci);
615 if (l_bel == BelId()) {
616 continue;
617 }
618 if (ctx->debug) {
619 log_info(" trying %s ff as Input Register of %s IO\n", ctx->nameOf(ff), ctx->nameOf(&ci));
620 }
621
622 clk_net = ff->getPort(id_CLK);
623 ce_net = ff->getPort(id_CE);
624 for (IdString port : {id_SET, id_RESET, id_PRESET, id_CLEAR}) {
625 lsr_net = ff->getPort(port);
626 if (lsr_net != nullptr) {
627 break;
628 }
629 }
630 reg_type = ff->type;
631
632 // create IOLOGIC cell for flipflop
633 IdString iologic_name = gwu.create_aux_name(ci.name, 0, "_iobff$");
634 auto iologic_cell = gwu.create_cell(iologic_name, id_IOLOGICI_EMPTY);
635 new_cells.push_back(std::move(iologic_cell));
636 iologic_i = new_cells.back().get();
637
638 // move ports
639 for (auto &port : ff->ports) {
640 IdString port_name = port.first;
641 ff->movePortTo(port_name, iologic_i, port_name != id_Q ? port_name : id_Q4);
642 }
643 if (ctx->verbose) {
644 log_info(" place FF %s into IBUF %s, make iologic_i %s\n", ctx->nameOf(ff), ctx->nameOf(&ci),
645 ctx->nameOf(iologic_i));
646 }
647 iologic_i->setAttr(id_HAS_REG, 1);
648 iologic_i->setAttr(id_IREG_TYPE, ff->type.str(ctx));
649 cells_to_remove.push_back(ff->name);
650 }
651
652 // output reg in IO
653 CellInfo *iologic_o = nullptr;
654 if ((ci.type == id_OBUF && (ctx->settings.count(id_OREG_IN_IOB) || ci.attrs.count(id_IOBFF))) ||
655 (ci.type == id_IOBUF && (ctx->settings.count(id_IOREG_IN_IOB) || ci.attrs.count(id_IOBFF)))) {
656 do {
657 if (ci.getPort(id_I) == nullptr) {
658 break;
659 }
660 // OBUF I <- Q FF
661 CellInfo *ff = net_driven_by(ctx, ci.ports.at(id_I).net, is_ff, id_Q);
662 if (ff == nullptr) {
663 if (ci.attrs.count(id_IOBFF)) {
664 log_warning("Port I of %s is not connected to FF.\n", ctx->nameOf(&ci));
665 }
666 } else {
667 if (ci.ports.at(id_I).net->users.entries() != 1) {
668 if (ci.attrs.count(id_IOBFF)) {
669 log_warning("Port I of %s is not the only sink on the %s network.\n", ctx->nameOf(&ci),
670 ctx->nameOf(ci.ports.at(id_I).net));
671 }
672 break;
673 }
674 BelId l_bel = get_iologico_bel(&ci);
675 if (l_bel == BelId()) {
676 break;
677 }
678
679 const NetInfo *this_clk_net = ff->getPort(id_CLK);
680 const NetInfo *this_ce_net = ff->getPort(id_CE);
681 const NetInfo *this_lsr_net;
682 for (IdString port : {id_SET, id_RESET, id_PRESET, id_CLEAR}) {
683 this_lsr_net = ff->getPort(port);
684 if (this_lsr_net != nullptr) {
685 break;
686 }
687 }
688 // The IOBUF may already have registers placed
689 if (ci.type == id_IOBUF) {
690 if (iologic_i != nullptr) {
691 if (incompatible_ffs(ff->type, reg_type)) {
692 if (ci.attrs.count(id_IOBFF)) {
693 log_warning("OREG type conflict:%s:%s vs %s IREG:%s\n", ctx->nameOf(ff),
694 ff->type.c_str(ctx), ctx->nameOf(&ci), reg_type.c_str(ctx));
695 }
696 break;
697 } else {
698 if (clk_net != this_clk_net || ce_net != this_ce_net || lsr_net != this_lsr_net) {
699 if (clk_net != this_clk_net) {
700 if (ci.attrs.count(id_IOBFF)) {
701 log_warning("Conflicting OREG CLK nets at %s:'%s' vs '%s'\n",
702 ctx->nameOf(&ci), ctx->nameOf(clk_net),
703 ctx->nameOf(this_clk_net));
704 }
705 }
706 if (ce_net != this_ce_net) {
707 if (ci.attrs.count(id_IOBFF)) {
708 log_warning("Conflicting OREG CE nets at %s:'%s' vs '%s'\n",
709 ctx->nameOf(&ci), ctx->nameOf(ce_net),
710 ctx->nameOf(this_ce_net));
711 }
712 }
713 if (lsr_net != this_lsr_net) {
714 if (ci.attrs.count(id_IOBFF)) {
715 log_warning("Conflicting OREG LSR nets at %s:'%s' vs '%s'\n",
716 ctx->nameOf(&ci), ctx->nameOf(lsr_net),
717 ctx->nameOf(this_lsr_net));
718 }
719 }
720 break;
721 }
722 }
723 } else {
724 clk_net = this_clk_net;
725 ce_net = this_ce_net;
726 lsr_net = this_lsr_net;
727 reg_type = ff->type;
728 }
729 }
730
731 // create IOLOGIC cell for flipflop
732 IdString iologic_name = gwu.create_aux_name(ci.name, 1, "_iobff$");
733 auto iologic_cell = gwu.create_cell(iologic_name, id_IOLOGICO_EMPTY);
734 new_cells.push_back(std::move(iologic_cell));
735 iologic_o = new_cells.back().get();
736
737 // move ports
738 for (auto &port : ff->ports) {
739 IdString port_name = port.first;
740 ff->movePortTo(port_name, iologic_o, port_name != id_D ? port_name : id_D0);
741 }
742 if (ctx->verbose) {
743 log_info(" place FF %s into OBUF %s, make iologic_o %s\n", ctx->nameOf(ff), ctx->nameOf(&ci),
744 ctx->nameOf(iologic_o));
745 }
746 iologic_o->setAttr(id_HAS_REG, 1);
747 iologic_o->setAttr(id_OREG_TYPE, ff->type.str(ctx));
748 cells_to_remove.push_back(ff->name);
749 }
750 } while (false);
751 }
752
753 // output enable reg in IO
754 if (ci.type == id_IOBUF && (ctx->settings.count(id_IOREG_IN_IOB) || ci.attrs.count(id_IOBFF))) {
755 do {
756 if (ci.getPort(id_OEN) == nullptr) {
757 break;
758 }
759 // IOBUF OEN <- Q FF
760 CellInfo *ff = net_driven_by(ctx, ci.ports.at(id_OEN).net, is_ff, id_Q);
761 if (ff != nullptr) {
762 if (ci.ports.at(id_OEN).net->users.entries() != 1) {
763 if (ci.attrs.count(id_IOBFF)) {
764 log_warning("Port OEN of %s is not the only sink on the %s network.\n", ctx->nameOf(&ci),
765 ctx->nameOf(ci.ports.at(id_OEN).net));
766 }
767 break;
768 }
769 BelId l_bel = get_iologico_bel(&ci);
770 if (l_bel == BelId()) {
771 break;
772 }
773 if (ctx->debug) {
774 log_info(" trying %s ff as Output Enable Register of %s IO\n", ctx->nameOf(ff),
775 ctx->nameOf(&ci));
776 }
777
778 const NetInfo *this_clk_net = ff->getPort(id_CLK);
779 const NetInfo *this_ce_net = ff->getPort(id_CE);
780 const NetInfo *this_lsr_net;
781 for (IdString port : {id_SET, id_RESET, id_PRESET, id_CLEAR}) {
782 this_lsr_net = ff->getPort(port);
783 if (this_lsr_net != nullptr) {
784 break;
785 }
786 }
787
788 // The IOBUF may already have registers placed
789 if (iologic_i != nullptr || iologic_o != nullptr) {
790 if (iologic_o == nullptr) {
791 iologic_o = iologic_i;
792 }
793 if (incompatible_ffs(ff->type, reg_type)) {
794 if (ci.attrs.count(id_IOBFF)) {
795 log_warning("TREG type conflict:%s:%s vs %s IREG/OREG:%s\n", ctx->nameOf(ff),
796 ff->type.c_str(ctx), ctx->nameOf(&ci), reg_type.c_str(ctx));
797 }
798 break;
799 } else {
800 if (clk_net != this_clk_net || ce_net != this_ce_net || lsr_net != this_lsr_net) {
801 if (clk_net != this_clk_net) {
802 if (ci.attrs.count(id_IOBFF)) {
803 log_warning("Conflicting TREG CLK nets at %s:'%s' vs '%s'\n", ctx->nameOf(&ci),
804 ctx->nameOf(clk_net), ctx->nameOf(this_clk_net));
805 }
806 }
807 if (ce_net != this_ce_net) {
808 if (ci.attrs.count(id_IOBFF)) {
809 log_warning("Conflicting TREG CE nets at %s:'%s' vs '%s'\n", ctx->nameOf(&ci),
810 ctx->nameOf(ce_net), ctx->nameOf(this_ce_net));
811 }
812 }
813 if (lsr_net != this_lsr_net) {
814 if (ci.attrs.count(id_IOBFF)) {
815 log_warning("Conflicting TREG LSR nets at %s:'%s' vs '%s'\n", ctx->nameOf(&ci),
816 ctx->nameOf(lsr_net), ctx->nameOf(this_lsr_net));
817 }
818 }
819 break;
820 }
821 }
822 }
823
824 if (iologic_o == nullptr) {
825 // create IOLOGIC cell for flipflop
826 IdString iologic_name = gwu.create_aux_name(ci.name, 2, "_iobff$");
827 auto iologic_cell = gwu.create_cell(iologic_name, id_IOLOGICO_EMPTY);
828 new_cells.push_back(std::move(iologic_cell));
829 iologic_o = new_cells.back().get();
830 }
831
832 // move ports
833 for (auto &port : ff->ports) {
834 IdString port_name = port.first;
835 if (port_name == id_Q) {
836 continue;
837 }
838 ff->movePortTo(port_name, iologic_o, port_name != id_D ? port_name : id_TX);
839 }
840
841 nets_to_remove.push_back(ci.getPort(id_OEN)->name);
842 ci.disconnectPort(id_OEN);
843 ff->disconnectPort(id_Q);
844
845 if (ctx->verbose) {
846 log_info(" place FF %s into IOBUF %s, make iologic_o %s\n", ctx->nameOf(ff), ctx->nameOf(&ci),
847 ctx->nameOf(iologic_o));
848 }
849 iologic_o->setAttr(id_HAS_REG, 1);
850 iologic_o->setAttr(id_TREG_TYPE, ff->type.str(ctx));
851 cells_to_remove.push_back(ff->name);
852 }
853 } while (false);
854 }
855 }
856
857 for (auto cell : cells_to_remove) {
858 ctx->cells.erase(cell);
859 }
860
861 for (auto &ncell : new_cells) {
862 ctx->cells[ncell->name] = std::move(ncell);
863 }
864
865 for (auto net : nets_to_remove) {
866 ctx->nets.erase(net);
867 }
868 }
869
870 NEXTPNR_NAMESPACE_END