Mudong Posted April 8 Report Posted April 8 I want to control the SystemC model through an external program to complete the required instructions. I tried many methods, but failed. After reading the following posts : It realy helps a lot . I have solved my problem, and i want to share it. here is the example that "main thread controlling sub_thread(systemc module) to complete instructions": #include <systemc.h> #include <pthread.h> #include <unistd.h> using namespace std; 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; async_request_update(); } const sc_event &default_event(void) const { return event; } protected: virtual void update(void) { event.notify(delay); } sc_event event; sc_time delay; }; sc_event ConvTriggerEvent; sc_event StopEvent; SC_MODULE(Foo) { public: SC_CTOR(Foo) { SC_THREAD(main); SC_METHOD(eventTriggered); sensitive << threadSafeEvent; dont_initialize(); SC_METHOD(stopEventTriggered); sensitive << threadStopEvent; dont_initialize(); } private: void main() { //extra forever thread to avoid sistemc eixt while (1) { wait(100, SC_NS); } } void eventTriggered() { printf("Foo: Got event from pthread \n"); ConvTriggerEvent.notify(); } void stopEventTriggered(){ printf("Foo: want to stop sc module \n"); StopEvent.notify(); } public: ThreadSafeEvent threadSafeEvent; ThreadSafeEvent threadStopEvent; } void* AIThread(void* arg) { sc_start(); // The systemc runs continuously, waiting external instruction return NULL; } class AI_Process : sc_module{ public: SC_HAS_PROCESS(AI_Process); AI_Process(sc_module_name name){ SC_THREAD(RunAIChip); SC_THREAD(StopAIChip); } int cnt = 0; void RunAIChip(){ while(true){ printf("[ai chip info]: Wait instruction ... \n"); wait(ConvTriggerEvent); cnt ++; printf("[ai chip info]: %d Conv Process ... \n",cnt); } } void StopAIChip(){ wait(StopEvent); sc_stop(); } }; int sc_main(int argc, char *argv[]) { Foo foo("foo"); AI_Process ai("ai_process"); pthread_t thread; pthread_create(&thread, NULL, AIThread, NULL); int cnt = 0; while (cnt < 2) { cnt++; usleep(3*1000*1000); foo.threadSafeEvent.notify(); } printf("==========================================") while (cnt < 4) { cnt++; usleep(3*1000*1000); foo.threadSafeEvent.notify(); // call systemc at any time you want } printf("==========================================") while (cnt < 6) { cnt++; usleep(3*1000*1000); foo.threadSafeEvent.notify(); } printf("==========================================") usleep(5*1000*1000); foo.threadStopEvent.notify(); pthread_join(thread, NULL); printf("==========================================") return 0; } the result : [ai chip info]: Wait instruction ... Foo: Got event from pthread [ai chip info]: 1 Conv Process ... [ai chip info]: Wait instruction ... ================================================= Foo: Got event from pthread [ai chip info]: 2 Conv Process ... [ai chip info]: Wait instruction ... Foo: Got event from pthread [ai chip info]: 3 Conv Process ... [ai chip info]: Wait instruction ... ================================================= Foo: Got event from pthread [ai chip info]: 4 Conv Process ... [ai chip info]: Wait instruction ... Foo: Got event from pthread [ai chip info]: 5 Conv Process ... [ai chip info]: Wait instruction ... ================================================= Foo: Got event from pthread [ai chip info]: 6 Conv Process ... [ai chip info]: Wait instruction ... Foo: want to stop sc module Info: /OSCI/SystemC: Simulation stopped by user. ================================================= Quote
Mudong Posted April 10 Author Report Posted April 10 #include <systemc.h> #include <pthread.h> #include <unistd.h> using namespace std; 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; async_request_update(); } const sc_event &default_event(void) const { return event; } protected: virtual void update(void) { event.notify(delay); } sc_event event; sc_time delay; }; sc_event GenScEvent; sc_event workingFinishEvent; // finish event int workingFlag = 0; // maybe dnot need a lock SC_MODULE(Foo) { public: SC_CTOR(Foo) { SC_THREAD(main); SC_METHOD(eventTriggered); sensitive << threadSafeEvent; dont_initialize(); } private: void main() { //extra forever thread to avoid simulation exit while (1) { usleep(1*1000*1000); // check if there is any instruction every one sec. wait(SC_ZERO_TIME); if(workingFlag){ // check working wait(workingFinishEvent); // wait the working finish } usleep(1*1000*1000); } } void eventTriggered() { // printf("Foo: Got event from pthread \n"); GenScEvent.notify(); } public: ThreadSafeEvent threadSafeEvent; }; void* PollingThread(void* arg) { int cnt = 0; while (1) { cnt++; printf("[POLL]: %d: Before generating event from PollingThread \n", cnt); usleep(3*1000*1000); Foo *foo = (Foo*)(arg); foo->threadSafeEvent.notify(); printf("[POLL]: %d: Event notified from PollingThread \n", cnt); } }; class sc_top : public sc_module { private: SC_HAS_PROCESS(sc_top); public: sc_top(sc_module_name name="SCTOP"): sc_module(name) { SC_THREAD(processing_thread); } void processing_thread(); }; void sc_top::processing_thread() { int cnt =0; while (1) { printf("[PROC]: processing_thread called \n"); cout << "[PROC]: Wait GenScEvent time: " << sc_time_stamp(); wait(GenScEvent); workingFlag = 1; cnt++; wait(10, SC_SEC); // advance simulation time cout << "[PROC]: Got and Finish "<<cnt << " GenScEvent time: " << sc_time_stamp(); workingFinishEvent.notify(); workingFlag = 0; } } int sc_main(int argc, char *argv[]) { Foo foo("foo"); sc_top u_sc_top("u_sc_top"); pthread_t thread; pthread_create(&thread, NULL, PollingThread, &foo); sc_start(); return 0; } this example is a better one. it won't destory simulation time. Quote
Recommended Posts
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.