Module 4: Introduction to Verilog HDL · 4.3 Operators

Arithmetic Operators

Verilog provides standard arithmetic operators for digital hardware modeling. These operators infer physical adders, subtractors, and multipliers. You must understand operator bit-width rules. Incorrect bit widths cause arithmetic overflow errors without generating compiler warnings.

Arithmetic Operators and Synthesized Hardware

wire [7:0] a = 8'd100, b = 8'd50;

wire [7:0]  sum    = a + b;    // Inactive carry bit discarded
wire [8:0]  sum9   = a + b;    // Carry bit preserved in bit 8
wire [7:0]  diff   = a - b;    // Subtractor using two's-complement adder
wire [15:0] prod16 = a * b;    // Hardware multiplier; requires 16-bit destination
wire [7:0]  quot   = a / b;    // Combinational divider: high area and delay
wire [7:0]  rem    = a % b;    // Modulo operator: high area and delay

The + operator infers a ripple-carry or carry-lookahead adder based on timing constraints. The - operator infers an adder with an inverted second operand and a carry-in set to 1. The * operator infers a hardware multiplier. On FPGAs, synthesis tools map multi-bit multipliers to dedicated DSP blocks.

The / and % operators with variable operands infer combinational divider circuits. A 32-bit combinational divider requires thousands of logic cells and causes critical path timing violations. Restrict the / and % operators to constant expressions or simulation testbenches.

Bit Width Evaluation Rules

In a continuous assignment, Verilog determines the calculation bit width from the widest operand or the destination signal:

wire [3:0] a = 4'hF;      // Decimal 15
wire [3:0] b = 4'h1;      // Decimal 1

wire [3:0] sum_4 = a + b;   // Evaluated at 4 bits; sum_4 = 4'h0 (overflow discarded)
wire [4:0] sum_5 = a + b;   // Evaluated at 5 bits because destination is 5 bits: 5'h10

Destination sizing applies only to simple assignments. In a self-determined context, the expression width depends solely on its operands:

wire [7:0] x, y;
wire       ovf = (x + y) > 8'hFF;   // Evaluated at 8 bits: sum wraps and never exceeds 255
wire [8:0] c   = {x + y};           // Inside concatenation braces, x + y evaluates to 8 bits

In relational comparisons, the operator width is self-determined. An 8-bit sum never exceeds 8'hFF. To prevent overflow loss, extend the operands explicitly before the addition:

wire [4:0] sum_5b = {1'b0, a} + {1'b0, b};       // Explicit 5-bit addition
wire       ovf_ok = ({1'b0, x} + {1'b0, y}) > 9'd255; // Valid 9-bit comparison

Explicit zero-extension creates predictable hardware without increasing gate count.

Exponentiation Operator

The power operator ** calculates exponential values:

wire [31:0] squared = base ** 2;    // Synthesizable: constant exponent unrolls to multipliers
localparam DEPTH = 2 ** ADDR_BITS;  // Common usage for constant memory depth

Synthesis tools reject the ** operator when the exponent is a variable signal.

Hardware Multiplication

The product of an $N$-bit operand and an $M$-bit operand requires $N + M$ bits to prevent numeric overflow:

wire [7:0]  a, b;
wire [15:0] full_product  = a * b;   // Complete 16-bit product preserved
wire [7:0]  trunc_product = a * b;   // Lower 8 bits preserved; upper bits discarded

Multiplication operations up to 18 bits map directly to single FPGA DSP slices. Wider multiplications chain multiple DSP slices together.

Efficient Division Alternatives

Avoid variable combinational division in synthesizable RTL. An $N$-bit combinational divider requires $O(N^2)$ gate area and creates $O(N)$ logic delay. Use these efficient alternatives:

Required Operation Hardware Implementation
Divide by power of two: $a / 2^k$ Right shift: a >> k
Modulo power of two: $a \bmod 2^k$ Bitwise mask: a & ((1 << k) - 1)
Divide by small constant: $a / c$ Multiply by reciprocal fraction and shift right
General variable division Multi-cycle sequential divider or vendor IP core
Trigonometric or logarithmic functions CORDIC algorithm or lookup table (LUT)
wire [7:0] a = 8'd200;

wire [7:0] div4 = a >> 2;                      // Divide by 4: wiring shift only
wire [7:0] mod8 = a & 8'h07;                   // Modulo 8: wiring mask only
wire [7:0] div3 = ({8'd0, a} * 16'd85) >> 8;   // Approximates divide by 3 (85/256)

Signed Arithmetic Operations

Signals are unsigned by default. The signed keyword changes sign-extension and comparison behavior without altering raw binary representations:

wire signed [7:0] a = -8'sd5;
wire signed [7:0] b =  8'sd3;
wire signed [7:0] r = a + b;      // Evaluates to -2

wire signed [3:0] narrow = 4'b1010;   // Represents -6 in two's complement
wire signed [7:0] wide   = narrow;    // Sign-extended: 8'b1111_1010 (-6)
wire        [7:0] wide_u = narrow;    // Zero-extended: 8'b0000_1010 (10)

The system function $signed(x) casts an unsigned signal to signed. The system function $unsigned(x) casts a signed signal to unsigned. If an expression mixes signed and unsigned operands, Verilog treats all operands as unsigned. Declare all signals as signed when implementing signed datapaths.

Parameterized Arithmetic Logic Unit

`timescale 1ns / 1ps
`default_nettype none

module alu_arith #(
    parameter WIDTH = 8
)(
    input  wire [WIDTH-1:0] a,
    input  wire [WIDTH-1:0] b,
    input  wire [1:0]       op,
    output wire [WIDTH-1:0] result,
    output wire             carry_out,
    output wire             zero
);

    wire [WIDTH:0] sum  = {1'b0, a} + {1'b0, b};   // Preserves carry bit
    wire [WIDTH:0] diff = {1'b0, a} - {1'b0, b};   // MSB indicates borrow flag

    assign {carry_out, result} =
        (op == 2'b00) ? sum  :                  // Addition
        (op == 2'b01) ? diff :                  // Subtraction
        (op == 2'b10) ? {1'b0, a + 1'b1} :      // Increment
                        {1'b0, a};              // Pass-through

    assign zero = ~|result;

endmodule

Simulation Considerations

An x or z bit on any arithmetic input propagates unknown values across the entire result bus. The simulator does not report overflow errors automatically. To print signed decimal values in testbenches, use $display("%0d", $signed(val)):

initial begin
    a = 8'hFF; b = 8'h01;
    #10;
    if ((a + b) > 8'hFF) 
        $display("Overflow detected");                    // Never executes: 8-bit evaluation
    if (({1'b0, a} + {1'b0, b}) > 9'd255) 
        $display("Overflow detected");                    // Executes correctly: 9-bit evaluation
end

Practice Exercise

Design a saturating 8-bit unsigned adder module. If the addition exceeds 255, clamp the output to 8'hFF.

Solution
`timescale 1ns / 1ps
`default_nettype none

module sat_adder (
    input  wire [7:0] a,
    input  wire [7:0] b,
    output wire [7:0] result
);

    wire [8:0] full_sum = {1'b0, a} + {1'b0, b};

    assign result = full_sum[8] ? 8'hFF : full_sum[7:0];

endmodule

Compute the addition at a 9-bit width. Bit 8 represents the overflow carry out of the 8-bit addition. When bit 8 is high, saturate the result to 8'hFF.

The next article examines relational and logical comparison operators and the operational distinction between bitwise and boolean operations.

← Style and Conventions Relational and Logical Operators →