Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Combinational Logic

Continuous Assignments with assign in Verilog

Learn how the Verilog assign statement describes combinational logic: continuous assignment, expressions, conditional muxes, implicit wires, multiple drivers and delays.

Combinational logic is logic whose outputs depend only on the current inputs, with no memory. The simplest way to describe it in Verilog is the continuous assignment, written with assign. It is the line-by-line equivalent of drawing gates on a schematic.

The basic form

VERILOG
assign <net> = <expression>;

The expression is evaluated whenever any operand changes, and the result drives the net. There is no "execution order": every assign is its own piece of hardware that is always active.

VERILOG
module logic_demo (
    input  wire a, b, c,
    output wire y1, y2
);
    assign y1 = a & b;
    assign y2 = (a | b) & ~c;
endmodule

You can read this as a schematic: y1 is an AND gate; y2 is an OR gate feeding an AND with an inverted c.

Declaring and assigning in one step

A net can be declared with its assignment:

VERILOG
wire is_zero = (value == 8'd0);
wire [3:0] nibble = byte[7:4];

This is shorthand for a declaration followed by an assign.

Building bigger logic from small assigns

Break complex expressions into named intermediate wires. It reads better and gives you signals to look at in a waveform.

VERILOG
module majority (
    input  wire a, b, c,
    output wire y
);
    wire ab = a & b;
    wire bc = b & c;
    wire ac = a & c;
    assign y = ab | bc | ac;
endmodule

The majority gate outputs 1 when at least two of its three inputs are 1; it is the carry function of a full adder.

Conditional operator as a multiplexer

VERILOG
assign y = sel ? d1 : d0;                 // 2:1 mux
assign out = (op == 2'd0) ? a + b :
             (op == 2'd1) ? a - b :
             (op == 2'd2) ? a & b :
                            a | b;        // priority chain of muxes

Nested ?: creates a chain; the first true condition wins. For selecting from many equal-priority choices a case statement in an always block (see always_comb and case) is clearer.

Rules for assign

  • The left side must be a net (wire), a bit-select, part-select or concatenation of nets.
  • A net can have one driver in normal logic. Two assign statements on the same wire fight: in simulation the value turns x when they disagree; in synthesis it is an error unless the driver outputs are tri-state.
  • Order does not matter: you could list assigns in any sequence.
  • Feedback is legal syntactically (a net depending on itself) but forms a combinational loop, which is almost always a bug.
VERILOG
assign a = b;    // fine
assign b = a;    // combinational loop with the line above! (self-referential)

Implicit wires

If you use an undeclared name on the left of an assign, Verilog creates a one-bit implicit wire. A typo in a vector name then silently creates a 1-bit wire and leaves the real bus undriven. Put `default_nettype none at the top of your files to make that an error.

Delays (simulation only)

VERILOG
assign #5 y = a & b;    // y changes 5 time units after the inputs change

Delays model gate propagation in simulation. Synthesis ignores them, so do not rely on them for design behavior.

What about x?

Continuous assignments follow the four-state rules. If a = 0 and b = x, then a & b is 0 but a | b is x. This is why a testbench can reveal uninitialized inputs without failing.

Stand-in: a combinational circuit as a pure function (C)

A continuous assignment is like a pure function of the current inputs: no state, no order. This stand-in (not Verilog) expresses the majority gate and a small ALU-like mux as pure functions and prints their truth tables.

C Stand-in: majority gate and 4-way mux as pure functions (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.

Because the functions have no side effects, calling them in any order gives the same answers. That is exactly how assign statements behave in hardware.

Common mistakes

  • Trying to assign a reg (use a wire, or move to an always block).
  • Writing two assign statements for the same signal, or driving a wire both from assign and from a module output.
  • Using = inside expressions: assign y = (a = b); is not equality; use ==.
  • Forgetting parentheses in ?: chains, which are right-associative and hard to read.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When is an assign expression re-evaluated?

    Continuous assignments react to any change in their right-hand side.
  2. 2. Does the order of assign statements in a module matter?

    Each assign is concurrent hardware, so ordering has no effect.
  3. 3. What happens when two assigns drive the same wire with different values?

    Multiple drivers conflict; plain nets resolve disagreeing 0 and 1 to x, and synthesis rejects it.
  4. 4. What does `default_nettype none do?

    It prevents typos from silently creating new wires.

Summary

  • assign describes combinational logic that is always active and re-evaluated on any input change.
  • Left side must be a net; each net should have a single driver; order is irrelevant.
  • ?: builds multiplexers; split big expressions into named intermediate wires.
  • Delays on assigns are simulation only, and implicit wires are a typo trap.

Next: Gate-Level Primitives