shared_500us_tick.v 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. `timescale 1ns/1ps
  2. module shared_500us_tick #(
  3. parameter integer CLOCK_HZ = 25_000_000,
  4. parameter integer TICK_US = 500
  5. ) (
  6. input wire clk,
  7. input wire rst_n,
  8. output reg tick
  9. );
  10. // Split the product before multiplying so 25 MHz * 500 us does not
  11. // overflow a 32-bit Verilog integer before the microsecond conversion.
  12. localparam integer TICK_CLKS =
  13. (CLOCK_HZ / 1_000_000) * TICK_US +
  14. ((CLOCK_HZ % 1_000_000) * TICK_US) / 1_000_000;
  15. localparam integer COUNTER_BITS = (TICK_CLKS <= 1) ? 1 : $clog2(TICK_CLKS);
  16. /* verilator lint_off WIDTH */
  17. localparam [COUNTER_BITS-1:0] LAST_TICK = TICK_CLKS - 1;
  18. /* verilator lint_on WIDTH */
  19. reg [COUNTER_BITS-1:0] counter;
  20. always @(posedge clk or negedge rst_n) begin
  21. if (!rst_n) begin
  22. counter <= {COUNTER_BITS{1'b0}};
  23. tick <= 1'b0;
  24. end else if (counter == LAST_TICK) begin
  25. counter <= {COUNTER_BITS{1'b0}};
  26. tick <= 1'b1;
  27. end else begin
  28. counter <= counter + 1'b1;
  29. tick <= 1'b0;
  30. end
  31. end
  32. endmodule