Constraint Practice Questions

  1. Write constraint for 32-bit variable and randomize it so that only 2 bits change from previous values.
class packet;
      rand bit [31:0] curr_data;
      bit [31:0] pre_data;
        constraint c1 {$countones(curr_data ^ pre_data) == 2;}
      endclass
      initial begin
        packet pkt;
        pkt = new();
        pkt.pre_data = 32'ha5a5a5a5;
          pkt.randomize();
        $display ("pre_data = %0h,curr_data = %0h",pkt.pre_data,pkt.curr_data);
      end

2. Write SV code using constraint on array size < 10 and array contents sum =100.

class packet;
  rand bit[7:0] array [];
//  constraint c1 {array[1] > array[0];}
  constraint c2 {array.size() inside {[1:9]};}
  constraint c3 {array.sum() == 100;}
endclass
module test();
  initial begin
    packet p1 = new();
    repeat(10) begin
      p1.randomize();
      $display("array.size = %0d",p1.array.size());
      $display("sum of array elements = %0d",p1.array.sum());
      foreach(p1.array[i])
        $display("array[%0d] = %0d",i,p1.array[i]);
      $display("end of array \n");
        end
        end
endmodule

3.  Write a SV code using constraint to generate 10 ones without using $countones for a variable.

class packet;
  rand bit [3:0] data[];
  constraint c1 {data.size()==15;}
  constraint c2 {foreach (data[i]) data[i] inside {0,1};}
  constraint c3 {data.sum()==10;}
endclass
 
module tb();
  packet p1;
  initial begin
    p1 = new();
    repeat(10) begin
      if(p1.randomize()) begin
        $display("data = %0p",p1.data);
      end
      else begin
        $display("randomization failed");
      end
    end
  end

4. write SV code using constraint for generating multiples of 16 .

class packet;
  rand bit[31:0] data;
  constraint c1 {data < 200;}
  constraint c2 {foreach(data[i]) if(i<4) data[i]== 0;}
endclass
 
module tb();
  packet p1;
  initial begin
    p1 = new();
    repeat (8) begin
      p1.randomize();
      $display("data = %0d",p1.data);
    end
  end
endmodule

5. write the constraint for an 10bit variable and display the values in descending order and explain with example values.

class packet;
  rand bit[7:0] data[10];
  constraint c1 { foreach(data[i]) if(i<10) data[i+1] < data[i];}
  
endclass
 
module tb();
  initial begin
    packet p1 = new();
      p1.randomize();
      foreach(p1.data[j])$display("data[%0d] = %0d",j,p1.data[j]);
  end
endmodule

6. Write a constraint for storing the even numbers in odd locations and odd numbers in even locations by using dynamic array.

class packet;
  rand int array1[];
  constraint size {array1.size() == 10;}
  constraint even_odd {foreach(array1[i])
    if(i%2==0)
      array1[i]%2 == 1;
                      else
                        array1[i]%2 == 0;}
endclass
 
module tb();
  initial begin
    packet p1 = new();
    p1.randomize();
    foreach(p1.array1[i]) $display("array1[%0d] = %0d",i,p1.array1[i]);
  end
endmodule

7. Write a SV code using constraint for 16bit addr to generate power of 2.

class packet;
  rand bit[15:0] addr;
  
  constraint c1 {$onehot(addr);}
  constraint c2 {addr != 0;}
  
endclass

module tb;
  initial begin
    packet pkt = new();
    repeat(7) begin
      pkt.randomize();
      $display("addr = %0d",pkt.addr);
    end
  end
  
endmodule

8. Write a constraint on a 32-bit variable such that 7 bits are set consecutively and rest of the bits can also have bits set to 1 but consecutive bit set count cannot exceed 7.

class packet;
  rand bit[31:0] data;
  constraint c1 {foreach(data[i])
    if(i<26) !(data[i] && data[i+1] && data[i+2] && data[i+3] && data[i+4] && data[i+5] && data[i+6]);}
endclass
 
module test();
  initial begin
    packet pkt = new();
    repeat(10) begin
      pkt.randomize();
      $display("data = %0b",pkt.data);
    end
  end
endmodule

9. Write a SV code using constraint to Randomize a dynamic array to generate a pattern for each index 9 99 999 999…

class packet;
  rand int d_array[];
  constraint c1 {d_array.size() inside {[3:8]};}
  constraint c2 {foreach(d_array[i])
    if(i==0) d_array[i] == 9;
                else
                  d_array[i] == d_array[i-1] * 10 + 9;}
endclass
 
module tb;
  initial begin
    packet pkt = new();
    repeat (7) begin
      pkt.randomize();
      $display("d_array = %0p",pkt.d_array);
    end
  end
endmodule

10. Write a SV code using constraint such that when rand bit[3:0] a is randomized, the value of “a” should not be same as 5 previous occurrences of the value of “a”.

class packet;
  rand bit[3:0] a;
  bit[3:0] prev_a[$];
  
  constraint c1 {!(a inside {prev_a});}
  
  function void post_randomize();
    prev_a.pop_front();
    prev_a.push_back(a);
  endfunction
  
endclass

module tb();
  
  initial begin
    packet pkt = new();
    pkt.prev_a = {1,2,3,4,6};
    repeat(7) begin
      pkt.randomize();
      $display("a = %0d",pkt.a);
    end
  end
  
endmodule