Electrical – Range must be bounded by constant expressions – Verilog

verilog

How can I write in Verilog the following lines of code :

SF_D <= RAW1_i[(127 - (8*bytes_cnt)) : (124 - (8*bytes_cnt))];
SF_D <= RAW1_i[((8*bytes_cnt)-1) : ((8*bytes_cnt)-4)];

It gives me an error saying Range must be bounded by constant expressions.
bytes_cnt is changing value, is a counter, so I guess this is the problem. With constant value it works, with dynamic it doesn't. How can I make it work?

Best Answer

Use part-select (-: and +:). A description and examples can be found in IEEE Std 1800-2012 § 11.5.1 Vector bit-select and part-select addressing. First IEEE appearance is IEEE 1364-2001 (Verilog) § 4.2.1 Vector bit-select and part-select addressing.

Example:

SF_D <= RAW1_i[ 124 - (8*bytes_cnt) +: 4];
SF_D <= RAW1_i[ (8*bytes_cnt)-1 -: 4];

Also discussed on stack overflow:
https://stackoverflow.com/questions/18067571
https://stackoverflow.com/questions/17778418