Module 5: Behavioral Verilog for Synthesis · 5.3 Conditional and Case Statements

if and else

In software programming, an if statement executes one branch while ignoring unselected branches. In digital hardware, all branch circuits exist and evaluate concurrently in silicon. The if-else construct infers a chain of 2-to-1 multiplexers that selects which evaluated result drives the output net.

The Priority Multiplexer Cascade

Consider a standard if-else chain:

always @(*) begin
    if (cond_a)
        y = a;
    else if (cond_b)
        y = b;
    else
        y = c;
end

This structure synthesizes into two cascaded 2-to-1 multiplexers (Figure 1). The first multiplexer selects a when cond_a is true. Otherwise, it selects the output of the second multiplexer.

Priority mux chain
Figure 1: Cascaded 2-to-1 multiplexer chain where earlier conditions evaluate with higher priority

If both cond_a and cond_b assert simultaneously, input a wins. Earlier conditions take precedence over subsequent conditions.

Priority Arbitration Applications

Hardware specifications frequently require priority arbitration. An interrupt controller must service a non-maskable interrupt (NMI) before servicing lower-priority peripheral requests:

`timescale 1ns / 1ps
`default_nettype none

module interrupt_priority_enc (
    input  wire       nmi,
    input  wire [3:0] irq,          // irq[3] is highest maskable priority
    output reg  [2:0] int_vec,      // 0 = None; 1 to 4 = IRQ; 5 = NMI
    output reg        int_valid
);

    always @(*) begin
        int_vec   = 3'd0;
        int_valid = 1'b0;

        if (nmi) begin
            int_vec   = 3'd5;
            int_valid = 1'b1;
        end else if (irq[3]) begin
            int_vec   = 3'd4;
            int_valid = 1'b1;
        end else if (irq[2]) begin
            int_vec   = 3'd3;
            int_valid = 1'b1;
        end else if (irq[1]) begin
            int_vec   = 3'd2;
            int_valid = 1'b1;
        end else if (irq[0]) begin
            int_vec   = 3'd1;
            int_valid = 1'b1;
        end
    end

endmodule

When nmi asserts, the controller routes vector 5 immediately, overriding active peripheral interrupts. Bus arbiters and exception handlers use this same cascading priority structure.

Propagation Delay Across Deep Chains

Each else if branch inserts an additional multiplexer into the critical timing path. A 4-level chain adds four multiplexer delays. A 32-level chain adds thirty-two cascaded multiplexers, which degrades clock operating frequency.

Limit if-else chains to eight or fewer priority levels. For wide decoders with mutually exclusive conditions, use parallel case statements.

Behavioral Semantics in Clocked Blocks

In a combinational always @(*) block, omitting an else branch infers an unintended transparent latch.

In a clocked always @(posedge clk) block, omitting an else branch infers a synchronous clock enable (Figure 2):

always @(posedge clk) begin
    if (load)
        q <= d;
    // When load is low, flip-flop retains current value q
end

The synthesis tool inserts a 2-to-1 multiplexer before the D input of the flip-flop. When load is 1, the multiplexer routes new data d. When load is 0, the multiplexer loops output q back to input D.

Register with enable
Figure 2: In clocked blocks, an if statement without an else branch infers a clock enable multiplexer

Complete Priority Encoder with Verification Testbench

`timescale 1ns / 1ps
`default_nettype none

module priority_enc4 (
    input  wire [3:0] req,     // req[3] is highest priority
    output reg  [1:0] enc,
    output reg        valid
);

    always @(*) begin
        enc   = 2'b00;
        valid = 1'b0;

        if      (req[3]) begin enc = 2'b11; valid = 1'b1; end
        else if (req[2]) begin enc = 2'b10; valid = 1'b1; end
        else if (req[1]) begin enc = 2'b01; valid = 1'b1; end
        else if (req[0]) begin enc = 2'b00; valid = 1'b1; end
    end

endmodule

module tb_priority_enc4;

    reg  [3:0] req;
    wire [1:0] enc;
    wire       valid;

    priority_enc4 dut (
        .req  (req), 
        .enc  (enc), 
        .valid(valid)
    );

    task check;
        input [3:0] r;
        input [1:0] expected_enc;
        input       expected_valid;
        begin
            req = r;
            #5;
            if (valid !== expected_valid || (expected_valid && enc !== expected_enc))
                $display("FAIL: req=%04b enc=%02b (exp %02b) valid=%b (exp %b)", 
                         r, enc, expected_enc, valid, expected_valid);
            else
                $display("PASS: req=%04b enc=%02b valid=%b", r, enc, valid);
        end
    endtask

    initial begin
        $dumpfile("tb.vcd");
        $dumpvars(0, tb_priority_enc4);

        check(4'b0000, 2'b00, 1'b0);     // Inactive request test

        check(4'b0001, 2'b00, 1'b1);     // Single request tests
        check(4'b0010, 2'b01, 1'b1);
        check(4'b0100, 2'b10, 1'b1);
        check(4'b1000, 2'b11, 1'b1);

        check(4'b1111, 2'b11, 1'b1);     // Priority resolution tests
        check(4'b1010, 2'b11, 1'b1);
        check(4'b0110, 2'b10, 1'b1);
        check(4'b0011, 2'b01, 1'b1);

        $finish;
    end

endmodule

Practice Exercise

A processor implements five exception sources, ordered from highest to lowest priority: machine_check, bus_fault, usage_fault, svc, and pend_sv. Construct an exception encoder module that generates a 3-bit vector exc_vec (values 5 down to 1, with 0 indicating no exceptions) and an exc_valid flag.

Solution
`timescale 1ns / 1ps
`default_nettype none

module exception_encoder (
    input  wire       machine_check,
    input  wire       bus_fault,
    input  wire       usage_fault,
    input  wire       svc,
    input  wire       pend_sv,
    output reg  [2:0] exc_vec,
    output reg        exc_valid
);

    always @(*) begin
        exc_vec   = 3'd0;
        exc_valid = 1'b0;

        if      (machine_check) begin exc_vec = 3'd5; exc_valid = 1'b1; end
        else if (bus_fault)     begin exc_vec = 3'd4; exc_valid = 1'b1; end
        else if (usage_fault)   begin exc_vec = 3'd3; exc_valid = 1'b1; end
        else if (svc)           begin exc_vec = 3'd2; exc_valid = 1'b1; end
        else if (pend_sv)       begin exc_vec = 3'd1; exc_valid = 1'b1; end
    end

endmodule

Because machine_check appears in the first condition, the first multiplexer stage evaluates it with highest priority.

The next article examines the case statement, which synthesizes parallel decoders without priority chain delays.

← Accidental Latches case →