You are on page 1of 26

Verilog programming Assignment 1 submitted by

v.Sireesha
Operators and Operands

Q. Develop a Verilog program to add two operands and store the result in one of the operands
using addition assignment operator.

Code:

module assignment_operands();
integer a =4,b=2;

initial begin
a += b;
$display("%d",a);
a -= b;
$display("%d",a);
a *= b;
$display("%d",a);
a %= b;
$display("%d",a);
a /= b;
$display("%d",a);
end
endmodule

output:
Q. Write a Verilog program to find the maximum of 2 numbers using Conditional operator.

Code:
module ex;
int a = 10;
int b = 16;

reg [0:4] o;

initial begin
o = (a > b) ? a : b;
$display("%d",o);
end

endmodule

output:

Q. Write a menu based Verilog program to perform operations (+, - and *) on matrices.

Code:

module mat(x);
output reg [31:0] x;
reg [7:0] a [0:1] [0:1] ;
reg [7:0] b [0:1] [0:1] ;
reg [7:0] c [0:1] [0:1] ;
integer i,j,k;
initial begin
{a[0][0],a[0][1],a[1][0],a[1][1]} = {8'd1,8'd2,8'd3,8'd4};
{b[0][0],b[0][1],b[1][0],b[1][1]} = {8'd1,8'd2,8'd3,8'd4};
end
always @ (*)
begin
{c[0][0],c[0][1],c[1][0],c[1][1]} = 32'b0;

for(i = 0; i < 2; i++) begin


for(j = 0; j < 2; j++) begin
c[i][j] = a[i][j]+b[i][j]; // for addition
// c[i][j] = a[i][j]-b[i][j]; //for subtraction
$display("%d %d %d",c[i][j],i,j); end
end

end
endmodule

output:

addition:

Subtraction:
Q. Write a program to perform operations on complex numbers.

Code:

// Code your testbench here


// or browse Examples
module complex();
real a,b,c,d,e;
initial begin
assign a = 2;
assign b = 3;
assign c = (-'d1)**0.5; //square root of -1
assign d = b**0.5;
assign e = b*c;
$display("%f + %f",a,b);
//addition operation
$display("%f + %f = %f",a,e,a+e);
//substraction operation
$display("%f - %f = %f",a,e,a-e);
end
endmodule

output:
Q. Write a program to find whether the given processor is little endian or big endian.

Code:

// Code your testbench here


// or browse Examples
module ex;
int a = 'h1234;

byte b;

initial begin
b = a;
if(b == 'h34)
$display("little endian");
else
$display("big endian");
end
endmodule

output:
Basic Data Types

Q. Develop a Verilog program to calculate simple interest using the formula I=PTR/100. Display
Interest with two digit precision after decimal point

Code:

// Code your testbench here


// or browse Examples
module ex;
int P=1200;
real T=12.4;
int R=2;
real SI;

initial begin
SI = (P*T*R)/100;
$display("%.2f", SI);
end
endmodule

output:

Control Sequence

Q. Develop a Verilog program having following logic. If i is 20 or j is 20, display as “Atleast one
variable is having 20” otherwise display “Both variables are not having 20”. If i is less than or
equal to 40 and j is less than or equal to 40, It should display “Both are less than or equal to 40”
otherwise, it should display as “Both are not less than or equal to 40”. Implement this using if-
else statement as well as with conditional operator.
Code:

// Code your testbench here


// or browse Examples
module cs1_a();
reg[6:0] i=7'h20,j=7'h40;

always @(*)
begin
if(i==7'h20 || j==7'h40)
$display("atleast one variable is having 20");
else
$display("both variables are not having 20");
if(i<=7'h40 && j<=7'h40)
$display("both are less than or equal to 40");
else
$display("both are not less than or equal to 40");
end

endmodule

output:

Q. Develop a Verilog program which accepts character type data items from users. In case if user
typed
• ‘A’ or ‘a’, it should display A for Apple
• ‘B’ or ‘b’, it should display B for Bat
• ‘D’ or ‘d’, it should display D for Dog
• ‘F’ or ‘f’, it should display F for Fan

Instead of the above 4 characters, if a user types any other character, it should display “
Character is not in the range”. Implement this using if-else statement and switch statement.

Code:
// Code your testbench here
// or browse Examples
module ex;
reg [6:0] x;

initial begin
assign x = "D" ;
$display("%c",x);
end

always @ (x)
begin
if(x=="a" || x=="A")
$display ("APPLE");
else if(x=="b" || x=="B")
$display("BAT");
else if(x=="c" || x=="C")
$display("CAT");
else if(x=="d" || x=="D")
$display("DOG");
else if(x=="f" || x=="F")
$display("FAN");
else
$display("Character is not in range");
end
endmodule

output:

Q. Develop a Verilog program which adds all numbers from 1 to N, except those which are
divisible by 5. Implement this using for loop and continue statement.

Code:

// Code your testbench here


// or browse Examples
module ex;
parameter N=10;
int count = 0;

initial begin
for(int i=0;i<N;i=i+1) begin
if(i%5 != 0)begin
count = count + i;
$display("%d",count);
end
else
continue;
end
end
endmodule

output:

Q. Develop a Verilog program to find factorial of a number N using for loop.

Code:

// Code your testbench here


// or browse Examples
module factorial();
integer a=4;
integer i,b;
integer fact =1;

initial begin
for(i = 0; i < a;i=i+1) begin
b = a-i;
fact = fact * b;
$display("%d %d",b,fact);
end
end
endmodule

output:

Q. Develop a Verilog program to find the sum of all odd numbers upto N using a while loop.

Code:

// Code your testbench here


// or browse Examples
module sum_odd();
integer n,q;
integer a=0,i;
integer b=0;
initial begin
assign n = 10;
assign i = 0;
while(i<n) begin
assign i = i+2;
assign q = i+1;
$display("odd: %d even:%d",q,i);
a = a + q;
b = b + i;
end
$display("odd_sum:%d even_sum:%d",a,b);
end
endmodule

output:
Q. Write a Program to find if a given number is Armstrong number. Hint: (153 = 1^3 + 5^3 +
3^3)

Code:

// Code your testbench here


// or browse Examples
module cs_7();
reg [9:0] N=10'd153;
reg [9:0] N1=10'd153;
reg [9:0] armstrong;
reg [9:0] r=0;
reg [9:0] num=0;

always@(*) begin
while(1<=N)
begin
r=N%10;
$display("%d",r);
num=num+r*r*r;
$display("%d",num);
N=N/10;
$display("%d",N);
end
assign armstrong=num;
end
always@* begin
if(armstrong==N1)
$display("yes");
else
$display("NO");
end

endmodule

output:
Q. Write a program to find the sum of digits of a given number.

Code:

// Code your design here


module exm1();
integer x=161,sum=0,t,y;
//input [15:0]x;
//output reg [15:0]y=0;
//reg i;

always @ (*) begin


t=x;
while(t>0) begin
y=t%10;
sum=sum+y;
t=t/10;
end
//x=x[i];
// y=y+x[i];
end
always @(sum)
$monitor("%d",sum);
endmodule

Output:

Arrays
Q. Program to print array in reverse order

Code:
// Code your testbench here
// or browse Examples
module arrar();
integer a [0:8] = {1,2,3,4,0,5,6,7,8};
integer i;
integer b [0:8],c[0:8];
initial begin
for(i=0;i<9;i++) begin
//$display("%d",a[i]);
//b[i]=a[i];
c[i]=a[i];
$display("%d\%d",a[i],b[i]);
end
for(i=9;i>=0;i--) begin
$display("%d",c[i]);

end
end
endmodule

output:

5. Program to Calculate Sum and Average of an array

Code:

// Code your testbench here


// or browse Examples
module ex;
static int array[10]='{'h1,'h2,'h3,'h4,'h5,'h6,'h7,'h8,'h9,'h0};
int arr1[10]='{'d101,'d102,'d103,'d105,'d110,'d140,'d150,'d160,'d190,'d180};
int arr_10[10]='{'b1,'b0,'b1,'b1,'b0,'b1,'b1,'b1,'b0,'b1};
int xor_1,xnor_1;
int even_1[$],odd[$];
int even[$];
int sum1,product1,product_1[$];
int unique_ele[$];
int avg;

initial begin

$display("%p",array);
arr1.shuffle;
for(int i=0;i<5;i++) $write("---5 random nums: %0d----",arr1[i]);
array.rsort;
$display("\n5th max num: %d",array[4]);
array.sort;
$display("5th min num: %d",array[4]);
even=array.find with (item%2==0);
$display("even nums: %p",even);
sum1=even.sum;//sum
$display("sum of even nums: %d",sum1); //sum
avg=sum1/$size(even);//avg
$display("avg:%d",avg); //average
product_1=array.find with (item%3==0 && item>1);
$display("elements divisible by 3: %p",product_1);
product1=product_1.product;
$display("product of elements divisible by 3: %d",product1);
xor_1=arr_10.xor;
$display("%d",xor_1);
if(xor_1 ==1)
$display("num of 1's odd");
else
$display("even");
unique_ele=array.unique;
$display("%p",unique_ele);
end
endmodule

output:
Q. Program to find the largest, smallest, second largest and second smallest element of an array
Q. Program to insert an element in a Specified Position in given array
Q. Program to delete the specified Integer from an array

Code for above programs:

// Code your testbench here

// or browse Examples

module ex;

int q[$]={5,7,9};

int q1[$]={1,8,7,10};

int q2[$]={1,2,3,4,5};

int q3[$]={2,9,1,4,6};

int q4[$]={0,9,4,6,5};

int pos=2;

initial begin

q={q[0:pos-1],8,q[pos:$]};//inserting element in sorting list

$display("%p",q);
q1={q1[0:pos-2],5,q1[pos-1:$]};//instertion in unsorted list

$display("%p",q1);

q2={q2[0:$-3],q2[pos+1:$]};//deletion from array

$display("%p",q2);

q3.rsort;

$display("%p",q3);

q3=q3[1:$-3];//second largest num

$display("%p",q3);

q4.sort;

q4=q4[1:$-3];//second min num

$display("%p",q4);

end

endmodule

output:

# KERNEL: ASDB file was created in location /home/runner/dataset.asdb


run -all;
# KERNEL: '{5, 7, 8, 9}
# KERNEL: '{1, 5, 8, 7, 10}
# KERNEL: '{1, 2, 4, 5}
# KERNEL: '{9, 6, 4, 2, 1}
# KERNEL: '{6}
# KERNEL: '{4}
# KERNEL: Simulation has finished. There are no more test vectors to simulate.
exit
# VSIM: Simulation has finished.

Q. Program to Put Even & Odd elements of an array in two separate arrays

Code:

Ans:

// Code your testbench here

// or browse Examples
module tb;

int even[$],odd[$];

int max1[$],min1[$];

int max_even[$],max_odd[$];

int min_even[$],min_odd[$];

initial begin

int s[10]='{'h5,'h6,'h3,'h2,'h8,'h9,'h1,'h4,'h5,'h6};

max1=s.max;

for(int i=0;i<max1.size;i++)$write("%0d",max1[i]);

min1=s.min;

for(int i=0;i<min1.size;i++)$display("\n%0d",min1[i]);

even=s.find with (item%2==0);

$display("%0p",even);

odd=s.find with (item%2==1);

$display("%0p",odd);

max_even=even.max;

for(int i=0;i<max_even.size;i++)$write("\n%0d",max_even[i]);

min_even=even.min;

for(int i=0;i<min_even.size;i++)$write("\n%0d",min_even[i]);

max_odd=odd.max;

for(int i=0;i<max_odd.size;i++)$write("\n%d",max_odd[i]);

min_odd=odd.min;

for(int i=0;i<min_odd.size;i++)$write("\n%d",min_odd[i]);

end
endmodule

output:
# KERNEL: 9
# KERNEL: 1
# KERNEL: '{6, 2, 8, 4, 6}
# KERNEL: '{5, 3, 9, 1, 5}
# KERNEL:
# KERNEL: 8
# KERNEL: 2
# KERNEL: 9
# KERNEL: 1
# KERNEL: Simulation has finished. There are no more test vectors to simulate.

Functions

Q. Write a recursive function to find the factorial of a number.

Code:

// Code your testbench here


// or browse Examples
// Code your testbench here
// or browse Examples
module function_auto ();

function automatic [7:0] factorial;


input [7:0] n;
begin
if (n == 1)
factorial = 1;
else
factorial = n * factorial(n-1);
end
endfunction

initial
begin
$display("%d",factorial(5));
end
endmodule
output:

Bitwise Operations
Q. Write a program to count the number of bits as “1” in an 8 bit number.

Code:

// Code your design here


module count_1s(a,b,c,d,e,f,g,h,out);
input a,b,c,d,e,f,g,h;
output reg [3:0] out;
reg [3:0] x,y,z,w,p,q,r;
always@(*)
begin
x=(a==1'b1)?1:0;
y=(b==1'b1)?x+1:x;
z=(c==1'b1)?y+1:y;
w=(d==1'b1)?z+1:z;
p=(e==1'b1)?w+1:w;
q=(f==1'b1)?p+1:p;
r=(g==1'b1)?q+1:q;

assign out=(h==1'b1)?r+1:r;
end
endmodule

Tb:
// Code your testbench here
// or browse Examples
module tb;
wire [3:0] out;
wire [3:0] x,y,z,w,p,q,r;
reg a,b,c,d,e,f,g,h;
count_1s dut(a,b,c,d,e,f,g,h,out);

initial begin
$monitor("%d",out);
a=1;
b=1;
#2 c=0;
d=1;
#2 e=1;
f=0;
#2 g=1;
h=0;
#10 $finish;
end
endmodule

output:

Q. Write a program in which define a as an unsigned integer whose value is (hexadecimal)


0xa2c3. Write the corresponding bit pattern for this value. Then evaluate each of the following
bitwise expressions, first showing the resulting bit pattern and then the equivalent hexadecimal
value. Utilize the original value of a in each expression. Assume that a is stored in a 16-bit word
3. ~a
4. a ^ 0x3f06
5. a | 0x3f06
6. a | ~0x3f06
7. a >> 3
8. a << 5
9. a ^ ~a
10. a | ~a
11. (a & ~0x3f06) << 8
12. a & ~ (0x3f06 >> 8)

Code:

// Code your testbench here


// or browse Examples
module ex;
reg [15:0] a = 'ha2c3;
reg [15:0] neg,oring,anding,noring,nanding;
reg [15:0] xoring,xnoring,nanding_shift;
reg [15:0] shift_l,shift_r;

initial begin
$display("0x%h",a);
neg = ~a;
oring = a | 16'h3f06;
anding = (a & 16'h3f06);
xoring = a ^ 16'h3f06;
xnoring = a ~^ 16'h3f06;
noring = a |~ 16'h3f06;
nanding = (a &~ 16'h3f06)>>8;
nanding_shift = a &~ (16'h3f06 >> 8);
shift_l = a << 3;
shift_r = a >> 5;
$display("negation:%d or:%d and:%d",neg,oring,anding);
$display("xor:%d Xnor:%d nor:%d nand:%d",xoring,xnoring,noring,nanding);
$display("nanding_shift:%d shift_l:%d shift_r:%d",nanding_shift,shift_l,shift_r);
end
endmodule

output:
Q. Write a Program to unset/clear a bit at nth position in the number.
Q. Write a Program to toggle a bit at nth position in number.
Q. Write a Program to check if the bit at n th position in number is set or not.

Code for above 3 progrms:

// Code your testbench here


// or browse Examples
module ex;
parameter N =4;
reg [N-1:0] a = 4'b0001;
bit set;
bit toggle;

initial begin
set =( a >> N-1 );
$display("%d",set);

/***** code to check set or unset ****/


if(set == 1) begin
$display("set occurs"); end
else begin
$display("unset");end

/******* nth bit setting ********/


if(set == 0) begin //nth bit setting
set = 1;
end

/******* toggling operation **********/


toggle = ((a >> N-1) == 1) ? 0 : 1;

$display("set: %d toggle:%d",set,toggle);
end
endmodule

output:
Q. Write a Verilog program that will illustrate the equivalence between
a. Shifting a binary number to the left n bits and multiplying the binary number by 2 n
b. Shifting a binary number to the right n bits and dividing the binary number by 2 n .

code:

// Code your testbench here


// or browse Examples
module ex;
parameter N = 4;
reg [N-1:0] a = 4'b1011;
// reg [N-1:0] d = 4'b1011;
reg [N-1:0] b,c;
real division,multiplication;

initial begin
b = a << N-1;
c = a >> N-1;
$display("%d",b);
division = (b / 2);
multiplication = (c * 2);
$display("%f %f",division,multiplication);
end
endmodule

output:

You might also like