anshkun
anshkun

Reputation: 115

How SC_CTOR works in SystemC

In SystemC module constructor can be defined using SC_CTOR macro:

#define SC_MODULE(user_module_name)                   \
    struct user_module_name : ::sc_core::sc_module

#define SC_CTOR(user_module_name)                     \
    typedef user_module_name SC_CURRENT_USER_MODULE;  \
    user_module_name( ::sc_core::sc_module_name )

Need to understand use scope specifier operator before sc_core. As per the macro user can generate constructor but it takes argument sc_module_name.

User module struct user module is inheriting class sc_module, In class sc_module there are different overloaded constructor like

sc_module();
sc_module( const sc_module_name& nm ); /* for those used to old style */

/* DEPRECATED */ sc_module( const char* nm );
/* DEPRECATED */ sc_module( const std::string& nm );

As per SC_CTOR it is declaring any object of type sc_moduke_name in constructor body if expanded the constructor will take form user_module(::sc_core::sc_module) { /*constructor body*/}. To understand simulate same code but failed to create object of type struct B

class mname {
    private:
            char *name;
    public:
            mname() {
                    cout << "mname constructor invoked\n";
                    cout << "\t name\t" << name;
            }
};

class A {
    friend class mname;
    private:
    mname * mn;
    public:
    A() {
            cout << "A constructor called\n";
    }
    A(const mname& m) {
            cout << "freind class constructor of A\n";
    }
    A(const std:: string& s) {
            cout << "string type construfctor\n";
    }
};

struct B: A {
    B(mname ) {
            cout     << "struct b cosntructor called\n";
    }

};

int main() {

    B obj(mname);
    return 0;
}

Upvotes: 1

Views: 4881

Answers (1)

Guillaume
Guillaume

Reputation: 1417

A typical code to create a module is :

SC_MODULE(my_module)
{
   SC_CTOR(my_module)
   {

   }
};

With macros, this code will expand to:

struct my_module : ::sc_core::sc_module
{
   typedef my_module SC_CURRENT_USER_MODULE;
   my_module( ::sc_core::sc_module_name )
   {

   }
};

If you want to create an instance of this module, you need to pass an instance of ::sc_core::sc_module_name in the constructor and not the class name as you did in your example code. You can also pass something to create implicitly an instance of ::sc_core::sc_module_name as a const char* (cf ::sc_core::sc_module_name constructors)

my_module a("name_of_my_module");

or

::sc_core::sc_module_name module_name("another_name");
my_module b(module_name);

In your example code, just replace the main with :

int main() {
    mname name; // instance of mname
    B obj(name);
    return 0;
}

Upvotes: 3

Related Questions