Saturating Counters
Part of the RTL Design & Verification section of Coddy's Verilog journey. Lesson 17 of 38.
Fundamentals used a wrapping counter. A saturating counter instead stops at a boundary, which is useful for bounded counts. Add an explicit guard to the arithmetic update and keep enable and reset behavior independent of the limit.
Relevant excerpt; surrounding declarations and connections are supplied in the challenge.
always @(posedge clk) begin
if(rst) count <= 0;
else if(en && count != 15) count <= count + 1;
endThe maximum value is retained rather than wrapping to zero.
Guard boundary updates explicitly when a counter must saturate instead of wrap.
Challenge
MediumCreate a 4-bit saturating counter q. Synchronous rst clears it. Enabled edges increment until 15, then hold at 15; disabled edges also hold. Reset has priority. Output columns: q. All controls and data are stable before each rising clock edge; results are observed afterward.
Complete design.v and preserve its module names and ports. The locked testbench.v supplies input changes and prints the outputs after they settle. It chooses a scenario using a simulator argument such as +CASE=1; no standard input is required. Do not add printing, delays or simulation termination to the design. Expected output is one row of decimal values per observation, separated by one space and ending with a newline.
Try it yourself
module dut (input clk, input rst, input en, output reg [3:0] q);
// Replace this placeholder with your design.
initial q = 0;
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in RTL Design & Verification
1Reliable Combinational RTL
Defaults Prevent LatchesPriority EncodersOne-Hot ValidationSafe Case DecodingRecap - Request Router4Controlled Sequential RTL
Reset and Enable PrioritySaturating CountersPipelining Data and ValidDetecting Sampled EdgesRecap - Event CounterPractice on your own: Online Verilog compiler