counter.v 437 B

12345678910111213141516171819202122
  1. module counter #(parameter CYCLES_SEC = 100_000_000) (
  2. input clk,
  3. input reset_n,
  4. output reg toggled
  5. );
  6. parameter COUNT_BITS = $clog2(CYCLES_SEC - 1);
  7. reg [COUNT_BITS - 1 : 0] count;
  8. wire overflow = (count == CYCLES_SEC - 1);
  9. always @(posedge clk) begin
  10. if (reset_n == 1'b0) begin
  11. count <= 0;
  12. toggled <= 1'b0;
  13. end else begin
  14. count <= overflow ? 0 : count + 1;
  15. toggled <= overflow ? ~toggled : toggled;
  16. end
  17. end
  18. endmodule