Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module FPCVT(D, S, E, F);
- input wire [11:0] D;
- output reg S;
- output reg [2:0] E;
- output reg [3:0] F;
- reg out_of_range;
- reg round_bit;
- reg [11:0] D2;
- integer i;
- integer zero_count;
- always @* begin
- D2 = D;
- S = D2 >> 11; // Get the sign bit of the input number D
- D2 = S == 1 ? ~D2 + 1 : D2; // If negative, invert the bits and add 1 (get the magnitude)
- zero_count = 0; // count how many leading zeros
- for(i=11; i>=0 && ~D2[i]; i=i-1) begin
- zero_count=zero_count+1;
- end
- E = zero_count >= 8 ? 0 : 8 - zero_count; // calculate the exponent
- if (E != 0) begin
- for (i = 0; i < 4; i = i + 1) begin // calculate the significand
- F[i] = D2[8-zero_count+i];
- end
- end else begin
- for (i = 0; i < 4; i = i + 1) begin
- F[i] = D2[i]; // least significant 4 bits
- end
- end
- out_of_range = 0;
- round_bit = D2[7-zero_count]; // what happens if no 5th bit?
- if (round_bit) begin
- if (F == 4'b1111) begin // need to increase exponent
- if (E == 3'b111) begin
- out_of_range = 1;
- end else begin
- E = E+1;
- end
- F = 4'b1000; // 1111 + 1 = 10000 then shift to 1000
- end else begin
- F = F+1;
- end
- end
- // note: our range should be 1_111_1111 = -1920 to 0_111_1111 = 1920
- // i guess we could hard code those limits . . .
- if (out_of_range || D == 100000000000) begin
- F = 15;
- E = 7;
- end
- end
- endmodule
- module FPCVT_testbench();
- wire S;
- wire [2:0] E;
- wire [3:0] F;
- wire [11:0] D;
- assign D = 12'b000000101100;
- FPCVT myFPCVT(D, S, E, F);
- always @* begin
- $display("%b",D);
- $display("%b",S);
- $display("%b",E);
- $display("%b",F);
- end
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement