Module 4: Introduction to Verilog HDL · 4.3 Operators

Relational and Logical Operators

Control decisions in digital logic depend on single-bit boolean conditions. Digital circuits evaluate condition flags to branch execution, assert FIFO status flags, or terminate counting loops. Two distinct operator families generate condition flags: relational operators evaluate comparative magnitude, and logical operators combine boolean conditions.

Relational Operators

Relational operators compare numeric values and produce a single-bit result (1'b0 or 1'b1):

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

wire gt  = (a > b);    // Evaluates to 1'b1
wire lt  = (a < b);    // Evaluates to 1'b0
wire gte = (a >= b);   // Evaluates to 1'b1
wire lte = (a <= b);   // Evaluates to 1'b0
wire eq  = (a == b);   // Evaluates to 1'b0
wire neq = (a != b);   // Evaluates to 1'b1

Always enclose relational expressions in parentheses to prevent operator precedence errors.

Equality comparators (== and !=) require minimal gate area. The circuit infers bitwise XOR gates feeding an OR/NOR reduction tree. The propagation delay scales logarithmically with vector width ($O(\log N)$).

Magnitude comparators (>, <, >=, <=) require carry-chain subtractors. The propagation delay scales linearly with vector width ($O(N)$) (Figure 1). When either operator satisfies functional specifications, select == to reduce critical path delay.

Equality and magnitude comparators
Figure 1: Comparison circuit implementations: XOR tree for equality and carry chain for magnitude

Case Equality Operators

In four-state simulation, standard equality (==) evaluates to x if any input bit contains x or z. Physical silicon does not implement four-state logic.

To verify exact four-state values in testbenches, Verilog provides case equality (===) and case inequality (!==):

wire [1:0] x = 2'bx0;

wire eq1 = (x == 2'b00);    // Evaluates to x: bit 1 is unknown
wire eq2 = (x == 2'bx0);    // Evaluates to x: == cannot compare x
wire eq3 = (x === 2'bx0);   // Evaluates to 1'b1: exact 4-state literal match
wire eq4 = (x === 2'b00);   // Evaluates to 1'b0

Case equality operators are strictly non-synthesizable. Synthesis tools reject === and !==. Use them exclusively in testbenches to verify reset clearing and detect floating bus states.

Logical Operators

The logical operators && (logical AND), || (logical OR), and ! (logical NOT) evaluate the truth value of entire expressions. Any non-zero operand evaluates to true (1'b1). Zero evaluates to false (1'b0). The evaluation result is always a 1-bit value:

wire valid = (count > 0) && (count < MAX) && enable;
wire fault = error_a || error_b || watchdog_expired;

Logical Versus Bitwise Operators

Do not confuse logical operators with bitwise operators:

wire [3:0] a = 4'b1010;
wire [3:0] b = 4'b1100;

wire [3:0] bitwise_and = a & b;    // Evaluates to 4'b1000 (bit-by-bit AND gates)
wire       logical_and = a && b;   // Evaluates to 1'b1 (both operands are non-zero)

wire [3:0] bitwise_or  = a | b;    // Evaluates to 4'b1110 (bit-by-bit OR gates)
wire       logical_or  = a || b;   // Evaluates to 1'b1

The bitwise expression a & b infers four parallel AND gates and produces a 4-bit bus. The logical expression a && b infers two multi-input OR reductions feeding a single 2-input AND gate, producing a 1-bit flag.

Use && and || to combine control conditions. Use & and | to mask or manipulate data buses:

wire [7:0] masked  = data & 8'h0F;
wire [7:0] flagged = status | 8'h80;
wire [7:0] merged  = ({8{sel_a}} & a) | ({8{sel_b}} & b);   // AND-OR bus multiplexer

In software programming languages, && performs short-circuit evaluation. In hardware synthesis, both branches infer physical gates that operate continuously. Never rely on short-circuit evaluation behavior in RTL designs.

Range Checker Implementation

The following module combines magnitude comparators and a logical operator to verify that an input value falls within an allowable range:

`timescale 1ns / 1ps
`default_nettype none

module range_checker #(
    parameter WIDTH = 8,
    parameter LO    = 8'd10,
    parameter HI    = 8'd100
)(
    input  wire [WIDTH-1:0] value,
    output wire             in_range,   // Active when LO <= value <= HI
    output wire             below,
    output wire             above
);

    assign in_range = (value >= LO) && (value <= HI);
    assign below    = (value < LO);
    assign above    = (value > HI);

endmodule

The module synthesizes two magnitude comparators and one 2-input AND gate (Figure 2). Exactly one output flag asserts high at any time.

Range checker
Figure 2: Hardware schematic of range checker module

Verify range comparators thoroughly around threshold boundaries in simulation:

module tb_range;
    reg [7:0] value;
    wire in_range, below, above;

    range_checker #(.LO(10), .HI(100)) dut (
        .value(value), .in_range(in_range), .below(below), .above(above)
    );

    initial begin
        value = 8'd9;   #10;   // Below range
        value = 8'd10;  #10;   // Lower boundary (in range)
        value = 8'd55;  #10;   // Interior point (in range)
        value = 8'd100; #10;   // Upper boundary (in range)
        value = 8'd101; #10;   // Above range
        value = 8'hxx;  #10;   // Unknown input: outputs resolve to x
        $finish;
    end
endmodule

Practice Exercise

Design an 8-input priority encoder using continuous assignment statements. The module accepts an 8-bit input bus in. It produces a 3-bit output out representing the highest active bit index, and asserts a 1-bit valid flag when any bit is high.

Solution
`timescale 1ns / 1ps
`default_nettype none

module priority_enc_8 (
    input  wire [7:0] in,
    output wire [2:0] out,
    output wire       valid
);

    assign valid = |in;    // Unary OR reduction detects any active bit

    assign out = in[7] ? 3'd7 :
                 in[6] ? 3'd6 :
                 in[5] ? 3'd5 :
                 in[4] ? 3'd4 :
                 in[3] ? 3'd3 :
                 in[2] ? 3'd2 :
                 in[1] ? 3'd1 :
                         3'd0;

endmodule

Each input bit in[N] acts directly as a single-bit condition. The nested conditional operator establishes evaluation priority. The unary reduction operator |in computes the valid signal without arithmetic comparisons.

The next article examines bitwise and unary reduction operators, including single-line parity generators and wide vector status detectors.

← Arithmetic Operators Bitwise and Reduction Operators →