syntax question

R

Ron Smith

Guest
Why does this work:

wire [1:0] foo,bar;

assign {foo,bar}=4'b1000;

at setting foo to 2'b10 and bar to 2'b00, but this gives a syntax error:

wire [1:0] foo,bar;

assign {foo,bar}=4'b0000;
assign {foo,bar}[3]=1'b1;

but this also works:

wire [3:0] foobar;

assign foobar=4'b0000;
assign foobar[3]=1'b1;

Please note that I'm not trying to argue over the finer points of style,
the above is simply a trivial and overly simplistic example of something
I tried that I thought "should" work, but of course it didn't.
 
Ron Smith wrote:
Why does this work:

wire [1:0] foo,bar;

assign {foo,bar}=4'b1000;

at setting foo to 2'b10 and bar to 2'b00, but this gives a syntax error:

wire [1:0] foo,bar;

assign {foo,bar}=4'b0000;
assign {foo,bar}[3]=1'b1;

but this also works:

wire [3:0] foobar;

assign foobar=4'b0000;
assign foobar[3]=1'b1;
Because according to the language definition, bit selects and
part selects can only be applied to identifiers, not expressions.
It's simply the way it is.

(That doesn't mean it won't change in future iterations of the
language standard. It may.)
--
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."
 
Ron Smith <rdNOsmith@sedSPAMona.intel.com> wrote in message news:<3F3D5290.95FA9920@sedSPAMona.intel.com>...

at setting foo to 2'b10 and bar to 2'b00, but this gives a syntax error:

wire [1:0] foo,bar;

assign {foo,bar}=4'b0000;
assign {foo,bar}[3]=1'b1;
Bit and part selects can only be applied to an identifier (e.g. a
vector net or reg). They are not ordinary operators that can be
applied to arbitrary expressions.

The obvious reason why is that the meaning of a particular index
value depends on the range used in the declaration of the identifier.
The meaning of foo[3] is different for foo declared with [3:0] and
foo declared with [0:4]. In the first case, it refers to the most
significant bit of of the vector, while in the second case it refers
to the second least significant bit. Identifiers declared as vector
objects have a range in their declaration which provides this meaning,
while an expression does not.

This could be resolved by defining an implicit range to be used
for any expression that is not an identifier. The obvious choice
would be [WIDTH-1:0]. There would be some potential for confusion
with this. For foo declared with a range [0:3], foo[3] would have
a different meaning than (foo)[3]. The first would use the range
declared on the object or [0:3], while the second is being applied
to an expression and would use the implicit range for a four-bit
expression or [3:0].

Even if selects were allowed on general vector expressions, that
would still not necessarily allow them in net lvalues like the
left-hand-side of a continuous assignment.
 

Welcome to EDABoard.com

Sponsor

Back
Top