junecng
junecng

Reputation: 535

How do I implement a parameterized barrel shifter (rotator)?

I just implemented a rotator that rotates an 8 bits from 0 to 7 bits using an 8:1 muxes.

Now, I need to implement a rotator that has an input of 64 bits and an amount shifted. I could just make a 64:1 bit mux but that's too much work and can't be right.

How do I solve this problem? (preferable a parameterized version that works for both the 8 and 64 bit version)

Upvotes: 0

Views: 2509

Answers (1)

Morgan
Morgan

Reputation: 20514

A rotator can be as straight forward as this:

parameter W = 64;
logic [W-1:0] data_in;
logic [W-1:0] data_out;

logic [$clog2(W)-1:0] shift;

always_comb begin
  data_out = {data_in, data_in} >> shift;
end

If you need to remove issue surrounding assignment width mismatch you can add do:

logic [W-1:0] temp;
always_comb begin
  {temp, data_out} = {data_in, data_in} >> shift;
end

Upvotes: 5

Related Questions