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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [f2i.sv] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      f2i.v
9
//              - convert floating point to integer
10
//              - single cycle latency floating point unit
11
//              - parameterized width
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 .
27
//
28
//      i2f - convert integer to floating point
29
//  f2i - convert floating point to integer
30
//
31
// ============================================================================
32
 
33
import fp::*;
34
 
35
module f2i(clk, ce, op, i, o, overflow);
36
input clk;
37
input ce;
38
input op;                                       // 1 = signed, 0 = unsigned
39
input [MSB:0] i;
40
output [MSB:0] o;
41
output overflow;
42
 
43
wire [MSB:0] maxInt  = op ? {MSB{1'b1}} : {FPWID{1'b1}};                // maximum integer value
44
wire [EMSB:0] zeroXp = {EMSB{1'b1}};    // simple constant - value of exp for zero
45
 
46
// Decompose fp value
47
reg sgn;                                                                        // sign
48
always @(posedge clk)
49
        if (ce) sgn = i[MSB];
50
wire [EMSB:0] exp = i[MSB-1:FMSB+1];            // exponent
51
wire [FMSB+1:0] man = {exp!=0,i[FMSB:0]};       // mantissa including recreate hidden bit
52
 
53
wire iz = i[MSB-1:0]==0;                                        // zero value (special)
54
 
55
assign overflow  = exp - zeroXp > (op ? MSB : FPWID);           // lots of numbers are too big - don't forget one less bit is available due to signed values
56
wire underflow = exp < zeroXp - 1;                      // value less than 1/2
57
 
58
wire [7:0] shamt = (op ? MSB : FPWID) - (exp - zeroXp); // exp - zeroXp will be <= MSB
59
 
60
wire [MSB+1:0] o1 = {man,{EMSB+1{1'b0}},1'b0} >> shamt; // keep an extra bit for rounding
61
wire [MSB:0] o2 = o1[MSB+1:1] + o1[0];          // round up
62
reg [MSB:0] o3;
63
 
64
always @(posedge clk)
65
        if (ce) begin
66
                if (underflow|iz)
67
                        o3 <= 0;
68
                else if (overflow)
69
                        o3 <= maxInt;
70
                // value between 1/2 and 1 - round up
71
                else if (exp==zeroXp-1)
72
                        o3 <= 1;
73
                // value > 1
74
                else
75
                        o3 <= o2;
76
        end
77
 
78
assign o = (op & sgn) ? -o3 : o3;                                        // adjust output for correct signed value
79
 
80
endmodule
81
 

powered by: WebSVN 2.1.0

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