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!
VHDL also uses variables and they have exactly the same role as in most imperative languages. But VHDL also offers another kind of value container: the signal. Signals also store values, can also be assigned and read. The type of values that can be stored in signals is (almost) the same as in variables.
Variables are local to a process. They are used to store the intermediate values and cannot be accessed outside of the process. The assignment to a variable uses the “:=” notation, whereas, the signal assignment uses “<=”.
A signal is a primary object describing a hardware system and is equivalent to “wires”. On the other hand, a variable is an object that stores the information that is local to the processes and subprograms (procedures and functions) in which they are defined.
What is the difference between SIGNAL and VARIABLE? Explanation: SIGNALs are used to pass information between entities, they act as interconnection between different entities whereas VARIABLEs can be used in the process or subprogram in which it is declared.
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 => --...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With