Advertisement
Guest User

MultP.sv

a guest
Jun 1st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date: 10.05.2019 22:03:23
  7. // Design Name:
  8. // Module Name: Mp
  9. // Project Name:
  10. // Target Devices:
  11. // Tool Versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////
  21.  
  22.  
  23. module Mp(
  24.     input [15:0] a,
  25.     input [15:0] b,
  26.     input clk,
  27.     input reset,
  28.     output logic [15:0] result
  29.     );
  30.    
  31.     logic sign1, sign2;
  32.     logic [9:0] mant1, mant2;
  33.     logic [4:0] exp1, exp2;
  34.    
  35.     assign sign1 = a[15];
  36.     assign sign2 = b[15];
  37.     assign mant1 = a[9:0];
  38.     assign mant2 = b[9:0];
  39.     assign exp1 = a[14:10];
  40.     assign exp2 = b[14:10];
  41.    
  42.     logic isNan1, isZero1, isInf1;
  43.     logic isNan2, isZero2, isInf2;
  44.    
  45.     assign isNan1 = (exp1==5'b11111) & (mant1!=10'b0);
  46.     assign isNan2 = (exp2==5'b11111) & (mant2!=10'b0);
  47.     assign isZero1 = (exp1==5'b00000) & (mant1==10'b0);
  48.     assign isZero2 = (exp2==5'b00000) & (mant2==10'b0);
  49.     assign isInf1 = (exp1==5'b11111) & (mant1==10'b0);
  50.     assign isInf2 = (exp2==5'b11111) & (mant2==10'b0);
  51.  
  52.     logic[5:0] tmpExpSum;
  53.     logic[5:0] convExpSum;
  54.     logic[5:0] convExpSum2;
  55.     assign tmpExpSum = exp1 + exp2;
  56.     assign convExpSum = tmpExpSum[4:0] - 5'd15;
  57.     assign convExpSum2 = tmpExpSum[4:0] - 5'd14;
  58.    
  59.     logic[21:0] multRes;
  60.    
  61.     //multInt Lul (.a({1'b1, mant1}), .b({1'b1, mant2}), .c(multRes), .clock(clk), .reset(reset));
  62.     assign multRes = {1'b1, mant1}*{1'b1, mant2};
  63.    
  64.     always @(posedge clk) begin
  65.         if (reset) begin
  66.             result <= 16'b0111110000000001; //result - NaN
  67.         end else
  68.         if (isNan1 | isNan2)
  69.              result <= 16'b0111110000000001; //result - NaN
  70.         else
  71.             if ((isInf1 & isZero2) | (isZero1 & isInf2))
  72.                 result <= 16'b0111110000000001; //result - NaN
  73.         else
  74.             if ((isInf1 & !isZero2) | (isInf2 & !isZero1))
  75.                 result <= 16'b0111110000000000; //result - Inf
  76.         else
  77.             if ((isZero1 & !isInf2) | (!isInf1 & isZero2))
  78.                 result <= 16'b0000000000000000; //result - Zero
  79.         else begin
  80.             if (multRes[21]==1'b0) begin
  81.                 if (tmpExpSum <= 6'd15)
  82.                     result <= 16'b0000000000000000; //result - Zero
  83.                 else if (tmpExpSum >= 6'd46)
  84.                     result <= 16'b0111110000000000; //result - Inf            
  85.                 else begin
  86.                     result <= {1'b0, convExpSum, multRes[19:10]};
  87.                 end
  88.             end else begin
  89.                 if (tmpExpSum <= 6'd14)
  90.                     result <= 16'b0000000000000000; //result - Zero              
  91.                 else if (tmpExpSum >= 6'd45)
  92.                     result <= 16'b0111110000000000; //result - Inf                
  93.                 else begin
  94.                     result <= {1'b0, convExpSum2, multRes[20:11]};
  95.                 end
  96.             end          
  97.         end
  98.     end
  99.        
  100.     always @(posedge clk) begin
  101.         if ((sign1 & !sign2) | (!sign1 & sign2))
  102.                 result[15] <= 1;
  103.         else
  104.             result[15] <= 0;
  105.         end
  106.        
  107. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement