can i use a variable as index

V

vittal

Guest
Hi,
Can i use a variable as an indiex in another array.
For example
wire [15:0] a;
wire [3:0] b;
wire [15:0] result;

assign result = {a[b - 1 : 0],a[15 : b]};

This is needed for shift operations.

Thanks,
Vittal
 
No, you can't. In current technologies HDL languages do not support
dynamic indexing. Operation on length/part of variables must be
determined during compile or elaboration time.

Therefore you must build a case statement with long list of cases:

reg [15:0] result;

always (b)
case (b)
4'b0000: result = {a[0], a[15:1]};
4'b0001: result = {a[1:0], a[15:2]};
....
endcase

It seems that you are trying to do a rotate operation.
Are you a student or an engineer in a company?

Utku.

vittal schrieb:

Hi,
Can i use a variable as an indiex in another array.
For example
wire [15:0] a;
wire [3:0] b;
wire [15:0] result;

assign result = {a[b - 1 : 0],a[15 : b]};

This is needed for shift operations.

Thanks,
Vittal
 
vittal wrote:
Can i use a variable as an indiex in another array.
You can use it as an index to select a single bit, or to select the
starting point in an indexed part-select, which must still have a
constant width specified. You cannot use it to select a starting or
ending bit in a normal part-select, since that would generally result
in an expression with a width that is not known at compile time.


};

This is needed for shift operations.
Or in this case, a rotate.

There are several solutions to this. The simplest is to concatenate
together two copies of the value being rotated, then shift it. After
truncation during assignment, this leaves a rotated value. Another
solution is to OR together the value right-shifted with the value
left-shifted by a complementary amount. Synthesis might do a better
job with one of these approaches than the other.
 
Utku Özcan wrote:
No, you can't. In current technologies HDL languages do not support
dynamic indexing. Operation on length/part of variables must be
determined during compile or elaboration time.

Therefore you must build a case statement with long list of cases:

reg [15:0] result;

always (b)
case (b)
4'b0000: result = {a[0], a[15:1]};
4'b0001: result = {a[1:0], a[15:2]};
....
endcase

It seems that you are trying to do a rotate operation.
Are you a student or an engineer in a company?

Utku.

vittal schrieb:
Fresher in a company. I used ur method and the one described in the
other post together.
Hi,
Can i use a variable as an indiex in another array.
For example
wire [15:0] a;
wire [3:0] b;
wire [15:0] result;

assign result = {a[b - 1 : 0],a[15 : b]};

This is needed for shift operations.

Thanks,
Vittal
 

Welcome to EDABoard.com

Sponsor

Back
Top