What is the result of this code? why?

wire [1:0] aaa,

assign temp_n[2:0] = ( ~aaa ) + 3’d1;

if aaa== 2’b01, then temp_n = ? why ?

Bit select or part select cannot be used on the left hand side of this
assignment
The offending expression is : temp_n[2:0]
Source info: assign temp_n[2:0] = ((~aaa) + 3’b1);

In reply to jeyanthis:

It is not “Bit select or part select” since temp_n is declared as:
wire [2:0] temp_n;

I tried running this with irun and temp_n=3’b111. After trying a few more tests, I conclude that it extends aaa to 3 bits first by padding the extra left-most bits with zeros, then inverts all bits. If I change the bitwidth of temp_n to 8, the result is 'b11111111. Verilog-XL simulates it the same way as irun. You don’t need to add the 3’d1 to see this. If you leave out 3’d1, you will get temp_n=3’b110.

If you want the result of ~aaa to be zero padded, then you can change your code to:

assign temp_n = {1'b0, ~aaa} + 3'd1;

or

assign temp_n = ~{1'b1, aaa} + 3'd1;

In reply to edcarstens:

One thing to remember is that parentheses ‘()’ only affect precedence of operator ordering. The parentheses have no effect on the original example because unary ~ has higher precedence than binary + anyways. Verilog bit-length rules say that operands of context determined expressions (+ and = are both context determined expressions) are extended to match the size of the largest operand, and the LHS of an assignment is an operand as well.

If you write the expression as a concatenation:

assign temp_n = {~aaa} + 3'd1;

This works as you originally expected because because all operands of a concatenation are self-determined - the bit-length of ~aaa is solely determined by the bit-length of aaa.

See 11.6.1 Rules for expression bit lengths in the IEEE 1800-2012 LRM

In reply to dave_59:

Thanks Dave!

What I am wondering is why ‘{}’ in the context is self-determined but ‘~’ is not?

‘Table 11-21—Bit lengths resulting from self-determined expressions’ in 11.6.1 Rules for

expression bit lengths in the IEEE 1800-2012 LRM also includes ‘~’.
.

In reply to Wanglj:

The table means that if the expression in the left-hand column is self-determined, then the result length is given in the second column.

In your case, “( ~aaa ) + 3’d1”, aaa is not a self-determined expression, but rather a context-determined expression, extended to 3 bits before ~ is applied to it.

Shalom

In reply to shalom:

What is also implied by the table, is that when a comment says an operand is self-determined, it is understood to mean that operand is always evaluated in a self-determined context, regardless of being inside a self or context-determined expression.

In reply to dave_59:

Thanks!
In that case, “{}” is an exception since in “{~aaa}+3’d1” aaa will not be expanded first?

In reply to Wanglj:

~aaa will not be expanded. The result of {~aaa} will be expanded.