Verilog question mark (?) operator
Asked Answered
S

3

12

I'm trying to translate a Verilog program into VHDL and have stumbled across a statement where a question mark (?) operator is used in the Verilog program.

The following is the Verilog code;

1  module music(clk, speaker);
2  input clk;
3  output speaker;
4  parameter clkdivider = 25000000/440/2;

5  reg [23:0] tone;
6  always @(posedge clk) tone <= tone+1;

7  reg [14:0] counter;
8  always @(posedge clk) if(counter==0) counter <= (tone[23] ? clkdivider-1 : clkdivider/2-1); else counter <= counter-1;

9  reg speaker;
10  always @(posedge clk) if(counter==0) speaker <= ~speaker;
11  endmodule

I don't understand the 8th line, could anyone please shed some light on this? I've read on the asic-world website that the question mark is the Verilog alternate for the Z character. But I don't understand why it's being used in this context.

Kind regards

Streetwalker answered 9/9, 2012 at 2:48 Comment(0)
J
18

That's a ternary operator. It's shorthand for an if statement

Format:

condition ? if true : if false

Example:

tone[23] ? clkdivider-1 : clkdivider/2-1

Translates to something like (not correct syntax but I think you'll get it):

if tone[23] is 1, counter = clkdivider-1
else counter = clkdivider/2-1

Here are two examples of a 2 to 1 MUX using if statement and ternary operator.

On the asic-world website, it is covered under Conditional Operators

Jewry answered 9/9, 2012 at 2:49 Comment(1)
What if tone[23] is X or Z?Listen
C
7

Another way of writing, e.g. the following Verilog:

q <= tone[23] ? clkdivider-1 : clkdivider/2-1;

in VHDL would be:

q <= clkdivider-1 when tone[23] else clkdivider/2-1;
Corduroy answered 28/3, 2013 at 8:53 Comment(5)
This is why declarations are so essential in questions. A condition must evaluate to a boolean value. The only way tone[23] will meet that criteria is if tone is a BOOLEAN_VECTOR.Factitious
@Factitious Not so. In VHDL-2008 it is allowed to be a boolean, std_logic, or bit.Gnathion
The condition operator (9.2.9) which can be applied implicitly (after the when in a conditional assignment statement) is predefined in package standard for type BIT. Absent declarations (or an entity header) the type type of tone is not known. Note the overload condition operator is defined for type std_ulogic in package std_logic_1164 (and would cover elements of the various array types with an element base type of std_ulogic). There's nothing from preventing you providing your own overloaded operator, it converts an element type to a BOOLEAN.Factitious
A hurried survey apparently shows no synthesis vendors supporting the condition operator implicit or otherwise.Factitious
Oops! shows up in the Synopsys FPGA Synthesis Synplify Pro for Microsemi Edition Reference Manual October 2011. Page 752, both implicit and explict condition operators. There may be earlier references.Factitious
H
0

Think of it as a MUX, before the ? is the selection bit and on two sides of : are the inputs

Hyps answered 22/3, 2021 at 7:9 Comment(2)
Please be more specific.Lazurite
More specific about Mux or the question mark? After knowing it's a MUX it should be enough to translate to VHDL, assuming you understand hardware and vhdl.Hyps

© 2022 - 2024 — McMap. All rights reserved.