Beruflich Dokumente
Kultur Dokumente
Outline
Sequential statements
These statements can appear inside a process
description :
variable assignments
if-then-else
case
loop
infinite loop
while loop
for loop
assertion and report
signal assignments
function and procedure calls
Processes
A process is a region of VHDL code that
executes SEQUENTIALLY
Exists inside the architecture
Multiple processes execute with each
other concurrently
3
Process Statement
[process_label:]
PROCESS [(signal_name {,signal_name})
]
[VARIABLE declarations]
BEGIN
[WAIT statement]
[Simple Signal Assignment
Statements]
[Variable Assignment Statements]
[IF Statements]
[CASE Statements]
[LOOP Statements]
END PROCESS [process_label];
IF statement
IF expression THEN
statement;
{statement;}
ELSIF expression
THEN
statement;
{statement;}
ELSE
statement;
{statement;}
END IF;
19-Feb-11
LOOP Statements
CASE Statement
CASE expression IS
-- example 2to1mux
WHEN constant_value =>
CASE Sel IS
statement;
WHEN 0 =>
f <= x1;
{statement;}
WHEN OTHERS =>
WHEN constant_value => f <= x2;
END CASE;
statement;
{statement;}
WHEN OTHERS =>
statement;
{statement;}
END CASE;
[loop_label:]
FOR variable_name IN range LOOP
statement;
{statement;}
END LOOP [loop_label];
[loop_label:]
WHILE boolean_expression LOOP
statement;
{statement;}
END LOOP [loop_label];
Statement Ordering
Statements are evaluated in order they
appear within process, but
SIGNAL data objects are not assigned values
until the end of the process. Only last
statement that assigns a value to a given
signal updates that signal.
If f <= x1; were moved
Example:
BAD!
11
10
assignment operator is :=
12
19-Feb-11
Introduction
Digital Logic
Y=f(X)
Latches, Flip-flops
RAM, etc.
Multivibrators
14
Latches
Latch /
flip-flop Q
Oneshot
Clock
16
Input Output
Comments
S R Q Q
0 0 1 1 Not allowed
Set
0 1 1 0
Reset
1 0 0 1
1 1 Q Q No change
18
19-Feb-11
The SR Latch
More on SR Latches
SR Latch
Basic Storage
Element
S
0
0
1
1
Q
Q
NAND-based SR Latch
R Q(t+1)
0 Q(t)
1
0
0
1
1
??
S Q
R Q
R
0
1
0
1
q <= p;
qbar <= pbar;
end sr_df;
Behavioral Implementation
Q
Q
0
1
1
Qbar
Qbar
1
0
1
entity sr_bhv is
port( s, r in bit;
q, qbar out bit);
end sr_bhv;
State
Hold
Reset
Set
Illegal
S
0
0
1
1
R
0
1
0
1
Q
Q
0
1
1
Qbar
Qbar
1
0
1
State
Hold
Reset
Set
Illegal
test = 0
Q
test = 1
end sr_bhv;
22
D Latch
Alternative Implementation
D Q
C Q
D Q
C
Q
S Q
C
R Q
Q
C
Q
Q
The D latch stores the value on the D input when the enable
input is asserted.
no forbidden input combinations
but input should be stable when the control input drops
if not, outputs may become metastable
TG
TG
20
Example: SR Latch
D Latch
19
21
S Q
C
R Q
Ex: SR Latch
S
0
0
1
1
Dataflow Implementation
entity sr_df is
port( s, r in bit;
q, qbar out bit);
end sr_df;
S R Q(t+1)
0 0 ??
01
1
10
0
1 1 Q(t)
23
19-Feb-11
S Q
C
R Q
S Q
C
R Q
Q
Q
Latches
S
C
R
S
C
R
J
C
K
J
C
K
>C
>C
J
>C
K
J
>C
K
Characteristic Tables
J K Q(t+1)
0 0 Q(t)
0 1
0
1 0
1
1 1 Q(t)
D Q(t+1)
0
1
0
1
T Q(t+1)
0
1
Q(t)
Q(t)
26
Loops
A loop repeatedly executes the sequential statements
contained within the loop structure
for loop
Entry point
Iteration
Terminal test
for identifier in starting value to stopping value loop
VHDL statements
end loop
27
28
While loop
Ex 7-9) Write a VHDL
program that reads a
set of 7-input bits, then
counts the number of
bits that are 1 and
send the count to an
output port.
Sol)
7
Bit
counter
entity OnesCount is
port (A: in std_logic_vector(0 to 6);
X: out natural range 0 to 7);
end entity OnesCount;
architecture MyCount of OnesCount is
begin
process(A)
variable V1: natural range 0 to 7;
begin
V1:=0;
for i in 0 to Alength-1 loop
if(A(i)=1) then
V1:= V1 + 1;
end if;
end loop;
X<=V1;
end process;
end architecture MyCount;
29
30
19-Feb-11
Shift registers
Serial-to-parallel converters
Parallel-to-serial converters
Delay
Counters
31
32
library ieee;
architecture rtl of sipo is
use ieee.std_logic_1164.all;
begin
entity sipo is
p0: process (CLK,SIN) is
generic(N : natural := 8);
variable REG : std_logic_vector(N-1 downto 0);
port(SIN: in std_logic;
begin
Q : out std_logic_vector(n-1 downto 0); if rising_edge(CLK) then
CLK : in std_logic);
REG := REG(N-2 downto 0) & SIN;
end entity sipo;
Q <= REG;
end if;
end process p0;
end architecture rtl;
(b) SIPO
33
34
library ieee;
architecture rtl of sipo is
use ieee.std_logic_1164.all;
begin
entity sipo is
p0: process (CLK,SIN) is
generic(N : natural := 8);
variable REG : std_logic_vector(N-1 downto 0);
port(SIN: in std_logic;
begin
Q : out std_logic_vector(n-1 downto 0); if rising_edge(CLK) then
CLK : in std_logic);
REG := REG(N-2 downto 0) & SIN;
end entity sipo;
end if;
Q <= REG;
end process p0;
end architecture rtl;
library ieee;
architecture rtl of sipo is
use ieee.std_logic_1164.all;
begin
entity sipo is
p0: process (CLK,Q,SIN) is
generic(N : natural := 8);
begin
port(SIN : in std_logic;
if rising_edge(clk) then
Q : inout std_logic_vector(N-1 downto 0);
Q <= Q(N-2 downto 0)&SIN;
CLK : in std_logic);
end if;
end entity sipo;
end process p0;
end architecture rtl;
36
19-Feb-11
library ieee;
use ieee.std_logic_1164.all;
entity siso is
generic(N : natural := 8);
port(SIN,CLK : in std_logic;
SOUT : out std_logic);
end entity siso;
37
library ieee;
use ieee.std_logic_1164.all;
entity usr is
generic(n : natural := 8);
port(a : in std_logic_vector(n-1 downto 0);
lin, rin : in std_logic;
s : in std_logic_vector(1 downto 0);
clk, reset : in std_logic;
q : out std_logic_vector(n-1 downto 0));
end entity usr;
S1 S0 control signals:
Hold
Shift right
Shift left
Parallel load
38
00
01
10
11
Sequential Statements
Implied Registers
begin
Sequential Statements
Implied Registers
Positive edge triggered D-FF with asynchronous reset
Process (d,clock,reset)
In
begin
if (reset = 0) then
q <= 0;
elsif( clockevent and clock=1) then
q <= d;
end if;
end process;
Registers
SET
Qn+1
Clock
CLR
Reset
41
42
19-Feb-11
Sequential Statements
Implied Registers
Sequential Statements
Implied Registers
We can easily extend this to a register block by using a
std_logic_vector datatype instead of a std_logic datatype.
SET
CLR
Qn+1
.
In hardware, this
Signal ns,ps:std_logic_vector(7 downto 0);
..
Process (ns,clock,reset)
R
ns
begin
E
if (reset = 0) then
G
ps <= 00000000;
elsif( clockevent and clock=1) then
ps <= ns;
end if;
reset
end process;
becomes
ps
43
Sequential Statements
Implied Registers
44
Clock edges:
Use of wait-until to represent a Flip Flop
Counters
48
19-Feb-11
library IEEE; -- moore asyn. Clock counter with asyn. reset, synthesized ok
use IEEE.std_logic_1164.all;
entity asyn_counter is
port (
CLK: in STD_LOGIC;
RESET: in STD_LOGIC;
COUNT: inout STD_LOGIC_VECTOR(3 downto 0));
end asyn_counter;
14 end process;
clock
Count(1)
Count(2)
Count(3)
D(3)
D(0)
D(1)
D(2)
1FF
FF
FF
FF
Q(0) ck
Q(2) ck
Q(3)
ck
Q(1) ck
reset
clock
Q(0)
t= time delay at one FF
19-Feb-11
16-bit
din (data in)
DIR
CE
16-bit
count output
clock
-4-bit syn. Clock counter with count enable, asyn. reset and syn. load
--CLK: in STD_LOGIC; --from language assistant of Xilinx-fundation
-RESET: in STD_LOGIC;
-CE, LOAD, DIR: in STD_LOGIC;
-DIN: in INTEGER range 0 to 15;
-COUNT: inout INTEGER range 0 to 15;
10
end if;
11
end if;
12
end if;
13 end if;
14 end process;
--4-bit syn.clock counter with asyn reset ,from language assistant of X-fundation,
synthesized ok
library IEEE; --4-bit syn. counter with count enable, asyn. reset and syn. load, synthesized
ok
use IEEE.std_logic_1164.all;
entity syn_counter is
port (
CLK: in STD_LOGIC;
RESET: in STD_LOGIC;
CE, LOAD, DIR: in STD_LOGIC;
DIN: in INTEGER range 0 to 15;
COUNT: inout INTEGER range 0 to 15);
end syn_counter; --Since count is an IO bus connected outside, it must have type inout or
buffer
Architecture syn_counter_arch of syn_counter is
begin process (CLK, RESET) begin
if RESET='1' then COUNT <= 0;
elsif CLK='1' and CLK'event then
if LOAD='1' then COUNT <= DIN;
else if CE='1' then
if DIR='1' then
COUNT <= COUNT + 1;
else
COUNT <= COUNT - 1;
end if;
end if;
end if;
end if;
10
19-Feb-11
VHDL Counters
if rising_edge(clk) then
if load = 1 then
cnt <= data;
elsif enable = 1 then
cnt <= cnt+1;
end if;
end if;
end process;
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY cnt IS
PORT(clk: IN STD_LOGIC;
reset: IN STD_LOGIC;
z: OUT STD_LOGIC_VECTOR(2 downto 0));
END cnt;
ARCHITECTURE behavior OF cnt IS
Type state_type is (A,B,C,D,E,F);
SIGNAL state: state_type;
BEGIN
Declare possible states
PROCESS(reset,clk)
(A-F is six states)
BEGIN
if (reset=1) then
state <= A;
z <=000;
Asynchronous reset to 000
elsif (clk'event and (clk=1)) then
case state is
when A=> state <= B; z <= "011";
when B=> state <= C; z <= 100";
when C=> state <= D; z <= "101";
Heres the counter
when D=> state <= E; z <= 010";
when E=> state <= F; z <= 001";
when F=> state <= A; z <= "000";
END CASE;
end if;
end process;
END behavior;
000
011 A 001
B
F
100C
E
010
D
101
-- three-state buffers
three_state: process(oe,cnt)
begin
if oe = 0 then
cnt_out <= (others => Z);
else
cnt_out <= cnt;
end if;
end process three_state;
end counter;
62
Common Errors
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity term-count is port
clock, reset, oe : in bit;
--Errors: (i) missing ( (ii) - not acceptable in the identifier
data
: out std_logic_vector(7 downto 0);
equals, cnt_out
: out std_logic;);
-- Error: ; before ) not acceptable.
end term-count;
architecture a of term-count
signal cnt: std_logic_vector(7 downto 0);
-- Error : is missing
begin
process (cnt, data)
begin
if data = cnt
equals = 1;
--Error then missing, use <= instead of =
end if;
end process;
Common Errors
11
19-Feb-11
defined in unsigned.all
package
Dummy variable, QQ
12
19-Feb-11
Asynchronous reset
Asynchronous LOAD
Clock Enables: CTEN,
D/U
CLK
CTEN
UP/DOWN
LOAD
Synchronous
Binary Counter
counter
output
CLOCK Enable
RCO
Max/Min
Asynchronous
RESET and LOAD
Output
combinational
function block
Decimal Up Counting
else
if QQ="0000" then
QQ<="1001";
else
QQ<=QQ-1;
end if;
end if;
end if;
end if;
end process;
Q<=std_logic_vector(QQ);
13
19-Feb-11
Pattern generators
Irregular pattern counter examples: traffic
light, memory read/write patterns.
The control unit of a computer is a pattern
generator.
Or the whole digital computer is a pattern
generator counting according to the clock
and inputs (keyboard, memory, disk etc.)
State concepts
Answer the following questions:
How many states can a 4-bit counter have?
How many bits for the state registers
(using binary encoding) are required if you
need
One-hot encoding:
Using N flip-flops for N states.
Use more flip-lops but less combination logic.
4 states?
9 states?
21 states?
14
19-Feb-11
s1
s2
R
Y
Y
G
out_light(0) red
out_light(1) yellow
out_light(2) green
R
Y
Y
G
Design flow
Process1(p1): -- clocked sequential
process
define state transitions
s3
Y
page
R
RY
G
Y
15
19-Feb-11
clock: in std_logic);
end traffic;----------------------------------------------- Architecture lightA of traffic is
type traffic_state_type is (s0, s1,s2,s3);
signal L_stateA: traffic_state_type;
begin
----------------------continue next page----------------------
p1:process
-- exec. Once when clock rises
begin wait until clock=1; --s sequential process
case L_stateA is
when s0 => L_stateA <= s1;
when s1 => L_stateA<= s2;
when s2 => L_stateA<= s3;
when s3 => L_stateA<= s0;
end case;
end process; --to be continued , see next page
---- convert L_statesA to out_light
p2:process(L_stateA) -- combin. process
begin case (L_stateA) is
when s0 => out_light <="100";
when s1 => out_light <="110";
when s2 => out_light <="001";
when s3 => out_light <="010";
end case;
end process;
end lightA;
Programming hints:
In practice, lig0_nr.vhd does not have a
reset/set for sequential flip-flops, I.e.
(L_stateA).
Warning: In Xilinx-Foundation, the timing
simulator may not know how to initialize
L_stateA, hence does not know how to
begin the simulation.
So we have to modify the program.
L_stateA =
reset
s1
s0
InB=1
InB=0
R
R
Y
s2
inB=1
s3
InB=1
inB=0
Liga1_sr.vhd
Add synchronous reset
programming
Y
G
InB=0
16
19-Feb-11
Liga2_ar.vhd
Add asynchronous reset
programming
17
19-Feb-11
Liga3_ar.vhd
Based on liga2_ar.vhd combine
two processes (p1+p2) into one.
Further exercises
Liga3_ar.vhd: Rewrite liga2_ar using only
one process; combine the two processes.
18