Search

Quick Links

Constraints are our best friends when it comes to random generation. They allow us to limit the range of possible scenarios/values as much as we like, from the level of "everything is allowed" down to the level of such heavy constraints that really imply a very directed scenario. In most cases what we want to is to have a set of default constraints that give us a nice distribution of scenarios and values. We will then need to override the default set in some of our test cases by applying additional constraints, or reshaping the distribution or even removing previous constraints. Here's a cool way to do this in SystemVerilog:

First, let's see how you can specify custom distribution in System Verilog:

This means that the frame will be valid 90% of the times. This very much resembles the soft constraints mechanism in e. Make it a million to 1 ratio, and you''ll end up with a soft constraint "de facto". If you still get an invalid frame after that, go buy a lottery ticket because it''s probably your lucky day.

Now what if we wanted to override this default distribution in a specific test case? For example, we want to write a test case for invalid frames. No problemo. SystemVerilog kind of mimics AOP behavior in allowing you to have placeholders for constraints that you can later on define in your test case. No complaints here... Check this out:

Now for the tricky part. What if you wanted to completely remove the default constraints becuase you want, for example, a 50/50 chance of valid frames in your test case? There are 3 ways to do that.

1. Change the default constraints in the environment. Hm.. Not so good. In fact - a VERY BAD idea.

2. Use SystemVerilog''s built-in mechanism for setting constraints on and off (look up constraint_mode() in SystemVerilog LRM). Possible, but not really efficient. You can only change constraints mode in a procedural way (i.e. from a function). If you want to eliminate constraints in a declarative manner (and trust me - you want) then check out number 3.

3. Add an auxiliary boolean (bit) variable that controls the default constraints. Keep it at 1 ("TRUE") using a "soft" constraint and override it in your program, if needed, to provide alternative constraints. Here''s the full example:

Comments, questions or requests are welcome! Email us at mail@thinkverification.com. We love your feedback.

More articles :

» VMM Hackers Guide - Creating Smart Scenarios With Atomic Generators

VMM ships with some pretty useful built-in components and applications. VMM''s Atomic Generator is probably one of the most powerful ones, yet it's pretty basic. It can definitely help you generate a flow of random items but it was not intended for...

» Method Manipulation In SV and e

If you're familiar with SystemVerilog and taking your first steps in e (or vice versa) you might find this useful. Here are some of the most common method manipulations that you''ll need to master and how you should go about implementing them in e...

» VMM Hackers Guide - Shutting Down Atomic Generators

Everybody likes atomic generators. If you have a vmm_data class, all you have to do is add a quick macro line and you get a free VMM-compliant generator that you only need to instantiate in your environment. An atomic generator is a simple yet...

» DVT Eclipse - For SystemVerilog/Specman Code Developers

» Coverage Driven Thoughts

In today’s short post what I’ll try to do is share with you some of the recent trends and ideas that deal with coverage. I won’t go into much technical detail today in order not to wear you (and myself) out, but really - if I want to be more...