Jump to content

Use SC_THREAD to realize delay


scsc

Recommended Posts

There are quite a few examples here and on the net that using SC_METHOD to implement delay is the right way. I just want to deepen my understanding on SC_THREAD. I look at the gas station example in SystemC from the Ground Up. The customer_thread2 actually has such delay implemented with a dynamic sensitivity setting:

// header file declares SC_THREAD, no sensitivity

SC_THREAD(customer2_thread);

The implementation of customer2_thread is:

void gas_station::customer2_thread(void) {
  for (;;) {
    // Simulate gas tank emptying time
    wait((m_full2+rand()%int(m_full2*0.10))*t_MIN);
    cout << "INFO: " << name() 
         << " Customer2 needs gas (1) at "
         << hms() << endl;
    m_tank2 = 0;
    // Request fillup from attendant and then
    // wait for acknowledging event.
    do {
      e_request2.notify(); // I need fillup! (2)
      wait(e_filled); // use dynamic sensitivity   <<<<<<<<< this is the delay between e_request2.notify() and e_filled event.
    } while (m_tank2 == 0);
  }//endforever
}//end customer2_thread()

Since e_filled in the customer2_thread comes from another method, I wonder if I can put a standalone event inside the same thread? In other words, the event just triggers the delay from the same thread? For example, a delay thread in a NAND2 function:

void nand2_thread() {     // nand2_thread has no sensitivity, just like customer2_thread
        bool aa, bb, c;
        aa = a.read();
        bb = b.read();
        c = !(a & b);
        e.notify(4, SC_NS);     // e is an object in nand2. This event seems not notified and triggered correctly, 
        for (;;) {
            wait(e);                   // e never comes
            ff.write(c);
        }

}

I think the above code didn't have the right event notified so wait(e) didn't work. What's the right way to setup this event?
 

By the way, how to set enclosure in this forum for code? Pasting code as raw text is hard to read.   

 

Link to comment
Share on other sites

I don't have EDA Playground account. But I drop test code here:

#include <systemc.h>

SC_MODULE(nand2) {
    sc_in<bool>  a;
    sc_in<bool>  b;
    sc_out<bool> f;
    sc_out<bool> ff;
    sc_event e;

    SC_CTOR(nand2) {
        SC_METHOD(do_nand2);       // the right way is use SC_METHOD + next_trigger.
        sensitive << a << b;
        SC_THREAD(nand2_thread);  // but how to use use SC_THREAD instead?
    }

    void nand2_thread() {
        bool aa, bb, c;
        aa = a.read();
        bb = b.read();
        c = !(a & b);
        e.notify(4, SC_NS);
        for (;;) {
            wait(e);
            ff.write(c);
        }
    }
    
    void do_nand2() {
        bool aa, bb, ff;
        aa = a.read();
        bb = b.read();
        ff = !(a & b);
        next_trigger(4, SC_NS);  // next_trigger in SC_METHOD had the expteced delay
        f.write(ff);
    }
};

SC_MODULE(stim) {
    sc_out<bool>  A, B;
    sc_in<bool>   clk;

    void stimGen() {
        wait();
        A.write(false);
        B.write(false);
        wait();
        A.write(false);
        B.write(true);
        wait();
        A.write(true);
        B.write(true);
        wait();
        A.write(false);
        B.write(true);
    }
    SC_CTOR(stim) {
        SC_THREAD(stimGen);
        sensitive << clk.pos();
    }
};

int sc_main(int argc, char* argv[])
{
    sc_signal<bool> a;
    sc_signal<bool> b;
    sc_signal <bool> f, ff;
    sc_clock clk("clk", 100, SC_NS, 0.5);

    stim s("stim");
    s.A(a);
    s.B(b);
    s.clk(clk);

    nand2 n2("n2");
    n2.a(a);
    n2.b(b);
    n2.f(f);
    n2.ff(ff);

    sc_trace_file* wf = sc_create_vcd_trace_file("sim");
    sc_trace(wf, s.clk, "clk");
    sc_trace(wf, n2.a, "a");
    sc_trace(wf, n2.b, "b");
    sc_trace(wf, n2.f, "f");
    sc_trace(wf, n2.ff, "ff");

    sc_start(400, SC_NS);

    sc_close_vcd_trace_file(wf);

    return 0;
}

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