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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [i2f.v] - Blame information for rev 15

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

Line No. Rev Author Line
1 8 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2016  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      i2f.v
9
//  - convert integer to floating point
10
//  - parameterized width
11
//  - IEEE 754 representation
12
//  - pipelineable
13
//  - single cycle latency
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
// ============================================================================
29
 
30
module i2f
31
#(      parameter WID = 32)
32
(
33
        input clk,
34
        input ce,
35
        input [2:0] rm,                  // rounding mode
36
        input [WID-1:0] i,               // integer input
37
        output [WID-1:0] o               // float output
38
);
39
localparam MSB = WID-1;
40
localparam EMSB = WID==128 ? 14 :
41
                  WID==96 ? 14 :
42
                  WID==80 ? 14 :
43
                  WID==64 ? 10 :
44
                                  WID==52 ? 10 :
45
                                  WID==48 ? 10 :
46
                                  WID==44 ? 10 :
47
                                  WID==42 ? 10 :
48
                                  WID==40 ?  9 :
49
                                  WID==32 ?  7 :
50
                                  WID==24 ?  6 : 4;
51
localparam FMSB = WID==128 ? 111 :
52
                  WID==96 ? 79 :
53
                  WID==80 ? 63 :
54
                  WID==64 ? 51 :
55
                                  WID==52 ? 39 :
56
                                  WID==48 ? 35 :
57
                                  WID==44 ? 31 :
58
                                  WID==42 ? 29 :
59
                                  WID==40 ? 28 :
60
                                  WID==32 ? 22 :
61
                                  WID==24 ? 15 : 9;
62
 
63
wire [EMSB:0] zeroXp = {EMSB{1'b1}};
64
 
65
wire iz;                        // zero input ?
66
wire [MSB:0] imag;       // get magnitude of i
67
wire [MSB:0] imag1 = i[MSB] ? -i : i;
68
wire [7:0] lz;           // count the leading zeros in the number
69
wire [EMSB:0] wd;        // compute number of whole digits
70
wire so;                        // copy the sign of the input (easy)
71
wire [2:0] rmd;
72
 
73
delay1 #(3)   u0 (.clk(clk), .ce(ce), .i(rm),     .o(rmd) );
74
delay1 #(1)   u1 (.clk(clk), .ce(ce), .i(i==0),   .o(iz) );
75
delay1 #(WID) u2 (.clk(clk), .ce(ce), .i(imag1),  .o(imag) );
76
delay1 #(1)   u3 (.clk(clk), .ce(ce), .i(i[MSB]), .o(so) );
77
generate
78
if (WID==128) begin
79
cntlz128Reg    u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );
80
end else if (WID==96) begin
81
cntlz96Reg    u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );
82
end else if (WID==80) begin
83
cntlz80Reg    u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );
84
end else if (WID==64) begin
85
cntlz64Reg    u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );
86
end else begin
87
cntlz32Reg    u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );
88
assign lz[6]=1'b0;
89
end
90
endgenerate
91
 
92
assign wd = zeroXp - 1 + WID - lz;      // constant except for lz
93
 
94
wire [EMSB:0] xo = iz ? 0 : wd;
95
wire [MSB:0] simag = imag << lz;         // left align number
96
 
97
wire g =  simag[EMSB+2];        // guard bit (lsb)
98
wire r =  simag[EMSB+1];        // rounding bit
99
wire s = |simag[EMSB:0]; // "sticky" bit
100
reg rnd;
101
 
102
// Compute the round bit
103
always @(rmd,g,r,s,so)
104
        case (rmd)
105
        3'd0:   rnd = (g & r) | (r & s);        // round to nearest even
106
        3'd1:   rnd = 0;                                 // round to zero (truncate)
107
        3'd2:   rnd = (r | s) & !so;            // round towards +infinity
108
        3'd3:   rnd = (r | s) & so;                     // round towards -infinity
109
        3'd4:   rnd = (r | s);
110
        endcase
111
 
112
// "hide" the leading one bit = MSB-1
113
// round the result
114
wire [FMSB:0] mo = simag[MSB-1:EMSB+1]+rnd;
115
 
116
assign o = {so,xo,mo};
117
 
118
endmodule
119
 
120
 
121
module i2f_tb();
122
 
123
reg clk;
124
reg [7:0] cnt;
125
wire [31:0] fo;
126
reg [31:0] i;
127
initial begin
128
clk = 1'b0;
129
cnt = 0;
130
end
131
always #10 clk=!clk;
132
 
133
always @(posedge clk)
134
        cnt = cnt + 1;
135
 
136
always @(cnt)
137
case(cnt)
138
8'd0:   i <= 32'd0;
139
8'd1:   i <= 32'd16777226;
140
endcase
141
 
142
i2f #(32) u1 (.clk(clk), .ce(1), .rm(2'd0), .i(i), .o(fo) );
143
i2f #(80) u2 (.clk(clk), .ce(1), .rm(2'd0), .i(i), .o(fo) );
144
 
145
endmodule

powered by: WebSVN 2.1.0

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