| 12345678910111213141516171819202122232425262728293031323334 |
- `timescale 1ns/1ps
- module shared_500us_tick #(
- parameter integer CLOCK_HZ = 25_000_000,
- parameter integer TICK_US = 500
- ) (
- input wire clk,
- input wire rst_n,
- output reg tick
- );
- // Split the product before multiplying so 25 MHz * 500 us does not
- // overflow a 32-bit Verilog integer before the microsecond conversion.
- localparam integer TICK_CLKS =
- (CLOCK_HZ / 1_000_000) * TICK_US +
- ((CLOCK_HZ % 1_000_000) * TICK_US) / 1_000_000;
- localparam integer COUNTER_BITS = (TICK_CLKS <= 1) ? 1 : $clog2(TICK_CLKS);
- /* verilator lint_off WIDTH */
- localparam [COUNTER_BITS-1:0] LAST_TICK = TICK_CLKS - 1;
- /* verilator lint_on WIDTH */
- reg [COUNTER_BITS-1:0] counter;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- counter <= {COUNTER_BITS{1'b0}};
- tick <= 1'b0;
- end else if (counter == LAST_TICK) begin
- counter <= {COUNTER_BITS{1'b0}};
- tick <= 1'b1;
- end else begin
- counter <= counter + 1'b1;
- tick <= 1'b0;
- end
- end
- endmodule
|