cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Observer
Observer
442 Views
Registered: ‎07-05-2019

HLS stream coding questions

Jump to solution

Hi,

i do a simple HLS module and a test bench for it. It is very simple but the test result  seems wrong. It is my first time using the HLS and I have no knowledge  of  C/C++ language. So can anyone help to see what is the problem?

///detect_top.cpp file ///////

#include "detect_h.h"
void detect_top(
  stream<uint16_md> &adcIStream,   // [in]
  stream<uint16_md> &adcQStream,   // [in]
  stream<uint16_md> &MagStream,   // [out]
  short detect_threshold,     // [in]
  bool pass_flag       // [out]
)
{
#pragma HLS INTERFACE axis reverse port=adcIStream
#pragma HLS INTERFACE axis reverse port=adcQStream
#pragma HLS INTERFACE axis forward port=MagStream
 uint16_md inI;
 uint16_md inQ;
 uint16_md outMag;
 // read inputs data
 adcIStream >> inI;
 adcQStream >> inQ;
 outMag = inI*inI + inQ*inQ;
 if(outMag > detect_threshold)
  pass_flag = 1;
 else
  pass_flag = 0;
 MagStream << outMag;
}

////test bench file ////

#include "detect_h.h"
void detect_top(
   stream<uint16_md> &adcIStream,   // [in]
   stream<uint16_md> &adcQStream,   // [in]
   stream<uint16_md> &MagStream,   // [out]
   short detect_threshold,     // [in]
   bool pass_flag     // [out]
   );
int main()
{
 stream<uint16_md> istream;
 stream<uint16_md> qstream;
 stream<uint16_md> ostream;
 uint16_md istream_in;
 uint16_md qstream_in;
 uint16_md ostream_out;
 uint16_md hd_mag;
  unsigned int Ai;
  unsigned int Aq;
  short  B_threshold;
  unsigned int mag_out;
  bool flag_out;
  unsigned int mag_sf;
  bool flag_sf;
  //put data into hardware function
  istream_in << 0x3;
  qstream_in << 0x4;
  istream << istream_in;
  qstream << qstream_in;
   printf("hello my test\n");
   //Put data into A
   Ai = 3;
   Aq = 4;
   B_threshold = 10;
   //Call the hardware function
    detect_top(istream,qstream,ostream,B_threshold,flag_out);
    ostream >> hd_mag;
   //Run a software version of the hardware function to validate results
   mag_sf = Ai*Ai + Aq*Aq;
   if(mag_sf > B_threshold)
    flag_sf = true;
   else
    flag_sf = false;
   //Compare results
   if(flag_sf != flag_out)
    printf("ERROR HW and SW results mismatch\n");
   else
    printf("Success HW and SW results match\n");
   printf("mag_sf = %d\n", mag_sf);
   printf("flag_sf = %d\n", flag_sf);
   printf("flag_out = %d\n", flag_out);
   printf("hd_mag = %d\n", hd_mag);
  return 0;
}

///detect_h.h file///

#include <ap_int.h>
#include <hls_stream.h>

using namespace hls;

typedef ap_uint<16> uint16_md; // 16-bit user defined type

 ///C simulation result ////

INFO: [SIM 2] *************** CSIM start ***************
INFO: [SIM 4] CSIM will launch GCC as the compiler.
   Compiling ../../../detect_tb.cpp in debug mode
   Compiling ../../../detect_top.cpp in debug mode
   Generating csim.exe
hello my test
ERROR HW and SW results mismatch
mag_sf = 25
flag_sf = 1
flag_out = 0
hd_mag = 7404576
INFO: [SIM 1] CSim done with 0 errors.
INFO: [SIM 3] *************** CSIM finish ***************

0 Kudos
Reply
1 Solution

Accepted Solutions
Moderator
Moderator
323 Views
Registered: ‎05-27-2018

Hi @sungh ,

The '>>' operator is overloaded to allow use similar to the stream extraction operator for C++ stream (for example, iostreams and filestreams). The hls::stream is supplied as the LHS argument and the destination variable the RHS.

// Usage of void operator >> (T & rdata)
hls::stream<int> my_stream;
int dst_var;
my_stream >> dst_var;

But we cannot assign the value ap_uint using the '>>' operator. Please use the '=' operator instead.

  istream_in << 0x3;
  qstream_in << 0x4;
#include "header.h"
void detect_top(
  stream<uint16_md> &adcIStream,   // [in]
  stream<uint16_md> &adcQStream,   // [in]
  stream<uint16_md> &MagStream,   // [out]
  short detect_threshold,     // [in]
  bool *pass_flag       // [out]
)
{
#pragma HLS INTERFACE s_axilite port=detect_threshold
#pragma HLS INTERFACE s_axilite port=pass_flag
#pragma HLS INTERFACE axis reverse port=adcIStream
#pragma HLS INTERFACE axis reverse port=adcQStream
#pragma HLS INTERFACE axis forward port=MagStream
 uint16_md inI = 0;
 uint16_md inQ = 0;
 uint16_md outMag = 0;
 // read inputs data
 adcIStream >> inI;
 adcQStream >> inQ;
 outMag = inI*inI + inQ*inQ;
 #ifndef  __SYNTHESIS__
     		std::cout<<"kernel outMag\t"<<std::dec<< outMag<<std::endl;
 #endif
 if(outMag > detect_threshold)
  *pass_flag = 1;
 else
  *pass_flag = 0;
 MagStream << outMag;
}
#include "header.h"
void detect_top(
   stream<uint16_md> &adcIStream,   // [in]
   stream<uint16_md> &adcQStream,   // [in]
   stream<uint16_md> &MagStream,   // [out]
   short detect_threshold,     // [in]
   bool *pass_flag     // [out]
   );
int main()
{
 stream<uint16_md> istream;
 stream<uint16_md> qstream;
 stream<uint16_md> ostream;
 uint16_md istream_in;
 uint16_md qstream_in;
 uint16_md ostream_out;
 uint16_md hd_mag;
  unsigned int Ai;
  unsigned int Aq;
  short  B_threshold;
  unsigned int mag_out;
  bool flag_out;
  unsigned int mag_sf;
  bool flag_sf;
  //put data into hardware function
  istream_in = 0x3;
  qstream_in = 0x4;
  istream << istream_in;
  qstream << qstream_in;
   printf("hello my test\n");
   //Put data into A
   Ai = 3;
   Aq = 4;
   B_threshold = 10;
   //Call the hardware function
    detect_top(istream,qstream,ostream,B_threshold, &flag_out);
    ostream >> hd_mag;
    std::cout<<"hd_mag\t"<<std::hex<< hd_mag.range(15,0)<<std::endl;
   //Run a software version of the hardware function to validate results
   mag_sf = Ai*Ai + Aq*Aq;
   if(mag_sf > B_threshold)
    flag_sf = true;
   else
    flag_sf = false;
   //Compare results
   if(flag_sf != flag_out)
    printf("ERROR HW and SW results mismatch\n");
   else
    printf("Success HW and SW results match\n");
   printf("mag_sf = %d\n", mag_sf);
   printf("flag_sf = %d\n", flag_sf);
   printf("flag_out = %d\n", flag_out);
   std::cout<<"hd_mag\t"<<std::dec<< hd_mag.range(15,0)<<std::endl;
  return 0;
}
INFO: [SIM 2] *************** CSIM start ***************
INFO: [SIM 4] CSIM will launch GCC as the compiler.
   Compiling ../../../test.cpp in debug mode
   Generating csim.exe
hello my test
kernel outMag	25
hd_mag	0x19
Success HW and SW results match
mag_sf = 25
flag_sf = 1
flag_out = 1
hd_mag	25
INFO: [SIM 1] CSim done with 0 errors.
INFO: [SIM 3] *************** CSIM finish ***************

Thanks,

Wen

 

-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
如果提供的信息能解决您的问题,请标记为“接受为解决方案”。
如果您认为帖子有帮助,请点击“奖励”。谢谢!
-------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
Reply
2 Replies
Moderator
Moderator
324 Views
Registered: ‎05-27-2018

Hi @sungh ,

The '>>' operator is overloaded to allow use similar to the stream extraction operator for C++ stream (for example, iostreams and filestreams). The hls::stream is supplied as the LHS argument and the destination variable the RHS.

// Usage of void operator >> (T & rdata)
hls::stream<int> my_stream;
int dst_var;
my_stream >> dst_var;

But we cannot assign the value ap_uint using the '>>' operator. Please use the '=' operator instead.

  istream_in << 0x3;
  qstream_in << 0x4;
#include "header.h"
void detect_top(
  stream<uint16_md> &adcIStream,   // [in]
  stream<uint16_md> &adcQStream,   // [in]
  stream<uint16_md> &MagStream,   // [out]
  short detect_threshold,     // [in]
  bool *pass_flag       // [out]
)
{
#pragma HLS INTERFACE s_axilite port=detect_threshold
#pragma HLS INTERFACE s_axilite port=pass_flag
#pragma HLS INTERFACE axis reverse port=adcIStream
#pragma HLS INTERFACE axis reverse port=adcQStream
#pragma HLS INTERFACE axis forward port=MagStream
 uint16_md inI = 0;
 uint16_md inQ = 0;
 uint16_md outMag = 0;
 // read inputs data
 adcIStream >> inI;
 adcQStream >> inQ;
 outMag = inI*inI + inQ*inQ;
 #ifndef  __SYNTHESIS__
     		std::cout<<"kernel outMag\t"<<std::dec<< outMag<<std::endl;
 #endif
 if(outMag > detect_threshold)
  *pass_flag = 1;
 else
  *pass_flag = 0;
 MagStream << outMag;
}
#include "header.h"
void detect_top(
   stream<uint16_md> &adcIStream,   // [in]
   stream<uint16_md> &adcQStream,   // [in]
   stream<uint16_md> &MagStream,   // [out]
   short detect_threshold,     // [in]
   bool *pass_flag     // [out]
   );
int main()
{
 stream<uint16_md> istream;
 stream<uint16_md> qstream;
 stream<uint16_md> ostream;
 uint16_md istream_in;
 uint16_md qstream_in;
 uint16_md ostream_out;
 uint16_md hd_mag;
  unsigned int Ai;
  unsigned int Aq;
  short  B_threshold;
  unsigned int mag_out;
  bool flag_out;
  unsigned int mag_sf;
  bool flag_sf;
  //put data into hardware function
  istream_in = 0x3;
  qstream_in = 0x4;
  istream << istream_in;
  qstream << qstream_in;
   printf("hello my test\n");
   //Put data into A
   Ai = 3;
   Aq = 4;
   B_threshold = 10;
   //Call the hardware function
    detect_top(istream,qstream,ostream,B_threshold, &flag_out);
    ostream >> hd_mag;
    std::cout<<"hd_mag\t"<<std::hex<< hd_mag.range(15,0)<<std::endl;
   //Run a software version of the hardware function to validate results
   mag_sf = Ai*Ai + Aq*Aq;
   if(mag_sf > B_threshold)
    flag_sf = true;
   else
    flag_sf = false;
   //Compare results
   if(flag_sf != flag_out)
    printf("ERROR HW and SW results mismatch\n");
   else
    printf("Success HW and SW results match\n");
   printf("mag_sf = %d\n", mag_sf);
   printf("flag_sf = %d\n", flag_sf);
   printf("flag_out = %d\n", flag_out);
   std::cout<<"hd_mag\t"<<std::dec<< hd_mag.range(15,0)<<std::endl;
  return 0;
}
INFO: [SIM 2] *************** CSIM start ***************
INFO: [SIM 4] CSIM will launch GCC as the compiler.
   Compiling ../../../test.cpp in debug mode
   Generating csim.exe
hello my test
kernel outMag	25
hd_mag	0x19
Success HW and SW results match
mag_sf = 25
flag_sf = 1
flag_out = 1
hd_mag	25
INFO: [SIM 1] CSim done with 0 errors.
INFO: [SIM 3] *************** CSIM finish ***************

Thanks,

Wen

 

-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
如果提供的信息能解决您的问题,请标记为“接受为解决方案”。
如果您认为帖子有帮助,请点击“奖励”。谢谢!
-------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
Reply
Observer
Observer
282 Views
Registered: ‎07-05-2019

Thanks for your detail reply. I will have a try.

Best regards,

0 Kudos
Reply