SystemVerilog约束用于两个2D阵列之间的映射

时间:2018-03-12 00:26:26

标签: mapping constraints system-verilog

有两个MxN 2D阵列:

rand bit [M-1:0] src [N-1:0]; 
rand bit [M-1:0] dst [N-1:0];

它们都将被随机分配分开,这样它们的P值为1'b1,其余为1'b0。

名为'map'的第三个MxN整数数组在两个数组'src'和'dst'之间建立一对一的映射。

rand int [M-1:0] map [N-1:0]; 

需要'map'的约束,以便在随机化之后,对于src [i] [j]的每个元素,其中src [i] [j] == 1'b1,map [i] [j] == M当dst [k] [l] ==时,* k + l。对于映射的每个非零元素,k和l必须是唯一的。

举个例子: 设M = 3,N = 2。

让src成为

[1   0   1
 0   1   0]

让dst成为

[0   1   1
 1   0   0]

然后一个可能的'map'随机化将是:

[3   0   1
 0   2   0]

在上面的地图中:

  • 3表示从src [0,0]指向dst [1,0](3 = 1 * M + 0)
  • 1表示从src [0,2]指向dst [0,1](1 = 0 * M + 1)
  • 2表示从src [1,1]指向dst [0,2](2 = 0 * M + 2)

1 个答案:

答案 0 :(得分:2)

这很难表达为SystemVerilog约束,因为

  1. 无法有条件地选择数组中的元素是唯一的
  2. 您不能将随机变量作为数组元素的索引表达式的一部分。
  3. 由于您分别随机化srcdst,因此计算指针可能更容易,然后随机选择要填充地图的指针。

      module top;
       parameter M=3,N=4,P=4;
       bit  [M-1:0] src [N];
       bit  [M-1:0] dst [N];
       int  map [N][M]; 
       int  pointers[$];
    
       initial begin
          assert( randomize(src) with {src.sum() with ($countones(item)) == P;} );
          assert( randomize(dst) with {dst.sum() with ($countones(item)) == P;} );
          foreach(dst[K,L]) if (dst[K][L]) pointers.push_back(K*M+L);
          pointers.shuffle();
          foreach(map[I,J]) map[I][J] = pointers.pop_back();
          $displayb("%p\n%p",src,dst);
          $display("%p",map);
       end
    endmodule