Jump to content

sc_inout class


Nithin

Recommended Posts

Hi,

I am trying to create a simple SystemC code to add two numbers. Here is my code:

 

class sum : public sc_core::sc_module
{
public:
    SC_HAS_PROCESS(sum);
    sc_core::sc_in<sc_int<10>> A_val_in;
    sc_core::sc_in<sc_int<10>> B_val_in;
    sc_core::sc_inout<sc_int<10>> S_val_out;


    void do_sum();
    sum (const char* name) : sc_core::sc_module(name) {
        SC_THREAD(do_sum);
            sensitive << A_val_in << B_val_in;
    }
   

};

void sum::do_sum() {
    while(true) {
        wait();
        std::cout << "DBG: Inside do_sum\n";
        sc_int<10> A = A_val_in.read();
        sc_int<10> B = B_val_in.read();
        sc_int<10> S = A + B;
        S_val_out.write(S);
        std::cout << "DBG: Inside do_sum: A_val_in = " << A_val_in << ", B_val_in = " << B_val_in << std::endl;
        std::cout << "DBG: Inside do_sum: A = " << A << ", B = " << B << std::endl;
        std::cout << "DBG: Inside do_sum: S = " << S <<std::endl;
        std::cout << "DBG: Inside do_sum: S_val_out = " <<S_val_out<< std::endl;
    }

}

I feel there is some problem with S_val_out.write(S) command as highlighted below. Here is the print messages. S_val_out is always 0. I tried changing the definition of S_val_out to sc_out. But it is not helping. 

DBG: Inside do_sum
DBG: Inside do_sum: A_val_in = 5, B_val_in = 10
DBG: Inside do_sum: A = 5, B = 10
DBG: Inside do_sum: S = 15
DBG: Inside do_sum: S_val_out = 0

Can someone please help me to understand what the problem is?

Thanks in advance.

Regards,

Nithin

Link to comment
Share on other sites

  • Nithin changed the title to sc_inout class

Nothing wrong. Your output will be available after the delta cycle completes. You can view the new value in the next delta cycle.

The problem for you conceptually is that you think 'S_val_out = expression' is a blocking statement. In other words, you expect the value to be transferred to the current value of S_val_out at the end of the assignment. Actually what is happening is akin to:

S_val_out->write( A_val_in->read() +B_val_in->read() ); 

External to your sum class object, the S_val_out is bound to a channel sc_signal<T>, where the write() method is implemented. The write method does something like this:

template<typename T> class sc_signal : sc_signal_in_if<T>, sc_signal_inout_if<T>
{
  void write( T value ) { next_val = value; request_notify(); }
  T read() { return curr_val; }
  void notify() { curr_val = next_val; }
};

SystemC will call notify at the end of the delta cycle after all other processes in the current delta cycle have completed their work.

You could force code displaying the sum into the next delta cycle with:

wait(SC_ZERO_TIME);

but that could theoretically affect the other wait depending on how the inputs are being driven and cause you to miss updates.

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...