Learn SystemC - Process: Method

本文为英文版本翻译,原文地址:Process: Method

方法(Method)

  1. 可以使用静态敏感元
  2. 可通过调用next_trigger创建动态敏感元
  3. 不能响应自身的即时通知

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()

  1. 是类class sc_moduleclass sc_prim_channel的成员函数,同时也是一个普通函数
  2. 可以通过以下方式调用:
    a) 模型的成员函数中调用
    b) 通道(channel)的成员函数中调用
    c) 方法过程(SC_METHOD)调用的其它函数中调用

注意

  1. 方法过程中的局部变量在返回时会销毁;若要在方法过程中存取持久状态的数据,使用成员变量。

回忆下SC_METHODSC_THREAD的区别:

  1. SC_METHOD(func): 没有线程上下文;不消耗仿真时间;不能挂起;不能调用wait()
  2. 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" 累加
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值