Constraint issue

Hi,
I’m trying to write a simple constraint like the one below:

class my_class extends uvm_obect;
    rand int  a;
    rand int  b;
    rand int  c;


    constraint partition_c
    {
        a >= 0 ;
        b >= 0 ;
        c >= 0 ;

        (a+ b+ c) < 1212;
        (a+ b+ b) >= 500;
    }

endclass

when I randomize the class after creating it a, b and c are way out of the constraint limits I mentioned. does anyone see why?

Thanks!

In reply to moshiko:

Hi,
I’m trying to write a simple constraint like the one below:

class my_class extends uvm_obect;
rand int  a;
rand int  b;
rand int  c;
constraint partition_c
{
a >= 0 ;
b >= 0 ;
c >= 0 ;
(a+ b+ c) < 1212;
(a+ b+ b) >= 500;
}
endclass

when I randomize the class after creating it a, b and c are way out of the constraint limits I mentioned. does anyone see why?
Thanks!

forgot to say that I registered the class to the fctpry

In reply to moshiko:

what is your expected output for a, b, and c ?

In reply to kddholak:

I do not understand why you are complaining about. Look here it is perfect

These are classic randomization problems. I’ll show examples with 2 variables.

First, it looks like you don’t want negative values. Instead of constraining the values, change how you declare the variables. I’m going to use 8-bits instead of 32 as this is easier to read. Instead of byte or int, use the unsigned bit type.

class sum12;
rand bit [7:0] a, b; // 8-bits, 0..255 
constraint c12 {
  (a + b) == 12;
}
endclass

This will give you positive values that add up to 12.

Wait - that ‘naked’ 12 looks bad. Shouldn’t it have a size, like in my RTL? Try this new class.

class sum12_8;
rand bit [7:0] a, b; // 8-bits, 0..255 
constraint c12_8 {
  (a + b) == 8'd12; // 8-bit value - fails!
}
endclass

Now you will sometimes get large values such as 250 and 18 that are bigger than 12. Why?

250+18 = 268. The above constraint does 8-bit math 268 modulo 256 is … 12! The original constraint worked as ‘12’ is actually 32’d12. If you use 9’d12, all the math is done with 9-bit precision, and you will get small values of a and b.