gpt4 book ai didi

while-loop - SystemC 中的线程和时钟线程

转载 作者:行者123 更新时间:2023-12-04 04:47:59 26 4
gpt4 key购买 nike

在阅读 SystemC 中的线程时,据说 while(true) loop 必须在函数内部使用。为什么会这样?

你能看看下面给出的示例代码并解释为什么while循环用于线程和wait()命令与循环一起使用:

 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

最佳答案

SC_THREADSC_CTHREAD应该有无限循环以保持线程不被终止。如果你不放 for(;;)while(true)在函数体中,当执行到达函数作用域的末尾时,线程被终止。在这种情况下,您的线程永远不会被敏感列表唤醒以处理某些事情。或者您可以将其转换为等效的 SC_METHOD ,那么你就没有无限循环了。

SystemC 使用的是非抢占式线程,所以如果你不使用 wait()等待静态或动态敏感列表中列出的事情发生,它会导致您的线程无限执行。并且您的进程的 CPU 执行不会超出函数范围。这样 SystemC 内核将无法继续处理其他方法/线程和事件以继续模拟。而在基于事件的模拟中,线程应该只在指定条件(敏感列表中的事件)发生时运行。所以wait()函数可以等到下一个事件发生并将 CPU 执行移交给其他线程/方法和 SystemC 内核。下次事件发生时,例如时钟的上升沿,wait()语句将返回并继续您的过程。

在您的示例中,您的程序正在等待时钟的上升沿触发将计数器的值增加 1。因此,如果您不输入 wait()在您的代码中,然后 incr_count无论时钟的状态如何,计数器的值总是会无限增加,并且不会停止。通过使用 wait() ,您的线程将被阻塞并等待下一个时钟正沿触发。

关于while-loop - SystemC 中的线程和时钟线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com