Debian Codegraph @

generic/viaduct/fabulous/fabulous.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2021-22 gatecat <[email protected]>
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 "fabric_parsing.h"
21 #include "log.h"
22 #include "nextpnr.h"
23 #include "util.h"
24 #include "viaduct_api.h"
25 #include "viaduct_helpers.h"
26
27 #include <fstream>
28
29 #define GEN_INIT_CONSTIDS
30 #define VIADUCT_CONSTIDS "viaduct/fabulous/constids.inc"
31 #include "viaduct_constids.h"
32
33 #include "fab_cfg.h"
34 #include "fab_defs.h"
35 #include "fasm.h"
36 #include "pack.h"
37 #include "pcf.h"
38 #include "validity_check.h"
39
40 #include <filesystem>
41
42 NEXTPNR_NAMESPACE_BEGIN
43
44 namespace {
45 struct FabulousImpl : ViaductAPI
46 {
47 FabulousImpl(const dict<std::string, std::string> &args)
48 {
49 for (auto a : args) {
50 if (a.first == "fasm")
51 fasm_file = a.second;
52 else if (a.first == "lut_k")
53 cfg.clb.lut_k = std::stoi(a.second);
54 else if (a.first == "pcf")
55 pcf_file = a.second;
56 else
57 log_error("unrecognised fabulous option '%s'\n", a.first.c_str());
58 }
59 }
60
61 ~FabulousImpl() {};
62 void init(Context *ctx) override
63 {
64 init_uarch_constids(ctx);
65 ViaductAPI::init(ctx);
66 h.init(ctx);
67 fab_root = get_env_var("FAB_ROOT", ", set it to the fabulous build output or project path");
68 if (std::filesystem::exists(fab_root + "/.FABulous"))
69 is_new_fab = true;
70 else
71 is_new_fab = false;
72 log_info("Detected FABulous %s format project.\n", is_new_fab ? "2.0" : "1.0");
73 init_default_ctrlset_cfg();
74 // To consider: a faster serialised form of the device data (like bba that other arches use) so we don't have to
75 // go through the whole csv parsing malarkey each time
76 blk_trk = std::make_unique<BlockTracker>(ctx, cfg);
77 is_new_fab ? init_bels_v2() : init_bels_v1();
78 init_pips();
79 init_pseudo_constant_wires();
80 setup_lut_permutation();
81 ctx->setDelayScaling(3.0, 3.0);
82 ctx->delay_epsilon = 0.25;
83 ctx->ripup_penalty = 0.5;
84 }
85
86 void init_default_ctrlset_cfg()
87 {
88 // TODO: loading from file or something
89 uint64_t default_routing = (1ULL << (cfg.clb.lc_per_clb * cfg.clb.ff_per_lc)) - 1;
90 auto setup_cfg = [&](ControlSetConfig &ctrl, int mask) {
91 ctrl.routing.clear();
92 ctrl.routing.push_back(default_routing);
93 ctrl.can_mask = mask;
94 ctrl.can_invert = false;
95 };
96
97 setup_cfg(cfg.clb.clk, -1);
98 setup_cfg(cfg.clb.en, 1);
99 setup_cfg(cfg.clb.sr, 0);
100 }
101
102 void update_cell_timing(Context *ctx)
103 {
104 // These timings are not realistic. They just make sure nextpnr does some timing-driven optimisation...
105 for (auto &cell : ctx->cells) {
106 CellInfo *ci = cell.second.get();
107 if (ci->type == id_FABULOUS_LC) {
108 auto &lct = cell_tags.get(ci);
109 if (lct.comb.carry_used) {
110 ctx->addCellTimingDelay(ci->name, id_Ci, id_Co, 0.2);
111 ctx->addCellTimingDelay(ci->name, ctx->id("I1"), id_Co, 1.0);
112 ctx->addCellTimingDelay(ci->name, ctx->id("I2"), id_Co, 1.0);
113 }
114 if (lct.ff.ff_used) {
115 ctx->addCellTimingClock(ci->name, id_CLK);
116 for (unsigned i = 0; i < cfg.clb.lut_k; i++)
117 ctx->addCellTimingSetupHold(ci->name, ctx->idf("I%d", i), id_CLK, 2.5, 0.1);
118 ctx->addCellTimingClockToOut(ci->name, id_Q, id_CLK, 1.0);
119 if (bool_or_default(ci->params, id_I0MUX))
120 ctx->addCellTimingSetupHold(ci->name, id_Ci, id_CLK, 2.5, 0.1);
121 } else {
122 for (unsigned i = 0; i < cfg.clb.lut_k; i++)
123 ctx->addCellTimingDelay(ci->name, ctx->idf("I%d", i), id_O, 3.0);
124 if (bool_or_default(ci->params, id_I0MUX))
125 ctx->addCellTimingDelay(ci->name, id_Ci, id_O, 3.0);
126 }
127 } else if (ci->type == id_OutPass4_frame_config) {
128 for (unsigned i = 0; i < 4; i++)
129 ctx->addCellTimingSetupHold(ci->name, ctx->idf("I%d", i), id_CLK, 2.5, 0.1);
130 } else if (ci->type == id_InPass4_frame_config) {
131 for (unsigned i = 0; i < 4; i++)
132 ctx->addCellTimingClockToOut(ci->name, ctx->idf("O%d", i), id_CLK, 2.5);
133 }
134 }
135 }
136
137 void pack() override
138 {
139 if (!pcf_file.empty())
140 fabulous_pcf(ctx, pcf_file);
141 else
142 log_info("No PCF file specified, skipping constraints application.\n");
143 fabulous_pack(ctx, cfg);
144 }
145
146 void postRoute() override
147 {
148 if (!fasm_file.empty())
149 fabulous_write_fasm(ctx, cfg, pp_tags, fasm_file);
150 }
151
152 void prePlace() override
153 {
154 assign_cell_info();
155 update_cell_timing(ctx);
156 }
157 bool isBelLocationValid(BelId bel, bool explain_invalid) const override
158 {
159 return blk_trk->check_validity(bel, cfg, cell_tags);
160 }
161
162 private:
163 FabricConfig cfg; // TODO: non-default config
164 ViaductHelpers h;
165
166 std::string fasm_file;
167
168 std::string pcf_file;
169
170 std::unique_ptr<BlockTracker> blk_trk;
171
172 std::string get_env_var(const std::string &name, const std::string &prompt = "")
173 {
174 const char *var = getenv(name.c_str());
175 if (var == nullptr)
176 log_error("environment variable '%s' is not set%s\n", name.c_str(), prompt.c_str());
177 return std::string(var);
178 }
179
180 std::ifstream open_data_rel(const std::string &postfix)
181 {
182 const std::string filename(fab_root + postfix);
183 std::ifstream in(filename);
184 if (!in)
185 log_error("failed to open data file '%s' (is FAB_ROOT set correctly?)\n", filename.c_str());
186 return in;
187 }
188
189 std::string fab_root;
190 bool is_new_fab;
191
192 pool<IdString> warned_beltypes;
193
194 std::vector<PseudoPipTags> pp_tags;
195
196 void add_pseudo_pip(WireId src, WireId dst, IdString pip_type, float delay = 1.0,
197 PseudoPipTags tags = PseudoPipTags())
198 {
199 const auto &src_data = ctx->wire_info(src);
200 IdStringList pip_name = IdStringList::concat(ctx->getWireName(src), ctx->getWireName(dst));
201 PipId idx =
202 ctx->addPip(pip_name, pip_type, src, dst, ctx->getDelayFromNS(delay), Loc(src_data.x, src_data.y, 0));
203 if (idx.index >= int(pp_tags.size()))
204 pp_tags.resize(idx.index + 1);
205 pp_tags.at(idx.index) = tags;
206 }
207
208 void handle_bel_ports(BelId bel, IdString tile, IdString bel_type, const std::vector<parser_view> &ports)
209 {
210 // TODO: improve the scalability here as we support more bel types
211 IdString idx = ctx->getBelName(bel)[1];
212 Loc loc = ctx->getBelLocation(bel);
213 if (bel_type == id_IO_1_bidirectional_frame_config_pass) {
214 for (parser_view p : ports) {
215 IdString port_id = p.to_id(ctx);
216 WireId port_wire = get_wire(tile, port_id, ctx->idf("W_IO_%s", port_id.c_str(ctx)));
217 IdString pin = p.back(1).to_id(ctx);
218 ctx->addBelPin(bel, pin, port_wire, pin.in(id_I, id_T) ? PORT_IN : PORT_OUT);
219 }
220 } else if (bel_type.in(id_InPass4_frame_config, id_OutPass4_frame_config)) {
221 WireId clk_wire = get_wire(tile, id_CLK, id_REG_CLK);
222 if (ctx->wires.at(clk_wire.index).uphill.empty()) {
223 WireId global_clk_wire = get_wire(ctx->id("X0Y0"), id_CLK, id_CLK);
224 add_pseudo_pip(global_clk_wire, clk_wire, id_global_clock);
225 }
226 ctx->addBelInput(bel, id_CLK, clk_wire);
227 for (parser_view p : ports) {
228 IdString port_id = p.to_id(ctx);
229 WireId port_wire = get_wire(tile, port_id, port_id);
230 IdString pin = p.back(2).to_id(ctx);
231 ctx->addBelPin(bel, pin, port_wire, bel_type == id_OutPass4_frame_config ? PORT_IN : PORT_OUT);
232 }
233 } else if (bel_type == id_RegFile_32x4) {
234 WireId clk_wire = get_wire(tile, id_CLK, id_REG_CLK);
235 ctx->addBelInput(bel, id_CLK, clk_wire);
236 WireId global_clk_wire = get_wire(ctx->id("X0Y0"), id_CLK, id_CLK);
237 add_pseudo_pip(global_clk_wire, clk_wire, id_global_clock);
238 for (parser_view p : ports) {
239 IdString port_id = p.to_id(ctx);
240 // TODO: nicer way of determining port type?
241 if (p[0] == 'D') {
242 ctx->addBelInput(bel, port_id, get_wire(tile, port_id, id_WRITE_DATA));
243 } else if (p[0] == 'W') {
244 ctx->addBelInput(bel, port_id, get_wire(tile, port_id, id_WRITE_ADDRESS));
245 } else if (p[1] == 'D') {
246 ctx->addBelOutput(bel, port_id, get_wire(tile, port_id, id_READ_DATA));
247 } else {
248 ctx->addBelInput(bel, port_id, get_wire(tile, port_id, id_READ_ADDRESS));
249 }
250 }
251 } else if (bel_type == id_MULADD) {
252 // TODO: do DSPs need a clock too like regfiles?
253 for (parser_view p : ports) {
254 IdString port_id = p.to_id(ctx);
255 if (p[0] == 'Q') {
256 ctx->addBelOutput(bel, port_id, get_wire(tile, port_id, id_DSP_DATA_OUT));
257 } else if (port_id == id_clr) {
258 ctx->addBelInput(bel, port_id, get_wire(tile, port_id, id_DSP_CLR));
259 } else {
260 ctx->addBelInput(bel, port_id, get_wire(tile, port_id, id_DSP_DATA_IN));
261 }
262 }
263 } else if (bel_type == id_MUX8LUT_frame_config) {
264 for (parser_view p : ports) {
265 IdString port_id = p.to_id(ctx);
266 ctx->addBelPin(bel, port_id, get_wire(tile, port_id, ctx->idf("LUTMUX_%s", port_id.c_str(ctx))),
267 p[0] == 'M' ? PORT_OUT : PORT_IN);
268 }
269 } else if (bel_type == id_FABULOUS_LC) {
270 // TODO: split LC mode, LUT permutation pseudo-switchbox, LUT thru pseudo-pips
271 WireId clk_wire = get_wire(tile, ctx->idf("L%s_CLK", idx.c_str(ctx)), id_LUT_CLK);
272 ctx->addBelInput(bel, id_CLK, clk_wire);
273 WireId global_clk_wire = get_wire(ctx->id("X0Y0"), id_CLK, id_CLK);
274 add_pseudo_pip(global_clk_wire, clk_wire, id_global_clock);
275 blk_trk->set_bel_type(bel, BelFlags::BLOCK_CLB, BelFlags::FUNC_LC_COMB, loc.z);
276 for (parser_view p : ports) {
277 IdString port_id = p.to_id(ctx);
278 WireId port_wire = get_wire(tile, port_id, ctx->idf("LUT_%s", port_id.c_str(ctx)));
279 // TODO: more robust port name handling
280 if (p[3] == 'S' || p[3] == 'E' || p[3] == 'I') { // set/reset, enable, LUT input
281 ctx->addBelInput(bel, p.substr(3).to_id(ctx), port_wire);
282 } else if (p[3] == 'O') { // LUT otuput
283 ctx->addBelOutput(bel, p.substr(3, 1).to_id(ctx), port_wire);
284 } else if (p[3] == 'C') { // carry chain
285 if (p[4] == 'i') {
286 ctx->addBelInput(bel, id_Ci, port_wire);
287 } else {
288 NPNR_ASSERT(p[4] == 'o');
289 ctx->addBelOutput(bel, id_Co, port_wire);
290 }
291 } else {
292 log_error("don't know what to do with LC port '%s'\n", port_id.c_str(ctx));
293 }
294 }
295 } else {
296 // ...
297 if (!warned_beltypes.count(bel_type) && !ports.empty()) {
298 log_warning("don't know how to handle ports for bel type '%s'\n", bel_type.c_str(ctx));
299 warned_beltypes.insert(bel_type);
300 }
301 }
302 }
303
304 void init_global_clock()
305 {
306 // TODO: how do we extend this to more complex clocking topologies?
307 auto found = ctx->wire_by_name.find(IdStringList::concat(ctx->id("X0Y0"), id_CLK));
308 if (found != ctx->wire_by_name.end()) {
309 BelId global_clk_bel = ctx->addBel(IdStringList::concat(ctx->id("X0Y0"), id_CLK), id_Global_Clock,
310 Loc(0, 0, 0), true, false);
311 WireId global_clk_wire = get_wire(ctx->id("X0Y0"), id_CLK, id_CLK);
312 ctx->addBelOutput(global_clk_bel, id_CLK, global_clk_wire);
313 }
314 }
315
316 // TODO: this is for legacy fabulous only, the new code path can be a lot simpler
317 void init_bels_v1()
318 {
319 std::ifstream in = open_data_rel("/npnroutput/bel.txt");
320 CsvParser csv(in);
321 while (csv.fetch_next_line()) {
322 IdString tile = csv.next_field().to_id(ctx);
323 int bel_x = csv.next_field().substr(1).to_int();
324 int bel_y = csv.next_field().substr(1).to_int();
325 auto bel_idx = csv.next_field();
326 IdString bel_type = csv.next_field().to_id(ctx);
327 NPNR_ASSERT(bel_idx.size() == 1);
328 int bel_z = bel_idx[0] - 'A';
329 NPNR_ASSERT(bel_z >= 0 && bel_z < 26);
330 std::vector<parser_view> ports;
331 parser_view port;
332 while (!(port = csv.next_field()).empty()) {
333 ports.push_back(port);
334 }
335 IdString bel_name = bel_idx.to_id(ctx);
336 if (bel_type.in(id_InPass4_frame_config, id_OutPass4_frame_config)) {
337 // Assign BRAM IO a nicer name than just a letter
338 bel_name = ports.front().rsplit('_').first.to_id(ctx);
339 }
340 /*
341 In the future we will need to handle optionally splitting SLICEs into separate LUT/COMB and FF bels
342 This is the preferred approach in nextpnr for arches where the LUT and FF can be used separately of
343 each other (e.g. there is a way of routing the LUT and FF outputs individually, and some extra
344 optional FF input).
345 While this isn't yet the standard fabulous SLICE, it should be considered as a future option in fabulous.
346 */
347 Loc loc(bel_x, bel_y, bel_z);
348 BelId bel = ctx->addBel(IdStringList::concat(tile, bel_name), bel_type, loc, false, false);
349 handle_bel_ports(bel, tile, bel_type, ports);
350 }
351 init_global_clock();
352 postprocess_bels();
353 }
354
355 void init_bels_v2()
356 {
357 std::ifstream in = open_data_rel("/.FABulous/bel.v2.txt");
358 CsvParser csv(in);
359 BelId curr_bel;
360 while (csv.fetch_next_line()) {
361 IdString cmd = csv.next_field().to_id(ctx);
362 if (cmd == id_BelBegin) {
363 IdString tile = csv.next_field().to_id(ctx);
364 auto bel_idx = csv.next_field();
365 IdString bel_type = csv.next_field().to_id(ctx);
366 NPNR_ASSERT(bel_idx.size() == 1);
367 int bel_z = bel_idx[0] - 'A';
368 NPNR_ASSERT(bel_z >= 0 && bel_z < 26);
369 IdString bel_name = bel_idx.to_id(ctx);
370 if (bel_type.in(id_InPass4_frame_config, id_OutPass4_frame_config, id_InPass4_frame_config_mux,
371 id_OutPass4_frame_config_mux)) {
372 // Assign BRAM IO a nicer name than just a letter
373 auto prefix = csv.next_field();
374 if (prefix.empty()) {
375 log_error("Bel definition missing field; please update FABulous!\n");
376 }
377 bel_name = prefix.rsplit('_').first.to_id(ctx);
378 }
379 Loc loc = tile_loc(tile);
380 curr_bel = ctx->addBel(IdStringList::concat(tile, bel_name), bel_type, Loc(loc.x, loc.y, bel_z), false,
381 false);
382 } else if (cmd.in(id_I, id_O)) {
383 IdString port = csv.next_field().to_id(ctx);
384 auto wire_name = csv.next_field().split('.');
385 WireId wire =
386 get_wire(wire_name.first.to_id(ctx), wire_name.second.to_id(ctx), wire_name.second.to_id(ctx));
387 ctx->addBelPin(curr_bel, port, wire, cmd == id_O ? PORT_OUT : PORT_IN);
388 } else if (cmd == id_GlobalClk) {
389 IdStringList bel_name = ctx->getBelName(curr_bel);
390 WireId clk_wire = get_wire(bel_name[0], ctx->idf("%s_CLK", bel_name[1].c_str(ctx)), id_REG_CLK);
391 ctx->addBelInput(curr_bel, id_CLK, clk_wire);
392 WireId global_clk_wire = get_wire(ctx->id("X0Y0"), id_CLK, id_CLK);
393 add_pseudo_pip(global_clk_wire, clk_wire, id_global_clock);
394 } else if (cmd == id_CFG) {
395 // TODO...
396 } else if (cmd == id_BelEnd) {
397 curr_bel = BelId();
398 } else if (cmd != IdString()) {
399 log_error("unsupported command %s in definition of bel %s\n", cmd.c_str(ctx),
400 curr_bel == BelId() ? "<none>" : ctx->nameOfBel(curr_bel));
401 }
402 }
403 init_global_clock();
404 postprocess_bels();
405 }
406
407 void generate_split_mux8(BelId bel)
408 {
409 // _don't_ take a reference here because it might be invalidated by adding bels
410 auto data = ctx->bel_info(bel);
411 const std::array<IdString, 4> mux_outs{id_M_AB, id_M_AD, id_M_EF, id_M_AH};
412 for (unsigned k = 1; k <= 3; k++) {
413 // create MUX2 through 8
414 unsigned m = (1U << k);
415 for (unsigned i = 0; i < 8; i += m) {
416 // mux indexing scheme
417 // - MUX2s are at (z % 2) == 0
418 // - MUX4s are at (z % 4) == 1
419 // - MUX8s are at (z % 8) == 7
420 int idx = (m == 2) ? i : (m == 4) ? (i + 1) : (i + 7);
421 BelId mux =
422 ctx->addBel(IdStringList::concat(data.name[0], ctx->idf("MUX%d_%d", m, i)),
423 ctx->idf("FABULOUS_MUX%d", m), Loc(data.x, data.y, data.z + 1 + idx), false, false);
424 blk_trk->set_bel_type(mux, BelFlags::BLOCK_CLB, BelFlags::FUNC_MUX, idx);
425 // M data inputs
426 for (unsigned j = 0; j < m; j++) {
427 ctx->addBelInput(mux, ctx->idf("I%d", j), data.pins.at(ctx->idf("%c", char('A' + i + j))).wire);
428 }
429 // K select inputs
430 for (unsigned j = 0; j < k; j++) {
431 ctx->addBelInput(mux, ctx->idf("S%d", j),
432 data.pins.at(ctx->idf("S%d", (m == 8 && j == 2) ? 3 : ((i / m) * k + j))).wire);
433 }
434 // Output
435 IdString output = (m == 2) ? mux_outs.at(i / m)
436 : (m == 4) ? mux_outs.at((i / m) * k + 1)
437 : mux_outs.at(3);
438 ctx->addBelOutput(mux, id_O, data.pins.at(output).wire);
439 }
440 }
441 }
442
443 void postprocess_bels()
444 {
445 // This does some post-processing on bels to make them useful for nextpnr place-and-route regardless of the code
446 // path that creates them. In the future, splitting muxes and creating split LCs would be done here, too
447 for (auto bel : ctx->getBels()) {
448 // _don't_ take a reference here because it might be invalidated by adding bels
449 auto data = ctx->bel_info(bel);
450 if (data.type == id_FABULOUS_LC) {
451 if (!data.pins.count(id_Q)) {
452 // Add a Q pseudo-pin and pseudo-pip from Q to O
453 WireId o_wire = ctx->getBelPinWire(bel, id_O);
454 IdString q_name = ctx->idf("%s_Q", data.name[1].c_str(ctx));
455 WireId q_wire = get_wire(data.name[0], q_name, q_name);
456 ctx->addBelOutput(bel, id_Q, q_wire);
457 // Pseudo-pip for FF mode
458 add_pseudo_pip(q_wire, o_wire, id_O2Q);
459 }
460 } else if (data.type.in(id_MUX8LUT_frame_config, id_MUX8LUT_frame_config_mux)) {
461 generate_split_mux8(bel);
462 ctx->bel_info(bel).hidden = true;
463 } else if (data.type == id_IO_1_bidirectional_frame_config_pass) {
464 if (!data.pins.count(id_PAD)) {
465 // Add a PAD pseudo-pin for the top level
466 ctx->addBelInout(bel, id_PAD,
467 get_wire(data.name[0], ctx->idf("PAD_%s", data.name[1].c_str(ctx)), id_PAD));
468 }
469 }
470 }
471 }
472
473 int max_x = 0, max_y = 0;
474 void init_pips()
475 {
476 std::ifstream in = open_data_rel(is_new_fab ? "/.FABulous/pips.txt" : "/npnroutput/pips.txt");
477 CsvParser csv(in);
478 while (csv.fetch_next_line()) {
479 IdString src_tile = csv.next_field().to_id(ctx);
480 IdString src_port = csv.next_field().to_id(ctx);
481 IdString dst_tile = csv.next_field().to_id(ctx);
482 IdString dst_port = csv.next_field().to_id(ctx);
483 int delay = csv.next_field().to_int();
484 IdString pip_name = csv.next_field().to_id(ctx);
485 WireId src_wire = get_wire(src_tile, src_port, src_port);
486 WireId dst_wire = get_wire(dst_tile, dst_port, dst_port);
487 Loc loc = tile_loc(src_tile);
488 max_x = std::max(loc.x, max_x);
489 max_y = std::max(loc.y, max_y);
490 ctx->addPip(IdStringList::concat(src_tile, pip_name), pip_name, src_wire, dst_wire,
491 ctx->getDelayFromNS(0.05 * delay), loc);
492 }
493 }
494
495 void remove_bel_pin(BelId bel, IdString pin)
496 {
497 auto &bel_data = ctx->bel_info(bel);
498 auto &wire_data = ctx->wire_info(ctx->getBelPinWire(bel, pin));
499 std::vector<BelPin> new_wire_pins;
500 for (const auto &wire_pin : wire_data.bel_pins) {
501 if (wire_pin.bel == bel && wire_pin.pin == pin)
502 continue;
503 new_wire_pins.push_back(wire_pin);
504 }
505 wire_data.bel_pins = new_wire_pins;
506 bel_data.pins.erase(pin);
507 }
508
509 void setup_lut_permutation()
510 {
511 for (auto bel : ctx->getBels()) {
512 auto &bel_data = ctx->bel_info(bel);
513 if (!bel_data.type.in(id_FABULOUS_LC, id_FABULOUS_COMB))
514 continue;
515 std::vector<WireId> orig_inputs, new_inputs;
516 for (unsigned i = 0; i < cfg.clb.lut_k; i++) {
517 // Rewire the LUT input to a permutation pseudo-wire
518 IdString pin = ctx->idf("I%d", i);
519 orig_inputs.push_back(ctx->getBelPinWire(bel, pin));
520 remove_bel_pin(bel, pin);
521 WireId in_wire = get_wire(bel_data.name[0], ctx->idf("%s_PERM_I%d", bel_data.name[1].c_str(ctx), i),
522 id__LUT_PERM_IN);
523 ctx->addBelInput(bel, pin, in_wire);
524 new_inputs.push_back(in_wire);
525 }
526 for (unsigned i = 0; i < cfg.clb.lut_k; i++) {
527 for (unsigned j = 0; j < cfg.clb.lut_k; j++) {
528 add_pseudo_pip(orig_inputs.at(i), new_inputs.at(j), id__LUT_PERM, 0.1,
529 PseudoPipTags(PseudoPipTags::LUT_PERM, bel, ((i << 4) | j)));
530 }
531 }
532 }
533 }
534
535 // Fast lookup of tile names to XY pairs
536 dict<IdString, Loc> tile2loc;
537 Loc tile_loc(IdString tile)
538 {
539 if (!tile2loc.count(tile)) {
540 std::string tile_name = tile.str(ctx);
541 parser_view view(tile_name);
542 NPNR_ASSERT(view[0] == 'X');
543 size_t ypos = view.find('Y');
544 NPNR_ASSERT(ypos != parser_view::npos);
545 int x = view.substr(1, ypos - 1).to_int();
546 int y = view.substr(ypos + 1).to_int();
547 tile2loc[tile] = Loc(x, y, 0);
548 }
549 return tile2loc.at(tile);
550 }
551
552 // Create a wire if it doesn't exist, otherwise just return it
553 WireId get_wire(IdString tile, IdString wire, IdString type)
554 {
555 // Create a wire name by using the built-in IdStringList mechanism to store a (tile, wire) pair
556 // this way we don't store a full string in memory of every concatenated wire name, reducing the memory
557 // footprint and start time significantly beyond the ~1k LUT scale
558 auto wire_name = IdStringList::concat(tile, wire);
559 auto found = ctx->wire_by_name.find(wire_name);
560 if (found != ctx->wire_by_name.end())
561 return found->second;
562 // doesn't exist
563 Loc loc = tile_loc(tile);
564 return ctx->addWire(wire_name, type, loc.x, loc.y);
565 }
566
567 void init_pseudo_constant_wires()
568 {
569 for (int y = 0; y <= max_y; y++) {
570 for (int x = 0; x <= max_x; x++) {
571 for (int c = 0; c <= 1; c++) {
572 IdString name = ctx->idf("$CONST%d", c);
573 IdString tile = ctx->idf("X%dY%d", x, y);
574 WireId const_wire = get_wire(tile, name, name);
575 // Driver bel; always at 0;0
576 if (x == 0 && y == 0) {
577 int z = 0;
578 while (ctx->bel_by_loc.count(Loc(x, y, z)))
579 z++;
580 BelId const_driver = ctx->addBel(IdStringList::concat(tile, ctx->idf("_CONST%d_DRV", c)),
581 ctx->idf("_CONST%d_DRV", c), Loc(x, y, z), true, true);
582 ctx->addBelInput(const_driver, id_O, const_wire);
583 }
584 if (x > 0) {
585 // 'right' pip
586 WireId prev_wire = get_wire(ctx->idf("X%dY%d", 0, y), name, name);
587 add_pseudo_pip(prev_wire, const_wire, name, 0.1);
588 }
589 if (y > 0) {
590 // 'down' pip
591 WireId prev_wire = get_wire(ctx->idf("X%dY%d", x, 0), name, name);
592 add_pseudo_pip(prev_wire, const_wire, name, 0.1);
593 }
594 }
595 }
596 }
597 // LUTs can act as constant drivers if they aren't used.
598 // To avoid an exorbitant number of pips, only do this for the first LUT in a tile
599 // This pip will only be enabled if the LUT isn't used
600 for (BelId bel : ctx->getBels()) {
601 if (!ctx->getBelType(bel).in(id_FABULOUS_LC, id_FABULOUS_COMB))
602 continue;
603 Loc loc = ctx->getBelLocation(bel);
604 WireId o = ctx->getBelPinWire(bel, id_O);
605 for (int c = 0; c <= 1; c++) {
606 IdString const_name = ctx->idf("$CONST%d", c);
607 WireId const_wire = get_wire(ctx->idf("X%dY%d", loc.x, loc.y), const_name, const_name);
608 add_pseudo_pip(const_wire, o, const_name, 0.1, PseudoPipTags(PseudoPipTags::LUT_CONST, bel, c));
609 }
610 }
611 // We can also have dedicated constant wires in the fabric
612 for (WireId wire : ctx->getWires()) {
613 auto &wire_data = ctx->wire_info(wire);
614 IdString name_suffix = wire_data.name[1];
615 int const_val = -1;
616 if (name_suffix.in(id_GND, id_GND0))
617 const_val = 0;
618 else if (name_suffix.in(id_VCC, id_VCC0, id_VDD, id_VDD0))
619 const_val = 1;
620 else
621 continue;
622 IdString const_name = ctx->idf("$CONST%d", const_val);
623 WireId const_wire = get_wire(ctx->idf("X%dY%d", wire_data.x, wire_data.y), const_name, const_name);
624 add_pseudo_pip(const_wire, wire, const_name, 0.1);
625 }
626 }
627
628 CellTagger cell_tags;
629 void assign_cell_info()
630 {
631 for (auto &cell : ctx->cells) {
632 cell_tags.assign_for(ctx, cfg, cell.second.get());
633 }
634 }
635 void notifyBelChange(BelId bel, CellInfo *cell)
636 {
637 CellInfo *old = ctx->getBoundBelCell(bel);
638 blk_trk->update_bel(bel, old, cell);
639 }
640
641 bool checkPipAvail(PipId pip) const override
642 {
643 if (pip.index >= int(pp_tags.size()))
644 return true;
645 const auto &tags = pp_tags.at(pip.index);
646 if (tags.type == PseudoPipTags::LUT_CONST) {
647 return ctx->checkBelAvail(tags.bel);
648 } else if (tags.type == PseudoPipTags::LUT_PERM) {
649 uint8_t from = (tags.data >> 4) & 0xF, to = (tags.data & 0xF);
650 if (from == to)
651 return true;
652 const CellInfo *lut = ctx->getBoundBelCell(tags.bel);
653 if (!lut)
654 return true;
655 bool is_carry = cell_tags.get(lut).comb.carry_used;
656 if (is_carry) {
657 // Because you have to make sure you route _something_ to each HA input in this mode (undefined I1/I2
658 // inputs aren't OK) and you also can't swap I0 because it's fixed internally LUT permutation in carry
659 // mode is just more trouble than it's worth.
660 return false;
661 } else {
662 return true; // TODO: other cases where perm illegal; e.g. LUTRAM
663 }
664 } else {
665 // TODO: LUT permuation pseudopips
666 return true;
667 }
668 }
669
670 delay_t predictDelay(BelId src_bel, IdString src_pin, BelId dst_bel, IdString dst_pin) const override
671 {
672 if (src_pin == id_Co && dst_pin == id_Ci)
673 return 0.5;
674
675 auto driver_loc = ctx->getBelLocation(src_bel);
676 auto sink_loc = ctx->getBelLocation(dst_bel);
677
678 int dx = abs(sink_loc.x - driver_loc.x);
679 int dy = abs(sink_loc.y - driver_loc.y);
680 return (dx + dy) * ctx->args.delayScale + ctx->args.delayOffset;
681 }
682 };
683
684 struct FabulousArch : ViaductArch
685 {
686 FabulousArch() : ViaductArch("fabulous") {};
687 std::unique_ptr<ViaductAPI> create(const dict<std::string, std::string> &args)
688 {
689 return std::make_unique<FabulousImpl>(args);
690 }
691 } fabulousArch;
692 } // namespace
693
694 NEXTPNR_NAMESPACE_END