0% found this document useful (0 votes)
442 views

System Verilog Assingment 9 PDF

The document contains 6 assignments involving randomization of variables and arrays using SystemVerilog. Each assignment defines a class with random variables, randomizes the class variables a specified number of times, and displays the results. The assignments demonstrate randomizing individual variables, fixing some variables, constraining variable values, and randomizing a dynamic array.

Uploaded by

Divya srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
442 views

System Verilog Assingment 9 PDF

The document contains 6 assignments involving randomization of variables and arrays using SystemVerilog. Each assignment defines a class with random variables, randomizes the class variables a specified number of times, and displays the results. The assignments demonstrate randomizing individual variables, fixing some variables, constraining variable values, and randomizing a dynamic array.

Uploaded by

Divya srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SYSTEM VERILOG ASSINGMENT 9

1. Define a class with variable a and b and generate random value 5 times and
print them. Overwrite the value of a to 55 after randomization
class random_value;
rand bit [7:0] a;
rand bit [7:0] b;
constraint a_limit {a>0;}
constraint b_limit {b>100;}
endclass
module random_numb;
initial
begin
random_value r1;
r1=new();
$display("The random numbers are");
repeat(5)
begin
r1.randomize();
$display("a=%d b=%d",r1.a,r1.b);
end
$display("The random value a after alteration");
r1.a=55;
$display("a=%d",r1.a);
end
endmodule
TRANSCRIPT:
# The random numbers are
# a=101 b=148
# a=246 b=134
# a=221 b=127
# a=208 b=126
# a= 36 b=199
# The random value a after alteration
# a= 55
2. Define a class with variable u, v and w and generate random value of u and v
with fix value of w (=70).
class random_num2;
rand bit [7:0] u,v;
int w=70;
constraint u_limit{u<10;}
constraint v_limit{v<100;}
endclass
module random_numb_2;
initial
begin
random_num2 r2;
r2=new();
$display("The random number");
r2.randomize(u,v);
$display("u=%d, v=%d, w=%d",r2.u,r2.v,r2.w);
end
endmodule
TRANSCRIPT:
# The random number
# u= 5, v= 23, w= 70
3. Define a class with variable a with size of 3. Randomize class 8 times such
that each time a holds a different value.
class random_numb3;
randc bit [2:0] a;
endclass
module random_numb_3;
initial
begin
random_numb3 r3;
r3=new();
$display("The random numbers are :");
repeat(8)
begin
r3.randomize();
$display("a=%d",r3.a);
end
end
endmodule
TRANSCRIPT:
# The random numbers are :
# a=0
# a=1
# a=6
# a=7
# a=4
# a=5
# a=2
# a=3
4. Define a class with variable a & b with size of 3. Randomize class 10 times
such that a should have value 5 or 7 and b should have value between 4 to 6.
class random_num4;
rand bit [2:0] a,b;
constraint a_limit {a inside {5,7};}
constraint b_limit {b inside {[4:6]};}
endclass
module random_num_4;
initial
begin
random_num4 r4;
r4=new();
$display("The random` value are:");
repeat(10)
begin
r4.randomize();
$display("a=%d,b=%d",r4.a,r4.b);
end
end
endmodule
TRANSCRIPT:
# The random` value are:
# a=5, b=5
# a=7, b=5
# a=7, b=6
# a=7, b=6
# a=5, b=6
# a=7, b=4
# a=7, b=6
# a=5, b=5
# a=7, b=4
# a=7, b=5

5. Define a class with variable a & b with size of 3. Randomize class such that if
b has a value 4 or 5 then a should have value 0.
class random_numb5;
rand bit [2:0] a,b;
endclass
module random_num_5;
initial
begin
random_numb5 r5;
r5=new();
$display("The random value for a and b are:");
repeat(10)
begin
r5.randomize(a,b);
if(r5.b==4 || r5.b==5)
r5.a=0;
$display("a=%d,b=%d",r5.a,r5.b);
end
end
endmodule
TRANSCRIPT:
# The random value for a and b are:
# a=0, b=5
# a=6, b=6
# a=1, b=3
# a=0, b=5
# a=0, b=4
# a=0, b=5
# a=0, b=5
# a=0, b=5
# a=5, b=2
# a=0, b=4

6. Define a class with dynamic array a. Randomize class such that each element of
dynamic array a has value greater than 7.

class random_numb6;
int a[];
randc bit [3:0] i;
constraint i_limit {i>7;}
endclass
module random_numb_6;
initial
begin
random_numb6 r6;
r6 = new();
r6.a=new[10];
$display("The elements in the array are:");
for(int j=1;j<10;j++)
begin
r6.randomize(a,i);
r6.a[j]=r6.i;
$display("%d",r6.a[j]);
end
end
endmodule
TRANSCRIPT:
# The elements in the array are:
# 12
# 11
# 15
# 10
# 9
# 14
# 13
# 8
# 15

You might also like