You are on page 1of 14

Questions

Sec paper: Assertions, Randomization, Functional Coverage

1. Given following constraints, which of the following options are wrong?


class packet;
rand logic [15:0] a, b, c;

constraint c_abc { a<c; b==a; c<30; b>25; }


endclass

module constr_blocks;
initial begin
packet pkt;
pkt = new();
repeat(5) begin
pkt.randomize();
$display("\ta = %0d b = %0d c = %0d",pkt.a, pkt.b, pkt.c);
end
end

A: b can be any value between 26 & 29


B: c can be any value between 0 & 29
C: c can be any value between 26 & 29
D: All of the above

Ans: c can be any value between 0 & 29(is wrong)


simulation:

run -all;
# KERNEL: a = 26 b = 26 c = 29
# KERNEL: a = 26 b = 26 c = 29
# KERNEL: a = 26 b = 26 c = 27
# KERNEL: a = 26 b = 26 c = 27
# KERNEL: a = 26 b = 26 c = 29

2. Will there be any difference in the values generated in the following


constraints?
class ABSolveBefore;
rand bit a;
rand bit [1:0] b;
constraint c_ab {
(a == 0) -> b == 0;
solve a before b;
}

class ABSolveBefore;
rand bit a;
rand bit [1:0] b;
constraint c_ab {
(a == 0) -> b == 0;
solve b before a;
}

ANS:

in both cases by default ‘a’ can have value 0 or 1, ‘b’ can have 0,1,2,3.
solve a before b
# KERNEL: a = 1 b = 0
# KERNEL: a = 1 b = 1
# KERNEL: a = 0 b = 0
# KERNEL: a = 1 b = 3
# KERNEL: a = 0 b = 0
if we solve ‘a’, a=0 or 1 is picked with ½ probability each. If ‘a’ = 0 ,‘b’ will be
0.if a=1, ‘b’ has probability of taking 4 values.

solve b before a
# KERNEL: a = 1 b = 1
# KERNEL: a = 1 b = 1
# KERNEL: a = 0 b = 0
# KERNEL: a = 1 b = 1
# KERNEL: a = 1 b = 2
if solve ‘b’ first, b=0,1,2,3 has probaility of1/4. If b=0, ‘a’ is 0 . if b=1, ‘a’ will be
1.

3. What is unique constraint in sv


a unique constraint is used to randomize a group od variables such that no
two members of the group have the same value.

Class test;
rand byte a[5];
rand byte b;
constraint ab_cons{unique {b,a[0:5]};}
endclass

4. For what value of ‘d’, ‘a’ will be less than 2?


class abc;
rand bit [1:0] d;
constraint p {
if(d==4) a < 2;
else a < 4;
}
endclass

A: 00
B: 100
C: 4
D: All of the above
ANS: # KERNEL: d = 1 a = 0
# KERNEL: d = 1 a = 1
# KERNEL: d = 0 a = 3
# KERNEL: d = 1 a = 3
# KERNEL: d = 2 a = 2
# KERNEL: d = 2 a = 3
# KERNEL: d = 3 a = 1
# KERNEL: d = 0 a = 0

5. What is the output of this program?


class c;
rand int a;
function void print();
$display(“a=%0d”, a);
endfunction
endclass
module m;
c c_h;
initial begin
c h = new();
c_h.randomize() with {a==11};
c_h.print();
end
endmodule

A: compile time error


B: any random value
C: 11
D: 10

ANS: # KERNEL: a=11

6. How many bins are created in the below coverpoint?


bit[3:0] var_a;
covergroup test_cg @ (posedge clk);
cp_a: coverpoint var_a {
bins low_bins[] = {[0:3]};
bins med_bins = {[4:12]);
}
endgroup

Ans: 5 bins are created for coverpoint cp_a

four bins are created for low_bins[] where each of the bin value from 0 to 3 for
coverage . One bin is created for med_bins which will look for any values between
4 and 12 for coverage

7. What is the output of this prog?


class a;
rand int ar;
virtual function disp(int ar);
$display("ar %0d", ar);
endfunction
endclass

class b extends a;
rand int br,ar;
function disp(int ar, br);
$display("ar %0d br %0d", ar, br);
endfunction
endclass

module sv_problems();
a a_h;
b b_h;
initial begin
b_h=new();
b_h.ar = 15;
a_h = b_h;
b_h.disp(25,30);
a_h.disp(25,30);
end
endmodule

ANS: "Invalid virtual method override, method is not identical with method
from base class..." "sectest.sv" 46 17
see virtual method declaration from base class

8. What is the output of this program?


module constr_test();
class parent;
rand int unsigned a;
constraint c1{
(a < 10);
}
endclass

class child extends parent;


rand int unsigned a;
constraint c2{
(a > 10);
}
endclass

parent parent_handle;
child child_handle = new();

initial
begin
parent_handle = child_handle;
repeat(10)
parent_handle.randomize();
$display("a = %d",parent_handle.a);
end
endmodule

ANS: parent_handle.randomize();
$display(“a=%d”, parent_handle.a);
# KERNEL: a = 4
# KERNEL: a = 6
# KERNEL: a = 3
# KERNEL: a = 1
# KERNEL: a = 7
# KERNEL: a = 2
# KERNEL: a = 3
# KERNEL: a = 1
# KERNEL: a = 1
# KERNEL: a = 8
child_handle.randomize();
$display(“a=%d”, child_handle.a);

# KERNEL: a = 703395684
# KERNEL: a = 1917131665
# KERNEL: a = 2774526250
# KERNEL: a = 4284694325
# KERNEL: a = 164896079
# KERNEL: a = 3009957482
# KERNEL: a = 971932844
# KERNEL: a = 4273817917
# KERNEL: a = 2399377532
# KERNEL: a = 2281081911
9. What is the output of this code?
module m();
rand int mi;
initial begin
std::randomize(mi) with {mi <100};
$display(“mi = %0d”, mi);
end
endmodule

Ans:
module m();
//rand int mi;
int mi;
initial begin
void'(std:: randomize(mi) with {mi<100;});
$display("mi = %0d",mi);
end
endmodule

simulation:

10. Write clock frequency check assertion

module timem;
timeunit 1ns; timeprecision 100ps;
bit clk, a, b;
initial forever #5 clk=!clk;

property period_chk;
time current_time;
('1,current_time = $time ) ##1
($time -current_time == 14ns);
endproperty
ap_time: assert property(@(posedge clk) period_chk);
initial #200 $stop;
endmodule

green line for current time = 10ns


red line for current time = 14ns (assertion fails)
What is the output of this code?
class a;
rand in ar;
endclass

initial begin
a ah;
ah.ar = 10;
$display(“class interger = %0d”, ah.ar);
end

ANS:
# RUNTIME: Fatal Error: RUNTIME_0029 sectest.sv (12): Null pointer
access.
after creating an object for base class handle
a ah;
ah = new();
ah.ar = 10;

output is:
# KERNEL: class intege = 10
# KERNEL: class intege = 10
# KERNEL: class intege = 10
# KERNEL: class intege = 10

11. What is the output of this code?


class a;
int ar;
endclass

initial begin
a ah = new();
ah.randomize();
$display(“class integer = %0d”, ah.ar);
end

ANS:
# KERNEL: class intege = 0
# KERNEL: class intege = 0
# KERNEL: class intege = 0
# KERNEL: class intege = 0

What is the output of this program?


class a;
rand int ar;
endclass

initial begin
a ah = new();
$display(“class integer = %0d”, ah.ar);
end
ANS:
# KERNEL: class intege = 0
# KERNEL: class intege = 0
# KERNEL: class intege = 0

12. How can you override a constraint? How can you enable/disable a constraint
selectively for a single variable?
Ans:

class base;
rand int addr;
constraint range_p { addr < 50; addr >0;}
endclass

class child extends base;

constraint range_b {addr == 15;} // override

endclass

module override;
child c_h;
base b_h;
initial begin
c_h = new();
b_h = c_h;
c_h.constraint_mode(0); // will turn off ali constraint
$display("all constraint_1 is off addr = %0d", c_h.addr);

// c_h.range_b.constraint_mode(0); //to selectively disable_1 specific


constrai
// $display("to selectively disable_1 specific constraint_1:addr = %0d",
c_h.addr);

repeat(10)begin
if(c_h.randomize())
$display(" randomization successfull : addr = %0d", c_h.addr);
else
$display("random failed");

end
end
endmodule

ANS:
constraint range_b {addr == 15;} // override
# KERNEL: randomization successfull : addr = 15
# KERNEL: randomization successfull : addr = 15
# KERNEL: randomization successfull : addr = 15
# KERNEL: randomization successfull : addr = 15
# KERNEL: randomization successfull : addr = 15

c_h.constraint_mode(0); // will turn off ali constraint


$display("all constraint_1 is off addr = %0d", c_h.addr);

# KERNEL: all constraint_1 is off addr = 0


# KERNEL: randomization successfull : addr = 925303956
# KERNEL: randomization successfull : addr = 353094454
# KERNEL: randomization successfull : addr = 399998160
# KERNEL: randomization successfull : addr = -1436006705

// c_h.range_b.constraint_mode(0); //to selectively disable_1 specific


constrai
// $display("to selectively disable_1 specific constraint_1:addr = %0d",
c_h.addr);

# KERNEL: to selectively disable_1 specific constraint_1:addr = 0


# KERNEL: randomization successfull : addr = 4
# KERNEL: randomization successfull : addr = 34
# KERNEL: randomization successfull : addr = 6
# KERNEL: randomization successfull : addr = 17
# KERNEL: randomization successfull : addr = 32

13. Given a packet class with the following constraints, how can we generate a
packet object with address value > 200?
class packet;
rand bit[31:0] addr;
constraint c_addr { addr inside [0:100];}
endclass

ANS:

class packet;
rand bit[31:0] addr;

constraint c_addr { addr inside {[0:100]};}


endclass

module override;
packet b_h;

initial begin
b_h = new();
repeat(10)begin
b_h.c_addr.constraint_mode(0);

if(b_h.randomize() with {addr > 200;})


$display(" randomization successfull : addr = %0d", b_h.addr);
else
$display("random failed");

end
end
nitial #500 $stop;
endmodule

simulation

# KERNEL: randomization successfull : addr = 925303956


14. # KERNEL: randomization successfull : addr = 353094454
15. # KERNEL: randomization successfull : addr = 399998160
16. # KERNEL: randomization successfull : addr = 2858960591
17. What is wrong with the following constraint?
constraint pkt_c { 0<a<b<c;}

ANS: There can be a maximum of only one relational operator (<,<=,==,>=,


or>) in an expression. If multiple variables need to be in some order,
we will need to write multiple expressions as below.
Constraint pkt_c{0 < a; a<b; b<c;}

18. Write a constraint to generate elements of a dynamic array such that


a. each element is <10 & size of the array is < 10
b. each element is > previous element & size is between 10 & 16

class dynamic_array;
rand int abc[];

constraint c_abc { abc.size()<10; foreach (abc[i]) abc[i] < 10;}

function void pre_randomize();


abc = new[20];
foreach (abc[i])
abc[i] = new();
endfunction
endclass

module tb;
initial begin
dynamic_array Da = new();
if(Da.randomize() == 1)
$display(" array size = %0d elememt[%0d] = %0d", Da.abc.size(),
Da.abc[i]);
end
endmodule
I am getting error sir.

You might also like