Lecture 6 Process
Lecture 6 Process
3
Concurrent and Sequential Statements
Concurrent type such as: when-else, with-select, signal
assignment, and etc.
4
Process statement (I)
The process in VHDL is the mechanism by which
sequential statements can be executed in the
correct sequence.
Several processes execute concurrently
Processes in an architecture are executed
concurrently with all other concurrent
statements.
Execution is controlled either via sensitivity list
(contains trigger signals), or wait-statements
The statements inside the process is executed
whenever one or more elements of the sensitive
list change value.
5
Process statement (II)
Process statements are executed
concurrently but the statements inside a
process are executed sequentially.
Every process is executed once upon
initialization
A process statement has a declaration
section and a statement part.
In the declaration section, types, variables,
constants, subprograms, and so on can be
declared.
The statement part contains only sequential
statements.
6
Process Definition
7
Process
8
Process
JustToShow: process ( )
JustToShow: process Begin
Begin
Some statement 1;
Some statement 1;
Some statement 2; Some statement 2;
Some statement 3; Some statement 3;
Some statement 4; Some statement 4;
wait on SomeSig end process JustToShow;
end process JustToShow;
9
Process
Sensitive list
10
Process: an example
ARCHITECTURE archlist OF list IS
BEGIN
nand_test: PROCESS (a,b) the process ‘nand’ is
BEGIN the process ‘nand’ is
sensitive to signals ‘a’ and
c <= NOT (a AND b); sensitive to signals ‘a’ and
END PROCESS nand_test; ‘b’
‘b’i.e.,
i.e.,whenever
wheneversignal
signal
END archlist; ‘a’
‘a’or
or‘b’
‘b’changes
changesvalue,
value,
the
thestatements
statementsinside
insideof
ofthe
the
c process
processwill
willbe
beevaluated
evaluated
11
Wait statement
A process may be suspended upon execution of a wait statement in
the process. The process remains suspended until its reactivation
condition is met
12
Wait Until
WAIT UNTIL signal_condition;
The WAIT UNTIL statement accepts only one
signal
More appropriate for synchronous code than
asynchronous
WAIT UNTIL must be the first statement in the
PROCESS
The PROCESS will be executed every time
the condition is met.
13
Wait Until (Example)
PROCESS -- no sensitivity list
BEGIN
WAIT UNTIL (clk'EVENT AND clk='1');
IF (rst='1') THEN
output <= "00000000";
ELSIF (clk'EVENT AND clk='1') THEN
output <= input;
END IF;
END PROCESS;
14
WAIT ON
WAIT ON signal1 [, signal2, ... ];
WAIT ON accepts multiple signals
The PROCESS is put on hold until any of the
signals listed changes
PROCESS
BEGIN
WAIT ON clk, rst;
IF (rst='1') THEN
output <= "00000000";
ELSIF (clk'EVENT AND clk='1') THEN
output <= input;
END IF;
END PROCESS;
15
Wait: an Example
Or_process : process
Or_process : process (In1,
In2) Begin
begin wait on In1, In2;
Output <= In1 or In2; Output <= In1 or
In2;
end process;
end process;
16
Variables versus signals in process
17
Variables vs. signals in process
Variables and signals show a
fundamentally different behavior.
processes:
• Name within process declarations
• Known only in this process Possible assignments:
Signal to variable
Variable to signal
Types have to match
20
Variables in process
architecture RTL of XYZ is
signal A, B, C : integer range 0 to 7;
signal Y, Z : integer range 0 to 15;
begin
process (A, B, C)
variable M, N : integer range 0 to 7;
begin
M := A;
N := B;
Z <= M + N;
M := C;
Y <= M + N;
end process;
end RTL;
21
Variables vs. signals in process
signal A,B,C:
signal A,B,C: integer;
integer; signal Y, Z :
signal Y, Z : integer;
integer; signal M, N :
integer;
begin begin
process (A,B,C) process
variable M, N: (A,B,C,M,N)
integer;
begin
begin
M := A; M <= A;
N := B; N <= B;
Z <= M + N; Z <= M + N;
M := C; 22 M <= C;
Variables vs. signals in process (II)
Signal values are assigned after the
process execution
Only the last signal assignment is carried
out
M ⇐ A; is overwritten by M ⇐ C;
The 2nd adder input is connected to C
23
Exam question (Check Yourself)
What is the value of sum in the following two pieces of
code? Please determine the value of sum for the first 4
clocks
entity mysig is entity myvar is
port(clk: in bit; port(clk: in bit;
sum: out integer range 0 to 256); sum: out integer range 0 to 256);
end mysig; end myvar;
architecture beh of mysig is architecture beh of myvar is
signal sig1: integer :=1; begin
signal sig2: integer :=2; process(clk)
signal sig3: integer :=3; variable var1: integer :=1;
begin variable var2: integer :=2;
process(clk) variable var3: integer :=3;
begin begin
if(clk'event and clk='1') then if(clk'event and clk='1') then
sig1 <=sig2 + sig3; var1:=var2 + var3;
sig2 <= sig1; var2 := var1;
sig3 <=sig2; var3 :=var2;
sum<=sig1 + sig2 + sig3; sum<=var1 + var2 + var3;
end if; end if;
end process; end process;
end architecture; end architecture;
24
Exam Question: Solution I
In myvar program:
In clock 1:
Var1 = 2+3 = 5
Var2 = 5
Var3 = 5 sum = 5 + 5 + 5 sum = 15
In clock 2:
Var1 = 5 + 5 = 10
Var2 = 10
Var3 = 10 sum = 10 + 10 + 10 sum =30;
In clock3:
Var1 = 10 + 10 = 20
Var2 = 20
Var3 = 20 sum 20 + 20 + 20 sum = 60
In clock4:
Var1 = 20 + 20 = 40
Var2 = 40
Var3 = 40 sum = 40 + 40 + 40 sum = 120 25
Exam Question: Solution II
In clock3:
In mysig program:
Now, the signal assignments of clock 2 are taken here;
In clock1:
while the signal assignments of clock 3 are taken at the
All signal assignments are taken at the beginning of beginning of clock 4. So, we have
clock 2; thereby in clock 1 you have
Sig1 = 1 + 2 = 3
Sig1 = 1
Sig2 = 5 (the value of signal sig1 of clock 2)
Sig2 = 2
Sig3 = 1 (the value of sig2 of clock 2) sum = 3 + 5
Sig3 = 3 sum = 1+2+3 sum = 6 + 1 sum = 9
In clock2: In clock4:
Now, the signal assignments of clock 1 are taken here; Now, the signal assignments of clock 3 are taken here;
while the signal assignments of clock 2 are taken at the while the signal assignments of clock 4 are taken at the
beginning of clock 3. So, we have beginning of clock 5. So, we have
Sig1 = 2 + 3 = 5
Sig2 = 1 (the value of signal sig1 of clock 1) Sig1 = 5 + 1 = 6
Sig3 = 2 (the value of sig2 of clock 1) sum = 5 + 1 Sig2 = 3 (the value of signal sig1 of clock 3)
+2=8
Sig3 = 5 (the value of sig2 of clock 3) sum = 6 + 3
+ 5 sum = 14
26
Global variables (Shared)
27
Global variables
In VHDL 93, global variables are allowed.
These variables are not only visible within a process but within the entire architecture.
The problem may occur, that two processes assign a different value to a global variable at
the same time. It is not clear then, which of these processes assigns the value to the
variable last.
This can lead to a non-deterministic behavior!
28
Global variables (II)
• Accessible by all processes of an
architecture (shared variables)
architecture BEHAVE of SHARED is
shared variable S : integer;
begin
• Can introduce non-determinism
process (A, B)
begin
S := A + B;
end process;
process (A, B)
begin
S := A - B;
end process;
end BEHAVE;
29
Postponed Process
30
Postponed Process (I)
Activates 3 Times
Ex:Process (a, b, c) Once Per Sensitivity
List Signal Change
Begin
......
End Process;
Example
33
If Statement
The general form is
if condition1 then
statement1
elsif condition2 then
statement2
else
statement3
end if;
34
If Example: Two-input NAND gate
The process declaration section
Declares a VHDL package that provides the declares a local variable named
necessary information with 9 state logic. temp.
36
If: an Example
process
begin
if (reset = ‘1’) then
A <= ‘0’ ;
elsif (clk’event and clk = ‘1’) then
A <= ‘B’;
end if;
wait on reset, clk;
end process;
process (clk,reset)
begin
if (reset = ‘1’) then
A <= ‘0’;
elsif (clk’event and clk = ‘1’) then
A <= ‘B’;
end if;
end process;
37
If: an Example
38
One-digit Counter
LIBRARY ieee;
USE ieee.std_logic_1164.all;
---------------------------------------------
ENTITY counter IS
PORT (clk : IN STD_LOGIC;
digit : OUT INTEGER RANGE 0 TO 9);
END counter;
---------------------------------------------
ARCHITECTURE counter OF counter IS
BEGIN
count: PROCESS(clk)
VARIABLE temp : INTEGER RANGE 0 TO 10;
BEGIN
IF (clk'EVENT AND clk='1') THEN
temp := temp + 1;
IF (temp=10) THEN temp := 0;
END IF;
END IF;
digit <= temp;
END PROCESS count;
END counter;
39
Case Statements
40
Case statement
Syntax
case expression is
when choice 1 =>
statement_A;
when choice 3 to 5 =>
statement_B;
when choice 8 downto 6 =>
statement_C;
when choice 9 | 13 | 17 =>
statement_D;
when others =>
statement_E;
end case;
41
Case statement: an Example
MUX (41)
mycase_pro: process (s, c, d, e, f)
begin
case s is
when "00" =>
pout <= c; C
when "01" =>
pout <= d; D POUT
when "10" => E
pout <= e;
when others => F
pout <= f; S
end case;
end process mycase_pro;
42
Case statement
CASE allows multiple assignments for
each test condition
WHEN value -- single value
WHEN value1 To value2 -- range, for enumerated data types
only
WHEN value1 | value2 |... -- value1 or value2 or ...
43
Null
Nothing to do
Equivalent to No Operation
It can be used by case statement
process (count)
begin
case count is
when 0 =>
dout <= “00”;
when 1 to 15 =>
dout <= “01”;
when 16 to 255 =>
dout <= “10”;
when others =>
null;
end case;
end process;
44
Loops
45
Loop (I)
The LOOP statement is used whenever an
operation needs to be repeated.
The LOOP statement has an optional
label, which can be used to identify the
LOOP statement.
VHDL provides three kinds of Loop statements
Simple loop
for loop
while loop
46
Simple Loop
Simple loop
Simple loop encloses a set of statements in a
structure which is set to loop forever
The general form is
label1 : loop
statements
end loop label1;
47
Simple loop: Example
library ieee;
use ieee.std_logic_1164.all;
entity WhileTest is
port(A: in integer range 0 to 31;
Z: out std_logic_vector(3 downto 0));
end entity;
Note VHDL allows maximum 10000 iterations.
architecture test of WhileTest is
begin
process (A)
variable I : integer range 0 to 4;
begin
Z <= "0000";
I := 0;
L1: loop
exit L1 when I = 4;
if (A = I) then
Z(I) <= '1';
end if;
I := I + 1;
end loop;
end process;
48
For loop (I)
The FOR loop loops as many times as specified in the
discrete_range, unless the loop is exited
The general form
loop_label: -- optional
for loop_variable in range loop
statements
end loop loop_label;
Example
49
For Loop Example
50
For loop (II)
In some languages, the loop index (in this
example, i) can be assigned a value inside
the loop to change its value.
VHDL does not allow any assignment to the
loop index.
The index value i is locally declared by the
FOR statement.
The variable i does not need to be declared
explicitly in the process, function, or procedure
If another variable of the same name exists in
the process, then these two variables are
treated as separate variables
51
For loop (III)
PROCESS(i)
BEGIN
x <= i + 1; -- x is a signal
FOR i IN 1 to a/2 LOOP
q(i) := a; -- q is a variable
END LOOP;
END PROCESS;
The index value i is not the same object as the signal i that was
used to calculate the new value for signal x.
Inside the FOR loop, when a reference is made to i, the local index is
retrieved.
But outside the FOR loop, when a reference is made to i, the value of the
signal i in the sensitivity list of the process is retrieved.
52
For loop (IV)
The values used to specify the range in the
FOR loop need not be specific integer
values.
The range can be any discrete range.
See Examples
53
For loop (V)
PROCESS(clk)
TYPE day_of_week IS (sun, mon, tue, wed, thur, fri, sat);
BEGIN
FOR i IN day_of_week LOOP The range is specified by the type.
IF i = sat THEN
son <= mow_lawn; Here, the compiler determines that
ELSIF i = sun THEN the leftmost value is sun, and the
church <= family; rightmost value is sat.
ELSE
dad <= go_to_work; The range then is determined as from
END IF;
END LOOP; sun to sat.
END PROCESS;
54
For loop (VI)
If an ascending range is desired, use the
to clause. The downto clause can be
used to create a descending range.
See Example
PROCESS(x, y)
BEGIN
FOR i IN x downto y LOOP
q(i) := w(i);
END LOOP;
END PROCESS;
55
While Loop
56
While loop (I)
The WHILE condition LOOP statement loops as long as
the condition expression is TRUE.
The general form:
loop_label:
while condition loop
statements
end loop loop_label;
See Example
57
While loop (II)
library ieee; architecture test of WhileTest is
use ieee.std_logic_1164.all; begin
process (A)
entity WhileTest is variable I :
port(A: in integer range 0 to 3; integer range 0 to 4;
Z: out std_logic_vector(3 downto 0)); begin
end entity; Z <= "0000";
I := 0;
while (I <= 3) loop
if (A = I) then
Z(I) <= '1';
end if;
I := I + 1;
end loop;
end process;
end architecture;
58
Exit and Next statement
Exit statement is a sequential statement closely
associated with loops and causes the loop to be exited
for i in 0 to 7 loop
if ( i = 4 ) then
exit;
end if;
end loop;
Next statement is used to advance control to the next iteration of the
loop
for i in 0 to 7 loop
if ( i = 4 ) then
next;
end if;
end loop;
59
Nested loop
process
begin
for i in 0 to 3 loop
for j in 0 to 3 loop
wait for 10 ns;
b <= b + 1;
end loop;
a <= a + 1;
end loop;
wait;
end process;
60
Generate positive Edge of Clock
61
CLK Generation
The most popular clk generation
entity d_ff is
port (data, clk : in std_logic;
q : out std_logic);
end d_ff;
63
D-Flip Flop with asynchron reset
library IEEE;
use IEEE.std_logic_1164.all;
entity d_ff is
port (data, clk,reset : in std_logic;
q : out std_logic);
end d_ff;
64
D-Flip Flop with synchron reset
library IEEE;
use IEEE.std_logic_1164.all;
entity d_ff is
port (data, clk,reset : in std_logic;
q : out std_logic);
end d_ff;
65
Using Sequential Code to Design
Combinational Circuits
66
Using Sequential Code to Design Combinational
Circuits
67
Example