|
| 1 | +`timescale 1ns / 1ps |
| 2 | +module add_N32_ES6_PIPE(clk, in1, in2, start, out, inf, zero, done); |
| 3 | + |
| 4 | +function [31:0] log2; |
| 5 | +input reg [31:0] value; |
| 6 | + begin |
| 7 | + value = value-1; |
| 8 | + for (log2=0; value>0; log2=log2+1) |
| 9 | + value = value>>1; |
| 10 | + end |
| 11 | +endfunction |
| 12 | + |
| 13 | +parameter N = 32; |
| 14 | +parameter Bs = log2(N); |
| 15 | +parameter es = 6; |
| 16 | + |
| 17 | + |
| 18 | +input clk; |
| 19 | +input [N-1:0] in1, in2; |
| 20 | +input start; |
| 21 | +output reg [N-1:0] out; |
| 22 | +output reg inf, zero; |
| 23 | +output reg done; |
| 24 | + |
| 25 | +wire start0= start; |
| 26 | +wire s1 = in1[N-1]; |
| 27 | +wire s2 = in2[N-1]; |
| 28 | +wire zero_tmp1 = |in1[N-2:0]; |
| 29 | +wire zero_tmp2 = |in2[N-2:0]; |
| 30 | +wire inf1 = in1[N-1] & (~zero_tmp1), |
| 31 | + inf2 = in2[N-1] & (~zero_tmp2); |
| 32 | +wire zero1 = ~(in1[N-1] | zero_tmp1), |
| 33 | + zero2 = ~(in2[N-1] | zero_tmp2); |
| 34 | +wire inf_t = inf1 | inf2, |
| 35 | + zero_t = zero1 & zero2; |
| 36 | + |
| 37 | + |
| 38 | +//Data Extraction |
| 39 | +wire rc1, rc2; |
| 40 | +wire [Bs-1:0] regime1, regime2; |
| 41 | +wire [es-1:0] e1, e2; |
| 42 | +wire [N-es-1:0] mant1, mant2; |
| 43 | +wire [N-1:0] xin1 = s1 ? -in1 : in1; |
| 44 | +wire [N-1:0] xin2 = s2 ? -in2 : in2; |
| 45 | +wire in1_gt_in2 = (xin1[N-2:0] >= xin2[N-2:0]) ? 1'b1 : 1'b0; |
| 46 | +data_extract_N32_ES6 #(.N(N),.es(es)) uut_de1(.in(xin1), .rc(rc1), .regime(regime1), .exp(e1), .mant(mant1)); |
| 47 | +data_extract_N32_ES6 #(.N(N),.es(es)) uut_de2(.in(xin2), .rc(rc2), .regime(regime2), .exp(e2), .mant(mant2)); |
| 48 | + |
| 49 | +wire [N-es:0] m1 = {zero_tmp1,mant1}, |
| 50 | + m2 = {zero_tmp2,mant2}; |
| 51 | + |
| 52 | +reg start_r0; |
| 53 | +reg in1_gt_in2_r0, s1_r0, s2_r0, inf_r0, zero_r0, rc1_r0, rc2_r0; |
| 54 | +reg [Bs-1:0] regime1_r0, regime2_r0; |
| 55 | +reg [es-1:0] e1_r0, e2_r0; |
| 56 | +reg [N-es:0] m1_r0, m2_r0; |
| 57 | + |
| 58 | +always @(posedge clk) begin |
| 59 | +start_r0 <= start0; in1_gt_in2_r0 <= in1_gt_in2; s1_r0 <= s1; s2_r0 <= s2; |
| 60 | +inf_r0 <= inf_t; zero_r0 <= zero_t; rc1_r0 <= rc1; rc2_r0 <= rc2; |
| 61 | +regime1_r0 <= regime1; regime2_r0 <= regime2; |
| 62 | +e1_r0 <= e1; e2_r0 <= e2; |
| 63 | +m1_r0 <= m1; m2_r0 <= m2; |
| 64 | +end |
| 65 | + |
| 66 | +//Large Checking and Assignment |
| 67 | +wire ls = in1_gt_in2_r0 ? s1_r0 : s2_r0; |
| 68 | +wire op = s1_r0 ~^ s2_r0; |
| 69 | + |
| 70 | +wire lrc = in1_gt_in2_r0 ? rc1_r0 : rc2_r0; |
| 71 | +wire src = in1_gt_in2_r0 ? rc2_r0 : rc1_r0; |
| 72 | + |
| 73 | +wire [Bs-1:0] lr = in1_gt_in2_r0 ? regime1_r0 : regime2_r0; |
| 74 | +wire [Bs-1:0] sr = in1_gt_in2_r0 ? regime2_r0 : regime1_r0; |
| 75 | + |
| 76 | +wire [es-1:0] le = in1_gt_in2_r0 ? e1_r0 : e2_r0; |
| 77 | +wire [es-1:0] se = in1_gt_in2_r0 ? e2_r0 : e1_r0; |
| 78 | + |
| 79 | +wire [N-es:0] lm = in1_gt_in2_r0 ? m1_r0 : m2_r0; |
| 80 | +wire [N-es:0] sm = in1_gt_in2_r0 ? m2_r0 : m1_r0; |
| 81 | + |
| 82 | +//Exponent Difference: Lower Mantissa Right Shift Amount |
| 83 | +wire [es+Bs+1:0] diff; |
| 84 | +assign diff = {lrc ? {2'b0,lr} : -lr,le} - {src ? {2'b0,sr} : -sr, se}; |
| 85 | +wire [Bs-1:0] exp_diff = (|diff[es+Bs:Bs]) ? {Bs{1'b1}} : diff[Bs-1:0]; |
| 86 | + |
| 87 | +//DSR Right Shifting |
| 88 | +wire [N-1:0] DSR_right_in = {sm,5'b0}; |
| 89 | +wire [N-1:0] DSR_right_out; |
| 90 | +wire [Bs-1:0] DSR_e_diff = exp_diff; |
| 91 | +DSR_right_N_S #(.N(N), .S(Bs)) dsr1(.a(DSR_right_in), .b(DSR_e_diff), .c(DSR_right_out)); |
| 92 | + |
| 93 | +reg start_r1; |
| 94 | +reg ls_r1, op_r1, lrc_r1, inf_r1, zero_r1; |
| 95 | +reg [Bs-1:0] lr_r1; |
| 96 | +reg [es-1:0] le_r1; |
| 97 | +reg [N-es:0] lm_r1; |
| 98 | +reg [N-1:0] DSR_right_out_r1; |
| 99 | +always @(posedge clk) begin |
| 100 | +start_r1 <= start_r0; ls_r1 <= ls; op_r1 <= op; inf_r1 <= inf_r0; zero_r1 <= zero_r0; |
| 101 | +lrc_r1 <= lrc; |
| 102 | +le_r1 <= le; |
| 103 | +lr_r1 <= lr; |
| 104 | +lm_r1 <= lm; |
| 105 | +DSR_right_out_r1 <= DSR_right_out; |
| 106 | +end |
| 107 | + |
| 108 | +//Mantissa Addition |
| 109 | +wire [N-1:0] add_m_in1 = {lm_r1,5'b0}; |
| 110 | +wire [N:0] add_m; |
| 111 | +add_sub_N #(.N(N)) uut_add_sub_N (op_r1, add_m_in1, DSR_right_out_r1, add_m); |
| 112 | +wire [1:0] mant_ovf = add_m[N:N-1]; |
| 113 | + |
| 114 | +//LOD |
| 115 | +wire [N-1:0] LOD_in = {(add_m[N] | add_m[N-1]), add_m[N-2:0]}; |
| 116 | +wire [Bs-1:0] left_shift; |
| 117 | +LOD_32 l2(.in(LOD_in), .out(left_shift)); |
| 118 | + |
| 119 | + |
| 120 | +reg start_r2; |
| 121 | +reg ls_r2, lrc_r2, inf_r2, zero_r2; |
| 122 | +reg [Bs-1:0] lr_r2, left_shift_r2; |
| 123 | +reg [es-1:0] le_r2; |
| 124 | +reg [N:0] add_m_r2; |
| 125 | +reg [1:0] mant_ovf_r2; |
| 126 | +always @(posedge clk) begin |
| 127 | +start_r2 <= start_r1; ls_r2 <= ls_r1; inf_r2 <= inf_r1; zero_r2 <= zero_r1; |
| 128 | +lrc_r2 <= lrc_r1; lr_r2 <= lr_r1; le_r2 <= le_r1; |
| 129 | +left_shift_r2 <= left_shift; |
| 130 | +add_m_r2 <= add_m; |
| 131 | +mant_ovf_r2 <= mant_ovf; |
| 132 | +end |
| 133 | + |
| 134 | +//DSR Left Shifting |
| 135 | +wire [N-1:0] DSR_left_out_t; |
| 136 | +DSR_left_N_S #(.N(N), .S(Bs)) dsl1(.a(add_m_r2[N:1]), .b(left_shift_r2), .c(DSR_left_out_t)); |
| 137 | +wire [N-1:0] DSR_left_out = DSR_left_out_t[N-1] ? DSR_left_out_t[N-1:0] : {DSR_left_out_t[N-2:0],1'b0}; |
| 138 | + |
| 139 | + |
| 140 | +//Exponent and Regime Computation |
| 141 | +wire [Bs:0] lr_N = lrc_r2 ? {1'b0,lr_r2} : -{1'b0,lr_r2}; |
| 142 | +wire [es+Bs+1:0] le_o_tmp, le_o; |
| 143 | +sub_N #(.N(es+Bs+1)) sub3 ({lr_N,le_r2}, {{es+1{1'b0}},left_shift_r2}, le_o_tmp); |
| 144 | +add_mantovf #(es+Bs+1) uut_add_mantovf (le_o_tmp, mant_ovf_r2[1], le_o); |
| 145 | + |
| 146 | +wire [es+Bs:0] le_oN = le_o[es+Bs] ? -le_o : le_o; |
| 147 | +wire [es-1:0] e_o = le_o[es-1:0]; |
| 148 | +wire [Bs-1:0] r_o = (~le_o[es+Bs] || (|le_o[es-1:0])) + le_oN[es+Bs-1:es]; |
| 149 | + |
| 150 | +//Exponent and Mantissa Packing |
| 151 | +wire [2*N-1+3:0] tmp_o; |
| 152 | +assign tmp_o = { {N{~le_o[es+Bs]}}, le_o[es+Bs], e_o, DSR_left_out[N-2:4], |DSR_left_out[3:0]}; |
| 153 | + |
| 154 | +reg start_r3, ls_r3, inf_r3, zero_r3; |
| 155 | +reg [2*N-1+3:0] tmp_o_r3; |
| 156 | +reg [Bs-1:0] r_o_r3; |
| 157 | +reg [N-1:0] DSR_left_out_r3; |
| 158 | +always @(posedge clk) begin |
| 159 | +start_r3 <= start_r2; ls_r3 <= ls_r2; inf_r3 <= inf_r2; zero_r3 <= zero_r2; |
| 160 | +r_o_r3 <= r_o; |
| 161 | +tmp_o_r3 <= tmp_o; |
| 162 | +DSR_left_out_r3 <= DSR_left_out; |
| 163 | +end |
| 164 | + |
| 165 | +//Including/Pushing Regime bits in Exponent-Mantissa Packing |
| 166 | +wire [3*N-1+3:0] tmp1_o; |
| 167 | +DSR_right_N_S #(.N(3*N+3), .S(Bs)) dsr2 (.a({tmp_o_r3,{N{1'b0}}}), .b(r_o_r3), .c(tmp1_o)); |
| 168 | + |
| 169 | + |
| 170 | +//Rounding RNE : ulp_add = G.(R + S) + L.G.(~(R+S)) |
| 171 | +wire L = tmp1_o[N+4], G = tmp1_o[N+3], R = tmp1_o[N+2], St = |tmp1_o[N+1:0], |
| 172 | + ulp = ((G & (R | St)) | (L & G & ~(R | St))); |
| 173 | +wire [N-1:0] rnd_ulp = {{N-1{1'b0}},ulp}; |
| 174 | +wire [N-1:0] tmp1_o_rnd = (r_o_r3 < N-es-2) ? tmp1_o[2*N-1+3:N+3] + rnd_ulp : tmp1_o[2*N-1+3:N+3]; |
| 175 | + |
| 176 | + |
| 177 | +//Final Output |
| 178 | +wire [N-1:0] tmp1_oN = ls_r3 ? -tmp1_o_rnd : tmp1_o_rnd; |
| 179 | +wire [N-1:0] out_tmp = inf_r3|zero_r3|(~DSR_left_out_r3[N-1]) ? {inf_r3,{N-1{1'b0}}} : {ls_r3, tmp1_oN[N-1:1]}; |
| 180 | + |
| 181 | +always @(posedge clk) begin |
| 182 | +out <= out_tmp; |
| 183 | +done <= start_r3; |
| 184 | +inf <= inf_r3; |
| 185 | +zero <= zero_r3; |
| 186 | +end |
| 187 | +endmodule |
| 188 | + |
| 189 | +///////////////////////// |
| 190 | +module data_extract_N32_ES6(in, rc, regime, exp, mant); |
| 191 | + |
| 192 | +function [31:0] log2; |
| 193 | +input reg [31:0] value; |
| 194 | + begin |
| 195 | + value = value-1; |
| 196 | + for (log2=0; value>0; log2=log2+1) |
| 197 | + value = value>>1; |
| 198 | + end |
| 199 | +endfunction |
| 200 | + |
| 201 | + |
| 202 | +parameter N=32; |
| 203 | +parameter Bs=log2(N); |
| 204 | +parameter es = 5; |
| 205 | + |
| 206 | + |
| 207 | +input [N-1:0] in; |
| 208 | +output rc; |
| 209 | +output [Bs-1:0] regime; |
| 210 | +output [es-1:0] exp; |
| 211 | +output [N-es-1:0] mant; |
| 212 | + |
| 213 | +//Data Extraction |
| 214 | + |
| 215 | +wire [N-1:0] xin = in; |
| 216 | +assign rc = xin[N-2]; |
| 217 | + |
| 218 | +wire [N-1:0] xin_r = rc ? ~xin : xin; |
| 219 | + |
| 220 | +wire [Bs-1:0] k; |
| 221 | +LOD_32 uut_lod32_5 (.in({xin_r[N-2:0],rc^1'b0}), .out(k)); |
| 222 | + |
| 223 | +assign regime = rc ? k-1 : k; |
| 224 | + |
| 225 | +wire [N-1:0] xin_tmp; |
| 226 | +DSR_left_N_S #(.N(N), .S(Bs)) ls (.a({xin[N-3:0],2'b0}),.b(k),.c(xin_tmp)); |
| 227 | + |
| 228 | +assign exp= xin_tmp[N-1:N-es]; |
| 229 | +assign mant= xin_tmp[N-es-1:0]; |
| 230 | + |
| 231 | +endmodule |
| 232 | + |
| 233 | +///////////////// |
| 234 | +module sub_N (a,b,c); |
| 235 | +parameter N=10; |
| 236 | +input [N-1:0] a,b; |
| 237 | +output [N:0] c; |
| 238 | +assign c = {1'b0,a} - {1'b0,b}; |
| 239 | +endmodule |
| 240 | + |
| 241 | +///////////////////////// |
| 242 | +module add_N (a,b,c); |
| 243 | +parameter N=10; |
| 244 | +input [N-1:0] a,b; |
| 245 | +output [N:0] c; |
| 246 | +assign c = {1'b0,a} + {1'b0,b}; |
| 247 | +endmodule |
| 248 | + |
| 249 | +///////////////////////// |
| 250 | +module add_sub_N (op,a,b,c); |
| 251 | +parameter N=10; |
| 252 | +input op; |
| 253 | +input [N-1:0] a,b; |
| 254 | +output [N:0] c; |
| 255 | +assign c = op ? {1'b0,a} + {1'b0,b} : {1'b0,a} - {1'b0,b}; |
| 256 | +endmodule |
| 257 | + |
| 258 | +///////////////////////// |
| 259 | +module add_mantovf (a,mant_ovf,c); |
| 260 | +parameter N=10; |
| 261 | +input [N:0] a; |
| 262 | +input mant_ovf; |
| 263 | +output [N:0] c; |
| 264 | +assign c = a + mant_ovf; |
| 265 | +endmodule |
| 266 | + |
| 267 | +///////////////////////// |
| 268 | +module DSR_left_N_S(a,b,c); |
| 269 | + parameter N=16; |
| 270 | + parameter S=4; |
| 271 | + input [N-1:0] a; |
| 272 | + input [S-1:0] b; |
| 273 | + output [N-1:0] c; |
| 274 | + |
| 275 | +wire [N-1:0] tmp [S-1:0]; |
| 276 | +assign tmp[0] = b[0] ? a << 7'd1 : a; |
| 277 | +genvar i; |
| 278 | +generate |
| 279 | + for (i=1; i<S; i=i+1)begin:loop_blk |
| 280 | + assign tmp[i] = b[i] ? tmp[i-1] << 2**i : tmp[i-1]; |
| 281 | + end |
| 282 | +endgenerate |
| 283 | +assign c = tmp[S-1]; |
| 284 | + |
| 285 | +endmodule |
| 286 | + |
| 287 | + |
| 288 | +///////////////////////// |
| 289 | +module DSR_right_N_S(a,b,c); |
| 290 | + parameter N=16; |
| 291 | + parameter S=4; |
| 292 | + input [N-1:0] a; |
| 293 | + input [S-1:0] b; |
| 294 | + output [N-1:0] c; |
| 295 | + |
| 296 | +wire [N-1:0] tmp [S-1:0]; |
| 297 | +assign tmp[0] = b[0] ? a >> 7'd1 : a; |
| 298 | +genvar i; |
| 299 | +generate |
| 300 | + for (i=1; i<S; i=i+1)begin:loop_blk |
| 301 | + assign tmp[i] = b[i] ? tmp[i-1] >> 2**i : tmp[i-1]; |
| 302 | + end |
| 303 | +endgenerate |
| 304 | +assign c = tmp[S-1]; |
| 305 | + |
| 306 | +endmodule |
| 307 | + |
| 308 | +/////////////////////// |
| 309 | +module LOD_32(in, out); |
| 310 | +input [31:0] in; |
| 311 | +output [4:0] out; |
| 312 | +wire [3:0] out_l, out_h; |
| 313 | +wire out_vl, out_vh; |
| 314 | +LOD16_4 pl(.in(in[15:0]), .out(out_l), .out_v(out_vl)); |
| 315 | +LOD16_4 ph(.in(in[31:16]), .out(out_h), .out_v(out_vh)); |
| 316 | +assign out = out_vh ? {1'b0,out_h} : {out_vl,out_l}; |
| 317 | +endmodule |
| 318 | + |
| 319 | +//////////////////// |
| 320 | +module LOD16_4(in, out, out_v); |
| 321 | +input [15:0] in; |
| 322 | +output [3:0] out; |
| 323 | +output out_v; |
| 324 | +wire [2:0] out_l, out_h; |
| 325 | +wire out_vl, out_vh; |
| 326 | +LOD8_3 pl(.in(in[7:0]), .out(out_l), .out_v(out_vl)); |
| 327 | +LOD8_3 ph(.in(in[15:8]), .out(out_h), .out_v(out_vh)); |
| 328 | +assign out = out_vh ? {1'b0,out_h} : {out_vl,out_l}; |
| 329 | +assign out_v = out_vl | out_vh; |
| 330 | +endmodule |
| 331 | + |
| 332 | + |
| 333 | +//////////////////// |
| 334 | +module LOD8_3(in, out, out_v); |
| 335 | +input [7:0] in; |
| 336 | +output [2:0] out; |
| 337 | +output out_v; |
| 338 | + |
| 339 | +wire [1:0] out_l, out_h; |
| 340 | +wire out_vl, out_vh; |
| 341 | +LOD4_2 pl(.in(in[3:0]), .out(out_l), .out_v(out_vl)); |
| 342 | +LOD4_2 ph(.in(in[7:4]), .out(out_h), .out_v(out_vh)); |
| 343 | +assign out = out_vh ? {1'b0,out_h} : {out_vl,out_l}; |
| 344 | +assign out_v = out_vl | out_vh; |
| 345 | + |
| 346 | +endmodule |
| 347 | + |
| 348 | + |
| 349 | +//////////////////// |
| 350 | +module LOD4_2(in, out, out_v); |
| 351 | +input [3:0] in; |
| 352 | +output [1:0] out; |
| 353 | +output out_v; |
| 354 | + |
| 355 | +wire out_l, out_h; |
| 356 | +wire out_vl, out_vh; |
| 357 | +LOD2_1 pl(.in(in[1:0]), .out(out_l), .out_v(out_vl)); |
| 358 | +LOD2_1 ph(.in(in[3:2]), .out(out_h), .out_v(out_vh)); |
| 359 | +assign out = out_vh ? {1'b0,out_h} : {out_vl,out_l}; |
| 360 | +assign out_v = out_vl | out_vh; |
| 361 | + |
| 362 | +endmodule |
| 363 | + |
| 364 | + |
| 365 | +module LOD2_1(in, out, out_v); |
| 366 | +input [1:0] in; |
| 367 | +output out; |
| 368 | +output out_v; |
| 369 | + |
| 370 | +assign out = ~in[1] & in[0]; |
| 371 | +assign out_v = |in; |
| 372 | +endmodule |
0 commit comments