1

I came across this code and was hoping someone could help me with it.

I understand that this means that out equals to out_1 if either a0, a1, a2, a3 is 1. If not, we have to look at the second expression which is 1'd0. Any idea what variable does the 1'd0 refer to? Or is this not allowed (I could compile it though).

out <= (a0 | a1 | a2 | a3) ? out_1 :
       ( 1'd0 )            ? out_2 :
       (a4 | a5 | a6 | a7) ? out_3 :
       out_4;

2 Answers 2

0

I think you are confused by this code because the code is a little odd.

You have a compound conditional assignment. There are 3 conditions: the expressions within parentheses.

You understand how the 1st condition works. That's great.

The 2nd condition works in a similar fashion: if the condition is true, then assign out to out_2. In this case, the condition is a constant, not a variable. Since the constant has the value 0, the condition will never be true, and out will never be assigned to out_2.

If the 1st and 2nd conditions are not true, the statement continues with the 3rd condition, etc.

This code can be simplified by removing the 2nd condition:

out <= (a0 | a1 | a2 | a3) ? out_1 :
       (a4 | a5 | a6 | a7) ? out_3 :
       out_4;

This code will simulate the same way as the code in the question.

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

Comments

0

It is allowed, but probably a bug in someone's code, or code generator.

To understand this, you need to know that the ?: has right-to-left associativity (Table 11-2—Operator precedence and associativity in the IEEE 1800-2017 SystemVerilog LRM ). It would help to rewrite the code in a more simple manner

out = a ? out_1 : 0  ? out_2 : b ? out_3 : out_4;

Then put parenthesis in the precedence order

out = (a ? out_1 : (0  ? out_2 : (b ? out_3 : out_4))) ;

You will see that out_2 can never be selected.

3 Comments

@ dave_59: Does that mean that out_3 is evaluated first?
No, associativity and precedence ties certain operands and subexpression to particular operators. The order of evaluation of the operands is indeterminate except as defined by section 11.3.5 Operator expression short circuiting. If a was true or b was false, out_3 would never get evaluated.
I think I meant to ask 'does that mean the b is evaluated first' . I think your answer is the same though. a has priority (goes first) then b.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.