0
0
VhdlHow-ToBeginner · 4 min read

VHDL Code for Barrel Shifter: Syntax and Example

A barrel shifter in VHDL shifts input bits by a variable amount in one clock cycle using multiplexers. You can implement it using a with-select statement or a combination of shifts and concatenations controlled by a shift amount signal.
📐

Syntax

A barrel shifter uses a shift amount input to select how many bits to shift the data input. The main parts are:

  • Input data: The bits to shift.
  • Shift amount: Number of positions to shift.
  • Output data: The shifted result.
  • Shift direction: Left or right shift.

Common syntax uses with-select or case statements to choose the shifted output based on the shift amount.

vhdl
with shift_amount select
  output <= data(7 downto 0) when 0,
            data(6 downto 0) & '0' when 1,
            data(5 downto 0) & "00" when 2,
            data(4 downto 0) & "000" when 3,
            data(3 downto 0) & "0000" when 4,
            data(2 downto 0) & "00000" when 5,
            data(1 downto 0) & "000000" when 6,
            data(0) & "0000000" when 7,
            (others => '0') when others;
💻

Example

This example shows a 8-bit barrel shifter that shifts left by the amount given on shift_amount. It uses a with-select statement to select the shifted output.

vhdl
library ieee;
use ieee.std_logic_1164.all;

entity barrel_shifter is
  port(
    data        : in  std_logic_vector(7 downto 0);
    shift_amount: in  std_logic_vector(2 downto 0);
    result      : out std_logic_vector(7 downto 0)
  );
end barrel_shifter;

architecture behavioral of barrel_shifter is
begin
  with shift_amount select
    result <= data(7 downto 0) when "000",
              data(6 downto 0) & '0' when "001",
              data(5 downto 0) & "00" when "010",
              data(4 downto 0) & "000" when "011",
              data(3 downto 0) & "0000" when "100",
              data(2 downto 0) & "00000" when "101",
              data(1 downto 0) & "000000" when "110",
              data(0) & "0000000" when "111",
              (others => '0') when others;
end behavioral;
⚠️

Common Pitfalls

Common mistakes when writing a barrel shifter in VHDL include:

  • Not handling all shift amounts, causing synthesis errors.
  • Forgetting to pad with zeros after shifting, which leads to incorrect output.
  • Mixing up shift directions (left vs right) without adjusting bit slices.
  • Using incomplete when others clauses, which can cause simulation mismatches.

Always cover all cases and test with different shift values.

vhdl
wrong_code: process(data, shift_amount)
begin
  if shift_amount = "001" then
    result <= data(6 downto 0); -- Missing zero padding
  else
    result <= data;
  end if;
end process;

-- Correct way:
with shift_amount select
  result <= data(7 downto 0) when "000",
            data(6 downto 0) & '0' when "001",
            (others => '0') when others;
📊

Quick Reference

ConceptDescription
dataInput bits to shift
shift_amountNumber of positions to shift (usually std_logic_vector)
resultShifted output bits
with-selectUsed to select output based on shift amount
PaddingAdd zeros to fill shifted bits
Shift directionLeft or right shift affects bit slicing

Key Takeaways

Use a with-select statement to implement variable shifts in a barrel shifter.
Always pad shifted bits with zeros to maintain correct output width.
Cover all shift amount cases including a when others clause.
Test the barrel shifter with different shift values to avoid logic errors.