Unit V - VHDL: Very High Speed Harware Description Language (VHDL) VHDL Coding For Arithmetic Circuits
Unit V - VHDL: Very High Speed Harware Description Language (VHDL) VHDL Coding For Arithmetic Circuits
Unit V - VHDL: Very High Speed Harware Description Language (VHDL) VHDL Coding For Arithmetic Circuits
RTL Design – Combinational logic – Sequential circuit – Operators – Behavioral, Dataflow, and structural modeling-
Introduction to Packages – Subprograms – Test bench. (Simulation /Tutorial Examples: adders, counters, flip flops,
• Statement : library. ieee ; use.std_logic_1164. all – indicates that the VHDL program can use all the definitions
of IEEE standard 1164 logic package developed by IEEE to satisfy the requirements of the most of the designers.
• Signal declaration used for providing wire (internal connection) in a circuit. It is similar to port except that no
modes (in and out) need to be specified.
• Pre defined data types such as bit and bit _vector, STD_LOGIC, STD_LOGIC_Signed and STD_LOGIC_Unsigned
can be used with the signal declaration.
• VHDL provides a wait statement – used in a test program to stop an operation for a specified period of time.
Each VHDL description contains two blocks - an Interface and a body
The interface – ENTITY, Body - ARCHITECTURE
The ENTITY description specifies the input and output connections (ports) to the hardware.
The architectural component defines the behavior of the hardware entity being designed.
x y C S
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
VHDL Code for Full adder - Behavioral Modelling
x X1
y S X1 < = x xor y
x z
S < = X1 xor z Output signals
x A1
y y A1 < = x and y
A2 A2 < = x and z
z x Intermediate or Local signals
z C A3 < = y and z
𝑺= 𝒙⊕𝒚⊕𝒛
𝑪 = 𝒚𝒛 + 𝒙𝒛 + 𝒙𝒚
VHDL Code for Full adder - Behavioral Modelling
x y z C S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
VHDL Code for Full Subtractors – Behavioral Modelling
entity FSBM is
-- Stimulus process
Port ( x, y, Bin : in bit ; D , Bout : out bit ) ;
stim_proc1: process
end FSBM; begin
x <= not x;
architecture Behavioral of FSBM is
wait for 300 ns;
Signal X1, N1L, N2L, A1,A2: bit end process;
𝒙 X1L
begin
stim_proc2: process
𝒚 D X1 < = x xor y ; begin
y <= not y;
𝑩𝒊𝒏 N2L
A2L
D < = X1 xor Bin ;
wait for 150 ns;
N1L
N1L <= not x; end process;
B N2L <= not X1L;
A1L stim_proc3: process
A1 < = N1L and x; begin
Bin <= not Bin;
A2 < = N2L and Bin ;
wait for 50 ns;
Bout < = A1L or A2 L; end process;
END;
end Behavioral;
VHDL Code for Full Subtractors – Behavioral Modeling
VHDL Code for 4 bit adder– Behavioral Modeling
stim_proc: process
entity fourbitadder is begin
Port ( Cin : in STD_LOGIC;
X : in STD_LOGIC_VECTOR (3 downto 0); wait for 100 ns;
Y : in STD_LOGIC_VECTOR (3 downto 0); X <= "0110";
Cout : out STD_LOGIC; Y <= "1100";
S : out STD_LOGIC_VECTOR (3 downto 0));
end fourbitadder; wait for 100 ns;
X <= "1111";
architecture Behavioral of fourbitadder is Y <= "1100";
end Structural;
entity FA is
Port ( x, y, z : in bit; S, C : out bit);
end FA;
architecture Structural of FA is
entity XorGate is Structural Modelling
port(X,Y: in bit; Z: out bit);
component AndGate end XorGate;
port(X,Y: in bit; Z: out bit); architecture Behavioral of XorGate is stim_proc: process
end component; begin begin
Z <= X xor Y; In1 <= not In1;
component OrGate end Behavioral; wait for 100 ns;
port(X,Y,W: in bit; Z: out bit); end process;
end component; entity AndGate is
port(X,Y: in bit; Z: out bit); stim_proc1: process
component XorGate end AndGate; begin
port(X,Y: in bit; Z: out bit); architecture Behavioral of AndGate is In2 <= not In2;
end component; begin wait for 50 ns;
Z <= X and Y; end process;
signal X1, X2 ,A1, A2, A3: bit; end Behavioral;
stim_proc2: process
Begin entity OrGate is begin
port(X,Y,W: in bit; Z: out bit); Cin <= not Cin;
X1:XorGate port map(x, y, X1); end OrGate; wait for 150 ns;
X2:XorGate port map(X1, z, S); architecture Behavioral of OrGate is end process;
A1:AndGate port map(x, y, A1); begin
A2:AndGate port map(x, z, A2); Z <= (X or Y)or W;
A3:AndGate port map(y, z, A3); end Behavioral;
O1:OrGate port map(A1, A2, A3, C);
end Structural;
entity andgate is
port( X, Y : in bit ; Z : out bit);
VHDL Code for Full Subtractor – end andgate;
architecture Behavioral of andgate is
begin
entity notgate is
port( X: in bit; Z: out bit);
end notgate;
architecture Behavioral of notgate is
𝒙 X1L
begin
• XOR gate Z <= not X;
𝒚 D end Behavioral;
• AND gate
𝑩𝒊𝒏 N2L
A2L entity xorgate is
N1L • OR gate port( X,Y: in bit; Z: out bit);
end xorgate;
• NOT gate architecture Behavioral of xorgate is
B
begin
A1L
Z <= X xor Y;
end Behavioral;
entity orgate is
port(X,Y: in bit; Z: out bit);
end orgate;
architecture Behavioral of orgate is
begin
Z <= X or Y;
end Behavioral;
entity Fullsub is
Port ( Bin : in bit;
x : in bit;
VHDL Code for Full Subtractor – y : in bit;
D : out bit;
Bout : out bit);
Structural Modeling end Fullsub;
architecture Structural of Fullsub is
component andgate
port(X, Y: in bit; Z: out bit);
end component;
component xorgate
𝒙 X1L port(X, Y: in bit; Z: out bit);
end component;
component notgate
𝒚 D port(X: in bit; Z: out bit);
end component;
𝑩𝒊𝒏 N2L
A2L component orgate
N1L
port(X, Y: in bit; Z: out bit);
end component;
B signal X1L,N1L,N2L,A1L,A2L,A3L: bit;
A1L Begin
end Structural;
entity Fullsub is
entity andgate is
Port ( Bin : in bit;
x : in bit;
port( X, Y : in bit ; Z : out bit); Structural Modelling
end andgate;
y : in bit;
architecture Behavioral of andgate is
D : out bit;
begin
Bout : out bit);
Z <= X and Y;
end Fullsub;
end Behavioral; -- Stimulus process
architecture Structural of Fullsub is
component andgate stim_proc1: process
entity notgate is
port(X, Y: in bit; Z: out bit);
port( X: in bit; Z: out bit);
begin
end component; A <= not A;
end notgate;
component xorgate
port(X, Y: in bit; Z: out bit);
architecture Behavioral of notgate is wait for 300 ns;
begin end process;
end component;
Z <= not X;
component notgate
end Behavioral;
port(X: in bit; Z: out bit);
stim_proc2: process
end component;
component orgate
entity xorgate is begin
port( X,Y: in bit; Z: out bit);
port(X, Y: in bit; Z: out bit);
end xorgate;
B <= not B;
end component; wait for 150 ns;
architecture Behavioral of xorgate is
signal X1L,N1L,N2L,A1L,A2L,A3L: bit;
Begin
begin end process;
Z <= X xor Y;
end Behavioral;
X1: xorgate port map(x,y,X1L); stim_proc3: process
X2: xorgate port map (X1L,Bin,D);
entity orgate is begin
N1: notgate port map(x,N1L);
A1: andgate port map(y, N1L, A1L);
port(X,Y: in bit; Z: out bit); Bin <= not Bin;
end orgate;
N2: notgate port map(X1L, N2L);
architecture Behavioral of orgate is
wait for 50 ns;
A2: andgate port map(Bin, N2L,A2L); end process;
begin
O1: orgate port map(A2L, A1L, Bout);
Z <= X or Y; END;
end Behavioral;
end Structural;
VHDL Code for Full Subtractors – Structural Modeling
VHDL Code for 4 bit adder– Structural Modeling
entity Fulladder is
port ( x, y, z : in bit; s,c: out bit);
end Fulladder;
begin
X1 <= x xor y ;
s <= X1 xor z ;
𝑺 = 𝒙 ⊕ 𝒚 ⊕ 𝑪𝒊𝒏 A1 <= x and y;
A2 <= x and z ;
A3 <= y and z ;
𝑪 = 𝒚𝑪𝒊𝒏 + 𝒙𝑪𝒊𝒏 + 𝒙𝒚 c <= (A1 or A2) or A3 ;
end Behavioral;
entity RippleadderS is
VHDL Code for Ripple/parallel – Port ( Cin : in bit;
A : in bit_VECTOR (3 downto 0);
Structural Modeling B : in bit_VECTOR (3 downto 0);
Cout : out bit;
S : out bit_VECTOR (3 downto 0));
end RippleadderS;
component Fulladder
port ( x, y, z : in bit; s,c: out bit);
end component;
begin
Design of a 1:2 & 1:4 De Multiplexer using Dataflow, Behavioural and structural modeling
A
B f f f f
2n : n : 1 f
C S1 S0 S1 S0 S1 S0 S1 S0
MUX
D
E
S0 S1
S1 S 0
E S1 S0 A B C D F F A1
A
0 0 0 X X X X 0 0
A2
1 0 0 0 X X X 0 A B
1 0 0 1 X X X 1 A f
1 0 1 X 0 X X 0 B C
1 0 1 X 1 X X 1 B A3
1 1 0 X X 0 X 0 C
D
1 1 0 X X 1 X 1 C A4
1 1 1 X X X 0 0 D
𝒇 = 𝑬(𝑺𝟏 𝑺𝟎 𝑨 + 𝑺𝟏 𝑺𝟎 𝑩 + 𝑺𝟏 𝑺𝟎 𝑪 + 𝑺𝟏 𝑺𝟎 𝑫)
1 1 1 X X X 1 1 D
stim: process
A
entity MUX4_1 is Begin
B
Port ( i : in STD_LOGIC_VECTOR (3 downto 0); 2n : n : 1 i <= "1010";
C f
s : in STD_LOGIC_VECTOR (1 downto 0); MUX
D s <= "00";
y : out STD_LOGIC);
E wait for 20 ns;
end MUX4_1;
s <= "01";
architecture dataflow of MUX4_1 is
wait for 20 ns;
begin S1 S0
with s select s <= "10";
y <= i(0) when "00", wait for 20 ns;
i(1) when "01", s <= "11";
i(2) when "10", wait for 20 ns;
i(3) when others;
wait;
end dataflow;
end process;
end tb;
Dataflow modeling of 4:1 mux
stim_proc: process
Begin
entity MuxB is
A <= '1';
Port ( A,B,C,D,sel0,sel1 : in bit; y : out bit);
sel0 <= '0';
end MuxB;
sel1 <= '0';
wait for 20 ns;
architecture Dataflow of MuxB is
B <= '0';
sel0 <= '0';
signal selbar0, selbar1, A1, A2, A3, A4: bit;
sel1 <= '1';
wait for 20 ns;
begin
C <= '1';
sel0 <= '1';
selbar0 <= not sel0;
sel1 <= '0';
selbar1 <= not sel1;
wait for 20 ns;
A1 <= (A and selbar0) and selbar1;
D <= '1';
A2 <= (B and sel0) and selbar1;
sel0 <= '1';
A3 <= (C and selbar0) and sel1;
sel1 <= '1';
A4 <= (D and sel0) and sel1;
wait for 20 ns;
Y <= (A1 or A2) or (A3 or A4);
wait;
end process;
end Dataflow;
entity MUX4_1 is
Port ( i : in STD_LOGIC_VECTOR (3 downto 0);
s : in STD_LOGIC_VECTOR (1 downto 0); stim: process
y : out STD_LOGIC);
Begin
end MUX4_1;
i <= "1010";
s <= "00";
architecture Behavioral of MUX4_1 is
begin wait for 100 ns;
library ieee;
use ieee.std_logic_1164.all; stim: process
entity MuxBcase is Begin
Port ( i : in STD_LOGIC_VECTOR (3 downto 0);
sel : in STD_LOGIC_VECTOR (1 downto 0); i <= "1010";
y : out STD_LOGIC);
end MuxBcase; s <= "00";
wait for 100 ns;
architecture Behavioral of MuxBcase is
s <= "01";
begin
mux : process (i,sel) is wait for 100 ns;
begin
s <= "10";
case sel is
when "00" => y <= i(3); wait for 100 ns;
when "01" => y <= i(2);
when "10" => y <= i(1); s <= "11";
when others => y <= i(0);
end case;
wait for 100 ns;
end process mux; wait;
end Behavioral; end process;
end tb;
signal selbar0,selbar1,A1,A2,A3,A4: std_logic;
begin
INV0: inv port map (Sel0, selbar0);
INV1: inv port map (Sel1, selbar1);
Structural modeling of 4:1 mux A1: and3 port map (A, selbar0, selbar1, A1);
A2: and3 port map (B, Sel0, selbar1, A2);
library ieee; A3: and3 port map (C, selbar0, Sel1, A2);
use ieee.std_logic_1164.all; A4: and3 port map (D, Sel0, Sel1, A4);
O1: or4 port map (A1, A2, A3, A4, Y);
entity MUX4_1 is end structural;
port ( Sel0,Sel1 : in std_logic;
A, B, C, D : in std_logic;
Y : out std_logic );
end MUX4_1;
component inv
port (pin : in std_logic; pout :out std_logic);
end component;
component and3
port (a0,a1,a2: in std_logic; aout:out std_logic);
end component;
component or4
port (r0,r1,r2,r3:in std_logic; rout:out std_logic);
end component;
entity and3 is stim_proc: process
port( X, Y,W: in bit ; Z : out bit); Begin
end and3; A <= '1';
sel0 <= '0';
architecture Behavioral of and3 is
sel1 <= '0';
begin wait for 20 ns;
Z <= (X and Y) and W ; B <= '0';
end Behavioral; sel0 <= '0';
sel1 <= '1';
entity inv is wait for 20 ns;
port( X: in bit; Z: out bit); C <= '1';
end inv; sel0 <= '1';
sel1 <= '0';
architecture Behavioral of inv is
wait for 20 ns;
begin D <= '1';
Z <= not X; sel0 <= '1';
end Behavioral; sel1 <= '1';
wait for 20 ns;
entity or4 is wait;
port(X,Y,W,U: in bit; Z: out bit); end process;
end or4;
architecture Behavioral of or4 is
begin
Z <= (X or Y) or (W or U);
end Behavioral;
library IEEE; stim: process
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL; Begin
use IEEE.STD_LOGIC_UNSIGNED.ALL; I <= '0';
S <= '0';
entity DEMUX_SOURCE is wait for 100 ns; Routing device
Port ( I,S : in STD_LOGIC; y1, y2 : out STD_LOGIC); S <= '1';
end DEMUX_SOURCE; wait for 100 ns; I/E O1
I <= '1'; S <= '0';
DeMUX
O2
architecture dataflow of DEMUX_SOURCE is wait for 100 ns;
S <= '1'; S
begin wait for 100 ns;
O1 <= I and (not S); wait;
O2 <= I and S; end process;
entity demux1_4 is
port (
out0, out1, out2, out3 : out std_logic;
Stimulus:Process
sel : in std_logic_vector(1 downto 0);
i : in std_logic); BEGIN
end demux1_4;
i <= '1';
architecture Behavioral of demux1_4 is sel <="00";
wait for 2 ns;
begin
process(i,sel) sel <="01";
begin
wait for 2 ns;
case sel is
when "00" => out0 <= i; out1 <= '0'; out2 <= '0'; out3 <='0'; sel <="10";
when "01" => out1 <= i; out0 <= '0'; out2 <= '0'; out3 <='0';
wait for 2 ns;
when "10" => out2 <= i; out0 <= '0'; out1 <= '0'; out3 <='0';
when others => out3 <= i; out0 <= '0'; out1 <= '0'; out2 <='0'; sel <="11";
end case;
wait for 2 ns;
end process;
END PROCESS tb;
end Behavioral;
Dataflow modelling uses only Concurrent signal assignment statements
entity ckt is
port (A: in BIT:=1; B: in BIT; Y,Z: out BIT);
end ckt;
architecture ckt of ckt is
Three concurrent signal assignment statements.
begin
B <= A and A; VHDL compiler executes those three statements
An important detail to remember in dataflow modeling is that due to the concurrent nature of execution, the order of
the statements does not matter.
Conditional signal assignment statements
• This conditional statement will assign the value to its target signal only when a condition is true.
• You can give multiple values with multiple conditions. They are similar to if-else statements.
Targeted_signal <= Value when condition else VHDL compiler assigns a value (or values) to ‘Targeted_signal’
Value_2 when condition_2 else when its corresponding condition is true. Conditions can be a
.....
Boolean expression, and value (or values) can be any waveform
Value_last;
element.
Output <= input(0) after 20 ns when S(0) = ‘0’ and S(1) = ‘0’ else
input(1) after 20 ns when S(0) = ‘0’ and S(1) = ‘1’ else When more than one conditions are true, then compiler
input(2) after 20 ns when S(0) = ‘1’ and S(1) = ‘0’ else
gives precedence to the first occurring true condition.
input(3) after 20 ns;
Whenever there is an event on signals input(0), input(1), input(2), input(3), S(0) or S(1). The VHDL compiler will
execute the above statements. So, these signals act as a sensitivity list (list of signals) for this conditional statement.
Selected signal assignment statements
This signal assignment statement will select and assign a value to its target signal based on the value of the select
expression. They are similar to case statements in the C-programming language.
You can also give multiple values with multiple choices.
of statements means that the compiler will execute them in the same variable-assignment statement
A process with a sensitivity list will be executed when the value of any signal from the list will change.
Process data-object
These are data-objects like signals, variables, constants, generics, etc. Data objects defined inside a process only
belong to that particular process; no other process can access those data-objects. You can think of them as the local
variables in C
Sequential statements
The VHDL compiler allows sequential statements in the process block only. There are various sequential
statements, some of them are listed below:
1. variable-assignment-statement
2. signal-assignment-statement
3. case statement
4. loop statement
5. if-else statement
6. assertion-statement
7. wait-statement
After the execution of all the statements inside the process block, the compiler suspends the process.
Variable assignment statement
Variables are declared inside a process before the begin keyword. The variable declared inside the process only
belongs to it.
Signal assignment statement
Signals can be defined inside a process or outside a process. When they appear inside a process, they behave as
sequential signal assignment statements. And when they appear outside a process, then they behave as concurrent
signal assignment statements.
case statement
The case statement is a type of conditional statement. We provide an expression as a case, and when the compiler
executes this block, it evaluates the expression first. Then it goes through all the choices, and when a choice matches
the expression’s value, it executes the corresponding statements.
Next statement next statement to skip the current iteration of the loop
The execution of the next statement causes skipping of the remaining statement of the specified loop in the current
iteration. If we don’t provide any label, then the only innermost loop is considered. So as a comparison,
the exit statement terminates the loop while the next statement terminates the current iteration only.
Assertion statements
1. Component declaration
2. Package Declaration
3. Component instantiation
4. Association techniques
a. Positional association
b. Named association
c. Rules for association while instantiating a
component
5. Internal signal declaration
6. Mixed Modeling Style
Component declaration
In structural modeling, the first thing to do is to instantiate components using component declaration.
In component declaration, define the name, Inputs/outputs, and type of Inputs/outputs of the component.
component AND1
port(A,B : in BIT; Y : out BIT);
end component; Declare components in the declaration part of the architecture body
component halfadder
port(A,B : in BIT; sum, carry : out BIT);
end component;
Package Declaration
The use of packages actually makes reusable code as designers can share the packages with other designers. Or one
can use a package in multiple designs without writing it again and again.
The use of packages and libraries does not reduce the size of actual code. While the compilation occurs, the compiler
fetches the respective code block automatically. The actual advantage is in the aesthetics of the code, especially in
large design units.
Component instantiation
Component instantiation is nothing but just the syntax for using the component in the circuit.
component label: component-name port map ( association-list) ;
1. We can use any legal identifier as a label for the component, and it has to be unique in the architecture.
2. The component label is also known as the name of the instance.
3. The name of the component will remain the same as in its declaration.
4. The association list contains the signals and ports of the entity.
5. We use signals for the interconnection of components inside the circuit, and ports serve as a terminal point or input
/outputs.
1. Positional association.
2. Named association.
Positional association
In positional association, we map the signal or port to the port at the respective positions in the component
declaration.
component halfadder
port(A,B : in BIT; sum, carry : out BIT);
end component;
H1: halfadder port map(S1, S2, P(1), P(2));
H1 is the label for the current instantiation of component halfadder. And it will map S1 to A, S2 to B, P(1) to sum and,
P(2) to carry. As association occurs based on the position of the actual in the list
Named association
In named association, every local is attached to an actual explicitly using the syntax
local => actual;
Here position and sequence do not matter as we defined each of them explicitly.
component halfadder
port(A,B : in BIT; sum, carry : out BIT);
end component;
H1: halfadder port map ( S1 => A, S2 => B, P(1) => sum, P(2) => carry);
Rules for association while instantiating a component - applies to both named and positional associations
1. The type of local and the actual must be the same for a valid association.
2. If the local is in readable mode, so must the actual.
3. Signals that are defined locally, we can read/ write to them so those can be associated with local of any mode.
4. If an actual is of mode in, it can’t be associated with a local of mode out/ inout.
5. If an actual is of mode out, it can’t be associated with a local of mode in/ inout.
6. But if an actual is of mode inout, it can be associated with a local of mode in, out or input.
Internal / Intermediate signal declaration
in structural modeling, we use signals interconnect components. We initialize these signals inside the architecture
body, so we call them the internal signals.
architecture STRUCTURAL of DECADE_COUNTER is
component AND_GATE
port (A, B: in BIT; C: out BIT);
end component;
– subtraction sla shift left arithmetic < test for less than
* multiplication sra shift right arithmetic <= test for less than or equal
** exponentiation ror rotate right >= test for greater than or equal
abs absolute value shift_left() New and more and logical and
& concatenation