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 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, s_b, and s_sum. No addition is performed in this module; use those three bits to detect signed overflow.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
a | in | 4-bit vector | 4-bit two's complement sensor reading |
ext | out | 8-bit vector | The reading, sign-extended to 8 bits |
neg | out | 1 bit | High when the reading is negative |
s_a | in | 1 bit | Sign bit of the adder's operand A |
s_b | in | 1 bit | Sign bit of the adder's operand B |
s_sum | in | 1 bit | Sign bit of the adder's result |
ovf | out | 1 bit | High when the addition overflowed |
Behavior
extcarries the same signed value asa: the low four bits pass through. Each of the four new upper bits is a copy ofa's sign bitnegis exactlya's sign bitovfis high when both operand signs agree and the result's sign differs from them- Opposite operand signs never overflow:
ovfstays low whenevers_aands_bdiffer
The testbench checks sign extension on positive and negative readings, then sweeps all eight sign-bit combinations.
Constraints
only bit selection, concatenation, and the gates and, or, not (&, |, ~); no arithmetic operators, no comparisons.
- No
xor(^), even though it could shorten the overflow check. The point is practicing the sign rule as written: two dangerous sign combinations, each an AND term, ORed together. The testbench can only check values, but this shape is the exercise. - One concurrent assignment per output
Click Run to execute your code. Output will appear here.