简体   繁体   English

是否有更好/替代方法来实现 SystemC 模块中的延迟

[英]Is there a better/alternate way to implement delay in SystemC Modules

I have implemented a 4 bit adder with delay in its output port.我在其 output 端口中实现了一个 4 位加法器。

SC_MODULE(adder4){
  sc_in<sc_uint<4>>   A,B;
  sc_out<sc_uint<4>>  OUT;
  
  sc_event ev;

  sc_uint<4> val_a,val_b,val_s;
  
  void add(){
    val_a = A.read();
    val_b = B.read();
  
    val_s = val_a + val_b;
    ev.notify(2,SC_NS);
  }

  void write(){
    OUT.write(val_s);
    ev.cancel();
  }
  
  SC_CTOR(adder4){
    SC_METHOD(add);
    dont_initialize();
    sensitive<<A<<B;
    
    SC_METHOD(write);
    dont_initialize();
    sensitive<<ev;
  }
};

My question is:我的问题是:

  • Is there any better way to implement a delay in a method?有没有更好的方法来实现方法的延迟?
  • Is there any direct/indirect disadvantages of using the sc_event?使用 sc_event 是否有任何直接/间接的缺点?

This is an edited version of original answer that had an event dependency missing in the adder thread.这是原始答案的编辑版本,在加法器线程中缺少事件依赖项。 This code has that dependency added.此代码添加了该依赖项。

Better is subjective I guess and is always subject to "better under what criteria".我猜更好是主观的,并且总是受制于“在什么标准下更好”。 Here is an alternative method which is something like I would use.这是我会使用的另一种方法。 I say something like as it's all mushed into a bunch of templates but if you untangle them it comes out to something morally equivalent to the code below.我这样说是因为它们都被塞进了一堆模板中,但是如果你解开它们,它就会在道德上等同于下面的代码。

Since we are dealing with time I have added a system clock and a driver module to provide the inputs to the adder.由于我们正在处理时间,我添加了一个系统时钟和一个驱动模块来为加法器提供输入。 I have also traced the signals to a vcd file so we can inspect the waveforms for correct delay against the system clock.我还将信号跟踪到一个 vcd 文件,这样我们就可以检查波形是否有正确的系统时钟延迟。

#include <systemc.h>

using data_t = sc_uint<4>;
const auto delay = sc_time(2.0, SC_NS);

// Asynchronous adder with output propagation delay
SC_MODULE(adder4){
    sc_in<data_t>   A,B;
    sc_out<data_t>  OUT;

    void add(){

       while(true){
         wait(A.default_event() | B.default_event());
         auto sum = A.read() + B.read();  //calculate sum
         wait(delay);                     //wait delay
         OUT.write(sum);                  //write sum after delay
       }
    }

    SC_CTOR(adder4){
        SC_THREAD(add)  //thread instead of process         
    }
};

SC_MODULE(driver){
    unsigned a,b;                //internal data values
    sc_in<bool> clk_in;          //system clock  input
    sc_out<data_t> out_a, out_b; //driver data outputs

    void proc(){
        out_a.write(a);
        out_b.write(b);

        //change internal data to drive test device. just increment
        a += 1;
        b += 2;
    }
    SC_CTOR(driver){
        a = b = 0;
        SC_METHOD(proc);
        sensitive << clk_in.pos();
        dont_initialize();
    }
};

using signal_t = sc_signal<data_t>;

int sc_main(int, char**){
    sc_clock clk("clk", 5, SC_NS); //system clock
    driver drv("driver");          //test case driver
    adder4 adder("adder");         //device under test

    //connect devices
    signal_t s1, s2, s3;
    drv.clk_in(clk);
    drv.out_a(s1);
    drv.out_b(s2);
    adder.A(s1);
    adder.B(s2);
    adder.OUT(s3);
    
    //trace signals to waveforms.vcd
    sc_trace_file *tf = sc_create_vcd_trace_file("waveforms");
    sc_trace(tf, clk, "clk");
    sc_trace(tf, s1, "A");
    sc_trace(tf, s2, "B");
    sc_trace(tf, s3, "OUT");

    sc_start(40, SC_NS);

    sc_close_vcd_trace_file(tf);

    return 0;
}

Executing this generates the vcd file "waveforms.vcd" which I have displayed in GTKViewer, an image of which is shown below.执行此操作会生成我在 GTKViewer 中显示的 vcd 文件“waveforms.vcd”,其图像如下所示。 It can be clearly seen that the adder output is delayed by 2ns.可以清楚的看到加法器output延迟了2ns。 Is this "the best" or "better"?这是“最好的”还是“更好的”? I can't say but it is, more or less, a standard(ish) method of meeting your requirements.我不能说,但它或多或少是满足您要求的标准(ish)方法。 Leastwise it fulfills the requirement of being an alternative method as you asked.至少它满足了您要求的替代方法的要求。 There are surely countably infinite ways of achieving the same result but, as I say, it's a standard(ish) method.肯定有无数种方法可以达到相同的结果,但正如我所说,这是一种标准(ish)方法。

GTKViewer 系统 Wavefroms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM