Module 4: Introduction to Verilog HDL · 4.5 Dataflow Modeling

Muxes, Encoders and Decoders

Multiplexers, priority encoders, and decoders perform fundamental data steering functions in digital systems. A multiplexer selects one input channel from several alternatives. A priority encoder converts the highest-priority active request into a binary address. A decoder activates one output line based on a binary code. All three circuits map directly to conditional dataflow expressions.

Multiplexer Structures

// 2-to-1 multiplexer
assign y = sel ? a : b;

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

// 8-to-1 multiplexer
assign y8 = (sel == 3'd0) ? d0 :
            (sel == 3'd1) ? d1 :
            (sel == 3'd2) ? d2 :
            (sel == 3'd3) ? d3 :
            (sel == 3'd4) ? d4 :
            (sel == 3'd5) ? d5 :
            (sel == 3'd6) ? d6 : d7;

Because select values are mutually exclusive, logic synthesis tools optimize the sequential conditional chain into a balanced multiplexer tree (Figure 1). For multiplexers wider than 8-to-1, use procedural case statements inside an always @(*) block.

4-to-1 mux from a conditional chain
Figure 1: Conditional chain mapped to balanced 4-to-1 multiplexer tree

Priority Encoders

Arrange conditions from highest priority to lowest priority. The first matching condition drives the output:

`timescale 1ns / 1ps
`default_nettype none

module priority_enc_4to2 (
    input  wire [3:0] req,      // req[3] has the highest priority
    output wire [1:0] grant,
    output wire       valid
);
    assign valid = |req;        // Asserts high when any request bit is active

    assign grant = req[3] ? 2'd3 :
                   req[2] ? 2'd2 :
                   req[1] ? 2'd1 :
                            2'd0;
endmodule
Input Request (req) Valid Flag (valid) Output Code (grant)
4'b0000 0 2'b00 (ignored; valid is low)
4'b0001 1 2'b00
4'b0010 1 2'b01
4'b0101 1 2'b10
4'b1111 1 2'b11

The valid signal distinguishes a valid request for channel 0 from an idle condition where no requests are active.

Binary Decoders

A decoder performs the inverse function of an encoder. It decodes an $N$-bit binary address to activate one of $2^N$ output lines:

`timescale 1ns / 1ps
`default_nettype none

module decoder_2to4 (
    input  wire [1:0] sel,
    input  wire       en,
    output wire [3:0] y      // Active-high one-hot output
);
    assign y[0] = en & ~sel[1] & ~sel[0];
    assign y[1] = en & ~sel[1] &  sel[0];
    assign y[2] = en &  sel[1] & ~sel[0];
    assign y[3] = en &  sel[1] &  sel[0];
endmodule

You can also express the decoder using a conditional statement:

assign y = !en           ? 4'b0000 :
           (sel == 2'd0) ? 4'b0001 :
           (sel == 2'd1) ? 4'b0010 :
           (sel == 2'd2) ? 4'b0100 :
                           4'b1000;

BCD to Seven-Segment Display Decoder

A seven-segment display contains LED segments labeled a through g (Figure 2). Most FPGA development boards utilize active-low display drivers: logic 0 illuminates a segment. The 7-bit bus is arranged as {g, f, e, d, c, b, a}.

Seven-segment display
Figure 2: Seven-segment display layout and segment bit assignment for digit 2

BCD Digit Illuminated Segments Active-Low Pattern seg[6:0]
0 a, b, c, d, e, f 7'b100_0000
1 b, c 7'b111_1001
2 a, b, d, e, g 7'b010_0100
3 a, b, c, d, g 7'b011_0000
4 b, c, f, g 7'b001_1001
5 a, c, d, f, g 7'b001_0010
6 a, c, d, e, f, g 7'b000_0010
7 a, b, c 7'b111_1000
8 All segments 7'b000_0000
9 a, b, c, d, f, g 7'b001_0000
`timescale 1ns / 1ps
`default_nettype none

module seg7_decoder (
    input  wire [3:0] bcd,   // BCD input value: 0 to 9
    output wire [6:0] seg    // {g, f, e, d, c, b, a}, active-low outputs
);
    //                              gfedcba
    assign seg = (bcd == 4'd0) ? 7'b100_0000 :
                 (bcd == 4'd1) ? 7'b111_1001 :
                 (bcd == 4'd2) ? 7'b010_0100 :
                 (bcd == 4'd3) ? 7'b011_0000 :
                 (bcd == 4'd4) ? 7'b001_1001 :
                 (bcd == 4'd5) ? 7'b001_0010 :
                 (bcd == 4'd6) ? 7'b000_0010 :
                 (bcd == 4'd7) ? 7'b111_1000 :
                 (bcd == 4'd8) ? 7'b000_0000 :
                 (bcd == 4'd9) ? 7'b001_0000 :
                                 7'b111_1111;   // Values 10 to 15: display blank
endmodule

On an FPGA, synthesis tools map this 10-entry truth table into seven parallel lookup tables (LUTs), one per display segment.

Self-Checking Testbench Verification

`timescale 1ns / 1ps
`default_nettype none

module tb_seg7;
    reg  [3:0] bcd;
    wire [6:0] seg;

    seg7_decoder u_dut (.bcd(bcd), .seg(seg));

    reg [6:0] expected [0:9];
    integer i, errors;

    initial begin
        expected[0] = 7'b100_0000;
        expected[1] = 7'b111_1001;
        expected[2] = 7'b010_0100;
        expected[3] = 7'b011_0000;
        expected[4] = 7'b001_1001;
        expected[5] = 7'b001_0010;
        expected[6] = 7'b000_0010;
        expected[7] = 7'b111_1000;
        expected[8] = 7'b000_0000;
        expected[9] = 7'b001_0000;

        errors = 0;

        for (i = 0; i <= 9; i = i + 1) begin
            bcd = i[3:0]; 
            #10;
            if (seg !== expected[i]) begin
                $display("FAIL digit %0d: got %b, expected %b", i, seg, expected[i]);
                errors = errors + 1;
            end else begin
                $display("PASS digit %0d: seg=%b", i, seg);
            end
        end

        // Verify blank display for invalid BCD input
        bcd = 4'd10; 
        #10;
        if (seg !== 7'b111_1111) begin
            $display("FAIL invalid BCD: got %b", seg);
            errors = errors + 1;
        end

        if (errors == 0) 
            $display("All seven-segment tests passed successfully.");
        $finish;
    end
endmodule

Execution Command

iverilog -o seg7_tb tb_seg7.v seg7_decoder.v && vvp seg7_tb

Practice Exercise

Add a 1-bit valid output flag to seg7_decoder. The flag asserts high when bcd represents a valid decimal value (0 through 9). Write the logic using a single assign statement.

Solution

Using a direct relational comparison:

assign valid = (bcd <= 4'd9);

Using gate-level bit pattern detection: values 10 to 15 (4'b1010 through 4'b1111) have bit 3 high and either bit 2 or bit 1 high:

assign valid = !(bcd[3] & (bcd[2] | bcd[1]));

Logic synthesis tools optimize both expressions into an identical minimal gate netlist.

The next article presents a complete worked dataflow example: designing and verifying a parameterized multi-bit magnitude comparator.

← Dataflow in Practice A Parameterised Comparator, Start to Finish →