| 1 |
/* |
| 2 |
* nextpnr -- Next Generation Place and Route |
| 3 |
* |
| 4 |
* Copyright (C) 2024 The Project Peppercorn Authors. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <boost/algorithm/string.hpp> |
| 22 |
#include <boost/lexical_cast.hpp> |
| 23 |
#include <fstream> |
| 24 |
#include <regex> |
| 25 |
|
| 26 |
#include "extra_data.h" |
| 27 |
#include "himbaechel_api.h" |
| 28 |
#include "log.h" |
| 29 |
#include "nextpnr.h" |
| 30 |
#include "util.h" |
| 31 |
|
| 32 |
#include "gatemate.h" |
| 33 |
|
| 34 |
#define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc" |
| 35 |
#include "himbaechel_constids.h" |
| 36 |
|
| 37 |
NEXTPNR_NAMESPACE_BEGIN |
| 38 |
|
| 39 |
struct GateMateCCFReader |
| 40 |
{ |
| 41 |
Context *ctx; |
| 42 |
GateMateImpl *uarch; |
| 43 |
std::istream ∈ |
| 44 |
int lineno; |
| 45 |
dict<IdString, Property> defaults; |
| 46 |
std::vector<int> count; |
| 47 |
|
| 48 |
GateMateCCFReader(Context *ctx, GateMateImpl *uarch, std::istream &in) : ctx(ctx), uarch(uarch), in(in){}; |
| 49 |
|
| 50 |
std::string strip_quotes(const std::string &str) |
| 51 |
{ |
| 52 |
if (str.at(0) == '"') { |
| 53 |
if (str.back() != '"') { |
| 54 |
log_error("Expected '\"' at end of string '%s' (on line %d).\n", str.c_str(), lineno); |
| 55 |
} |
| 56 |
return str.substr(1, str.size() - 2); |
| 57 |
} else { |
| 58 |
return str; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
void parse_params(std::vector<std::string> ¶ms, bool is_default, dict<IdString, Property> *props) |
| 63 |
{ |
| 64 |
for (auto param : params) { |
| 65 |
std::vector<std::string> expr; |
| 66 |
boost::split(expr, param, boost::is_any_of("=")); |
| 67 |
std::string name = expr.at(0); |
| 68 |
boost::algorithm::trim(name); |
| 69 |
boost::algorithm::to_upper(name); |
| 70 |
|
| 71 |
if (expr.size() != 2) { |
| 72 |
if (name == "LOC" || name == "DRIVE" || name == "DELAY_IBF" || name == "DELAY_OBF" || name == "DIE") |
| 73 |
log_error("Parameter must be in form NAME=VALUE (on line %d).\n", lineno); |
| 74 |
log_warning("Parameter '%s' missing value, defaulting to '1' (on line %d).\n", name.c_str(), lineno); |
| 75 |
expr.push_back("1"); |
| 76 |
} |
| 77 |
|
| 78 |
std::string value = strip_quotes(expr.at(1)); |
| 79 |
boost::algorithm::trim(value); |
| 80 |
boost::algorithm::to_upper(value); |
| 81 |
|
| 82 |
if (name == "LOC") { |
| 83 |
if (is_default) |
| 84 |
log_error("Value '%s' can not be defined for default GPIO in line %d.\n", name.c_str(), lineno); |
| 85 |
if (ctx->get_package_pin_bel(ctx->id(value)) == BelId() && value != "SER_CLK" && value != "SER_CLK_N") |
| 86 |
log_error("Unknown location '%s' used in line %d.\n", value.c_str(), lineno); |
| 87 |
if (!uarch->available_pads.count(ctx->id(value))) |
| 88 |
log_error("Pad '%s' used in line %d not available.\n", value.c_str(), lineno); |
| 89 |
props->emplace(id_LOC, Property(value)); |
| 90 |
for (int i = 0; i < uarch->dies; i++) { |
| 91 |
if (uarch->locations.count(std::make_pair(ctx->id(value), i))) |
| 92 |
count[i]++; |
| 93 |
else |
| 94 |
count[i]--; |
| 95 |
} |
| 96 |
uarch->available_pads.erase(ctx->id(value)); |
| 97 |
// Erase aliases as well |
| 98 |
if (value == "SER_CLK") |
| 99 |
uarch->available_pads.erase(ctx->id("SER_CLK_N")); |
| 100 |
if (value == "SER_CLK_N") |
| 101 |
uarch->available_pads.erase(ctx->id("SER_CLK")); |
| 102 |
} else if (name == "DIE") { |
| 103 |
if (uarch->die_to_index.count(ctx->id(value))) { |
| 104 |
props->emplace(ctx->id(name), Property(value)); |
| 105 |
} else |
| 106 |
log_error("Unknown value '%s' for parameter '%s' in line %d.\n", value.c_str(), name.c_str(), |
| 107 |
lineno); |
| 108 |
} else if (name == "SCHMITT_TRIGGER" || name == "PULLUP" || name == "PULLDOWN" || name == "KEEPER" || |
| 109 |
name == "FF_IBF" || name == "FF_OBF" || name == "LVDS_BOOST" || name == "LVDS_RTERM") { |
| 110 |
if (value == "1") |
| 111 |
value = "TRUE"; |
| 112 |
else if (value == "0") |
| 113 |
value = "FALSE"; |
| 114 |
if (value == "TRUE") { |
| 115 |
props->emplace(ctx->id(name), Property(Property::State::S1)); |
| 116 |
} else if (value == "FALSE") { |
| 117 |
props->emplace(ctx->id(name), Property(Property::State::S0)); |
| 118 |
} else |
| 119 |
log_error("Unknown value '%s' for parameter '%s' in line %d, must be TRUE or FALSE.\n", |
| 120 |
value.c_str(), name.c_str(), lineno); |
| 121 |
} else if (name == "SLEW") { |
| 122 |
if (value == "1" || value == "TRUE") |
| 123 |
value = "SLOW"; |
| 124 |
else if (value == "0" || value == "FALSE") |
| 125 |
value = "FAST"; |
| 126 |
if (value == "FAST" || value == "SLOW") { |
| 127 |
props->emplace(ctx->id(name), Property(value)); |
| 128 |
} else |
| 129 |
log_error("Unknown value '%s' for parameter '%s' in line %d, must be SLOW or FAST.\n", |
| 130 |
value.c_str(), name.c_str(), lineno); |
| 131 |
} else if (name == "DRIVE") { |
| 132 |
try { |
| 133 |
int drive = boost::lexical_cast<int>(value.c_str()); |
| 134 |
if (drive == 3 || drive == 6 || drive == 9 || drive == 12) { |
| 135 |
props->emplace(ctx->id(name), Property(drive, 2)); |
| 136 |
} else |
| 137 |
log_error("Parameter '%s' must have value 3,6,9 or 12 in line %d.\n", name.c_str(), lineno); |
| 138 |
} catch (boost::bad_lexical_cast const &) { |
| 139 |
log_error("Parameter '%s' must be number in line %d.\n", name.c_str(), lineno); |
| 140 |
} |
| 141 |
} else if (name == "DELAY_IBF" || name == "DELAY_OBF") { |
| 142 |
try { |
| 143 |
int delay = boost::lexical_cast<int>(value.c_str()); |
| 144 |
if (delay >= 0 && delay <= 15) { |
| 145 |
props->emplace(ctx->id(name), Property(delay, 4)); |
| 146 |
} else |
| 147 |
log_error("Parameter '%s' must have value from 0 to 15 in line %d.\n", name.c_str(), lineno); |
| 148 |
} catch (boost::bad_lexical_cast const &) { |
| 149 |
log_error("Parameter '%s' must be number in line %d.\n", name.c_str(), lineno); |
| 150 |
} |
| 151 |
} else { |
| 152 |
log_error("Unknown parameter name '%s' in line %d.\n", name.c_str(), lineno); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
std::regex pattern_to_regex(const std::string &pat) |
| 158 |
{ |
| 159 |
std::string expr; |
| 160 |
expr.reserve(pat.size() * 2); |
| 161 |
expr += '^'; |
| 162 |
for (char c : pat) { |
| 163 |
switch (c) { |
| 164 |
case '*': |
| 165 |
expr += ".*"; |
| 166 |
break; |
| 167 |
case '?': |
| 168 |
expr += "."; |
| 169 |
break; |
| 170 |
// Escape regex metacharacters |
| 171 |
case '.': |
| 172 |
case '+': |
| 173 |
case '(': |
| 174 |
case ')': |
| 175 |
case '{': |
| 176 |
case '}': |
| 177 |
case '^': |
| 178 |
case '$': |
| 179 |
case '|': |
| 180 |
case '\\': |
| 181 |
case '[': |
| 182 |
case ']': |
| 183 |
expr += '\\'; |
| 184 |
expr += c; |
| 185 |
break; |
| 186 |
|
| 187 |
default: |
| 188 |
expr += c; |
| 189 |
} |
| 190 |
} |
| 191 |
expr += '$'; |
| 192 |
return std::regex(expr); |
| 193 |
} |
| 194 |
|
| 195 |
void run() |
| 196 |
{ |
| 197 |
log_info("Parsing CCF file..\n"); |
| 198 |
|
| 199 |
std::string line; |
| 200 |
std::string linebuf; |
| 201 |
defaults.clear(); |
| 202 |
|
| 203 |
auto isempty = [](const std::string &str) { |
| 204 |
return std::all_of(str.begin(), str.end(), [](char c) { return isblank(c) || c == '\r' || c == '\n'; }); |
| 205 |
}; |
| 206 |
lineno = 0; |
| 207 |
count = std::vector<int>(uarch->dies, 0); |
| 208 |
bool floorplanning = false; |
| 209 |
while (std::getline(in, line)) { |
| 210 |
++lineno; |
| 211 |
// Both // and # are considered start of comment |
| 212 |
size_t com_start = line.find("//"); |
| 213 |
if (com_start != std::string::npos) |
| 214 |
line = line.substr(0, com_start); |
| 215 |
com_start = line.find('#'); |
| 216 |
if (com_start != std::string::npos) |
| 217 |
line = line.substr(0, com_start); |
| 218 |
if (isempty(line)) |
| 219 |
continue; |
| 220 |
linebuf += line; |
| 221 |
|
| 222 |
boost::algorithm::to_lower(line); |
| 223 |
if (line.find("start floorplanning") != std::string::npos) { |
| 224 |
floorplanning = true; |
| 225 |
linebuf = ""; |
| 226 |
continue; |
| 227 |
} else if (line.find("end floorplanning") != std::string::npos) { |
| 228 |
floorplanning = false; |
| 229 |
linebuf = ""; |
| 230 |
continue; |
| 231 |
} |
| 232 |
if (floorplanning) { |
| 233 |
// int size = -1; |
| 234 |
std::string src_location; |
| 235 |
|
| 236 |
std::string s = linebuf; |
| 237 |
boost::trim(s); |
| 238 |
|
| 239 |
// split input into segments by ';' |
| 240 |
std::vector<std::string> segments; |
| 241 |
boost::split(segments, s, boost::is_any_of(";")); |
| 242 |
for (auto &seg : segments) |
| 243 |
boost::trim(seg); |
| 244 |
|
| 245 |
if (!segments.empty()) { |
| 246 |
std::vector<std::string> parts; |
| 247 |
boost::split(parts, segments[0], boost::is_any_of(":")); |
| 248 |
for (auto &p : parts) |
| 249 |
boost::trim(p); |
| 250 |
|
| 251 |
// index is numeric token |
| 252 |
// int index = -1; |
| 253 |
if (!parts.empty() && !parts[0].empty() && |
| 254 |
std::all_of(parts[0].begin(), parts[0].end(), ::isdigit)) { |
| 255 |
// index = std::stoi(parts[0]); |
| 256 |
parts.erase(parts.begin()); |
| 257 |
} |
| 258 |
|
| 259 |
// find size |
| 260 |
// size = -1; |
| 261 |
for (size_t i = 0; i + 1 < parts.size(); ++i) { |
| 262 |
if (boost::iequals(parts[i], "size")) { |
| 263 |
if (!parts[i + 1].empty() && |
| 264 |
std::all_of(parts[i + 1].begin(), parts[i + 1].end(), ::isdigit)) { |
| 265 |
// size = std::stoi(parts[i + 1]); |
| 266 |
} |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// always the last piece of the main segment |
| 272 |
if (!parts.empty()) { |
| 273 |
src_location = parts.back(); |
| 274 |
boost::trim(src_location); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (segments.size() > 1) { |
| 279 |
std::string &tmp = segments[1]; |
| 280 |
boost::trim(tmp); |
| 281 |
|
| 282 |
if (!tmp.empty()) { |
| 283 |
std::vector<std::string> subparts; |
| 284 |
boost::split(subparts, tmp, boost::is_any_of(":")); |
| 285 |
for (auto &p : subparts) |
| 286 |
boost::trim(p); |
| 287 |
|
| 288 |
if (!subparts.empty() && boost::iequals(subparts[0], "pb")) { |
| 289 |
if (subparts.size() == 2) { |
| 290 |
std::string pb_position = subparts[1]; |
| 291 |
|
| 292 |
std::regex pb_regex(R"(x(-?\d+)y(-?\d+)x(-?\d+)y(-?\d+))"); |
| 293 |
std::smatch match; |
| 294 |
if (std::regex_match(pb_position, match, pb_regex)) { |
| 295 |
int x1 = std::stoi(match[1]) + 2; |
| 296 |
int y1 = std::stoi(match[2]) + 2; |
| 297 |
int x2 = std::stoi(match[3]) + 2; |
| 298 |
int y2 = std::stoi(match[4]) + 2; |
| 299 |
|
| 300 |
if (x1 < 0 || x1 >= ctx->getGridDimX() || x2 < 0 || x2 >= ctx->getGridDimX() || |
| 301 |
y1 < 0 || y1 >= ctx->getGridDimY() || y2 < 0 || y2 >= ctx->getGridDimY()) |
| 302 |
log_error("Placebox coordinates out of range '%s' in line %d.\n", |
| 303 |
pb_position.c_str(), lineno); |
| 304 |
|
| 305 |
IdString scopename(ctx, src_location.c_str()); |
| 306 |
std::regex expr = pattern_to_regex(src_location); |
| 307 |
bool matched_any = false; |
| 308 |
|
| 309 |
for (const IdString &name : uarch->scopenames) { |
| 310 |
if (std::regex_match(name.str(ctx), expr)) { |
| 311 |
matched_any = true; |
| 312 |
|
| 313 |
ctx->createRectangularRegion(name, x1, y1, x2, y2); |
| 314 |
uarch->scopenames_used.emplace(name); |
| 315 |
|
| 316 |
log_info(" Constraining region '%s' to '%s'\n", name.c_str(ctx), |
| 317 |
pb_position.c_str()); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (!matched_any) { |
| 322 |
log_error("Unknown scope name or pattern: '%s' in line %d.\n", |
| 323 |
src_location.c_str(), lineno); |
| 324 |
} |
| 325 |
} else { |
| 326 |
log_error("Placebox format invalid: %s in line %d.\n", pb_position.c_str(), lineno); |
| 327 |
} |
| 328 |
} else { |
| 329 |
log_error("Missing data for pB (in line %d).\n", lineno); |
| 330 |
} |
| 331 |
} else { |
| 332 |
log_error("Unexpected content in last segment (in line %d).\n", lineno); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
linebuf = ""; |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
size_t pos = linebuf.find(';'); |
| 342 |
// Need to concatenate lines until there is closing ; sign |
| 343 |
while (pos != std::string::npos) { |
| 344 |
std::string content = linebuf.substr(0, pos); |
| 345 |
|
| 346 |
std::vector<std::string> params; |
| 347 |
boost::split(params, content, boost::is_any_of("|")); |
| 348 |
std::string command = params.at(0); |
| 349 |
|
| 350 |
std::stringstream ss(command); |
| 351 |
std::vector<std::string> words; |
| 352 |
std::string tmp; |
| 353 |
while (ss >> tmp) |
| 354 |
words.push_back(tmp); |
| 355 |
std::string type = words.at(0); |
| 356 |
|
| 357 |
boost::algorithm::to_lower(type); |
| 358 |
if (type == "default_gpio") { |
| 359 |
if (words.size() != 1) |
| 360 |
log_error("Line with default_GPIO should not contain only parameters (in line %d).\n", lineno); |
| 361 |
params.erase(params.begin()); |
| 362 |
parse_params(params, true, &defaults); |
| 363 |
|
| 364 |
} else if (type == "net" || type == "pin_in" || type == "pin_out" || type == "pin_inout") { |
| 365 |
if (words.size() < 3 || words.size() > 5) |
| 366 |
log_error("Pin definition line not properly formed (in line %d).\n", lineno); |
| 367 |
std::string pin_name = strip_quotes(words.at(1)); |
| 368 |
|
| 369 |
// put back other words and use them as parameters |
| 370 |
std::stringstream ss; |
| 371 |
for (size_t i = 2; i < words.size(); i++) |
| 372 |
ss << words.at(i); |
| 373 |
params[0] = ss.str(); |
| 374 |
|
| 375 |
IdString cellname = ctx->id(pin_name); |
| 376 |
if (ctx->cells.count(cellname)) { |
| 377 |
CellInfo *cell = ctx->cells.at(cellname).get(); |
| 378 |
for (auto p : defaults) |
| 379 |
cell->params[p.first] = p.second; |
| 380 |
parse_params(params, false, &cell->params); |
| 381 |
} else |
| 382 |
log_warning("Pad with name '%s' not found in netlist.\n", pin_name.c_str()); |
| 383 |
} else if (type == "start" || type == "end") { |
| 384 |
std::string word2 = words.at(1); |
| 385 |
boost::algorithm::to_lower(word2); |
| 386 |
if (word2 == "floorplanning") { |
| 387 |
log("Found floor planning\n"); |
| 388 |
} else |
| 389 |
log_error("Unknown command '%s' in line %d.\n", word2.c_str(), lineno); |
| 390 |
|
| 391 |
} else { |
| 392 |
log_error("Unknown type '%s' in line %d.\n", type.c_str(), lineno); |
| 393 |
} |
| 394 |
|
| 395 |
linebuf = linebuf.substr(pos + 1); |
| 396 |
pos = linebuf.find(';'); |
| 397 |
} |
| 398 |
} |
| 399 |
if (!isempty(linebuf)) |
| 400 |
log_error("Unexpected end of CCF file.\n"); |
| 401 |
int max_num = 0; |
| 402 |
uarch->preferred_die = 0; |
| 403 |
for (int i = 0; i < uarch->dies; i++) { |
| 404 |
if (count[i] > max_num) { |
| 405 |
max_num = count[i]; |
| 406 |
uarch->preferred_die = i; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
}; |
| 411 |
|
| 412 |
void GateMateImpl::parse_ccf(const std::string &filename) |
| 413 |
{ |
| 414 |
std::ifstream in(filename); |
| 415 |
if (!in) |
| 416 |
log_error("Failed to open CCF file '%s'.\n", filename.c_str()); |
| 417 |
GateMateCCFReader reader(ctx, this, in); |
| 418 |
reader.run(); |
| 419 |
} |
| 420 |
|
| 421 |
NEXTPNR_NAMESPACE_END |