Codiode

Parameterized Saturating Adder

MediumVerilog / SystemVerilogBuild

In audio and video, an overflowing sum should stick at the maximum. Wrapping around flips the sign and turns a loud sample into a loud click.

The module computes the sum of two signed, two's-complement integers. If the true mathematical sum exceeds the maximum positive value representable by the bit width, the output clamps to that maximum positive positive value. If the true sum falls below the minimum negative value, the output clamps to that minimum negative value. Otherwise, the output is the exact sum.

The module is purely combinational. There is no clock or reset. The output sum must reflect the saturated addition of inputs a and b at all times. Priority rules do not apply as all inputs are evaluated concurrently.

Worked Trace (WIDTH = 8):

  • Case 1: a = 8'h32 (50), b = 8'h14 (20) → sum = 8'h46 (70)
  • Case 2: a = 8'h64 (100), b = 8'h32 (50) → True sum is 150. Clamps to max positive. sum = 8'h7F (127)
  • Case 3: a = 8'h9C (-100), b = 8'hCE (-50) → True sum is -150. Clamps to min negative. sum = 8'h80 (-128)
Parameter Type Default Description
WIDTH integer 8 Bit width of the operands and result
Signal Direction Width Description
a input WIDTH First signed operand
b input WIDTH Second signed operand
sum output WIDTH Saturated signed sum

Constraints

  • The design must be purely combinational.
  • The design must strictly use the WIDTH parameter to determine saturation boundaries.
  • Operands and output must be treated as signed two's-complement integers.
  • Overflow occurs when two positive numbers yield a negative sum.
  • Underflow occurs when two negative numbers yield a positive sum.

Topics

Combinational LogicDatapathDSP

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 Datapath Design

Problem 25 of 27 in the Datapath Design track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

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