Learn
Practice
Sign Extension and Overflow Flags
A temperature sensor delivers 4-bit two's complement readings, but the processing datapath is 8 bits wide and its adder needs an overflow flag. You are wiring the datapath's signed support unit: widen the reading correctly, expose its sign, and judge overflow from three sign bits. No arithmetic anywhere, only wiring and gates.
This module contains two independent pieces of logic. First, sign-extend the sensor reading a_i and expose its sign. Separately, assume an external adder has already added two operands. It supplies only the sign bits of operand A, operand B, and the result through s_a_i, s_b_i, and s_sum_i. No addition is performed in this module; use those three bits to detect signed overflow.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
a_i | in | 4-bit vector | 4-bit two's complement sensor reading |
ext_o | out | 8-bit vector | The reading, sign-extended to 8 bits |
neg_o | out | 1 bit | High when the reading is negative |
s_a_i | in | 1 bit | Sign bit of the adder's operand A |
s_b_i | in | 1 bit | Sign bit of the adder's operand B |
s_sum_i | in | 1 bit | Sign bit of the adder's result |
ovf_o | out | 1 bit | High when the addition overflowed |
Behavior
ext_ocarries the same signed value asa_i: the low four bits pass through. Each of the four new upper bits is a copy ofa_i's sign bitneg_ois exactlya_i's sign bitovf_ois high when both operand signs agree and the result's sign differs from them- Opposite operand signs never overflow:
ovf_ostays low whenevers_a_iands_b_idiffer
The testbench checks sign extension on positive and negative readings, then sweeps all eight sign-bit combinations.
Constraints
OPERATORS: only bit selection, concatenation, and the gates
and,or, andnot; no arithmetic operators and no comparisons.
-
Leave
xorout for this gate-expression practice. Derive the overflow expression from the sign combinations that cannot represent a correct sum. -
One assignment outside a procedural block per output
Click Run to execute your code. Output will appear here.