2013-07-27 4 views
0

systemc의 쓰레드에 대해 읽는 동안 함수 내에서 while(true) 루프를 사용해야한다고합니다. 왜 그래야만하지?시스템 C의 쓰레드 및 클럭 스레드

1 //----------------------------------------------------- 
    2 // This is my second Systemc Example 
    3 // Design Name : first_counter 
    4 // File Name : first_counter.cpp 
    5 // Function : This is a 4 bit up-counter with 
    6 // Synchronous active high reset and 
    7 // with active high enable signal 
    8 //----------------------------------------------------- 
    9 #include "systemc.h" 
10 
11 SC_MODULE (first_counter) { 
12 sc_in_clk  clock ;  // Clock input of the design 
13 sc_in<bool> reset ;  // active high, synchronous Reset input 
14 sc_in<bool> enable;  // Active high enable signal for counter 
15 sc_out<sc_uint<4> > counter_out; // 4 bit vector output of the counter 
16 
17 //------------Local Variables Here--------------------- 
18 sc_uint<4> count; 
19 
20 //------------Code Starts Here------------------------- 
21 // Below function implements actual counter logic 
22 void incr_count() { 
23  // For threads, we need to have while true loop 
24  while (true) { 
25  // Wait for the event in sensitivity list to occure 
26  // In this example - positive edge of clock 
27  wait(); 
28  if (reset.read() == 1) { 
29   count = 0; 
30   counter_out.write(count); 
31  // If enable is active, then we increment the counter 
32  } else if (enable.read() == 1) { 
33   count = count + 1; 
34   counter_out.write(count); 
35  } 
36  } 
37 } // End of function incr_count 
38 
39 // Below functions prints value of count when ever it changes 
40 void print_count() { 
41  while (true) { 
42  wait(); 
43  cout<<"@" << sc_time_stamp() << 
44   " :: Counter Value "<<counter_out.read()<<endl; 
45  } 
46 } 
47 
48 // Constructor for the counter 
49 // Since this counter is a positive edge trigged one, 
50 // We trigger the below block with respect to positive 
51 // edge of the clock 
52 SC_CTOR(first_counter) { 
53  // Edge sensitive to clock 
54  SC_THREAD(incr_count); 
55  sensitive << clock.pos(); 
56  // Level Sensitive to change in counter output 
57  SC_THREAD(print_count); 
58  sensitive << counter_out; 
59 } // End of Constructor 
60 
61 }; // End of Module counter 

답변

1

SC_THREAD 또는 SC_CTHREAD가 무한 루프가 있어야합니다

당신은 아래의 예제 코드를 참조하십시오 및 명령 루프와 함께 사용) (while 루프가 스레드에 사용되는 이유를 설명하고 대기 할 수 스레드가 종료되지 않게하십시오. for(;;) 또는 while(true)을 함수 본문에 넣지 않으면 함수 범위의 끝에 도달하면 스레드가 종료됩니다. 이 경우 스레드는 중요한 목록에 의해 처리되어 절대로 처리되지 않습니다. 또는 동일한 SC_METHOD로 변환 할 수 있습니다. 그러면 무한 루프를 수행 할 수 없습니다. SystemC는 비 선점 스레드를 사용하므로 wait()을 사용하여 정적 또는 동적 민감 목록에서 어떤 일이 발생하기를 기다리지 않으면 스레드에서 무한 실행이 발생합니다. 그리고 프로세스의 CPU 실행은 기능 범위를 벗어나지 않습니다. 따라서 SystemC 커널은 시뮬레이션을 계속하기 위해 다른 메소드/스레드 및 이벤트를 계속 처리 할 수 ​​없습니다. 그리고 이벤트 기반 시뮬레이션에서 스레드는 지정된 조건 (중요한 목록의 이벤트)이 발생했을 때만 실행되어야합니다. 따라서 wait() 함수는 다음 이벤트가 발생했는지 감시하고 CPU 실행을 다른 스레드/메소드 및 SystemC 커널로 넘겨줍니다. 다음 번에 이벤트가 발생합니다 (예 : 시계의 양수). wait() 문이 반환되고 처리가 계속됩니다. 예에서 프로그램은 시계의 포지티브 에지 트리거가 카운터 값을 1 씩 증가시킬 때까지 기다리고 있습니다. 코드에 wait()을 넣지 않으면 incr_count은 시계의 상태에 관계없이 항상 카운터 값을 무한대로 늘립니다. 그리고 그것은 멈추지 않을 것입니다. wait()을 사용하면 스레드가 차단되고 다음 클럭 포지티브 에지 트리거를 기다립니다. >는 컨텍스트에게
대기()를 얻을 때 실행 유지할 수 있도록, 무한 스레드 수 - -

0

동안 (사실은)>는 문맥을 잃고 스레드를 강제로 뭔가 다른

을 실행해야 할 수 있습니다