【问题标题】:Number of Clock Cycles between events in SVSV 中事件之间的时钟周期数
【发布时间】:2018-03-02 22:00:58
【问题描述】:

我想计算信号高值和低值之间的时钟周期数。我有一个信号 SIG,它在模拟过程中多次变高和变低。我想计算高/低时的平均周期数。

有人可以指导我如何实现它。如果您需要更多说明,请告诉我。

【问题讨论】:

  • 您是在问如何构建进行此计算的硬件,还是只是想在模拟中分析信号?
  • 分析信号。我想编写一个只捕获写信号和时钟的逻辑。并计算平均每次写入/读取需要多少个时钟周期。

标签: system-verilog clock


【解决方案1】:

您可以有一个时钟周期计数器,当信号SIG 为高电平时开始计数,并在SIG 变为低电平时停止计数,以便在SIG = " 时测量时钟周期数1":

int counter = 0; // counter initialization
@(posedge SIG); // waits for SIG to go high
while(SIG == 1) begin // while SIG = "1"
      @(posedge CLK); // when clock signal gets high
      counter++; // increase counter by 1
end

SIG = "0",@(negedge SIG); 也可以这样做。

【讨论】:

    【解决方案2】:

    您可以做的是计算每个值的高/低周期数并将它们放入队列中。然后在终点取平均值。

    module top;
       bit sig, clk;
    
       int hiCount[$]={0}, loCount[$]={0};
    
       always #5 clk++;
    
       always @(negedge clk) // 5:1 chance of keeping the same value
         randomize(sig) with {sig == const'(sig) dist { 1 := 5, 0 := 1 }; };
    
       always @(posedge clk) begin
          case(sig)
        1: if ($stable(sig))
          hiCount[$]++;
        else
          hiCount.push_back(1); // first cycle high
        0: if ($stable(sig))
          loCount[$]++;
        else
          loCount.push_back(1); // first cycle low
          endcase // case (sig)
          $display("sig: %0d\nhi:%p\nlo: %p",sig,hiCount,loCount);
          end
       initial begin
          repeat(100) @(posedge clk);
          if (loCount[0] == 0) void'(loCount.pop_front());
          if (hiCount[0] == 0) void'(hiCount.pop_front());
    
          $display("sig was high %0d times with average cycle count of %0d",
               hiCount.size(), hiCount.sum()/hiCount.size);
                $display("sig was low %0d times with average cycle count of %0d",
               loCount.size(), loCount.sum()/loCount.size);
          $finish;
       end
    endmodule // top
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-04-01
      • 2017-03-12
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多