38

I am seeing a code in SystemVerilog which has something like this:

if(address[2*pointer+:2]) do_something; 

How should I understand the +: when indexing this vector?

I found that it is called bit slicing, but I can't find an explanation about it.

0

2 Answers 2

88

Description and examples can be found in IEEE Std 1800-2017 § 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". Here is an direct example from the LRM:

logic [31: 0] a_vect; logic [0 :31] b_vect; logic [63: 0] dword; integer sel; a_vect[ 0 +: 8] // == a_vect[ 7 : 0] a_vect[15 -: 8] // == a_vect[15 : 8] b_vect[ 0 +: 8] // == b_vect[0 : 7] b_vect[15 -: 8] // == b_vect[8 :15] dword[8*sel +: 8] // variable part-select with fixed width 

If sel is 0 then dword[8*(0) +: 8] == dword[7:0]
If sel is 7 then dword[8*(7) +: 8] == dword[63:56]

The value to the left always the starting index. The number to the right is the width and must be a positive constant. the + and - indicates to select the bits of a higher or lower index value then the starting index.

Assuming address is in little endian ([msb:lsb]) format, then if(address[2*pointer+:2]) is the equivalent of if({address[2*pointer+1],address[2*pointer]})

Sign up to request clarification or add additional context in comments.

3 Comments

So what would happen if we used a_vect[15 -: 32] ?
@umayneverknow I don't remember if the LRM mentioned what happens in take scenario. Should resolve/error the same as a_vect[15 : -16]. Try it and find out
Could not stop myself from commenting that a_vect[ 0 +: 8] and b_vect[ 0 +: 8] both resolve to a_vect[7:0] and b_vect[7:0] depending on the manner in which a_vect and b_vect were defined. Now that is confusing if you ask me
29

This is another way to specify the range of the bit-vector.

x +: N, The start position of the vector is given by x and you count up from x by N.

There is also

x -: N, in this case the start position is x and you count down from x by N.

N is a constant and x is an expression that can contain iterators.

It has a couple of benefits -

  1. It makes the code more readable.

  2. You can specify an iterator when referencing bit-slices without getting a "cannot have a non-constant value" error.

3 Comments

Thanks. But, what if we want to keep both x and N as variables. How shall we implement it?
@vineeshvs can you provide an example?
I was thinking of keeping two loops with x and N as variables (say from 0 15 or something). But I think that's not allowed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.