Codiode

Up Down Counter with Saturation

MediumVerilog / SystemVerilogBuild

A FIFO level counter that wraps from full back to zero reports a full FIFO as empty. A saturating counter stops at its limits instead.

This module tracks an 8-bit value based on directional control signals. It increments when the up signal is asserted and decrements when the down signal is asserted. To prevent overflow and underflow, the counter saturates. It halts at 255 when counting up and halts at 0 when counting down. If both control signals are asserted simultaneously, or if neither is asserted, the current value is held.

The circuit is driven by a positive-edge triggered clock. It features an asynchronous, active-low reset that clears the count to zero. All outputs are registered.

Cycle-by-cycle behaviour:
Cycle 1: rst_n=0 → count=0
Cycle 2: rst_n=1, up=0, down=1 → count=0 (saturates at minimum)
Cycle 3: rst_n=1, up=1, down=0 → count=1
Cycle 4: rst_n=1, up=1, down=1 → count=1 (hold due to simultaneous inputs)
Cycle 5: rst_n=1, up=1, down=0 → count=2
Cycle 6: rst_n=1, up=0, down=1 → count=1
Cycle 7: rst_n=1, up=0, down=0 → count=1 (hold due to no inputs)
Cycle 8: rst_n=1, up=1, down=0 → count=2

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; clears count to 8'b00000000
up input 1 Increment command
down input 1 Decrement command
count output 8 Current counter value; saturates at 8'hFF and 8'h00

Constraints

  • The module must trigger on the positive edge of clk.
  • The reset must be asynchronous and active-low.
  • The counter must not wrap around under any circumstances; it must saturate at 255 for the maximum limit and 0 for the minimum limit.
  • If up and down are both 1, the counter must hold its current value.
  • If up and down are both 0, the counter must hold its current value.

Topics

sequentialcountersaturation

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 17 of 26 in the Sequential RTL track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.