Trung tâm đào tạo thiết kế vi mạch Semicon


  • ĐĂNG KÝ TÀI KHOẢN ĐỂ TRUY CẬP NHIỀU TÀI LIỆU HƠN!
  • Create an account
    *
    *
    *
    *
    *
    Fields marked with an asterisk (*) are required.
wafer.jpg

Channels In SystemC Part III

E-mail Print PDF

sc_semaphore
sc_semaphore is a predefined primitive channel intended to model the behavior of a software semaphore as used to provide limited concurrent access to a shared resource. A semaphore has an integer value, the semaphore value, which is set to the permitted number of concurrent accesses when the semaphore is constructed.

sc_semaphore has following predefined methods.

  • int wait() : If the semaphore value is equal to 0, member function wait shall suspend until the semaphore value is incremented (by another process), at which point it shall resume and attempt to decrement the semaphore value.
  • int trywait() : If the semaphore value is equal to 0, member function trywait shall immediately return the value -1 without modifying the semaphore value.
  • int post(); Member function post shall increment the semaphore value. If processes exist that are suspended and are waiting for the semaphore value to be incremented, exactly one of these processes shall be permitted to decrement the semaphore value (the choice of process being non-deterministic) while the remaining processes shall suspend again.
  • int get_value() :Member function get_value shall return the semaphore value.
  • char* kind() : Return string "sc_semaphore"

Example : sc_semaphore

  1 #include 
  2 
  3 SC_MODULE (sc_semaphore_example) {
  4   sc_in<bool> clock;
  5 
  6   sc_semaphore   bus;
  7   int     cnt;
  8 
  9   void bus_semaphore() {
 10     while (true) {
 11       wait();
 12       cout << "@" << sc_time_stamp() <<" Check if semaphore is 0 " << endl;
 13       if (bus.get_value() == 0) {
 14         cout << "@" << sc_time_stamp() <<" Posting 2 to semaphore " << endl;
 15         bus.post();
 16         bus.post();
 17         if (cnt >= 3) {
 18           sc_stop(); // sc_stop triggers end of simulation
 19         }
 20         cnt ++;
 21       }
 22     }
 23   }
 24 
 25   void do_read() {
 26     while (true) {
 27       wait();
 28       cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 0"<<endl;
 29       // Check if semaphore is available
 30       if (bus.trywait()  ! = -1) {
 31         cout << "@" << sc_time_stamp() <<" Got semaphore for intance 0"<<endl;
 32         wait(2);
 33       }
 34     }
 35   }
 36 
 37   void do_write() {
 38     while (true) {
 39       wait();
 40       cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 1"<<endl;
 41       // Wait till semaphore is available
 42       bus.wait();
 43       cout << "@" << sc_time_stamp() <<" Got semaphore for intance 1"<<endl;
 44       wait(3);
 45     }
 46   }
 47 
 48   SC_CTOR(sc_semaphore_example) : bus(0){
 49     cnt = 0;
 50     SC_CTHREAD(do_read,clock.pos());
 51     SC_CTHREAD(do_write,clock.pos());
 52     SC_CTHREAD(bus_semaphore,clock.pos());
 53   }
 54 }; 
 55 
 56 int sc_main (int argc, char* argv[]) {
 57   sc_clock clock ("my_clock",1,0.5);
 58 
 59   sc_semaphore_example  object("semaphore");
 60     object.clock (clock.signal());
 61 
 62   sc_start(0); // First time called will init schedular
 63   sc_start();  // Run the simulation till sc_stop is encountered
 64   return 0;// Terminate simulation
 65 }

Simulation Output : sc_semaphore

@1 ns Check if semaphore is 0 
@1 ns Posting 2 to semaphore 
@1 ns Checking semaphore for intance 1
@1 ns Got semaphore for intance 1
@1 ns Checking semaphore for intance 0
@1 ns Got semaphore for intance 0
@2 ns Check if semaphore is 0 
@2 ns Posting 2 to semaphore 
@3 ns Check if semaphore is 0 
@4 ns Check if semaphore is 0 
@4 ns Checking semaphore for intance 0
@4 ns Got semaphore for intance 0
@5 ns Check if semaphore is 0 
@5 ns Checking semaphore for intance 1
@5 ns Got semaphore for intance 1
@6 ns Check if semaphore is 0 
@6 ns Posting 2 to semaphore 
@7 ns Check if semaphore is 0 
@7 ns Checking semaphore for intance 0
@7 ns Got semaphore for intance 0
@8 ns Check if semaphore is 0  
@9 ns Check if semaphore is 0 
@9 ns Checking semaphore for intance 1
@9 ns Got semaphore for intance 1
@10 ns Check if semaphore is 0 
@10 ns Posting 2 to semaphore 
@10 ns Checking semaphore for intance 0
@10 ns Got semaphore for intance 0
SystemC: simulation stopped by user.

   Bạn Có Đam Mê Với Vi Mạch hay Nhúng      -     Bạn Muốn Trau Dồi Thêm Kĩ Năng

Mong Muốn Có Thêm Cơ Hội Trong Công Việc

Và Trở Thành Một Người Có Giá Trị Hơn

Bạn Chưa Biết Phương Thức Nào Nhanh Chóng Để Đạt Được Chúng

Hãy Để Chúng Tôi Hỗ Trợ Cho Bạn. SEMICON  

Last Updated ( Tuesday, 29 March 2022 00:50 )  
Chat Zalo