VHDL Variable Vs. Signal
Asked Answered
V

5

37

I have been reading a text (Don't have it in front so can't give the title) about VHDL programming. One problem I've been having a hard time understanding from the text is when to use a variable vs a signal. I think I have a clear understanding of when to use a signal (internal signal that is) but not so much for a variable.

I did notice that the text generally declares and initializes signals before defining a process whereas a variable is declared (and I guess never initialized..) inside of a process.

Anyway to clear that up, either by definition or by example would be great!

Velleman answered 18/3, 2013 at 19:58 Comment(0)
G
43

Variables are used when you want to create serialized code, unlike the normal parallel code. (Serialized means that the commands are executed in their order, one after the other instead of together). A variable can exist only inside a process, and the assignment of values is not parallel. For example, consider the following code:

signal a,b : std_logic_vector(0 to 4);

process (CLK)
    begin
        if (rising_edge(clk)) then
            a <= '11111';
            b <= a;
        end if;
end process;

will put into b the value of a before the process ran, and not '11111'. On the other hand, the code:

signal a,b : std_logic_vector(0 to 4);

process (CLK)
    variable var : std_logic_vector(0 to 4);
    begin 
        if (rising_edge(clk)) then
            var := '11111';
            a <= var;
            b <= var;
        end if;
end process;

will put the value '11111' into both a and b.

Frankly, in my experience, most of the time you don't need to use variables, the only place I used it was in a loop where I needed to check if any of a number of signals is 1:

type    BitArray        is array (natural range <>) of std_logic;

--...

entity CAU_FARM is
    port
        (
            --   IN   --
              REQUEST         : in BitArray(0 to (FLOW_num -1));
              --..
        );
end CAU_FARM;
--...

farm_proc: process(CLK_FARM, RESET)
    variable request_was_made_var : std_logic;
    begin
    if RESET = C_INIT then 
       -- ...

    elsif rising_edge(CLK_FARM) then

            -- read state machine --
        case read_state is
            when        st_read_idle =>

                request_was_made_var := '0';
                for i in 0 to (FLOW_num -1) loop
                    if (REQUEST(i) = '1') then
                        request_was_made_var := '1';
                    end if;
                end loop;
                if (request_was_made_var = '1') and (chosen_cau_read_sig /= 8) then
                    read_state <= st_read_stage_1;
                    for i in 0 to (FLOW_num -1) loop
                        if (i = choice_out_sig) then
                            ACKNOWLEDGE(i) <= '1';
                        end if;
                    end loop;
                else
                    read_state <= st_read_idle;
                end if;
            ------------------------
            when        st_read_stage_1 =>
            --...
Galinagalindo answered 19/3, 2013 at 12:8 Comment(1)
Thank you both! I selected LLya's answer as the answer explicitly says that variables only exist in a process for serialized code. Seems like you were both saying the same thing but to me the key word 'only' made it a tad more clear.Velleman
E
13

Variables are intended to be a used for storing a value within a process. As such It's scope is limited. There tends to be a less direct relationship to synthesized hardware.

Variables also get a value immediately, whereas signals don't. the following two processes have the same effect:

signal IP, NEXTP : STD_LOGIC_VECTOR(0 to 5);

process (CLK)
    Variable TEMP : STD_LOGIC_VECTOR(0 to 5);
    begin
        if (rising_edge(clk)) then
            TEMP := IP;
            IP <= NEXTP;
            NEXTP <= TEMP(5) & TEMP(0 to 4);
        end if;
end process;

signal IP, NEXTP : STD_LOGIC_VECTOR(0 to 5);

process (CLK)

    begin
        if (rising_edge(clk)) then
            IP <= NEXTP;
            NEXTP <= IP(5) & IP(0 to 4);
        end if;
end process;

This is because the updates get scheduled, but haven't actually changed yet. the <= includes a temporal element.

Epigene answered 18/3, 2013 at 21:8 Comment(0)
N
8

variables: Temporary location; they are used to store intermediate values within "process".

signals: Update signal values. Run process activated by changes on signal.While process is running all signals in system remain unchanged.

Differences:

variables: They are local; no delay; declared within process

signals: They are global (before begin); delay due to wire; declared before key word begin

Neuman answered 16/4, 2015 at 9:3 Comment(0)
D
1

On a side note variables can't just live in processes (but also e.g. in procedures), furthermore they can be shared variables accessible from multiple processes (see: http://www.ics.uci.edu/~jmoorkan/vhdlref/var_dec.html).

Damoiselle answered 31/8, 2015 at 15:15 Comment(0)
A
0

Variables - they are local to a process, their value is updated as soon as the variable gets a new value.

Shared Variables- are like variables but they can be accessed from different processes.

Signals- Their scope is bigger, every process can access signals declared in the architecture or a specific block( if there is). There value updates after the process is suspended or encounters a wait statement.

Apothecary answered 12/5, 2017 at 11:37 Comment(1)
With the VHDL-2000/2002 update, shared variables are not permitted to be used with regular types. Instead they may only be used with protected types. Protected types do not allow assignment. Hence, the shared variable is much more like a handle to the object than it is a variable.Kooima

© 2022 - 2024 — McMap. All rights reserved.