Barrel shifter with testing circuit

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
    with amt select
        y <= a                  when "000",
            a(0) & a(7 downto 1) when "001",
            a(1 downto 0) &a(7 downto 2) when "010",
            a(2 downto 0) &a(7 downto 3) when "011",
            a(3 downto 0) &a(7 downto 4) when "100",
            a(4 downto 0) &a(7 downto 5) when "101",
            a(5 downto 0) &a(7 downto 6) when "110",
            a(6 downto 0) &a(7) when others; -- 111
end sel_arch;

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);
    --stage 0, shift 0 or 1 bit
    s0 <= a(0) & a(7 downto 1) when amt(0)='1' else a;
    --stage 1, shift 0 or 2 bits
    s1<= s0(1 downto 0) & s0(7 downto 0) when amt(1)='1' else s0;
    --stage 2, shift 0 or 4 bits
    y<= s1(3 downto 0) s0(7 downto 4) when amt(2)='1' else s1;
end multi_stage_arch;

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
        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
    shifter_unit: entity work.barrel_shifter(multi_stage_arch)
        port map(a=>sw, amt=>btn, y=>led);
end arch;
News Reporter
Dr. Lu