URL
https://opencores.org/ocsvn/ft816float/ft816float/trunk
Subversion Repositories ft816float
[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [i2f.sv] - Rev 48
Compare with Previous | Blame | View Log
// ============================================================================// __// \\__/ o\ (C) 2006-2020 Robert Finch, Waterloo// \ __ / All rights reserved.// \/_// robfinch<remove>@finitron.ca// ||//// i2f.sv// - convert integer to floating point// - parameterized width// - IEEE 754 representation// - pipelineable// - single cycle latency//// 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/>.//// ============================================================================import fp::*;module i2f (clk, ce, op, rm, i, o);input clk;input ce;input op; // 1 = signed, 0 = unsignedinput [2:0] rm; // rounding modeinput [FPWID-1:0] i; // integer inputoutput [FPWID-1:0] o; // float outputwire [EMSB:0] zeroXp = {EMSB{1'b1}};wire iz; // zero input ?wire [MSB:0] imag; // get magnitude of iwire [MSB:0] imag1 = (op & i[MSB]) ? -i : i;wire [7:0] lz; // count the leading zeros in the numberwire [EMSB:0] wd; // compute number of whole digitswire so; // copy the sign of the input (easy)wire [2:0] rmd;delay1 #(3) u0 (.clk(clk), .ce(ce), .i(rm), .o(rmd) );delay1 #(1) u1 (.clk(clk), .ce(ce), .i(i==0), .o(iz) );delay1 #(FPWID) u2 (.clk(clk), .ce(ce), .i(imag1), .o(imag) );delay1 #(1) u3 (.clk(clk), .ce(ce), .i(i[MSB]), .o(so) );generateif (FPWID==128) begincntlz128Reg u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz) );end else if (FPWID==96) begincntlz96Reg u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz[6:0]) );assign lz[7]=1'b0;end else if (FPWID==84) begincntlz96Reg u4 (.clk(clk), .ce(ce), .i({imag1,12'hfff}), .o(lz[6:0]) );assign lz[7]=1'b0;end else if (FPWID==80) begincntlz80Reg u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz[6:0]) );assign lz[7]=1'b0;end else if (FPWID==64) begincntlz64Reg u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz[6:0]) );assign lz[7]=1'b0;end else if (FPWID==32) begincntlz32Reg u4 (.clk(clk), .ce(ce), .i(imag1), .o(lz[5:0]) );assign lz[7:6]=2'b00;end else beginalways @* begin$display("Uncoded leading zero count in i2f");$finish;endendendgenerateassign wd = zeroXp - 1 + FPWID - lz; // constant except for lzwire [EMSB:0] xo = iz ? 0 : wd;wire [MSB:0] simag = imag << lz; // left align numberwire g = simag[EMSB+2]; // guard bit (lsb)wire r = simag[EMSB+1]; // rounding bitwire s = |simag[EMSB:0]; // "sticky" bitreg rnd;// Compute the round bitalways @(rmd,g,r,s,so)case (rmd)3'd0: rnd = (g & r) | (r & s); // round to nearest even3'd1: rnd = 0; // round to zero (truncate)3'd2: rnd = (r | s) & !so; // round towards +infinity3'd3: rnd = (r | s) & so; // round towards -infinity3'd4: rnd = (r | s);default: rnd = (g & r) | (r & s); // round to nearest evenendcase// "hide" the leading one bit = MSB-1// round the resultwire [FMSB:0] mo = simag[MSB-1:EMSB+1]+rnd;assign o = {op & so,xo,mo};endmodule
