Parameterized Modulo N Counter
Real timebases are rarely a power of two: a baud rate divider or a PWM period might need to count to 100. A modulo-N counter wraps at whatever limit you set.
The module maintains a running count that increments every clock cycle when enabled. When the count reaches a configurable maximum value, it resets to zero on the following active clock edge. A tick pulse is generated combinatorially during the exact cycle the counter equals the maximum value, signaling to other modules that the terminal count has been reached.
- Clock edge: posedge
clk - Reset type: asynchronous active-low
rst_n - Output values on reset:
countgoes to 0;tickis evaluated combinatorially based oncountandmax_val - Priority rules: Reset has highest priority. If
enis low, the counter holds its current value without incrementing or wrapping.
Cycle 1: rst_n=0, en=0, max_val=2 → count=0, tick=0
Cycle 2: rst_n=1, en=1, max_val=2 → count=1, tick=0
Cycle 3: rst_n=1, en=1, max_val=2 → count=2, tick=1
Cycle 4: rst_n=1, en=0, max_val=2 → count=2, tick=1 (hold)
Cycle 5: rst_n=1, en=1, max_val=2 → count=0, tick=0 (wrap)
Cycle 6: rst_n=1, en=1, max_val=2 → count=1, tick=0
Cycle 7: rst_n=1, en=1, max_val=2 → count=2, tick=1
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive-edge triggered clock |
rst_n |
input | 1 | Asynchronous active-low reset; count goes to 0 when asserted |
en |
input | 1 | Enable; counter increments or wraps on posedge clk when high |
max_val |
input | WIDTH |
The terminal value of the counter |
count |
output | WIDTH |
Current count value; resets to 0 |
tick |
output | 1 | Combinational output; asserted high when count equals max_val |
Constraints
- The
tickoutput must be purely combinational, not registered. - The module must be parameterized with
WIDTHdefaulting to 8. - The counter must reset to 0 synchronously when
countequalsmax_valandenis high. - The counter must hold its value when
enis low, even if it is currently atmax_val. - Reset is asynchronous and active-low.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Sequential RTL
Problem 16 of 26 in the Sequential RTL track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks