Lecture 3-4 - VHDL Basics
Lecture 3-4 - VHDL Basics
Lecture 3-4 - VHDL Basics
VHDL Basics
TIE-50206 Logic Synthesis
Arto Perttula
Tampere University of Technology
Fall 2016
Contents
• VHDL basics
– Entity – the interface
• Ports, generics
– Architecture – the behavior
• Signals, types
• Process, component instantiation, control statements
– Library, package
signal
variable
7.11.2016 5
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;
7.11.2016 39
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
51
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 7.11.2016 72
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 7.11.2016 77
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
7.11.2016 88
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
90
EXTRA SLIDES ON VHDL
92
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
98
Type Conversion Between
Number-Related Data Types
regs : BLOCK
BEGIN
statements
END BLOCK regs;
END behav;