Question

Following up on this question about the difference between packed and unpacked vectors in SV, why would I ever want to use unpacked vectors?
Packed vectors have these advantages that unpacked vectors don't have:

  • You can perform bit-wise operations on them
  • You can perform arithmetic operations on them
  • You can take slices of them
  • You can copy them as a whole vector
  • You can do anything you can with unpacked vectors (to the best of my knowledge)

What advantage do unpacked vectors have over packed vectors?

Was it helpful?

Solution

There is another reason why I like to use unpacked. With unpacked, there is no temptation (and accidental possibility) of treating the whole array name as a variable, and make an erroneous assignment. There is also no possibility of bit-bleeding from one element to another, when you may be thinking you are accessing B bits of element N, but in reality you may be accessing K bits of element N and B-K bits of element N+-1..

My philosophy is to keep only the things that belong together as a "unit of information" in the packed dimension. Everything else in the unpacked dimension. The default thinking should be unpacked, and pack only what you need to.

For example, if I have 9 ports, each with 21 bits of information, I would like to declare it as :

input logic [20:0] p1 [9];

The 20:0 part constitutes a unit of information, assigned and sampled together (nominally). Splitting those bits apart will destroy the protocol or the character of the port. On the other hand, changing the number of ports from 9 to, say, 16, is not going to affect the nature of the information in each port, so the 9 ports really belong in the unpacked dimension in my mind.

Hope that might give you a paradigm to think along... In this paradigm, you would be surprised how many things start to appear unpacked that you always thought were packed !!

OTHER TIPS

Unpacked arrays exist for several reasons.

1) Packed arrays are stored in memory as a continuous sequence of bits. Unpacked arrays can have each element stored independently which can yield greater simulation performance.

2) Unpacked arrays can be of types that aren't bit vectors. Arrays of ints, bytes, events, structs, classes, etc. can only be unpacked.

3) Most of the array manipulation methods only work on unpacked arrays.

4) Perhaps, only unpacked arrays can be assigned to using array literals. I'm not sure.

There may be other reasons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top