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

Subversion Repositories thor

[/] [thor/] [trunk/] [rtl/] [verilog/] [fpUnit/] [f2i.v] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 robfinch
/* ===============================================================
2
        (C) 2006  Robert Finch
3
        All rights reserved.
4
        rob@birdcomputer.ca
5
 
6
        f2i.v
7
                - convert floating point to integer
8
                - parameterized width
9
                - IEEE 754 representation
10
 
11
        This source code is free for use and modification for
12
        non-commercial or evaluation purposes, provided this
13
        copyright statement and disclaimer remains present in
14
        the file.
15
 
16
        If the code is modified, please state the origin and
17
        note that the code has been modified.
18
 
19
        NO WARRANTY.
20
        THIS Work, IS PROVIDEDED "AS IS" WITH NO WARRANTIES OF
21
        ANY KIND, WHETHER EXPRESS OR IMPLIED. The user must assume
22
        the entire risk of using the Work.
23
 
24
        IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25
        ANY INCIDENTAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES
26
        WHATSOEVER RELATING TO THE USE OF THIS WORK, OR YOUR
27
        RELATIONSHIP WITH THE AUTHOR.
28
 
29
        IN ADDITION, IN NO EVENT DOES THE AUTHOR AUTHORIZE YOU
30
        TO USE THE WORK IN APPLICATIONS OR SYSTEMS WHERE THE
31
        WORK'S FAILURE TO PERFORM CAN REASONABLY BE EXPECTED
32
        TO RESULT IN A SIGNIFICANT PHYSICAL INJURY, OR IN LOSS
33
        OF LIFE. ANY SUCH USE BY YOU IS ENTIRELY AT YOUR OWN RISK,
34
        AND YOU AGREE TO HOLD THE AUTHOR AND CONTRIBUTORS HARMLESS
35
        FROM ANY CLAIMS OR LOSSES RELATING TO SUCH UNAUTHORIZED
36
        USE.
37
 
38
        - pipelinable
39
        - one cycle latency
40
 
41
        Ref: Spartan3-4
42
        212 LUTs / 135 slices / (28.2 ns no clock)
43
=============================================================== */
44
 
45
module f2i
46
#(      parameter WID = 32)
47
(
48
        input clk,
49
        input ce,
50
        input [WID-1:0] i,
51
        output [WID-1:0] o,
52
        output overflow
53
);
54
localparam MSB = WID-1;
55
localparam EMSB = WID==80 ? 14 :
56
                  WID==64 ? 10 :
57
                                  WID==52 ? 10 :
58
                                  WID==48 ? 10 :
59
                                  WID==44 ? 10 :
60
                                  WID==42 ? 10 :
61
                                  WID==40 ?  9 :
62
                                  WID==32 ?  7 :
63
                                  WID==24 ?  6 : 4;
64
localparam FMSB = WID==80 ? 63 :
65
                  WID==64 ? 51 :
66
                                  WID==52 ? 39 :
67
                                  WID==48 ? 35 :
68
                                  WID==44 ? 31 :
69
                                  WID==42 ? 29 :
70
                                  WID==40 ? 28 :
71
                                  WID==32 ? 22 :
72
                                  WID==24 ? 15 : 9;
73
 
74
 
75
wire [MSB:0] maxInt  = {MSB{1'b1}};              // maximum unsigned integer value
76
wire [EMSB:0] zeroXp = {EMSB{1'b1}};     // simple constant - value of exp for zero
77
 
78
// Decompose fp value
79
reg sgn;                                                                        // sign
80
always @(posedge clk)
81
        if (ce) sgn = i[MSB];
82
wire [EMSB:0] exp = i[MSB-1:FMSB+1];             // exponent
83
wire [FMSB+1:0] man = {exp!=0,i[FMSB:0]};  // mantissa including recreate hidden bit
84
 
85
wire iz = i[MSB-1:0]==0;                                  // zero value (special)
86
 
87
assign overflow  = exp - zeroXp > MSB;          // lots of numbers are too big - don't forget one less bit is available due to signed values
88
wire underflow = exp < zeroXp - 1;                      // value less than 1/2
89
 
90
wire [6:0] shamt = MSB - (exp - zeroXp); // exp - zeroXp will be <= MSB
91
 
92
wire [MSB+1:0] o1 = {man,{EMSB+1{1'b0}},1'b0} >> shamt;  // keep an extra bit for rounding
93
wire [MSB:0] o2 = o1[MSB+1:1] + o1[0];            // round up
94
reg [MSB:0] o3;
95
 
96
always @(posedge clk)
97
        if (ce) begin
98
                if (underflow|iz)
99
                        o3 <= 0;
100
                else if (overflow)
101
                        o3 <= maxInt;
102
                // value between 1/2 and 1 - round up
103
                else if (exp==zeroXp-1)
104
                        o3 <= 1;
105
                // value > 1
106
                else
107
                        o3 <= o2;
108
        end
109
 
110
assign o = sgn ? -o3 : o3;                                      // adjust output for correct signed value
111
 
112
endmodule
113
 
114
module f2i_tb();
115
 
116
wire ov0,ov1;
117
wire [31:0] io0,io1;
118
reg clk;
119
 
120
initial begin
121
        clk = 0;
122
end
123
 
124
always #10 clk = ~clk;
125
 
126
f2i #(32) u1 (.clk(clk), .ce(1'b1), .i(32'h3F800000), .o(io1), .overflow(ov1) );
127
f2i #(32) u2 (.clk(clk), .ce(1'b1), .i(32'h00000000), .o(io0), .overflow(ov0) );
128
 
129
endmodule

powered by: WebSVN 2.1.0

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