Module 4: Introduction to Verilog HDL · 4.5 Dataflow Modeling

A Parameterised Comparator, Start to Finish

This article demonstrates the complete design and verification lifecycle of a digital hardware block. We design a parameterized magnitude comparator that evaluates signed two’s-complement numbers or unsigned binary values based on a mode control input.

1. Functional Specification

  • Module Parameters: WIDTH (default value: 8 bits).
  • Data Inputs: a and b (WIDTH bits wide).
  • Control Input: signed_mode (1 = signed two’s complement; 0 = unsigned binary).
  • Status Outputs: gt (greater than), eq (equal to), and lt (less than).
  • Invariant Property: Exactly one output flag asserts high at any time (one-hot mutual exclusion).

The control input signed_mode resolves ambiguous values. For example, the 8-bit vector 8'hFE represents decimal 254 in unsigned binary, but represents decimal $-2$ in signed two’s-complement arithmetic.

2. Circuit Architecture

Equality is independent of signed representation: two bit vectors match if and only if every corresponding bit position matches. The equality test a == b infers a bitwise XOR reduction tree.

Magnitude comparisons depend on representation mode. Unsigned comparison evaluates the MSB with positive weight $+2^{N-1}$. Signed comparison evaluates the MSB with negative weight $-2^{N-1}$. The circuit computes both comparisons concurrently and selects between them using a 2-to-1 multiplexer (Figure 1).

The less-than output derives directly from the other two flags:

$$lt = \overline{gt} \cdot \overline{eq}$$

This derivation eliminates a third comparator and guarantees mutual exclusion by construction.

Comparator architecture
Figure 1: Comparator architecture with dual magnitude comparators, mode multiplexer, and derived less-than flag

3. Dataflow RTL Implementation

`timescale 1ns / 1ps
`default_nettype none

module comparator #(
    parameter WIDTH = 8
)(
    input  wire [WIDTH-1:0] a,
    input  wire [WIDTH-1:0] b,
    input  wire             signed_mode,   // 0 = Unsigned; 1 = Two's complement
    output wire             gt,
    output wire             eq,
    output wire             lt
);

    assign eq = (a == b);

    wire gt_unsigned = (a > b);
    wire gt_signed   = ($signed(a) > $signed(b));

    assign gt = signed_mode ? gt_signed : gt_unsigned;

    assign lt = ~gt & ~eq;

endmodule

[!CAUTION] Do not write assign lt = !gt;. When a equals b, gt is 0, which would erroneously force lt to 1. You must exclude equality explicitly.

4. Directed Self-Checking Testbench

The directed testbench verifies boundary conditions, signed/unsigned disparities, and checks the one-hot invariant property after every test vector:

`timescale 1ns / 1ps
`default_nettype none

module tb_comparator;

    parameter WIDTH = 8;

    reg  [WIDTH-1:0] a, b;
    reg              signed_mode;
    wire             gt, eq, lt;

    comparator #(.WIDTH(WIDTH)) u_dut (
        .a          (a),
        .b          (b),
        .signed_mode(signed_mode),
        .gt         (gt),
        .eq         (eq),
        .lt         (lt)
    );

    // Verifies one-hot mutual exclusion property
    task check_invariant;
        input [WIDTH-1:0] a_val, b_val;
        input             s_mode;
        begin
            if ((gt + eq + lt) !== 1)
                $display("INVARIANT VIOLATION: a=%0d b=%0d mode=%b gt=%b eq=%b lt=%b",
                         a_val, b_val, s_mode, gt, eq, lt);
        end
    endtask

    // Compares actual outputs against expected reference values
    task check;
        input [WIDTH-1:0] a_val, b_val;
        input             s_mode;
        input             exp_gt, exp_eq, exp_lt;
        input [200*8:1]   test_name;
        begin
            if (gt !== exp_gt || eq !== exp_eq || lt !== exp_lt)
                $display("FAIL [%0s]: a=%0d b=%0d mode=%b gt=%b eq=%b lt=%b (expected %b %b %b)",
                         test_name, a_val, b_val, s_mode, gt, eq, lt, exp_gt, exp_eq, exp_lt);
            else
                $display("PASS [%0s]", test_name);
            check_invariant(a_val, b_val, s_mode);
        end
    endtask

    initial begin
        $dumpfile("comparator_waves.vcd");
        $dumpvars(0, tb_comparator);

        $display("=== Unsigned Mode Tests ===");
        signed_mode = 1'b0;
        a = 8'd100; b = 8'd50;  #10; check(a, b, signed_mode, 1, 0, 0, "Unsigned 100 > 50");
        a = 8'd50;  b = 8'd50;  #10; check(a, b, signed_mode, 0, 1, 0, "Unsigned 50 == 50");
        a = 8'd30;  b = 8'd50;  #10; check(a, b, signed_mode, 0, 0, 1, "Unsigned 30 < 50");
        a = 8'd0;   b = 8'd0;   #10; check(a, b, signed_mode, 0, 1, 0, "Unsigned 0 == 0");
        a = 8'd255; b = 8'd0;   #10; check(a, b, signed_mode, 1, 0, 0, "Unsigned 255 > 0");
        a = 8'd0;   b = 8'd255; #10; check(a, b, signed_mode, 0, 0, 1, "Unsigned 0 < 255");
        a = 8'hFE;  b = 8'h02;  #10; check(a, b, signed_mode, 1, 0, 0, "Unsigned 254 > 2");

        $display("=== Signed Mode Tests ===");
        signed_mode = 1'b1;
        a = 8'd100; b = 8'd50;  #10; check(a, b, signed_mode, 1, 0, 0, "Signed +100 > +50");
        a = 8'd50;  b = 8'd50;  #10; check(a, b, signed_mode, 0, 1, 0, "Signed +50 == +50");
        a = 8'hFE;  b = 8'h02;  #10; check(a, b, signed_mode, 0, 0, 1, "Signed -2 < +2");
        a = 8'h7F;  b = 8'h80;  #10; check(a, b, signed_mode, 1, 0, 0, "Signed +127 > -128");
        a = 8'hFF;  b = 8'hFE;  #10; check(a, b, signed_mode, 1, 0, 0, "Signed -1 > -2");
        a = 8'h00;  b = 8'h00;  #10; check(a, b, signed_mode, 0, 1, 0, "Signed 0 == 0");

        $display("=== Dynamic Mode Switch Test ===");
        a = 8'hFE; b = 8'h02;
        signed_mode = 1'b0; 
        #10;
        $display("Unsigned: a=%h b=%h gt=%b eq=%b lt=%b", a, b, gt, eq, lt);
        signed_mode = 1'b1; 
        #10;
        $display("Signed:   a=%h b=%h gt=%b eq=%b lt=%b", a, b, gt, eq, lt);

        $finish;
    end

endmodule

Simulation Execution and Output

iverilog -o comparator_sim tb_comparator.v comparator.v
vvp comparator_sim
=== Unsigned Mode Tests ===
PASS [Unsigned 100 > 50]
PASS [Unsigned 50 == 50]
PASS [Unsigned 30 < 50]
PASS [Unsigned 0 == 0]
PASS [Unsigned 255 > 0]
PASS [Unsigned 0 < 255]
PASS [Unsigned 254 > 2]
=== Signed Mode Tests ===
PASS [Signed +100 > +50]
PASS [Signed +50 == +50]
PASS [Signed -2 < +2]
PASS [Signed +127 > -128]
PASS [Signed -1 > -2]
PASS [Signed 0 == 0]
=== Dynamic Mode Switch Test ===
Unsigned: a=fe b=02 gt=1 eq=0 lt=0
Signed:   a=fe b=02 gt=0 eq=0 lt=1

5. Random Stimulus Verification with Reference Model

To verify unpredicted edge cases, create a randomized testbench that compares the DUT against a behavioral software model:

`timescale 1ns / 1ps
`default_nettype none

module tb_comparator_random;

    parameter WIDTH   = 8;
    parameter N_TESTS = 200;

    reg  [WIDTH-1:0] a, b;
    reg              signed_mode;
    wire             gt, eq, lt;

    comparator #(.WIDTH(WIDTH)) u_dut (
        .a(a), .b(b), .signed_mode(signed_mode), .gt(gt), .eq(eq), .lt(lt)
    );

    integer i, errors;

    initial begin
        errors = 0;

        // Verify unsigned random vectors
        signed_mode = 1'b0;
        for (i = 0; i < N_TESTS; i = i + 1) begin
            a = $random; b = $random; #5;
            if (gt !== (a > b) || eq !== (a == b) || lt !== (a < b)) begin
                $display("FAIL Unsigned: a=%0d b=%0d gt=%b eq=%b lt=%b", a, b, gt, eq, lt);
                errors = errors + 1;
            end
        end

        // Verify signed random vectors
        signed_mode = 1'b1;
        for (i = 0; i < N_TESTS; i = i + 1) begin
            a = $random; b = $random; #5;
            if (gt !== ($signed(a) > $signed(b)) ||
                eq !== ($signed(a) == $signed(b)) ||
                lt !== ($signed(a) < $signed(b))) begin
                $display("FAIL Signed: a=%0d b=%0d gt=%b eq=%b lt=%b", a, b, gt, eq, lt);
                errors = errors + 1;
            end
        end

        if (errors == 0)
            $display("All %0d randomized tests passed successfully (WIDTH=%0d).", 
                     N_TESTS * 2, WIDTH);
        else
            $display("%0d tests FAILED.", errors);

        $finish;
    end

endmodule

6. Logic Synthesis Results

On an FPGA, logic synthesis maps this module into:

  • Dedicated carry-chain slices for signed and unsigned comparators.
  • One LUT-based 2-to-1 multiplexer for gt.
  • An XOR reduction tree for eq.
  • A 2-input NOR gate for lt.

For an 8-bit bus, the complete circuit requires approximately 12 FPGA lookup tables (LUTs) and exhibits a propagation delay under 3 nanoseconds.

Standard Digital Design Methodology

Follow this systematic procedure for digital RTL projects:

  1. Define module interface ports, parameters, and operational invariants.
  2. Sketch block diagrams and internal net architectures.
  3. Implement register-transfer level source files.
  4. Construct directed testbenches targeting boundary conditions.
  5. Execute random stimulus verification using golden reference models.
  6. Verify gate count and timing reports from logic synthesis tools.

The final article in Module 4 compares structural, dataflow, and behavioral modeling styles.

← Muxes, Encoders and Decoders Structural, Dataflow or Behavioural →