Search code examples
vhdlfpgaxilinxvivadoghdl

Efficiently derive parameter from generics vhdl


I have two generics on my entity:

clk_freq, io_delay: integer

From this, I want to calculate the number of cycles required for io_delay which is in ms. I also want to store this counter value in an unsigned variable so I'm not wasting loads of resources on an integer type. So I've implemented the following to do this:

constant COUNT_MAX_I: integer := integer(ceil(real(io_delay) * 0.001 * real(clk_freq)));
constant COUNTER_WIDTH: integer := integer(ceil(log2(real(COUNT_MAX_I))));
constant COUNT_HIGH: integer := unsigned(COUNTER_WIDTH - 1 downto 0) := to_unsigned(COUNT_MAX_I, COUNTER_WIDTH);

However, just looking at it that looks hugely inefficient. So my question is this:

Is there a more efficient way to do this and will the synthesis tools get rid of COUNT_MAX_I as it's only used in the calculation of other constants?


Solution

  • Generally speaking constant declarations themselves do not directly result in any synthesized logic. As such, there is nothing inefficient about the code as written.