In 2's complement -B
is ~B+1
(~
is bit invert). Therefor A - B == A + (-B) == A + ~B + 1
. But your doing RTL, so you don't need to write the 2's complement for subtraction as it is default. A - B
and A + ~B + 1
will synthesize the same.
A[N-1:0] + B[N-1:0]
is always an unsigned operation. A + B
can be a signed operation if A and B are declared as input signed [N-1:0] A, B
, otherwise it is an unsigned operation.
Other notes:
There is an issue with your header. Many simulators, synthesizers, and other Verilog tools will accept what you have, but it is not complaint with the IEEE standard. There are two header styles, ANSI and non-ANSI. I recommend ANSI unless required to follow the IEEE1364-1995 version of the standard.
ANSI style (IEEE Std 1364-2001 and above):
module ALU #(parameter N=32)(
output reg [N-1:0] ALUOut,
output reg Zero,
input [N-1:0] ALUinA, ALUinB,
input [3:0] ALUop );
Non-ANSI style (IEEE Std 1364-1995 and above):
module ALU (ALUOut, Zero, ALUinA, ALUinB, ALUop);
parameter N=32;
output [N-1:0] ALUOut;
output Zero;
input [3:0] ALUop;
input [N-1:0] ALUinA, ALUinB;
reg [N-1:0] ALUOut;
reg Zero;
always @(ALUinA or ALUinB or ALUop)
is syntax legal. However since IEEE1364-2001 combinational logic is recommenced to be written as always @*
or always @(*)
(@*
and @(*)
are synonymous, user preference). With SystemVerilog (IEEE1800), the successor of Verilog (IEEE1364), always_comb
is recommend over always @*
for combinational logic, and always_latch
for level-sensitive latching logic.