use $bitstoshortreal: ... $shortrealtobits converts values from a shortreal type to the 32-bit vector representation of the real number. $bitstoshortreal converts a bit pattern created by $shortrealtobits to a value of the shortreal type. ...
display a real in verilog but bitstoreal returning only 0.000000
题
I am trying to display a real number during the simulation of my verilog code in modelsim. But I only get 0 as output. I am trying to use the bitstoreal system function. I'm not so good at verilog so it could be a stupid beginner's mistake.
Following is my code:
reg [31:0] y[1:0];
integer file;
localparam [31:0] test = 32'h3fb0d05d;
task read_data_from_fifo();
begin
file = $fopen("/tmp/data.fifo", "r");
$fread(y, file);
$display("y0 = %d, %f, %h", $bitstoreal(y[0]), $bitstoreal(test), $bitstoreal(y[0]));
$display("y1 = %f, %f, %h", y[1], $bitstoreal(32'h5dd0_b03f), y[1]);
end
endtask
(the task is called from an initial begin block) Outputs:
# y0 = 0, 0.000000, 00000000
# y1 = 3742779199.000000, 0.000000, df16473f
All help appreciated.
Update
Looks like bitstoreal only supports double precision floats (64-bit). Because
localparam [63:0] test = 64'h_3FF61A0BE5109071;
$display("%f", $bitstoreal(test));
results in
1.381359
解决方案
其他提示
$bitstoreal takes a 64bit input double precision float.
Solution: Do a bit conversion from single to double precision float. Like this:
reg [31:0] z; // single precision float
reg [63:0] double; // double precision float
double = {z[31], z[30], {3{~z[30]}}, z[29:23], z[22:0], {29{1'b0}}};
$display("%f", $bitstoreal(double));
Disclaimer: I'm not sure if this single to double conversion is safe/correct.
Update
This is to be read in the IEEE document mentioned by toolic:
$realtobits converts values from a real type to a 64-bit vector representation of the real number. $bitstoreal converts a bit pattern created by $realtobits to a value of the real type.