| 1 |
/* |
| 2 |
* nextpnr -- Next Generation Place and Route |
| 3 |
* |
| 4 |
* Copyright (C) 2024 The Project Beyond 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 <boost/algorithm/string.hpp> |
| 21 |
#include <boost/range/adaptor/reversed.hpp> |
| 22 |
|
| 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 "ng_ultra.h" |
| 33 |
|
| 34 |
#define HIMBAECHEL_CONSTIDS "uarch/ng-ultra/constids.inc" |
| 35 |
#include "himbaechel_constids.h" |
| 36 |
|
| 37 |
NEXTPNR_NAMESPACE_BEGIN |
| 38 |
|
| 39 |
namespace { |
| 40 |
struct BitstreamJsonBackend |
| 41 |
{ |
| 42 |
Context *ctx; |
| 43 |
NgUltraImpl *uarch; |
| 44 |
std::ostream &out; |
| 45 |
bool first_instance; |
| 46 |
|
| 47 |
BitstreamJsonBackend(Context *ctx, NgUltraImpl *uarch, std::ostream &out) : ctx(ctx), uarch(uarch), out(out) {}; |
| 48 |
|
| 49 |
std::string get_string(std::string str) |
| 50 |
{ |
| 51 |
std::string newstr = "\""; |
| 52 |
for (char c : str) { |
| 53 |
if (c == '\\') |
| 54 |
newstr += c; |
| 55 |
newstr += c; |
| 56 |
} |
| 57 |
return newstr + "\""; |
| 58 |
} |
| 59 |
|
| 60 |
std::string update_name(std::string tile, std::string name) |
| 61 |
{ |
| 62 |
if (boost::starts_with(tile, "FENCE[")) { |
| 63 |
char last = tile[tile.size() - 2]; |
| 64 |
switch (last) { |
| 65 |
case 'T': |
| 66 |
case 'B': |
| 67 |
case 'U': |
| 68 |
case 'L': |
| 69 |
std::string loc = tile.substr(tile.find("[") + 1, tile.find("x") - tile.find("[")); |
| 70 |
boost::replace_all(name, "1x", loc); |
| 71 |
return name; |
| 72 |
} |
| 73 |
} |
| 74 |
if (boost::starts_with(tile, "TILE[") && boost::algorithm::contains(name, ".FE")) { |
| 75 |
std::string last = name.substr(name.rfind('.') + 1); |
| 76 |
if (last[0] == 'D') { |
| 77 |
boost::replace_all(name, ".D", "."); |
| 78 |
boost::replace_all(name, ".FE", ".DFF"); |
| 79 |
return name; |
| 80 |
} |
| 81 |
if (last == "L" || last == "R") { |
| 82 |
boost::replace_all(name, ".FE", ".DFF"); |
| 83 |
return name; |
| 84 |
} |
| 85 |
if (last == "CK") { |
| 86 |
boost::replace_all(name, ".FE", ".DFF"); |
| 87 |
return name; |
| 88 |
} |
| 89 |
if (last[0] == 'L') { |
| 90 |
boost::replace_all(name, ".L", "."); |
| 91 |
boost::replace_all(name, ".FE", ".LUT"); |
| 92 |
return name; |
| 93 |
} |
| 94 |
if (last[0] == 'P') { |
| 95 |
boost::replace_all(name, ".PI", ".I"); |
| 96 |
boost::replace_all(name, ".FE", ".LUT"); |
| 97 |
return name; |
| 98 |
} |
| 99 |
} |
| 100 |
return name; |
| 101 |
} |
| 102 |
|
| 103 |
void add_net(std::set<std::string> &nets, std::string src_tile, std::string src_name, std::string dst_tile, |
| 104 |
std::string dst_name, IdString src_type, IdString dst_type) |
| 105 |
{ |
| 106 |
if (src_type.in(ctx->id("LUT_PERMUTATION_WIRE"), ctx->id("MUX_WIRE"), ctx->id("INTERCONNECT_INPUT"))) |
| 107 |
return; |
| 108 |
if (boost::starts_with(src_type.c_str(ctx), "CROSSBAR_") && boost::ends_with(src_type.c_str(ctx), "INPUT_WIRE")) |
| 109 |
return; |
| 110 |
if (dst_type == ctx->id("MUX_WIRE")) |
| 111 |
dst_name = dst_name.substr(0, dst_name.rfind('.')); |
| 112 |
src_name = update_name(src_tile, src_name); |
| 113 |
dst_name = update_name(dst_tile, dst_name); |
| 114 |
|
| 115 |
nets.emplace(stringf("%s:%s->%s:%s", src_tile.c_str(), src_name.c_str(), dst_tile.c_str(), dst_name.c_str())); |
| 116 |
} |
| 117 |
|
| 118 |
std::string cleanup_name(std::string name) |
| 119 |
{ |
| 120 |
std::replace(name.begin(), name.end(), '$', '_'); |
| 121 |
return name; |
| 122 |
} |
| 123 |
|
| 124 |
void write_nets() |
| 125 |
{ |
| 126 |
out << "\t\"nets\": {\n"; |
| 127 |
bool first_net = true; |
| 128 |
for (auto &net : ctx->nets) { |
| 129 |
NetInfo *ni = net.second.get(); |
| 130 |
if (ni->wires.empty()) |
| 131 |
continue; |
| 132 |
out << (first_net ? "" : ",\n"); |
| 133 |
first_net = false; |
| 134 |
out << stringf("\t\t%s: [\n", get_string(cleanup_name(ni->name.c_str(ctx))).c_str()); |
| 135 |
std::set<std::string> nets; |
| 136 |
for (auto &w : ni->wires) { |
| 137 |
if (w.second.pip != PipId()) { |
| 138 |
PipId pip = w.second.pip; |
| 139 |
auto &pd = chip_pip_info(ctx->chip_info, pip); |
| 140 |
const auto &extra_data = *uarch->pip_extra_data(pip); |
| 141 |
WireId swire = ctx->getPipSrcWire(pip); |
| 142 |
IdString src = ctx->getWireName(swire)[1]; |
| 143 |
IdString src_type = ctx->getWireType(swire); |
| 144 |
|
| 145 |
IdString src_orig = IdString(chip_tile_info(ctx->chip_info, pip.tile).wires[pd.src_wire].name); |
| 146 |
IdString src_orig_type = |
| 147 |
IdString(chip_tile_info(ctx->chip_info, pip.tile).wires[pd.src_wire].wire_type); |
| 148 |
|
| 149 |
WireId dwire = ctx->getPipDstWire(pip); |
| 150 |
IdString dst = ctx->getWireName(dwire)[1]; |
| 151 |
IdString dst_type = ctx->getWireType(dwire); |
| 152 |
|
| 153 |
std::string s_tile_name = uarch->tile_name(swire.tile); |
| 154 |
std::string tile_name = uarch->tile_name(pip.tile); |
| 155 |
|
| 156 |
if (src_orig != src) |
| 157 |
add_net(nets, s_tile_name, src.c_str(ctx), tile_name, src_orig.c_str(ctx), src_type, |
| 158 |
src_orig_type); |
| 159 |
if (!extra_data.name || |
| 160 |
(extra_data.type != PipExtra::PIP_EXTRA_BYPASS && |
| 161 |
extra_data.type != PipExtra::PIP_EXTRA_VIRTUAL && extra_data.type != PipExtra::PIP_EXTRA_MUX)) |
| 162 |
add_net(nets, tile_name, src_orig.c_str(ctx), tile_name, dst.c_str(ctx), src_orig_type, |
| 163 |
dst_type); |
| 164 |
} else if (ni->wires.size() == 1) { |
| 165 |
IdString src = ctx->getWireName(w.first)[1]; |
| 166 |
IdString src_type = ctx->getWireType(w.first); |
| 167 |
std::string s_tile_name = uarch->tile_name(w.first.tile); |
| 168 |
for (auto &u : ni->users) { |
| 169 |
std::string tile_name = uarch->tile_name(u.cell->bel.tile); |
| 170 |
IdString bel_name = ctx->getBelName(u.cell->bel)[1]; |
| 171 |
add_net(nets, s_tile_name, src.c_str(ctx), tile_name, |
| 172 |
stringf("%s.%s", bel_name.c_str(ctx), u.port.c_str(ctx)), src_type, src_type); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
bool first = true; |
| 177 |
for (auto &str : nets) { |
| 178 |
out << (first ? "" : ",\n"); |
| 179 |
out << stringf("\t\t\t%s", get_string(str).c_str()); |
| 180 |
first = false; |
| 181 |
} |
| 182 |
out << "\n\t\t]"; |
| 183 |
} |
| 184 |
out << "\n\t},\n"; |
| 185 |
} |
| 186 |
|
| 187 |
template <typename KeyType> |
| 188 |
std::string str_or_n_value(const dict<KeyType, Property> &ct, const KeyType &key, std::string def = "N") |
| 189 |
{ |
| 190 |
auto found = ct.find(key); |
| 191 |
if (found == ct.end()) |
| 192 |
return def; |
| 193 |
else { |
| 194 |
if (!found->second.is_string) |
| 195 |
log_error("Expecting string value but got integer %d.\n", int(found->second.intval)); |
| 196 |
if (found->second.as_string().empty()) |
| 197 |
return def; |
| 198 |
return found->second.as_string(); |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
template <typename KeyType> |
| 203 |
std::string str_or_n_value_lower(const dict<KeyType, Property> &ct, const KeyType &key, std::string def = "N") |
| 204 |
{ |
| 205 |
auto found = ct.find(key); |
| 206 |
if (found == ct.end()) |
| 207 |
return def; |
| 208 |
else { |
| 209 |
if (!found->second.is_string) |
| 210 |
log_error("Expecting string value but got integer %d.\n", int(found->second.intval)); |
| 211 |
if (found->second.as_string().empty()) |
| 212 |
return def; |
| 213 |
std::string tmp = found->second.as_string(); |
| 214 |
boost::algorithm::to_lower(tmp); |
| 215 |
return tmp; |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
template <typename KeyType> |
| 220 |
std::string extract_bits_or_default(const dict<KeyType, Property> &ct, const KeyType &key, int bits, int def = 0) |
| 221 |
{ |
| 222 |
Property extr = get_or_default(ct, key, Property()).extract(0, bits); |
| 223 |
std::string str = extr.str; |
| 224 |
std::reverse(str.begin(), str.end()); |
| 225 |
return str; |
| 226 |
}; |
| 227 |
|
| 228 |
std::vector<std::string> config; |
| 229 |
|
| 230 |
void open_instance(CellInfo *cell, std::string rename = "") |
| 231 |
{ |
| 232 |
out << stringf("%s", first_instance ? "" : ",\n"); |
| 233 |
first_instance = false; |
| 234 |
out << stringf("\t\t%s: {\n", |
| 235 |
get_string(cleanup_name(rename.empty() ? cell->name.c_str(ctx) : rename.c_str())).c_str()); |
| 236 |
std::string tile_name = uarch->tile_name(cell->bel.tile); |
| 237 |
IdString idx = ctx->getBelName(cell->bel)[1]; |
| 238 |
std::string belname = idx.c_str(ctx); |
| 239 |
config.clear(); |
| 240 |
out << stringf("\t\t\t\"location\": %s,\n", get_string(tile_name + ":" + belname).c_str()); |
| 241 |
out << stringf("\t\t\t\"type\": %s", get_string(cell->type.c_str(ctx)).c_str()); |
| 242 |
} |
| 243 |
|
| 244 |
void open_instance_fe(CellInfo *cell, std::string type, std::string replace, std::string postfix = "") |
| 245 |
{ |
| 246 |
out << stringf("%s", first_instance ? "" : ",\n"); |
| 247 |
first_instance = false; |
| 248 |
out << stringf("\t\t%s: {\n", get_string(cleanup_name(cell->name.c_str(ctx)) + postfix).c_str()); |
| 249 |
std::string tile_name = uarch->tile_name(cell->bel.tile); |
| 250 |
IdString idx = ctx->getBelName(cell->bel)[1]; |
| 251 |
std::string belname = idx.c_str(ctx); |
| 252 |
boost::replace_all(belname, ".FE", replace); |
| 253 |
config.clear(); |
| 254 |
out << stringf("\t\t\t\"location\": %s,\n", get_string(tile_name + ":" + belname).c_str()); |
| 255 |
out << stringf("\t\t\t\"type\": %s", get_string(type).c_str()); |
| 256 |
} |
| 257 |
|
| 258 |
inline void add_config(std::string name, int val) |
| 259 |
{ |
| 260 |
config.push_back(stringf("\t\t\t\t%s:%d", get_string(name).c_str(), val)); |
| 261 |
} |
| 262 |
|
| 263 |
inline void add_config(std::string name, bool val) |
| 264 |
{ |
| 265 |
config.push_back(stringf("\t\t\t\t%s:%s", get_string(name).c_str(), val ? "true" : "false")); |
| 266 |
} |
| 267 |
|
| 268 |
inline void add_config(std::string name, std::string val) |
| 269 |
{ |
| 270 |
config.push_back(stringf("\t\t\t\t%s:%s", get_string(name).c_str(), get_string(val).c_str())); |
| 271 |
} |
| 272 |
|
| 273 |
void close_instance() |
| 274 |
{ |
| 275 |
bool first = true; |
| 276 |
if (!config.empty()) |
| 277 |
out << ",\n\t\t\t\"config\": {\n"; |
| 278 |
for (auto &str : config) { |
| 279 |
out << (first ? "" : ",\n"); |
| 280 |
out << str.c_str(); |
| 281 |
first = false; |
| 282 |
} |
| 283 |
if (!config.empty()) |
| 284 |
out << "\n\t\t\t}"; |
| 285 |
out << "\n\t\t}"; |
| 286 |
config.clear(); |
| 287 |
} |
| 288 |
|
| 289 |
void write_iop(CellInfo *cell) |
| 290 |
{ |
| 291 |
open_instance(cell, str_or_default(cell->params, id_iobname, "")); |
| 292 |
add_config("location", str_or_default(cell->params, id_location, "")); |
| 293 |
add_config("differential", str_or_n_value_lower(cell->params, id_differential, "false")); |
| 294 |
add_config("slewRate", str_or_default(cell->params, id_slewRate, "Medium")); |
| 295 |
add_config("turbo", str_or_n_value_lower(cell->params, id_turbo, "false")); |
| 296 |
add_config("weakTermination", str_or_n_value(cell->params, id_weakTermination, "PullUp")); |
| 297 |
add_config("inputDelayLine", str_or_default(cell->params, id_inputDelayLine, "0")); |
| 298 |
add_config("outputDelayLine", str_or_default(cell->params, id_outputDelayLine, "0")); |
| 299 |
add_config("inputSignalSlope", str_or_default(cell->params, id_inputSignalSlope, "0")); |
| 300 |
add_config("outputCapacity", str_or_default(cell->params, id_outputCapacity, "0")); |
| 301 |
add_config("standard", str_or_default(cell->params, id_standard, "LVCMOS")); |
| 302 |
add_config("drive", str_or_default(cell->params, id_drive, "2mA")); |
| 303 |
add_config("inputDelayOn", str_or_n_value_lower(cell->params, id_inputDelayOn, "false")); |
| 304 |
add_config("outputDelayOn", str_or_n_value_lower(cell->params, id_outputDelayOn, "false")); |
| 305 |
add_config("dynDrive", str_or_n_value_lower(cell->params, id_dynDrive, "false")); |
| 306 |
add_config("dynInput", str_or_n_value_lower(cell->params, id_dynInput, "false")); |
| 307 |
add_config("dynTerm", str_or_n_value_lower(cell->params, id_dynTerm, "false")); |
| 308 |
if (cell->type.in(id_OTP, id_ITP, id_IOTP)) { |
| 309 |
add_config("termination", str_or_n_value(cell->params, id_termination, "0")); |
| 310 |
add_config("terminationReference", str_or_n_value(cell->params, id_terminationReference, "VT")); |
| 311 |
} |
| 312 |
close_instance(); |
| 313 |
std::string tile_name = uarch->tile_name(cell->bel.tile); |
| 314 |
std::string bank = tile_name.substr(0, tile_name.rfind(':')); |
| 315 |
if (uarch->bank_voltage.count(bank) == 0) { |
| 316 |
if (bank == "IOB0" || bank == "IOB1" || bank == "IOB6" || bank == "IOB7") |
| 317 |
uarch->bank_voltage[bank] = "3.3V"; |
| 318 |
else |
| 319 |
uarch->bank_voltage[bank] = "1.8V"; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
void write_ddfr(CellInfo *cell) |
| 324 |
{ |
| 325 |
open_instance(cell); |
| 326 |
add_config("dff_load", bool_or_default(cell->params, id_dff_load, false)); |
| 327 |
add_config("dff_sync", bool_or_default(cell->params, id_dff_sync, false)); |
| 328 |
add_config("dff_type", bool_or_default(cell->params, id_dff_type, false)); |
| 329 |
add_config("iobname", str_or_default(cell->params, id_iobname, "")); |
| 330 |
add_config("path", int_or_default(cell->params, id_path, 0)); |
| 331 |
close_instance(); |
| 332 |
} |
| 333 |
|
| 334 |
void write_dfr(CellInfo *cell) |
| 335 |
{ |
| 336 |
open_instance(cell); |
| 337 |
add_config("data_inv", bool_or_default(cell->params, id_data_inv, false)); |
| 338 |
add_config("dff_edge", bool_or_default(cell->params, id_dff_edge, false)); |
| 339 |
add_config("dff_init", bool_or_default(cell->params, id_dff_init, false)); |
| 340 |
add_config("dff_load", bool_or_default(cell->params, id_dff_load, false)); |
| 341 |
add_config("dff_sync", bool_or_default(cell->params, id_dff_sync, false)); |
| 342 |
add_config("dff_type", bool_or_default(cell->params, id_dff_type, false)); |
| 343 |
add_config("mode", int_or_default(cell->params, id_mode, 3)); |
| 344 |
add_config("iobname", str_or_default(cell->params, id_iobname, "")); |
| 345 |
close_instance(); |
| 346 |
} |
| 347 |
|
| 348 |
void write_bfr(CellInfo *cell) |
| 349 |
{ |
| 350 |
open_instance(cell); |
| 351 |
add_config("mode", int_or_default(cell->params, id_mode, 2)); |
| 352 |
add_config("iobname", str_or_default(cell->params, id_iobname, "")); |
| 353 |
if (cell->params.count(id_data_inv)) { |
| 354 |
add_config("data_inv", bool_or_default(cell->params, id_data_inv, false)); |
| 355 |
} |
| 356 |
close_instance(); |
| 357 |
} |
| 358 |
|
| 359 |
void write_cy(CellInfo *cell) |
| 360 |
{ |
| 361 |
open_instance(cell); |
| 362 |
add_config("add_carry", int_or_default(cell->params, id_add_carry, 0)); |
| 363 |
add_config("shifter", bool_or_default(cell->params, id_shifter, false)); |
| 364 |
close_instance(); |
| 365 |
} |
| 366 |
|
| 367 |
void write_fe(CellInfo *cell) |
| 368 |
{ |
| 369 |
if (bool_or_default(cell->params, id_lut_used)) { |
| 370 |
open_instance_fe(cell, "LUT", ".LUT"); |
| 371 |
add_config("lut_table", extract_bits_or_default(cell->params, id_lut_table, 16)); |
| 372 |
close_instance(); |
| 373 |
} |
| 374 |
if (bool_or_default(cell->params, id_dff_used)) { |
| 375 |
std::string subtype = str_or_default(cell->params, id_type, "DFF"); |
| 376 |
open_instance_fe(cell, subtype, ".DFF", "_D"); |
| 377 |
if (subtype == "DFF") { |
| 378 |
add_config("dff_ctxt", std::to_string(int_or_default(cell->params, id_dff_ctxt, 0))); |
| 379 |
add_config("dff_edge", bool_or_default(cell->params, id_dff_edge, false)); |
| 380 |
add_config("dff_init", bool_or_default(cell->params, id_dff_init, false)); |
| 381 |
add_config("dff_load", bool_or_default(cell->params, id_dff_load, false)); |
| 382 |
add_config("dff_sync", bool_or_default(cell->params, id_dff_sync, false)); |
| 383 |
add_config("dff_type", bool_or_default(cell->params, id_dff_type, false)); |
| 384 |
} |
| 385 |
close_instance(); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void write_xlut(CellInfo *cell) |
| 390 |
{ |
| 391 |
open_instance(cell); |
| 392 |
add_config("lut_table", extract_bits_or_default(cell->params, id_lut_table, 16)); |
| 393 |
close_instance(); |
| 394 |
} |
| 395 |
|
| 396 |
void write_iom(CellInfo *cell) |
| 397 |
{ |
| 398 |
open_instance(cell); |
| 399 |
add_config("pads_path", str_or_default(cell->params, id_pads_path, ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;")); |
| 400 |
close_instance(); |
| 401 |
} |
| 402 |
|
| 403 |
void write_gck(CellInfo *cell) |
| 404 |
{ |
| 405 |
open_instance(cell); |
| 406 |
add_config("inv_in", bool_or_default(cell->params, id_inv_in, false)); |
| 407 |
add_config("inv_out", bool_or_default(cell->params, id_inv_out, false)); |
| 408 |
add_config("std_mode", str_or_default(cell->params, id_std_mode, "BYPASS")); |
| 409 |
close_instance(); |
| 410 |
} |
| 411 |
|
| 412 |
void write_wfb(CellInfo *cell) |
| 413 |
{ |
| 414 |
open_instance(cell); |
| 415 |
add_config("delay_on", bool_or_default(cell->params, id_delay_on, false)); |
| 416 |
add_config("delay", int_or_default(cell->params, id_delay, 0)); |
| 417 |
add_config("wfg_edge", bool_or_default(cell->params, id_wfg_edge, false)); |
| 418 |
close_instance(); |
| 419 |
} |
| 420 |
|
| 421 |
void write_wfg(CellInfo *cell) |
| 422 |
{ |
| 423 |
open_instance(cell); |
| 424 |
add_config("mode", int_or_default(cell->params, id_mode, 0)); |
| 425 |
add_config("delay_on", bool_or_default(cell->params, id_delay_on, false)); |
| 426 |
add_config("delay", int_or_default(cell->params, id_delay, 0)); |
| 427 |
add_config("wfg_edge", bool_or_default(cell->params, id_wfg_edge, false)); |
| 428 |
add_config("pattern", extract_bits_or_default(cell->params, id_pattern, 16)); |
| 429 |
add_config("pattern_end", int_or_default(cell->params, id_pattern_end, 0)); |
| 430 |
add_config("div_ratio", int_or_default(cell->params, id_div_ratio, 0)); |
| 431 |
add_config("div_phase", bool_or_default(cell->params, id_div_phase, false)); |
| 432 |
add_config("reset_on_pll_lock_n", bool_or_default(cell->params, id_reset_on_pll_lock_n, false)); |
| 433 |
add_config("reset_on_pll_locka_n", bool_or_default(cell->params, id_reset_on_pll_locka_n, false)); |
| 434 |
add_config("reset_on_cal_lock_n", bool_or_default(cell->params, id_reset_on_cal_lock_n, false)); |
| 435 |
close_instance(); |
| 436 |
} |
| 437 |
|
| 438 |
void write_pll(CellInfo *cell) |
| 439 |
{ |
| 440 |
open_instance(cell); |
| 441 |
add_config("clk_outdiv1", extract_bits_or_default(cell->params, id_clk_outdiv1, 3)); |
| 442 |
add_config("clk_outdiv2", extract_bits_or_default(cell->params, id_clk_outdiv2, 3)); |
| 443 |
add_config("clk_outdiv3", extract_bits_or_default(cell->params, id_clk_outdiv3, 3)); |
| 444 |
add_config("clk_outdiv4", extract_bits_or_default(cell->params, id_clk_outdiv4, 3)); |
| 445 |
add_config("clk_outdivd1", extract_bits_or_default(cell->params, id_clk_outdivd1, 4)); |
| 446 |
add_config("clk_outdivd2", extract_bits_or_default(cell->params, id_clk_outdivd2, 4)); |
| 447 |
add_config("clk_outdivd3", extract_bits_or_default(cell->params, id_clk_outdivd3, 4)); |
| 448 |
add_config("clk_outdivd4", extract_bits_or_default(cell->params, id_clk_outdivd4, 4)); |
| 449 |
add_config("clk_outdivd5", extract_bits_or_default(cell->params, id_clk_outdivd5, 4)); |
| 450 |
add_config("use_cal", bool_or_default(cell->params, id_use_cal, false)); |
| 451 |
add_config("clk_cal_sel", extract_bits_or_default(cell->params, id_clk_cal_sel, 2)); |
| 452 |
add_config("pll_odf", extract_bits_or_default(cell->params, id_pll_odf, 2)); |
| 453 |
add_config("pll_lpf_res", extract_bits_or_default(cell->params, id_pll_lpf_res, 4)); |
| 454 |
add_config("pll_lpf_cap", extract_bits_or_default(cell->params, id_pll_lpf_cap, 4)); |
| 455 |
add_config("cal_div", extract_bits_or_default(cell->params, id_cal_div, 4)); |
| 456 |
add_config("cal_delay", extract_bits_or_default(cell->params, id_cal_delay, 6)); |
| 457 |
add_config("use_pll", bool_or_default(cell->params, id_use_pll, true)); |
| 458 |
add_config("ref_intdiv", extract_bits_or_default(cell->params, id_ref_intdiv, 5)); |
| 459 |
add_config("ref_osc_on", bool_or_default(cell->params, id_ref_osc_on, false)); |
| 460 |
add_config("pll_cpump", extract_bits_or_default(cell->params, id_pll_cpump, 4)); |
| 461 |
add_config("pll_lock", extract_bits_or_default(cell->params, id_pll_lock, 4)); |
| 462 |
add_config("ext_fbk_on", bool_or_default(cell->params, id_ext_fbk_on, false)); |
| 463 |
add_config("fbk_intdiv", extract_bits_or_default(cell->params, id_fbk_intdiv, 7)); |
| 464 |
add_config("fbk_delay_on", bool_or_default(cell->params, id_fbk_delay_on, false)); |
| 465 |
add_config("fbk_delay", extract_bits_or_default(cell->params, id_fbk_delay, 6)); |
| 466 |
close_instance(); |
| 467 |
} |
| 468 |
|
| 469 |
void write_rfb(CellInfo *cell) |
| 470 |
{ |
| 471 |
open_instance(cell); |
| 472 |
std::string context = str_or_default(cell->params, id_mem_ctxt, ""); |
| 473 |
if (!context.empty()) |
| 474 |
add_config("mem_ctxt", context); |
| 475 |
add_config("wck_edge", bool_or_default(cell->params, id_wck_edge, false)); |
| 476 |
close_instance(); |
| 477 |
} |
| 478 |
|
| 479 |
void write_ram(CellInfo *cell) |
| 480 |
{ |
| 481 |
open_instance(cell); |
| 482 |
add_config("mcka_edge", bool_or_default(cell->params, id_mcka_edge, false)); |
| 483 |
add_config("mckb_edge", bool_or_default(cell->params, id_mckb_edge, false)); |
| 484 |
add_config("pcka_edge", bool_or_default(cell->params, id_pcka_edge, false)); |
| 485 |
add_config("pckb_edge", bool_or_default(cell->params, id_pckb_edge, false)); |
| 486 |
add_config("raw_config0", extract_bits_or_default(cell->params, id_raw_config0, 4)); |
| 487 |
add_config("raw_config1", extract_bits_or_default(cell->params, id_raw_config1, 16)); |
| 488 |
std::string context = str_or_default(cell->params, id_mem_ctxt, ""); |
| 489 |
if (!context.empty()) |
| 490 |
add_config("mem_ctxt", context); |
| 491 |
close_instance(); |
| 492 |
} |
| 493 |
|
| 494 |
void write_dsp(CellInfo *cell) |
| 495 |
{ |
| 496 |
open_instance(cell); |
| 497 |
add_config("raw_config0", extract_bits_or_default(cell->params, id_raw_config0, 27)); |
| 498 |
add_config("raw_config1", extract_bits_or_default(cell->params, id_raw_config1, 24)); |
| 499 |
add_config("raw_config2", extract_bits_or_default(cell->params, id_raw_config2, 14)); |
| 500 |
add_config("raw_config3", extract_bits_or_default(cell->params, id_raw_config3, 3)); |
| 501 |
close_instance(); |
| 502 |
} |
| 503 |
|
| 504 |
void write_cdc(CellInfo *cell) |
| 505 |
{ |
| 506 |
open_instance(cell); |
| 507 |
if (cell->type.in(id_DDE, id_TDE, id_CDC, id_XCDC)) { |
| 508 |
add_config("ck0_edge", bool_or_default(cell->params, id_ck0_edge, false)); |
| 509 |
add_config("ck1_edge", bool_or_default(cell->params, id_ck1_edge, false)); |
| 510 |
add_config("ack_sel", bool_or_default(cell->params, id_ack_sel, false)); |
| 511 |
add_config("bck_sel", bool_or_default(cell->params, id_bck_sel, false)); |
| 512 |
add_config("use_adest_arst", bool_or_default(cell->params, id_use_adest_arst, false)); |
| 513 |
add_config("use_bdest_arst", bool_or_default(cell->params, id_use_bdest_arst, false)); |
| 514 |
if (cell->type != id_DDE) { |
| 515 |
add_config("use_asrc_arst", bool_or_default(cell->params, id_use_asrc_arst, false)); |
| 516 |
add_config("use_bsrc_arst", bool_or_default(cell->params, id_use_bsrc_arst, false)); |
| 517 |
} |
| 518 |
if (cell->type == id_XCDC) { |
| 519 |
add_config("cck_sel", bool_or_default(cell->params, id_cck_sel, false)); |
| 520 |
add_config("dck_sel", bool_or_default(cell->params, id_dck_sel, false)); |
| 521 |
add_config("use_csrc_arst", bool_or_default(cell->params, id_use_csrc_arst, false)); |
| 522 |
add_config("use_dsrc_arst", bool_or_default(cell->params, id_use_dsrc_arst, false)); |
| 523 |
add_config("use_cdest_arst", bool_or_default(cell->params, id_use_cdest_arst, false)); |
| 524 |
add_config("use_ddest_arst", bool_or_default(cell->params, id_use_ddest_arst, false)); |
| 525 |
add_config("link_BA", bool_or_default(cell->params, id_link_BA, false)); |
| 526 |
add_config("link_CB", bool_or_default(cell->params, id_link_CB, false)); |
| 527 |
add_config("link_DC", bool_or_default(cell->params, id_link_DC, false)); |
| 528 |
} |
| 529 |
} |
| 530 |
close_instance(); |
| 531 |
} |
| 532 |
|
| 533 |
void write_fifo(CellInfo *cell) |
| 534 |
{ |
| 535 |
open_instance(cell); |
| 536 |
add_config("rck_edge", bool_or_default(cell->params, id_rck_edge, false)); |
| 537 |
add_config("wck_edge", bool_or_default(cell->params, id_wck_edge, false)); |
| 538 |
if (cell->type != id_FIFO) { |
| 539 |
add_config("use_read_arst", bool_or_default(cell->params, id_use_read_arst, false)); |
| 540 |
add_config("use_write_arst", bool_or_default(cell->params, id_use_write_arst, false)); |
| 541 |
} |
| 542 |
add_config("read_addr_inv", extract_bits_or_default(cell->params, id_read_addr_inv, 7)); |
| 543 |
close_instance(); |
| 544 |
} |
| 545 |
|
| 546 |
void write_interconnections() |
| 547 |
{ |
| 548 |
for (auto &net : ctx->nets) { |
| 549 |
NetInfo *ni = net.second.get(); |
| 550 |
if (ni->wires.size() == 0) |
| 551 |
continue; |
| 552 |
std::vector<std::string> nets; |
| 553 |
for (auto &w : ni->wires) { |
| 554 |
if (w.second.pip != PipId()) { |
| 555 |
PipId pip = w.second.pip; |
| 556 |
const auto &extra_data = *uarch->pip_extra_data(w.second.pip); |
| 557 |
if (!extra_data.name || extra_data.type != PipExtra::PIP_EXTRA_INTERCONNECT) |
| 558 |
continue; |
| 559 |
auto &pd = chip_pip_info(ctx->chip_info, pip); |
| 560 |
IdString src = IdString(chip_tile_info(ctx->chip_info, pip.tile).wires[pd.src_wire].name); |
| 561 |
std::string tile_name = uarch->tile_name(pip.tile); |
| 562 |
std::string src_name = src.c_str(ctx); |
| 563 |
std::string type = "OTC"; |
| 564 |
if (src_name.find("UI1x") != std::string::npos) |
| 565 |
type = "ITC"; |
| 566 |
if (boost::starts_with(src_name, "SO1.")) |
| 567 |
type = "OTS"; |
| 568 |
if (boost::starts_with(src_name, "SI1.")) |
| 569 |
type = "ITS"; |
| 570 |
src_name = update_name(tile_name, src_name); |
| 571 |
src_name = src_name.substr(0, src_name.size() - 2); |
| 572 |
|
| 573 |
std::string name = cleanup_name(std::string(ni->name.c_str(ctx)) + "_" + src_name.substr(4)); |
| 574 |
out << stringf(",\n\t\t%s: {\n", get_string(name).c_str()); |
| 575 |
out << stringf("\t\t\t\"location\": %s,\n", get_string(tile_name + ":" + src_name).c_str()); |
| 576 |
out << stringf("\t\t\t\"type\": %s\n\t\t}", get_string(type).c_str()); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
void write_instances() |
| 583 |
{ |
| 584 |
out << "\t\"instances\": {\n"; |
| 585 |
first_instance = true; |
| 586 |
for (auto &cell : ctx->cells) { |
| 587 |
switch (cell.second->type.index) { |
| 588 |
case id_BEYOND_FE.index: |
| 589 |
write_fe(cell.second.get()); |
| 590 |
break; |
| 591 |
case id_IOP.index: |
| 592 |
case id_IP.index: |
| 593 |
case id_OP.index: |
| 594 |
case id_IOTP.index: |
| 595 |
case id_ITP.index: |
| 596 |
case id_OTP.index: |
| 597 |
write_iop(cell.second.get()); |
| 598 |
break; |
| 599 |
case id_CY.index: |
| 600 |
write_cy(cell.second.get()); |
| 601 |
break; |
| 602 |
case id_WFB.index: |
| 603 |
write_wfb(cell.second.get()); |
| 604 |
break; |
| 605 |
case id_WFG.index: |
| 606 |
write_wfg(cell.second.get()); |
| 607 |
break; |
| 608 |
case id_GCK.index: |
| 609 |
write_gck(cell.second.get()); |
| 610 |
break; |
| 611 |
case id_IOM.index: |
| 612 |
write_iom(cell.second.get()); |
| 613 |
break; |
| 614 |
case id_BFR.index: |
| 615 |
write_bfr(cell.second.get()); |
| 616 |
break; |
| 617 |
case id_DDFR.index: |
| 618 |
write_ddfr(cell.second.get()); |
| 619 |
break; |
| 620 |
case id_DFR.index: |
| 621 |
write_dfr(cell.second.get()); |
| 622 |
break; |
| 623 |
case id_RAM.index: |
| 624 |
write_ram(cell.second.get()); |
| 625 |
break; |
| 626 |
case id_RF.index: |
| 627 |
case id_RFSP.index: |
| 628 |
case id_XHRF.index: |
| 629 |
case id_XWRF.index: |
| 630 |
case id_XPRF.index: |
| 631 |
write_rfb(cell.second.get()); |
| 632 |
break; |
| 633 |
case id_XLUT.index: |
| 634 |
write_xlut(cell.second.get()); |
| 635 |
break; |
| 636 |
case id_FIFO.index: // mode 0 |
| 637 |
case id_XHFIFO.index: // mode 1 |
| 638 |
case id_XWFIFO.index: |
| 639 |
write_fifo(cell.second.get()); |
| 640 |
break; // mode 2 |
| 641 |
case id_DDE.index: // mode 0 |
| 642 |
case id_TDE.index: // mode 1 |
| 643 |
case id_CDC.index: // mode 2 |
| 644 |
case id_BGC.index: // mode 3 |
| 645 |
case id_GBC.index: // mode 4 |
| 646 |
case id_XCDC.index: |
| 647 |
write_cdc(cell.second.get()); |
| 648 |
break; // mode 5 |
| 649 |
case id_DSP.index: |
| 650 |
write_dsp(cell.second.get()); |
| 651 |
break; |
| 652 |
case id_PLL.index: |
| 653 |
write_pll(cell.second.get()); |
| 654 |
break; |
| 655 |
// case id_CRX.index: |
| 656 |
// case id_CTX.index: |
| 657 |
// case id_PMA.index: |
| 658 |
// case id_Service.index: |
| 659 |
// case id_SOCIF.index: |
| 660 |
default: |
| 661 |
log_error("Unhandled cell %s of type %s\n", cell.second.get()->name.c_str(ctx), |
| 662 |
cell.second->type.c_str(ctx)); |
| 663 |
} |
| 664 |
} |
| 665 |
write_interconnections(); |
| 666 |
out << "\n\t},\n"; |
| 667 |
} |
| 668 |
|
| 669 |
void write_setup() |
| 670 |
{ |
| 671 |
out << "\t\"setup\": {\n"; |
| 672 |
out << "\t\t\"variant\": \"NG-ULTRA\",\n"; |
| 673 |
out << "\t\t\"iobanks\": {\n"; |
| 674 |
bool first = true; |
| 675 |
for (auto &bank : uarch->bank_voltage) { |
| 676 |
out << (first ? "" : ",\n"); |
| 677 |
out << stringf("\t\t\t%s:%s", get_string(bank.first).c_str(), get_string(bank.second).c_str()); |
| 678 |
first = false; |
| 679 |
} |
| 680 |
out << "\n\t\t}\n\t}\n"; |
| 681 |
} |
| 682 |
|
| 683 |
void write_json() |
| 684 |
{ |
| 685 |
out << "{\n"; |
| 686 |
write_nets(); |
| 687 |
write_instances(); |
| 688 |
write_setup(); |
| 689 |
out << "}\n"; |
| 690 |
} |
| 691 |
}; |
| 692 |
|
| 693 |
} // namespace |
| 694 |
|
| 695 |
void NgUltraImpl::write_bitstream_json(const std::string &filename) |
| 696 |
{ |
| 697 |
std::ofstream out(filename); |
| 698 |
if (!out) |
| 699 |
log_error("failed to open file %s for writing (%s)\n", filename.c_str(), strerror(errno)); |
| 700 |
|
| 701 |
BitstreamJsonBackend be(ctx, this, out); |
| 702 |
be.write_json(); |
| 703 |
} |
| 704 |
|
| 705 |
NEXTPNR_NAMESPACE_END |