\$\begingroup\$
\$\endgroup\$
12
I'm creating an ALU in Verilog, and I would like overflow to be dealt with by wrapping around to lower values.
For example:
0000_0001 + ffff_ffff == 0000_0001
0000_0011 + ffff_ffff == 0000_0011
0000_0012 + ffff_fffe == 0000_0011
Is it possible to achieve this in Verilog?
1 Answer 1
\$\begingroup\$
\$\endgroup\$
Verilog arithmetic is defined for 2's complement numbers. If you want to perform 1's complement arithmetic, you need to convert the numbers first.
module top;
function [31:0] comp (reg [31:0] in);
if (in[31])
comp = -(~in);
else
comp = in;
endfunction
reg [31:0] A,B;
initial begin
A = 32'h0000_0012; B = 32'hffff_fffe;
$displayh(comp(A) + comp(B));
end
endmodule
answered Sep 5, 2023 at 15:59
Explore related questions
See similar questions with these tags.
lang-vhdl
0000_0001 + ffff_ffff
to be0000_0001
and not0000_0000
(which is what you'd get in typical integer arithmetic on a computer)? What result do you want forffff_ffff + 0000_0000
? \$\endgroup\$