SourceRTL Design Directory

Mod-N Counter: An Overview

A modulo-N counter (Mod-N) is counter that counts from 0 to N-1 and then wraps back to 0. This type of counter is useful in [Frequency Divider], timers and state machines. The number of flop-flop required to calculate a mod-N counter is ceil(log2(N)). Forexample: for a mod 10 counter the number of flip-flop required is 4 (ceil(log2(10))).

Verilog Code: Mod-N Counter

module mod_cntr #(parameter N = 4)(/*AUTOARG*/
   // Outputs
   mod_cntr,
   // Inputs
   clk, rst
   );
   // Outputs
   output [N-1:0] mod_cntr;
   // Inputs
   input	  clk;
   input	  rst;

   logic [$clog2(N)-1:0] cntr_reg, cntr_nxt;

   /*AUTOREG*/
   /*AUTOWIRE*/

   always_ff@(posedge clk)
     if(rst)
       cntr_reg <= 0;
     else
       cntr_reg <= cntr_nxt;

   always_comb
     cntr_nxt = (cntr_reg == N-1) ? 0 : cntr_reg + 1;

   assign mod_cntr = cntr_reg;

endmodule