Verilog gives you two ways to describe logic: continuous assign and always blocks. Both can build combinational or sequential circuits โ but only when paired with the right sensitivity list and assignment operator. This lesson maps each construct to hardware.
assign for simple combo connections. always @(*) for procedural combo. always @(posedge clk) for sequential. Never mix drivers.
A continuous assignment drives a wire whenever any signal on the right-hand side changes. In simulation it behaves like a permanent wire connection; in synthesis it becomes combinational logic.
wire y, carry; assign y = a ^ b; assign carry = a & b; // Conditional (mux) with assign wire [7:0] result; assign result = sel ? b : a; // Vector operations wire [3:0] nibble; assign nibble = data[7:4];
| Feature | assign |
|---|---|
| Target type | wire only |
| Evaluation | Continuous โ re-evaluates on any RHS change |
| Synthesis | Combinational logic only |
| Best for | Simple gates, muxes, bit slices, connects |
| Multiple statements? | One per assign; use multiple assign lines |
assign is like soldering two points together โ the output is permanently "listening" to the inputs. Change the input, the output updates immediately (after gate delay in sim).
The always @(*) block (or always @(...) with full sensitivity) describes combinational logic procedurally. Use blocking assignment (=) inside. Every path must assign every output โ or you get latches.
reg [7:0] result;
always @(*) begin
case (opcode)
3'b000: result = a + b;
3'b001: result = a - b;
3'b010: result = a & b;
default: result = 8'd0;
endcase
end
Both build combinational logic. Choose based on readability, not capability.
Clocked always blocks describe sequential logic. Use non-blocking assignment (<=) for flops. Include async reset in the sensitivity list when needed.
reg [7:0] count;
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
count <= 8'd0;
else if (en)
count <= count + 8'd1;
end
| always form | Assignment | Hardware | Sensitivity |
|---|---|---|---|
always @(*) | = blocking | Combinational | All inputs (auto with *) |
always @(posedge clk) | <= non-blocking | Flip-flop | Clock edge (+ async reset) |
always @(a or b) | = blocking | Combo (legacy style) | Explicit signal list |
always @(posedge clk) | = blocking | โ ๏ธ Wrong โ sim/synth mismatch | Don't do this |
Golden rule: Combo always โ blocking =. Clocked always โ non-blocking <=. Mixing them in a clocked block is the #1 beginner bug (next lesson).
In simulation, an always block only runs when a signal in its sensitivity list changes. Incomplete lists cause simulation/synthesis mismatches โ the hardware always "sees" everything, but your sim might not.
// GOOD โ @(*) auto-includes all RHS inputs always @(*) y = a & b; // LEGACY โ manual list (error-prone) always @(a or b) y = a & b; // BAD โ missing b in sensitivity list always @(a) y = a & b; // sim won't update when b changes! // SEQUENTIAL โ only clock (and async reset) always @(posedge clk) q <= d; // d NOT in sensitivity โ correct for flops
| List style | When to use | Risk |
|---|---|---|
@(*) or @(*) | All combinational always | Low โ tool fills list |
@(a or b or c) | Legacy Verilog | High โ easy to omit a signal |
@(posedge clk) | Sequential | Low โ only clock matters for sync logic |
@(posedge clk or negedge rst_n) | Async reset flops | Low โ standard template |
Using always @(clk) instead of always @(posedge clk) โ triggers on both edges in simulation, but synthesis only builds rising-edge flops. Always use explicit edge keywords.
// Style A: assign (concise for simple mux) wire [7:0] out_a; assign out_a = sel ? b : a;
// Style B: always @(*) (better for multi-output / complex)
reg [7:0] out_b;
always @(*) begin
if (sel)
out_b = b;
else
out_b = a;
end
Both synthesize to the same 2:1 mux. Pick the style that reads clearest for your team. Many codebases use always @(*) for all combo and assign only for trivial connects.
| Scenario | Recommended | Why |
|---|---|---|
| Simple gate (AND, XOR, invert) | assign | One line, obvious |
| 2:1 mux, bit slice | assign | Ternary is clean |
| Case statement / FSM next-state (combo) | always @(*) | Branching is readable |
| Counter, shift register, pipeline | always @(posedge clk) | Only always does sequential |
| Multiple related combo outputs | always @(*) | One block, shared logic |
| Tie-off, constant connect | assign | assign unused = 1'b0; |
Concise, no sensitivity worries, impossible to infer latches accidentally, great for dataflow-style RTL.
Handles if/case/for loops, multiple outputs in one block, sequential logic, complex control flow.
assign inside an always block โ illegalassign and always โ multi-driver errorassign โ no flops from assign alonereg) with assign โ type mismatch// ILLEGAL examples โ do not compile // assign inside always always @(*) assign x = a; // syntax error // dual drivers wire y; assign y = a; always @(*) y = b; // multi-driver // flop attempt with assign assign q = d; // combo only โ not a register!
"Can you build a flip-flop with assign?" โ No. assign is purely combinational. Flops require a clocked always block (or a library cell instance).
| Verilog | SystemVerilog | Intent |
|---|---|---|
assign y = expr; | assign y = expr; | Same โ continuous |
always @(*) | always_comb | Combo โ tool checks completeness |
always @(posedge clk) | always_ff | Sequential โ tool enforces <= |
always_comb automatically triggers once at time 0 and errors if you accidentally infer latches or write to the same variable from multiple blocks.
assign = continuous combinational drive on wire.always @(*) + = = procedural combinational on reg/logic.always @(posedge clk) + <= = sequential (flip-flops).@(*) for auto sensitivity โ never guess signal lists.