【问题标题】:SystemC: channels vs port value updateSystemC:通道与端口值更新
【发布时间】:2014-10-08 04:58:36
【问题描述】:

在从事 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)之后内存线程找到它的datainenableaddress_enable 值已更新。这是我预期会发生的。当刺激改变enable_es 值时也会发生同样的情况,以便在内存线程中运行另一个循环并将data_cell 值复制到内存单元dataout 端口中。我不明白为什么在内存模块写入其dataout 端口并在while 循环开始时再次进入wait() 语句后,刺激模块在其dataout_s 通道上仍然具有旧值(0),而不是刚刚被内存模块复制的新值(81)。然后,如果我运行另一个内存循环循环(例如更改激励通道上的一些值),数据输出通道最终会更新。

换句话说,如果我写入刺激通道然后切换到内存线程,内存会发现更新的值。但是如果内存线程写入它的端口,然后我切换到激励线程,线程仍然会在其通道上看到旧值(绑定到内存端口)。

【问题讨论】:

    标签: multithreading wait ports systemc channels


    【解决方案1】:

    由于错误的增量周期同步,上面的示例无法正常工作。

    一般来说,假设我们有两个线程在两个模块上运行,A 和 B,通过一个通道连接。如果我在 delta 周期 1 期间在线程 A 中写了一些东西,它只会在 delta 周期 2 期间在线程 B 中可用。如果线程 B 在其 delta 周期 2 期间写了一些东西,线程 A 必须等到 delta 周期 3 才能读取它。

    意识到这一点,刺激线程需要两个连续的wait(SC_ZERO_TIME) 语句才能从内存中读取正确的输出,因为它必须转发其增量值。

    【讨论】:

      猜你喜欢
      • 2017-06-15
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多