verilog计算某个时钟信号clk_dut的频率[附源代码]

来源:互联网 发布:开发者没有网络adb调试 编辑:程序博客网 时间:2024/04/24 00:04

  • 背景
  • 算法
    • 注意
  • 源代码

背景

SOC系统顶层验证的时候,会关注各个时钟信号的频率。
如果一一查看波形,然后用计算器计算频率。缺点很多,不是好的做法。
搞一个模板,以后可以用。

算法

假设待测时钟信号为clk_dut;期望计算出clk_dut的频率为freq_dut。怎么搞?

解:
利用已知时钟clk_1G为1000MHz,计算仿真时间内该频率下的计数器值;根据待测时钟clk_dut下的计数器值与clk_1G下的计数器值的比值,就可以得出freq_dut答案。
公式:

仿真时间(复位释放之后的仿真时间)=`freq_dut`*`counter_dut`=`freq_clk_1G`*`counter_1G`

注意:

  1. 要在clk_dutclk_1G对应的复位信号全部释放之后,才开始计数。
  2. 如果是PLL的话,还要注意PLL处于锁定lock状态后,才开始计数。

源代码

   `timescale 1ns/10ps   //////////////////////////////////////////////////////   reg        clk_dut;   reg [63:0]   counter_dut;   initial begin      clk_dut=1'b0;   end   always #5   clk_dut = ~clk_dut;   always @(posedge clk_dut or negedge rstn) begin      if(~rstn) counter_dut=64'h0;      else counter_dut = counter_dut + 1'b1;   end   ////////////////////////////////////////////////////   reg        clk_1G;   reg [63:0] counter_1G; //bit-width is 64;prevent counter_1G overflow   initial begin      clk_1G=1'b0;   end   always #1   clk_1G = ~clk_1G;   always @(posedge clk_1G or negedge rstn) begin      if(~rstn) counter_1G=64'h0;      else counter_1G = counter_1G + 1'b1;   end   //////////////////////////////////   assign freq_dut = 1000/(counter_1G/counter_dut);//unit is MHz
0 0
原创粉丝点击