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

PortDirectionTypeDescription
ain4-bit vector4-bit two's complement sensor reading
extout8-bit vectorThe reading, sign-extended to 8 bits
negout1 bitHigh when the reading is negative
s_ain1 bitSign bit of the adder's operand A
s_bin1 bitSign bit of the adder's operand B
s_sumin1 bitSign bit of the adder's result
ovfout1 bitHigh when the addition overflowed

Behavior

  • ext carries the same signed value as a: the low four bits pass through. Each of the four new upper bits is a copy of a's sign bit
  • neg is exactly a's sign bit
  • ovf is high when both operand signs agree and the result's sign differs from them
  • Opposite operand signs never overflow: ovf stays low whenever s_a and s_b differ

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, 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
Loading editor...

Click Run to execute your code. Output will appear here.