0

I am a newbie with Verilog. While doing a lab assignment (yes, I've read the homework policy here), I came across a conditional statement (ternary operator).

assign w1 = load ? in : out;

I understand that assign updates whenever anything on the right changes. In this case, does assign update w1 whenever load, in, or out change? Or does assign update only when load changes?

The lab assignment is for implementing a register. This question doesn't directly relate to register implementation so I feel safe to ask for clarification here. All the documentation I've found on assign suggests that it updates whenever any operand on the right hand side changes. How does this apply with ternary operator?

Logically, the statement is equivalent to two "or-ed" "and" gates, correct ((load and in) or (~load and out))?

Thanks.

I expect assign will update only when load changes. The situation in which I'm using the statement is kinda confusing to isolate which behavior is used so I'm curious to learn from someone who knows more.

I'm also interested in understanding the logic equivalent.

2
  • 1
    Logic equivalent: the conditional/ternary operator is used to implement a multiplexer (generally a 2:1 mux), so it would be useless if it only responded to changes on the control input. It's generally identical to an if-else mux, although the 2 simulate differently in the presence of metavalues. The ternary version also propagates X, which can be useful behaviour. Commented May 16, 2024 at 15:53
  • @EML thank you for the explanation. Yeah, it's being used for a 2:1 MUX for what I am doing so that makes sense now. Commented May 16, 2024 at 22:09

1 Answer 1

1

Understand that there are evaluations on the right hand side and updates on the left-hand side.

An evaluation gets scheduled whenever ANY signal in the expression on the right hand side has an update. This is true for any expression with any operators. If the evaluation on the RHS causes the value on the LHS to change, then an update to the LHS gets scheduled.

In your example, if load was true and in was 0, then w1 would be driven to 0. When in updates to 1, and nothing else changed, you would want w1 to change to 1 also.

See this answer about the logic behind the ternary operator.

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

1 Comment

Thanks for clarifying the behavior and linking the logic explanation.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.