Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

generics

vhdl

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;
like image 494
NickD Avatar asked Oct 05 '12 16:10

NickD


People also ask

How are integers defined in VHDL?

The integer type is used to define objects whose value is always a whole number. VHDL doesn't specify the exact number of bits for the integer type, but any VHDL implementation should support at least a 32-bit realization. We can specify the range of values that an object of type integer is going to have.

What is the purpose of generics in VHDL?

VHDL allows the designer to parametrize the entity during the component instantiation.

What is generic type in VHDL?

In VHDL generics are constants. In VHDL-2008 they may also be types, subprograms or packages. Here is an entity that has a type generic (data_type) and a subprogram generic (function increment). We can't use the "+" operator in the architecture, because "+" is not supported for arbitrary data types.


2 Answers

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.

like image 180
Khanh N. Dang Avatar answered Sep 20 '22 08:09

Khanh N. Dang


You can instead of inputting the NUMBITS value as 8, input it as 2 (log2(8)), then retype as below to get around the problem, your generic just won't be as clean but it is scale-able.

entity BarrelShifter is

generic ( NUMBITS : integer :=2);                                                   
Port    ( INPUT     : in   std_logic_vector (((2**Nbits)-1) downto 0);                
          OUTPUT    : out  std_logic_vector (((2**Nbits)-1) downto 0);                
          SHIFT_CNT : in   std_logic_vector ((NUMBITS-1) downto 0)                 
        );                                                               
end BarrelShifter;
like image 45
Ben Parkes Avatar answered Sep 22 '22 08:09

Ben Parkes