diff options
author | Keith Rothman <537074+litghost@users.noreply.github.com> | 2021-03-22 15:55:34 -0700 |
---|---|---|
committer | Keith Rothman <537074+litghost@users.noreply.github.com> | 2021-03-23 09:01:43 -0700 |
commit | 8a50b02b9bbaf292fb6294eda5df162f3295f149 (patch) | |
tree | 5caa463594b7e2468b4d17ec33aa019ec42cb22d /common | |
parent | af1fba9f5232706af4ae31ad5b0186b44de03cfa (diff) | |
download | nextpnr-8a50b02b9bbaf292fb6294eda5df162f3295f149.tar.gz nextpnr-8a50b02b9bbaf292fb6294eda5df162f3295f149.tar.bz2 nextpnr-8a50b02b9bbaf292fb6294eda5df162f3295f149.zip |
Use new parameter definition data in FPGA interchange processing.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
Diffstat (limited to 'common')
-rw-r--r-- | common/dynamic_bitarray.h | 129 | ||||
-rw-r--r-- | common/property.h | 2 |
2 files changed, 130 insertions, 1 deletions
diff --git a/common/dynamic_bitarray.h b/common/dynamic_bitarray.h index 605d59d5..be41835b 100644 --- a/common/dynamic_bitarray.h +++ b/common/dynamic_bitarray.h @@ -24,6 +24,8 @@ #include <limits> #include <vector> +#include "log.h" +#include "nextpnr_assertions.h" #include "nextpnr_namespaces.h" NEXTPNR_NAMESPACE_BEGIN @@ -73,6 +75,133 @@ template <typename Storage = std::vector<uint8_t>> class DynamicBitarray void clear() { return storage.clear(); } + // Convert IntType to a DynamicBitarray of sufficent width + template <typename IntType> static DynamicBitarray<Storage> to_bitarray(const IntType &value) + { + if (std::numeric_limits<IntType>::is_signed) { + if (value < 0) { + log_error("Expected position value, got %s\n", std::to_string(value).c_str()); + } + } + + DynamicBitarray<Storage> result; + result.resize(std::numeric_limits<IntType>::digits); + result.fill(false); + + // Use a 1 of the right type (for shifting) + IntType one(1); + + for (size_t i = 0; i < std::numeric_limits<IntType>::digits; ++i) { + if ((value & (one << i)) != 0) { + result.set(i, true); + } + } + + return result; + } + + // Convert binary bitstring to a DynamicBitarray of sufficent width + // + // string must be satisfy the following regex: + // + // [01]+ + // + // width can either be specified explicitly, or -1 to use a size wide + // enough to store the given string. + // + // If the width is specified and the width is insufficent it will result + // in an error. + static DynamicBitarray<Storage> parse_binary_bitstring(int width, const std::string &bits) + { + NPNR_ASSERT(width == -1 || width > 0); + + DynamicBitarray<Storage> result; + // If no width was supplied, use the width from the input data. + if (width == -1) { + width = bits.size(); + } + + NPNR_ASSERT(width >= 0); + if ((size_t)width < bits.size()) { + log_error("String '%s' is wider than specified width %d\n", bits.c_str(), width); + } + result.resize(width); + result.fill(false); + + for (size_t i = 0; i < bits.size(); ++i) { + // bits[0] is the MSB! + size_t index = width - 1 - i; + if (!(bits[i] == '1' || bits[i] == '0')) { + log_error("String '%s' is not a valid binary bitstring?\n", bits.c_str()); + } + result.set(index, bits[i] == '1'); + } + + return result; + } + + // Convert hex bitstring to a DynamicBitarray of sufficent width + // + // string must be satisfy the following regex: + // + // [0-9a-fA-F]+ + // + // width can either be specified explicitly, or -1 to use a size wide + // enough to store the given string. + // + // If the width is specified and the width is insufficent it will result + // in an error. + static DynamicBitarray<Storage> parse_hex_bitstring(int width, const std::string &bits) + { + NPNR_ASSERT(width == -1 || width > 0); + + DynamicBitarray<Storage> result; + // If no width was supplied, use the width from the input data. + if (width == -1) { + // Each character is 4 bits! + width = bits.size() * 4; + } + + NPNR_ASSERT(width >= 0); + int rem = width % 4; + size_t check_width = width; + if (rem != 0) { + check_width += (4 - rem); + } + if (check_width < bits.size() * 4) { + log_error("String '%s' is wider than specified width %d (check_width = %zu)\n", bits.c_str(), width, + check_width); + } + + result.resize(width); + result.fill(false); + + size_t index = 0; + for (auto nibble_iter = bits.rbegin(); nibble_iter != bits.rend(); ++nibble_iter) { + char nibble = *nibble_iter; + + int value; + if (nibble >= '0' && nibble <= '9') { + value = nibble - '0'; + } else if (nibble >= 'a' && nibble <= 'f') { + value = 10 + (nibble - 'a'); + } else if (nibble >= 'A' && nibble <= 'F') { + value = 10 + (nibble - 'A'); + } else { + log_error("Invalid hex string '%s'?\n", bits.c_str()); + } + NPNR_ASSERT(value >= 0); + NPNR_ASSERT(value < 16); + + // Insert nibble into bitarray. + for (size_t i = 0; i < 4; ++i) { + result.set(index++, (value & (1 << i)) != 0); + } + } + + return result; + } + private: Storage storage; }; diff --git a/common/property.h b/common/property.h index 79fbf881..7a377b97 100644 --- a/common/property.h +++ b/common/property.h @@ -78,7 +78,7 @@ struct Property result.push_back(c == S1); return result; } - std::string as_string() const + const std::string &as_string() const { NPNR_ASSERT(is_string); return str; |