cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
common.cpp
Go to the documentation of this file.
1 
17 
30 #include "../include/common.hpp"
31 
32 using namespace std;
33 
34 
35 
36 
37 
45 bool setInputDataStream(stream<UdpWord> &sDataStream, const string dataStreamName,
46  const string inpFileName, int simCnt) {
47  string strLine;
48  ifstream inpFileStream;
49  string datFile = "../../../../test/" + inpFileName;
50  UdpWord udpWord=NetworkWord(0,0,0);
51 
52  //-- STEP-1 : OPEN FILE
53  inpFileStream.open(datFile.c_str());
54  if ( !inpFileStream ) {
55  cout << "### ERROR : Could not open the input data file " << datFile << endl;
56  return(KO);
57  }
58 
59  //-- STEP-2 : SET DATA STREAM
60  while (inpFileStream) {
61 
62  if (!inpFileStream.eof()) {
63 
64  getline(inpFileStream, strLine);
65  if (strLine.empty()) continue;
66  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
67 
68  // Write to sDataStream
69  if (sDataStream.full()) {
70  printf("### ERROR : Stream is full. Cannot write stream with data from file \"%s\".\n", inpFileName.c_str());
71  return(KO);
72  } else {
73  sDataStream.write(udpWord);
74  // Print Data to console
75  #if DEBUG_LEVEL == TRACE_ALL
76  printf("[%4.4d] TB is filling input stream [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
77  simCnt, dataStreamName.c_str(),
78  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
79  #endif
80  }
81  }
82  }
83 
84  //-- STEP-3: CLOSE FILE
85  inpFileStream.close();
86  //strLine.clear();
87 
88  return(OK);
89 }
90 
91 
92 
93 
94 
103 bool readDataStream(stream <UdpWord> &sDataStream, UdpWord *udpWord) {
104  // Get the DUT/Data results
105  sDataStream.read(*udpWord);
106  return(VALID);
107 }
108 
109 
110 
116 ap_uint<64> pack_ap_uint_64_ (ap_uint<8> *buffer) {
117 
118  ap_uint<64> value = 0;
119 
120  value = buffer[7];
121  value = (value << 8 ) + buffer[6];
122  value = (value << 8 ) + buffer[5];
123  value = (value << 8 ) + buffer[4];
124  value = (value << 8 ) + buffer[3];
125  value = (value << 8 ) + buffer[2];
126  value = (value << 8 ) + buffer[1];
127  value = (value << 8 ) + buffer[0];
128 
129  return value ;
130 
131 }
132 
133 
139 void unpack_ap_uint_64_ (ap_uint<64> value, ap_uint<8> *buffer) {
140 
141  for (unsigned int i=0; i<8; i++) {
142  buffer[i] = (value >> 8*i );
143  }
144 }
145 
146 
153 bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream) {
154  if (!outFileStream.is_open()) {
155  printf("### ERROR : Output file stream is not open. \n");
156  return(KO);
157  }
158  outFileStream << hex << noshowbase << setfill('0') << setw(16) << udpWord->tdata.to_uint64();
159  outFileStream << " ";
160  outFileStream << hex << noshowbase << setfill('0') << setw(2) << udpWord->tkeep.to_int();
161  outFileStream << " ";
162  outFileStream << setw(1) << udpWord->tlast.to_int() << "\n";
163  return(OK);
164 }
165 
166 
167 
168 
176 bool getOutputDataStream(stream<UdpWord> &sDataStream,
177  const string dataStreamName, const string outFileName, int simCnt)
178 {
179  //string strLine;
180  ofstream outFileStream;
181  string datFile = "../../../../test/" + outFileName;
182  UdpWord udpWord=NetworkWord(0,0,0);
183  bool rc = OK;
184 
185  //-- STEP-1 : OPEN FILE
186  outFileStream.open(datFile.c_str());
187  if ( !outFileStream ) {
188  cout << "### ERROR : Could not open the output data file " << datFile << endl;
189  return(KO);
190  }
191 
192  //-- STEP-2 : EMPTY STREAM AND DUMP DATA TO FILE
193  while (!sDataStream.empty()) {
194  if (readDataStream(sDataStream, &udpWord) == VALID) {
195  // Print DUT/Data to console
196  #if DEBUG_LEVEL == TRACE_ALL
197  printf("[%4.4d] TB is draining output stream [%s] - Data read = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
198  simCnt, dataStreamName.c_str(),
199  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
200  #endif
201  if (!dumpDataToFile(&udpWord, outFileStream)) {
202  rc = KO;
203  break;
204  }
205  }
206  }
207 
208  //-- STEP-3: CLOSE FILE
209  outFileStream.close();
210 
211  return(rc);
212 }
213 
214 
222 bool dumpStringToFile(const string s, const string outFileName, int simCnt)
223 {
224  //string strLine;
225  ofstream outFileStream;
226  string datFile = "../../../../test/" + outFileName;
227  UdpWord udpWord=NetworkWord(0,0,0);
228  bool rc = OK;
229  unsigned int bytes_per_line = 8;
230 
231  //-- STEP-1 : OPEN FILE
232  outFileStream.open(datFile.c_str());
233  if ( !outFileStream ) {
234  cout << "### ERROR : Could not open the output data file " << datFile << endl;
235  return(KO);
236  }
237  #if DEBUG_LEVEL == TRACE_ALL
238  printf("came to dumpStringToFile: s.length()=%u\n", s.length());
239  #endif
240 
241  ap_uint<8> value[bytes_per_line];
242  unsigned int total_bytes = 0;
243 
244  //-- STEP-2 : DUMP STRING DATA TO FILE
245  for (unsigned int i = 0; i < s.length(); i+=bytes_per_line, total_bytes+=bytes_per_line) {
246  //if (NPIX == XF_NPPC8) {
247  for (unsigned int k = 0; k < bytes_per_line; k++) {
248  if (i+k < s.length()) {
249  value[k] = s[i+k];
250  }
251  else {
252  value[k] = 0;
253  }
254  #if DEBUG_LEVEL == TRACE_ALL
255  printf("DEBUG: In dumpStringToFile: value[%u]=%c\n", k, (char)value[k]);
256  #endif
257  }
258  udpWord.tdata = pack_ap_uint_64_(value);
259  udpWord.tkeep = 255;
260  // We are signaling a packet termination either at the end of the image or the end of MTU
261  if ((total_bytes >= (s.length() - bytes_per_line)) ||
262  ((total_bytes + bytes_per_line) % PACK_SIZE == 0)) {
263  udpWord.tlast = 1;
264  }
265  else {
266  udpWord.tlast = 0;
267  }
268  #if DEBUG_LEVEL == TRACE_ALL
269  printf("[%4.4d] IMG TB is dumping string to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
270  simCnt, datFile.c_str(), total_bytes, value,
271  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
272  #endif
273  if (!dumpDataToFile(&udpWord, outFileStream)) {
274  rc = KO;
275  break;
276  }
277  }
278  //-- STEP-3: CLOSE FILE
279  outFileStream.close();
280 
281  return(rc);
282 }
283 
284 
292 bool dumpStringToFileOnlyRawData(const string s, const string outFileName, int simCnt, size_t out_size)
293 {
294  //printStringHex(s, out_size);
295 
296  //string strLine;
297  ofstream outFileStream;
298  string datFile = outFileName; //"../../../../test/" + outFileName;
299  bool rc = OK;
300  unsigned int bytes_per_line = 8;
301  ap_uint<64> tdata = 0;
302 
303  //-- STEP-1 : OPEN FILE
304  outFileStream.open(datFile.c_str());
305  if ( !outFileStream ) {
306  cout << "### ERROR : Could not open the output data file " << datFile << endl;
307  return(KO);
308  }
309  #if DEBUG_LEVEL == TRACE_ALL
310  printf("came to dumpStringToFileOnlyRawData: s.length()=%u\n", out_size);
311  #endif
312 
313  ap_uint<8> value[bytes_per_line];
314  unsigned int total_bytes = 0;
315  //-- STEP-2 : DUMP STRING DATA TO FILE
316  for (unsigned int i = 0; i < out_size; i+=bytes_per_line, total_bytes+=bytes_per_line) {
317  for (unsigned int k = 0; k < bytes_per_line; k++) {
318  if (i+k < out_size) {
319  value[k] = s[i+k];
320  }
321  else {
322  value[k] = 0;
323  }
324  #if DEBUG_LEVEL == TRACE_ALL
325  printf("DEBUG: In dumpStringToFileOnlyRawData: value[%u]=%c\n", k, (char)value[k]);
326  #endif
327  }
328  tdata = pack_ap_uint_64_(value);
329  #if DEBUG_LEVEL == TRACE_ALL
330  printf("[%4.4d] IMG TB is dumping string to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX} \n",
331  simCnt, datFile.c_str(), total_bytes, value, tdata.to_long());
332  #endif
333  outFileStream << hex << setfill('0') << setw(16) << tdata.to_uint64();
334  //outFileStream << hex << noshowbase << setfill('0') << setw(16) << tdata.to_uint64();
335  outFileStream << "\n";
336  }
337  //-- STEP-3: CLOSE FILE
338  outFileStream.close();
339 
340  return(rc);
341 }
342 
343 
354 bool dumpStringToFileWithLastSetEveryGnoPackets(string s, const string outFileName, int simCnt, int gno)
355 {
356  string strLine;
357  ofstream outFileStream;
358  string datFile = "../../../../test/" + outFileName;
359  UdpWord udpWord=NetworkWord(0,0,0);
360  bool rc = OK;
361  unsigned int bytes_per_line = 8;
362 
363  //-- STEP-1 : OPEN FILE
364  outFileStream.open(datFile.c_str());
365  if ( !outFileStream ) {
366  cout << "### ERROR : Could not open the output data file " << datFile << endl;
367  return(KO);
368  }
369  #if DEBUG_LEVEL == TRACE_ALL
370  printf("came to dumpStringToFile: s.length()=%u\n", s.length());
371  #endif
372 
373  ap_uint<8> value[bytes_per_line];
374  unsigned int total_bytes = 0;
375  int cntr = 0;
376 
377  //-- STEP-2 : DUMP STRING DATA TO FILE
378  for (unsigned int i = 0; i < s.length(); cntr += 1, i+=bytes_per_line, total_bytes+=bytes_per_line) {
379  //if (NPIX == XF_NPPC8) {
380  for (unsigned int k = 0; k < bytes_per_line; k++) {
381  if (i+k < s.length()) {
382  value[k] = s[i+k];
383  }
384  else {
385  value[k] = 0;
386  }
387  #if DEBUG_LEVEL == TRACE_ALL
388  printf("DEBUG: In dumpStringToFile: value[%u]=%c\n", k, (char)value[k]);
389  #endif
390  }
391  udpWord.tdata = pack_ap_uint_64_(value);
392  udpWord.tkeep = 255;
393  // We are signaling a packet termination either at the end of the image or the end of MTU
394  if ((total_bytes >= (s.length() - bytes_per_line)) ||
395  ((total_bytes + bytes_per_line) % PACK_SIZE == 0) || ( cntr!= 0 && ((cntr+1) % gno == 0))) {
396  udpWord.tlast = 1;
397  }
398  else {
399  udpWord.tlast = 0;
400  }
401  #if DEBUG_LEVEL == TRACE_ALL
402  printf("[%4.4d] IMG TB is dumping string to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
403  simCnt, datFile.c_str(), total_bytes, value,
404  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
405  #endif
406  if (!dumpDataToFile(&udpWord, outFileStream)) {
407  rc = KO;
408  break;
409  }
410  }
411  //-- STEP-3: CLOSE FILE
412  outFileStream.close();
413 
414  return(rc);
415 }
416 
417 
424 bool dumpFileToString(const string inpFileName, char* charOutput, int simCnt) {
425  string strLine;
426  ifstream inpFileStream;
427  string datFile = "../../../../test/" + inpFileName;
428  UdpWord udpWord=NetworkWord(0,0,0);
429  unsigned int i = 0;
430  unsigned int bytes_per_line = 8;
431  ap_uint<8> value[bytes_per_line];
432 
433  for(i=0; i < bytes_per_line; i++){
434  value[i]=0;
435  }
436  i=0;
437  //-- STEP-1 : OPEN FILE
438  inpFileStream.open(datFile.c_str());
439  if ( !inpFileStream ) {
440  cout << "### ERROR : Could not open the input data file " << datFile << endl;
441  return(KO);
442  }
443 
444  //-- STEP-2 : SET DATA STREAM
445  while (inpFileStream) {
446 
447  if (!inpFileStream.eof()) {
448 
449  getline(inpFileStream, strLine);
450  //cout << strLine << endl;
451  if (strLine.empty()) continue;
452  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
453  // Write to strOutput
454  //printf("Debug: (char)udpWord.tdata.to_long()=%c\n", (char)udpWord.tdata.to_long());
455  unpack_ap_uint_64_(udpWord.tdata, value);
456  for (unsigned int k = 0; k < bytes_per_line; k++) {
457  charOutput[i++] = value[k];
458  // Print Data to console
459  #if DEBUG_LEVEL == TRACE_ALL
460  printf("[%4.4d] TB is filling string with character %c\n",
461  simCnt, (char)value[k]);
462  #endif
463  }
464  }
465  }
466  //-- STEP-3: CLOSE FILE
467  inpFileStream.close();
468  //strLine.clear();
469 
470  return(OK);
471 }
472 
473 
480 template<unsigned int bytes_per_line = 8>
481 string dumpFileToStringRawDataString(const string inpFileName, int * rawdatalines, size_t outputSize) {
482  string strLine;
483  string tmp_Out;
484  ifstream inpFileStream;
485  string datFile = inpFileName;
486  string charOutput;
487  //charOutput.reserve(outputSize);
488  //strLine.reserve(outputSize);
489  //tmp_Out.reserve(bytes_per_line);
490  unsigned long long int mylongunsigned;
491  unsigned long long int zero_byte=0;
492  unsigned int i = 0;
493  char my_tmp_buf [bytes_per_line];
494  //-- STEP-1 : OPEN FILE
495  inpFileStream.open(datFile.c_str());
496 
497  if ( !inpFileStream ) {
498  cout << "### ERROR : Could not open the input data file " << datFile << endl;
499  return "";
500  }
501 
502  //-- STEP-2 : SET DATA STREAM
503  while (inpFileStream) {
504 
505  if (!inpFileStream.eof()) {
506 
507  getline(inpFileStream, strLine);
508  memcpy(my_tmp_buf,&zero_byte, bytes_per_line);
509  if (strLine.empty()) continue;
510  *rawdatalines+=1;
511  mylongunsigned=stoul(strLine,nullptr,16);
512  hex2ascii(strLine, tmp_Out);
513  // Write to strOutput
514  memcpy(my_tmp_buf,(char *)&mylongunsigned, sizeof(unsigned long long int));
515  charOutput.append(my_tmp_buf, bytes_per_line);
516  i++;
517  }
518  }
519  //-- STEP-3: CLOSE FILE
520  inpFileStream.close();
521  //tmp_Out.clear();
522 
523  return string(charOutput);
524 }
525 
526 
532 // C++98 guarantees that '0', '1', ... '9' are consecutive.
533 // It only guarantees that 'a' ... 'f' and 'A' ... 'F' are
534 // in increasing order, but the only two alternative encodings
535 // of the basic source character set that are still used by
536 // anyone today (ASCII and EBCDIC) make them consecutive.
537 unsigned char hexval(unsigned char c)
538 {
539  if ('0' <= c && c <= '9')
540  return c - '0';
541  else if ('a' <= c && c <= 'f')
542  return c - 'a' + 10;
543  else if ('A' <= c && c <= 'F')
544  return c - 'A' + 10;
545  else abort();
546 }
547 
548 
554 void hex2ascii(const string& in, string& out)
555 {
556  out.clear();
557  out.reserve(in.length() / 2);
558  for (string::const_iterator p = in.begin(); p != in.end(); p++)
559  {
560  unsigned char c = hexval(*p);
561  p++;
562  if (p == in.end()) break; // incomplete last digit - should report error
563  c = (c << 4) + hexval(*p); // + takes precedence over <<
564  out.push_back(c);
565  }
566 }
567 
568 
574 void ascii2hex(const string& in, string& out)
575 {
576  std::stringstream sstream;
577  for ( string::const_iterator item = in.begin(); item != in.end(); item++){
578  sstream << std::hex << int(*item);
579  }
580  out=sstream.str();
581 }
582 
583 
590 void ascii2hexWithSize(const string& in, string& out, size_t bytesize)
591 {
592  std::stringstream sstream;
593  for ( int i=0; i<bytesize; i++){
594  sstream << std::hex << int(in[i]);
595  }
596  out=sstream.str();
597 }
598 
599 
606 bool isCornerPresent(string str, string corner)
607 {
608  int n = str.length();
609  int cl = corner.length();
610 
611  // If length of corner string is more, it
612  // cannot be present at corners.
613  if (n < cl)
614  return false;
615 
616  // Return true if corner string is present at
617  // both corners of given string.
618  return (str.substr(0, cl).compare(corner) == 0 &&
619  str.substr(n-cl, cl).compare(corner) == 0);
620 }
621 
622 
629 template<typename T>
630 void string2hexnumerics(const string& in, char * out, size_t byteSize)
631 {
632  for (unsigned int i = 0; i < byteSize; i++)
633  {
634  std::sprintf(out+i, "%d", (T)in[i]);
635  }
636 }
637 
638 void stringHex2Unsigned(const string& in, unsigned int * out, size_t byteSize)
639 {
640  for (unsigned int i = 0; i < byteSize; i++)
641  {
642  std::sprintf((char*)out+i, "%u", (unsigned int)in[i]);
643  }
644 }
645 
652 void string2hexnumericsString(const string& in, string &out, size_t byteSize)
653 {
654  char tmp_out [byteSize];
655  for (unsigned int i = 0; i < byteSize; i++)
656  {
657  std::sprintf(tmp_out+i, "%d", (int)in[i]);
658  }
659  out.append(tmp_out);
660 }
661 
662 
670 template<unsigned int bytes_per_line = 8>
671 string createMemTestCommands(unsigned long long int mem_address, unsigned int testingNumber, unsigned int burst_size)
672 {
673  string out;
674  char start_cmd [bytes_per_line];
675  char stop_cmd [bytes_per_line];
676  char value_cmd[bytes_per_line];
677  char burst_cmd[bytes_per_line];
678  //WARNING: currently hardcoded way of start and stop commands with a 1 and 2 for start and stop respectively
679  for (unsigned int k = 0; k < bytes_per_line; k++) {
680  value_cmd[k] = (char)0;
681  if (k != 0) {
682  stop_cmd[k] = (char)0;
683  start_cmd[k] = (char)0;
684  burst_cmd[k] = (char)0;
685  }
686  else {
687  start_cmd[k] = (char)1;
688  stop_cmd[k] = (char)2;
689  burst_cmd[k] = (char)4;
690  }
691  }
692  memcpy(start_cmd+1, (char*)&testingNumber, 2);
693  out = out.append(start_cmd,3);
694  memcpy(value_cmd, (char*)&mem_address, 5);
695  out = out.append(value_cmd,5);
696  memcpy(burst_cmd+1,(char*)&burst_size,2);
697  out = out.append(burst_cmd,8);
698  return string(out);
699  }
700 
701 
709 template<unsigned int bytes_per_line = 8>
710 string createMemTestGoldenOutput(unsigned long long int mem_address, unsigned int testingNumber, bool with_bw_analysis)
711 {
712  char addr_cmd [bytes_per_line]; // Half of the command filled with start other half with the address
713  char fault_cntr_cmd [bytes_per_line];
714  char fault_addr_cmd [bytes_per_line];
715  char filler_cmd [bytes_per_line];
716  char clock_cycles_cmd [bytes_per_line];
717  char end_of_tests_cmd [bytes_per_line];
718  char stop_cmd [bytes_per_line];
719  unsigned int fault_addr = 0;
720  unsigned int clock_cycles = 0;
721  const unsigned int first_faultTests = 3;
722  string out;
723 
724 //given parameters
725  unsigned int mem_word_size = 512;
726  unsigned int mem_size_of_word_size = 20;
727 // computations the first faulty address and the the fault counter
728  unsigned int mem_addr_per_word = mem_word_size / 8; // byte size of this word
729  unsigned int fault_cntr = 0;
730 //precomputing the cc
731  clock_cycles = mem_address % mem_addr_per_word == 0 ? mem_address / mem_addr_per_word : (unsigned int) mem_address / mem_addr_per_word + 1;
732 
733 
734 //simulating the fault injection
735  for (unsigned int j = 1; j < mem_address/64; j++)
736  {
737  ap_uint<512> currentNumber = j;
738  ap_uint<512> nextNumber = (currentNumber+1) xor 1;
739  ap_uint<512> tmpNumber = nextNumber;
740  tmpNumber = nextNumber & 0;
741  if( nextNumber != (tmpNumber)){
742  fault_cntr+=1;
743  }
744  }
745  // for (unsigned int j = 0; j < mem_address; j+=mem_addr_per_word)
746  // {
747  // ap_uint<32> currentNumber = j;
748  // ap_uint<32> nextNumber = (currentNumber+1) xor 1;
749  // ap_uint<32> prevNumber = currentNumber;
750  // ap_uint<32> tmpNumber = nextNumber;
751  // ap_uint<32> mask = 0;
752 
753  // for (unsigned int i = 0; i < mem_word_size/32 && j > 0; i++){
754  // tmpNumber = nextNumber & 0;
755 
756  // if( nextNumber != (tmpNumber)){
757  // fault_cntr+=1;
758  // }
759  // prevNumber = currentNumber;
760  // currentNumber = nextNumber;
761  // nextNumber = (nextNumber + 1 ) xor i;
762  // }
763  // }
764 
765 
766 //init the commands and fill em out of the fault simulation before
767  for(unsigned int i = 0; i < testingNumber; i++){
768  for (unsigned int k = 0; k < bytes_per_line; k++) {
769  addr_cmd[k] = (char)0;
770  filler_cmd[k] = (char)0;
771  fault_cntr_cmd[k] = (char)0;
772  fault_addr_cmd[k] = (char)0;
773  stop_cmd[k] = (char)0;
774  stop_cmd[k] = (char)0;
775  end_of_tests_cmd[k] = (char)0;
776  clock_cycles_cmd[k] = (char)0;
777  }
778  stop_cmd[0] = (char)2;
779  end_of_tests_cmd[0] = (char)3;
780  memcpy(end_of_tests_cmd+1, (char*)&testingNumber, 2);
781  memcpy(addr_cmd, (char*)&mem_address, sizeof(unsigned long long int));
782  out = out.append(addr_cmd,bytes_per_line);
783  //if not yet in the fault injection point just let em empty as expected from good tests
784  if(i < first_faultTests-1 || mem_address <= mem_addr_per_word)
785  {
786  out = out.append(filler_cmd,bytes_per_line);
787  out = out.append(filler_cmd,bytes_per_line);
788  }else{
789  memcpy(fault_cntr_cmd, (char*)&fault_cntr, sizeof(unsigned int));
790  out = out.append(fault_cntr_cmd,bytes_per_line);
791  memcpy(fault_addr_cmd, (char*)&mem_addr_per_word, sizeof(unsigned int));
792  out = out.append(fault_addr_cmd,bytes_per_line);
793  }
794  // memcpy(clock_cycles_cmd, (char*)&clock_cycles, sizeof(unsigned int));
795  // memcpy(clock_cycles_cmd+sizeof(unsigned int), (char*)&clock_cycles, sizeof(unsigned int));
796  // out = out.append(clock_cycles_cmd,bytes_per_line);
798  const unsigned int dummycc=1;
799  memcpy(clock_cycles_cmd,(char*)& dummycc, sizeof(unsigned int));
800  out = out.append(clock_cycles_cmd,bytes_per_line);//read
801  out = out.append(filler_cmd,bytes_per_line);//write
802  }
803  out = out.append(end_of_tests_cmd,bytes_per_line);
804  // out.append(stop_cmd,bytes_per_line);
805  return string(out);
806 }
807 
808 
815 std::string createUppercaseGoldenOutput(std::string input_string){
816  std::string strGold;
817  strGold = input_string;
818  uppercase_conversion:
819  for (unsigned int i = 0; i < strGold.length(); i++ ) {
820  if (strGold[i] >= 'a' && strGold[i] <= 'z'){
821  strGold[i] = strGold[i] - ('a' - 'A');
822  }
823  }
824  return strGold;
825 }
826 
827 
828 
829 
835 void reverseStr(string& str)
836 {
837  int n = str.length();
838 
839  // Swap character starting from two
840  // corners
841  for (int i = 0; i < n / 2; i++)
842  swap(str[i], str[n - i - 1]);
843 }
844 
845 
846 
854 template<unsigned int bytes_per_line=8>
855 std::vector<MemoryTestResult> parseMemoryTestOutput(const string longbuf, size_t charOutputSize, int rawdatalines)
856 {
857  std::vector<MemoryTestResult> testResults_vector;
858 
859  int rawiterations = charOutputSize / 8; //should be equivalent to rawdatalines
860  unsigned int mem_word_size = 512;
861  unsigned int mem_word_byte_size = mem_word_size/8;
862  bool is_stop_present = rawdatalines % (3+1+1) == 0; //guard to check if multiple data of 3 64bytes or with
863 
864  int k = 1;
865  char myTmpOutBuff [bytes_per_line];
866  for (int i = 0; i < bytes_per_line; ++i)
867  {
868  myTmpOutBuff[i]=(char)0;
869  }
870  unsigned int testingNumber_out=0, fault_cntr_out=0, fault_addr_out=0;
871  unsigned long long int max_memory_addr_out=0, clock_cycles_read=0, clock_cycles_write=0;
872  for (int i = 1; i < rawdatalines+1; i++)
873  {
874  string tmp_outbuff;
875  tmp_outbuff= longbuf.substr((i-1)*bytes_per_line,bytes_per_line);
876  if(is_stop_present && k==7){
877  cout << "DEBUG the stop is present and is here" << endl;
878  } else if( ( (i == rawdatalines-1) || (i == rawdatalines) ) && k==6){ //check it is either the last or one before the last
879  //substr extraction and parsing
880  //strncpy(myTmpOutBuff,tmp_outbuff.c_str()+1,bytes_per_line-1);
881  //testingNumber_out = *reinterpret_cast<unsigned long long*>(myTmpOutBuff);
882  memcpy(&testingNumber_out,tmp_outbuff.c_str()+1,bytes_per_line/2);
883 
884  #if DEBUG_LEVEL == TRACE_ALL
885  cout << "DEBUG last command with the iterations " << testingNumber_out << endl;
886  #endif
887  }else if(k==5){
888  //strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line);
889  //clock_cycles_read = *reinterpret_cast<unsigned long long int*>(myTmpOutBuff);
890  memcpy(&clock_cycles_read,tmp_outbuff.c_str(),bytes_per_line);
891 
892  #if DEBUG_LEVEL == TRACE_ALL
893  cout << "DEBUG clock_cycles_read (or the fourth half data pckt) " << clock_cycles_read << endl;
894  cout << "DEBUG clock_cycles_write (or the fourth half data pckt) " << clock_cycles_write << endl;
895  #endif
896  MemoryTestResult tmpResult(max_memory_addr_out,fault_cntr_out,fault_addr_out,clock_cycles_write,clock_cycles_read);
897  testResults_vector.push_back(tmpResult);
898  if(!( (i+1 == rawdatalines-1) || (i+1 == rawdatalines) )){
899  k=0;
900  #if DEBUG_LEVEL == TRACE_ALL
901  cout << "DEBUG reinit the counter" << endl;
902  #endif
903  }
904  unsigned int written_words = max_memory_addr_out%mem_word_byte_size == 0 ? max_memory_addr_out/mem_word_byte_size : max_memory_addr_out/mem_word_byte_size + 1;
905  double rd_bndwdth = ( (double)written_words*(double)mem_word_size / ( (double)tmpResult.clock_cycles_read * ( 6.4 ) ) ); // Gbit/T
906  double wr_bndwdth = ( (double)written_words*(double)mem_word_size / ( (double)tmpResult.clock_cycles_write * ( 6.4 ) ) );
907  #if DEBUG_LEVEL == TRACE_ALL
908  cout << "Written " << written_words << " words" << endl;
909  cout << "DEBUG overall test results: target address " << tmpResult.target_address << " ";
910  cout << "Fault counter: " << tmpResult.fault_cntr << " ";
911  cout << "First fault at address: " << tmpResult.first_fault_address << " " << endl;
912  cout << " RD BW " << rd_bndwdth << "[GBit/s] with cc equal to " << tmpResult.clock_cycles_read << " " << endl;
913  cout << " WR BW " << wr_bndwdth << "[GBit/s] with cc equal to " << tmpResult.clock_cycles_write << " " << endl;
914  #endif
915  } else if(k==4){ //clock cycless
916  //char mySecondTmpOutBuff[bytes_per_line/2];
917  //string additional_string;
918  //init the buffer
919  //for(int i=0;i<bytes_per_line;i++){myTmpOutBuff[i]=(char)0;mySecondTmpOutBuff[i%(bytes_per_line/2)]=(char)0;}
920  // additional_string=tmp_outbuff.substr(bytes_per_line/2,bytes_per_line/2);
921  //
922  // tmp_outbuff = tmp_outbuff.erase(bytes_per_line/2,bytes_per_line/2);
923  // strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line/2);
924  // clock_cycles_read = *reinterpret_cast<unsigned int*>(myTmpOutBuff);
925  //
926  // strncpy(mySecondTmpOutBuff,additional_string.c_str(),bytes_per_line/2);
927  // clock_cycles_write = *reinterpret_cast<unsigned int*>(mySecondTmpOutBuff);
928  //strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line);
929  //clock_cycles_write = *reinterpret_cast<unsigned long long int*>(myTmpOutBuff);
930  memcpy(&clock_cycles_write,tmp_outbuff.c_str(),bytes_per_line);
931 
932  }else if(k==3){ // first fault address
933  //substr extraction and parsing
934  //strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line);
935  //fault_addr_out = *reinterpret_cast<unsigned long long int *>(myTmpOutBuff);
936  memcpy(&fault_addr_out,tmp_outbuff.c_str(),bytes_per_line/2);
937 
938  #if DEBUG_LEVEL == TRACE_ALL
939  cout << "DEBUG first fault address (or the third data pckt) " << fault_addr_out << endl;
940  #endif
941  }else if(k==2){ // fault cntr
942  //strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line);
943  //fault_cntr_out = *reinterpret_cast<unsigned long long int *>(myTmpOutBuff);
944  memcpy(&fault_cntr_out,tmp_outbuff.c_str(),bytes_per_line/2);
945 
946  #if DEBUG_LEVEL == TRACE_ALL
947  cout << "DEBUG the fault counters (or the second data pack) " << fault_cntr_out << endl;
948  #endif
949  }else { //max addrss
950  //substr extraction and parsing
951  // strncpy(myTmpOutBuff,tmp_outbuff.c_str(),bytes_per_line);
952  // max_memory_addr_out = *reinterpret_cast<unsigned long long *>(myTmpOutBuff);
953  memcpy(&max_memory_addr_out,tmp_outbuff.c_str(),bytes_per_line);
954  #if DEBUG_LEVEL == TRACE_ALL
955  cout << "DEBUG max address (or the first data pack) " << max_memory_addr_out << endl;
956  #endif
957  }
958  k++;
959  tmp_outbuff.clear();
960  }
961  return testResults_vector;
962 }
963 
964 
971 void printStringHex(const string inStr, size_t strSize){
972  #if DEBUG_LEVEL == TRACE_ALL
973  printf("Going to print a hex string :D\n");
974  #endif
975  for (size_t i = 0; i < strSize; i++)
976  {
977  printf("%x",inStr[i]);
978  }
979  printf("\n");
980 
981 }
982 
983 
990 void printCharBuffHex(const char * inStr, size_t strSize){
991  #if DEBUG_LEVEL == TRACE_ALL
992  printf("Going to prit a hex char buff :D\n");
993  #endif
994  for (size_t i = 0; i < strSize; i++)
995  {
996  printf("%x",inStr[i]);
997  }
998  printf("\n");
999 
1000 }
1001 
1002 
1003 
1010 void printBits(size_t const size, void const * const ptr)
1011 {
1012  unsigned char *b = (unsigned char*) ptr;
1013  unsigned char byte;
1014  int i, j;
1015 
1016  for (i = size-1; i >= 0; i--) {
1017  for (j = 7; j >= 0; j--) {
1018  byte = (b[i] >> j) & 1;
1019  printf("%u", byte);
1020  }
1021  }
1022  puts("");
1023 }
1024 
1025 static inline ssize_t
1026 __file_size(const char *fname)
1027 {
1028  int rc;
1029  struct stat s;
1030 
1031  rc = lstat(fname, &s);
1032  if (rc != 0) {
1033  fprintf(stderr, "err: Cannot find %s!\n", fname);
1034  return rc;
1035  }
1036  return s.st_size;
1037 }
1038 
1039 static inline ssize_t
1040 __file_read(const char *fname, char *buff, size_t len)
1041 {
1042  int rc;
1043  FILE *fp;
1044 
1045  if ((fname == NULL) || (buff == NULL) || (len == 0))
1046  return -EINVAL;
1047 
1048  fp = fopen(fname, "r");
1049  if (!fp) {
1050  fprintf(stderr, "err: Cannot open file %s: %s\n",
1051  fname, strerror(errno));
1052  return -ENODEV;
1053  }
1054  rc = fread(buff, len, 1, fp);
1055  if (rc == -1) {
1056  fprintf(stderr, "err: Cannot read from %s: %s\n",
1057  fname, strerror(errno));
1058  fclose(fp);
1059  return -EIO;
1060  }
1061  fclose(fp);
1062  return rc;
1063 }
1064 
1065 static inline ssize_t
1066 __file_write(const char *fname, const char *buff, size_t len)
1067 {
1068  int rc;
1069  FILE *fp;
1070 
1071  if ((fname == NULL) || (buff == NULL) || (len == 0))
1072  return -EINVAL;
1073 
1074  fp = fopen(fname, "w+");
1075  if (!fp) {
1076  fprintf(stderr, "err: Cannot open file %s: %s\n",
1077  fname, strerror(errno));
1078  return -ENODEV;
1079  }
1080  rc = fwrite(buff, len, 1, fp);
1081  if (rc == -1) {
1082  fprintf(stderr, "err: Cannot write to %s: %s\n",
1083  fname, strerror(errno));
1084  fclose(fp);
1085  return -EIO;
1086  }
1087  fclose(fp);
1088  return rc;
1089 }
1090 
int simCnt
Definition: tb_fmc.cpp:113
void ascii2hex(const string &in, string &out)
Convert a ascii string to a hexadecimal string FIXME:
Definition: common.cpp:574
void reverseStr(string &str)
reverse a given string
Definition: common.cpp:835
bool dumpStringToFile(const string s, const string outFileName, int simCnt)
Fill an output file with data from an image.
Definition: common.cpp:222
ap_uint< 64 > pack_ap_uint_64_(ap_uint< 8 > *buffer)
Pack an array of 8 x ap_uint<8> into a ap_uint<64> word.
Definition: common.cpp:116
void string2hexnumericsString(const string &in, string &out, size_t byteSize)
Convert a hex string to a integer into a char buffer with the SAME dimensions FIXME:
Definition: common.cpp:652
std::vector< MemoryTestResult > parseMemoryTestOutput(const string longbuf, size_t charOutputSize, int rawdatalines)
Parse the memory test output contained in astring with a given size.
Definition: common.cpp:855
bool dumpStringToFileWithLastSetEveryGnoPackets(string s, const string outFileName, int simCnt, int gno)
Fill an output file with data from a string and set the tlast every gno packets.
Definition: common.cpp:354
bool isCornerPresent(string str, string corner)
Check the presence of a given corner value at the begin and the end of a string.
Definition: common.cpp:606
void string2hexnumerics(const string &in, char *out, size_t byteSize)
Convert a hex string to a integer into a char buffer with the SAME dimensions FIXME:
Definition: common.cpp:630
string createMemTestGoldenOutput(unsigned long long int mem_address, unsigned int testingNumber, bool with_bw_analysis)
Create the expected output results for the memory test (with FAULT INJECTION)
Definition: common.cpp:710
string dumpFileToStringRawDataString(const string inpFileName, int *rawdatalines, size_t outputSize)
Initialize an input data stream from a file with only data.
Definition: common.cpp:481
bool dumpFileToString(const string inpFileName, char *charOutput, int simCnt)
Initialize an input data stream from a file.
Definition: common.cpp:424
void stringHex2Unsigned(const string &in, unsigned int *out, size_t byteSize)
Definition: common.cpp:638
void unpack_ap_uint_64_(ap_uint< 64 > value, ap_uint< 8 > *buffer)
Unpack an ap_uint<64> word to an array of 8 x ap_uint<8>.
Definition: common.cpp:139
void ascii2hexWithSize(const string &in, string &out, size_t bytesize)
Convert a ascii string to a hexadecimal string FIXME:
Definition: common.cpp:590
string createMemTestCommands(unsigned long long int mem_address, unsigned int testingNumber, unsigned int burst_size)
Create the commands for a memory test with start/max address to test-nop to execute-stop.
Definition: common.cpp:671
bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream)
Dump a data word to a file.
Definition: common.cpp:153
std::string createUppercaseGoldenOutput(std::string input_string)
Create the expected output results for the uppercase.
Definition: common.cpp:815
bool setInputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string inpFileName, int simCnt)
Initialize an input data stream from a file.
Definition: common.cpp:45
void printStringHex(const string inStr, size_t strSize)
print byte-per-byte a given string in hexadecimal format
Definition: common.cpp:971
void hex2ascii(const string &in, string &out)
Convert a hexadecimal string to a ascii string FIXME:
Definition: common.cpp:554
bool dumpStringToFileOnlyRawData(const string s, const string outFileName, int simCnt, size_t out_size)
Fill an output file with data from an image.
Definition: common.cpp:292
bool getOutputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string outFileName, int simCnt)
Fill an output file with data from an output stream.
Definition: common.cpp:176
unsigned char hexval(unsigned char c)
convert a char to its hexadecimal representation.
Definition: common.hpp:234
unsigned long long int clock_cycles_read
Definition: common.hpp:418
void printBits(size_t const size, void const *const ptr)
print the binary representation of a target pointer buffer of a given size. Assumes little endian.
Definition: common.hpp:98
unsigned int first_fault_address
Definition: common.hpp:417
void printCharBuffHex(const char *inStr, size_t strSize)
print byte-per-byte a given char buff in hexadecimal format
Definition: common.hpp:382
unsigned long long int clock_cycles_write
Definition: common.hpp:419
unsigned int fault_cntr
Definition: common.hpp:416
#define PACK_SIZE
Definition: config.h:51
unsigned long long int target_address
Definition: common.hpp:415
bool readDataStream(stream< UdpAppData > &sDataStream, UdpAppData *udpWord)
Read data from a stream.
Definition: tb_nal.cpp:395
#define VALID
Definition: tb_nal.cpp:43
#define OK
Definition: nts_types.hpp:57
#define KO
Definition: nts_types.hpp:58
out
Definition: test.py:12
ap_uint< 64 > tdata
ap_uint< 1 > tlast
ap_uint< 8 > tkeep
ap_uint< 32 > size