Every programming language has objects for storing values. VHDL too have them. Two of these object types are called Signals and Variables. They might look very similar for a beginner, but there are few fundamental differences between them.
- Variables are assigned using the := operator. And signals are assigned with the <= operator.
- Variables can be declared and used only within a process/function/procedure but Signals can be declared and used anywhere.
A very fundamental difference:
In a block of statements, the statements with variables immediately take their values. Very similar to how it works in programming languages like C. But in a group of statements with Signals on the left hand side, the signals does not take it's new value until the process has suspended (either hit the bottom or hit a wait statement).
This can be further explained with the following example scenario.
Suppose I want to implement a swapping function in VHDL using Signals.
I can simply write,
signal x,y : std_logic := 0;process(Clk) begin if(rising_edge(Clk)) then x <= y; y <= x; end if; end process;
What happens above is that, though the 'x' is assigned the value of 'y' sequentially first, the new value isn't updated to 'x' until we "exit" the process. So from a practical point of looking at it, it looks like they happen in parallel.
Now if I have to use variables for implementing a swapping function, I need three statements. Like below:
process(Clk) variable x,y,temp : std_logic := 0; begin if(rising_edge(Clk)) then temp := x; x := y; y := x; end if; end process;Since variables take the values assigned to them right away, we need a temporary variable to hold the value of 'x' before assigning 'y' to it.
- Variables declared in different processes cannot communicate with each other. They are local to the process. On the other hand signals declared in a VHDL entity can be used anywhere in the entity.
- You cannot declare or use a Signal inside a VHDL Function. Functions are purely combinatorial in VHDL and thus you have to have use variables.
If you want the code to be synthesised, then beware of the consequences of using a variable. Variables often create latches when implemented on a FPGA and synthesis tools often pass a warning to notify. If not needed its good to avoid latches in your design.
Though using variables might seem make the work easier, it might not pass the synthesis stage. For many, who come to VHDL from a C background, using variables is very tempting.
Be easy with the use of Variables:
Check this Matrix Multiplication code using Variables to see some of the dangers involved with them. Multiplication of two matrices requires a large number of multipliers and adders. In C, you would use some nested "for" loops to achieve this. And with the use of variables you can do the same thing in VHDL too like you can see from the link.
But using this same piece of code in a real FPGA is impossible to achieve. Either the design wont pass the synthesis stage or it will take days to get it done.
All those individual additions and multiplications gets done in "one" clock cycle. None of the adders and multipliers get reused and the loops get unfolded into a concatenated series of resources.
In such a case its necessary to use signals and spilt the whole operation over many clock cycles. This reduces the resource usage and more importantly you have a chance to get your design synthesised.
No comments:
Post a Comment