Constraint

Hi,
I have a 24 bit variable and i have to restrict its transitions in the randomization to some number.
I want to write a constraint for this to control the transitions to 16. Please give some idea to proceed…
Thanks in Advance,

class A;
rand bit [23:0] AA;
constraint AA_C { //something };
endclass

In reply to avpchandra:

Below code restrict AA to be 16:

class A;
rand bit [23:0] AA;

constraint AA_C {
    AA != 16;
}
endclass

In reply to rohitk:
Hi Rohitk,
i am talking about the transitions in the variable value, i mean 0 to 1 and 1 to 0 transitions should not exceed 16.

In reply to avpchandra:

so basically you want only 16 bits at most to be set at once. Is that correct?

In reply to rohitk:

No rohitk,
as far as you are concerned about 16 bits to set that is wrong. I want only transitions but not how many bits are getting set. Transitions means for example,
AA took randomly the value as 'b1010 ----> No. of transitions are 3
like this…these transitions should not exceed the value 16 that is what i want.

Hope you understood the question what i am asking…

Thanking you,

In reply to avpchandra:
I think what you are asking is to look at a series of randomizations, and you are defining a transition as a change from one randomization to the next. Then what you want is

class A;
rand bit [23:0] AA;
   constraint AA_C { $countones(const'(AA) ^ AA) == 16;}
endclass // A


In reply to dave_59:

Brilliant Dave! Thanks for sharing this tip!

Srini

In reply to dave_59:

Dave,
Can you please elaborate on constraint, I didn’t get why const is used.

Regards,
Rohit

For the users, who doesn’t have const() supported in constraint.

class abc;
    rand bit[23:0] AA;
    bit[23:0] AA_past = 0;
    
    constraint AA_C { $countones(AA_past ^ AA) == 16;}

    function void post_randomize();
        AA_past = AA;
    endfunction // post_randomize

endclass // abc


In reply to rohitk:
Section 6.24.1 Cast operator says

An expression may be changed to a constant with a const cast.

const'(x)

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.

Basically it’s like a sampled value at the point of evaluation of the expression.

In reply to dave_59:

Thanks Dave, so const casting will add an implicit order on variable and it will retain it’s value from last randomization. Is my understanding correct?

Regards,
Rohit

In reply to dave_59:

In reply to avpchandra:
I think what you are asking is to look at a series of randomizations, and you are defining a transition as a change from one randomization to the next. Then what you want is

class A;
rand bit [23:0] AA;
constraint AA_C { $countones(const'(AA) ^ AA) == 16;}
endclass // A

Hi Dave,
what you gave is transitions of series of randomization, but i want the variable AA shall not have more than 16 transitions. This is my restriction on variable AA so i mean, whatever the value it takes in randomization but the value should not exceed 16 transitions in it.

Thanking you,

In reply to avpchandra:

Sorry, then maybe I should have written

   constraint AA_C { $countones(const'(AA) ^ AA) <= 16;}

But I’m not sure how you are defining a transition, Can you give a few examples and explain how you count transitions. The only example you gave was a 4-bit value, and AA is 24 bits.

In reply to dave_59:
Hi Dave,
Thanks for your interest…

How to count the transitions:****
If you start either from LSB or from MSB, start counting the transitions for every 0 to 1 and 1 to 0 till the end then the total count will be your number of transitions.

Example:

  1. AA = 24’b0010_1011_1010;
    Transitions means the bit transitions like 0 to 1 or 1 to 0. So from above we have 7 transitions in the example 1.

  2. AA = 24’b101001010101;
    Here the bit transitions are 10.
    so, my requirement is these transitions in a randomized variable should not exceed the 16 that is violation.

see below example:
Violation case:__
3) AA = 24’b0110_1001_1001_0010_0101_1010_1010_1010
This example contains 24 transitions which is greater than 16 transitions which need to be avoided in the randomization of AA by using the constraint.

Hope now you got my point…

Thanking you,

Constraint to limit the transitions in 24bit register

class A;
rand bit [23:0] AA;
constraint AA_C {  
                 ($countones( AA ^ (AA>>1)) - 1) <= 16;
             
                }
endclass

In reply to avpchandra:

In reply to srikanth_6313:

Constraint to limit the transitions in 24bit register

class A;
rand bit [23:0] AA;
constraint AA_C {  
($countones( AA ^ (AA>>1)) - 1) <= 16;
}
endclass

In reply to avpchandra:

Hi Srikanth,
Please explain in detail your solution…? Your solution is working

Thanks,

In reply to avpchandra:
OK, we are getting closer. I think what you are saying is that if the adjacent bits in AA are not the same, that is a transition. If AA[4] is 1’b1 and AA[5] is 0, that counts as a transition. There can only be 23 adjacent bits in a 24-bit variable.

But your transition counts do not match my understanding. If I re-write your examples using _ everywhere that should be counted as transition

  1. AA = 24’b00000000000000_1_0_1_0_111_0_1_0; // 8 transitions
  2. AA = 24’b000000000000_1_0_1_00_1_0_1_0_1_0_1; // 11 transitions
  3. AA = 24’b0_11_0_1_00_11_00_1_00_1_00_1_0_11_0_1_0_1_0_1_0_1_0_1_0 // this is a 32 bit literal, but there are 24 transitions. There could only be 19 transitions with the 1st 24 bits.

In reply to avpchandra:

In reply to srikanth_6313:
Hi Srikanth,
Please explain in detail your solution…? Your solution is working
Thanks,

This constraint is not valid for all conditions. Consider a 4 bit number, AA = 4’b0011; Then, $countones((0011 ^ 0001) - 1) is 0. But number of bit transition is 1.

That was a limitation in the constraint if a number is starting with zeros. The below logic works fine with all combinations.

 
class A;
    rand bit [23:0]AA;
    
    constraint AA_C {
      if(AA[23] == 0)
        ($countones(AA ^ (AA>>1))) == 16;
      else 
        ($countones(AA ^ (AA>>1))-1) == 16;
    }
  
endclass

In reply to mayurkubavat:

In reply to srikanth_6313:

Great! Simple and Awesome!!