Home / SystemC / Communication via Ports with User-defined Data Types

Communication via Ports with User-defined Data Types

SystemC supports many data types to communicate between 2 ports. eg: bool, unsigned char, unsigned int, sc_uint, … But, in some cases, we want to use a user-defined data type. In this post, we are going to discuss it.

Suppose, DUT_1 model needs some info of DUT_2 model. It is a group, ID, and address. Instead of, we declare 3 input ports for DUT_1 model corresponding 3 output ports of DUT_2 model, we will define an “extension_info” struct to store and information exchange between DUT_1 and DUT_2 model by 1 port.

It is the same as :

struct extension_info
{
    /// Constructor & Destructor
    extension_info();
    ~extension_info();
    
    /// Variables
    unsigned int  group;
    unsigned int  ID;
    unsigned int  address;

    /// Functions
    void initialize(void);

};

...
//output port
sc_out <extension_info > output_p;

... 
//input port 
sc_in <extension_info > input_p;

... 
//bind port 
sc_signal<extension_info> signal_p;

DUT_2->output_p(signal_p); 
DUT_1->input_p(signal_p);

For extension_info works correct. We need :

  • Overload “==” operator
bool extension_info::operator == (const extension_info &indata) const
{
    return ((this->group == indata.group) &&
        (this->ID == indata.ID) &&
        (this->address == indata.address));
}

“==” operator using for judge value change or not in sc_signal_t::write( const T& value_ ) and sc_signal_t::update() functions (sc_signal.h)

  • Overload “<<“ operator
std::ostream& operator << (std::ostream& os,const extension_info& data)
{
    os << "GROUP   " <<": " << data.group             << endl;
    os << "ID      " <<": "    << data.ID             << endl;
    os << "ADDRESS " <<": 0x"  <<std::hex<< (unsigned int) data.address  << endl;
    return os;
}

“<<“ operator using in sc_signal_t::print( ::std::ostream& os ) const and sc_signal_t::dump( ::std::ostream& os ) const functions (sc_signal.h)

  • Implement sc_trace function
inline void sc_trace(sc_core::sc_trace_file* tf, const extension_info& data, const std::string& fn)
{
    std::ostringstream str_tmp;
    str_tmp.str("");
    str_tmp << "group";
    sc_trace(tf, data.group, str_tmp.str().c_str());

    str_tmp.str("");
    str_tmp << "ID";
    sc_trace(tf, data.ID, str_tmp.str().c_str());

    str_tmp.str("");
    str_tmp << "address";
    sc_trace(tf, data.address, str_tmp.str().c_str());

}

It is necessary for end_of_elaboration function (sc_signal_port.h)

And final, you can use extension_info as other data types for sc_in, sc_out, sc_signal… and using read(), write() function to read/write data.

Download :

Google Drive 1

__FreeSourceC.com__

About admin

Check Also

[ SC Tutorial ] 7. Module : Alternative Constructors: SC_HAS_PROCESS

SC_HAS_PROCESS là một macro thay thế cho cách tạo hàm khởi tạo (constructor) trong SystemC, …

Leave a Reply

Your email address will not be published. Required fields are marked *