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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpTrunc.sv] - Rev 21

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

// ============================================================================
//        __
//   \\__/ o\    (C) 2019  Robert Finch, Waterloo
//    \  __ /    All rights reserved.
//     \/_//     robfinch<remove>@finitron.ca
//       ||
//
//      fpTrunc.v
//              - convert floating point to integer (chop off fractional bits)
//              - single cycle latency floating point unit
//              - parameterized width
//              - IEEE 754 representation
//
//
// This source file is free software: you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation, either version 3 of the License, or     
// (at your option) any later version.                                      
//                                                                          
// This source file is distributed in the hope that it will be useful,      
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
// GNU General Public License for more details.                             
//                                                                          
// You should have received a copy of the GNU General Public License        
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
//                                                                          
// ============================================================================

module fpTrunc
#(      parameter WID = 32)
(
        input clk,
        input ce,
        input [WID-1:0] i,
        output reg [WID-1:0] o,
        output overflow
);
localparam MSB = WID-1;
localparam EMSB = WID==128 ? 14 :  
                  WID==96 ? 14 :
                  WID==80 ? 14 :
                  WID==64 ? 10 :
                                  WID==52 ? 10 :
                                  WID==48 ? 11 :
                                  WID==44 ? 10 :
                                  WID==42 ? 10 :
                                  WID==40 ?  9 :
                                  WID==32 ?  7 :
                                  WID==24 ?  6 : 4;
localparam FMSB = WID==128 ? 111 :
                  WID==96 ? 79 :
                  WID==80 ? 63 :
                  WID==64 ? 51 :
                                  WID==52 ? 39 :
                                  WID==48 ? 34 :
                                  WID==44 ? 31 :
                                  WID==42 ? 29 :
                                  WID==40 ? 28 :
                                  WID==32 ? 22 :
                                  WID==24 ? 15 : 9;

integer n;
wire [MSB:0] maxInt  = {MSB{1'b1}};             // maximum unsigned integer value
wire [EMSB:0] zeroXp = {EMSB{1'b1}};    // simple constant - value of exp for zero

// Decompose fp value
reg sgn;                                                                        // sign
reg [EMSB:0] exp;
reg [FMSB:0] man;
reg [FMSB:0] mask;

wire [7:0] shamt = FMSB - (exp - zeroXp);
always @*
for (n = 0; n <= FMSB; n = n +1)
        mask[n] = (n > shamt);

always @*       
        sgn = i[MSB];
always @*
        exp = i[MSB-1:FMSB+1];
always @*
        if (exp > zeroXp + FMSB)
                man = i[FMSB:0];
        else
                man = i[FMSB:0] & mask;

always @(posedge clk)
        if (ce) begin
                if (exp < zeroXp)
                        o <= 1'd0;
                else
                        o <= {sgn,exp,man};
        end

endmodule

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

powered by: WebSVN 2.1.0

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