Jump to content

sc_signal<vector<T> >


gurunath.kadam

Recommended Posts

Hi all,

I am creating some sc_signal<vector<T> > ports with a user defined T. I have already found a proper link for the same (answer provided by AR/Ravi, inheriting from STL vector).

http://www.accellera...g=msg00004.html

But now my question is, I am defining other ports where T will be sc_bv, double etc. So shall I write above code for each of the variable type or is it ok with all? (I am particularly worried about << and ==).

Thank you.

P.S. Will next IEEE1666 talk about sc_vector? Will some provision for above scenario provided in later versions?

Link to comment
Share on other sites

I am creating some sc_signal<vector<T> > ports with a user defined T. I have already found a proper link for the same (answer provided by AR/Ravi, inheriting from STL vector).

But now my question is, I am defining other ports where T will be sc_bv, double etc. So shall I write above code for each of the variable type or is it ok with all? (I am particularly worried about << and ==).

The "NewVector" class proposed by AR/Ravi in the linked post is a class template already. Therefore, you should be able to define the missing functions (==,<<,sc_trace) for all datatypes T based on T's operations elementwise.

P.S. Will next IEEE1666 talk about sc_vector? Will some provision for above scenario provided in later versions?

sc_vector<T> is meant for vectors of SystemC object (moduls, signals, ports). The element type of a signal is not covered here. I'm not aware of any proposal to add a "signal-aware" element vector class to SystemC.

Link to comment
Share on other sites

I am writing the new vector for type of struct. I am getting some errors. I will post both codes and errors here.

cdma_data.h

/*
* cdma_data.h
*
*  Created on: Sep 12, 2012
*	  Author: gurunath
*/
#ifndef CDMA_DATA_H_
#define CDMA_DATA_H_
#include "noc_package.h"
struct cdma_data{
int data[MOD_SIZE];
inline bool operator ==(const cdma_data & cd) const {
 unsigned int check_int;
 check_int = 1;
 for (unsigned int i = 0; i < MOD_SIZE; i++){
  if(cd.data[i] == data[i])
   check_int++;
 }
 if (check_int == MOD_SIZE)
  return 1;
 else
  return 0;
};

};
inline void sc_trace(sc_trace_file * &tf, const cdma_data & cd, string & name)
{
for (unsigned int i = 0; i < MOD_SIZE; i++){
 sc_trace(tf, cd.data[i], name + ".data");
}
}
inline ostream & operator <<(ostream & os, const cdma_data & cd)
{
os << "[ ";
for (unsigned int i = 0; i < MOD_SIZE; i++){
 os << cd.data[i] << ", ";
}
os << "]";
return os;
}


#endif /* CDMA_DATA_H_ */

cdma_vec.h

/*
* cdma_vec.h
*
*  Created on: Oct 30, 2012
*	  Author: gurunath
*/
#ifndef CDMA_VEC_H_
#define CDMA_VEC_H_
#include "cdma_data.h"
#include <vector>
#include <iostream>
using namespace std;
template <class X> class CDMAVec : public vector <X>
{
 public:
  CDMAVec(void) : vector <X> ()
    {
    }
inline friend void sc_trace(sc_trace_file * &tf, const CDMAVec & cv<X>, const string & name)
{
 unsigned int j = 0;
 for (size_t i = 0; i < cv.size(); ++i){
  cv[i] -> sc_trace(tf, *cv[i], name + j);
  j++;
 }
}
};
#endif /* CDMA_VEC_H_ */

Errors:

../src/cdma_vec.h:24:69: error: expected ‘,’ or ‘...’ before ‘<’ token
../src/cdma_vec.h: In function ‘void sc_trace(sc_core::sc_trace_file*&, const CDMAVec<X>&)’:
../src/cdma_vec.h:28:34: error: ‘name’ was not declared in this scope

There are other errors while using

sc_in<CDMAVec> data_in;

but as of now i am concentrating on this part.

Any suggestions.

Thank you.

Link to comment
Share on other sites

Some general remarks on your code:

 unsigned int check_int;
 check_int = 1;
 for (unsigned int i = 0; i < MOD_SIZE; i++){
  if(cd.data[i] == data[i])
check_int++;
 }
 if (check_int == MOD_SIZE)
  return 1;
 else
  return 0;

This is a quite complicated way to compare two arrays. A shorter way, following the same idea, would be

 unsigned int check = 0;
 for( ; check<MOD_SIZE && data[check] == cd.data[check]; ++check )
{ /* empty */ }
 return (check == MOD_SIZE);

inline void sc_trace(sc_trace_file * &tf, const cdma_data & cd, string & name)
// ...
inline ostream & operator <<(ostream & os, const cdma_data & cd)

These functions should either be defined within the class as "inline friend" (as in "CDMAVec<X>") or be put into a "correct" namespace ("sc_core" for "sc_trace", or in the namespace of cdma_data.).

for (unsigned int i = 0; i < MOD_SIZE; i++){
 sc_trace(tf, cd.data[i], name + ".data");
}

I think you need to provide unique names for the individual traces, which means you need to append the index of the element correctly. Something like the following could work:

for (unsigned int i = 0; i < MOD_SIZE; i++){
 std::stringstream str;
 str << name << ".data_" << i;
 sc_trace(tf, cd.data[i], str.str() );
}

using namespace std;
template <class X> class CDMAVec : public vector <X>

General recommendations:

  • don't use "using namespace" in header files
  • don't inherit from std::vector publicly (it is not designed for polymorphism)

// using namespace std;
template <class X>
class CDMAVec
 : protected std::vector <X>
{
 typedef std::vector<X> base_type;
 typedef CDMAVec		this_type;
public:
 // make some parts of the interface public
 using base_type::value_type;

 using base_type::iterator;
 using base_type::const_iterator;
 using base_type::begin;
 using base_type::end;

 using base_type::size;
 using base_type::operator [];

 using base_type::resize;
 using base_type::push_back;


 // no need for (empty) default constructor/destructor

 // ...
};

Now we come to the really problematic parts of your code:

inline friend void sc_trace(sc_trace_file * &tf, const CDMAVec & cv<X>, const string & name)
{
 unsigned int j = 0;
 for (size_t i = 0; i < cv.size(); ++i){
  cv[i] -> sc_trace(tf, *cv[i], name + j);
  j++;
 }
}

There are several issues in this function:

  • "CDMAVec & cv<X>" is syntactically wrong, it should have been "CDMAVec<X> & cv"
    (better use a local typedef like "this_type" in my example above)
  • The body of the loop is broken in several ways.
  • "name + j" won't do what you expect (and fail compile, if you're lucky)

inline friend void sc_trace(sc_trace_file * &tf, const CDMAVec<X> & cv, const string & name)
{
 // unsigned int j = 0;
 for (size_t i = 0; i < cv.size(); ++i){
  // cv[i] -> sc_trace(tf, *cv[i], name + j);
  // j++;
  // - neither "c[i]->" nor "*cv[i]" makes any sense for the general case
  // - why j?
  std::stringstream str;
  str << name << "_" << i;
  sc_trace(tf, cv[i], str.str() );
 }
}

There are other errors while using

sc_in<CDMAVec> data_in;

but as of now i am concentrating on this part.

The template parameter is missing.

sc_in<CDMAVec<cdma_data> > data_in;

Hope that helps,

Philipp

Edited by Philipp A. Hartmann
Link to comment
Share on other sites

Hi Philipp,

Thank you for your reply! And thanks for the suggestions!! I am probably little embarrassed by the much needed spoon feeding thought!!

I updated the code as suggested, but as of now I am getting another error, which I could not figure out.

cdma_data.h:


#ifndef CDMA_DATA_H_
#define CDMA_DATA_H_

#include "noc_package.h"


struct cdma_data{
int data[MOD_SIZE];
inline bool operator ==(const cdma_data & cd) const {
 unsigned int check_int;
 check_int = 1;
 for (unsigned int i = 0; i < MOD_SIZE; i++){
  if(cd.data[i] == data[i])
check_int++;
 }
 return (check_int == MOD_SIZE);
};
inline friend void sc_trace(sc_trace_file * &tf, const cdma_data & cd, string & name)
{
 for (unsigned int i = 0; i < MOD_SIZE; i++){
std::stringstream str;
str << name << ".data_" << i;
sc_trace(tf, cd.data[i], str.str());
 }
};
inline friend ostream & operator <<(ostream & os, const cdma_data & cd)
{
 os << "[ ";
 for (unsigned int i = 0; i < MOD_SIZE; i++){
  os << cd.data[i] << ", ";
 }
 os << "]";
 return os;
};
};

#endif /* CDMA_DATA_H_ */

cdma_vec.h :

#ifndef CDMA_VEC_H_
#define CDMA_VEC_H_
#include "cdma_data.h"
#include <vector>
#include <iostream>
template <class X> class CDMAVec : protected vector <X>
{
  typedef std::vector<X> base_type;
  typedef CDMAVec			   this_type;
public:
  // make some parts of the interface public
  using base_type::value_type;
  using base_type::iterator;
  using base_type::const_iterator;
  using base_type::begin;
  using base_type::end;
  using base_type::size;
  using base_type::operator [];
  using base_type::resize;
  using base_type::push_back;
  // no need for (empty) default constructor/destructor
  inline friend void sc_trace(sc_trace_file * &tf, const CDMAVec<X> & cv, const string & name)
  {
 for (size_t i = 0; i < cv.size(); ++i){
  std::stringstream str;
  str << name << "_" << i;
  sc_trace(tf, cv[i], str.str());  //ERROR
 }
  };
  inline friend ostream & operator <<(ostream & os, const CDMAVec<X> & cv)
  {
for (size_t i = 0; i < cv.size(); ++i){
 os << i << " " << cv[i] << std::endl;
}
return os;
  };
};
#endif /* CDMA_VEC_H_ */

error:

Description Resource Path Location Type
no matching function for call to ‘sc_trace(sc_core::sc_trace_file*&, const cdma_data&, std::basic_stringstream<char>::__string_type)’ cdma_vec.h /WNoC_Rev1/src line 41 C/C++ Problem

As far as I know, the 'friend' keyword should have solved this problem, but there after I am not sure.

Thanks.

Does it have to do something with, CDMAVec<cdma_data> not being derived from sc_signal_in_if/sc_signal_in/sc_signal_inout<T>?!

P.S. After the bad code I posted previously, I would like to ask experts to refer me to proper material to go through so as to save myself and others to go through such base level trouble again!!

Edited by gurunath.kadam
Link to comment
Share on other sites

Hello Gurunath,

your code is still buggy for both cdma_data and CDMAVec. Both functions, sc_trace() as well as operator<<() have to be declared as free functions and not as member functions of the respective data structures! You have to move them out of the struct/class definition. Inside the class definition of CDMAVec, only a friend declaration (without function body) needs to remain so that the implementations of sc_trace() and operator<< can access the private members.

You should reconsider your choice of inheriting protected from std::vector<T>. Is your CDMAVec to be considered as a specialization of std::vector<T> or not? If yes, the inherit public from this type. If not, then inherit private from this type. If you look into Scott Meyer's Effective C++/STL book series, you'll find some guidance to answer this question (sorry for being vague, but I don't have copies of the books at hand). There may be good reason to choose composition instead of inheritance, i.e. to have a private std::vector member instead of inheriting from it. This allows you to better decouple your interface from the interface fixed by std::vector.

You should also consider to check out the C++ FAQ lite on http://www.parashift.com/c++-faq/ which also discusses these issues and how to correctly overload operators.

Link to comment
Share on other sites

Gurunath, Torsten,

inline friend void sc_trace(sc_trace_file * &tf, const cdma_data & cd, string & name)

The "name" parameter needs to be passed as const-reference const std::string&, since the stringstream returns a temporary object which can't be bound to a non-const reference.

your code is still buggy for both cdma_data and CDMAVec. Both functions, sc_trace() as well as operator<<() have to be declared as free functions and not as member functions of the respective data structures! You have to move them out of the struct/class definition. Inside the class definition of CDMAVec, only a friend declaration (without function body) needs to remain so that the implementations of sc_trace() and operator<< can access the private members.

The "inline friend definition" used by Gurunath does not define a member function. It should work as expected and will be selected via argument-dependent lookup.

Greetings from Oldenburg,

Philipp

Link to comment
Share on other sites

Hi Gurunath,

the message is saying there is no overloaded sc_trace where the third argument is a *stringstream*. So something's wrong with using str.str(). Unfortunately I can't see what's wrong. Perhaps calling the stringstream str is confusing. Could you also try str.str().c_str()? Or calling str something like tempstr, and using tempstr.str()?

Alan

P.S. Just seen Philipp's reply! Follow his advice ...

Link to comment
Share on other sites

Philipp, Torsten, Alan,

Thank you for your prompt replies.

Philipp,

I followed what you said and it worked.

All,

Now I have problem with == operator, by looking at sc_signal.h.

Exact error:

Description Resource Path Location Type

‘std::vector<cdma_data, std::allocator<cdma_data> >’ is an inaccessible base of ‘CDMAVec<cdma_data>’ WNoC_Rev1  line 231, external location: /home/gurunath/Documents/systemc-2.3.0/include/sysc/communication/sc_signal.h C/C++ Problem

no match for ‘operator<<’ in ‘os << ((const sc_core::sc_signal<std::vector<unsigned int> >*)this)->sc_core::sc_signal<std::vector<unsigned int> >::m_cur_val’ WNoC_Rev1  line 247, external location: /home/gurunath/Documents/systemc-2.3.0/include/sysc/communication/sc_signal.h C/C++ Problem

no match for ‘operator<<’ in ‘std::operator<< <std::char_traits<char> >((* & os), ((const char*)"    value = ")) << ((const sc_core::sc_signal<std::vector<unsigned int> >*)this)->sc_core::sc_signal<std::vector<unsigned int> >::m_cur_val’ WNoC_Rev1  line 255, external location: /home/gurunath/Documents/systemc-2.3.0/include/sysc/communication/sc_signal.h C/C++ Problem

no match for ‘operator<<’ in ‘std::operator<< <std::char_traits<char> >((* & os), ((const char*)"new value = ")) << ((const sc_core::sc_signal<std::vector<unsigned int> >*)this)->sc_core::sc_signal<std::vector<unsigned int> >::m_new_val’ WNoC_Rev1  line 256, external location: /home/gurunath/Documents/systemc-2.3.0/include/sysc/communication/sc_signal.h C/C++ Problem

‘std::vector<cdma_data, std::allocator<cdma_data> >’ is an inaccessible base of ‘CDMAVec<cdma_data>’ WNoC_Rev1  line 265, external location: /home/gurunath/Documents/systemc-2.3.0/include/sysc/communication/sc_signal.h C/C++ Problem

I think I am making a mistake while using sc_in<CDMAVec<cdma_data> > data_in etc.

But looking at the code I am little bit lost.

Thanks.

Link to comment
Share on other sites

  • 1 year later...

Hi all,

 

Can i create sc_signal<struct> or sc_signal<struct*>

Hello Sir,

Why not declare/define your own interface and

create/instantiate a customized signal channel,

instead of trying to massage an existing signal

channel that might not be particularly suited for

your custom requirements. Although creating

one's own signal channel might appear like

re-inventing the wheel, you would have far, far

tighter control over what you are trying to do.

Link to comment
Share on other sites

Hi

 

Thank you Alan

 

 

in this example http://www.doulos.co...ystemc/faq/#q1 

i need to check the value for info "membre of my struct "

so in the code source of mon.cpp i add

if ((A.info.read())==100)
    cout << "OK";

 

#include "systemc.h"
#include "mon.h"

void mon::monitor() {
  cout << "A\t\t";
  cout << "B" << endl;

  while (true) {
    wait();                             // wait for 1 clock cycle
    cout << A.read() << "\t";
    if ((A.info.read())==100)
    cout << "OK";
    cout << B.read() << endl;
  }
}

 

but i have this error class sc_core::sc_in<MyType>’ has no member named ‘info’

 bellow you find the code source of Mytype.h

 

 

#ifndef MYTYPE_H
#define MYTYPE_H
#include "systemc.h"
#include <string>
#include <iomanip>

class MyType {
 public:
    unsigned info;
    bool flag;
  public:

    // constructor
    MyType (unsigned _info = 0, bool _flag = false) {
      info = _info;
      flag = _flag;
    }

    inline bool operator == (const MyType & rhs) const {
      return (rhs.info == info && rhs.flag == flag );
    }

    inline MyType& operator = (const MyType& rhs) {
      info = rhs.info;
      flag = rhs.flag;
      return *this;
    }

    inline friend void sc_trace(sc_trace_file *tf, const MyType & v,
    const std::string& NAME ) {
      sc_trace(tf,v.info, NAME + ".info");
      sc_trace(tf,v.flag, NAME + ".flag");
    }

    inline friend ostream& operator << ( ostream& os,  MyType const & v ) {
      os << "(" << v.info << "," << std::boolalpha << v.flag << ")";
      return os;
    }

};
#endif

 

please help me

Link to comment
Share on other sites

read() is  a method of SystemC classes such as sc_signal and sc_in. info is just a data member of your struct.

 

So presumably you need

 

A.read().info

 

As the error says

 

class sc_core::sc_in<MyType> has no member named info

 

because info is a member of MyType, not a member of sc_in<MyType>

 

regards

Alan

Link to comment
Share on other sites

Hi

 

Thank you Alan

 

 

in this example http://www.doulos.co...ystemc/faq/#q1 

i need to check the value for info "membre of my struct "

so in the code source of mon.cpp i add

if ((A.info.read())==100)

    cout << "OK";

 

#include "systemc.h"

#include "mon.h"

void mon::monitor() {

  cout << "A\t\t";

  cout << "B" << endl;

  while (true) {

    wait();                             // wait for 1 clock cycle

    cout << A.read() << "\t";

    if ((A.info.read())==100)

    cout << "OK";

    cout << B.read() << endl;

  }

}

 

but i have this error class sc_core::sc_in<MyType>’ has no member named ‘info’

 bellow you find the code source of Mytype.h

 

 

#ifndef MYTYPE_H

#define MYTYPE_H

#include "systemc.h"

#include <string>

#include <iomanip>

class MyType {

 public:

    unsigned info;

    bool flag;

  public:

    // constructor

    MyType (unsigned _info = 0, bool _flag = false) {

      info = _info;

      flag = _flag;

    }

    inline bool operator == (const MyType & rhs) const {

      return (rhs.info == info && rhs.flag == flag );

    }

    inline MyType& operator = (const MyType& rhs) {

      info = rhs.info;

      flag = rhs.flag;

      return *this;

    }

    inline friend void sc_trace(sc_trace_file *tf, const MyType & v,

    const std::string& NAME ) {

      sc_trace(tf,v.info, NAME + ".info");

      sc_trace(tf,v.flag, NAME + ".flag");

    }

    inline friend ostream& operator << ( ostream& os,  MyType const & v ) {

      os << "(" << v.info << "," << std::boolalpha << v.flag << ")";

      return os;

    }

};

#endif

 

please help me

Hello Sir,

Once more, please spend some effort/time and create your own

custom signal interface and that will eliminate your problems. A

recent new book on SystemC has a fully worked out example.

Check it out.

Link to comment
Share on other sites

Hi,

thank you very much Alan,

 

 i have another problem when  i change the type of an membre of mytype to string

the code soure is bellow :

 

#ifndef MYTYPE_H
#define MYTYPE_H
#include "systemc.h"
#include <string>
#include <iomanip>
using namespace std;
class MyType {
 public:
    int info;
   string flag;
  public:

    // constructor
    MyType (int _info = 0, string _flag = "") {
      info = _info;
      flag = _flag;
    }

    inline bool operator == (const MyType & rhs) const {
      return (rhs.info == info && rhs.flag == flag );
    }

    inline MyType& operator = (const MyType& rhs) {
      info = rhs.info;
      flag = rhs.flag;
      return *this;
    }

    inline friend void sc_trace(sc_trace_file *tf, const MyType & v,
    const string& NAME ) {
      sc_trace(tf,v.info, NAME + ".info");
      sc_trace(tf,v.flag, NAME + ".flag");
    }

    inline friend ostream& operator << ( ostream& os,  MyType const & v ) {
      os << "(" << v.info << "," << std::boolalpha << v.flag << ")";
      return os;
    }

};
#endif

 

but i have this error in compilation:

 

mytype.h: In function ‘void sc_trace(sc_core::sc_trace_file*, const MyType&, const std::string&)’:
mytype.h:32: error: no matching function for call to ‘sc_trace(sc_core::sc_trace_file*&, const std::string&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)’

 

 

help me Please

 

Thank you again

Link to comment
Share on other sites

That message is saying that there's no overloaded sc_trace for std::string.

 

One option might be to copy the string into an sc_bv and trace that (as there is an sc_trace for sc_bv).

 

The list of built-in sc_trace functions is in the 1666-2011 standard,

 

regards

Alan

 

P.S. I expect that free waveform viewers like gtkwave won't be able to display strings on the waveforms anyway.

Link to comment
Share on other sites

  • 1 month later...

Hi,

thank you very much Alan,

 

I have a compilation problem

 

code source packet.h

#ifndef CDMA_DATA_H_
#define CDMA_DATA_H_

#include "systemc.h"
#include <string>
#include <iomanip>

//#include "declaration.h"



class cdma_data {
public :
  static int MOD_SIZE ;
   int *data;
    // constructor

    cdma_data ( int taille)
    {
   MOD_SIZE=taille;
   data=new int [MOD_SIZE];
    }


inline bool operator ==(const cdma_data & cd) const
    {
      unsigned int check_int;
      check_int = 1;
      for (unsigned int i = 0; i < MOD_SIZE; i++){
       if(cd.data == data)
        check_int++;
      }
      return (check_int == MOD_SIZE);
    }
    
inline friend void sc_trace(sc_trace_file * &tf, const cdma_data & cd, std::string & name)
    {
      for (unsigned int i = 0; i < MOD_SIZE; i++)
          {
        std::stringstream str;
        str << name << ".data_" << i;
        sc_trace(tf, cd.data, str.str());
            }
        }
inline friend ostream & operator <<(ostream & os, const cdma_data & cd)
{
  os << "[ ";
  for (unsigned int i = 0; i < MOD_SIZE; i++){
   os << cd.data << ", ";
  }
  os << "]";
  return os;
}
};

#endif /* CDMA_DATA_H_

 

 

and main.cpp

 

 

#include "mytype.h"
#include "systemc.h"
#include "stim.h"
#include "mon.h"

int sc_main(int argc, char* argv[]) {
  // signals to wire up the device under test
  sc_signal<MyType> ASig, BSig;

  // declare a clk for the stimulus process
  sc_clock TestClk("TestClock", 10, SC_NS);

  // stimulus instance
  stim Stim1("Stimulus1");
  Stim1.A(ASig);
  Stim1.B(BSig);
  Stim1.Clk(TestClk);

  // monitor instance
  mon Mon1("Monitor1");
  Mon1.A(ASig);
  Mon1.B(BSig);
  Mon1.Clk(TestClk);

  sc_start();

  return (0);

}[/background]

 

the error is :

 

 

include/sysc/communication/sc_signal.h: In constructor ‘sc_core::sc_signal<IF>::sc_signal() [with T = cdma_data]’:
main.cpp:9:   instantiated from here
/home/hend/systemc/systemc-2.2.0//include/sysc/communication/sc_signal.h:75: error: no matching function for call to ‘cdma_data::cdma_data()’
packet.h:18: note: candidates are: cdma_data::cdma_data(int)
packet.h:12: note:                 cdma_data::cdma_data(const cdma_data&)
/home/hend/systemc/systemc-2.2.0//include/sysc/communication/sc_signal.h:75: error: no matching function for call to ‘cdma_data::cdma_data()’
packet.h:18: note: candidates are: cdma_data::cdma_data(int)
packet.h:12: note:                 cdma_data::cdma_data(const cdma_data&)

 

 

 

 

please Help me

 

 

 Thank you again Alan

Link to comment
Share on other sites

The compiler error tells you that the sc_signal<T> implementation is trying to call a default constructor T() for the type passed as template argument. Your cdma_data struct does not provide a default constructor (taking no arguments). Your data structure has also to fulfill other requirements to be compatible with sc_core::sc_signal: For details, please, cf. to IEEE Std 1666-2011 clause 6.4.3.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...