Professional Documents
Culture Documents
HDL Manual New
HDL Manual New
Course Instructors:
CONTENTS:
Introduction:
Programs:
Interfacing experiments:
VHDL VHDL
HDL XILINX XILINX
PROJECT PROJECTTOOL
NAVIGATOR NAVIGATOR TOOL TUTORIAL
TUTORIAL
XILINX PROJECT NAVIGATOR TOOL TUTORIAL
BEHAVIOURAL SIMULATION
ECE Dept. BMS College of Engineering 8
Dept of Electronics IV-Sem, HD-Lab
Select Behavioural Simulation from the scroll down list.
Give wave pattern on the input signals as required. Then double click on Simulate
Behavioural Model for simulating.
The wave form according to the inputs given will appear in the window. The wave form
You will be finding the signals and ports of your module and right click on each input signal
to force a particular value into it .
Referring to the FPGA manual ,assign the pin number to the ports you had defined in
your design.
6. Select a *.bit file (For FPGA) *.jed file (For CPLD) and open it
7. Select OK tab
Select the pins on the board that are assigned in the previous steps to drive the inputs and
observe the out on the LED or what ever interfacing output monitor peripherals you had
connected to the board.
THEORY: The gate is a digital circuit with one or more input voltages but only one output
voltage. By connecting the different gates in different ways, we can build circuits that
perform arithmetic and other functions. Logic gates are the basic elements that make up a
digital system. The Electronic gate is a circuit that is able to operate on a number of binary
inputs in order to perform a particular logic function. The types of gates available are the
NOT, AND, OR, NAND, NOR, EXCLUSIVE-OR, and EXCLUSIVE-NOR.
a a a
a a h=a^b
f=~(a&b) g=~(a!b)
a
d=a&b e=a!b 7400 b
7402
b 7486
c=~a 7408 7432
7404 b b b
1 1 0 1 1 0 0 0
library ieee;
use ieee.std_logic_1164.all;
entity basic_gates is
port (a,b : in bit;
c,d,e,f,g,h : out bit );
end basic_gates;
NET "a" LOC = "<input pin numbers>" ;(to be referred from datasheet)
NET "b" LOC = "<input pin numbers>" ;
NET "c" LOC = "<output pin numbers>" ;
NET "d" LOC = "<output pin numbers>";
NET "e" LOC = "<output pin numbers>" ;
NET "f" LOC = "<output pin numbers>";
NET "g" LOC = "<output pin numbers>";
NET "h" LOC = "<output pin numbers>";
Experiment No. 2
2a) 2 to 4 DECODER
A 2-to-4-line decoder, the two inputs are decoded into four outputs, each output
representing one of the minterms of the 2-input variables. A 2-to-4 decoder can be used for
decoding any 2 –bit code to provide 4-outputs, one for each element of the code.
Block Diagram:
enable
y(0)
a(0)
y(1)
2:4 decoder
a(1) y(2)
y(3)
Selector Output
0 X X Z Z Z Z
1 0 0 0 0 0 1
1 0 1 0 0 1 0
1 1 0 0 1 0 0
1 1 1 1 0 0 0
library ieee;
use ieee.std_logic_1164.all;
entity Decoder2to4 is
Port ( enable : in bit;
a : in std_logic_vector(1 downto 0);
y : out std_logic_vector(3 downto 0));
end Decoder2to4;
module decoder2to4(a,enable,y);
input enable;
input [1:0] a ;
output [3:0] y;
reg [3:0] y;
always @(a,enable)
begin
if (enable==1'b1)
begin
case(a)
2'b00:y = 4'b0001;
2'b01:y = 4'b0010;
2'b10:y = 4'b0100;
2'b11:y = 4'b1000;
endcase
end
else
y=4'bZZZZ;
end
endmodule
ECE Dept. BMS College of Engineering 21
Dept of Electronics IV-Sem, HD-Lab
2b) 8 to 3 ENCODER
THEORY:
An encoder is a digital function that produces a reverse operation from that of
decoder. An encoder has 2n (or less) input lines and n output lines. The output lines generate
the binary code for 2n input variables. The encoder assumes that only one input line can be
equal to 1 at any time. Otherwise the circuit has no meaning. If the encoder has 8 inputs and
could have 28 = 256 possible input combinations.
Priority Encoder:
These encoders establish an input priority to ensure that only the highest priority line
is encoded. For example 8-to-3-line priority encoder the inputs are I(0) ,I(1) ,…….I(7).If the
priority is given to an input with higher subscript number over one with a lower subscript
number , then if both I(2) and I(5) are logic-1 simultaneously , the output will be 101
because I(5) has higher priority over I(2).
Truth Table
Inputs Output
En a0 a1 a2 a3 a4 a5 a6 a7 b0 b1 b2
0 x x x x x x x x z z z
1 0 0 0 0 0 0 0 1 1 1 1
1 0 0 0 0 0 0 1 0 1 1 0
1 0 0 0 0 0 1 0 0 1 0 1
1 0 0 0 0 1 0 0 0 1 0 0
1 0 0 0 1 0 0 0 0 0 1 1
1 0 0 1 0 0 0 0 0 0 1 0
1 0 1 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 0
library ieee;
use ieee.std_logic_1164.all;
entity Encoder_withoutpriority is
Port ( en : in bit;
a : in std_logic_vector(0 to 7);
b : out std_logic_vector(2 downto 0));
end Encoder_withoutpriority;
module enc_wop(en,a,b);
input en;
input[0:7]a;
output[2:0]b;
reg[2:0]b;
always @ (en,a)
begin
if (en==1'b1)
begin
case (a)
8'b 00000001 :b=3'd7;
8'b 00000010 :b=3'd6;
8'b 00000100 :b=3'd5;
8'b 00001000 :b=3'd4;
8'b 00010000 :b=3'd3;
8'b 00100000 :b=3'd2;
8'b 01000000 :b=3'd1;
8'b 10000000 :b=3'd0;
default:b=3'bz;
endcase
end
else if (en==1'b0)
b=3'bz;
end
endmodule
Inputs Output
en a0 a1 a2 a3 a4 a5 a6 a7 b0 b1 b2
0 x x x x x x x x z z z
1 X X X X X X X 1 1 1 1
1 X X X X X X 1 0 1 1
1 X X X X X 1 0 0 1 0 1
1 X X X X 1 0 0 0 1 0 0
1 X X X 1 0 0 0 0 0 1 1
1 X X 1 0 0 0 0 0 0 1 0
1 X 1 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 0
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity encoder is
Port ( e : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (7 downto 0);
y : out STD_LOGIC_VECTOR (2 downto 0));
end encoder;
begin
process(e,a)
begin
if (e='1') then
if (a(0)='1') then
y<="000";
elsif (a(1)='1') then
y<="001";
elsif (a(2)='1') then
y<="010";
ECE Dept. BMS College of Engineering 24
Dept of Electronics IV-Sem, HD-Lab
elsif (a(3)='1') then
y<="011";
elsif (a(4)='1') then
y<="100";
elsif (a(5)='1') then
y<="101";
elsif (a(6)='1') then
y<="110";
elsif (a(7)='1') then
y<="111";
else
y<="ZZZ";
end if;
end if;
end process;
end Behavioral;
module Encoder8to3ver(en,a,b);
input en;
input [0:7] a;
output [2:0] b;
reg [2:0] b;
always @(en,a)
begin
if(en==1)
begin
casex(a)
8'bXXXXXXX1:b=3'd7;
8'bXXXXXX10:b=3'd6;
8'bXXXXX100:b=3'd5;
8'bXXXX1000:b=3'd4;
8'bXXX10000:b=3'd3;
8'bXX100000:b=3'd2;
8'bX1000000:b=3'd1;
8'b10000000:b=3'd0;
endcase
end
else
b=3'dZ;
end
endmodule
2 (c) 8 :1 MULTIPLEXER.
entity mux8to1 is
begin
process (i,s)
begin
case s is
when "000"=>y<=i(0);
when "001"=>y<=i(1);
when "010"=>y<=i(2);
when "011"=>y<=i(3);
when "100"=>y<=i(4);
when "101"=>y<=i(5);
when "110"=>y<=i(6);
when others => y<=i(7);
end case;
end process;
end Behavioral;
ECE Dept. BMS College of Engineering 26
Dept of Electronics IV-Sem, HD-Lab
endmodule
THEORY: A Gray code represents each number in the sequence of integers as a binary
string of length N in an order such that adjacent integers have Gray code representations that
differ in only one bit position. The advantage of the gray code is that only one bit will change
as it proceeds from one number to the next. To obtain gray code, one can start with any bit
combination by changing only one bit from 0 to 1 or 1 to 0 in any desired random fashion, as
long as two numbers do not have identical code assignments.
BCD-I/P Gray-Code-O/P
b(3) b(2) b(1) b(0) g(3) g(2) g(1) g(0)
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1
b(0) b(1) b(2) b(3)
g(3) 0 0 1 1 0 0 1 0
0 1 0 0 0 1 1 0
g(2) 0 1 0 1 0 1 1 1
7486
0 1 1 0 0 1 0 1
0 1 1 1 0 1 0 0
g(1)
7486 1 0 0 0 1 1 0 0
1 0 0 1 1 1 0 1
1 0 1 0 1 1 1 1
g(0)
7486 1 0 1 1 1 1 1 0
1 1 0 0 1 0 1 0
1 1 0 1 1 0 1 1
1 1 1 0 1 0 0 1
1 1 1 1 1 0 0 0
library ieee;
use ieee.std_logic_1164.all;
entity Bin_gray is
Port ( b : in std_logic_vector(3 downto 0);
g : out std_logic_vector(3 downto 0));
end Bin_gray;
module bintogrey(b,g);
input [3:0] b;
output [3:0] g;
wire [3:0] g;
Multiplexer (MUX) is a digital switch which connects data from one of n sources to
the output. A number of select inputs determine which data source is connected to the output.
A 2 to 1 line multiplexer is shown in figure below, A & B are two inputs. En is enable .
Enable is used to enable or disable the mulitiplexer. Selection line S are decoded to select a
particular input. Y is a output which selects any one of the input depending on the select
lines. The truth table for the 2:1 mux is given in the table below.
A Input Output
2:1-
SEL En Y
MUX
Y
X 1 0
B 0 0 A
1 0 B
SEL
library ieee;
use ieee.std_logic_1164.all;
entity MUX2_1 is
port (A, B, SEL, En : in std_logic;
Y : out std_logic);
end MUX2_1;
THEORY: A demultiplexer is a circuit that receives the information on a single line and
transmits this information on one of 2n possible output lines. The selection of specific output
lines is controlled by the values of n selection lines. For 1: 8 demultiplexers, the single input
ECE Dept. BMS College of Engineering 29
Dept of Electronics IV-Sem, HD-Lab
variable has a path to all the eight outputs, but the input information is directed to only one of
the 8 output lines.
Truth Table
Selector Output
e
I S2 S1 S0 y7 y6 y5 y4 y3 y2 y1 y0
n
0 1/0 X X X Z Z Z Z Z Z Z Z
1 1/0 0 0 0 Z Z Z Z Z Z Z 1/0
1 1/0 0 0 1 Z Z Z Z Z Z 1/0 Z
1 1/0 0 1 0 Z Z Z Z Z 1/0 Z Z
1 1/0 0 1 1 Z Z Z Z 1/0 Z Z Z
1 1/0 1 0 0 Z Z Z 1/0 Z Z Z Z
1 1/0 1 0 1 Z Z 1/0 Z Z Z Z Z
1 1/0 1 1 0 Z 1/0 Z Z Z Z Z Z
1 1/0 1 1 1 1/0 Z Z Z Z Z Z Z
entity Demux1to8 is
Port ( i : in STD_LOGIC;
s : in STD_LOGIC_VECTOR (2 downto 0);
y : out STD_LOGIC_VECTOR (7 downto 0));
end mux8to1;
begin
process (i,s)
begin
case s is
when "000"=>y(0)<=i;
when "001"=>y(1)<=i;
when "010"=>y(2)<=i;
when "011"=>y(3)<=i;
when "100"=>y(4)<=i;
when "101"=>y(5)<=i;
when "110"=>y(6)<=i;
when others => y(7)<=i;
end case;
end process;
end Behavioral;
endmodule
2(e) 4-BIT COMPARATOR.
Comparator Table
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity comparator is
Port ( a,b : in std_logic_vector(3 downto 0);
agb, alb, aeb : out std_logic);
end comparator;
architecture Behavioral of comparator is
begin
process(a,b)
begin
if( a=b) then
aeb<= '1';
alb<= '0';
agb<= '0';
elsif ( a>b) then
agb<= '1';
aeb<= '0';
alb<= '0';
elsif ( a<b) then
alb<= '1';
agb<= '0';
aeb<= '0';
end if;
end process;
end Behavioral;
Verilog code to implement 4-BIT COMPARATOR.
module comparator(a,b,x,y,z);
input [3:0] a;
input [3:0] b;
output x;
output y;
output z;
reg x,y,z;
always @(a , b)
begin
x = 1'b0;
y = 1'b0;
z = 1'b0;
if(a < b)
x = 1'b1; //when a is less than b then x is high
else if(a == b)
y = 1'b1; //when a is equal to b then y is high
else if(a > b)
z = 1'b1; //when a is greater than b then z is high
end
endmodule
AIM: Write a VHDL/ Verilog code to describe the functions of a FULL ADDER using
Following modeling styles.
i) Dataflow description
ii) Behavioral description
iii) Structural description
THEORY: A combinational circuit that performs the addition of two bits is called a half
adder. One that performs the addition of three bits (two significant bits and a previous carry)
is a Full adder.
The most basic arithmetic operation is the addition of two binary digits. This simple
addition consists of four possible elementary operations namely 0 + 0 = 0, 0 + 1 = 1, 1 + 0 =
1, 1 + 1 = 10.The first three operations produce a sum whose length is one digit, but when
both augend and addend bits are equal to 1,the binary sum consists of two digits. The higher
significant bit is called carry.
q
7408
7408
s2 carry r
carry
7432
Cin
q
s3
Truth Table:
Inputs output
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_dataflow is
ECE Dept. BMS College of Engineering 33
Dept of Electronics IV-Sem, HD-Lab
port (a,b,cin : in bit;
sum, carry : out bit );
end full_adder_dataflow;
begin
sum<= a xor b xor cin;
carry<= ( a and b) or (b and cin) or (cin and a);
end Behavioral;
Verilog code to implement Full Adder in Dataflow method.
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_behav is
Port ( a,b,cin : in std_logic;
sum,carry : out std_logic);
end full_adder_behav;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity full_adder is
Port ( a, b, cin : in std_logic;
sum, cout : out std_logic);
end full_adder;
signal s1,s2,s3:std_logic;
begin
u0:xor_3 port map(a,b,cin,sum);
u1:and_2 port map(a,b,s1);
u2:and_2 port map(b,cin,s2);
u3:and_2 port map(cin,a,s3);
u4:or_3 port map(s1,s2,s3,cout);
end structural;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity and_2 is
Port ( x,y : in std_logic;
z : out std_logic);
end and_2;
architecture Behavioral of and_2 is
begin
z<=x and y;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity or_3 is
Port ( x,y,z : in std_logic;
Cout : out std_logic);
end or_3;
architecture Behavioral of or_3 is
begin
cout<=x or y or z;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity xor_3 is
Port ( a,b,cin : in std_logic;
sum : out std_logic);
end xor_3;
VHDL code to implement Full Adder Using two half adders in Structural method.
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_struct is
port ( a,b,cin : in bit;
sum, carry : out bit);
end full_adder_struct;
----------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity half_adder is
Port ( a : in std_logic;
b : in std_logic;
sum : out std_logic;
carry : out std_logic);
end half_adder;
begin
sum<= a xor b;
carry<= a and b;
end Behavioral;
----------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity or_gate is
Port ( a : in std_logic;
b : in std_logic;
e : out std_logic);
end or_gate;
begin
e<= a or b;
end Behavioral;
Experiment No. 4
VHDL code to implement 32 - BIT ALU.
AIM: Write a model for 32bit ALU using schematic diagram shown below
THEORY: The arithmetic logic unit (ALU) is a digital circuit that calculates an arithmetic
operation (like an addition, subtraction, etc.) and logic operations (like XOR, AND, NOT
etc.,) between two numbers. The ALU is a fundamental building block of the central
processing unit of a computer.
Many types of electronic circuits need to perform some type of arithmetic operation,
so even the circuit inside a digital watch will have a tiny ALU that keeps adding 1 to the
current time, and keeps checking if it should beep the timer, etc...
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity ALU is
Port ( a,b : in std_logic_vector(31 downto 0);
opcode : in std_logic_vector(3 downto 0);
enable : in bit;
prod : out std_logic_vector(63 downto 0);
res1 : out std_logic_vector(31 downto 0));
end ALU;
always @ (a,b,enable,opcode)
begin
if(enable==1)
begin
ECE Dept. BMS College of Engineering 39
Dept of Electronics IV-Sem, HD-Lab
case(opcode)
4'b0000 : y=a+b;
4'b0001 : y=a-b;
4'b0010 : prod=a[31:0]*b[31:0];
4'b0011 : y=~a;
4'b0100 : y=a&b;
4'b0101 : y=a|b;
4'b0110 :y=~(a&b);
4'b0111 :y=~(a|b);
4'b1000 :y=(a^b);
default :y=32'bx;
endcase
end
else
y=32'bz;
end
endmodule
Experiment No. 5
s s1
3 clk S R Q Qbar
1 q 1 0 0 Hold
clk
1 1 0 1 0
1 0 1 0 1
2 qn 1 1 1 z z
4
r r1 0 x x Hold
end if;
end process;
qbar<= not q;
end Behavioral;
THEORY: A flip-flop is a binary cell capable of storing one bit of information. A flip-flop
circuit has two outputs, one for the normal value and one for the compliment of bit stored in
it. A flip-flop circuit can maintain a binary state indefinitely until directed by an input signal
to switch states. The major difference between various flip-flops is in number of inputs they
possess and in the manner in which the inputs affect the binary state.
The D flip-flop receives the designation from its ability to transfer data into a flip-
flop. It’s basically an RS flip-flop with an inverter in the R input. This type of flip-flop
sometimes called as D- latch.
d q
Clk D Q Qb
1 1 1 0
clk D-Flip qb
1 0 0 1
Flop
0 X X Hold
entity dF_F is
port ( d, clk : in std_logic;
Q : out std_logic;
Qb : out std_logic);
end dF_F;
begin
if (clk='1')then
Q<=d;
Qb<=not d;
end if;
end process ;
end Behavioral;
always @ (d,clk)
begin
if(clk==1)
begin
q=d;
qb=~d;
end
end
endmodule
T Q Qn
D Q 0 0 1
0 1 0
T T-FF
1 0 0
1 1 1
Qn
if(rising_edge(sclk)) then
clkdiv<=clkdiv+1;
end if;
clk <=clkdiv(0);
end process;
begin
process(clk)
begin
if(rising_edge(clk)) then
if(reset='1') then
q<='0';
elsif(t='1') then
q<= not q;
else
q<=q;
always @ (posedge(clk))
begin
clkdiv= clkdiv+1;
end
assign sclk= clkdiv[22];
always @ (posedge sclk)
begin
case(t)
1'd0 : q = q;
1'd1 : q =~q;
endcase
qb=~q;
end
endmodule
Clk J K Q Qb
J
Q 1 0 0 Hold
J K FF 1 0 1 0 1
Clk
1 1 0 1 0
Qb
K 1 1 1 Qb Q
if(rising_edge(sclk)) then
clkdiv<=clkdiv+1;
end if;
clk <=clkdiv(0);
end process;
process(clk,reset)
begin
if(reset='1') then
q<='0';
elsif(rising_edge(clk))then
module jkff(jk,q,qb,clk);
input [1:0]jk;
input clk;
output reg q,qb;
wire sclk;
reg [22:0]clkdiv;
always @ (posedge(clk))
ECE Dept. BMS College of Engineering 45
Dept of Electronics IV-Sem, HD-Lab
begin
clkdiv= clkdiv+1;
end
assign sclk= clkdiv[22];
always @ (posedge(sclk))
begin
case (jk)
2'b00:q=q;
2'b01:q=0;
2'b10:q=1;
2'b11:q=~q;
endcase
qb=~q;
end
endmodule
Experiment No. 6
AIM: Design a 4-bit BCD COUNTER with synchronous reset and asynchronous
Reset.
THEORY: A counter is a register capable of counting the number of clock pulses arriving at
its clock input. There are two types of counters, synchronous and asynchronous. In
synchronous counter the common clock input is used to connect all the flip-flops and they are
clocked simultaneously. In Asynchronous counters the external clock pulse clocks the first
flip-flop and then each successive flip-flop is clocked by the output of previous flip-flop.
BCD stands for Binary Coded Decimal. A BCD counter has four outputs usually
labeled A, B, C, D. By convention A is the least significant bit, or LSB. In other words, the
counter outputs follow a binary sequence representing the decimal numbers 0-9.... this is why
its called as binary coded decimal counter.
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity asbcdcounter is
Port ( sclk, reset : in std_logic;
q : out std_logic_vector(3 downto 0));
end asbcdcounte;
process(clk,reset)
begin
if(reset='1') then
count<="0000";
elsif(rising_edge(clk)) then
if(count="1001") then
count<="0000";
else
count<=count+1;
end if;
end if;
end process;
q<=count;
end Behavioral;
VERILOG
module count(sclk,reset,q);
input sclk;
output reg [3:0]q;
wire clk;
reg [20:0]clkdiv;
ECE Dept. BMS College of Engineering 47
Dept of Electronics IV-Sem, HD-Lab
always @ (posedge(sclk))
begin
clkdiv=clkdiv+1;
end
assign clk= clkdiv[20];
always @ (posedge(clk) or posedge(reset))
begin
if (reset==1)
q = 4'b0000;
else if (q==4'b1001)
q = 4'b0000;
else
q=q+1;
end
endmodule
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity cbcd is
Port ( sclk, reset : in std_logic;
q : out std_logic_vector(3 downto 0));
end cbcd;
process(sclk,reset)
begin
if(reset='1') then
count<="0000";
elsif(rising_edge(sclk)) then
if(count="1001") then
count<="0000";
else
count<=count+1;
end if;
end if;
end process;
q<=count;
end Behavioral;
VERILOG
module count(sclk,reset,q);
input sclk;
output reg [3:0]q;
ECE Dept. BMS College of Engineering 48
Dept of Electronics IV-Sem, HD-Lab
q3 q2 q1 qo
Clock Reset Current state Next state
1 1 xxx 0000
1 0 0000 0001
Counter 1 0 0001 0010
1 0 0010 0011
1 0 0011 0100
1 0 0100 0101
1 0 0101 0110
1 0 0110 0111
Reset clk 1 0 0111 1000
1 0 1000 1001
1 0 1001 1010
1 0 1010 1011
1 0 1011 1100
1 0 1100 1101
1 0 1101 1110
1 0 1110 1111
1 x 1111 0000
BINARY-counter program with synchronous reset
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity binaryc is
Port ( sclk, reset : in std_logic;
q : out std_logic_vector(3 downto 0));
end binaryc;
process(clk,reset)
begin
if(rising_edge(clk)) then
if(reset='1') then
count<="0000";
else
count<=count+1;
end if;
end if;
end process;
q<=count;
end Behavioral;
Verilog:
module count(sclk,reset,q);
input sclk;
output reg [3:0]q;
wire clk;
reg [20:0]clkdiv;
always @ (posedge(sclk))
begin
clkdiv=clkdiv+1;
end
assign clk= clkdiv[20];
always @ (posedge(clk))
begin
if (reset==1)
q = 4'b0000;
else
q=q+1;
end
endmodule
VHDL
library ieee;
use ieee.std_logic_1164.all;
ECE Dept. BMS College of Engineering 50
Dept of Electronics IV-Sem, HD-Lab
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity anysequencec is
Port ( sclk, reset : in std_logic;
q : out std_logic_vector(3 downto 0));
end anysequencec;
process(clk,reset)
begin
if(rising_edge(clk)) then
if(reset='1') then
count<="0000";
else
case count is
when "0011" =>count<="0100";
when "0100" =>count<="1000";
when "1000" =>count<="1001";
when "1001" =>count<="1011";
when "1011" =>count<="1111";
when "1111" =>count<="0011";
when others =>count<="0000";
end case;
end if;
end if;
end process;
q<=count;
end Behavioral;
Verilog:
module count(sclk,reset,q);
input sclk;
output reg [3:0]q = 4’d3; //counter is inialized to starting value i,e 3
wire clk;
reg [20:0]clkdiv;
always @ (posedge(sclk))
ECE Dept. BMS College of Engineering 51
Dept of Electronics IV-Sem, HD-Lab
begin
clkdiv=clkdiv+1;
end
assign clk= clkdiv[20];
always @ (posedge(clk))
begin
if (reset==1)
q = 4'b0000;
else
begin
case (q)
4’d3: q = 4’d4;
4’d4: q = 4’d8;
4’d8: q = 4’d9;
4’d9: q = 4’d11;
4’d11: q = 4’d15;
4’d15: q = 4’d3;
default: q = 4’d3;
endcase
end
end
endmodule
VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity UpDown is
Port ( sclk, reset,ud : in std_logic;
q : out std_logic_vector(3 downto 0));
end UpDown;
process(clk,reset)
begin
ECE Dept. BMS College of Engineering 52
Dept of Electronics IV-Sem, HD-Lab
if(rising_edge(clk)) then
if(reset='1') then
count<="0000";
else
if(ud='1')then
count<=count+1;
else
count<=count-1;
end if;
end if;
end if;
end process;
q<=count;
end Behavioral;
Verilog
module count(sclk,ud,reset,q);
input sclk,ud;
output reg [3:0]q;
wire clk;
reg [20:0]clkdiv;
always @ (posedge(sclk))
begin
clkdiv=clkdiv+1;
end
assign clk= clkdiv[20];
always @ (posedge(clk))
begin
if (reset==1)
q = 4'b0000;
else if (ud==1)
q=q+1;
else
q=q-1;
end
endmodule
INTERFACING EXPERIMENTS
Experiment No. 1
SEVEN-SEGMENT DISPLAY INTERFACE
THEORY: 7-Segment display can display the digits 0-9 and the hex extension (A-F). A
signal-character displays bring out leads for 7-segments & the common elect code (Common
cathode & common anode). Here in FPGA/CPLD board to interface one 7-segment LED
display whose elements are connected to any I/O pins of the FPGA/CPLD.
Here we can consider common-anode 7-segment LED displays. The user can then ON by
driving associated signal low.
INPUT OUTPUT
BCD LED
0000 1111110
0001 0110000
0010 1101101
0011 1111001
0100 0110011
0101 1011011
0110 1011111
0111 1110000
1000 1111111
1001 1111011
1010 1110111
1011 0011111
1100 1001110
1101 0111101
1110 1001111
1111 1000111
Experiment No. 1a
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity segment1 is
port ( header6 :out std_logic_vector(3 downto 0);
header7 :out std_logic_vector(6 downto 0);
sclk :in std_logic );
end segment1;
begin
process(sclk)
begin
if( rising_edge(sclk)) then
clkdiv <= clkdiv + 1;
end if;
clk1 <= clkdiv(13);
end process ;
end process ;
process (state)
begin
case state is
when "00" => header7<="1110110"; header6 <= "1110"; -- 'H'
when "01" => header7 <="1011110";header6 <= "1101"; -- 'D'
when "10" => header7 <="0111000";header6 <= "1011"; -- 'L'
when others => header7 <="1110110";header6 <= "1110"; --‘H’
end case ;
end process;
end prp;
Experiment No. 1b
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity SEVSEG is
Port ( sclk : in std_logic;
q : inout std_logic_vector(3 downto 0);
header6 :out std_logic_vector(3 downto 0);
begin
process (sclk)
begin
if(rising_edge(sclk)) then
clkdiv<=clkdiv+1;
end if;
end process;
clk1 <=clkdiv(22);
ECE Dept. BMS College of Engineering 55
Dept of Electronics IV-Sem, HD-Lab
process (clk1)
begin
if (rising_edge(clk1)) then
if (count="1001") then
count<="0000";
else
count<=count+1;
end if;
end if;
end process;
q<=count;
process(q)
begin
case q is
when "0000"=>led<="0111111";
when "0001"=>led<="0000110";
when "0010"=>led<="1011011";
when "0011"=>led<="1001111";
when "0100"=>led<="1100110";
when "0101"=>led<="1101101";
when "0110"=>led<="1111100";
when "0111"=>led<="0000111";
when "1000"=>led<="1111111";
when "1001"=>led<="1100111";
when others=>led<="0000000";
end case;
header6<="0000";
end process;
end Behavioral;
Experiment No. 2a
AIM: Write the VHDL code to control speed and direction of Stepper Motor
THEORY: Stepper motors are electromechanical devices, which convert a digital pulses in
mechanical rotation, that provide accurate incremental rotation. The most common stepper
motor uses four windings for a four-phase operation. A typical four-phase motor driving
circuit is shown in Figure using an FPGA to generate the sequence logic. The clock (CLK)
input synchronizes the logic and determines the speed of rotation. The motor advances one
step per clock period; the angle of rotation of the shaft will depend on the particular motor.
To determine the clock period, consider that the stepper motor torque increases as frequency
decreases. The direction (DIR) control input changes the sequence at the outputs (PH1 to
PH4) to reverse the motor direction.
VDD
A
B
C
D
entity stepmot1 is
port(dir : in std_logic;
rst:in std_logic;
sclk : in std_logic);
end stepmot1;
begin
process(sclk)
begin
if(rising_edge(sclk)) then
end if;
end process;
process(step_clk,rst,dir)
begin
step_signal <="0110";
case step_signal is
when "0110"=>step_signal<="1010";
end case;
else --anticlackwise
case step_signal is
end case;
end if;
end if;
end process;
end Behavioral;
begin
process(str,dir)
begin
Experiment No. 4
EXTERNAL LIGHT CONTROL INTERFACE USING RELAYS
AIM: Write the VHDL code to control external lights using relays
THEORY: The basic function of the relay is switching of a load circuit is controlled by a low
power, electrically isolated input signal. In Electromechanical Relays (EMRs) has been the
component of choice, largely due to price, function, and availability. An input voltage is
applied to the coil mechanism. The input voltage magnetizes the core, which pulls the arm
towards it. This action causes the output contacts to touch, closing the load circuit. When the
input voltage is removed, the spring lever will push the contacts away from each other,
breaking the load circuit connection.
CIRCUIT DIAGRAM:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity relayp is
Port ( en : in STD_LOGIC;
sw1,sw2 : in STD_LOGIC;
relay1,relay2 : out STD_LOGIC);
end relayp;
architecture Behavioral of relayp is
begin
process(en,sw1,sw2)
begin
if(en='1') then
relay1<=sw1;
relay2<=sw2;
else
relay1<='0';
relay2<='0';
end if;
end process;
end Behavioral;
Experiment No. 5
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity elevator is
Port ( fl_req : in STD_LOGIC_VECTOR (2 downto 0);
clk : in STD_LOGIC;
fl_disp : out STD_LOGIC_VECTOR (7 downto 0));
end elevator;
What is FPGA :Field Programmable Gate Array, an array of logic gates whose configuration
can be programmed by the customer.
VHDL, intended as a specification langauge, is very exact in its nature and hence very
verbose.
Verilog, intended as a simulation langauge, it much closer to C in style, in that it is terse and
elegant to write but requires much more care to avoid nasty bugs. VHDL doesn't let you get
away with much; Verilog assumes that whatever you wrote was exactly what you intended to
write. If you get a VHDL architecture to compile, it's probably going to approximate to the
function you wanted. For Verilog, successful compilation merely indicates that the syntax
rules were met, nothing more. VHDL has some features that make it good for system-level
modelling, whereas Verilog is much better than VHDL at gate-level simulation.
"A Complex Programmable Logic Device (CPLD) is a Programmable Logic Device with
complexity between that of PALs (Programmable Array Logic) and FPGAs, and architectural
features of both. The building block of a CPLD is the macro cell, which contains logic
implementing disjunctive normal form expressions and more specialized logic operations".
FPGA have special routing resources to implement binary counters,arithmetic functions like
adders, comparators and RAM. CPLD don't have special features like this.
FPGA can contain very large digital designs, while CPLD can contain small designs only.The
limited complexity (<500>
Speed: CPLDs offer a single-chip solution with fast pin-to-pin delays, even for wide input
functions. Use CPLDs for small designs, where "instant-on", fast and wide decoding, ultra-
low idle power consumption, and design security are important (e.g., in battery-operated
equipment).
Security: In CPLD once programmed, the design can be locked and thus made secure. Since
the configuration bit stream must be reloaded every time power is re-applied, design security
in FPGA is an issue.
Power: The high static (idle) power consumption prohibits use of CPLD in battery-operated
equipment. FPGA idle power consumption is reasonably low, although it is sharply
increasing in the newest families.
FIELD PROGRAMABLE GATE ARRAY. These ICs have programmable logic circuit
inside it. So it can be programmed in field itself according to the requirement of application.
No prior knowledge of application is required. Nowadays FPGAs are more popular than
ASIC.
What is Synthesis?
Synthesis is the stage in the design flow which is concerned with translating your VHDL
code into gates - and that's putting it very simply! First of all, the VHDL must be written in a
particular way for the target technology that you are using. Of course, a synthesis tool doesn't
actually produce gates - it will output a netlist of the design that you have synthesised that
represents the chip which can be fabricated through an ASIC or FPGA vendor.
ECE Dept. BMS College of Engineering 62
Dept of Electronics IV-Sem, HD-Lab
Function always returns a value. Procedure may or may not return a value.
Functions can be called in SQL statements. Procedure can not be called in an SQL statement.
D-latch is level Triggering and D Flip Flop is Edge triggering. latch can have a clock.
difference is that for a latch the output can follow input(like a buffer) if latch is in "pass"
state, else if the clock input is such that the its in "latch"state then output is preserved.
Whereas, flip-flop output only changes at the clock edge(rising or falling depending upon
type of flop)
latch-it consists of both enable and clock.flip flop-it consists of only clock and no enable is
present for flip
flop.
mealy
it has less number of states.
it more prone to noise.
moore
it has more number of states.
it is less prone to noise.
just put a T FF. if u put a T FF u reduce the freq by half. if u put 2 power n FF, u reduce
the frequency by n times.
Whenever the simulator need storage to evaluate the value a registe is used. It is obvious
when you sample relative to clock. But combinatorial logic within an always process need to
be declared as process as well.
Nets, which connect modules use wire declaration. Same is for combinatorial, which are
assigned values by the assign value.
delay any pulse of small width is propagated to the output. The transport delay is especially
useful for modeling delay line drivers, wire delays on PC board, and on path delays on ASIC.
The inertial delay, which is the default delay type for VHDL, is used to model propagation
delay of gates and other devices. Inertial delay do not propagate short pulses from the input to
the output i.e. if a gate has an ideal inertial delay T, the input signal is delayed by time T, but
any pulse with a width less than T is rejected.
What are the different State machine Styles ? Which is better ? Explain disadvantages and
advantages.
Expand UCF?
What is the difference between compiled, interpreted, event based and cycle based
simulators?
What is code coverage and what are the different types of code coverage that one does ?
What is CPLD: CPLD are known to have short pin-to-pin delays, and can accept wide i/p’s,
but have relatively high power consumption and fewer f/f’s compared to FPGA’s
What is JTAG :Joint Test Action Group, older name for IEEE1149.1 Boundary scan, a
method to test PC board and ICs
K-map :a graphical tool for minimizing SOP or POS logic functions( useful for upto 6 logic
variables)