Ian Burris
Ian Burris

Reputation: 6515

Selecting a range of wires based on a bitmask

I have a one-hot line that I want to use to select a range of wires. So for example if my selector line is 0001 I want to select some_wire[2:0], if it's 0010 I want to select some_wire[5:3], if it's 0100 I want to select some_wire[8:6], etc. Currently I'm doing this with a huge case statement however this doesn't scale if for example I want to parameterize the module. Is there a more succinct way of doing this?

Upvotes: 2

Views: 740

Answers (1)

Peter de Rivaz
Peter de Rivaz

Reputation: 33509

Barrel shifters can be efficiently synthesized so it might be worth comparing any solution against the baseline where you convert the selector back into a binary encoding and use a shift. For example, something like:

shift = 0;
for(i=0;i<N;i=i+1)
begin
  if (selector[i])
  begin
     shift = i;
  end
end 

shifted_wire = some_wire >> shift;
output = shifted_wire[2:0];

Alternatively, you could use a for loop to compute each bit independently via:

for(i=0;i<N;i=i+1)
begin
   output[i] = | ((some_wire>>i) & selector);
end

Upvotes: 4

Related Questions