9#include "trg/cdc/FpgaUtility.h"
11#include "FpgaUtility.h"
26using std::stringstream;
43 if (value - floor(value) < 0.00001) value = floor(value);
44 if (floor(value + 1) - value < 0.00001) value = floor(value + 1);
50 if (-value + ceil(value) < 0.00001) value = ceil(value);
51 if (-ceil(value - 1) + value < 0.00001) value = ceil(value - 1);
62 if (inRadix < 2 || inRadix > 26) {
63 cout <<
"[Error] arbToDouble() => Radix is out of range [2,26]. Aborting." << endl;
67 for (
unsigned int iChar = 0; iChar < in.size(); iChar++) {
68 if (in[iChar] ==
'-')
continue;
69 result = result * inRadix;
70 if (in[iChar] >=
'0' && in[iChar] <=
'9') result += in[iChar] - 48;
71 else if (in[iChar] >=
'A' && in[iChar] <=
'Z') result += in[iChar] - 55;
72 else if (in[iChar] >=
'a' && in[iChar] <=
'z') result += in[iChar] - 87;
74 cout <<
"[Error] arbToDouble() => Strange character detected. Aborting." << endl;
78 if (in[0] ==
'-') result *= -1;
91 int rem = fmod(in, outRadix);
92 result.insert(result.begin(),
char((rem >= 0 && rem <= 9) ? rem + 48 : rem + 55));
93 in = (in / outRadix) - (rem / outRadix);
95 result.insert(result.begin(), char((in >= 0 && in <= 9) ? in + 48 : in + 55));
100 if (
int(result.size()) < numberOfDigits) {
101 int nBitsAdd = numberOfDigits - result.size();
102 for (
int iZero = 0; iZero < nBitsAdd; iZero++) {
103 result.insert(0,
"0");
105 }
else if (numberOfDigits == -1) {
106 }
else if (
int(result.size()) > numberOfDigits) {
107 cout <<
"[Error] doubleToArb() => numberOfDigits too small. Aborting." << endl;
110 if (sign == -1) result.insert(0,
"-");
128 if (
int(in.size()) < numberOfDigits) {
129 unsigned nBits = numberOfDigits - in.size();
130 for (
unsigned iZero = 0; iZero < nBits; iZero++) {
133 }
else if (
int(in.size()) >= numberOfDigits) {
134 cout <<
"[Error] signedToTwosComplement() => numberOfDigits too small. Aborting." << endl;
142 for (
unsigned int iChar = 0; iChar < in.size(); iChar++) {
143 if (in[iChar] ==
'1') in[iChar] =
'0';
144 else if (in[iChar] ==
'0') in[iChar] =
'1';
145 else cout <<
"[Error] signedToTwosComplement() => Strange character in string. Aborting." << endl;
149 for (
int iDigit = in.size() - 1; iDigit >= 0; iDigit--) {
150 if (in[
unsigned(iDigit)] ==
'0') {
151 in[unsigned(iDigit)] =
'1';
154 in[unsigned(iDigit)] =
'0';
169 for (
int iDigit = in.size() - 1; iDigit >= 0; iDigit--) {
170 if (in[
unsigned(iDigit)] ==
'1') {
171 in[unsigned(iDigit)] =
'0';
174 in[unsigned(iDigit)] =
'1';
179 for (
unsigned int iChar = 0; iChar < in.size(); iChar++) {
180 if (in[iChar] ==
'1') in[iChar] =
'0';
181 else if (in[iChar] ==
'0') in[iChar] =
'1';
182 else cout <<
"[Error] twosComplementToSigned() => Strange character in string. Aborting." << endl;
186 for (
int iDigit = 0; iDigit < int(in.size()); iDigit++) {
187 if (in[
unsigned(iDigit)] ==
'0') {
188 if (in.size() == 1)
break;
206 outFile.open(outFilePath.c_str());
208 for (
auto it = data.begin(); it != data.end(); it++) {
209 outFile << (*it).first <<
" ";
210 for (
unsigned iVector = 0; iVector < (*it).second.size(); iVector++) {
211 outFile << (*it).second[iVector] <<
" ";
221 const std::string& fileDirectory)
224 for (
auto it = data.begin(); it != data.end(); it++) {
234 coeFile.open(outFilePath.c_str());
236 coeFile <<
"memory_initialization_radix=10;" << endl;
237 coeFile <<
"memory_initialization_vector=" << endl;
238 unsigned nEntries = pow(2, lutInBitsize);
240 unsigned nDataSize = data.size();
241 for (
unsigned index = 0; index < (nEntries - 1); index++) {
242 if (index < nDataSize) coeFile << data[index] <<
"," << endl;
243 else coeFile << 0 <<
"," << endl;
245 if (nEntries <= nDataSize) coeFile << data[nEntries - 1] <<
";" << endl;
246 else coeFile << 0 <<
";" << endl;
255 coeFile.open(inFilePath.c_str());
256 if (coeFile.fail()) {
257 cout <<
"[Error] FpgaUtility::readCoe() => Can not open file: " << inFilePath << endl;
262 stringstream reformatCoeFile;
264 while (getline(coeFile, t_line)) {
266 t_line.erase(remove_if(t_line.begin(), t_line.end(), ::isspace), t_line.end());
268 if (t_line.size() == 0)
continue;
270 size_t t_iFind = t_line.find(
";");
271 if (t_iFind != string::npos) {
272 t_line.erase(t_iFind + 1, string::npos);
275 if (t_line ==
";")
continue;
277 if (t_line.find(
";") == string::npos) reformatCoeFile << t_line;
279 else reformatCoeFile << t_line.substr(0, t_line.size() - 1) << endl;
284 vector<string> t_rawData;
285 while (getline(reformatCoeFile, t_line)) {
288 size_t t_iFind = t_line.find(
"=");
289 if (t_iFind != string::npos) {
292 if (t_line.substr(0, t_iFind) ==
"memory_initialization_radix") {
294 t_radix = atoi(t_line.substr(t_iFind + 1, string::npos).c_str());
295 }
else if (t_line.substr(0, t_iFind) ==
"memory_initialization_vector") {
297 string t_dataLine = t_line.substr(t_iFind + 1, string::npos);
299 t_iFind = t_dataLine.find(
",");
300 if (t_iFind != string::npos) {
301 t_rawData.push_back(t_dataLine.substr(0, t_iFind));
302 t_dataLine.erase(0, t_iFind + 1);
305 t_rawData.push_back(t_dataLine);
310 cout <<
"[Error] FpgaUtility::readCoe() => .coe format keyword is wrong. Aborting" << endl;
314 cout <<
"[Error] FpgaUtility::readCoe() => .coe format is wrong. Needs keywords. Aborting" << endl;
325 data.resize(t_rawData.size());
327 if (!twosComplement) {
328 for (
int iData = 0; iData < int(t_rawData.size()); iData++) {
334 int nBits = t_rawData[0].size();
336 int t_max = pow(t_radix, nBits) - 1;
338 nBits = floor(log(t_max) / log(2)) + 1;
339 for (
int iData = 0; iData < int(t_rawData.size()); iData++) {
static std::string doubleToArb(double in, int outRadix, int numberOfDigits=-1)
Change octal to string.
static void writeCoe(std::string outFilePath, int lutInBitsize, std::vector< signed long long > &data)
Writes a signal's values to a file in coe format.
static double arbToDouble(std::string in, int inRadix)
Radix changing functions.
static void writeSignals(std::string outFilePath, std::map< std::string, std::vector< signed long long > > &data)
COE file functions.
static std::string arbToArb(const std::string &in, int inRadix, int outRadix, int numberOfDigits=-1)
Change string number to another string number depending on radix.
static std::string twosComplementToSigned(std::string in)
Changes string two complements to string signed binary(-sign).
static double roundInt(double value)
Round double value.
static std::string signedToTwosComplement(std::string in, int numberOfDigits)
Changes string signed binary(-sign) to two complements.
static void multipleWriteCoe(int lutInBitsize, std::map< std::string, std::vector< signed long long > > &data, const std::string &fileDirectory)
Writes multiple signal values to a file in coe format.
static void readCoe(std::string inFilePath, std::vector< signed long long > &data, bool twoscomplement=0)
Reads a coe format file and stores the values in vector.