SystemVerilog constraints allow you to limit random variable values to specific ranges or sets of values, ensuring randomness within desired boundaries rather than allowing unlimited values; constraints can be added to random variables using the 'constraint' keyword with syntax like 'constraint name { variable inside [low to high]; }', and multiple constraints can be combined with conditional logic (using 'if' statements) while ensuring they don't contradict each other, as the constraint solver will fail if contradictions exist.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Course : Systemverilog Verification 4 : L3.1 : Constrain the Randomness
Added:now you have learned how to declare a variable as random in a class and how to use them in the program but if you simply declare a variable as random it can take any value that is allowed by the variable declaration so this may not be of the real use case of random variables as your randomness is unlimited in certain cases or in most of the cases you you may want to limit your randomness to a certain set of ranges so here system will log supports another feature called constraints which can be added to the random declaration here in this session you will be learning how you can add a constraint to the random variables in a class system by log supports the feature to constrain the randomness to a set of values in which you are interested and you will constrain by adding a constraint to your class so here is example the same example that we have learned in the previous session the class named packet so it got three data members which are random which uh named address data and crc and here if you wanted to add a constraint to the address saying that the address should be limited to a set of values say for example you want to limit the address to a set of values inside 0 to 7 it can be done by adding the constraint so this is a syntax to add a constraint constraint is a system will keyword and next next is the name of the constraint so this is user specific and within the open opening and closing braces you will list all the constraint associated with this particular name so here i am adding a single constraint where i am saying address is inside this setup value so inside is under systematic keyword and so this is how we list the values so again you can list the values within the opening and closing braces so all the values are listed by separating commas now when you create and randomize an object of the spike class named pike it's a p1 so the p1 dot address will be of any value between 0 to 7 because we added a constraint here and p1 dot data and p1 dot crc will be of absolute random value there are many variations of constraint block statements in system value which i'll be going one by one see this constraint statement so constraint is a systematic keyword and here this name of the general prop prp is the name of this constraint statement block and within the same constraint block you can see there are multiple statements so with the same name associated you can put multiple constraint statements and all of them should be separated by a semicolon so for to make you understand more easily i have listed uh separate statement in separate lines you can even club them together into single line but there should be a semicolon separating them so here the first constraint statement says address is inside 0 to 7 or addresses inside a value set called 0 1 2 3 4 5 1 7 and the same statement you can put in a different way so here i am saying data is inside this value set and instead of listing out all the individual values i am given the range of there so this is a format to give a range of values within the square bracket you can give the low on higher value so here data will be inside any value between 0 and 2 to 2 128 and if you want to negate that any statement you can add a node statement so in the third statement says data should should not be inside the value set 200 to 255 and the fourth constraint statement says data should be less than 100 and the fifth statement says data should be greater than 50 there is one point you should note down here that you can add multiple constraint statement with the same that random data member within the class but none of these statements should contradict each other say um you are adding two statements saying data is less than 100 and the second statement if it is saying data is greater than 150 then both statement will counter the teacher so there is no solution for this uh two statements you can add any number of constraint blocks or constraint statements but the constraint server should be able to solve each and every constraint statements within the same class declaration so in other ways you should be able to list all the constraints without contradicting each other otherwise the constraint solver will fail and randomization will return a zero value i am listing a few more points related to the constraint block statements so here you can as you can see so variables can be used within the insert keyword statements so here you can say data inside low to high so the low on high can be any they can be variable or they can be of constant values also and this is an example of a conditional constraint say if the address is say then in that case then the data should be limited to some set of value so this is how you add a conditional constraint so constrain the keyword as a name of the constraint and this is a conditional constraint statement if address is equal to equal 0 equal equal to 3 then followed this is a syntax saying and then data should be inside 0 to 3 so you can add conditional constraints by using this syntax and this we have already seen to to get any value that is not within an inside range invert the constraint by adding a exclamatory mark and you have seen that you can add relational operators within an expression but uh there can be a maximum of only one relational operator in in a single conditional expression so if you are adding like ah an expression like 50 less than data less than 100 then it's an error you can if you wanted to add a constraint like that you can say data should be greater than 50 and next statement you can say data should be less than 100
Related Videos
LBF101 Creating an XML Changelog
liquibase7511
3K views•2026-06-15
Alta Labs Cloud Dashboard Real time Network & Xnet Insights!
ShinyTechThings
158 views•2026-06-17
Wait... Group Policy Not Applying? Check This First!
keeplearning_iT
144 views•2026-06-15
Leetcode Weekly Contest 506 | Life's boring these days
Pudeesht
2K views•2026-06-14
microJAM: MAKING A MICRO GAME FOR A GAME JAM IN CLOJURESCRIPT AND TOTALLY NOT C
janetacarr
156 views•2026-06-18
Partitioning vs Bucketing vs Clustering: How to Make Queries 100x Faster
thedataandaiguy
194 views•2026-06-16
Design Claude Code Like a Senior Engineer
hayk.simonyan
344 views•2026-06-19
Linus Torvalds: AI Won’t Replace Understanding Code
SavvyNik
140 views•2026-06-19











