Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why var is not taking value as per constraint

Status
Not open for further replies.

jdshah

Junior Member level 3
Joined
Dec 17, 2010
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Ahmedabad
Activity points
1,481
Code:
class Base;
rand integer vari;
  constraint range { 0< vari < 100 ;}
endclass

module test;
  Base objh =new();
initial
begin
if(objh.randomize())
begin
$display(" Randomization is done ");
end
  $display(" objh.baseh.vari : %d ", objh.vari );
end
endmodule

Output : objh.vari : 1245020823

My question is constraint did not failed, but value did not generated as per constraint. Why?
The reason I find out is, this happened as constraint are declarative. But I did not understood the reason. Can anyone explain?
 

I dont think 0 < vari < 100 is valid syntax

try:

Code:
constraint range { vari > 0;
                   vari < 100; }
 

It is valid syntax, but your constraint expression is evaluated like any other expression, with left to right association: ( ( 0 < vari ) < 100 ). Since the result of ( 0 < vari ) can only be 0 or 1 for any value of vari, your constraint expression is satisfied for any value of vari. Some tools have verbose solve modes that would show you this.

For ranges or set of values, I recommend using the inside operator (LRM section 18.5.3 Set membership ) instead:

Code:
constraint range { vari inside {[1:99];}
 

It is valid syntax, but your constraint expression is evaluated like any other expression, with left to right association: ( ( 0 < vari ) < 100 ). Since the result of ( 0 < vari ) can only be 0 or 1 for any value of vari, your constraint expression is satisfied for any value of vari. Some tools have verbose solve modes that would show you this.

For ranges or set of values, I recommend using the inside operator (LRM section 18.5.3 Set membership ) instead:

Code:
constraint range { vari inside {[1:99];}

Thanks Dave for explanation.
Why constraint is evaluated like this. Is it because it is declarative? Is there any connection with that?
 

There is no connection with whether the constraints are declarative or not. You wrote a expression that always evaluates to true (1'b1). Consider a plain-old Verilog example that does the same thing without any constraint solver.

Code:
module test;
  integer vari;
  `define RANGE  (0< vari < 100 )
  initial begin
      vari = $random;
      while ( ! `RANGE )
             vari = $random; // never executes 
      $display(" Randomization is done ");
      $display("vari : %d ", vari );
  end
endmodule
The while loop never executes its body because the while expression is always false, because `RANGE is always true. The value of vari is irrelevant to the expression. If you change RANGE to ((vari > 0) && (vari < 100)), then you will eventually get a value that satisfies the constraint.

But there are a number of problems with this procedural method of generating a random number. Assuming you wrote a meaningful constraint, there is no guarantee that $random will ever generate a value that satisfies the constraint. Or it could take a very long time before generating that value. And if you wrote a constraint that had no possible solutions, there would be no way to know that unless you exhaustively tried every value. Add to this a number of random variables with interdependent constraints and the problem becomes insurmountably.

That's where the declarative nature of the constraint solver comes in to play. It analyzes all the constraints together and comes up with an algorithm to generate values for all the random variables in what seems like a single step. Unfortunately, this makes it difficult to debug because you can't see all the intermediate constraint expression results.

But conceptually, you can sill think of the constraint solver as exhaustively trying every possible combination random values, then building a solution set of values that satisfies the constraints. When you call randomize() it just picks one of those randomly from the set.
 
  • Like
Reactions: jdshah

    jdshah

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top