VHDL: use the length of an integer generic to determine number of select lines

前端 未结 5 1562
-上瘾入骨i
-上瘾入骨i 2021-02-08 08:34

I\'m trying to create a reusable barrel shifter; it takes an input array of bits and shifts them a certain number of positions (determined by another input). I want to parameter

5条回答
  •  误落风尘
    2021-02-08 08:55

    You can create a log2-function in a library, like this:

       function f_log2 (x : positive) return natural is
          variable i : natural;
       begin
          i := 0;  
          while (2**i < x) and i < 31 loop
             i := i + 1;
          end loop;
          return i;
       end function;
    

    If the library is imported you may then specify the port like this:

    shift_cnt : in std_logic_vector(f_log2(NUMBITS)-1 downto 0)
    

    It is a somewhat ugly solution, but it doesn't use any resources (since the function is pure and all the inputs are known at compile time).

    I usually do this, but you may prefer specifying the log value as the generic like you're mentioning.

提交回复
热议问题