| 1 |
/* |
| 2 |
* nextpnr -- Next Generation Place and Route |
| 3 |
* |
| 4 |
* Copyright (C) 2021-22 gatecat <[email protected]> |
| 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 "fasm.h" |
| 21 |
#include "log.h" |
| 22 |
#include "nextpnr.h" |
| 23 |
#include "util.h" |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <boost/range/adaptor/reversed.hpp> |
| 27 |
#include <fstream> |
| 28 |
|
| 29 |
#define VIADUCT_CONSTIDS "viaduct/fabulous/constids.inc" |
| 30 |
#include "viaduct_constids.h" |
| 31 |
|
| 32 |
NEXTPNR_NAMESPACE_BEGIN |
| 33 |
|
| 34 |
namespace { |
| 35 |
struct FabFasmWriter |
| 36 |
{ |
| 37 |
FabFasmWriter(const Context *ctx, const FabricConfig &cfg, const std::vector<PseudoPipTags> &pip_tags, |
| 38 |
const std::string &filename) |
| 39 |
: ctx(ctx), cfg(cfg), pip_tags(pip_tags), out(filename) |
| 40 |
{ |
| 41 |
if (!out) |
| 42 |
log_error("failed to open fasm file '%s' for writing\n", filename.c_str()); |
| 43 |
} |
| 44 |
std::string format_name(IdStringList name) |
| 45 |
{ |
| 46 |
std::string result; |
| 47 |
for (IdString entry : name) { |
| 48 |
if (!result.empty()) |
| 49 |
result += "."; |
| 50 |
result += entry.str(ctx); |
| 51 |
} |
| 52 |
return result; |
| 53 |
} |
| 54 |
void write_pip(PipId pip) |
| 55 |
{ |
| 56 |
auto &data = ctx->pip_info(pip); |
| 57 |
if (pip.index < int(pip_tags.size()) && pip_tags.at(pip.index).type != PseudoPipTags::NONE) { |
| 58 |
// pseudo PIP |
| 59 |
const auto &tag = pip_tags.at(pip.index); |
| 60 |
if (tag.type == PseudoPipTags::LUT_CONST) { |
| 61 |
NPNR_ASSERT(ctx->checkBelAvail(tag.bel)); |
| 62 |
prefix = format_name(ctx->getBelName(tag.bel)) + "."; |
| 63 |
write_int_vector(stringf("INIT[%d:0]", (1U << cfg.clb.lut_k) - 1), 0, 1U << cfg.clb.lut_k, |
| 64 |
(tag.data & 0x1)); |
| 65 |
prefix = ""; |
| 66 |
} |
| 67 |
return; |
| 68 |
} |
| 69 |
if (data.type.in(id_global_clock, id_O2Q) || data.type.c_str(ctx)[0] == '$') |
| 70 |
return; // pseudo-pips with no underlying bitstream bits |
| 71 |
// write pip name but with '.' instead of '/' for separator |
| 72 |
out << format_name(data.name) << std::endl; |
| 73 |
} |
| 74 |
void write_routing(const NetInfo *net) |
| 75 |
{ |
| 76 |
std::vector<PipId> sorted_pips; |
| 77 |
for (auto &w : net->wires) |
| 78 |
if (w.second.pip != PipId()) |
| 79 |
sorted_pips.push_back(w.second.pip); |
| 80 |
std::sort(sorted_pips.begin(), sorted_pips.end()); |
| 81 |
out << stringf("# routing for net '%s'\n", ctx->nameOf(net)) << std::endl; |
| 82 |
for (auto pip : sorted_pips) |
| 83 |
write_pip(pip); |
| 84 |
out << std::endl; |
| 85 |
} |
| 86 |
|
| 87 |
std::string prefix; |
| 88 |
|
| 89 |
// Write a FASM bitvector; optionally inverting the values in the process |
| 90 |
void write_vector(const std::string &name, const std::vector<bool> &value, bool invert = false) |
| 91 |
{ |
| 92 |
out << prefix << name << " = " << int(value.size()) << "'b"; |
| 93 |
for (auto bit : boost::adaptors::reverse(value)) |
| 94 |
out << ((bit ^ invert) ? '1' : '0'); |
| 95 |
out << std::endl; |
| 96 |
} |
| 97 |
// Write a FASM bitvector given an integer value |
| 98 |
void write_int_vector(const std::string &name, uint64_t value, int width, bool invert = false) |
| 99 |
{ |
| 100 |
std::vector<bool> bits(width, false); |
| 101 |
for (int i = 0; i < width; i++) |
| 102 |
bits[i] = (value & (1ULL << unsigned(i))) != 0; |
| 103 |
write_vector(name, bits, invert); |
| 104 |
} |
| 105 |
// Write an int vector param |
| 106 |
void write_int_vector_param(const CellInfo *cell, const std::string &name, uint64_t defval, int width, |
| 107 |
bool invert = false) |
| 108 |
{ |
| 109 |
uint64_t value = int_or_default(cell->params, ctx->id(name), defval); |
| 110 |
std::vector<bool> bits(width, false); |
| 111 |
for (int i = 0; i < width; i++) |
| 112 |
bits[i] = (value & (1ULL << i)) != 0; |
| 113 |
write_vector(stringf("%s[%d:0]", name.c_str(), width - 1), bits, invert); |
| 114 |
} |
| 115 |
|
| 116 |
void write_bool(const CellInfo *cell, const std::string &name) |
| 117 |
{ |
| 118 |
if (bool_or_default(cell->params, ctx->id(name))) { |
| 119 |
out << prefix << name << std::endl; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void add_feature(const std::string &name) { out << prefix << name << std::endl; } |
| 124 |
|
| 125 |
uint64_t depermute_lut(const CellInfo *lut) |
| 126 |
{ |
| 127 |
uint64_t orig_init = 0; |
| 128 |
if (lut->params.count(id_INIT)) |
| 129 |
orig_init = lut->params.at(id_INIT).as_int64(); |
| 130 |
std::vector<std::vector<unsigned>> phys_to_log; |
| 131 |
phys_to_log.resize(cfg.clb.lut_k); |
| 132 |
for (unsigned i = 0; i < cfg.clb.lut_k; i++) { |
| 133 |
WireId pin_wire = ctx->getBelPinWire(lut->bel, ctx->idf("I%d", i)); |
| 134 |
for (PipId pip : ctx->getPipsUphill(pin_wire)) { |
| 135 |
if (!ctx->getBoundPipNet(pip)) |
| 136 |
continue; |
| 137 |
unsigned pip_data = pip_tags.at(pip.index).data; |
| 138 |
unsigned from_pin = (pip_data >> 4) & 0xF; |
| 139 |
unsigned to_pin = (pip_data) & 0xF; |
| 140 |
NPNR_ASSERT(to_pin == i); |
| 141 |
phys_to_log[from_pin].push_back(i); |
| 142 |
} |
| 143 |
} |
| 144 |
if (bool_or_default(lut->params, id_I0MUX, false)) // internal, hardcoded |
| 145 |
phys_to_log[0].push_back(0); |
| 146 |
for (unsigned i = 0; i < cfg.clb.lut_k; i++) { |
| 147 |
for (auto j : phys_to_log.at(i)) |
| 148 |
out << stringf(" # permute phys %d log %d\n", i, j); |
| 149 |
} |
| 150 |
uint64_t permuted_init = 0; |
| 151 |
for (unsigned i = 0; i < (1U << cfg.clb.lut_k); i++) { |
| 152 |
unsigned log_idx = 0; |
| 153 |
for (unsigned j = 0; j < cfg.clb.lut_k; j++) { |
| 154 |
if ((i >> j) & 0x1) { |
| 155 |
for (auto log_pin : phys_to_log[j]) |
| 156 |
log_idx |= (1 << log_pin); |
| 157 |
} |
| 158 |
} |
| 159 |
if ((orig_init >> log_idx) & 0x1) |
| 160 |
permuted_init |= (uint64_t(1U) << i); |
| 161 |
} |
| 162 |
return permuted_init; |
| 163 |
} |
| 164 |
|
| 165 |
void write_logic(const CellInfo *lc) |
| 166 |
{ |
| 167 |
prefix = format_name(ctx->getBelName(lc->bel)) + "."; |
| 168 |
if (lc->type.in(id_FABULOUS_LC, id_FABULOUS_COMB)) { |
| 169 |
uint64_t init = depermute_lut(lc); |
| 170 |
unsigned width = 1U << cfg.clb.lut_k; |
| 171 |
write_int_vector(stringf("INIT[%d:0]", width - 1), init, width); // todo lut depermute and thru |
| 172 |
if (bool_or_default(lc->params, id_I0MUX, false)) |
| 173 |
add_feature("IOmux"); // typo in FABulous? |
| 174 |
} |
| 175 |
if (lc->type == id_FABULOUS_LC) { |
| 176 |
write_bool(lc, "FF"); |
| 177 |
} |
| 178 |
if (lc->type.in(id_FABULOUS_LC, id_FABULOUS_FF)) { |
| 179 |
write_bool(lc, "SET_NORESET"); |
| 180 |
write_bool(lc, "NEG_CLK"); |
| 181 |
write_bool(lc, "NEG_EN"); |
| 182 |
write_bool(lc, "NEG_SR"); |
| 183 |
write_bool(lc, "ASYNC_SR"); |
| 184 |
} |
| 185 |
if (lc->type.in(id_FABULOUS_MUX4, id_FABULOUS_MUX8)) { |
| 186 |
// TODO: don't hardcode prefix |
| 187 |
out << prefix << "I.c0" << std::endl; |
| 188 |
} |
| 189 |
if (lc->type == id_FABULOUS_MUX8) { |
| 190 |
// TODO: don't hardcode prefix |
| 191 |
out << prefix << "I.c1" << std::endl; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
void write_io(const CellInfo *io) |
| 196 |
{ |
| 197 |
#if 0 |
| 198 |
prefix = format_name(ctx->getBelName(io->bel)) + "."; |
| 199 |
write_bool(io, "INPUT_USED"); |
| 200 |
write_bool(io, "OUTPUT_USED"); |
| 201 |
write_bool(io, "ENABLE_USED"); |
| 202 |
#endif |
| 203 |
} |
| 204 |
|
| 205 |
void write_generic_cell(const CellInfo *ci) |
| 206 |
{ |
| 207 |
if (ci->bel == BelId()) |
| 208 |
return; |
| 209 |
prefix = format_name(ctx->getBelName(ci->bel)) + "."; |
| 210 |
for (auto ¶m : ci->params) { |
| 211 |
// TODO: better parameter type auto-detection |
| 212 |
if (param.second.is_string) { |
| 213 |
// enum type parameter |
| 214 |
out << prefix << param.first.c_str(ctx) << "." << param.second.str << std::endl; |
| 215 |
} else if (param.second.str.size() == 1) { |
| 216 |
// boolean type parameter |
| 217 |
if (param.second.intval != 0) |
| 218 |
out << prefix << param.first.c_str(ctx) << std::endl; |
| 219 |
} else { |
| 220 |
// vector type parameter |
| 221 |
int msb = int(param.second.str.size()) - 1; |
| 222 |
out << prefix << param.first.c_str(ctx) << "[" << msb << ":0] = "; |
| 223 |
for (auto bit : boost::adaptors::reverse(param.second.str)) |
| 224 |
out << bit; |
| 225 |
out << std::endl; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void write_iopass(const CellInfo *ci) |
| 231 |
{ |
| 232 |
Loc loc = ctx->getBelLocation(ci->bel); |
| 233 |
// We use 'nice' names based on function for the IOPass bels inside nextpnr |
| 234 |
// but in the bitstream we need to use character names |
| 235 |
NPNR_ASSERT(loc.z <= 26); |
| 236 |
prefix = stringf("X%dY%d.%c.", loc.x, loc.y, 'A' + loc.z); |
| 237 |
if (ci->params.count(id_I_reg)) { |
| 238 |
uint64_t regval = int_or_default(ci->params, id_I_reg); |
| 239 |
for (unsigned i = 0; i < 4; i++) { |
| 240 |
if (regval & (1 << i)) |
| 241 |
out << prefix << stringf("I%d_reg", i) << std::endl; |
| 242 |
} |
| 243 |
} |
| 244 |
prefix = ""; |
| 245 |
} |
| 246 |
|
| 247 |
void write_cell(const CellInfo *ci) |
| 248 |
{ |
| 249 |
out << stringf("# config for cell '%s'\n", ctx->nameOf(ci)) << std::endl; |
| 250 |
if (ci->type.in(id_FABULOUS_COMB, id_FABULOUS_FF, id_FABULOUS_LC, id_FABULOUS_MUX2, id_FABULOUS_MUX4, |
| 251 |
id_FABULOUS_MUX8)) |
| 252 |
write_logic(ci); |
| 253 |
else if (ci->type == id_IO_1_bidirectional_frame_config_pass) |
| 254 |
write_io(ci); |
| 255 |
else if (ci->type.in(id_InPass4_frame_config, id_OutPass4_frame_config)) |
| 256 |
write_iopass(ci); |
| 257 |
else |
| 258 |
write_generic_cell(ci); |
| 259 |
// TODO: other cell types |
| 260 |
out << std::endl; |
| 261 |
} |
| 262 |
|
| 263 |
void write_fasm() |
| 264 |
{ |
| 265 |
for (const auto &net : ctx->nets) |
| 266 |
write_routing(net.second.get()); |
| 267 |
for (const auto &cell : ctx->cells) |
| 268 |
write_cell(cell.second.get()); |
| 269 |
} |
| 270 |
|
| 271 |
const Context *ctx; |
| 272 |
const FabricConfig &cfg; |
| 273 |
const std::vector<PseudoPipTags> &pip_tags; |
| 274 |
std::ofstream out; |
| 275 |
}; |
| 276 |
} // namespace |
| 277 |
|
| 278 |
void fabulous_write_fasm(const Context *ctx, const FabricConfig &cfg, const std::vector<PseudoPipTags> &pip_tags, |
| 279 |
const std::string &filename) |
| 280 |
{ |
| 281 |
FabFasmWriter wr(ctx, cfg, pip_tags, filename); |
| 282 |
wr.write_fasm(); |
| 283 |
} |
| 284 |
|
| 285 |
NEXTPNR_NAMESPACE_END |