Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Data Types and Signals

Signals and Variables

Understand the difference between VHDL signals and variables: when updates take effect, scheduled assignments, initial values, scope and which to choose.

If you remember one thing from this chapter, make it this: a signal assignment does not take effect immediately. It is scheduled, and the signal gets its new value only after the current process has finished running (one delta cycle later). A variable, in contrast, changes instantly, like a variable in C. Mixing up the two is the most common source of "my code behaves strangely" in VHDL.

Signals

A signal models a wire or a register. It is declared in an architecture (or a package, or a port), is visible to every process in that architecture, and is assigned with <=.

VHDL
signal count : unsigned(3 downto 0) := (others => '0');
...
count <= count + 1;

A signal has a current value and a queue of scheduled values. Reading a signal always returns the current value. When you write count <= count + 1;, the right side is evaluated with the current value and the result is scheduled. After every process has suspended, the simulator applies all scheduled updates at once, and processes that were waiting on changed signals wake up.

Variables

A variable exists only inside a process or subprogram. It is assigned with := and updates immediately:

VHDL
process (a, b)
  variable tmp : unsigned(7 downto 0);
begin
  tmp := a + b;          -- tmp has the new value right now
  y   <= tmp + 1;        -- uses the new tmp; y is scheduled
end process;

Variables keep their value between process activations (a process is like an infinite loop), but they are local: no other process can see them (VHDL-93 added shared variables, which are rarely safe for hardware).

Side by side

Signal Variable
Assignment operator <= :=
Takes effect After the process suspends (next delta) Immediately
Declared in Architecture, package, ports Process, subprogram
Visible to All processes in scope Only its own process
Hardware meaning Wire or register Intermediate value (usually just wires)

The classic experiment

Consider three statements inside a clocked process:

VHDL
process (clk)
begin
  if rising_edge(clk) then
    a <= d;
    b <= a;
    c <= b;
  end if;
end process;

Because every right-hand side reads the current values, a, b and c form a three-stage shift register: the input d reaches c after three clock edges. If you replaced these with variables and kept the order a := d; b := a; c := b;, all three would get d at the first edge. Reversing the order of the variable statements would recreate the shift register. With signals the order never matters; with variables it does.

The C stand-in below mimics both behaviors. A "signal" assignment writes to a next slot that is copied into current after the clock edge.

C Stand-in (C): signals vs variables on 3 clock edges

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.

Several assignments to the same signal

If a process assigns the same signal twice, the last assignment wins, because each one replaces the pending value:

VHDL
process (clk)
begin
  if rising_edge(clk) then
    count <= count + 1;
    count <= count + 2;     -- only this one takes effect: +2
  end if;
end process;

This is a legal idiom for defaults: assign a default value at the top of the process and override it later inside an if. It also shows why x <= x + 1; x <= x + 1; adds only 1, not 2, per clock.

C Stand-in (C): last signal assignment wins

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.

Initial values and reset

Signals start as 'U' (for std_logic) or the leftmost value of their type unless you give an initial value in the declaration. On FPGAs, initial values in declarations are usually honored (flip-flops are loaded at configuration), but ASIC flows ignore them. For portable, robust designs, use a proper reset signal and do not rely on initial values for anything important.

When to use which

  • Use signals to communicate between processes and for anything that must be a register across clock cycles.
  • Use variables for temporary values inside a process: intermediate sums, loop counters, results of helper functions.
  • A variable can still become a flip-flop: if it is read before being assigned in a clocked process, the tool must remember it between clock edges.

Do not read a signal right after assigning it and expect the new value: y <= a and b; z <= y; in the same process uses the old y. In a combinational process, put every signal you read into the sensitivity list, or use process (all) in VHDL-2008.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does a signal assignment s <= x; inside a process take effect?

    Signal updates are scheduled and applied after all running processes finish, in the next delta cycle.
  2. 2. With signals, a <= d; b <= a; c <= b; in a clocked process creates...

    Each right-hand side reads the current value, so data moves one stage per clock edge.
  3. 3. Which operator assigns a variable?

    Variables use :=, signals use <=.
  4. 4. A process assigns x <= 1; and later x <= 2; in the same run. What does x become?

    Each assignment replaces the pending value, so the last one wins.

Summary

  • Signals model wires and registers; assignments (<=) are scheduled and applied after the process finishes.
  • Variables (:=) are local to a process and update instantly.
  • Signal reads give the current value, which explains shift registers written as plain assignments.
  • The last assignment to a signal in a process wins.
  • Use reset rather than initial values for robust hardware.

Next: Vectors, Arrays and Slices