SV SystemVerilog陣列約束方法總結

2021-10-08 03:58:35 字數 3435 閱讀 5888

前言:在驗證工作中,陣列是我們最常用的資料結構之一了,那麼如何對陣列元素做約束呢?本文將給出一些實戰的例子。除此外,之前介紹過的unique約束(link)也可以產生具有唯一元素的陣列。

class dynamic_size_array();

rand logic [31:0] dyn_array;

constraint cst_dyn_array ;

foreach(dyn_array[i]) ;

}}endclass

特別注意:求和越界的問題!

在sv中宣告陣列,如果陣列元素的位寬是8bit,那麼預設求和之後還是8bit,這樣就會導致越界。因此對於陣列求和的約束需要注意以下2個點:

注意位寬,預估求和之後的資料有多大

注意約束陣列中的每個元素。

program array_sum_constraint();

parameter max_transaction_len = 10;

class strobe_pat;

rand bit strobe[max_transaction_len];

constraint cst_set_length ;}}

endclass

initial begin

strobe_pat strobe_pat_i;

strobe_pat_i = new();

assert(strobe_pat_i.randomize()) else $error("strobe_pat_i randomize error.");

$display("strobe_pat_i.strobe.sum() = %0d", strobe_pat_i.strobe.sum());

foreach(strobe_pat_i.strobe[i]) begin

$display("strobe_pat_i.strobe[%0d] = %0d", i, strobe_pat_i.strobe[i]);

endend

endprogram

systemverilog可以用foreach對陣列的每乙個元素進行約束(防止求和越界、每個元素要有意義)。和直接寫出的對固定大小的陣列的每乙個元素的約束相比,使用foreach要更簡潔。比較實際的做法就是使用foreach約束動態陣列和佇列。

class good_sum;

rand uint len;

constraint cst_len ;

}len.sum < 1024;

len.size() inside ;

}endclass

class ascend;

rand unit d[10];

constraint cst_d

}}endclass

class unique_slow;

rand bit [7:0] ua[64];

constraint cst_ua

}}endclass

class randc_value;

randc bit [7:0] val;

endclass

class list_unique_array;

bit [7:0] ua[64];

function void pre_randomize();

randc_value rand_val;

rand_val = new();

foreach(ua[i]) begin

assert(rand_val.randomize());

ua[i] = rand_val.val;

endendfunction

endclass

class randc_range;

randc bit [15:0] value;

int max_value;

constraint cst_value_range

} function new(int max_value = 10);

this.max_value = max_value;

endfunction

endclass

class unique_array;

int max_array_size;

int max_value;

rand bit [7:0] array;

function new(int max_array_size=2, max_value=2);

this.max_array_size = max_array_size;

if(max_value < max_array_size)

this.max_value = max_array_size;

else

this.this.max_value = max_value;

endfunction

constraint array_size ;

} function void post_randomize();

randc_range rand_val;

rand_val = new(max_value);

foreach(array[i]) begin

assert(rand_val.randomize());

array = rand_val.value;

endendfunction

function void display()

$display("array.size() = %0d", array.size());

foreach(array[i])

$display("array[%0d] = 0x%0d", i, array[i]);

endfunction

endclass

program unique_array_inst();

unique_array unique_array_inst;

initial begin

unique_array_inst = new(50);

repeat(10) begin

assert(unique_array_inst.randomize());

unique_array_inst.display();

endend

endprogram

javascript Array陣列方法總結

1 陣列方法 1.arr.concat arr1 把arr1拼接早arr後邊 2.arr.indexof 1 判斷某個值是否在陣列中,如果有返回索引號,日過如果沒有返回 1 3.arr.join 用指定的拼接陣列為字串 2 必須記住的四個陣列方法 1.push 最後一位追加。2.pop 刪除最後一位...

陣列簡單總

1 定義格式 型別 陣列名 元素個數 2 遍歷 按順序檢視陣列的每乙個元素 for i 0 i 3 注意事項 如果想在定義陣列的同時進行初始化,陣列元素個數必須是常量或者是常量表示式,或者不寫 正確寫法 int count 5 int ages count ages 0 1 錯誤寫法 int age...

差分約束系統的粗淺總

前些日子,看了下這個差分約束系統,今天刷了幾道題目,就稍微總結下。有x1,x2,x3.xn這些未知量,出現一系列xi xj k k是常量,題目給出 的不等式,問這些等式有沒有解,或者求xn x1的最大值 最小值 就可以向差分約束系統方面考慮。附註 有時候是給出xi xj k,那就轉化成xj xi k...