Debian Codegraph @

himbaechel/uarch/gatemate/bitstream.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include <fstream>
21
22 #include <boost/algorithm/string.hpp>
23 #include "config.h"
24 #include "gatemate.h"
25 #include "gatemate_util.h"
26 #include "uarch/gatemate/pack.h"
27
28 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
29 #include "himbaechel_constids.h"
30
31 NEXTPNR_NAMESPACE_BEGIN
32
33 namespace {
34
35 struct BitstreamBackend
36 {
37 Context *ctx;
38 GateMateImpl *uarch;
39 const std::string &device;
40 std::ostream &out;
41
42 BitstreamBackend(Context *ctx, GateMateImpl *uarch, const std::string &device, std::ostream &out)
43 : ctx(ctx), uarch(uarch), device(device), out(out) {};
44
45 const GateMateTileExtraDataPOD *tile_extra_data(int tile) const
46 {
47 return reinterpret_cast<const GateMateTileExtraDataPOD *>(ctx->chip_info->tile_insts[tile].extra_data.get());
48 }
49
50 bool need_inversion(CellInfo *cell, IdString port)
51 {
52 PortRef sink;
53 sink.cell = cell;
54 sink.port = port;
55
56 NetInfo *net_info = cell->getPort(port);
57 if (!net_info)
58 return false;
59
60 WireId src_wire = ctx->getNetinfoSourceWire(net_info);
61 WireId dst_wire = ctx->getNetinfoSinkWire(net_info, sink, 0);
62
63 if (src_wire == WireId())
64 return false;
65
66 WireId cursor = dst_wire;
67 bool invert = false;
68 if (net_info->driver.cell && uarch->pass_backtrace.count(net_info->driver.cell->name)) {
69 auto &bt = uarch->pass_backtrace[net_info->driver.cell->name];
70 if (bt.count(net_info->driver.port)) {
71 IdString src_port = bt[net_info->driver.port];
72 invert ^= need_inversion(net_info->driver.cell, src_port);
73 }
74 }
75 while (cursor != WireId() && cursor != src_wire) {
76 auto it = net_info->wires.find(cursor);
77
78 if (it == net_info->wires.end())
79 break;
80
81 PipId pip = it->second.pip;
82 if (pip == PipId())
83 break;
84
85 invert ^= ctx->isPipInverting(pip);
86 cursor = ctx->getPipSrcWire(pip);
87 }
88
89 return invert;
90 }
91
92 void update_cpe_lt(CellInfo *cell, IdString port, IdString init, dict<IdString, Property> &params, bool even)
93 {
94 unsigned init_val = int_or_default(params, init);
95 bool invert = need_inversion(cell, port);
96 if (invert) {
97 if (even)
98 init_val = (init_val & 0b1010) >> 1 | (init_val & 0b0101) << 1;
99 else
100 init_val = (init_val & 0b0011) << 2 | (init_val & 0b1100) >> 2;
101 params[init] = Property(init_val, 4);
102 }
103 }
104
105 void update_cpe_inv(CellInfo *cell, IdString port, IdString param, dict<IdString, Property> &params)
106 {
107 unsigned init_val = int_or_default(params, param);
108 bool invert = need_inversion(cell, port);
109 if (invert) {
110 params[param] = Property(3 - init_val, 2);
111 }
112 }
113
114 void update_cpe_mux(CellInfo *cell, IdString port, IdString param, int bit, dict<IdString, Property> &params)
115 {
116 // Mux inversion data is contained in other CPE half
117 unsigned init_val = int_or_default(params, param);
118 bool invert = need_inversion(cell, port);
119 if (invert) {
120 int old = (init_val >> bit) & 1;
121 int val = (init_val & (~(1 << bit) & 0xf)) | ((!old) << bit);
122 params[param] = Property(val, 4);
123 }
124 }
125
126 std::vector<bool> int_to_bitvector(int val, int size)
127 {
128 std::vector<bool> bv;
129 for (int i = 0; i < size; i++) {
130 bv.push_back((val & (1 << i)) != 0);
131 }
132 return bv;
133 }
134
135 CfgLoc get_config_loc(int tile)
136 {
137 auto ti = *tile_extra_data(tile);
138 CfgLoc loc;
139 loc.die = ti.die;
140 loc.x = ti.bit_x;
141 loc.y = ti.bit_y;
142 return loc;
143 }
144
145 CfgLoc get_ram_config_loc(int tile)
146 {
147 auto ti = *tile_extra_data(tile);
148 CfgLoc loc;
149 loc.die = ti.die;
150 loc.x = (ti.bit_x - 17) / 16;
151 loc.y = (ti.bit_y - 1) / 8;
152 return loc;
153 }
154
155 void export_connection(ChipConfig &cc, PipId pip)
156 {
157 const auto &extra_data = *uarch->pip_extra_data(pip);
158 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && (extra_data.flags & MUX_VISIBLE)) {
159 IdString name = IdString(extra_data.name);
160 CfgLoc loc = get_config_loc(pip.tile);
161 std::string word = name.c_str(ctx);
162 if (extra_data.flags & MUX_CONFIG) {
163 cc.configs[loc.die].add_word(word, int_to_bitvector(extra_data.value, extra_data.bits));
164 } else {
165 int id = tile_extra_data(pip.tile)->prim_id;
166 if (boost::starts_with(word, "IM."))
167 boost::replace_all(word, "IM.", stringf("IM%d.", id));
168 else if (boost::starts_with(word, "OM."))
169 boost::replace_all(word, "OM.", stringf("OM%d.", id));
170 else if (boost::starts_with(word, "CPE."))
171 boost::replace_all(word, "CPE.", stringf("CPE%d.", id));
172 else if (boost::starts_with(word, "IOES."))
173 boost::replace_all(word, "IOES.", stringf("IOES%d.", id));
174 else if (boost::starts_with(word, "LES."))
175 boost::replace_all(word, "LES.", stringf("LES%d.", id));
176 else if (boost::starts_with(word, "BES."))
177 boost::replace_all(word, "BES.", stringf("BES%d.", id));
178 else if (boost::starts_with(word, "RES."))
179 boost::replace_all(word, "RES.", stringf("RES%d.", id));
180 else if (boost::starts_with(word, "TES."))
181 boost::replace_all(word, "TES.", stringf("TES%d.", id));
182 if (boost::starts_with(word, "SB_DRIVE.")) {
183 Loc l;
184 auto ti = *tile_extra_data(pip.tile);
185 tile_xy(ctx->chip_info, pip.tile, l.x, l.y);
186 l.z = CPE_LT_U_Z;
187 BelId cpe_bel = ctx->getBelByLocation(l);
188 // Only if switchbox is inside core (same as sharing location with CPE)
189 if (cpe_bel != BelId() && ctx->getBelType(cpe_bel) == id_CPE_LT_U) {
190 // Bitstream data for certain SB_DRIVES is located in other tiles
191 switch (word[14]) {
192 case '3':
193 if (ti.tile_x >= 4) {
194 loc.x -= 2;
195 word[14] = '1';
196 };
197 break;
198 case '4':
199 if (ti.tile_y >= 4) {
200 loc.y -= 2;
201 word[14] = '2';
202 };
203 break;
204 case '1':
205 if (ti.tile_x <= 3) {
206 loc.x += 2;
207 word[14] = '3';
208 };
209 break;
210 case '2':
211 if (ti.tile_y <= 3) {
212 loc.y += 2;
213 word[14] = '4';
214 };
215 break;
216 default:
217 break;
218 }
219 }
220 }
221
222 cc.tiles[loc].add_word(word, int_to_bitvector(extra_data.value, extra_data.bits));
223 }
224 }
225 }
226
227 void update_multiplier_input(IdString cell_name, dict<IdString, Property> &params)
228 {
229 auto *net = ctx->cells.at(cell_name)->ports.at(id_OUT).net;
230
231 int64_t driver_l10 = ctx->cells.at(cell_name)->params[id_INIT_L10].as_int64();
232 bool driver_is_inverted = driver_l10 == LUT_INV_D0;
233
234 bool all_correct = true;
235 bool all_inverted = true;
236
237 for (PortRef user : net->users) {
238 auto column_parity = user.cell->constr_x % 2;
239 auto should_be_inverted = driver_is_inverted ? column_parity == 0 : column_parity == 1;
240 auto inversion = need_inversion(user.cell, user.port);
241 all_correct &= (inversion == should_be_inverted);
242 all_inverted &= (inversion != should_be_inverted);
243 }
244
245 NPNR_ASSERT(!(all_correct && all_inverted) && "net doesn't drive any ports?");
246
247 if (!all_correct && !all_inverted) {
248 log_warning("Multiplier net '%s' has inconsistent inversion.\n", net->name.c_str(ctx));
249
250 auto driver_loc = ctx->getBelLocation(net->driver.cell->bel);
251 log_warning("Net is driven from (%d, %d)\n", driver_loc.x, driver_loc.y);
252
253 log_warning(" these ports are not inverted:\n");
254 for (PortRef user : net->users) {
255 auto loc = ctx->getBelLocation(user.cell->bel);
256
257 auto should_be_inverted = user.cell->constr_x % 2 == 1;
258 auto inversion = need_inversion(user.cell, user.port);
259 if (inversion == should_be_inverted)
260 log_warning(" %s.%s at (%d, %d)\n", user.cell->name.c_str(ctx), user.port.c_str(ctx), loc.x,
261 loc.y);
262 }
263
264 log_warning(" these ports are inverted:\n");
265 for (PortRef user : net->users) {
266 auto loc = ctx->getBelLocation(user.cell->bel);
267
268 auto should_be_inverted = user.cell->constr_x % 2 == 1;
269 auto inversion = need_inversion(user.cell, user.port);
270 if (inversion != should_be_inverted)
271 log_warning(" %s.%s at (%d, %d)\n", user.cell->name.c_str(ctx), user.port.c_str(ctx), loc.x,
272 loc.y);
273 }
274 } else if (all_inverted) {
275 params[id_INIT_L10] = Property(~driver_l10 & 0b1111, 4);
276 if (ctx->debug)
277 log_info("Multiplier net '%s': fixed inversion.\n", net->name.c_str(ctx));
278 }
279 }
280
281 void write_bitstream()
282 {
283 ChipConfig cc;
284 cc.chip_name = device;
285 std::vector<std::array<int, 9>> bank(uarch->dies);
286 for (auto &cell : ctx->cells) {
287 CfgLoc loc = get_config_loc(cell.second.get()->bel.tile);
288 auto &params = cell.second.get()->params;
289 switch (cell.second->type.index) {
290 case id_CPE_IBUF.index:
291 case id_CPE_TOBUF.index:
292 case id_CPE_OBUF.index:
293 case id_CPE_IOBUF.index:
294 case id_CPE_LVDS_IBUF.index:
295 case id_CPE_LVDS_TOBUF.index:
296 case id_CPE_LVDS_OBUF.index:
297 case id_CPE_LVDS_IOBUF.index:
298 break;
299 case id_IOSEL.index:
300 for (auto &p : params) {
301 bank[loc.die][ctx->get_bel_package_pin(cell.second.get()->bel)->pad_bank] = 1;
302 cc.tiles[loc].add_word(stringf("GPIO.%s", p.first.c_str(ctx)), p.second.as_bits(),
303 cell.second->name.c_str(ctx));
304 }
305 break;
306 case id_CPE_CPLINES.index:
307 case id_CPE_COMP.index:
308 case id_CPE_L2T4.index:
309 case id_CPE_ADDF.index:
310 case id_CPE_ADDF2.index:
311 case id_CPE_MULT.index:
312 case id_CPE_MX4.index:
313 case id_CPE_CONCAT.index:
314 case id_CPE_FF.index:
315 case id_CPE_LATCH.index:
316 case id_CPE_RAMI.index:
317 case id_CPE_RAMO.index:
318 case id_CPE_BRIDGE.index:
319 case id_CPE_RAMIO.index: {
320 // Update configuration bits based on signal inversion
321 dict<IdString, Property> params = cell.second->params;
322 Loc l = ctx->getBelLocation(cell.second->bel);
323 params.erase(id_L2T4_UPPER);
324 params.erase(id_MULT_INVERT);
325 int c_i1 = int_or_default(params, id_C_I1, 0);
326 int c_i2 = int_or_default(params, id_C_I2, 0);
327 int c_i3 = int_or_default(params, id_C_I3, 0);
328 int c_i4 = int_or_default(params, id_C_I4, 0);
329 if (cell.second->type.in(id_CPE_L2T4, id_CPE_LT_L, id_CPE_LT_U)) {
330 if (l.z == CPE_LT_U_Z) {
331 update_cpe_lt(cell.second.get(), id_IN1, id_INIT_L00, params, true);
332 update_cpe_lt(cell.second.get(), c_i1 ? id_PINY1 : id_IN2, id_INIT_L00, params, false);
333 update_cpe_lt(cell.second.get(), id_IN3, id_INIT_L01, params, true);
334 update_cpe_lt(cell.second.get(), c_i2 ? id_CINX : id_IN4, id_INIT_L01, params, false);
335 } else {
336 // These will be renamed later
337 update_cpe_lt(cell.second.get(), id_IN1, id_INIT_L00, params, true);
338 update_cpe_lt(cell.second.get(), c_i3 ? id_PINY1 : id_IN2, id_INIT_L00, params, false);
339 update_cpe_lt(cell.second.get(), id_IN3, id_INIT_L01, params, true);
340 update_cpe_lt(cell.second.get(), c_i4 ? id_PINX : id_IN4, id_INIT_L01, params, false);
341 }
342 }
343 if (l.z == CPE_LT_FULL_Z) {
344 if (!cell.second->type.in(id_CPE_MULT)) {
345 if (cell.second->type.in(id_CPE_MX4)) {
346 if (int_or_default(params, id_INIT_L20, 0) == LUT_D0) {
347 update_cpe_lt(cell.second.get(), id_IN1, id_INIT_L00, params, true);
348 update_cpe_lt(cell.second.get(), c_i1 ? id_PINY1 : id_IN2, id_INIT_L00, params, false);
349 update_cpe_lt(cell.second.get(), id_IN3, id_INIT_L01, params, true);
350 update_cpe_lt(cell.second.get(), c_i2 ? id_CINX : id_IN4, id_INIT_L01, params, false);
351 update_cpe_mux(cell.second.get(), id_IN5, id_INIT_L10, 0, params);
352 update_cpe_mux(cell.second.get(), id_IN6, id_INIT_L10, 1, params);
353 update_cpe_mux(cell.second.get(), id_IN7, id_INIT_L10, 2, params);
354 update_cpe_mux(cell.second.get(), id_IN8, id_INIT_L10, 3, params);
355 } else {
356 update_cpe_mux(cell.second.get(), id_IN1, id_INIT_L11, 0, params);
357 update_cpe_mux(cell.second.get(), id_IN2, id_INIT_L11, 1, params);
358 update_cpe_mux(cell.second.get(), id_IN3, id_INIT_L11, 2, params);
359 update_cpe_mux(cell.second.get(), id_IN4, id_INIT_L11, 3, params);
360 update_cpe_lt(cell.second.get(), id_IN5, id_INIT_L02, params, true);
361 update_cpe_lt(cell.second.get(), c_i3 ? id_PINY1 : id_IN6, id_INIT_L02, params, false);
362 update_cpe_lt(cell.second.get(), id_IN7, id_INIT_L03, params, true);
363 update_cpe_lt(cell.second.get(), c_i4 ? id_PINX : id_IN8, id_INIT_L03, params, false);
364 }
365 } else {
366 update_cpe_lt(cell.second.get(), id_IN1, id_INIT_L00, params, true);
367 update_cpe_lt(cell.second.get(), c_i1 ? id_PINY1 : id_IN2, id_INIT_L00, params, false);
368 update_cpe_lt(cell.second.get(), id_IN3, id_INIT_L01, params, true);
369 update_cpe_lt(cell.second.get(), c_i2 ? id_CINX : id_IN4, id_INIT_L01, params, false);
370 update_cpe_lt(cell.second.get(), id_IN5, id_INIT_L02, params, true);
371 update_cpe_lt(cell.second.get(), c_i3 ? id_PINY1 : id_IN6, id_INIT_L02, params, false);
372 update_cpe_lt(cell.second.get(), id_IN7, id_INIT_L03, params, true);
373 update_cpe_lt(cell.second.get(), c_i4 ? id_PINX : id_IN8, id_INIT_L03, params, false);
374 }
375 }
376 }
377
378 if (cell.second->type.in(id_CPE_FF, id_CPE_LATCH)) {
379 update_cpe_inv(cell.second.get(), id_CLK, id_C_CPE_CLK, params);
380 update_cpe_inv(cell.second.get(), id_EN, id_C_CPE_EN, params);
381 bool set = int_or_default(params, id_C_EN_SR, 0) == 1;
382 if (set)
383 update_cpe_inv(cell.second.get(), id_SR, id_C_CPE_SET, params);
384 else
385 update_cpe_inv(cell.second.get(), id_SR, id_C_CPE_RES, params);
386 }
387
388 if (uarch->multiplier_a_passthru_lowers.count(cell.first) ||
389 uarch->multiplier_a_passthru_uppers.count(cell.first) ||
390 uarch->multiplier_zero_drivers.count(cell.first))
391 update_multiplier_input(cell.first, params);
392
393 int id = tile_extra_data(cell.second.get()->bel.tile)->prim_id;
394 for (auto &p : params) {
395 IdString name = p.first;
396 switch (l.z) {
397 case CPE_LT_L_Z:
398 switch (p.first.index) {
399 case id_INIT_L00.index:
400 name = id_INIT_L02;
401 break;
402 case id_INIT_L01.index:
403 name = id_INIT_L03;
404 break;
405 case id_INIT_L10.index:
406 name = id_INIT_L11;
407 break;
408 }
409 break;
410 case CPE_RAMIO_U_Z:
411 switch (p.first.index) {
412 case id_C_RAM_I.index:
413 name = id_C_RAM_I2;
414 break;
415 case id_C_RAM_O.index:
416 name = id_C_RAM_O2;
417 break;
418 }
419 break;
420 case CPE_RAMIO_L_Z:
421 switch (p.first.index) {
422 case id_C_RAM_I.index:
423 name = id_C_RAM_I1;
424 break;
425 case id_C_RAM_O.index:
426 name = id_C_RAM_O1;
427 break;
428 }
429 break;
430 }
431 cc.tiles[loc].add_word(stringf("CPE%d.%s", id, name.c_str(ctx)), p.second.as_bits(),
432 cell.second->name.c_str(ctx));
433 }
434 } break;
435 case id_CLKIN.index: {
436 for (auto &p : params) {
437 cc.configs[loc.die].add_word(stringf("CLKIN.%s", p.first.c_str(ctx)), p.second.as_bits(),
438 cell.second->name.c_str(ctx));
439 }
440 } break;
441 case id_GLBOUT.index: {
442 for (auto &p : params) {
443 cc.configs[loc.die].add_word(stringf("GLBOUT.%s", p.first.c_str(ctx)), p.second.as_bits(),
444 cell.second->name.c_str(ctx));
445 }
446 } break;
447 case id_PLL.index: {
448 Loc l = ctx->getBelLocation(cell.second->bel);
449 for (auto &p : params) {
450 cc.configs[loc.die].add_word(stringf("PLL%d.%s", l.z - 2, p.first.c_str(ctx)), p.second.as_bits(),
451 cell.second->name.c_str(ctx));
452 }
453 } break;
454 case id_RAM.index: {
455 CfgLoc loc = get_ram_config_loc(cell.second.get()->bel.tile);
456 auto &bram = cc.brams[loc];
457 for (auto &p : params) {
458 std::string name = p.first.c_str(ctx);
459 if (boost::starts_with(name, "RAM_cfg"))
460 bram.add_word(name, p.second.as_bits());
461 }
462
463 bool is_fifo = params.count(id_RAM_cfg_fifo_sync_enable) | params.count(id_RAM_cfg_fifo_async_enable);
464 if (!is_fifo) {
465 auto &bram_data = cc.bram_data[loc];
466 bram_data = std::vector<uint8_t>(5120);
467 for (int i = 0; i < 128; i++) {
468 for (int j = 0; j < 40; j++) {
469 bram_data[i * 40 + j] = extract_bits(params, ctx->idf("INIT_%02X", i), j * 8, 8);
470 }
471 }
472 }
473 } break;
474 case id_SERDES.index: {
475 auto &serdes = cc.serdes[0];
476 for (auto &p : params) {
477 serdes.add_word(p.first.c_str(ctx), p.second.as_bits());
478 }
479 } break;
480 case id_USR_RSTN.index:
481 case id_CFG_CTRL.index:
482 break;
483 default:
484 log_error("Unhandled cell %s of type %s.\n", cell.second.get()->name.c_str(ctx),
485 cell.second->type.c_str(ctx));
486 }
487 }
488
489 for (int i = 0; i < uarch->dies; i++) {
490 cc.configs[i].add_word("GPIO.BANK_N1", int_to_bitvector(bank[i][0], 1));
491 cc.configs[i].add_word("GPIO.BANK_N2", int_to_bitvector(bank[i][1], 1));
492 cc.configs[i].add_word("GPIO.BANK_E1", int_to_bitvector(bank[i][2], 1));
493 cc.configs[i].add_word("GPIO.BANK_E2", int_to_bitvector(bank[i][3], 1));
494 cc.configs[i].add_word("GPIO.BANK_W1", int_to_bitvector(bank[i][4], 1));
495 cc.configs[i].add_word("GPIO.BANK_W2", int_to_bitvector(bank[i][5], 1));
496 cc.configs[i].add_word("GPIO.BANK_S1", int_to_bitvector(bank[i][6], 1));
497 cc.configs[i].add_word("GPIO.BANK_S2", int_to_bitvector(bank[i][7], 1));
498 cc.configs[i].add_word("GPIO.BANK_CFG", int_to_bitvector(bank[i][8], 1));
499 }
500 if (uarch->dies == 2) {
501 cc.configs[0].add_word("D2D.N", int_to_bitvector(1, 1));
502 cc.configs[1].add_word("D2D.S", int_to_bitvector(1, 1));
503 }
504
505 for (auto &net : ctx->nets) {
506 NetInfo *ni = net.second.get();
507 if (ni->wires.empty())
508 continue;
509 for (auto &w : ni->wires) {
510 if (w.second.pip != PipId())
511 export_connection(cc, w.second.pip);
512 }
513 }
514 out << cc;
515 }
516 };
517
518 } // namespace
519
520 void GateMateImpl::write_bitstream(const std::string &device, const std::string &filename)
521 {
522 std::ofstream out(filename);
523 if (!out)
524 log_error("Failed to open file '%s' for writing (%s).\n", filename.c_str(), strerror(errno));
525
526 BitstreamBackend be(ctx, this, device, out);
527 be.write_bitstream();
528 }
529
530 NEXTPNR_NAMESPACE_END