VHDL Lab File
VHDL Lab File
VHDL Lab File
Lab File
SUBMITTED BY
Giric Goyal
A2305208361
5CS3 (Y)
B.TECH (CSE)
SESSION: 2008-2012
INDEX
Theory:
Half adder is the most basic digital arithmetic circuit. It is a logical circuit that
performs an addition operation on two binary digits. It is a combinational circuit
which produces a sum and a carry value, which are both binary digits.
A half adder has two inputs, generally labeled A and B, and two outputs, the
“sum” S and “carry flag” C.
S is the two-bit “XOR gate” of A and B, and C is the “AND gate” of A and B.
S= A xor B
C=A and B
OUTPUT WAVEFORM:
Behavioral code for Half Adder
OUTPUT WAVEFORM:
Structural code for Half Adder
OUTPUT WAVEFORM:
EXPERIMENT NO 3
Theory:
Full adder is a combinational circuit that performs an addition operation on three
binary digits. It produces a sum and a carry value, which are both binary digits.
A half adder has three inputs, generally labelled A, B, and Cin, and two outputs,
the “sum” S and “carry flag” Cout.
CODE
library ieee;
use ieee.std_logic_1164.all;
entity full_adder1 is
port (a,b,c:in bit;
sum,carry:out bit);
end full_adder1;
architecture e1 of full_adder1 is
begin
carry<=(a and b) or (b and c) or (c and a);
sum<=a xor b xor c;
end e1;
OUTPUT WAVEFORM:
Behavioral code for full adder
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity fulladder is
port(a:in bit_vector(0 to 2);
sum,carry:out bit);
end fulladder;
architecture e1 of fulladder is
begin
P1:process(a)
begin
case a is
when "000"=>sum<='0';carry<='0';
when "001"=>sum<='1';carry<='0';
when "010"=>sum<='1';carry<='0';
when "011"=>sum<='0';carry<='1';
when "100"=>sum<='1';carry<='0';
when "101"=>sum<='0';carry<='1';
when "110"=>sum<='0';carry<='1';
when "111"=>sum<='1';carry<='1';
end case;
end process p1;
OUTPUT WAVEFORM:
Structural code for full adder
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity andg is
port (a,b:in bit;
z:out bit);
end andg;
architecture E of andg is
begin
z<= a and b;
end E;
entity org is
port (a,b,c: in bit;
z: out bit);
end org;
architecture E1 of org is
begin
z<=a or b or c;
end E1;
entity xorg is
port(a,b,c:in bit;
z:out bit);
end xorg;
architecture E2 of xorg is
begin
z<=a xor b xor c;
end E2;
entity full_adderg is
port (a1,b,c:in bit;
sum,carry:out bit);
end full_adderg;
architecture E3 of full_adderg is
component andg
port (a,b:in bit;
z:out bit);
end component;
component org
port (a,b,c: in bit;
z: out bit);
end component;
component xorg
port(a,b,c:in bit;
z:out bit);
end component;
signal x1,x2,x3:bit;
begin
A4: andg port map (a1,b,x1);
A2: andg port map (b,c,x2);
A3: andg port map (a1,c,x3);
OR1: org port map (x1,x2,x3,sum);
XOR1: xorg port map (a1,b,c,carry);
end E3;
OUTPUT WAVEFORM:
EXPERIMENT NO 4
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity mux41 is
port (a,b,c,d,s1,s2:in bit;
z:out bit);
end mux41;
architecture e1 of mux41 is
begin
z<=(((not s1) and (not s2) and a) or ((not s1) and (s2) and b) or ((s1) and (not s2) and c) or
((s1) and (s2) and d));
end e1;
OUTPUT WAVEFORM
Behavioral code for 4 to 1 multiplexer
TRUTH TABLE:
I/P O/P
S0 S1 Y
0 0 D0
0 1 D1
1 0 D2
1 1 D3
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity mux4 is
port(a,b,c,d,s1,s2:in bit;
z:out bit);
end mux4;
architecture e1 of mux4 is
begin
process(a,b,c,d,s1,s2)
begin
if s1&s2="00"
then z<=a;
elsif s1&s2="01"
then z<=b;
elsif s1&s2="10"
then z<=c;
else z<=d;
end if;
end process;
end e1;
OUTPUT WAVEFORM:
OUTPUT WAVEFORM:
EXPERIMENT NO 5
Theory:
A decoder is a device which does the reverse of an encoder, undoing the
encoding so that the original information can be retrieved. The same method
used to encode is usually just reversed in order to decode.
In digital electronics, a decoder can take the form of a multiple-input, multiple-
output logic circuit that converts coded inputs into coded outputs, where the
input and output codes are different. e.g. n-to-2n, binary-coded decimal decoders.
Enable inputs must be on for the decoder to function, otherwise its outputs
assume a single "disabled" output code word. Decoding is necessary in
applications such as data multiplexing, 7 segment display and memory address
decoding.
Dataflow code for 3:8 decoder:
CODE: library ieee;
use ieee.std_logic_1164.all;
entity decoder is
port(x,y,z,enable:in bit;a,b,c,d,e,f,g,h:out bit);
end decoder;
architecture deco1 of decoder is
begin
a<=((not x)and (not y)and (not z));
b<=((not x)and (not y)and z);
c<=((not x)and y and (not z));
d<=((not x)and y and z);
e<=(x and (not y)and (not z));
f<=(x and (not y)and z);
g<=(x and y and (not z));
h<=(x and y and z);
end deco1;
OUTPUT WAVEFORM
OUTPUT WAVEFORM
Behavioral code for 3:8 decoder:
OUTPUT WAVEFORM
EXPERIMENT NO 6
Theory:
D FLIP FLOP : The Q output always takes on the state of the D input at the
moment of a rising clock edge (or falling edge if the clock input is active low).
[7]
It is called the D flip-flop for this reason, since the output takes the value of
the D input or Data input, and Delays it by one clock count. The D flip-flop can
be interpreted as a primitive memory cell, zero-order hold, or delay line.
Truth table:
Clock D Q Qprev
Rising
0 0 X
edge
Rising
1 1 X
edge
Non-Rising X Qprev
T FLIP FLOP :
If the T input is high, the T flip-flop changes state ("toggles") whenever the clock
input is strobed. If the T input is low, the flip-flop holds the previous value. This
behavior is described by the characteristic equation:
(or, without benefit of the XOR operator, the
equivalent: )
and can be described in a truth table:
1 0 1 toggle 0 1 1 Complement
1 1 0 toggle 1 0 1 Complement
OUTPUT WAVEFORM
SR FLIP FLOP :
The fundamental latch is the simple SR flip-flop, where S and R stand
for set and reset, respectively. It can be constructed from a pair of cross-
coupled NAND or NORlogic gates. The stored bit is present on the output
marked Q.
Normally, in storage mode, the S and R inputs are both low,
and feedback maintains the Q and Q outputs in a constant state, with Q the
complement of Q. If S is pulsed high while R is held low, then the Q output is
forced high, and stays high even after S returns low; similarly, if R is pulsed high
while S is held low, then the Q output is forced low, and stays low even after R
returns low.
0 1 Q=0 0 1 1 0 reset
1 0 Q=1 1 0 0 1 set
OUTPUT WAVEFORM
JK FLIP FLOP :
The JK flip-flop augments the behavior of the SR flip-flop (J=Set, K=Reset) by
interpreting the S = R = 1 condition as a "flip" or toggle command. Specifically,
the combination J = 1, K = 0 is a command to set the flip-flop; the combination J
= 0, K = 1 is a command to reset the flip-flop; and the combination J = K = 1 is a
command to toggle the flip-flop, i.e., change its output to the logical complement
of its current value. Setting J = K = 0 does NOT result in a D flip-flop, but rather,
will hold the current state. To synthesize a D flip-flop, simply set K equal to the
complement of J. The JK flip-flop is therefore a universal flip-flop, because it
can be configured to work as an SR flip-flop, a D flip-flop, or a T flip-flop.
The characteristic equation of the JK flip-flop is:
0 1 0 reset 0 1 1 X Set
1 0 1 set 1 0 X 1 Reset
OUTPUT WAVEFORM
EXPERIMENT NO 7
Theory:
These are the simplest kind of shift registers. The data string is presented at 'Data
In', and is shifted right one stage each time 'Data Advance' is brought high. At
each advance, the bit on the far left (i.e. 'Data In') is shifted into the
0 0 0 0
first flip-flop's output. The bit on the far right (i.e. 'Data Out') is shifted
out and lost. 1 0 0 0
0 1 0 0
The data are stored after each flip-flop on the 'Q' output, so there are
four storage 'slots' available in this arrangement, hence it is a 4-Bit 1 0 1 0
Register. To give an idea of the shifting pattern, imagine that the 1 1 0 1
register holds 0000 (so all storage slots are empty). As 'Data In' 0 1 1 0
presents 1,0,1,1,0,0,0,0 (in that order, with a pulse at 'Data Advance' 0 0 1 1
each time. This is called clocking or strobing) to the register, this is the
0 0 0 1
result. The left hand column corresponds to the left-most flip-flop's
output pin, and so on. 0 0 0 0
So the serial output of the entire register is 10110000 (). As you can see if we
were to continue to input data, we would get exactly what was put in, but offset
by four 'Data Advance' cycles. This arrangement is the hardware equivalent of
a queue. Also, at any time, the whole register can be set to zero by bringing the
reset (R) pins high.
This arrangement performs destructive readout - each datum is lost once it been
shifted out of the right-most bit.
Structural code for SISO:
CODE: library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(d,clk:in bit;
q:out bit);
end dff;
architecture e1 of dff is
begin
process(d, clk)
begin
if clk='1' and clk'event then
q<=d;
end if;
end process;
end e1;
entity siso is
port( input,clk:in bit;
qout:out bit);
end siso;
architecture e1 of siso is
component dff
port(d,clk:in bit;
q:out bit);
end component;
signal q0,q1,q2:bit;
begin
n1:dff port map(input,clk,q0);
n2:dff port map(q0,clk,q1);
n3:dff port map(q1,clk,q2);
n4:dff port map(q2,clk,qout);
end e1;
OUTPUT WAVEFORM
PIPO:
The PARALLEL IN/PARALLEL OUT shift register is loaded with four bits
simultaneously, in parallel.They are also clocked out simultaneously, in parallel.
PISO:
This configuration has the data input on lines D1 through D4 in parallel format.
To write the data to the register, the Write/Shift control line must be held LOW.
To shift the data, the W/S control line is brought HIGH and the registers are
clocked. The arrangement now acts as a SISO shift register, with D1 as the Data
Input. However, as long as the number of clock cycles is not more than the
length of the data-string, the Data Output, Q, will be the parallel data read off in
order.
Structural code for PIPO:
CODE: library ieee;
use ieee.std_logic_1164.all;
entity ctrl is
port(outf1,x,control:in bit;
inpf2:out bit);
end ctrl;
architecture e1 of ctrl is
begin
process(x,control,outf1)
begin
if control='1' then
inpf2<=x;
else
inpf2<=outf1;
end if;
end process;
end e1;
entity dff is
port (d,clk:in bit;
q,qbar:out bit);
end dff;
architecture behav of dff is
begin
process (d,clk)
begin
if clk = '1' and clk'event then
q<=d;
qbar<=not d;
end if;
end process;
end behav;
entity piso is
port(a,b,c,d,d0,d1,d2,d3:inout bit;
q0,q1,q2,q3:inout bit;
control,clk:in bit);
end piso;
architecture e2 of piso is
component dff
port (d,clk:in bit;
q,qbar:out bit);
end component;
component ctrl
port(outf1,x,control:in bit;
inpf2:out bit);
end component;
begin
d0<=a;
N1: dff port map (a,clk,q0);
N2: ctrl port map (q0,b,control,d1);
N3: dff port map (d1,clk,q1);
N4: ctrl port map (q1,c,control,d2);
N5: dff port map (d2,clk,q2);
N6: ctrl port map (q2,c,control,d3);
N7: dff port map (d3,clk,q3);
end e2;
OUTPUT WAVEFORM
SIPO:
This configuration allows conversion from serial to parallel format. Data is input
serially, as described in the SISO section above. Once the data has been input, it
may be either read off at each output simultaneously, or it can be shifted out and
replaced.
OUTPUT WAVEFORM
EXPERIMENT NO 9
Theory:
In digital logic and computing, a counter is a device which stores (and
sometimes displays) the number of times a particular event or process has
occurred, often in relationship to a clock signal. In practice, there are two types
of counters:
A mod 5 counter counts till binary four and in fifth clock pulse it counts back to
zero.
OUTPUT WAVEFORM
EXPERIMENT NO 8
Theory:
In digital logic and computing, a counter is a device which stores (and
sometimes displays) the number of times a particular event or process has
occurred, often in relationship to a clock signal. In practice, there are two types
of counters:
OUTPUT WAVEFORM
EXPERIMENT NO 10
Theory:
An arithmetic logic unit (ALU) is a digital circuit that performs arithmetic and
logical operations. The ALU is a fundamental building block of the central
processing unit (CPU) of a computer, and even the simplest microprocessors
contain one for purposes such as maintaining timers. The processors found inside
modern CPUs and graphics processing units accommodate very powerful and
very complex ALUs; a single component may contain a number of ALUs.
Behavioral code forALU:
CODE: 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(2 downto 0);
sel: in std_logic_vector(3 downto 0);
cin: in std_logic;
y:out std_logic_vector( 2 downto 0));
END ENTITY alu;
ARCHITECTURE alu OF alu IS
BEGIN
process( a,b,sel)
begin
case sel is
--arithmetic unit
when"0000"=> y<=a;
when"0001"=> y<=a+1;
when"0010"=> y<=a-1;
when"0011"=> y<=b;
when"0100"=> y<=b+1;
when"0101"=> y<=b-1;
when"0110"=> y<=a+b;
when"0111"=> y<=a+b+cin;
-- logic unit
when"1000"=> y<= not a;
when"1001"=> y<= not b;
when"1010"=> y<= a and b;
when"1011"=> y<= a or b;
when"1100"=> y<= a nand b;
when"1101"=> y<= a nor b;
when"1110"=> y<= a xor b;
when"1111"=> y<= not ( a xor b);
when others=> null;
end case;
end process;
END ARCHITECTURE alu;
OUTPUT WAVEFORM
EXPERIMENT NO 1
Theory:
OR GATE
The OR gate is a digital logic gate that implements logical disjunction - it
behaves according to the truth table to the right. A HIGH output (1) results if one
or both the inputs to the gate are HIGH (1). If neither input is HIGH, a LOW
output (0) results.
INPUT OUTPUT
A B A+B
0 0 0
0 1 1
1 0 1
1 1 1
OUTPUT WAVEFORM
NOR GATE:
The NOR gate is a digital logic gate that implements logical NOR - it behaves
according to the truth table to the right. A HIGH output (1) results if both the
inputs to the gate are LOW (0). If one or both input is HIGH (1), a LOW output
(0) results. NOR is the result of the negation of the OR operator. NOR is a
functionally complete operation -- combinations of NOR gates can be combined
to generate any other logical function. By contrast, the OR operator is monotonic
as it can only change LOW to HIGH but not vice versa.
INPUT OUTPUT
A B A NOR B
0 0 1
0 1 0
1 0 0
1 1 0
OUTPUT WAVEFORM
XOR GATE:
The XOR gate (sometimes EOR gate) is a digital logic gate that implements
exclusive disjunction - it behaves according to the truth table above. A HIGH
output (1) results if one, and only one, of the inputs to the gate is HIGH (1). If
both inputs are LOW (0) or both are HIGH (1), a LOW output (0) results.
XOR gate is short for exclusive OR. This means that precisely one input must be
1 (true) for the output to be 1 (true). A way to remember XOR is "one or the
other but not both."
INPUT OUTPUT
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
The XNOR gate (sometimes spelled "exnor" or "enor") is a digital logic gate
whose function is the inverse of the exclusive OR (XOR) gate. The two-input
version implements logical equality, behaving according to the truth table to the
right. A HIGH output (1) results if both of the inputs to the gate are the same. If
one but not both inputs are HIGH (1), a LOW output (0) results.
Behavioral code for XOR gate:
CODE: library ieee;
use ieee.std_logic_1164.all;
entity norgate is
port(a,b:in bit;
x:out bit);
end norgate;
architecture behav of norgate is
begin
p1:process(a,b)
begin
if a='0' and b='0'
then x<='1';
elsif a='0' and b='1'
then x<='0';
elsif a='1' and b='0'
then x<='0';
else x<='0';
end if;
end process p1;
end behav;
OUTPUT WAVEFORM
NAND GATE:
NAND gates are one of the two basic logic gates (along with NOR gates) from
which any other logic gates can be built. Due to this property, NAND and NOR
gates are sometimes called "universal gates". However, contrary to popular
belief, modern integrated circuits are not constructed exclusively from a single
type of gate. Instead, EDA tools are used to convert the description of a logical
circuit to a netlist of complex gates (standard cells).
Behavioral code for XOR gate:
CODE: library ieee;
use ieee.std_logic_1164.all;
entity nandg is
port(a,b:in bit;z:out bit);
end nandg;
architecture a1 of nandg is
constant nandg_delay: time := 10 ns;
begin
process(a,b)
variable t:bit_vector(0 to 1);
begin
t:=a & b;
case t is
when "00"=>z<='1';
when "01"=>z<='1';
when "10"=>z<='1';
when "11"=>z<='0';
end case;
end process;
end a1;
OUTPUT WAVEFORM