// ============================================================================
|
// ============================================================================
|
// __
|
// __
|
// \\__/ o\ (C) 2022 Robert Finch, Waterloo
|
// \\__/ o\ (C) 2022 Robert Finch, Waterloo
|
// \ __ / All rights reserved.
|
// \ __ / All rights reserved.
|
// \/_// robfinch@finitron.ca
|
// \/_// robfinch@finitron.ca
|
// ||
|
// ||
|
//
|
//
|
// df128Toi.sv
|
// df128Toi.sv
|
// - convert decimal floating point to integer
|
// - convert decimal floating point to integer
|
//
|
//
|
// BSD 3-Clause License
|
// BSD 3-Clause License
|
// Redistribution and use in source and binary forms, with or without
|
// Redistribution and use in source and binary forms, with or without
|
// modification, are permitted provided that the following conditions are met:
|
// modification, are permitted provided that the following conditions are met:
|
//
|
//
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
// list of conditions and the following disclaimer.
|
// list of conditions and the following disclaimer.
|
//
|
//
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
// this list of conditions and the following disclaimer in the documentation
|
// this list of conditions and the following disclaimer in the documentation
|
// and/or other materials provided with the distribution.
|
// and/or other materials provided with the distribution.
|
//
|
//
|
// 3. Neither the name of the copyright holder nor the names of its
|
// 3. Neither the name of the copyright holder nor the names of its
|
// contributors may be used to endorse or promote products derived from
|
// contributors may be used to endorse or promote products derived from
|
// this software without specific prior written permission.
|
// this software without specific prior written permission.
|
//
|
//
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
//
|
//
|
// ============================================================================
|
// ============================================================================
|
|
|
import DFPPkg::*;
|
import DFPPkg::*;
|
|
|
module df128Toi (rst, clk, ce, ld, op, i, o, overflow, done);
|
module df128Toi (rst, clk, ce, ld, op, i, o, overflow, done);
|
input rst;
|
input rst;
|
input clk;
|
input clk;
|
input ce;
|
input ce;
|
input ld;
|
input ld;
|
input op; // 1 = signed, 0 = unsigned
|
input op; // 1 = signed, 0 = unsigned
|
input [127:0] i; // float input
|
input [127:0] i; // float input
|
output [127:0] o; // integer output
|
output [127:0] o; // integer output
|
output overflow;
|
output overflow;
|
output done;
|
output done;
|
|
|
wire done1;
|
wire done1;
|
reg done2;
|
reg done2;
|
assign done = done1 & done2;
|
assign done = done1 & done2;
|
|
|
wire [127:0] sig;
|
wire [127:0] sig;
|
|
|
DFP128U ui;
|
DFP128U ui;
|
DFPUnpack128 uunpk1 (i, ui);
|
DFPUnpack128 uunpk1 (i, ui);
|
|
|
wire [127:0] maxInt = op ? {1'd0,{127{1'b1}}} : {128{1'b1}}; // maximum integer value
|
wire [127:0] maxInt = op ? {1'd0,{127{1'b1}}} : {128{1'b1}}; // maximum integer value
|
wire [13:0] zeroXp = {1'd0,{13{1'b1}}};
|
wire [13:0] zeroXp = 14'h17FF;
|
|
|
reg sgn; // sign
|
reg sgn; // sign
|
always @(posedge clk)
|
always @(posedge clk)
|
if (ce) sgn = ui.sign;
|
if (ce) sgn = ui.sign;
|
wire [13:0] exp = ui.exp; // exponent
|
wire [13:0] exp = ui.exp; // exponent
|
|
|
wire iz = i[126:0]==0; // zero value (special)
|
wire iz = i[126:0]==0; // zero value (special)
|
|
|
wire [14:0] ovx = exp - zeroXp;
|
wire [14:0] ovx = exp - zeroXp;
|
assign overflow = ovx > 32 && !ovx[14]; // lots of numbers are too big - don't forget one less bit is available due to signed values
|
assign overflow = ovx > 32 && !ovx[14]; // lots of numbers are too big - don't forget one less bit is available due to signed values
|
wire underflow = exp < zeroXp - 2'd1; // value less than 1/2
|
wire underflow = exp < zeroXp - 2'd1; // value less than 1/2
|
|
|
wire [7:0] shamt = 8'd172 - {(exp - zeroXp),2'd0}; // exp - zeroXp will be <= MSB
|
wire [7:0] shamt = 8'd172 - {(exp - zeroXp),2'd0}; // exp - zeroXp will be <= MSB
|
|
|
wire [176:0] o1 = {ui.sig,41'b0} >> shamt; // keep an extra bit for rounding
|
wire [176:0] o1 = {ui.sig,41'b0} >> shamt; // keep an extra bit for rounding
|
wire [127:0] o2; // round up
|
wire [127:0] o2; // round up
|
reg [127:0] o3;
|
reg [127:0] o3;
|
|
|
DDBCDToBin ub2b1
|
DDBCDToBin ub2b1
|
(
|
(
|
.rst(rst),
|
.rst(rst),
|
.clk(clk),
|
.clk(clk),
|
.ld(ld),
|
.ld(ld),
|
.bcd({o1[172:1]+o1[0]}),
|
.bcd({o1[172:1]+o1[0]}),
|
.bin(o2),
|
.bin(o2),
|
.done(done1)
|
.done(done1)
|
);
|
);
|
|
|
|
|
always @(posedge clk)
|
always @(posedge clk)
|
if (ce) begin
|
if (ce) begin
|
if (underflow|iz)
|
if (underflow|iz)
|
o3 <= 0;
|
o3 <= 0;
|
else if (overflow)
|
else if (overflow)
|
o3 <= maxInt;
|
o3 <= maxInt;
|
// value between 1/2 and 1 - round up
|
// value between 1/2 and 1 - round up
|
else if (exp==zeroXp-1)
|
else if (exp==zeroXp-1)
|
o3 <= 128'd1;
|
o3 <= 128'd1;
|
// value > 1
|
// value > 1
|
else
|
else
|
o3 <= o2;
|
o3 <= o2;
|
end
|
end
|
always @(posedge clk)
|
always @(posedge clk)
|
if (ce) done2 <= done1;
|
if (ce) done2 <= done1;
|
|
|
assign o = (op & sgn) ? -o3 : o3; // adjust output for correct signed value
|
assign o = (op & sgn) ? -o3 : o3; // adjust output for correct signed value
|
|
|
endmodule
|
endmodule
|
|
|