Saturating Subtractor
Subtracting two signed numbers can overflow in both directions. A saturating subtractor pins the result to the most positive or most negative value instead of wrapping.
The module computes the difference between two 8-bit signed two's complement numbers, specifically a minus b. Because two's complement limits are asymmetric, the maximum positive value is +127 and the minimum negative value is -128. If the true mathematical result of the subtraction is greater than +127, the output must clamp to exactly +127. If the true mathematical result is less than -128, the output must clamp to exactly -128. In all other cases, the output is the exact difference.
This is a purely combinational circuit. There is no clock or reset.
| Signal | Direction | Width | Description |
|---|---|---|---|
a |
input | 8 | Signed two's complement minuend |
b |
input | 8 | Signed two's complement subtrahend |
diff |
output | 8 | Saturated signed difference |
Constraints
- The circuit must be purely combinational.
- The output must clamp to
8'h7F(+127) upon positive overflow. - The output must clamp to
8'h80(-128) upon negative overflow. - The module must correctly handle the asymmetric nature of 8-bit two's complement bounds.
Topics
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 9 of 27 in the Datapath Design track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks