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 parameterize the module so that it works for any n
.
The number of select lines required is determined by n
--> i.e., SHIFT_CNT = log2(NUMBITS-1)+1
in the code below. It's considered bad form in my organization (and I think overall) to have ports that are not of std_logic_vector
or std_logic
, so I used a std_logic_vector
for the number of select lines. I need to adjust the length of the std_logic_vector
based on the input generic. Is there a way to do this without using a second generic? I've seen this post, but it doesn't deal with generics. This post eliminates the generics entirely or uses the log value as the generic, which isn't as intuitive to future users (and could cause problems if the INPUT
is not a power of two).
The declaration of SHIFT_CNT
below is definitely incorrect; is there a way to automatically generate the length in the entity declaration without using a second generic?
entity BarrelShifter is
generic ( NUMBITS : integer :=8);
Port ( INPUT : in std_logic_vector (NUMBITS-1 downto 0);
OUTPUT : out std_logic_vector (NUMBITS-1 downto 0);
SHIFT_CNT : in std_logic_vector ((NUMBITS-1)'length downto 0)
);
end BarrelShifter;
You can use the math library to calculate log2 and ceil of the logarit result to declare the size of SHIFT_CNT.
use IEEE.math_real.all;
or specific functions
use IEEE.math_real."ceil";
use IEEE.math_real."log2";
For example you want to calculate clog2 of value a
result := integer(ceil(log2(real(a))));
If you just use these function to calculate paramater, your code is synthesizable (I did it).
If you don't want use it in entities, you can declare them in a library or generic with these functions.