OpenCores
URL https://opencores.org/ocsvn/fpu/fpu/trunk

Subversion Repositories fpu

[/] [fpu/] [trunk/] [verilog/] [pre_norm.v] - Diff between revs 3 and 11

Only display areas with differences | Details | Blame | View Log

Rev 3 Rev 11
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
////                                                             ////
////                                                             ////
////  Pre Normalize                                              ////
////  Pre Normalize                                              ////
////  Pre Normalization Unit for Add/Sub Operations              ////
////  Pre Normalization Unit for Add/Sub Operations              ////
////                                                             ////
////                                                             ////
////  Author: Rudolf Usselmann                                   ////
////  Author: Rudolf Usselmann                                   ////
////          rudi@asics.ws                                      ////
////          rudi@asics.ws                                      ////
////                                                             ////
////                                                             ////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
////                                                             ////
////                                                             ////
//// Copyright (C) 2000 Rudolf Usselmann                         ////
//// Copyright (C) 2000 Rudolf Usselmann                         ////
////                    rudi@asics.ws                            ////
////                    rudi@asics.ws                            ////
////                                                             ////
////                                                             ////
//// This source file may be used and distributed without        ////
//// This source file may be used and distributed without        ////
//// restriction provided that this copyright statement is not   ////
//// restriction provided that this copyright statement is not   ////
//// removed from the file and that any derivative work contains ////
//// removed from the file and that any derivative work contains ////
//// the original copyright notice and the associated disclaimer.////
//// the original copyright notice and the associated disclaimer.////
////                                                             ////
////                                                             ////
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
////                                                             ////
////                                                             ////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
 
 
`timescale 1ns / 100ps
`timescale 1ns / 100ps
 
 
 
 
module pre_norm(clk, rmode, add, opa, opb, opa_nan, opb_nan, fracta_out,
module pre_norm(clk, rmode, add, opa, opb, opa_nan, opb_nan, fracta_out,
                fractb_out, exp_dn_out, sign, nan_sign, result_zero_sign,
                fractb_out, exp_dn_out, sign, nan_sign, result_zero_sign,
                fasu_op);
                fasu_op);
input           clk;
input           clk;
input   [1:0]    rmode;
input   [1:0]    rmode;
input           add;
input           add;
input   [31:0]   opa, opb;
input   [31:0]   opa, opb;
input           opa_nan, opb_nan;
input           opa_nan, opb_nan;
output  [26:0]   fracta_out, fractb_out;
output  [26:0]   fracta_out, fractb_out;
output  [7:0]    exp_dn_out;
output  [7:0]    exp_dn_out;
output          sign;
output          sign;
output          nan_sign, result_zero_sign;
output          nan_sign, result_zero_sign;
output          fasu_op;                        // Operation Output
output          fasu_op;                        // Operation Output
 
 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
//
// Local Wires and registers
// Local Wires and registers
//
//
 
 
wire            signa, signb;           // alias to opX sign
wire            signa, signb;           // alias to opX sign
wire    [7:0]    expa, expb;             // alias to opX exponent
wire    [7:0]    expa, expb;             // alias to opX exponent
wire    [22:0]   fracta, fractb;         // alias to opX fraction
wire    [22:0]   fracta, fractb;         // alias to opX fraction
wire            expa_lt_expb;           // expa is larger than expb indicator
wire            expa_lt_expb;           // expa is larger than expb indicator
wire            fractb_lt_fracta;       // fractb is larger than fracta indicator
wire            fractb_lt_fracta;       // fractb is larger than fracta indicator
reg     [7:0]    exp_dn_out;             // de normalized exponent output
reg     [7:0]    exp_dn_out;             // de normalized exponent output
wire    [7:0]    exp_small, exp_large;
wire    [7:0]    exp_small, exp_large;
wire    [7:0]    exp_diff;               // Numeric difference of the two exponents
wire    [7:0]    exp_diff;               // Numeric difference of the two exponents
wire    [22:0]   adj_op;                 // Fraction adjustment: input
wire    [22:0]   adj_op;                 // Fraction adjustment: input
wire    [26:0]   adj_op_tmp;
wire    [26:0]   adj_op_tmp;
wire    [26:0]   adj_op_out;             // Fraction adjustment: output
wire    [26:0]   adj_op_out;             // Fraction adjustment: output
wire    [26:0]   fracta_n, fractb_n;     // Fraction selection after normalizing
wire    [26:0]   fracta_n, fractb_n;     // Fraction selection after normalizing
wire    [26:0]   fracta_s, fractb_s;     // Fraction Sorting out
wire    [26:0]   fracta_s, fractb_s;     // Fraction Sorting out
reg     [26:0]   fracta_out, fractb_out; // Fraction Output
reg     [26:0]   fracta_out, fractb_out; // Fraction Output
reg             sign, sign_d;           // Sign Output
reg             sign, sign_d;           // Sign Output
reg             add_d;                  // operation (add/sub)
reg             add_d;                  // operation (add/sub)
reg             fasu_op;                // operation (add/sub) register
reg             fasu_op;                // operation (add/sub) register
wire            expa_dn, expb_dn;
wire            expa_dn, expb_dn;
reg             sticky;
reg             sticky;
reg             result_zero_sign;
reg             result_zero_sign;
reg             add_r, signa_r, signb_r;
reg             add_r, signa_r, signb_r;
wire    [4:0]    exp_diff_sft;
wire    [4:0]    exp_diff_sft;
wire            exp_lt_27;
wire            exp_lt_27;
wire            op_dn;
wire            op_dn;
wire    [26:0]   adj_op_out_sft;
wire    [26:0]   adj_op_out_sft;
reg             fracta_lt_fractb, fracta_eq_fractb;
reg             fracta_lt_fractb, fracta_eq_fractb;
wire            nan_sign1;
wire            nan_sign1;
reg             nan_sign;
reg             nan_sign;
 
 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
//
// Aliases
// Aliases
//
//
 
 
assign  signa = opa[31];
assign  signa = opa[31];
assign  signb = opb[31];
assign  signb = opb[31];
assign   expa = opa[30:23];
assign   expa = opa[30:23];
assign   expb = opb[30:23];
assign   expb = opb[30:23];
assign fracta = opa[22:0];
assign fracta = opa[22:0];
assign fractb = opb[22:0];
assign fractb = opb[22:0];
 
 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
//
// Pre-Normalize exponents (and fractions)
// Pre-Normalize exponents (and fractions)
//
//
 
 
assign expa_lt_expb = expa > expb;              // expa is larger than expb
assign expa_lt_expb = expa > expb;              // expa is larger than expb
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Normalize
// Normalize
 
 
assign expa_dn = !(|expa);                      // opa denormalized
assign expa_dn = !(|expa);                      // opa denormalized
assign expb_dn = !(|expb);                      // opb denormalized
assign expb_dn = !(|expb);                      // opb denormalized
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Calculate the difference between the smaller and larger exponent
// Calculate the difference between the smaller and larger exponent
 
 
wire    [7:0]    exp_diff1, exp_diff1a, exp_diff2;
wire    [7:0]    exp_diff1, exp_diff1a, exp_diff2;
 
 
assign exp_small  = expa_lt_expb ? expb : expa;
assign exp_small  = expa_lt_expb ? expb : expa;
assign exp_large  = expa_lt_expb ? expa : expb;
assign exp_large  = expa_lt_expb ? expa : expb;
assign exp_diff1  = exp_large - exp_small;
assign exp_diff1  = exp_large - exp_small;
assign exp_diff1a = exp_diff1-1;
assign exp_diff1a = exp_diff1-1;
assign exp_diff2  = (expa_dn | expb_dn) ? exp_diff1a : exp_diff1;
assign exp_diff2  = (expa_dn | expb_dn) ? exp_diff1a : exp_diff1;
assign  exp_diff  = (expa_dn & expb_dn) ? 8'h0 : exp_diff2;
assign  exp_diff  = (expa_dn & expb_dn) ? 8'h0 : exp_diff2;
 
 
always @(posedge clk)   // If numbers are equal we should return zero
always @(posedge clk)   // If numbers are equal we should return zero
        exp_dn_out <= #1 (!add_d & expa==expb & fracta==fractb) ? 8'h0 : exp_large;
        exp_dn_out <= #1 (!add_d & expa==expb & fracta==fractb) ? 8'h0 : exp_large;
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Adjust the smaller fraction
// Adjust the smaller fraction
 
 
 
 
assign op_dn      = expa_lt_expb ? expb_dn : expa_dn;
assign op_dn      = expa_lt_expb ? expb_dn : expa_dn;
assign adj_op     = expa_lt_expb ? fractb : fracta;
assign adj_op     = expa_lt_expb ? fractb : fracta;
assign adj_op_tmp = { ~op_dn, adj_op, 3'b0 };   // recover hidden bit (op_dn) 
assign adj_op_tmp = { ~op_dn, adj_op, 3'b0 };   // recover hidden bit (op_dn) 
 
 
// adj_op_out is 27 bits wide, so can only be shifted 27 bits to the right
// adj_op_out is 27 bits wide, so can only be shifted 27 bits to the right
assign exp_lt_27        = exp_diff  > 8'd27;
assign exp_lt_27        = exp_diff  > 8'd27;
assign exp_diff_sft     = exp_lt_27 ? 5'd27 : exp_diff[4:0];
assign exp_diff_sft     = exp_lt_27 ? 5'd27 : exp_diff[4:0];
assign adj_op_out_sft   = adj_op_tmp >> exp_diff_sft;
assign adj_op_out_sft   = adj_op_tmp >> exp_diff_sft;
assign adj_op_out       = {adj_op_out_sft[26:1], adj_op_out_sft[0] | sticky };
assign adj_op_out       = {adj_op_out_sft[26:1], adj_op_out_sft[0] | sticky };
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Get truncated portion (sticky bit)
// Get truncated portion (sticky bit)
 
 
always @(exp_diff_sft or adj_op_tmp)
always @(exp_diff_sft or adj_op_tmp)
   case(exp_diff_sft)           // synopsys full_case parallel_case
   case(exp_diff_sft)           // synopsys full_case parallel_case
        00: sticky = 1'h0;
        00: sticky = 1'h0;
        01: sticky =  adj_op_tmp[0];
        01: sticky =  adj_op_tmp[0];
        02: sticky = |adj_op_tmp[01:0];
        02: sticky = |adj_op_tmp[01:0];
        03: sticky = |adj_op_tmp[02:0];
        03: sticky = |adj_op_tmp[02:0];
        04: sticky = |adj_op_tmp[03:0];
        04: sticky = |adj_op_tmp[03:0];
        05: sticky = |adj_op_tmp[04:0];
        05: sticky = |adj_op_tmp[04:0];
        06: sticky = |adj_op_tmp[05:0];
        06: sticky = |adj_op_tmp[05:0];
        07: sticky = |adj_op_tmp[06:0];
        07: sticky = |adj_op_tmp[06:0];
        08: sticky = |adj_op_tmp[07:0];
        08: sticky = |adj_op_tmp[07:0];
        09: sticky = |adj_op_tmp[08:0];
        09: sticky = |adj_op_tmp[08:0];
        10: sticky = |adj_op_tmp[09:0];
        10: sticky = |adj_op_tmp[09:0];
        11: sticky = |adj_op_tmp[10:0];
        11: sticky = |adj_op_tmp[10:0];
        12: sticky = |adj_op_tmp[11:0];
        12: sticky = |adj_op_tmp[11:0];
        13: sticky = |adj_op_tmp[12:0];
        13: sticky = |adj_op_tmp[12:0];
        14: sticky = |adj_op_tmp[13:0];
        14: sticky = |adj_op_tmp[13:0];
        15: sticky = |adj_op_tmp[14:0];
        15: sticky = |adj_op_tmp[14:0];
        16: sticky = |adj_op_tmp[15:0];
        16: sticky = |adj_op_tmp[15:0];
        17: sticky = |adj_op_tmp[16:0];
        17: sticky = |adj_op_tmp[16:0];
        18: sticky = |adj_op_tmp[17:0];
        18: sticky = |adj_op_tmp[17:0];
        19: sticky = |adj_op_tmp[18:0];
        19: sticky = |adj_op_tmp[18:0];
        20: sticky = |adj_op_tmp[19:0];
        20: sticky = |adj_op_tmp[19:0];
        21: sticky = |adj_op_tmp[20:0];
        21: sticky = |adj_op_tmp[20:0];
        22: sticky = |adj_op_tmp[21:0];
        22: sticky = |adj_op_tmp[21:0];
        23: sticky = |adj_op_tmp[22:0];
        23: sticky = |adj_op_tmp[22:0];
        24: sticky = |adj_op_tmp[23:0];
        24: sticky = |adj_op_tmp[23:0];
        25: sticky = |adj_op_tmp[24:0];
        25: sticky = |adj_op_tmp[24:0];
        26: sticky = |adj_op_tmp[25:0];
        26: sticky = |adj_op_tmp[25:0];
        27: sticky = |adj_op_tmp[26:0];
        27: sticky = |adj_op_tmp[26:0];
   endcase
   endcase
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Select operands for add/sub (recover hidden bit)
// Select operands for add/sub (recover hidden bit)
 
 
assign fracta_n = expa_lt_expb ? {~expa_dn, fracta, 3'b0} : adj_op_out;
assign fracta_n = expa_lt_expb ? {~expa_dn, fracta, 3'b0} : adj_op_out;
assign fractb_n = expa_lt_expb ? adj_op_out : {~expb_dn, fractb, 3'b0};
assign fractb_n = expa_lt_expb ? adj_op_out : {~expb_dn, fractb, 3'b0};
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Sort operands (for sub only)
// Sort operands (for sub only)
 
 
assign fractb_lt_fracta = fractb_n > fracta_n;  // fractb is larger than fracta
assign fractb_lt_fracta = fractb_n > fracta_n;  // fractb is larger than fracta
assign fracta_s = fractb_lt_fracta ? fractb_n : fracta_n;
assign fracta_s = fractb_lt_fracta ? fractb_n : fracta_n;
assign fractb_s = fractb_lt_fracta ? fracta_n : fractb_n;
assign fractb_s = fractb_lt_fracta ? fracta_n : fractb_n;
 
 
always @(posedge clk)
always @(posedge clk)
        fracta_out <= #1 fracta_s;
        fracta_out <= #1 fracta_s;
 
 
always @(posedge clk)
always @(posedge clk)
        fractb_out <= #1 fractb_s;
        fractb_out <= #1 fractb_s;
 
 
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Determine sign for the output
// Determine sign for the output
 
 
// sign: 0=Positive Number; 1=Negative Number
// sign: 0=Positive Number; 1=Negative Number
always @(signa or signb or add or fractb_lt_fracta)
always @(signa or signb or add or fractb_lt_fracta)
   case({signa, signb, add})            // synopsys full_case parallel_case
   case({signa, signb, add})            // synopsys full_case parallel_case
 
 
        // Add
        // Add
        3'b0_0_1: sign_d = 0;
        3'b0_0_1: sign_d = 0;
        3'b0_1_1: sign_d = fractb_lt_fracta;
        3'b0_1_1: sign_d = fractb_lt_fracta;
        3'b1_0_1: sign_d = !fractb_lt_fracta;
        3'b1_0_1: sign_d = !fractb_lt_fracta;
        3'b1_1_1: sign_d = 1;
        3'b1_1_1: sign_d = 1;
 
 
        // Sub
        // Sub
        3'b0_0_0: sign_d = fractb_lt_fracta;
        3'b0_0_0: sign_d = fractb_lt_fracta;
        3'b0_1_0: sign_d = 0;
        3'b0_1_0: sign_d = 0;
        3'b1_0_0: sign_d = 1;
        3'b1_0_0: sign_d = 1;
        3'b1_1_0: sign_d = !fractb_lt_fracta;
        3'b1_1_0: sign_d = !fractb_lt_fracta;
   endcase
   endcase
 
 
always @(posedge clk)
always @(posedge clk)
        sign <= #1 sign_d;
        sign <= #1 sign_d;
 
 
// Fix sign for ZERO result
// Fix sign for ZERO result
always @(posedge clk)
always @(posedge clk)
        signa_r <= #1 signa;
        signa_r <= #1 signa;
 
 
always @(posedge clk)
always @(posedge clk)
        signb_r <= #1 signb;
        signb_r <= #1 signb;
 
 
always @(posedge clk)
always @(posedge clk)
        add_r <= #1 add;
        add_r <= #1 add;
 
 
always @(posedge clk)
always @(posedge clk)
        result_zero_sign <= #1  ( add_r &  signa_r &  signb_r) |
        result_zero_sign <= #1  ( add_r &  signa_r &  signb_r) |
                                (!add_r &  signa_r & !signb_r) |
                                (!add_r &  signa_r & !signb_r) |
                                ( add_r & (signa_r |  signb_r) & (rmode==3)) |
                                ( add_r & (signa_r |  signb_r) & (rmode==3)) |
                                (!add_r & (signa_r == signb_r) & (rmode==3));
                                (!add_r & (signa_r == signb_r) & (rmode==3));
 
 
// Fix sign for NAN result
// Fix sign for NAN result
always @(posedge clk)
always @(posedge clk)
        fracta_lt_fractb <= #1 fracta < fractb;
        fracta_lt_fractb <= #1 fracta < fractb;
 
 
always @(posedge clk)
always @(posedge clk)
        fracta_eq_fractb <= #1 fracta == fractb;
        fracta_eq_fractb <= #1 fracta == fractb;
 
 
assign nan_sign1 = fracta_eq_fractb ? (signa_r & signb_r) : fracta_lt_fractb ? signb_r : signa_r;
assign nan_sign1 = fracta_eq_fractb ? (signa_r & signb_r) : fracta_lt_fractb ? signb_r : signa_r;
 
 
always @(posedge clk)
always @(posedge clk)
        nan_sign <= #1 (opa_nan & opb_nan) ? nan_sign1 : opb_nan ? signb_r : signa_r;
        nan_sign <= #1 (opa_nan & opb_nan) ? nan_sign1 : opb_nan ? signb_r : signa_r;
 
 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
//
// Decode Add/Sub operation
// Decode Add/Sub operation
//
//
 
 
// add: 1=Add; 0=Subtract
// add: 1=Add; 0=Subtract
always @(signa or signb or add)
always @(signa or signb or add)
   case({signa, signb, add})            // synopsys full_case parallel_case
   case({signa, signb, add})            // synopsys full_case parallel_case
 
 
        // Add
        // Add
        3'b0_0_1: add_d = 1;
        3'b0_0_1: add_d = 1;
        3'b0_1_1: add_d = 0;
        3'b0_1_1: add_d = 0;
        3'b1_0_1: add_d = 0;
        3'b1_0_1: add_d = 0;
        3'b1_1_1: add_d = 1;
        3'b1_1_1: add_d = 1;
 
 
        // Sub
        // Sub
        3'b0_0_0: add_d = 0;
        3'b0_0_0: add_d = 0;
        3'b0_1_0: add_d = 1;
        3'b0_1_0: add_d = 1;
        3'b1_0_0: add_d = 1;
        3'b1_0_0: add_d = 1;
        3'b1_1_0: add_d = 0;
        3'b1_1_0: add_d = 0;
   endcase
   endcase
 
 
always @(posedge clk)
always @(posedge clk)
        fasu_op <= #1 add_d;
        fasu_op <= #1 add_d;
 
 
endmodule
endmodule
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.