本文为英文版本翻译,原文地址:Event
事件
事件(event)是一个sc_event对象,用于仿真过程同步。
仿真过程可以通过事件触发。
sc_event包含以下方法:
void notify(): 创建一个即时通知(immediate notification)void notify(const sc_time&),void notify(double, sc_time_unit):
a) zero time: 创建一个delta notification.
b) non-zero time: 创建一个定时通知(timed notification)cancel(): 取消该事件挂起的通知(pending notification)
a) 事件最多有一个挂起的通知
b) 即时通知不能取消
限制
sc_event对象可以在阐述(elaboration)或仿真(simulation)阶段构造- 事件可以在阐述或仿真阶段通知,但是即时通知不能在阐述阶段及以下回调中创建:
a)before_end_of_elaboration
b)end_of_elaboration
c)start_of_simulation
挂起通知
一个事件最多只能有一个挂起通知(pending notification)
- 如果已经有挂起通知的事件调用
notify,只有最临近当前时间的通知生效 - 较晚时间发生的通知会取消(或者不会被安排)
- 即时通知(immediate notification)比delta notification发生时间早, delta notification比定时通知(timed notification)发生时间早,这和调用顺序无关
事件可以相互组合,以及和定时器组合。
下面是只等待一个事件的示例:
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(EVENT) {
sc_event e; // 声明一个时间
SC_CTOR(EVENT) {
SC_THREAD(trigger); // register a trigger process
SC_THREAD(catcher); // register a catcher process
}
void trigger() {
while (true) { // infinite loop
e.notify(1, SC_SEC); // 1秒后触发
if (sc_time_stamp() == sc_time(4, SC_SEC)) {
e.cancel(); // 当时间为4秒时,取消事件
}
wait(2, SC_SEC); // 等待2秒
}
}
void catcher() {
while (true) { // loop forever
wait(e); // wait for event
std::cout << "Event cateched at " << sc_time_stamp() << std::endl; // print to console
}
}
};
int sc_main(int, char*[]) {
EVENT event("event"); // define object
sc_start(8, SC_SEC); // run simulation for 8 seconds
return 0;
}
运行结果:
Event cateched at 1 s # trigged at 0 s
Event cateched at 3 s # triggered at 2 s
# the event triggered at 4 s is cancelled
Event cateched at 7 s # triggered at 6 s
本文介绍了SystemC中的事件对象sc_event,如何创建即时、定时通知,以及事件的挂起和取消机制。通过示例展示了如何使用这些功能进行事件触发和捕获。
798

被折叠的 条评论
为什么被折叠?



