I had to think a little bit, and here's what I came up with:


module addsub(
    input [31:0] a,
    input [31:0] b,
    input Cin,
    output [31:0] c,
    output Cout,
    input sub);

wire [31:0] b0 = {32{sub}} ^ b;
wire [33:0] b1 = {1'b0, b0, 1'b1};
wire [33:0] a1 = {1'b0, a,  Cin};
wire [33:0] sum = b1 + a1;

assign Cout = sum[33];
assign c = sum[32:1];

endmodule


This computes the following function:

{Cout, c} = sub ? (a-b+!c} : {a+b+c}


You get a subtraction by taking the 1's complement of b and adding.
If Cin is zero, then you get a borrow, or a-b-1.  If Cin is 1, then
that effectively turns the 1's complement into a 2's complement,
giving a straight subtraction.

Also, the way did it in the past yields a similar or maybe even
identical circuit, but only after the optimizer gets through wrangling
with it, and it's represented in a convoluted way.  The above is much
better.

Thanks, Yann, for teaching me basic math.  :)


-- 
Timothy Normand Miller
http://www.cse.ohio-state.edu/~millerti
Open Graphics Project
_______________________________________________
Open-graphics mailing list
[email protected]
http://lists.duskglow.com/mailman/listinfo/open-graphics
List service provided by Duskglow Consulting, LLC (www.duskglow.com)

Reply via email to