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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [f2i.v] - Blame information for rev 84

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      f2i.v
9
//              - convert floating point to integer
10
//              - single cycle latency floating point unit
11
//              - parameterized FPWIDth
12
//              - IEEE 754 representation
13
//
14
//
15
// This source file is free software: you can redistribute it and/or modify 
16
// it under the terms of the GNU Lesser General Public License as published 
17
// by the Free Software Foundation, either version 3 of the License, or     
18
// (at your option) any later version.                                      
19
//                                                                          
20
// This source file is distributed in the hope that it will be useful,      
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
23
// GNU General Public License for more details.                             
24
//                                                                          
25
// You should have received a copy of the GNU General Public License        
26
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
27
//                                                                          
28
//      i2f - convert integer to floating point
29
//  f2i - convert floating point to integer
30
//
31
// ============================================================================
32
 
33
`include "fpConfig.sv"
34
 
35
module f2i(clk, ce, i, o, overflow);
36
parameter FPWID = 32;
37
`include "fpSize.sv"
38
input clk;
39
input ce;
40
input [MSB:0] i;
41
output [MSB:0] o;
42
output overflow;
43
 
44
wire [MSB:0] maxInt  = {MSB{1'b1}};              // maximum unsigned integer value
45
wire [EMSB:0] zeroXp = {EMSB{1'b1}};     // simple constant - value of exp for zero
46
 
47
// Decompose fp value
48
reg sgn;                                                                        // sign
49
always @(posedge clk)
50
        if (ce) sgn = i[MSB];
51
wire [EMSB:0] exp = i[MSB-1:FMSB+1];             // exponent
52
wire [FMSB+1:0] man = {exp!=0,i[FMSB:0]};  // mantissa including recreate hidden bit
53
 
54
wire iz = i[MSB-1:0]==0;                                  // zero value (special)
55
 
56
assign overflow  = exp - zeroXp > MSB - `EXTRA_BITS;            // lots of numbers are too big - don't forget one less bit is available due to signed values
57
wire underflow = exp < zeroXp - 1;                      // value less than 1/2
58
 
59
wire [7:0] shamt = MSB - (exp - zeroXp); // exp - zeroXp will be <= MSB
60
 
61
wire [MSB+1:0] o1 = {man,{EMSB+1{1'b0}},1'b0} >> shamt;  // keep an extra bit for rounding
62
wire [MSB:0] o2 = o1[MSB+1:1] + o1[0];            // round up
63
reg [MSB:0] o3;
64
 
65
always @(posedge clk)
66
        if (ce) begin
67
                if (underflow|iz)
68
                        o3 <= 0;
69
                else if (overflow)
70
                        o3 <= maxInt;
71
                // value between 1/2 and 1 - round up
72
                else if (exp==zeroXp-1)
73
                        o3 <= 1;
74
                // value > 1
75
                else
76
                        o3 <= o2;
77
        end
78
 
79
assign o = sgn ? -o3 : o3;                                      // adjust output for correct signed value
80
 
81
endmodule
82
 
83
module f2i_tb();
84
 
85
wire ov0,ov1;
86
wire [31:0] io0,io1;
87
reg clk;
88
 
89
initial begin
90
        clk = 0;
91
end
92
 
93
always #10 clk = ~clk;
94
 
95
f2i #(32) u1 (.clk(clk), .ce(1'b1), .i(32'h3F800000), .o(io1), .overflow(ov1) );
96
f2i #(32) u2 (.clk(clk), .ce(1'b1), .i(32'h00000000), .o(io0), .overflow(ov0) );
97
f2i #(80) u3 (.clk(clk), .ce(1'b1), .i(80'h3FF80000000000000000), .o(io1), .overflow(ov1) );
98
 
99
endmodule

powered by: WebSVN 2.1.0

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