Chuyển đến nội dung

Bài 12 · Combinational Logic

Combinational always Blocks: if, case and always @*

Write combinational logic in procedural always blocks: sensitivity lists, always @*, always_comb, if/else, case, casez, default values, avoiding latches and a runnable C model.

assign is great for one-line expressions, but multi-way decisions are clearer in a procedural block: an always block with if and case statements. The big rule of this lesson: a combinational always block must assign every output on every path, or you create unwanted memory (a latch).

The always block

An always block repeats forever: it waits for an event in its sensitivity list, runs its body, then waits again.

VERILOG
always @(a or b or sel)      // Verilog-95/2001 explicit list
    if (sel) y = a;
    else     y = b;

always @(*)  y = sel ? a : b;   // automatic list
always @*    y = sel ? a : b;   // same, shorter
always_comb  y = sel ? a : b;   // SystemVerilog: best choice

If you forget a signal in an explicit list, simulation misses changes while synthesis ignores your list: the two behaviors mismatch. Use @* (or always_comb) for combinational blocks so the list is automatically complete. always_comb also runs once at time zero and warns about latches.

Variables assigned inside must be reg (or logic). Use blocking assignment (=) in combinational blocks; the difference from <= comes in the sequential chapter.

if / else

VERILOG
always @* begin
    if (sel == 2'b00)      y = a;
    else if (sel == 2'b01) y = b;
    else if (sel == 2'b10) y = c;
    else                   y = d;
end

An if/else if chain describes priority: the first true condition wins, producing a chain of multiplexers. A multi-statement branch needs begin ... end.

case

When one expression selects among many values, case is cleaner and is not priority-based by default:

VERILOG
always @* begin
    case (sel)
        2'b00: y = a;
        2'b01: y = b;
        2'b10: y = c;
        2'b11: y = d;
        default: y = 1'bx;
    endcase
end
  • Cases are tested in order with ===-style exact matching (including x and z).
  • default catches everything not listed. Always include it.
  • Several values can share a branch: 2'b00, 2'b01: y = 1'b0;.
  • Each branch is one statement; use begin ... end for more.

casez and casex

casez treats z (written ?) as a wildcard, handy for priority encoding:

VERILOG
always @* begin
    casez (req)
        4'b1???: grant = 2'd3;
        4'b01??: grant = 2'd2;
        4'b001?: grant = 2'd1;
        4'b0001: grant = 2'd0;
        default: grant = 2'd0;
    endcase
end

casex also treats x as a wildcard and is dangerous because an unknown input can match a branch it should not; avoid it. SystemVerilog adds unique case and priority case to state your intent.

The latch trap

If a combinational block fails to assign an output on some path, the output must hold its old value, and synthesis infers a latch, a level-sensitive storage element:

VERILOG
// BUG: no else, so y holds its value when en is 0
always @*
    if (en) y = d;

Two standard cures:

  1. Give outputs a default at the top of the block, then override:
VERILOG
always @* begin
    y = 1'b0;            // default for every path
    if (en) y = d;
end
  1. Cover every branch (else, default).

Synthesis reports "inferred latch" as a warning. Treat it as an error unless you wanted a latch.

Procedural statements allowed

Inside a combinational block you may use if, case, for (with constant bounds, it unrolls), local temporaries and function calls. You may not use delays, wait or initial-style code in synthesizable logic.

VERILOG
always @* begin
    parity = 1'b0;
    for (i = 0; i < 8; i = i + 1)
        parity = parity ^ data[i];
end

The loop is replaced by a chain of XOR gates, one per iteration.

Functions

A function is a combinational calculation with a return value, callable from assign or always:

VERILOG
function [3:0] ones_count;
    input [7:0] v;
    integer i;
    begin
        ones_count = 4'd0;
        for (i = 0; i < 8; i = i + 1)
            ones_count = ones_count + v[i];
    end
endfunction

assign n = ones_count(data);

Functions cannot contain delays or timing controls and return a single value.

Stand-in: case and priority in C

The C switch is the stand-in for case, and an if chain gives the priority encoder. This is not Verilog: it computes the grant for all 16 request patterns, mirroring the casez above, and counts ones as the function does.

C Stand-in: casez-style priority encoder and ones count (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Stand-in: modeling a latch (C)

In C, a missing assignment simply keeps the previous value of a variable. That is exactly the behavior of a latch, which is why the Verilog code above creates storage. The program below makes the difference visible: the first function is combinational, the second "forgets" to assign.

C Stand-in: why a missing else creates storage (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

When en is 0 (rows 2, 3, 5, 6), the combinational output is 0 but the latch keeps its last value. In hardware, that memory costs a latch cell and usually breaks timing analysis.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which sensitivity list is best for a combinational block?

    @* includes every signal read in the block, so simulation matches synthesis.
  2. 2. What creates an unintended latch?

    If a path leaves an output unassigned, the output must hold its old value, which requires storage.
  3. 3. Which assignment type is normal in a combinational always block?

    Use blocking assignments for combinational procedural logic.
  4. 4. What does a ? in a casez item mean?

    In casez, ? (or z) in the item matches any value on that bit.

Summary

  • A combinational always block uses @* or always_comb, blocking assignments and reg/logic outputs.
  • if/else if makes priority chains; case selects among values; casez allows wildcards; avoid casex.
  • Assign every output on every path (defaults first, default: in case) or you infer a latch.
  • for loops with constant bounds unroll into hardware; functions package combinational calculations.

Next: Multiplexers, Decoders and Encoders

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.