本文为英文版本翻译,原文地址:
Event Queue
Combined Event Queue
事件队列(Event Queue)
事件队列特点:
- 和事件(event)一样有成员函数
notify()
- 可以按层级挂起多个通知,而事件(event)只能调度一个待处理的通知
- 只能在阐述(elaboration)阶段构造
- 不支持即时通知
成员函数:
void notify(double, sc_time_unit)
orvoid notify(const sc_time&)
:
a) 零时间zero time i.e. SC_ZERO_TIME: 增量通知
b) 非零时间non-zero time: 当notify
被调用时,通知将按照相对仿真时间被调度void cancel_all()
: 立即删除该事件队列中挂起的通知,包括增量和定时通知
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(QUEUE) {
sc_event e;
sc_event_queue eq;
SC_CTOR(QUEUE) {
SC_THREAD(trigger);
SC_THREAD(catch_e);
sensitive << e; // catch_e() will be triggered by event e
dont_initialize(); // don't run cach_e() during initialization phase
SC_THREAD(catch_eq);
sensitive << eq; // cach_eq() will be triggered by event queue eq
dont_initialize(); // don't run catch_eq() during initialization phase
}
void trigger() {
while (true) {
e.notify(2, SC_SEC); // trigger e afer 2 s
e.notify(1, SC_SEC); // trigger e after 1 s, replaces previous trigger
eq.notify(2, SC_SEC); // trigger eq after 2 s
eq.notify(1, SC_SEC); // trigger eq after 1 s, both triggers available
wait(10, SC_SEC); // another round
}
}
void catch_e() {
while (true) {
std::cout << sc_time_stamp() << ": catches e" << std::endl;
wait(); // no parameter --> wait for static sensitivity, i.e. e
}
}
void catch_eq() {
while (true) {
std::cout << sc_time_stamp() << ": catches eq" << std::endl;
wait(); // wait for eq
}
}
};
int sc_main(int, char*[]) {
QUEUE queue("queue"); // instantiate object
sc_start(20, SC_SEC); // run simulation for 20 s
return 0;
}
运行结果:
1 s: catches e
# e scheduled at 2 s is not triggered
1 s: catches eq
2 s: catches eq # eq triggered at both 1 s and 2 s
11 s: catches e
# e scheduled at 12 s is not triggered
11 s: catches eq
12 s: catches eq
组合事件队列(Combined Event Queue)
- 多个事件队列可使用“OR”操作作为仿真进程的静态敏感元,不能在镜头敏感元使用“AND”操作。
- 事件队列不能作为
wait()
的输入,因此不能作为动态敏感元使用。
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(QUEUE_COMBINED) {
sc_event_queue eq1, eq2;
SC_CTOR(QUEUE_COMBINED) {
SC_THREAD(trigger);
SC_THREAD(catcher);
sensitive << eq1 << eq2; // eq1 "or" eq2, cannot "and"
dont_initialize();
}
void trigger() {
eq1.notify(1, SC_SEC); // eq1 at 1 s
eq1.notify(2, SC_SEC); // eq1 at 2 s
eq2.notify(2, SC_SEC); // eq2 at 2 s
eq2.notify(3, SC_SEC); // eq2 at 3 s
}
void catcher() {
while (true) {
std::cout << sc_time_stamp() << ": catches trigger" << std::endl;
wait(); // cannot use event queue in dynamic sensitivity
}
}
};
int sc_main(int, char*[]) {
QUEUE_COMBINED combined("combined");
sc_start();
return 0;
}
运行结果:
1 s: catches trigger # eq1
2 s: catches trigger # both eq1 and eq2
3 s: catches trigger # eq2