Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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??????????
 

tarkyss

Full Member level 6
Joined
Aug 1, 2005
Messages
345
Helped
26
Reputation
52
Reaction score
9
Trophy points
1,298
Location
China
Activity points
4,187
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};
 

Matrix_YL

Advanced Member level 4
Joined
Aug 19, 2005
Messages
108
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,298
Activity points
2,272
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top