| 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 "config.h" |
| 21 |
#include <boost/range/adaptor/reversed.hpp> |
| 22 |
#include <iomanip> |
| 23 |
#include <set> |
| 24 |
#include "log.h" |
| 25 |
|
| 26 |
NEXTPNR_NAMESPACE_BEGIN |
| 27 |
|
| 28 |
#define fmt(x) (static_cast<const std::ostringstream &>(std::ostringstream() << x).str()) |
| 29 |
|
| 30 |
inline std::string to_string(const std::vector<bool> &bv) |
| 31 |
{ |
| 32 |
std::ostringstream os; |
| 33 |
for (auto bit : boost::adaptors::reverse(bv)) |
| 34 |
os << (bit ? '1' : '0'); |
| 35 |
return os.str(); |
| 36 |
} |
| 37 |
|
| 38 |
std::ostream &operator<<(std::ostream &out, const ConfigWord &cw) |
| 39 |
{ |
| 40 |
out << cw.name << " " << to_string(cw.value) << std::endl; |
| 41 |
return out; |
| 42 |
} |
| 43 |
|
| 44 |
std::ostream &operator<<(std::ostream &out, const TileConfig &tc) |
| 45 |
{ |
| 46 |
auto sorted = tc.cwords; |
| 47 |
std::sort(sorted.begin(), sorted.end(), [](const ConfigWord &a, const ConfigWord &b) { return a.name < b.name; }); |
| 48 |
for (const auto &cword : sorted) |
| 49 |
out << cword; |
| 50 |
return out; |
| 51 |
} |
| 52 |
|
| 53 |
void TileConfig::add_word(const std::string &name, const std::vector<bool> &value, const char *c) |
| 54 |
{ |
| 55 |
if (!added.count(name)) { |
| 56 |
cwords.push_back({name, value}); |
| 57 |
added.emplace(name, value); |
| 58 |
} else { |
| 59 |
auto val = added.at(name); |
| 60 |
if (val != value) |
| 61 |
log_error("Trying to add value to already assigned word %s for %s.\n", name.c_str(), c ? c : ""); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
std::string TileConfig::to_string() const |
| 66 |
{ |
| 67 |
std::stringstream ss; |
| 68 |
ss << *this; |
| 69 |
return ss.str(); |
| 70 |
} |
| 71 |
|
| 72 |
bool TileConfig::empty() const { return cwords.empty(); } |
| 73 |
|
| 74 |
std::ostream &operator<<(std::ostream &out, const ChipConfig &cc) |
| 75 |
{ |
| 76 |
out << ".device " << cc.chip_name << std::endl << std::endl; |
| 77 |
for (const auto &config : cc.configs) { |
| 78 |
if (!config.second.empty()) { |
| 79 |
out << ".config " << config.first << " " << std::endl; |
| 80 |
out << config.second; |
| 81 |
out << std::endl; |
| 82 |
} |
| 83 |
} |
| 84 |
for (const auto &ser : cc.serdes) { |
| 85 |
if (!ser.second.empty()) { |
| 86 |
out << ".serdes " << ser.first << " " << std::endl; |
| 87 |
out << ser.second; |
| 88 |
out << std::endl; |
| 89 |
} |
| 90 |
} |
| 91 |
for (const auto &tile : cc.tiles) { |
| 92 |
if (!tile.second.empty()) { |
| 93 |
out << ".tile " << tile.first.die << " " << tile.first.x << " " << tile.first.y << std::endl; |
| 94 |
out << tile.second; |
| 95 |
out << std::endl; |
| 96 |
} |
| 97 |
} |
| 98 |
for (const auto &bram : cc.brams) { |
| 99 |
if (!bram.second.empty()) { |
| 100 |
out << ".bram " << bram.first.die << " " << bram.first.x << " " << bram.first.y << std::endl; |
| 101 |
out << bram.second; |
| 102 |
out << std::endl; |
| 103 |
} |
| 104 |
} |
| 105 |
for (const auto &bram : cc.bram_data) { |
| 106 |
if (!bram.second.empty()) { |
| 107 |
out << ".bram_init " << bram.first.die << " " << bram.first.x << " " << bram.first.y << std::endl; |
| 108 |
std::ios_base::fmtflags f(out.flags()); |
| 109 |
for (size_t i = 0; i < bram.second.size(); i++) { |
| 110 |
out << std::setw(2) << std::setfill('0') << std::hex << (int)bram.second.at(i); |
| 111 |
if (i % 40 == 39) |
| 112 |
out << std::endl; |
| 113 |
else |
| 114 |
out << " "; |
| 115 |
} |
| 116 |
out.flags(f); |
| 117 |
out << std::endl; |
| 118 |
} |
| 119 |
} |
| 120 |
return out; |
| 121 |
} |
| 122 |
|
| 123 |
NEXTPNR_NAMESPACE_END |