本文为英文版本翻译,原文地址:Process: Method
方法(Method)
- 可以使用静态敏感元
- 可通过调用
next_trigger
创建动态敏感元 - 不能响应自身的即时通知
cannot be made runnable as a result of an immediate notification
executed by the process itself, regardless of the static sensitivity
or dynamic sensitivity of the method process instance.
next_trigger()
- 是类
class sc_module
和class sc_prim_channel
的成员函数,同时也是一个普通函数 - 可以通过以下方式调用:
a) 模型的成员函数中调用
b) 通道(channel)的成员函数中调用
c) 方法过程(SC_METHOD)调用的其它函数中调用
注意
- 方法过程中的局部变量在返回时会销毁;若要在方法过程中存取持久状态的数据,使用成员变量。
回忆下SC_METHOD
和SC_THREAD
的区别:
- SC_METHOD(func): 没有线程上下文;不消耗仿真时间;不能挂起;不能调用
wait()
- SC_THREAD(func): 有线程上下问;可能消耗仿真时间;可以挂起;可以调用
wait()
示例代码:
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(PROCESS) {
SC_CTOR(PROCESS) { // constructor
SC_THREAD(thread); // register a thread process
SC_METHOD(method); // register a method process
}
void thread() {
int idx = 0; // declare only once
while (true) { // loop forever
std::cout << "thread"<< idx++ << " @ " << sc_time_stamp() << std::endl;
wait(1, SC_SEC); // re-trigger after 1 s
}
}
void method() {
// notice there's no while loop here
int idx = 0; // 每次调用会重新声明
std::cout << "method" << idx++ << " @ " << sc_time_stamp() << std::endl;
next_trigger(1, SC_SEC);
}
};
int sc_main(int, char*[]) {
PROCESS process("process");
sc_start(4, SC_SEC);
return 0;
}
运行结果:
method0 @ 0 s # first triggered during initialization
thread0 @ 0 s # first triggered during initialization
method0 @ 1 s # re-triggered by next_trigger()
thread1 @ 1 s # re-triggered by wait()
method0 @ 2 s
thread2 @ 2 s
method0 @ 3 s # "idx" 不累加
thread3 @ 3 s # "idx" 累加