Constraint for walking pattern (walking 1's)

Hi All,

I am writing constraint for walking 1’s algorithm, but not able to get the required output

module p1;
  class c1;
    rand bit [7:0] value;
    constraint c1{
      foreach (value[m]){
        value[0] == 1;
        (m>0) -> (value[m]==(value[m-1]<<1));
      }
    }
    function void post_randomize;
      $displayb("value %b", value);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (8) begin
    obj.randomize();
    end
  end
endmodule

OUTPUT of the above code

value 00000001
value 00000001
value 00000001
value 00000001
value 00000001
value 00000001
value 00000001
value 00000001

But I am expecting the following output, where 1bit is left shifted everytime,

value 00000001
value 00000010
value 00000100
value 00001000
value 00010000
value 00100000
value 01000000
value 1000000

Could you please suggest?

Thank You,

Hi Mahesh,

You have used repeat(8) in initial, so expecting a new value everytime. So, I have used static variable in the class.

module p1;
  class c1;
    rand bit [7:0] value;
    static bit[2:0] a;
    constraint c1{
      value == 1 << (a);
    }
    function void post_randomize;
      a++;
      $displayb("value %b", value);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (8) begin
    obj.randomize();
    end
  end
endmodule

value 00000001
value 00000010
value 00000100
value 00001000
value 00010000
value 00100000
value 01000000
value 10000000

Please format your code making it easier for others to read . I have done that for both of you.

@Sabarish, In this case, you do not need to make the variable static. It works because the variable is not random.

Hi Dave,
Thanks for your response. Actually @Mahesh_K called constructor of “obj” once, hence I kept the variable “a” as static

A static class variable is useful when creating multiple object instances and you need to share a variable amongst them. If there is only one object instance, it serves no purpose.

1 Like

Thanks Dave. I have removed the static keyword.

module p1;
  class c1;
    rand bit [7:0] value;
    bit[2:0] a;
    constraint c1{
      value == 1 << (a);
    }
    function void post_randomize;
      a++;
      $displayb("value %b", value);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (8) begin
    obj.randomize();
    end
  end
endmodule



value 00000001
value 00000010
value 00000100
value 00001000
value 00010000
value 00100000
value 01000000
value 10000000

Thank You Sabarish and Dave.

Similarly, I was thinking about generating walking zero pattern, I tried with initialzing and shifting the value, but output is zero, could you give a hint to get the output?

module p1;
  class c1;
    rand bit [7:0] value;
    bit [2:0] a;
    constraint c1{
      foreach (value[j]){
        value[j] == value[0] << a;
      }
	}
    function void pre_randomize();
      value = 8'hFE;
      endfunction
      
   	function void post_randomize();
      a++;
      $displayb("value %b", value);
    endfunction
      
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (8)
      obj.randomize();
  end
endmodule

Expected output :

value 11111110
value 11111101
value 11111011
value 11110111
value 11101111
value 11011111
value 10111111
value 01111111

But actual output is 00000000

Hi Dave,
Before I put the code here, I copied into
https://markdown-it.github.io/
and then I pasted here.
Is this fine or somethings needs to be done in that webister before we paste the code here?

Thank You,

That link was just an example to show you how markdown works. Highlight your code and press the </> button above.

Your code does not work for a couple of reasons.

Setting the random variable value in pre_randomize() or anytime before randomization has no effect as the constraint solver will set it solely based on constraints.

The foreach loop in your constraint will set every bit of value to the same value.

You can use the same algorithm Sabarish gave you for walking 0’s, just invert the result.

1 Like