0% found this document useful (0 votes)
1K views

Constraint Examples

Uploaded by

194g1a0496
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)
1K views

Constraint Examples

Uploaded by

194g1a0496
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/ 33

>constraint to genarate FACTORIAL of first 5 even numbers

class factt;
rand int num[];
constraint size{num.size ==5;}
constraint fact_num{ foreach(num[i])

num[i]==fact((i+1)*2);} //even
//num[i]==fact(((i+1)*2)-1);} //odd

function int fact(int j);


if (j==0)
fact =1;
else
fact = j*fact(j-1);
endfunction
endclass

module factorial;
factt f =new();
initial
begin
assert(f.randomize);
$display("%p",f);
end
endmodule
=============================================================================
==========
constraint to genarate prime numbers

class prime_numbers;
// parameter RANGE=1000;// this will give all prime number with in 1000, we
can give any range here.......!!!

rand int prime[];


constraint const_prime {prime.size ==10;}
constraint c{ foreach(prime[i])
if (i>1 && (!((i%2==0 && i!=2) || (i%3==0 &&
i!=3) || (i%4==0 && i!=4) || (i%5==0 && i!=5) ||
(i%6==0 && i!=6) || (i%7==0 && i!=7) ||
(i%8==0 && i!=8) || (i%9==0 && i!=9))))
prime[i]==i;
else
prime[i]==2;
}

function void post_randomize;


prime = prime.unique();
endfunction

endclass

module top;
prime_numbers arr;

initial
begin
arr=new();
assert(arr.randomize());
$display("the prime: %p",arr.prime);
end
endmodule

=============================================================================
=========================
>write a constraint to generate below pattern 01010101...
class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])
if(i%2==0)
a[i]==0;
else
a[i]==1;}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule
_____________________________________________________________________________
_________________________

>write a constraint to generate below pattern 1234554321


class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])
if(i<5)
a[i]==(i+1);
else
a[i]==(10-i);}

endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule
_____________________________________________________________________________
_________________________
>write a constraint to generate below pattern 5 -10 15 -20 25 -30
class packet;
rand int a[];
constraint size{a.size==18;}
constraint more{foreach (a[i])
if(i<=5)
a[i]==5+i;
else if(i<=11)
a[i]== 9+i;
else
a[i]==13+i;

}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule

_____________________________________________________________________________
_________________________
>constraint to generate the below pattern 9 19 29 39 49 59 69 79 a[];
class packet;
rand int a[];
constraint size{a.size==8;}
constraint more{foreach (a[i])

a[i]==(i*10)+9;}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule

_____________________________________________________________________________
_________________________
>constraint to generate the below pattern 9 7 5 3 1 8 6 4 2 0
class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])
if(i<5)
a[i]==10-(((i+1)*2)-1);
else
a[i]==20-(i+1)*2;}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule

_____________________________________________________________________________
_________________________
>constraint to generate the below pattern 7 17 27 37 47 57 67
class packet;
rand int a[];
constraint size{a.size==7;}
constraint more{foreach (a[i])

a[i]==(i*10)+7;}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule

_____________________________________________________________________________
_________________________
>constraint to generate the below pattern 1 2 4 3 5 6 8 7
class packet;
rand int a[];
constraint size{a.size==7;}
constraint more{foreach (a[i])

a[i]==(i+1);}

endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule
_____________________________________________________________________________
_____________________________
> Write a constraint to generate the following pattern 1122334455 in an array
named da[ ]
class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])

a[i]==(i+2)/2;}

endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt);
end
endmodule

_____________________________________________________________________________
_____________________________

>Write a code to generate a random number between 1.35 to 2.57


module tb;

class real_num;

rand int r_num[];

real num[10];

constraint size1{r_num.size==10;}
constraint real_num{foreach (r_num[i])
r_num[i] inside {[1350:2570]};}

function void post_randomize();


foreach (num[i])
begin
num[i]=r_num[i]/1000.0;
$display("Number =%f", num[i]);
end
endfunction
endclass

real_num rn=new();
initial
begin

rn.randomize();
end
endmodule
_____________________________________________________________________________
_____________________________
>Write constraint to generate random values 25,27,30,36,40,45 without using
"set membership".

module constraint_check();
class con_check;
rand int a;
constraint range {a>24;a<46;(a%9==0)||(a%5==0);a!=35;}
endclass
initial
begin
con_check ch;
ch=new();
repeat(15)
begin
assert(ch.randomize());
$display("a=%d",ch.a);
end
end
endmodule
=============================================================================
=========
>Write a code to generate 0102030405

class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])
if(i%2==0)
a[i]==0;
else
a[i]==(i+1)/2;}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt.a);
end
endmodule
_____________________________________________________________________________
____________________________
>constraint to generate the below pattern 9 7 5 3 1 8 6 4 2 0

class packet;
rand int a[];
constraint size{a.size==10;}
constraint more{foreach (a[i])
if(i<5)
a[i]==10-((i*2)+1);
else
a[i]==18-(i*2);
}
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt.a);
end
endmodule
=============================================================================
=========================
write a constraint to generate a random even number between 50 and 100?

class packet;

rand bit[6:0] array[10];


constraint elements { foreach (array[i])
array[i] inside {[50:100]};}

constraint even { foreach(array[i])


array[i]%2==0;} // even genartion

//constraint odd { foreach(array[i])


//array[i]%2!=0;} // odd generation

endclass

module test;
packet pkt;
initial begin
pkt=new;
repeat(5)
begin
pkt.randomize();
$display(" the values of odd and even %p",pkt);
end
end
endmodule
_____________________________________________________________________________
_________________________

>constraint to generate 021346578

class packet;
rand int a[];
int j=0,k=0;

constraint size{a.size==9;}
constraint C1{foreach (a[i])
a[i]==fun(i);}
function int fun(int i);
if(i%2==0)
begin
if(j==1)
begin
fun=i-1;
j=0;
end
else
begin
fun=i;
j++;
end
end
else
begin
if(k==0)
begin
fun=i+1;
k=1;
end
else
begin
fun=i;
k=0;
end
end
endfunction
endclass

module tb;
initial
begin
packet pkt;
pkt=new();
pkt.randomize();
$display("OUTPUT = %p",pkt.a);
end
endmodule

=============================================================================
=================

1) Write a constraint sunch that array size between 5 to 10 values of the


array are in asscending order ?
2) Write a constraint sunch that array size between 5 to 10 values of the
array are in descending order ?

module even();
class eve;
rand bit[6:0] a[];

constraint a1 { a.size inside {[5:10]};}


// constraint a2 {foreach(a[i]) if (i>0) a[i]<a[i-1];} // vlaues of
decending order
constraint a3 {foreach(a[i])
if (i>0)
a[i]>a[i-1];} // vlaues of ascending order
endclass
initial
begin
eve p1=new;
repeat(5)
begin
assert(p1.randomize());
foreach(p1.a[i])
$display( " [%0d] %0d " ,i , p1.a[i]);
$display(" %p %0d ", p1,p1.a.sum());
end
end
endmodule

=============================================================================
==================
I'm having 16 bit of variable , only single bit has to acess how to use of
constraints ?
ex : 4 100
8 1000
16 10000

// Code your testbench here


// or browse Examples
class abc;
rand bit [31:0] var1;
rand bit [31:0] c1;

//constraint C11{c1==1<<var1;} // 1st view


//constraint c2{ $onehot(c1)==1;} // 2nd view
constraint c2{ $countones(c1)==1;} // 3rd view
//constraint c2 {(c1 & (c1-1)) == 0; c1 !=0; }

endclass

module bb();

abc ab_h;
initial begin
ab_h = new();
repeat(10) begin
ab_h.randomize();
$display("values are %0d %b",ab_h.c1,ab_h.c1);
end
end
endmodule

=============================================================================
===================
Write a constraint to genarate multiples of 2 power ?

// Code your testbench here


// or browse Examples
class addr2power;
rand bit[7:0] addr;
randc bit[2:0] add2;
constraint ADDR { addr==2**add2;} // differnt way to approch
// constraint power_of_2{ addr != 0; (addr & (addr-1)) == 0;} // other
way to approch
endclass
module mmmm;
initial begin
addr2power a_h;
a_h=new();
repeat(10)
begin
a_h.randomize();
// $display("addr=%d add2=%d",a_h.addr,a_h.add2);
$display("%0d", a_h.addr);
end
end
endmodule

=============================================================================
===================
1) Write a constraint to generate consecutive elements using Fixed size array
?
same way how to generate for non conescutive elements.

// Code your testbench here


// or browse Examples
module test1;
class test;
rand bit [3:0] arr[10]; // Fixed size array
constraint c2 { foreach(arr[i]) arr[i]==i;} //conesctive elements
//constraint c1 { foreach(arr[i]) // fixed size array non
consectives elements
// foreach(arr[j])
// if(i!=j)
// arr[i]!=arr[j];}

//constraint c2{unique {arr};}


function void post_randomize;
$display("array : %p",arr);
endfunction
endclass

initial begin
test t1;
t1=new();
repeat(10)
begin
t1.randomize();
$display("%p", t1.arr);
end
end
endmodule

=============================================================================
=====================

2) Write a constraint to genrate consecutive elements using Dynamic array ?


same way how to generate for non consecutive elements .
module bp;
class good_sum5;
rand bit[7:0] A[];

// constraint c_len {foreach(A[i]) A[i] inside {[10:15]}; }


// constraint c1 {A.size inside {[0:5]};}
// constraint c2 { foreach(A[i]) foreach(A[j]) if(i!=j) A[i] != A[j];
}
constraint c2 { A.size() inside {[2:15]}; foreach(A[i]) A[i] inside
{[0:15]}; foreach(A[i]) foreach(A[j]) if(i!=j) A[i] != A[j]; }

endclass

good_sum5 kiran;
initial begin
kiran=new();
repeat(6) begin
kiran.randomize();
$display("%p , %d, %t", kiran.A ,kiran.A.size(),$realtime);
end
end
endmodule

=============================================================================
====================
1)Write a Constraint to generate Unique elements without unique keyword
Case1: Try to get Unique size of elements using dynamic array?
class packet;
rand bit [5:0] array [];
randc int c;

//constraint size_array {array.size inside {[0:20]};} //Size


of the elements should repeat these case
constraint size_array {c inside {[4:20]}; array.size == c;} // it
wont repeat size of the array because used randc
constraint elements { foreach (array[i])
array[i] inside {[0:64]};}
constraint abc {foreach(array[i])
foreach(array[j])
if (i!=j)
array[i]!=array[j];}
endclass: packet

module foreach_constraint;
packet pkt = new;
initial
begin
repeat (15)
begin
assert(pkt.randomize());
$display("\nThe size of the array is %0d",pkt.array.size());
$display("elements of the array = %0p",pkt.array);
end
end
endmodule

=============================================================================
====================
2) How to delate/remove duplicate elements using Queue?
3) How to delate/remove duplicate elements using Associative array?

module test;
int q_int[$];
int Dup_a[int];

//int age[string];
//string dup_aa1[int];

initial begin
q_int = {600,1000,600,200,400,600,700,700,900};

foreach(q_int[i])
Dup_a[q_int[i]]++;

q_int = {};
foreach(Dup_a[i])
q_int.push_back(i);
$display("After Deletion Duplicate array =%p",q_int);
end
endmodule

////*********How to remove duplicate elements from Assocaitive array


**************//

/*

module test;
//int q_int[$];
//int dup_aa[int];

int age[string];
string dup_aa2[int];

initial begin
age["prashu1"] = 32;
age["prashu2"] = 4;
age["prashu3"] = 31;
age["prashu4"] = 2;
age["prashu5"] = 4;
age["prashu6"] = 31;

foreach(age[name])
dup_aa2[age[name]] = name;
age.delete();
$display("After Deletion Duplicate array ===== ");
foreach(dup_aa2[num])
age[num] = dup_aa2[num];
foreach(age[name])
$display("age[%0s]=%0d",age[name],name);
end
endmodule
*/
=============================================================================
======================
1) Write a constraint to generate even numbers should in the range of 10 t0
30 using Fixed size array, Dynamic array & Queue.
2) Write a constraint to generate odd number should in the range of 10 to 30
using Fixed size array, Dynamic array &Queue.

class vish;
// rand bit[31:0] a;
// rand int a[5];
rand bit[4:0] array[10]; // fixed size array

constraint odd_even { foreach(array[i]) if(i%2==0) array[i]%2 !=0; else


array[i]%2 ==0;} // odd even genration for fixed size array
// constraint even { foreach(array[i]) array[i]%2==0;} // even
genartion
// constraint odd { foreach(array[i]) array[i]!2=0;} // odd generation

endclass

module test;
vish v;
initial begin
v=new;
repeat(10)
begin
v.randomize();
$display(" the values of odd and even %p",v);
end
end
endmodule

=============================================================================
=====================
3)Write a constraint to generate both even and odd number using Fixed size
array Dynamic array & Queue.
// Code your design here
class prashu;

// rand bit[7:0] b[10]; // fixed size array


rand bit[7:0] b[]; // dyanamic array
rand bit en;

constraint abc1 { b.size() inside {[1:10]};}


//if using DA enable this one
// constraint odd { foreach(b[i]) b[i] inside {[10:20]}; foreach(b[i])
b[i]%2!=0;} // odd number genartion betwwen 10 to 30
//constraint even { foreach(b[i]) b[i] inside {[10:30]}; foreach(b[i])
b[i]%2==0;} // even number genartion between 10 to 30
constraint odd_even { foreach(b[i]) b[i] inside {[10:40]};
foreach(b[i]){ if(en) b[i]%2==0; else b[i]%2!=0;} } // both even and
odd genartion between 10 to 30
constraint odd_even_2 { foreach(b[i]) if(i%2==0) b[i]%2 !=0;else
b[i]%2 ==0;} // other way to print odd & even
endclass
module test;
prashu v;
initial begin
v=new;
repeat(20)
begin
v.randomize();
$display("%p",v.b);
foreach(v.b[i])
$display(" the value of %0d %0b %0h", v.b[i],v.b[i],v.b[i]);

end
end
endmodule

=============================================================================
========================

1) Write a constraint to generate a variable with 0-31 bits should be 1, 32-


61 bits should be 0.

class prashu;
rand bit [61:0] num;

constraint abc {foreach(num[i])


if(i>=0&&i<32)
num[i]==1'b1;
else if(i>31&&i<62)
num[i]==1'b0;}

function void post_randomize();


$display("num= %d %b",num, num, );
endfunction

endclass

module test;
prashu v;

initial begin
v=new;
v.randomize();
end
endmodule
=============================================================================
======================

2) If we randomize a single bit variable for 10 times values should generate


be like 101010101010.

class prashu;
rand bit a;
static bit b=0;

constraint abc{a!=b;} //to get toggled otput


function void post_randomize();
$display("a=%d, b=%d",a,b);
b=a;
endfunction
endclass

module test;
prashu v;
initial begin
v=new;
repeat(10)
begin
v.randomize();
end
end
endmodule
=============================================================================
======================

3) we have a randomize variable with size of 32 bit data, but randomize only
12th bit.
class prashu;
randc bit[31:0] a;

constraint abc{ foreach(a[i])


if(i==12)
a[i] inside{0,1};
else
a[i]==0;} //to get toggled otput

endclass

module test;
prashu v;
initial begin
v=new;
repeat(10)
begin
v.randomize();
$display(" a= %d a=%b",v.a, v.a);
end
end
endmodule
=============================================================================
======================
1) Write a constraint for 2D 3D for dynamic array to print consecutive
elements
class ABC;
rand bit[4:0] md_array [][]; // 2d array
// rand bit[4:0] md_array [][][]; // 3d array

////**************below code for 2d dimentional *************//

constraint c_md_array {md_array.size inside {[1:5]};


foreach (md_array[i]) { md_array[i].size() inside
{[3:5]}; // 1d
// foreach (md_array[i][j]) { md_array[i][j] inside
{[10:30]};}}} between 10 to 30 elements enable these constraint
foreach (md_array[i][j]) { md_array[i][j]
==i+j;}}} // to access conscustive elements

///**************below code for 3d dimentional***********//


/*
constraint c_md_array {md_array.size inside {[1:5]};
foreach (md_array[i]) { md_array[i].size() inside
{[3:5]}; // 1d
foreach (md_array[i][j]) { md_array[i][j].size()
inside {[1:3]};
foreach (md_array[i][j][k]) { md_array[i][j][k]
==i+j+k; } } }}

*/

endclass

module tb;

initial begin
ABC abc = new;
repeat(5)
begin
abc.randomize();
/*
foreach(abc.md_array[i,j])
abc.md_array[i][j] = $random;
foreach(abc.md_array[i,j])
$display("\td_aaray[%0d,%0d] = %0d",i,j, abc.md_array[i][j]);
*/
$display ("md_array = %p", abc);
end
end
endmodule
=============================================================================
============================
2) Identify how many number of zeros count in a array / number of ones count
in a array
class test;
rand bit [7:0] value;
bit [3:0] num,count;
endclass

module abc;
test t = new;

initial begin

repeat(5)
begin
t.randomize();
foreach(t.value[i])
if(!t.value[i]) // if(t.value[i]) gives number of ones
count
t.count=t.count+1;
$display("value : %b,\t number of zeros : %d", t.value, t.count);
t.count=0;
end
$display("------another way---------");
repeat(5)
begin
t.randomize();
t.num = $countones(t.value);
t.count = $size(t.value) - t.num; //size of array
$display("value : %b,\t number of zeros : %d", t.value, t.count);
end

end
endmodule
=============================================================================
===============================

3) Define constraint should be even number in odd location, odd number in


even location
module test;
class a;
rand bit[3:0] k[];
constraint b {k.size() inside {[4:20]};}
constraint c { foreach(k[i])
{ if (i%2==0) // even location
k[i]%2==1; // printing odd number
else if (i%2==1) //odd location
k[i]%2==0;} } //Printing even number
endclass
initial
begin
a a1=new;
repeat(5)
begin
assert(a1.randomize());
foreach(a1.k[i])
$display(" the value of locations even and odd %0d %0d" , i,
a1.k[i]);
$display("%p",a1.k);
end
end
endmodule
=============================================================================
=============================
1) How to differentiate use of For loop vs Foreach loop
packed array of 1D , 2D & 3D one hot encoding .

// bit [7:0] m_data; // 1_d


//bit [3:0][7:0] m_data; // 2_d
bit [2:0][3:0][7:0] m_data; // 3d

initial begin
int i,j,k;
// m_data =8'hab; // 1 d
// m_data= 32'b11111010110011101100101011111110 ; // 2d

m_data=32'habcdefef; // 3d

m_data[0] = 32'hface_cafe;
m_data[1] = 32'hbace_fdab;
m_data[2] = 32'hfdfd_abab;

////***********perofrm for loop please enable below statements


**************//

// 2. Iterate through each bit of the vector and print value


begin
for (int i = 0; i < $size(m_data); i++)
$display (" for loop m_data[%0d][%0d][%0d] = %0b, %0h", i, j,k,
m_data[i][j][k], m_data[i][j][k]); // 3d dimenstional
// $display ("m_data for loop [%0d] = %ob %0d", i, m_data[i],
m_data[i]); // 1 D dimentional

end
//*************Pefrom foreac loop enable below statements*************//
begin

// foreach(m_data[i]) //1 D dimenrtional


// $display ("m_data[%0d] = %d ", i, m_data[i]);

// foreach(m_data[i,j]) //2 D dimentional


// $display ("m_data[%0d][%0d] = %b %0h", i,j, m_data[i][j],
m_data[i][j]);

foreach(m_data[i,j,k]) // 3D dimentional
$display ("for each m_data[%0d][%0d][%0d] = %0h %0d", i,j,k,
m_data[i][j][k], m_data[i][j][k]);

$display(" m_data %p", m_data);


// $display ("m_data[%0d][%0d] = %b %0h", i,j , m_data[i][j],
m_data[i][j]);
end
end
endmodule
=============================================================================
=============================
2) Write code for converting 32bit data to 8bit serial interface data /32 bit
array to 8 bit array assignment .
Example : 12345678 32 bit data
10010001101000101011001111000
split into 8 bits
78 1111 1000
56 0101 0110
34 0011 0100
12 0001 0010

module data_on_strobe;
int abc [];
bit [8:0] dcd [];
initial begin

abc = new[4];
dcd = new[abc.size() * 4];
foreach(abc[i])
abc[i] = 32'h12345678* (i+1);
foreach(abc[i])
$display("acdf[%0d] %0b %0h",i,abc[i] , abc[i]);
for(int i=0,j=0,k=0;
i<dcd.size();i++)
begin
dcd[i] = abc[k][8*(j) +: 8];
j++;

if(j % 4 == 0) begin
k++;
end
end

foreach(dcd[i])

$display("dcd[%d] %0b %0h ",i,dcd[i], dcd[i]);


end
endmodule

//-------------------------------------------------------------------------
//variable part-select with fixed width: Data[8*(i) +: 8]
//If i is 0 then Data[8*(0) +: 8] == Data[7:0]
//If i is 3 then Data[8*(3) +: 8] == Data[31:24]
//-------------------------------------------------------------------------
/*
//below code excuted by me

module A;
int abc[]; //32 bit
bit[7:0] def[]; // 8 bit
//int j,l;
initial begin
abc = new[4];
def = new[abc.size*4];

foreach(abc[i])
abc[i] = 32'h12345678* (i+1);
foreach(abc[i]) $display("The values of 32 bits are
abc[%0d]=%b",i,abc[i]);

for(int i=0,j=0;i<4;i++)
begin
for(int k=j,l=0;k<(4+j);k++)
begin

//def[k] = abc[i][((8*l)+(8-1)):(8*l)];
def[k] = abc[i][8*(l)+:8];
l++;
end
j=j+4;
end
foreach(def[i]) $display("The values of 8 bits are abc[%0d]=%b",i,def[i]);
end
endmodule

*/
=============================================================================
================
Interview Prepartion #systemverilog #packed_array #unapcked_array
#1D_2D_3D_4D
#For_loop
#Foreach_loop
======================
Packed array 1D, 2D
======================
module tb;
// bit [3:0][7:0] stack [1:0][3:0]; // 2 rows, 4 cols
byte stack[3:0][7:0];

initial
begin
// Assign random values to each slot of the stack
foreach (stack[i])
// begin
// stack[i]=i;
foreach (stack[i][j])
begin
stack[i][j]=i+j;
// stack = '{'{1,2,3,4} ,'{5,6,7,8}};
// stack[i][j] = $random;
$display ("stack[%0d][%0d] = %0d" , i, j, stack[i][j]);
// $display("stack[%0d]=%0d", i, stack[i]);
end
// end

// Print contents of the stack


$display ("stack = %p", stack);

// Print content of a given index


// $display("stack[0][0][2] = 0x%0d", stack[0][0][2]);
end
endmodule
======================
Packed 2D Unpacked 2D
======================
// Code your testbench here
// or browse Examples
module array_ex;
logic [2:0][4:1] arr[2:1][2:0]; // packed 2D unpacked 2D
integer i,j,k;
initial begin
for(i=1; i<3; i++) begin
for(j=0; j<3; j++) begin
for(k=0;k<2; k++) begin
// #1 arr[i][j]=$random; // using 2d unpacked array
#1 arr[i][j]=i+j+k;; // using 2 d unpacked along with packed
array also
// $display("array[%0d][%0d] = %d",i,j,arr[i][j]);
$display("array[%0d][%0d][%0d] = %d",i,j,k,arr[i][j][k]);

end
end
end
$display ("arr = %p", arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule

======================
UNPACKED 1D Packed 2D
======================
module tb;
// bit [3:0][7:0] stack [1:0][3:0]; // 2 rows, 4 cols
byte stack[3:0][7:0];

initial
begin
// Assign random values to each slot of the stack
foreach (stack[i])
// begin
// stack[i]=i;
foreach (stack[i][j])
begin
stack[i][j]=i+j;
// stack = '{'{1,2,3,4} ,'{5,6,7,8}};
// stack[i][j] = $random;
$display ("stack[%0d][%0d] = %0d" , i, j, stack[i][j]);
// $display("stack[%0d]=%0d", i, stack[i]);
end
// end

// Print contents of the stack


$display ("stack = %p", stack);

// Print content of a given index


// $display("stack[0][0][2] = 0x%0d", stack[0][0][2]);
end
endmodule

======================
Unpacked 1D Packed 3D
======================
// Code your testbench here
// or browse Examples
module array_ex;
logic [2:0][1:0][4:1] arr[2:1];
integer i,j,k,l;
initial begin
for(i=1; i<3; i++) begin // unpacked 1D
// for (j=0;j<3;j++) begin // packed 1d
// for(k=0; k<2; k++) begin // packed 2D
// for (l=1; l<5; l++)begin //packed 3D
#1 arr[i]=$random;
// #1 arr[i][j][k][l]= i+j+k+l; //
$display("array[%0d] = %d",i,arr[i]);
// $display("array[%0d][%0d][%0d][%0d]=
%d",i,j,k,l,arr[i][j][k][l]);
// end
// end
// end
end
$display ("arr = %p", arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule

======================
Unpacked 3D Packed 1D
======================
// Code your testbench here
// or browse Examples
module array_ex;
// bit[4:1] arr[2:1][2:0][1:0]; // 3 d unpacked 1 d packed
logic arr[3:1][2:0][2:0]; // 3d unpacked only 0s 1s
initial begin
for(integer i=1; i<4; i++) begin
for(integer j=0; j<3; j++) begin
for(integer k=0; k<3; k++) begin
#1 arr[i][j][k]=$random;
// #1 arr[i][j][k]= i+j+k;
$display("array[%0d][%0d][%0d] = %d",i,j,k,arr[i][j][k]);
end
end
end
$display ("arr = %p", arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule

======================
Unpacked 4D Packed 2D
======================
// Code your testbench here
// or browse Examples

module array_ex;
logic [2:1][2:0][1:0][4:1] arr;
//integer i;
initial begin
arr=$random;
// arr= i;
$display("array = %d",arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule

//************Unpacked 4 D************//
/*
module test;
bit arr[2][3][2][4]; // unpacked gives only postive values
// byte arr[2][3][2][4]; // gives negative values

initial begin
foreach(arr[i,j,k,l]) begin
arr[i][j][k][l]=$random;
$display("array = %d",arr[i][j][k][l]);
end
$display(" %p" ,arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule
*/

=============================================================================
==========
Packed 4D
===========
// Code your testbench here
// or browse Examples
module array_ex;
logic [2:1][2:0][1:0][4:1] arr; // packed 4D

initial begin
arr=$random;
$display("array = %b",arr);
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule
===========================
Unpacked 2D
===========================
// Code your testbench here
// or browse Examples
module tb;
bit [3:0][7:0] stack [1:0][3:0]; // 2 rows, 4 cols

initial begin
// Assign random values to each slot of the stack
foreach (stack[i])
foreach (stack[i][j]) begin
stack = '{'{1,2,3,4} ,'{5,6,7,8}};
// stack[i][j] = $random;
$display ("stack[%0d][%0d] = 0x%0h", i, j, stack[i][j]);
end

// Print contents of the stack


$display ("stack = %p", stack);

// Print content of a given index


$display("stack[0][0][2] = 0x%0d", stack[0][0][2]);
end
endmodule
===========================
Unpacked 3D
===========================
// Code your testbench here
// or browse Examples
module three_d_array;
//declaration of array’s
int array [3:0][2:0][1:0]; //2 dimension array
initial begin

//array = '{'{0,1,2,3},'{4,5,6,7},'{8,9,10,11}};
foreach(array[i])
foreach(array[i,j])
foreach(array[i,j,k])
begin
array[i][j][k] = i+j+k;

// $display("\t array[%0d][%0d] = %0d",i,j,array[i][j]);


$display("\t array[%0d][%0d][%0d] = %0d",i,j,k,array[i][j][k]);
$display(" %p", array);
// end
end
end
endmodule
===========================
Unpacked 4D
===========================
// Code your testbench here
// or browse Examples
module array_ex;
logic[3:0][7:0] arr[2:1][2:0][1:0][4:1];

initial begin
for(integer i=1; i<3; i++) begin
for(integer j=0; j<3; j++) begin
for(integer k=0; k<2; k++) begin
for(integer l=1; l<5; l++) begin
for(integer m=0; m<4; m++) begin
arr[i][j][k][l][m]=$urandom_range(0,255);
$display("array[%0d][%0d][%0d][%0d][%0d] =
%h",i,j,k,l,m,arr[i][j][k][l][m]);
end
end
end
end
end
end

initial begin
$dumpfile("dump.vcd");
$dumpvars;
#100 $finish;
end
endmodule
=============================================================================
=================
---> The event queue in Verilog is divided into 4 regions
which are active, inactive, NBA and postponed region
--->SV it is divided into 17 regions including PLI and Re-active, Re-inactive
and Re-NBA regions
apart from the region mentioned above which separate the execution of events
between DUT and the
Testbench.The observed region is kept for assertions.

// Code your testbench here


// or browse Examples
//*****Verilog_regions***********//
/*
module test ;
reg[1:0] a ;
initial
begin
a=0; // Active Region Blocking Assignments If one statement excuted
then the next statement will excueted
a=1; // Assignment happens immedtaitely
$display( "the value of a is %0d",a);
end
endmodule
*/
///**********Active region************//

module test ;
reg[1:0] a ;
initial
begin
a=0;
$display( "the valeu of a is %0d",a);
end

initial
begin
a=1;
$display( "the valeu of a is %0d",a);
end
endmodule
==========================================
// Code your testbench here
// or browse Examples
/*
module display_cmds;
reg a;
initial
$monitor("\$monitor: a = %b", a); // Print the values end of the
current time slot If any value change last call will overide the previous one
initial
begin
$strobe ("\$strobe : a = %b", a); // Print the values end of the
current time step
a = 0;
a <= 1;
$display ("\$display: a = %b", a); // print the values of updated one
#1 $finish;
end
endmodule
*/

//////////////****Verilog******Regions////

module display_cmds;
reg[1:0] a;
initial
begin
$strobe ("\$strobe : a = %d %t", a , $time);
$monitor("\$monitor: a = %d", a);
a=1;
a<=2;
//$finish;
end
endmodule
============================================
module test;
reg a,b;

initial
begin
a = 0; /// Blocking Assingments
b = 1;
a <= b; /// Non Blocking Assigmnets
b <= a;
$monitor ("%0dns: \$monitor: a=%b b=%b", $stime, a, b);
$display ("%0dns: \$display: a=%b b=%b", $stime, a, b);
$strobe ("%0dns: \$strobe : a=%b b=%b\n", $stime, a, b);
#0 $display ("%0dns: #0 : a=%b b=%b", $stime, a, b);

#1 $monitor ("%0dns: \$monitor: a=%b b=%b", $stime, a, b);


$display ("%0dns: \$display: a=%b b=%b", $stime, a, b);
$strobe ("%0dns: \$strobe : a=%b b=%b\n", $stime, a, b);
$display ("%0dns: #0 : a=%b b=%b", $stime, a, b);
#1 $finish;
end
endmodule
*/

/*
module test;
reg[2:0] a,b;
initial
$monitor ("%0dns: \$monitor: a=%d b=%d", $stime, a, b);
initial
begin
$strobe ("%0dns: \$strobe : a=%d b=%d\n", $stime, a, b);
// $monitor ("%0dns: \$monitor: a=%d b=%d", $stime, a, b);
a = 1;
a <= 2;
b <= a;
$display ("%0dns: \$display: a=%d b=%d", $stime, a, b);
#1 a=3;
a<=4;
$display ("%0dns: \$display: a=%d b=%d", $stime, a, b);
#1 $finish;
end
endmodule

*/

module test_fork ;
reg [1:0]var1 ;
reg a,b ;
initial
$monitor(" the value of monitor a .b %0d . %0d %t ", a, b, $time);
initial
begin
{a,b} = {1'b1,1'b0} ;
fork
begin
#10 var1 = a + b ;
$display("the value of var1 %0d", var1);
end
begin
#5 var1 = a + b;
$display("the value2 of var1 %0d %t", var1, $time);
end
begin
#5 {a,b} = {1'b1,1'b1} ;
$display("the value3 of var1 %0d %0d %t", a, b, $time);
end
join
var1 <= #5 (a - b) ;
$display("the value of last var1 , %0d, %0d %0d %t",var1, a,b,$time);
#20 $finish ;
end
endmodule
=============================================================================
============================================
std::randomize();
Variables can be randomized by using std::randomize method. It can accept the
inline constraints using the “with” clause.
std::randomize (variable);
std::randomize (variable) with { constraint's; };
std::randomize() is 1'b1 for success, 0'b0 for failure

Q) randomization condition: dynamic array has 3 element.


if the element index 0 > 10, then the element index 1 inside {0,1,2};
if the element index 0 < 10, then the element index 1 inside {3,4,5} and
element index 2 inside {6,7,8}.
class Packet;
bit[7:0] d_array[];
function void display();
std::randomize(d_array) with {
d_array.size() == 3; if (d_array[0] < 10) {d_array[1] inside {0,1,2};
}
else {
d_array[1] inside {3,4,5};
d_array[2] inside {6,7,8};
}
};

foreach (d_array[i])
$display ("d_array[%0d] = %d", i, d_array[i]);
endfunction
endclass

//testbench module
module tb;
Packet pkt;

// Create a new packet, randomize it and display contents


initial begin
pkt = new();
pkt.display();
end
endmodule
==============================================================

Q)associative d_array randomization condition: index is color_e type


Each element has unique value.each element value must be in [50:0]
class Packet;
typedef enum {red=1, green, blue, pink, yellow} color_e;
int d_array[color_e] = '{red:10, green:20, blue:30, pink:40, yellow:150};
//int d_array[]; DA

//must construct the array element before randomizing.

function void display();


std::randomize(d_array) with {unique{d_array};
// std::randomize(d_array) with {unique{d_array}; d_array.size==10; //
with unquie word using dynamic array
// std::randomize(d_array) with { foreach (d_array[i]) foreach(d_array[j])
if(i!=j) d_array[i]!=d_array[j]; //without unquie word
foreach (d_array[i]) {d_array[i] < 50;d_array[i] > 0; } }; //condtion
operator also
// foreach(d_array[i]) d_array[i] inside {[0:50]};}; we can use
inside operator
foreach (d_array[i]) $display ("d_array[%s] = %d", i.name(), d_array[i]);
// foreach (d_array[i]) $display ("the value of fixed array = %d",
d_array[i]); // using DA and FIxed suze array
endfunction
endclass

module tb;
Packet pkt;
// Create a new packet, randomize it and display contents
initial begin
// int i,j;
pkt = new();
// begin
//repeat(10)
pkt.display();
// end
end
endmodule
===================================

Q) dynamic/ Queue randomization condition: array has 10 element.Each element


has unique value.Sum of all element in arrays is 100.

class Packet;
int d_array[]; //also work with queue, try d_array[$]
function void display();
// std::randomize(d_array) with {unique{d_array};
std::randomize(d_array) with { foreach (d_array[i]) foreach(d_array[j])
if(i!=j) d_array[i]!=d_array[j];
d_array.size==10;
d_array.sum == 100;
foreach (d_array[i]) {
d_array[i] <30;
d_array[i] >5;
d_array[i] != i;
} //note: no semicolon here
};

foreach (d_array[i])
$display ("d_array[%0d] = %0d arraysze %0d %0d ", i, d_array.size()
, d_array.sum(), d_array[i]);
endfunction
endclass

//testbench module
module tb;
Packet pkt;

// Create a new packet, randomize it and display contents


initial begin
pkt = new();
pkt.display();
end
endmodule
====================================

Q)dynamic array randomization condition:


all element in 2-D array are unique and having value from 0-16
class Packet;
int d_array [5][];

function void display();


// std::randomize(d_array) with { unique {d_array};
std::randomize(d_array) with { foreach (d_array[i]) foreach(d_array[j])
if(i!=j) d_array[i]!=d_array[j];
foreach (d_array[i]) {d_array[i].size() inside {[1:2]}; }
foreach (d_array[i,j]) { d_array[i][j] inside {[0:16]}; }
};

foreach (d_array[i])
$display ("d_array[%0d] = %p", i, d_array[i]);
foreach (d_array[i,j])
$display ("d_array[%0d][%0d] = %p", i,j ,d_array[i],d_array[j]);
endfunction
endclass

//testbench module
module tb;
Packet pkt;

// Create a new packet, randomize it and display contents


initial begin
pkt = new();
pkt.display();
end
endmodule
=============================================================================
=========================================
1)Generate odd number / Even number in a fixed size array
Use with $random

//****genreate odd values & Even values also only**********//

module test;
bit [4:0] a[10];
initial begin
foreach(a[i])
begin
a[i] = $random;
$display(" default a[i]=%0d",a[i]); // by default value printing
if(a[i]%2==0) // to get even values here
begin
a[i]=a[i]+1; //after geeting even adding +1 here to genrate odd
values // uncoment so will print both even and odd values here
$display("array of odd values : %p",a);
end
else
$display("array : %p",a);
end
end
endmodule

//****genreate even values only**********//


/*
module test;
bit [3:0] a[6];
initial begin
foreach(a[i])
begin
a[i] = $random;
$display("a[i]=%0d",a[i]); // by default value printing
if(a[i]%2==0) // to get even values here
$display("array : %p",a);
else

a[i]=a[i]+1;
$display("array of even values : %p",a);

end
end
endmodule

*/
=============================================================================
=============
Write a constraint for 2D fixed size array randomization using Dynamic array
?

class fs_array;
rand bit [7:0] array1[6][5];

constraint array_c { foreach(array1[i,j]) array1[i][j]==i+j;}

function void display();


$display("array1 = %p",array1);
endfunction

endclass

program fixedsize_array_randomization;
fs_array pkt;
int i,j;
initial begin
pkt = new();

repeat(1)
begin
pkt.randomize();
foreach(pkt.array1[i]) // 1d
foreach(pkt.array1[i][j]) // 2d
begin
pkt.array1[i][j]=i+j;
$display(" array [%0d][%0d]= %0d", i , j , pkt.array1[i][j]);
end

pkt.display();
// $display("%p",pkt);
end
end
endprogram

=============================================================================
=============
Write a constraint to generate the below pattern in dynamic array ?
0 1 0 2 0 3 0 4 0 5 0

module even();
class eve;
rand bit[7:0] a[];

constraint a1 { a.size inside {[7:10]};}

constraint a3 {foreach(a[i]) if ( i%2 ==0) a[i] ==0; else a[i] ==


(i+1)/2;}

endclass

initial
begin
eve p1=new;
repeat(5)
begin
assert(p1.randomize());
foreach(p1.a[i])
$display( " [%0d] %0d " ,i , p1.a[i]);
$display(" %p %0d ", p1,p1.a.sum());
end
end
endmodule
=============================================================================
=============
Pre Post Randomization examples

class abc;
rand bit [3:0] data;
rand bit dummy_bit;
constraint c_data { data >= 3; data <= 6; }

//display function get executed during randomization


constraint c_disp { dummy_bit == display(); }

function void pre_randomize ();


$display ("before randomization");
endfunction

function bit display();


$display("during randomization");
return 1;
endfunction

function void post_randomize ();


$display ("after randomization");
endfunction
endclass

program my_prg;

initial begin
abc a1;
a1 = new();
a1.randomize();
end
endprogram : my_prg
=============================================================================
=============
write a constraint to randmoly genrate 10 unquie numbers between 99 to 100

module factorial();
class eve;
rand int a;
real re_num;

constraint a1 { a inside {[990:1000]};}


function void post_randomize();
re_num = a/10.0;
$display(" the rela num %2f", re_num);

endfunction

endclass

initial
begin
eve p1=new;
repeat(5)
begin
assert(p1.randomize());
$display(" %p ", p1.a);
end
end
endmodule

You might also like