Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module FPCVT(D, S, E, F);
  2.     input wire [11:0] D;
  3.    
  4.     output reg S;
  5.     output reg [2:0] E;
  6.     output reg [3:0] F;
  7.    
  8.     reg out_of_range;
  9.     reg round_bit;
  10.     reg [11:0] D2;
  11.     integer i;
  12.     integer zero_count;
  13.    
  14.     always @* begin
  15.         D2 = D;
  16.      
  17.         S = D2 >> 11; // Get the sign bit of the input number D
  18.         D2 = S == 1 ? ~D2 + 1 : D2; // If negative, invert the bits and add 1 (get the magnitude)
  19.        
  20.         zero_count = 0; // count how many leading zeros
  21.         for(i=11; i>=0 && ~D2[i]; i=i-1) begin
  22.             zero_count=zero_count+1;
  23.         end
  24.        
  25.         E = zero_count >= 8 ? 0 : 8 - zero_count; // calculate the exponent
  26.        
  27.         if (E != 0) begin
  28.             for (i = 0; i < 4; i = i + 1) begin // calculate the significand
  29.                 F[i] = D2[8-zero_count+i];
  30.             end
  31.         end else begin
  32.             for (i = 0; i < 4; i = i + 1) begin
  33.                 F[i] = D2[i]; // least significant 4 bits
  34.             end
  35.         end
  36.        
  37.         out_of_range = 0;
  38.         round_bit = D2[7-zero_count]; // what happens if no 5th bit?
  39.         if (round_bit) begin
  40.             if (F == 4'b1111) begin // need to increase exponent
  41.                 if (E == 3'b111) begin
  42.                     out_of_range = 1;
  43.                 end else begin
  44.                     E = E+1;
  45.                 end
  46.                 F = 4'b1000; // 1111 + 1 = 10000 then shift to 1000
  47.             end else begin
  48.                 F = F+1;
  49.             end
  50.         end
  51.        
  52.         // note: our range should be 1_111_1111 = -1920 to 0_111_1111 = 1920
  53.         // i guess we could hard code those limits . . .
  54.         if (out_of_range || D == 100000000000) begin
  55.             F = 15;
  56.             E = 7;
  57.         end
  58.     end
  59. endmodule
  60.  
  61. module FPCVT_testbench();
  62.     wire S;
  63.     wire [2:0] E;
  64.     wire [3:0] F;
  65.     wire [11:0] D;
  66.     assign D = 12'b000000101100;
  67.    
  68.     FPCVT myFPCVT(D, S, E, F);
  69.    
  70.     always @* begin
  71.       $display("%b",D);
  72.       $display("%b",S);
  73.       $display("%b",E);
  74.       $display("%b",F);
  75.     end
  76. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement