0
\$\begingroup\$

I was running the following code and found out that the out1 and out2 are different if I pass in1 or in2 as negative.

module multiplexor
#(
 parameter WIDTH1 = 4,
 parameter WIDTH2 = 8
)
(
 input logic signed [WIDTH1 - 1 : 0] in1 ,
 input logic signed [WIDTH2 - 1 : 0] in2 ,
 input logic sel ,
 output logic signed [WIDTH2 : 0] out1 ,
 output logic signed [WIDTH2 : 0] out2 
);
 logic signed [WIDTH2 : 0] w_sum_in;
 
 assign w_sum_in = $signed(in1) + in2;
 assign out1 = sel ? w_sum_in : {(WIDTH2+1){1'b0}};
 assign out2 = sel ? ($signed(in1) + in2) : {(WIDTH2+1){1'b0}};
endmodule

The input-output values are as follows while I was testing it:

xcelium> run
---------------------
in1 = 15 in2 = 89
out1 = 104
out2 = 104
---------------------
in1 = 10 in2 = 0
out1 = 10
out2 = 10
---------------------
in1 = 4 in2 = -15
out1 = -11
out2 = 245
---------------------
in1 = 0 in2 = -15
out1 = -15
out2 = 241
--------------------

I feel both the outputs should be same, but instead of doing the signed extension, it's padding 0 at the MSB and making the whole output positive.

Is there any rule to be followed while using ternary operator or solution to this problem?

toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked Jan 6 at 5:54
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your problem in because the width of the signed addition in

assign w_sum_in = $signed(in1) + in2;

is WIDTH2+1 (9 bits). But it is unsigned addition in

assign out2 = sel ? ($signed(in1) + in2) : {(WIDTH2+1){1'b0}};

because {(WIDTH2+1){1'b0}} is unsigned, making the entire expression unsigned. You can fix this by writing

assign out2 = sel ? in1 + in2 : signed'('0);
answered Jan 6 at 7:23
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.