How can I multiply by -1 in system Verilog? Inside an ALU?
I tried:
logic [DPWIDTH-1:0] alu_result;
always_comb
case (alusel)
ALU_REV: alu_result = alu_a * (-1);
endcase
But for some reason it doesn't work as expected.
How can I multiply by -1 in system Verilog? Inside an ALU?
I tried:
logic [DPWIDTH-1:0] alu_result;
always_comb
case (alusel)
ALU_REV: alu_result = alu_a * (-1);
endcase
But for some reason it doesn't work as expected.
The first answer in my mind is:
alu_result = -alu_a;
But if you want to use multiplication you must use signed variables:
localparam logic signed [DPWIDTH/2-1:0] NEGATIVE_ONE_C = -1;
logic signed [DPWIDTH-1:0] alu_result;
always_comb begin
case (alusel)
ALU_REV: begin
alu_result = signed'(alu_a) * NEGATIVE_ONE_C;
end
endcase
end
Instead of typecasting alu_a you should change its declaration, too. Also, note the size of NEGATIVE_ONE_C.