| 1 |
2 |
specular |
//=======================================================================
|
| 2 |
|
|
// Project Monophony
|
| 3 |
|
|
// Wire-Frame 3D Graphics Accelerator IP Core
|
| 4 |
|
|
//
|
| 5 |
|
|
// File:
|
| 6 |
|
|
// fm_3d_norm.v
|
| 7 |
|
|
//
|
| 8 |
|
|
// Abstract:
|
| 9 |
|
|
// 22-bit floating point normalize function
|
| 10 |
|
|
//
|
| 11 |
|
|
// Author:
|
| 12 |
4 |
specular |
// Kenji Ishimaru (info.wf3d@gmail.com)
|
| 13 |
2 |
specular |
//
|
| 14 |
|
|
//======================================================================
|
| 15 |
|
|
//
|
| 16 |
|
|
// Copyright (c) 2015, Kenji Ishimaru
|
| 17 |
|
|
// All rights reserved.
|
| 18 |
|
|
//
|
| 19 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 20 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 21 |
|
|
//
|
| 22 |
|
|
// -Redistributions of source code must retain the above copyright notice,
|
| 23 |
|
|
// this list of conditions and the following disclaimer.
|
| 24 |
|
|
// -Redistributions in binary form must reproduce the above copyright notice,
|
| 25 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 26 |
|
|
// and/or other materials provided with the distribution.
|
| 27 |
|
|
//
|
| 28 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 29 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
| 30 |
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
| 31 |
|
|
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
| 32 |
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| 33 |
|
|
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
| 34 |
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
| 35 |
|
|
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
| 36 |
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
| 37 |
|
|
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
| 38 |
|
|
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 39 |
|
|
//
|
| 40 |
|
|
// Revision History
|
| 41 |
|
|
|
| 42 |
|
|
module fm_3d_norm (
|
| 43 |
|
|
i_s,
|
| 44 |
|
|
i_e,
|
| 45 |
|
|
i_f,
|
| 46 |
|
|
o_b
|
| 47 |
|
|
);
|
| 48 |
|
|
|
| 49 |
|
|
///////////////////////////////////////////
|
| 50 |
|
|
// port definition
|
| 51 |
|
|
///////////////////////////////////////////
|
| 52 |
|
|
input i_s; // input s1, e5, f2.15
|
| 53 |
|
|
input [4:0] i_e; // input s1, e5, f2.15
|
| 54 |
|
|
input [16:0] i_f; // input s1, e5, f2.15
|
| 55 |
|
|
output [21:0] o_b; // normalized out
|
| 56 |
|
|
///////////////////////////////////////////
|
| 57 |
|
|
// wire definition
|
| 58 |
|
|
///////////////////////////////////////////
|
| 59 |
|
|
// intermidiate wire
|
| 60 |
|
|
wire w_carry; // fraction carry bit
|
| 61 |
|
|
wire [3:0] w_penc; // result of priority encode
|
| 62 |
|
|
wire w_c_zero; // fraction zero flag
|
| 63 |
|
|
wire w_ce_zero; // exp zero flag
|
| 64 |
|
|
wire [15:0] w_lshifted; // left shifted fraction value
|
| 65 |
|
|
wire [4:0] w_lshift_val; // left shift value
|
| 66 |
|
|
wire [15:0] w_c_fraction;
|
| 67 |
|
|
wire [15:0] w_c_frac;
|
| 68 |
|
|
wire [4:0] w_c_exp; // normalized final exp
|
| 69 |
|
|
wire w_c_sign; // final exp
|
| 70 |
|
|
wire [5:0] w_incdec_out; // result of exp incdec
|
| 71 |
|
|
|
| 72 |
|
|
///////////////////////////////////////////
|
| 73 |
|
|
// assign statement
|
| 74 |
|
|
///////////////////////////////////////////
|
| 75 |
|
|
assign w_carry = i_f[16];
|
| 76 |
|
|
// normalize
|
| 77 |
|
|
|
| 78 |
|
|
// fraction zero flag
|
| 79 |
|
|
assign w_c_zero = (i_f == 17'h0);
|
| 80 |
|
|
|
| 81 |
|
|
// fraction priority encode
|
| 82 |
|
|
assign w_penc = f_prenc(i_f[15:0]);
|
| 83 |
|
|
|
| 84 |
|
|
// if w_incdec_out[5] == 1, under flow
|
| 85 |
|
|
assign w_incdec_out = f_incdec(i_e, w_penc, w_carry);
|
| 86 |
|
|
|
| 87 |
|
|
// left shift value for normalize
|
| 88 |
|
|
assign w_lshift_val = w_penc;
|
| 89 |
|
|
// left shift for nornamize
|
| 90 |
|
|
assign w_lshifted = i_f[15:0] << w_lshift_val;
|
| 91 |
|
|
// decide final fraction
|
| 92 |
|
|
assign w_c_frac = (w_carry) ? i_f[16:1] : w_lshifted;
|
| 93 |
|
|
// decide final exp
|
| 94 |
|
|
assign w_c_exp = (w_c_zero|w_incdec_out[5]) ? 5'h0 : w_incdec_out[4:0];
|
| 95 |
|
|
// exp zero flag
|
| 96 |
|
|
assign w_ce_zero = (w_c_exp == 5'h0);
|
| 97 |
|
|
// decide final sign
|
| 98 |
|
|
assign w_c_sign = i_s & !w_ce_zero;
|
| 99 |
|
|
// decide final fraction
|
| 100 |
|
|
assign w_c_fraction = (w_ce_zero) ? 16'h0 : w_c_frac;
|
| 101 |
|
|
|
| 102 |
|
|
// output port connection
|
| 103 |
|
|
assign o_b = {w_c_sign, w_c_exp, w_c_fraction};
|
| 104 |
|
|
|
| 105 |
|
|
///////////////////////////////////////////
|
| 106 |
|
|
// function statement
|
| 107 |
|
|
///////////////////////////////////////////
|
| 108 |
|
|
function [3:0] f_prenc;
|
| 109 |
|
|
input [15:0] mat;
|
| 110 |
|
|
begin
|
| 111 |
|
|
if (mat[15] == 1'b1) begin
|
| 112 |
|
|
f_prenc = 4'h0;
|
| 113 |
|
|
end else if (mat[14] == 1'b1) begin
|
| 114 |
|
|
f_prenc = 4'h1;
|
| 115 |
|
|
end else if (mat[13] == 1'b1) begin
|
| 116 |
|
|
f_prenc = 4'h2;
|
| 117 |
|
|
end else if (mat[12] == 1'b1) begin
|
| 118 |
|
|
f_prenc = 4'h3;
|
| 119 |
|
|
end else if (mat[11] == 1'b1) begin
|
| 120 |
|
|
f_prenc = 4'h4;
|
| 121 |
|
|
end else if (mat[10] == 1'b1) begin
|
| 122 |
|
|
f_prenc = 4'h5;
|
| 123 |
|
|
end else if (mat[9] == 1'b1) begin
|
| 124 |
|
|
f_prenc = 4'h6;
|
| 125 |
|
|
end else if (mat[8] == 1'b1) begin
|
| 126 |
|
|
f_prenc = 4'h7;
|
| 127 |
|
|
end else if (mat[7] == 1'b1) begin
|
| 128 |
|
|
f_prenc = 4'h8;
|
| 129 |
|
|
end else if (mat[6] == 1'b1) begin
|
| 130 |
|
|
f_prenc = 4'h9;
|
| 131 |
|
|
end else if (mat[5] == 1'b1) begin
|
| 132 |
|
|
f_prenc = 4'ha;
|
| 133 |
|
|
end else if (mat[4] == 1'b1) begin
|
| 134 |
|
|
f_prenc = 4'hb;
|
| 135 |
|
|
end else if (mat[3] == 1'b1) begin
|
| 136 |
|
|
f_prenc = 4'hc;
|
| 137 |
|
|
end else if (mat[2] == 1'b1) begin
|
| 138 |
|
|
f_prenc = 4'hd;
|
| 139 |
|
|
end else if (mat[1] == 1'b1) begin
|
| 140 |
|
|
f_prenc = 4'he;
|
| 141 |
|
|
end else begin
|
| 142 |
|
|
f_prenc = 4'hf;
|
| 143 |
|
|
end
|
| 144 |
|
|
end
|
| 145 |
|
|
endfunction
|
| 146 |
|
|
|
| 147 |
|
|
function [5:0] f_incdec;
|
| 148 |
|
|
input [4:0] a;
|
| 149 |
|
|
input [3:0] b;
|
| 150 |
|
|
input inc;
|
| 151 |
|
|
reg [5:0] r_inc;
|
| 152 |
|
|
reg [5:0] r_dec;
|
| 153 |
|
|
reg r_1f_flag;
|
| 154 |
|
|
begin
|
| 155 |
|
|
r_1f_flag = (a == 5'h1f);
|
| 156 |
|
|
r_inc = a + !r_1f_flag;
|
| 157 |
|
|
r_dec = a - b;
|
| 158 |
|
|
f_incdec = (inc) ? r_inc : r_dec;
|
| 159 |
|
|
/*
|
| 160 |
|
|
if (inc) begin
|
| 161 |
|
|
if (a == 5'h1f) f_incdec = a;
|
| 162 |
|
|
else f_incdec = a + 1'b1;
|
| 163 |
|
|
end else begin
|
| 164 |
|
|
f_incdec = a - b;
|
| 165 |
|
|
end
|
| 166 |
|
|
*/
|
| 167 |
|
|
end
|
| 168 |
|
|
endfunction
|
| 169 |
|
|
|
| 170 |
|
|
endmodule
|
| 171 |
|
|
|