Regarding const'() cast

Hi ,
I was trying to understand working of const’() cast.
LRM 6.24.1 mentions


An expression may be changed to a constant with a const cast. 
const'(x)

Consider the following code :


class C;
  
  rand bit[2:0] a ;  
  function void pre_randomize();
   // 'a' could be assigned here
  endfunction
  constraint CONST { a == const'(a) + 1 ; }
  function void post_randomize();
   // 'a' could be assigned here
  endfunction
  
endclass  

C c1;

module test;
  
initial begin
  c1 = new();
  
  repeat(5) begin
    if( c1.randomize() )
      $display("Success with %p",c1);
    else  $display("Fails");
      // c1.a could also be assigned here prior to call randomize() again
  end
  
end  
endmodule

Consider following 3 cases where variable ‘a’ is assigned.

  1. Via pre_randomize()
  2. Via post_randomize()
  3. Via assignment to variable ‘a’ after randomize() call i.e assignment is done after post_randomize()

When we mention const’(a) which value of a should it pick ? Is it just before the constraints are solved i,e value assigned in pre_randomize() ?
There could be any possible combination of 1,2,3 i.e all 3 exist or only 1 and 2 exist or 1 and 3 exist , etc

In reply to hisingh:

As with any other non-random variable or expression, the constraint solver uses that values after pre_randomize completes to solve the constraints.

In reply to dave_59:

Thanks Dave.
So in absence of assignment within pre_randomize() , the value assigned procedurally would be used. In absence of both assignment within pre_randomize() and procedural assignment after randomize() is successful , the value assigned within post_randomize() would be used.

When casting an expression as a constant, the type of the expression to be cast shall pass through unchanged. The only effect is to treat the value as though it had been used to define a const variable of the type of the expression.
I see some simulators failed the compilation, based on the statements above it seems to be illegal to do constant cast to rand variable in constraint.

Pass through unchanged means it keeps the value it has as if defined as a const. So randomization can’t change the value.

Hi Dave,
If randomization can’t change the value of a, how could this constraint be satisfied in this case?
Isn’t it a conflict?