1 |
60 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2022 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// df128Toi.sv
|
9 |
|
|
// - convert decimal floating point to integer
|
10 |
|
|
//
|
11 |
|
|
// BSD 3-Clause License
|
12 |
|
|
// Redistribution and use in source and binary forms, with or without
|
13 |
|
|
// modification, are permitted provided that the following conditions are met:
|
14 |
|
|
//
|
15 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
16 |
|
|
// list of conditions and the following disclaimer.
|
17 |
|
|
//
|
18 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
19 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
20 |
|
|
// and/or other materials provided with the distribution.
|
21 |
|
|
//
|
22 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
23 |
|
|
// contributors may be used to endorse or promote products derived from
|
24 |
|
|
// this software without specific prior written permission.
|
25 |
|
|
//
|
26 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
27 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
28 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
29 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
30 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
31 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
32 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
33 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
34 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
35 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
36 |
|
|
//
|
37 |
|
|
// ============================================================================
|
38 |
|
|
|
39 |
|
|
import DFPPkg::*;
|
40 |
|
|
|
41 |
|
|
module df128Toi (rst, clk, ce, ld, op, i, o, overflow, done);
|
42 |
|
|
input rst;
|
43 |
|
|
input clk;
|
44 |
|
|
input ce;
|
45 |
|
|
input ld;
|
46 |
|
|
input op; // 1 = signed, 0 = unsigned
|
47 |
|
|
input [127:0] i; // float input
|
48 |
|
|
output [127:0] o; // integer output
|
49 |
|
|
output overflow;
|
50 |
|
|
output done;
|
51 |
|
|
|
52 |
|
|
wire done1;
|
53 |
|
|
reg done2;
|
54 |
|
|
assign done = done1 & done2;
|
55 |
|
|
|
56 |
|
|
wire [127:0] sig;
|
57 |
|
|
|
58 |
|
|
DFP128U ui;
|
59 |
|
|
DFPUnpack128 uunpk1 (i, ui);
|
60 |
|
|
|
61 |
|
|
wire [127:0] maxInt = op ? {1'd0,{127{1'b1}}} : {128{1'b1}}; // maximum integer value
|
62 |
|
|
wire [13:0] zeroXp = {1'd0,{13{1'b1}}};
|
63 |
|
|
|
64 |
|
|
reg sgn; // sign
|
65 |
|
|
always @(posedge clk)
|
66 |
|
|
if (ce) sgn = ui.sign;
|
67 |
|
|
wire [13:0] exp = ui.exp; // exponent
|
68 |
|
|
|
69 |
|
|
wire iz = i[126:0]==0; // zero value (special)
|
70 |
|
|
|
71 |
|
|
assign overflow = (exp - zeroXp) > 32; // lots of numbers are too big - don't forget one less bit is available due to signed values
|
72 |
|
|
wire underflow = exp < zeroXp - 2'd1; // value less than 1/2
|
73 |
|
|
|
74 |
|
|
wire [7:0] shamt = 8'd172 - {(exp - zeroXp),2'd0}; // exp - zeroXp will be <= MSB
|
75 |
|
|
|
76 |
|
|
wire [176:0] o1 = {ui.sig,41'b0} >> shamt; // keep an extra bit for rounding
|
77 |
|
|
wire [127:0] o2; // round up
|
78 |
|
|
reg [127:0] o3;
|
79 |
|
|
|
80 |
|
|
DDBCDToBin ub2b1
|
81 |
|
|
(
|
82 |
|
|
.rst(rst),
|
83 |
|
|
.clk(clk),
|
84 |
|
|
.ld(ld),
|
85 |
|
|
.bcd({o1[172:1]+o1[0]}),
|
86 |
|
|
.bin(o2),
|
87 |
|
|
.done(done1)
|
88 |
|
|
);
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
always @(posedge clk)
|
92 |
|
|
if (ce) begin
|
93 |
|
|
if (underflow|iz)
|
94 |
|
|
o3 <= 0;
|
95 |
|
|
else if (overflow)
|
96 |
|
|
o3 <= maxInt;
|
97 |
|
|
// value between 1/2 and 1 - round up
|
98 |
|
|
else if (exp==zeroXp-1)
|
99 |
|
|
o3 <= 128'd1;
|
100 |
|
|
// value > 1
|
101 |
|
|
else
|
102 |
|
|
o3 <= o2;
|
103 |
|
|
end
|
104 |
|
|
always @(posedge clk)
|
105 |
|
|
if (ce) done2 <= done1;
|
106 |
|
|
|
107 |
|
|
assign o = (op & sgn) ? -o3 : o3; // adjust output for correct signed value
|
108 |
|
|
|
109 |
|
|
endmodule
|