🌐
Verification Academy
verificationacademy.com › systemverilog
Ternary operator in Verilog - SystemVerilog - Verification Academy
September 25, 2018 - Dear Dave, I hope you are doing good and you are fine. Please help me for the following query : Can we use ternary operator in-order to design a Priority Encoder? I found if I use nested ternary operator it works lik…
🌐
Stack Overflow
stackoverflow.com › questions › 43928605 › are-there-any-performance-penalties-by-using-nested-conditional-operator
verilog - Are there any performance penalties by using nested Conditional Operator? - Stack Overflow
Here is an example that use nested conditional operator to map register address to it's value. reg [4:0] mux; reg [1:0] addr; mux = (addr == 2'b00) ? i0 : ((addr == 2'b01) ? i1 : ((addr == 2'b...
🌐
ChipVerify
chipverify.com › home › verilog
Verilog Conditional Statements
May 17, 2023 - There are several types of conditional statements in Verilog listed below. <variable> = <condition> ? <expression_1> : <expression_2>; The conditional operator allows you to assign a value to a variable based on a condition. If the condition is true, expression_1 is assigned to the variable. Otherwise, expression_2 is assigned. Conditional operators can be nested to any level but it can affect readability of code.
🌐
Edaboard
edaboard.com › digital design and embedded programming › pld, spld, gal, cpld, fpga design
How to use 2 condition in assign [verilog] | Forum for Electronics
October 17, 2012 - In any case, the conditional ?: operator can be nested: assign z = (a==b) ? a : (b==c) ? b : z; That z at the end represents the missing else clause. ... Click to expand... Uhm, no? It's in the rough same area of things that look like similar stuff, but I would not say they are identical. Even easier ... if you want to do it in verilog, and are a bit miffed that plain old verilog doesn't have "always_comb" ...
🌐
Verificationstudio
learn.verificationstudio.com › tutorials › 1 › systemverilog-tutorial › subcontents › 17 › conditional-operator-
Conditional Operator (?:) - SystemVerilog Tutorial - Verification Studio
If the condition is true, the operator ... will be 20, because the condition a > b is false, so the value of b is assigned to c. You can nest conditional operators to check multiple conditions....
🌐
Nandland
nandland.com › conditional-operator
Verilog Conditional Operator
June 30, 2022 - This condition might be things like, “Is the value in A greater than the value in B?” or “Is A=1?”. Depending on if this condition evaluates to true, the first expression is chosen. If the condition evaluates to false, the part after the colon is chosen. I wrote an example of this. The code below is really elegant stuff. The way I look at the question mark operator is I say to myself, “Tell me about the value in r_Check.
🌐
Intel
intel.com › content › www › us › en › programmable › quartushelp › 17.0 › reference › glossary › def_ternary.htm
ternary operator Definition
An operator that selects between two expressions within an AHDL or Verilog HDL arithmetic expression. The ternary operator is used in the following format:
🌐
Verification Academy
verificationacademy.com › systemverilog
Is there a alternative way to nested ternary operator inside module?
July 26, 2017 - Hi, I have scenario inside my module… I have continuous assignments inside my module. I would like to use conditions on these continuous assignments… which I tried below manner which seems to be working fine. But when I have multiple conditions, is there a better way of doing these assignments ? module dummy(); assign dummy_tb.io_output_data = (SCENARIO==5'b00001)?
🌐
EDA Playground
edaplayground.com › x › TGM
Ternary operator
using EDA Playground VHDL Verilog/SystemVerilog UVM EasierUVM SVAUnit SVUnit VUnit (Verilog/SV) VUnit (VHDL) TL-Verilog e + Verilog Python + Verilog Python Only C++/SystemC
Find elsewhere
🌐
YouTube
youtube.com › shane oberloier
Comparing Ternary Operator with If-Then-Else in Verilog - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   June 1, 2020
Views   20
🌐
Maia-eda
maia-eda.net › muxes › style1
Verilog conditional (ternary) operator - MaiaEDA
This operator is particularly convenient, because it can be used in an expression, and so can form the right-hand-side of a continuous assignment. This is therefore a compact way of driving a net ("wire") without the overhead of the procedural if statement. A Verilog mux described in this way is essentially identical to one described using an if-else statement, although the two will simulate differently when the control expression (A) contains metavalues.
🌐
Louis-dr
louis-dr.github.io › ternaryverilog.html
TernaryVerilog
The first operand is the condition, followed by two or three operands called paths. The syntax uses the ? symbol after the condition and the : symbol to separate the paths. If the condition is a binary signal, it expects two paths, respectively for logic values $$1$$ and $$0$$ ; and if the ...
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › conditional-assignment
Conditional Assignment - an overview | ScienceDirect Topics
?: is especially useful for describing ... using the conditional operator. ... If s is 1, then y = d1. If s is 0, then y = d0. ?: is also called a ternary operator because it takes three inputs....
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › conditional-operator
Conditional Operator - an overview | ScienceDirect Topics
?: is especially useful for describing ... for a 2:1 multiplexer with 4-bit inputs and outputs using the conditional operator. ... If s is 1, then y = d1. If s is 0, then y = d0. ?: is also called a ternary operator because it takes three inputs....
🌐
Reddit
reddit.com › r/fpga › are ternary operator and parallel synthesize output same?
r/FPGA on Reddit: Are ternary operator and parallel synthesize output same?
April 24, 2023 -

I am new to Verilog and DC, I want to know ternary operator and parallel synthesize output is same? The two methods are like below

Top answer
1 of 4
9
This is a bit pedantic, but, why do you care? If the logical output is the same, then it doesn’t really matter if it synthesizes to the exact same circuit. Nobody is trying to save a few gates with different syntax; write what is most readable. To answer your question directly, it depends on the synthesis tool. Theoretically, after 50 years of work, they’d both end up with the same circuit. Practically, they may not, I don’t know. This is such a micro optimization, it doesn’t matter unless you work for the company making the synthesis tool.
2 of 4
3
TL;DR both snippets descrine exactly the same functionality so will synthsize the same. Adding easing assumptions like non-priority can reduce logic. Both snippiets can be described as a priority encoder feedong the select signal of amux, and have exactly the same behavior. Therfore, if you haven"t put vonstraints / limitations on the tool, it will produce tje same results. If you know the select vector should not bother with priority (will at most have 1 hot), you could wrote logic for a non-priority encoder (say, for example, ANDing every bit with its corresponding select signal, the ORing all bits together). Theoretically (and without real world experience on my behalf), for a large number of inputs this can produce less logic, more so for an actual ASIC synthsizer and less for FPGA (as a LUT is a very powerful primitive, so there is less granularity for the synthesizer. For example, in real ASIC it could choose to use 6 input AND instead of 6 input XOR, and as AND is smaller than XOR benefit from that. In FPGA both will translate to a single LUT.) SystemVerilog added the 'unique' keyword for this reason, to allow for if-else-if or cases to be treated parallel and without priority. On paper it could let the tool decide how to implement it and save you the need to make your own non-prio implementation, Though I'm sceptic the synthesis tools really have support for this.
Top answer
1 of 2
5

Formatting

There is nothing wrong with a nested conditional continuous assignment, but there are ways to make it more readable:

assign a = (b)    ? '1
         : (c&d)  ? '0
         : (f&h)  ? '1
                  : '0;

However, this is still an "if...else if...else if...else" structure, and a question you should ask yourself is what is this code meant to do and how it would 'read'. The above may be easier to read (while synthesizing same code) if it is code using an always and ""if...else if...else if...else" structure.

Here is an example of a clean use of the nested conditional continuous assignment:

assign a = (state == STATE1) ? '1
         : (state == STATE2) ? '0
         : (state == STATE3) ? '1 
           /* default */     : '0;

Readability

Do consider, that your shown form may save time in the original typing of the code, but there is much higher value in having your code readable. Be it yourself, or another designer, looking at the code a year or more later will appreciate a form that allows them to quickly grasp what the logic is doing.

Coding can be sped up without loss of readability by using an editor that supports auto-expanding snippets (or abbreviations). I use vim with abbreviations that really speed up all block structure entry, and alignment scripts that allow me to vertically align given character (like "=" or "(") or string in selection.

2 of 2
4

The answer to this question is probably more of a personal opinion, but in general long nested conditional statements like this are considered to be bad programming style because they are hard to debug and hard to maintain: http://cse.psu.edu/~cg577/DOCUMENTS/codingstandards/verilog.html

Besides if-else in always_comb, you can also use a case statement:

casez ({b,c,d,f,h})
    5'b1????: '1
    5'b011??: '0
    5'b00011: '1
    default: '0
endcase

You can also qualify casez with priority, unique, and unique_0.

🌐
Fpga Insights
fpgainsights.com › blog › exploring-systemverilogs-if-else-constructs-a-comprehensive-guide
Exploring System Verilog’s If-Else Constructs: A Comprehensive Guide - Fpga Insights
January 12, 2024 - These include logical comparisons, equality checks, and relational operators. Here’s a brief overview: These expressions provide the foundation for creating powerful conditions that drive the flow of your SystemVerilog code. To handle more complex decision-making scenarios, SystemVerilog allows for the nesting of if-else statements.