Saturating counter

Hold the count at its limit even as more events arrive. Compare saturation and wraparound with the same event sequence, and specify how a completion signal behaves on reaching the limit and on later events.

The collector needs five work events. It should notify the controller on the fifth. Later events leave the count at 5, and the controller can still read that the work is complete.

At the limit, count holds at 5

Reset starts the count at zero. An event increments only while the old count is below 5. The 4-to-5 transition raises reached for one cycle. done reflects count equal to 5 and stays high while saturated.

E6 holds count at 5. The next event at E7 leaves count at 5 and done high.
An event at saturation leaves the count unchanged.

Represent the pulse and the held level separately

At a rising edge, reached checks the old count for 4 along with the current event. done follows the updated count. Synchronous reset takes priority and returns the count and both indicators to zero.

module saturating_counter (
    input  logic       clk,
    input  logic       rst_n,
    input  logic       event_i,
    output logic [2:0] count,
    output logic       reached,
    output logic       done
);
    assign done = (count == 3'd5);

    always_ff @(posedge clk) begin
        if (!rst_n) begin
            count <= 3'd0;
            reached <= 1'b0;
        end else begin
            reached <= event_i && (count == 3'd4);
            if (event_i && count < 3'd5)
                count <= count + 3'd1;
        end
    end
endmodule

Replay the wrapping counter’s event sequence

E0 resets. E1 through E9 use 111110110, and the first four events reach 4. The wrap column comes from the wrapping counter lab with those same events. E10 adds a reset sample to the saturation test.

Edgeevent_iSaturatingreacheddoneWrapping
E515115
E605015
E715010
E815011
E905011
E100000None
E5 first reaches 5. Reached stays high for one cycle while done stays high until E10 reset.
At the limit, idle and later event edges leave reached low.

Use reached to notify a controller on arrival. Use done so it can check completion later. State which timing the interface needs before wiring it to the state machine.

Replay the lab

Extract the executable lab on Linux or WSL and run bash run.sh. Icarus Verilog 12.0 checked 11 timed observations. The archive contains the RTL, testbench, log, VCD and generic Yosys structure. The test checks count, reached and done, including clearing at the last reset edge. Continue with F09 on counters, F10 on state machines.

Interactive lab

Reset, then bring count to 5. Compare the flags on arrival, an idle edge and another event.

Count
Just reached
Done level
Show the last eight observations

    Teaching film

    Read the transcript

    Run the RTL yourself

    Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

    Download Saturating counter RTL (.zip)

    Check your understanding

    1. Count is 4 and E5 has an event. What are count, reached and done after E5?
    2. E6 is idle at count 5. What are reached and done?
    3. At E7 another event arrives at the limit. What counts do saturation and wrap produce?
    4. Events continue indefinitely. When does the saturated count clear?
    5. An FSM needs to know whether the count is already complete. Which signal should it read?