Professional Documents
Culture Documents
VHDL
VHDL
The content is the short summary of the topic as mentioned. Reader is advised to study further
for acquiring an in-depth knowledge.
biswajitb
Entity declaration
Architecture declaration
■ Entity declaration:
Entity is a black box representing the hardware terminals (ports) only. In entity
declaration, following things are declared:
1) Name of the black box, called Entity name.
2) Signal labels, i.e., name of the signal-terminals (ports)
3) Signal types (e.g., whether a signal is in the form of bit etc.)
4) Signal direction (e.g., in, out etc.)
Here x, y and z are labels for signals. The labels are inscribed within the entity as black
box.
■ Architecture declaration:
Architecture represents functionality of entity in terms of relation among i/o signals. Thus
architecture is the circuit realization of functions.
2
biswajitb
Solution:
Name of the entity for the present application is arbitrarily given as and2. In the present
case, this is the entity name.
The relation among x, y and z is defined within architecture declaration.
Here, exm1 is the name of the architecture wherein an AND function, as an example, is
implemented.
Evidently, there may have numerous relations among x, y and z, and a number of
architectures exm1, exm2, exm3 etc. (architecture name may be anything else) may be
defined against a single entity. Thus, all these architectures will be a part of this particular
entity.
library ieee;
use ieee.std_logic_1164.all;
entity and21 is
port (x,y: in BIT; z:out BIT);
end entity and21;
3
biswajitb
Note:
1) The first two lines are mandatory, significance of which will be explained later.
3) For the entity and2 (user-given name), there may be multiple architectures like
exm1, exm2, exm3 etc., or any other name given by user.
5) The keyword ‘port’ specifies the signal-terminals, and type as well as the
direction of the signals.
6) ‘<=’ is the assignment operator indicating that the signal, generated as a result
of AND operation between x and y, is assigned (placed) to a signal, called z.
The above program, implementing and-gate, should be tested now with external signals
applied to ports (terminals) of entity.
It is assumed that a and b are the name of the input signals connected to entity labels x
and y, respectively. Thus,
These signals are externally generated in the laboratory through a hardware setup, called
testbench. However, in software environment, a testbench program is used to generate
and connect the signals to the internal labels x, y and z of the entity and2.
library ieee;
use ieee.std_logic_1164.all;
entity tb_and2 is
end entity tb_and2;
architecture io of tb_and2 is
4
biswajitb
Note:
2) The statement:
The command port map establishes a mapping between x and a, between y and
b, and also between z and c.
The direction of mapping is always from internal label to external label, irrespective of
the type of the signals.
5
biswajitb
Both the files must be compiled simultaneously under a common project environment.
The testbench program runs on top of other programs as if the signals are generated in a
test bench and connected down to targeted hardware.
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity or21 is
port(x,y: in BIT; z:out BIT);
end entity or21;
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity not1 is
port(x:in BIT; z:out BIT);
end entity not1;
6
biswajitb
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity xor2 is
port(a, b:in BIT; c:out BIT);
end entity xor2;
7
biswajitb
Three signals p, q and r are defined within architecture. They are neither in, nor out as
they are used as interface between gates. With respect to some gates, they act as input
signals, to others they act as output signals.
library ieee;
use ieee.std_logic_1164.all;
entity tb_xor2 is
end entity tb_xor2;
architecture io of tb_xor2 is
signal u, v, w : BIT;
begin
gx: entity WORK.xor2(exm1) port map(a => u, b => v, c => w);
u <= '0', '1' after 100 ns, '0' after 200 ns;
v <= '0', '1' after 150 ns, '0' after 300 ns, '1' after 420 ns;
end architecture io;
In previous programs, all signals were of type BIT. Each of BIT variables may have two
values only – ‘0’ and ‘1’, written in program within single quote.
8
biswajitb
‘U’ - Uninitialized
‘X’ - Forcing (i.e., strong) unknown
‘0’ - Forcing 0
‘1’ - Forcing 1
‘Z’ - High impedance
‘W’ - Weak unknown
‘L’ - Weak 0
‘H’ - Weak 1
‘-’ - Don’t care
If it is required to develop a signal with three logic states like ‘0’, ‘1’ and ‘Z’ only, then
one could define a variable tri_var (defined with arbitrary name by user) like:
However, there is a problem in this definition. If two signals of type tri_var is connected
to the same node, such that one signal forces a ‘1’ upon the node while the other forces a
‘0’, then the resulting state of the node will remain undefined. The actual state of the
node depends on the particular state of the signal having greater power.
Thus, a function must be defined to decide a result out of such a conflicting situation.
This is called resolution function. A resolution function may be defined for signals of
type tri_var as:
0 1 Z
0 0 0 Z
1 0 1 Z
Z Z Z Z
It is apparent that if Z is one input, output from the system will be always Z, irrespective
of the other input. Except Z acting as input, if 0 is an input, output must be 0 irrespective
of whether the other input is 0 or 1. Thus, in this system, Z has the highest power (effect)
and 1 as the lowest.
However, a resolution function for all nine variables is already defined in IEEE standard
1164. This is:
9
biswajitb
U X 0 1 Z W L H -
U U U U U U U U U U
X U X X X X X X X X
0 U X 0 X 0 0 0 0 X
1 U X X 1 1 1 1 1 X
Z U X 0 1 Z W L H X
W U X 0 1 W W W W X
L U X 0 1 L W L W X
H U X 0 1 H W W H X
- U X X X X X X X X
Resolution function for nine variables
The table (resolution function) as shown above is constructed according to the following
logic:
The variable type std_logic supports these nine states along with the resolution function
mentioned above.
Hence, in the program we incorporate library IEEE and then use IEEE.std_logic_1164.all
within it.
Note:
Library IEEE;
Use IEEE.std_logic_1164.all;
should appear before each entity declaration and will apply to any architecture(s)
declared for that entity.
If more than one entity declaration appears in a file (for instance, of a model and of its
testbench), the library and use statements must appear before each entity. In other
words, VHDL scope rules apply to design units and not to the files in which these design
units are declared.
10
biswajitb
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity tristate is
port(a, en: in std_logic; z: out std_logic);
end entity tristate;
library ieee;
use ieee.std_logic_1164.all;
entity test_tristate is
end entity test_tristate;
11
biswajitb
architecture io of test_tristate is
signal p,q,r: std_logic;
begin
g1: entity WORK.tristate(exm1) port map (a=>p, en=>q, z=>r);
p <='1', '0' after 100 ns, '1' after 250 ns, '0' after 500 ns;
q <= '1', '0' after 130 ns, '1' after 280 ns, '0' after 460 ns;
end architecture io;
Solution:
12
biswajitb
A1 A0 Z3 Z2 Z1 Z0
0 0 0 0 0 1
0 1 0 0 1 0
1 0 0 1 0 0
1 1 1 0 0 0
Truth Table
library ieee;
use ieee.std_logic_1164.all;
entity decoder2to4 is
port (a: in std_logic_vector(1 downto 0);
z: out std_logic_vector(3 downto 0));
end entity decoder2to4;
library ieee;
use ieee.std_logic_1164.all;
entity test_decoder2to4 is
end entity test_decoder2to4;
architecture io of test_decoder2to4 is
signal ain: std_logic_vector(1 downto 0);
signal zout: std_logic_vector(3 downto 0);
begin
g1:entity WORK.decoder2to4(exm1) port map(a=> ain, z=>
zout);
ain <= "00", "01" after 150 ns, "10" after 300 ns;
end architecture io;
13
biswajitb
Note:
2) Last else containing “XXXX” may be omitted. At this, when all four conditions of a
are false, z continues to take last value that was assigned to it earlier. This is the
implementation of latched action.
…
…
else
unaffected;
3) Each of when – else pair is checked one by one until a match is found.
Note:
1) In with – select statement, all the alternatives are checked simultaneously, unlike the
checking strategy in when – else statement.
2) Therefore, when others clause must be written to cover all possible combinations,
otherwise compilation will fail.
4) Furthermore, the patterns must be constants so that these can be determined when the
VHDL is being compiled, NOT dynamically in the course of a simulation.
5) If more than one input pattern gives the same output, the patterns can be listed by use
of OR – symbol, such as
14
biswajitb
Solution:
For any particular pattern of input bits in vector a, only one output bit from the vector z
will be 1. This 1 is kept initially at z(0).
Now, if the vector a represents a number m, this 1 in z has to be rotated by m positions.
Thus, a = “000 … 000” makes z(0) = 1, all other bits in z being at 0;
a = “000 … 001” makes z(1) = 1, all other bits in z being at 0;
a = “000 … 010” makes z(2) =1, all other bits in z being at 0; and so on.
sll
sla
15
biswajitb
srl
sra
rol
ror
Note:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity decoder is
generic (n: positive);
port(a: in std_logic_vector(n-1 downto 0);
z: out std_logic_vector(2**n-1 downto 0));
end entity decoder;
16
biswajitb
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test_decoder is
end entity test_decoder;
architecture io of test_decoder is
constant n : natural := 3;
signal s: std_logic_vector(n-1 downto 0);
signal y: std_logic_vector(2**n-1 downto 0);
begin
g1: entity WORK.decoder(exm1) generic map(n)
port map(s, y);
s <= "000", "001" after 200 ns, "110" after 300 ns;
end architecture io;
Solution:
17
biswajitb
library ieee;
use ieee.std_logic_1164.all;
entity priority4to2 is
port (a: in std_logic_vector(3 downto 0);
y: out std_logic_vector(1 downto 0);
v: out std_logic);
end entity priority4to2;
Note:
The logic ‘-’ in VHDL is just another logic. It is not the conventional don’t care state. It
does not include all other logic states in std_logic.
18
biswajitb
Structural
Uses component instantiations
-- already used.
Dataflow
Uses concurrent signal assignment
-- already used.
Sequential
Resembles conventional programming language
1) Subprograms
i. Procedures
ii. Functions
2) Processes
Solution:
19
biswajitb
Note:
2) The process is evaluated only when the signals in the sensitivity list changes.
Thus, the sensitivity list includes all signals that might cause an output to change.
Here, a is a vector and the process is evaluated when any bit of a changes.
#. Option – 1 :
#. Option – 2 :
for n in 1 to N loop
…
…
end loop;
Constant:
N = maximum loop count
Variable:
n = takes decremented / incremented value
20
biswajitb
1) The ascending & descending nature of the indices of x is defined in entity using to &
downto, respectively.
Solution:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity priority is
generic (n:positive);
port (a: in std_logic_vector(2**n-1 downto 0);
y: out std_logic_vector(n-1 downto 0);
v: out std_logic);
end entity priority;
21
biswajitb
end if;
end loop;
end process prc;
end architecture exm1;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test_priority is
end entity test_priority;
architecture io of test_priority is
constant n : natural := 3;
signal ain: std_logic_vector(2**n-1 downto 0);
signal yout: std_logic_vector(n-1 downto 0);
signal vout: std_logic;
begin
g1: entity WORK.priority(exm1) generic map(n)
port map(ain, yout, vout);
ain <= "0000 0000", “0000 0110" after 150 ns, “0100 0000" after 300 ns,
“0001 0011” after 400 ns;
end architecture io;
Solution:
library ieee;
use ieee.std_logic_1164.all;
22
biswajitb
entity fulladder is
port(a, b, cin: in std_logic;
sum, cout: out std_logic);
end entity fulladder;
Solution:
The n-bit ripple adder circuit is a combination of n number of full adder circuits,
connected in cascade.
23
biswajitb
In general, the k-th full adder block, where k varies from 0 to n-1, is shown below.
library ieee;
use ieee.std_logic_1164.all;
entity ripple_adder is
generic (n:positive);
port(ar, br: in std_logic_vector(n-1 downto 0);
crin: in std_logic;
sumr: out std_logic_vector(n-1 downto 0);
crout: out std_logic);
end entity ripple_adder;
24
biswajitb
library ieee;
use ieee.std_logic_1164.all;
entity test_ripple_adder is
end entity test_ripple_adder;
architecture io of test_ripple_adder is
constant n : natural := 4;
signal A, B, S: std_logic_vector(n-1 downto 0);
signal Ci, Co: std_logic;
begin
g1: entity WORK.ripple_adder(exm1) generic map(n)
port map(A, B, Ci, S, Co);
Ci <= ‘0’, ‘1’ after 110 ns, ‘0’ after 300 ns;
A <= “0000”, “1101” after 150 ns, “0111” after 200 ns;
B <= “0000”, “1011” after 150 ns, “1101” after 200 ns;
end architecture io;
In this program, absolute time may be replaced by relative time … The corresponding
architecture of testbench program is given herewith.
Ci <= ‘0’;
A <= “0000”;
B <= “0000”;
wait for 110 ns;
Ci <= ‘1’;
wait for 40 ns;
A <= x“d”;
B <= “1011”;
wait for 50 ns;
25
biswajitb
A <= “0111”;
B <= x“d”;
wait for 100 ns;
Ci <= ‘0’;
wait;
end process;
end architecture ioX;
Note:
1) without any inclusion of final wait statement, the process will simply repeat.
3) Execution of Process with sensitivity list, depends on change in signal present in the
list. It does not depend on any specific time. Hence, wait and sensitivity list do not
coexist.
#1. SR Latch:
Solution:
S R Q
0 0 Q = Q =1
0 1 0
1 0 1
1 1 LS
library ieee;
use ieee.std_logic_1164.all;
entity SR_latch1 is
port(S, R: in std_logic;
Q, Qbar: out std_logic);
end entity SR_latch1;
26
biswajitb
begin
prc: process (R,S) is
begin
case std_logic_vector(R,S) is
when “00” =>
Q <= ‘1’;
Qbar <= ‘1’;
when “01” =>
Q <= ‘1’;
Qbar <= ‘0’;
when “10” =>
Q <= ‘0’;
Qbar <= ‘1’;
when others =>
null;
end case;
end process prc;
end architecture exm1;
Note:
Q <= not Qbar is wrong as type of Qbar is out, and hence it cannot be read.
#2. D Latch:
Solution:
EN D Q
0 X LS
1 0 0
1 1 1
library ieee;
use ieee.std_logic_1164.all;
entity D_latch is
port(D, EN: in std_logic;
Q: out std_logic);
end entity D_latch;
27
biswajitb
if (EN=‘1’) then
Q <= D;
end if; -- No ‘else’ means that a latch holds the
end process prc; -- last value under ‘else’ condition.
end architecture exm1;
Solution:
CLK D Q
0 X LS
1 X LS
+ve edge 0 0
+ve edge 1 1
library ieee;
use ieee.std_logic_1164.all;
entity D_FF is
port(D, CLK: in std_logic;
Q: out std_logic);
end entity D_FF;
Solution:
28
biswajitb
library ieee;
use ieee.std_logic_1164.all;
entity D_FF_RS is
port(D, clock, reset, set: in std_logic;
Q: out std_logic);
end entity D_FF_RS;
Solution:
Here, circuit pin-outs are same. However, ‘reset’ signal has higher priority over ‘set’.
29
biswajitb
end if;
end if;
end process prcs;
end architecture exm2;
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity Dff is
port(D, reset, CLK: in std_logic;
Q, Qbar: out std_logic);
end entity Dff;
Note:
30
biswajitb
state <= D;
Q <= state;
Qbar <= not state;
Then all the assignments take effect simultaneously just after process suspends…
This means …
Thus,
Q is not updated with new value of D. It requires one more cycle for such update.
As if, two flipflops are in series – interpretation taken by synthesis tool.
Alternate architecture where assignment will be within process …
Note:
Solution:
31
biswajitb
library ieee;
use ieee.std_logic_1164.all;
entity pipo_reg is
generic (n: natural := 8);
port(D : in std_logic_vector(n-1 downto 0);
clock, reset: in std_logic;
Q : out std_logic_vector(n-1 downto 0));
end entity pipo_reg;
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity sipo_reg is
generic (n: natural := 8);
port(Si : in std_logic;
clock, reset: in std_logic;
Q : out std_logic_vector(n-1 downto 0);
CLK: in std_logic);
end entity sipo_reg;
32
biswajitb
Solution:
library ieee;
use ieee.std_logic_1164.all;
entity counter_ud is
generic (n: natural := 8);
port(clock, IC, upcount, downcount: in std_logic;
count : out std_logic_vector(n-1 downto 0));
end entity counter_ud;
33
biswajitb
Solution:
34
biswajitb
library ieee;
use ieee.std_logic_1164.all;
entity clkgenN is
generic (N: natural := 4);
port(clock: in std_logic;
clock1x, clockNd : out std_logic);
end entity clkgenN;
library ieee;
use ieee.std_logic_1164.all;
entity test_clkgenN is
end entity test_clkgenN;
architecture io of test_clkgenN is
signal clkin: std_logic:= ‘0’;
signal clkout1, clkoutN: std_logic;
begin
gx: entity WORK. clkgenN(exm1)port map
(clkin, clkout1, clkoutN);
clkin <= not clkin after 100 ns;
end architecture io;
35
biswajitb
Solution: It is assumed that number of data available within the ROM is 2M-1.
Expression of LUT:
2π
x(n) = 128 + (255 − 128) sin( ⋅ n), 0 ≤ n ≤ N − 1, N = 64.
N
X n = round [ x(n)]
The array of values are: [128 140 153 165 177 188 199 209 218 226 234
240 245 250 253 254 255 254 253 250 245 240
234 226 218 209 199 188 177 165 153 140 128
116 103 91 79 68 57 47 38 30 22 16
11 6 3 2 1 2 3 6 11 16 22
30 38 47 57 68 79 91 103 116]
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity sine_ROM is
generic (M: natural := 6; N: natural :=8);
port(CSbar, CLK, RDbar: in std_logic;
data: out std_logic_vector(N-1 downto 0));
end entity sine_ROM;
36
biswajitb
begin
prc: process(CLK) is
variable addr : integer range 0 to 2**M-1 := 63;
begin
if rising_edge(CLK) and CSbar = '0' and RDbar = '0' then
if addr = 63 then
addr := 0;
else
addr := addr + 1;
end if;
37