RTL Studio Open IDE

Arithmetic & Math

4-bit Adder-Subtractor with Overflow

A 4-bit arithmetic unit supporting addition and subtraction with signed overflow detection using two's complement logic.

About this block

Simulate it in the Verilog IDE

Easyverilogsimsyn

4-bit Adder-Subtractor with Overflow is a Verilog reference block in the Arithmetic & Math catalog. Adders, multipliers, dividers, ALUs, and arithmetic datapaths. Marked Easy: a small block you can read, simulate, and synthesize in one sitting.

This IP implements a 4-bit arithmetic unit capable of both addition and subtraction using two's complement logic. It features a ripple-carry architecture with explicit overflow detection for signed arithmetic. The module is designed for low-complexity applications where simplicity and predictability are prioritized over speed.

  1. Choose Open in IDE. RTL Studio loads the packaged workspace for 4-bit Adder-Subtractor with Overflow.
  2. Read the Verilog module in the editor. The catalog line for this block: A 4-bit arithmetic unit supporting addition and subtraction with signed overflow detection using two's complement logic.
  3. Run the packaged testbench with the in-browser Verilator simulator, then open the VCD in the waveform viewer.
  4. Run Yosys in the same IDE for a generic netlist. Sky130 liberty mapping is a separate Pro library option.

RTL Studio is the Verilog simulator and IDE around this block: Slang lint, Verilator, VCD waveforms, and Yosys, in the browser. Simulation guide · Yosys guide.

Features

  • 4-bit Operation: Handles operands a and b within the range [-8, 7] for signed arithmetic.
  • Dual Mode: Supports addition (sub=0) and subtraction (sub=1).
  • Signed Overflow Detection: Flags when the result exceeds the 4-bit signed range.
  • Carry Out: Provides the unsigned carry-out bit for chaining or unsigned arithmetic checks.
  • Self-Checking Testbench: Includes a comprehensive testbench (tb_adder_subtractor.v) verifying boundary conditions and overflow scenarios.

Architecture

The design uses a conditional XOR gate array to invert the subtrahend b when sub is high. The carry-in for the least significant bit is set to sub to complete the two's complement negation. Overflow is detected by XORing the carry into the most significant bit (MSB) with the carry out of the MSB.

Ports

Port NameDirectionWidthDescription
aInput[3:0]First operand (minuend or augend)
bInput[3:0]Second operand (subtrahend or addend)
subInput1Operation select: 0 for Add, 1 for Subtract
sumOutput[3:0]Result of the arithmetic operation
coutOutput1Carry out from the most significant bit
overflowOutput1Signed overflow flag

Verification

The testbench tb_adder_subtractor.v covers:

  • Basic addition and subtraction.
  • Boundary conditions (e.g., 0, max positive, min negative).
  • Overflow scenarios for both addition and subtraction.
Test CaseDescriptionExpected ResultStatus
TC1.10 + 0sum=0, cout=0, ovf=0PASS
TC1.21 + 1sum=2, cout=0, ovf=0PASS
TC1.37 + 1 (Overflow)sum=8, cout=0, ovf=1PASS
TC1.5-8 + -1 (Overflow)sum=7, cout=1, ovf=1PASS
TC2.10 - 0sum=0, cout=1, ovf=0PASS
TC2.25 - 3sum=2, cout=1, ovf=0PASS
TC2.35 - 7sum=-2, cout=0, ovf=0PASS
TC2.45 - (-8) (Overflow)sum=-3, cout=0, ovf=1PASS
TC2.5-8 - 1 (Overflow)sum=7, cout=1, ovf=1PASS

More in Arithmetic & Math

All Arithmetic & Math blocks