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

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [rtl/] [core/] [fm_3d_fadd.v] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   fm_3d_add.v
7
//
8
// Abstract:
9
//   Floating point adder (22-bits floating point format), latency = 3
10
//   i_adsb == 0 : add
11
//   i_adsb == 1 : subtract
12
// Author:
13 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
14 2 specular
//
15
//======================================================================
16
//
17
// Copyright (c) 2015, Kenji Ishimaru
18
// All rights reserved.
19
//
20
// Redistribution and use in source and binary forms, with or without
21
// modification, are permitted provided that the following conditions are met:
22
//
23
//  -Redistributions of source code must retain the above copyright notice,
24
//   this list of conditions and the following disclaimer.
25
//  -Redistributions in binary form must reproduce the above copyright notice,
26
//   this list of conditions and the following disclaimer in the documentation
27
//   and/or other materials provided with the distribution.
28
//
29
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
31
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
33
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
36
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
37
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
//
41
// Revision History
42
 
43
module fm_3d_fadd (
44
  clk_core,
45
  i_en,
46
  i_a,
47
  i_b,
48
  i_adsb,
49
  o_c
50
);
51
 
52
////////////////////////////
53
// I/O definition
54
////////////////////////////
55
    input         clk_core;
56
    input         i_en;
57
    input  [21:0] i_a;          // input A
58
    input  [21:0] i_b;          // input B
59
    input         i_adsb;       // 0 : A + B, 1 : A - B
60
    output [21:0] o_c;          // result
61
 
62
 
63
///////////////////////////////////////////
64
//  register definition
65
///////////////////////////////////////////
66
    reg    [21:0] r_c;         // result
67
 
68
    reg           r_sign_1z;
69
    reg    [4:0]  r_exp_1z;
70
    reg           r_sign_2z;
71
    reg    [4:0]  r_exp_2z;
72
    reg    [16:0] r_mats;
73
///////////////////////////////////////////
74
//  wire 
75
///////////////////////////////////////////
76
    // input data separation
77
    wire         w_a_sign;
78
    wire [15:0]  w_a_fraction;
79
    wire [4:0]   w_a_exp;
80
    wire         w_b_sign;
81
    wire [15:0]  w_b_fraction;
82
    wire [4:0]   w_b_exp;
83
    // intermidiate wire
84
    wire        w_mag_frac;   // mag of fraction
85
    wire        w_mag_exp;    // mag of exponent
86
    wire [4:0]  w_amb;        // exp a - b
87
    wire [4:0]  w_bma;        // exp b - a
88
    wire [4:0]  w_ex0;        // larger exp - smaller exp
89
    wire [4:0]  w_exp_l;      //
90
    wire        w_mag;        // mag of A/B
91
    wire [15:0] w_f0;         // larger fraction
92
    wire [15:0] w_f1_sa;      // smaller fraction
93
    wire [15:0] w_f1_sb;      // smaller fraction
94
    wire [15:0] w_f1t;        // smaller fraction
95
    wire        w_sign;       // sign bit
96
    wire        w_sub;        // subtract frag
97
    wire [16:0] w_mats;       // result of ADD/SUB + carry
98
    // finale result
99
    wire [21:0] w_c;
100
 
101
///////////////////////////////////////////
102
//  assign
103
///////////////////////////////////////////
104
    // separate input
105
    assign w_a_sign = i_a[21];
106
    assign w_a_exp  = i_a[20:16];
107
    assign w_a_fraction = i_a[15:0];
108
 
109
    assign w_b_sign = i_b[21];
110
    assign w_b_exp  = i_b[20:16];
111
    assign w_b_fraction = i_b[15:0];
112
 
113
    // output port
114
    assign o_c = r_c;
115
 
116
////////////////////////////////////
117
// stage 0
118
///////////////////////////////////
119
    assign w_mag_frac = (w_a_fraction <= w_b_fraction );
120
    assign w_mag_exp = (w_a_exp <= w_b_exp );
121
    assign w_exp_l = (w_mag_exp) ?  w_b_exp : w_a_exp;
122
    assign w_amb = w_a_exp - w_b_exp;
123
    assign w_bma = w_b_exp - w_a_exp;
124
    // larger exp - smaller exp
125
    assign w_ex0 = (w_mag_exp) ? w_bma : w_amb;
126
 
127
    assign w_mag = (w_ex0 == 4'b0) ? w_mag_frac : w_mag_exp;
128
 
129
    // select larger/smaller fraction
130
    assign w_f0 = (!w_mag) ? w_a_fraction : w_b_fraction;
131
//    assign w_f1 = (!w_mag) ? w_b_fraction : w_a_fraction;
132
 
133
    // A >= B : MAG = 0
134
    // ADD :              SUB :
135
    //         a + b             a - b
136
    //         a - b             a + b
137
    //        -a + b            -a - b
138
    //        -a - b            -a + b
139
    // SGN = sign of a
140
    //
141
    // A < B : MAG = 1
142
    // ADD :              SUB :
143
    //         a + b             a - b *
144
    //         a - b             a + b *
145
    //        -a + b            -a - b *
146
    //        -a - b            -a + b *
147
    //      (sign of b)       (sign of ~b)
148
    // if (adsb ==1 ) then b is sign, else not b is sign
149
 
150
    assign w_sign = ( w_mag == 1'b0) ? w_a_sign : (i_adsb ^ w_b_sign);
151
 
152
    // adsb = 0(add) : a  + -b  or b + -a ( sub)
153
    // adsb = 1(sub) : -a  - -b  or b - a
154
    assign w_sub = ((w_a_sign ^ w_b_sign) & !i_adsb) |
155
                   (!(w_a_sign ^ w_b_sign) & i_adsb) ;
156
 
157
    assign w_f1_sa = w_a_fraction >> w_ex0;
158
    assign w_f1_sb = w_b_fraction >> w_ex0;
159
    assign w_f1t = (w_mag) ? w_f1_sa : w_f1_sb;
160
 
161
////////////////////////////////////
162
// stage 1
163
///////////////////////////////////
164
    reg         r_sub;
165
    reg  [15:0] r_f0;
166
    reg  [15:0] r_f1t;
167
 
168
    always @(posedge clk_core) begin
169
        if (i_en) begin
170
            r_sign_1z <= w_sign;
171
            r_exp_1z <= w_exp_l;
172
            r_sub <= w_sub;
173
            r_f0 <= w_f0;
174
            r_f1t <= w_f1t;
175
        end
176
    end
177
 
178
 
179
///////////////////////////////////////////
180
//  stage 2
181
///////////////////////////////////////////
182
 
183
    assign w_mats =  (!r_sub) ? (r_f0 + r_f1t) : (r_f0 - r_f1t);
184
    always @(posedge clk_core) begin
185
        if (i_en) begin
186
            r_sign_2z <= r_sign_1z;
187
            r_exp_2z <= r_exp_1z;
188
            r_mats <= w_mats;
189
        end
190
    end
191
 
192
 
193
///////////////////////////////////////////
194
//  stage 3
195
///////////////////////////////////////////
196
    // normalize
197
    fm_3d_norm norm (
198
        .i_s(r_sign_2z),
199
        .i_e(r_exp_2z),
200
        .i_f(r_mats),
201
        .o_b(w_c)
202
    );
203
 
204
 
205
    always @(posedge clk_core) begin
206
        if (i_en) r_c <= w_c;
207
    end
208
 
209
 
210
 
211
endmodule

powered by: WebSVN 2.1.0

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