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.
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.
| Edge | event_i | Saturating | reached | done | Wrapping |
|---|---|---|---|---|---|
| E5 | 1 | 5 | 1 | 1 | 5 |
| E6 | 0 | 5 | 0 | 1 | 5 |
| E7 | 1 | 5 | 0 | 1 | 0 |
| E8 | 1 | 5 | 0 | 1 | 1 |
| E9 | 0 | 5 | 0 | 1 | 1 |
| E10 | 0 | 0 | 0 | 0 | None |
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
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)