SpinalHDL
1 / 1
View past submissionsLeaderboard

Description

Barrel Shifter

Design a hardware module for the Barrel Shifter problem.

Given a 32-bit unsigned value data, shift or rotate it by shift_amount bits.

  • if direction = 1, operate to the left
  • if direction = 0, operate to the right
  • if rotate = 1, bits that leave one side wrap around to the other side
  • if rotate = 0, emptied bit positions are filled with zeros

Hardware I/O Encoding

  • data is a scalar unsigned 32-bit integer
  • shift_amount is a scalar unsigned 5-bit integer in the range 0..31
  • direction is a scalar unsigned 1-bit integer
  • rotate is a scalar unsigned 1-bit integer
  • the output is one unsigned 32-bit integer

Examples

Example 1

  • input data = 177, shift_amount = 3, direction = 1, rotate = 0
  • output: 1416

This is a left shift by 3 bits.

Example 2

  • input data = 13, shift_amount = 1, direction = 0, rotate = 1
  • output: 2147483654

This rotates 0b...1101 right by one bit.

Input

data

32-bit Unsigned Integer
Data to shift.

shift_amount

5-bit Unsigned Integer
Shift amount (0-31).

direction

1-bit Unsigned Integer
0=right, 1=left.

rotate

1-bit Unsigned Integer
0=shift, 1=rotate.

Output

32-bit Unsigned Integer
Shifted result.