Refactor BitstreamReader::InitWithBytes template method

Signed-off-by: Tomasz Michalak <tmichalak@antmicro.com>
This commit is contained in:
Tomasz Michalak 2019-10-17 21:44:48 +02:00
parent 3f574743cf
commit 7bc54d0c56
1 changed files with 7 additions and 58 deletions

View File

@ -72,16 +72,16 @@ class BitstreamReader {
std::vector<uint32_t> words_;
};
template <>
template <typename ArchType>
template <typename T>
absl::optional<BitstreamReader<Series7>>
BitstreamReader<Series7>::InitWithBytes(T bitstream) {
absl::optional<BitstreamReader<ArchType>>
BitstreamReader<ArchType>::InitWithBytes(T bitstream) {
// If this is really a Xilinx bitstream, there will be a sync
// word somewhere toward the beginning.
auto sync_pos = std::search(bitstream.begin(), bitstream.end(),
kSyncWord.begin(), kSyncWord.end());
if (sync_pos == bitstream.end()) {
return absl::optional<BitstreamReader<Series7>>();
return absl::optional<BitstreamReader<ArchType>>();
}
sync_pos += kSyncWord.size();
@ -91,63 +91,12 @@ BitstreamReader<Series7>::InitWithBytes(T bitstream) {
bitstream_span.subspan(sync_pos - bitstream.begin());
// Convert the bytes into 32-bit, big-endian words.
auto big_endian_reader = make_big_endian_span<uint32_t>(config_packets);
auto big_endian_reader =
make_big_endian_span<typename ArchType::WordType>(config_packets);
std::vector<uint32_t> words{big_endian_reader.begin(),
big_endian_reader.end()};
return BitstreamReader<Series7>(std::move(words));
}
template <>
template <typename T>
absl::optional<BitstreamReader<UltraScale>>
BitstreamReader<UltraScale>::InitWithBytes(T bitstream) {
// If this is really a Xilinx bitstream, there will be a sync
// word somewhere toward the beginning.
auto sync_pos = std::search(bitstream.begin(), bitstream.end(),
kSyncWord.begin(), kSyncWord.end());
if (sync_pos == bitstream.end()) {
return absl::optional<BitstreamReader<UltraScale>>();
}
sync_pos += kSyncWord.size();
// Wrap the provided container in a span that strips off the preamble.
absl::Span<typename T::value_type> bitstream_span(bitstream);
auto config_packets =
bitstream_span.subspan(sync_pos - bitstream.begin());
// Convert the bytes into 32-bit, big-endian words.
auto big_endian_reader = make_big_endian_span<uint32_t>(config_packets);
std::vector<uint32_t> words{big_endian_reader.begin(),
big_endian_reader.end()};
return BitstreamReader<UltraScale>(std::move(words));
}
template <>
template <typename T>
absl::optional<BitstreamReader<UltraScalePlus>>
BitstreamReader<UltraScalePlus>::InitWithBytes(T bitstream) {
// If this is really a Xilinx bitstream, there will be a sync
// word somewhere toward the beginning.
auto sync_pos = std::search(bitstream.begin(), bitstream.end(),
kSyncWord.begin(), kSyncWord.end());
if (sync_pos == bitstream.end()) {
return absl::optional<BitstreamReader<UltraScalePlus>>();
}
sync_pos += kSyncWord.size();
// Wrap the provided container in a span that strips off the preamble.
absl::Span<typename T::value_type> bitstream_span(bitstream);
auto config_packets =
bitstream_span.subspan(sync_pos - bitstream.begin());
// Convert the bytes into 32-bit, big-endian words.
auto big_endian_reader = make_big_endian_span<uint32_t>(config_packets);
std::vector<uint32_t> words{big_endian_reader.begin(),
big_endian_reader.end()};
return BitstreamReader<UltraScalePlus>(std::move(words));
return BitstreamReader<ArchType>(std::move(words));
}
// Sync word as specified in UG470 page 81