Learn SystemC - Event Queue & Combined Event Queue

本文为英文版本翻译,原文地址:
Event Queue
Combined Event Queue

事件队列(Event Queue)

事件队列特点:

  1. 和事件(event)一样有成员函数notify()
  2. 可以按层级挂起多个通知,而事件(event)只能调度一个待处理的通知
  3. 只能在阐述(elaboration)阶段构造
  4. 不支持即时通知

成员函数:

  1. void notify(double, sc_time_unit) or void notify(const sc_time&):
    a) 零时间zero time i.e. SC_ZERO_TIME: 增量通知
    b) 非零时间non-zero time: 当notify被调用时,通知将按照相对仿真时间被调度
  2. 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)

  1. 多个事件队列可使用“OR”操作作为仿真进程的静态敏感元,不能在镜头敏感元使用“AND”操作。
  2. 事件队列不能作为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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值