Files
openFPGALoader/src/efinixHexParser.cpp
T

78 lines
1.7 KiB
C++
Raw Normal View History

2021-06-26 15:24:07 +02:00
// SPDX-License-Identifier: Apache-2.0
2020-10-31 08:46:53 +01:00
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <[email protected]>
*/
2021-02-24 06:36:48 +01:00
#include <sstream>
2020-10-31 08:46:53 +01:00
#include <stdexcept>
#include "configBitstreamParser.hpp"
#include "display.hpp"
#include "efinixHexParser.hpp"
using namespace std;
2022-03-20 08:51:18 +01:00
EfinixHexParser::EfinixHexParser(const string &filename):
2020-10-31 08:46:53 +01:00
ConfigBitstreamParser(filename, ConfigBitstreamParser::ASCII_MODE,
2022-03-20 08:51:18 +01:00
false)
2020-10-31 08:46:53 +01:00
{}
int EfinixHexParser::parseHeader()
{
string buffer;
istringstream lineStream(_raw_data);
int bytesRead = 0;
string headerText;
bool foundPaddedBits = false;
while (std::getline(lineStream, buffer, '\n')) {
bytesRead += buffer.size() + 1;
if (buffer != "0A") {
try {
uint8_t byte = std::stol(buffer, nullptr, 16);
headerText += (char)byte;
} catch (...) {
}
} else {
headerText += '\n';
if (foundPaddedBits)
break;
}
if (headerText.find("PADDED_BITS") != string::npos)
foundPaddedBits = true;
}
size_t pos;
if ((pos = headerText.find("Mode: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["mode"] = headerText.substr(pos + 6, end - pos - 6);
}
if ((pos = headerText.find("Width: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["width"] = headerText.substr(pos + 7, end - pos - 7);
}
if ((pos = headerText.find("Device: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["device"] = headerText.substr(pos + 8, end - pos - 8);
}
return bytesRead;
}
2020-10-31 08:46:53 +01:00
int EfinixHexParser::parse()
{
2021-02-24 06:36:48 +01:00
string buffer;
parseHeader();
2021-02-24 06:36:48 +01:00
istringstream lineStream(_raw_data);
2020-10-31 08:46:53 +01:00
2021-02-24 06:36:48 +01:00
while (std::getline(lineStream, buffer, '\n')) {
2023-08-08 16:54:27 +03:00
_bit_data.push_back(std::stol(buffer, nullptr, 16));
2020-10-31 08:46:53 +01:00
}
_bit_length = _bit_data.size() * 8;
return EXIT_SUCCESS;
}