VHDL Variable
Quick Syntax
PROC_VAR: process (input1,input2)
variable my_var : std_logic := '0';
begin
my_var := '0'; -- reset every time process runs
if input1 = '1' then
my_var <= '0';
else
my_var <= '1';
end if;
output1 <= my_var;
if input2 = '1' then
my_var <= '1';
else
my_var <= '0';
end if;
output2 <= my_var;
end process;
Purpose
The purpose of variables in VHDL is in cases where you need to drive outputs of a process by something that will change throughout the process, and you need to capture each of those changes.
Variables are like a local memory storage ability. Whereas with signals in processes, you get the last valid option in the process as it goes from top to bottom.
Some people like using variables, many do not. In my professional experience, I rarely come across variables.
Personally, I never use them. Why? Because you can do the same functionality with multiple signals, and most people don't know how to use variables correctly. Therefore, it's a bit risky to use them in code that you want to be easily maintainable by others.
I usually see lots of newbies use them incorrectly. Perhaps because they think it's close to a software language variable for which they are comfortable with. However, this is hardware description, not software.
My recommendation would be to use them rarely, and only in cases where you can't get the same functionality without using them. However, I would challenge that you can always do without them.
Best Practices
1. Many people don't know how to use variables correctly, so if you use them, chances are someone else coming behind you will probably break your code.
2. If you do use variables, make sure you understand how to use them correctly.
3. Some people like variables, many do not. They are just another option in the language. They come with a risk if you are working with others.
4. If you need multiple outputs throughout a process based on the same variable, you can do the same functionality with multiple signals instead and forgo using variables.