URL
https://opencores.org/ocsvn/ft816float/ft816float/trunk
Subversion Repositories ft816float
[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [f2i.sv] - Rev 48
Compare with Previous | Blame | View Log
// ============================================================================// __// \\__/ o\ (C) 2006-2020 Robert Finch, Waterloo// \ __ / All rights reserved.// \/_// robfinch<remove>@finitron.ca// ||//// f2i.v// - convert floating point to integer// - 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/>.//// i2f - convert integer to floating point// f2i - convert floating point to integer//// ============================================================================import fp::*;module f2i(clk, ce, op, i, o, overflow);input clk;input ce;input op; // 1 = signed, 0 = unsignedinput [MSB:0] i;output [MSB:0] o;output overflow;wire [MSB:0] maxInt = op ? {MSB{1'b1}} : {FPWID{1'b1}}; // maximum integer valuewire [EMSB:0] zeroXp = {EMSB{1'b1}}; // simple constant - value of exp for zero// Decompose fp valuereg sgn; // signalways @(posedge clk)if (ce) sgn = i[MSB];wire [EMSB:0] exp = i[MSB-1:FMSB+1]; // exponentwire [FMSB+1:0] man = {exp!=0,i[FMSB:0]}; // mantissa including recreate hidden bitwire iz = i[MSB-1:0]==0; // zero value (special)assign overflow = exp - zeroXp > (op ? MSB : FPWID); // lots of numbers are too big - don't forget one less bit is available due to signed valueswire underflow = exp < zeroXp - 1; // value less than 1/2wire [7:0] shamt = (op ? MSB : FPWID) - (exp - zeroXp); // exp - zeroXp will be <= MSBwire [MSB+1:0] o1 = {man,{EMSB+1{1'b0}},1'b0} >> shamt; // keep an extra bit for roundingwire [MSB:0] o2 = o1[MSB+1:1] + o1[0]; // round upreg [MSB:0] o3;always @(posedge clk)if (ce) beginif (underflow|iz)o3 <= 0;else if (overflow)o3 <= maxInt;// value between 1/2 and 1 - round upelse if (exp==zeroXp-1)o3 <= 1;// value > 1elseo3 <= o2;endassign o = (op & sgn) ? -o3 : o3; // adjust output for correct signed valueendmodule
