2022-07-07 23:01:33 +00:00
|
|
|
///////////////////////////////////////////
|
2022-08-29 11:04:05 +00:00
|
|
|
// fdivsqrtpreproc.sv
|
2022-07-07 23:01:33 +00:00
|
|
|
//
|
2022-09-19 21:26:32 +00:00
|
|
|
// Written: David_Harris@hmc.edu, me@KatherineParry.com, cturek@hmc.edu
|
2022-07-07 23:01:33 +00:00
|
|
|
// Modified:13 January 2022
|
|
|
|
//
|
|
|
|
// Purpose: Combined Divide and Square Root Floating Point and Integer Unit
|
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// MIT LICENSE
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
|
|
// software and associated documentation files (the "Software"), to deal in the Software
|
|
|
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
|
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
|
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or
|
|
|
|
// substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
|
|
// OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2022-08-29 11:04:05 +00:00
|
|
|
module fdivsqrtpreproc (
|
2022-07-21 19:38:06 +00:00
|
|
|
input logic clk,
|
2022-12-02 19:30:49 +00:00
|
|
|
input logic IFDivStartE,
|
2022-07-15 20:16:59 +00:00
|
|
|
input logic [`NF:0] Xm, Ym,
|
2022-07-21 19:38:06 +00:00
|
|
|
input logic [`NE-1:0] Xe, Ye,
|
|
|
|
input logic [`FMTBITS-1:0] Fmt,
|
|
|
|
input logic Sqrt,
|
2022-12-19 04:02:40 +00:00
|
|
|
input logic XZeroE,
|
2022-12-23 08:18:39 +00:00
|
|
|
input logic [2:0] Funct3E,
|
2022-12-30 20:03:10 +00:00
|
|
|
output logic [`NE+1:0] QeM,
|
|
|
|
output logic [`DIVb+3:0] X,
|
|
|
|
output logic [`DIVb-1:0] DPreproc,
|
|
|
|
// Int-specific
|
|
|
|
input logic [`XLEN-1:0] ForwardedSrcAE, ForwardedSrcBE, // *** these are the src outputs before the mux choosing between them and PCE to put in srcA/B
|
2022-09-29 23:30:25 +00:00
|
|
|
input logic MDUE, W64E,
|
2022-12-22 00:43:27 +00:00
|
|
|
output logic [`DIVBLEN:0] nE, nM, mM,
|
2022-12-27 07:18:28 +00:00
|
|
|
output logic NegQuotM, ALTBM, MDUM, W64M,
|
2022-12-24 06:46:52 +00:00
|
|
|
output logic AsM, AZeroM, BZeroM, AZeroE, BZeroE,
|
2022-12-29 16:02:44 +00:00
|
|
|
output logic [`XLEN-1:0] AM
|
2022-07-07 23:01:33 +00:00
|
|
|
);
|
2022-12-10 21:56:35 +00:00
|
|
|
|
|
|
|
logic [`DIVb-1:0] XPreproc;
|
2022-12-30 14:55:20 +00:00
|
|
|
logic [`DIVb:0] PreSqrtX;
|
2022-12-31 14:19:15 +00:00
|
|
|
logic [`DIVb+3:0] DivX, DivXShifted, SqrtX, PreShiftX; // Variations of dividend, to be muxed
|
2022-12-30 20:03:10 +00:00
|
|
|
logic [`NE+1:0] QeE; // Quotient Exponent (FP only)
|
|
|
|
logic [`DIVb-1:0] IFNormLenX, IFNormLenD; // Correctly-sized inputs for iterator
|
|
|
|
logic [`DIVBLEN:0] mE, ell; // Leading zeros of inputs
|
2022-12-30 22:13:33 +00:00
|
|
|
logic NumerZeroE; // Numerator is zero (X or A)
|
2022-07-07 23:01:33 +00:00
|
|
|
|
2022-12-30 22:31:23 +00:00
|
|
|
if (`IDIV_ON_FPU) begin // Int Supported
|
2022-12-30 22:13:33 +00:00
|
|
|
logic signedDiv, NegQuotE;
|
|
|
|
logic AsBit, BsBit, AsE, BsE, ALTBE;
|
|
|
|
logic [`XLEN-1:0] AE, BE, PosA, PosB;
|
2022-12-31 14:19:15 +00:00
|
|
|
logic [`DIVBLEN:0] ZeroDiff, p;
|
2022-12-29 15:36:26 +00:00
|
|
|
|
2022-12-29 16:02:44 +00:00
|
|
|
// Extract inputs, signs, zero, depending on W64 mode if applicable
|
|
|
|
assign signedDiv = ~Funct3E[0];
|
2022-12-31 02:41:40 +00:00
|
|
|
assign NegQuotE = AsE ^ BsE; // Quotient is negative
|
|
|
|
|
|
|
|
// Source handling
|
2022-12-29 15:36:26 +00:00
|
|
|
if (`XLEN==64) begin // 64-bit, supports W64
|
2022-12-30 22:13:33 +00:00
|
|
|
mux2 #(1) azeromux(~(|ForwardedSrcAE), ~(|ForwardedSrcAE[31:0]), W64E, AZeroE);
|
|
|
|
mux2 #(1) bzeromux(~(|ForwardedSrcBE), ~(|ForwardedSrcBE[31:0]), W64E, BZeroE);
|
|
|
|
mux2 #(1) abitmux(ForwardedSrcAE[63], ForwardedSrcAE[31], W64E, AsBit);
|
|
|
|
mux2 #(1) bbitmux(ForwardedSrcBE[63], ForwardedSrcBE[31], W64E, BsBit);
|
|
|
|
mux2 #(64) amux(ForwardedSrcAE, {{(`XLEN-32){AsE}}, ForwardedSrcAE[31:0]}, W64E, AE);
|
|
|
|
mux2 #(64) bmux(ForwardedSrcBE, {{(`XLEN-32){BsE}}, ForwardedSrcBE[31:0]}, W64E, BE);
|
|
|
|
assign AsE = signedDiv & AsBit;
|
|
|
|
assign BsE = signedDiv & BsBit;
|
2022-12-29 15:36:26 +00:00
|
|
|
end else begin // 32 bits only
|
2022-12-30 22:13:33 +00:00
|
|
|
assign AsE = signedDiv & ForwardedSrcAE[31];
|
|
|
|
assign BsE = signedDiv & ForwardedSrcBE[31];
|
2022-12-29 16:02:44 +00:00
|
|
|
assign AE = ForwardedSrcAE;
|
|
|
|
assign BE = ForwardedSrcBE;
|
2022-12-29 15:36:26 +00:00
|
|
|
assign AZeroE = ~(|ForwardedSrcAE);
|
|
|
|
assign BZeroE = ~(|ForwardedSrcBE);
|
|
|
|
end
|
2022-12-28 05:53:00 +00:00
|
|
|
|
2022-12-30 22:13:33 +00:00
|
|
|
// Force integer inputs to be postiive
|
|
|
|
mux2 #(`XLEN) posamux(AE, -AE, AsE, PosA);
|
|
|
|
mux2 #(`XLEN) posbmux(BE, -BE, BsE, PosB);
|
2022-12-29 15:36:26 +00:00
|
|
|
|
2022-12-30 22:13:33 +00:00
|
|
|
// Select integer or floating point inputs
|
|
|
|
mux2 #(`DIVb) ifxmux({Xm, {(`DIVb-`NF-1){1'b0}}}, {PosA, {(`DIVb-`XLEN){1'b0}}}, MDUE, IFNormLenX);
|
|
|
|
mux2 #(`DIVb) ifdmux({Ym, {(`DIVb-`NF-1){1'b0}}}, {PosB, {(`DIVb-`XLEN){1'b0}}}, MDUE, IFNormLenD);
|
2022-12-29 16:02:44 +00:00
|
|
|
|
2022-12-30 22:13:33 +00:00
|
|
|
// calculate number of fractional bits p
|
|
|
|
assign ZeroDiff = mE - ell; // Difference in number of leading zeros
|
|
|
|
assign ALTBE = ZeroDiff[`DIVBLEN]; // A less than B?
|
|
|
|
mux2 #(`DIVBLEN+1) pmux(ZeroDiff, 0, ALTBE, p);
|
2022-12-28 05:53:00 +00:00
|
|
|
|
|
|
|
/* verilator lint_off WIDTH */
|
2022-12-30 20:03:10 +00:00
|
|
|
// calculate number of fractional digits nE and right shift amount RightShiftX to complete in discrete number of steps
|
2022-12-31 14:19:15 +00:00
|
|
|
|
|
|
|
if (`LOGRK > 0) begin // more than 1 bit per cycle
|
|
|
|
logic [`LOGRK-1:0] IntTrunc, RightShiftX;
|
|
|
|
logic [`DIVBLEN:0] TotalIntBits, IntSteps;
|
|
|
|
|
|
|
|
assign TotalIntBits = `LOGR + p; // Total number of result bits (r integer bits plus p fractional bits)
|
|
|
|
assign IntTrunc = TotalIntBits % `RK; // Truncation check for ceiling operator
|
|
|
|
assign IntSteps = (TotalIntBits >> `LOGRK) + |IntTrunc; // Number of steps for int div
|
|
|
|
assign nE = (IntSteps * `DIVCOPIES) - 1; // Fractional digits
|
|
|
|
assign RightShiftX = `RK - 1 - ((TotalIntBits - 1) % `RK); // Right shift amount
|
|
|
|
assign DivXShifted = DivX >> RightShiftX; // shift X to complete in nE steps
|
|
|
|
end else begin // radix 2 1 copy doesn't require shifting
|
2022-12-31 15:01:50 +00:00
|
|
|
assign nE = p;
|
2022-12-31 14:19:15 +00:00
|
|
|
assign DivXShifted = DivX;
|
|
|
|
end
|
2022-12-28 05:53:00 +00:00
|
|
|
/* verilator lint_on WIDTH */
|
|
|
|
|
2022-12-29 16:02:44 +00:00
|
|
|
// Selet integer or floating-point operands
|
2022-12-30 22:13:33 +00:00
|
|
|
mux2 #(1) numzmux(XZeroE, AZeroE, MDUE, NumerZeroE);
|
2022-12-31 14:19:15 +00:00
|
|
|
mux2 #(`DIVb+4) xmux(PreShiftX, DivXShifted, MDUE, X);
|
2022-12-29 15:36:26 +00:00
|
|
|
|
|
|
|
// pipeline registers
|
2022-12-30 20:03:10 +00:00
|
|
|
flopen #(1) mdureg(clk, IFDivStartE, MDUE, MDUM);
|
|
|
|
flopen #(1) w64reg(clk, IFDivStartE, W64E, W64M);
|
|
|
|
flopen #(1) altbreg(clk, IFDivStartE, ALTBE, ALTBM);
|
2022-12-29 15:36:26 +00:00
|
|
|
flopen #(1) negquotreg(clk, IFDivStartE, NegQuotE, NegQuotM);
|
2022-12-30 20:03:10 +00:00
|
|
|
flopen #(1) azeroreg(clk, IFDivStartE, AZeroE, AZeroM);
|
|
|
|
flopen #(1) bzeroreg(clk, IFDivStartE, BZeroE, BZeroM);
|
|
|
|
flopen #(1) asignreg(clk, IFDivStartE, AsE, AsM);
|
|
|
|
flopen #(`DIVBLEN+1) nreg(clk, IFDivStartE, nE, nM);
|
|
|
|
flopen #(`DIVBLEN+1) mreg(clk, IFDivStartE, mE, mM);
|
|
|
|
flopen #(`XLEN) srcareg(clk, IFDivStartE, AE, AM);
|
|
|
|
|
2022-12-30 22:31:23 +00:00
|
|
|
end else begin // Int not supported
|
2022-12-29 16:02:44 +00:00
|
|
|
assign IFNormLenX = {Xm, {(`DIVb-`NF-1){1'b0}}};
|
|
|
|
assign IFNormLenD = {Ym, {(`DIVb-`NF-1){1'b0}}};
|
2022-12-30 22:13:33 +00:00
|
|
|
assign NumerZeroE = XZeroE;
|
2022-12-28 05:53:00 +00:00
|
|
|
assign X = PreShiftX;
|
|
|
|
end
|
2022-11-06 21:53:48 +00:00
|
|
|
|
2022-12-29 16:02:44 +00:00
|
|
|
// count leading zeros for denorm FP and to normalize integer inputs
|
2022-12-10 21:56:35 +00:00
|
|
|
lzc #(`DIVb) lzcX (IFNormLenX, ell);
|
2022-12-22 00:43:27 +00:00
|
|
|
lzc #(`DIVb) lzcY (IFNormLenD, mE);
|
2022-07-07 23:01:33 +00:00
|
|
|
|
2022-12-29 16:02:44 +00:00
|
|
|
// Normalization shift
|
|
|
|
assign XPreproc = IFNormLenX << (ell + {{`DIVBLEN{1'b0}}, 1'b1});
|
|
|
|
assign DPreproc = IFNormLenD << (mE + {{`DIVBLEN{1'b0}}, 1'b1});
|
2022-07-07 23:01:33 +00:00
|
|
|
|
2022-12-30 22:13:33 +00:00
|
|
|
// append leading 1 (for nonzero inputs) and conditionally shift left by one to avoid sqrt(2)
|
|
|
|
mux2 #(`DIVb+1) sqrtxmux({~NumerZeroE, XPreproc}, {1'b0, ~NumerZeroE, XPreproc[`DIVb-1:1]}, (Xe[0]^ell[0]), PreSqrtX);
|
|
|
|
assign DivX = {3'b000, ~NumerZeroE, XPreproc};
|
2022-12-30 20:03:10 +00:00
|
|
|
|
|
|
|
// Sqrt is initialized on step one as R(X-1), so depends on Radix
|
2022-12-30 14:55:20 +00:00
|
|
|
if (`RADIX == 2) assign SqrtX = {3'b111, PreSqrtX};
|
|
|
|
else assign SqrtX = {2'b11, PreSqrtX, 1'b0};
|
2022-12-30 22:13:33 +00:00
|
|
|
mux2 #(`DIVb+4) prexmux(DivX, SqrtX, Sqrt, PreShiftX);
|
2022-12-30 14:55:20 +00:00
|
|
|
|
2022-12-29 16:02:44 +00:00
|
|
|
// Floating-point exponent
|
|
|
|
fdivsqrtexpcalc expcalc(.Fmt, .Xe, .Ye, .Sqrt, .XZero(XZeroE), .ell, .m(mE), .Qe(QeE));
|
2022-12-30 14:55:20 +00:00
|
|
|
flopen #(`NE+2) expreg(clk, IFDivStartE, QeE, QeM);
|
2022-07-21 19:38:06 +00:00
|
|
|
endmodule
|
|
|
|
|