What does it mean whe you have: case state is when vale1 => state <= value2 in vhdl?

250 views Asked by At

This line of code gets me confused. I don't get how it works, I know => and <= are assigning symbols, but why 2 assignments to the same thing?

1

There are 1 answers

2
AudioBubble On BEST ANSWER

As David points out, => is not an assignment symbol.

It's a mapping symbol, or an association. Its use is consistent throughout VHDL - it's always used to associate something (often a name) with something else (often a value).

Here, within a case statement, it's used to associate a case choice when vale1 with the correct action for that state, which can be any valid sequence of statements. In this case, state <= value2;

You may find it clearer to write

case state is
   when vale1 =>
      state <= value2;
   when ...
      ...
   when others =>
      state <= idle;
end case;

which makes the logical structure clearer than trying to do too much in a line.

Other places you'll see it are in parameter lists. Given a function with several parameters (some of which are optional) you may have seen errors creep into programs in other languages where values get associated with the wrong parameters...

setPixelColour (100, 50, 17, 0.5);   -- not obvious, prone to mistakes

You can make it clearer what's going on, and avoid mistakes by associating each parameter with its name :

setPixelColour ( x => 100, 
                 y => 50, 
                 alpha => 17, 
                 opacity => 0.5);   -- obvious what you're trying to do

The same is true of port maps, where you are connecting signals up to the ports of a component.

As David says, another use is in creating aggregates, such as records or arrays.

If you define an Employee record, you might then declare

Boss : Employee := (Name => "Richard Branson", Job => CEO, Salary => 999000);

Or you might create an array using an aggregate:

type Month is (Jan, Feb, Mar, ... Dec);   -- we'll index the array with an enumeration
Days : array (Month range Jan to Dec) of Natural := (
   Feb => 28,
   Apr => 30,
   Jun => 30,
   Sep => 30,
   Nov => 30,
   others => 31);