Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VHDL Variable Vs. Signal

Tags:

vhdl

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!

like image 763
doddy Avatar asked Mar 18 '13 19:03

doddy


People also ask

Can I assign a variable to a signal in VHDL?

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.

What is a variable in VHDL?

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 “<=”.

What is the difference between signal and wire in VHDL?

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 in VHDL Mcq?

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.


1 Answers

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 =>             --... 
like image 178
SIMEL Avatar answered Sep 18 '22 04:09

SIMEL