Constraints make sure that inputs applied to the design are valid.
Constraints make sure that we focus each test on a specific feature of the design.
What is the need for Randomization?
Can we use randomize on integer or bit vector? How to randomize non-object variable? System tasks used?
Randomize can be used only on objects
Int a;
a.randomize() //not possible
a = $random, $urandom, $urandom_range
How to randomize a string? String randomization not works in class.
What is significance of seed?
What is the seed?
Tool is not perfectly random
It also needs a starting point for randomization
If same seed is used(given that TB didn’t change majorly), it will always generate same pattern.
When running a regression, tests run with different seeds.
For a failing testcase, we analyze what is causing it to fail.
Report design issue to the RTL design team
They will fix the RTL code, release the new version
How do we confirm that the bug has been fixed?
Run the failing testcase with the same seed, while ensuring that no other changes done to the testbench.
Running with the same seed is making sure that, same pattern is getting generated as part of the test. With the same pattern if test passes, it means that design has been fixed.
What if we use a different seed?
To cover different parts of design.
What is the use of pre_randomize and post_randomize? Explain with use case.
These methods are the callbacks of randomize method
If for some reason, we want to modify the randomize behaviour, we do that in pre_randomize
If for some reasons, we want to change/update the randomized values or do some analysis based on randomized values, we do that in post_randomize.
Why it is not suggested to keep all constraints in to one big constraint?
What are the reasons for randomization failure? What is the approach to fix the issue?
Failure cause
Conflicting constraints
Some non rand variable used in constraints
vsim top -solvefailtestcase -solvefaildebug=2
Having two different Constraints conflicting each other?
What will happen?
What else I need to do to make the randomization to pass if without deleting any constraint
Where to write constraints?
Transaction or packet class
Inline constraints in generator or sequence
What is the significance of new() in randc ?
If we use pkt=new, it will reset the randc behavior.
Can we write constraints inside a module? If not why?
Constraints are dynamic in nature. Module is static in nature.
What are all the types of constraints? Explain those.
Simple constraints
Distribution constraints
Implication constraints
If-else constraints
Iterative constraints
Variable ordering constraints
Soft constraints
Uniqueness constraints
Inline constraints
What is an in-line constraint? Examples of In-Class constraints?
Constraints used inside generator or sequence at the time of transaction randomize is called in-line constraint.
What is uniqueness constraints?
Constraints that generate unique values to the variables inside the unique set
What are distribution constraints?
Constraints that are used to constraint a variable value based on the weightage.
What are iterative constraints?
Used for applying constraints on arrays using foreach construct.
How will you assign range of values to a variable?
{[a:b]}
What is inverted inside?
It is used for negative constraints
Constraining values outside the range
What is the advantage of constraint_mode and rand_mode, when they can only be used for disabling purpose.
Can we enabling a non rand declared variable randomization by using rand_mode(1)?
How inside constraint is related to distribution constraints?
Are Constraints static or dynamic? How to make it static?
What is the use of randcase?
We don’t need to create many testcases
Just by changing the variable weightages, same test can be used to generate multiple scenarios.
When do we use rand and randc?
What are variable ordering constraints? What is the need for solve before.
Variable ordering constraints used for indicating the tool the order of solving the variable values.
Constraints are virtual by default, explain.
What are soft constraints?
What are hard constraints and how to declare hard constraints?
Any constraint that is not declared as soft, is a hard constraint.
Easy
Write a constraint for the below scenario if A<20 then b value should generate 10 to 30 and if a>50 then the B value should be 30 to 50 only
Write the constraint to generate the three unique numbers , without using “unique”?
write a single constraint to generate the random values for bit[8:0] variable in the below range 1-54 , 127 ,137-166,196-209 & 231-262
Constraints to have Unique elements in an Array with Size in {5 – 10}
Write a constraint for 16-bit addr which should contain 9th bit as 1
Write a constraint for 16bit addr which should contain 9th bit as 1 but should not have constitutive 1
Write a constraint for 16bit addr to generate power of 2.
Write a constraint for apb slave select signal?
Write a constraint without an inside function to generate variable value within the range of 34 to 43?
Multi bit vector, generate value with only one bit in vector being ‘1’
Declare a Queue, Fill with 20 random values between 200 to 300, no repetition
Generate a random number between -5 to -15 using system tasks.
Generate a,b,c,d in such a way that all of them take unique values, but all of them inside 10 to 20 only.
Soft constraint using eth_pkt where constraint len is 42 to 1500 and inline constraint with 2000 len.
Explain Inline constraints using packet type and payload length example.
generate random number between 100, 200, which is a multiple of 5
Populate a queue with 10 elements, each element inside 100 to 200 and must also be a multiple of 5.
randomize only one variable in a class
use rand_mode(0) to disable
randomize(variable_to_randomize)
We can print rand_mode() of variable also using $display
random number between 5 to 10 with weight of 5, -5 to -15 with weight of 10
Generate a random real number between -2.35 to 4.32
Generate a random real number between -2.35 to -4.32
MEDIUM
The constraint for an array of 10 elements in which the first 5 elements are in increment in nature and the next 5 elements are in decrement nature.
All elements in range of 50 to 100.
All elements should be multiple of 5
write a constraint to store odd data in even addr and even data in odd addr
write a constraint to generate unique even no. on even location & unique odd no. on odd loc
Write a constraint for an array of numbers such that the size of array ranges from 16 to 32 elements, and even index locations should have odd numbers and odd index locations should have even numbers, numbers rage is between 16 to 127
Write constraint for randomly generating an array of numbers with size 20, sort in ascending order without using inbuilt sort method, and sum of all array elements should be 300.
write the constraint to gen. this seq. 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 1 0 2 0 3 0 4 0 5 … in array the same for a vector variable.
0 0 1 0 0 2 0 0 3 0 0 4 …. 0 0 9 0 0 1 0 0 2…
Write a constraint to generate Real numbers from 3.5 to 5.5
Write a constraint for 10101010.. sequence
Given a 16-bit address field as a class member, write a constraint to generate a random value such that it always has 9bits as 1 and 111 or 000 sequences should not occur in that address.
Randomize a value with even and odd values alternatively.
Without using randomization or rand keyword, generates array of random values.
Generate unique elements in an array without using the keyword unique.
Constraint to generate 1’bx and 1’bz randomly
Use post randomize to assign x and z based on other variable.
Write constraint on an array to generate values in ascending order.
Write constraint on an array such that even indexed elements are even values, odd indexed are odd values.
Write a constraint to generate a pattern 01020304050
Declare an array
For even index of array, fill 0’s
Odd index use the i+1/2 relation in foreach loop
Factorial of first 5 even numbers using constraints
Write a function to calculate factorial
Generate an array of random real random number
Write constraint to generate pattern 9-7-5-3-1-8-6-4-2-0
Write constraint to fill array with value in ascending order
Write constraints to randomize only specific bit of a bit vector, others are all 0’s
Generate a random string whose name is between “pkt1” to “pkt20”
Declare 2 array(queue) elements each of size 10, randomize in such a way that 2nd array values are 1st array value added with 5 for each element.
Implement randc behavior using rand
Add “size” number of entries to a queue. The entry of queue is randomized between 0 to “size”
Write constraints to generate a n bit value such that the number of bits set is equal to number of bits that are zero
Write a constraint such that an array of inputs will never be equal and also never equal to the other input when the index is same
write a constraint for 3x3x3 array to have unique elements
Write constraint for an integer array with 10 elements such that exactly 3 of them are same and rest are unique
Constraint to randomize a 100 bit var such that always and only 5 consecutive bits are 1s
Question variation: 5-1’s and 5-0’s repeating pattern
Constraint to randomize an array such that one specific element picked is always a constant value, say element at index 5 is always 100.
write constraint to generate a random number with only 5 bits set and consecutively set for 80% of the time
Write a constraint to make sure every even item in an array is even and every odd item is odd.
Write constraint such that sum of arr[10] is 100 without using .sum method
Write constraint for an integer array with 10 elements such that exactly 3 of them are same and rest are unique
Difficult
Write a Constraint for the prime number generation in a given range?
Constraints on below 2-Dimensional array to have lower triangle matrix
7 0 0 0
11 27 0 0
57 63 92 0
107 117 290 700
Write a constraint on write and read with following conditions
read can be followed by another read on the next cycle
write can be followed by another read but not write on next cycle
More than 4 read cannot be back to back.
Write a constraint to generate for the wr_rd signal which is of 1bit 00110011001100
Write constraints to meet the following requirements
a. The dynamic array should contain 20 items.
b. Out of 20 items, the values of 3 items should be equal to 5 at random positions.
c. The values of 5 items must be 10 at random positions
d. The values of 8 items must be 15 and the remaining 4 items must be 20.
Note: Don’t use distribution constraint, as it will not really meet this requirement
Design has 2 interfaces
One Master interface
One Slave interface that connects with multiple slaves.
Only one slave can be targeted at any time by means of Chip select.
BFM will do the design register configuration followed by generating transactions for various slaves to check write and reads to those slaves.
Each slave has specific address range from `CSn_START to `CSn_END
Need flexible testcases where we can target multiple slaves in the same test
user should be able to decide which specific slave to target
Design has multiple(6) slaves connected, slaves connected on CS0 to CS5
Each slave has specific address range.
`CSn_START to `CSn_END with n from 0 to 5
Generator and BFM connected to the design on the host(master) interface generates transactions to these slaves. We want a flexibility where, we choose which slaves my transactions should be targeted to.
USB frame generation example
Frame consist of packet targeted for different EPs
EP are targeted for
Isochronous transfers
Bulk transfers
Interrupt transfers
EP can be
IN
OUT
Test case should be developed in such a way that Eps should get selected automatically for given transfer type and direction.
Write constraints to pick a ball out of 10 different colored balls and that color should not be repeated for in next 3 draws
Write constraints to generate MxN matrix with each element with 0,1 and sum of all elements less the MAX_SUM
Write a constraint to divide values of 1 queue into 3 queues so that all 3 queues have unique elements
Write a constraint to generate dynamic array of 300 elements. each element can have value 0/1/2/3/4 each of the above values should be present more than 40 times in the array element 0 can be repeated while 1/2/3/4 are not allowed to repeat consecutively ex: 001342.. allowed(0 can be repeated) ex: 0122431.. not allowed(2 is repeated)
generate a solved sudoku puzzle
For a 8 bit variable if the past randomization resulted in a odd value, the next randomization should be even with 75% probability else be even with 25% probability. Write a constraint
Stream of random numbers are getting generated. how to write constraints to make sure most recent four numbers are unique.
Write a constraint to generate two dynamic arrays such that array1 size = [6:9], array2 size = array1 size. Array 1 should be assembled in ascending order while array2 should have all the values picked from array1
Write a constraint on a 4 bit variable such that the probability of values being the same on the lower two bits have only 5% chance
write a constraint for generating leap years
a. Matrix size should be randomized with only odd numbered square matrix. b. Each sub square matrix should have only one max element and rest less than max and can be repetitive c. Max elements of each square sub matrix should be unique, mean no two sub matrix should have same max element d. Constraint Should scale up to any size of limited by 32 bit
Write a constraint for a 10 bit variable so that; -> 10% of the time 1 bit in en is high -> 10% of the time 2 bits in en are high … -> 10% of the time all 10 bits in en are high.
Write a constraint to generate a number which has binary all 1s grouped together
Write a constraint to randomly map elements from an array into N non-empty queues
write code so each element from input_array appears in output_queues, no output queue should be empty
The dynamic array size should be 300 and the elements in it can be 0,1,2,3,4,5. Each element should occur atleast 40 times in the array. Also, there shouldn’t be 0’s consecutively.
Write a constraint for distinct adjacent element (adjacent elements should not be the same) in a 2d array (specifically for the boundary elements size of 2d array is mxm where m can be any integer)
Write a constraint for a 2d array such that it has a unique max value in each row and that max value should not be equal to any other max value in other rows
Generate a parameterized array whose values are equal to a Magic square using constraints
How to write a constraint to generate a random value of 10 bits as 1 and no two bits should be next to each other. This should be written without using $countones.
Write SV constraint to limit sum of odd elements of an array to be 30 and sum of even elements to be 60
Write constraint for number to be power of 4.
Write a constraint that generates Add, mul, sub, nop instructions. Such that no Add instruction is repeated in 3 clock cycles and sub is not repeated in the last 3 valid instructions. Nop is not a valid instruction
Write a SV constraint to generate 2 3×3 matrices s.t the min value in each matrix is unique
Write a SV constraint for a tic tac toe game. Matrix size is 3×3.
Write a constraint for square matrix and then rotate 90 counter clock wise
Say you have 4 instructions: ADD, SUB, MUL, NOP.
They execute in X cycles, Y cycles, Z cycles, and 1 cycle respectively.
How would you generate a constraint to inject these into a design so that two like instructions don’t overlap during the same period of time
Bonus: Is there a way you could generate this without using SV constraints
Write system verilog constraints for the eight queens problem? Write system verilog constraints for the Knight’s tour problem.