signed addition in verilog

Status
Not open for further replies.

superhet

Junior Member level 3
Joined
Jun 7, 2005
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,599
verilog addition

im doing a project in which i need to add two signed numbers. how can i check for underflow and overflow?

Code:
module stimulus;

reg signed [7:0] a,b;
wire signed [7:0] c;

signed_adder my_adder(a,b,c);

initial
begin
$monitor($time, " a = %d,  b = %d, c = %d", a, b, c);
end

initial
begin
	a = 127; b = 127;
end

endmodule

module signed_adder(a,b,c);

input signed [7:0] a,b;
output signed [7:0] c;

assign c = a+b;

endmodule

the above simulation gives me 127+127=-2

can somebody help??????????
 

verilog signed addition

the width a, b, and c are all 8 bits
8 bits can present -128~127 only
but 127+127=254, but c is only 8 bits
so the results is wrong
so the width must be 9 bits at least
the code is as follow
wire [8:0] c;
assign c={a[7],a}+{b[7],b};
 

verilog signed

Hi tarkyss

Code:
assign c={a[7],a}+{b[7],b};
We must do like this ?
I feel following description is ok
Code:
module signed_adder(a,b,c); 

input signed [7:0] a,b; 
output signed [8:0] c; 

assign c = a+b; 

endmodule
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…