Jump to content

how to implement delay in systemC?


jatin jatin

Recommended Posts

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

Link to comment
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.

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