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-09-29 23:30:25 +00:00
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-12-23 08:18:39 +00:00
input logic [ 2 : 0 ] Funct3E ,
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-11-06 21:53:48 +00:00
output logic [ `NE + 1 : 0 ] QeM ,
2022-09-14 17:26:56 +00:00
output logic [ `DIVb + 3 : 0 ] X ,
2022-12-22 05:44:55 +00:00
output logic [ `DIVb - 1 : 0 ] DPreproc ,
output logic [ `XLEN - 1 : 0 ] ForwardedSrcAM
2022-07-07 23:01:33 +00:00
) ;
2022-12-10 21:56:35 +00:00
logic [ `DIVb - 1 : 0 ] XPreproc ;
logic [ `DIVb : 0 ] SqrtX ;
2022-10-26 16:18:05 +00:00
logic [ `DIVb + 3 : 0 ] DivX ;
2022-12-10 21:56:35 +00:00
logic [ `NE + 1 : 0 ] QeE ;
2022-10-26 16:18:05 +00:00
// Intdiv signals
2022-12-10 21:56:35 +00:00
logic [ `DIVb - 1 : 0 ] IFNormLenX , IFNormLenD ;
2022-10-26 16:18:05 +00:00
logic [ `XLEN - 1 : 0 ] PosA , PosB ;
2022-12-27 05:03:56 +00:00
logic AsE , BsE , ALTBE , NegQuotE ;
2022-12-27 07:18:28 +00:00
logic [ `XLEN - 1 : 0 ] A64 , B64 , A64Src ;
2022-12-22 00:43:27 +00:00
logic [ `DIVBLEN : 0 ] mE ;
2022-11-06 22:08:18 +00:00
logic [ `DIVBLEN : 0 ] ZeroDiff , IntBits , RightShiftX ;
2022-12-10 21:56:35 +00:00
logic [ `DIVBLEN : 0 ] pPlusr , pPrCeil , p , ell ;
2022-12-22 02:22:01 +00:00
logic [ `LOGRK : 0 ] pPrTrunc ;
2022-12-19 04:02:40 +00:00
logic [ `DIVb + 3 : 0 ] PreShiftX ;
logic NumZeroE ;
2022-07-07 23:01:33 +00:00
// ***can probably merge X LZC with conversion
// cout the number of leading zeros
2022-11-06 21:53:48 +00:00
2022-12-27 14:35:17 +00:00
// *** W64 muxes conditional on RV64
2022-12-28 03:57:10 +00:00
// *** why !FUnct3E
2022-12-27 07:18:28 +00:00
assign AsE = ~ Funct3E [ 0 ] & ( W64E ? ForwardedSrcAE [ 31 ] : ForwardedSrcAE [ `XLEN - 1 ] ) ;
assign BsE = ~ Funct3E [ 0 ] & ( W64E ? ForwardedSrcBE [ 31 ] : ForwardedSrcBE [ `XLEN - 1 ] ) ;
2022-12-24 06:46:52 +00:00
assign A64 = W64E ? { { ( `XLEN - 32 ) { AsE } } , ForwardedSrcAE [ 31 : 0 ] } : ForwardedSrcAE ;
assign B64 = W64E ? { { ( `XLEN - 32 ) { BsE } } , ForwardedSrcBE [ 31 : 0 ] } : ForwardedSrcBE ;
2022-12-27 07:18:28 +00:00
assign A64Src = W64E ? { { ( `XLEN - 32 ) { ForwardedSrcAE [ 31 ] } } , ForwardedSrcAE [ 31 : 0 ] } : ForwardedSrcAE ;
2022-11-06 23:09:09 +00:00
2022-12-27 05:03:56 +00:00
assign NegQuotE = ( AsE ^ BsE ) & MDUE ;
2022-11-06 21:53:48 +00:00
2022-12-24 06:46:52 +00:00
assign PosA = AsE ? - A64 : A64 ;
assign PosB = BsE ? - B64 : B64 ;
2022-12-27 07:18:28 +00:00
assign AZeroE = W64E ? ~ ( | ForwardedSrcAE [ 31 : 0 ] ) : ~ ( | ForwardedSrcAE ) ;
assign BZeroE = W64E ? ~ ( | ForwardedSrcBE [ 31 : 0 ] ) : ~ ( | ForwardedSrcBE ) ;
2022-11-06 21:53:48 +00:00
2022-12-10 21:56:35 +00:00
assign IFNormLenX = MDUE ? { PosA , { ( `DIVb - `XLEN ) { 1 'b0 } } } : { Xm , { ( `DIVb - `NF - 1 ) { 1 'b0 } } } ;
assign IFNormLenD = MDUE ? { PosB , { ( `DIVb - `XLEN ) { 1 'b0 } } } : { Ym , { ( `DIVb - `NF - 1 ) { 1 'b0 } } } ;
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-19 04:02:40 +00:00
assign XPreproc = IFNormLenX < < ( ell + { { `DIVBLEN { 1 'b0 } } , 1 'b1 } ) ; // had issue with (`DIVBLEN+1)'(~MDUE) so using this instead
2022-12-22 00:43:27 +00:00
assign DPreproc = IFNormLenD < < ( mE + { { `DIVBLEN { 1 'b0 } } , 1 'b1 } ) ; // replaced ~MDUE with 1 bc we always want that extra left shift
2022-07-07 23:01:33 +00:00
2022-12-22 00:43:27 +00:00
assign ZeroDiff = mE - ell ;
2022-12-02 20:31:08 +00:00
assign ALTBE = ZeroDiff [ `DIVBLEN ] ; // A less than B
assign p = ALTBE ? '0 : ZeroDiff ;
2022-11-06 21:53:48 +00:00
2022-12-22 02:22:01 +00:00
/* verilator lint_off WIDTH */
2022-11-06 22:31:48 +00:00
assign pPlusr = ( `DIVBLEN ) ' ( `LOGR ) + p ;
2022-12-22 02:22:01 +00:00
assign pPrTrunc = pPlusr % `RK ;
//assign pPrTrunc = (`LOGRK == 0) ? 0 : pPlusr[`LOGRK-1:0];
2022-12-10 21:56:35 +00:00
assign pPrCeil = ( pPlusr > > `LOGRK ) + { { `DIVBLEN { 1 'b0 } } , | ( pPrTrunc ) } ;
2022-12-22 02:22:01 +00:00
assign nE = ( pPrCeil * ( `DIVBLEN + 1 ) ' ( `DIVCOPIES ) ) - { { ( `DIVBLEN ) { 1 'b0 } } , 1 'b1 } ;
assign IntBits = ( `DIVBLEN ) ' ( `LOGR ) + p - { { ( `DIVBLEN ) { 1 'b0 } } , 1 'b1 } ;
assign RightShiftX = ( ( `DIVBLEN ) ' ( `RK ) - 1 ) - ( IntBits % `RK ) ;
//assign RightShiftX = (`LOGRK == 0) ? 0 : ((`DIVBLEN)'(`RK) - 1) - {{(`DIVBLEN - `RK){1'b0}}, IntBits[`LOGRK-1:0]};
/* verilator lint_on WIDTH */
2022-11-06 21:53:48 +00:00
2022-12-19 04:02:40 +00:00
assign NumZeroE = MDUE ? AZeroE : XZeroE ;
assign SqrtX = ( Xe [ 0 ] ^ ell [ 0 ] ) ? { 1 'b0 , ~ NumZeroE , XPreproc [ `DIVb - 1 : 1 ] } : { ~ NumZeroE , XPreproc } ; // Bottom bit of XPreproc is always zero because DIVb is larger than XLEN and NF
assign DivX = { 3 'b000 , ~ NumZeroE , XPreproc } ;
2022-09-21 11:55:43 +00:00
2022-10-25 17:48:43 +00:00
// *** explain why X is shifted between radices (initial assignment of WS=RX)
2022-12-10 21:56:35 +00:00
if ( `RADIX = = 2 ) assign PreShiftX = Sqrt ? { 3 'b111 , SqrtX } : DivX ;
else assign PreShiftX = Sqrt ? { 2 'b11 , SqrtX , 1 'b0 } : DivX ;
2022-12-02 20:31:08 +00:00
assign X = MDUE ? DivX > > RightShiftX : PreShiftX ;
2022-07-07 23:01:33 +00:00
2022-12-26 16:45:43 +00:00
fdivsqrtexpcalc expcalc ( . Fmt , . Xe , . Ye , . Sqrt , . XZeroE , . ell , . m ( mE ) , . Qe ( QeE ) ) ;
2022-07-12 01:30:21 +00:00
// radix 2 radix 4
// 1 copies DIVLEN+2 DIVLEN+2/2
// 2 copies DIVLEN+2/2 DIVLEN+2/2*2
// 4 copies DIVLEN+2/4 DIVLEN+2/2*4
// 8 copies DIVLEN+2/8 DIVLEN+2/2*8
// DIVRESLEN = DIVLEN or DIVLEN+2
// r = 1 or 2
// DIVRESLEN/(r*`DIVCOPIES)
2022-12-02 20:31:08 +00:00
2022-12-27 05:03:56 +00:00
flopen # ( 1 ) negquotreg ( clk , IFDivStartE , NegQuotE , NegQuotM ) ;
2022-12-02 20:31:08 +00:00
flopen # ( 1 ) altbreg ( clk , IFDivStartE , ALTBE , ALTBM ) ;
2022-12-19 04:02:40 +00:00
flopen # ( 1 ) azeroreg ( clk , IFDivStartE , AZeroE , AZeroM ) ;
flopen # ( 1 ) bzeroreg ( clk , IFDivStartE , BZeroE , BZeroM ) ;
2022-12-24 06:46:52 +00:00
flopen # ( 1 ) asignreg ( clk , IFDivStartE , AsE , AsM ) ;
flopen # ( 1 ) mdureg ( clk , IFDivStartE , MDUE , MDUM ) ;
2022-12-27 07:18:28 +00:00
flopen # ( 1 ) w64reg ( clk , IFDivStartE , W64E , W64M ) ;
2022-12-22 00:43:27 +00:00
flopen # ( `DIVBLEN + 1 ) nreg ( clk , IFDivStartE , nE , nM ) ;
flopen # ( `DIVBLEN + 1 ) mreg ( clk , IFDivStartE , mE , mM ) ;
2022-12-27 07:18:28 +00:00
flopen # ( `NE + 2 ) expreg ( clk , IFDivStartE , QeE , QeM ) ;
flopen # ( `XLEN ) srcareg ( clk , IFDivStartE , A64Src , ForwardedSrcAM ) ;
2022-12-26 16:45:43 +00:00
2022-07-12 01:30:21 +00:00
2022-07-21 19:38:06 +00:00
endmodule