Jump to content

how is .read() implemented in systemC


trehalose
 Share

Recommended Posts

hi, everyone! I'm trying to figure out what happens when use .read() to get current value of  a  sc_in<T> instance . I checked the definition of sc_in, I found several related codes:

[sc_signal_ports.h]

 // read the current value

    const data_type& read() const
    { return (*this)->read(); }

    operator const data_type& () const
    { return (*this)->read(); }

It seems the .read() is inherited from parent class?

Then I went to the parent class, sc_port <- sc_port_b <-sc_port_base and eventually to sc_object, but I still cannot find the  .read() routine. So how is .read() implemented in systemC. Can I use access to the value using a point instead of the .read() routine?

Thank you!

Link to comment
Share on other sites

I CS terms sc_port is a proxy. Eventually during elaboration all ports being connected to  signal are configured to hold a pointer to the signal. So the read() or write() can be called without knowing anything partuclar of the signal.

sc_in is a typedef to sc_port<sc_signal_in_if<T>>. This means a port can carry any type of interface, not only sc_signal_in_if or sc_signal_inout_if

Link to comment
Share on other sites

Oh, thank you! ' ...all ports being connected to  signal are configured to hold a pointer to the signal.' Is that means the port stores a pointer to the signal? 

I'm still curious about where systemC defines read() or write(). 

read() seems to return the value of signal that binds to the port. I need to pass the address of value to a function.  So I tried to take the address of a X.read() by & . It does not work. 

BTW. what does 'I CS' mean?

Link to comment
Share on other sites

1 hour ago, trehalose said:

Oh, thank you! ' ...all ports being connected to  signal are configured to hold a pointer to the signal.' Is that means the port stores a pointer to the signal? 

Yes, correct.

1 hour ago, trehalose said:

I'm still curious about where systemC defines read() or write(). 

This is defined in the channel class that is bound to the port - for example, `sc_signal`. From sc_signal.h,

template< class T, sc_writer_policy POL >
class sc_signal_t
  : public    sc_signal_inout_if<T>
  , public    sc_signal_channel
  , protected sc_writer_policy_check<POL>
{
   // ...
public:
    // read the current value
    virtual const T& read() const
	{ return m_cur_val; }
};

As a matter of style, I prefer using the arrow operator ("->") over the dot operator (".") when it comes to invoking interface functions on a port. So, call `in_port->read()`, not `in_port.read()`.

Link to comment
Share on other sites

Thank you! I found the definition. I still have a question.  Note the code in sc_signal.h

protected:

    // constructor and destructor

    sc_signal_t( const char* name_, const T& initial_value_ )
      : base_type( name_ )
      , m_cur_val( initial_value_ )
      , m_new_val( initial_value_ )
    {}


the current value is confined by the ‘protected’ keyword.  So I cannot access it using a & unless modifying the source code?

Actually I'm gong to use AXV instruction to speedup the program. the _mm256_loadu_si256() routine needs to accept a address. Therefore I'm figuring out how to load the value of a sc_in<> class to the routine. 

Link to comment
Share on other sites

2 hours ago, trehalose said:

the current value is confined by the ‘protected’ keyword.  So I cannot access it using a & unless modifying the source code?

 

2 hours ago, trehalose said:

Actually I'm gong to use AXV instruction to speedup the program. the _mm256_loadu_si256() routine needs to accept a address. Therefore I'm figuring out how to load the value of a sc_in<> class to the routine. 

I'm not familiar with AXV (or AVX?) extensions - I looked it up and they seem to be instructions to support SIMD operations - added as extensions to x86 instruction set. To exploit architecture specific instructions, you may very well need to change the SystemC simulator sources - but I don't have the technical background on AVX to comment with authority.

The public `read()` function does return the current value as a const-ref, so you can find out the address.

Link to comment
Share on other sites

Thank you! AVX indeed. I made a mistake 🙂

1 hour ago, karthickg said:

The public `read()` function does return the current value as a const-ref, so you can find out the address.

Yes, but if I use & to take the address, it doesn't work cause the current value is a protected member in official systemC.

I'm afraid I might modify the code.  Is it casue side effects if I just get rid of the "protected"?

Link to comment
Share on other sites

Just now, trehalose said:

Yes, but if I use & to take the address, it doesn't work cause the current value is a protected member in official systemC.

No - it doesn't matter whether the member is protected or private.

Check with:

#include <iostream>

class A
{
   private:
      int m_current_value{0};
   public:
      int & read() { return m_current_value; }
      void write(int v) { m_current_value = v; }
};


int main()
{
   A a;

   std::cout << "Initially (should print 0): " << a.read() << std::endl;
   a.write(42);
   std::cout << "Step 1 (should print 42): " << a.read() << std::endl;

   int &val_ref = a.read();
   std::cout << "Step 2 (should print 42): " << val_ref << std::endl;
   val_ref = 100; // Will change m_current_value
   std::cout << "Step 3 (should print 100): " << a.read() << std::endl;

   return 0;
}

The only difference in the code above is that `read()` is returning a non-const ref.

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.

 Share

×
×
  • Create New...