在从事 SystemC 项目时,我发现我可能对信号和端口有一些混淆的想法。假设我有这样的事情:
//cell.hpp
SC_MODULE(Cell)
{
sc_in<sc_uint<16> > datain;
sc_in<sc_uint<1> > addr_en;
sc_in<sc_uint<1> > enable;
sc_out<sc_uint<16> > dataout;
SC_CTOR(Cell)
{
SC_THREAD(memory_cell);
sensitive << enable << datain << addr_en;
}
private:
void memory_cell();
};
//cell.cpp
void Cell::memory_cell()
{
unsigned short data_cell=11;
while(true)
{
//wait for some input
wait();
if (enable->read()==1 && addr_en->read()==1)
{
data_cell=datain->read();
}
else
{
if(enable->read()==0 && addr_en->read()==1)
{
dataout->write(data_cell);
}
}
}
}
//test.cpp
SC_MODULE(TestBench)
{
sc_signal<sc_uint<1> > address_en_s;
sc_signal<sc_uint<16> > datain_s;
sc_signal<sc_uint<1> > enable_s;
sc_signal<sc_uint<16> > dataout_s;
Cell cella;
SC_CTOR(TestBench) : cella("cella")
{
// Binding
cella.addr_en(address_en_s);
cella.datain(datain_s);
cella.enable(enable_s);
cella.dataout(dataout_s);
SC_THREAD(stimulus_thread);
}
private:
void stimulus_thread() {
//write a value:
datain_s.write(81);
address_en_s.write(1);
enable_s.write(1);
wait(SC_ZERO_TIME);
//read what we have written:
enable_s.write(0);
address_en_s.write(1);
wait(SC_ZERO_TIME);
cout << "Output value: " << dataout_s.read() << endl;
//let's cycle the memory again:
address_en_s.write(0);
wait(SC_ZERO_TIME);
cout << "Output value: " << dataout_s.read() << endl;
}
};
我试过运行这个模块,我注意到一些奇怪的东西(至少,对我来说很奇怪):当刺激写入一个值(81)时,在wait(SC_ZERO_TIME)
内存线程找到它之后datain
,enable
并且address_enable
值已经更新。这是我预期会发生的。当刺激改变enable_es
值时也会发生同样的情况,以便在内存线程中运行另一个循环并将data_cell
值复制到内存单元dataout
端口中。我不明白的是为什么在内存模块写入其dataout
端口并再次进入wait()
while循环开头的语句后,刺激模块仍然具有其旧值dataout_s
通道 (0),而不是刚刚被内存模块复制的新值 (81)。然后,如果我运行另一个内存循环周期(例如更改激励通道上的一些值),数据输出通道最终会更新。
换句话说,如果我写入刺激通道然后切换到内存线程,内存会发现更新的值。但是如果内存线程写入它的端口,然后我切换到激励线程,线程仍然会在其通道上看到旧值(绑定到内存端口)。