2019-12-20 08:47:38 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2019 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2019-12-22 08:16:37 +01:00
|
|
|
#include <string.h>
|
2019-12-20 08:47:38 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <locale>
|
2021-02-24 06:36:48 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include "display.hpp"
|
2019-12-20 08:47:38 +01:00
|
|
|
|
|
|
|
|
#include "latticeBitParser.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
LatticeBitParser::LatticeBitParser(const string &filename, bool verbose):
|
|
|
|
|
ConfigBitstreamParser(filename, ConfigBitstreamParser::BIN_MODE, verbose),
|
2021-02-24 06:36:48 +01:00
|
|
|
_endHeader(0)
|
2019-12-20 08:47:38 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
LatticeBitParser::~LatticeBitParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LatticeBitParser::parseHeader()
|
|
|
|
|
{
|
2021-02-24 06:36:48 +01:00
|
|
|
int currPos = 0;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* check header signature */
|
2020-09-26 07:58:03 +02:00
|
|
|
|
|
|
|
|
/* radiant .bit start with LSCC */
|
2021-02-24 06:36:48 +01:00
|
|
|
if (_raw_data[0] == 'L') {
|
|
|
|
|
if (_raw_data.substr(0, 4) != "LSCC") {
|
|
|
|
|
printf("Wrong File %s\n", _raw_data.substr(0, 4).c_str());
|
2020-09-26 07:58:03 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2021-02-24 06:36:48 +01:00
|
|
|
currPos += 4;
|
2020-09-26 07:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* bit file comment area start with 0xff00 */
|
2021-02-24 06:36:48 +01:00
|
|
|
if ((uint8_t)_raw_data[currPos] != 0xff || (uint8_t)_raw_data[currPos + 1] != 0x00) {
|
|
|
|
|
printf("Wrong File %02x%02x\n", (uint8_t) _raw_data[currPos],
|
|
|
|
|
(uint8_t)_raw_data[currPos]);
|
2019-12-20 08:47:38 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2021-02-24 06:36:48 +01:00
|
|
|
currPos+=2;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
|
|
|
|
|
_endHeader = _raw_data.find(0xff, currPos);
|
|
|
|
|
if (_endHeader == string::npos) {
|
|
|
|
|
printError("Error: preamble not found\n");
|
2019-12-22 08:16:37 +01:00
|
|
|
return EXIT_FAILURE;
|
2021-02-24 06:36:48 +01:00
|
|
|
}
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* parse header */
|
|
|
|
|
istringstream lineStream(_raw_data.substr(currPos, _endHeader-currPos));
|
|
|
|
|
string buff;
|
|
|
|
|
while (std::getline(lineStream, buff, '\0')) {
|
|
|
|
|
size_t pos = buff.find_first_of(':', 0);
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
string key(buff.substr(0, pos));
|
|
|
|
|
string val(buff.substr(pos+1, buff.size()));
|
|
|
|
|
int startPos = val.find_first_not_of(" ");
|
|
|
|
|
int endPos = val.find_last_not_of(" ")+1;
|
|
|
|
|
_hdr[key] = val.substr(startPos, endPos).c_str();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LatticeBitParser::parse()
|
|
|
|
|
{
|
|
|
|
|
/* until 0xFFFFBDB3 0xFFFF */
|
|
|
|
|
if (parseHeader() < 0)
|
|
|
|
|
return EXIT_FAILURE;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* check preamble */
|
|
|
|
|
if ((*(uint32_t *)&_raw_data[_endHeader+1]) != 0xb3bdffff) {
|
|
|
|
|
printError("Error: missing preamble\n");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* read All data */
|
|
|
|
|
_bit_data.resize(_raw_data.size() - _endHeader);
|
|
|
|
|
std::move(_raw_data.begin()+_endHeader, _raw_data.end(), _bit_data.begin());
|
2019-12-20 08:47:38 +01:00
|
|
|
_bit_length = _bit_data.size() * 8;
|
2021-02-24 06:36:48 +01:00
|
|
|
|
2019-12-20 08:47:38 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|