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

case

While an if-else chain establishes sequential priority, a case statement evaluates an expression against a list of candidate values. When candidate values represent mutually exclusive constants, the synthesis tool infers a flat parallel decoder and a balanced multiplexer tree.

Synthesized Hardware Architecture

always @(*) begin
    case (alu_op)
        2'b00:   result = a + b;
        2'b01:   result = a - b;
        2'b10:   result = a & b;
        2'b11:   result = a | b;
        default: result = 8'hxx;
    endcase
end

In an if-else chain with eight branches, worst-case signals traverse up to seven cascaded multiplexers. In a case statement with distinct constant branches, all branch decoders operate in parallel at uniform logic depth. For wide opcode decoders, parallel case evaluation prevents critical path timing bottlenecks.

In language simulation semantics, Verilog executes the first matching branch. When branch constants are mutually exclusive, multiple matches cannot occur. The synthesis tool recognizes mutual exclusion and flattens the priority structure into parallel logic.

Grouping Multiple Values in a Single Branch

A comma-separated list groups multiple values into a shared branch:

case (state)
    IDLE, RESET: next_state = INIT;
    RUN:         next_state = EVAL;
    default:     next_state = IDLE;
endcase

The synthesis tool infers an OR operation across grouped values (IDLE or RESET).

The Critical Function of the default Branch

Omitting input patterns without assigning default outputs infers an unintended transparent latch. The default branch guarantees complete assignment:

  • Don’t-Care Optimization (default: result = 'x;): Informs the synthesis tool that unspecified values never occur in physical operation. The optimizer reduces gate count by treating unlisted patterns as “don’t cares”.
  • Deterministic Default (default: result = 1'b0;): Drives known inactive values if illegal states occur during hardware execution.

Choose deterministic safe values when designing safety-critical state machines.

Decoding State Machine Registers

The case statement provides the standard construct for decoding state registers:

always @(*) begin
    active_led = 1'b0;
    done_led   = 1'b0;

    case (state)
        ACTIVE:  active_led = 1'b1;
        DONE:    done_led   = 1'b1;
        default: ; // IDLE and unlisted states leave outputs at default 0
    endcase
end

Complete ALU Decoder with Verification Testbench

`timescale 1ns / 1ps
`default_nettype none

module alu_decoder (
    input  wire [2:0] op,
    input  wire [7:0] a, b,
    output reg  [7:0] result
);

    always @(*) begin
        case (op)
            3'b000:  result = a + b;
            3'b001:  result = a - b;
            3'b010:  result = a & b;
            3'b011:  result = a | b;
            3'b100:  result = a ^ b;
            3'b101:  result = ~a;
            3'b110:  result = a << 1;
            3'b111:  result = a >> 1;
            default: result = 8'hxx;
        endcase
    end

endmodule

module tb_alu_decoder;
    reg  [2:0] op;
    reg  [7:0] a, b;
    wire [7:0] result;

    alu_decoder dut (
        .op    (op), 
        .a     (a), 
        .b     (b), 
        .result(result)
    );

    task check;
        input [2:0] o;
        input [7:0] exp;
        begin
            op = o; 
            #5;
            if (result !== exp)
                $display("FAIL: op=%03b result=%h (expected %h)", o, result, exp);
            else
                $display("PASS: op=%03b result=%h", o, result);
        end
    endtask

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

        a = 8'h0F; 
        b = 8'h03;

        check(3'b000, a + b);
        check(3'b001, a - b);
        check(3'b010, a & b);
        check(3'b011, a | b);
        check(3'b100, a ^ b);     // 0x0F ^ 0x03 = 0x0C
        check(3'b101, ~a);
        check(3'b110, a << 1);
        check(3'b111, a >> 1);

        $finish;
    end
endmodule

Selection Summary

  • Use case: When matching an expression against mutually exclusive constant states or opcode fields. It produces flat parallel decoders.
  • Use if-else: When decisions evaluate multiple independent signals with strict priority order.

The next article examines wildcard case statements (casez and casex) and explains why casex introduces hazardous simulation defects.

← if and else casez, and Why Not casex →