verilog syntax question

J

Jason Zheng

Guest
Is there a quick way to express that we want certain bits of an
expression but not all of them?

e.g., I want the last 2 bits of A[1:0] + 1

Or do I have to declare another wire and assign the result to it?
 
Jason Zheng wrote:

Is there a quick way to express that we want certain bits of an
expression but not all of them?

e.g., I want the last 2 bits of A[1:0] + 1

Or do I have to declare another wire and assign the result to it?
Bitwise anding the expression with n'b11..11 will of course mask off all
other bits leaving only the last n. Eg: (A[1:0] + 1) & 2'b11. Beware
however that the resulting expression will still be as long as the
longer of the two operands to '&', which in the case of your example
will be 32bits because of the integer 1; the result contains the last 2
bits you wanted, only it'll be padded with zeros to make it 32 bits
long. Did you want a result expression length equal to the number of
bits you want extracted?

Getting at certain bits of an expression in general (ie. not necessarily
the last n bits) can be more tedious:

Eg to get the 6th to 3rd bits incl of A[1:0] + 1, ((A[1:0] + 1) &
7'b1111000) >> 3.

Any other ideas?

AW
 
Arjuna Wijesurendra wrote:

Jason Zheng wrote:


Is there a quick way to express that we want certain bits of an
expression but not all of them?

e.g., I want the last 2 bits of A[1:0] + 1

Or do I have to declare another wire and assign the result to it?


Bitwise anding the expression with n'b11..11 will of course mask off all
other bits leaving only the last n. Eg: (A[1:0] + 1) & 2'b11. Beware
however that the resulting expression will still be as long as the
longer of the two operands to '&', which in the case of your example
will be 32bits because of the integer 1; the result contains the last 2
bits you wanted, only it'll be padded with zeros to make it 32 bits
long. Did you want a result expression length equal to the number of
bits you want extracted?

Getting at certain bits of an expression in general (ie. not necessarily
the last n bits) can be more tedious:

Eg to get the 6th to 3rd bits incl of A[1:0] + 1, ((A[1:0] + 1) &
7'b1111000) >> 3.

Any other ideas?

AW
Yah, but that's too cumbersome and hard to understand. I doubt that any
synthesizer is smart enough to figure out that you don't need those
logical functions (and gates and shifter).
 
Jason Zheng <jzheng@jpl.nasa.gov> wrote in message news:<celuoh$lqk$1@nntp1.jpl.nasa.gov>...
Is there a quick way to express that we want certain bits of an
expression but not all of them?

e.g., I want the last 2 bits of A[1:0] + 1

Or do I have to declare another wire and assign the result to it?
There is no way to part-select the result of an expression. A
part-select can only be applied to an object, such as a wire,
reg or parameter. This is partly because the range from the
declaration is needed in order to determine what bits a particular
index value refers to.
 

Welcome to EDABoard.com

Sponsor

Back
Top