Jump to content

Time, initialization and async_request_update


katang

Recommended Posts

SC started @ 0 s
External host thread initial processing
Before Event 1 notified from an external host thread
ThreadSafeNotify called @ 0 s
After Event 1 notified from an external host thread
Event 2 notified from an external host thread
ThreadSafeNotify called @ 0 s
ThreadSafeUpdate called @ 0 s
Got event in triggered @ 0 s
Got event after next_trigger(10,SC_NS) @ 0 s
Got event in triggered again @ 0 s
Initializing main() @ 0 s
Wait event in main() @ 15 ns
SC returned

Maybe this issue deserves a separate thread. The output shows, that the time passes only in thread but not method,

or only by wait() but not by next_request()?

This explains (for me, at least) why main() does not receive an event. The listing tells that next_trigger() advanced the time,

but the next line tells it did not. The wait() does.

It seems to me that the engine starts before intializing main().

 

#include <pthread.h>
#include <unistd.h>

using namespace sc_core;

class ThreadSafeEventIf : public sc_interface {
    virtual void notify(sc_time delay = SC_ZERO_TIME) = 0;
    virtual const sc_event &default_event(void) const = 0;
protected:
    virtual void update(void) = 0;
};

class ThreadSafeEvent : public sc_prim_channel, public ThreadSafeEventIf {
public:
    ThreadSafeEvent(const char *name = ""): event(name) {}

    void notify(sc_time delay = SC_ZERO_TIME) {
        this->delay = delay;
    std::cout << "ThreadSafeNotify called @ "<<sc_time_stamp()<<std::endl;
        async_request_update();
    }

    const sc_event &default_event(void) const {
        return event;
    }
protected:
    virtual void update(void) {
        event.notify(delay);
     std::cout << "ThreadSafeUpdate called @ "<<sc_time_stamp()<<std::endl;
     }
    sc_event event;
    sc_time delay;
};

SC_MODULE(Foo)
{
public:
    SC_CTOR(Foo)
    {
        SC_THREAD(main);
        sensitive << event;

        SC_METHOD(eventTriggered);
        sensitive << event;
        dont_initialize();
     std::cout << "SC started @ "<<sc_time_stamp()<<std::endl;
    }
private:
    void main() {
       std::cout << "Initializing main() @ "<<sc_time_stamp()<<std::endl;
    int i=0;
    while(i++<3)
    {
        wait(15,SC_NS);
        std::cout << "Wait event in main() @ "<<sc_time_stamp()<<std::endl;
        wait();
        std::cout << "Got event in main() @ "<<sc_time_stamp()<<std::endl;
        }
}

    void eventTriggered() {
        std::cout << "Got event in triggered @ "<<sc_time_stamp()<<std::endl;
        next_trigger(10,SC_NS);
        std::cout << "Got event after next_trigger(10,SC_NS) @ "<<sc_time_stamp()<<std::endl;
        next_trigger();
        std::cout << "Got event in triggered again @ "<<sc_time_stamp()<<std::endl;
}

public:
    ThreadSafeEvent event;
};

void *externalHostThread(void *arg) {
    std::cout << "External host thread initial processing" << std::endl;
      for(int i=0; i<100; i++){ i += 1;}
    Foo* foo = (Foo*)(arg);
    std::cout << "Before Event 1 notified from an external host thread" << std::endl;
    foo->event.notify();
    std::cout << "After Event 1 notified from an external host thread" << std::endl;
      for(int i=0; i<100; i++){ i += 1;}
    std::cout << "Event 2 notified from an external host thread" << std::endl;
    foo->event.notify();
}

int sc_main(int argc, char *argv[])
{
    Foo foo("foo");

    pthread_t thread;
    pthread_create(&thread, NULL, externalHostThread, &foo);

    sc_start();
    std::cout << "SC returned" << std::endl;
    return 0;
}

 

Link to comment
Share on other sites

You should seriously read the SystemC standard or related books.

Neither SC_METHOD nor SC_THREAD pass time, time advances only in the SystemC kernel and the kernel returns control to them at certain time points.

The difference is that SC_THREAD preserves state when returning control to the kernel by calling wait(). SC_METHOD cannot preserve the state (it is always called as a function) and is therefore not allowed to call wait(). next_trigger() tells the SC kernel when to invoke the SC_METHOD next time. Calling next_trigger() several times in the same method as you do it is meaningless as the last call to next_trigger() prevails. Again: next_trigger() does not stop execution rather tells the kernel when to start the method next time.

 

Link to comment
Share on other sites

One good resource for understanding the fundamental concepts of SystemC is "SystemC from the Ground Up", 2nd edition, Springer by David Black et al. Of course, IEEE Std. 1666-2011 is the definitive source to be consulted for any semantic details.

Link to comment
Share on other sites

Concerning the waiting: the next_trigger header tells

next_trigger() uses   all the syntax of wait(), and behaves as if the actual process of  "waiting" occurs after the method exits. 

The next sentence gives a hint, that it "may gain the effect of a wait" but does tell that !  ONLY if I use it in a state machine!.

By using a state variable  member in the module class, a method may gain the effect of a wait
as illustrated in the following example.

example code tells

      // Wait 1 ns similar to Verilog #1
      next_trigger(1,SC_NS);

Thanks for the explanation. When one skims the example and attempts to understand the new terms, can easily misinterpret the term. Especially if compared to an already known construction. Sorry.

Concerning the timing, could you, please, pinpoint the sentence I took from the book you mentioned.

"After initialization, simulation processes are run when events occur to which they are sensitive."

In my output, it looks like the main() , without

dont_initialize();

is just one of the processes executed at zero time, but not part of the initialization. Correct?
 

 

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