FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-27-2007, 11:21 AM
Sailu
Guest
 
Posts: n/a
Default Verilog ternary operator

Hi All..


Can ny one explain the difference between these two pieces of code at
the output of simulator?

reg y;

always @ (a or b or select)
if (select)
y = a;
else
y = b;


AND

wire t = (select ? a : b);



To put it in a easier way, What is the difference between a ternary
operator and if-else conditional statement?

Thanks in advance,
Sailaja.

Reply With Quote
  #2 (permalink)  
Old 08-27-2007, 12:17 PM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: Verilog ternary operator

On Mon, 27 Aug 2007 10:21:49 -0000,
Sailu <[email protected]> wrote:

>reg y;
>
>always @ (a or b or select)
> if (select)
> y = a;
> else
> y = b;
>
> AND
>
>wire t = (select ? a : b);
>
> To put it in a easier way, What is the difference between a ternary
>operator and if-else conditional statement?


One obvious difference you've already seen: if you use a procedural
"if" you must, of course, drive a variable ("reg") because the
assignment is made from procedural code. But ?: forms an
expression, which can easily be used as the right-hand side
of a continuous "assign", so driving a net ("wire") if you wish.

The other big difference is what happens if "select" is unknown
(X or Z). An if() statement, given an unknown condition, takes
the false (else) branch. A conditional operator, on the
other hand, acts much more like a hardware multiplexer.
Try this example to see the difference.

reg [3:0] result_if, result_cond;
initial begin
if (1'bx)
result_if = 4'b0110;
else
result_if = 4'b0101;
result_cond = 1'bx ? 4'b0110 : 4'b0101;
$display("if: %b cond: %b", result_if, result_cond);
end

Having said all this, it is probably fair to say that there
is NO functional difference when the two different constructs
are used in synthesis.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Reply With Quote
  #3 (permalink)  
Old 09-08-2007, 12:02 AM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: Verilog ternary operator

Sailu wrote:

> Can ny one explain the difference between these two pieces of code at
> the output of simulator?


> reg y;
> always @ (a or b or select)
> if (select)
> y = a;
> else
> y = b;


> AND


> wire t = (select ? a : b);


There are many things that can be written in both behavioral
verilog (always blocks) and structural verilog (continuous
assignment).

Personally, I prefer structural verilog except for FF's which are
normally written in behavioral form. As someone else said,
there may be differences for X and Z values, but they should
both synthesize to the same logic.

-- glen

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Verilog operator bir Verilog 1 04-10-2007 07:39 PM
Ternary operator and signed numbers vertreko Verilog 2 03-02-2007 05:44 PM
Verilog exponential operator issues in simulation (ISE 7.1 SP3 w/ ModelSim 6.0a) Nju Njoroge Verilog 6 07-12-2005 06:03 PM
Re: Operator Overloading in Verilog Ajeetha Kumari Verilog 0 06-28-2003 05:39 PM
Re: Operator Overloading in Verilog Jonathan Bromley Verilog 0 06-27-2003 09:44 AM


All times are GMT +1. The time now is 04:24 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved