ch2-Array Manipulation

SystemVerilog Array Manipulation

There are many built-in methods in SystemVerilog to help in array searching and ordering.

Array manipulation methods simply iterate through the array elements and each element is used to evaluate the expression specified by the with clause. The iterator argument specifies a local variable that can be used within the with expression to refer to the current element in the iteration. If an argument is not provided, item is the name used by default.

Specifying an iterator argument without the with clause is illegal.

Array Locator Methods

The with clause and expresison is mandatory for some of these methods and for some others its optional.

Mandatory ‘with’ clause

​ These methods are used to filter out certain elements from an existing array based on a given expression. All such elements that satisfy the given expression is put into an array and returned. Hence the with clause is mandatory for the following methods.

Method name Description
find() Returns all elements satisfying the given expression
find_index() Returns the indices of all elements satisfying the given expression
find_first() Returns the first element satisfying the given expression
find_first_index() Returns the index of the first element satisfying the given expression
find_last() Returns the last element satisfying the given expression
find_last_index() Returns the index of the last element satisfying the given expression

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};
int res[$];

initial begin
res = array.find(x) with (x > 3);
$display ("find(x) : %p", res);

res = array.find_index with (item == 4);
$display ("find_index : res[%0d] = 4", res[0]);

res = array.find_first with (item < 5 & item >= 3);
$display ("find_first : %p", res);

res = array.find_first_index(x) with (x > 5);
$display ("find_first_index: %p", res);

res = array.find_last with (item <= 7 & item > 3);
$display ("find_last : %p", res);

res = array.find_last_index(x) with (x < 3);
$display ("find_last_index : %p", res);
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
ncsim> run
find(x) : '{4, 7, 5, 7, 6}
find_index : res[0] = 4
find_first : '{4}
find_first_index: '{1}
find_last : '{6}
find_last_index : '{8}
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img

Optional ‘with’ clause

Methods Description
min() Returns the element with minimum value or whose expression evaluates to a minimum
max() Returns the element with maximum value or whose expression evaluates to a maximum
unique() Returns all elements with unique values or whose expression evaluates to a unique value
unique_index() Returns the indices of all elements with unique values or whose expression evaluates to a unique value

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};
int res[$];// 返回的类型是队列

initial begin
res = array.min();
$display ("min : %p", res);

res = array.max();
$display ("max : %p", res);

res = array.unique();
$display ("unique : %p", res);

res = array.unique(x) with (x < 3);
$display ("unique : %p", res);

res = array.unique_index;
$display ("unique_index : %p", res);
end
endmodule

Simulation Log

1
2
3
4
5
6
7
ncsim> run
min : '{1}
max : '{7}
unique : '{4, 7, 2, 5, 1, 6, 3}
unique : '{4, 2}
unique_index : '{0, 1, 2, 3, 5, 6, 7}
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img

Array Ordering Methods

These methods operate and alter the array directly.

Method Description
reverse() Reverses the order of elements in the array
sort() Sorts the array in ascending order, optionally using with clause
rsort() Sorts the array in descending order, optionally using with clause
shuffle() Randomizes the order of the elements in the array. with clause is not allowed here.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module tb;
int array[9] = '{4, 7, 2, 5, 7, 1, 6, 3, 1};

initial begin
array.reverse();
$display ("reverse : %p", array);

array.sort();
$display ("sort : %p", array);

array.rsort();
$display ("rsort : %p", array);

for (int i = 0; i < 5; i++) begin
array.shuffle();
$display ("shuffle Iter:%0d = %p", i, array);
end
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
9
10
ncsim> run
reverse : '{1, 3, 6, 1, 7, 5, 2, 7, 4}
sort : '{1, 1, 2, 3, 4, 5, 6, 7, 7}
rsort : '{7, 7, 6, 5, 4, 3, 2, 1, 1}
shuffle Iter:0 = '{6, 7, 1, 7, 3, 2, 1, 4, 5}
shuffle Iter:1 = '{5, 1, 3, 4, 2, 7, 1, 7, 6}
shuffle Iter:2 = '{3, 1, 7, 4, 6, 7, 1, 2, 5}
shuffle Iter:3 = '{6, 4, 7, 3, 1, 7, 5, 2, 1}
shuffle Iter:4 = '{3, 6, 2, 5, 4, 7, 7, 1, 1}
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img

Using array ordering on classes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Register;
string name;
rand bit [3:0] rank;
rand bit [3:0] pages;

function new (string name);
this.name = name;
endfunction

function void print();
$display("name=%s rank=%0d pages=%0d", name, rank, pages);
endfunction

endclass

module tb;
Register rt[4];
string name_arr[4] = '{"alexa", "siri", "google home", "cortana"};

initial begin
$display ("\n-------- Initial Values --------");
foreach (rt[i]) begin
rt[i] = new (name_arr[i]);
rt[i].randomize();
rt[i].print();
end

$display ("\n--------- Sort by name ------------");
// 根据class中的数据对类对象排序
rt.sort(x) with (x.name);
foreach (rt[i]) rt[i].print();

$display ("\n--------- Sort by rank, pages -----------");

rt.sort(x) with ( {x.rank, x.pages});
foreach (rt[i]) rt[i].print();
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ncsim> run

-------- Initial Values --------
name=alexa rank=12 pages=13
name=siri rank=6 pages=12
name=google home rank=12 pages=13
name=cortana rank=7 pages=11

--------- Sort by name ------------
name=alexa rank=12 pages=13
name=cortana rank=7 pages=11
name=google home rank=12 pages=13
name=siri rank=6 pages=12

--------- Sort by rank, pages -----------
name=siri rank=6 pages=12
name=cortana rank=7 pages=11
name=alexa rank=12 pages=13
name=google home rank=12 pages=13
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img

Array Reduction Methods

Method Description
sum() Returns the sum of all array elements
product() Returns the product of all array elements
and() Returns the bitwise AND (&) of all array elements
or() Returns the bitwise OR (|) of all array elements
xor() Returns the bitwise XOR (^) of all array elements
1
2
3
4
5
6
7
8
9
10
11
12
13
module tb;
int array[4] = '{1, 2, 3, 4};
int res[$];

initial begin
$display ("sum = %0d", array.sum());
$display ("product = %0d", array.product());
$display ("and = 0x%0h", array.and());
$display ("or = 0x%0h", array.or());
$display ("xor = 0x%0h", array.xor());
end
endmodule

Simulation Log

1
2
3
4
5
6
7
ncsim> run
sum = 10
product = 24
and = 0x0
or = 0x7
xor = 0x4
ncsim: *W,RNQUIE: Simulation is complete.

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!