SystemVerilog for Combinational Logic

Describe combinational logic with vectors, operators, assign, always_comb, if and case, and avoid the missing branch that builds a latch.

You Will Be Able To

  • Read vectors, part-selects, concatenation and replication.
  • Choose between a bitwise and a logical operator.
  • Write one mux with assign and with always_comb.
  • Give every path a value so that no latch is built.
Lesson
G4.1
Time
25 min
Runs on
Your browser
Check
5 questions

A lift shows its floor on a seven-segment display. At floor 9 the display still shows 8. The decoder has an arm for the digits 0 to 8 and none for 9. With no arm to run, the output keeps its old value, and that is memory.

The Decoder with No Arm for 9One digit per step. seg is {g, f, e, d, c, b, a}, and a 1 lights the segment.
01234d78939seg000011111111111001111display shows783asks for 9, shows 8
Scroll horizontally ↔
  1. Cycle 1

    d is 8. The arm for 8 runs and all seven segments light.

  2. Cycle 2

    d is 9. No arm matches, so no statement assigns seg. It keeps 1111111 and the display still shows 8.

  3. Cycle 3

    d is 3. The arm for 3 runs and seg is correct again.

  4. Cycle 4

    d is 9 once more. Now the display shows 3. The wrong digit depends on what came before.

Measured on the simulator. An output that depends on the past is not combinational.

Vectors, Part-Selects and Concatenation

A vector is a group of bits with one name. logic [7:0] bus has eight bits, and bit 7 is the most significant bit.

A part-select such as bus[7:4] picks some of the bits. Concatenation {x, y} joins values, with the left one on top. Replication {4{x}} repeats a value four times.

ExpressionResultWhat it does
bus[7:4]athe upper four bits
bus[3:0]5the lower four bits
bus[0]1one bit
{bus[3:0], bus[7:4]}5athe two halves swapped
{4{bus[7]}}1111the top bit, four times

Measured with bus = 8'hA5. Multi-bit results are printed in hex, the last one in binary.

Bitwise and Logical Operators

A bitwise operator works on each pair of bits and gives a vector. A logical operator treats each side as true or false and gives one bit. A value is true when any of its bits is 1.

BitwiseResultLogicalResult
x & y00x && y1
x | y11x || y1
~x01!x0

Measured with x = 2'b10 and y = 2'b01. No bit position has a 1 in both, yet both values are true.

So if (x & y) is false here and if (x && y) is true. Use &&, || and ! between conditions. Use &, |, ^ and ~ on data.

Comparisons give one bit: x == y is 0 and x > y is 1. Arithmetic keeps only the bits that fit. Here 4'd9 + 4'd8 is 17, but a four-bit result holds 1.

One Mux, Written Two Ways

A continuous assignment (assign) drives a signal from an expression at all times. The conditional operator s ? b : a gives b when s is 1, and a when it is 0.

An always_comb block describes the same logic with statements. It runs again each time one of its inputs changes. Inside it, use the blocking assignment =.

With assign

module mux2 (
  input  logic [3:0] a, b,
  input  logic       s,
  output logic [3:0] y
);
  assign y = s ? b : a;
endmodule

Synthesized on this site: 4 mux cells, 1 logic level.

With always_comb

module mux2 (
  input  logic [3:0] a, b,
  input  logic       s,
  output logic [3:0] y
);
  always_comb begin
    if (s) y = b;
    else   y = a;
  end
endmodule

The same result: 4 mux cells, 1 logic level.

Both forms become the same gates. Use assign for one short expression. Use always_comb when the logic needs if or case.

Both use the type logic. The type does not choose the hardware. A flip-flop appears only when a clock edge controls the block, which is the subject of G6.1.

Every Path Must Assign the Output

A case statement compares d with each arm and runs the first one that matches. The default arm runs when no other arm matches.

What a Case Statement BecomesArrows show signals. The compare block finds the arm that matches d. The select block passes that arm's pattern to seg.
  • I/O
  • Control / FSM
  • Neutral
  • Datapath
  • Storage
  • Data
  • Control
CallValueReturnWaiting
What a case statement becomesArrows show signals. The compare block finds the arm that matches `d`. The select block passes that arm's pattern to `seg`. io d[3:0] the digit control Compare one line per arm neutral Patterns seven bits per arm datapath Select passes one pattern storage Latch keeps the old seg io seg[6:0] to the display digitmatching armpatterndrivesold value
  1. io d[3:0]the digit
    • digit → Compare
  2. control Compareone line per arm
    • matching arm → Select
  3. neutral Patternsseven bits per arm
    • pattern → Select
  4. datapath Selectpasses one pattern
    • drives → seg[6:0]
  5. io seg[6:0]to the display
  6. storage Latchkeeps the old seg
    • old value → Select
  1. An arm matches

    d is 3. The compare block finds the arm for 3, and the select block passes its pattern to seg.

    Follow the calls, values and waits
    1. data 1001111: Patterns → Select
    2. data 1001111: Select → seg[6:0]
  2. No arm and no default

    d is 9 and no arm matches. The code says nothing, so seg must keep its old value. Only a storage cell can do that: a latch, a one-bit memory.

    Follow the calls, values and waits
    1. data old seg: Latch → Select
    2. data old seg: Select → seg[6:0]
  3. With a default arm

    The default arm gives the pattern 0000000 for every other value. Every path now has a pattern, so no latch is needed.

    Follow the calls, values and waits
    1. data 0000000: Patterns → Select
    2. data 0000000: Select → seg[6:0]
Read the diagram as text
  • d[3:0]: the digit
  • Compare: one line per arm
  • Patterns: seven bits per arm
  • Select: passes one pattern
  • Latch: keeps the old seg
  • seg[6:0]: to the display
  • d[3:0] → Compare: digit
  • Compare → Select: matching arm
  • Patterns → Select: pattern
  • Select → seg[6:0]: drives
  • Latch → Select: old value

A path with no assignment asks for memory. A default arm removes the question.

The decoder below was measured with every digit from 0 to 9 written, in two versions.

DecoderSimulation of digits 0 to 9LintSynthesize
no default arm0 of 10 digits wrongLatch inferred for signal 'seg'refused: found logic loop in module seg7
with a default arm0 of 10 digits wrongno warning26 cells, 0 latches, 5 logic levels

Measured on this site. The test passes both times, because it never sends a value above 9.

  1. Fix 1: a default arm

    Add default: seg = 7'b0000000; as the last arm of the case.

  2. Fix 2: a default value first

    Write seg = 7'b0000000; before the case. The arms then replace it when they match.

Asked in interviews
  1. What is the difference between & and &&?

    & is bitwise: it works on each pair of bits and gives a vector. && is logical: it gives 1 when both sides are non-zero. For 2'b10 and 2'b01, & gives 00 and && gives 1.

  2. How do you avoid an unwanted latch?

    Assign every output on every path of a combinational block. Use a default arm, an else, or a default value at the top. Write the block as always_comb so that the tools check it.

  3. Does logic or reg mean a flip-flop?

    No. The type only allows the signal to be assigned in a block. A clock edge in the block makes a flip-flop. A missing path in a combinational block makes a latch. Otherwise the result is gates.

In Plain Verilog

Older code is written in Verilog-2001. There, wire is the type for a signal driven by assign, and reg is the type for a signal assigned in an always block. The block always @* is the older form of always_comb.

SystemVerilog

logic [3:0] y;
always_comb begin
  if (s) y = b;
  else   y = a;
end

One type for every signal. The block says what kind of logic it is.

Verilog-2001

reg [3:0] y;
always @* begin
  if (s) y = b;
  else   y = a;
end

The same mux. Here reg does not mean register: this y is plain gates.

The older block makes no promise, so the tools check less. With always @*, the decoder with no arm for 9 synthesizes here without an error. The report says 7 latch cell(s) inferred. Lesson R1.3 in the RTL Design track goes deeper.

Try It

The decoder has no arm for 9, so the default arm turns every segment off. The bench marks that line as wrong.

Add the arm 4'd9: seg = 7'b1101111; and run again. The last line becomes 0 of 10 digits wrong. Press Synthesize: the Gates tab shows 26 cells and no latch.

Now delete the default arm and run. The bench still reports 0 wrong digits, because it never sends a value above 9. Press Lint: it reports Latch inferred for signal 'seg'. Press Synthesize: the design is refused.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: d=0 seg=0111111 d=1 seg=0000110 d=2 seg=1011011 d=3 seg=1001111 d=4 seg=1100110 d=5 seg=1101101 d=6 seg=1111101 d=7 seg=0000111 d=8 seg=1111111 d=9 seg=0000000 wrong 1 of 10 digits wrong

Original design · 25 cells · 0 flip-flops · 0 latches · 5 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
// tb.sv: the bench (read-only). It shows every digit from 0 to 9 and checks the segments.
module tb;
  logic [3:0] d;
  logic [6:0] seg;
  int wrong = 0;
  seg7 dut (.d(d), .seg(seg));

  // The pattern each digit needs, as {g, f, e, d, c, b, a}.
  function automatic logic [6:0] want(input logic [3:0] digit);
    case (digit)
      4'd0: return 7'b0111111;
      4'd1: return 7'b0000110;
      4'd2: return 7'b1011011;
      4'd3: return 7'b1001111;
      4'd4: return 7'b1100110;
      4'd5: return 7'b1101101;
      4'd6: return 7'b1111101;
      4'd7: return 7'b0000111;
      4'd8: return 7'b1111111;
      default: return 7'b1101111;   // digit 9
    endcase
  endfunction

  initial begin
    for (int i = 0; i < 10; i++) begin
      d = i[3:0];
      #1;                                   // wait for the logic to settle
      if (seg !== want(d)) wrong++;
      $display("d=%0d seg=%b%s", d, seg, seg === want(d) ? "" : "  wrong");
    end
    $display("%0d of 10 digits wrong", wrong);
  end
endmodule

Check Yourself

Predict the outputPredict the exact line this program prints.
module tb;
  initial begin
    $display("%b", {2{2'b10}});
  end
endmodule
Spot the bugEach option is the body of an always_comb block. The decoder knows the digits 8 and 9 and must turn all segments off for any other value. The bench sends 8 and then 12. Which body fails?
Conceptx is 2'b10 and y is 2'b01. Which line is correct?
Read the waveformA decoder has arms for the digits 2 and 4 only, and no default. In which step does seg first show a pattern that d did not ask for?
A decoder with two arms
0123d4929seg11001101011011
Scroll horizontally ↔

One value of d per step, from step 0. Measured on the simulator.

ConceptA signal is declared as logic [6:0] seg and assigned in an always_comb block on every path. What hardware does it become?

Common Mistakes

Wrong Leave out the default arm, or the else.

On the missing path nothing assigns the output, so it keeps its old value and needs a latch.

Fix Assign every output on every path: a default arm, an else, or a default value first.

Before
case (d)
  4'd8: seg = 7'b1111111;
endcase
After
case (d)
  4'd8: seg = 7'b1111111;
  default: seg = 7'b0000000;
endcase

Wrong Use <= inside an always_comb block.

The nonblocking assignment is for clocked blocks, and Lint warns: Non-blocking assignment '<=' in combinational logic process.

Fix Use the blocking assignment = in combinational blocks.

Wrong Write the sensitivity list by hand, as in always @(a), and forget an input.

Simulation ignores a change on the missing input, but synthesis still builds the full gate.

Fix Write always_comb. It reacts to every signal the block reads.

Before
always @(a)
  y = a & b;   // b is missing
After
always_comb
  y = a & b;

Wrong Read logic or reg as "this signal is a flip-flop".

The type only says the signal is assigned in a block; the block decides the hardware.

Fix Look for a clock edge. With none, and every path assigned, the result is plain gates.

Remember

  • A part-select picks bits, {x, y} joins values and {n{x}} repeats one.
  • &, | and ~ work bit by bit; &&, || and ! give one true-or-false bit.
  • assign and always_comb describe the same gates; inside always_comb use =.
  • Assign every output on every path. A missing default or else asks for a latch.
  • logic and reg are types; the block, not the type, decides the hardware.

Practice

Quick reference: always_comb

Report a problem with this page