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

Subversion Repositories m32632

[/] [m32632/] [trunk/] [rtl/] [DP_FPU.v] - Blame information for rev 23

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

Line No. Rev Author Line
1 9 ns32kum
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
//
3
// This file is part of the M32632 project
4
// http://opencores.org/project,m32632
5
//
6 23 ns32kum
//      Filename:       DP_FPU.v
7
//      Version:        2.0
8
//      History:        1.0 first release of 30 Mai 2015
9
//      Date:           14 August 2016
10 9 ns32kum
//
11 23 ns32kum
// Copyright (C) 2016 Udo Moeller
12 9 ns32kum
// 
13
// This source file may be used and distributed without 
14
// restriction provided that this copyright statement is not 
15
// removed from the file and that any derivative work contains 
16
// the original copyright notice and the associated disclaimer.
17
// 
18
// This source file is free software; you can redistribute it 
19
// and/or modify it under the terms of the GNU Lesser General 
20
// Public License as published by the Free Software Foundation;
21
// either version 2.1 of the License, or (at your option) any 
22
// later version. 
23
// 
24
// This source is distributed in the hope that it will be 
25
// useful, but WITHOUT ANY WARRANTY; without even the implied 
26
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
27
// PURPOSE. See the GNU Lesser General Public License for more 
28
// details. 
29
// 
30
// You should have received a copy of the GNU Lesser General 
31
// Public License along with this source; if not, download it 
32
// from http://www.opencores.org/lgpl.shtml 
33
// 
34
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
35
//
36
//      Modules contained in this file:
37
//      1. PREPDATA     Prepare data for the big multiplier
38
//      2. BCDADDER     4 bit BCD adder
39
//      3. DFPU_BCD             Binary coded decimal (BCD) adder and subtractor
40
//      4. DFPU_ADDSUB  Double precision floating point adder and subtractor
41
//      5. DFPU_MISC    Double precision floating point miscellaneous operations 
42
//      6. DFPU_MUL             Double precision floating point multiplier
43 23 ns32kum
//      7. SCANDIG              Scan digit for leading one
44
//      8. DIVI_PREP    Prepare data for the divider
45
//      9. DFPU_DIV             The divider for all divide opcodes : double, single and integer
46
// 10. DP_LOGIK         Control logic and result path for different functions
47
// 11. DP_FPU           Top level of long operations datapath
48 9 ns32kum
//
49 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50 9 ns32kum
 
51 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52 9 ns32kum
//
53
//      1. PREPDATA     Prepare data for the big multiplier
54
//
55 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56 9 ns32kum
module PREPDATA ( START, MEI, DFLOAT, BWD, SRC1, SRC2,
57
                                  MSD_1, MSD_2, LSD_1, LSD_2, LOAD_MSD, LOAD_LSD1, LOAD_LSD2 );
58
 
59
        input    [1:0]   START;
60
        input                   MEI,DFLOAT;
61
        input    [1:0]   BWD;
62
        input   [31:0]   SRC1,SRC2;
63
 
64
        output [52:32]  MSD_1,MSD_2;
65
        output  [31:0]   LSD_1,LSD_2;
66
        output                  LOAD_MSD,LOAD_LSD1,LOAD_LSD2;
67
 
68
        reg             [31:0]   LSD_1,LSD_2;
69
 
70
        assign MSD_1 = MEI ? 21'h0 : {1'b1,SRC1[19:0]};
71
        assign MSD_2 = MEI ? 21'h0 : {1'b1,SRC2[19:0]};
72
 
73
        always @(MEI or BWD or SRC1)
74
                casex ({MEI,BWD})
75
                  3'b100 : LSD_1 = {24'h000000,SRC1[7:0]};
76
                  3'b101 : LSD_1 = {16'h0000,SRC1[15:0]};
77
                 default : LSD_1 = SRC1;
78
                endcase
79
 
80
        always @(MEI or BWD or SRC2)
81
                casex ({MEI,BWD})
82
                  3'b100 : LSD_2 = {24'h000000,SRC2[7:0]};
83
                  3'b101 : LSD_2 = {16'h0000,SRC2[15:0]};
84
                 default : LSD_2 = SRC2;
85
                endcase
86
 
87
        assign LOAD_MSD  = (START[0] & MEI) | (START[0] & DFLOAT);        // 1. step data load at DFLOAT
88
        assign LOAD_LSD1 = (START[0] & MEI) | (START[1] & DFLOAT);       // 2. step execute at DFLOAT
89
        assign LOAD_LSD2 = (START[1] & MEI) | (START[1] & DFLOAT);      // 2. step execute at DFLOAT
90
 
91
endmodule
92
 
93 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
94 9 ns32kum
//
95
//      2. BCDADDER     4 bit BCD adder
96
//
97 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98 9 ns32kum
module BCDADDER ( A_IN, B_IN, CY_IN, SUBP, OUT, CY_OUT );
99
 
100
        input    [3:0]   A_IN,B_IN;
101
        input                   CY_IN;
102
        input                   SUBP;
103
 
104
        output   [3:0]   OUT;
105
        output                  CY_OUT;
106
 
107 23 ns32kum
        reg              [4:0]   data;
108 9 ns32kum
        wire     [4:0]   result;
109
        wire                    over;
110
 
111 23 ns32kum
        always @(B_IN)
112
                case (B_IN)
113
                        4'h0 : data = 5'h00;
114
                        4'h1 : data = 5'h1F;
115
                        4'h2 : data = 5'h1E;
116
                        4'h3 : data = 5'h1D;
117
                        4'h4 : data = 5'h1C;
118
                        4'h5 : data = 5'h1B;
119
                        4'h6 : data = 5'h1A;
120
                        4'h7 : data = 5'h19;
121
                        4'h8 : data = 5'h18;
122
                        4'h9 : data = 5'h17;
123
                 default : data = 5'hxx;
124
                endcase
125 9 ns32kum
 
126 23 ns32kum
        assign result = {1'b0,A_IN} + (SUBP ? data : {1'b0,B_IN}) + {{4{SUBP & CY_IN}},CY_IN};
127
 
128 9 ns32kum
        assign over = result[4] | (result[3] & (result[2] | result[1]));
129
 
130
                                                                //              if result<0 : -6                                if result>9 : -10
131
        assign OUT = result[3:0] - (SUBP ? {1'b0,result[4],result[4],1'b0} : {over,1'b0,over,1'b0});
132 23 ns32kum
 
133 9 ns32kum
        assign CY_OUT = SUBP ? result[4] : over;
134
 
135
endmodule
136
 
137 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
138 9 ns32kum
//
139
//      3. DFPU_BCD             Binary coded decimal (BCD) adder and subtractor
140
//
141 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
142
module DFPU_BCD ( BCLK, BRESET, START, DO_BCD, BWD, SRC1, SRC2, CY_IN, SUBP, BCD_Q, CY_OUT, BCD_DONE );
143 9 ns32kum
 
144
        // Byte : 3 cycles in shortest case REG-REG, Word : 4 cycles and Double : 6 cycles
145
        input                   BCLK;
146
        input                   BRESET;
147
        input                   START;  // START[1]
148
        input                   DO_BCD; // BCD Opcode is valid
149
        input    [1:0]   BWD;
150
        input   [31:0]   SRC1,SRC2;      // Source , Destination, data is stable during operation
151
        input                   CY_IN;  // comes from PSR
152
        input                   SUBP;   // SUBP = 1 : SUBP , 0 : ADDP
153
 
154
        output  reg     [31:0]   BCD_Q;
155
        output  reg             CY_OUT; // went to PSR if DONE is valid
156
        output                  BCD_DONE;
157
 
158
        reg                             run_bcd;
159
        reg              [1:0]   byte_cou;
160
        reg             [15:0]   datain;
161
 
162
        wire     [7:0]   result;
163
        wire                    carry,carry_lsd,carry_msd;
164
 
165
        // START :     _/---\________________
166
        // byte_cou :  xxxxxx 0 x 1 x 2 x 3 x
167
        // BCD_DONE :  _____/---\____________  if BWD = Byte
168
 
169
        always @(posedge BCLK or negedge BRESET)
170
                if (!BRESET) run_bcd <= 1'b0;
171
                  else
172 23 ns32kum
                        run_bcd <= (START & DO_BCD & (BWD != 2'd0)) | (run_bcd & (BWD != byte_cou));
173 9 ns32kum
 
174 23 ns32kum
        always @(posedge BCLK) byte_cou <= START ? 2'd1 : byte_cou + {1'b0,run_bcd};
175 9 ns32kum
 
176
        always @(*)
177
                casex ({START,byte_cou})
178
                  3'b1_xx : datain = {SRC1[7:0],  SRC2[7:0]};
179 23 ns32kum
                  3'b0_0x : datain = {SRC1[15:8], SRC2[15:8]};
180
                  3'b0_10 : datain = {SRC1[23:16],SRC2[23:16]};
181
                  3'b0_11 : datain = {SRC1[31:24],SRC2[31:24]};
182 9 ns32kum
                endcase
183
 
184
        assign carry = START ? CY_IN : CY_OUT;
185
 
186
        BCDADDER        lsd_inst ( .A_IN(datain[3:0]), .B_IN(datain[11:8]), .CY_IN(carry), .SUBP(SUBP),
187
                                                   .OUT(result[3:0]), .CY_OUT(carry_lsd) );
188
 
189
        BCDADDER        msd_inst ( .A_IN(datain[7:4]), .B_IN(datain[15:12]), .CY_IN(carry_lsd), .SUBP(SUBP),
190
                                                   .OUT(result[7:4]), .CY_OUT(carry_msd) );
191
 
192
        always @(posedge BCLK) CY_OUT <= carry_msd;
193
 
194
        always @(posedge BCLK) if (START)                        BCD_Q[7:0]   <= result;
195 23 ns32kum
        always @(posedge BCLK) if (~byte_cou[1])         BCD_Q[15:8]  <= result;
196
        always @(posedge BCLK) if (byte_cou == 2'd2) BCD_Q[23:16] <= result;
197
        always @(posedge BCLK) if (byte_cou == 2'd3) BCD_Q[31:24] <= result;
198 9 ns32kum
 
199 23 ns32kum
        assign BCD_DONE = (START & DO_BCD & (BWD == 2'd0)) | (run_bcd & (BWD == byte_cou));
200 9 ns32kum
 
201
endmodule
202
 
203 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
204 9 ns32kum
//
205
//      4. DFPU_ADDSUB  Double precision floating point adder and subtractor
206
//
207 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
208
module DFPU_ADDSUB ( BCLK, START, SRC1, SRC2, MAN1, MAN2, SRCFLAGS, BWD, SELECT, OUT, IOUT, CMPRES );
209 9 ns32kum
 
210
        input                   BCLK;
211
        input    [1:0]   START;
212
        input   [31:0]   SRC1,SRC2;      // The input data
213
        input   [20:0]   MAN1,MAN2;
214
        input    [5:0]   SRCFLAGS;       // NAN, ZERO and SIGN of operands
215
        input    [1:0]   BWD;            // size of integer
216
        input    [3:0]   SELECT;         // upper 2 bits : R.T.F. code
217
 
218
        output  [69:0]   OUT;
219
        output  [31:0]   IOUT;           // result of ROUNDLi/TRUNCLi/FLOORLi = R.T.F.
220
        output  [1:0]    CMPRES;
221
 
222
        reg             [69:0]   outreg;
223
        reg             [31:0]   IOUT;
224
 
225
        // MOViL   : 2 cycles
226
        // ROUNDLi : 3 cycles (+TRUNC & FLOOR)
227
        // ADD/SUB : 4 cycles
228
        // CMP     : 2 cycles
229
 
230
        // ++++++++++++++++++++++++++++++++++
231
        // MOViL : 1. Pipeline stage : needs 3 cycles
232
 
233 23 ns32kum
        reg  [31:0]      movdat;
234 9 ns32kum
        reg      [31:0]  movif;
235
        reg                     sign_movif;
236
 
237
        always @(BWD or SRC1)
238 23 ns32kum
                casex(BWD)
239
                  2'b00 : movdat = {{24{SRC1[7]}}, SRC1[7:0]};   // Byte
240
                  2'b01 : movdat = {{16{SRC1[15]}},SRC1[15:0]};  // Word
241
                default : movdat =              SRC1[31:0];                              // Double
242 9 ns32kum
                endcase
243
 
244
        // This  pipeline stage for better timing 
245 23 ns32kum
        always @(posedge BCLK) movif <= ({32{movdat[31]}} ^ movdat) + {31'h0,movdat[31]};       // -2^31 is kept !
246 9 ns32kum
 
247
        always @(posedge BCLK) sign_movif <= movdat[31];
248
 
249 11 ns32kum
        // ROUNDLi/TRUNCLi/FLOORLi : 1. pipeline stage : can Opcode-Decoder deliver direct the 64 bit operand ? From register "yes"
250 9 ns32kum
 
251
        reg                     ovflag,ovflag2;
252
        reg                     rovfl;
253
        reg                     minint;
254
        wire [11:0]      rexdiff,rexo;
255
        wire            ganzklein;      // Flag for 0
256
 
257 11 ns32kum
        assign rexdiff = 12'h41D - {1'b0,SRC1[30:20]};  // 4..0 is the right shift value : like Single FP same value space
258 9 ns32kum
 
259
        // ovflag2 at the end of rounding : Check for Overflow
260
        always @(posedge BCLK) rovfl <= (ovflag | ovflag2) & (SELECT[1:0] == 2'b11) & ~minint;
261
 
262
        // a large positiv difference is a very small number :
263
        assign ganzklein = (~rexdiff[11] & (rexdiff[10:5] != 6'b0));    // 0 is implicit via SRC1[30:20]=0
264
 
265
        // Detection of Overflow
266
        assign rexo = ({1'b0,SRC1[30:20]} - {11'h1FF,~BWD[1]}); // subtract B/W = 3FF , D = 3FE
267
 
268
        always @(BWD or rexo)   // 0 ist in implicitly
269
                casex (BWD)
270
                  2'b00 : ovflag = (~rexo[11] & (rexo[10:3] != 8'h0));  // Exponent 0..7 because -128.4 => -128
271
                  2'b01 : ovflag = (~rexo[11] & (rexo[10:4] != 7'h0));  // Exponent 0..15 look above
272
                default : ovflag = (~rexo[11] & (rexo[10:5] != 6'h0));  // but Exponent only 0..30
273
                endcase
274
 
275
        always @(posedge BCLK)
276
                if (START[1]) minint <= (SRC1 == 32'hC1E0_0000) & (SRC2 == 32'h0) & BWD[1];     // detection of -2^31
277
 
278
        // ++++++++++++++++++++++++++++++++++++
279
        // ADD/SUB : 1. Pipeline Stage : which operand ist bigger ? Exchange if neccessary
280
        // SUB/CMP : SRC2 - SRC1
281
 
282
        reg                             ex_null,ma_null,ex_msb,ma_msb;
283
        reg             [10:0]   expo1,expo2;
284
        wire    [11:0]   exdiff,exdiff12;
285
        wire    [20:0]   madiff;
286
        wire                    switch,nan,sign,sign1,sign2;
287
        reg              [5:0]   shift1,shift2;
288
 
289
                // Pipeline register :
290
        reg             [63:0]   muxsrc2;
291 23 ns32kum
        wire    [55:3]  pipe1;  // Nummbers for right shifter
292
        wire     [5:0]   shift;
293
        reg              [2:0]   pshift;
294 9 ns32kum
        reg                             vorz,addflag;
295
 
296
        wire    [52:0]   muxsrc1;
297
        wire    [32:0]   lowdiff;
298
 
299 11 ns32kum
        assign nan = (SELECT[1:0] == 2'b11) ? SRCFLAGS[1] : (~SELECT[1] & (SRCFLAGS[3] | SRCFLAGS[1]));  // used at the end
300 9 ns32kum
 
301
        assign exdiff   = {1'b0,SRC2[30:20]} - {1'b0,SRC1[30:20]};      // Difference of Exponents
302
        assign madiff   = {1'b0,SRC2[19:0]}  - {1'b0,SRC1[19:0]}; // Difference of Mantissa 
303
        assign exdiff12 = {1'b0,SRC1[30:20]} - {1'b0,SRC2[30:20]};      // Diff. Exponents exchanged
304
 
305
        always @(posedge BCLK)
306
                if (START[0])
307
                  begin
308
                        ex_null <= (exdiff[10:0] == 11'h0);
309
                        ma_null <= (madiff[19:0] == 20'h0);
310
                        ex_msb  <= exdiff[11];
311
                        ma_msb  <= madiff[20];
312
                        shift1  <= (exdiff[10:6]   != 5'h0) ? 6'h3F : exdiff[5:0];
313
                        shift2  <= (exdiff12[10:6] != 5'h0) ? 6'h3F : exdiff12[5:0];
314
                        expo1   <= SRC1[30:20];
315
                        expo2   <= SRC2[30:20];
316
                  end
317
 
318
        assign lowdiff = {1'b0,SRC2} - {1'b0,SRC1};     // LSD compare
319
 
320
        assign switch = ex_msb | (ex_null & (ma_msb | (ma_null & lowdiff[32])));        // exchange ?
321
 
322
        assign muxsrc1 = switch ? {MAN2,SRC2} : {MAN1,SRC1};
323
 
324 23 ns32kum
        assign pipe1 = SELECT[1] ? (ganzklein ? 53'd0  : {1'b1,SRC1[19:0],SRC2}) : muxsrc1;      // feeding of R.T.F.
325
        assign shift = SELECT[1] ? {1'b0,rexdiff[4:0]} : (switch ? shift2 : shift1);
326
 
327 9 ns32kum
        always @(posedge BCLK)  // Pipeline Reg
328
          begin
329 23 ns32kum
                muxsrc2 <= switch  ? {expo1,MAN1,SRC1}   : {expo2,MAN2,SRC2};   // Incl. Exponent & "1" of mantisse
330
                pshift  <= shift[2:0];
331 9 ns32kum
          end
332 23 ns32kum
 
333 9 ns32kum
        //      SRC2   SRC1     : switch = 0            SRC2   SRC1 : switch = 1
334
        //        5  +   3  : +(5 + 3) =  8               3  +   5  : +(5 + 3) =  8             SELECT[0] = 0
335
        //        5  + (-3) : +(5 - 3) =  2               3  + (-5) : -(5 - 3) = -2
336
        //      (-5) +   3  : -(5 - 3) = -2             (-3) +   5  : +(5 - 3) =  2
337
        //      (-5) + (-3) : -(5 + 3) = -8             (-3) + (-5) : -(5 + 3) = -8
338
        //        5  -   3  : +(5 - 3) =  2               3  -   5  : -(5 - 3) = -2             SELECT[0] = 1
339
        //        5  - (-3) : +(5 + 3) =  8               3  - (-5) : +(5 + 3) =  8
340
        //      (-5) -   3  : -(5 + 3) = -8             (-3) -   5  : -(5 + 3) = -8
341
        //      (-5) - (-3) : -(5 - 3) = -2             (-3) - (-5) : +(5 - 3) =  2
342
 
343
        assign sign1 = SRCFLAGS[4];
344
        assign sign2 = SRCFLAGS[5];
345
 
346
        always @(posedge BCLK)  // Pipeline Reg
347
          begin
348
                vorz    <= switch ? (SELECT[0] ^ sign1) : sign2;
349
                addflag <= ~(SELECT[0] ^ (sign1 ^ sign2));
350
          end
351
 
352
        // CMPF : 1. Pipeline Stage : first result : is stored one level higer in Reg
353
 
354
        assign CMPRES[1] = ~CMPRES[0] & (switch ? ~sign1 : sign2);       // look table above
355 11 ns32kum
        assign CMPRES[0] = (ex_null & ma_null & (sign1 == sign2) & (lowdiff == 33'h0)) | (SRCFLAGS[2] & SRCFLAGS[0]);
356 9 ns32kum
 
357
        // ++++++++++++++++++++++++++++++++++
358
        // ADD/SUB + ROUND/TRUNC : 2. Step : Barrelshifter to the right -->
359
 
360 23 ns32kum
        wire [55:0] brshifta,brshiftb,brshiftd,brshifte,brshiftf;
361
        reg      [55:0] brshiftc;
362 9 ns32kum
 
363
        // 5..33322222222221111111111   is this picture still correct ? Took over from Single FP
364
        // 5..2109876543210987654321098765432-10
365
        // 1..VVVVVVVVVVVVVVVVVVVVVVVV0000000-00        // last 2 bit for rounding
366
 
367 23 ns32kum
        assign brshifta  =  shift[5] ? {32'h0,   pipe1[55:33],   (pipe1[32:3] != 30'h0)} : {pipe1,3'h0};
368
        assign brshiftb  =  shift[4] ? {16'h0,brshifta[55:17],(brshifta[16:0] != 17'h0)} : brshifta;
369
        always @(posedge BCLK)
370
                   brshiftc <=  shift[3] ? { 8'h0, brshiftb[55:9], (brshiftb[8:0] !=  9'h0)} : brshiftb;
371
        assign brshiftd  = pshift[2] ? { 4'h0, brshiftc[55:5], (brshiftc[4:0] !=  5'h0)} : brshiftc;
372
        assign brshifte  = pshift[1] ? { 2'h0, brshiftd[55:3], (brshiftd[2:0] !=  3'h0)} : brshiftd;
373
        assign brshiftf  = pshift[0] ? { 1'b0, brshifte[55:2], (brshifte[1:0] !=  2'h0)} : brshifte;
374 9 ns32kum
 
375
        // ++++++++++++++++++++++++++++++++++
376
        // ROUNDLi/TRUNCLi/FLOORLi : 3. Step : round to Integer
377
 
378
        reg                     car_ry;
379
        wire  [1:0] inex;
380 23 ns32kum
        wire [32:0] iadder;
381 9 ns32kum
        wire            restbits;
382
 
383
        assign restbits = (brshiftf[23:0] != 24'h0);
384 11 ns32kum
        assign inex     = {brshiftf[24],restbits};              // Inexact-Flag-Data transfered to multiplexer at the end
385 9 ns32kum
 
386
        always @(SELECT or sign1 or brshiftf or restbits or inex or ganzklein)
387
                casex (SELECT[3:2])
388 11 ns32kum
                    2'b00 : car_ry = sign1 ^ (((brshiftf[25:24] == 2'b11) & ~restbits) | (inex == 2'b11));      // ROUNDLi
389
                    2'b1x : car_ry = sign1 ? (~ganzklein & (inex == 2'b00)) : 1'b0;     // +numbers like TRUNCLi, -numbers to "-infinity" round
390 9 ns32kum
                  default : car_ry = sign1;     // TRUNCLi , simple cut off
391
                endcase
392
 
393 23 ns32kum
        assign iadder = (sign1 ? {2'b11,~brshiftf[55:25]} : {2'b0,brshiftf[55:25]}) + {32'h0,car_ry};
394 9 ns32kum
 
395 23 ns32kum
        always @(posedge BCLK) IOUT <= minint ? 32'h8000_0000 : iadder[31:0];
396 9 ns32kum
 
397
        always @(iadder or BWD or sign1)        // special overflow detection i.e. -129 to -255 at Byte
398
                casex (BWD)                                             // or 127.9 -> 128 = error !
399
                  2'b00 : ovflag2 = (iadder[8]  != iadder[7]);  // Byte
400
                  2'b01 : ovflag2 = (iadder[16] != iadder[15]); // Word
401 23 ns32kum
                default : ovflag2 = (iadder[32] != iadder[31]); // Double
402 9 ns32kum
                endcase
403
 
404
        // ++++++++++++++++++++++++++++++++++
405
        // ADD/SUB : 3. Step : Addition or Subtraction
406
 
407
        wire    [67:0]   result;
408
        wire    [55:0]   blshifti;
409
        wire    [12:0]   shiftl;
410
        wire                    shift_32;
411
        wire    [65:0]   add_q;
412
 
413
        // The central adder : the subtraction needs 3 Guard-Bits after LSB for correct rounding
414 11 ns32kum
        assign result = {1'b0,muxsrc2,3'b000} + (addflag ? {12'h0,brshiftf} : {12'hFFF,~brshiftf}) + {67'd0,~addflag};
415 9 ns32kum
 
416 11 ns32kum
        assign blshifti = SELECT[1] ? {movif,24'h0} : result[55:0];      // Feeding of MOViL, comes from Register
417 9 ns32kum
 
418
        assign shiftl = SELECT[1] ? 13'h041E : {1'b0,result[67:56]};    // MOViL
419
 
420
        assign shift_32 = (blshifti[55:24] == 32'h0);
421
 
422
        // In case of ADD the result bypasses the barrelshifter : LSB of exponent has changed
423
        assign add_q = (muxsrc2[53] != result[56]) ? {result[67:3],(result[2:0] != 3'b000)}
424
                                                                                           : {result[67:56],result[54:2],(result[1:0] != 2'b00)} ;
425
 
426
        // ++++++++++++++++++++++++++++++++++
427
        // ADD/SUB : 4. Step : Barrelshifter left for SUB and MOViF :
428
 
429
        wire            shift_16,shift_8,shift_4,shift_2,shift_1,zero;
430
        wire  [1:0] lsb_bl;
431
        wire [55:0]      blshifta,blshiftb,blshiftc,blshiftd,blshifte,blshiftf;
432
        wire [12:0]      expol;
433
 
434
        assign blshifta = shift_32 ? {blshifti[23:0],32'h0} : blshifti;
435
        assign shift_16 = (blshifta[55:40] == 16'h0);
436
        assign blshiftb = shift_16 ? {blshifta[39:0],16'h0}      : blshifta;
437
        assign shift_8  = (blshiftb[55:48] == 8'h00);
438
        assign blshiftc = shift_8  ? {blshiftb[47:0],8'h0}       : blshiftb;
439
        assign shift_4  = (blshiftc[55:52] == 4'h0);
440
        assign blshiftd = shift_4  ? {blshiftc[51:0],4'h0}       : blshiftc;
441
        assign shift_2  = (blshiftd[55:54] == 2'b00);
442
        assign blshifte = shift_2  ? {blshiftd[53:0],2'b0}       : blshiftd;
443
        assign shift_1  = ~blshifte[55];
444
        assign blshiftf = shift_1  ? {blshifte[54:0],1'b0}       : blshifte;
445
 
446
        // Overflow at ROUNDLi/TRUNCLi/FLOORLi is shown in overflow of exponent , SELECT[1] is then 1
447
        assign expol = shiftl - {7'h00,shift_32,shift_16,shift_8,shift_4,shift_2,shift_1};
448
 
449
        // Inexact at ROUNDLi/TRUNCLi/FLOORLi : evaluation for all one level higher
450
        assign lsb_bl = (SELECT == 2'b11) ? inex : {blshiftf[2],(blshiftf[1:0] != 2'b0)};
451
 
452
        assign zero =  (~SELECT[1] & SRCFLAGS[2] & SRCFLAGS[0])
453
                                 | ((blshifti == 56'h0) & ((~addflag & ~SELECT[1]) | (SELECT[1:0] == 2'b10)));
454
 
455
        assign sign = SELECT[1] ? sign_movif : (vorz & ~zero);  // sign for MOViL
456
 
457
        // 2. Pipeline register for ADD , SUB and MOViL 
458
        always @(posedge BCLK)
459
                outreg <= (addflag & ~SELECT[1]) ? {nan,zero,sign,1'b0,add_q}
460
                                                                                 : {nan,zero,sign,expol,blshiftf[54:3],lsb_bl};
461
 
462
        // ++++++++++++++++++++++++++++++++++
463
 
464
        assign OUT = {outreg[69:67],(rovfl ? 2'b01 : outreg[66:65]),outreg[64:0]};
465
 
466
endmodule
467
 
468 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
469 9 ns32kum
//
470
//      5. DFPU_MISC    Double precision floating point miscellaneous operations 
471
//
472 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
473 23 ns32kum
module DFPU_MISC ( BCLK, START, SRC1, SRC2, MAN2, SRCFLAGS, MODE, OUT );
474 9 ns32kum
 
475
        input                   BCLK;
476 23 ns32kum
        input    [1:0]   START;
477 9 ns32kum
        input   [31:0]   SRC1,SRC2;
478 23 ns32kum
        input   [19:0]   MAN2;
479
        input    [5:0]   SRCFLAGS;
480 9 ns32kum
        input    [3:0]   MODE;
481
        output  [69:0]   OUT;
482
 
483
        reg             [69:0]   OUT;
484
        reg             [63:0]   daten;
485
 
486 23 ns32kum
        wire                    sign;
487 9 ns32kum
        wire    [12:0]   lexpo,sexpo;
488
        wire    [69:0]   scalb_res,logb_res,fl_lf;
489
 
490 23 ns32kum
        always @(posedge BCLK) if (START[1]) daten <= {SRC1,SRC2};
491
        assign sign = daten[63];
492 9 ns32kum
 
493 23 ns32kum
        // +++++++++++++++++++++++++++ MOVFL and MOVLF +++++++++++++++++++++++++++++++++++
494 9 ns32kum
 
495
        assign lexpo = {5'b0,daten[62:55]} + 13'h0380;  // -7F + 3FF
496
 
497
        assign sexpo = (daten[62:52] > 11'h47E) ? 13'h0FFF
498
                                                                                        : ((daten[62:52] < 11'h381) ? 13'h0 : {2'b0,{4{daten[62]}},daten[58:52]});
499
 
500 23 ns32kum
        assign fl_lf = MODE[0] ? {SRCFLAGS[1:0],sign,lexpo,daten[54:32],31'h0}                                                                            // MOVFL
501
                                                   : {SRCFLAGS[1:0],sign,sexpo,daten[51:29],28'h0,daten[29:28],(daten[27:0] != 28'h0)};   // MOVLF
502 9 ns32kum
 
503
        // +++++++++++++++++++++++++++  LOGBf  +++++++++++++++++++++++++++++++++++
504
 
505
        wire     [9:0]   sel_data,unbiased,shift_l8,shift_l4,shift_l2;
506
        wire     [8:0]   shift_l;
507
        wire                    posi_8,posi_4,posi_2,posi_1;
508
        wire     [4:0]   calc_exp;
509
        wire     [6:0]   logb_exp;
510
 
511
        assign sel_data  = MODE[1] ? {{3{~daten[62]}},daten[61:55]} : daten[61:52];
512
        assign unbiased  = daten[62] ? (sel_data + 10'h001) : ~sel_data;
513
 
514
        // detection of leading "1"
515
        assign posi_8   = (unbiased[9:2] == 8'h00);
516
        assign shift_l8 = posi_8 ? {unbiased[1:0],8'h00} : unbiased;
517
        assign posi_4   = (shift_l8[9:6] == 4'h0);
518
        assign shift_l4 = posi_4 ? {shift_l8[5:0],4'h0}  : shift_l8;
519
        assign posi_2   = (shift_l4[9:8] == 2'b00);
520
        assign shift_l2 = posi_2 ? {shift_l4[7:0],2'b0}  : shift_l4;
521
        assign posi_1   = ~shift_l2[9];
522
        assign shift_l  = posi_1 ? {shift_l2[7:0],1'b0}  : shift_l2[8:0]; // top bit is hidden "1"
523
 
524 11 ns32kum
        assign calc_exp = 5'h08 - {1'b0,posi_8,posi_4,posi_2,posi_1};   // Minimum is "F" = for exponent +/-1 <=> 2^0
525 9 ns32kum
 
526
        // exponent is set one level higher for F and L
527
        assign logb_exp = MODE[1] ? {{4{~calc_exp[4]}},{3{calc_exp[4]}}} : {~calc_exp[4],{6{calc_exp[4]}}};
528
 
529 23 ns32kum
        assign logb_res = {SRCFLAGS[1],1'b0,~daten[62],2'b00,logb_exp,calc_exp[3:0],shift_l,45'h0};
530 9 ns32kum
 
531
        // ++++++++++++++++++++++++  SCALBf  ++++++++++++++++++++++++++++++++++
532
 
533 23 ns32kum
        reg              [3:0]   rshift;
534
        reg             [10:0]   shf_r0,dexpo;   // dexpo = Exponent Destination
535
        reg                             huge;
536
        reg                             svorz,dvorz;
537 9 ns32kum
 
538 23 ns32kum
        wire    [10:0]   shf_r1,shf_r2,shf_r4,shf_r8;
539
        wire    [12:0]   addexp,newexp,finexp;
540
        wire                    nan;
541 9 ns32kum
 
542 23 ns32kum
        always @(posedge BCLK)  // 2**0,9.. is transformed to 2**0 = 1 -> no change at SRC2
543
                if (START[0])
544
                        begin
545
                                shf_r0 <= ( SRC1[30] | ((SRC1[29:23] ==  7'h7F) & (MODE[1] | (SRC1[22:20] == 3'd7))) ) ?
546
                                                                (MODE[1] ? {4'd1,SRC1[22:16]} : {1'b1,SRC1[19:10]}) : 11'd0;
547
                                rshift <= MODE[1] ? 4'd6 - SRC1[26:23] : 4'd9 - SRC1[23:20];
548
                                huge   <= MODE[1] ? ( SRC1[30] & ((SRC1[29:26] != 4'd0) | (SRC1[25:23] == 3'h7)) )      // >406 in Double Style
549
                                                                  : ( SRC1[30] & ((SRC1[29:24] != 6'd0) | (SRC1[23] & (SRC1[22] | SRC1[21]))) );        // >409
550
                                svorz  <= SRC1[31];
551
                                dvorz  <= SRC2[31];
552
                                dexpo  <= MODE[1] ? {3'd0,SRC2[30:23]} : SRC2[30:20];
553
                        end
554
 
555
        assign shf_r1 = rshift[0] ? {1'b0,shf_r0[10:1]} : shf_r0;        // a mini-TRUNC of 11 Bits
556
        assign shf_r2 = rshift[1] ? {2'd0,shf_r1[10:2]} : shf_r1;
557
        assign shf_r4 = rshift[2] ? {4'd0,shf_r2[10:4]} : shf_r2;
558
        assign shf_r8 = rshift[3] ? {8'd0,shf_r4[10:8]} : shf_r4;
559
 
560
        assign addexp = svorz ? {2'd0,dexpo} - {2'd0,shf_r8} : {2'd0,dexpo} + {2'd0,shf_r8};
561
 
562
        assign newexp = MODE[1] ? {addexp[9:8],{3{addexp[7]}},addexp[7:0]} : addexp[12:0];
563
 
564
        assign finexp = SRCFLAGS[2] ? {3'd0,newexp[9:0]} // never an Overflow if SRC2 = 0.0 !
565
                                                                : {(huge ? {svorz,1'b1} : newexp[12:11]),newexp[10:0]};  // Overflow or Underflow
566
 
567
        assign nan = SRCFLAGS[3] | SRCFLAGS[1];
568
 
569
        assign scalb_res = MODE[1] ?    // Mantisse doesn't change !
570
                                           {nan,SRCFLAGS[2],daten[31],finexp,daten[22:0],daten[28:0],2'b00}
571
                                         : {nan,SRCFLAGS[2],dvorz,finexp,MAN2,daten[31:0],2'b00};
572 9 ns32kum
 
573
        // ++++++++++++++++++++++++  Output  ++++++++++++++++++++++++++++++++++++++++++++++++++++++
574
 
575 11 ns32kum
        always @(posedge BCLK) OUT <= MODE[3] ? (MODE[2] ? logb_res : scalb_res) : fl_lf ;      // LOGB/SCALB : MOVLF/MOVFL
576 9 ns32kum
 
577
endmodule
578
 
579 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
580 9 ns32kum
//
581
//      6. DFPU_MUL             Double precision floating point multiplier
582
//
583 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
584 9 ns32kum
module DFPU_MUL ( BCLK, SRC1, SRC2, START, MRESULT, SRCFLAGS, OUT );
585
 
586
        input                   BCLK;
587
        input   [31:0]   SRC1,SRC2;
588
        input                   START;          // that is START[0]
589
        input  [105:0]   MRESULT;
590
        input    [5:0]   SRCFLAGS;       // NAN and ZERO flags
591
        output  [69:0]   OUT;            // The result
592
 
593
        reg             [69:0]   OUT;
594
        reg             [12:0]   exponent;
595
        wire                    orlow;
596
        wire    [12:0]   expoh,expol;
597
        wire     [1:0]   restlow,resthigh;
598
        wire                    zero,nan,sign;
599
 
600
        assign zero =   SRCFLAGS[2] | SRCFLAGS[0];       // one is NULL -> NULL is the result
601
        assign nan =    SRCFLAGS[3] | SRCFLAGS[1];      // one is NAN -> error
602
        assign sign =   (SRCFLAGS[5] ^ SRCFLAGS[4]) & ~zero;
603
 
604
        assign orlow = (MRESULT[50:0] != 51'b0);
605
 
606
        assign restlow  = {MRESULT[51],orlow};
607
        assign resthigh = {MRESULT[52],(MRESULT[51] | orlow)};
608
 
609
        always @(posedge BCLK) if (START) exponent <= {2'b00,SRC1[30:20]} + {2'b00,SRC2[30:20]};
610
 
611
        assign expoh    = exponent - 13'h03FE;
612
        assign expol    = exponent - 13'h03FF;  // for MSB if MRESULT=0
613
 
614
        always @(posedge BCLK)
615
                OUT <= MRESULT[105] ? {nan,zero,sign,expoh,MRESULT[104:53],resthigh}    // 52 Bit Mantissa
616
                                                        : {nan,zero,sign,expol,MRESULT[103:52],restlow};
617
 
618
endmodule
619
 
620 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
621 9 ns32kum
//
622 23 ns32kum
//      7. SCANDIG              Scan digit for leading one
623 9 ns32kum
//
624 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
625 23 ns32kum
module SCANDIG (DIN, MBIT, LBIT, NONZ);
626
 
627
        input   [3:0]    DIN;
628
        output                  MBIT,LBIT,NONZ;
629
 
630
        assign MBIT = DIN[3] | DIN[2];                                  // 1xxx = 11
631
        assign LBIT = DIN[3] | (DIN[3:1] == 3'b001);    // 01xx = 10
632
        assign NONZ = (DIN != 4'd0);                                    // 001x = 01
633
 
634
endmodule
635
 
636
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
637
//
638
//      8. DIVI_PREP    Prepare data for the divider
639
//
640
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
641 9 ns32kum
module DIVI_PREP (SRC, BWD, NOT_DEI, EXTDATA, DOUT, MSB, NULL, MINUS);
642
 
643
        input   [31:0]   SRC;
644
        input    [1:0]   BWD;
645
        input                   NOT_DEI;
646
        input                   EXTDATA;
647
 
648
        output  [31:0]   DOUT;
649
        output   [4:0]   MSB;
650
        output                  NULL;
651
        output                  MINUS;
652
 
653
        reg             [31:0]   double;
654 23 ns32kum
        reg              [2:0]   select;
655 9 ns32kum
 
656
        wire     [1:0]   modus;
657 23 ns32kum
        wire     [7:0]   mbits,lbits,dnonz;
658 9 ns32kum
 
659
        assign modus = (NOT_DEI | EXTDATA) ? BWD : {(BWD[1] | BWD[0]),1'b1};
660
 
661
        always @(modus or SRC or NOT_DEI)
662
                casex (modus)
663
                  2'b00 : double = {{24{SRC[7]  & NOT_DEI}},SRC[7:0]};
664
                  2'b01 : double = {{16{SRC[15] & NOT_DEI}},SRC[15:0]};
665
                  2'b1x : double = SRC;
666
                endcase
667
 
668
        assign MINUS = double[31] & NOT_DEI;
669
 
670 11 ns32kum
        assign DOUT = ({32{MINUS}} ^ double) + {31'h0,MINUS};   //      assign DOUT = MINUS ? (32'd0 - double) : double;
671 9 ns32kum
 
672
        // now find most significant set bit : FFS
673
 
674 23 ns32kum
        SCANDIG digit_0 (.DIN(DOUT[3:0]),   .MBIT(mbits[0]), .LBIT(lbits[0]), .NONZ(dnonz[0]) );
675
        SCANDIG digit_1 (.DIN(DOUT[7:4]),   .MBIT(mbits[1]), .LBIT(lbits[1]), .NONZ(dnonz[1]) );
676
        SCANDIG digit_2 (.DIN(DOUT[11:8]),  .MBIT(mbits[2]), .LBIT(lbits[2]), .NONZ(dnonz[2]) );
677
        SCANDIG digit_3 (.DIN(DOUT[15:12]), .MBIT(mbits[3]), .LBIT(lbits[3]), .NONZ(dnonz[3]) );
678
        SCANDIG digit_4 (.DIN(DOUT[19:16]), .MBIT(mbits[4]), .LBIT(lbits[4]), .NONZ(dnonz[4]) );
679
        SCANDIG digit_5 (.DIN(DOUT[23:20]), .MBIT(mbits[5]), .LBIT(lbits[5]), .NONZ(dnonz[5]) );
680
        SCANDIG digit_6 (.DIN(DOUT[27:24]), .MBIT(mbits[6]), .LBIT(lbits[6]), .NONZ(dnonz[6]) );
681
        SCANDIG digit_7 (.DIN(DOUT[31:28]), .MBIT(mbits[7]), .LBIT(lbits[7]), .NONZ(dnonz[7]) );
682
 
683
        always @(dnonz)
684
                casex (dnonz[7:1])
685
                  7'b1xxx_xxx : select = 3'b111;
686
                  7'b01xx_xxx : select = 3'b110;
687
                  7'b001x_xxx : select = 3'b101;
688
                  7'b0001_xxx : select = 3'b100;
689
                  7'b0000_1xx : select = 3'b011;
690
                  7'b0000_01x : select = 3'b010;
691
                  7'b0000_001 : select = 3'b001;
692
                  default     : select = 3'b000;
693
                endcase
694 9 ns32kum
 
695 23 ns32kum
        assign NULL = (dnonz == 8'd0);
696 9 ns32kum
 
697 23 ns32kum
        assign MSB = {select,mbits[select],lbits[select]};
698
 
699 9 ns32kum
endmodule
700
 
701 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
702 9 ns32kum
//
703 23 ns32kum
//      9. DFPU_DIV             The divider for all divide opcodes : double, single and integer
704 9 ns32kum
//
705 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
706
module DFPU_DIV ( BCLK, BRST, START, SRC1, SRC2, MAN1, MAN2, SRCFLAGS, FL, BWD, OPCODE, OUT, DONE, DIVI_OUT, DVZ_TRAP, DEI_OVF );
707 9 ns32kum
 
708
        // This version needs for Double 28+1 cycles if MAN1<MAN2 otherwise 28+2.
709
        // For Single it needs 13+1 cyckes or 13+2.
710
 
711
        input                   BCLK,BRST;
712
        input    [3:0]   START;          // START & recognized Divider Operation
713
        input   [31:0]   SRC1,SRC2;      // input data
714
        input   [20:0]   MAN1,MAN2;
715
        input    [5:0]   SRCFLAGS;       // NAN and ZERO
716
        input                   FL;
717
        input    [1:0]   BWD;
718
        input    [2:0]   OPCODE;         // for all DIVi variants
719
 
720
        output  [69:0]   OUT;            // the result
721
        output  reg             DONE;           // Pipeline-Flag
722
        output  [63:0]   DIVI_OUT;       // for Integer Division
723
        output                  DVZ_TRAP;       // Divide by Zero Trap
724
        output   [1:0]   DEI_OVF;        // DEI Overflow
725
 
726
        // ++++++++++++++  for Integer Division  ++++++++++++++
727
        reg                             run_divi;
728
        reg                             divi_pipe1,divi_pipe2,divi_pipe3,divi_pipe4;
729
        reg                             neg_src1,neg_src2,nul_src2;
730
        reg              [4:0]   msb_src1;
731
        reg              [5:0]   msb_src2;
732
        reg             [31:0]   ivalue,src2_reg,pipe_reg;
733
        reg              [4:0]   divi_counter;
734
        reg                             sub_case;
735
        reg                             negativ;
736
        reg             [32:0]   divi_result;
737
        reg             [63:0]   DIVI_OUT;
738
        reg                             DVZ_TRAP,dvz_pipe;
739
        reg                             sel_in;
740
        reg             [62:0]   din_mux;
741
        reg                             dei_pipe;
742
        reg                             extdata;        // extended data : 2 data packets, only apply to DEI
743
        reg              [2:0]   addoff;
744
        reg                             next_msb2;
745
        reg             [31:0]   dei_result;
746
        reg              [1:0]   DEI_OVF;
747
 
748
        wire    [31:0]   i_in;
749
        wire    [37:0]   i_out;
750
        wire     [6:0]   diff_msb;
751
        wire     [5:1]  shift_r;
752
        wire    [62:0]   shift_2;
753
        wire    [62:0]   shift_4;
754
        wire    [62:0]   shift_8;
755
        wire    [62:0]   shift_16;
756
        wire    [64:0]   shift_32;
757
        wire                    stop_divi,neg_flag;
758
        wire                    rest_null,plus_1,ist_null;
759
        wire                    not_dei;
760
        wire                    valdata;        // Data <> 0 at DEI
761
 
762
        // ++++++++++++++  Floating Point & calculation path  ++++++++
763
        reg             [69:0]   OUT;
764
        reg             [32:0]   save1;
765
        reg                             runflag;
766
        reg             [55:0]   dreimal;
767
        reg             [56:0]   divreg,divsr;
768
        reg             [31:0]   divreg_ext;
769
        reg             [12:0]   exponent;
770
 
771
        wire                    load_src1,load_src2;
772
        wire    [56:0]   sub1,sub2,sub3;
773
        wire    [32:0]   src_1;
774
        wire    [20:0]   man_1;
775
        wire    [12:0]   expoh,expol,offset;
776
        wire                    restlsb,restlow,resthigh;
777
        wire                    zero,nan,sign,ende;
778
        wire                    orlow_s,orlow_d;
779
        wire                    short;
780
 
781
        // +++++++++++++++++++++++++++  Integer Division, DEI  +++++++++++++++++++++++++++
782
 
783
        assign not_dei = OPCODE[2];     // 0 = DEI
784
        always @(posedge BCLK) if (START[3]) extdata <= ~START[1];      // during START[0] for SRC1 not valid
785
 
786
        always @(posedge BCLK or negedge BRST)
787
                if (!BRST) run_divi <= 1'b0;
788
                        else
789
                                run_divi <= (START[3] & ~ist_null) | (~divi_pipe4 & run_divi);  // Abort at DVZ Trap
790
 
791
        always @(posedge BCLK) divi_pipe1 <= START[3] & ~ist_null;      // no start if SRC1 = 0 : DVZ Trap
792
        always @(posedge BCLK) dei_pipe   <= divi_pipe1 & extdata;
793
        always @(posedge BCLK) divi_pipe2 <= extdata ? dei_pipe : divi_pipe1;
794
 
795
        always @(posedge BCLK) src2_reg <= SRC2;
796
 
797
        always @(posedge BCLK) sel_in <= START[3] | divi_pipe1; // two times data for DEI
798
        assign i_in = sel_in ? src2_reg : SRC1;
799
 
800
        DIVI_PREP prep_inst ( .SRC(i_in), .BWD(BWD), .NOT_DEI(not_dei), .EXTDATA(extdata | START[0]),
801
                                                  .DOUT(i_out[31:0]), .MSB(i_out[36:32]), .NULL(ist_null), .MINUS(i_out[37]) );
802
 
803
        always @(posedge BCLK) dvz_pipe <= START[3] & ist_null; // Pulse 1 cycle long
804
        always @(posedge BCLK) DVZ_TRAP <= dvz_pipe;    // one cycle later if DEI with extdata
805
 
806
        always @(posedge BCLK)
807
                if (START[3])
808
                        begin
809
                                neg_src1 <= i_out[37];
810
                                msb_src1 <= i_out[36:32];
811
                        end
812
 
813
        always @(posedge BCLK)
814
                if (divi_pipe1)
815
                        begin
816
                                nul_src2 <= ist_null;
817
                                neg_src2 <= i_out[37];
818
                        end
819
 
820
        always @(posedge BCLK) ivalue   <= i_out[31:0];
821
 
822
        // The following is only for DEI :
823
        always @(posedge BCLK) pipe_reg <= {32{extdata}} & ivalue;      // Register must be 0 if not used
824
 
825
        assign valdata = extdata & ~ist_null;
826
        always @(BWD or valdata)
827
                casex (BWD)
828
                  2'b00   : addoff = {   1'b0,   1'b0,valdata};
829
                  2'b01   : addoff = {   1'b0,valdata,   1'b0};
830
                  default : addoff = {valdata,   1'b0,   1'b0};
831
                endcase
832
 
833 11 ns32kum
        always @(posedge BCLK) next_msb2 <= extdata & ist_null & divi_pipe1;    // Special case at DEI : MSD = 0
834 9 ns32kum
 
835
        always @(posedge BCLK)
836
                if (divi_pipe1) msb_src2 <= {addoff[2],(addoff[1:0] | i_out[36:35]),i_out[34:32]};
837
                  else
838
                        if (next_msb2) msb_src2 <= {1'b0,i_out[36:32]};
839
 
840
        // Shifter for Source2 
841
 
842
        assign diff_msb = {1'b0,msb_src2} - {2'b0,msb_src1};
843
 
844
        // negativ shift limited to 0 : Source2=0 calculated without special handling, result always 0
845
        assign shift_r = diff_msb[6] ? 5'd0 : diff_msb[5:1];    // LSB does not count
846
 
847
        always @(BWD or extdata or ivalue or pipe_reg)
848
                casex ({BWD,extdata})
849
                        3'b0x0  : din_mux = {31'b0,ivalue};     // the normal case for all except DEI
850
                        3'b001  : din_mux = {23'b0,pipe_reg,ivalue[7:0]};
851
                        3'b011  : din_mux = {15'b0,pipe_reg,ivalue[15:0]};
852
                        default : din_mux = {pipe_reg[30:0],ivalue};             // 63 Bit wide
853
                endcase
854
 
855
        assign shift_2  = shift_r[1] ? din_mux : {din_mux[60:0], 2'b0};
856
        assign shift_4  = shift_r[2] ? shift_2 : {shift_2[58:0], 4'b0};
857
        assign shift_8  = shift_r[3] ? shift_4 : {shift_4[54:0], 8'b0};
858
        assign shift_16 = shift_r[4] ? shift_8 : {shift_8[46:0],16'b0};  // Result is 63 Bit wide
859
 
860
        // 65 Bit result because of DEI 
861 11 ns32kum
        assign shift_32 = shift_r[5] ? {1'b0,pipe_reg,ivalue} : {shift_16,2'b00};       // special case DEI : 32 times shift
862 9 ns32kum
 
863
        always @(posedge BCLK or negedge BRST)  // Flag for rounding, only if DEST <>0 
864
                if (!BRST) divi_pipe3 <= 1'b0;
865
                  else
866
                    divi_pipe3 <= divi_pipe2 | (divi_pipe3 & ~stop_divi);
867
 
868
        always @(posedge BCLK)
869
                if (divi_pipe2) divi_counter <= shift_r;
870
                  else divi_counter <= divi_counter - {4'b000,~stop_divi};      // should stop at 0 
871
 
872
        assign stop_divi = (divi_counter == 5'h0);      // caclulation ready
873
 
874
        always @(posedge BCLK) divi_pipe4 <= divi_pipe3 & stop_divi;
875
 
876
        assign neg_flag  = neg_src1 ^ neg_src2;
877
        assign rest_null = (divreg[33:2] == 32'h0);
878
 
879
        always @(posedge BCLK) sub_case <= neg_flag & ~nul_src2;        // little help for MODi opcode
880
 
881
        // Result preparation :
882
        // DEST  SRC    QUO  REM /  DIV  MOD
883
        //  +33  +13 :   2    7  /   2    7
884
        //      +33  -13 :  -2    7  /  -3   -6
885
        //      -33  +13 :  -2   -7  /  -3    6
886
        //      -33  -13 :   2   -7  /   2   -7
887
        always @(*)
888
                case (OPCODE[1:0])
889
                  2'b00 : divi_result = {neg_flag,divsr[31:0]};          // QUO
890
                  2'b01 : divi_result = {neg_src2,divreg[33:2]};        // REM
891 11 ns32kum
                  2'b10 : divi_result = {neg_src1,((sub_case & ~rest_null) ? (save1[31:0] - divreg[33:2]) : divreg[33:2])};      // MOD
892 9 ns32kum
                  2'b11 : divi_result = {neg_flag,divsr[31:0]};          // DIV
893
                endcase
894
 
895
        always @(posedge BCLK) negativ <= divi_result[32];
896
 
897 11 ns32kum
        assign plus_1 = (OPCODE[1:0] == 2'b11) ? (negativ & rest_null) : negativ;        // Special case Rest=0 at DIV
898 9 ns32kum
 
899
        always @(posedge BCLK)
900 11 ns32kum
                if (divi_pipe4) DIVI_OUT[63:32] <= not_dei ? (({32{negativ}} ^ divi_result[31:0]) + {31'd0,plus_1}) : dei_result;
901 9 ns32kum
 
902
        always @(posedge BCLK) if (divi_pipe4) DIVI_OUT[31:0] <= divreg[33:2];
903
 
904
        always @(extdata or BWD or divsr or divreg)
905
                casex ({extdata,BWD})
906
                  3'b000  : dei_result = {16'hxxxx,divsr[7:0],divreg[9:2]};
907
                  3'b001  : dei_result = {divsr[15:0],divreg[17:2]};
908
                  default : dei_result = divsr[31:0];
909
                endcase
910
 
911
        // +++++++++++++++++++++++++++  Calculation path for Division  ++++++++++++++++++++++++++++
912
 
913
        always @(posedge BCLK or negedge BRST)
914
                if (!BRST) runflag <= 1'b0;
915
                        else
916
                                runflag <= START[2] | (~ende & runflag);
917
 
918
        always @(posedge BCLK) DONE <= (ende & runflag) | divi_pipe4;
919
 
920
        assign man_1 = (FL | run_divi) ? 21'h0 : MAN1;
921
        assign src_1 = run_divi ? {1'b0,ivalue} : ( FL ? {10'h001,SRC1[22:0]} : {SRC1,1'b0});
922
 
923
        assign load_src1 = START[2] | divi_pipe1;
924
 
925
        //                                                                                                              *2                 +       *1   
926 11 ns32kum
        always @(posedge BCLK) if (load_src1) dreimal <= {1'b0,man_1,src_1,1'b0} + {2'b00,man_1,src_1}; // 54 Bit Reg
927 9 ns32kum
 
928
        always @(posedge BCLK) if (load_src1) save1 <= src_1;
929
 
930
        assign sub1 = divreg - {3'b000, man_1,save1     };
931
        assign sub2 = divreg - {2'b00 ,man_1,save1,1'b0};
932
        assign sub3 = divreg - {1'b0, dreimal         };
933
 
934
        assign load_src2 = START[2] | divi_pipe2;
935
 
936
        always @(posedge BCLK)
937 11 ns32kum
                if (load_src2) divreg <= divi_pipe2 ? {23'h0,shift_32[64:32]} : ( FL ? {34'h0_0000_0001,SRC2[22:0]} : {3'b0,MAN2,SRC2,1'b0});
938 9 ns32kum
                else
939
                        begin
940
                          casex ({sub3[56],sub2[56],sub1[56]})
941
                                3'b0xx : divreg <=   {sub3[54:0],divreg_ext[31:30]};
942
                                3'b10x : divreg <=   {sub2[54:0],divreg_ext[31:30]};
943
                                3'b110 : divreg <=   {sub1[54:0],divreg_ext[31:30]};
944
                          default  : divreg <= {divreg[54:0],divreg_ext[31:30]};
945
                          endcase
946
                        end
947
 
948
        always @(posedge BCLK)  // Extension Register for Integer Division
949
                if (load_src2) divreg_ext <= divi_pipe2 ? shift_32[31:0] : 32'd0;
950
                  else
951
                    divreg_ext <= {divreg_ext[29:0],2'b0};
952
 
953
        always @(posedge BCLK)
954
                if (load_src2) divsr <= 57'h0;
955
                else
956
                        begin
957
                          casex ({sub3[56],sub2[56],sub1[56]})
958
                                3'b0xx : divsr <= {divsr[54:0],2'b11};
959
                                3'b10x : divsr <= {divsr[54:0],2'b10};
960
                                3'b110 : divsr <= {divsr[54:0],2'b01};
961
                          default  : divsr <= {divsr[54:0],2'b00};
962
                          endcase
963
                        end
964
 
965
        // Overflow Detection for DEI : serial calculation
966
        always @(posedge BCLK)
967
                if (load_src2) DEI_OVF[0] <= 1'b0;
968 11 ns32kum
                  else DEI_OVF[0] <= DEI_OVF[0] | (BWD[1] ? |divsr[33:32] : (BWD[0] ? |divsr[17:16] : |divsr[9:8]));
969 9 ns32kum
 
970
        always @(posedge BCLK) DEI_OVF[1] <= divi_pipe4;        // Timing pulse for OVF inclusiv for DIV and QUO
971
 
972
        assign short = (SRCFLAGS[3:0] != 4'h0) & runflag;
973
 
974
        assign ende = ((FL ? (divsr[26] | divsr[25]) : (divsr[56] | divsr[55])) & runflag) | short;
975
 
976
        assign sign = (SRCFLAGS[4] ^ SRCFLAGS[5]) & ~zero;
977
        assign zero =  SRCFLAGS[2] & ~SRCFLAGS[0];               // SRC2 = NULL -> NULL as result
978
        assign nan  =  SRCFLAGS[3] | SRCFLAGS[1] | (SRCFLAGS[2] & SRCFLAGS[0]);
979
                        // one of both NAN or both 0 -> invalid Operation 
980
 
981
        assign orlow_d = (divreg[56:27] != 29'b0) & ~zero & ~FL;        // is there Rest ? [1:0] are always 0.
982
        assign orlow_s = (divreg[26:2]  != 25'b0) & ~zero;
983
 
984
        assign restlsb  = divsr[0] | orlow_s;
985
        assign restlow  = (divsr[1:0] != 2'b00) | orlow_s | orlow_d;
986
        assign resthigh = divsr[2] | restlow;
987
 
988
        always @(posedge BCLK) if (START[0]) exponent <= FL ? ({5'b00,SRC2[30:23]} - {5'b00,SRC1[30:23]})
989
                                                                                                                : ({2'b00,SRC2[30:20]} - {2'b00,SRC1[30:20]});
990
        assign offset   = FL ? 13'h007E : 13'h03FE;
991
        assign expoh    = exponent + {offset[12:1],1'b1};       // Double = 3FF/3FE     Single = 7F/7E
992
        assign expol    = exponent + offset;                            // in case of normalizing
993
 
994
        always @(posedge BCLK)
995
          if (ende && runflag)
996
                casex ({FL,divsr[26],divsr[56]})
997 11 ns32kum
                  3'b11x : OUT <= {nan,zero,sign,expoh[9:8],expoh[7],expoh[7],expoh[7],expoh[7:0],divsr[25:3],28'b0,divsr[3:2],restlow};
998
                  3'b10x : OUT <= {nan,zero,sign,expol[9:8],expol[7],expol[7],expol[7],expol[7:0],divsr[24:2],28'b0,divsr[2:1],restlsb};
999 9 ns32kum
                  3'b0x1 : OUT <= {nan,zero,sign,expoh,divsr[55:3],resthigh};
1000
                  3'b0x0 : OUT <= {nan,zero,sign,expol,divsr[54:2],restlow};
1001
                endcase
1002
 
1003
endmodule
1004
 
1005 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1006 9 ns32kum
//
1007 23 ns32kum
//      10. DP_LOGIK            Control logic and result path for different functions
1008 9 ns32kum
//
1009 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1010
module DP_LOGIK ( BCLK, BRESET, OPCODE, SRC1, SRC2, FSR, START, MRESULT, BWD, FL, MAN1, MAN2, WR_REG, CY_IN,
1011 9 ns32kum
                                  COP_DONE, COP_OP, COP_IN,
1012 23 ns32kum
                                  DOUT, TT_DP, DP_CMP, OVF_BCD, MEI, DFLOAT, DONE, UP_DP, CLR_LSB, WREN_L, DVZ_TRAP, COP_GO );
1013 9 ns32kum
 
1014
// Definition of output word OUT of sub-moduls : the hidden-bit of the mantissa is already gone
1015
//
1016
//   N Z S   Exponent                   Mantissa                                                                                                 Round
1017
//   A E I  Double : 13 Bit             52 Bit                                                                                                           2 Bit
1018
//   N R G  Single : 10 Bit     23 Bit                                                                                                           2 Bit
1019
//     O N                                 -mmmm.mmmm.mmmm.mmmm.mmmm.mmm-.--                                                      -m.
1020
//  -F-F-F-E.EEEE.EEEE.EEEE-MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.MMMM.RR
1021
//
1022
//   6 6 6 6 6666 6655 5555 5555 4444 4444 4433 3333 3333 2222 2222 2211 1111 1111 0000 0000 00
1023
//   9 8 7 6 5432 1098 7654 3210 9876 5432 1098 7654 3210 9876 5432 1098 7654 3210 9876 5432 10
1024
//
1025
// Single FP delivers the exponent in a way, that it is identical for rounding :
1026
//
1027
//  Exponent 61 - 54 => kept
1028
//  Bits 64 - 62 are filled with bit 61 , carry should come through
1029
//  Exponent 62 => Bit 65  , Overflow
1030
//  Exponent 63 => Bit 66  , Underflow
1031
 
1032
        input                   BCLK,BRESET;
1033
        input    [7:0]   OPCODE;
1034
        input   [31:0]   SRC1,SRC2;      // the input data
1035
        input   [20:0]   MAN1,MAN2;      // the MSB of mantissa
1036
        input    [8:3]  FSR;            // Floating Point Status Register
1037
        input    [1:0]   START;
1038
        input  [105:0]   MRESULT;        // Result of multiplier
1039
        input    [1:0]   BWD;            // Size of integer
1040
        input                   FL;
1041
        input                   WR_REG;         // from DECODER
1042
        input                   CY_IN;
1043
        input                   COP_DONE;       // Coprozessor Interface
1044
        input   [23:0]   COP_OP;
1045
        input   [63:0]   COP_IN;
1046
 
1047
        output  [63:0]   DOUT;
1048
        output   [4:0]   TT_DP;          // Trap-Info to FSR
1049
        output   [2:0]   DP_CMP;         // CMPL result
1050
        output   [3:0]   OVF_BCD;        // Integer Division Overflow + BCD Carry update
1051
        output                  MEI,DFLOAT;
1052 23 ns32kum
        output                  DONE,UP_DP,WREN_L;
1053
        output  reg             CLR_LSB;
1054 9 ns32kum
        output                  DVZ_TRAP;
1055
        output  reg             COP_GO;
1056
 
1057
        reg             [63:0]   DOUT;
1058
        reg              [2:0]   DP_CMP;
1059
        reg              [5:0]   preflags;
1060
        reg              [5:0]   srcflags;
1061
        reg             [69:0]   fpout;
1062
        reg              [2:0]   tt;
1063 23 ns32kum
        reg              [7:0]   select;
1064
        reg              [5:0]   wctrl;
1065 9 ns32kum
        reg              [2:1]  sequ;
1066
        reg                             misc_op;
1067
        reg                     car_ry;
1068
        reg                             wr_part2;
1069
        reg                             up_flag;
1070
        reg                             ovf_div;
1071 23 ns32kum
        reg                             late_bcd_done;
1072 9 ns32kum
 
1073
        wire                    zexp2,zman2,zexp1,zman1,znan1;
1074
        wire                    make_i;
1075
        wire                    op_cmp;
1076
        wire    [69:0]   mulout,addout,divout,miscout;
1077
        wire                    go_divf,go_divi,divi_ops,div_done;
1078
        wire                    bcd_ops,man_ops;
1079
        wire    [31:0]   i_out;
1080
        wire    [63:0]   divi_out;
1081
        wire    [66:2]  rund,cy_val;    // Indexnumber like in xxxout
1082
        wire                    div_zero,overflow,underflow,inexact;
1083
        wire     [1:0]   cmpres;
1084
        wire    [63:0]   fp_out,fp_res;
1085
        wire                    wr_part1;
1086
        wire                    done_i;
1087 23 ns32kum
        wire                    later;
1088 9 ns32kum
        wire    [31:0]   bcd_q;
1089
        wire                    bcd_done;
1090
        wire                    bcd_carry;
1091
        wire     [1:0]   dei_ovf;
1092
        wire                    quo_div;
1093
        wire                    copop;
1094
        wire                    copwr;
1095
 
1096
        // Control of datapath : together with START the Double Unit becomes activ 
1097
 
1098
        always @(OPCODE or FL)
1099
                casex (OPCODE)
1100 23 ns32kum
                  8'b1001_000x : select = {5'b0000_0,                   ~FL ,2'b10};    // 0 1 0 :      MOViL
1101
                  8'b1001_010x : select = {5'b0001_1,                   1'b1,2'b00};    // MOVLF
1102
                  8'b1001_011x : select = {5'b0011_1,                   1'b1,2'b00};    // MOVFL
1103
                  8'b1001_100x : select = {5'b0001_0,                   ~FL ,2'b11};    // 0 1 1 :      ROUNDLi
1104
                  8'b1001_101x : select = {5'b0001_0,                   ~FL ,2'b11};    // 0 1 1 :  TRUNCLi
1105
                  8'b1001_111x : select = {5'b0001_0,                   ~FL ,2'b11};    // 0 1 1 :      FLOORLi
1106
                  8'b1011_0000 : select = {5'b1010_0,                   ~FL ,2'b00};    // 0 0 0 :      ADDL    Es werden Shifter wiederverwendet...
1107
                  8'b1011_0010 : select = {5'b1010_0,                   ~FL ,2'b01};    // 0 0 1 :      CMPL
1108
                  8'b1011_0100 : select = {5'b1010_0,                   ~FL ,2'b01};    // 0 0 1 :      SUBL
1109
                  8'b1011_1000 : select = {1'b1,FL,1'b1,FL,1'b0,1'b1,2'b00};    // 1 0 1 :  DIVf , Default Float fuer srcflags
1110
                  8'b1011_1100 : select = {5'b1010_0,                   ~FL ,2'b00};    // 1 0 0 :      MULL
1111
                  8'b1011_0110 : select = {1'b1,FL,1'b1,FL,1'b1,1'b1,2'b00};    // SCALBf
1112
                  8'b1011_0111 : select = {2'b00,FL,2'b1_1,             1'b1,2'b00};    // LOGBf
1113
                  default      : select = 8'b0;
1114 9 ns32kum
                endcase
1115
 
1116
        assign MEI      = (OPCODE == 8'h79);
1117
        assign divi_ops = (OPCODE[7:2] == 6'b0111_11) | (OPCODE == 8'h7B);      // QUO/REM/MOD/DIV & DEI
1118
        assign go_divf  = (OPCODE == 8'hB8) & START[1];                                 // because of runflag in DIV Unit
1119
        assign go_divi  = divi_ops & (OPCODE[2] ? START[1] : START[0]);  // DEI starts with START[0]
1120 23 ns32kum
        assign bcd_ops  = (OPCODE == 8'h71) | (OPCODE == 8'h70);        // ADDP , SUBP
1121 9 ns32kum
 
1122 11 ns32kum
        assign man_ops  = (OPCODE == 8'hB1) | (OPCODE == 8'hB5) | (OPCODE == 8'hB9) | (OPCODE == 8'hBD);        // MOVf,NEGf,XXXf,ABSf
1123 9 ns32kum
 
1124 23 ns32kum
        assign DFLOAT   = (select[2] | copop) & ~FL;    // all Double Floating Point Operations for PREPDATA
1125
        assign make_i   = (select[1:0] == 2'b11) | divi_ops | bcd_ops;   // ROUND/TRUNC/FLOOR for output multiplexer
1126 9 ns32kum
        assign op_cmp   = (OPCODE == 8'hB2) & ~FL;
1127 23 ns32kum
        always @(posedge BCLK) misc_op <= select[3];    // for OUT-Multiplexer
1128 9 ns32kum
 
1129
        assign copop    = (OPCODE == 8'hDD);
1130 11 ns32kum
        assign copwr    = (COP_OP[18:17] == 2'd0) & (COP_OP[13:11] == 3'b111) & (COP_OP[7:5] == 3'b001);        // Custom Convert
1131 9 ns32kum
 
1132
        // SRCFLAGS : special handling for operands is done locally
1133
 
1134
        assign zexp2 = (SRC2[30:20] == 11'd0);
1135
        assign zman2 = (SRC2[19:0] == 20'd0);
1136
        assign zexp1 = (SRC1[30:20] == 11'd0);
1137
        assign zman1 = (SRC1[19:0] == 20'd0);
1138
        assign znan1 = (SRC1[30:20] == 11'h7FF);
1139
 
1140
        always @(posedge BCLK)
1141
                if (START[0])
1142
                  begin
1143
                        srcflags[5] <= SRC2[31];
1144
                        srcflags[4] <= SRC1[31];
1145
                        preflags    <= {(SRC2[30:20] == 11'h7FF),zexp2,zman2,znan1,zexp1,zman1};
1146
                   end
1147
 
1148
        // case Definition : 00 : 0             , if START[i]=0 then there are always 2 long operands
1149
        //                                       01 : 1 Float Operand SCR1
1150
        //                                       10 : 1 Long Operand SRC1+SRC2
1151
        //                                       11 : 2 Float Operands SRC1 , SRC2
1152
 
1153
        always @(posedge BCLK)  // NaN 
1154
                if (START[1])
1155 23 ns32kum
                        case (select[7:6])
1156
                          2'b10 : srcflags[3] <= preflags[5] | (preflags[4] & (~preflags[3] | SRC2[31] | ~zexp2 | ~zman2));
1157
                          2'b11 : srcflags[3] <= (SRC2[30:23] == 8'hFF) | ((SRC2[30:23] == 8'd0) & ((SRC2[22:20] != 3'd0) | ~zman2));   // F:SRC2 = NaN
1158
                        default : srcflags[3] <= 1'b0;
1159 9 ns32kum
                        endcase
1160
 
1161
        always @(posedge BCLK)  // Zero : only exponent ! If denormalized => NaN !
1162 23 ns32kum
                if (START[1])
1163
                        case (select[7:6])
1164
                          2'b10 : srcflags[2] <= preflags[4];   // L:SRC2 = Zero , 2*SRC2
1165
                          2'b11 : srcflags[2] <= (SRC2[30:23] == 8'd0); // F:SRC2 = Zero
1166
                        default : srcflags[2] <= 1'b0;
1167 9 ns32kum
                        endcase
1168
 
1169
        always @(posedge BCLK)  // NaN 
1170
                if (START[1])
1171 23 ns32kum
                        case (select[5:4])
1172
                          2'b01 : srcflags[1] <= znan1 | (zexp1 & (~zman1 | SRC2[31] | ~zexp2 | ~zman2));       // L:(SRC1,SRC2) = NaN , SRC1 = MSB
1173
                          2'b10 : srcflags[1] <= preflags[2] | (preflags[1] & (~preflags[0] | SRC1[31] | ~zexp1 | ~zman1));
1174
                          2'b11 : srcflags[1] <= (SRC1[30:23] == 8'hFF) | ((SRC1[30:23] == 8'd0) & ((SRC1[22:20] != 3'd0) | ~zman1));   // F:SRC1 = NaN
1175
                        default : srcflags[1] <= 1'b0;
1176 9 ns32kum
                        endcase
1177
 
1178
        always @(posedge BCLK)  // Zero : only exponent ! If denormalized => NaN !
1179 23 ns32kum
                if (START[1])
1180
                        case (select[5:4])
1181
                          2'b01 : srcflags[0] <= zexp1;  // L:(SRC1,SRC2) = Zero , SRC1 = MSB, Special Case ROUNDL,etc.
1182
                          2'b10 : srcflags[0] <= preflags[1];    // L:SRC1 = Zero , 2*SRC1
1183
                          2'b11 : srcflags[0] <= (SRC1[30:23] == 8'd0);  // F:SRC1 = Zero
1184
                        default : srcflags[0] <= 1'b0;
1185 9 ns32kum
                        endcase
1186
 
1187
                        // The Sub-moduls : 
1188
 
1189
        DFPU_ADDSUB as_inst     ( .BCLK(BCLK), .START(START), .SRC1(SRC1), .SRC2(SRC2),
1190 23 ns32kum
                                                  .MAN1({~preflags[1],MAN1[19:0]}), .MAN2({~preflags[4],MAN2[19:0]}),
1191 9 ns32kum
                                                  .SRCFLAGS(srcflags), .BWD(BWD), .SELECT({OPCODE[2:1],select[1:0]}),
1192
                                                  .OUT(addout), .IOUT(i_out), .CMPRES(cmpres) );
1193
 
1194
        DFPU_MUL mul_inst       ( .BCLK(BCLK), .SRC1(SRC1), .SRC2(SRC2), .START(START[0]), .MRESULT(MRESULT),
1195
                                                  .OUT(mulout), .SRCFLAGS(srcflags) );
1196
 
1197 11 ns32kum
        DFPU_DIV div_inst       ( .BCLK(BCLK), .BRST(BRESET), .START({go_divi,go_divf,START}), .SRC1(SRC1), .SRC2(SRC2),
1198 9 ns32kum
                                                  .MAN1(MAN1), .MAN2(MAN2), .SRCFLAGS(srcflags), .FL(FL), .OUT(divout), .DONE(div_done),
1199 11 ns32kum
                                                  .BWD(BWD), .OPCODE(OPCODE[2:0]), .DIVI_OUT(divi_out), .DVZ_TRAP(DVZ_TRAP), .DEI_OVF(dei_ovf) );
1200 9 ns32kum
 
1201 23 ns32kum
        DFPU_MISC misc_inst     ( .BCLK(BCLK), .START(START), .SRC1(SRC1), .SRC2(SRC2), .MAN2(MAN2[19:0]), .SRCFLAGS(srcflags),
1202
                                                  .MODE({OPCODE[5],OPCODE[0],FL,OPCODE[1]}), .OUT(miscout) );
1203 9 ns32kum
 
1204 11 ns32kum
        DFPU_BCD bcd_inst       ( .BCLK(BCLK), .BRESET(BRESET), .START(START[1]), .DO_BCD(bcd_ops), .BWD(BWD), .SRC1(SRC1), .SRC2(SRC2),
1205 23 ns32kum
                                                  .CY_IN(CY_IN), .SUBP(~OPCODE[0]), .BCD_Q(bcd_q), .CY_OUT(bcd_carry), .BCD_DONE(bcd_done) );
1206 9 ns32kum
 
1207
        // FP - path : selection of result and rounding :
1208
 
1209
        always @(misc_op or OPCODE or mulout or addout or divout or miscout)
1210
                casex ({misc_op,OPCODE[5],OPCODE[3:2]}) //OPCODE[5] only for Flags i.e. NAN 
1211
                  4'b1xxx : fpout = miscout;            // for MOVLF,MOVFL,SCALB & LOGB
1212
                  4'b0110 : fpout = divout;
1213
                  4'b0111 : fpout = mulout;
1214
                  default : fpout = addout;
1215
                endcase
1216
 
1217
        always @(FSR or fpout)  // Calculation of Carry according to rounding mode, fpout[67] = sign bit
1218
                casex (FSR[8:7])
1219
                  2'b00 : car_ry = ((fpout[1:0] == 2'b10) & fpout[2]) | (fpout[1:0] == 2'b11);    // round to nearest
1220
                  2'b10 : car_ry = ~fpout[67] & (fpout[1:0] != 2'b00);   // round to positiv infinity
1221
                  2'b11 : car_ry =  fpout[67] & (fpout[1:0] != 2'b00);   // round to negativ infinity
1222
                default : car_ry = 1'b0;                                                                // round to zero
1223
                endcase
1224
 
1225
        assign cy_val = {35'h0,(FL & car_ry),28'h0,(~FL & car_ry)};
1226
 
1227
        assign rund = {fpout[66:2]} + cy_val;
1228
 
1229
        // Detection of Div-by-0, Overflow, Underflow and Inexact : Epxonent from [66:54] = 13 Bits
1230 23 ns32kum
        assign div_zero  = (srcflags[3:0] == 4'h1) & ((OPCODE == 8'hB8) | (OPCODE == 8'hB7));    // true FPU Divide by Zero also for LOGBf
1231 9 ns32kum
        assign overflow  = ~rund[66] & (rund[65] | (rund[64:54] == 11'h7FF));
1232
        assign underflow = (rund[66] | (rund[65:54] == 12'h0)) & ~fpout[68];    // Zero-Flag
1233
        assign inexact   = (fpout[1:0] != 2'b00);
1234
 
1235
        always @(fpout or op_cmp or div_zero or overflow or underflow or inexact or FSR)
1236
                casex ({fpout[69],op_cmp,div_zero,overflow,FSR[3],underflow,FSR[5],inexact})    // [69] = NAN
1237
                        8'b1xxxxxxx : tt = 3'b101;      // Invalid operation
1238
                        8'b001xxxxx : tt = 3'b011;      // Divide by Zero
1239
                        8'b0001xxxx : tt = 3'b010;      // Overflow
1240
                        8'b000011xx : tt = 3'b001;      // Underflow
1241
                        8'b00000011 : tt = 3'b110;      // Inexact Result
1242
                        default         : tt = 3'b000;  // no error
1243
                endcase
1244
 
1245 11 ns32kum
        assign TT_DP = man_ops ? 5'd0 : {(inexact & ~op_cmp),(underflow & ~op_cmp),tt}; // at ABSf/NEGf no error : different to NS32381 !
1246 9 ns32kum
 
1247
        assign fp_res = FL ? {fpout[67],rund[61:31],rund[33:2]}
1248
                                           : {fpout[67],rund[64:2]};    // lower 32 bits identical
1249
 
1250
        // Underflow special case and get ZERO
1251
        assign fp_out = (underflow | fpout[68]) ? 64'h0 : fp_res;
1252
 
1253
        // 63..32 goes to memory if Word or Byte ! Also in ODD Register , 31..0 goes in EVEN Register
1254
        // DEI comes without WR_REG information
1255 11 ns32kum
        always @(make_i or copop or MEI or BWD or WR_REG or MRESULT or COP_IN or i_out or fp_out or divi_ops or divi_out or bcd_ops or bcd_q)
1256 9 ns32kum
                casex ({make_i,copop,MEI,BWD})
1257 11 ns32kum
                  5'b00100 : DOUT = {MRESULT[31:8], (WR_REG ? MRESULT[15:8]  : MRESULT[7:0]), MRESULT[31:0]};     // LSD always the same
1258 9 ns32kum
                  5'b00101 : DOUT = {MRESULT[31:16],(WR_REG ? MRESULT[31:16] : MRESULT[15:0]),MRESULT[31:0]};
1259
                  5'b0011x : DOUT =  MRESULT[63:0];
1260
                  5'b01xxx : DOUT =  COP_IN;    // true alignment in Coprocessor
1261 11 ns32kum
                  5'b1xxxx : DOUT = divi_ops ? divi_out : {(bcd_ops ? bcd_q : i_out),fp_out[31:0]};      // MSD is written first
1262 9 ns32kum
                  default  : DOUT = fp_out;
1263
                endcase
1264
 
1265
        always @(posedge BCLK) DP_CMP <= {(srcflags[3] | srcflags[1]),cmpres};  // Only valid if not NaN
1266
 
1267
        // Pipeline Control + Registerfile write control
1268
 
1269
        always @(posedge BCLK or negedge BRESET)
1270
                if (!BRESET) sequ <= 2'b00;
1271
                  else
1272 23 ns32kum
                        sequ <= {(sequ[1] & ~DONE),(START[1] & ~wctrl[5])};
1273 9 ns32kum
 
1274
        always @(FL or OPCODE or copwr)
1275
                casex ({FL,OPCODE})     // WRITE Control : [2] = clr_lsb, [1] = wr_part2, [0] = wr_part1
1276 23 ns32kum
                  9'b0_1001_000x : wctrl = 6'b001_111;  // MOViL
1277
                  9'b1_1001_000x : wctrl = 6'b100_010;  // MOViF  <= SFPU
1278
                  9'bx_1001_010x : wctrl = 6'b000_010;  // MOVLF
1279
                  9'bx_1001_011x : wctrl = 6'b001_111;  // MOVFL
1280
                  9'b0_1001_100x : wctrl = 6'b000_010;  // ROUNDLi
1281
                  9'b0_1001_101x : wctrl = 6'b000_010;  // TRUNCLi
1282
                  9'b0_1001_111x : wctrl = 6'b000_010;  // FLOORLi
1283
                  9'b1_1001_100x : wctrl = 6'b100_010;  // ROUNDFi
1284
                  9'b1_1001_101x : wctrl = 6'b100_010;  // TRUNCFi
1285
                  9'b1_1001_111x : wctrl = 6'b100_010;  // FLOORFi
1286
                  9'b0_1011_0000 : wctrl = 6'b001_111;  // ADDL
1287
                  9'b1_1011_0000 : wctrl = 6'b100_010;  // ADDF   <= SFPU
1288
                  9'b0_1011_0010 : wctrl = 6'b000_000;  // CMPL
1289
                  9'b1_1011_0010 : wctrl = 6'b100_000;  // CMPF
1290
                  9'b0_1011_0100 : wctrl = 6'b001_111;  // SUBL
1291
                  9'b1_1011_0100 : wctrl = 6'b100_010;  // SUBF   <= SFPU
1292
                  9'b1_1011_1000 : wctrl = 6'b010_001;  // DIVF - measured 18 clocks Reg-Reg
1293
                  9'b0_1011_1000 : wctrl = 6'b010_111;  // DIVL - measured 34 clocks Reg-Reg
1294
                  9'b0_1011_1100 : wctrl = 6'b001_111;  // MULL
1295
                  9'b1_1011_1100 : wctrl = 6'b100_010;  // MULF   <= SFPU
1296
                  9'bx_0111_000x : wctrl = 6'b100_010;  // ADDP,SUBP
1297
                  9'bx_0111_1001 : wctrl = 6'b000_111;  // MEIi
1298
                  9'bx_0111_1011 : wctrl = 6'b010_111;  // DEIi
1299
                  9'bx_0111_11xx : wctrl = 6'b010_001;  // QUOi,REMi,MODi,DIVi
1300
                  9'b1_1011_011x : wctrl = 6'b000_010;  // SCALBF/LOGBF
1301
                  9'b0_1011_011x : wctrl = 6'b001_111;  // SCALBL/LOGBL
1302
                  9'bx_1101_1101 : wctrl = {5'b010_00,copwr};   // Coprocessor opcode
1303
                  default        : wctrl = 6'b00;
1304 9 ns32kum
                endcase
1305
 
1306 23 ns32kum
        assign later = wctrl[3] & WR_REG;       // if DEST = Reg and 64 bit of data then DONE comes 1 clock later
1307
        assign done_i = wctrl[4] ? (div_done | COP_DONE) : ( later ? sequ[2] : sequ[1] );
1308
        assign DONE = wctrl[5] ? (bcd_ops ? bcd_done : START[1]) : ~START[1] & done_i;  // DONE is valid for all opcodes
1309 9 ns32kum
 
1310
        assign wr_part1 = DONE & WR_REG & wctrl[0];
1311
 
1312
        always @(posedge BCLK) CLR_LSB  <= DONE & WR_REG & wctrl[2];
1313
        always @(posedge BCLK) wr_part2 <= DONE & WR_REG & wctrl[1];
1314
 
1315
        assign WREN_L   = wr_part1 | wr_part2;
1316
 
1317 23 ns32kum
        always @(posedge BCLK) up_flag <= DONE & ~later;                // DONE one cycle later
1318
        assign UP_DP    = (select[2] & (later ? DONE : up_flag)) | man_ops;     // Update FSR Trap etc. : all FPU opcodes of DP_FPU
1319 9 ns32kum
 
1320
        // Overflow Trap for Division : DEI, QUO, DIV
1321
        assign quo_div = (OPCODE == 8'h7C) | (OPCODE == 8'h7F);
1322
        always @(*)
1323
                casex ({OPCODE[2],BWD})
1324
                   3'b100 : ovf_div = (divi_out[39] & SRC1[7]  & SRC2[7] ) & quo_div;
1325
                   3'b101 : ovf_div = (divi_out[47] & SRC1[15] & SRC2[15]) & quo_div;
1326
                   3'b11x : ovf_div = (divi_out[63] & SRC1[31] & SRC2[31]) & quo_div;
1327
                  default : ovf_div = dei_ovf[0] & (OPCODE == 8'h7B);    // DEI
1328
                endcase
1329
 
1330 23 ns32kum
        always @(posedge BCLK) late_bcd_done <= bcd_done;       // parallel to data write
1331
 
1332
        assign OVF_BCD = {dei_ovf[1],ovf_div,late_bcd_done,bcd_carry};  // to I_PFAD
1333 9 ns32kum
 
1334
        always @(posedge BCLK) COP_GO <= START[1] & copop;
1335
 
1336
endmodule
1337
 
1338 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1339 9 ns32kum
//
1340 23 ns32kum
// 11. DP_FPU           Top level of long operations datapath
1341 9 ns32kum
//
1342 11 ns32kum
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1343 23 ns32kum
module DP_FPU( BCLK, FL, BRESET, LD_OUT, WR_REG, BWD, FSR, OPCODE, SRC1, SRC2, START, CY_IN, COP_DONE, COP_OP, COP_IN,
1344
                           DONE, UP_DP, WREN_L, CLR_LSB, DVZ_TRAP, DP_CMP, DP_OUT, DP_Q, TT_DP, OVF_BCD, COP_GO, COP_OUT );
1345 9 ns32kum
 
1346
input                   BCLK;
1347
input                   FL;
1348
input                   BRESET;
1349 23 ns32kum
input    [1:0]   LD_OUT;
1350 9 ns32kum
input                   WR_REG;
1351
input    [1:0]   BWD;
1352
input    [8:3]  FSR;
1353
input    [7:0]   OPCODE;
1354
input   [31:0]   SRC1;
1355
input   [31:0]   SRC2;
1356
input    [1:0]   START;
1357
input                   CY_IN;
1358
input                   COP_DONE;
1359
input   [23:0]   COP_OP;
1360
input   [63:0]   COP_IN;
1361
 
1362
output                  DONE;
1363
output                  UP_DP;
1364
output                  WREN_L;
1365
output                  CLR_LSB;
1366
output                  DVZ_TRAP;
1367
output   [2:0]   DP_CMP;
1368
output  [31:0]   DP_OUT;
1369
output  [31:0]   DP_Q;
1370
output   [4:0]   TT_DP;
1371
output   [3:0]   OVF_BCD;
1372
output                  COP_GO;
1373
output [127:0]   COP_OUT;
1374
 
1375
reg             [52:0]   MDA;
1376
reg             [52:0]   MDB;
1377
reg             [31:0]   DP_Q;
1378
reg        [31:20]      RCOPA,RCOPB;
1379
 
1380
wire    [63:0]   DOUT;
1381
wire   [105:0]   MRESULT;
1382
wire                    MEI;
1383
wire                    DFLOAT;
1384
wire                    LOAD_MSD;
1385
wire                    LOAD_LSD1;
1386
wire                    LOAD_LSD2;
1387
wire    [31:0]   LSD_1;
1388
wire    [31:0]   LSD_2;
1389
wire   [52:32]  MSD_1;
1390
wire   [52:32]  MSD_2;
1391
 
1392
 
1393
DP_LOGIK        DOUBLE_U(
1394
        .FL(FL),
1395
        .BRESET(BRESET),
1396
        .BCLK(BCLK),
1397
        .WR_REG(WR_REG),
1398
        .BWD(BWD),
1399
        .FSR(FSR),
1400
        .MAN1(MDA[52:32]),
1401
        .MAN2(MDB[52:32]),
1402
        .MRESULT(MRESULT),
1403
        .OPCODE(OPCODE),
1404
        .SRC1(SRC1),
1405
        .SRC2(SRC2),
1406
        .START(START),
1407
        .MEI(MEI),
1408
        .DFLOAT(DFLOAT),
1409
        .DONE(DONE),
1410
        .UP_DP(UP_DP),
1411
        .CLR_LSB(CLR_LSB),
1412
        .WREN_L(WREN_L),
1413
        .DVZ_TRAP(DVZ_TRAP),
1414
        .DOUT(DOUT),
1415
        .DP_CMP(DP_CMP),
1416
        .TT_DP(TT_DP),
1417
        .CY_IN(CY_IN),
1418
        .OVF_BCD(OVF_BCD),
1419
        .COP_DONE(COP_DONE),
1420
        .COP_OP(COP_OP),
1421
        .COP_IN(COP_IN),
1422
        .COP_GO(COP_GO));
1423
 
1424
PREPDATA        DP_PREP(
1425
        .MEI(MEI),
1426
        .DFLOAT(DFLOAT),
1427
        .BWD(BWD),
1428
        .SRC1(SRC1),
1429
        .SRC2(SRC2),
1430
        .START(START),
1431
        .LOAD_LSD1(LOAD_LSD1),
1432
        .LOAD_LSD2(LOAD_LSD2),
1433
        .LOAD_MSD(LOAD_MSD),
1434
        .LSD_1(LSD_1),
1435
        .LSD_2(LSD_2),
1436
        .MSD_1(MSD_1),
1437
        .MSD_2(MSD_2));
1438
 
1439
        assign MRESULT = MDA * MDB;     // unsigned multiplier 53 * 53 bits = 106 bits
1440
 
1441
        assign DP_OUT = CLR_LSB ? DP_Q : DOUT[63:32];
1442
 
1443 23 ns32kum
        always@(posedge BCLK) if (LD_OUT[1] || LD_OUT[0] || WREN_L) DP_Q <= LD_OUT[0] ? SRC2 : DOUT[31:0];
1444 9 ns32kum
 
1445
        always@(posedge BCLK) if (LOAD_LSD1) MDA[31:0] <= LSD_1;
1446
 
1447
        always@(posedge BCLK) if (LOAD_LSD2) MDB[31:0] <= LSD_2;
1448
 
1449
        always@(posedge BCLK)
1450
                if (LOAD_MSD)
1451
                        begin
1452
                                MDA[52:32] <= MSD_1;
1453
                                MDB[52:32] <= MSD_2;
1454
                                RCOPA      <= SRC1[31:20];
1455
                                RCOPB      <= SRC2[31:20];
1456
                        end
1457
 
1458
        assign COP_OUT = {RCOPA,MDA[51:32],SRC1,RCOPB,MDB[51:32],SRC2};
1459
 
1460
endmodule

powered by: WebSVN 2.1.0

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