Question on if (vector) ...

D

David Rogoff

Guest
I need some guru clarification on an issue. I tried google and the
bnf, but I'm still not sure. I have some old RTL code I had to look
at that had something like this (simplified):

reg [MSB_PARAM-1:0] a_reg;

always @ ...
if (a_reg) begin
blah blah ...
end

Supposedly, a_reg will be considered true if any of the bits are
non-zero. It's not very clear, but it seems like it works with
various tools.

What's the preferred way?
if (a_reg != MSB_PARAM'b0)
if (|a_reg)
if (|a_reg = 1'b1)

Can someone please point me to some specific documentation that covers
this case? Also, any examples of this failing would be useful.

Thanks,

David
 
David Rogoff <david@therogoffs.com> wrote in message news:<fzj4bir0.fsf@therogoffs.com>...
Supposedly, a_reg will be considered true if any of the bits are
non-zero.
Yes.

It's not very clear, but it seems like it works with
various tools.
Yes, unless the tools have bugs.

What's the preferred way?
if (a_reg)

works just fine. I assume that you think this could be made
clearer and easier to understand.

if (a_reg != MSB_PARAM'b0)
You cannot use a parameter as part of a constant literal. If you want
to have a zero that is exactly MSB_PARAM bits wide, you can use a
multi-concat {MSB_PARAM{1'b0}}. But there is no need to do that here.
If you want to compare to zero, compare to zero, and let Verilog worry
about any width conversions.

if (a_reg != 0)

if (|a_reg)
This would work too, and is equivalent even with X or Z bits.

if (|a_reg = 1'b1)
This is a syntax error. I think you mean == here, though === would
work also.

Can someone please point me to some specific documentation that covers
this case? Also, any examples of this failing would be useful.
IEEE Std 1364-2001, clause 9.4: "If the expression evaluates to true
(that is, has a nonzero known value), the first statement shall be
executed. If it evaluates to false (has a zero value or the value is
x or z), the first statement shall not execute. If there is an else
statement and expression is false, the else statement shall be executed.

Since the numeric value of the if expression is tested for being zero,
certain shortcuts are possible. For example, the following two statements
express the same logic:

if (expression)
if (expression != 0)"
 

Welcome to EDABoard.com

Sponsor

Back
Top