Module 4: Introduction to Verilog HDL · 4.5 Dataflow Modeling

Dataflow in Practice

Most register-transfer level (RTL) designs use a core set of combinational patterns. These include bitwise masking, carry-preserving adders, sign-extension, and multiplexer chains. This article presents standard dataflow idioms and integrates them into a parameterized arithmetic logic unit (ALU).

Common Dataflow Idioms

1. Bitwise Logic Operations

assign y_not  = ~a;
assign y_and  = a & b;
assign y_or   = a | b;
assign y_xor  = a ^ b;
assign y_nand = ~(a & b);
assign y_nor  = ~(a | b);
assign y_xnor = ~(a ^ b);

These operations synthesize parallel logic gates across the width of the input operands.

2. Unary Vector Reductions

wire [7:0] data_byte;

assign y_and8   = &data_byte;    // Asserts high when all bits equal 1
assign y_or8    = |data_byte;    // Asserts high when any bit equals 1
assign y_parity = ^data_byte;    // Asserts high on odd parity (odd number of 1s)
assign y_nor8   = ~|data_byte;   // Asserts high when all bits equal 0 (zero flag)

Reduction operations synthesize balanced logic trees with a depth of $\lceil \log_2 N \rceil$ gate delays.

3. Adder with Output Carry Bit

wire [7:0] a, b;
wire       cin;
wire [7:0] sum;
wire       cout;

// Zero-extend operands to 9 bits to capture overflow carry
assign {cout, sum} = {1'b0, a} + {1'b0, b} + {8'h00, cin};

Concatenation on the left unpacks the 9-bit addition into the 1-bit carry cout and 8-bit sum sum.

4. Relational Comparators

assign equal   = (a == b);
assign gt_uns  = (a > b);
assign gt_sgn  = ($signed(a) > $signed(b));

The system function $signed() enables signed two’s-complement magnitude comparisons. Without casting, 8'hFE (254 unsigned) is greater than 8'h01. With signed casting, 8'hFE ($-2$) is less than 8'h01.

5. Multiplexers

// 2-to-1 multiplexer
assign mux2 = sel_1bit ? a : b;

// 4-to-1 multiplexer tree
assign mux4 = (sel_2bit == 2'b00) ? a :
              (sel_2bit == 2'b01) ? b :
              (sel_2bit == 2'b10) ? c : d;

6. Shifters

assign shl_fixed = data << 2;                 // Hardwired routing: 0 gates
assign sar_fixed = $signed(data) >>> 2;       // Hardwired sign-extended routing: 0 gates
assign shl_var   = data << shift_amount;      // Inferred barrel shifter: multiplexer tree

7. Bit Rearrangement and Width Extension

assign sign_ext = {{8{narrow[7]}}, narrow};     // Sign-extends 8 bits to 16 bits
assign zero_ext = {8'h00, narrow};              // Zero-extends 8 bits to 16 bits
assign reversed = {narrow[0], narrow[1], narrow[2], narrow[3],
                   narrow[4], narrow[5], narrow[6], narrow[7]}; // Reverses bit order
assign upper    = narrow[7:4];                  // High nibble part-select
assign replaced = {new_upper[3:0], narrow[3:0]}; // Replaces high nibble

These operations require zero physical logic gates. The synthesis tool implements them through netlist wiring.

Parameterized Dataflow ALU

Digital ALUs evaluate all arithmetic and logic operations concurrently, using an output multiplexer to route the requested result (Figure 1).

Dataflow ALU
Figure 1: Concurrent evaluation datapath and multiplexer selection in dataflow ALU

`timescale 1ns / 1ps
`default_nettype none

module alu_dataflow #(
    parameter WIDTH = 8
)(
    input  wire [WIDTH-1:0] a,
    input  wire [WIDTH-1:0] b,
    input  wire [2:0]       op,
    output wire [WIDTH-1:0] result,
    output wire             zero,
    output wire             carry,      // Carry out of addition; not-borrow out of subtraction
    output wire             overflow    // Signed two's-complement overflow
);
    // All functional operations evaluate concurrently
    wire [WIDTH:0]   add_result = {1'b0, a} + {1'b0, b};
    wire [WIDTH:0]   sub_result = {1'b0, a} - {1'b0, b};   // MSB indicates borrow condition
    wire [WIDTH-1:0] and_result = a & b;
    wire [WIDTH-1:0] or_result  = a | b;
    wire [WIDTH-1:0] xor_result = a ^ b;
    wire [WIDTH-1:0] not_result = ~a;
    wire [WIDTH-1:0] shl_result = a << 1;
    wire [WIDTH-1:0] shr_result = a >> 1;

    // Output result multiplexer
    assign result = (op == 3'd0) ? add_result[WIDTH-1:0] :
                    (op == 3'd1) ? sub_result[WIDTH-1:0] :
                    (op == 3'd2) ? and_result :
                    (op == 3'd3) ? or_result  :
                    (op == 3'd4) ? xor_result :
                    (op == 3'd5) ? not_result :
                    (op == 3'd6) ? shl_result :
                                   shr_result;

    // ALU status flags
    assign zero = ~|result;

    assign carry = (op == 3'd0) ?  add_result[WIDTH] :   // Addition carry out
                   (op == 3'd1) ? ~sub_result[WIDTH] :   // Active-high not-borrow convention
                                   1'b0;

    // Signed overflow: operands share sign bit, but output sign differs
    assign overflow =
        (op == 3'd0) ? (a[WIDTH-1] == b[WIDTH-1]) && (result[WIDTH-1] != a[WIDTH-1]) :
        (op == 3'd1) ? (a[WIDTH-1] != b[WIDTH-1]) && (result[WIDTH-1] != a[WIDTH-1]) :
                       1'b0;

endmodule

The extra bit on add_result and sub_result captures overflow carry and borrow bits. The chained ternary operator forms an 8-to-1 multiplexer tree.

Functional Simulation Verification

`timescale 1ns / 1ps
`default_nettype none

module tb_alu_quick;
    reg  [7:0] a, b;
    reg  [2:0] op;
    wire [7:0] result;
    wire       zero, carry, overflow;

    alu_dataflow #(.WIDTH(8)) u_dut (
        .a(a), .b(b), .op(op),
        .result(result), .zero(zero), .carry(carry), .overflow(overflow)
    );

    initial begin
        a = 8'd100; b = 8'd50;  op = 3'd0; #10;    // 150: carry=0, overflow=0
        $display("ADD: %0d + %0d = %0d | zero=%b carry=%b overflow=%b", 
                 a, b, result, zero, carry, overflow);

        a = 8'd100; b = 8'd100; op = 3'd0; #10;    // 200: unsigned carry=0; signed overflow=1
        $display("ADD: %0d + %0d = %0d | zero=%b carry=%b overflow=%b", 
                 a, b, result, zero, carry, overflow);

        a = 8'd200; b = 8'd100; op = 3'd0; #10;    // 300 wraps to 44: carry=1; signed overflow=0
        $display("ADD: %0d + %0d = %0d | zero=%b carry=%b overflow=%b", 
                 a, b, result, zero, carry, overflow);

        a = 8'hAA;  b = 8'hF0;  op = 3'd2; #10;
        $display("AND: 0x%h & 0x%h = 0x%h | zero=%b", a, b, result, zero);

        a = 8'hAB;  b = 8'hAB;  op = 3'd4; #10;    // Zero flag test
        $display("XOR: 0x%h ^ 0x%h = 0x%h | zero=%b", a, b, result, zero);

        $finish;
    end
endmodule

Execution Log

ADD: 100 + 50 = 150 | zero=0 carry=0 overflow=0
ADD: 100 + 100 = 200 | zero=0 carry=0 overflow=1
ADD: 200 + 100 = 44 | zero=0 carry=1 overflow=0
AND: 0xaa & 0xf0 = 0xa0 | zero=0
XOR: 0xab ^ 0xab = 0x00 | zero=1

Note the distinction between carry and overflow:

  • $100 + 100$: Produces unsigned 200 without carry (carry = 0). In signed arithmetic, $+100 + (+100) = +200$, which exceeds the signed 8-bit maximum ($+127$), asserting overflow = 1.
  • $200 + 100$: Produces 300, which wraps modulo-256 to 44 (carry = 1). In signed two’s complement, $-56 + (+100) = +44$, which represents a valid signed result (overflow = 0).

Practice Exercise

Update the ALU carry logic to capture bits discarded during shift operations. For left shifts (op = 3'd6), assign carry to the bit discarded from the MSB. For right shifts (op = 3'd7), assign carry to the bit discarded from the LSB. Add an output flag negative that indicates negative two’s-complement results.

Solution
assign carry = (op == 3'd0) ?  add_result[WIDTH] :
               (op == 3'd1) ? ~sub_result[WIDTH] :
               (op == 3'd6) ?  a[WIDTH-1] :          // Bit shifted out of MSB
               (op == 3'd7) ?  a[0] :                // Bit shifted out of LSB
                               1'b0;

assign negative = result[WIDTH-1];                   // Sign bit of result bus

The negative flag directly exposes the result sign bit without adding gate logic.

The next article examines conditional dataflow modeling, focusing on multiplexer networks, decoders, and priority encoders.

← Continuous Assignments Muxes, Encoders and Decoders →