3

If i declare a signal integer range 0 to 6 , will it be better or worse opposing to declaring a std_logic_vector (2 downto 0) to do the exact same job.I am referring to design cost so that i can decide whether to use an integer with a small range or a one-hot representation with a vector.

To put it plainly how much space will be reserved for an integer range 0 to n ?

2 Answers 2

7

If a synthesis tool implements an integer with range 0 to N with minimum resources, it will have a size of:

integer range 0 to N : std_logic_vector(ceil(log2(N + 1)) - 1 downto 0)

So your integer range 0 to 6 will have the size of a std_logic_vector(2 downto 0).

But the VHDL language itself does not have a cost function for different data structures, since the cost depends on the implementation. Simulation tools implement the data structures one way, and synthesis tools does it differently.

For example, the size in a FPGA depends on how good a job the synthesis tool does. The synthesis tool must make an implementation where the operation of the resulting design is equivalent to the VHDL specification, but the synthesis tool is free to make an implementation that is larger than required, for example by implementing all integers 32-bit std_logic_vector.

The best way to find out the actually size is to make small experiments with the synthesis tools, whereby you will also learn a lot about synthesis tools and VHDL implementations.

Sign up to request clarification or add additional context in comments.

Comments

1

In simulation, integers will simulate faster, as vectors are more complex (they can store the non-'1' and -'0' states, and technically each bit is a signal in its own right, so the simulator has to work harder to keep track of all the potentially independent transitions). Some quick testing on my simulator showed that a simple 31-bit counter simulated in nearly 2x the time when coded with vectors rather than integers!

Regarding synthesis, if you are just using the object arithmetically, then the synthesiser will produce exactly the same logic for a restricted range integer as for a vector that can contain the same range. If it doesn't, get a refund! If you use it as a state variable, then it may get converted to one-hot, which reduces the decoding logic at the expense of flipflop usage, but flipflops are very very cheap these days.

There's some comparison of integer/vector coding styles here:

https://electronics.stackexchange.com/questions/27921/when-is-it-neater-to-use-vector-representations-vs-integers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.