Вы находитесь на странице: 1из 10

Case Statement

Format:

Example:
Process (sel, a, b, c, d)
Begin
CASE sel IS
When 00 =>
q <= a;
When 01 =>
q <= b;
When 10 =>
q <= c;
When others =>
q <= d;
End case;
End process;

CASE {expression} IS
WHEN <condition1> =>
{sequence of statements}
WHEN <condition2> =>
{sequence of statements}

.
.
When others =>
-- (optional)
{Sequence of statements}
End case;

a
b
c
d
sel

Case Statement
Conditions are evaluated at once
No prioritization
All possible conditions must be considered
WHEN OTHERS clause evaluates all other
possible conditions that are not specifically
stated

Case Statement
Similar to selected signal assignment
Implied process
WITH sel SELECT
q <= a WHEN 00,
b WHEN 01,
c WHEN 10,
d WHEN OTHERS;

a
b
c
d
sel

explicit process
PROCESS(sel, a, b, c, d)
BEGIN
CASE sel IS
WHEN 00 =>
q <= a;
WHEN 01 =>
q <= b;
WHEN 10 =>
q <= c;
WHEN OTHERS =>
q <= d;
END CASE;
END PROCESS;

Exercise 3
Please Go to Exercise 3

Sequential LOOPS
Infinite loop
Loops infinitely unless
EXIT statement exists

While loop
Conditional test to end
loop

For loop
Iteration loop

[Loop_label]loop
--Sequential statement
EXIT loop_label ;
END LOOP;
WHILE <condition> LOOP
--Sequential statements
End loop;
For <identifier> in <range> loop
--Sequential statements
End loop;

FOR LOOP Using a Variable: 4-bit Left Shifter


Library IEEE;
Use IEEE.Std_logic_1164.ALL;
Use IEEE.Std_logic_unsigned.All;
ENTITY shift4 IS
PORT ( shft_lft : in Std_logic;
D_in : in Std_logic_vector(3 downto 0);
Q_out : out Std_logic_vector(7 downto 0));
END shift4;
ARCHITECTURE logic OF shift4 IS
Begin
Process(d_in, shft_lft)
VARIABLE shft_var : Std_logic_vector(7 DOWNTO 0);
Begin
Shft_var(7 downto 4) := "0000";
Shft_var(3 downto 0) := d_in;
Variable is initialized
6

Variable declaration

FOR LOOP Using a Variable: 4-bit Left Shifter


IF shft_lft = '1' THEN
FOR i IN 7 DOWNTO 4 LOOP
Shft_var(i) := shft_var(i-4);
End loop;
Shft_var(3 downto 0) := "0000";
Else
shft_var := shft_var;
End if;
Q_out <= shft_var;
End process;
END logic;

Enables shift-left
I is the index for the FOR LOOP
And does not need to be declared
Shifts left by 4
Fills the LSBs with zeros
No shifting

Variable is assigned to a signal


before the end of the process to
synthesize to a piece of hardware

FOR LOOP: 1s Counter


Library IEEE;
Use IEEE.Std_logic_1164.ALL;
Use IEEE.Std_logic_unsigned.All;
Use IEEE.Std_logic_arith.All;
Entity bc is port (invec: in Std_logic_vector(31 downto 0);
Outvec: out Std_logic_vector(7 downto 0));
END bc;
ARCHITECTURE rtl OF bc IS
Begin
Process(invec)
VARIABLE count: Std_logic_vector(7 downto 0);

Variable declaration

FOR LOOP: 1s Counter


Begin
Count:=(others=>0);
FOR i IN invecright TO invecleft LOOP
Variable is initialized
IF (invec(i)/=0) THEN
Count:=count+1; I is the loop index
This loop examines all 32 bits of invec. If
End if;
the current bit does not equal zero, count
is incremented.
End loop;
Outvec<=count;
End process;
Variable is assigned to a signal
END rtl;
before the end of the process to
synthesize to a piece of hardware

Exercise 4
Please Go to Exercise 4

10

Вам также может понравиться