Jump to content
jatin jatin

how to implement delay in systemC?

Recommended Posts

Hi all,

is there any way to implement the intra assignment delay in systemC without using sc_event()?

for example : in verilog we write

out = #10 in1 + in2;  // intra assignment delay.

how would i implement the same in systemC?

regards,

jatin 

Share this post


Link to post
Share on other sites

Hi Roman,

I am glad that you respond. Here is a code of an adder i implemented. Here the output is updating after 2ns of input change. i.e. i implemented the intra assignment delay of 2 ns using sc_event and SC_METHOD(). This code works fine.

***NOW, SOMEONE WHO KNOWS SYSTEMC VERY WELL WANTS ME TO IMPLEMENT THE SAME USING SC_THREAD AND WITHOUT USING SC_EVENT()***  

so it would be great if you could suggest somthing.

#include<systemc.h>
sc_int<32> out;
sc_event e;
    //  design module//

SC_MODULE(adder)  
    { 
          sc_in< sc_int<16> > a;
          sc_in< sc_int<16> > b;         
          sc_out< sc_int<32> > outp;
  
          
 void toggle()
    {            
                       out= a.read() + b.read();
                       e.notify(2,SC_NS);                            
    }
                    
void add()
              {                                      
                       outp.write(out);                      
              }
            
void show()
            {                
                    cout<<"||sum  @ "<<sc_time_stamp()<<"\t:\t\t\t"<<outp<<endl;
            }
      

 SC_CTOR(adder)
          {   
       SC_METHOD(add);
               sensitive << out;    
               dont_initialize();
   
       SC_METHOD(toggle);
              sensitive << a << b;      
              dont_initialize();
              
       SC_METHOD(show);
               sensitive <<outp; 
               dont_initialize();                       
          }
    };


             // test bench //

SC_MODULE(tb)
    {   
          sc_out< sc_int<16> > a,b;             
          
  void stimulus()
           {  
                   a.write(0);
                   b.write(0);   
                          wait(5,SC_NS);   
                  a.write(5);
                  b.write(0);                
                          wait(2,SC_NS);   
                  a.write(5);
                  b.write(5);  
                          wait(8,SC_NS);    
                   a.write(7);
                   b.write(8);    
                          wait(5,SC_NS);                              
                sc_stop();                    
             }
  
  void show()
        {
           cout<<"inputs @"<<sc_time_stamp()<<" :\t"<<a<<"\t|\t"<<b<<"\t";
        }
  
         SC_CTOR(tb)
            {
                  
          SC_THREAD(stimulus);
                  
          SC_METHOD(show);
                  sensitive << a<< b;
              }   
    };

int sc_main(int argc, char* argv[])
    {         
           
          tb tb0("tbinst");
           adder ad("adinst");
  
              sc_signal< sc_int<16> > a_sig, b_sig;
             sc_signal< sc_int<32> > outp_sig;
        
              tb0.a(a_sig);
            tb0.b(b_sig);        
    
            ad.a(a_sig);
            ad.b(b_sig);
            ad.outp(outp_sig);
          
  cout<<"a \t:\tb \n";

   
         sc_start(); 
          return 0;
    }

regards

jatin

Share this post


Link to post
Share on other sites
Quote

***NOW, SOMEONE WHO KNOWS SYSTEMC VERY WELL WANTS ME TO IMPLEMENT THE SAME USING SC_THREAD AND WITHOUT USING SC_EVENT()***  

If you use SC_THREAD that means that you are using sc_event under the hood. Because every SC_THREAD in SystemC has event that activates the process.

You can for example spawn a SC_THREAD process to do delayed assignment like this:

#include <systemc.h>

struct spawned_assignment_demo : sc_module {

    SC_CTOR(spawned_assignment_demo) {
        SC_THREAD(test_thread);
    }

    sc_signal<int, SC_MANY_WRITERS> out{"out", 0};

    void print_out_value() {
        cout << " out value = " << out.read() << " at " << sc_time_stamp() << "\n";
    }

    void test_thread() {

        print_out_value();

        sc_spawn([&]() {
            wait(10, SC_NS);
            out = 42;
        });

        wait(5, SC_NS);
        print_out_value();

        sc_spawn([&]() {
            wait(10, SC_NS);
            out = 124;
        });

        wait(6, SC_NS);
        print_out_value();

        wait(6, SC_NS);
        print_out_value();

    }

};

int sc_main (int argc, char**argv) {
    spawned_assignment_demo demo{"demo"};
    sc_start();
    sc_stop();
}

 

Output will be as expected:

 out value = 0 at 0 s
 out value = 0 at 5 ns
 out value = 42 at 11 ns
 out value = 124 at 17 ns

 

But when you call  wait(10,SC_NS) you create timed event notification. You can use debugger to step into wait() to see what happens there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×