Beruflich Dokumente
Kultur Dokumente
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
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
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.
Class test;
rand byte a[5];
rand byte b;
constraint ab_cons{unique {b,a[0:5]};}
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
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
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
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:
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
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
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
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
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);
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
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;
module override;
packet b_h;
initial begin
b_h = new();
repeat(10)begin
b_h.c_addr.constraint_mode(0);
end
end
nitial #500 $stop;
endmodule
simulation
class dynamic_array;
rand int abc[];
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.