`timescale 1ns / 1ps module timer#( parameter CLK_FREQ = 100_000_000, parameter TIMES_PER_SECOND = 20 ) ( input wire iClk, iRst_n, output wire oQ ); // size of the instantiated counter localparam N = $clog2( (CLK_FREQ/TIMES_PER_SECOND)-1); // signal declaration reg [N-1:0] r_CntCurr; // the counter register wire [N-1:0] w_CntNext; // the input to the counter register // the counter register always @(posedge iClk) if (iRst_n==0) r_CntCurr <= 0; else if (r_CntCurr==(CLK_FREQ/TIMES_PER_SECOND)-1) r_CntCurr <= 0; else r_CntCurr <= w_CntNext; // the increment circuit (combinational) assign w_CntNext = r_CntCurr + 1; // generate the output of the timer // when the counter reaches its limit, we set it to 1 // otherwise we set it to 0 wire wCmp = (r_CntCurr==(CLK_FREQ/TIMES_PER_SECOND)-1); assign oQ = (wCmp) ? 1 : 0; endmodule