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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpTrunc.sv] - Blame information for rev 21

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

Line No. Rev Author Line
1 21 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      fpTrunc.v
9
//              - convert floating point to integer (chop off fractional bits)
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
// ============================================================================
29
 
30
module fpTrunc
31
#(      parameter WID = 32)
32
(
33
        input clk,
34
        input ce,
35
        input [WID-1:0] i,
36
        output reg [WID-1:0] o,
37
        output overflow
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 ? 11 :
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 ? 34 :
57
                                  WID==44 ? 31 :
58
                                  WID==42 ? 29 :
59
                                  WID==40 ? 28 :
60
                                  WID==32 ? 22 :
61
                                  WID==24 ? 15 : 9;
62
 
63
integer n;
64
wire [MSB:0] maxInt  = {MSB{1'b1}};             // maximum unsigned integer value
65
wire [EMSB:0] zeroXp = {EMSB{1'b1}};    // simple constant - value of exp for zero
66
 
67
// Decompose fp value
68
reg sgn;                                                                        // sign
69
reg [EMSB:0] exp;
70
reg [FMSB:0] man;
71
reg [FMSB:0] mask;
72
 
73
wire [7:0] shamt = FMSB - (exp - zeroXp);
74
always @*
75
for (n = 0; n <= FMSB; n = n +1)
76
        mask[n] = (n > shamt);
77
 
78
always @*
79
        sgn = i[MSB];
80
always @*
81
        exp = i[MSB-1:FMSB+1];
82
always @*
83
        if (exp > zeroXp + FMSB)
84
                man = i[FMSB:0];
85
        else
86
                man = i[FMSB:0] & mask;
87
 
88
always @(posedge clk)
89
        if (ce) begin
90
                if (exp < zeroXp)
91
                        o <= 1'd0;
92
                else
93
                        o <= {sgn,exp,man};
94
        end
95
 
96
endmodule

powered by: WebSVN 2.1.0

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