SV_5_Data Type2

1. Array

  • 数组是所有相同类型变量的集合,并用名称加索引进行访问;
  • 在数组名之前声明的为数组宽度,数组名之后的为数组维度;

1.1 Packed and unpacked array

  • 在SV里packed array指只有在数组名之前做了声明,带有数组宽度的数组,         unpacked array 指只有在数组名之后做了声明,带有数组维度的数组;
 1 module packed_unpacked_data();
 2 
 3 // packed array
 4 bit [7:0] packed_array = 8'hAA; 
 5 // unpacked array
 6 reg unpacked_array [7:0] = '{0,0,0,0,0,0,0,1}; 
 7 
 8 initial begin
 9   $display ("packed array[0]   = %b", packed_array[0]);
10   $display ("unpacked array[0] = %b", unpacked_array[0]);
11   $display ("packed array      = %b", packed_array);
12   // Below one is wrong syntax
13   //$display("unpacked array[0] = %b",unpacked_array);
14   #1 $finish;
15 end
16 
17 endmodule
18 
19 //compile result
20  packed array[0]   = 0
21  unpacked array[0] = 1
22  packed array      = 10101010

1.2 Multi-dimension arrays

将packed array 与 unpacked array 相结合,具体用法详见实例:

 1 module arrays_data();
 2 
 3 // 2 dimension array of Verilog 2001
 4 reg [7:0] mem [0:3] = '{8'h0,8'h1,8'h2,8'h3};
 5 // one more example of multi dimention array
 6 reg [7:0] mem1 [0:1] [0:3] = 
 7    '{'{8'h0,8'h1,8'h2,8'h3},'{8'h4,8'h5,8'h6,8'h7}};
 8 // One more example of multi dimention array
 9 reg [7:0] [0:4] mem2 [0:1] = 
10    '{{8'h0,8'h1,8'h2,8'h3},{8'h4,8'h5,8'h6,8'h7}};
11 // One more example of multi dimention array
12 reg [7:0] [0:4] mem3 [0:1] [0:1]  = 
13    '{'{{8'h0,8'h1,8'h2,8'h3},{8'h4,8'h5,8'h6,8'h7}},
14    '{{8'h0,8'h1,8'h2,8'h3},{8'h4,8'h5,8'h6,8'h7}}};
15 // Multi arrays in same line declaration
16 bit [7:0] [31:0] mem4 [1:5] [1:10], mem5 [0:255]; 
17 
18 initial begin
19   $display ("mem[0]            = %b", mem[0]);
20   $display ("mem[1][0]         = %b", mem[1][0]);
21   $display ("mem1[0][1]        = %b", mem1[0][1]);
22   $display ("mem1[1][1]        = %b", mem1[1][1]);
23   #1 $finish;
24 end
25 
26 endmodule
27 
28 //compile result
29     
30  mem[0]            = 00000000
31  mem[1][0]         = 1
32  mem1[0][1]        = 00000001
33  mem1[1][1]        = 00000101

1.3 Dynamatic arrays

动态数组是一个在运行时可以更改的unpacked array。

  • new[] : 用于创建和更改的数组;
  • size(): 返回数组的大小;
  • delete() : 清空数组的所有元素。
 1 module dynamic_array_data();
 2 
 3 // Declare dynamic array
 4 reg [7:0] mem [];
 5 
 6 initial begin
 7   // Allocate array for 4 locations
 8   $display ("Setting array size to 4");
 9   mem = new[4];
10   $display("Initial the array with default values");
11   for (int i = 0; i < 4; i ++) begin
12     mem[i] = i;
13   end
14   // Doubling the size of array, with old content still valid
15   mem = new[8] (mem);
16   // Print current size
17   $display ("Current array size is %d",mem.size());
18   for (int i = 0; i < 9; i ++) begin
19     $display ("Value at location %g is %d ", i, mem[i]);
20   end
21   // Delete array
22   $display ("Deleting the array");
23   mem.delete();
24   $display ("Current array size is %d",mem.size());
25   #1 $finish;
26 end
27 
28 endmodule
29 
30 //compile result
31 Setting array size to 4
32  Initial the array with default values
33  Current array size is          8
34  Value at location 0 is   0 
35  Value at location 1 is   1 
36  Value at location 2 is   2 
37  Value at location 3 is   3 
38  Value at location 4 is   x
39  Value at location 5 is   x 
40  Value at location 6 is   x 
41  Value at location 7 is   x 
42  Deleting the array
43  Current array size is          0

1.4 Associative arrays

  • 动态数组对于处理数量动态变化的连续变量集合很有用,当集合的大小或者数据稀疏时,关联数组(又称哈希数组)是更好的选择;
  • 关联数组在使用之前不会分配任何存储空间,索引表达式不受integer表达式的限制,可以是任意类型;
  • 索引可以是integer, string, class, *;

1.4.1 Associative arrays methods

 Methods Description
 exists() 目标数组的指定索引处是否存在元素,存在返回元素,否则0;
 first() 将指定数组的第一个最小索引值赋给索引变量;
 last() 将指定数组的最后一个最大索引值赋给索引变量;
next()  查找索引大于给定索引的条目;
prev()  查找索引小于给定索引的条目;
num()  返回关联数组的条目数;
delete()  清空关联数组

1.4.2 Integer or int index

  • 包含x,z的四态索引无效;
  • 小于integer的索引会被扩展到32位;
  • 索引是有符号的,顺序是与符号相关的;
  • 索引可以是表达式;
  • 例子: int array_name [ integer ];

1.4.3 string index

  • 空字符串索引 "" 是无效的;
  • 顺序遵循字母顺序;
  • 索引可以是任意长度的字符串;
  • 例子: int array_name [ string ];

1.4.4 class index

  • 空索引无效;
  • 顺序是确定的;
  • 索引可以是对象或者派生对象;
  • 例子: int array_name [ some_Class ];

1.4.5 wild character index 

  • 包含x,z的四态索引无效;
  • 索引是无符号的;
  • 索引表达式是自信定义的
  • 排序遵循数字大小;
  • 字符串文字会自动转换为同等大小的bit-向量;
  • 数组可以被任何整型索引引用;
  • 例子: int array_name [*];

例子:

 1 module integer_associative_array ();
 2 
 3 integer as_mem [integer];
 4 
 5 integer i;
 6 
 7 initial begin
 8   // Add element array
 9   as_mem[100] = 101;
10   $display ("value stored in 100 is %d", 101);
11   as_mem[1]   = 100;
12   $display ("value stored in 1   is %d", 100);
13   as_mem[50]   = 99;
14   $display ("value stored in 50  is %d", 99);
15   as_mem[256] = 77;
16   $display ("value stored in 256 is %d", 77);
17   // Print the size of array
18   $display ("size of array is %d", as_mem.num());
19   // Check if index 2 exists
20   $display ("index 2 exists   %d", as_mem.exists(2));
21   // Check if index 100 exists
22   $display ("index 100 exists %d", as_mem.exists(100));
23   // Value stored in first index
24   if (as_mem.first(i)) begin
25     $display ("value at first index %d value %d", i, as_mem[i]);
26   end
27   // Value stored in last index
28   if (as_mem.last(i)) begin
29     $display ("value at last index  %d value %d", i,  as_mem[i]);
30   end
31   // Delete the first index
32   as_mem.delete(100);
33   $display ("Deleted index 100");
34   // Value stored in first index
35   if (as_mem.first(i)) begin
36     $display ("value at first index %d value %d", i, as_mem[i]);
37   end
38   #1 $finish;
39 end
40 
41 endmodule
42 
43 //compile result
44     
45  value stored in 100 is         101
46  value stored in 1   is         100
47  value stored in 50  is          99
48  value stored in 256 is          77
49  size of array is          4
50  index 2 exists            0
51  index 100 exists          1
52  value at first index           1 value         100
53  value at last index          256 value          77
54  Deleted index 100
55  value at first index           1 value         100

2. Queue

  • 队列是一个大小可变、有序的同构元素集合;
  • 队列中的每一个元素的位置都由序号标识,0标识第一个,$表示最后一个;
  • 队列类似于一维的unpacked 数组,会自动收缩,增加,数组的方法也同样适用于队列;

2.1 一些操作符

  • 0:用于访问队列的第一个元素;
  • $:用于访问队列的最后一个元素;
  • {}:与0 和$ 配合使用,增加删除队列中的元素;

2.2 Queue methods

Method

Description

insert()

在指定的索引位置插入元素;

delete()

在指定的索引位置删除元素;

pop_front()

删除队列第一个元素,并返回新的队列;

pop_back()

删除队列最后一个元素,并返回新的队列;

push_front()

将给定的元素插入到队列的首位;

push_back()

将给定的元素插入到队列的末位;

size()

返回队列的大小;

2.3 例子:

 1 module queue_data();
 2 
 3 // Queue is declated with $ in array size
 4 integer queue[$] = { 0, 1, 2, 3, 4 };
 5 integer i;
 6 
 7 initial begin
 8   $display ("Initial value of queue");
 9   print_queue;
10   // Insert new element at begin of queue
11   queue = {5, queue};
12   $display ("new element added using concate");
13   print_queue;
14   // Insert using method at begining
15   queue.push_front(6);
16   $display ("new element added using push_front");
17   print_queue;
18   // Insert using method at end
19   queue.push_back(7);
20   $display ("new element added using push_back");
21   print_queue;
22   // Using insert to insert, here 4 is index
23   // and 8 is value
24   queue.insert(4,8);
25   $display ("new element added using insert(index,value)");
26   print_queue;
27   // get first queue element method at begining
28   i = queue.pop_front();
29   $display ("element poped using pop_front");
30   print_queue;
31   // get last queue element method at end
32   i = queue.pop_back();
33   $display ("element poped using pop_end");
34   print_queue;
35   // Use delete method to delete element at index 4 in queue
36   queue.delete(4);
37   $display ("deleted element at index 4");
38   print_queue;
39   #1 $finish;
40 end
41 
42 task print_queue;
43   integer i;
44   $write("Queue contains ");
45   for (i = 0; i < queue.size(); i ++) begin
46     $write (" %g", queue[i]);
47   end
48   $write("\n");
49 endtask
50 
51 endmodule
52 
53 //compile result
54     
55  Initial value of queue
56  Queue contains  0 1 2 3 4
57  new element added using concate
58  Queue contains  5 0 1 2 3 4
59  new element added using push_front
60  Queue contains  6 5 0 1 2 3 4
61  new element added using push_back
62  Queue contains  6 5 0 1 2 3 4 7
63  new element added using insert(index,value)
64  Queue contains  6 5 0 1 8 2 3 4 7
65  element poped using pop_front
66  Queue contains  5 0 1 8 2 3 4 7
67  element poped using pop_end
68  Queue contains  5 0 1 8 2 3 4
69  deleted element at index 4
70  Queue contains  5 0 1 8 3 4

3. methods

下面的方法对数组、队列都是适用的;

Method

Description

sum

返回所有数组元素的和;

product

返回所有数组元素的乘积

and

按位返回所有数组元素的and(&)

or

按位返回所有数组元素的或(|)

xor

按位返回所有数组元素的异或(^)

min

返回数组的最小元素;

max

返回数组的最大元素;

unique

返回所有不重复、唯一的元素;

例子:

 1 module array_methods();
 2 
 3 int data [0:9] = '{1,2,3,6,5,7,8,9,9,2};
 4 int queue [$];
 5 
 6 initial begin
 7   queue = data.min;
 8   $display("Min size element is %0d",queue.pop_front());
 9   queue = data.max;
10   $display("Max size element is %0d",queue.pop_front());
11   $display("Sum of array %0d",data.sum);
12   $display("Product of array %0d",data.product);
13   $display("XOR of array %0d",data.xor);
14   $display("AND of array %0d",data.and);
15   $display("OR  of array %0d",data.or);
16 end
17 
18 endmodule
19 
20 //compile result;
21     
22  Min size element is 1
23  Max size element is 9
24  Sum of array 52
25  Product of array 1632960
26  XOR of array 14
27  AND of array 0
28  OR  of array 15

4. $cast:动态类型转换

动态类型转换允许将一个数据类型分配给另一个数据类型以实现数据类型转换;

  • $cast可以作为函数或任务使用;
  • 1 task $cast( singular dest_handle, singular source_handle );
    2 or
    3 function int $cast( singular dest_handle, singular source_handle );
    
  •  
    • sourc_exp Source expressio.
    • dest_var Destination variable.

例子:

 1 module cast();
 2 
 3 typedef enum {IDLE,SFD,PREAMBLE,
 4               DATA,FCS,EFD} eth_state;
 5 
 6 eth_state fsm;
 7 
 8 initial begin
 9  #5 fsm = IDLE;
10  $display("@%0dns Current value of FSM : %s\n",
11     $time,get_name(fsm));
12  #5 fsm = SFD;
13  $display("@%0dns Current value of FSM : %s\n",
14     $time,get_name(fsm));
15  #1 $cast(fsm,1+2);
16  $display("@%0dns Current value of FSM : %s\n",
17     $time,get_name(fsm));
18  // Below line should give compile error
19  //fsm = 2 + 1;
20 end
21 
22 eth_state temp;
23 eth_state temp2;
24 integer i ;
25 
26 initial begin
27   #20;
28   for(i = 0; i <= 5; i++) begin
29     $cast(temp,i);
30     $display("Value of temp is %s",
31          get_name(temp.next()));
32   end
33 end
34 
35 function string get_name;
36   input eth_state lstate;
37   case(lstate)
38     IDLE     : get_name = "IDLE";
39     SFD      : get_name = "SFD";
40     PREAMBLE : get_name = "PREAMBLE";
41     DATA     : get_name = "DATA";
42     FCS      : get_name = "FCS";
43     EFD      : get_name = "EFD";
44   endcase
45 endfunction
46 
47 endmodule
48 
49 //compile result
50     
51  @5ns Current value of FSM : IDLE
52  
53  @10ns Current value of FSM : SFD
54  
55  @11ns Current value of FSM : DATA
56  
57  Value of temp is SFD
58  Value of temp is PREAMBLE
59  Value of temp is DATA
60  Value of temp is FCS
61  Value of temp is EFD
62  Value of temp is IDLE

具体内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值