Problems

19. Barrel shifter and rotator

MediumWrite the moduleAbout 25 min
combinationalshifterrotate

Build a combinational 8-bit shifter that moves d by amt places (0 to 7) in one step.

dir = 0 moves bits towards the top (left); dir = 1 moves them towards the bottom (right). With rotate = 0 it is a logical shift: zeros come in. With rotate = 1 the bits that fall off one end come back in at the other.

Build it as a logarithmic shifter: three stages that shift by 1, 2 and 4 places, each enabled by one bit of amt.

Ports

Module barrel8

PortDirBitsWhat it is
din8Value to shift.
amtin3Places to move, 0 to 7.
dirin10 left, 1 right.
rotatein11 rotates, 0 shifts in zeros.
yout8Result.

Example

d = 1011_0100, amt = 3: shift left gives 1010_0000, rotate left gives 1010_0101, rotate right gives 1001_0110.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogbarrel8.v
14px
Loading the editor…

Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.

Barrel shifter and rotator: Verilog interview question | breadboardhub