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

Priority or Parallel

Digital circuit specifications determine whether selection logic requires priority arbitration or flat parallel decoding. An if-else chain establishes sequential priority. A case statement decodes mutually exclusive patterns in parallel. This article analyzes hardware trade-offs and explains how to select the correct construct.

Architectural Trade-Off Analysis

Structure Logic Delay Scaling Functional Precondition Typical Application
if-else Chain $O(N)$ linear delay through cascaded multiplexers Conditions can overlap; earlier conditions resolve contention Interrupt controllers, bus arbiters, exception handlers
case Statement $O(\log N)$ tree delay Conditions are mutually exclusive constants Instruction opcode decoders, arithmetic mode decoders
One-Hot Decode $O(1)$ constant depth AND-OR logic Exactly one input bit is active high One-hot state machine decoders, register select lines

Priority logic resolves contention when multiple requests assert simultaneously, but it accumulates propagation delay across cascaded multiplexer stages. Parallel decoding minimizes logic depth, but requires mutually exclusive input patterns.

One-Hot Decoding Pattern

When an input vector is guaranteed to be one-hot, use the case (1'b1) construct to describe a flat parallel lookup table:

always @(*) begin
    grant = 4'b0000;
    case (1'b1)
        req[0]:  grant = 4'b0001;
        req[1]:  grant = 4'b0010;
        req[2]:  grant = 4'b0100;
        req[3]:  grant = 4'b1000;
        default: grant = 4'b0000;
    endcase
end

Each branch tests a single bit against logic 1. The synthesis tool infers a flat AND-OR gate array.

Comparing Priority Chains and Parallel Tables

Consider an identical 8-way opcode decoder written in both styles:

// Style 1: Priority chain syntax (infers cascaded multiplexers)
always @(*) begin
    if      (opcode == 3'b000) result = op0_result;
    else if (opcode == 3'b001) result = op1_result;
    else if (opcode == 3'b010) result = op2_result;
    else if (opcode == 3'b011) result = op3_result;
    else if (opcode == 3'b100) result = op4_result;
    else if (opcode == 3'b101) result = op5_result;
    else if (opcode == 3'b110) result = op6_result;
    else                       result = op7_result;
end

// Style 2: Parallel case table (infers balanced parallel multiplexer tree)
always @(*) begin
    case (opcode)
        3'b000: result = op0_result;
        3'b001: result = op1_result;
        3'b010: result = op2_result;
        3'b011: result = op3_result;
        3'b100: result = op4_result;
        3'b101: result = op5_result;
        3'b110: result = op6_result;
        3'b111: result = op7_result;
    endcase
end

The case statement documents mutual exclusivity directly. Modern synthesis tools recognize that constants 3'b000 through 3'b111 are distinct, synthesizing both styles into equivalent balanced multiplexer trees. For wide decoders, write case statements to prevent unintended priority chains.

Intentional Priority Versus Accidental Priority

Hardware specifications require priority when multiple control signals assert concurrently:

  • Non-maskable interrupts must preempt general interrupts.
  • DMA bus controllers must arbitrate among competing peripheral requests.

Document intentional priority directly in source comments:

// Priority arbitration: NMI must preempt IRQ by specification

Avoid “accidental priority” bugs. An accidental priority bug occurs when an engineer writes a bus arbiter using if-else chains without an architectural priority requirement. If testbenches never assert multiple requests simultaneously, the defect escapes verification and causes starvation in production.

Parameterized Priority Encoder Example

`timescale 1ns / 1ps
`default_nettype none

module priority_encoder #(
    parameter N = 4
) (
    input  wire [N-1:0]         req,
    output reg  [$clog2(N)-1:0] grant,
    output reg                  valid
);

    integer i;

    always @(*) begin
        grant = {$clog2(N){1'b0}};
        valid = 1'b0;

        // Iterate downward from highest index to lowest index
        for (i = N - 1; i >= 0; i = i - 1) begin
            if (req[i] && !valid) begin
                grant = i[$clog2(N)-1:0];
                valid = 1'b1;            // Guard flag locks priority to highest hit
            end
        end
    end

endmodule

Exhaustive Verification Testbench

`timescale 1ns / 1ps
`default_nettype none

module tb_priority_encoder;
    parameter N = 4;

    reg  [N-1:0]         req;
    wire [$clog2(N)-1:0] grant;
    wire                 valid;

    priority_encoder #(.N(N)) dut (
        .req  (req), 
        .grant(grant), 
        .valid(valid)
    );

    integer k;

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

        // Exhaustive test: verify all 2^N input combinations
        for (k = 0; k < (1 << N); k = k + 1) begin
            req = k[N-1:0];
            #5;
            $display("req=%04b grant=%0d valid=%b", req, grant, valid);
        end
        $finish;
    end
endmodule

Practice Exercise

A system receives three request signals: high_pri_req, med_pri_req, and low_pri_req. When multiple requests assert simultaneously, the arbiter must grant access to the highest-priority request. Select between priority and parallel logic, and implement the grant module.

Solution

Select priority logic. The specification requires contention resolution among overlapping requests.

`timescale 1ns / 1ps
`default_nettype none

module arbiter_3way (
    input  wire       high_pri_req,
    input  wire       med_pri_req,
    input  wire       low_pri_req,
    output reg  [1:0] grant
);
    always @(*) begin
        grant = 2'b00;    // Default assignment: no grant

        if      (high_pri_req) grant = 2'b01;
        else if (med_pri_req)  grant = 2'b10;
        else if (low_pri_req)  grant = 2'b11;
    end
endmodule

An if-else chain models the required priority structure directly.

This concludes Section 5.3. Section 5.4 analyzes procedural loops (for, while, repeat) and unrolled hardware synthesis.

← full_case and parallel_case for Loops and Synthesis →