2019-09-26 18:29:20 +02:00
|
|
|
#include "bitparser.hpp"
|
2021-02-17 19:02:57 +01:00
|
|
|
#include "display.hpp"
|
2019-09-26 18:29:20 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <iostream>
|
2020-07-25 04:55:41 +02:00
|
|
|
#ifndef _WIN32
|
2019-09-26 18:29:20 +02:00
|
|
|
#include <arpa/inet.h>
|
2020-07-25 04:55:41 +02:00
|
|
|
#else
|
2020-07-25 08:21:27 +02:00
|
|
|
//for ntohs
|
|
|
|
|
#include <winsock2.h>
|
2020-07-25 04:55:41 +02:00
|
|
|
#endif
|
2019-09-26 18:29:20 +02:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
#define display(...) \
|
2019-11-21 08:55:58 +01:00
|
|
|
do { if (_verbose) fprintf(stdout, __VA_ARGS__);} while(0)
|
|
|
|
|
|
2020-09-25 18:42:32 +02:00
|
|
|
BitParser::BitParser(const string &filename, bool reverseOrder, bool verbose):
|
2019-11-21 08:55:58 +01:00
|
|
|
ConfigBitstreamParser(filename, ConfigBitstreamParser::BIN_MODE,
|
2021-02-17 19:02:57 +01:00
|
|
|
verbose), _reverseOrder(reverseOrder)
|
2019-09-26 18:29:20 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
BitParser::~BitParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 10:28:44 +02:00
|
|
|
int BitParser::parseField()
|
2019-09-26 18:29:20 +02:00
|
|
|
{
|
2019-10-05 10:28:44 +02:00
|
|
|
int ret = 1;
|
2019-09-26 18:29:20 +02:00
|
|
|
short length;
|
2021-02-17 19:02:57 +01:00
|
|
|
string tmp(64, ' ');
|
2019-09-26 18:29:20 +02:00
|
|
|
int pos, prev_pos;
|
2019-10-05 10:28:44 +02:00
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
|
uint8_t type;
|
|
|
|
|
_fd.read((char *)&type, sizeof(uint8_t));
|
|
|
|
|
|
2019-09-26 18:29:20 +02:00
|
|
|
if (type != 'e') {
|
2019-10-05 10:28:44 +02:00
|
|
|
_fd.read((char*)&length, sizeof(uint16_t));
|
2019-09-26 18:29:20 +02:00
|
|
|
length = ntohs(length);
|
|
|
|
|
} else {
|
|
|
|
|
length = 4;
|
|
|
|
|
}
|
2021-02-17 19:02:57 +01:00
|
|
|
_fd.read(&tmp[0], sizeof(uint8_t)*length);
|
|
|
|
|
|
2019-09-26 18:29:20 +02:00
|
|
|
switch (type) {
|
|
|
|
|
case 'a': /* design name:userid:synthesize tool version */
|
|
|
|
|
prev_pos = 0;
|
2021-02-17 19:02:57 +01:00
|
|
|
pos = tmp.find(";");
|
|
|
|
|
_hdr["design_name"] = tmp.substr(prev_pos, pos);
|
2019-09-26 18:29:20 +02:00
|
|
|
prev_pos = pos+1;
|
|
|
|
|
|
2021-02-17 19:02:57 +01:00
|
|
|
pos = tmp.find(";", prev_pos);
|
|
|
|
|
prev_pos = tmp.find("=", prev_pos) + 1;
|
|
|
|
|
_hdr["userID"] = tmp.substr(prev_pos, pos-prev_pos);
|
2019-09-26 18:29:20 +02:00
|
|
|
prev_pos = pos+1;
|
|
|
|
|
|
2021-02-17 19:02:57 +01:00
|
|
|
prev_pos = tmp.find("=", prev_pos) + 1;
|
|
|
|
|
_hdr["toolVersion"] = tmp.substr(prev_pos);
|
2019-09-26 18:29:20 +02:00
|
|
|
break;
|
|
|
|
|
case 'b': /* FPGA model */
|
2021-02-17 19:02:57 +01:00
|
|
|
_hdr["part_name"] = tmp;
|
2019-09-26 18:29:20 +02:00
|
|
|
break;
|
|
|
|
|
case 'c': /* buildDate */
|
2021-02-17 19:02:57 +01:00
|
|
|
_hdr["date"] = tmp;
|
2019-09-26 18:29:20 +02:00
|
|
|
break;
|
|
|
|
|
case 'd': /* buildHour */
|
2021-02-17 19:02:57 +01:00
|
|
|
_hdr["hour"] = tmp;
|
2019-09-26 18:29:20 +02:00
|
|
|
break;
|
|
|
|
|
case 'e': /* file size */
|
2019-10-05 10:28:44 +02:00
|
|
|
_bit_length = 0;
|
2019-09-26 18:29:20 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2019-10-05 10:28:44 +02:00
|
|
|
_bit_length <<= 8;
|
|
|
|
|
_bit_length |= 0xff & tmp[i];
|
2019-09-26 18:29:20 +02:00
|
|
|
}
|
2019-10-05 10:28:44 +02:00
|
|
|
ret = 0;
|
2019-09-26 18:29:20 +02:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-05 10:28:44 +02:00
|
|
|
return ret;
|
2019-09-26 18:29:20 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
int BitParser::parse()
|
|
|
|
|
{
|
2019-10-05 10:28:44 +02:00
|
|
|
uint16_t length;
|
2019-09-26 18:29:20 +02:00
|
|
|
display("parser\n\n");
|
|
|
|
|
|
|
|
|
|
/* Field 1 : misc header */
|
2019-10-05 10:28:44 +02:00
|
|
|
_fd.read((char*)&length, sizeof(uint16_t));
|
2019-09-26 18:29:20 +02:00
|
|
|
length = ntohs(length);
|
2019-10-05 10:28:44 +02:00
|
|
|
_fd.seekg(length, _fd.cur);
|
|
|
|
|
|
|
|
|
|
_fd.read((char*)&length, sizeof(uint16_t));
|
2019-09-26 18:29:20 +02:00
|
|
|
length = ntohs(length);
|
|
|
|
|
|
|
|
|
|
/* process all field */
|
2019-10-05 10:28:44 +02:00
|
|
|
do {} while (parseField());
|
2019-09-26 18:29:20 +02:00
|
|
|
|
2019-11-21 08:55:58 +01:00
|
|
|
if (_verbose) {
|
2021-02-17 19:02:57 +01:00
|
|
|
cout << "bitstream header infos" << endl;
|
|
|
|
|
for (auto it = _hdr.begin(); it != _hdr.end(); it++) {
|
|
|
|
|
printInfo((*it).first + ": ", false);
|
|
|
|
|
printSuccess((*it).second);
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
2019-11-21 08:55:58 +01:00
|
|
|
}
|
2019-09-26 18:29:20 +02:00
|
|
|
|
|
|
|
|
/* rest of the file is data to send */
|
2019-10-05 10:28:44 +02:00
|
|
|
_fd.read((char *)&_bit_data[0], sizeof(uint8_t) * _bit_length);
|
|
|
|
|
if (_fd.gcount() != _bit_length) {
|
2021-02-17 19:02:57 +01:00
|
|
|
printError("Error: data read different to asked length ", false);
|
|
|
|
|
printError(to_string(_fd.gcount()) + " " + to_string(_bit_length));
|
2019-09-26 18:29:20 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-25 18:42:32 +02:00
|
|
|
if (_reverseOrder) {
|
|
|
|
|
for (int i = 0; i < _bit_length; i++) {
|
|
|
|
|
_bit_data[i] = reverseByte(_bit_data[i]);
|
|
|
|
|
}
|
2019-09-26 18:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-25 18:42:32 +02:00
|
|
|
/* convert size to bit */
|
|
|
|
|
_bit_length *= 8;
|
|
|
|
|
|
2019-09-26 18:29:20 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|