| 1 |
78 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2019-2022 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// DFPScaleb96.sv
|
| 9 |
|
|
// - floating point Scaleb()
|
| 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 DFPScaleb96(clk, ce, a, b, o);
|
| 42 |
|
|
localparam N=25;
|
| 43 |
|
|
input clk;
|
| 44 |
|
|
input ce;
|
| 45 |
|
|
input DFP96 a;
|
| 46 |
|
|
input [31:0] b;
|
| 47 |
|
|
output DFP96 o;
|
| 48 |
|
|
|
| 49 |
|
|
wire [4:0] cmp_o;
|
| 50 |
|
|
wire nana, nanb;
|
| 51 |
|
|
wire xza, mza;
|
| 52 |
|
|
|
| 53 |
|
|
wire [11:0] infXp = 12'hBFF; // infinite / NaN - all ones
|
| 54 |
|
|
wire [11:0] bias = 12'h5FF;
|
| 55 |
|
|
wire xinfa;
|
| 56 |
|
|
wire anan;
|
| 57 |
|
|
reg anan1;
|
| 58 |
|
|
wire sa;
|
| 59 |
|
|
reg sa1, sa2;
|
| 60 |
|
|
wire [N*4-1:0] ma;
|
| 61 |
|
|
reg [13:0] xa1a, xa1b, xa2;
|
| 62 |
|
|
reg [N*4-1:0] ma1, ma2;
|
| 63 |
|
|
wire bs = b[31];
|
| 64 |
|
|
reg bs1;
|
| 65 |
|
|
|
| 66 |
|
|
DFP96U au, bu;
|
| 67 |
|
|
DFPUnpack96 u01 (a, au);
|
| 68 |
|
|
|
| 69 |
|
|
// ----------------------------------------------------------------------------
|
| 70 |
|
|
// Clock cycle 1
|
| 71 |
|
|
// ----------------------------------------------------------------------------
|
| 72 |
|
|
always @(posedge clk)
|
| 73 |
|
|
if (ce) xa1a <= au.exp;
|
| 74 |
|
|
always @(posedge clk)
|
| 75 |
79 |
robfinch |
if (ce) xa1b <= bs ? au.exp - (~b[11:0] + 2'd1) : au.exp + b;
|
| 76 |
78 |
robfinch |
always @(posedge clk)
|
| 77 |
|
|
if (ce) bs1 <= bs;
|
| 78 |
|
|
always @(posedge clk)
|
| 79 |
|
|
if (ce) anan1 <= au.nan;
|
| 80 |
|
|
always @(posedge clk)
|
| 81 |
|
|
if (ce) sa1 <= au.sign;
|
| 82 |
|
|
always @(posedge clk)
|
| 83 |
|
|
if (ce) ma1 <= au.sig;
|
| 84 |
|
|
|
| 85 |
|
|
// ----------------------------------------------------------------------------
|
| 86 |
|
|
// Clock cycle 2
|
| 87 |
|
|
// ----------------------------------------------------------------------------
|
| 88 |
|
|
reg nan2;
|
| 89 |
|
|
reg qnan2;
|
| 90 |
|
|
reg snan2;
|
| 91 |
|
|
reg infinity2;
|
| 92 |
|
|
|
| 93 |
|
|
always @(posedge clk)
|
| 94 |
|
|
if (ce) sa2 <= sa1;
|
| 95 |
|
|
always @(posedge clk)
|
| 96 |
|
|
if (ce) nan2 <= anan1;
|
| 97 |
|
|
always @(posedge clk)
|
| 98 |
|
|
if (ce) qnan2 <= anan1 && ma1[N*4-1:N*4-4]==4'h1;
|
| 99 |
|
|
always @(posedge clk)
|
| 100 |
|
|
if (ce) snan2 <= anan1 && ma1[N*4-1:N*4-4]==4'h0;
|
| 101 |
|
|
always @(posedge clk)
|
| 102 |
|
|
if (ce) begin
|
| 103 |
79 |
robfinch |
infinity2 <= 1'b0;
|
| 104 |
78 |
robfinch |
if (anan1) begin
|
| 105 |
|
|
xa2 <= xa1a;
|
| 106 |
|
|
ma2 <= ma1;
|
| 107 |
|
|
end
|
| 108 |
|
|
// Underflow? -> limit exponent to zero
|
| 109 |
|
|
else if (bs1 & xa1b[13]) begin
|
| 110 |
|
|
xa2 <= 'd0;
|
| 111 |
|
|
ma2 <= ma1;
|
| 112 |
|
|
end
|
| 113 |
|
|
// overflow ? -> set value to infinity
|
| 114 |
|
|
else if (~bs1 & xa1b[12]) begin
|
| 115 |
|
|
xa2 <= infXp;
|
| 116 |
|
|
ma2 <= 'd0;
|
| 117 |
|
|
infinity2 <= 1'b1;
|
| 118 |
|
|
end
|
| 119 |
|
|
else begin
|
| 120 |
|
|
xa2 <= xa1b;
|
| 121 |
|
|
ma2 <= ma1;
|
| 122 |
|
|
end
|
| 123 |
|
|
end
|
| 124 |
|
|
|
| 125 |
|
|
assign bu.nan = nan2;
|
| 126 |
|
|
assign bu.snan = snan2;
|
| 127 |
|
|
assign bu.qnan = qnan2;
|
| 128 |
|
|
assign bu.infinity = infinity2;
|
| 129 |
|
|
assign bu.sign = sa2;
|
| 130 |
|
|
assign bu.exp = xa2;
|
| 131 |
|
|
assign bu.sig = ma2;
|
| 132 |
|
|
|
| 133 |
|
|
DFPPack96 u02 (bu, o);
|
| 134 |
|
|
|
| 135 |
|
|
endmodule
|