ch2-Packed array

2019-10-24

SystemVerilog Packed Arrays

There are two types of arrays in SystemVerilog - packed and unpacked arrays.

A packed array is used to refer to dimensions declared before the variable name.

1
2
3
bit [3:0]   data;       // Packed array or vector
logic queue [9:0]; // Unpacked array

​ A packed array is guaranteed to be represented as a contiguous set of bits. They can be made of only the single bit data types like bit, logic, and other recursively packed arrays.

Single Dimensional Packed Arrays

A one-dimensional packed array is also called as a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
module tb;
bit [7:0] m_data; // A vector or 1D packed array

initial begin
// 1. Assign a value to the vector
m_data = 8'hA2;

// 2. Iterate through each bit of the vector and print value
for (int i = 0; i < $size(m_data); i++) begin
$display ("m_data[%0d] = %b", i, m_data[i]);
end
end
endmodule

Multidimensional Packed Arrays

A multidimensional packed array is still a set of contiguous bits but are also segmented into smaller groups.

Example #1

The code shown below declares a 2D packed array that occupies 32-bits or 4 bytes and iterates through the segments and prints its value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module tb;
bit [3:0][7:0] m_data; // A MDA, 4 bytes

initial begin
// 1. Assign a value to the MDA
m_data = 32'hface_cafe;

$display ("m_data = 0x%0h", m_data);

// 2. Iterate through each segment of the MDA and print value
for (int i = 0; i < $size(m_data); i++) begin
$display ("m_data[%0d] = %b (0x%0h)", i, m_data[i], m_data[i]);
end
end
endmodule

Simulation Log

注意上面遍历的索引范围是4,代表4个byte。

1
2
3
4
5
6
7
ncsim> run
m_data = 0xfacecafe
m_data[0] = 11111110 (0xfe)
m_data[1] = 11001010 (0xca)
m_data[2] = 11001110 (0xce)
m_data[3] = 11111010 (0xfa)
ncsim: *W,RNQUIE: Simulation is complete.

Example #2

Let us see a 3D packed array now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module tb;
bit [2:0][3:0][7:0] m_data; // An MDA, 12 bytes

initial begin
// 1. Assign a value to the MDA
m_data[0] = 32'hface_cafe;
m_data[1] = 32'h1234_5678;
m_data[2] = 32'hc0de_fade;

// m_data gets a packed value
$display ("m_data = 0x%0h", m_data);

// 2. Iterate through each segment of the MDA and print value
foreach (m_data[i]) begin
$display ("m_data[%0d] = 0x%0h", i, m_data[i]);
foreach (m_data[i][j]) begin
$display ("m_data[%0d][%0d] = 0x%0h", i, j, m_data[i][j]);
end
end
end
endmodule

Simulation Log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ncsim> run
m_data = 0xc0defade12345678facecafe
m_data[2] = 0xc0defade
m_data[2][3] = 0xc0
m_data[2][2] = 0xde
m_data[2][1] = 0xfa
m_data[2][0] = 0xde
m_data[1] = 0x12345678
m_data[1][3] = 0x12
m_data[1][2] = 0x34
m_data[1][1] = 0x56
m_data[1][0] = 0x78
m_data[0] = 0xfacecafe
m_data[0][3] = 0xfa
m_data[0][2] = 0xce
m_data[0][1] = 0xca
m_data[0][0] = 0xfe
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img


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