Can anyone help me out explaining the basic difference between ternary operator (:?) / conditional and if-else conditions?
When I evaluate for x and z values in the condition, the behaviour is unusual in ternary operator. When it incurs x or z for evaluation, it evaluated both true and false conditions and results in an unknown value (x), as it holds 2 mutually exclusive values at the same time step.
Whereas its not the same in if-else, which evaluates as expected sticking to one condition as per its definition.
My sample code :
module ternary_test;
logic [1:0] x;
logic ternary_out,ifelse_out;
function func(int m, string msg);
if (m) begin
$display($time,"\tFalse\t%s",msg);
func = 1;
end
else begin
$display($time,"\tTrue\t%s",msg);
func = 0;
end
endfunction
initial ternary_out = (x ==? 2'b0?) ? func(0,"ternary call") : func(1,"ternary call");
initial begin
if(x ==? 2'b0?)
ifelse_out = func(0,"ifelse call");
else
ifelse_out = func(1,"ifelse call");
end
initial $monitor($time,"\tternary_out = %d\t ifelse_out = %d",ternary_out,ifelse_out);
initial x = 2'b0x;
endmodule : ternary_test
Output :
0 True ternary call
0 False ternary call
0 False ifelse call
0 ternary_out = x ifelse_out = 1
And please do let me know why is this unusual behaviour of ternary operator?
The behavior of the ternary operator was an attempt to be more realistic in the propagation of unknowns during simulation.
With the ternary operator, it is easy to define its evaluation behavior since the true and false branches must produce a result with the same type. An if/else statement is more general; the code in the true and false branches do not even have to be related to each other. The branches could event have different blocking delays.