本文为英文版本翻译,原文地址:Initialization
初始化(Initialization)
初始化在sc_start()
后调用,属于执行阶段(execution stage)。初始化按照下列顺序进行3个操作:
- 运行更新阶段(update phase),但不做delta notifaction阶段
- 添加除下面两种其他的方法,过程到可运行过程:
a) 设置了dont_initialize
的过程实例
b) CTHREAD实例(clocked thread processes) - 运行delta notification phase. delta notification阶段结束时, 进行evaluation 阶段.
NOTE
- 由于
update requests
可能在阐述阶段(elaboration)创建并用于设置primitive channels的初始值,因此update and delta notification
是必需的。例如sc_inout
的初始化。 - SystemC 1.0版本:
a)thread processes
在仿真的初始化阶段(initializaiton phase)不执行
b)method processes
如果设置对输入信号/端口敏感,那么它们在仿真的初始化阶段(initializaiton phase)会执行 - SystemC 2.0 在仿真的初始化阶段执行所有的
thread processes
和method processes
If the behavior of a thread process is different between SystemC 1.0 and SystemC 2.0, insert one wait() statement before the infinite loop of the thread process. - 在初始化阶段,仿真过程(SC_METHODs in SystemC 1.0; SC_METHODs and SC_THREADs in SystemC 2.0)执行顺序不确定
- dont_initialize(): 阻止调度器在初始化阶段执行
thread processes
,method processes
示例代码
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(INITIALIZATION) {
sc_event e; // 用于仿真过程间触发
SC_CTOR(INITIALIZATION) {
SC_THREAD(trigger); // no static sensitivity
SC_THREAD(catcher_1); // no static sensitivity
SC_THREAD(catcher_2); // no static sensitivity
SC_THREAD(catcher_3);
sensitive << e; // statically sensitive to e
dont_initialize(); // catcher_3初始化阶段不执行
}
void trigger() {
while (true) { // e triggered at 1, 3, 5, 7 ...
e.notify(1, SC_SEC); // notify after 1 s
wait(2, SC_SEC); // trigger every 2 s
}
}
void catcher_1() {
while (true) {
std::cout << sc_time_stamp() << ": catcher_1 triggered" << std::endl;
wait(e); // dynamic sensitivity
}
}
void catcher_2() {
wait(e); // avoid initialization --- mimic systemC 1.0 behavior
while (true) {
std::cout << sc_time_stamp() << ": catcher_2 triggered" << std::endl;
wait(e); // dynamic sensitivity
}
}
void catcher_3() { // relies on dont_initialize() to avoid initialization
while (true) {
std::cout << sc_time_stamp() << ": catcher_3 triggered" << std::endl;
wait(e); // dynamic sensitivity
}
}
};
int sc_main(int, char*[]) {
INITIALIZATION init("init");
sc_start(4, SC_SEC);
return 0;
}
运行结果:
# catcher_1 is executed during initialization, catcher_2 and catcher_3 have same behavior but are implemented differently
0 s: catcher_1 triggered # during initialization
1 s: catcher_3 triggered # e triggered, not executed during initialization
1 s: catcher_1 triggered # e triggered
1 s: catcher_2 triggered # e triggered, not executed during initialization
3 s: catcher_3 triggered # e triggered
3 s: catcher_2 triggered # e triggered
3 s: catcher_1 triggered # e triggered