Tuesday, August 16, 2016

SV program for tic tac toe game

Another example of sum method in the constraint for 2D array.

module ttt;
class class1;

rand int unsigned a[3][3];
rand int unsigned i,j;

constraint c3 {(a.sum() == 5);}
constraint c4 {foreach(a[i,j]) (a[i][j] >=0 && a[i][j]<=1);}

endclass: class1

initial
begin
class1 cl = new();


repeat (10) begin
  cl.randomize();

$display("Game Over:");
$display("%d %d %d", cl.a[0][0],cl.a[0][1],cl.a[0][2]);
$display("%d %d %d", cl.a[1][0],cl.a[1][1],cl.a[1][2]);
$display("%d %d %d", cl.a[2][0],cl.a[2][1],cl.a[2][2]);
end
end

endmodule


Result:-
Game Over:
         1          1          0
         1          1          0
         0          0          1
Game Over:
         1          1          0
         1          1          0
         0          1          0
Game Over:
         0          1          0
         1          1          1
         0          0          1
Game Over:
         1          0          1
         0          0          1
         1          1          0
Game Over:
         1          0          1
         0          1          1
         0          1          0
Game Over:
         0          1          1
         0          1          0
         1          1          0
Game Over:
         0          1          1
         1          0          1
         0          0          1
Game Over:
         0          0          1
         1          1          0
         1          0          1
Game Over:
         0          0          0
         0          1          1
         1          1          1
Game Over:
         1          1          0
         1          0          1
         0          0          1

           V C S   S i m u l a t i o n   R e p o r t 

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