| 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 |
// Constant nets |
| 19 |
// =================================== |
| 20 |
void GowinPacker::handle_constants(void) |
| 21 |
{ |
| 22 |
log_info("Create constant nets...\n"); |
| 23 |
const dict<IdString, Property> vcc_params; |
| 24 |
const dict<IdString, Property> gnd_params; |
| 25 |
h.replace_constants(CellTypePort(id_GOWIN_VCC, id_V), CellTypePort(id_GOWIN_GND, id_G), vcc_params, gnd_params); |
| 26 |
|
| 27 |
// disconnect the constant LUT inputs |
| 28 |
log_info("Modify LUTs...\n"); |
| 29 |
for (IdString netname : {ctx->id("$PACKER_GND"), ctx->id("$PACKER_VCC")}) { |
| 30 |
auto net = ctx->nets.find(netname); |
| 31 |
if (net == ctx->nets.end()) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
NetInfo *constnet = net->second.get(); |
| 35 |
constnet->constant_value = (constnet->name == ctx->id("$PACKER_GND")) ? id_VSS : id_VCC; |
| 36 |
for (auto user : constnet->users) { |
| 37 |
CellInfo *uc = user.cell; |
| 38 |
if (is_lut(uc) && (user.port.str(ctx).at(0) == 'I')) { |
| 39 |
if (ctx->debug) { |
| 40 |
log_info("%s user %s/%s\n", ctx->nameOf(constnet), ctx->nameOf(uc), user.port.c_str(ctx)); |
| 41 |
} |
| 42 |
|
| 43 |
auto it_param = uc->params.find(id_INIT); |
| 44 |
if (it_param == uc->params.end()) |
| 45 |
log_error("No initialization for lut found.\n"); |
| 46 |
|
| 47 |
int64_t uc_init = it_param->second.intval; |
| 48 |
int64_t mask = 0; |
| 49 |
uint8_t amt = 0; |
| 50 |
|
| 51 |
if (user.port == id_I0) { |
| 52 |
mask = 0x5555; |
| 53 |
amt = 1; |
| 54 |
} else if (user.port == id_I1) { |
| 55 |
mask = 0x3333; |
| 56 |
amt = 2; |
| 57 |
} else if (user.port == id_I2) { |
| 58 |
mask = 0x0F0F; |
| 59 |
amt = 4; |
| 60 |
} else if (user.port == id_I3) { |
| 61 |
mask = 0x00FF; |
| 62 |
amt = 8; |
| 63 |
} else { |
| 64 |
log_error("Port number invalid.\n"); |
| 65 |
} |
| 66 |
|
| 67 |
if ((constnet->name == ctx->id("$PACKER_GND"))) { |
| 68 |
uc_init = (uc_init & mask) | ((uc_init & mask) << amt); |
| 69 |
} else { |
| 70 |
uc_init = (uc_init & (mask << amt)) | ((uc_init & (mask << amt)) >> amt); |
| 71 |
} |
| 72 |
|
| 73 |
size_t uc_init_len = it_param->second.to_string().length(); |
| 74 |
uc_init &= (1LL << uc_init_len) - 1; |
| 75 |
|
| 76 |
if (ctx->verbose && it_param->second.intval != uc_init) |
| 77 |
log_info("%s lut config modified from 0x%" PRIX64 " to 0x%" PRIX64 "\n", ctx->nameOf(uc), |
| 78 |
it_param->second.intval, uc_init); |
| 79 |
|
| 80 |
it_param->second = Property(uc_init, uc_init_len); |
| 81 |
uc->disconnectPort(user.port); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// =================================== |
| 88 |
// Wideluts |
| 89 |
// =================================== |
| 90 |
void GowinPacker::pack_wideluts(void) |
| 91 |
{ |
| 92 |
log_info("Pack wide LUTs...\n"); |
| 93 |
// children's offsets |
| 94 |
struct _children |
| 95 |
{ |
| 96 |
IdString port; |
| 97 |
int dx, dz; |
| 98 |
} mux_inputs[4][2] = {{{id_I0, 1, -7}, {id_I1, 0, -7}}, |
| 99 |
{{id_I0, 0, 4}, {id_I1, 0, -4}}, |
| 100 |
{{id_I0, 0, 2}, {id_I1, 0, -2}}, |
| 101 |
{{id_I0, 0, -BelZ::MUX20_Z}, {id_I1, 0, 2 - BelZ::MUX20_Z}}}; |
| 102 |
typedef std::function<void(CellInfo &, CellInfo *, int, int)> recurse_func_t; |
| 103 |
recurse_func_t make_cluster = [&, this](CellInfo &ci_root, CellInfo *ci_cursor, int dx, int dz) { |
| 104 |
_children *inputs; |
| 105 |
if (is_lut(ci_cursor)) { |
| 106 |
return; |
| 107 |
} |
| 108 |
switch (ci_cursor->type.hash()) { |
| 109 |
case ID_MUX2_LUT8: |
| 110 |
inputs = mux_inputs[0]; |
| 111 |
break; |
| 112 |
case ID_MUX2_LUT7: |
| 113 |
inputs = mux_inputs[1]; |
| 114 |
break; |
| 115 |
case ID_MUX2_LUT6: |
| 116 |
inputs = mux_inputs[2]; |
| 117 |
break; |
| 118 |
case ID_MUX2_LUT5: |
| 119 |
inputs = mux_inputs[3]; |
| 120 |
break; |
| 121 |
default: |
| 122 |
log_error("Bad MUX2 node:%s\n", ctx->nameOf(ci_cursor)); |
| 123 |
} |
| 124 |
for (int i = 0; i < 2; ++i) { |
| 125 |
// input src |
| 126 |
NetInfo *in = ci_cursor->getPort(inputs[i].port); |
| 127 |
NPNR_ASSERT(in && in->driver.cell && in->driver.cell->cluster == ClusterId()); |
| 128 |
int child_dx = dx + inputs[i].dx; |
| 129 |
int child_dz = dz + inputs[i].dz; |
| 130 |
ci_root.constr_children.push_back(in->driver.cell); |
| 131 |
in->driver.cell->cluster = ci_root.name; |
| 132 |
in->driver.cell->constr_abs_z = false; |
| 133 |
in->driver.cell->constr_x = child_dx; |
| 134 |
in->driver.cell->constr_y = 0; |
| 135 |
in->driver.cell->constr_z = child_dz; |
| 136 |
make_cluster(ci_root, in->driver.cell, child_dx, child_dz); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
// look for MUX2 |
| 141 |
// MUX2_LUT8 create right away, collect others |
| 142 |
std::vector<IdString> muxes[3]; |
| 143 |
int packed[4] = {0, 0, 0, 0}; |
| 144 |
for (auto &cell : ctx->cells) { |
| 145 |
auto &ci = *cell.second; |
| 146 |
if (ci.cluster != ClusterId()) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
if (ci.type == id_MUX2_LUT8) { |
| 150 |
ci.cluster = ci.name; |
| 151 |
ci.constr_abs_z = false; |
| 152 |
make_cluster(ci, &ci, 0, 0); |
| 153 |
++packed[0]; |
| 154 |
continue; |
| 155 |
} |
| 156 |
if (ci.type.in(id_MUX2_LUT7, id_MUX2_LUT6, id_MUX2_LUT5)) { |
| 157 |
switch (ci.type.hash()) { |
| 158 |
case ID_MUX2_LUT7: |
| 159 |
muxes[0].push_back(cell.first); |
| 160 |
break; |
| 161 |
case ID_MUX2_LUT6: |
| 162 |
muxes[1].push_back(cell.first); |
| 163 |
break; |
| 164 |
default: // ID_MUX2_LUT5 |
| 165 |
muxes[2].push_back(cell.first); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
// create others |
| 171 |
for (int i = 0; i < 3; ++i) { |
| 172 |
for (IdString cell_name : muxes[i]) { |
| 173 |
auto &ci = *ctx->cells.at(cell_name); |
| 174 |
if (ci.cluster != ClusterId()) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
ci.cluster = ci.name; |
| 178 |
ci.constr_abs_z = false; |
| 179 |
make_cluster(ci, &ci, 0, 0); |
| 180 |
++packed[i + 1]; |
| 181 |
} |
| 182 |
} |
| 183 |
log_info("Packed MUX2_LUT8:%d, MUX2_LU7:%d, MUX2_LUT6:%d, MUX2_LUT5:%d\n", packed[0], packed[1], packed[2], |
| 184 |
packed[3]); |
| 185 |
} |
| 186 |
|
| 187 |
// =================================== |
| 188 |
// ALU |
| 189 |
// =================================== |
| 190 |
// create ALU CIN block |
| 191 |
std::unique_ptr<CellInfo> GowinPacker::alu_add_cin_block(Context *ctx, CellInfo *head, NetInfo *cin_net, |
| 192 |
bool cin_is_vcc, bool cin_is_gnd) |
| 193 |
{ |
| 194 |
std::string name = head->name.str(ctx) + "_HEAD_ALULC"; |
| 195 |
IdString name_id = ctx->id(name); |
| 196 |
|
| 197 |
NetInfo *cout_net = ctx->createNet(name_id); |
| 198 |
head->disconnectPort(id_CIN); |
| 199 |
head->connectPort(id_CIN, cout_net); |
| 200 |
|
| 201 |
auto cin_ci = std::make_unique<CellInfo>(ctx, name_id, id_ALU); |
| 202 |
cin_ci->addOutput(id_COUT); |
| 203 |
cin_ci->connectPort(id_COUT, cout_net); |
| 204 |
|
| 205 |
if (cin_is_gnd) { |
| 206 |
cin_ci->setParam(id_ALU_MODE, std::string("C2L")); |
| 207 |
cin_ci->addInput(id_I2); |
| 208 |
cin_ci->connectPort(id_I2, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 209 |
return cin_ci; |
| 210 |
} |
| 211 |
if (cin_is_vcc) { |
| 212 |
cin_ci->setParam(id_ALU_MODE, std::string("ONE2C")); |
| 213 |
cin_ci->addInput(id_I2); |
| 214 |
cin_ci->connectPort(id_I2, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 215 |
return cin_ci; |
| 216 |
} |
| 217 |
// CIN from logic |
| 218 |
cin_ci->addInput(id_I2); |
| 219 |
cin_ci->connectPort(id_I2, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 220 |
cin_ci->addInput(id_I0); |
| 221 |
cin_ci->connectPort(id_I0, cin_net); |
| 222 |
cin_ci->setParam(id_RAW_ALU_LUT, 0x505a); // 0101_0000_0101_1010 -> ignore I1 and I3, out carry = I0 |
| 223 |
cin_ci->setParam(id_CIN_NETTYPE, Property("LOGIC")); |
| 224 |
return cin_ci; |
| 225 |
} |
| 226 |
|
| 227 |
// create ALU COUT block |
| 228 |
std::unique_ptr<CellInfo> GowinPacker::alu_add_cout_block(Context *ctx, CellInfo *tail, NetInfo *cout_net) |
| 229 |
{ |
| 230 |
std::string name = tail->name.str(ctx) + "_TAIL_ALULC"; |
| 231 |
IdString name_id = ctx->id(name); |
| 232 |
|
| 233 |
NetInfo *cin_net = ctx->createNet(name_id); |
| 234 |
tail->disconnectPort(id_COUT); |
| 235 |
tail->connectPort(id_COUT, cin_net); |
| 236 |
|
| 237 |
auto cout_ci = std::make_unique<CellInfo>(ctx, name_id, id_ALU); |
| 238 |
cout_ci->addOutput(id_COUT); // may be needed for the ALU filler |
| 239 |
cout_ci->addInput(id_CIN); |
| 240 |
cout_ci->connectPort(id_CIN, cin_net); |
| 241 |
cout_ci->addOutput(id_SUM); |
| 242 |
cout_ci->connectPort(id_SUM, cout_net); |
| 243 |
cout_ci->addInput(id_I2); |
| 244 |
cout_ci->connectPort(id_I2, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 245 |
|
| 246 |
cout_ci->setParam(id_ALU_MODE, std::string("C2L")); |
| 247 |
return cout_ci; |
| 248 |
} |
| 249 |
|
| 250 |
// create ALU filler block |
| 251 |
std::unique_ptr<CellInfo> GowinPacker::alu_add_dummy_block(Context *ctx, CellInfo *tail) |
| 252 |
{ |
| 253 |
std::string name = tail->name.str(ctx) + "_DUMMY_ALULC"; |
| 254 |
IdString name_id = ctx->id(name); |
| 255 |
|
| 256 |
auto dummy_ci = std::make_unique<CellInfo>(ctx, name_id, id_ALU); |
| 257 |
dummy_ci->setParam(id_ALU_MODE, std::string("C2L")); |
| 258 |
return dummy_ci; |
| 259 |
} |
| 260 |
|
| 261 |
// optimize ALU wiring |
| 262 |
// A very simple ALU optimization: once we detect that one of the inputs is |
| 263 |
// a constant, we modify the main LUT that describes the ALU function so |
| 264 |
// that this primitive input is ignored, and then disconnect it from the |
| 265 |
// network, freeing up the PIP. |
| 266 |
// For example (unrealistic, since a real ALU LUT has a larger size and |
| 267 |
// service bits in the middle, etc.), the addition function of A and B when |
| 268 |
// A = 1 is converted from the general case (A isn't a constant and B isn't a |
| 269 |
// constant) to a special case: |
| 270 |
// 0110 -> 0011 |
| 271 |
void GowinPacker::optimize_alu_lut(CellInfo *ci, int mode) |
| 272 |
{ |
| 273 |
auto uni_shift = [&](unsigned int val, int amount) { |
| 274 |
if (amount < 0) { |
| 275 |
return val >> -amount; |
| 276 |
} |
| 277 |
return val << amount; |
| 278 |
}; |
| 279 |
|
| 280 |
IdString vcc_net_name = ctx->id("$PACKER_VCC"); |
| 281 |
IdString gnd_net_name = ctx->id("$PACKER_GND"); |
| 282 |
bool optimized = false; |
| 283 |
switch (mode) { |
| 284 |
case 2: { |
| 285 |
// ALU LUT for mode 2 is 0110_0000_1001_1010 for all chips |
| 286 |
// We will change this feature if the next |
| 287 |
// unreleased Gowin chip series changes this |
| 288 |
// representation. |
| 289 |
// If ADDSUB dynamically switches between + and -, |
| 290 |
// optimization is not possible. |
| 291 |
int possible_carry = 0b1100U; |
| 292 |
IdString inp_net_name = ci->getPort(id_I3)->name; |
| 293 |
if (inp_net_name != vcc_net_name && inp_net_name != gnd_net_name) { |
| 294 |
break; |
| 295 |
} |
| 296 |
if (inp_net_name == gnd_net_name) { |
| 297 |
possible_carry = 0b0011U; |
| 298 |
} |
| 299 |
unsigned int alu_lut = 0b0110000010011010U; |
| 300 |
for (int i = 0; i < 3; ++i) { |
| 301 |
if (i == 2) { |
| 302 |
break; |
| 303 |
} |
| 304 |
IdString inp_name = ctx->idf("I%d", i); |
| 305 |
inp_net_name = ci->getPort(inp_name)->name; |
| 306 |
if (inp_net_name == vcc_net_name || inp_net_name == gnd_net_name) { |
| 307 |
ci->disconnectPort(inp_name); |
| 308 |
optimized = true; |
| 309 |
|
| 310 |
// fix the carry |
| 311 |
if (i == 0) { |
| 312 |
if (inp_net_name == vcc_net_name) { |
| 313 |
alu_lut |= 0xfU; |
| 314 |
} else { |
| 315 |
alu_lut &= ~0xfU; |
| 316 |
alu_lut |= possible_carry; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// We rearrange bits to account for constant networks |
| 321 |
int bit_n = 4; |
| 322 |
int copy_dist = 1 << i; |
| 323 |
if (inp_net_name == vcc_net_name) { |
| 324 |
bit_n += copy_dist; |
| 325 |
copy_dist = -copy_dist; |
| 326 |
} |
| 327 |
for (int j = 0; j < 4; ++j) { |
| 328 |
alu_lut &= ~(1 << (bit_n + copy_dist)); |
| 329 |
alu_lut |= uni_shift(alu_lut & (1 << bit_n), copy_dist); |
| 330 |
switch (i) { |
| 331 |
case 0: // skip the service bits |
| 332 |
bit_n += j == 1 ? 5 : 1; |
| 333 |
break; |
| 334 |
case 1: // skip the service bits |
| 335 |
bit_n += j == 1 ? 6 : 0; |
| 336 |
break; |
| 337 |
default: |
| 338 |
break; |
| 339 |
} |
| 340 |
++bit_n; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
if (optimized) { |
| 345 |
ci->setParam(id_RAW_ALU_LUT, alu_lut); |
| 346 |
} |
| 347 |
} break; |
| 348 |
default: |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
// create ALU chain |
| 354 |
void GowinPacker::pack_alus(void) |
| 355 |
{ |
| 356 |
const CellTypePort cell_alu_cout = CellTypePort(id_ALU, id_COUT); |
| 357 |
const CellTypePort cell_alu_cin = CellTypePort(id_ALU, id_CIN); |
| 358 |
std::vector<std::unique_ptr<CellInfo>> new_cells; |
| 359 |
|
| 360 |
log_info("Pack ALUs...\n"); |
| 361 |
for (auto &cell : ctx->cells) { |
| 362 |
auto ci = cell.second.get(); |
| 363 |
if (ci->cluster != ClusterId()) { |
| 364 |
continue; |
| 365 |
} |
| 366 |
if (is_alu(ci)) { |
| 367 |
// The ALU head is when the input carry is not a dedicated wire from the previous ALU |
| 368 |
NetInfo *cin_net = ci->getPort(id_CIN); |
| 369 |
if (!cin_net || !cin_net->driver.cell) { |
| 370 |
log_error("CIN disconnected at ALU:%s\n", ctx->nameOf(ci)); |
| 371 |
} |
| 372 |
if (CellTypePort(cin_net->driver) != cell_alu_cout || cin_net->users.entries() > 1) { |
| 373 |
if (ctx->debug) { |
| 374 |
log_info("ALU head found %s. CIN net is %s\n", ctx->nameOf(ci), ctx->nameOf(cin_net)); |
| 375 |
} |
| 376 |
|
| 377 |
bool cin_is_vcc = cin_net->name == ctx->id("$PACKER_VCC"); |
| 378 |
bool cin_is_gnd = cin_net->name == ctx->id("$PACKER_GND"); |
| 379 |
bool cin_is_logic = !cin_is_vcc && !cin_is_gnd; |
| 380 |
CellInfo *cin_block_ci; |
| 381 |
int alu_chain_len; |
| 382 |
|
| 383 |
// According to the documentation, GW5A can use CIN from |
| 384 |
// logic using the input MUX, but in practice this has not |
| 385 |
// yet been achieved. We are leaving the old mechanism in |
| 386 |
// place for this case. |
| 387 |
if ((!gwu.has_CIN_MUX()) || cin_is_logic) { |
| 388 |
// prepend first ALU with carry generator block |
| 389 |
// three cases: CIN == 0, CIN == 1 and CIN == ? |
| 390 |
new_cells.push_back(alu_add_cin_block(ctx, ci, cin_net, cin_is_vcc, cin_is_gnd)); |
| 391 |
cin_block_ci = new_cells.back().get(); |
| 392 |
// CIN block is the cluster root and is always placed in ALU0 |
| 393 |
alu_chain_len = 1; |
| 394 |
} else { |
| 395 |
cin_block_ci = ci; |
| 396 |
ci->disconnectPort(id_CIN); |
| 397 |
if (cin_is_vcc) { |
| 398 |
ci->setParam(id_CIN_NETTYPE, Property("VCC")); |
| 399 |
} else { |
| 400 |
ci->setParam(id_CIN_NETTYPE, Property("GND")); |
| 401 |
} |
| 402 |
alu_chain_len = 0; |
| 403 |
} |
| 404 |
cin_block_ci->cluster = cin_block_ci->name; |
| 405 |
cin_block_ci->constr_z = BelZ::ALU0_Z; |
| 406 |
cin_block_ci->constr_abs_z = true; |
| 407 |
|
| 408 |
while (true) { |
| 409 |
if (ci != cin_block_ci) { |
| 410 |
// add to cluster |
| 411 |
if (ctx->debug) { |
| 412 |
log_info("Add ALU to the chain (len:%d): %s\n", alu_chain_len, ctx->nameOf(ci)); |
| 413 |
} |
| 414 |
cin_block_ci->constr_children.push_back(ci); |
| 415 |
NPNR_ASSERT(ci->cluster == ClusterId()); |
| 416 |
ci->cluster = cin_block_ci->name; |
| 417 |
ci->constr_abs_z = false; |
| 418 |
ci->constr_x = alu_chain_len / 6; |
| 419 |
ci->constr_y = 0; |
| 420 |
ci->constr_z = alu_chain_len % 6; |
| 421 |
} |
| 422 |
// optimize only MODE=2 for now |
| 423 |
if (ci->params.at(id_ALU_MODE).as_int64() == 2) { |
| 424 |
optimize_alu_lut(ci, 2); |
| 425 |
} |
| 426 |
// XXX I2 is pin C which must be set to 1 for all ALU modes except MUL |
| 427 |
// we use only mode 2 ADDSUB so create and connect this pin |
| 428 |
ci->addInput(id_I2); |
| 429 |
ci->connectPort(id_I2, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 430 |
|
| 431 |
++alu_chain_len; |
| 432 |
|
| 433 |
// check for the chain end |
| 434 |
NetInfo *cout_net = ci->getPort(id_COUT); |
| 435 |
if (!cout_net || cout_net->users.empty()) { |
| 436 |
break; |
| 437 |
} |
| 438 |
if (CellTypePort(*cout_net->users.begin()) != cell_alu_cin || cout_net->users.entries() > 1) { |
| 439 |
new_cells.push_back(alu_add_cout_block(ctx, ci, cout_net)); |
| 440 |
CellInfo *cout_block_ci = new_cells.back().get(); |
| 441 |
cin_block_ci->constr_children.push_back(cout_block_ci); |
| 442 |
NPNR_ASSERT(cout_block_ci->cluster == ClusterId()); |
| 443 |
cout_block_ci->cluster = cin_block_ci->name; |
| 444 |
cout_block_ci->constr_abs_z = false; |
| 445 |
cout_block_ci->constr_x = alu_chain_len / 6; |
| 446 |
cout_block_ci->constr_y = 0; |
| 447 |
cout_block_ci->constr_z = alu_chain_len % 6; |
| 448 |
if (ctx->debug) { |
| 449 |
log_info("Add ALU carry out to the chain (len:%d): %s COUT-net: %s\n", alu_chain_len, |
| 450 |
ctx->nameOf(cout_block_ci), ctx->nameOf(cout_net)); |
| 451 |
} |
| 452 |
|
| 453 |
++alu_chain_len; |
| 454 |
|
| 455 |
break; |
| 456 |
} |
| 457 |
ci = (*cout_net->users.begin()).cell; |
| 458 |
} |
| 459 |
// ALUs are always paired |
| 460 |
if (alu_chain_len & 1) { |
| 461 |
// create dummy cell |
| 462 |
new_cells.push_back(alu_add_dummy_block(ctx, ci)); |
| 463 |
CellInfo *dummy_block_ci = new_cells.back().get(); |
| 464 |
cin_block_ci->constr_children.push_back(dummy_block_ci); |
| 465 |
NPNR_ASSERT(dummy_block_ci->cluster == ClusterId()); |
| 466 |
dummy_block_ci->cluster = cin_block_ci->name; |
| 467 |
dummy_block_ci->constr_abs_z = false; |
| 468 |
dummy_block_ci->constr_x = alu_chain_len / 6; |
| 469 |
dummy_block_ci->constr_y = 0; |
| 470 |
dummy_block_ci->constr_z = alu_chain_len % 6; |
| 471 |
if (ctx->debug) { |
| 472 |
log_info("Add ALU dummy cell to the chain (len:%d): %s\n", alu_chain_len, |
| 473 |
ctx->nameOf(dummy_block_ci)); |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
for (auto &ncell : new_cells) { |
| 480 |
ctx->cells[ncell->name] = std::move(ncell); |
| 481 |
} |
| 482 |
new_cells.clear(); |
| 483 |
// The placer doesn't know "a priori" that LUTs and ALUs conflict. So create blocker LUTs to make this explicit and |
| 484 |
// reduce wasted legalisation effort |
| 485 |
for (auto &cell : ctx->cells) { |
| 486 |
auto ci = cell.second.get(); |
| 487 |
if (ci->cluster == ClusterId()) { |
| 488 |
continue; |
| 489 |
} |
| 490 |
if (is_alu(ci)) { |
| 491 |
auto cell = std::make_unique<CellInfo>(ctx, ctx->idf("%s_BLOCKER_LUT", ctx->nameOf(ci)), id_BLOCKER_LUT); |
| 492 |
cell->cluster = ci->cluster; |
| 493 |
ctx->cells.at(cell->cluster)->constr_children.push_back(cell.get()); |
| 494 |
cell->constr_abs_z = true; |
| 495 |
cell->constr_x = ci->constr_x; |
| 496 |
cell->constr_y = ci->constr_y; |
| 497 |
cell->constr_z = 2 * (ci->constr_z - (ci->constr_abs_z ? BelZ::ALU0_Z : 0)); |
| 498 |
new_cells.emplace_back(std::move(cell)); |
| 499 |
} |
| 500 |
} |
| 501 |
for (auto &ncell : new_cells) { |
| 502 |
ctx->cells[ncell->name] = std::move(ncell); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
// =================================== |
| 507 |
// glue LUT and FF |
| 508 |
// =================================== |
| 509 |
void GowinPacker::constrain_lutffs(void) |
| 510 |
{ |
| 511 |
// Constrain directly connected LUTs and FFs together to use dedicated resources |
| 512 |
const pool<CellTypePort> lut_outs{{id_LUT1, id_F}, {id_LUT2, id_F}, {id_LUT3, id_F}, {id_LUT4, id_F}}; |
| 513 |
const pool<CellTypePort> dff_ins{{id_DFF, id_D}, {id_DFFE, id_D}, {id_DFFN, id_D}, {id_DFFNE, id_D}, |
| 514 |
{id_DFFS, id_D}, {id_DFFSE, id_D}, {id_DFFNS, id_D}, {id_DFFNSE, id_D}, |
| 515 |
{id_DFFR, id_D}, {id_DFFRE, id_D}, {id_DFFNR, id_D}, {id_DFFNRE, id_D}, |
| 516 |
{id_DFFP, id_D}, {id_DFFPE, id_D}, {id_DFFNP, id_D}, {id_DFFNPE, id_D}, |
| 517 |
{id_DFFC, id_D}, {id_DFFCE, id_D}, {id_DFFNC, id_D}, {id_DFFNCE, id_D}}; |
| 518 |
|
| 519 |
int lutffs = h.constrain_cell_pairs(lut_outs, dff_ins, 1, 1); |
| 520 |
log_info("Constrained %d LUTFF pairs.\n", lutffs); |
| 521 |
} |
| 522 |
|
| 523 |
// =================================== |
| 524 |
// SSRAM cluster |
| 525 |
// =================================== |
| 526 |
std::unique_ptr<CellInfo> GowinPacker::ssram_make_lut(Context *ctx, CellInfo *ci, int index) |
| 527 |
{ |
| 528 |
IdString name_id = ctx->idf("%s_LUT%d", ci->name.c_str(ctx), index); |
| 529 |
auto lut_ci = std::make_unique<CellInfo>(ctx, name_id, id_LUT4); |
| 530 |
if (index) { |
| 531 |
for (IdString port : {id_I0, id_I1, id_I2, id_I3}) { |
| 532 |
lut_ci->addInput(port); |
| 533 |
} |
| 534 |
} |
| 535 |
IdString init_name = ctx->idf("INIT_%d", index); |
| 536 |
if (ci->params.count(init_name)) { |
| 537 |
lut_ci->setParam(id_INIT, ci->params.at(init_name)); |
| 538 |
} else { |
| 539 |
lut_ci->setParam(id_INIT, std::string("1111111111111111")); |
| 540 |
} |
| 541 |
return lut_ci; |
| 542 |
} |
| 543 |
|
| 544 |
void GowinPacker::pack_ssram(void) |
| 545 |
{ |
| 546 |
std::vector<std::unique_ptr<CellInfo>> new_cells; |
| 547 |
std::vector<IdString> cells_to_remove; |
| 548 |
|
| 549 |
log_info("Pack SSRAMs...\n"); |
| 550 |
for (auto &cell : ctx->cells) { |
| 551 |
auto ci = cell.second.get(); |
| 552 |
if (ci->cluster != ClusterId()) { |
| 553 |
continue; |
| 554 |
} |
| 555 |
|
| 556 |
if (is_ssram(ci)) { |
| 557 |
if (ci->type == id_ROM16) { |
| 558 |
new_cells.push_back(ssram_make_lut(ctx, ci, 0)); |
| 559 |
CellInfo *lut_ci = new_cells.back().get(); |
| 560 |
// inputs |
| 561 |
ci->movePortBusTo(id_AD, 0, true, lut_ci, id_I, 0, false, 4); |
| 562 |
// output |
| 563 |
ci->movePortTo(id_DO, lut_ci, id_F); |
| 564 |
|
| 565 |
cells_to_remove.push_back(ci->name); |
| 566 |
continue; |
| 567 |
} |
| 568 |
// make cluster root |
| 569 |
ci->cluster = ci->name; |
| 570 |
ci->constr_abs_z = true; |
| 571 |
ci->constr_x = 0; |
| 572 |
ci->constr_y = 0; |
| 573 |
ci->constr_z = BelZ::RAMW_Z; |
| 574 |
|
| 575 |
ci->addInput(id_CE); |
| 576 |
ci->connectPort(id_CE, ctx->nets.at(ctx->id("$PACKER_VCC")).get()); |
| 577 |
|
| 578 |
// RAD networks |
| 579 |
NetInfo *rad[4]; |
| 580 |
for (int i = 0; i < 4; ++i) { |
| 581 |
rad[i] = ci->getPort(ctx->idf("RAD[%d]", i)); |
| 582 |
} |
| 583 |
|
| 584 |
// active LUTs |
| 585 |
int luts_num = 4; |
| 586 |
if (ci->type == id_RAM16SDP1) { |
| 587 |
luts_num = 1; |
| 588 |
} else { |
| 589 |
if (ci->type == id_RAM16SDP2) { |
| 590 |
luts_num = 2; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
// make actual storage cells |
| 595 |
for (int i = 0; i < 4; ++i) { |
| 596 |
new_cells.push_back(ssram_make_lut(ctx, ci, i)); |
| 597 |
CellInfo *lut_ci = new_cells.back().get(); |
| 598 |
ci->constr_children.push_back(lut_ci); |
| 599 |
lut_ci->cluster = ci->name; |
| 600 |
lut_ci->constr_abs_z = true; |
| 601 |
lut_ci->constr_x = 0; |
| 602 |
lut_ci->constr_y = 0; |
| 603 |
lut_ci->constr_z = i * 2; |
| 604 |
// inputs |
| 605 |
// LUT0 is already connected when generating the base |
| 606 |
if (i && i < luts_num) { |
| 607 |
for (int j = 0; j < 4; ++j) { |
| 608 |
lut_ci->connectPort(ctx->idf("I%d", j), rad[j]); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
for (int i = 4; i < 8; ++i) { |
| 613 |
auto cell = std::make_unique<CellInfo>(ctx, ctx->idf("%s_BLOCKER_LUT_%d", ctx->nameOf(ci), i), |
| 614 |
id_BLOCKER_LUT); |
| 615 |
cell->cluster = ci->cluster; |
| 616 |
ci->constr_children.push_back(cell.get()); |
| 617 |
cell->constr_abs_z = true; |
| 618 |
cell->constr_x = 0; |
| 619 |
cell->constr_y = 0; |
| 620 |
cell->constr_z = 2 * i; |
| 621 |
new_cells.emplace_back(std::move(cell)); |
| 622 |
} |
| 623 |
for (int i = 0; i < (gwu.has_DFF67() ? 8 : 6); ++i) { |
| 624 |
auto cell = std::make_unique<CellInfo>(ctx, ctx->idf("%s_BLOCKER_FF_%d", ctx->nameOf(ci), i), |
| 625 |
id_BLOCKER_FF); |
| 626 |
cell->cluster = ci->cluster; |
| 627 |
ci->constr_children.push_back(cell.get()); |
| 628 |
cell->constr_abs_z = true; |
| 629 |
cell->constr_x = 0; |
| 630 |
cell->constr_y = 0; |
| 631 |
cell->constr_z = 2 * i + 1; |
| 632 |
new_cells.emplace_back(std::move(cell)); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
for (auto &ncell : new_cells) { |
| 637 |
ctx->cells[ncell->name] = std::move(ncell); |
| 638 |
} |
| 639 |
for (auto cell : cells_to_remove) { |
| 640 |
ctx->cells.erase(cell); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
NEXTPNR_NAMESPACE_END |