Problem 1: Prime Numbers Only
Generate 10 random prime numbers between 2 and 100.
Solution
class prime_num;
rand int a[10];
constraint range_c
{
foreach(a[i])
a[i] inside {[2:100]};
}
function bit is_prime(int n);
if(n<2)
return 0;
for(int i=2;i<n;i++)
if(n%i==0)
return 0;
return 1;
endfunction
constraint prime_c
{
foreach(a[i])
is_prime(a[i]);
}
endclass
Problem 2: No Two Adjacent Numbers Differ by Less Than 10
Generate
20 random numbers
0-200
Difference between adjacent numbers >=10
Solution
class adj;
rand int a[20];
constraint c1
{
foreach(a[i])
a[i] inside {[0:200]};
}
constraint c2
{
foreach(a[i])
if(i>0)
abs(a[i]-a[i-1])>=10;
}
function int abs(int x);
return (x<0)?-x:x;
endfunction
endclass
Problem 3: Strict Mountain Pattern
Generate
1 4 9 15 22 18 13 7 2
Pattern
Strictly increasing
Peak
Strictly decreasing
Solution
class mountain;
rand int a[9];
constraint peak
{
foreach(a[i])
a[i] inside {[1:50]};
a[0]<a[1];
a[1]<a[2];
a[2]<a[3];
a[3]<a[4];
a[4]>a[5];
a[5]>a[6];
a[6]>a[7];
a[7]>a[8];
}
endclass
Problem 4: Sudoku Row
Generate
9 numbers
1-9
No duplicates
Solution
class sudoku;
rand int row[9];
constraint c1
{
foreach(row[i])
row[i] inside {[1:9]};
}
constraint c2
{
unique {row};
}
endclass
Problem 5: 64-bit Address Generator
Rules
- aligned to 64 bytes
- inside DDR range
- not inside reserved range
Solution
class addr;
rand bit [63:0] addr;
constraint c1
{
addr inside {[64'h8000_0000:64'h8FFF_FFFF]};
}
constraint align
{
addr[5:0]==0;
}
constraint reserve
{
!(addr inside {[64'h8001_0000:64'h8001_FFFF]});
}
endclass
Problem 6: Exactly Three Consecutive Ones
Generate
000111000
Valid
11001111
Invalid
Solution
class bits;
rand bit [15:0] data;
function bit check(bit [15:0] d);
int count=0;
for(int i=0;i<16;i++)
begin
if(d[i])
count++;
else
count=0;
if(count==3)
return 1;
if(count>3)
return 0;
end
return 0;
endfunction
constraint c1
{
check(data);
}
endclass
Problem 7: Balanced Parentheses
Generate
(()())
Valid
())(
Invalid
Solution
class bracket;
rand bit seq[8];
function bit valid();
int bal=0;
foreach(seq[i])
begin
if(seq[i])
bal++;
else
bal--;
if(bal<0)
return 0;
end
return (bal==0);
endfunction
constraint c1
{
valid();
}
endclass
Problem 8: Arithmetic Progression
Generate
7
12
17
22
27
32
Difference = 5
Solution
class ap;
rand int a[6];
constraint c1
{
a[0] inside {[1:50]};
foreach(a[i])
if(i>0)
a[i]==a[i-1]+5;
}
endclass
Problem 9: Random Packet Generator
Rules
- payload size = 64–512
- address aligned
- CRC enabled only if payload > 256
- burst = 1,2,4,8
Solution
class packet;
rand int payload;
rand bit crc;
rand bit [31:0] addr;
rand int burst;
constraint c1
{
payload inside {[64:512]};
}
constraint c2
{
addr[3:0]==0;
}
constraint c3
{
(payload>256)->crc==1;
}
constraint c4
{
burst inside {1,2,4,8};
}
endclass
Problem 10: Hardest Interview Question (Cross Constraints)
Generate 20 numbers such that:
- Every number is unique.
- Values are between 1 and 100.
- Exactly 10 numbers are even.
- Exactly 10 numbers are odd.
- Sum of all numbers is exactly 1000.
- The array is sorted in ascending order.
- No adjacent numbers differ by less than 2.
- The first element is prime.
- The last element is divisible by 5.
Solution
class hardest;
rand int a[20];
// Range
constraint range_c {
foreach(a[i])
a[i] inside {[1:100]};
}
// Unique values
constraint unique_c {
unique {a};
}
// Ascending order with minimum gap of 2
constraint order_c {
foreach(a[i])
if(i > 0)
a[i] > a[i-1] + 1;
}
// Exactly 10 even and 10 odd
constraint parity_c {
(a.sum() with (int'(item % 2 == 0))) == 10;
(a.sum() with (int'(item % 2 == 1))) == 10;
}
// Total sum
constraint sum_c {
a.sum() == 1000;
}
// Last element divisible by 5
constraint last_c {
a[19] % 5 == 0;
}
// First element must be prime
constraint prime_c {
is_prime(a[0]);
}
function automatic bit is_prime(int n);
if (n < 2)
return 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
endfunction
endclass
0 Comments