| 1 |
#include <boost/program_options.hpp> |
| 2 |
#include <fstream> |
| 3 |
#include <functional> |
| 4 |
#include <regex> |
| 5 |
|
| 6 |
#include "log.h" |
| 7 |
#include "pcf.h" |
| 8 |
#include "util.h" |
| 9 |
|
| 10 |
#define VIADUCT_CONSTIDS "viaduct/fabulous/constids.inc" |
| 11 |
#include "viaduct_constids.h" |
| 12 |
|
| 13 |
NEXTPNR_NAMESPACE_BEGIN |
| 14 |
|
| 15 |
namespace { |
| 16 |
namespace po = boost::program_options; |
| 17 |
|
| 18 |
// Command parser structure for PCF commands using boost::program_options |
| 19 |
struct PCFCommand |
| 20 |
{ |
| 21 |
std::string name; |
| 22 |
po::options_description desc; |
| 23 |
po::positional_options_description pos; |
| 24 |
std::function<void(const po::variables_map &, int)> handler; |
| 25 |
|
| 26 |
PCFCommand() : desc("") {} |
| 27 |
PCFCommand(const std::string &name, const std::string &description) : name(name), desc(description) {} |
| 28 |
}; |
| 29 |
|
| 30 |
struct FABulousDesignConstraints |
| 31 |
{ |
| 32 |
Context *ctx; |
| 33 |
std::string filename; |
| 34 |
int lineno = 0; |
| 35 |
std::map<std::string, PCFCommand> commands; |
| 36 |
|
| 37 |
FABulousDesignConstraints(Context *ctx, const std::string &filename) : ctx(ctx), filename(filename) |
| 38 |
{ |
| 39 |
setup_commands(); |
| 40 |
} |
| 41 |
|
| 42 |
bool parse_loc_from_string(const std::string &s, Loc &loc) const |
| 43 |
{ |
| 44 |
static const std::regex loc_re(R"(X(\d+)Y(\d+)/\w+)"); |
| 45 |
std::smatch match; |
| 46 |
if (!std::regex_search(s, match, loc_re)) |
| 47 |
return false; |
| 48 |
loc = Loc(std::stoi(match[1].str()), std::stoi(match[2].str()), 0); |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
// Find the iopadmap-created IO cell whose PAD port is connected to `net`. |
| 53 |
// Errors if multiple distinct IO cells share the same net. |
| 54 |
CellInfo *find_pad_peer(NetInfo *net, int line_number) const |
| 55 |
{ |
| 56 |
if (!net) |
| 57 |
return nullptr; |
| 58 |
|
| 59 |
if (net->driver.cell && net->driver.port == id_PAD) |
| 60 |
return net->driver.cell; |
| 61 |
|
| 62 |
CellInfo *found = nullptr; |
| 63 |
for (auto &usr : net->users) { |
| 64 |
if (usr.port != id_PAD) |
| 65 |
continue; |
| 66 |
if (found && usr.cell != found) |
| 67 |
log_error("Multiple IO cells connected via PAD on net '%s' (on line %d)\n", net->name.c_str(ctx), |
| 68 |
line_number); |
| 69 |
found = usr.cell; |
| 70 |
} |
| 71 |
return found; |
| 72 |
} |
| 73 |
|
| 74 |
void execute_set_io_command(const po::variables_map &vm, int line_number) |
| 75 |
{ |
| 76 |
std::string cell = vm["cell"].as<std::string>(); |
| 77 |
std::string pin = vm["pin"].as<std::string>(); |
| 78 |
|
| 79 |
auto buf_it = ctx->cells.find(ctx->id(cell)); |
| 80 |
if (buf_it == ctx->cells.end()) { |
| 81 |
if (ctx->debug) |
| 82 |
log_info("Ignoring constraint for '%s': port does not exist (on line %d)\n", cell.c_str(), line_number); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
CellInfo *buf_ci = buf_it->second.get(); |
| 87 |
if (!buf_ci->type.in(ctx->id("$nextpnr_ibuf"), ctx->id("$nextpnr_obuf"), ctx->id("$nextpnr_iobuf"))) |
| 88 |
log_error("Can only constrain IO cells (on line %d)\n", line_number); |
| 89 |
|
| 90 |
CellInfo *io_cell = find_pad_peer(buf_ci->getPort(id_O), line_number); |
| 91 |
if (!io_cell) |
| 92 |
io_cell = find_pad_peer(buf_ci->getPort(id_I), line_number); |
| 93 |
if (!io_cell) |
| 94 |
log_error("No IO cell found connected to '%s' via PAD port (on line %d). " |
| 95 |
"Was iopadmap run in Yosys?\n", |
| 96 |
cell.c_str(), line_number); |
| 97 |
|
| 98 |
BelId pin_bel = ctx->getBelByNameStr(pin); |
| 99 |
if (pin_bel == BelId()) |
| 100 |
log_error("Cannot find a pin named '%s' (on line %d)\n", pin.c_str(), line_number); |
| 101 |
|
| 102 |
if (ctx->getBelType(pin_bel) != io_cell->type) |
| 103 |
log_error("Pin '%s' bel type '%s' does not match IO cell type '%s' (on line %d)\n", pin.c_str(), |
| 104 |
ctx->getBelType(pin_bel).c_str(ctx), io_cell->type.c_str(ctx), line_number); |
| 105 |
|
| 106 |
if (io_cell->attrs.count(id_BEL)) |
| 107 |
log_error("duplicate pin constraint on '%s' (on line %d)\n", cell.c_str(), line_number); |
| 108 |
|
| 109 |
io_cell->attrs[id_BEL] = ctx->getBelName(pin_bel).str(ctx); |
| 110 |
log_info("constrained '%s' to bel '%s'\n", cell.c_str(), io_cell->attrs[id_BEL].as_string().c_str()); |
| 111 |
} |
| 112 |
|
| 113 |
void execute_set_frequency_command(const po::variables_map &vm, int line_number) |
| 114 |
{ |
| 115 |
std::string net = vm["net"].as<std::string>(); |
| 116 |
float frequency = vm["frequency"].as<float>(); |
| 117 |
|
| 118 |
if (frequency <= 0.0f) { |
| 119 |
log_error("frequency must be positive (on line %d)\n", line_number); |
| 120 |
} |
| 121 |
|
| 122 |
ctx->addClock(ctx->id(net), frequency); |
| 123 |
log_info("set frequency constraint: %s = %.3f MHz\n", net.c_str(), frequency); |
| 124 |
} |
| 125 |
|
| 126 |
void execute_set_cell_command(const po::variables_map &vm, int line_number) |
| 127 |
{ |
| 128 |
std::string cell = vm["cell"].as<std::string>(); |
| 129 |
std::string bel = vm["bel"].as<std::string>(); |
| 130 |
|
| 131 |
auto fnd_cell = ctx->cells.find(ctx->id(cell)); |
| 132 |
if (fnd_cell == ctx->cells.end()) { |
| 133 |
log_warning("unmatched constraint '%s' (on line %d)\n", cell.c_str(), line_number); |
| 134 |
} else { |
| 135 |
BelId targetBel = ctx->getBelByNameStr(bel); |
| 136 |
if (targetBel == BelId()) |
| 137 |
log_error("package does not have a bel named '%s' (on line %d)\n", bel.c_str(), line_number); |
| 138 |
if (fnd_cell->second->attrs.count(id_BEL)) |
| 139 |
log_error("duplicate bel constraint on '%s' (on line %d)\n", cell.c_str(), line_number); |
| 140 |
fnd_cell->second->attrs[id_BEL] = ctx->getBelName(targetBel).str(ctx); |
| 141 |
log_info("constrained '%s' to bel '%s'\n", cell.c_str(), |
| 142 |
fnd_cell->second->attrs[id_BEL].as_string().c_str()); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void execute_pseudo_plug_command(const po::variables_map &vm, int line_number) |
| 147 |
{ |
| 148 |
std::string plug_name = vm["plug-name"].as<std::string>(); |
| 149 |
IdString plug_name_id = ctx->id(plug_name); |
| 150 |
size_t port_count = vm.count("port") ? vm["port"].as<std::vector<std::string>>().size() : 0; |
| 151 |
|
| 152 |
// Find the plug cell |
| 153 |
CellInfo *plug = nullptr; |
| 154 |
if (ctx->cells.count(plug_name_id)) { |
| 155 |
plug = ctx->cells.at(plug_name_id).get(); |
| 156 |
if (plug->ports.size() != port_count && port_count > 0) { |
| 157 |
log_error("Port count on pseudo-plug '%s' (%d) does not match number of --port mappings (%ld) (on " |
| 158 |
"line %d). A pseudo-plug need to be fully constrained\n", |
| 159 |
plug_name.c_str(), int(plug->ports.size()), port_count, line_number); |
| 160 |
} |
| 161 |
} else { |
| 162 |
log_error("Cannot find cell '%s' (on line %d)\n", plug_name.c_str(), line_number); |
| 163 |
} |
| 164 |
|
| 165 |
if (!plug->isPseudo()) |
| 166 |
ctx->createRegionPlug(plug_name_id, plug->type, Loc(0, 0, 0)); |
| 167 |
|
| 168 |
// Process port mappings |
| 169 |
if (vm.count("port")) { |
| 170 |
const auto &port_specs = vm["port"].as<std::vector<std::string>>(); |
| 171 |
for (const auto &mapping : port_specs) { |
| 172 |
// Parse port:wire format |
| 173 |
size_t colon_pos = mapping.find(':'); |
| 174 |
if (colon_pos == std::string::npos || colon_pos == 0 || colon_pos == mapping.length() - 1) { |
| 175 |
log_error("Invalid port:wire mapping format '%s' (expected port:wire) (on line %d)\n", |
| 176 |
mapping.c_str(), line_number); |
| 177 |
} |
| 178 |
std::string port_name = mapping.substr(0, colon_pos); |
| 179 |
std::string wire_name = mapping.substr(colon_pos + 1); |
| 180 |
|
| 181 |
Loc wire_loc; |
| 182 |
if (!parse_loc_from_string(wire_name, wire_loc)) |
| 183 |
log_error("Cannot parse location from '%s' (expected X<num>Y<num>/...) (on line %d)\n", |
| 184 |
wire_name.c_str(), line_number); |
| 185 |
|
| 186 |
RegionPlug *rplug = dynamic_cast<RegionPlug *>(plug->pseudo_cell.get()); |
| 187 |
rplug->loc = wire_loc; |
| 188 |
|
| 189 |
IdString port_name_id = ctx->id(port_name); |
| 190 |
WireId wire = ctx->getWireByNameStr(wire_name); |
| 191 |
|
| 192 |
if (wire == WireId()) |
| 193 |
log_error("Cannot find wire '%s' (on line %d)\n", wire_name.c_str(), line_number); |
| 194 |
|
| 195 |
auto bel_pins = ctx->getBelPinsForCellPin(plug, port_name_id); |
| 196 |
if (bel_pins.empty()) |
| 197 |
log_error("Cannot find port '%s' on cell '%s' (on line %d)\n", port_name.c_str(), plug_name.c_str(), |
| 198 |
line_number); |
| 199 |
|
| 200 |
PortType dir; |
| 201 |
if (plug->ports.count(port_name_id)) { |
| 202 |
dir = plug->ports[port_name_id].type; |
| 203 |
} else { |
| 204 |
log_error("port '%s' not found on cell '%s' (on line %d)\n", port_name.c_str(), plug_name.c_str(), |
| 205 |
line_number); |
| 206 |
} |
| 207 |
|
| 208 |
if (bel_pins.size() > 1) |
| 209 |
log_warning("Port '%s' on cell '%s' has multiple possible pin mappings, using first\n", |
| 210 |
port_name.c_str(), plug_name.c_str()); |
| 211 |
|
| 212 |
ctx->addPlugPin(plug_name_id, bel_pins[0], dir, wire); |
| 213 |
log_info("constrained pseudo-plug '%s' port '%s' to wire '%s'\n", plug_name.c_str(), port_name.c_str(), |
| 214 |
wire_name.c_str()); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Process timing constraints |
| 219 |
if (vm.count("timing")) { |
| 220 |
log_warning("Timing constraints on pseudo-plugs are currently NOT respected and will be implemented in the " |
| 221 |
"future (on line %d)\n", |
| 222 |
line_number); |
| 223 |
const auto &timing_specs = vm["timing"].as<std::vector<std::string>>(); |
| 224 |
for (const auto &spec : timing_specs) { |
| 225 |
// Parse port-in:port-out:min-delay:max-delay format |
| 226 |
std::vector<std::string> parts; |
| 227 |
std::stringstream spec_ss(spec); |
| 228 |
std::string part; |
| 229 |
while (std::getline(spec_ss, part, ':')) { |
| 230 |
parts.push_back(part); |
| 231 |
} |
| 232 |
|
| 233 |
if (parts.size() != 4) { |
| 234 |
log_error("Invalid timing constraint format '%s' (expected " |
| 235 |
"port-in:port-out:min-delay:max-delay) (on line %d)\n", |
| 236 |
spec.c_str(), line_number); |
| 237 |
} |
| 238 |
|
| 239 |
std::string port_in = parts[0]; |
| 240 |
std::string port_out = parts[1]; |
| 241 |
float min_delay = 0.0f, max_delay = 0.0f; |
| 242 |
try { |
| 243 |
min_delay = std::stof(parts[2]); |
| 244 |
max_delay = std::stof(parts[3]); |
| 245 |
} catch (const std::exception &) { |
| 246 |
log_error("Invalid numeric value in timing constraint '%s' (on line %d)\n", spec.c_str(), |
| 247 |
line_number); |
| 248 |
} |
| 249 |
|
| 250 |
if (min_delay < 0.0f) { |
| 251 |
log_error("min-delay must be non-negative in timing constraint '%s' (on line %d)\n", spec.c_str(), |
| 252 |
line_number); |
| 253 |
} |
| 254 |
if (max_delay < 0.0f) { |
| 255 |
log_error("max-delay must be non-negative in timing constraint '%s' (on line %d)\n", spec.c_str(), |
| 256 |
line_number); |
| 257 |
} |
| 258 |
if (min_delay > max_delay) { |
| 259 |
log_error("min-delay (%.3f) cannot be greater than max-delay (%.3f) in timing constraint '%s' " |
| 260 |
"(on line %d)\n", |
| 261 |
min_delay, max_delay, spec.c_str(), line_number); |
| 262 |
} |
| 263 |
|
| 264 |
IdString port_in_id = ctx->id(port_in); |
| 265 |
IdString port_out_id = ctx->id(port_out); |
| 266 |
|
| 267 |
if (!plug->ports.count(port_in_id) || plug->ports.at(port_in_id).type != PORT_IN) |
| 268 |
log_error("input port '%s' not found on cell '%s' (on line %d)\n", port_in.c_str(), |
| 269 |
plug_name.c_str(), line_number); |
| 270 |
if (!plug->ports.count(port_out_id) || plug->ports.at(port_out_id).type != PORT_OUT) |
| 271 |
log_error("output port '%s' not found on cell '%s' (on line %d)\n", port_out.c_str(), |
| 272 |
plug_name.c_str(), line_number); |
| 273 |
|
| 274 |
ctx->addCellTimingDelayMinMax(plug_name_id, port_in_id, port_out_id, min_delay, max_delay); |
| 275 |
log_info("applied timing constraint %.3f-%.3f ns from port '%s' to port '%s' on pseudo-plug '%s'\n", |
| 276 |
min_delay, max_delay, port_in.c_str(), port_out.c_str(), plug_name.c_str()); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void execute_prohibit_pip_command(const po::variables_map &vm, int line_number) |
| 282 |
{ |
| 283 |
std::string pip_pattern = vm["pip"].as<std::string>(); |
| 284 |
std::string dummy_net_name = "$prohibit_pip"; |
| 285 |
NetInfo *dummy_net = nullptr; |
| 286 |
if (ctx->nets.count(ctx->id(dummy_net_name))) |
| 287 |
dummy_net = ctx->nets.at(ctx->id(dummy_net_name)).get(); |
| 288 |
else |
| 289 |
dummy_net = ctx->createNet(ctx->id(dummy_net_name)); |
| 290 |
|
| 291 |
try { |
| 292 |
std::regex pip_regex(pip_pattern); |
| 293 |
std::vector<PipId> matching_pips; |
| 294 |
|
| 295 |
// Find all pips matching the regex pattern |
| 296 |
for (auto pip : ctx->getPips()) { |
| 297 |
std::string pip_name = ctx->nameOfPip(pip); |
| 298 |
if (std::regex_match(pip_name, pip_regex)) { |
| 299 |
matching_pips.push_back(pip); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
if (matching_pips.empty()) { |
| 304 |
log_error("No pips found matching pattern '%s' (on line %d)\n", pip_pattern.c_str(), line_number); |
| 305 |
} |
| 306 |
|
| 307 |
// Prohibit all matching pips |
| 308 |
for (auto pip : matching_pips) { |
| 309 |
ctx->bindPip(pip, dummy_net, STRENGTH_USER); |
| 310 |
if (ctx->debug) |
| 311 |
log_info("forbade pip '%s' by binding to dummy net '%s'\n", ctx->nameOfPip(pip), |
| 312 |
dummy_net_name.c_str()); |
| 313 |
} |
| 314 |
|
| 315 |
log_info("Prohibited %d pips matching pattern '%s'\n", static_cast<int>(matching_pips.size()), |
| 316 |
pip_pattern.c_str()); |
| 317 |
|
| 318 |
} catch (const std::regex_error &e) { |
| 319 |
log_error("Invalid regex pattern '%s' in prohibit_pip command (on line %d): %s\n", pip_pattern.c_str(), |
| 320 |
line_number, e.what()); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
void execute_prohibit_wire_command(const po::variables_map &vm, int line_number) |
| 325 |
{ |
| 326 |
std::string wire_pattern = vm["wire"].as<std::string>(); |
| 327 |
std::string dummy_net_name = "$prohibit_wire"; |
| 328 |
NetInfo *dummy_net = nullptr; |
| 329 |
if (ctx->nets.count(ctx->id(dummy_net_name))) |
| 330 |
dummy_net = ctx->nets.at(ctx->id(dummy_net_name)).get(); |
| 331 |
else |
| 332 |
dummy_net = ctx->createNet(ctx->id(dummy_net_name)); |
| 333 |
|
| 334 |
try { |
| 335 |
std::regex wire_regex(wire_pattern); |
| 336 |
std::vector<WireId> matching_wires; |
| 337 |
|
| 338 |
// Find all wires matching the regex pattern |
| 339 |
for (auto wire : ctx->getWires()) { |
| 340 |
std::string wire_name = ctx->nameOfWire(wire); |
| 341 |
if (std::regex_match(wire_name, wire_regex)) { |
| 342 |
matching_wires.push_back(wire); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (matching_wires.empty()) { |
| 347 |
log_error("No wires found matching pattern '%s' (on line %d)\n", wire_pattern.c_str(), line_number); |
| 348 |
} |
| 349 |
|
| 350 |
// Prohibit all matching wires |
| 351 |
for (auto wire : matching_wires) { |
| 352 |
ctx->bindWire(wire, dummy_net, STRENGTH_USER); |
| 353 |
if (ctx->debug) |
| 354 |
log_info("forbade wire '%s' by binding to dummy net '%s'\n", ctx->nameOfWire(wire), |
| 355 |
dummy_net_name.c_str()); |
| 356 |
} |
| 357 |
|
| 358 |
log_info("Prohibited %d wires matching pattern '%s'\n", static_cast<int>(matching_wires.size()), |
| 359 |
wire_pattern.c_str()); |
| 360 |
|
| 361 |
} catch (const std::regex_error &e) { |
| 362 |
log_error("Invalid regex pattern '%s' in prohibit_wire command (on line %d): %s\n", wire_pattern.c_str(), |
| 363 |
line_number, e.what()); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
void execute_prohibit_bel_command(const po::variables_map &vm, int line_number) |
| 368 |
{ |
| 369 |
std::string bel_pattern = vm["bel"].as<std::string>(); |
| 370 |
|
| 371 |
try { |
| 372 |
std::regex bel_regex(bel_pattern); |
| 373 |
std::vector<BelId> matching_bels; |
| 374 |
|
| 375 |
// Find all bels matching the regex pattern |
| 376 |
for (auto bel : ctx->getBels()) { |
| 377 |
std::string bel_name = ctx->nameOfBel(bel); |
| 378 |
if (std::regex_match(bel_name, bel_regex)) { |
| 379 |
matching_bels.push_back(bel); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if (matching_bels.empty()) { |
| 384 |
log_error("No bels found matching pattern '%s' (on line %d)\n", bel_pattern.c_str(), line_number); |
| 385 |
} |
| 386 |
|
| 387 |
// Prohibit all matching bels |
| 388 |
for (auto bel : matching_bels) { |
| 389 |
std::string bel_name = ctx->nameOfBel(bel); |
| 390 |
std::string dummy_cell_name = "$prohibit_bel_" + bel_name; |
| 391 |
IdString bel_type = ctx->getBelType(bel); |
| 392 |
CellInfo *dummy_cell = ctx->createCell(ctx->id(dummy_cell_name), bel_type); |
| 393 |
|
| 394 |
ctx->bindBel(bel, dummy_cell, STRENGTH_USER); |
| 395 |
if (ctx->debug) |
| 396 |
log_info("forbade bel '%s' by binding to dummy cell '%s'\n", bel_name.c_str(), |
| 397 |
dummy_cell_name.c_str()); |
| 398 |
} |
| 399 |
|
| 400 |
log_info("Prohibited %d bels matching pattern '%s'\n", static_cast<int>(matching_bels.size()), |
| 401 |
bel_pattern.c_str()); |
| 402 |
|
| 403 |
} catch (const std::regex_error &e) { |
| 404 |
log_error("Invalid regex pattern '%s' in prohibit_bel command (on line %d): %s\n", bel_pattern.c_str(), |
| 405 |
line_number, e.what()); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void setup_commands() |
| 410 |
{ |
| 411 |
// Setup set_io command |
| 412 |
// Syntax: set_io cell pin |
| 413 |
commands.emplace("set_io", PCFCommand("set_io", "Constrain IO cell to pin")); |
| 414 |
auto &set_io = commands.at("set_io"); |
| 415 |
set_io.desc.add_options()("cell", po::value<std::string>()->required(), |
| 416 |
"Cell name")("pin", po::value<std::string>()->required(), "Pin name"); |
| 417 |
set_io.pos.add("cell", 1); |
| 418 |
set_io.pos.add("pin", 1); |
| 419 |
set_io.handler = [this](const po::variables_map &vm, int line_number) { |
| 420 |
execute_set_io_command(vm, line_number); |
| 421 |
}; |
| 422 |
|
| 423 |
// Setup set_frequency command |
| 424 |
// Syntax: set_frequency net frequency |
| 425 |
commands.emplace("set_frequency", PCFCommand("set_frequency", "Set clock frequency constraint")); |
| 426 |
auto &set_freq = commands.at("set_frequency"); |
| 427 |
set_freq.desc.add_options()("net", po::value<std::string>()->required(), |
| 428 |
"Net name")("frequency", po::value<float>()->required(), "Frequency in MHz"); |
| 429 |
set_freq.pos.add("net", 1); |
| 430 |
set_freq.pos.add("frequency", 1); |
| 431 |
set_freq.handler = [this](const po::variables_map &vm, int line_number) { |
| 432 |
execute_set_frequency_command(vm, line_number); |
| 433 |
}; |
| 434 |
|
| 435 |
// Setup set_cell command |
| 436 |
// Syntax: set_cell cell bel |
| 437 |
commands.emplace("set_cell", PCFCommand("set_cell", "Constrain cell to bel")); |
| 438 |
auto &set_cell = commands.at("set_cell"); |
| 439 |
set_cell.desc.add_options()("cell", po::value<std::string>()->required(), |
| 440 |
"Cell name")("bel", po::value<std::string>()->required(), "Bel name"); |
| 441 |
set_cell.pos.add("cell", 1); |
| 442 |
set_cell.pos.add("bel", 1); |
| 443 |
set_cell.handler = [this](const po::variables_map &vm, int line_number) { |
| 444 |
execute_set_cell_command(vm, line_number); |
| 445 |
}; |
| 446 |
|
| 447 |
// Setup set_pseudo_plug command with flag-based interface and timing constraints support |
| 448 |
// Syntax: set_pseudo_plug plug_name --port <port>:<wire> --timing <port-in>:<port-out>:<min>:<max> |
| 449 |
commands.emplace("set_pseudo_plug", |
| 450 |
PCFCommand("set_pseudo_plug", |
| 451 |
"Configure pseudo plug with flag-based port mappings and timing constraints")); |
| 452 |
auto &pseudo_plug = commands.at("set_pseudo_plug"); |
| 453 |
pseudo_plug.desc.add_options()("plug-name", po::value<std::string>()->required(), "Pseudo plug cell name")( |
| 454 |
"port", po::value<std::vector<std::string>>()->multitoken(), |
| 455 |
"Port mapping in format port:wire (repeatable)")( |
| 456 |
"timing", po::value<std::vector<std::string>>()->multitoken(), |
| 457 |
"Timing constraint in format port-in:port-out:min-delay:max-delay (repeatable)"); |
| 458 |
pseudo_plug.pos.add("plug-name", 1); |
| 459 |
pseudo_plug.handler = [this](const po::variables_map &vm, int line_number) { |
| 460 |
execute_pseudo_plug_command(vm, line_number); |
| 461 |
}; |
| 462 |
|
| 463 |
// Setup prohibit_pip command |
| 464 |
// Syntax: prohibit_pip pip_pattern |
| 465 |
commands.emplace("prohibit_pip", PCFCommand("prohibit_pip", "Prohibit use of pips matching regex pattern")); |
| 466 |
auto &prohibit_pip = commands.at("prohibit_pip"); |
| 467 |
prohibit_pip.desc.add_options()("pip", po::value<std::string>()->required(), |
| 468 |
"Pip name pattern (regex) to prohibit"); |
| 469 |
prohibit_pip.pos.add("pip", 1); |
| 470 |
prohibit_pip.handler = [this](const po::variables_map &vm, int line_number) { |
| 471 |
execute_prohibit_pip_command(vm, line_number); |
| 472 |
}; |
| 473 |
|
| 474 |
// Setup prohibit_wire command |
| 475 |
// Syntax: prohibit_wire wire_pattern |
| 476 |
commands.emplace("prohibit_wire", PCFCommand("prohibit_wire", "Prohibit use of wires matching regex pattern")); |
| 477 |
auto &prohibit_wire = commands.at("prohibit_wire"); |
| 478 |
prohibit_wire.desc.add_options()("wire", po::value<std::string>()->required(), |
| 479 |
"Wire name pattern (regex) to prohibit"); |
| 480 |
prohibit_wire.pos.add("wire", 1); |
| 481 |
prohibit_wire.handler = [this](const po::variables_map &vm, int line_number) { |
| 482 |
execute_prohibit_wire_command(vm, line_number); |
| 483 |
}; |
| 484 |
|
| 485 |
// Setup prohibit_bel command |
| 486 |
// Syntax: prohibit_bel bel_pattern |
| 487 |
commands.emplace("prohibit_bel", PCFCommand("prohibit_bel", "Prohibit use of bels matching regex pattern")); |
| 488 |
auto &prohibit_bel = commands.at("prohibit_bel"); |
| 489 |
prohibit_bel.desc.add_options()("bel", po::value<std::string>()->required(), |
| 490 |
"Bel name pattern (regex) to prohibit"); |
| 491 |
prohibit_bel.pos.add("bel", 1); |
| 492 |
prohibit_bel.handler = [this](const po::variables_map &vm, int line_number) { |
| 493 |
execute_prohibit_bel_command(vm, line_number); |
| 494 |
}; |
| 495 |
} |
| 496 |
|
| 497 |
bool parse_and_execute_command(const std::vector<std::string> &words) |
| 498 |
{ |
| 499 |
if (words.empty()) |
| 500 |
return false; |
| 501 |
|
| 502 |
std::string cmd_name = words[0]; |
| 503 |
auto it = commands.find(cmd_name); |
| 504 |
if (it == commands.end()) { |
| 505 |
log_error("unsupported command '%s' (on line %d)\n", cmd_name.c_str(), lineno); |
| 506 |
return false; |
| 507 |
} |
| 508 |
|
| 509 |
try { |
| 510 |
// Convert words to argc/argv style for program_options |
| 511 |
std::vector<const char *> argv; |
| 512 |
for (const auto &word : words) { |
| 513 |
argv.push_back(word.c_str()); |
| 514 |
} |
| 515 |
|
| 516 |
po::variables_map vm; |
| 517 |
|
| 518 |
po::parsed_options parsed = po::command_line_parser(static_cast<int>(argv.size()), argv.data()) |
| 519 |
.options(it->second.desc) |
| 520 |
.positional(it->second.pos) |
| 521 |
.run(); |
| 522 |
|
| 523 |
po::store(parsed, vm); |
| 524 |
po::notify(vm); |
| 525 |
|
| 526 |
// Execute the command |
| 527 |
it->second.handler(vm, lineno); |
| 528 |
return true; |
| 529 |
|
| 530 |
} catch (const po::error &e) { |
| 531 |
log_error("Error parsing command '%s' on line %d: %s\n", cmd_name.c_str(), lineno, e.what()); |
| 532 |
} catch (const std::exception &e) { |
| 533 |
log_error("Error executing command '%s' on line %d: %s\n", cmd_name.c_str(), lineno, e.what()); |
| 534 |
} |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
void apply_constraints() |
| 539 |
{ |
| 540 |
std::ifstream in(filename); |
| 541 |
if (!in) { |
| 542 |
log_error("failed to open constraint file\n"); |
| 543 |
} |
| 544 |
|
| 545 |
std::string line; |
| 546 |
std::string accumulated_line; |
| 547 |
lineno = 0; |
| 548 |
int command_start_line = 0; |
| 549 |
|
| 550 |
while (std::getline(in, line)) { |
| 551 |
lineno++; |
| 552 |
|
| 553 |
// Remove comments before checking for continuation |
| 554 |
size_t cstart = line.find('#'); |
| 555 |
if (cstart != std::string::npos) |
| 556 |
line = line.substr(0, cstart); |
| 557 |
|
| 558 |
// Trim trailing whitespace |
| 559 |
while (!line.empty() && std::isspace(line.back())) |
| 560 |
line.pop_back(); |
| 561 |
|
| 562 |
// Check for line continuation (ends with backslash) |
| 563 |
bool has_continuation = false; |
| 564 |
if (!line.empty() && line.back() == '\\') { |
| 565 |
has_continuation = true; |
| 566 |
line.pop_back(); // Remove the backslash |
| 567 |
// Trim any whitespace before the backslash |
| 568 |
while (!line.empty() && std::isspace(line.back())) |
| 569 |
line.pop_back(); |
| 570 |
} |
| 571 |
|
| 572 |
// If this is the start of a new command, record the line number |
| 573 |
if (accumulated_line.empty() && !line.empty()) { |
| 574 |
command_start_line = lineno; |
| 575 |
} |
| 576 |
|
| 577 |
// Accumulate the line |
| 578 |
if (!accumulated_line.empty() && !line.empty()) { |
| 579 |
accumulated_line += " "; // Add space between continued lines |
| 580 |
} |
| 581 |
accumulated_line += line; |
| 582 |
|
| 583 |
// If no continuation, process the accumulated command |
| 584 |
if (!has_continuation) { |
| 585 |
// Parse the accumulated command |
| 586 |
std::stringstream ss(accumulated_line); |
| 587 |
std::vector<std::string> words; |
| 588 |
std::string tmp; |
| 589 |
while (ss >> tmp) |
| 590 |
words.push_back(tmp); |
| 591 |
|
| 592 |
if (!words.empty()) { |
| 593 |
// Set line number to the start of the command for error reporting |
| 594 |
int saved_lineno = lineno; |
| 595 |
lineno = command_start_line; |
| 596 |
parse_and_execute_command(words); |
| 597 |
lineno = saved_lineno; |
| 598 |
} |
| 599 |
|
| 600 |
// Reset for next command |
| 601 |
accumulated_line.clear(); |
| 602 |
command_start_line = 0; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
// Handle case where file ends with a continuation (error) |
| 607 |
if (!accumulated_line.empty()) { |
| 608 |
log_error("File ends with incomplete command starting at line %d (missing continuation or final command)\n", |
| 609 |
command_start_line); |
| 610 |
} |
| 611 |
} |
| 612 |
}; |
| 613 |
} // namespace |
| 614 |
|
| 615 |
void fabulous_pcf(Context *ctx, const std::string &filename) |
| 616 |
{ |
| 617 |
FABulousDesignConstraints PCF(ctx, filename); |
| 618 |
PCF.apply_constraints(); |
| 619 |
log_info("Finished applying constraints from '%s'\n", filename.c_str()); |
| 620 |
} |
| 621 |
|
| 622 |
NEXTPNR_NAMESPACE_END |