Use full namespace

This commit is contained in:
Miodrag Milanovic 2024-12-18 10:19:43 +01:00
parent 172c570a28
commit 4c8b566e62
10 changed files with 111 additions and 119 deletions

View File

@ -49,8 +49,8 @@ struct CfgLoc
class ChipConfig
{
public:
string chip_name;
string chip_package;
std::string chip_name;
std::string chip_package;
std::map<CfgLoc, TileConfig> tiles;
std::map<CfgLoc, TileConfig> brams;
std::map<int, TileConfig> configs;

View File

@ -32,9 +32,9 @@ struct WordSettingBits
{
int start;
int end;
vector<bool> get_value(const vector<bool> &tile) const;
std::vector<bool> get_value(const std::vector<bool> &tile) const;
void set_value(vector<bool> &tile, const vector<bool> &value) const;
void set_value(std::vector<bool> &tile, const std::vector<bool> &value) const;
inline bool operator==(const WordSettingBits &other) const { return (start == other.start) && (end == other.end); }
};
@ -45,7 +45,7 @@ class BaseBitDatabase
BaseBitDatabase(int num_bits);
virtual ~BaseBitDatabase();
TileConfig data_to_config(const vector<uint8_t> &data);
TileConfig data_to_config(const std::vector<uint8_t> &data);
std::vector<uint8_t> config_to_data(const TileConfig &cfg);
protected:
@ -54,7 +54,7 @@ class BaseBitDatabase
std::vector<uint8_t> bits_to_bytes(std::vector<bool> &bits);
int num_bits;
map<std::string, WordSettingBits> words;
std::map<std::string, WordSettingBits> words;
std::vector<uint8_t> known_bits;
};
@ -94,10 +94,10 @@ class ConfigBitDatabase : public BaseBitDatabase
ConfigBitDatabase();
};
class DatabaseConflictError : public runtime_error
class DatabaseConflictError : public std::runtime_error
{
public:
explicit DatabaseConflictError(const string &desc);
explicit DatabaseConflictError(const std::string &desc);
};
} // namespace GateMate

View File

@ -26,37 +26,35 @@
#include <string>
#include <vector>
using namespace std;
namespace GateMate {
struct ConfigWord
{
string name;
vector<bool> value;
std::string name;
std::vector<bool> value;
inline bool operator==(const ConfigWord &other) const { return other.name == name && other.value == value; }
};
ostream &operator<<(ostream &out, const ConfigWord &cw);
std::ostream &operator<<(std::ostream &out, const ConfigWord &cw);
istream &operator>>(istream &in, ConfigWord &cw);
std::istream &operator>>(std::istream &in, ConfigWord &cw);
struct TileConfig
{
vector<ConfigWord> cwords;
std::vector<ConfigWord> cwords;
int total_known_bits = 0;
void add_word(const string &name, const vector<bool> &value);
void add_word(const std::string &name, const std::vector<bool> &value);
string to_string() const;
static TileConfig from_string(const string &str);
std::string to_string() const;
static TileConfig from_string(const std::string &str);
bool empty() const;
};
ostream &operator<<(ostream &out, const TileConfig &tc);
std::ostream &operator<<(std::ostream &out, const TileConfig &tc);
istream &operator>>(istream &in, TileConfig &ce);
std::istream &operator>>(std::istream &in, TileConfig &ce);
} // namespace GateMate

View File

@ -27,8 +27,6 @@
#include <string>
#include <vector>
using namespace std;
namespace GateMate {
enum class VerbosityLevel
{
@ -38,29 +36,29 @@ enum class VerbosityLevel
};
extern VerbosityLevel verbosity;
inline string uint32_to_hexstr(uint32_t val)
inline std::string uint32_to_hexstr(uint32_t val)
{
ostringstream os;
os << "0x" << hex << setw(8) << setfill('0') << val;
std::ostringstream os;
os << "0x" << std::hex << std::setw(8) << std::setfill('0') << val;
return os.str();
}
// Hex is not allowed in JSON, to avoid an ugly decimal integer use a string
// instead But we need to parse this back to a uint32_t
inline uint32_t parse_uint32(string str) { return uint32_t(strtoul(str.c_str(), nullptr, 0)); }
inline uint32_t parse_uint32(std::string str) { return uint32_t(strtoul(str.c_str(), nullptr, 0)); }
inline string to_string(const vector<bool> &bv)
inline std::string to_string(const std::vector<bool> &bv)
{
ostringstream os;
std::ostringstream os;
for (auto bit : boost::adaptors::reverse(bv))
os << (bit ? '1' : '0');
return os.str();
}
inline istream &operator>>(istream &in, vector<bool> &bv)
inline std::istream &operator>>(std::istream &in, std::vector<bool> &bv)
{
bv.clear();
string s;
std::string s;
in >> s;
for (auto c : boost::adaptors::reverse(s)) {
assert((c == '0') || (c == '1'));
@ -70,7 +68,7 @@ inline istream &operator>>(istream &in, vector<bool> &bv)
}
// Skip whitespace, optionally including newlines
inline void skip_blank(istream &in, bool nl = false)
inline void skip_blank(std::istream &in, bool nl = false)
{
int c = in.peek();
while (in && (((c == ' ') || (c == '\t')) || (nl && ((c == '\n') || (c == '\r'))))) {
@ -79,7 +77,7 @@ inline void skip_blank(istream &in, bool nl = false)
}
}
// Return true if end of line (or file)
inline bool skip_check_eol(istream &in)
inline bool skip_check_eol(std::istream &in)
{
skip_blank(in, false);
if (!in)
@ -99,7 +97,7 @@ inline bool skip_check_eol(istream &in)
}
// Skip past blank lines and comments
inline void skip(istream &in)
inline void skip(std::istream &in)
{
skip_blank(in, true);
while (in && (in.peek() == '#')) {
@ -110,7 +108,7 @@ inline void skip(istream &in)
}
// Return true if at the end of a record (or file)
inline bool skip_check_eor(istream &in)
inline bool skip_check_eor(std::istream &in)
{
skip(in);
int c = in.peek();
@ -118,7 +116,7 @@ inline bool skip_check_eor(istream &in)
}
// Return true if at the end of file
inline bool skip_check_eof(istream &in)
inline bool skip_check_eof(std::istream &in)
{
skip(in);
int c = in.peek();

View File

@ -98,10 +98,10 @@ class BitstreamReadWriter
public:
BitstreamReadWriter() : data(), iter(data.begin()) {};
BitstreamReadWriter(const vector<uint8_t> &data) : data(data), iter(this->data.begin()) {};
BitstreamReadWriter(const std::vector<uint8_t> &data) : data(data), iter(this->data.begin()) {};
vector<uint8_t> data;
vector<uint8_t>::iterator iter;
std::vector<uint8_t> data;
std::vector<uint8_t>::iterator iter;
Crc16 crc16;
// Return a single byte and update CRC
@ -218,8 +218,8 @@ class BitstreamReadWriter
// cerr << hex << int(crc_bytes[0]) << " " << int(crc_bytes[1]) << endl;
uint16_t exp_crc = (crc_bytes[0] << 8) | crc_bytes[1];
if (actual_crc != exp_crc) {
ostringstream err;
err << "crc fail, calculated 0x" << hex << actual_crc << " but expecting 0x" << exp_crc;
std::ostringstream err;
err << "crc fail, calculated 0x" << std::hex << actual_crc << " but expecting 0x" << exp_crc;
throw BitstreamParseError(err.str(), get_offset());
}
crc16.reset_crc16();
@ -235,7 +235,7 @@ class BitstreamReadWriter
bool is_end() { return (iter >= data.end()); }
const vector<uint8_t> &get() { return data; };
const std::vector<uint8_t> &get() { return data; };
void write_nops(size_t count)
{
@ -374,21 +374,21 @@ void check_crc(BitstreamReadWriter &rd)
uint16_t actual_crc = rd.crc16.get_crc16();
uint16_t exp_crc = rd.get_crc(); // crc
if (actual_crc != exp_crc) {
ostringstream err;
err << "crc fail, calculated 0x" << hex << actual_crc << " but expecting 0x" << exp_crc;
std::ostringstream err;
err << "crc fail, calculated 0x" << std::hex << actual_crc << " but expecting 0x" << exp_crc;
throw BitstreamParseError(err.str());
}
}
#define BITSTREAM_DEBUG(x) \
if (verbosity >= VerbosityLevel::DEBUG) \
cerr << "bitstream: " << x << endl
std::cerr << "bitstream: " << x << std::endl
#define BITSTREAM_NOTE(x) \
if (verbosity >= VerbosityLevel::NOTE) \
cerr << "bitstream: " << x << endl
std::cerr << "bitstream: " << x << std::endl
#define BITSTREAM_FATAL(x, pos) \
{ \
ostringstream ss; \
std::ostringstream ss; \
ss << x; \
throw BitstreamParseError(ss.str(), pos); \
}
@ -408,7 +408,7 @@ Bitstream Bitstream::read(std::istream &in)
Chip Bitstream::deserialise_chip()
{
cerr << "bitstream size: " << data.size() * 8 << " bits" << endl;
std::cerr << "bitstream size: " << data.size() * 8 << " bits" << std::endl;
Chip chip(1);
Die &die = chip.get_die(0);
@ -635,7 +635,8 @@ Chip Bitstream::deserialise_chip()
rd.skip_bytes(3);
break;
default:
BITSTREAM_FATAL("Unhandled command 0x" << hex << setw(2) << setfill('0') << int(cmd), rd.get_offset());
BITSTREAM_FATAL("Unhandled command 0x" << std::hex << std::setw(2) << std::setfill('0') << int(cmd),
rd.get_offset());
break;
}
}
@ -803,7 +804,7 @@ const char *BitstreamParseError::what() const noexcept
ss << "Bitstream Parse Error: ";
ss << desc;
if (offset != -1)
ss << " [at 0x" << std::hex << setw(8) << setfill('0') << offset << "]";
ss << " [at 0x" << std::hex << std::setw(8) << std::setfill('0') << offset << "]";
return strdup(ss.str().c_str());
}
} // namespace GateMate

View File

@ -30,41 +30,41 @@ namespace GateMate {
std::string ChipConfig::to_string() const
{
std::stringstream ss;
ss << ".device " << chip_name << endl << endl;
ss << ".device " << chip_name << std::endl << std::endl;
for (const auto &config : configs) {
if (!config.second.empty()) {
ss << ".config " << config.first << " " << endl;
ss << ".config " << config.first << " " << std::endl;
ss << config.second;
ss << endl;
ss << std::endl;
}
}
for (const auto &tile : tiles) {
if (!tile.second.empty()) {
ss << ".tile " << tile.first.die << " " << tile.first.x << " " << tile.first.y << endl;
ss << ".tile " << tile.first.die << " " << tile.first.x << " " << tile.first.y << std::endl;
ss << tile.second;
ss << endl;
ss << std::endl;
}
}
for (const auto &bram : brams) {
if (!bram.second.empty()) {
ss << ".bram " << bram.first.die << " " << bram.first.x << " " << bram.first.y << endl;
ss << ".bram " << bram.first.die << " " << bram.first.x << " " << bram.first.y << std::endl;
ss << bram.second;
ss << endl;
ss << std::endl;
}
}
for (const auto &bram : bram_data) {
if (!bram.second.empty()) {
ss << ".bram_init " << bram.first.die << " " << bram.first.x << " " << bram.first.y << endl;
ios_base::fmtflags f(ss.flags());
ss << ".bram_init " << bram.first.die << " " << bram.first.x << " " << bram.first.y << std::endl;
std::ios_base::fmtflags f(ss.flags());
for (size_t i = 0; i < bram.second.size(); i++) {
ss << setw(2) << setfill('0') << hex << (int)bram.second.at(i);
ss << std::setw(2) << std::setfill('0') << std::hex << (int)bram.second.at(i);
if (i % 32 == 31)
ss << endl;
ss << std::endl;
else
ss << " ";
}
ss.flags(f);
ss << endl;
ss << std::endl;
}
}
return ss.str();
@ -106,15 +106,15 @@ ChipConfig ChipConfig::from_string(const std::string &config)
ss >> loc.die;
ss >> loc.x;
ss >> loc.y;
ios_base::fmtflags f(ss.flags());
std::ios_base::fmtflags f(ss.flags());
while (!skip_check_eor(ss)) {
uint16_t value;
ss >> hex >> value;
ss >> std::hex >> value;
cc.bram_data[loc].push_back(value);
}
ss.flags(f);
} else {
throw runtime_error("unrecognised config entry " + verb);
throw std::runtime_error("unrecognised config entry " + verb);
}
}
return cc;

View File

@ -27,7 +27,7 @@
namespace GateMate {
std::vector<bool> data_bytes_to_array(const vector<uint8_t> &data, size_t count)
std::vector<bool> data_bytes_to_array(const std::vector<uint8_t> &data, size_t count)
{
std::vector<bool> result(count * 8);
for (size_t j = 0; j < count; j++) {
@ -98,13 +98,13 @@ std::vector<uint8_t> BaseBitDatabase::config_to_data(const TileConfig &cfg)
if (words.count(w.name)) {
words[w.name].set_value(tile, w.value);
} else {
throw runtime_error(fmt("unknown word " << w.name << " found while converting data"));
throw std::runtime_error(fmt("unknown word " << w.name << " found while converting data"));
}
}
return bits_to_bytes(tile);
}
TileConfig BaseBitDatabase::data_to_config(const vector<uint8_t> &data)
TileConfig BaseBitDatabase::data_to_config(const std::vector<uint8_t> &data)
{
TileConfig cfg;
std::vector<bool> d = data_bytes_to_array(data, num_bits * 8);
@ -431,7 +431,7 @@ ConfigBitDatabase::ConfigBitDatabase() : BaseBitDatabase(Die::DIE_CONFIG_SIZE *
}
}
vector<bool> WordSettingBits::get_value(const vector<bool> &tile) const
std::vector<bool> WordSettingBits::get_value(const std::vector<bool> &tile) const
{
std::vector<bool> val;
for (int i = start; i < end; i++)
@ -439,12 +439,12 @@ vector<bool> WordSettingBits::get_value(const vector<bool> &tile) const
return val;
}
void WordSettingBits::set_value(vector<bool> &tile, const vector<bool> &value) const
void WordSettingBits::set_value(std::vector<bool> &tile, const std::vector<bool> &value) const
{
for (int i = start; i < end; i++)
tile[i] = value[i - start];
}
DatabaseConflictError::DatabaseConflictError(const string &desc) : runtime_error(desc) {}
DatabaseConflictError::DatabaseConflictError(const std::string &desc) : runtime_error(desc) {}
} // namespace GateMate

View File

@ -21,31 +21,30 @@
#include <algorithm>
#include <sstream>
#include "Util.hpp"
using namespace std;
namespace GateMate {
ostream &operator<<(ostream &out, const ConfigWord &cw)
std::ostream &operator<<(std::ostream &out, const ConfigWord &cw)
{
out << cw.name << " " << to_string(cw.value) << endl;
out << cw.name << " " << to_string(cw.value) << std::endl;
return out;
}
istream &operator>>(istream &in, ConfigWord &cw)
std::istream &operator>>(std::istream &in, ConfigWord &cw)
{
in >> cw.name;
in >> cw.value;
return in;
}
ostream &operator<<(ostream &out, const TileConfig &tc)
std::ostream &operator<<(std::ostream &out, const TileConfig &tc)
{
for (const auto &cword : tc.cwords)
out << cword;
return out;
}
istream &operator>>(istream &in, TileConfig &tc)
std::istream &operator>>(std::istream &in, TileConfig &tc)
{
tc.cwords.clear();
while (!skip_check_eor(in)) {
@ -56,18 +55,18 @@ istream &operator>>(istream &in, TileConfig &tc)
return in;
}
void TileConfig::add_word(const string &name, const vector<bool> &value) { cwords.push_back({name, value}); }
void TileConfig::add_word(const std::string &name, const std::vector<bool> &value) { cwords.push_back({name, value}); }
string TileConfig::to_string() const
std::string TileConfig::to_string() const
{
stringstream ss;
std::stringstream ss;
ss << *this;
return ss.str();
}
TileConfig TileConfig::from_string(const string &str)
TileConfig TileConfig::from_string(const std::string &str)
{
stringstream ss(str);
std::stringstream ss(str);
TileConfig tc;
ss >> tc;
return tc;

View File

@ -29,8 +29,6 @@
#include "ChipConfig.hpp"
#include "version.hpp"
using namespace std;
int main(int argc, char *argv[])
{
using namespace GateMate;
@ -52,49 +50,49 @@ int main(int argc, char *argv[])
po::store(parsed, vm);
po::notify(vm);
} catch (po::required_option &e) {
cerr << "Error: input file is mandatory." << endl << endl;
std::cerr << "Error: input file is mandatory." << std::endl << std::endl;
goto help;
} catch (std::exception &e) {
cerr << "Error: " << e.what() << endl << endl;
std::cerr << "Error: " << e.what() << std::endl << std::endl;
goto help;
}
if (vm.count("help")) {
help:
boost::filesystem::path path(argv[0]);
cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << endl;
cerr << "Copyright (C) 2024 The Project Peppercorn Authors" << endl;
cerr << endl;
cerr << path.stem().c_str() << ": GateMate bitstream packer" << endl;
cerr << endl;
cerr << "Usage: " << argv[0] << " input.config [output.bit] [options]" << endl;
cerr << endl;
cerr << options << endl;
std::cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << std::endl;
std::cerr << "Copyright (C) 2024 The Project Peppercorn Authors" << std::endl;
std::cerr << std::endl;
std::cerr << path.stem().c_str() << ": GateMate bitstream packer" << std::endl;
std::cerr << std::endl;
std::cerr << "Usage: " << argv[0] << " input.config [output.bit] [options]" << std::endl;
std::cerr << std::endl;
std::cerr << options << std::endl;
return vm.count("help") ? 0 : 1;
}
ifstream config_file(vm["input"].as<string>());
std::ifstream config_file(vm["input"].as<std::string>());
if (!config_file) {
cerr << "Failed to open input file" << endl;
std::cerr << "Failed to open input file" << std::endl;
return 1;
}
string textcfg((std::istreambuf_iterator<char>(config_file)), std::istreambuf_iterator<char>());
std::string textcfg((std::istreambuf_iterator<char>(config_file)), std::istreambuf_iterator<char>());
ChipConfig cc;
try {
cc = ChipConfig::from_string(textcfg);
} catch (runtime_error &e) {
cerr << "Failed to process input config: " << e.what() << endl;
} catch (std::runtime_error &e) {
std::cerr << "Failed to process input config: " << e.what() << std::endl;
return 1;
}
Chip c = cc.to_chip();
Bitstream b = Bitstream::serialise_chip(c);
if (vm.count("bit")) {
ofstream bit_file(vm["bit"].as<string>(), ios::binary);
std::ofstream bit_file(vm["bit"].as<std::string>(), std::ios::binary);
if (!bit_file) {
cerr << "Failed to open output file" << endl;
std::cerr << "Failed to open output file" << std::endl;
return 1;
}
b.write_bit(bit_file);

View File

@ -29,8 +29,6 @@
#include "ChipConfig.hpp"
#include "version.hpp"
using namespace std;
int main(int argc, char *argv[])
{
using namespace GateMate;
@ -51,48 +49,48 @@ int main(int argc, char *argv[])
po::store(parsed, vm);
po::notify(vm);
} catch (po::required_option &e) {
cerr << "Error: input file is mandatory." << endl << endl;
std::cerr << "Error: input file is mandatory." << std::endl << std::endl;
goto help;
} catch (std::exception &e) {
cerr << "Error: " << e.what() << endl << endl;
std::cerr << "Error: " << e.what() << std::endl << std::endl;
goto help;
}
if (vm.count("help")) {
help:
boost::filesystem::path path(argv[0]);
cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << endl;
cerr << "Copyright (C) 2024 The Project Peppercorn Authors" << endl;
cerr << endl;
cerr << path.stem().c_str() << ": GateMate bitstream to text config converter" << endl;
cerr << endl;
cerr << "Usage: " << argv[0] << " input.bit [output.config] [options]" << endl;
cerr << endl;
cerr << options << endl;
std::cerr << "Open Source Tools for GateMate FPGAs Version " << git_describe_str << std::endl;
std::cerr << "Copyright (C) 2024 The Project Peppercorn Authors" << std::endl;
std::cerr << std::endl;
std::cerr << path.stem().c_str() << ": GateMate bitstream to text config converter" << std::endl;
std::cerr << std::endl;
std::cerr << "Usage: " << argv[0] << " input.bit [output.config] [options]" << std::endl;
std::cerr << std::endl;
std::cerr << options << std::endl;
return vm.count("help") ? 0 : 1;
}
ifstream bit_file(vm["input"].as<string>(), ios::binary);
std::ifstream bit_file(vm["input"].as<std::string>(), std::ios::binary);
if (!bit_file) {
cerr << "Failed to open input file" << endl;
std::cerr << "Failed to open input file" << std::endl;
return 1;
}
try {
Chip c = Bitstream::read(bit_file).deserialise_chip();
ChipConfig cc = ChipConfig::from_chip(c);
ofstream out_file(vm["textcfg"].as<string>());
std::ofstream out_file(vm["textcfg"].as<std::string>());
if (!out_file) {
cerr << "Failed to open output file" << endl;
std::cerr << "Failed to open output file" << std::endl;
return 1;
}
out_file << cc.to_string();
return 0;
} catch (BitstreamParseError &e) {
cerr << "Failed to process input bitstream: " << e.what() << endl;
std::cerr << "Failed to process input bitstream: " << e.what() << std::endl;
return 1;
} catch (runtime_error &e) {
cerr << "Failed to process input bitstream: " << e.what() << endl;
} catch (std::runtime_error &e) {
std::cerr << "Failed to process input bitstream: " << e.what() << std::endl;
return 1;
}
}