Aloha friend, in this blog post we will see some interview questions on systemverilog constraints.
Q1 : Give 3 ways to change/override constraints in given example.
Explanation : you have constraint called addr_range in class packet as shown below, give 3 ways in which you can make addr < 100.
class packet;
rand bit[7:0] addr;
constraint addr_range{addr>100;}
endclass
// ----------------------------- 1st way -----------------
class packet;
rand bit[7:0] addr;
constraint addr_range{addr>100;}
function void print();
$display("\t Adrr=%0d",addr);
endfunction
endclass
class child_packet extends packet;
constraint addr_range{addr<100;}
endclass
module tb;
child_packet pkt;
initial begin
pkt=new();
$display("---------------------------------------");
repeat(10) begin
pkt.randomize();
pkt.print();
end
$display("---------------------------------------");
end
endmodule
/* output
# KERNEL: ---------------------------------------
# KERNEL: Adrr=70
# KERNEL: Adrr=22
# KERNEL: Adrr=57
# KERNEL: Adrr=80
# KERNEL: Adrr=89
# KERNEL: Adrr=58
# KERNEL: Adrr=92
# KERNEL: Adrr=20
# KERNEL: Adrr=67
# KERNEL: Adrr=73
# KERNEL: ---------------------------------------
*/
// ----------------------------- 2nd way -----------------
class packet;
rand bit[7:0] addr;
constraint addr_range{soft addr>100;}
function void print();
$display("\t Adrr=%0d",addr);
endfunction
endclass
module tb;
packet pkt;
initial begin
pkt=new();
$display("---------------------------------------");
repeat(10) begin
pkt.randomize()with{addr<100};
pkt.print();
end
$display("---------------------------------------");
end
endmodule
/* output
# KERNEL: ---------------------------------------
# KERNEL: Adrr=70
# KERNEL: Adrr=22
# KERNEL: Adrr=57
# KERNEL: Adrr=80
# KERNEL: Adrr=89
# KERNEL: Adrr=58
# KERNEL: Adrr=92
# KERNEL: Adrr=20
# KERNEL: Adrr=67
# KERNEL: Adrr=73
# KERNEL: ---------------------------------------
*/
// --------------------------- 3rd way ---------------------
class packet;
rand bit[7:0] addr;
constraint addr_range{addr>100;}
function void print();
$display("\t Adrr=%0d",addr);
endfunction
endclass
module tb;
packet pkt;
initial begin
pkt=new();
$display("Disable constraint : pkt.addr_range.constraint_mode(0) ");
pkt.addr_range.constraint_mode(0);
$display("---------------------------------------");
repeat(10) begin
pkt.randomize()with{pkt.addr<100;};
pkt.print();
end
$display("---------------------------------------");
end
endmodule
/* output
# KERNEL: Disable constraint : pkt.addr_range.constraint_mode(0)
# KERNEL: ---------------------------------------
# KERNEL: Adrr=70
# KERNEL: Adrr=22
# KERNEL: Adrr=57
# KERNEL: Adrr=80
# KERNEL: Adrr=89
# KERNEL: Adrr=58
# KERNEL: Adrr=92
# KERNEL: Adrr=20
# KERNEL: Adrr=67
# KERNEL: Adrr=73
# KERNEL: ---------------------------------------
*/
Q2: Write a constraint which generate 10 unique numbers in array size of 10.
class packet;
rand bit[7:0] array[];
constraint array_size{array.size==10;};
constraint unique_array{unique{array};
foreach(array[i]){
array[i]<10;
}
};
function void print();
$display("\t array=%p",array);
endfunction
endclass
module tb;
packet pkt;
initial begin
pkt=new();
$display("-------------------------------------------------");
repeat(5) begin
pkt.randomize();
pkt.print();
end
$display("-------------------------------------------------");
end
endmodule
/* output
# KERNEL: -------------------------------------------------
# KERNEL: array='{3, 9, 8, 6, 4, 0, 1, 2, 7, 5}
# KERNEL: array='{1, 4, 2, 7, 8, 0, 6, 3, 9, 5}
# KERNEL: array='{7, 9, 2, 0, 5, 1, 8, 6, 3, 4}
# KERNEL: array='{9, 8, 1, 7, 6, 5, 3, 4, 0, 2}
# KERNEL: array='{7, 8, 9, 3, 1, 6, 2, 4, 5, 0}
# KERNEL: -------------------------------------------------
*/
Q3: Write a constraint which generate even sequence (0,2,4,6,8...) without using array or queue.
class packet;
rand bit [7:0]addr;
bit is_even;
function void print();
$display("\t Addr=%0d",addr);
endfunction
constraint constraint_addr_even{ addr==addr_even();}
function bit[7:0] addr_even();
static int unsigned count=0; // fro odd sequence make count=1;
addr_even=count;
count=count+2;
endfunction
endclass
module tb;
packet pkt;
initial begin
pkt=new();
$display("---------------------------------------");
repeat(10) begin
pkt.randomize();
pkt.print();
end
$display("---------------------------------------");
end
endmodule
/* output
# KERNEL: ---------------------------------------
# KERNEL: Addr=0
# KERNEL: Addr=2
# KERNEL: Addr=4
# KERNEL: Addr=6
# KERNEL: Addr=8
# KERNEL: Addr=10
# KERNEL: Addr=12
# KERNEL: Addr=14
# KERNEL: Addr=16
# KERNEL: Addr=18
# KERNEL: ---------------------------------------
*/
Q4 : Generate stimulus in which there is only one "1" in vector of width 8. i.e onehot input.
class packet;
rand bit [7:0] in;
randc bit [2:0] n;
bit [7:0] map[8]={8'b0000_0001,
8'b0000_0010,
8'b0000_0100,
8'b0000_1000,
8'b0001_0000,
8'b0010_0000,
8'b0100_0000,
8'b1000_0000};
function void print();
$display("\t in = %8b",in);
endfunction
constraint c_block{in==map[n];}
endclass
module tb;
packet p;
initial begin
repeat(20) begin
p=new();
assert(p.randomize());
if($onehot(p.in)) $display("\t onehot pass ");
else $display("\t onehot fail ");
p.print();
#5;
end
end
endmodule
/* output result
onehot pass
in = 10000000
onehot pass
in = 00000001
onehot pass
in = 00100000
onehot pass
in = 00000010
onehot pass
in = 00000001
onehot pass
in = 10000000
onehot pass
in = 00000001
onehot pass
in = 01000000
onehot pass
in = 00000001
onehot pass
in = 00000100
onehot pass
in = 00000001
onehot pass
in = 00000010
onehot pass
in = 01000000
onehot pass
in = 00010000
onehot pass
in = 10000000
onehot pass
in = 10000000
onehot pass
in = 00001000
onehot pass
in = 00100000
onehot pass
in = 10000000
onehot pass
in = 00000001
V C S S i m u l a t i o n R e p o r t
LINK : https://www.edaplayground.com/x/eKuY
*/
Thank you for reading i will upload more examples on the topic.
Have a nice day😇😇
You Might Also Like