1、接口
使用方法:
a.首先例化一個接口,將testbench里的時鍾模塊傳進來;
b.例化一個testcase,將接口傳到testcase里面;
c.將DUT連接到接口上。
例子:
1 router_io top_io(SystemClock); //——>a 2 test t(top_io); //——>b 3 router dut( 4 .reset(top_io.reset_n), //——>c 5 .clock(top_io.clock) 6 );
即testcase驅動interface,interface驅動dut。
2、在sv中,logic類型替代了reg和wire類型數據。
3、enum
默認數據類型是int
格式:typedef enum [data_type]{name constant} enumtype;
例子:
1 typedef enum{IDLE,TEST,START} state;//沒有定義data_type,默認是int 2 enum bit[2:0] {s0='b001,s1='b010,s2='b100} st;
4、固定數組
格式:type array_name[size]{initial value}
注:$dimensions()函數,用於求數組的維度;
例子:
1 int c[2][3]='{{3,7,1},{5,1,9}};//2行3列,2是第一維,3是第二維 2 bit [31:0] a[2][3]=c; //[31:0]第三維,定義了一個三維數組a,並將c的值賦給a; 3 for(int i=0;i<$dimensions(a)); 4 $dispaly($size(a,i+1)); //2,3,32
5、動態數組
格式:typearray_name[][=initial_value]
例子:
1 reg [7:0] array[]=new[16]; 2 reg [7:0] ID,data_array; 3 ID = new[100]; 4 data_array=new[$size(ID)](ID);//給data_array分配一個和ID一樣大的空間,並將ID的內部值復制給data_array 5 data_array=ID;//直接復制
6、隊列
格式:type array_name[$][initial_value]
7、聯合數組
格式:type array_name[index_type];
type array_name[*];
注:index——>索引可以是數字、字符串和類
例子:
1 integer ID_array[*]; 2 ID_array[71]=99; //給地址71賦值99 3 ID_array.delete(71); //直接刪除元素
8、Array Method
a.function array_type[$] array.find() with (expression) //找到數組中匹配的元素,並將結果存到隊列中
b.function int_or_index[$] array.find_index() with (expression) //找出數組中匹配的元素,並將數組的索引存到隊列中
注:如果找不到匹配值,則返回空隊列。
Example:
1 program test; 2 bit [7:0] SQ_array[$] = {2,1,8,3,5}; 3 bit [7:0] SQ[$]; 4 int idx[$]; 5 SQ=SQ_array.find() with (item > 3);//SQ[$] contains 5,8 6 idx=SQ_array.find_index() with (item > 3);//idx[$] contains 2,4 7 endprogram
c.function array_type[$] array.find_first()[with (expression)]
//First element satisfying the with expression is return
//If with expression is omitted,first element is return
//First matching element is return in array_type[0]
d. function int_or_index_type[$] array.find_first_index [with (expression)]
//First index satisfying the with expression is return
//If with expression is omitted,first index is return
//First matching index is return in int_or_index_type[0]
注:如果找不到匹配值,則返回空隊列。
Example:
1 program test; 2 int array[]=new[4]; 3 int idx[$],value[$]; 4 foreach(array[i]) 5 array[i] = 4-i//{4,3,2,1} 6 value = array.find_first() with (item > 3);//value[0]=4 7 8 idx = array.find_first_index() with (item < 0);//idx.size()=0 9 endprogram 10 11 Examples of other array methods: 12 find_last(),find_last_index(),unique(),unique_index, 13 min(),max(),reverse(),sort(),rsort(),shuffle() 14 15 program test; 16 initial begin 17 int data[]=new[10],data_min[$],data_max[$]; 18 foreach(data[i]) begin 19 data[i] = 255 >> 1; 20 %dispaly("data[%0d] = %0d",i,data[i]); 21 end 22 $display("size of data array=%0d",data.size()); 23 $display("sum of array content=%0d",data.sum()); 24 data_max=data.max(); 25 data_min=data.min(); 26 $display("smallest value is =%0d",data_min[0]); 27 $display("largest value is = %0d",data_max[0]); 28 end 29 endprogram
9、System Function:Randomization
a.$random:返回一個32位的有符號隨機數
方法:$random(seed):set random seed for $random,seed不同,返回不同的隨機數;
b.$urandom:返回一個32位的無符號隨機數
方法:$urandom(seed):set random seed for $random,seed不同,返回不同的隨機數;
c.urandom_range():指定無符號隨機數的范圍
d.randcase:選擇一個可執行語句
1 randcase 2 10:f1(); 3 20:f2();//f2() is twice as likely to be executed as f1() 4 50:x=100; 5 30:randcase ... endcase;//randcase can be nested(嵌套) 6 endcase
10、用戶自定義類型:
a.Use typedef to creat a synonym for another type
1 typedef bit [31:0] uint; 2 typedef bit [0:5] bsix_t;//define new type 3 bsix_t my_var; //creat 6-bit variable
b.Use<type>'(<value>|<variable>) to dynamically convert data types
1 typedef bit [31:0] uint; 2 bit [7:0] payload[]; 3 int temp = $random; 4 payload = new[(temp%3)+2]; //(0,1,2,3,4) 5 payload = new[(uint'(temp)%3)+2];//(2,3,4) 6 payload = new[(unsigned'(temp)%3)+2];//{2,3,4}
11、Know your operators !!!
VCS對於含有x的運算結果返回x,如果此時用x去判斷的話,VCS會不知道怎么去判斷,應避免這種寫法:
1 reg [3:0] sample,rwf_data; 2 sample = dut.cb.dout[3:0]; 3 if(sample != ref_data) $display("Error!"); //對x無法控制 when sample = 4'b101x & ref_data = 4'b1010,VCS無法判斷 4 else $display("Pass!");
//應使用下面的方法
1 sample = dut.cb.dout[3:0]; 2 if(sample == ref_data); 3 else $display("Erros!");
12、函數和任務
區別:函數內部不消耗時間,任務內部可以消耗時間,函數不能調用任務,任務可以調用函數;
const ref——>只讀取值,不做改變
task里可以使用ref傳遞參數:
1 task print_sum(ref integer a[],input int start=0);//ref將數組的地址傳進來,此時如果外面的操作對此數組影響,此值也變化,它的變化也影響原有的值 2 //start=0 默認參數定義,調用此任務是可以task(a); 3 automatic int sum = 0;//聲明動態變量,每調用一次,sum的值都是變化的 4 for(int j = start;j<a.size();++) 5 sum += a[j]; 6 $display("sum of array is %0d",sum); 7 endtask 8 ... 9 print_sum(my_array);
Function Example:
1 function automatic int factorial(int n);//function只有輸入沒有輸出,但是有返回值,返回值就是函數名 2 satic int shared_value = 0; 3 if(n<2) return(1); 4 else return(n*factorial(n-1)); 5 endfunction 6 ... 7 result = factorial(my_valeu);
注:task缺省的參數默認是 logic 輸入:
1 task my_task(a,b,output bit [15:0] u,v,const ref byte c[]);//a,b默認為logic 輸入