HDLbits
Start Practicing

Verilog Language/Procedures

Min/Max of Four Values

medium
proceduresalways-blockif-elsecomparators

Combinational always blocks aren’t limited to a single output — they can compute several related results together, sharing the same set of conditions. Finding both the min and max of a set of values is a common example.

Build a circuit with four 4-bit unsigned inputs a, b, c, d, and two 4-bit outputs min_val and max_val equal to the smallest and largest of the four inputs, respectively.

Interface

Signal Direction Width Description
a input 4 Unsigned operand
b input 4 Unsigned operand
c input 4 Unsigned operand
d input 4 Unsigned operand
min_val output 4 Smallest of a, b, c, d
max_val output 4 Largest of a, b, c, d

Notes

  • All comparisons are unsigned (values 0-15).
  • One approach: for max_val, check each input against all the others (a >= b && a >= c && a >= d) to find the one that is greater than or equal to every other input; the analogous approach with <= works for min_val.
  • If there are ties, any of the tied values is an acceptable choice — the output equals the matching value either way.