OpenCores
URL https://opencores.org/ocsvn/verilog_fixed_point_math_library/verilog_fixed_point_math_library/trunk

Subversion Repositories verilog_fixed_point_math_library

[/] [verilog_fixed_point_math_library/] [trunk/] [qadd.v] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 tomburkeii
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date:    09:28:18 08/24/2011 
7
// Design Name: 
8
// Module Name:    q15_add 
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module qadd #(
22
        //Parameterized values
23
        parameter Q = 15,
24
        parameter N = 32
25
        )
26
        (
27
    input [N-1:0] a,
28
    input [N-1:0] b,
29
    output [N-1:0] c
30
    );
31
 
32
reg [N-1:0] res;
33
 
34
assign c = res;
35
 
36
always @(a,b) begin
37
        // both negative or both positive
38
        if(a[N-1] == b[N-1]) begin                                              //      Since they have the same sign, absolute magnitude increases
39
                res[N-2:0] = a[N-2:0] + b[N-2:0];          //              So we just add the two numbers
40
                res[N-1] = a[N-1];                                                      //              and set the sign appropriately...  Doesn't matter which one we use, 
41
                                                                                                                        //              they both have the same sign
42
                                                                                                                        //      Do the sign last, on the off-chance there was an overflow...  
43
                end                                                                                             //              Not doing any error checking on this...
44
        //      one of them is negative...
45
        else if(a[N-1] == 0 && b[N-1] == 1) begin                //      subtract a-b
46
                if( a[N-2:0] > b[N-2:0] ) begin                                   //      if a is greater than b,
47
                        res[N-2:0] = a[N-2:0] - b[N-2:0];                  //              then just subtract b from a
48
                        res[N-1] = 0;                                                                            //              and manually set the sign to positive
49
                        end
50
                else begin                                                                                              //      if a is less than b,
51
                        res[N-2:0] = b[N-2:0] - a[N-2:0];                  //              we'll actually subtract a from b to avoid a 2's complement answer
52
                        if (res[N-2:0] == 0)
53
                                res[N-1] = 0;                                                                            //              I don't like negative zero....
54
                        else
55
                                res[N-1] = 1;                                                                           //              and manually set the sign to negative
56
                        end
57
                end
58
        else begin                                                                                              //      subtract b-a (a negative, b positive)
59
                if( a[N-2:0] > b[N-2:0] ) begin                                   //      if a is greater than b,
60
                        res[N-2:0] = a[N-2:0] - b[N-2:0];                  //              we'll actually subtract b from a to avoid a 2's complement answer
61
                        if (res[N-2:0] == 0)
62
                                res[N-1] = 0;                                                                            //              I don't like negative zero....
63
                        else
64
                                res[N-1] = 1;                                                                           //              and manually set the sign to negative
65
                        end
66
                else begin                                                                                              //      if a is less than b,
67
                        res[N-2:0] = b[N-2:0] - a[N-2:0];                  //              then just subtract a from b
68
                        res[N-1] = 0;                                                                            //              and manually set the sign to positive
69
                        end
70
                end
71
        end
72
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.