// counter_mod.sv // Modulo-N counter with enable: counts 0, 1, ..., N-1, 0, ... // wrap is high in the cycle where the counter is enabled at N-1. // N must be at least 2. module counter_mod #( parameter int N = 10 ) ( input logic clk, input logic rst, input logic en, output logic [$clog2(N)-1:0] count, output logic wrap ); localparam int W = $clog2(N); assign wrap = en && (count == W'(N - 1)); always_ff @(posedge clk) begin if (rst) count <= '0; else if (wrap) count <= '0; else if (en) count <= count + W'(1); end endmodule