| 1 |
#include <boost/algorithm/string.hpp> |
| 2 |
#include <regex> |
| 3 |
#include <utility> |
| 4 |
|
| 5 |
#include "log.h" |
| 6 |
#include "nextpnr.h" |
| 7 |
#include "util.h" |
| 8 |
|
| 9 |
#define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc" |
| 10 |
#include "himbaechel_constids.h" |
| 11 |
#include "himbaechel_helpers.h" |
| 12 |
|
| 13 |
#include "cst.h" |
| 14 |
#include "gowin.h" |
| 15 |
|
| 16 |
NEXTPNR_NAMESPACE_BEGIN |
| 17 |
|
| 18 |
struct GowinCstReader |
| 19 |
{ |
| 20 |
Context *ctx; |
| 21 |
std::istream ∈ |
| 22 |
|
| 23 |
GowinCstReader(Context *ctx, std::istream &in) : ctx(ctx), in(in){}; |
| 24 |
|
| 25 |
const PadInfoPOD *pinLookup(const PadInfoPOD *list, const size_t len, const IdString idx) |
| 26 |
{ |
| 27 |
for (size_t i = 0; i < len; i++) { |
| 28 |
const PadInfoPOD *pin = &list[i]; |
| 29 |
if (IdString(pin->package_pin) == idx) { |
| 30 |
return pin; |
| 31 |
} |
| 32 |
} |
| 33 |
return nullptr; |
| 34 |
} |
| 35 |
|
| 36 |
Loc getLoc(std::smatch match, int maxX, int maxY) |
| 37 |
{ |
| 38 |
int col = std::stoi(match[2]); |
| 39 |
int row = 1; // Top |
| 40 |
std::string side = match[1].str(); |
| 41 |
if (side == "R") { |
| 42 |
row = col; |
| 43 |
col = maxX; |
| 44 |
} else if (side == "B") { |
| 45 |
row = maxY; |
| 46 |
} else if (side == "L") { |
| 47 |
row = col; |
| 48 |
col = 1; |
| 49 |
} |
| 50 |
int z = match[3].str()[0] - 'A' + BelZ::IOBA_Z; |
| 51 |
return Loc(col - 1, row - 1, z); |
| 52 |
} |
| 53 |
|
| 54 |
BelId getConstrainedHCLKBel(std::smatch match, int maxX, int maxY) |
| 55 |
{ |
| 56 |
int idx = std::stoi(match[3]); |
| 57 |
int bel_z = BelZ::CLKDIV_0_Z + 2 * idx; |
| 58 |
|
| 59 |
std::string side = match[2].str(); |
| 60 |
bool lr = (side == "LEFT") || (side == "RIGHT"); |
| 61 |
int y_coord = (side == "BOTTOM") ? maxY - 1 : 0; |
| 62 |
int x_coord = (side == "RIGHT") ? maxX - 1 : 0; |
| 63 |
|
| 64 |
for (auto &bel : ctx->getBelsInBucket(ctx->getBelBucketForCellType(id_CLKDIV))) { |
| 65 |
auto this_loc = ctx->getBelLocation(bel); |
| 66 |
if (lr && this_loc.x == x_coord && this_loc.z == bel_z && this_loc.y != 0 && |
| 67 |
this_loc.y != maxY - 1) // left or right side |
| 68 |
return bel; |
| 69 |
else if (!lr && this_loc.y == y_coord && this_loc.z == bel_z) // top or bottom side |
| 70 |
return bel; |
| 71 |
} |
| 72 |
return BelId(); |
| 73 |
} |
| 74 |
|
| 75 |
bool run(void) |
| 76 |
{ |
| 77 |
pool<std::pair<IdString, IdStringList>> constrained_cells; |
| 78 |
auto debug_cell = [this, &constrained_cells](IdString cellId, IdStringList belId) { |
| 79 |
if (ctx->debug) { |
| 80 |
constrained_cells.insert(std::make_pair(cellId, belId)); |
| 81 |
} |
| 82 |
}; |
| 83 |
pool<IdString> adc_ios; // bus#/X#Y# |
| 84 |
|
| 85 |
log_info("Reading constraints...\n"); |
| 86 |
try { |
| 87 |
// If two locations are specified separated by commas (for differential I/O buffers), |
| 88 |
// only the first location is actually recognized and used. |
| 89 |
// And pin A will be Positive and pin B will be Negative in any case. |
| 90 |
std::regex iobre = std::regex("IO_LOC +\"([^\"]+)\" +([^ ,;]+)(, *[^ ;]+)? *;.*[\\s\\S]*"); |
| 91 |
std::regex portre = std::regex("IO_PORT +\"([^\"]+)\" +([^;]+;).*[\\s\\S]*"); |
| 92 |
std::regex port_attrre = std::regex("([^ =;]+=[^ =;]+) *([^;]*;)"); |
| 93 |
std::regex iobelre = std::regex("IO([TRBL])([0-9]+)\\[?([A-Z])\\]?"); |
| 94 |
std::regex inslocre = |
| 95 |
std::regex("INS_LOC +\"([^\"]+)\" +R([0-9]+)C([0-9]+)\\[([0-9])\\]\\[([AB])\\] *;.*[\\s\\S]*"); |
| 96 |
std::regex hclkre = |
| 97 |
std::regex("INS_LOC +\"([^\"]+)\" +(TOP|RIGHT|BOTTOM|LEFT)SIDE\\[([0,1])\\] *;*[\\s\\S]*"); |
| 98 |
std::regex clockre = std::regex("CLOCK_LOC +\"([^\"]+)\" +BUF([GS])(\\[([0-7])\\])?[^;]*;.*[\\s\\S]*"); |
| 99 |
std::regex adcre = std::regex("USE_ADC_SRC +bus([0-9]) +IO([TRBL])([0-9]+) *;.*[\\s\\S]*"); |
| 100 |
std::smatch match, match_attr, match_pinloc; |
| 101 |
std::string line, pinlines[2]; |
| 102 |
std::vector<IdStringList> constrained_clkdivs; |
| 103 |
enum |
| 104 |
{ |
| 105 |
ioloc, |
| 106 |
ioport, |
| 107 |
insloc, |
| 108 |
clock, |
| 109 |
hclk, |
| 110 |
adc |
| 111 |
} cst_type; |
| 112 |
|
| 113 |
while (!in.eof()) { |
| 114 |
std::getline(in, line); |
| 115 |
boost::trim_right(line); |
| 116 |
if (line.empty()) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
cst_type = ioloc; |
| 121 |
if (!std::regex_match(line, match, iobre)) { |
| 122 |
if (std::regex_match(line, match, portre)) { |
| 123 |
cst_type = ioport; |
| 124 |
} else { |
| 125 |
if (std::regex_match(line, match, clockre)) { |
| 126 |
cst_type = clock; |
| 127 |
} else { |
| 128 |
if (std::regex_match(line, match, inslocre)) { |
| 129 |
cst_type = insloc; |
| 130 |
} else { |
| 131 |
if (std::regex_match(line, match, hclkre)) { |
| 132 |
cst_type = hclk; |
| 133 |
} else { |
| 134 |
if (std::regex_match(line, match, adcre)) { |
| 135 |
cst_type = adc; |
| 136 |
} else { |
| 137 |
if ((!line.empty()) && (line.rfind("//", 0) == std::string::npos)) { |
| 138 |
log_warning("Invalid constraint: %s\n", line.c_str()); |
| 139 |
} |
| 140 |
continue; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
IdString net = ctx->id(match[1]); |
| 149 |
auto it = ctx->cells.find(net); |
| 150 |
if (cst_type != clock && cst_type != adc && it == ctx->cells.end()) { |
| 151 |
log_info("Cell %s not found\n", net.c_str(ctx)); |
| 152 |
continue; |
| 153 |
} |
| 154 |
switch (cst_type) { |
| 155 |
case adc: { // USE_ADC_SRC bus# IOLOC |
| 156 |
int col = std::stoi(match[3]); |
| 157 |
int row = 1; // Top |
| 158 |
std::string side = match[2].str(); |
| 159 |
if (side == "R") { |
| 160 |
row = col; |
| 161 |
col = ctx->getGridDimX(); |
| 162 |
} else if (side == "B") { |
| 163 |
row = ctx->getGridDimY(); |
| 164 |
} else if (side == "L") { |
| 165 |
row = col; |
| 166 |
col = 1; |
| 167 |
} |
| 168 |
adc_ios.insert(ctx->idf("%d/X%dY%d", std::stoi(match[1]), col - 1, row - 1)); |
| 169 |
} break; |
| 170 |
case clock: { // CLOCK name BUFG|S=# |
| 171 |
std::string which_clock = match[2]; |
| 172 |
std::string lw = match[4]; |
| 173 |
int lw_idx = -1; |
| 174 |
if (lw.length() > 0) { |
| 175 |
lw_idx = atoi(lw.c_str()); |
| 176 |
log_info("lw_idx:%d\n", lw_idx); |
| 177 |
} |
| 178 |
if (which_clock.at(0) == 'S') { |
| 179 |
auto ni = ctx->nets.find(net); |
| 180 |
if (ni == ctx->nets.end()) { |
| 181 |
log_info("Net %s not found\n", net.c_str(ctx)); |
| 182 |
continue; |
| 183 |
} |
| 184 |
// if (!allocate_longwire(ni->second.get(), lw_idx)) { |
| 185 |
log_info("Can't use the long wires. The %s network will use normal routing.\n", net.c_str(ctx)); |
| 186 |
//} |
| 187 |
} else { |
| 188 |
auto ni = ctx->nets.find(net); |
| 189 |
if (ni == ctx->nets.end()) { |
| 190 |
log_info("Net %s not found\n", net.c_str(ctx)); |
| 191 |
continue; |
| 192 |
} |
| 193 |
if (ctx->debug) { |
| 194 |
log_info("Mark net '%s' as CLOCK\n", net.c_str(ctx)); |
| 195 |
} |
| 196 |
// XXX YES for now. May be put the number here |
| 197 |
ni->second->attrs[id_CLOCK] = Property(std::string("YES")); |
| 198 |
} |
| 199 |
} break; |
| 200 |
case ioloc: { // IO_LOC name pin |
| 201 |
int nb_iter = 1; |
| 202 |
IdString nets[2]; |
| 203 |
|
| 204 |
// Prepare pinlines and nets (default: one Pin, one LOC). |
| 205 |
pinlines[0] = match[2]; |
| 206 |
nets[0] = ctx->id(match[1]); |
| 207 |
|
| 208 |
// Differential case: one Pin (_p), two LOCs separated by a ',' |
| 209 |
if (match[3].length() > 0) { |
| 210 |
nb_iter++; |
| 211 |
// Uses second pin after removing ',' |
| 212 |
pinlines[1] = std::regex_replace(match[3].str(), std::regex("^,"), ""); |
| 213 |
|
| 214 |
// Replaces _p with _n in pinname. |
| 215 |
std::string tmp = std::regex_replace(match[1].str(), std::regex("_p$"), "_n"); |
| 216 |
|
| 217 |
nets[1] = ctx->id(tmp); |
| 218 |
it = ctx->cells.find(nets[1]); |
| 219 |
if (cst_type != clock && it == ctx->cells.end()) { |
| 220 |
log_info("Cell %s not found\n", nets[1].c_str(ctx)); |
| 221 |
continue; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
for (int iter = 0; iter < nb_iter; iter++) { |
| 226 |
IdString pinname = ctx->id(pinlines[iter]); |
| 227 |
auto it = ctx->cells.find(nets[iter]); |
| 228 |
|
| 229 |
const PadInfoPOD *belname = |
| 230 |
pinLookup(ctx->package_info->pads.get(), ctx->package_info->pads.ssize(), pinname); |
| 231 |
if (belname != nullptr) { |
| 232 |
IdStringList bel = IdStringList::concat(IdString(belname->tile), IdString(belname->bel)); |
| 233 |
it->second->setAttr(IdString(ID_BEL), bel.str(ctx)); |
| 234 |
debug_cell(it->second->name, bel); |
| 235 |
} else { |
| 236 |
if (std::regex_match(pinlines[iter], match_pinloc, iobelre)) { |
| 237 |
// may be it's IOx#[AB] style? |
| 238 |
Loc loc = getLoc(match_pinloc, ctx->getGridDimX(), ctx->getGridDimY()); |
| 239 |
BelId bel = ctx->getBelByLocation(loc); |
| 240 |
if (bel == BelId()) { |
| 241 |
log_error("Pin %s not found (TRBL style). \n", pinlines[iter].c_str()); |
| 242 |
} |
| 243 |
it->second->setAttr(IdString(ID_BEL), std::string(ctx->nameOfBel(bel))); |
| 244 |
debug_cell(it->second->name, ctx->getBelName(bel)); |
| 245 |
} else { |
| 246 |
log_error("Pin %s not found (pin# style)\n", pinname.c_str(ctx)); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} break; |
| 251 |
case hclk: { |
| 252 |
IdString cell_type = it->second->type; |
| 253 |
if (cell_type != id_CLKDIV) { |
| 254 |
log_error("Unsupported or invalid cell type %s for hclk\n", cell_type.c_str(ctx)); |
| 255 |
} |
| 256 |
BelId hclk_bel = getConstrainedHCLKBel(match, ctx->getGridDimX(), ctx->getGridDimY()); |
| 257 |
if (hclk_bel != BelId()) { |
| 258 |
auto hclk_bel_name = ctx->getBelName(hclk_bel); |
| 259 |
if (std::find(constrained_clkdivs.begin(), constrained_clkdivs.end(), hclk_bel_name) != |
| 260 |
constrained_clkdivs.end()) { |
| 261 |
log_error("Only one CLKDIV can be placed at %sSIDE[%s]\n", match[2].str().c_str(), |
| 262 |
match[3].str().c_str()); |
| 263 |
} |
| 264 |
constrained_clkdivs.push_back(hclk_bel_name); |
| 265 |
it->second->setAttr(id_BEL, ctx->getBelName(hclk_bel).str(ctx)); |
| 266 |
debug_cell(it->second->name, ctx->getBelName(hclk_bel)); |
| 267 |
} else { |
| 268 |
log_error("No Bel of type CLKDIV found at constrained location %sSIDE[%s]\n", |
| 269 |
match[2].str().c_str(), match[3].str().c_str()); |
| 270 |
} |
| 271 |
} break; |
| 272 |
default: { // IO_PORT attr=value |
| 273 |
std::string attr_val = match[2]; |
| 274 |
while (std::regex_match(attr_val, match_attr, port_attrre)) { |
| 275 |
std::string attr = "&"; |
| 276 |
attr += match_attr[1]; |
| 277 |
boost::algorithm::to_upper(attr); |
| 278 |
it->second->setAttr(ctx->id(attr), 1); |
| 279 |
attr_val = match_attr[2]; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
if (ctx->debug) { |
| 285 |
for (auto &cell : constrained_cells) { |
| 286 |
log_info("Cell %s is constrained to %s\n", cell.first.c_str(ctx), cell.second.str(ctx).c_str()); |
| 287 |
} |
| 288 |
if (!adc_ios.empty()) { |
| 289 |
log_info("ADC iobufs:\n"); |
| 290 |
for (auto &bus_io : adc_ios) { |
| 291 |
log_info(" bus %s\n", bus_io.c_str(ctx)); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
if (!adc_ios.empty()) { |
| 296 |
for (auto &cell : ctx->cells) { |
| 297 |
auto &ci = *cell.second; |
| 298 |
|
| 299 |
if (is_adc(&ci)) { |
| 300 |
int idx = 0; |
| 301 |
for (auto &bus_io : adc_ios) { |
| 302 |
ci.setAttr(ctx->idf("ADC_IO_%d", idx), bus_io.str(ctx)); |
| 303 |
++idx; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
return true; |
| 309 |
} catch (log_execution_error_exception) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
static void add_sip_constraints(Context *ctx, const Extra_package_data_POD *extra) |
| 316 |
{ |
| 317 |
for (auto cst : extra->cst) { |
| 318 |
auto it = ctx->cells.find(IdString(cst.net)); |
| 319 |
if (it == ctx->cells.end()) { |
| 320 |
log_info("Cell %s not found\n", IdString(cst.net).c_str(ctx)); |
| 321 |
continue; |
| 322 |
} |
| 323 |
Loc loc = Loc(cst.col, cst.row, cst.bel); |
| 324 |
BelId bel = ctx->getBelByLocation(loc); |
| 325 |
if (bel == BelId()) { |
| 326 |
log_error("Pin not found.\n"); |
| 327 |
} |
| 328 |
it->second->setAttr(IdString(ID_BEL), std::string(ctx->nameOfBel(bel))); |
| 329 |
|
| 330 |
if (cst.iostd > 0) { |
| 331 |
std::string attr = "&IO_TYPE="; |
| 332 |
attr += IdString(cst.iostd).c_str(ctx); |
| 333 |
boost::algorithm::to_upper(attr); |
| 334 |
it->second->setAttr(ctx->id(attr), 1); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
bool gowin_apply_constraints(Context *ctx, std::istream &in) |
| 340 |
{ |
| 341 |
// implicit constraints from SiP pins |
| 342 |
if (!ctx->package_info->extra_data.is_null()) { |
| 343 |
const Extra_package_data_POD *extra = |
| 344 |
reinterpret_cast<const Extra_package_data_POD *>(ctx->package_info->extra_data.get()); |
| 345 |
add_sip_constraints(ctx, extra); |
| 346 |
} |
| 347 |
|
| 348 |
GowinCstReader reader(ctx, in); |
| 349 |
return reader.run(); |
| 350 |
} |
| 351 |
|
| 352 |
NEXTPNR_NAMESPACE_END |