Tutorial 02 VHDL Synchronous Systems Counters
Tutorial 02 VHDL Synchronous Systems Counters
Tutorial 2
Michal Kubíček
Department of Radio Electronics, FEEC BUT Brno
Vytvořeno za podpory projektu OP VVV Moderní a otevřené studium techniky CZ.02.2.69/0.0/0.0/16_015/0002430.
Tutorial 2
page 2 kubicek@vutbr.cz
Tutorial 1 summary
strana 3 kubicek@vutbr.cz
FPGA - structure
strana 4 kubicek@vutbr.cz
FPGA - LUT
4-input LUT: Memory 16x1b
Address Data
a b d
c y
d c b a y minterm 0 0 0 0 b
0 0 0 0 0 /a·/b·/c·/d
c a
0 0 0 1 0 a·/b·/c·/d 0 0 0 1
0 0 1 0 0 /a·b·/c·/d
0 0 1 1 0 a·b·/c·/d d 1 1 0 0
0 1 0 0 0 /a·/b·c·/d
0 1 0 1 0 a·/b·c·/d 1 1 0 0
0 1 1 0 1 /a·b·c·/d
0 1 1 1 0 a·b·c·/d
1 0 0 0 1 /a·/b·/c·d y = /a·/b·c·d + /a·/b·/c·d + a·/b·/c·d + a·/b·c·d + /a·b·c·/d =
1 0 0 1 1 a·/b·/c·d = /b·d + /a·b·c·/d
1 0 1 0 0 /a·b·/c·d
1 0 1 1 0 a·b·/c·d
Minterms
1 1 0 0 1 /a·/b·c·d
1 1 0 1 1 a·/b·c·d Term
1 1 1 0 0 /a·b·c·d
1 1 1 1 0 a·b·c·d
strana 5 kubicek@vutbr.cz
Implementation - Synthesis
Synthesis
volba: IN STD_LOGIC_VECTOR(1 DOWNTO 0);
y: OUT STD_LOGIC);
END prepinac;
ARCHITECTURE Behavioral OF prepinac IS BEGIN
y <= a WHEN volba = "00" ELSE
b WHEN volba = "01" OR volba = "10" ELSE
c;
END Behavioral;
strana 6 kubicek@vutbr.cz
Implementation - MAP
Constraints
FREQ "clk" = 80 MHz
LOC "clk" = 193
LOC "Tx_data" = 87
...
strana 7 kubicek@vutbr.cz
Implementation - PLACE
strana 8 kubicek@vutbr.cz
Implementation - ROUTE
strana 9
Short introduction to VHDL
The aim of the MPLD course is not to teach you complete VHDL language. During tutorials and PC labs
you will get familiar with the most important and most problematic aspects of the VHDL language with
regards to implementation and simulation of digital systems targeting FPGAs.
page 10 kubicek@vutbr.cz
VHDL
❑ Standard IEEE Std 1076-xxxx (1987, 1993, 2000, 2002, 2008, 2019)
❑ Originally only for simulation of large digital systems
❑ For synthesis (implementation of the code into chips) one can use only a
"synthesable subset", some functions are not supported.
• Some arithmetic operations (/, MOD, REM)
• Some basic commands (WAIT)
• Algorithmic code, particularly loops (FOR, WHILE)
❑ When not respecting some basic rules, a simulation may give different results
from implementation (for example incomplete sensitivity list)
page 11 kubicek@vutbr.cz
VHDL
VHDL-2008 for Vivado synthesis : limited support. Please refer to UG901 Vivado Design
Suite User Guide: Synthesis under Appendix D for more details.
VHDL-2008 for Vivado simulation : limited support. Please refer to UG900 Vivado Design
Suite User Guide: Simulation under Appendix D for more details.
Xilinx currently does not support VHDL-2019. Xilinx might consider adding VHDL-2019
support based on customer demand in future. Xilinx simulation flow also supports simulation
tools from 3rd party EDA vendors that might be offering VHDL-2019 support. We encourage
our customers to evaluate 3rd party EDA vendor tools for simulation support for VHDL-2019.
page 12 kubicek@vutbr.cz
VHDL
page 13 kubicek@vutbr.cz
VHDL
page 14 kubicek@vutbr.cz
VHDL – basic terms
-----------------------------------------------
ALU
ARCHITECTURE Behavioral OF ALU IS
BEGIN A
SUM <= STD_LOGIC_VECTOR(UNSIGNED(A) + UNSIGNED(B)); + SUM
END Behavioral; B
page 15 kubicek@vutbr.cz
VHDL – basic terms
page 16 kubicek@vutbr.cz
VHDL – basic terms
Entity
Entity declaration describes its interface to the outside world. The
PORT section defines inputs and outputs of the block.
Available modes of the PORT signals:
❑ IN it is not allowed to assign any value to this signal (port) within the
entity (read only)
❑ OUT it is not allowed to read value of this signal (port) within the
entity (write only, cannot be used in conditions of conditional
statements, cannot be used on the right side of assignments)
❑ INOUT used for tri-state signals on the top level entity only (usually in
conjunction with a platform specific buffer)
❑ BUFFER shall not be used => no need to know anything about it ;-)
page 17 kubicek@vutbr.cz
VHDL – basic terms
Architecture
Defines relations between entity inputs and outputs (contains the description of
entity functionality). It is always related to a particular Entity.
Functionality of one Entity can be described using several Architectures.
Several different types of description:
❑ Behavioral Algorithmic, high-level language constructs
❑ RTL Register Transfer Logic; using processes
❑ Structural Text equivalent of schema (netlist)
The best praxis is to combine all the description methods to get most
EFFECTIVE and most READABLE description of the required functionality.
page 18 kubicek@vutbr.cz
VHDL – basic terms
page 19 kubicek@vutbr.cz
VHDL
page 20 kubicek@vutbr.cz
VHDL – objects
SIGNAL
❑ One of basic VHDL objects
❑ Similar to variables in other programming languages (as important)
❑ After synthesis the signal is represented by a physical wire (can be physically
located)
❑ Signals are declared within the entity as PORTs or inside the architecture as
internal SIGNALs, which are not visible from outside of the entity.
❑ To assign a value to a signal use the signal assignment <= (also called
concurrent, delayed)
page 21 kubicek@vutbr.cz
VHDL – objects
page 22 kubicek@vutbr.cz
VHDL – objects
VARIABLE
❑ Another basic object of VHDL
❑ Auxiliary variable for algorithmic description (iteration variable of loops...)
❑ No direct equivalent of the variable in the synthesized/implemented design
❑ Variables may be declared in declarative part of the architecture (shared
variables; rarely used) or in a declarative part of a process (non-shared
variables)
❑ To assign a value to a variable use variable assignment := (also called
immediate), which can only be used either in declaration of the variable or
within a process body.
page 23 kubicek@vutbr.cz
VHDL – objects
CONSTANT
❑ Another VHDL object
❑ Used to store fixed values, can be assigned to both SIGNALS and
VARIABLES
❑ Constants may be declared in declarative part of an architecture or a process
(similar to shared and non-shared variable)
❑ It is only allowed to assign a value to the constant during its declaration
using the := assignment.
page 24 kubicek@vutbr.cz
VHDL
Data types
page 25 kubicek@vutbr.cz
VHDL – data types (of objects)
STANDARD
BOOLEAN -- True or False
INTEGER -- 32 or 64 bits
NATURAL -- Integers >= 0
POSITIVE -- Integers > 0
REAL -- Floating-point
BIT -- '0','1'
BIT_VECTOR -- Array of bits
CHARACTER -- 7-bit ASCII
STRING -- Array of characters
TIME -- hr, min, sec, ms, us, ns, ps, fs
STD_LOGIC_1164
STD_ULOGIC -- 'U','X','0','1','Z','W','L','H','-'
STD_ULOGIC_VECTOR -- Array of STD_ULOGIC
STD_LOGIC -- Resolved STD_LOGIC
STD_LOGIC_VECTOR -- Array of STD_LOGIC
NUMERIC_STD, STD_LOGIC_ARITH (Never use both at once !!!)
UNSIGNED -- Array of STD_LOGIC
SIGNED -- Array of STD_LOGIC
page 26 kubicek@vutbr.cz
VHDL – data types
❑ BIT / BIT_VECTOR
TYPE bit IS ('0','1');
Simulation should run faster, than with STD_LOGIC
❑ BOOLEAN
TYPE boolean IS (false,true);
Argument of conditional statements
page 27 kubicek@vutbr.cz
VHDL – data types
❑ STD_ULOGIC / STD_ULOGIC_VECTOR
TYPE std_ulogic IS (
'U', -- Uninitialized
'X', -- Forcing Unknown
'0', -- Forcing 0
'1', -- Forcing 1
'Z', -- High Impedance
'W', -- Weak Unknown
'L', -- Weak 0
'H', -- Weak 1
'-'); -- Don’t care
TYPE std_ulogic_vector IS ARRAY ( NATURAL RANGE <> ) OF std_ulogic;
page 28 kubicek@vutbr.cz
VHDL – data types
❑ STD_LOGIC / STD_LOGIC_VECTOR
Based on STD_ULOGIC
There is a "resolution function" defined over the type
❑ It is possible to interconnect outputs
'L' (Weak 0) connected to '1' (Forcing 1) results in '1'
❑ It is said to be slower in simulation (no benchmarks available)
❑ No difference in synthesis (same RTL)
❑ The whole world uses STD_LOGIC / STD_LOGIC_VECTOR
page 29 kubicek@vutbr.cz
VHDL – data types
-- ---------------------------------------------------------
-- | U X 0 1 Z W L H - | |
-- ---------------------------------------------------------
( 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U' ), -- | U |
( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' ), -- | X |
( 'U', 'X', '0', 'X', '0', '0', '0', '0', 'X' ), -- | 0 |
( 'U', 'X', 'X', '1', '1', '1', '1', '1', 'X' ), -- | 1 |
( 'U', 'X', '0', '1', 'Z', 'W', 'L', 'H', 'X' ), -- | Z |
( 'U', 'X', '0', '1', 'W', 'W', 'W', 'W', 'X' ), -- | W |
( 'U', 'X', '0', '1', 'L', 'W', 'L', 'W', 'X' ), -- | L |
( 'U', 'X', '0', '1', 'H', 'W', 'W', 'H', 'X' ), -- | H |
( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' ) -- | - |
page 30 kubicek@vutbr.cz
VHDL – data types
❑ STD_LOGIC / STD_LOGIC_VECTOR
SIGNAL state : STD_LOGIC := '1';
SIGNAL counter : STD_LOGIC_VECTOR(1 TO 8);
SIGNAL data : STD_LOGIC_VECTOR(15 DOWNTO 0);
...
state <= '0'; -- SINGLE QUOTE MARKS for single bits
counter <= "00000000"; -- QUOTATION MARKS for vectors
counter <= "0000_0000"; -- separator of nibbles
counter <= X"00"; -- hexadecimal notation
-- set all members (bits) of a vector to '0'
data <= X"0000";
data <= (OTHERS => '0');
-- set selected members (bits) of a vector to '1', others to '0'
data <= "1111000001000000";
data <= (X"F" , 6 => '1', OTHERS => '0');
page 31 kubicek@vutbr.cz
VHDL – data types
❑ SIGNED / UNSIGNED
Data type for arithmetic functions.
The definition is identical to STD_LOGIC_VECTOR, but as it is a formally
different type, it is necessary to use conversion functions when dealing
with these types.
7 6 5 4 3 2 1 0
SIGNAL data_I : SIGNED(7 DOWNTO 0);
7 6 5 4 3 2 1 0
SIGNAL data_Q : SIGNED(7 DOWNTO 0);
SIGNAL data_out : STD_LOGIC_VECTOR(7 DOWNTO 0); 7 6 5 4 3 2 1 0
...
data_out <= STD_LOGIC_VECTOR (data_I + data_Q);
page 32 kubicek@vutbr.cz
VHDL – data types
Conversion functions
use IEEE.NUMERIC_STD.ALL;
...
SIGNAL a_VEC : STD_LOGIC_VECTOR (7 DOWNTO 0);
SIGNAL a_UNS : UNSIGNED (7 DOWNTO 0);
SIGNAL a_INT : INTEGER RANGE 0 TO 255;
...
page 33 kubicek@vutbr.cz
VHDL – data types
page 34 kubicek@vutbr.cz
VHDL – data types
Array declaration
TYPE RamType IS ARRAY(0 TO 15) of STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL ram : RamType:= (OTHERS => X"00");
page 35 kubicek@vutbr.cz
VHDL
Operators
page 36 kubicek@vutbr.cz
VHDL – operators
page 37 kubicek@vutbr.cz
VHDL – operators
* multiplication
<numeric> = <numeric> * <numeric>;
/ division
<numeric> = <numeric> / <numeric>;
page 38 kubicek@vutbr.cz
VHDL – operators
+ sign
<numeric> = + <numeric>;
- sign
<numeric> = - <numeric>;
page 39 kubicek@vutbr.cz
VHDL – operators
+ addition
<numeric> = <numeric> + <numeric>;
- difference
<numeric> = <numeric> - <numeric>;
& concatenate
<array> = <element> & <element>;
<array> = <element> & <array> ;
<array> = <array> & <element>;
<array> = <array> & <array> ;
page 40 kubicek@vutbr.cz
VHDL – operators
page 41 kubicek@vutbr.cz
VHDL – operators
Relation operators
= /= < <= > >=
page 42 kubicek@vutbr.cz
VHDL – operators
Logic operators
AND, OR, NAND, NOR, XOR, XNOR
page 43 kubicek@vutbr.cz
VHDL – operators
Concatenate &
SIGNAL A,B: STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL S,T,U,V: STD_LOGIC;
SIGNAL X,Y: STD_LOGIC_VECTOR(7 DOWNTO 0);
...
X(7) <= A(3);
X(6) <= A(2);
X(5) <= A(1);
X <= A & B; Equivalent X(4) <= A(0);
X(3) <= B(3);
X(2) <= B(2);
X(1) <= B(1);
X(0) <= B(0);
A 3 2 1 0 3 2 1 0 B
X 7 6 5 4 3 2 1 0
page 44 kubicek@vutbr.cz
VHDL – operators
Concatenate &
SIGNAL A,B: STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL S,T,U,V: STD_LOGIC;
SIGNAL X,Y: STD_LOGIC_VECTOR(7 DOWNTO 0);
...
X(7) <= A(3);
X(6) <= A(2);
X(7 DOWNTO 4) <= A; X(5) <= A(1);
X(3 DOWNTO 0) <= S & T & U & V; X(4) <= A(0);
X(3) <= S;
X(2) <= T;
Equivalent
X(1) <= U;
X(0) <= V;
page 45 kubicek@vutbr.cz
VHDL – operators
Concatenate &
data_in : IN STD_LOGIC;
...
data_reg 7 6 5 4 3 2 1 0 data_in
page 46 kubicek@vutbr.cz
VHDL – operators
Concatenate &
data_in : IN STD_LOGIC;
...
data_reg 7 6 5 4 3 2 1 0
data_reg 7 6 5 4 3 2 1 0 data_in
page 47 kubicek@vutbr.cz
VHDL
Statements
(commands)
page 48 kubicek@vutbr.cz
VHDL – statements
Concurrent statement:
• conditional statement WITH-SELECT-WHEN
• conditional statement WHEN-ELSE
• PROCESS as a whole
• component instantiation
• GENERATE statement
Sequential statement:
• variable assignment :=
• conditional statement IF-THEN-ELSIF-ELSE
• conditional statement CASE-WHEN
• loops (FOR, LOOP, NEXT, EXIT) and NULL statement
Concurrent/Sequential statement:
• signal assignment <= (expressions)
• function and procedure calls
• ASSERT-REPORT command
page 49 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
❑ Each concurrent statement represents part of a digital circuitry.
page 50 kubicek@vutbr.cz
VHDL – concurrent statements
page 51 kubicek@vutbr.cz
VHDL – concurrent statements
page 52 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
ARCHITECTURE Behavioral OF ALU IS
BEGIN
Z <= X + Y; 3 concurrent statements
X <= A;
Y <= B; Each statement has its
END Behavioral; physical image in
ALU implemented design
The sequence of
A X
statements has no effect
+ Z on implementation!
B Y
page 53 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
ARCHITECTURE Behavioral OF ALU IS
BEGIN
Z <= X + Y; 3 concurrent statements
X <= A + B;
Y <= C + D; Each statement has its
END Behavioral; physical image in
ALU implemented design
A The sequence of
+ X statements has no effect
B
+ Z on implementation!
C
+ Y
D
page 54 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
WITH mux_sel SELECT
y <= a WHEN "00",
b WHEN "01" | "10",
c WHEN OTHERS;
page 55 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
page 56 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
...
Z <= "0000" WHEN reset = '1' ELSE (X + Y);
...
page 57 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
❑ Sequence of the statements is not important
... ...
y <= a + b; x <= c + y;
x <= c + y; The same result and structure y <= a + b;
... ...
Resulting HW
A
+ Y
B + X
C
page 58 kubicek@vutbr.cz
VHDL – concurrent statements
Concurrent statements
❑ However, the manner of description is important, as well as implementation
options (resource sharing, optimization...)
... ...
y <= a + b; y <= a + b;
x <= y + c; Not always same structure x <= a + b + c;
... ...
Resulting HW
A
A A
+ Y
B
+ Y B + X
+ X B C
C
page 59 kubicek@vutbr.cz
VHDL
Sequential statements,
PROCESS
page 60 kubicek@vutbr.cz
VHDL – sequential statements
Sequential statement
❑ Used in processes, functions and procedures
❑ The order of the statements is important
❑ Can be used to describe both combinatorial and sequential logic
page 61 kubicek@vutbr.cz
VHDL – PROCESS
page 62 kubicek@vutbr.cz
VHDL – PROCESS
PROCESS
Combinatorial; the sensitivity list contains all
the signal from the right side of assignments and
from conditional statement arguments
With a
sensitivity list
Sequential; the sensitivity list contains only
clock signal and occasionally asynchronous
set/reset signal
page 63 kubicek@vutbr.cz
VHDL – PROCESS
sensitivity list
PROCESS (a,b,reset) List of all the signals that affect the process
BEGIN results – "output" signals (signal x in this case).
IF reset = '1' THEN
x <= 0; The sensitivity list must contain:
ELSE • all the signals of the right side of all the
x <= a + b; assignments (a and b in this case)
END IF; • all the signals present in arguments of
END PROCESS; conditional statements (reset in this case)
page 64 kubicek@vutbr.cz
VHDL – PROCESS
page 65 kubicek@vutbr.cz
VHDL – PROCESS
page 66 kubicek@vutbr.cz
VHDL – PROCESS
Combinatorial PROCESS
page 67 kubicek@vutbr.cz
VHDL – PROCESS
Combinatorial PROCESS
page 68 kubicek@vutbr.cz
VHDL – PROCESS
page 69 kubicek@vutbr.cz
VHDL – PROCESS
page 70 kubicek@vutbr.cz
VHDL – PROCESS
page 71 kubicek@vutbr.cz
VHDL – PROCESS
For Xilinx FPGA it is not recommended to use asynchronous set/reset in the design.
i
page 72 kubicek@vutbr.cz
VHDL – PROCESS
page 73 kubicek@vutbr.cz
VHDL – PROCESS
page 74 kubicek@vutbr.cz
VHDL – PROCESS
page 75 kubicek@vutbr.cz
VHDL – PROCESS
page 76 kubicek@vutbr.cz
VHDL – PROCESS
page 77 kubicek@vutbr.cz
VHDL – PROCESS
page 78 kubicek@vutbr.cz
VHDL
Variables
page 79 kubicek@vutbr.cz
VHDL – variables
ENTITY cnt_en_gen IS
PORT( clk : IN STD_LOGIC;
!
cnt_en_o : OUT STD_LOGIC);
END cnt_en_gen;
---------------------------------------------------------------
ARCHITECTURE Behavioral_1 OF cnt_en_gen IS
SIGNAL cnt_en : STD_LOGIC := '0';
---------------------------------------------------------------
BEGIN
PROCESS (clk)
VARIABLE cnt : UNSIGNED(7 DOWNTO 0) := (OTHERS => '0');
BEGIN
IF rising_edge(clk) THEN
cnt_en <= '0'; -- default assignment
cnt := cnt + 1; Fully functional code, but some
IF cnt = X"FF" THEN
cnt_en <= '1'; simulators cannot monitor variables
END IF; ➔ very difficult to debug the code!
END IF;
END PROCESS;
page 80 kubicek@vutbr.cz
VHDL – variables
ENTITY cnt_en_gen IS
PORT( clk : IN STD_LOGIC;
cnt_en_o : OUT STD_LOGIC);
END cnt_en_gen;
---------------------------------------------------------------
ARCHITECTURE Behavioral_2 OF cnt_en_gen IS
SIGNAL cnt_en : STD_LOGIC := '0';
SIGNAL cnt : UNSIGNED(7 DOWNTO 0) := (OTHERS => '0');
---------------------------------------------------------------
BEGIN
PROCESS (clk)
BEGIN
IF rising_edge(clk) THEN
cnt_en <= '0'; -- default assignment
cnt <= cnt + 1; The synthesis results in cnt signal
IF cnt = X"FF" THEN
cnt_en <= '1'; being output of a register, so it
END IF; makes perfect sense to define it as
END IF; a SIGNAL immediately in the code.
END PROCESS;
page 81 kubicek@vutbr.cz
VHDL – variables
page 82 kubicek@vutbr.cz
VHDL – variables
-- sig_A, sig_B, sig_C: STD_LOGIC_VECTOR(3 DOWTNO 0);
-- At the moment of process start:
-- sig_A = 3, sig_B = 5, sig_C = 2
PROCESS (clk)
VARIABLE v_A, v_B, v_C: STD_LOGIC_VECTOR(3 DOWTNO 0):= X"0";
BEGIN
IF rising_edge(clk) THEN
v_A := sig_A + 1; -- v_A = 3 + 1 = 4
v_B := sig_B + v_A; -- v_B = 5 + 4 = 9
v_C := v_A + v_B; -- v_C = 4 + 9 = 13
sig_A <= v_A; sig_B <= v_B; sig_C <= v_C;
END IF;
END PROCESS;
page 83 kubicek@vutbr.cz
VHDL – variables
-- sig_A, sig_B, sig_C: STD_LOGIC_VECTOR(3 DOWTNO 0);
-- At the moment of process start:
-- sig_A = 3, sig_B = 5, sig_C = 2
PROCESS (clk)
BEGIN
IF rising_edge(clk) THEN
sig_A <= sig_A + 1; -- sig_A = 3 + 1 = 4
sig_B <= sig_B + sig_A; -- sig_B = 5 + 3 = 8
sig_C <= sig_A + sig_B; -- sig_C = 3 + 5 = 8
END IF;
END PROCESS;
page 84 kubicek@vutbr.cz
VHDL
Conditional statements
page 85 kubicek@vutbr.cz
VHDL – conditional statements
Conditional statements
Concurrent statement
without PROCESS statement a 00
01
• WITH – SELECT – WHEN b
10
y
• WHEN – ELSE c 11
Sequential statement
select
inside PROCESS statement
• IF – THEN – ELSIF – ELSE
• CASE – WHEN
page 86 kubicek@vutbr.cz
VHDL – conditional statements
page 87 kubicek@vutbr.cz
VHDL – conditional statements
page 88 kubicek@vutbr.cz
VHDL – conditional statements
Entity description
ENTITY MUX_simple IS
PORT(
a,b,c : IN STD_LOGIC;
a 00
select : IN STD_LOGIC_VECTOR(1 DOWNTO 0); 01
y : OUT STD_LOGIC b y
10
);
c 11
END MUX_simple;
page 89 kubicek@vutbr.cz
VHDL – conditional statements
a 00
01
WITH select SELECT b y
10
y <= a WHEN "00",
c 11
b WHEN "01" | "10",
c WHEN OTHERS;
select
page 90 kubicek@vutbr.cz
VHDL – conditional statements
WHEN – ELSE
a 00
01
b y
10
y <= a WHEN select = "00" ELSE
c 11
b WHEN select = "01" OR select = "10" ELSE
c;
select
page 91 kubicek@vutbr.cz
VHDL – conditional statements
a 00
01
mux_4: PROCESS (select,a,b,c) b y
10
BEGIN
c 11
IF (select = "00") THEN
y <= a;
ELSIF (select = "01" OR select = "10") THEN
y <= b; select
ELSE
y <= c;
END IF;
END PROCESS mux_4;
page 92 kubicek@vutbr.cz
VHDL – conditional statements
CASE – WHEN
a 00
01
mux_4: PROCESS (select,a,b,c) b y
10
BEGIN
c 11
CASE select IS
WHEN "00" => y <= a;
WHEN "01" | "10" => y <= b;
WHEN OTHERS => y <= c; --"11" select
END CASE;
END PROCESS mux_4;
page 93 kubicek@vutbr.cz
VHDL
Loops
page 94 kubicek@vutbr.cz
VHDL - loops
Simple LOOP
LOOP
... <statements>
IF <condition> THEN
... <statements>
EXIT; -- termination of the loop at <condition>
END IF;
... <statements>
END LOOP;
page 95 kubicek@vutbr.cz
VHDL - loops
Simple LOOP
LOOP_1: LOOP
... <statements>
IF <condition> THEN
... <statements>
EXIT LOOP_1; -- termination of the loop at <condition>
END IF;
... <statements>
❑ Loop name (LOOP_1 in this case) is optional. Using loop names it is easier to read the
code and it is also possible to create nested loops.
page 96 kubicek@vutbr.cz
VHDL - loops
FOR LOOP
LOOP_1: FOR k IN 100 DOWNTO 1 LOOP
... <statements>
NEXT LOOP_1 WHEN <condition>;
EXIT LOOP_1 WHEN <condition>;
END LOOP LOOP_1;
WHILE LOOP
LOOP_1: WHILE <condition> LOOP
... <statements>
NEXT LOOP_1 WHEN <condition>;
EXIT LOOP_1 WHEN <condition>;
END LOOP LOOP_1;
page 98 kubicek@vutbr.cz
VHDL - loops
Loops: properties
❑ Not well supported by synthesis tools – algorithmic character of
descriptions does not suit well to digital circuitry
❑ Before using loops it is wise to learn abilities of your synthesizer
(ug901-vivado-synthesis.pdf)
❑ Loops are great tool for simulation – unlimited use, nested loops,
variable limits...
page 99 kubicek@vutbr.cz
VHDL - loops
SI
PROCESS (clk) BEGIN
7 6 5 4 3 2 1 0
IF rising_edge(clk) THEN clk
for i in 0 to 6 loop
shreg(i+1) <= shreg(i);
end loop;
shreg(0) <= SI;
END IF;
END PROCESS;
clk
Structural description
AND_4in
I1
I2
Y
I3
I4
LIBRARY IEEE;
A Y
use IEEE.std_logic_1164.ALL;
-------------------------------------------- B
ENTITY AND_gate IS
PORT ( A : IN STD_LOGIC;
B : IN STD_LOGIC;
Y : OUT STD_LOGIC);
END AND_gate;
--------------------------------------------
ARCHITECTURE Behavioral OF AND_gate IS
BEGIN
Y <= '1' WHEN (A='1' AND B='1') ELSE '0';
END Behavioral;
BEGIN
AND_3: AND_gate PORT MAP ( A => S1, B => S2, Y => Y );
AND_1: AND_gate PORT MAP ( A => I1, B => I2, Y => S1);
AND_2: AND_gate PORT MAP ( A => I3, B => I4, Y => S2);
END Structural;
AND_3: AND_gate
PORT MAP(
A => S1,
B => S2,
Y => Y );
-----------------------------------------
END Structural;
Final notes
❑ Synthesizer is not interpreting the code exactly (as simulator does), it is trying to
recognize known structures
❑ For reliable design it is necessary to use standard design coding styles (especially
for synchronous sequence systems)
General rules
❑ Each statement is terminated with semicolon
❑ Complex commands can be written on more lines; CR/LF, spaces and TABs
can be combined to create well-readable code
❑ Identifiers are not allowed to start with a number or underscore, end with
underscore, or contain two underscores in a row
❑ Never use spaces or special characters in filename, project name or
any directory (path) related to your project.
General rules
❑ Do not underestimate warnings, read and analyze them from the beginning,
use message filtering to suppress irrelevant warnings, Google the message if you are
not sure what does it mean (many other designers have had the same problem).
D Q D Q
CLK CLK
Asynchronous systems
❑ In case they contain a clock signal, it is not common for all its
sequential components (registers)
❑ They are able to respond immediately to an input stimulus
❑ They are usually less complex (in terms of utilized gates and
registers) than equivalent synchronous systems
❑ Very difficult to design and even more to verify
❑ Gradual signal propagation, risk of hazards
Synchronous systems
❑ Can change its state only in certain moments defined by a single
control (clock) signal
❑ Much easier design and verification
❑ Much more reliable, less dependent on external conditions
❑ Perfectly suitable for CAD tools (design automation)
❑ Today almost all digital systems are synchronous
KČ3
KČ3
Counters
Very simple Finite State Machines (FSMs)
❑ Binary
❑ Decimal
❑ Gray
❑ Johnson
❑ LFSR
❑ ...
Binary counter
4-bit counter: 2N = 16 states (0 to (2N – 1) = 15)
State diagram (free-running counter)
0 1 2 ... 14 15
BIN
clk
Binary counter
Incremented with each rising edge of a clock signal (CLK)
4b counter: 2N = 16 states (0 to (2N – 1) = 15)
N bits ➔ N registers (flip-flops)
clk
b[0]
b[1]
b[2]
b[3]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 ....
D Q D Q D Q D Q
_ _ _ _
Q Q Q Q
D Q D Q D Q D Q
_ _ _ _
clk Q Q Q Q
clk
b[0]
b[1]
b[2]
b[3]
TP TP TP TP 4·TP
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0000 0001...
Variants:
• Output inversion = down counting
• Bidirectional (up/down input)
• Synchronous/asynchronous set/reset
• Load a value
+1
REG
clk
System clock signal 125 MHz
clk_EN
1:5 => 1/5 * 125 MHz = 25 MHz
clk
System clock signal 125 MHz
clk_EN
1:5 => 1/5 * 125 MHz = 25 MHz
Decimal counter
binary counter with shortened cycle
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001
0 1 2 ... 8 9
No treatment of unused states, counter is not User defined treatment of unused states; can
forced to return to a valid operating state. It consume more FPGA resources but behavior is
is possible to ask the implementation tool to explicitly defined. Be aware of optimization
take care of this automatically. setting of the synthesis tool, some can result in
complete removal of this unused state transition
logic!
0 1 2 ... 8 9
10 11 12 13 14 15
0 1 2 ... 8 9
10 11 12 13 14 15
Gray Counter
Only single bit changes when transitioning between adjacent states
Usage:
• FIFO addressing
• A/D converters
• Absolute position sensors
Gray counter
Only single bit changes when transitioning between
adjacent states
b(0) g(0)
b(1) g(1)
b(2) g(2)
b(3) g(3)
b(0)
g(0)
b(1)
g(1)
b(2)
g(2)
b(3)
g(3)
15 14 10 2 0
10 2
Gray counter
Recommended description in VHDL (Xilinx Vivado: Language Templates)
constant N : INTEGER := 8;
signal gray_cnt : UNSIGNED( N-1 downto 0 );
signal next_bin_cnt : UNSIGNED(gray_cnt'range);
signal next_bin_cnt : UNSIGNED(gray_cnt'range);
...
process (clk) begin
if rising_edge(clk) then
bin_cnt <= next_bin_cnt;
gray_cnt <= (('0' & next_bin_cnt(next_bin_cnt 'HIGH downto 1))
XOR next_bin_cnt);
end if;
end process;
REG
page 155 kubicek@vutbr.cz
Counters
Gray counter
next_bin_cnt
The same code written in a different
way (strictly separated combinatorial bin_cnt
g(0) b(0)
XOR b(0) XOR g(0)
g(1) b(1)
XOR b(1) XOR g(1)
g(2) b(2)
XOR b(2) XOR g(2)
Johnson counter A B C D E Y
0 0 0 0 0 0
❑ Very simple implementation
1 0 0 0 0 1
❑ High maximum clock frequency 1 1 0 0 0 2
❑ The cycle length is only 2·N (compared to 2N in 1 1 1 0 0 3
case of binary or Gray counter) ➔ large number of
1 1 1 1 0 4
unused states
1 1 1 1 1 5
❑ Sometimes used as a decimal counter (with a
decoder on its output) in DA converters 0 1 1 1 1 6
0 0 1 1 1 7
0 0 0 1 1 8
0 0 0 0 1 9
REG N-bit
LFSR counter
❑ Liner Feedback Shift Register
❑ Very simple implementation
❑ High maximum clock frequency
❑ The cycle length is 2N-1
❑ Generator of PRBS (Pseudo-Random Binary
Sequence, also called M-sekvence)
REG N-bit
REG N-bit 14
15
14,5,3,1
15,14
41
42
41,38
42,41,20,19
16 16,15,13,4 43 43,42,38,37
17 17,14 44 44,43,18,17
18 18,11 45 45,44,42,41
19 19,6,2,1 46 46,45,26,25
20 20,17 47 47,42
21 21,19 48 48,47,21,20
22 22,21 49 49,40
23 23,18 50 50,49,24,23
24 24,23,22,17 51 51,50,36,35
25 25,22 52 52,49
26 26,6,2,1 53 53,52,38,37
27 27,5,2,1 54 54,53,18,17
page 160 kubicek@vutbr.cz 28 28,25 55 55,31
29 29,27 56 56,55,35,34
Counters
...
...
...
...
...
...
ENTITY LFSR_gen IS
GENERIC( Tap_1 : INTEGER := 23; LFSR_gen
Tap_2 : INTEGER := 18)
PORT( clk : IN STD_LOGIC;
PRBS_o : OUT STD_LOGIC);
Tap_1 = 23
END LFSR_gen; Tap_2 = 18
...
clk PRBS_o
PROCESS (clk) BEGIN
IF rising_edge(clk) THEN
ShReg <= ShReg(ShReg'HIGH-1 DOWNTO 1) & Feedback;
END IF;
END PROCESS;
,,
,,::,, ,::::
;;;;;;;,,;;;;;
(;;;;;;;;;;;;)
( ° ° )==\\
( ) \\
( ° ° ) ||
( ° ° ) ||
( ° ° ) ||
( ° ° )==//
\__________/
\--------/
PROCESS( ========================== ) BEGIN