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
assignand withalways_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.
seg is {g, f, e, d, c, b, a}, and a 1 lights the segment.- Cycle 1
dis 8. The arm for 8 runs and all seven segments light. - Cycle 2
dis 9. No arm matches, so no statement assignsseg. It keeps 1111111 and the display still shows 8. - Cycle 3
dis 3. The arm for 3 runs andsegis correct again. - Cycle 4
dis 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.
| Expression | Result | What it does |
|---|---|---|
bus[7:4] | a | the upper four bits |
bus[3:0] | 5 | the lower four bits |
bus[0] | 1 | one bit |
{bus[3:0], bus[7:4]} | 5a | the two halves swapped |
{4{bus[7]}} | 1111 | the 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.
| Bitwise | Result | Logical | Result |
|---|---|---|---|
x & y | 00 | x && y | 1 |
x | y | 11 | x || y | 1 |
~x | 01 | !x | 0 |
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.
d. The select block passes that arm's pattern to seg.- I/O
- Control / FSM
- Neutral
- Datapath
- Storage
- Data
- Control
- An arm matches
dis 3. The compare block finds the arm for 3, and the select block passes its pattern toseg.Follow the calls, values and waits
- data 1001111: Patterns → Select
- data 1001111: Select → seg[6:0]
- No arm and no default
dis 9 and no arm matches. The code says nothing, sosegmust keep its old value. Only a storage cell can do that: a latch, a one-bit memory.Follow the calls, values and waits
- data old seg: Latch → Select
- data old seg: Select → seg[6:0]
- With a default arm
The
defaultarm 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
- data 0000000: Patterns → Select
- 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.
| Decoder | Simulation of digits 0 to 9 | Lint | Synthesize |
|---|---|---|---|
no default arm | 0 of 10 digits wrong | Latch inferred for signal 'seg' | refused: found logic loop in module seg7 |
with a default arm | 0 of 10 digits wrong | no warning | 26 cells, 0 latches, 5 logic levels |
Measured on this site. The test passes both times, because it never sends a value above 9.
Fix 1: a default arm
Add
default: seg = 7'b0000000;as the last arm of thecase.Fix 2: a default value first
Write
seg = 7'b0000000;before thecase. The arms then replace it when they match.
Asked in interviews
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. For2'b10and2'b01,&gives00and&&gives 1.How do you avoid an unwanted latch?
Assign every output on every path of a combinational block. Use a
defaultarm, anelse, or a default value at the top. Write the block asalways_combso that the tools check it.Does
logicorregmean 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.
Synthesize the design to measure its cells and inspect the mapped gates.
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 wrongOriginal 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
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.
case (d)
4'd8: seg = 7'b1111111;
endcasecase (d)
4'd8: seg = 7'b1111111;
default: seg = 7'b0000000;
endcaseWrong 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.
always @(a)
y = a & b; // b is missingalways_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.assignandalways_combdescribe the same gates; insidealways_combuse=.- Assign every output on every path. A missing
defaultorelseasks for a latch. logicandregare types; the block, not the type, decides the hardware.
Practice
Quick reference: always_comb