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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [or1200/] [rtl/] [verilog/] [or1200_fpu_arith.v] - Blame information for rev 443

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 258 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  OR1200 FPU arith                                            ////
4
////                                                              ////
5
////  This file is part of the OpenRISC 1200 project              ////
6
////  http://opencores.org/project,or1k                           ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Wrapper for floating point arithmetic units.                ////
10
////                                                              ////
11
////  To Do:                                                      ////
12
////   - lf.rem.s and lf.madd.s instruction support               ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Original design (FPU100) -                            ////
16
////        Jidan Al-eryani, jidan@gmx.net                        ////
17
////      - Conv. to Verilog and inclusion in OR1200 -            ////
18
////        Julius Baxter, julius@opencores.org                   ////
19
////                                                              ////
20
//////////////////////////////////////////////////////////////////////
21
//
22
//  Copyright (C) 2006, 2010
23
//
24
//      This source file may be used and distributed without        
25
//      restriction provided that this copyright statement is not   
26
//      removed from the file and that any derivative work contains 
27
//      the original copyright notice and the associated disclaimer.
28
//                                                           
29
//              THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     
30
//      EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   
31
//      TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   
32
//      FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      
33
//      OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         
34
//      INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    
35
//      (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   
36
//      GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        
37
//      BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  
38
//      LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  
39
//      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  
40
//      OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         
41
//      POSSIBILITY OF SUCH DAMAGE. 
42
//
43
 
44
module or1200_fpu_arith
45
  (
46
   clk_i,
47
   opa_i,
48
   opb_i,
49
   fpu_op_i,
50
   rmode_i,
51
   output_o,
52
   start_i,
53
   ready_o,
54
   ine_o,
55
   overflow_o,
56
   underflow_o,
57
   div_zero_o,
58
   inf_o,
59
   zero_o,
60
   qnan_o,
61
   snan_o
62
   );
63
 
64
   parameter FP_WIDTH = 32;
65
   parameter MUL_SERIAL = 1; // 0 for parallel multiplier, 1 for serial
66
   parameter MUL_COUNT = 34; //11 for parallel multiplier, 34 for serial
67
   parameter FRAC_WIDTH = 23;
68
   parameter EXP_WIDTH = 8;
69
   parameter ZERO_VECTOR = 31'd0;
70
   parameter INF = 31'b1111111100000000000000000000000;
71
   parameter QNAN = 31'b11111111_10000000000000000000000;
72
   parameter SNAN = 31'b11111111_00000000000000000000001;
73
 
74
   // fpu operations (fpu_op_i):
75
   // ========================
76
   // 000 = add, 
77
   // 001 = substract, 
78
   // 010 = multiply, 
79
   // 011 = divide,
80
   // 100 = square root - DISABLED - JPB
81
   // 101 = unused
82
   // 110 = unused
83
   // 111 = unused
84
 
85
   // Rounding Mode: 
86
   // ==============
87
   // 00 = round to nearest even (default), 
88
   // 01 = round to zero, 
89
   // 10 = round up, 
90
   // 11 = round down
91
 
92
   input  clk_i;
93
   input [FP_WIDTH-1:0] opa_i;
94
   input [FP_WIDTH-1:0] opb_i;
95
   input [2:0]           fpu_op_i;
96
   input [1:0]           rmode_i;
97
   input                start_i;
98
   output reg           ready_o;
99
   output reg [FP_WIDTH-1:0] output_o;
100
   output reg                ine_o;
101
   output reg                overflow_o;
102
   output reg                underflow_o;
103
   output reg                div_zero_o;
104
   output reg                inf_o;
105
   output reg                zero_o;
106
   output reg                qnan_o;
107
   output reg                snan_o;
108
 
109
   reg [FP_WIDTH-1:0]         s_opa_i;
110
   reg [FP_WIDTH-1:0]         s_opb_i;
111
   reg [2:0]                  s_fpu_op_i;
112
   reg [1:0]                  s_rmode_i;
113
   reg                       s_start_i;
114
   reg [5:0]                  s_count; // Max value of 64
115
 
116
   reg [FP_WIDTH-1:0]         s_output1;
117
   reg [FP_WIDTH-1:0]         s_output_o; // Comb
118
 
119
   reg                       s_ine_o;
120
 
121
   wire                      s_overflow_o,
122
                             s_underflow_o,
123
                             s_div_zero_o,
124
                             s_inf_o, s_zero_o, s_qnan_o, s_snan_o;
125
 
126
   wire                      s_infa, s_infb;
127
 
128
   parameter t_state_waiting = 0,
129
               t_state_busy = 1;
130
 
131
   reg                       s_state;
132
 
133
   //// ***Add/Substract units signals***
134
   wire [27:0]                prenorm_addsub_fracta_28_o;
135
   wire [27:0]                prenorm_addsub_fractb_28_o;
136
 
137
   wire [7:0]                 prenorm_addsub_exp_o;
138
 
139
   wire [27:0]                addsub_fract_o;
140
   wire                      addsub_sign_o;
141
 
142
   wire [31:0]                postnorm_addsub_output_o;
143
   wire                      postnorm_addsub_ine_o;
144
 
145
   //// ***Multiply units signals***
146
 
147
   wire [9:0]                 pre_norm_mul_exp_10;
148
   wire [23:0]                pre_norm_mul_fracta_24 ;
149
   wire [23:0]                pre_norm_mul_fractb_24 ;
150
   wire [47:0]                mul_fract_48;
151
   wire [47:0]                mul_24_fract_48;
152
   wire                      mul_24_sign;
153
   wire [47:0]                serial_mul_fract_48;
154
   wire                      serial_mul_sign;
155
   wire                      mul_sign;
156
   wire [31:0]                post_norm_mul_output   ;
157
   wire                      post_norm_mul_ine;
158
 
159
 
160
   //// ***Division units signals***
161
 
162
   wire [49:0]                pre_norm_div_dvdnd;
163
   wire [26:0]                pre_norm_div_dvsor;
164
   wire [EXP_WIDTH+1:0]      pre_norm_div_exp;
165
   wire [26:0]                serial_div_qutnt;
166
   wire [26:0]                serial_div_rmndr;
167
   wire                      serial_div_sign;
168
   wire                      serial_div_div_zero;
169
   wire [31:0]                post_norm_div_output;
170
   wire                      post_norm_div_ine;
171
 
172
 
173
   //// ***Square units***
174
 
175
   wire [51:0]                pre_norm_sqrt_fracta_o;
176
   wire [7:0]                 pre_norm_sqrt_exp_o;
177
   wire [25:0]                sqrt_sqr_o;
178
   wire                      sqrt_ine_o;
179
 
180
   wire [31:0]                post_norm_sqrt_output  ;
181
   wire                      post_norm_sqrt_ine_o;
182
 
183
   //***Add/Substract units***
184
 
185
   or1200_fpu_pre_norm_addsub fpu_prenorm_addsub
186
     (
187
      .clk_i(clk_i)  ,
188
      .opa_i(s_opa_i)  ,
189
      .opb_i(s_opb_i)  ,
190
      .fracta_28_o(prenorm_addsub_fracta_28_o)  ,
191
      .fractb_28_o(prenorm_addsub_fractb_28_o)  ,
192
      .exp_o(prenorm_addsub_exp_o) );
193
 
194
   or1200_fpu_addsub fpu_addsub
195
     (
196
            .clk_i(clk_i)  ,
197
            .fpu_op_i(s_fpu_op_i[0]),
198
            .fracta_i(prenorm_addsub_fracta_28_o)        ,
199
            .fractb_i(prenorm_addsub_fractb_28_o)        ,
200
            .signa_i( s_opa_i[31]),
201
            .signb_i( s_opb_i[31]),
202
            .fract_o(addsub_fract_o)  ,
203
            .sign_o(addsub_sign_o)  );
204
 
205
   or1200_fpu_post_norm_addsub fpu_postnorm_addsub
206
     (
207
      .clk_i(clk_i)  ,
208
      .opa_i(s_opa_i)  ,
209
      .opb_i(s_opb_i)  ,
210
      .fract_28_i(addsub_fract_o)  ,
211
      .exp_i(prenorm_addsub_exp_o)  ,
212
      .sign_i(addsub_sign_o)  ,
213
      .fpu_op_i(s_fpu_op_i[0]),
214
      .rmode_i(s_rmode_i)  ,
215
      .output_o(postnorm_addsub_output_o)  ,
216
      .ine_o(postnorm_addsub_ine_o)
217
      );
218
 
219
   //***Multiply units***
220
 
221
   or1200_fpu_pre_norm_mul fpu_pre_norm_mul
222
     (
223
      .clk_i(clk_i),
224
      .opa_i(s_opa_i),
225
      .opb_i(s_opb_i),
226
      .exp_10_o(pre_norm_mul_exp_10),
227
      .fracta_24_o(pre_norm_mul_fracta_24),
228
      .fractb_24_o(pre_norm_mul_fractb_24));
229
   /*
230
    mul_24 i_mul_24
231
    (
232
    .clk_i(clk_i)  ,
233
    .fracta_i(pre_norm_mul_fracta_24)  ,
234
    .fractb_i(pre_norm_mul_fractb_24)  ,
235
    .signa_i(s_opa_i[31]),
236
    .signb_i(s_opb_i[31]),
237
    .start_i(start_i)  ,
238
    .fract_o(mul_24_fract_48)  ,
239
    .sign_o(mul_24_sign)        ,
240
    .ready_o()  );
241
    */
242
   // Serial multiply is default and only one included here
243
   or1200_fpu_mul fpu_mul
244
     (
245
      .clk_i(clk_i)  ,
246
      .fracta_i(pre_norm_mul_fracta_24)  ,
247
      .fractb_i(pre_norm_mul_fractb_24)  ,
248
      .signa_i(s_opa_i[31]),
249
      .signb_i(s_opb_i[31]),
250
      .start_i(s_start_i)  ,
251
      .fract_o(serial_mul_fract_48)  ,
252
      .sign_o(serial_mul_sign)  ,
253
      .ready_o()
254
      );
255
 
256
   // Serial or parallel multiplier will be chosen depending on constant 
257
   // MUL_SERIAL
258
   assign mul_fract_48 = MUL_SERIAL ? serial_mul_fract_48 : mul_24_fract_48;
259
   assign mul_sign = MUL_SERIAL ? serial_mul_sign : mul_24_sign;
260
 
261
   or1200_fpu_post_norm_mul fpu_post_norm_mul
262
     (
263
      .clk_i(clk_i)  ,
264
      .opa_i(s_opa_i)  ,
265
      .opb_i(s_opb_i)  ,
266
      .exp_10_i(pre_norm_mul_exp_10)  ,
267
      .fract_48_i(mul_fract_48)  , // Parallel multiplier input
268
      .sign_i(mul_sign)  , // Parallel multiplier input
269
      .rmode_i(s_rmode_i)  ,
270
      .output_o(post_norm_mul_output)  ,
271
      .ine_o(post_norm_mul_ine)
272
      );
273
 
274
   ////***Division units***
275
 
276
   or1200_fpu_pre_norm_div fpu_pre_norm_div
277
     (
278
      .clk_i(clk_i)  ,
279
      .opa_i(s_opa_i)  ,
280
      .opb_i(s_opb_i)  ,
281
      .exp_10_o(pre_norm_div_exp)  ,
282
      .dvdnd_50_o(pre_norm_div_dvdnd)    ,
283
      .dvsor_27_o(pre_norm_div_dvsor)    );
284
 
285
   or1200_fpu_div fpu_div
286
     (
287
      .clk_i(clk_i) ,
288
      .dvdnd_i(pre_norm_div_dvdnd)  ,
289
      .dvsor_i(pre_norm_div_dvsor)  ,
290
      .sign_dvd_i(s_opa_i[31]),
291
      .sign_div_i(s_opb_i[31]),
292
      .start_i(s_start_i)  ,
293
      .ready_o()  ,
294
      .qutnt_o(serial_div_qutnt)  ,
295
      .rmndr_o(serial_div_rmndr)  ,
296
      .sign_o(serial_div_sign)  ,
297
      .div_zero_o(serial_div_div_zero)   );
298
 
299
   or1200_fpu_post_norm_div fpu_post_norm_div
300
     (
301
      .clk_i(clk_i)  ,
302
      .opa_i(s_opa_i)  ,
303
      .opb_i(s_opb_i)  ,
304
      .qutnt_i(serial_div_qutnt)        ,
305
      .rmndr_i(serial_div_rmndr)  ,
306
      .exp_10_i(pre_norm_div_exp)  ,
307
      .sign_i(serial_div_sign)   ,
308
      .rmode_i(s_rmode_i)       ,
309
      .output_o(post_norm_div_output)  ,
310
      .ine_o(post_norm_div_ine)  );
311
 
312
   //////////////////////////////////////////////////////////////////-
313
 
314
   // Input Registers
315
   always @(posedge clk_i)
316
     begin
317
        s_opa_i <= opa_i;
318
        s_opb_i <= opb_i;
319
        s_fpu_op_i <= fpu_op_i;
320
        s_rmode_i <= rmode_i;
321
        s_start_i <= start_i;
322
     end
323
 
324
   // Output registers
325
   always @(posedge clk_i)
326
     begin
327
        output_o <= s_output_o;
328
        ine_o <= s_ine_o;
329
        overflow_o <= s_overflow_o;
330
        underflow_o <= s_underflow_o;
331
        div_zero_o <= s_div_zero_o & !s_infa;
332
        inf_o <= s_inf_o;
333
        zero_o <= s_zero_o;
334
        qnan_o <= s_qnan_o;
335
        snan_o <= s_snan_o;
336
     end
337
 
338
   always @(posedge clk_i)
339
     begin
340
        if (s_start_i)  begin
341
           s_state <= t_state_busy;
342
           s_count <= 0;
343
        end
344
        else if (s_state == t_state_busy) begin
345
           // Ready cases
346
           if (((s_count == 6) & ((fpu_op_i==3'd0) | (fpu_op_i==3'd1))) |
347
               ((s_count==MUL_COUNT) & (fpu_op_i==3'd2)) |
348
               ((s_count==33) & (fpu_op_i==3'd3)))
349
             begin
350
                s_state <= t_state_waiting;
351
                ready_o <= 1;
352
                s_count <= 0;
353
             end
354
           else
355
             s_count <= s_count + 1;
356
        end // if (s_state == t_state_busy)
357
        else begin
358
           s_state <= t_state_waiting;
359
           ready_o <= 0;
360
        end // else: !if(s_state == t_state_busy)
361
     end // else: !if(s_start_i)
362
 
363
   //// Output Multiplexer
364
   always @(posedge clk_i)
365
     begin
366
        case(fpu_op_i)
367
          3'd0,
368
            3'd1: begin
369
               s_output1 <= postnorm_addsub_output_o;
370
               s_ine_o <= postnorm_addsub_ine_o;
371
            end
372
          3'd2: begin
373
             s_output1 <= post_norm_mul_output;
374
             s_ine_o <= post_norm_mul_ine;
375
          end
376
          3'd3: begin
377
             s_output1 <= post_norm_div_output;
378
             s_ine_o <= post_norm_div_ine;
379
          end
380
          //      3'd4: begin
381
          //            s_output1       <= post_norm_sqrt_output;
382
          //            s_ine_o         <= post_norm_sqrt_ine_o;
383
          //    end
384
          default: begin
385
             s_output1 <= 0;
386
             s_ine_o <= 0;
387
          end
388
        endcase // case (fpu_op_i)
389
     end // always @ (posedge clk_i)
390
 
391
   // Infinte exponent
392
   assign s_infa = &s_opa_i[30:23];
393
   assign s_infb = &s_opb_i[30:23];
394
 
395
   always @*
396
     begin
397
        if (s_rmode_i==2'd0 | s_div_zero_o | s_infa | s_infb | s_qnan_o |
398
            s_qnan_o) // Round to nearest even
399 364 julius
          s_output_o = s_output1;
400 258 julius
        else if (s_rmode_i==2'd1 & (&s_output1[30:23]))
401
          // In round-to-zero: the sum of two non-infinity operands is never 
402
          // infinity,even if an overflow occures
403 364 julius
          s_output_o = {s_output1[31], 31'b1111111_01111111_11111111_11111111};
404 258 julius
        else if (s_rmode_i==2'd2 & (&s_output1[31:23]))
405
          // In round-up: the sum of two non-infinity operands is never 
406
          // negative infinity,even if an overflow occures
407 364 julius
          s_output_o = {32'b11111111_01111111_11111111_11111111};
408 258 julius
        else if (s_rmode_i==2'd3) begin
409
           if (((s_fpu_op_i==3'd0) | (s_fpu_op_i==3'd1)) & s_zero_o &
410
               (s_opa_i[31] | (s_fpu_op_i[0] ^ s_opb_i[31])))
411
             // In round-down: a-a= -0
412 364 julius
             s_output_o = {1'b1,s_output1[30:0]};
413 258 julius
           else if (s_output1[31:23]==9'b0_11111111)
414 364 julius
             s_output_o = 32'b01111111011111111111111111111111;
415 258 julius
           else
416 364 julius
             s_output_o = s_output1;
417 258 julius
        end
418
        else
419 364 julius
          s_output_o = s_output1;
420 258 julius
     end // always @ *
421
 
422
   // Exception generation
423
   assign s_underflow_o = (s_output1[30:23]==8'h00) & s_ine_o;
424
   assign s_overflow_o = (s_output1[30:23]==8'hff) & s_ine_o;
425
   assign s_div_zero_o = serial_div_div_zero & fpu_op_i==3'd3;
426 364 julius
   assign s_inf_o = s_output1[30:23]==8'hff & !(s_qnan_o | s_snan_o);
427 258 julius
   assign s_zero_o = !(|s_output1[30:0]);
428
   assign s_qnan_o = s_output1[30:0]==QNAN;
429
   assign s_snan_o = s_output1[30:0]==SNAN;
430
 
431
endmodule // or1200_fpu_arith
432
 
433
 

powered by: WebSVN 2.1.0

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