Debian Codegraph @

himbaechel/uarch/xilinx/fasm.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2019-2023 gatecat <[email protected]>
5 * Copyright (C) 2023 Hans Baier <[email protected]>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 #include <boost/algorithm/string.hpp>
22 #include <boost/range/adaptor/reversed.hpp>
23
24 #include <fstream>
25 #include <regex>
26
27 #include "extra_data.h"
28 #include "himbaechel_api.h"
29 #include "log.h"
30 #include "nextpnr.h"
31 #include "pins.h"
32 #include "util.h"
33
34 #include "xilinx.h"
35
36 #define HIMBAECHEL_CONSTIDS "uarch/xilinx/constids.inc"
37 #include "himbaechel_constids.h"
38
39 NEXTPNR_NAMESPACE_BEGIN
40
41 namespace Xc7MMCM {
42 extern const uint16_t filter_lookup_low[];
43 extern const uint16_t filter_lookup_low_ss[];
44 extern const uint16_t filter_lookup_high[];
45 extern const uint16_t filter_lookup_optimized[];
46 extern const int64_t lk_table[];
47 }; // namespace Xc7MMCM
48
49 namespace {
50 struct FasmBackend
51 {
52 Context *ctx;
53 XilinxImpl *uarch;
54 std::ostream &out;
55 std::vector<std::string> fasm_ctx;
56 dict<int, std::vector<PipId>> pips_by_tile;
57
58 dict<IdString, pool<IdString>> invertible_pins;
59
60 FasmBackend(Context *ctx, XilinxImpl *uarch, std::ostream &out) : ctx(ctx), uarch(uarch), out(out) {};
61
62 void push(const std::string &x) { fasm_ctx.push_back(x); }
63
64 void pop() { fasm_ctx.pop_back(); }
65
66 void pop(int N)
67 {
68 for (int i = 0; i < N; i++)
69 fasm_ctx.pop_back();
70 }
71 bool last_was_blank = true;
72 void blank()
73 {
74 if (!last_was_blank)
75 out << std::endl;
76 last_was_blank = true;
77 }
78
79 void write_prefix()
80 {
81 for (auto &x : fasm_ctx)
82 out << x << ".";
83 last_was_blank = false;
84 }
85
86 void write_bit(const std::string &name, bool value = true)
87 {
88 if (value) {
89 write_prefix();
90 out << name << std::endl;
91 }
92 }
93
94 void write_vector(const std::string &name, const std::vector<bool> &value, bool invert = false)
95 {
96 write_prefix();
97 out << name << " = " << int(value.size()) << "'b";
98 for (auto bit : boost::adaptors::reverse(value))
99 out << ((bit ^ invert) ? '1' : '0');
100 out << std::endl;
101 }
102
103 void write_int_vector(const std::string &name, uint64_t value, int width, bool invert = false)
104 {
105 std::vector<bool> bits(width, false);
106 for (int i = 0; i < width; i++)
107 bits[i] = (value & (1ULL << i)) != 0;
108 write_vector(name, bits, invert);
109 }
110
111 struct PseudoPipKey
112 {
113 IdString tileType;
114 IdString dest;
115 IdString source;
116
117 bool operator==(const PseudoPipKey &b) const
118 {
119 return std::tie(this->tileType, this->dest, this->source) == std::tie(b.tileType, b.dest, b.source);
120 }
121
122 unsigned int hash() const { return mkhash(mkhash(tileType.hash(), source.hash()), dest.hash()); }
123 };
124
125 dict<PseudoPipKey, std::vector<std::string>> pp_config;
126 void get_pseudo_pip_data()
127 {
128 /*
129 * Create the mapping from pseudo pip tile type, dest wire, and source wire, to
130 * the config bits set when that pseudo pip is used
131 */
132 for (std::string s : {"L", "R"})
133 for (std::string s2 : {"", "_TBYTESRC", "_TBYTETERM", "_SING"})
134 for (std::string i :
135 (s2 == "_SING") ? std::vector<std::string>{"", "0", "1"} : std::vector<std::string>{"0", "1"}) {
136 pp_config[{ctx->id(s + "IOI3" + s2), ctx->id(s + "IOI_OLOGIC" + i + "_OQ"),
137 ctx->id("IOI_OLOGIC" + i + "_D1")}] = {"OLOGIC_Y" + i + ".OMUX.D1",
138 "OLOGIC_Y" + i + ".OQUSED",
139 "OLOGIC_Y" + i + ".OSERDES.DATA_RATE_TQ.BUF"};
140 pp_config[{ctx->id(s + "IOI3" + s2), ctx->id("IOI_ILOGIC" + i + "_O"),
141 ctx->id(s + "IOI_ILOGIC" + i + "_D")}] = {"IDELAY_Y" + i + ".IDELAY_TYPE_FIXED",
142 "ILOGIC_Y" + i + ".ZINV_D"};
143 pp_config[{ctx->id(s + "IOI3" + s2), ctx->id("IOI_ILOGIC" + i + "_O"),
144 ctx->id(s + "IOI_ILOGIC" + i + "_DDLY")}] = {"ILOGIC_Y" + i + ".IDELMUXE3.P0",
145 "ILOGIC_Y" + i + ".ZINV_D"};
146 pp_config[{ctx->id(s + "IOI3" + s2), ctx->id(s + "IOI_OLOGIC" + i + "_TQ"),
147 ctx->id("IOI_OLOGIC" + i + "_T1")}] = {"OLOGIC_Y" + i + ".ZINV_T1"};
148 if (i == "0") {
149 pp_config[{ctx->id(s + "IOB33" + s2), id_IOB_O_IN1, id_IOB_O_OUT0}] = {};
150 pp_config[{ctx->id(s + "IOB33" + s2), id_IOB_O_OUT0, id_IOB_O0}] = {};
151 pp_config[{ctx->id(s + "IOB33" + s2), id_IOB_T_IN1, id_IOB_T_OUT0}] = {};
152 pp_config[{ctx->id(s + "IOB33" + s2), id_IOB_T_OUT0, id_IOB_T0}] = {};
153 pp_config[{ctx->id(s + "IOB33" + s2), id_IOB_DIFFI_IN0, id_IOB_PADOUT1}] = {};
154 }
155 }
156
157 for (std::string s2 : {"", "_TBYTESRC", "_TBYTETERM", "_SING"})
158 for (std::string i : (s2 == "_SING") ? std::vector<std::string>{"0"} : std::vector<std::string>{"0", "1"}) {
159 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_OLOGIC" + i + "_OQ"),
160 ctx->id("IOI_OLOGIC" + i + "_D1")}] = {"OLOGIC_Y" + i + ".OMUX.D1",
161 "OLOGIC_Y" + i + ".OQUSED",
162 "OLOGIC_Y" + i + ".OSERDES.DATA_RATE_TQ.BUF"};
163 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_OLOGIC" + i + "_OFB"),
164 ctx->id("RIOI_OLOGIC" + i + "_OQ")}] = {};
165 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_O" + i), ctx->id("RIOI_ODELAY" + i + "_DATAOUT")}] = {};
166 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_OLOGIC" + i + "_OFB"),
167 ctx->id("IOI_OLOGIC" + i + "_D1")}] = {"OLOGIC_Y" + i + ".OMUX.D1",
168 "OLOGIC_Y" + i + ".OSERDES.DATA_RATE_TQ.BUF"};
169 pp_config[{ctx->id("RIOI" + s2), ctx->id("IOI_ILOGIC" + i + "_O"), ctx->id("RIOI_ILOGIC" + i + "_D")}] =
170 {"ILOGIC_Y" + i + ".ZINV_D"};
171 pp_config[{ctx->id("RIOI" + s2), ctx->id("IOI_ILOGIC" + i + "_O"),
172 ctx->id("RIOI_ILOGIC" + i + "_DDLY")}] = {"ILOGIC_Y" + i + ".IDELMUXE3.P0",
173 "ILOGIC_Y" + i + ".ZINV_D"};
174 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_OLOGIC" + i + "_TQ"),
175 ctx->id("IOI_OLOGIC" + i + "_T1")}] = {"OLOGIC_Y" + i + ".ZINV_T1"};
176 pp_config[{ctx->id("RIOI" + s2), ctx->id("RIOI_OLOGIC" + i + "_OFB"),
177 ctx->id("RIOI_ODELAY" + i + "_ODATAIN")}] = {"OLOGIC_Y" + i + ".ZINV_ODATAIN"};
178 if (i == "0") {
179 pp_config[{ctx->id("RIOB18" + s2), id_IOB_O_IN1, id_IOB_O_OUT0}] = {};
180 pp_config[{ctx->id("RIOB18" + s2), id_IOB_O_OUT0, id_IOB_O0}] = {};
181 pp_config[{ctx->id("RIOB18" + s2), id_IOB_T_IN1, id_IOB_T_OUT0}] = {};
182 pp_config[{ctx->id("RIOB18" + s2), id_IOB_T_OUT0, id_IOB_T0}] = {};
183 pp_config[{ctx->id("RIOB18" + s2), id_IOB_DIFFI_IN0, id_IOB_PADOUT1}] = {};
184 }
185 }
186
187 for (std::string s1 : {"TOP", "BOT"}) {
188 for (std::string s2 : {"L", "R"}) {
189 for (int i = 0; i < 12; i++) {
190 std::string ii = std::to_string(i);
191 std::string hck = s2 + ii;
192 std::string buf = std::string((s2 == "R") ? "X1Y" : "X0Y") + ii;
193 pp_config[{ctx->id("CLK_HROW_" + s1 + "_R"), ctx->id("CLK_HROW_CK_HCLK_OUT_" + hck),
194 ctx->id("CLK_HROW_CK_MUX_OUT_" + hck)}] = {"BUFHCE.BUFHCE_" + buf + ".IN_USE",
195 "BUFHCE.BUFHCE_" + buf + ".ZINV_CE"};
196 }
197 }
198
199 for (int i = 0; i < 16; i++) {
200 std::string ii = std::to_string(i);
201 pp_config[{ctx->id("CLK_BUFG_" + s1 + "_R"), ctx->id("CLK_BUFG_BUFGCTRL" + ii + "_O"),
202 ctx->id("CLK_BUFG_BUFGCTRL" + ii + "_I0")}] = {
203 "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".IN_USE", "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".IS_IGNORE1_INVERTED",
204 "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".ZINV_CE0", "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".ZINV_S0"};
205 pp_config[{ctx->id("CLK_BUFG_" + s1 + "_R"), ctx->id("CLK_BUFG_BUFGCTRL" + ii + "_O"),
206 ctx->id("CLK_BUFG_BUFGCTRL" + ii + "_I1")}] = {
207 "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".IN_USE", "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".IS_IGNORE0_INVERTED",
208 "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".ZINV_CE1", "BUFGCTRL.BUFGCTRL_X0Y" + ii + ".ZINV_S1"};
209 }
210 }
211
212 int rclk_y_to_i[4] = {2, 3, 0, 1};
213 for (int y = 0; y < 4; y++) {
214 std::string yy = std::to_string(y);
215 std::string ii = std::to_string(rclk_y_to_i[y]);
216 pp_config[{id_HCLK_IOI3, ctx->id("HCLK_IOI_RCLK_OUT" + ii), ctx->id("HCLK_IOI_RCLK_BEFORE_DIV" + ii)}] = {
217 "BUFR_Y" + yy + ".IN_USE", "BUFR_Y" + yy + ".BUFR_DIVIDE.BYPASS"};
218 pp_config[{id_HCLK_IOI, ctx->id("HCLK_IOI_RCLK_OUT" + ii), ctx->id("HCLK_IOI_RCLK_BEFORE_DIV" + ii)}] = {
219 "BUFR_Y" + yy + ".IN_USE", "BUFR_Y" + yy + ".BUFR_DIVIDE.BYPASS"};
220 }
221
222 // FIXME: shouldn't these be in the X-RAY ppips database?
223 for (char c : {'L', 'R'}) {
224 for (int i = 0; i < 24; i++) {
225 pp_config[{ctx->idf("INT_INTERFACE_%c", c), ctx->idf("INT_INTERFACE_LOGIC_OUTS_%c%d", c, i),
226 ctx->idf("INT_INTERFACE_LOGIC_OUTS_%c_B%d", c, i)}];
227 }
228 }
229 }
230
231 void write_pip(PipId pip, NetInfo *net)
232 {
233
234 pips_by_tile[pip.tile].push_back(pip);
235
236 auto dst_intent = ctx->getWireType(ctx->getPipDstWire(pip));
237 if (dst_intent == id_PSEUDO_GND || dst_intent == id_PSEUDO_VCC)
238 return;
239
240 auto &pd = chip_pip_info(ctx->chip_info, pip);
241 const auto &extra_data = *reinterpret_cast<const XlnxPipExtraDataPOD *>(pd.extra_data.get());
242 unsigned pip_type = pd.flags;
243
244 if (pip_type != PIP_TILE_ROUTING && pip_type != PIP_SITE_INTERNAL)
245 return;
246
247 IdString src = IdString(chip_tile_info(ctx->chip_info, pip.tile).wires[pd.src_wire].name);
248 IdString dst = IdString(chip_tile_info(ctx->chip_info, pip.tile).wires[pd.dst_wire].name);
249
250 // handle certain site internal pips:
251 // this is necessary, because in tristate outputs, the
252 // ZINV_T1 bit needs to be set, because in the OLOGIC tiles the
253 // tristate control signals are inverted if this bit is not set
254 // this only applies to router1, because router2 does not generate
255 // site internal pips here.
256 if (pip_type == PIP_SITE_INTERNAL) {
257 if (src.str(ctx) == "T1" && dst.str(ctx) == "T1INV_OUT") {
258 auto srcwire_uphill_iter = ctx->getPipsUphill(ctx->getPipSrcWire(pip));
259 auto uphill = srcwire_uphill_iter.begin();
260 if (uphill != srcwire_uphill_iter.end()) {
261 // source wire should be like: LIOI3_X0Y73/IOI_OLOGIC1_T1
262 auto loc = ctx->getWireName(ctx->getPipSrcWire(*uphill)).str(ctx);
263 boost::replace_all(loc, "/", ".");
264 boost::erase_all(loc, "_T1");
265 boost::replace_all(loc, "IOI_OLOGIC", "OLOGIC_Y");
266 // the replacements transformed it into : LIOI3_X0Y73.OLOGIC_Y1
267 out << loc << "."
268 << "ZINV_T1" << std::endl;
269 }
270 }
271 return;
272 }
273
274 // handle tile routing pips
275 IdString tile_type = IdString(chip_tile_info(ctx->chip_info, pip.tile).type_name);
276 PseudoPipKey ppk{tile_type, dst, src};
277
278 if (pp_config.count(ppk)) {
279 auto &pp = pp_config.at(ppk);
280 std::string tile_name = uarch->tile_name(pip.tile);
281 for (auto c : pp) {
282 if (boost::starts_with(tile_name, "RIOI3_SING") || boost::starts_with(tile_name, "LIOI3_SING") ||
283 boost::starts_with(tile_name, "RIOI_SING")) {
284 // Need to flip for top HCLK
285 bool is_top_sing = pip.tile < uarch->hclk_for_ioi(pip.tile);
286 if (is_top_sing) {
287 auto y0pos = c.find("Y0");
288 if (y0pos != std::string::npos)
289 c.replace(y0pos, 2, "Y1");
290 }
291 }
292 out << tile_name << "." << c << std::endl;
293 }
294 if (!pp.empty())
295 last_was_blank = false;
296 } else {
297 if (extra_data.pip_config == 1)
298 log_warning("Unprocessed route-thru %s.%s.%s\n!", tile_type.c_str(ctx), src.c_str(ctx), dst.c_str(ctx));
299
300 std::string tile_name = uarch->tile_name(pip.tile);
301 std::string dst_name = dst.str(ctx);
302 std::string src_name = src.str(ctx);
303
304 if (boost::starts_with(tile_name, "DSP_L") || boost::starts_with(tile_name, "DSP_R")) {
305 // FIXME: PPIPs missing for DSPs
306 return;
307 }
308 std::string orig_dst_name = dst_name;
309 if (boost::starts_with(tile_name, "RIOI3_SING") || boost::starts_with(tile_name, "LIOI3_SING") ||
310 boost::starts_with(tile_name, "RIOI_SING")) {
311 // FIXME: PPIPs missing for SING IOI3s
312 if ((boost::contains(src_name, "IMUX") || boost::contains(src_name, "CTRL0")) &&
313 !boost::contains(dst_name, "CLK"))
314 return;
315 auto spos = src_name.find("_SING_");
316 if (spos != std::string::npos)
317 src_name.erase(spos, 5);
318 // Need to flip for top HCLK
319 // TODO
320
321 bool is_top_sing = pip.tile < uarch->hclk_for_ioi(pip.tile);
322 if (is_top_sing) {
323 auto us0pos = dst_name.find("_0");
324 if (us0pos != std::string::npos)
325 dst_name.replace(us0pos, 2, "_1");
326 auto ol0pos = dst_name.find("OLOGIC0");
327 if (ol0pos != std::string::npos) {
328 dst_name.replace(ol0pos, 7, "OLOGIC1");
329 us0pos = src_name.find("_0");
330 if (us0pos != std::string::npos)
331 src_name.replace(us0pos, 2, "_1");
332 }
333 }
334 }
335 if (boost::contains(tile_name, "IOI")) {
336 if (boost::contains(dst_name, "OCLKB") && boost::contains(src_name, "IOI_OCLKM_"))
337 return; // missing, not sure if really a ppip?
338 }
339
340 out << tile_name << ".";
341 out << dst_name << ".";
342 out << src_name << std::endl;
343
344 if (boost::contains(tile_name, "IOI") && boost::starts_with(dst_name, "IOI_OCLK_")) {
345 dst_name.insert(dst_name.find("OCLK") + 4, 1, 'M');
346 orig_dst_name.insert(dst_name.find("OCLK") + 4, 1, 'M');
347
348 WireId w = uarch->lookup_wire(pip.tile, ctx->id(orig_dst_name));
349
350 NPNR_ASSERT(w != WireId());
351 if (ctx->getBoundWireNet(w) == nullptr) {
352 out << tile_name << ".";
353 out << dst_name << ".";
354 out << src_name << std::endl;
355 }
356 }
357
358 last_was_blank = false;
359 }
360 };
361
362 // Get the set of input signals for a LUT-type cell
363 std::vector<IdString> get_inputs(CellInfo *cell)
364 {
365 IdString type = ctx->id(str_or_default(cell->attrs, id_X_ORIG_TYPE, ""));
366 if (type == id_LUT1)
367 return {id_I0};
368 else if (type == id_LUT2)
369 return {id_I0, id_I1};
370 else if (type == id_LUT3)
371 return {id_I0, id_I1, id_I2};
372 else if (type == id_LUT4)
373 return {id_I0, id_I1, id_I2, id_I3};
374 else if (type == id_LUT5)
375 return {id_I0, id_I1, id_I2, id_I3, id_I4};
376 else if (type == id_LUT6)
377 return {id_I0, id_I1, id_I2, id_I3, id_I4, id_I5};
378 else if (type == id_RAMD64E)
379 return {id_RADR0, id_RADR1, id_RADR2, id_RADR3, id_RADR4, id_RADR5};
380 else if (type == id_SRL16E)
381 return {id_A0, id_A1, id_A2, id_A3};
382 else if (type == id_SRLC32E)
383 return {ctx->id("A[0]"), ctx->id("A[1]"), ctx->id("A[2]"), ctx->id("A[3]"), ctx->id("A[4]")};
384 else if (type == id_RAMD32)
385 return {id_RADR0, id_RADR1, id_RADR2, id_RADR3, id_RADR4};
386 else
387 NPNR_ASSERT_FALSE("unsupported LUT-type cell");
388 }
389
390 // Process LUT initialisation
391 std::vector<bool> get_lut_init(CellInfo *lut6, CellInfo *lut5)
392 {
393 std::vector<bool> bits(64, false);
394
395 std::vector<IdString> phys_inputs;
396 for (int i = 1; i <= 6; i++)
397 phys_inputs.push_back(ctx->id("A" + std::to_string(i)));
398
399 for (int i = 0; i < 2; i++) {
400 CellInfo *lut = (i == 1) ? lut5 : lut6;
401 if (lut == nullptr)
402 continue;
403 auto lut_inputs = get_inputs(lut);
404 dict<int, std::vector<std::string>> phys_to_log;
405 dict<std::string, int> log_to_bit;
406 for (int j = 0; j < int(lut_inputs.size()); j++)
407 log_to_bit[lut_inputs[j].str(ctx)] = j;
408 for (int j = 0; j < 6; j++) {
409 // Get the LUT physical to logical mapping
410 phys_to_log[j];
411 if (!lut->attrs.count(ctx->idf("X_ORIG_PORT_%s", phys_inputs[j].c_str(ctx))))
412 continue;
413 std::string orig = lut->attrs.at(ctx->idf("X_ORIG_PORT_%s", phys_inputs[j].c_str(ctx))).as_string();
414 boost::split(phys_to_log[j], orig, boost::is_any_of(" "));
415 }
416 int lbound = 0, ubound = 64;
417 // Fracturable LUTs
418 if (lut5 && lut6) {
419 lbound = (i == 1) ? 0 : 32;
420 ubound = (i == 1) ? 32 : 64;
421 }
422 Property init = get_or_default(lut->params, id_INIT, Property()).extract(0, 64);
423 for (int j = lbound; j < ubound; j++) {
424 int log_index = 0;
425 for (int k = 0; k < 6; k++) {
426 if ((j & (1 << k)) == 0)
427 continue;
428 for (auto &p2l : phys_to_log[k])
429 log_index |= (1 << log_to_bit[p2l]);
430 }
431 bits[j] = (init.str.at(log_index) == Property::S1);
432 }
433 }
434 return bits;
435 };
436
437 // Return the name for a half-logic-tile
438 std::string get_half_name(int half, bool is_m)
439 {
440 if (is_m)
441 return half ? "SLICEL_X1" : "SLICEM_X0";
442 else
443 return half ? "SLICEL_X1" : "SLICEL_X0";
444 }
445
446 std::string get_bel_name(BelId bel) { return uarch->bel_name_in_site(bel).str(ctx); }
447
448 void write_routing_bel(WireId dst_wire)
449 {
450 for (auto pip : ctx->getPipsUphill(dst_wire)) {
451 if (ctx->getBoundPipNet(pip) != nullptr) {
452 auto &pd = chip_pip_info(ctx->chip_info, pip);
453 const auto &extra_data = *reinterpret_cast<const XlnxPipExtraDataPOD *>(pd.extra_data.get());
454 std::string belname = IdString(extra_data.bel_name).str(ctx);
455 std::string pinname = IdString(extra_data.pip_config).str(ctx);
456 bool skip_pinname = false;
457 // Ignore modes with no associated bit (X-ray omission??)
458 if (belname == "WEMUX" && pinname == "WE")
459 continue;
460
461 if (belname.substr(1) == "DI1MUX") {
462 belname = "DI1MUX";
463 }
464
465 if (belname.substr(1) == "CY0") {
466 if (pinname.substr(1) == "5")
467 skip_pinname = true;
468 else
469 continue;
470 }
471
472 write_prefix();
473 out << belname;
474 if (!skip_pinname)
475 out << "." << pinname;
476 out << std::endl;
477 }
478 }
479 }
480
481 // Process flipflops in a half-tile
482 void write_ffs_config(int tile, int half)
483 {
484 bool found_ff = false;
485 bool negedge_ff = false;
486 bool is_latch = false;
487 bool is_sync = false;
488 bool is_clkinv = false;
489 bool is_srused = false;
490 bool is_ceused = false;
491
492 #define SET_CHECK(dst, src) \
493 do { \
494 if (found_ff) \
495 NPNR_ASSERT(dst == (src)); \
496 else \
497 dst = (src); \
498 } while (0)
499
500 std::string tname = uarch->tile_name(tile);
501
502 const auto &lts = uarch->tile_status.at(tile).lts;
503 if (!lts)
504 return;
505
506 push(tname);
507 push(get_half_name(half, boost::contains(tname, "CLBLM")));
508
509 for (int i = 0; i < 4; i++) {
510 CellInfo *ff1 = lts->cells[(half << 6) | (i << 4) | BEL_FF];
511 CellInfo *ff2 = lts->cells[(half << 6) | (i << 4) | BEL_FF2];
512 for (int j = 0; j < 2; j++) {
513 CellInfo *ff = (j == 1) ? ff2 : ff1;
514 if (ff == nullptr)
515 continue;
516 push(get_bel_name(ff->bel));
517 bool zrst = false, zinit = false;
518 zinit = (int_or_default(ff->params, id_INIT, 0) != 1);
519 IdString srsig;
520 std::string type = str_or_default(ff->attrs, id_X_ORIG_TYPE, "");
521 if (type == "FDRE") {
522 zrst = true;
523 SET_CHECK(negedge_ff, false);
524 SET_CHECK(is_latch, false);
525 SET_CHECK(is_sync, true);
526 } else if (type == "FDRE_1") {
527 zrst = true;
528 SET_CHECK(negedge_ff, true);
529 SET_CHECK(is_latch, false);
530 SET_CHECK(is_sync, true);
531 } else if (type == "FDSE") {
532 zrst = false;
533 SET_CHECK(negedge_ff, false);
534 SET_CHECK(is_latch, false);
535 SET_CHECK(is_sync, true);
536 } else if (type == "FDSE_1") {
537 zrst = false;
538 SET_CHECK(negedge_ff, true);
539 SET_CHECK(is_latch, false);
540 SET_CHECK(is_sync, true);
541 } else if (type == "FDCE") {
542 zrst = true;
543 SET_CHECK(negedge_ff, false);
544 SET_CHECK(is_latch, false);
545 SET_CHECK(is_sync, false);
546 } else if (type == "FDCE_1") {
547 zrst = true;
548 SET_CHECK(negedge_ff, true);
549 SET_CHECK(is_latch, false);
550 SET_CHECK(is_sync, false);
551 } else if (type == "FDPE") {
552 zrst = false;
553 SET_CHECK(negedge_ff, false);
554 SET_CHECK(is_latch, false);
555 SET_CHECK(is_sync, false);
556 } else if (type == "FDPE_1") {
557 zrst = false;
558 SET_CHECK(negedge_ff, true);
559 SET_CHECK(is_latch, false);
560 SET_CHECK(is_sync, false);
561 } else {
562 log_error("unsupported FF type: '%s'\n", type.c_str());
563 }
564
565 write_bit("ZINI", zinit);
566 write_bit("ZRST", zrst);
567
568 pop();
569 if (negedge_ff)
570 SET_CHECK(is_clkinv, true);
571 else
572 SET_CHECK(is_clkinv, int_or_default(ff->params, id_IS_C_INVERTED) == 1);
573
574 NetInfo *sr = ff->getPort(id_SR), *ce = ff->getPort(id_CE);
575
576 SET_CHECK(is_srused, sr != nullptr && sr->name != ctx->id("$PACKER_GND_NET"));
577 SET_CHECK(is_ceused, ce != nullptr && ce->name != ctx->id("$PACKER_VCC_NET"));
578
579 // Input mux
580 write_routing_bel(ctx->getBelPinWire(ff->bel, id_D));
581
582 found_ff = true;
583 }
584 }
585 write_bit("LATCH", is_latch);
586 write_bit("FFSYNC", is_sync);
587 write_bit("CLKINV", is_clkinv);
588 write_bit("NOCLKINV", !is_clkinv);
589 write_bit("SRUSEDMUX", is_srused);
590 write_bit("CEUSEDMUX", is_ceused);
591 pop(2);
592 }
593
594 // Get a named wire in the same site as a bel
595 WireId get_site_wire(BelId site_bel, std::string name)
596 {
597 IdStringList bel_name = ctx->getBelName(site_bel);
598 NPNR_ASSERT(bel_name.size() == 2);
599 IdString tile_name = bel_name[0];
600 const std::string &bel_name_str = bel_name[1].str(ctx);
601 size_t sep_pos = bel_name_str.find('.');
602 NPNR_ASSERT(sep_pos != std::string::npos);
603 std::string site_name = bel_name_str.substr(0, sep_pos);
604 IdString wire_name = ctx->idf("%s.%s", site_name.c_str(), name.c_str());
605 WireId wire = ctx->getWireByName(IdStringList::concat(tile_name, wire_name));
606 NPNR_ASSERT(wire != WireId());
607 return wire;
608 }
609
610 // Process LUTs and associated functionality in a half
611 void write_luts_config(int tile, int half)
612 {
613 bool wa7_used = false, wa8_used = false;
614
615 std::string tname = uarch->tile_name(tile);
616 bool is_mtile = boost::contains(tname, "CLBLM");
617 bool is_slicem = is_mtile && (half == 0);
618
619 const auto &lts = uarch->tile_status.at(tile).lts;
620 if (!lts)
621 return;
622
623 push(tname);
624 push(get_half_name(half, is_mtile));
625
626 BelId bel_in_half =
627 ctx->getBelByLocation(Loc(tile % ctx->chip_info->width, tile / ctx->chip_info->width, half << 6));
628
629 for (int i = 0; i < 4; i++) {
630 CellInfo *lut6 = lts->cells[(half << 6) | (i << 4) | BEL_6LUT];
631 CellInfo *lut5 = lts->cells[(half << 6) | (i << 4) | BEL_5LUT];
632 // Write LUT initialisation
633 if (lut6 != nullptr || lut5 != nullptr) {
634 std::string lutname = stringf("%cLUT", "ABCD"[i]);
635 push(lutname);
636 write_vector("INIT[63:0]", get_lut_init(lut6, lut5));
637
638 // Write LUT mode config
639 bool is_small = false, is_ram = false, is_srl = false;
640 for (int j = 0; j < 2; j++) {
641 CellInfo *lut = (j == 1) ? lut5 : lut6;
642 if (lut == nullptr)
643 continue;
644 std::string type = str_or_default(lut->attrs, id_X_ORIG_TYPE);
645 if (type == "RAMD64E" || type == "RAMS64E") {
646 is_ram = true;
647 } else if (type == "RAMD32" || type == "RAMS32") {
648 is_ram = true;
649 is_small = true;
650 } else if (type == "SRL16E") {
651 is_srl = true;
652 is_small = true;
653 } else if (type == "SRLC32E") {
654 is_srl = true;
655 }
656 wa7_used |= (lut->getPort(id_WA7) != nullptr);
657 wa8_used |= (lut->getPort(id_WA8) != nullptr);
658 }
659 if (is_slicem && i != 3) {
660 write_routing_bel(get_site_wire(bel_in_half, stringf("%cDI1MUX_OUT", "ABCD"[i])));
661 }
662 write_bit("SMALL", is_small);
663 write_bit("RAM", is_ram);
664 write_bit("SRL", is_srl);
665 pop();
666 }
667 write_routing_bel(get_site_wire(bel_in_half, stringf("%cMUX", "ABCD"[i])));
668 }
669 write_bit("WA7USED", wa7_used);
670 write_bit("WA8USED", wa8_used);
671 if (is_slicem)
672 write_routing_bel(get_site_wire(bel_in_half, "WEMUX_OUT"));
673
674 pop(2);
675 }
676
677 void write_carry_config(int tile, int half)
678 {
679 std::string tname = uarch->tile_name(tile);
680 bool is_mtile = boost::contains(tname, "CLBLM");
681
682 const auto &lts = uarch->tile_status.at(tile).lts;
683 if (!lts)
684 return;
685
686 CellInfo *carry = lts->cells[half << 6 | BEL_CARRY4];
687 if (carry == nullptr)
688 return;
689
690 push(tname);
691 push(get_half_name(half, is_mtile));
692
693 write_routing_bel(get_site_wire(carry->bel, "PRECYINIT_OUT"));
694 if (carry->getPort(id_CIN) != nullptr)
695 write_bit("PRECYINIT.CIN");
696 push("CARRY4");
697 for (char c : {'A', 'B', 'C', 'D'})
698 write_routing_bel(get_site_wire(carry->bel, stringf("%cCY0_OUT", c)));
699 pop(3);
700 }
701
702 void write_logic()
703 {
704 std::set<int> used_logic_tiles;
705 for (auto &cell : ctx->cells) {
706 if (uarch->is_logic_tile(cell.second->bel))
707 used_logic_tiles.insert(cell.second->bel.tile);
708 }
709 for (int tile : used_logic_tiles) {
710 write_luts_config(tile, 0);
711 write_luts_config(tile, 1);
712 write_ffs_config(tile, 0);
713 write_ffs_config(tile, 1);
714 write_carry_config(tile, 0);
715 write_carry_config(tile, 1);
716 blank();
717 }
718 }
719
720 void write_routing()
721 {
722 get_pseudo_pip_data();
723 for (auto &net : ctx->nets) {
724 NetInfo *ni = net.second.get();
725 out << stringf("# routing for net %s", ni->name.c_str(ctx)) << std::endl;
726 for (auto &w : ni->wires) {
727 if (w.second.pip != PipId())
728 write_pip(w.second.pip, ni);
729 }
730 blank();
731 }
732 }
733
734 struct BankIoConfig
735 {
736 bool stepdown = false;
737 bool vref = false;
738 bool tmds_33 = false;
739 bool lvds_25 = false;
740 bool only_diff = false;
741 };
742
743 dict<int, BankIoConfig> ioconfig_by_hclk;
744
745 bool warned_dci = false;
746
747 void write_io_config(CellInfo *pad)
748 {
749 NetInfo *pad_net = pad->getPort(id_PAD);
750 NPNR_ASSERT(pad_net != nullptr);
751 std::string iostandard = str_or_default(pad->attrs, id_IOSTANDARD, "LVCMOS33");
752 std::string pulltype = str_or_default(pad->attrs, id_PULLTYPE, "NONE");
753 std::string slew = str_or_default(pad->attrs, id_SLEW, "SLOW");
754
755 Loc ioLoc = uarch->rel_site_loc(uarch->get_bel_site(pad->bel));
756 bool is_output = false, is_input = false;
757 if (pad_net->driver.cell != nullptr)
758 is_output = true;
759 for (auto &usr : pad_net->users)
760 if (boost::contains(usr.cell->type.str(ctx), "INBUF"))
761 is_input = true;
762 std::string tile = uarch->tile_name(pad->bel.tile);
763 push(tile);
764
765 if (boost::ends_with(iostandard, "_T_DCI")) {
766 if (!warned_dci)
767 log_warning("DCI is not supported, will be removed.\n");
768 warned_dci = true;
769 iostandard.erase(iostandard.size() - 6, iostandard.size());
770 }
771
772 bool is_riob18 = boost::starts_with(tile, "RIOB18_");
773 bool is_sing = boost::contains(tile, "_SING_");
774 bool is_top_sing = pad->bel.tile < uarch->hclk_for_iob(pad->bel);
775 bool is_stepdown = false;
776 bool is_lvcmos = boost::starts_with(iostandard, "LVCMOS");
777 bool is_low_volt_lvcmos = iostandard == "LVCMOS12" || iostandard == "LVCMOS15" || iostandard == "LVCMOS18";
778
779 auto yLoc = is_sing ? (is_top_sing ? 1 : 0) : (1 - ioLoc.y);
780 push("IOB_Y" + std::to_string(yLoc));
781
782 bool has_diff_prefix = boost::starts_with(iostandard, "DIFF_");
783 bool is_tmds33 = iostandard == "TMDS_33";
784 bool is_lvds25 = iostandard == "LVDS_25";
785 bool is_lvds = boost::starts_with(iostandard, "LVDS");
786 bool only_diff = is_tmds33 || is_lvds;
787 bool is_diff = only_diff || has_diff_prefix;
788 if (has_diff_prefix)
789 iostandard.erase(0, 5);
790 bool is_sstl = iostandard == "SSTL12" || iostandard == "SSTL135" || iostandard == "SSTL15";
791
792 int hclk = uarch->hclk_for_iob(pad->bel);
793
794 if (only_diff)
795 ioconfig_by_hclk[hclk].only_diff = true;
796 if (is_tmds33)
797 ioconfig_by_hclk[hclk].tmds_33 = true;
798 if (is_lvds25)
799 ioconfig_by_hclk[hclk].lvds_25 = true;
800
801 if (is_output) {
802 // DRIVE
803 int default_drive = (is_riob18 && iostandard == "LVCMOS12") ? 8 : 12;
804 int drive = int_or_default(pad->attrs, id_DRIVE, default_drive);
805
806 if ((iostandard == "LVCMOS33" || iostandard == "LVTTL") && is_riob18)
807 log_error("high performance banks (RIOB18) do not support IO standard %s\n", iostandard.c_str());
808
809 if (iostandard == "SSTL135")
810 write_bit("SSTL135.DRIVE.I_FIXED");
811 else if (is_riob18) {
812 if ((iostandard == "LVCMOS18" || iostandard == "LVCMOS15"))
813 write_bit("LVCMOS15_LVCMOS18.DRIVE.I12_I16_I2_I4_I6_I8");
814 else if (iostandard == "LVCMOS12")
815 write_bit("LVCMOS12.DRIVE.I2_I4_I6_I8");
816 else if (iostandard == "LVDS")
817 write_bit("LVDS.DRIVE.I_FIXED");
818 else if (is_sstl) {
819 write_bit(iostandard + ".DRIVE.I_FIXED");
820 }
821 } else { // IOB33
822 if (iostandard == "TMDS_33" && yLoc == 0) {
823 write_bit("TMDS_33.DRIVE.I_FIXED");
824 write_bit("TMDS_33.OUT");
825 } else if (iostandard == "LVDS_25" && yLoc == 0) {
826 write_bit("LVDS_25.DRIVE.I_FIXED");
827 write_bit("LVDS_25.OUT");
828 } else if ((iostandard == "LVCMOS15" && drive == 16) || iostandard == "SSTL15")
829 write_bit("LVCMOS15_SSTL15.DRIVE.I16_I_FIXED");
830 else if (iostandard == "LVCMOS18" && (drive == 12 || drive == 8))
831 write_bit("LVCMOS18.DRIVE.I12_I8");
832 else if ((iostandard == "LVCMOS33" && drive == 16) || (iostandard == "LVTTL" && drive == 16))
833 write_bit("LVCMOS33_LVTTL.DRIVE.I12_I16");
834 else if ((iostandard == "LVCMOS33" && (drive == 8 || drive == 12)) ||
835 (iostandard == "LVTTL" && (drive == 8 || drive == 12)))
836 write_bit("LVCMOS33_LVTTL.DRIVE.I12_I8");
837 else if ((iostandard == "LVCMOS33" && drive == 4) || (iostandard == "LVTTL" && drive == 4))
838 write_bit("LVCMOS33_LVTTL.DRIVE.I4");
839 else if (drive == 8 && (iostandard == "LVCMOS12" || iostandard == "LVCMOS25"))
840 write_bit("LVCMOS12_LVCMOS25.DRIVE.I8");
841 else if (drive == 4 &&
842 (iostandard == "LVCMOS15" || iostandard == "LVCMOS18" || iostandard == "LVCMOS25"))
843 write_bit("LVCMOS15_LVCMOS18_LVCMOS25.DRIVE.I4");
844 else if (is_lvcmos || iostandard == "LVTTL")
845 write_bit(iostandard + ".DRIVE.I" + std::to_string(drive));
846 }
847
848 // SSTL output used
849 if (is_riob18 && is_sstl)
850 write_bit(iostandard + ".IN_USE");
851
852 // SLEW
853 if (is_riob18 && slew == "SLOW") {
854 if (iostandard == "SSTL135")
855 write_bit("SSTL135.SLEW.SLOW");
856 else if (iostandard == "SSTL15")
857 write_bit("SSTL15.SLEW.SLOW");
858 else
859 write_bit("LVCMOS12_LVCMOS15_LVCMOS18.SLEW.SLOW");
860 } else if (slew == "SLOW") {
861 if (iostandard != "LVDS_25" && iostandard != "TMDS_33")
862 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_LVCMOS25_LVCMOS33_LVTTL_SSTL135_SSTL15.SLEW.SLOW");
863 } else if (is_riob18)
864 write_bit(iostandard + ".SLEW.FAST");
865 else if (iostandard == "SSTL135" || iostandard == "SSTL15")
866 write_bit("SSTL135_SSTL15.SLEW.FAST");
867 else
868 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_LVCMOS25_LVCMOS33_LVTTL.SLEW.FAST");
869 }
870
871 if (is_input) {
872 if (!is_diff) {
873 if (iostandard == "LVCMOS33" || iostandard == "LVTTL" || iostandard == "LVCMOS25") {
874 if (!is_riob18)
875 write_bit("LVCMOS25_LVCMOS33_LVTTL.IN");
876 else
877 log_error("high performance banks (RIOB18) do not support IO standard %s\n",
878 iostandard.c_str());
879 }
880
881 if (is_sstl) {
882 ioconfig_by_hclk[hclk].vref = true;
883 if (!is_riob18)
884 write_bit("SSTL135_SSTL15.IN");
885
886 if (is_riob18) {
887 write_bit("SSTL12_SSTL135_SSTL15.IN");
888 }
889
890 if (!is_riob18 && pad->attrs.count(id_IN_TERM))
891 write_bit("IN_TERM." + pad->attrs.at(id_IN_TERM).as_string());
892 }
893
894 if (is_low_volt_lvcmos) {
895 write_bit("LVCMOS12_LVCMOS15_LVCMOS18.IN");
896 }
897 } else /* is_diff */ {
898 if (is_riob18) {
899 // vivado generates these bits only for Y0 of a diff pair
900 if (yLoc == 0) {
901 write_bit("LVDS_SSTL12_SSTL135_SSTL15.IN_DIFF");
902 if (iostandard == "LVDS")
903 write_bit("LVDS.IN_USE");
904 }
905 } else {
906 if (iostandard == "TDMS_33")
907 write_bit("TDMS_33.IN_DIFF");
908 else
909 write_bit("LVDS_25_SSTL135_SSTL15.IN_DIFF");
910 }
911
912 if (pad->attrs.count(id_IN_TERM))
913 write_bit("IN_TERM." + pad->attrs.at(id_IN_TERM).as_string());
914 }
915
916 // IN_ONLY
917 if (!is_output) {
918 if (is_riob18) {
919 // vivado also sets this bit for DIFF_SSTL
920 if (is_diff && (yLoc == 0))
921 write_bit("LVDS.IN_ONLY");
922 else
923 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_SSTL12_SSTL135_SSTL15.IN_ONLY");
924 } else
925 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_LVCMOS25_LVCMOS33_LVDS_25_LVTTL_SSTL135_SSTL15_TMDS_33.IN_"
926 "ONLY");
927 }
928 }
929
930 if (!is_riob18 && (is_low_volt_lvcmos || is_sstl)) {
931 if (iostandard == "SSTL12") {
932 log_error("SSTL12 is only available on high performance banks.");
933 }
934 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_SSTL135_SSTL15.STEPDOWN");
935 ioconfig_by_hclk[hclk].stepdown = true;
936 is_stepdown = true;
937 }
938
939 if (is_riob18 && (is_input || is_output) && (boost::contains(iostandard, "SSTL") || iostandard == "LVDS")) {
940 if (((yLoc == 0) && (iostandard == "LVDS")) || boost::contains(iostandard, "SSTL")) {
941 // TODO: I get bit conflicts with this, it seems to work anyway. Test more.
942 // write_bit("LVDS.IN_USE");
943 }
944 }
945
946 if (is_input && is_output && !is_diff && yLoc == 1) {
947 if (is_riob18 && boost::starts_with(iostandard, "SSTL"))
948 write_bit("SSTL12_SSTL135_SSTL15.IN");
949 }
950
951 // IN_TERM.NONE and IN_ONLY for TMDS_33 output, e.g. HDMI signals
952 if (is_output && is_diff) {
953 if (is_tmds33 && yLoc == 1) {
954 if (pad->attrs.count(id_IN_TERM))
955 write_bit("IN_TERM." + pad->attrs.at(id_IN_TERM).as_string());
956 else
957 write_bit("IN_TERM.NONE");
958 write_bit("LVCMOS12_LVCMOS15_LVCMOS18_LVCMOS25_LVCMOS33_LVDS_25_LVTTL_SSTL135_SSTL15_TMDS_33.IN_ONLY");
959 }
960 }
961
962 write_bit("PULLTYPE." + pulltype);
963 pop(); // IOB_YN
964
965 BelId inv;
966
967 auto pad_bel_site = uarch->get_bel_site(pad->bel);
968
969 if (is_riob18)
970 inv = uarch->get_site_bel(pad_bel_site, ctx->id("IOB18S.O_ININV"));
971 else
972 inv = uarch->get_site_bel(pad_bel_site, ctx->id("IOB33S.O_ININV"));
973
974 if (inv != BelId() && ctx->getBoundBelCell(inv) != nullptr)
975 write_bit("OUT_DIFF");
976
977 if (is_stepdown && !is_sing)
978 write_bit("IOB_Y" + std::to_string(ioLoc.y) + ".LVCMOS12_LVCMOS15_LVCMOS18_SSTL135_SSTL15.STEPDOWN");
979
980 pop(); // tile
981 }
982
983 void write_iol_config(CellInfo *ci)
984 {
985 std::string tile = uarch->tile_name(ci->bel.tile);
986 push(tile);
987 bool is_sing = boost::contains(tile, "_SING_");
988 bool is_top_sing = ci->bel.tile < uarch->hclk_for_ioi(ci->bel.tile);
989
990 auto site_key = uarch->get_bel_site(ci->bel);
991 std::string site = uarch->get_site_name(site_key).str(ctx);
992 std::string sitetype = site.substr(0, site.find('_'));
993 Loc siteloc = uarch->rel_site_loc(site_key);
994 push(stringf("%s_Y%d", sitetype.c_str(), is_sing ? (is_top_sing ? 1 : 0) : (1 - siteloc.y)));
995
996 if (ci->type == id_ILOGICE3_IFF) {
997 write_bit("IDDR.IN_USE");
998 write_bit("IDDR_OR_ISERDES.IN_USE");
999 write_bit("ISERDES.MODE.MASTER");
1000 write_bit("ISERDES.NUM_CE.N1");
1001
1002 // Switch IDELMUXE3 to include the IDELAY element, if we have an IDELAYE2 driving D
1003 NetInfo *d = ci->getPort(id_D);
1004 if (d == nullptr || d->driver.cell == nullptr)
1005 log_error("%s '%s' has disconnected D input\n", ci->type.c_str(ctx), ctx->nameOf(ci));
1006 CellInfo *drv = d->driver.cell;
1007 if (boost::contains(drv->type.str(ctx), "IDELAYE2"))
1008 write_bit("IDELMUXE3.P0");
1009 else
1010 write_bit("IDELMUXE3.P1");
1011
1012 // clock edge
1013 std::string edge = str_or_default(ci->params, id_DDR_CLK_EDGE, "OPPOSITE_EDGE");
1014 if (edge == "SAME_EDGE")
1015 write_bit("IFF.DDR_CLK_EDGE.SAME_EDGE");
1016 else if (edge == "OPPOSITE_EDGE")
1017 write_bit("IFF.DDR_CLK_EDGE.OPPOSITE_EDGE");
1018 else
1019 log_error("unsupported clock edge parameter for cell '%s' at %s: %s. Supported are: SAME_EDGE and "
1020 "OPPOSITE_EDGE",
1021 ci->name.c_str(ctx), site.c_str(), edge.c_str());
1022
1023 std::string srtype = str_or_default(ci->params, id_SRTYPE, "SYNC");
1024 if (srtype == "SYNC")
1025 write_bit("IFF.SRTYPE.SYNC");
1026 else
1027 write_bit("IFF.SRTYPE.ASYNC");
1028
1029 write_bit("IFF.ZINV_C", !bool_or_default(ci->params, id_IS_CLK_INVERTED, false));
1030 write_bit("ZINV_D", !bool_or_default(ci->params, id_IS_D_INVERTED, false));
1031
1032 auto init = int_or_default(ci->params, id_INIT_Q1, 0);
1033 if (init == 0)
1034 write_bit("IFF.ZINIT_Q1");
1035 init = int_or_default(ci->params, id_INIT_Q2, 0);
1036 if (init == 0)
1037 write_bit("IFF.ZINIT_Q2");
1038
1039 auto sr_name = str_or_default(ci->attrs, id_X_ORIG_PORT_SR, "R");
1040 if (sr_name == "R") {
1041 write_bit("IFF.ZSRVAL_Q1");
1042 write_bit("IFF.ZSRVAL_Q2");
1043 }
1044 } else if (ci->type.in(id_OLOGICE2_OUTFF, id_OLOGICE3_OUTFF)) {
1045 std::string edge = str_or_default(ci->params, id_DDR_CLK_EDGE, "OPPOSITE_EDGE");
1046 if (edge == "SAME_EDGE")
1047 write_bit("ODDR.DDR_CLK_EDGE.SAME_EDGE");
1048
1049 write_bit("ODDR_TDDR.IN_USE");
1050 write_bit("OQUSED");
1051 write_bit("OSERDES.DATA_RATE_OQ.DDR");
1052 write_bit("OSERDES.DATA_RATE_TQ.BUF");
1053
1054 std::string srtype = str_or_default(ci->params, id_SRTYPE, "SYNC");
1055 if (srtype == "SYNC")
1056 write_bit("OSERDES.SRTYPE.SYNC");
1057
1058 for (std::string d : {"D1", "D2"})
1059 write_bit("IS_" + d + "_INVERTED",
1060 bool_or_default(ci->params, ctx->id("IS_" + d + "_INVERTED"), false));
1061
1062 auto init = int_or_default(ci->params, id_INIT, 1);
1063 if (init == 0)
1064 write_bit("ZINIT_OQ");
1065
1066 write_bit("ODDR.SRUSED", ci->getPort(id_SR) != nullptr);
1067 auto sr_name = str_or_default(ci->attrs, id_X_ORIG_PORT_SR, "R");
1068 if (sr_name == "R")
1069 write_bit("ZSRVAL_OQ");
1070
1071 auto clk_inv = bool_or_default(ci->params, id_IS_CLK_INVERTED);
1072 if (!clk_inv)
1073 write_bit("ZINV_CLK");
1074 } else if (ci->type == id_OSERDESE2_OSERDESE2) {
1075 write_bit("ODDR.DDR_CLK_EDGE.SAME_EDGE");
1076 write_bit("ODDR.SRUSED");
1077 write_bit("ODDR_TDDR.IN_USE");
1078
1079 auto serdes_mode = str_or_default(ci->params, id_SERDES_MODE, "MASTER");
1080 bool is_cascaded = (serdes_mode == "SLAVE");
1081
1082 // For cascaded OSERDESE2, OQUSED must be set even though OQ is not connected
1083 write_bit("OQUSED", is_cascaded || ci->getPort(id_OQ));
1084 write_bit("ZINV_CLK", !bool_or_default(ci->params, id_IS_CLK_INVERTED, false));
1085 for (std::string t : {"T1", "T2", "T3", "T4"})
1086 write_bit("ZINV_" + t, (ci->getPort(ctx->id(t)) != nullptr || t == "T1") &&
1087 !bool_or_default(ci->params, ctx->id("IS_" + t + "_INVERTED"), false));
1088 for (std::string d : {"D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8"})
1089 write_bit("IS_" + d + "_INVERTED",
1090 bool_or_default(ci->params, ctx->id("IS_" + d + "_INVERTED"), false));
1091 write_bit("ZINIT_OQ", !bool_or_default(ci->params, id_INIT_OQ, false));
1092 write_bit("ZINIT_TQ", !bool_or_default(ci->params, id_INIT_TQ, false));
1093 write_bit("ZSRVAL_OQ", !bool_or_default(ci->params, id_SRVAL_OQ, false));
1094 write_bit("ZSRVAL_TQ", !bool_or_default(ci->params, id_SRVAL_TQ, false));
1095
1096 push("OSERDES");
1097 write_bit("IN_USE");
1098 std::string type = str_or_default(ci->params, id_DATA_RATE_OQ, "BUF");
1099 write_bit(std::string("DATA_RATE_OQ.") + ((ci->getPort(id_OQ) != nullptr) ? type : "DDR"));
1100 write_bit(std::string("DATA_RATE_TQ.") +
1101 ((ci->getPort(id_TQ) != nullptr) ? str_or_default(ci->params, id_DATA_RATE_TQ, "BUF") : "BUF"));
1102 int width = int_or_default(ci->params, id_DATA_WIDTH, 8);
1103 #if 0
1104 write_bit("DATA_WIDTH.W" + std::to_string(width));
1105 if (type == "DDR" && (width == 6 || width == 8)) {
1106 write_bit("DATA_WIDTH.DDR.W6_8");
1107 write_bit("DATA_WIDTH.SDR.W2_4_5_6");
1108 } else if (type == "SDR" && (width == 2 || width == 4 || width == 5 || width == 6)) {
1109 write_bit("DATA_WIDTH.SDR.W2_4_5_6");
1110 }
1111 #else
1112 if (type == "DDR")
1113 write_bit("DATA_WIDTH.DDR.W" + std::to_string(width));
1114 else if (type == "SDR")
1115 write_bit("DATA_WIDTH.SDR.W" + std::to_string(width));
1116 else
1117 write_bit("DATA_WIDTH.W" + std::to_string(width));
1118 #endif
1119 write_bit("SRTYPE.SYNC");
1120 write_bit("TSRTYPE.SYNC");
1121 if (is_cascaded)
1122 write_bit("SERDES_MODE.SLAVE");
1123 pop();
1124 } else if (ci->type == id_ISERDESE2_ISERDESE2) {
1125 std::string data_rate = str_or_default(ci->params, id_DATA_RATE);
1126 write_bit("IDDR_OR_ISERDES.IN_USE");
1127 if (data_rate == "DDR")
1128 write_bit("IDDR.IN_USE");
1129 write_bit("IFF.DDR_CLK_EDGE.OPPOSITE_EDGE");
1130 write_bit("IFF.SRTYPE.SYNC");
1131 for (int i = 1; i <= 4; i++) {
1132 write_bit("IFF.ZINIT_Q" + std::to_string(i),
1133 !bool_or_default(ci->params, ctx->idf("INIT_Q%d", i), false));
1134 write_bit("IFF.ZSRVAL_Q" + std::to_string(i),
1135 !bool_or_default(ci->params, ctx->idf("SRVAL_Q%d", i), false));
1136 }
1137 write_bit("IFF.ZINV_C", !bool_or_default(ci->params, id_IS_CLK_INVERTED, false));
1138 write_bit("IFF.ZINV_OCLK", !bool_or_default(ci->params, id_IS_OCLK_INVERTED, false));
1139
1140 std::string iobdelay = str_or_default(ci->params, id_IOBDELAY, "NONE");
1141 write_bit("IFFDELMUXE3.P0", (iobdelay == "IFD"));
1142 write_bit("ZINV_D", !bool_or_default(ci->params, id_IS_D_INVERTED, false) && (iobdelay != "IFD"));
1143
1144 push("ISERDES");
1145 write_bit("IN_USE");
1146 int width = int_or_default(ci->params, id_DATA_WIDTH, 8);
1147 std::string mode = str_or_default(ci->params, id_INTERFACE_TYPE, "NETWORKING");
1148 std::string rate = str_or_default(ci->params, id_DATA_RATE, "DDR");
1149 write_bit(mode + "." + rate + ".W" + std::to_string(width));
1150 write_bit("MODE." + str_or_default(ci->params, id_SERDES_MODE, "MASTER"));
1151 write_bit("NUM_CE.N" + std::to_string(int_or_default(ci->params, id_NUM_CE, 1)));
1152 pop();
1153 } else if (ci->type == id_IDELAYE2_IDELAYE2) {
1154 write_bit("IN_USE");
1155 write_bit("CINVCTRL_SEL", str_or_default(ci->params, id_CINVCTRL_SEL, "FALSE") == "TRUE");
1156 write_bit("PIPE_SEL", str_or_default(ci->params, id_PIPE_SEL, "FALSE") == "TRUE");
1157 write_bit("HIGH_PERFORMANCE_MODE", str_or_default(ci->params, id_HIGH_PERFORMANCE_MODE, "FALSE") == "TRUE");
1158 write_bit("DELAY_SRC_" + str_or_default(ci->params, id_DELAY_SRC, "IDATAIN"));
1159 write_bit("IDELAY_TYPE_" + str_or_default(ci->params, id_IDELAY_TYPE, "FIXED"));
1160 write_int_vector("IDELAY_VALUE[4:0]", int_or_default(ci->params, id_IDELAY_VALUE, 0), 5, false);
1161 write_int_vector("ZIDELAY_VALUE[4:0]", int_or_default(ci->params, id_IDELAY_VALUE, 0), 5, true);
1162 write_bit("IS_DATAIN_INVERTED", bool_or_default(ci->params, id_IS_DATAIN_INVERTED, false));
1163 write_bit("IS_IDATAIN_INVERTED", bool_or_default(ci->params, id_IS_IDATAIN_INVERTED, false));
1164 } else if (ci->type == id_ODELAYE2_ODELAYE2) {
1165 write_bit("IN_USE");
1166 write_bit("CINVCTRL_SEL", str_or_default(ci->params, id_CINVCTRL_SEL, "FALSE") == "TRUE");
1167 write_bit("HIGH_PERFORMANCE_MODE", str_or_default(ci->params, id_HIGH_PERFORMANCE_MODE, "FALSE") == "TRUE");
1168 auto type = str_or_default(ci->params, id_ODELAY_TYPE, "FIXED");
1169 if (type != "FIXED")
1170 write_bit("ODELAY_TYPE_" + type);
1171 write_int_vector("ODELAY_VALUE[4:0]", int_or_default(ci->params, id_ODELAY_VALUE, 0), 5, false);
1172 write_int_vector("ZODELAY_VALUE[4:0]", int_or_default(ci->params, id_ODELAY_VALUE, 0), 5, true);
1173 write_bit("ZINV_ODATAIN", !bool_or_default(ci->params, id_IS_ODATAIN_INVERTED, false));
1174 } else {
1175 NPNR_ASSERT_FALSE("unsupported IOLOGIC");
1176 }
1177 pop(2);
1178 }
1179
1180 void write_io()
1181 {
1182 for (auto &cell : ctx->cells) {
1183 CellInfo *ci = cell.second.get();
1184 if (ci->type == id_PAD) {
1185 write_io_config(ci);
1186 blank();
1187 } else if (ci->type.in(id_ILOGICE3_IFF, id_OLOGICE2_OUTFF, id_OLOGICE3_OUTFF, id_OSERDESE2_OSERDESE2,
1188 id_ISERDESE2_ISERDESE2, id_IDELAYE2_IDELAYE2, id_ODELAYE2_ODELAYE2)) {
1189 write_iol_config(ci);
1190 blank();
1191 }
1192 }
1193 for (auto &hclk : ioconfig_by_hclk) {
1194 push(uarch->tile_name(hclk.first));
1195 write_bit("STEPDOWN", hclk.second.stepdown);
1196 write_bit("VREF.V_675_MV", hclk.second.vref);
1197 write_bit("ONLY_DIFF_IN_USE", hclk.second.only_diff);
1198 write_bit("TMDS_33_IN_USE", hclk.second.tmds_33);
1199 write_bit("LVDS_25_IN_USE", hclk.second.lvds_25);
1200 pop();
1201 }
1202 }
1203
1204 std::vector<std::string> used_wires_starting_with(int tile, const std::string &prefix, bool is_source)
1205 {
1206 std::vector<std::string> wires;
1207 if (!pips_by_tile.count(tile))
1208 return wires;
1209 for (auto pip : pips_by_tile[tile]) {
1210 auto &pd = chip_pip_info(ctx->chip_info, pip);
1211 int wire_index = is_source ? pd.src_wire : pd.dst_wire;
1212 std::string wire = IdString(chip_wire_info(ctx->chip_info, WireId(pip.tile, wire_index)).name).str(ctx);
1213 if (boost::starts_with(wire, prefix))
1214 wires.push_back(wire);
1215 }
1216 return wires;
1217 }
1218
1219 void write_clocking()
1220 {
1221 std::string name, type;
1222
1223 std::set<std::string> all_gclk;
1224 dict<int, std::set<std::string>> hclk_by_row;
1225
1226 for (auto &cell : ctx->cells) {
1227 CellInfo *ci = cell.second.get();
1228 if (ci->type == id_BUFGCTRL) {
1229 push(uarch->tile_name(ci->bel.tile));
1230 auto xy = uarch->rel_site_loc(uarch->get_bel_site(ci->bel));
1231 push(stringf("BUFGCTRL.BUFGCTRL_X%dY%d", xy.x, xy.y));
1232 write_bit("IN_USE");
1233 write_bit("INIT_OUT", bool_or_default(ci->params, id_INIT_OUT));
1234 write_bit("IS_IGNORE0_INVERTED", bool_or_default(ci->params, id_IS_IGNORE0_INVERTED));
1235 write_bit("IS_IGNORE1_INVERTED", bool_or_default(ci->params, id_IS_IGNORE1_INVERTED));
1236 write_bit("ZINV_CE0", !bool_or_default(ci->params, id_IS_CE0_INVERTED));
1237 write_bit("ZINV_CE1", !bool_or_default(ci->params, id_IS_CE1_INVERTED));
1238 write_bit("ZINV_S0", !bool_or_default(ci->params, id_IS_S0_INVERTED));
1239 write_bit("ZINV_S1", !bool_or_default(ci->params, id_IS_S1_INVERTED));
1240 pop(2);
1241 } else if (ci->type == id_PLLE2_ADV_PLLE2_ADV) {
1242 write_pll(ci);
1243 } else if (ci->type == id_MMCME2_ADV_MMCME2_ADV) {
1244 write_mmcm(ci);
1245 }
1246 blank();
1247 }
1248
1249 for (int tile = 0; tile < ctx->chip_info->tile_insts.ssize(); tile++) {
1250 std::string name = uarch->tile_name(tile);
1251 std::string type = ctx->get_tile_type(tile).str(ctx);
1252 push(name);
1253 if (type == "HCLK_L" || type == "HCLK_R" || type == "HCLK_L_BOT_UTURN" || type == "HCLK_R_BOT_UTURN") {
1254 auto used_sources = used_wires_starting_with(tile, "HCLK_CK_", true);
1255 push("ENABLE_BUFFER");
1256 for (auto s : used_sources) {
1257 if (boost::contains(s, "BUFHCLK")) {
1258 write_bit(s);
1259 hclk_by_row[tile / ctx->chip_info->width].insert(s.substr(s.find("BUFHCLK")));
1260 }
1261 }
1262 pop();
1263 } else if (boost::starts_with(type, "CLK_HROW")) {
1264 auto used_gclk = used_wires_starting_with(tile, "CLK_HROW_R_CK_GCLK", true);
1265 auto used_ck_in = used_wires_starting_with(tile, "CLK_HROW_CK_IN", true);
1266 for (auto s : used_gclk) {
1267 write_bit(s + "_ACTIVE");
1268 all_gclk.insert(s.substr(s.find("GCLK")));
1269 }
1270 for (auto s : used_ck_in) {
1271 if (boost::contains(s, "HROW_CK_INT"))
1272 continue;
1273 write_bit(s + "_ACTIVE");
1274 }
1275 } else if (boost::starts_with(type, "HCLK_CMT")) {
1276 auto used_ccio = used_wires_starting_with(tile, "HCLK_CMT_CCIO", true);
1277 for (auto s : used_ccio) {
1278 write_bit(s + "_ACTIVE");
1279 write_bit(s + "_USED");
1280 }
1281 auto used_hclk = used_wires_starting_with(tile, "HCLK_CMT_CK_", true);
1282 for (auto s : used_hclk) {
1283 if (boost::contains(s, "BUFHCLK")) {
1284 write_bit(s + "_USED");
1285 hclk_by_row[tile / ctx->chip_info->width].insert(s.substr(s.find("BUFHCLK")));
1286 }
1287 }
1288 }
1289 pop();
1290 blank();
1291 }
1292
1293 for (int tile = 0; tile < ctx->chip_info->tile_insts.ssize(); tile++) {
1294 std::string name = uarch->tile_name(tile);
1295 std::string type = ctx->get_tile_type(tile).str(ctx);
1296 push(name);
1297 if (type == "CLK_BUFG_REBUF") {
1298 for (auto &gclk : all_gclk) {
1299 write_bit(gclk + "_ENABLE_ABOVE");
1300 write_bit(gclk + "_ENABLE_BELOW");
1301 }
1302 } else if (boost::starts_with(type, "HCLK_CMT")) {
1303 for (auto &hclk : hclk_by_row[tile / ctx->chip_info->width]) {
1304 write_bit("HCLK_CMT_CK_" + hclk + "_USED");
1305 }
1306 }
1307 pop();
1308 blank();
1309 }
1310 }
1311
1312 void write_bram_width(CellInfo *ci, const std::string &name, bool is_36, bool is_y1)
1313 {
1314 int width = int_or_default(ci->params, ctx->id(name), 0);
1315 if (width == 0)
1316 return;
1317 int actual_width = width;
1318 if (is_36) {
1319 if (width == 1)
1320 actual_width = 1;
1321 else
1322 actual_width = width / 2;
1323 }
1324 if (((is_36 && width == 72) || (is_y1 && actual_width == 36)) && name == "READ_WIDTH_A") {
1325 write_bit(name + "_18");
1326 }
1327 if (actual_width == 36) {
1328 write_bit("SDP_" + name.substr(0, name.length() - 2) + "_36");
1329 if (name.find("WRITE") == 0) {
1330 write_bit(name.substr(0, name.size() - 1) + "A_18");
1331 write_bit(name.substr(0, name.size() - 1) + "B_18");
1332 } else if (name.find("READ") == 0) {
1333 write_bit(name.substr(0, name.size() - 1) + "B_18");
1334 }
1335 } else {
1336 write_bit(name + "_" + std::to_string(actual_width));
1337 }
1338 }
1339
1340 void write_bram_init(int half, CellInfo *ci, bool is_36)
1341 {
1342 for (std::string mode : {"", "P"}) {
1343 for (int i = 0; i < (mode == "P" ? 8 : 64); i++) {
1344 bool has_init = false;
1345 std::vector<bool> init_data(256, false);
1346 if (is_36) {
1347 for (int j = 0; j < 2; j++) {
1348 IdString param = ctx->idf("INIT%s_%02X", mode.c_str(), i * 2 + j);
1349 if (ci->params.count(param)) {
1350 auto &init0 = ci->params.at(param);
1351 has_init = true;
1352 for (int k = half; k < 256; k += 2) {
1353 if (k >= int(init0.str.size()))
1354 break;
1355 init_data[j * 128 + (k / 2)] = init0.str[k] == Property::S1;
1356 }
1357 }
1358 }
1359 } else {
1360 IdString param = ctx->idf("INIT%s_%02X", mode.c_str(), i);
1361 if (ci->params.count(param)) {
1362 auto &init = ci->params.at(param);
1363 has_init = true;
1364 for (int k = 0; k < 256; k++) {
1365 if (k >= int(init.str.size()))
1366 break;
1367 init_data[k] = init.str[k] == Property::S1;
1368 }
1369 }
1370 }
1371 if (has_init)
1372 write_vector(stringf("INIT%s_%02X[255:0]", mode.c_str(), i), init_data);
1373 }
1374 }
1375 }
1376
1377 void write_bram_half(int tile, int half, CellInfo *ci)
1378 {
1379 push(uarch->tile_name(tile));
1380 push("RAMB18_Y" + std::to_string(half));
1381 if (ci != nullptr) {
1382 bool is_36 = ci->type == id_RAMB36E1_RAMB36E1;
1383 write_bit("IN_USE");
1384 write_bram_width(ci, "READ_WIDTH_A", is_36, half == 1);
1385 write_bram_width(ci, "READ_WIDTH_B", is_36, half == 1);
1386 write_bram_width(ci, "WRITE_WIDTH_A", is_36, half == 1);
1387 write_bram_width(ci, "WRITE_WIDTH_B", is_36, half == 1);
1388 write_bit("DOA_REG", bool_or_default(ci->params, id_DOA_REG, false));
1389 write_bit("DOB_REG", bool_or_default(ci->params, id_DOB_REG, false));
1390 for (auto &invpin : invertible_pins[ctx->id(ci->attrs[id_X_ORIG_TYPE].as_string())])
1391 write_bit("ZINV_" + invpin.str(ctx),
1392 !bool_or_default(ci->params, ctx->id("IS_" + invpin.str(ctx) + "_INVERTED"), false));
1393 for (auto wrmode : {"WRITE_MODE_A", "WRITE_MODE_B"}) {
1394 std::string mode = str_or_default(ci->params, ctx->id(wrmode), "WRITE_FIRST");
1395 if (mode != "WRITE_FIRST")
1396 write_bit(std::string(wrmode) + "_" + mode);
1397 }
1398 write_vector("ZINIT_A[17:0]", std::vector<bool>(18, true));
1399 write_vector("ZINIT_B[17:0]", std::vector<bool>(18, true));
1400 write_vector("ZSRVAL_A[17:0]", std::vector<bool>(18, true));
1401 write_vector("ZSRVAL_B[17:0]", std::vector<bool>(18, true));
1402
1403 write_bram_init(half, ci, is_36);
1404 }
1405 pop();
1406 if (half == 0) {
1407 auto used_rdaddrcasc = used_wires_starting_with(tile, "BRAM_CASCOUT_ADDRARDADDR", false);
1408 auto used_wraddrcasc = used_wires_starting_with(tile, "BRAM_CASCOUT_ADDRBWRADDR", false);
1409 write_bit("CASCOUT_ARD_ACTIVE", !used_rdaddrcasc.empty());
1410 write_bit("CASCOUT_BWR_ACTIVE", !used_wraddrcasc.empty());
1411 }
1412 pop();
1413 }
1414
1415 void write_bram()
1416 {
1417 for (int tile = 0; tile < ctx->chip_info->tile_insts.ssize(); tile++) {
1418 IdString type = ctx->get_tile_type(tile);
1419 if (type.in(id_BRAM_L, id_BRAM_R)) {
1420 CellInfo *l = nullptr, *u = nullptr;
1421 const auto &bts = uarch->tile_status[tile].bts;
1422 if (bts) {
1423 if (bts->cells[BEL_RAM36] != nullptr) {
1424 l = bts->cells[BEL_RAM36];
1425 u = bts->cells[BEL_RAM36];
1426 } else {
1427 l = bts->cells[BEL_RAM18_L];
1428 u = bts->cells[BEL_RAM18_U];
1429 }
1430 }
1431 write_bram_half(tile, 0, l);
1432 write_bram_half(tile, 1, u);
1433 blank();
1434 }
1435 }
1436 }
1437
1438 double float_or_default(CellInfo *ci, const std::string &name, double def)
1439 {
1440 IdString p = ctx->id(name);
1441 if (!ci->params.count(p))
1442 return def;
1443 auto &prop = ci->params.at(p);
1444 if (prop.is_string)
1445 return std::stod(prop.as_string());
1446 else
1447 return prop.as_int64();
1448 }
1449
1450 void write_pll_clkout(const std::string &name, CellInfo *ci)
1451 {
1452 // FIXME: variable duty cycle
1453 int high = 1, low = 1, phasemux = 0, delaytime = 0, frac = 0;
1454 bool no_count = false, edge = false;
1455 double divide = float_or_default(ci, name + ((name == "CLKFBOUT") ? "_MULT" : "_DIVIDE"), 1);
1456 double phase = float_or_default(ci, name + "_PHASE", 1);
1457 if (divide <= 1) {
1458 no_count = true;
1459 } else {
1460 high = floor(divide / 2);
1461 low = int(floor(divide) - high);
1462 if (high != low)
1463 edge = true;
1464 if (name == "CLKOUT1" || name == "CLKFBOUT")
1465 frac = floor(divide * 8) - floor(divide) * 8;
1466 int phase_eights = floor((phase / 360) * divide * 8);
1467 phasemux = phase_eights % 8;
1468 delaytime = phase_eights / 8;
1469 }
1470 bool used = false;
1471 if (name == "DIVCLK" || name == "CLKFBOUT") {
1472 used = true;
1473 } else {
1474 used = ci->getPort(ctx->id(name)) != nullptr;
1475 }
1476 if (name == "DIVCLK") {
1477 write_int_vector("DIVCLK_DIVCLK_HIGH_TIME[5:0]", high, 6);
1478 write_int_vector("DIVCLK_DIVCLK_LOW_TIME[5:0]", low, 6);
1479 write_bit("DIVCLK_DIVCLK_EDGE[0]", edge);
1480 write_bit("DIVCLK_DIVCLK_NO_COUNT[0]", no_count);
1481 } else if (used) {
1482 write_bit(name + "_CLKOUT1_OUTPUT_ENABLE[0]");
1483 write_int_vector(name + "_CLKOUT1_HIGH_TIME[5:0]", high, 6);
1484 write_int_vector(name + "_CLKOUT1_LOW_TIME[5:0]", low, 6);
1485 write_int_vector(name + "_CLKOUT1_PHASE_MUX[2:0]", phasemux, 3);
1486 write_bit(name + "_CLKOUT2_EDGE[0]", edge);
1487 write_bit(name + "_CLKOUT2_NO_COUNT[0]", no_count);
1488 write_int_vector(name + "_CLKOUT2_DELAY_TIME[5:0]", delaytime, 6);
1489 if (frac != 0) {
1490 write_bit(name + "_CLKOUT2_FRAC_EN[0]", edge);
1491 write_int_vector(name + "_CLKOUT2_FRAC[2:0]", frac, 3);
1492 }
1493 }
1494 }
1495
1496 void write_pll(CellInfo *ci)
1497 {
1498 push(uarch->tile_name(ci->bel.tile));
1499 push("PLLE2_ADV");
1500 write_bit("IN_USE");
1501 // FIXME: should be INV not ZINV (XRay error?)
1502 write_bit("ZINV_PWRDWN", bool_or_default(ci->params, id_IS_PWRDWN_INVERTED, false));
1503 write_bit("ZINV_RST", bool_or_default(ci->params, id_IS_RST_INVERTED, false));
1504 write_bit("INV_CLKINSEL", bool_or_default(ci->params, id_IS_CLKINSEL_INVERTED, false));
1505 write_pll_clkout("DIVCLK", ci);
1506 write_pll_clkout("CLKFBOUT", ci);
1507 write_pll_clkout("CLKOUT0", ci);
1508 write_pll_clkout("CLKOUT1", ci);
1509 write_pll_clkout("CLKOUT2", ci);
1510 write_pll_clkout("CLKOUT3", ci);
1511 write_pll_clkout("CLKOUT4", ci);
1512 write_pll_clkout("CLKOUT5", ci);
1513
1514 std::string comp = str_or_default(ci->params, id_COMPENSATION, "INTERNAL");
1515 push("COMPENSATION");
1516 if (comp == "INTERNAL") {
1517 // write_bit("INTERNAL");
1518 write_bit("Z_ZHOLD_OR_CLKIN_BUF");
1519 } else {
1520 NPNR_ASSERT_FALSE("unsupported compensation type");
1521 }
1522 pop();
1523
1524 // FIXME: should these be calculated somehow?
1525 write_int_vector("FILTREG1_RESERVED[11:0]", 0x8, 12);
1526 write_int_vector("LKTABLE[39:0]", 0xB5BE8FA401ULL, 40);
1527 write_bit("LOCKREG3_RESERVED[0]");
1528 write_int_vector("TABLE[9:0]", 0x3B4, 10);
1529 pop(2);
1530 }
1531
1532 void write_mmcm_clkout(const std::string &name, CellInfo *ci)
1533 {
1534 // FIXME: variable duty cycle
1535 int high = 1, low = 1, phasemux = 0, delaytime = 0, frac = 0;
1536 bool no_count = false, edge = false;
1537 double divide = float_or_default(
1538 ci, name + ((name == "CLKFBOUT") ? "_MULT_F" : (name == "CLKOUT0" ? "_DIVIDE_F" : "_DIVIDE")), 1);
1539 double phase = float_or_default(ci, name + "_PHASE", 1);
1540 if (divide <= 1) {
1541 no_count = true;
1542 } else {
1543 high = floor(divide / 2);
1544 low = int(floor(divide) - high);
1545 if (high != low)
1546 edge = true;
1547 if (name == "CLKOUT0" || name == "CLKFBOUT")
1548 frac = floor(divide * 8) - floor(divide) * 8;
1549 int phase_eights = floor((phase / 360) * divide * 8);
1550 phasemux = phase_eights % 8;
1551 delaytime = phase_eights / 8;
1552 }
1553 bool used = false;
1554 if (name == "DIVCLK" || name == "CLKFBOUT") {
1555 used = true;
1556 } else {
1557 used = ci->getPort(ctx->id(name)) != nullptr;
1558 }
1559 if (name == "DIVCLK") {
1560 write_int_vector("DIVCLK_DIVCLK_HIGH_TIME[5:0]", high, 6);
1561 write_int_vector("DIVCLK_DIVCLK_LOW_TIME[5:0]", low, 6);
1562 write_bit("DIVCLK_DIVCLK_EDGE[0]", edge);
1563 write_bit("DIVCLK_DIVCLK_NO_COUNT[0]", no_count);
1564 } else if (used) {
1565 auto is_clkout_5_or_6 = name == "CLKOUT5" || name == "CLKOUT6";
1566 auto is_clkout0 = name == "CLKOUT0";
1567 auto is_clkfbout = name == "CLKFBOUT";
1568
1569 if ((is_clkout0 || is_clkfbout) && frac != 0) {
1570 --high;
1571 --low;
1572
1573 auto frac_shifted = frac >> 1;
1574 // CLKOUT0 controls CLKOUT5_CLKOUT2, CLKFBOUT controls CLKOUT6_CLKOUT2
1575 std::string frac_conf_name = is_clkout0 ? "CLKOUT5_CLKOUT2_" : "CLKOUT6_CLKOUT2_";
1576
1577 if (1 <= frac_shifted) {
1578 write_bit(frac_conf_name + "FRACTIONAL_FRAC_WF_F[0]");
1579 write_int_vector(frac_conf_name + "FRACTIONAL_PHASE_MUX_F[1:0]", frac_shifted, 2);
1580 }
1581 }
1582
1583 write_bit(name + "_CLKOUT1_OUTPUT_ENABLE[0]");
1584 write_int_vector(name + "_CLKOUT1_HIGH_TIME[5:0]", high, 6);
1585 write_int_vector(name + "_CLKOUT1_LOW_TIME[5:0]", low, 6);
1586
1587 auto phase_mux_feature =
1588 name + (is_clkout_5_or_6 ? "_CLKOUT2_FRACTIONAL_PHASE_MUX_F[0]" : "_CLKOUT2_PHASE_MUX[0]");
1589 write_int_vector(name + "_CLKOUT1_PHASE_MUX[2:0]", phasemux, 3);
1590
1591 auto edge_feature = name + (is_clkout_5_or_6 ? "_CLKOUT2_FRACTIONAL_EDGE[0]" : "_CLKOUT2_EDGE[0]");
1592 write_bit(edge_feature, edge);
1593
1594 auto no_count_feature =
1595 name + (is_clkout_5_or_6 ? "_CLKOUT2_FRACTIONAL_NO_COUNT[0]" : "_CLKOUT2_NO_COUNT[0]");
1596 write_bit(no_count_feature, no_count);
1597
1598 auto delay_time_feature =
1599 name + (is_clkout_5_or_6 ? "_CLKOUT2_FRACTIONAL_DELAY_TIME[5:0]" : "_CLKOUT2_DELAY_TIME[5:0]");
1600 write_int_vector(delay_time_feature, delaytime, 6);
1601
1602 if (!is_clkout_5_or_6 && frac != 0) {
1603 write_bit(name + "_CLKOUT2_FRAC_EN[0]", 1);
1604 write_bit(name + "_CLKOUT2_FRAC_WF_R[0]", 1);
1605 write_int_vector(name + "_CLKOUT2_FRAC[2:0]", frac, 3);
1606 }
1607 }
1608 }
1609
1610 // From openXC7
1611 void write_mmcm(CellInfo *ci)
1612 {
1613 push(uarch->tile_name(ci->bel.tile));
1614 push("MMCME2_ADV");
1615 write_bit("IN_USE");
1616 // FIXME: should be INV not ZINV (XRay error?)
1617 write_bit("ZINV_PWRDWN", bool_or_default(ci->params, id_IS_PWRDWN_INVERTED, false));
1618 write_bit("ZINV_RST", bool_or_default(ci->params, id_IS_RST_INVERTED, false));
1619 write_bit("ZINV_PSEN", bool_or_default(ci->params, id_IS_PSEN_INVERTED, false));
1620 write_bit("ZINV_PSINCDEC", bool_or_default(ci->params, id_IS_PSINCDEC_INVERTED, false));
1621 write_bit("INV_CLKINSEL", bool_or_default(ci->params, id_IS_CLKINSEL_INVERTED, false));
1622 write_mmcm_clkout("DIVCLK", ci);
1623 write_mmcm_clkout("CLKFBOUT", ci);
1624 write_mmcm_clkout("CLKOUT0", ci);
1625 write_mmcm_clkout("CLKOUT1", ci);
1626 write_mmcm_clkout("CLKOUT2", ci);
1627 write_mmcm_clkout("CLKOUT3", ci);
1628 write_mmcm_clkout("CLKOUT4", ci);
1629 write_mmcm_clkout("CLKOUT5", ci);
1630 write_mmcm_clkout("CLKOUT6", ci);
1631
1632 std::string comp = str_or_default(ci->params, id_COMPENSATION, "INTERNAL");
1633 push("COMP");
1634 if (comp == "INTERNAL" || comp == "ZHOLD") {
1635 // does not seem to make a difference in vivado
1636 // both modes set this bit
1637 write_bit("Z_ZHOLD");
1638 } else {
1639 log_error("unsupported COMPENSATION type '%s' for MMCM (supported compensation types: INTERNAL, ZHOLD)\n",
1640 comp.c_str());
1641 }
1642 pop();
1643
1644 auto clkfbout_mult = (int)float_or_default(ci, "CLKFBOUT_MULT_F", 5.000);
1645 if (63 < clkfbout_mult)
1646 log_error("MMCME2_ADV: CLKFBOUT_MULT_F must not be greater than 63");
1647 if (0 == clkfbout_mult)
1648 log_error("MMCME2_ADV: CLKFBOUT_MULT_F must not be 0");
1649 write_int_vector("LKTABLE[39:0]", Xc7MMCM::lk_table[clkfbout_mult - 1], 40);
1650
1651 std::string bandwidth = str_or_default(ci->params, id_BANDWIDTH, "OPTIMIZED");
1652 const uint16_t *filter_lookup;
1653 if (bandwidth == "LOW")
1654 filter_lookup = Xc7MMCM::filter_lookup_low;
1655 else if (bandwidth == "LOW_SS")
1656 filter_lookup = Xc7MMCM::filter_lookup_low_ss;
1657 else if (bandwidth == "HIGH")
1658 filter_lookup = Xc7MMCM::filter_lookup_high;
1659 else
1660 filter_lookup = Xc7MMCM::filter_lookup_optimized;
1661 write_int_vector("FILTREG1_RESERVED[11:0]", filter_lookup[clkfbout_mult - 1], 12);
1662
1663 // 0x9900 enables fractional counters
1664 // only int counters would be 0x1 << 8
1665 // 0xffff enables everything, I suppose, this is what is used in xap888
1666 write_int_vector("POWER_REG_POWER_REG_POWER_REG[15:0]", 0xffff, 16);
1667 write_bit("LOCKREG3_RESERVED[0]");
1668 write_int_vector("TABLE[9:0]", 0x3d4, 10);
1669 pop(2);
1670 }
1671 void write_dsp_cell(CellInfo *ci)
1672 {
1673 auto tile_name = uarch->tile_name(ci->bel.tile);
1674 auto tile_side = tile_name.at(4);
1675 push(tile_name);
1676 push("DSP48");
1677 auto xy = uarch->rel_site_loc(uarch->get_bel_site(ci->bel));
1678 auto dsp = stringf("DSP_%d", xy.y);
1679 push(dsp);
1680
1681 auto write_bus_zinv = [&](std::string name, int width) {
1682 for (int i = 0; i < width; i++) {
1683 std::string b = stringf("[%d]", i);
1684 bool inv = (int_or_default(ci->params, ctx->id("IS_" + name + "_INVERTED"), 0) >> i) & 0x1;
1685 inv |= bool_or_default(ci->params, ctx->id("IS_" + name + b + "_INVERTED"), false);
1686 write_bit("ZIS_" + name + "_INVERTED" + b, !inv);
1687 }
1688 };
1689
1690 // value 1 is equivalent to 2, according to UG479
1691 // but in real life, Vivado sets AREG_0 is 0,
1692 // no bit is 1, and AREG_2 is 2
1693 auto areg = int_or_default(ci->params, ctx->id("AREG"), 1);
1694 if (areg == 0 or areg == 2)
1695 write_bit("AREG_" + std::to_string(areg));
1696
1697 auto ainput = str_or_default(ci->params, ctx->id("A_INPUT"), "DIRECT");
1698 if (ainput == "CASCADE")
1699 write_bit("A_INPUT[0]");
1700
1701 // value 1 is equivalent to 2, according to UG479
1702 // but in real life, Vivado sets AREG_0 is 0,
1703 // no bit is 1, and AREG_2 is 2
1704 auto breg = int_or_default(ci->params, ctx->id("BREG"), 1);
1705 if (breg == 0 or breg == 2)
1706 write_bit("BREG_" + std::to_string(breg));
1707
1708 auto binput = str_or_default(ci->params, ctx->id("B_INPUT"), "DIRECT");
1709 if (binput == "CASCADE")
1710 write_bit("B_INPUT[0]");
1711
1712 // Tolerate both int and string types for interoperability purposes
1713 auto use_dport = boolstr_or_default(ci->params, ctx->id("USE_DPORT"), false);
1714 if (use_dport == true)
1715 write_bit("USE_DPORT[0]");
1716
1717 auto use_simd = str_or_default(ci->params, ctx->id("USE_SIMD"), "ONE48");
1718 if (use_simd == "TWO24")
1719 write_bit("USE_SIMD_FOUR12_TWO24");
1720 if (use_simd == "FOUR12")
1721 write_bit("USE_SIMD_FOUR12");
1722
1723 // PATTERN
1724 const size_t pattern_size = 48;
1725 std::vector<bool> pattern_vector(pattern_size, false);
1726 bool pattern_found = boolvec_populate(ci->params, ctx->id("PATTERN"), pattern_vector);
1727 if (pattern_found) {
1728 write_vector("PATTERN[47:0]", pattern_vector);
1729 }
1730
1731 auto autoreset_patdet = str_or_default(ci->params, ctx->id("AUTORESET_PATDET"), "NO_RESET");
1732 if (autoreset_patdet == "RESET_MATCH")
1733 write_bit("AUTORESET_PATDET_RESET");
1734 if (autoreset_patdet == "RESET_NOT_MATCH")
1735 write_bit("AUTORESET_PATDET_RESET_NOT_MATCH");
1736
1737 // MASK
1738 // Yosys gives us 48 bit, but prjxray only recognizes 46 bits
1739 // The most significant two bits seem to be zero, so let us just truncate them
1740 const size_t mask_size = 48;
1741 std::vector<bool> mask_vector(mask_size, true);
1742 bool mask_found = boolvec_populate(ci->params, ctx->id("MASK"), mask_vector);
1743 if (mask_found) {
1744 mask_vector.resize(46);
1745 write_vector("MASK[45:0]", mask_vector);
1746 }
1747
1748 auto sel_mask = str_or_default(ci->params, ctx->id("SEL_MASK"), "MASK");
1749 if (sel_mask == "C")
1750 write_bit("SEL_MASK_C");
1751 if (sel_mask == "ROUNDING_MODE1")
1752 write_bit("SEL_MASK_ROUNDING_MODE1");
1753 if (sel_mask == "ROUNDING_MODE2")
1754 write_bit("SEL_MASK_ROUNDING_MODE2");
1755
1756 write_bit("ZADREG[0]", !bool_or_default(ci->params, ctx->id("ADREG"), true));
1757 write_bit("ZALUMODEREG[0]", !bool_or_default(ci->params, ctx->id("ALUMODEREG")));
1758 write_bit("ZAREG_2_ACASCREG_1", !bool_or_default(ci->params, ctx->id("ACASCREG")));
1759 write_bit("ZBREG_2_BCASCREG_1", !bool_or_default(ci->params, ctx->id("BCASCREG")));
1760 write_bit("ZCARRYINREG[0]", !bool_or_default(ci->params, ctx->id("CARRYINREG")));
1761 write_bit("ZCARRYINSELREG[0]", !bool_or_default(ci->params, ctx->id("CARRYINSELREG")));
1762 write_bit("ZCREG[0]", !bool_or_default(ci->params, ctx->id("CREG"), true));
1763 write_bit("ZDREG[0]", !bool_or_default(ci->params, ctx->id("DREG"), true));
1764 write_bit("ZINMODEREG[0]", !bool_or_default(ci->params, ctx->id("INMODEREG")));
1765 write_bus_zinv("ALUMODE", 4);
1766 write_bus_zinv("INMODE", 5);
1767 write_bus_zinv("OPMODE", 7);
1768 write_bit("ZMREG[0]", !bool_or_default(ci->params, ctx->id("MREG")));
1769 write_bit("ZOPMODEREG[0]", !bool_or_default(ci->params, ctx->id("OPMODEREG")));
1770 write_bit("ZPREG[0]", !bool_or_default(ci->params, ctx->id("PREG")));
1771 write_bit("USE_DPORT[0]", boolstr_or_default(ci->params, ctx->id("USE_DPORT"), false));
1772 write_bit("ZIS_CLK_INVERTED", !bool_or_default(ci->params, ctx->id("IS_CLK_INVERTED")));
1773 write_bit("ZIS_CARRYIN_INVERTED", !bool_or_default(ci->params, ctx->id("IS_CARRYIN_INVERTED")));
1774 pop(2);
1775
1776 auto write_const_pins = [&](std::string const_net_name) {
1777 std::vector<std::string> pins;
1778 const auto attr_name = "DSP_" + const_net_name + "_PINS";
1779 const auto attr_value = str_or_default(ci->attrs, ctx->id(attr_name), "");
1780 boost::split(pins, attr_value, boost::is_any_of(" "));
1781 for (auto pin : pins) {
1782 if (boost::empty(pin))
1783 continue;
1784 auto pin_basename = pin;
1785 boost::erase_all(pin_basename, "0123456789");
1786 auto inv = bool_or_default(ci->params, ctx->id("IS_" + pin_basename + "_INVERTED"), 0);
1787 auto net_name = inv ? (const_net_name == "GND" ? "VCC" : "GND") : const_net_name;
1788 write_bit(stringf("%s_%s.DSP_%s_%c", dsp.c_str(), pin.c_str(), net_name.c_str(), tile_side));
1789 }
1790 };
1791
1792 write_const_pins("GND");
1793 write_const_pins("VCC");
1794
1795 pop();
1796 }
1797
1798 void write_ip()
1799 {
1800 for (auto &cell : ctx->cells) {
1801 CellInfo *ci = cell.second.get();
1802 if (ci->type == id_DSP48E1_DSP48E1) {
1803 write_dsp_cell(ci);
1804 blank();
1805 }
1806 }
1807 }
1808
1809 void write_fasm()
1810 {
1811 get_invertible_pins(ctx, invertible_pins);
1812 write_logic();
1813 write_io();
1814 write_routing();
1815 write_bram();
1816 write_clocking();
1817 write_ip();
1818 }
1819 };
1820
1821 } // namespace
1822
1823 void XilinxImpl::write_fasm(const std::string &filename)
1824 {
1825 std::ofstream out(filename);
1826 if (!out)
1827 log_error("failed to open file %s for writing (%s)\n", filename.c_str(), strerror(errno));
1828
1829 FasmBackend be(this->ctx, this, out);
1830 be.write_fasm();
1831 }
1832
1833 NEXTPNR_NAMESPACE_END