E
Evan Lavelle
Guest
Is it possible to define an expression which evaluates to the defined
width of an arbitrary variable?
There's a $clog2 defined in V-2005, but it doesn't seem to be widely
supported. It's easy to write your own clog2 which returns the width
of a variable. However, this doesn't give you the right answer - it
only tells you the index of the top set bit at the time that it's
called.
To get the defined width of a variable, you need to set it to all 1's,
or at least set the top bit; an expression like 'var - var - 1'b1'
will do this for you. So, the question now is - how do you find the
number of set bits in this, or a similar, expression?
If you do a clog2-look-alike, you're likely to end up with something
like
((var - var - 1'b1) >= (1 << 16))? ... : ... // etc
This is where it falls over - the size of the literal '1' gets
propagated back to 'var' before the left-hand expression is evaluated,
and the LH expression ends up as 32'hffffffff, which is useless. I've
spent a couple of hours looking at this and the expression sizing
rules seem to screw up any attempt to do this. The obvious fix of
assigning (var - var - 1'b1) to a temporary doesn't work either, since
you have to know the size of var to get the size of the temporary,
which defeats the purpose of the exercise.
Any thoughts?
Thanks -
Evan
width of an arbitrary variable?
There's a $clog2 defined in V-2005, but it doesn't seem to be widely
supported. It's easy to write your own clog2 which returns the width
of a variable. However, this doesn't give you the right answer - it
only tells you the index of the top set bit at the time that it's
called.
To get the defined width of a variable, you need to set it to all 1's,
or at least set the top bit; an expression like 'var - var - 1'b1'
will do this for you. So, the question now is - how do you find the
number of set bits in this, or a similar, expression?
If you do a clog2-look-alike, you're likely to end up with something
like
((var - var - 1'b1) >= (1 << 16))? ... : ... // etc
This is where it falls over - the size of the literal '1' gets
propagated back to 'var' before the left-hand expression is evaluated,
and the LH expression ends up as 32'hffffffff, which is useless. I've
spent a couple of hours looking at this and the expression sizing
rules seem to screw up any attempt to do this. The obvious fix of
assigning (var - var - 1'b1) to a temporary doesn't work either, since
you have to know the size of var to get the size of the temporary,
which defeats the purpose of the exercise.
Any thoughts?
Thanks -
Evan