VLSI Lab Manual Exercise Problems
VLSI Lab Manual Exercise Problems
Exercise Problems
Exercise No. 2.1 Write a dataflow Verilog code to realize the given logic function in POS form
and verify the design by simulation.
F = (A̅+B ̅ + C). (A
̅+B̅+C ̅ ).(A + B + C). (A + B + C ̅)
Solution:
Verilog Code:
Simulation Results:
Input: A = 0, B = 0, C = 0
Output: F = 0
Exercise No. 2.2 Write dataflow Verilog code for following digital building blocks and verify
the design by simulation: [i] full adder [ii] full subtractor [iii] three variable majority function
[iv] three input exnor function [v] two bit equality detector.
Solution:
Verilog Code:
module fulladder(
input a,b,c,
output sum,carry );
wire x;
assign x = ( a & ~b) | ( ~a & b );
assign sum = ( x & ~c) | ( ~x & c);
assign carry = a&b | b&c | c&a;
endmodule
Simulation Results:
Input: abc = 011
Output: sum = 0, carry = 1
Verilog Code:
module fullsubtractor(
input a,b,c,
output diff,borrow,
inout x
);
assign x = ( a & ~b) | ( ~a & b );
assign diff = ( x & ~c) | ( ~x & c);
assign borrow = ~a&b | ~b&c | ~c&a;
endmodule
Simulation Results:
Verilog Code:
module majorityfunction(
input a,b,c,
output m );
assign m = a&c | b&c;
endmodule
Simulation Result:
Input: abc = 001
Output: m = 0
[iv] Equality detector
Verilog Code:
module equalitydetector(
input a,b,
output c
);
assign c = ( ~a & ~b) | ( a & b );
endmodule
Simulation Result:
Input: ab = 00
Output: c = 1
Exercise No. 2.3 Write a dataflow Verilog code for 8- to-3 encoder with enable input and verify
the design by simulation
Verilog Code:
module encoder8to3(
output [2:0] A,
input [7:0] D,
input E
);
wire Ebar;
assign Ebar=~E;
assign A[2]=(D[4]|D[5]|D[6]|D[7])&Ebar;
assign A[1]=(D[2]|D[3]|D[6]|D[7])&Ebar;
assign A[0]=(D[1]|D[3]|D[5]|D[7])&Ebar;
endmodule
Simulation Result:
Exercise No. 2.4 Write a dataflow Verilog code for 8- to-3 priority encoder and verify the design
by simulation
Verilog Code:
module PriorityEncoder_8_to_3(input [0:7] D, output [2:0] Y)
assign Y[1] = D[2] | D[3]|D[6]|D[7];
assign Y[2] = D[4] | D[5] | D[6] |D[7];
assign Y[0] = D[1] | D[3] | D[5] | D[7];
endmodule
Simulation Result:
Simulation Result:
Input: G[4:1] = 00002
Output: B[4:1] = 00002
Exercise No. 2.6 Write a dataflow Verilog code for 8421 to 2421 code converter and verify the
design by simulation.
Verilog Code:
module code8421to2421(
input a,b,c,d,
output p,q,r,s
);
assign p = a&~b&~c;
assign q = ~a&b | a&~b&~c;
assign r= ~a&c | a&~b&~c;
assign s= ~a&d | ~b&~c&d;
endmodule
Simulation Result:
Verilog Code:
module MAG_COMP_1BIT(
input A,B,
input G,E,L
);
wire Abar,Bbar;
assign Abar=~A;
assign Bbar=~B;
assign G=A & Bbar;
assign E=A^~B;
assign L=Abar & B;
endmodule
Simulation Result:
Input: A = 1, B = 1
Output: G = 0, E = 1, L = 0
Exercise No. 2.8 Write a dataflow Verilog code for N bit magnitude comparator and verify the
design by simulation.
Verilog Code:
Simulation Result:
Verilog Code:
Simulation Result:
Simulation Result:
Exercise No. 3.2 Write the sequential Verilog code for synchronous mod 5 counter.
Verilog Code:
Simulation Result:
Exercise No. 3.3 Write a sequential Verilog code for 4 bit priority encoder.
Verilog Code:
Simulation Result:
Exercise No. 3.4 Write the sequential Verilog code for Master-slave JK flip-flop (assume delay
of master and slave as 2ns and 1ns respectively)
Verilog code:
Module jkffmasterslave(Q,Qn,C,J,K,Resetn);
output Q;
output Qn;
input C,J,K,Resetn;
wire mq;
wire mqn;
wire cn;
wire j1,k1,j2,k2;
assign j2=!Resetn? 0 : J1;
assign K2=!Resetn ? 1 : K1;
and(J1,J,Qn);
and(K1,K,Q);
not(Cn,C);
srlatchmaster(mq,mqn,c,J2,K2);
srlatchslave(q,qn,cn,mq,mqn);
endmodule
Simulation Result:
Exercise No. 3.5 Write sequential Verilog code for 4 bit universal shift register
Verilog Code:
Simulation Result:
Exercise No. 3.6 Write sequential Verilog code to model ACTEL ACT 1 Logic Module (Use
initial statement).
Verilog Code:
module act1(a,b,c,d,e,f,x,y,dout);
output dout;
input a,b,c,d,e,f,x,y;
wire p,q,r;
initial
begin
p = (a&(~c)|(b&(c));
q = (d&(~f)|(e&(f));
r = x|y;
dout = (p&(~r)|(b&(r));
endmodule
Simulation Result:
Expt. No. 4 Verilog structural modeling
Exercise Problems
Exercise No. 4.1 Write structural Verilog code for mod-10 ripple counter and verify the design
by simulation
Exercise No. 4.2(a) Write structural Verilog code for 4 bit SIPO/ PISO shift register and verify
the design by simulation
Verilog Code:
d_flip_flop u0 (.din(din),
.clk(clk),
.reset(reset),
.dout(dout[0]));
d_flip_flop u1 (.din(dout[0]),
.clk(clk),
.reset(reset),
.dout(dout[1]));
d_flip_flop u2 (.din(dout[1]),
.clk(clk),
.reset(reset),
.dout(dout[2]));
d_flip_flop u3 (.din(dout[2]),
.clk(clk),
.reset(reset),
.dout(dout[3]));
endmodule
Simulation Result:
Input :
Output:
Exercise No. 4.2(b) Write structural Verilog code for 4 bit PISO shift register and verify the
design by simulation
Verilog Code:
d_flip_flop u2 (.din(s[11]),
.clk(clk),
.reset(reset),
.dout(s[4]));
d_flip_flop u3 (.din(s[12]),
.clk(clk),
.reset(reset),
.dout(dout));
not n1(s[0],shift_load);
and a1(s[1],shift_load,s[2]),
a2(s[5],din[1],s[0]),
a3(s[6],s[3],shift_load),
a4(s[7],s[0],din[2]),
a5(s[8],s[4],shift_load),
a6(s[9],s[0],din[3]);
or or1(s[10],s[1],s[5]),
or2(s[11],s[6],s[7]),
or3(s[12],s[8],s[9]);
endmodule
// -------------- D flip flop design - -----------------------
module d_flip_flop ( din ,clk ,reset ,dout );
output dout ;
reg dout;
input din ;
input clk ;
input reset ;
initial
begin
dout=0;
end
always @ (posedge clk)
begin
if (reset)
dout <= 1;
else
dout <= din;
end
endmodule
Simulation Result:
Input :
Output:
Exercise No. 4.3 Write structural Verilog code for 4 bit carry look ahead adder and verify the
design by simulation
Verilog Code:
Simulation Result:
Input :
Output:
Exercise No. 4.4 Write structural Verilog code for 4 bit carry save multiplier and verify the
design by simulation.
Verilog Code:
assign s[0]=p[1];
CSA C1(p[5],p[2],0,s[1],W[1]),
C2(p[6],p[3],W[1],W[2],W[3]),
C3(p[7],p[4],W[3],W[4],W[7]),
C4(p[8],p[11],W[7],W[10],W[12]),
C5(p[12],p[15],W[12],W[15],W[16]),
C6(W[2],p[9],0,s[2],W[5]),
C7(W[4],p[10],W[5],W[6],W[8]),
C8(p[14],W[10],W[8],W[11],W[13]),
C9(p[13],W[6],0,s[3],W[9]),
C10(W[11],W[9],0,s[4],W[14]),
C11(W[13],W[15],W[14],s[5],W[17]),
C12(p[16],W[17],W[16],s[6],s[7]);
endmodule
Simulation Result:
Input:
Output:
Exercise No. 4.5 Write structural Verilog code for 4- bit binary-to-gray code converter and
verify the design by simulation.
Verilog Code:
Simulation Result:
Input:
Output:
Exercise Problems
Exercise No. 5.1 Write a switch level Verilog code for the following combinational logic using
both gate based and TG based approach
Y = (AB + CD)’
Verilog Code:
Simulation Result:
Exercise No. 5.2 Write switch level Verilog code for a 3 input CMOS NAND gate with test
benches
Verilog Code:
Simulation Result:
Exercise No. 5.3 Write a Verilog code for ALU using mixed style of modelling. Model the
addition operation using carry-look ahead adders. The operation code for selection is given
below
Simulation Result:
Expt. No. 6 Verilog examples using task and functions, user defined primitive
Exercise Problems
Exercise No. 6.1 Write a Verilog code of 4 to 1 multiplexer as UDP and verify the design
description by simulation.
Solution:
Verilog Code:
module udp_mux4_tb;
reg s1, s0, d0, d1, d2, d3;
wire out;
initial
begin
//set the input lines to known values
d0 = 1; d1 = 0; d2 = 1; d3 = 0;
//display the input values
#10 $display ("d0=%b, d1=%b, d2=%b, d3=%b \n",
d0, d1, d2, d3); // \n is new line
//continued on next page
//select d0 = 1
s1 = 0; s0 = 0;
#10 $display ("s1=%b, s0=%b, output=%b \n",
s1, s0, out);
//select d1 = 0
s1 = 0; s0 = 1;
#10 $display ("s1=%b, s0=%b, output=%b \n",
s1, s0, out);
//select d2 = 1
s1 = 1; s0 = 0;
#10 $display ("s1=%b, s0=%b, output=%b \n",
s1, s0, out);
//select d3 = 0
s1 = 1; s0 = 1;
#10 $display ("s1=%b, s0=%b, output=%b \n",
s1, s0, out);
#10 $stop;
end
//instantiate the module into the test bench.
//if instantiating only the primitive with no module,
//then instantiation must be done using positional notation
udp_mux4 inst1 (out, s1, s0, d0, d1, d2, d3);
endmodule
Simulation Result:
Input: d3 = 0, d2 = 1, d1 = 0, do = 1, s1 = 1, s0 = 0
Output: out = 1
Exercise No. 6.2 Write a Verilog code for 4-bit binary to Gray code converter using two-input
xor gate UDP and verify the design by simulation.
Solution:
Verilog Code:
//UDP for a 2-input exclusive-OR
primitive udp_xor2 (z1, x1, x2);
input x1, x2;
output z1;
//define state table
table
//inputs are in the same order as the input list
// x1 x2 : z1; comment is for readability
0 0 : 0;
0 1 : 1;
1 0 : 1;
1 1 : 0;
endtable
endprimitive
Simulation Result:
Verilog Code:
Simulation Result:
Input: a1 = 0, b1 = 0
Output: d1= 0
Exercise No. 6.4 Write a Verilog code for half-adder using task and then describe the behaviour
of full-adder from two half-adders.
Solution:
Verilog Code:
Simulation Result:
Input: x = 1, y = 0, cin = 1
Exercise No. 6.5 Write a Verilog code for the positive-edge-triggered D flip-flop with active low
reset as UDP.
Solution:
Verilog code:
primitive udp_dff_edge1 (q, d, clk, rst_n);
input d, clk, rst_n;
output q;
reg q; //q is internal storage
//initialize q to 0
initial
q = 0;
//define state table
table
//inputs are in the same order as the input list
// d clk rst_n : q : q+; q+ is the next state
0 (01) 1 : ? : 0; //(01) is rising edge
1 (01) 1 : ? : 1; //rst_n = 1 means no rst
1 (0x) 1 : 1 : 1; //(0x) is no change
0 (0x) 1 : 0 : 0;
? (?0) 1 : ? : -; //ignore negative edge
//reset case when rst_n is 0 and clk has any transition
? (??) 0 : ? : 0; //rst_n = 0 means reset
//reset case when rst_n is 0. d & clk can be anything, q+=0
? ? 0 : ? : 0;
//reset case when 0 --> 1 transition on rst_n. Hold q+ state
? ? (01) : ? : -;
//non-reset case when d has any trans, but clk has no trans
(??) ? 1 : ? : -; //clk = ?, means no edge
endtable
endprimitive
`timescale 1 ns / 1 ps
Simulation Results
Input: rst_n = 12 ; d = 1; clk = ↑ (0 to 1 transition) ;
Output: q = 1
Expt. No. 7 Verilog examples using task and functions, user defined primitive
Exercise No. 7.1 Synthesize and implement the sequential Verilog model of 4 bit up down
counter [make use of control and reset inputs] using XILINX SPARTAN 3 chip and display the
decimal equivalent output using seven segment display. Use clock division principle.
Solution :
Verilog code:
module updown_sevenseg(up_down,display,control,reset,clk,counter);
input up_down,clk,reset;
output [3:0] counter;
output [7:0] display;
output [3:0] control;
reg [3:0] control;
reg [7:0] display;
reg [3:0] counter;
integer temp;
reg clkdiv;
initial
begin
temp=0;
counter=4'b0000;
clkdiv=1'b0;
end
always@(posedge clk)
begin
temp=temp+1;
if (temp==2000000)
begin
clkdiv=~clkdiv;
temp=0;
end
end
always@(posedge clkdiv)
begin
if(reset)
counter=4'b0000;
else if(up_down==1'b1)
counter=counter+1;
else if(up_down==1'b0)
counter=counter-1;
control=4'b0111;
case (counter)
4'd0:display=8'b11111100;
4'd1:display=8'b01100000;
4'd2:display=8'b11011010;
4'd3:display=8'b11110010;
4'd4:display=8'b01100110;
4'd5:display=8'b10110110;
4'd6:display=8'b10111110;
4'd7:display=8'b11100000;
4'd8:display=8'b11111110;
4'd9:display=8'b11110110;
4'd10:display=8'b11111010;
4'd11:display=8'b00111110;
4'd12:display=8'b10011100;
4'd13:display=8'b01111010;
4'd14:display=8'b10011110;
4'd15:display=8'b10001110;
endcase
end
endmodule
Solution:
Verilog code:
module squarewave(clk,reset,dacout);
input clk,reset;
output [7:0] dacout;
reg [7:0] dacout;
integer count,temp1;
reg temp2;
reg clkms;
initial
begin
count=0;
clkms=1'b0;
dacout=8'd0;
temp1=0;
temp2=1'b0;
end
Procedure:
Output waveform
Exercise No. 7.3 Interface the stepper motor with XILINX SPARTAN 3 board and run a
Verilog code to control stepper motor operations like clockwise/ anticlockwise rotation, step size
control, variation of speed (slow/ medium and high)
Verilog code:
end
always @(count)
begin
case (count)
2'b00 : stepout=4'b1100 ;
2'b01 : stepout=4'b0110 ;
2'b10 : stepout=4'b0011 ;
2'b11 : stepout=4'b1001 ;
endcase
end
endmodule
Procedure:
[i] Make the connection between FRC1 of the FPGA board with the Dip switch of
INTERFACING board. If anti-clk Dip switch is ON, direction will be anticlockwise
otherwise direction will be clockwise.
[ii] Make the connection between FRC9 of the FPGA board with the STEPPER MOTOR
INPUT of INTERFACING board. Connect STEPPER MOTOR OUTPUT TO STEPPER
MOTOR.
[iii] Connect the downloading cable and power supply to the FPGA board.
[iv] Then open the Xilinx iMPACT software and select the respective BIT file and click
program.
[v] Apply inputs to DIP SWITCHES i.e. HIGH, MED and LOW and observe the output in the
stepper motor.
Exercise No. 9.1 Write a Verilog code and test bench to model and simulate 4 bit ripple counter.
Solution :
Verilog code:
module ripple_counter (clock, toggle, reset, count);
input clock, toggle, reset;
output [3:0] count;
reg [3:0] count;
wire c0, c1, c2;
assign c0 = count[0], c1 = count[1], c2 = count[2];
Simulation Results:
input cin,x,y;
output s,cout;
assign s = x^y^cin;
assign cout =( x & y) | (x & cin) |( y & cin);
endmodule
//Instantiation of above module
module adder4 ( carryin,x,y,sum,carryout);
input carryin;
input [3:0] x,y;
output [3:0] sum;
output carryout;
fulladd stage0 (carryin,x[0],y[0],sum[0],c1);
fulladd stage1 (c1,x[1],y[1],sum[1],c2);
fulladd stage2 (c2,x[2],y[2],sum[2],c3);
fulladd stage3 (c3,x[3],y[3],sum[3],carryout);
endmodule
Simulation Results:
Input: x[3:0] = 11112, y[3:0] = 10102
Output: carryout = 12 , sum[3:0] = 10012
At console window, t = 0, 20, 60, 100 Sum is 0, 9, 1, 14. Stopped time is 150 ns.
Fig. 2. Waveform and console output of 4 bit ripple carry adder.
Exercise No. 9.3 Write a Verilog code and test bench to model and simulate following:
[i] D flip-flop [ii] T flip-flop
Solution :
Verilog code:
[i] D flip-flop
module d_ff(q,clk,n_rst,din);
output q;
input clk,din,n_rst;
reg q;
always @(posedge clk or negedge n_rst)
begin
if(!n_rst)
q <= 1'b0;
else
q <= din;
end
endmodule
module d_ff_test;
reg clk, din, n_rst;
wire q;
d_ff df1 (q, clk, n_rst, din);
initial
clk = 1'b0;
always
#10 clk = ~clk;
initial
begin
din = 1'b0;
n_rst = 1'b1;
#20 n_rst = 1'b0;
#10 din = 1'b1;
#20 n_rst = 1'b1;
#20 din = 1'b0;
#10 ;
end
always
#5 $display ($time," clk=%b din=%b q=%b", clk, din, q);
initial
#100 $finish;
endmodule
Simulation Results:
[ii] T flip-flop
module t_ff(q,qbar,clk,tin,rst);
output q,qbar;
input clk,tin,rst;
reg tq;
always @(posedge clk or negedge rst)
begin
if(!rst)
tq <= 1'b0;
else
begin
if (tin)
tq <= ~tq;
end
end
assign q = tq;
assign qbar = ~q;
endmodule
module t_ff_test;
reg clk,tin,rst;
wire q,qbar;
t_ff t1(q,qbar,clk,tin,rst);
initial
clk = 1'b0;
always
#10 clk = ~clk;
initial
begin
rst = 1'b0; tin = 1'b0;
#30 rst = 1'b1;
#10 tin = 1'b1;
#205 tin = 1'b0;
#300 tin = 1'b1;
#175 tin = 1'b0;
#280 rst = 1'b0;
#20 rst = 1'b1;
#280 tin = 1'b1;
#10 ;
end
initial
#2000 $finish;
Endmodule
Simulation Results:
Exercise No. 9.4 Write a Verilog code and test bench to model and simulate a CMOS inverter
using PMOS and NMOS switches.
Solution :
Verilog code:
module inverter
(out,in);
// Declarations of I/O, power and ground lines
output out;
input in;
supply1 pwr;
supply0 gnd;
// Instantiate pmos and nmos switches
pmos (out,pwr,in);
nmos (out,gnd,in);
endmodule
// Testbench for Inverter Module
module inv_test;
wire out ;
reg in ;
// Instantiate inverter Module
inverter i1
(out,in) ;
// Display
task display ;
begin
$display
(
"time=%0d" , $time , " ns"
, " Input=" , in
, " Output=", out
);
end
endtask
// Apply Stimulus
initial
begin
in = 1'b0 ; #10 ; display ;
in = 1'b1 ; #10 ; display ;
in = 1'bx ; #10 ; display ;
in = 1'bz ; #10 ; display ;
end
endmodule
Simulation Results:
Input: z = 1
Output: x = 0
Solution:
Verilog Code:
Simulation Results: