VHDL has built-in shift functions, however, they sometimes cannot be synthesized automatically. In this post, we will show a 8-bit barrel shifter that rotates an arbitray number of bits to right.
--Barrel shifter using a selected signal assignment statement library ieee; use ieee.std_logic_1164.all; entity barrel_shifter is port ( a: in std_logic_vector(7 downto 0); amt: in std_logic_vector(2 downto 0); y: out std_logic_vector(7 downto 0) ); end barrel_shifter; architecture sel_arch of barrel_shifter is begin with amt select y
An 8-bit data input, a, and a 3-bit control signal, amt, which defines the amount to be rotated.
While the code is straightforward, it will become cumbersome when the number of input bits increases. Moreover, a large number of choices implies a wide multiplexer, which makes synthesis difficult and leads to a large propagation delay.
Alternatively, we can construct the circuit by stage. In the nth stage, the input signal is either passed directly to output or rotated right by 2n positions. The nth stage is controlled by the nth bit of the amt signal. The following code is for this purpose.
-- barrel shifter usign multi-stage shifts architecture multi_stage_arch of barrel_shifter is signal s0,s1: std_logic_vector(7 downto 0); begin --stage 0, shift 0 or 1 bit s0
For testing the circuit, we can use the 8-bit switch for the a signal, three pushbutton switches for the amt signal, and the eight discrete LEDs for output.
--barrel shifter testing circuit library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std;all; entity shifter_test is port( sw: in std_logic_vector(7 downto 0); btn: in std_logic_vector(2 downto 0); led: out std_logic_vector(7 downto 0) ); end shifter_test; architecture arch of shifter_test is begin shifter_unit: entity work.barrel_shifter(multi_stage_arch) port map(a=>sw, amt=>btn, y=>led); end arch;