0
0
VHDLprogramming~5 mins

Signal vs variable vs constant in VHDL

Choose your learning style9 modes available
Introduction

Signals, variables, and constants are ways to store and change data in VHDL. They help control how your digital circuit behaves.

Use a signal when you want to communicate between different parts of your circuit or between processes.
Use a variable inside a process when you need temporary storage that updates immediately.
Use a constant when you have a fixed value that does not change during simulation.
Syntax
VHDL
signal signal_name : type := initial_value;
variable variable_name : type := initial_value;
constant constant_name : type := value;

Signals are declared outside processes, variables inside processes.

Constants must be assigned a value when declared and cannot change.

Examples
This declares a signal named clk of type std_logic with initial value '0'.
VHDL
signal clk : std_logic := '0';
This declares a variable count inside a process that updates immediately when assigned.
VHDL
process
  variable count : integer := 0;
begin
  count := count + 1;
end process;
This declares a constant MAX_VALUE with a fixed value 255.
VHDL
constant MAX_VALUE : integer := 255;
Sample Program

This example shows a signal sig_a, a variable var_c, and a constant CONST_B. The signal updates after the process ends, the variable updates immediately, and the constant stays the same.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity example is
end example;

architecture behavior of example is
  signal sig_a : integer := 0;
  constant CONST_B : integer := 10;
begin
  process
    variable var_c : integer := 0;
  begin
    sig_a <= sig_a + 1;  -- signal updates after process ends
    var_c := var_c + 1;   -- variable updates immediately
    report "Signal sig_a = " & integer'image(sig_a);
    report "Variable var_c = " & integer'image(var_c);
    report "Constant CONST_B = " & integer'image(CONST_B);
    wait for 10 ns;
  end process;
end behavior;
OutputSuccess
Important Notes

Signals represent wires and update after the process finishes.

Variables are like local memory inside a process and update immediately.

Constants never change and help make your code clearer and safer.

Summary

Signals communicate between processes and update after process ends.

Variables are temporary inside processes and update immediately.

Constants hold fixed values that never change.