简体   繁体   English

每 n 个时钟周期递增一次 mod 计数器

[英]incrementing mod counter every n clock cycle

This is the verilog code for mod 64 counter, incrementing every clock cycle这是 mod 64 计数器的 verilog 代码,每个时钟周期递增

module modulus64counter
#(parameter N=64,
parameter WIDTH=5)
(input clk,
input rstn,
output reg[WIDTH-1:0] out);
integer i;
always @(posedge clk) begin
if(!rstn) begin
out<=0;
end
else begin
if(out==N-1)
out<=0;
else 
out<= out+1;
end
end
endmodule

and the test bench is测试台是

module modulus64countertb;

    // Inputs
    reg clk;
    reg rstn;

    // Outputs
    wire [4:0] out;

    // Instantiate the Unit Under Test (UUT)
    modulus64counter uut (
        .clk(clk), 
        .rstn(rstn), 
        .out(out)
    );
    always #10 clk = ~clk;
    initial begin
        // Initialize Inputs
        clk = 1;
        rstn = 0;

        $monitor ("T=%0t rstn=%0b out=0X%h", $time,rstn,out);
repeat(2) @(posedge clk);
rstn <=1;
repeat(50) @(posedge clk);
$finish;
end
endmodule

Now if i want to increment the value of out every "n" clock cycle instead of consecutive clock cycle, how can i modify the program现在,如果我想增加每个“n”时钟周期而不是连续时钟周期的值我该如何修改程序

Kindly help请帮助

The code below should work for you.下面的代码应该适合你。 You can always swap the out and actual_out if you insist on using out as the final counting variable.如果您坚持使用out作为最终计数变量,您可以随时交换outactual_out

Also, removing the out on the monitor line in the testbench will only print the value when it reaches mod n .此外,删除测试台中monitor线上的out只会在达到mod n时打印该值。 I kept both out and actual_out on testbench's monitor to ease debugging purpose.我在测试台的monitor上保留了outactual_out以简化调试目的。

Verilog code Verilog 代码

module modulus64counter #(
    parameter               N=64,
    parameter               WIDTH=8
)(
    input                   clk,
    input                   rstn,
    output reg[WIDTH-1:0]   out,
    output reg[WIDTH-1:0]   actual_out
);
    integer                 i;
    reg       [WIDTH-1:0]   cntr;
    
    always @(posedge clk) begin
        if(!rstn) begin
            out <= 0;
            actual_out <= 0;
        end else if(out == N-1) begin
            out <= 0;
            actual_out <= actual_out + 1;
        end else begin
            out <= out + 1;
        end
    end
endmodule

Testbench试验台

module modulus64countertb;

    // Inputs
    reg         clk;
    reg         rstn;

    // Outputs
    wire [7:0]  out;
    wire [7:0]  actual_out;

    // Instantiate the Unit Under Test (UUT)
    modulus64counter uut (
        .clk(clk), 
        .rstn(rstn), 
        .out(out),
        .actual_out(actual_out)
    );
  
    always #10 clk = ~clk;
    initial begin
        // Initialize Inputs
        clk  = 1;
        rstn = 0;

        $monitor ("T=%0t rstn=%0b out=%d actual_out=%d", $time,rstn,out,actual_out);
        repeat(2) @(posedge clk);
        rstn <=1;
        repeat(200) @(posedge clk);
        $finish;
    end
endmodule

Output result simulated using edaplayground :使用edaplayground模拟的 Output 结果: 在此处输入图像描述

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

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