0% found this document useful (0 votes)
48 views35 pages

DSDV Lab Manual: Verilog Implementations

The document contains Verilog code examples for various digital logic circuits including multiplexers, decoders, flip-flops, counters, comparators, and arithmetic logic units. It provides the code to realize basic gates and functions as building blocks and implement more complex combinational and sequential circuits using behavioral descriptions in Verilog.

Uploaded by

Santosh ub Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views35 pages

DSDV Lab Manual: Verilog Implementations

The document contains Verilog code examples for various digital logic circuits including multiplexers, decoders, flip-flops, counters, comparators, and arithmetic logic units. It provides the code to realize basic gates and functions as building blocks and implement more complex combinational and sequential circuits using behavioral descriptions in Verilog.

Uploaded by

Santosh ub Yadav
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DSDV Lab (BEC302)

1)To simplify given Boolean expression and realize using Verilog


program
(i) Y= a’b’ + ab
(ii) Y=a’b + ab’

(i) module express(a,b,y);


input a, b;
output y;
assign y= ((~a) & (~b)) | ( a & b);
endmodule

(ii) module express(a,b,y);


input a, b;
output y;

ECE Dept. GURU NANAK DEV ENGINEERING


assign y= ((~a) & (b)) | ( a & (~b));

endmodule

2) To realize Adder/Subtractor (Full/Half)


circuits using data flow description.

a)Verilog code for Half adder

module halfadd(a,b,sum,carry);
input a,b;
output sum,carry;
assign sum= a ^ b;
assign carry = a & b;
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


ii. Verilog code for Full adder
module fulladd(a,b,c,sum,carry);
input a,b,c;
output sum,carry;
assign sum= a ^ b ^ c;
assign carry = (a & b)| (b & c) | (c & a);
endmodule

(iii) Verilog code for half subtractor

module halfsub(a,b,diff,borr);
input a, b;
output diff,borr;
assign diff= a ^ b;
assign borr= ((~a) & b);
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


(iv) Verilog program for full subtractor

module fullsub(a,b,c,diff,borr);
input a,b,c;
output diff,borr;
assign diff= a ^ b ^ c;
assign borr= ((~a)&b) | ((~a) &c) | (b & c);
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


3) Verilog code for 4 bit ALU using behavioral description

module alu(a,b, opcode,enable,result);


Input signed [3:0] a,b;------ a3 a2 a1 a0
Input [2:0] opcode;
Input enable;
Output [4:0] result;
reg [4:0] result;
always @ (opcode,a,b,enable)
begin
If (enable==0)
begin
result=4’bX;
end
else

ECE Dept. GURU NANAK DEV ENGINEERING


begin
Case (opcode)
3’b000:result=a +b;
3’b001:result=a-b;
3’b010:result=a+1;
3’b011:result=a-1;
3’b100:result=!a;
3’b101:result=~a;
3’b110:result=a|b;
3’b111:result=a & b;
endcase
result[4]=1’b1;
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


ECE Dept. GURU NANAK DEV ENGINEERING
4) Verilog code for 3:8 Decoder using behavioral
description
module decoder(a, en, d);
input [2:0] a;
input en;
output [7:0] d;
reg [7:0] d;
always @(en, a)
begin
if (en==0)
d<=8'b00000000;
else
case (a)
3'b000:d<=8'b00000001;

ECE Dept. GURU NANAK DEV ENGINEERING


3'b001:d<=8'b00000010;
3'b010:d<=8'b00000100;
3'b011:d<=8'b00001000;
3'b100:d<=8'b00010000;
3'b101:d<=8'b00100000;
3'b110:d<=8'b01000000;
3'b111:d<=8'b10000000;
default:d<=8'b00000000;
endcase
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


ECE Dept. GURU NANAK DEV ENGINEERING
4) Verilog code for 8:3 Encoder without priority
module encoder(i,y);
input [7:0] i;
Output [2:0] y;
reg [2:0] y;
always @ (i)
begin
Case (i)
8’b00000001:y=3’b000;
8’b00000010:y=3’b001;
8’b00000100:y=3’b010;
8’b00001000:y=3’b011;
8’b00010000:y=3’b100;
8’b00100000:y=3’b101;

ECE Dept. GURU NANAK DEV ENGINEERING


8’b01000000:y=3’b110;
8’b10000000:y=3’b111;
default:y=3’bXXX;
endcase
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


6) Verilog code for 8:3 encoder with priority
module encoepr(i,y);
input [7:0] i;
output [2:0] y;
reg [2:0] y;
always @ (i)
begin
if (i[7]==1)
y=3'b111;
else if (i[6]==1)
y=3'b110;
else if (i[5]==1)

ECE Dept. GURU NANAK DEV ENGINEERING


y=3'b101;
else if (i[4]==1)
y=3'b100;
else if (i[3]==1)
y=3'b011;
else if (i[2]==1)
y=3'b010;
else if (i[1]==1)
y=3'b001;
else
y=3'b000;
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


ECE Dept. GURU NANAK DEV ENGINEERING
6)(i) Verilog code for 8:1 multiplexer using case
statement
module mux(i,s,y);
input [7:0] i;
input [2:0] s;
output y;
reg y;
always@ (i,s)
begin
case (s)
3’b000:y=i[0];
3’b001:y=i[1];
3’b010:y=i[2];
3’b011:y=i[3];

ECE Dept. GURU NANAK DEV ENGINEERING


3’b100:y=i[4];
3’b101:y=i[5];
3’b110:y=i[6];
3’b111:y=i[7];
endcase
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


7) Write a verilog code for the following flip flops:
I) D flip flop
ii) JK flip flop
iii)SR flip flop
iv) T flip flop
Verilog code for D flip flop:
module DFF(d,clk,q,qb);
input d,clk;
output q,qb;
reg q,qb;
always @(posedge clk)
begin
q=d;
qb=~q;

ECE Dept. GURU NANAK DEV ENGINEERING


end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


(ii)Verilog code for JK Flip flop:
module JKFF(JK,clk,q,qb);
input clk;
input [1:0] JK;
output q,qb;
reg q,qb;
always @(posedge clk)
begin
case (JK)
2’b00:q=q;
2’b01:q=0;
2’b10:q=1;
2’b11:q=~q;
endcase

ECE Dept. GURU NANAK DEV ENGINEERING


qb=~q;
end
endmodule

9) Verilog code for 4 bit BCD counter

module bcdcounter (clk,rst,q);


input clk,rst;
output [3:0] q;

ECE Dept. GURU NANAK DEV ENGINEERING


reg [3:0] q;
always @(posedge clk)
begin
if (rst)
q=4’b0000;
else if (q<4’b1001)
begin
q=q+1;
end
else if (q==4’b1001)
begin
q=4’b0000;
end
end

ECE Dept. GURU NANAK DEV ENGINEERING


endmodule

10) Verilog code for 4 bit binary counter


Module bincounter(clk,rst,q);
input rst,clk;
output [3:0] q;
reg [3:0] q;
always @(posedge clk)
begin

ECE Dept. GURU NANAK DEV ENGINEERING


if (rst==1’b1)
begin
q=4’b0000;
end
else if (clk)
begin
q=q+1;
end
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


11) Verilog code for T-flip flop
module TFF(t,rst,clk,q,qb);
input t,rst,clk;
output q,qb;
reg q,qb;
always @(posedge clk)

ECE Dept. GURU NANAK DEV ENGINEERING


begin
if (rst)
q=0;
else if (t==0)
q=q;
else if (t==1)
begin
q=~q;
qb=~q;
end
end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


6)(ii)Verilog code for 1:8 demultiplexer
module demux(i, s,y);
input i;
input [2:0] s;
output [7:0] y;
reg [7:0] y;
always @( i,s)
begin
case (s)
3’b000:y[0]=i;
3’b001:y[1]=i;
3’b010:y[2]=i;

ECE Dept. GURU NANAK DEV ENGINEERING


3’b011:y[3]=i;
3’b100:y[4]=i ;
3’b101:y[5]= i;
3’b110:y[6]=i;
3’b111:y[7]=i;
default: y=8’bZZZZZZZZ;
endcase
end
endmodule
Program: Write a verilog code for 4 bit binary to
gray code converter
B3 B2 B1 B0 G3 G2 G1 G0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1

ECE Dept. GURU NANAK DEV ENGINEERING


0 0 1 0 0 0 1 1
0 0 1 1 0 0 1 0
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0 1 1 1 1
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1 1 0 0 0

ECE Dept. GURU NANAK DEV ENGINEERING


module bintogray(B,G);
input [3:0] B;
output [3:0] G;
assign G[3]=B[3];
assign G[2]=B[3] ^ B[2];
assign G[1]=B[2] ^ B[1];
assign G[0]=B[1] ^ B[0];
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


Program: Write a verilog code for 4 bit binary to
Excess-3 converter
B3 B2 B1 B0 E3 E2 E1 E0
0 0 0 0 0 0 1 1
0 0 0 1 0 1 0 0
0 0 1 0 0 1 0 1
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0

ECE Dept. GURU NANAK DEV ENGINEERING


1 0 1 1
1 1 0 0 1 1 1 1

case (b)
4’b0000:e=4’b0011;
6) Verilog program for 2 bit magnitude comparator
module comp (a b, lt, gt, et);
input [1:0] a;
input [1:0]b;
output lt, gt, et;

ECE Dept. GURU NANAK DEV ENGINEERING


reg lt, gt, et;
always @ (a,b)
begin
if (a <b)
lt=1'b1;
else
lt=1'b0;
if (a>b )
gt=1'b1;
else
gt=1'b0;
if (a ==b)
et =1'b1;
else

ECE Dept. GURU NANAK DEV ENGINEERING


et =1'b0;

end
endmodule

ECE Dept. GURU NANAK DEV ENGINEERING


ECE Dept. GURU NANAK DEV ENGINEERING

Common questions

Powered by AI

The primary difference is that the 8:3 Encoder with priority takes into account the highest significant bit that is set to 1 and outputs its corresponding 3-bit code, while the encoder without priority simply converts the given active input to its binary form without any priority consideration. In the priority encoder, the structure checks the highest bit first (i[7] down to i[0]), while the non-priority version just selects the binary equivalent of the single active input .

The behavioral differences between a Full Subtractor and a Half Subtractor in Verilog stem from their input configurations and handling of the borrow bit. A Half Subtractor only uses two inputs and computes the difference and borrow (using input a and b without considering an initial borrow), whereas a Full Subtractor uses three inputs to account for an initial borrow bit (a, b, and c), computing the difference as a ^ b ^ c and the borrow using the logic ((~a)&b) | ((~a)&c) | (b&c).

The design differences between a 1:8 Demultiplexer and an 8:1 Multiplexer in Verilog lie in their functionality and input-output configurations. A Demultiplexer takes a single input and channels it to one of the multiple outputs based on a select line, represented in Verilog by routing the input to a case-selected output line (y[s] = i). Conversely, a Multiplexer selects one of multiple inputs to forward to a single output based on select lines, described by assigning the output y the value of the input line indicated by the select signal (y = i[s]). This reflects their roles in routing data from one-to-many vs. many-to-one configurations .

A 4-bit Binary Counter implemented in Verilog operates by incrementing its 4-bit output q by 1 on each positive edge of the clock, unless the reset input rst is active. If rst is 1, the counter resets q to 4'b0000. This increment operation occurs within a procedural always block that conditions on the timing of the clock signal .

The Verilog code for a 3:8 Decoder functions by taking a 3-bit binary input and producing an 8-bit output where only one bit is high based on the binary input, provided the enable input is active. If enable is 0, all outputs are 0. When enable is 1, the decoder checks the binary input: for example, if the input is 3'b001, the output becomes 8'b00000010, as indicated in the case statement .

In the 4-bit ALU Verilog design, different opcode values determine the specific arithmetic or logical operation to be performed on the 4-bit inputs a and b. For instance, opcode '3'b000' performs addition (result = a + b), '3'b001' performs subtraction (result = a - b), and so on up to '3'b111', which performs AND operation (result = a & b). These are selected using a case statement conditional on the 3-bit opcode .

The reg data type in Verilog is significant for the behavioral description of sequential circuits because it can store values across discrete time points, essential for modeling the memory elements of circuits. Used within always blocks, reg types can hold values sampled at clock edges, enabling them to represent the state of flip-flops and other storage elements, facilitating complex event-driven simulations. This allows designers to model flip-flops and latches that update their state based on clock signals and event conditions .

The logic used to implement the borrow output in a Full Subtractor in Verilog involves a combination of NOT and AND operations to ensure that the borrow bit is set when necessary. Specifically, the borrow is calculated using the expression ((~a)&b) | ((~a)&c) | (b&c), where each term represents conditions under which a borrow would occur from an additional bit position .

The Verilog code for a 4-bit Binary to Gray Code Converter derives the gray code by applying bitwise XOR operations between adjacent bits of the binary input. Specifically, the most significant bit remains the same (G[3]=B[3]), while each subsequent bit is obtained by XOR of the current bit and the previous one (e.g., G[2] = B[3] ^ B[2], G[1] = B[2] ^ B[1], G[0] = B[1] ^ B[0]).

The Full Adder in Verilog data flow description is implemented by using bitwise operations to calculate the sum and carry outputs. The sum is calculated as an XOR operation of the three inputs a, b, and c (sum = a ^ b ^ c). The carry is calculated as the OR operation of the AND operations of each pair of inputs ((a & b) | (b & c) | (c & a)).

You might also like