本文为英文版本翻译,原文地址:Signal: read and write
sc_signal:
- 用于对一组数字线路进行模拟建模
- 通过
evaluate-update
以确保在同时读写操作的情况下具有确定性行为,使用者维护当前值及新值。 - 若新值与当前值不同,
write()
会提交一个更新请求(update request)
- i实现接口:
sc_signal_inout_if<T>
Constructors(构造函数):
- sc_signal(): 通过初始化列表调用基类构造函数:
sc_prim_channel(sc_gen_unique_name("signal"))
- sc_signal(const char* name_): 通过初始化列表调用基类构造函数:
c_prim_channel(name_)
Member functions(成员函数):
- T& read() or operator const T& (): 返回信号的当前值引用,不修改信号的状态
- void write(const T&): 修改信号的值,以使其在下一个增量周期后可获取新值
(read)
- operator=: 同
write()
- sc_event& default_event(), sc_event& value_changed_event(): 返回值变化事件的引用
- bool event(): 仅当信号的值在紧接前一个增量周期的更新阶段发生变化且在当前仿真时间发生变化时,返回true
As compared to fifo(与fifo对比):
- sc_signal仅有一个读写
(read/write)
槽位置 - sc_signal仅在新值与当前值不同时才出发更新请求
(update request)
- 读
(read)
操作不会删除信号值
Other than execution phase, a sc_signal:
- 在阐释阶段可写入以初始化信号值
- 在阐释阶段或仿真暂停时,即在调用函数 sc_start 之前或之后,可以在函数 sc_main 写入
示例代码:
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
SC_MODULE(SIGNAL) {
sc_signal<int> s;
SC_CTOR(SIGNAL) {
SC_THREAD(readwrite);
}
void readwrite() {
s.write(3);
std::cout << "s = " << s << "; " << s.read() << std::endl;
wait(SC_ZERO_TIME);
std::cout << "after delta_cycle, s = " << s << std::endl;
s = 4;
s = 5;
int tmp = s;
std::cout << "s = " << tmp << std::endl;
wait(SC_ZERO_TIME);
std::cout << "after delta_cycle, s = " << s.read() << std::endl;
}
};
int sc_main(int, char*[]) {
SIGNAL signal("signal");
signal.s = -1;
sc_start();
return 0;
}
运行结果:
s = -1; -1 # read() or operator = both returns current value of signal
after delta_cycle, s = 3 # new value replaces current value after a delta cycle
s = 3 # read the current value
# new value replaces current value; if multiple new values are written, only the last one remains
after delta_cycle, s = 5