CONSTRAINT ON AN ARRAY WITH SET BITS

Hello All,

I want to write a constraint on a variable “CHOOSE” that depends one a variable “AVAILABLE” with following rules : -

  1. AVAILABLE IS OF 16 BITS
    If AVAILABLE ='hFFFF means CHOOSE can be → {0,15} (as each AVAILABLE[0] to AVAILABLE[15] is set )
    if AVAILABLE ='hFFF3 means CHOOSE can be → any value {0,15} except 2,3

So it follows that CHOOSE can be value corresponding to the bit positions set in AVAILABLE. Ias there anyway to write it in constraint.

I have another way of doing it through do while loop as shown below.

if(AVAILABLE>0)
begin
do
CHOOSE =$urandom_range(0,15);
while(!AVAILABLE[CHOOSE]); ///will select random value whose bits are set
end

But I want to put it in constraint.Is there any way of doing the same thing in construct.

Thanks In advance.

In reply to prashantk:


rand bit [15:0] available;
rand bit [3:0]  choose;

constraint c {
   foreach(available[i])
      (available[i] == 0) -> (choose != i);
   solve available before choose;
}

// alternative way, not sure if it compiles lol
constraint b {
   available[choose] == 1;
   solve available before choose;
}

// same, I prefer this one, ha
constraint a {
   available | (1 << choose) == available;
   solve available before choose;
}



In reply to javatea:

Thanks for the reply Javatea. The problem with the above solution is that, available need not to be changed as it will be coming from some parameter which has to be fixed.In the above solution “available” will also get randomize which is not required .

In reply to prashantk:

It helps to show the declarations of the variables you describe. Also, what happens when AVAILABLE == 0?

class bit [15:0] available;
  rand bit [3:0]  choose;
  bit [3:0] list[$];
  function pre_randomize();
     list = {};
     foreach (available[v]) if (available[v]) list.push_back(v);
  endfunction
  constraint c { choose inside {list};}
endclass

In reply to prashantk:

make sure your requirement clear. if you want to put in a class, rand or not rand doesn’t matter.


class my_item;
  rand bit [15:0] available;
  rand bit [3:0]  choose;
  constraint a {
    available != 0;
    available | (1 << choose) == available;
  }
endclass
bit [15:0] AVAILABLE = 'hFF; // given value
my_item item = new();
item.randomize() with (available == AVAILABLE);

if you give me fixed available, and only want to have valid choose, this is good enough.


  bit [15:0] available;
  bit [3:0]  choose;
  std::randomize(available) with {available != 0;}; // given some value
  std::randomize(choose) with {(available | (1<<choose)) == available;}; // inline randomize

In reply to dave_59:

Thanks dave. Thats the correct implementation.It is to be noted that AVAILABLE should be at least=1 (which is a requirement )

You can use following solution also, although this solution may not work with all compilers. I have tested it works with VCS as it allows rand variable as array index inside constraints.

class array_bits;
 bit [15:0] available;
 rand bit [3:0]  choose;
 
 constraint c {
   (available[choose] == 1);
 }
endclass

program test;
	array_bits c;
	initial begin
	  c = new;
	  c.available = 16'hf0ff;
	  repeat (10) begin
	  	assert (c.randomize());
	  	$display("%p",c);
	  end
	end
endprogram

Workaround for compiler would be to try following, as its similar to above expression. This should work with all compilers.

 constraint c {
   (((available >> choose) & 1'h1) == 1);                
 }


  constraint c_choose {
    solve available before choose;
    ((1 << choose) & available) != 0;
  }