Lecture 2-4 - VHDL Basics PDF
Lecture 2-4 - VHDL Basics PDF
Introduction to System
Design, VHDL Basics
TIE-50206 Logic Synthesis
Arto Perttula
Tampere University of Technology
Fall 2015
Contents
• 1. Introduction to System Design
– Abstraction
– Main phases
• 2. VHDL basics
– Entity – the interface
• Ports, generics
– Architecture – the behavior
• Signals, types
• Process, component instantiation, control statements
– Library, package
Arto Perttula 19.10.2015 2
Acknowledgements
• Prof. Pong P. Chu provided ”official” slides for
the book which is gratefully acknowledged
– See also: http://academic.csuohio.edu/chu_p/
• Most slides were originally made by Ari Kulmala
– and other previous lecturers (Teemu Pitkänen, Konsta
Punkka, Mikko Alho, Erno Salminen…)
1. INTRODUCTION TO
SYSTEM DESIGN
Arto Perttula 19.10.2015 4
Examples of Different Views
• View: different perspectives of a system
1. Behavioral view:
– Describe functionalities and i/o behavior
– Treat system as a black box
2. Structural view:
– Describe the internal implementation
(components and interconnections)
– Essentially a block diagram (or schematic)
3. Physical view:
– Add more info to structural view: component size, component locations, routing wires
– E.g., layout of a print circuit board
outputs:
led0_out
audio_out
...
Function:
When user presses
button1, then...
When...
higher abstraction
Arto Perttula 19.10.2015 6
Complexity Management
• Q: How to manage complexity for a chip with 10 million transistors?
• A: Abstraction – a simplified model of a system
– Show the selected features
– Ignore many details
• E.g., timing of an inverter
ELT-
xxxx
DigiPer.
Dig Suunn
adder
divide Dig.Suunn.
FSM
this
SW CPU MEM
behavioral NOC System
I/O ACC design
Comb.
D Q using RTL
logic
abstraction
11
Register-Transfer Level (RTL) (2)
• RT (Register Transfer) is a bit misleading term
• Two meanings:
1. Loosely: represent the module level
2. Formally: a design methodology in which the system operation is described by how the data is manipulated
and moved among registers
T2 x2 foo(x1) bar(foo(x0))
a_r b_r c_r
...,x1,x0 D Q foo() D Q bar() D Q
12
Key for Success: Hierarchy
Syntax:
Corresponds to the entity
Parameters
Ports
Arto Perttula 14
Example
• A hierarchical two-digit decimal counter
– pulse=1 when q is 9
– p100=1 when both q_ten and q_one are 9
– However, if en goes 0 when q=9, something strange happens…
• Let’s concentrare on the structure…
Top-level block diagram, ”hundred_counter”
Single
counter
component
15
Example Implemented in VHDL
Top-level entity
1. INTRODUCTION TO
SYSTEM DESIGN
Arto Perttula 19.10.2015 17
System Development
• Developing a digital system is a refining and validating process
• Main tasks:
I. requirements
I-III. verification
capture, specification
1. INTRODUCTION TO
SYSTEM DESIGN
Arto Perttula 19.10.2015 25
EDA Software
• EDA (Electronic Design Automation) software can automate many tasks
• Mandatory for success together with re-use!
• Can software replace human hardware designer? (e.g., C-program to chip)
• Synthesis software
– Should be treated as a tool to perform transformation and local optimization
– Cannot alter the original architecture or convert a poor design into a good one
– See also the so called ”Mead & Conway revolution”
• EDA tools abstraction level in functional description has not increased
significantly since mid-90’s when RT-level gained popularity
– Increased abstraction always causes some penalty in performance, area etc.
when increasing abstraction, but significant improvement in time to design
Arto Perttula 19.10.2015 26
Design Flow
data file process
– Design partition
device simulation/
(Quartus)
C <= A AND B
Physical design 34
Interface
Ports Declarations
Functionality
• Provide communication channels (=pins) between the component and its environment
• Each port must have a name, direction and a type
– An entity may omit port declaration, e.g., in test bench
• Port directions:
1. IN: A value of a port can be read inside the component, but cannot be assigned. Multiple reads
of port are allowed.
2. OUT: Assignment can be made to a port, but data from a port cannot be read. Multiple
assignments are allowed.
3. INOUT: Bi-directional, assignments can be made and data can be read. Multiple assignments
are allowed. (not recommended inside a chip)
4. BUFFER: An out port with read capability. May have at most one assignment (not recommended)
Signals Declarations
Functionality
entity
VHDL declaration
source package
files declaration
architecture
architecture package configuration
architecture
body
body body declaration
body
arch declarations:
• Architectures contains usually either signals,
functions,
a) processes, or types
entity_name.vhd
Arto Perttula
Relation Between Circuit And VHDL
Realization Examples GENERIC VHDL STRUCTURE
A : IN
B : IN
Sel : IN
add C : OUT
A
Adder
B
0
Multiplier 1 C
mul signal add, mul
Components +, *
declaration
The interface of a
block ”even_detector”
Input a, 3 bits
Output even, 1 bit
p4
Functionality of the
block (gate level
representation).
The order of
assigments does
NOT matter here.
signal
variable
19.10.2015 48
Process (2)
• Processes are the basic building blocks of functional (in most cases
that means RTL) description
• Practically every design has at least one process
• In VHDL, the flip-flops are generated with (synchronous) processes
– No reserved word for registers in VHDL
– Synthesis/simulation tools recognize certain process structures that
imply registers (set of D-flip-flops)
– To be covered later
Modeling style
Location Inside architecture Inside process or function
Example statements process, component instance, concurrent if, for, switch-case, signal assignment,
signal assignment variable assignment
implies
= results in latch!
(in comb. processes, always include the else-branch)
PROCESS (...)
VARIABLE Q1_v : STD_LOGIC;
BEGIN -- PROCESS
Q1_v := ‘0’;
Q2_v := not Q1_v; -- Q2 sees the “new” value of Q1
• types-+-scalar----+-discrete-------+-integer-------+-integer
• | | | +-natural
• | | | +-positive
• | | |
• | | -enumeration---+-boolean
• | | +-bit
• | | +-character
• | | +-file_open_kind
• | | +-file_open_status
• | | +-severity_level
• | |
• | +-floating point-+-----------------real
• | |
• | +-physical-------+-----------------delay_length
• | +-----------------time
• |
• +-composite-+-array----------+-constrained-
• | | |
• | | +-unconstrained-+-bit_vector
• | | +-string
• | |
• | +-record-
• |
• +-file-
• |
• +-access
READLINE(my_in_file, in_line_v);
READ(in_line_v, tmp_v);
-- many flavors of read() available
WRITE(out_line_v, tmp_v);
WRITELINE(my_out_file, out_line_v);
end loop;
end if;
end process access_files;
entity test_file87 is
end test_file87;
architecture behav of test_file87 is
signal clk : std_logic := '0';
signal rst_n : std_logic;
begin -- behav
rst_n <= '0', '1' after 50 ns;
clk <= not clk after 10 ns;
access_files : process (clk, rst_n)
-- vhd'87 syntax
file my_in_file : text is in "input.txt";
file my_out_file : text is out "output.txt";
variable in_line_v, out_line_v : line; -- type "LINE" is a pointer to a string
variable tmp_v : integer := 0;
begin
if rst_n = '0' then -- asynchronous reset (active low)
end if;
end process access_files;
end behav;
19.10.2015 82
More Complex Example in VHDL’93
access_files : process (clk, rst_n)
-- vhd'93 syntax, only these 2 lines differ from previous in minimal case
file my_in_file : text open read_mode is "input.txt";
file my_out_file : text open write_mode is "output.txt";
variable in_line_v, out_line_v : line;
variable tmp_v : integer;
variable valid_number : boolean := false;
variable curr_line : integer := 0;
begin
if rst_n = '0' then -- asynchronous reset (active low)
…
elsif (clk'event and clk = '1') then -- rising clock edge
valid_number := false;
-- Loop until finding a line that is not a comment.
while valid_number = false and not(endfile(my_in_file)) loop
WRITE(out_line_v, tmp_v);
WRITELINE(my_out_file, out_line_v); -- a) write to file, b) to terminal: WRITELINE(output, out_line_v);
end loop;
end if;
end process access_files;
array E.g. std_logic_vector is array. Define the array type first and then
signal/constnat/variable of that type
…
• Package body consists of
– type and subtype declarations
– subprogram declarations and bodies
design_n.
– deferred constants (avoids some re-compilation, rare concept)
vhd
– file declarations
PACKAGE io_pkg IS
CONSTANT addr_width_c : NATURAL := 16;
CONSTANT data_width_c : NATURAL := 16;
CONSTANT stat_c : NATURAL := 1;
CONSTANT total_out_c : NATURAL := 10;
TYPE o_bits_arr IS ARRAY (0 to total_out-1)
OF NATURAL;
FUNCTION inmux(
data : STD_LOGIC_VECTOR(data_width_c-1 downto 0);
sel : NATURAL)
RETURN STD_LOGIC_VECTOR;
END io_pkg;
A package used in
this design
94
2f. Standard Packages
a a a
y ”00” y y
7 6 5 0 7 6 5 0 7 6 5 0
Array Aggregate
• Aggregate is a VHDL construct to assign a value to an array-typed object
• Different types supported, e.g.,
a <= "10100000"; --direct
a <= (7=>'1', 6=>'0', 0=>'0', 1=>'0',
5=>'1', 4=>'0', 3=>'0', 2=>'1');
a <= (7|5=>'1', 6|4|3|2|1|0=>'0');
a <= (7|5=>'1', others=>'0');
• E.g., setting all elements at the same time
a <= "00000000“ -- Size of a has to be known
a <= (others=>'0'); -- Size not needed, Flexible, Good
-- for reset. Superb!
-- Wrong
u5 <= sg; -- type mismatch
u6 <= 5; -- type mismatch, 5 is integer/natural
-- Fixed
u5 <= unsigned(sg); -- type casting
u6 <= to_unsigned(5,4); -- use conversion function,
-- use 4 bits to represent
-- the value 5
Arto Perttula 19.10.2015 115
Example (continued 2)
-- Wrong
u7 <= sg + u1; -- + undefined over these types
-- Fixed
u7 <= unsigned(sg) + u1; -- ok, but be careful
-- Wrong
s3 <= u3; -- type mismatch
s4 <= 5; -- type mismatch
-- Fixed
s3 <= std_logic_vector(u3); -- type casting
s4 <= std_logic_vector(to_unsigned(5,4));
-- Fixed
s5 <= std_logic_vector(unsigned(s2)
+ unsigned(s1));
-- Id: R.1
function RESIZE (ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED;
-- Result subtype: SIGNED(NEW_SIZE-1 downto 0)
-- Result: Resizes the SIGNED vector ARG to the specified size.
-- To create a larger vector, the new [leftmost] bit positions
-- are filled with the sign bit (ARG'LEFT). When truncating,
-- the sign bit is retained along with the rightmost part.
-- Id: R.2
function RESIZE (ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED;
-- Result subtype: UNSIGNED(NEW_SIZE-1 downto 0)
-- Result: Resizes the SIGNED vector ARG to the specified size.
-- To create a larger vector, the new [leftmost] bit positions
-- are filled with '0'. When truncating, the leftmost bits
-- are dropped.
Radix=
unsigned
hex signed
hex
Note: showing values in hex format is bit misleading with negative numbers 19.10.2015 120
Resize() in numeric_std.vhdl (2)
-- Id: R.1
function RESIZE (ARG: SIGNED; NEW_SIZE: NATURAL) return SIGNED is
alias INVEC: SIGNED(ARG'LENGTH-1 downto 0) is ARG;
variable RESULT: SIGNED(NEW_SIZE-1 downto 0) := (others => '0');
constant BOUND: INTEGER := MIN(ARG'LENGTH, RESULT'LENGTH)-2;
begin
if (NEW_SIZE < 1) then return NAS;
end if;
if (ARG'LENGTH = 0) then return RESULT;
end if;
RESULT := (others => ARG(ARG'LEFT)); -- sign extension
if BOUND >= 0 then
RESULT(BOUND downto 0) := INVEC(BOUND downto 0);
end if;
return RESULT;
end RESIZE;
-- Id: R.2
function RESIZE (ARG: UNSIGNED; NEW_SIZE: NATURAL) return UNSIGNED is
constant ARG_LEFT: INTEGER := ARG'LENGTH-1;
alias XARG: UNSIGNED(ARG_LEFT downto 0) is ARG;
variable RESULT: UNSIGNED(NEW_SIZE-1 downto 0) := (others => '0');
begin
if (NEW_SIZE < 1) then return NAU;
end if;
if XARG'LENGTH =0 then return RESULT;
end if;
if (RESULT'LENGTH < ARG'LENGTH) then
RESULT(RESULT'LEFT downto 0) := XARG(RESULT'LEFT downto 0);
else
RESULT(RESULT'LEFT downto XARG'LEFT+1) := (others => '0');
RESULT(XARG'LEFT downto 0) := XARG;
end if;
return RESULT;
end RESIZE;
http://www.tkt.cs.tut.fi/kurssit/50200/S14/Harjoitukset/conversion.html
• Example:
...
TYPE bit_array IS ARRAY (5 DOWNTO 1) OF BIT;
...
SIGNAL tmp_r : INTEGER;
...
tmp_r <= bit_array’LEFT;
-- tmp_r is assigned with a value of 5
Source: Zainalabedin Navabi, VHDL: Modular Design and Synthesis of Cores and Systems
19.10.2015 131
VHDL Summary
Language Purpose Other notes C++ counterpart
constructs in VHDL
ENTITY Defines interface. Includes generics and ports ”Public interface”, the actual implementation is Class definition
(their names, widths, and directions). hidden into architecture.
GENERIC Instance-specific constant value Excellent idea in HDL! Constant parameters, templates
PORT I/O pin of an entity. Defines direction and type. See also signal. Method of a class, inter-process
message
ARCHITECTURE Contains functionality. One entity may have many architectures in the Class implementation
library
SIGNAL, Communication channel between They are not the same! Variables only inside Variable
(VARIABLE) components/processes. processes
COMPONENT For instantiating a sub-block Needed for hierarchy. Class instance, object
PROCESS These capture most of the functionality. Processes are executed in parallel. Both seq. Thread
and comb.
IF,FOR,CASE, Control statements Bounds must be known for loops at compile- The same
ASSIGNMENT time
PACKAGE Contains shared definitions. Constants, functions, procedures, types Header file (file.h)
LIBRARY Holds analyzed (’compiled’) codes Standard ieee library is practically always used Compiled object codes (file.o)
SystemVerilog
( )
VHDL
Verilog-95
133
EXTRA SLIDES ON VHDL
136
Signal Drivers
• Every signal has at least one driver, if it is not disconnected
• Signal assignment changes driver
• A conceptual circuit that is created for every signal driver
• Example of driver:
signal ex: positive;
...
ex <= 3 AFTER 5 ns, 2 AFTER 10 ns,
4 AFTER 15 ns, 0 AFTER 20 ns;
ex
142
Type Conversion Between
Number-Related Data Types
regs : BLOCK
BEGIN
statements
END BLOCK regs;
END behav;