Debian Codegraph @

himbaechel/uarch/gowin/pack_bsram.cc

raw
1 #include "design_utils.h"
2 #include "log.h"
3 #include "nextpnr.h"
4
5 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
6 #include "himbaechel_constids.h"
7 #include "himbaechel_helpers.h"
8
9 #include "gowin.h"
10 #include "gowin_utils.h"
11 #include "pack.h"
12
13 #include <cinttypes>
14
15 NEXTPNR_NAMESPACE_BEGIN
16
17 // ===================================
18 // Block RAM
19 // ===================================
20 void GowinPacker::bsram_rename_ports(CellInfo *ci, int bit_width, char const *from, char const *to, int offset)
21 {
22 int num = (bit_width == 9 || bit_width == 18 || bit_width == 36) ? 36 : 32;
23 for (int i = 0, j = offset; i < num; ++i, ++j) {
24 if (((i + 1) % 9) == 0 && (bit_width == 16 || bit_width == 32)) {
25 ++j;
26 }
27 ci->renamePort(ctx->idf(from, i), ctx->idf(to, offset ? j % 36 : j));
28 }
29 }
30
31 // We solve the BLKSEL problems that are observed on some chips by
32 // connecting the BLKSEL ports to constant networks so that this BSRAM will
33 // be selected, the actual selection is made by manipulating the Clock
34 // Enable pin using a LUT-based decoder.
35 void GowinPacker::bsram_fix_blksel(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
36 {
37 // is BSRAM enabled
38 NetInfo *ce_net = ci->getPort(id_CE);
39 if (ce_net == nullptr || ce_net->name == ctx->id("$PACKER_GND")) {
40 return;
41 }
42
43 // port name, BLK_SEL parameter for this port
44 std::vector<std::pair<IdString, int>> dyn_blksel;
45
46 int blk_sel_parameter = ci->params.at(id_BLK_SEL).as_int64();
47 for (int i = 0; i < 3; ++i) {
48 IdString pin_name = ctx->idf("BLKSEL[%d]", i);
49 NetInfo *net = ci->getPort(pin_name);
50 if (net == nullptr || net->name == ctx->id("$PACKER_GND") || net->name == ctx->id("$PACKER_VCC")) {
51 continue;
52 }
53 dyn_blksel.push_back(std::make_pair(pin_name, (blk_sel_parameter >> i) & 1));
54 }
55
56 if (dyn_blksel.empty()) {
57 return;
58 }
59
60 if (ctx->verbose) {
61 log_info(" apply the BSRAM BLKSEL fix\n");
62 }
63
64 // Make a decoder
65 auto lut_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_blksel_lut$"), id_LUT4);
66 CellInfo *lut = lut_cell.get();
67 lut->addInput(id_I3);
68 ci->movePortTo(id_CE, lut, id_I3);
69 lut->addOutput(id_F);
70 ci->connectPorts(id_CE, lut, id_F);
71
72 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
73 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
74
75 // Connected CE to I3 to make it easy to calculate the decoder
76 int init = 0x100; // CE == 0 --> F = 0
77 // CE == 1 --> F = decoder result
78 int idx = 0;
79 for (auto &port : dyn_blksel) {
80 IdString lut_input_name = ctx->idf("I%d", idx);
81 ci->movePortTo(port.first, lut, lut_input_name);
82 if (port.second) {
83 init <<= (1 << idx);
84 ci->connectPort(port.first, vcc_net);
85 } else {
86 ci->connectPort(port.first, vss_net);
87 }
88 ++idx;
89 }
90 lut->setParam(id_INIT, init);
91
92 new_cells.push_back(std::move(lut_cell));
93 }
94
95 // Some chips cannot, for some reason, use internal BSRAM registers to
96 // implement READ_MODE=1'b1 (pipeline) with a word width other than 32 or
97 // 36 bits.
98 // We work around this by adding an external DFF and using BSRAM
99 // as READ_MODE=1'b0 (bypass).
100 void GowinPacker::bsram_fix_outreg(CellInfo *ci, int bit_width, IdString ce_pin, IdString oce_pin, IdString clk_pin,
101 IdString reset_pin, IdString do_pin, IdString read_mode_param,
102 std::vector<std::unique_ptr<CellInfo>> &new_cells)
103 {
104 if (ci->params.at(read_mode_param).as_int64() == 0) {
105 return;
106 }
107 NetInfo *ce_net = ci->getPort(ce_pin);
108 NetInfo *oce_net = ci->getPort(oce_pin);
109 if (ce_net == nullptr || oce_net == nullptr) {
110 return;
111 }
112 if (ce_net->name == ctx->id("$PACKER_GND") || oce_net->name == ctx->id("$PACKER_GND")) {
113 return;
114 }
115
116 if (ctx->verbose) {
117 log_info(" apply the BSRAM OUTREG fix\n");
118 }
119 ci->setParam(read_mode_param, 0);
120 ci->disconnectPort(oce_pin);
121 ci->connectPort(oce_pin, ce_net);
122
123 NetInfo *reset_net = ci->getPort(reset_pin);
124 bool sync_reset = ci->params.at(id_RESET_MODE).as_string() == std::string("SYNC");
125 IdString dff_type = sync_reset ? id_DFFRE : id_DFFCE;
126 IdString reset_port = sync_reset ? id_RESET : id_CLEAR;
127
128 for (int i = 0; i < bit_width; ++i) {
129 IdString do_name = ctx->idf("%s[%d]", do_pin.c_str(ctx), i);
130 const NetInfo *net = ci->getPort(do_name);
131 if (net != nullptr) {
132 if (net->users.empty()) {
133 ci->disconnectPort(do_name);
134 continue;
135 }
136
137 // create DFF
138 auto cache_dff_cell = gwu.create_cell(gwu.create_aux_name(ci->name, i, "_cache_dff$"), dff_type);
139 CellInfo *cache_dff = cache_dff_cell.get();
140 cache_dff->addInput(id_CE);
141 cache_dff->connectPort(id_CE, oce_net);
142
143 cache_dff->addInput(reset_port);
144 cache_dff->connectPort(reset_port, reset_net);
145
146 ci->copyPortTo(clk_pin, cache_dff, id_CLK);
147
148 cache_dff->addOutput(id_Q);
149 ci->movePortTo(do_name, cache_dff, id_Q);
150
151 cache_dff->addInput(id_D);
152 ci->connectPorts(do_name, cache_dff, id_D);
153
154 new_cells.push_back(std::move(cache_dff_cell));
155 }
156 }
157 }
158
159 // Analysis of the images generated by the IDE showed that some components
160 // are being added at the input and output of the BSRAM. Two LUTs are
161 // added on the WRE and CE inputs (strangely, OCE is not affected), a pair
162 // of LUT-DFFs on each DO output, and one or two flipflops of different
163 // types in the auxiliary network.
164 // The semantics of these additions are unclear, but we can replicate this behavior.
165 // Fix BSRAM in single port mode.
166 void GowinPacker::bsram_fix_sp(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
167 {
168 int bit_width = ci->params.at(id_BIT_WIDTH).as_int64();
169
170 if (ctx->verbose) {
171 log_info(" apply the SP fix\n");
172 }
173 // create WRE LUT
174 auto wre_lut_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_wre_lut$"), id_LUT4);
175 CellInfo *wre_lut = wre_lut_cell.get();
176 wre_lut->setParam(id_INIT, 0x8888);
177 ci->movePortTo(id_CE, wre_lut, id_I0);
178 ci->movePortTo(id_WRE, wre_lut, id_I1);
179 wre_lut->addOutput(id_F);
180 ci->connectPorts(id_WRE, wre_lut, id_F);
181
182 // create CE LUT
183 auto ce_lut_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_ce_lut$"), id_LUT4);
184 CellInfo *ce_lut = ce_lut_cell.get();
185 ce_lut->setParam(id_INIT, 0xeeee);
186 wre_lut->copyPortTo(id_I0, ce_lut, id_I0);
187 wre_lut->copyPortTo(id_I1, ce_lut, id_I1);
188 ce_lut->addOutput(id_F);
189 ci->connectPorts(id_CE, ce_lut, id_F);
190
191 // create ce reg
192 int write_mode = ci->params.at(id_WRITE_MODE).as_int64();
193 IdString dff_type = write_mode ? id_DFF : id_DFFR;
194 auto ce_pre_dff_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_ce_pre_dff$"), dff_type);
195 CellInfo *ce_pre_dff = ce_pre_dff_cell.get();
196 ce_pre_dff->addInput(id_D);
197 ce_lut->copyPortTo(id_I0, ce_pre_dff, id_D);
198 ci->copyPortTo(id_CLK, ce_pre_dff, id_CLK);
199 if (dff_type == id_DFFR) {
200 wre_lut->copyPortTo(id_I1, ce_pre_dff, id_RESET);
201 }
202 ce_pre_dff->addOutput(id_Q);
203
204 // new ce src with Q pin (used by output pins, not by BSRAM itself)
205 CellInfo *new_ce_net_src = ce_pre_dff;
206
207 // add delay register in pipeline mode
208 int read_mode = ci->params.at(id_READ_MODE).as_int64();
209 if (read_mode) {
210 auto ce_pipe_dff_cell = gwu.create_cell(gwu.create_aux_name(ci->name, 0, "_ce_pipe_dff$"), id_DFF);
211 new_cells.push_back(std::move(ce_pipe_dff_cell));
212 CellInfo *ce_pipe_dff = new_cells.back().get();
213 ce_pipe_dff->addInput(id_D);
214 new_ce_net_src->connectPorts(id_Q, ce_pipe_dff, id_D);
215 ci->copyPortTo(id_CLK, ce_pipe_dff, id_CLK);
216 ce_pipe_dff->addOutput(id_Q);
217 new_ce_net_src = ce_pipe_dff;
218 }
219
220 // used outputs of the BSRAM convert to cached
221 for (int i = 0; i < bit_width; ++i) {
222 IdString do_name = ctx->idf("DO[%d]", i);
223 const NetInfo *net = ci->getPort(do_name);
224 if (net != nullptr) {
225 if (net->users.empty()) {
226 ci->disconnectPort(do_name);
227 continue;
228 }
229 // create cache lut
230 auto cache_lut_cell = gwu.create_cell(gwu.create_aux_name(ci->name, i, "_cache_lut$"), id_LUT4);
231 CellInfo *cache_lut = cache_lut_cell.get();
232 cache_lut->setParam(id_INIT, 0xcaca);
233 cache_lut->addInput(id_I0);
234 cache_lut->addInput(id_I1);
235 cache_lut->addInput(id_I2);
236 ci->movePortTo(do_name, cache_lut, id_F);
237 ci->connectPorts(do_name, cache_lut, id_I1);
238 new_ce_net_src->connectPorts(id_Q, cache_lut, id_I2);
239
240 // create cache DFF
241 auto cache_dff_cell = gwu.create_cell(gwu.create_aux_name(ci->name, i, "_cache_dff$"), id_DFFE);
242 CellInfo *cache_dff = cache_dff_cell.get();
243 cache_dff->addInput(id_CE);
244 cache_dff->addInput(id_D);
245 ci->copyPortTo(id_CLK, cache_dff, id_CLK);
246 new_ce_net_src->connectPorts(id_Q, cache_dff, id_CE);
247 cache_lut->copyPortTo(id_I1, cache_dff, id_D);
248 cache_dff->addOutput(id_Q);
249 cache_dff->connectPorts(id_Q, cache_lut, id_I0);
250
251 new_cells.push_back(std::move(cache_lut_cell));
252 new_cells.push_back(std::move(cache_dff_cell));
253 }
254 }
255
256 new_cells.push_back(std::move(wre_lut_cell));
257 new_cells.push_back(std::move(ce_lut_cell));
258 new_cells.push_back(std::move(ce_pre_dff_cell));
259 }
260
261 void GowinPacker::pack_ROM(CellInfo *ci)
262 {
263 int default_bw = 32;
264 // XXX use block 111
265 ci->setParam(ctx->id("BLK_SEL"), Property(7, 32));
266 if (ci->type == id_pROM) {
267 ci->setAttr(id_BSRAM_SUBTYPE, Property(""));
268 } else {
269 ci->setAttr(id_BSRAM_SUBTYPE, Property("X9"));
270 default_bw = 36;
271 }
272
273 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
274 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
275 for (int i = 0; i < 3; ++i) {
276 IdString port = ctx->idf("BLKSEL%d", i);
277 ci->addInput(port);
278 ci->connectPort(port, vcc_net);
279 port = ctx->idf("BLKSELB%d", i);
280 ci->addInput(port);
281 ci->connectPort(port, vcc_net);
282 }
283
284 ci->addInput(id_WRE);
285 ci->connectPort(id_WRE, vss_net);
286 ci->addInput(id_WREB);
287 ci->connectPort(id_WREB, vss_net);
288
289 if (!ci->params.count(id_BIT_WIDTH)) {
290 ci->setParam(id_BIT_WIDTH, Property(default_bw, 32));
291 }
292
293 int bit_width = ci->params.at(id_BIT_WIDTH).as_int64();
294 if (bit_width == 32 || bit_width == 36) {
295 ci->copyPortTo(id_CLK, ci, id_CLKB);
296 ci->copyPortTo(id_CE, ci, id_CEB);
297 ci->copyPortTo(id_OCE, ci, id_OCEB);
298 ci->copyPortTo(id_RESET, ci, id_RESETB);
299
300 for (int i = 0; i < 14; ++i) {
301 ci->renamePort(ctx->idf("AD[%d]", i), ctx->idf("ADA%d", i));
302 ci->copyPortTo(ctx->idf("ADA%d", i), ci, ctx->idf("ADB%d", i));
303 }
304 bsram_rename_ports(ci, bit_width, "DO[%d]", "DO%d");
305 } else {
306 // use port B
307 ci->renamePort(id_CLK, id_CLKB);
308 ci->renamePort(id_OCE, id_OCEB);
309 ci->renamePort(id_CE, id_CEB);
310 ci->renamePort(id_RESET, id_RESETB);
311
312 ci->addInput(id_CEA);
313 ci->connectPort(id_CEA, vss_net);
314 for (int i = 0; i < 14; ++i) {
315 ci->renamePort(ctx->idf("AD[%d]", i), ctx->idf("ADB%d", i));
316 }
317 bsram_rename_ports(ci, bit_width, "DO[%d]", "DO%d", 18);
318 }
319 }
320
321 void GowinPacker::divide_sdp(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
322 {
323 if (ctx->verbose) {
324 log_info(" divide SDP\n");
325 }
326
327 int bw = ci->params.at(id_BIT_WIDTH_0).as_int64();
328 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
329 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
330
331 IdString cell_type = bw == 32 ? id_SDPB : id_SDPX9B;
332 IdString name = ctx->idf("%s_AUX", ctx->nameOf(ci));
333
334 auto sdp_cell = gwu.create_cell(name, cell_type);
335 CellInfo *sdp = sdp_cell.get();
336 sdp->setAttr(id_AUX, 1);
337
338 int new_bw = bw / 2;
339 ci->setParam(id_BIT_WIDTH_0, new_bw);
340 ci->setParam(id_BIT_WIDTH_1, new_bw);
341 sdp->params = ci->params;
342 sdp->setParam(id_BIT_WIDTH_0, new_bw);
343 sdp->setParam(id_BIT_WIDTH_1, new_bw);
344
345 // copy control ports
346 ci->copyPortBusTo(ctx->id("BLKSELA"), 0, true, sdp, ctx->id("BLKSELA"), 0, true, 3);
347 ci->copyPortBusTo(ctx->id("BLKSELB"), 0, true, sdp, ctx->id("BLKSELB"), 0, true, 3);
348 ci->copyPortTo(id_CEA, sdp, id_CEA);
349 ci->copyPortTo(id_CEB, sdp, id_CEB);
350 ci->copyPortTo(id_CLKA, sdp, id_CLKA);
351 ci->copyPortTo(id_CLKB, sdp, id_CLKB);
352 ci->copyPortTo(id_OCE, sdp, id_OCE);
353 ci->copyPortTo(id_RESET, sdp, id_RESET);
354
355 // Separate port A
356 ci->movePortTo(ctx->id("ADA[2]"), sdp, ctx->id("ADA[0]"));
357 ci->movePortTo(ctx->id("ADA[3]"), sdp, ctx->id("ADA[1]"));
358
359 ci->addInput(ctx->id("ADA[2]"));
360 ci->addInput(ctx->id("ADA[3]"));
361 ci->connectPort(ctx->id("ADA[2]"), vss_net);
362 ci->connectPort(ctx->id("ADA[3]"), vss_net);
363
364 sdp->addInput(ctx->id("ADA[2]"));
365 sdp->addInput(ctx->id("ADA[3]"));
366 sdp->connectPort(ctx->id("ADA[2]"), vss_net);
367 sdp->connectPort(ctx->id("ADA[3]"), vss_net);
368
369 ci->disconnectPort(ctx->id("ADA[4]"));
370 ci->connectPort(ctx->id("ADA[4]"), vss_net);
371 sdp->addInput(ctx->id("ADA[4]"));
372 sdp->connectPort(ctx->id("ADA[4]"), vcc_net);
373
374 ci->copyPortBusTo(id_ADA, 5, true, sdp, id_ADA, 5, true, 9);
375
376 // Separate port B
377 for (int i = 0; i < 4; ++i) {
378 IdString port = ctx->idf("ADB[%d]", i);
379 ci->disconnectPort(port);
380 ci->connectPort(port, vss_net);
381 ci->copyPortTo(port, sdp, port);
382 }
383
384 ci->disconnectPort(ctx->id("ADB[4]"));
385 ci->connectPort(ctx->id("ADB[4]"), vss_net);
386 sdp->addInput(ctx->id("ADB[4]"));
387 sdp->connectPort(ctx->id("ADB[4]"), vcc_net);
388
389 ci->copyPortBusTo(id_ADB, 5, true, sdp, id_ADB, 5, true, 9);
390
391 ci->movePortBusTo(id_DI, new_bw, true, sdp, id_DI, 0, true, new_bw);
392 ci->movePortBusTo(id_DO, new_bw, true, sdp, id_DO, 0, true, new_bw);
393
394 new_cells.push_back(std::move(sdp_cell));
395 }
396
397 void GowinPacker::pack_SDPB(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
398 {
399 int default_bw = 32;
400 if (ci->type == id_SDPB) {
401 ci->setAttr(id_BSRAM_SUBTYPE, Property(""));
402 } else {
403 ci->setAttr(id_BSRAM_SUBTYPE, Property("X9"));
404 default_bw = 36;
405 }
406
407 if (!ci->params.count(id_BIT_WIDTH_0)) {
408 ci->setParam(id_BIT_WIDTH_0, Property(default_bw, 32));
409 }
410 if (!ci->params.count(id_BIT_WIDTH_1)) {
411 ci->setParam(id_BIT_WIDTH_1, Property(default_bw, 32));
412 }
413
414 int bit_width = ci->params.at(id_BIT_WIDTH_0).as_int64();
415
416 if ((bit_width == 32 || bit_width == 36) && gwu.need_SDP_fix()) {
417 int bit_width_b = ci->params.at(id_BIT_WIDTH_1).as_int64();
418 if (bit_width == bit_width_b) {
419 divide_sdp(ci, new_cells);
420 } else {
421 log_error("The fix for SDP when ports A and B have different bit widths has not yet been implemented. "
422 "Cell: '%s'\n",
423 ci->type.c_str(ctx));
424 }
425 }
426
427 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
428 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
429
430 ci->copyPortTo(id_OCE, ci, id_OCEB);
431
432 // If misconnected RESET
433 if (gwu.need_BSRAM_RESET_fix()) {
434 ci->renamePort(id_RESET, id_RESETB);
435 }
436
437 // Port A
438 ci->addInput(id_WREA);
439 ci->connectPort(id_WREA, vcc_net);
440
441 // Port B
442 ci->addInput(id_WREB);
443 bit_width = ci->params.at(id_BIT_WIDTH_1).as_int64();
444
445 if (bit_width == 32 || bit_width == 36) {
446 ci->connectPort(id_WREB, vcc_net);
447 bsram_rename_ports(ci, bit_width, "DO[%d]", "DO%d");
448 } else {
449 ci->connectPort(id_WREB, vss_net);
450 bsram_rename_ports(ci, bit_width, "DO[%d]", "DO%d", 18);
451 }
452 bsram_rename_ports(ci, bit_width, "DI[%d]", "DI%d");
453 gwu.remove_brackets(ci);
454 }
455
456 void GowinPacker::pack_DPB(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
457 {
458 int default_bw = 16;
459 if (ci->type == id_DPB) {
460 ci->setAttr(id_BSRAM_SUBTYPE, Property(""));
461 } else {
462 ci->setAttr(id_BSRAM_SUBTYPE, Property("X9"));
463 default_bw = 18;
464 }
465
466 if (!ci->params.count(id_BIT_WIDTH_0)) {
467 ci->setParam(id_BIT_WIDTH_0, Property(default_bw, 32));
468 }
469 int bit_width = ci->params.at(id_BIT_WIDTH_0).as_int64();
470 bsram_rename_ports(ci, bit_width, "DIA[%d]", "DIA%d");
471
472 if (bit_width < 16 && gwu.need_BSRAM_OUTREG_fix()) {
473 bsram_fix_outreg(ci, bit_width, id_CEA, id_OCEA, id_CLKA, id_RESETA, id_DOA, id_READ_MODE0, new_cells);
474 }
475 bsram_rename_ports(ci, bit_width, "DOA[%d]", "DOA%d");
476 // In BYPASS mode, the OCE signal is dictated by CE.
477 if (gwu.need_BSRAM_DP_CE_fix()) {
478 if (bit_width <= 9) {
479 ci->disconnectPort(id_OCEA);
480 ci->copyPortTo(id_CEA, ci, id_OCEA);
481 }
482 }
483 if (!ci->params.count(id_BIT_WIDTH_1)) {
484 ci->setParam(id_BIT_WIDTH_1, Property(default_bw, 32));
485 }
486 bit_width = ci->params.at(id_BIT_WIDTH_1).as_int64();
487 bsram_rename_ports(ci, bit_width, "DIB[%d]", "DIB%d");
488
489 if (bit_width < 16 && gwu.need_BSRAM_OUTREG_fix()) {
490 bsram_fix_outreg(ci, bit_width, id_CEB, id_OCEB, id_CLKB, id_RESETB, id_DOB, id_READ_MODE1, new_cells);
491 }
492 bsram_rename_ports(ci, bit_width, "DOB[%d]", "DOB%d");
493
494 // In BYPASS mode, the OCE signal is dictated by CE.
495 if (gwu.need_BSRAM_DP_CE_fix()) {
496 if (bit_width <= 9) {
497 ci->disconnectPort(id_OCEB);
498 ci->copyPortTo(id_CEB, ci, id_OCEB);
499 }
500 }
501
502 gwu.remove_brackets(ci);
503 }
504
505 void GowinPacker::divide_sp(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
506 {
507 if (ctx->verbose) {
508 log_info(" divide SP\n");
509 }
510
511 int bw = ci->params.at(id_BIT_WIDTH).as_int64();
512 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
513 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
514
515 IdString cell_type = bw == 32 ? id_SP : id_SPX9;
516 IdString name = ctx->idf("%s_AUX", ctx->nameOf(ci));
517
518 auto sp_cell = gwu.create_cell(name, cell_type);
519 CellInfo *sp = sp_cell.get();
520 sp->setAttr(id_AUX, 1);
521
522 ci->copyPortTo(id_CLK, sp, id_CLK);
523 ci->copyPortTo(id_OCE, sp, id_OCE);
524 ci->copyPortTo(id_CE, sp, id_CE);
525 ci->copyPortTo(id_RESET, sp, id_RESET);
526 ci->copyPortTo(id_WRE, sp, id_WRE);
527
528 // XXX Separate "byte enable" port
529 ci->movePortTo(ctx->id("AD[2]"), sp, ctx->id("AD[0]"));
530 ci->movePortTo(ctx->id("AD[3]"), sp, ctx->id("AD[1]"));
531 ci->connectPort(ctx->id("AD[2]"), vss_net);
532 ci->connectPort(ctx->id("AD[3]"), vss_net);
533
534 sp->addInput(ctx->id("AD[2]"));
535 sp->connectPort(ctx->id("AD[2]"), vss_net);
536 sp->addInput(ctx->id("AD[3]"));
537 sp->connectPort(ctx->id("AD[3]"), vss_net);
538
539 ci->disconnectPort(ctx->id("AD[4]"));
540 ci->connectPort(ctx->id("AD[4]"), vss_net);
541 sp->addInput(ctx->id("AD[4]"));
542 sp->connectPort(ctx->id("AD[4]"), vcc_net);
543
544 ci->copyPortBusTo(id_AD, 5, true, sp, id_AD, 5, true, 9);
545
546 sp->params = ci->params;
547
548 bw /= 2;
549 ci->setParam(id_BIT_WIDTH, Property(bw, 32));
550 sp->setParam(id_BIT_WIDTH, Property(bw, 32));
551 ci->movePortBusTo(id_DI, bw, true, sp, id_DI, 0, true, bw);
552 ci->movePortBusTo(id_DO, bw, true, sp, id_DO, 0, true, bw);
553
554 ci->copyPortBusTo(ctx->id("BLKSEL"), 0, true, sp, ctx->id("BLKSEL"), 0, true, 3);
555
556 new_cells.push_back(std::move(sp_cell));
557 }
558
559 void GowinPacker::pack_SP(CellInfo *ci, std::vector<std::unique_ptr<CellInfo>> &new_cells)
560 {
561 int default_bw = 32;
562 if (ci->type == id_SP) {
563 ci->setAttr(id_BSRAM_SUBTYPE, Property(""));
564 } else {
565 ci->setAttr(id_BSRAM_SUBTYPE, Property("X9"));
566 default_bw = 36;
567 }
568 if (!ci->params.count(id_BIT_WIDTH)) {
569 ci->setParam(id_BIT_WIDTH, Property(default_bw, 32));
570 }
571
572 int bit_width = ci->params.at(id_BIT_WIDTH).as_int64();
573
574 if (!ci->attrs.count(id_AUX)) {
575 // XXX strange WRE<->CE relations
576 // Gowin IDE adds two LUTs to the WRE and CE signals. The logic is
577 // unclear, but without them effects occur. Perhaps this is a
578 // correction of some BSRAM defects.
579 if (gwu.need_SP_fix()) {
580 bsram_fix_sp(ci, new_cells);
581 }
582
583 // Some chips have problems with BLKSEL ports
584 if (gwu.need_BLKSEL_fix()) {
585 bsram_fix_blksel(ci, new_cells);
586 }
587
588 // Some chips have faulty output registers
589 if (bit_width < 32 && gwu.need_BSRAM_OUTREG_fix()) {
590 bsram_fix_outreg(ci, bit_width, id_CE, id_OCE, id_CLK, id_RESET, id_DO, id_READ_MODE, new_cells);
591 }
592 }
593
594 // XXX UG285-1.3.6_E Gowin BSRAM & SSRAM User Guide:
595 // For GW1N-9/GW1NR-9/GW1NS-4 series, 32/36-bit SP/SPX9 is divided into two
596 // SP/SPX9s, which occupy two BSRAMs.
597 // So divide it here
598 if ((bit_width == 32 || bit_width == 36) && !gwu.has_SP32()) {
599 divide_sp(ci, new_cells);
600 bit_width = ci->params.at(id_BIT_WIDTH).as_int64();
601 }
602
603 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
604 NetInfo *gnd_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
605 for (int i = 0; i < 3; ++i) {
606 ci->renamePort(ctx->idf("BLKSEL[%d]", i), ctx->idf("BLKSEL%d", i));
607 if (bit_width == 32 || bit_width == 36) {
608 ci->copyPortTo(ctx->idf("BLKSEL%d", i), ci, ctx->idf("BLKSELB%d", i));
609 }
610 }
611
612 for (int i = 0; i < 14; ++i) {
613 ci->renamePort(ctx->idf("AD[%d]", i), ctx->idf("AD%d", i));
614 if (bit_width == 32 || bit_width == 36) {
615 // Since we are dividing 32/36 bits into two parts between
616 // ports A and B, the ‘Byte Enables’ require special
617 // separation.
618 if (i < 4) {
619 if (i > 1) {
620 ci->movePortTo(ctx->idf("AD%d", i), ci, ctx->idf("ADB%d", i - 2));
621 ci->connectPort(ctx->idf("AD%d", i), gnd_net);
622 ci->addInput(ctx->idf("ADB%d", i));
623 ci->connectPort(ctx->idf("ADB%d", i), gnd_net);
624 }
625 } else {
626 ci->copyPortTo(ctx->idf("AD%d", i), ci, ctx->idf("ADB%d", i));
627 }
628 }
629 }
630 if (bit_width == 32 || bit_width == 36) {
631 ci->copyPortTo(id_CLK, ci, id_CLKB);
632 ci->copyPortTo(id_OCE, ci, id_OCEB);
633 ci->copyPortTo(id_CE, ci, id_CEB);
634 ci->copyPortTo(id_RESET, ci, id_RESETB);
635 ci->copyPortTo(id_WRE, ci, id_WREB);
636 ci->disconnectPort(ctx->id("AD4"));
637 ci->connectPort(ctx->id("AD4"), gnd_net);
638 ci->disconnectPort(ctx->id("ADB4"));
639 ci->connectPort(ctx->id("ADB4"), vcc_net);
640 }
641 bsram_rename_ports(ci, bit_width, "DI[%d]", "DI%d");
642 bsram_rename_ports(ci, bit_width, "DO[%d]", "DO%d");
643 }
644
645 void GowinPacker::pack_bsram(void)
646 {
647 std::vector<std::unique_ptr<CellInfo>> new_cells;
648 log_info("Pack BSRAMs...\n");
649
650 auto do_bsram = [&](CellInfo *ci) {
651 if (ctx->verbose) {
652 log_info(" pack %s\n", ci->type.c_str(ctx));
653 }
654 switch (ci->type.hash()) {
655 case ID_pROMX9: /* fallthrough */
656 case ID_pROM:
657 pack_ROM(ci);
658 ci->type = id_ROM;
659 break;
660 case ID_SDPX9B: /* fallthrough */
661 case ID_SDPB:
662 pack_SDPB(ci, new_cells);
663 ci->type = id_SDP;
664 break;
665 case ID_DPX9B: /* fallthrough */
666 case ID_DPB:
667 pack_DPB(ci, new_cells);
668 ci->type = id_DP;
669 break;
670 case ID_SPX9: /* fallthrough */
671 case ID_SP:
672 pack_SP(ci, new_cells);
673 ci->type = id_SP;
674 break;
675 default:
676 log_error("Unsupported BSRAM type '%s'\n", ci->type.c_str(ctx));
677 }
678 };
679
680 for (auto &cell : ctx->cells) {
681 auto ci = cell.second.get();
682 if (is_bsram(ci)) {
683 do_bsram(ci);
684 }
685 }
686
687 // Process new cells. New cells should not generate more.
688 for (auto &cell : new_cells) {
689 auto ci = cell.get();
690 if (is_bsram(ci)) {
691 do_bsram(ci);
692 }
693 ctx->cells[cell->name] = std::move(cell);
694 }
695 }
696 NEXTPNR_NAMESPACE_END