Tuesday, August 9, 2016

SV constraint program for shuffle


Here is the program to generate random array of signed integers which is shuffle of another random array.  This exhibits significance of array methods like sum,product or xor in the constraint.



class cfg_c;
  rand int signed A[];
  rand int signed B[];
 
  constraint A_B_size_cnstr{
    A.size() inside {[4:5]}; // can modify this
    B.size() == A.size();
  }
 
  constraint B_elements_cnstr{
    solve A.size() before A;
    solve B.size() before B;
    foreach(B[i])
      B[i] inside {A};    
    B.sum() == A.sum();
    (B.xor() ^ A.xor()) == 0;
    B.product() == A.product();
  }
endclass : cfg_c

module test;
  cfg_c cfg;
  initial begin
    cfg = new();
    repeat(10) begin
               cfg.randomize();
               $display("After randomization..");
               foreach(cfg.A[i])
                              $display("A[%0d] = %0d :: B[%0d] = %0d", i, cfg.A[i], i, cfg.B[i]);
    end
  end
endmodule : test


Result:-
After randomization..
A[0] = -2061227070 :: B[0] = -945796533
A[1] = 577051819 :: B[1] = -287061759
A[2] = -945796533 :: B[2] = 577051819
A[3] = -287061759 :: B[3] = -2061227070
A[4] = -1856519535 :: B[4] = -1856519535

3 comments:







  1. class c;
    rand bit[5:0] a[];
    rand bit[5:0] b[];

    constraint cc {
    a.size == 10;
    a.size == b.size;
    foreach(a[i])
    a[i] inside {b};
    foreach(b[i])
    b[i] inside {a};

    }
    endclass

    I think we can do it like what I have mentioned above too.

    ReplyDelete
  2. Hi Timoothy,
    The following way also work for your problem
    class cfg_c;
    rand int signed A[];
    rand int signed B[];

    constraint A_B_size_cnstr{
    A.size() inside {[4:5]}; // can modify this
    B.size() == A.size();
    }

    constraint B_elements_cnstr{
    solve A.size() before A;
    solve B.size() before B;
    foreach(B[i])
    B[i] inside {A};
    foreach(B[i])
    B[i] != A[i];
    B.sum() == A.sum();
    //(B.xor() ^ A.xor()) == 0;
    //B.product() == A.product();
    }
    endclass : cfg_c

    module test;
    cfg_c cfg;
    initial begin
    cfg = new();
    repeat(3) begin
    cfg.randomize();
    $display("After randomization..");
    foreach(cfg.A[i])
    $display("A[%0d] = %0d :: B[%0d] = %0d", i, cfg.A[i], i, cfg.B[i]);
    end
    end
    endmodule : test

    ReplyDelete
    Replies
    1. After randomization..
      A[0] = -1340462725 :: B[0] = 1076854330
      A[1] = -1922948115 :: B[1] = -1340462725
      A[2] = 1071517480 :: B[2] = -1922948115
      A[3] = 1076854330 :: B[3] = -1518083038
      A[4] = -1518083038 :: B[4] = 1071517480
      After randomization..
      A[0] = -330503887 :: B[0] = -13946166
      A[1] = -13946166 :: B[1] = 443178597
      A[2] = -2041423497 :: B[2] = -1131917638
      A[3] = -1131917638 :: B[3] = -2041423497
      A[4] = 443178597 :: B[4] = -330503887
      After randomization..
      A[0] = -32083502 :: B[0] = -1716998963
      A[1] = 1593681114 :: B[1] = 256280241
      A[2] = 1628166723 :: B[2] = -32083502
      A[3] = -1716998963 :: B[3] = 1628166723
      A[4] = 256280241 :: B[4] = 1593681114

      Delete