Jump to content

Mudong

Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by Mudong

  1. #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.
  2. hi yaohe, I understand your question and I met same problem too. We want the simulation keep alive and wait asyn events, but the extra SC_THREAD "main"(as you said) destroy simulation time. Do you solve this problem now? Or I hope the example blew can solve it: #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; }
  3. 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. =================================================
  4. maybe the solution above is not good. I find better solution here : thanks .
  5. hi, thank you for your solution. Here is my c++ program to call an systemc module according to your solution: class DoFFT: public sc_module { public: SC_HAS_PROCESS(DoFFT); DoFFT(sc_module_name name){ SC_THREAD(dofft); }; void dofft(){ cout << " # calculate fft !"<< endl; }; }; int sc_main(int argc, char* argv[]) { DoFFT dofft("do"); sc_start(1, SC_SEC); cout << " sc_main finish !" << endl; return 0; } int main(){ sc_main(0, NULL); cout << " here is main fun !" << endl; sc_main(0, NULL); return 0; } It works when i only add one "sc_main()" in main function. But there is error when i add more than one, as shown in above mian function. what if i want to call this systemc module multi times ?
×
×
  • Create New...