4
\$\begingroup\$

I have a generic VHDL entity and I have this code:

--- in testbench
const bits : integer := 13;          -- number of bits
const pow: integer := 8192;          -- 2^bits
const squared : integer := 67108864; -- pow^2

In where I pass those 3 constants to the entity. What I would really like is something like:

--- in testbench
const bits: integer := 13;

--- in architecture (generic bits:integer)
const pow : integer := 2^bits;
const squared : integer := pow^2;

Since bits is known at synthesis time is there any way to do it without using hardware?

\$\endgroup\$
4
  • \$\begingroup\$ Do you want the final circuit to be able to do these calculations, or do you just want to define constant values (constant during operation of the circuit) inside the description ? \$\endgroup\$
    – MaximGi
    Commented Mar 2, 2016 at 10:23
  • \$\begingroup\$ @MaximGi constant values (edited the answer) \$\endgroup\$
    – rnunes
    Commented Mar 2, 2016 at 10:23
  • 1
    \$\begingroup\$ This is not VHDL code .... \$\endgroup\$
    – Paebbels
    Commented Mar 2, 2016 at 10:30
  • \$\begingroup\$ @rhunes Ok. It is important you precise this because it will make a huge difference in the final layout, since doing these calculations would implement multipliers all over the place which are fairly complex components \$\endgroup\$
    – MaximGi
    Commented Mar 2, 2016 at 10:36

1 Answer 1

5
\$\begingroup\$

The VHDL power operator is **.

So your code should look like this:

-- in testbench
constant bits    : integer := 13;

-- in architecture
constant pow     : integer := 2**bits;
constant squared : integer := pow**2;
\$\endgroup\$
3
  • \$\begingroup\$ Is that supposed to generate hardware to calculate that or because is marked as const the math will be performed before synthesation? Any chance you could do the same with math functions (e.g round) ? \$\endgroup\$
    – rnunes
    Commented Mar 2, 2016 at 10:35
  • 3
    \$\begingroup\$ All constants are evaluated at elaboration time (synthesis time). In most cases no "extra hardware" is generated. Special cases are for example: constants, which are mapped to ROMs. You can even do floating point calculations and rounding as long as it results in a constant. \$\endgroup\$
    – Paebbels
    Commented Mar 2, 2016 at 10:44
  • 2
    \$\begingroup\$ You can even do time-based calculations involving clock periods and baud rates, then round to a natural constant. Much better than magic numbers in counters, timers and UARTs, \$\endgroup\$
    – user16324
    Commented Mar 2, 2016 at 14:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.